msgpackr 1.8.4 → 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/node.cjs CHANGED
@@ -73,6 +73,8 @@ class Unpackr {
73
73
  return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
74
74
  })
75
75
  }
76
+ if (!source.buffer && source.constructor === ArrayBuffer)
77
+ source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
76
78
  if (typeof options === 'object') {
77
79
  srcEnd = options.end || source.length;
78
80
  position$1 = options.start || 0;
@@ -210,7 +212,13 @@ function checkedRead(options) {
210
212
  // over read
211
213
  throw new Error('Unexpected end of MessagePack data')
212
214
  } else if (!sequentialMode) {
213
- throw new Error('Data read, but end of buffer not reached ' + JSON.stringify(result).slice(0, 100))
215
+ let jsonView;
216
+ try {
217
+ jsonView = JSON.stringify(result, (_, value) => typeof value === "bigint" ? `${value}n` : value).slice(0, 100);
218
+ } catch(error) {
219
+ jsonView = '(JSON view not available ' + error + ')';
220
+ }
221
+ throw new Error('Data read, but end of buffer not reached ' + jsonView)
214
222
  }
215
223
  // else more to read, but we are reading sequentially, so don't clear source yet
216
224
  return result
@@ -366,6 +374,9 @@ function read() {
366
374
  value += dataView.getUint32(position$1 + 4);
367
375
  } else if (currentUnpackr.int64AsType === 'string') {
368
376
  value = dataView.getBigUint64(position$1).toString();
377
+ } else if (currentUnpackr.int64AsType === 'auto') {
378
+ value = dataView.getBigUint64(position$1);
379
+ if (value<=BigInt(2)<<BigInt(52)) value=Number(value);
369
380
  } else
370
381
  value = dataView.getBigUint64(position$1);
371
382
  position$1 += 8;
@@ -388,6 +399,9 @@ function read() {
388
399
  value += dataView.getUint32(position$1 + 4);
389
400
  } else if (currentUnpackr.int64AsType === 'string') {
390
401
  value = dataView.getBigInt64(position$1).toString();
402
+ } else if (currentUnpackr.int64AsType === 'auto') {
403
+ value = dataView.getBigInt64(position$1);
404
+ if (value>=BigInt(-2)<<BigInt(52)&&value<=BigInt(2)<<BigInt(52)) value=Number(value);
391
405
  } else
392
406
  value = dataView.getBigInt64(position$1);
393
407
  position$1 += 8;
@@ -967,9 +981,10 @@ const recordDefinition = (id, highByte) => {
967
981
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
968
982
  currentExtensions[0].noBuffer = true;
969
983
 
984
+ let global = typeof globalThis === 'object' ? globalThis : window;
970
985
  currentExtensions[0x65] = () => {
971
986
  let data = read();
972
- return (globalThis[data[0]] || Error)(data[1])
987
+ return (global[data[0]] || Error)(data[1])
973
988
  };
974
989
 
975
990
  currentExtensions[0x69] = (data) => {
@@ -1013,7 +1028,7 @@ currentExtensions[0x74] = (data) => {
1013
1028
  if (!typedArrayName)
1014
1029
  throw new Error('Could not find typed array for code ' + typeCode)
1015
1030
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
1016
- return new globalThis[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
1031
+ return new global[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
1017
1032
  };
1018
1033
  currentExtensions[0x78] = () => {
1019
1034
  let data = read();
@@ -1250,7 +1265,7 @@ class Packr extends Unpackr {
1250
1265
  if (hasSharedUpdate)
1251
1266
  hasSharedUpdate = false;
1252
1267
  try {
1253
- if (packr.randomAccessStructure && value.constructor && value.constructor === Object)
1268
+ if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
1254
1269
  writeStruct(value);
1255
1270
  else
1256
1271
  pack(value);
@@ -1537,21 +1552,24 @@ class Packr extends Unpackr {
1537
1552
  } else if (constructor === Array) {
1538
1553
  packArray(value);
1539
1554
  } else if (constructor === Map) {
1540
- length = value.size;
1541
- if (length < 0x10) {
1542
- target[position++] = 0x80 | length;
1543
- } else if (length < 0x10000) {
1544
- target[position++] = 0xde;
1545
- target[position++] = length >> 8;
1546
- target[position++] = length & 0xff;
1547
- } else {
1548
- target[position++] = 0xdf;
1549
- targetView.setUint32(position, length);
1550
- position += 4;
1551
- }
1552
- for (let [ key, entryValue ] of value) {
1553
- pack(key);
1554
- pack(entryValue);
1555
+ if (this.mapAsEmptyObject) target[position++] = 0x80;
1556
+ else {
1557
+ length = value.size;
1558
+ if (length < 0x10) {
1559
+ target[position++] = 0x80 | length;
1560
+ } else if (length < 0x10000) {
1561
+ target[position++] = 0xde;
1562
+ target[position++] = length >> 8;
1563
+ target[position++] = length & 0xff;
1564
+ } else {
1565
+ target[position++] = 0xdf;
1566
+ targetView.setUint32(position, length);
1567
+ position += 4;
1568
+ }
1569
+ for (let [key, entryValue] of value) {
1570
+ pack(key);
1571
+ pack(entryValue);
1572
+ }
1555
1573
  }
1556
1574
  } else {
1557
1575
  for (let i = 0, l = extensions.length; i < l; i++) {
@@ -1614,6 +1632,8 @@ class Packr extends Unpackr {
1614
1632
  if (Array.isArray(value)) {
1615
1633
  packArray(value);
1616
1634
  } else {
1635
+ if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1636
+ return pack(value.toJSON());
1617
1637
  // no extension found, write as object
1618
1638
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1619
1639
  }
@@ -1934,6 +1954,7 @@ extensions = [{
1934
1954
  }
1935
1955
  }, {
1936
1956
  pack(set, allocateForWrite, pack) {
1957
+ if (this.setAsEmptyObject) return pack({})
1937
1958
  let array = Array.from(set);
1938
1959
  let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1939
1960
  if (this.moreTypes) {
@@ -2477,11 +2498,11 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2477
2498
  case 3:
2478
2499
  if (recordId >= 0x10000) return 0;
2479
2500
  target[start] = 0x39;
2480
- target.setUint16(start + 1, recordId, true);
2501
+ targetView.setUint16(start + 1, recordId, true);
2481
2502
  break;
2482
2503
  case 4:
2483
2504
  if (recordId >= 0x1000000) return 0;
2484
- target.setUint32(start, (recordId << 8) + 0x3a, true);
2505
+ targetView.setUint32(start, (recordId << 8) + 0x3a, true);
2485
2506
  break;
2486
2507
  }
2487
2508