kriszyp / msgpackr

Ultra-fast MessagePack implementation with extension for record and structural cloning / msgpack.org[JavaScript/NodeJS]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

setAsEmptyObject - null Exception

gonenduk-dy opened this issue · comments

Hi @kriszyp ,

I am running the test code attached below on Node.js 18.16 with msgpackr 1.9 using the 2 new flags and get this error:

TypeError: Cannot set properties of null (setting '10')
    at Packr.writeObject.useRecords.variableMapSize.target.<computed> (/Users/.../node_modules/msgpackr/dist/node.cjs:1683:24)

When turning off the 2 flags, it works well.

Thanks,
Gonen.

The test code:

const { Packr } = require('msgpackr');
const msgpackr = new Packr({ useRecords: false, encodeUndefinedAsNil: true, variableMapSize: true, mapAsEmptyObject: true, setAsEmptyObject: true  });

const map = new Map();
map.set('a', 1);
map.set('b', 2);
const set = new Set();
set.add('a');
set.add('b');
const input = { map, set };

const packed = msgpackr.pack(input);
const unpacked = msgpackr.unpack(packed);
console.log(JSON.stringify(unpacked));

@kriszyp
Any update?

Yes, and I don't know why github isn't properly showing it, looks linked to me, but added a test for this here ab6dcc4, and it doesn't seem to reproduce for me. I am doing something wrong?

hey @kriszyp,

I deleted my previous comment and write this one. It is much more simple.

I cloned your repo and wrote a test. Everything works, just like your unit test, as long as I import the lib locally (not using npm install).
When I import the lib in a different project after npm install - it fails.

Note: Doesn't matter if I use commonJS or ES module. Same results.

Any idea why this is happening?

The test code:

// Working from inside your repo:
import * as msgpackr from './node-index.js'

// Not working (from any other project after npm install msgpackr@1.9.0):
import * as msgpackr from 'msgpackr';

const { Packr } = msgpackr;
const packr = new Packr({ useRecords: false, encodeUndefinedAsNil: true, variableMapSize: true, mapAsEmptyObject: true, setAsEmptyObject: true });

const set = new Set();
set.add('a');
const input = { set };

const packed = packr.pack(input);
const unpacked = packr.unpack(packed);

For some reason the built/dist version in the last release is not up-to-date with the source. I will release a new version, and make sure it is fully built, as soon as I figure out how to fix #109 .

Published an update in 1.9.2.

Cool Thanks!

Verified the new version and it does solve the issue.
Closing the issue.

Thanks again!
Appreciate your huge effort and quick response.

P.S. Will continue to test backward compatibility. If we find anything - will let you know in yet another issue ;)