msgpackr 1.8.5 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/test.js CHANGED
@@ -71,6 +71,8 @@
71
71
  return this ? this.unpack(source, options) : Unpackr$1.prototype.unpack.call(defaultOptions, source, options)
72
72
  })
73
73
  }
74
+ if (!source.buffer && source.constructor === ArrayBuffer)
75
+ source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
74
76
  if (typeof options === 'object') {
75
77
  srcEnd = options.end || source.length;
76
78
  position$1 = options.start || 0;
@@ -208,7 +210,13 @@
208
210
  // over read
209
211
  throw new Error('Unexpected end of MessagePack data')
210
212
  } else if (!sequentialMode) {
211
- throw new Error('Data read, but end of buffer not reached ' + JSON.stringify(result).slice(0, 100))
213
+ let jsonView;
214
+ try {
215
+ jsonView = JSON.stringify(result, (_, value) => typeof value === "bigint" ? `${value}n` : value).slice(0, 100);
216
+ } catch(error) {
217
+ jsonView = '(JSON view not available ' + error + ')';
218
+ }
219
+ throw new Error('Data read, but end of buffer not reached ' + jsonView)
212
220
  }
213
221
  // else more to read, but we are reading sequentially, so don't clear source yet
214
222
  return result
@@ -364,6 +372,9 @@
364
372
  value += dataView.getUint32(position$1 + 4);
365
373
  } else if (currentUnpackr.int64AsType === 'string') {
366
374
  value = dataView.getBigUint64(position$1).toString();
375
+ } else if (currentUnpackr.int64AsType === 'auto') {
376
+ value = dataView.getBigUint64(position$1);
377
+ if (value<=BigInt(2)<<BigInt(52)) value=Number(value);
367
378
  } else
368
379
  value = dataView.getBigUint64(position$1);
369
380
  position$1 += 8;
@@ -386,6 +397,9 @@
386
397
  value += dataView.getUint32(position$1 + 4);
387
398
  } else if (currentUnpackr.int64AsType === 'string') {
388
399
  value = dataView.getBigInt64(position$1).toString();
400
+ } else if (currentUnpackr.int64AsType === 'auto') {
401
+ value = dataView.getBigInt64(position$1);
402
+ if (value>=BigInt(-2)<<BigInt(52)&&value<=BigInt(2)<<BigInt(52)) value=Number(value);
389
403
  } else
390
404
  value = dataView.getBigInt64(position$1);
391
405
  position$1 += 8;
@@ -963,9 +977,10 @@
963
977
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
964
978
  currentExtensions[0].noBuffer = true;
965
979
 
980
+ let global = typeof globalThis === 'object' ? globalThis : window;
966
981
  currentExtensions[0x65] = () => {
967
982
  let data = read();
968
- return (globalThis[data[0]] || Error)(data[1])
983
+ return (global[data[0]] || Error)(data[1])
969
984
  };
970
985
 
971
986
  currentExtensions[0x69] = (data) => {
@@ -1009,7 +1024,7 @@
1009
1024
  if (!typedArrayName)
1010
1025
  throw new Error('Could not find typed array for code ' + typeCode)
1011
1026
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
1012
- return new globalThis[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
1027
+ return new global[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
1013
1028
  };
1014
1029
  currentExtensions[0x78] = () => {
1015
1030
  let data = read();
@@ -1245,7 +1260,7 @@
1245
1260
  if (hasSharedUpdate)
1246
1261
  hasSharedUpdate = false;
1247
1262
  try {
1248
- if (packr.randomAccessStructure && value.constructor && value.constructor === Object)
1263
+ if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
1249
1264
  writeStruct(value);
1250
1265
  else
1251
1266
  pack(value);
@@ -1532,21 +1547,24 @@
1532
1547
  } else if (constructor === Array) {
1533
1548
  packArray(value);
1534
1549
  } else if (constructor === Map) {
1535
- length = value.size;
1536
- if (length < 0x10) {
1537
- target[position++] = 0x80 | length;
1538
- } else if (length < 0x10000) {
1539
- target[position++] = 0xde;
1540
- target[position++] = length >> 8;
1541
- target[position++] = length & 0xff;
1542
- } else {
1543
- target[position++] = 0xdf;
1544
- targetView.setUint32(position, length);
1545
- position += 4;
1546
- }
1547
- for (let [ key, entryValue ] of value) {
1548
- pack(key);
1549
- pack(entryValue);
1550
+ if (this.mapAsEmptyObject) target[position++] = 0x80;
1551
+ else {
1552
+ length = value.size;
1553
+ if (length < 0x10) {
1554
+ target[position++] = 0x80 | length;
1555
+ } else if (length < 0x10000) {
1556
+ target[position++] = 0xde;
1557
+ target[position++] = length >> 8;
1558
+ target[position++] = length & 0xff;
1559
+ } else {
1560
+ target[position++] = 0xdf;
1561
+ targetView.setUint32(position, length);
1562
+ position += 4;
1563
+ }
1564
+ for (let [key, entryValue] of value) {
1565
+ pack(key);
1566
+ pack(entryValue);
1567
+ }
1550
1568
  }
1551
1569
  } else {
1552
1570
  for (let i = 0, l = extensions.length; i < l; i++) {
@@ -1609,6 +1627,8 @@
1609
1627
  if (Array.isArray(value)) {
1610
1628
  packArray(value);
1611
1629
  } else {
1630
+ if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1631
+ return pack(value.toJSON());
1612
1632
  // no extension found, write as object
1613
1633
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1614
1634
  }
@@ -1929,6 +1949,7 @@
1929
1949
  }
1930
1950
  }, {
1931
1951
  pack(set, allocateForWrite, pack) {
1952
+ if (this.setAsEmptyObject) return pack({})
1932
1953
  let array = Array.from(set);
1933
1954
  let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1934
1955
  if (this.moreTypes) {
@@ -3039,6 +3060,15 @@
3039
3060
  var deserialized = unpack(serialized);
3040
3061
  assert.equal(deserialized, data);
3041
3062
  });
3063
+ test('use ArrayBuffer', function () {
3064
+ const data = {prop: 'a test'};
3065
+ var serialized = pack(data);
3066
+ let ab = new ArrayBuffer(serialized.length);
3067
+ let u8 = new Uint8Array(ab);
3068
+ u8.set(serialized);
3069
+ var deserialized = unpack(ab);
3070
+ assert.deepEqual(deserialized, data);
3071
+ });
3042
3072
  test('pack/unpack varying data with random access structures', function () {
3043
3073
  let structures = [];
3044
3074
  let packr = new Packr({
@@ -3806,6 +3836,25 @@
3806
3836
  var deserialized = packr.unpack(serialized);
3807
3837
  assert.deepEqual(deserialized.a, 325283295382932843);
3808
3838
  });
3839
+ test('bigint to auto (float or bigint)', function() {
3840
+ var data = {
3841
+ a: -9007199254740993n,
3842
+ b: -9007199254740992n,
3843
+ c: 0n,
3844
+ d: 9007199254740992n,
3845
+ e: 9007199254740993n,
3846
+ };
3847
+ let packr = new Packr({
3848
+ int64AsType: 'auto'
3849
+ });
3850
+ var serialized = packr.pack(data);
3851
+ var deserialized = packr.unpack(serialized);
3852
+ assert.deepEqual(deserialized.a, -9007199254740993n);
3853
+ assert.deepEqual(deserialized.b, -9007199254740992);
3854
+ assert.deepEqual(deserialized.c, 0);
3855
+ assert.deepEqual(deserialized.d, 9007199254740992);
3856
+ assert.deepEqual(deserialized.e, 9007199254740993n);
3857
+ });
3809
3858
  test('bigint to string', function() {
3810
3859
  var data = {
3811
3860
  a: 325283295382932843n,