msgpackr 1.8.5 → 1.9.1

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/index.js CHANGED
@@ -71,6 +71,8 @@
71
71
  return this ? this.unpack(source, options) : Unpackr.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;
@@ -204,7 +206,13 @@
204
206
  // over read
205
207
  throw new Error('Unexpected end of MessagePack data')
206
208
  } else if (!sequentialMode) {
207
- throw new Error('Data read, but end of buffer not reached ' + JSON.stringify(result).slice(0, 100))
209
+ let jsonView;
210
+ try {
211
+ jsonView = JSON.stringify(result, (_, value) => typeof value === "bigint" ? `${value}n` : value).slice(0, 100);
212
+ } catch(error) {
213
+ jsonView = '(JSON view not available ' + error + ')';
214
+ }
215
+ throw new Error('Data read, but end of buffer not reached ' + jsonView)
208
216
  }
209
217
  // else more to read, but we are reading sequentially, so don't clear source yet
210
218
  return result
@@ -360,6 +368,9 @@
360
368
  value += dataView.getUint32(position$1 + 4);
361
369
  } else if (currentUnpackr.int64AsType === 'string') {
362
370
  value = dataView.getBigUint64(position$1).toString();
371
+ } else if (currentUnpackr.int64AsType === 'auto') {
372
+ value = dataView.getBigUint64(position$1);
373
+ if (value<=BigInt(2)<<BigInt(52)) value=Number(value);
363
374
  } else
364
375
  value = dataView.getBigUint64(position$1);
365
376
  position$1 += 8;
@@ -382,6 +393,9 @@
382
393
  value += dataView.getUint32(position$1 + 4);
383
394
  } else if (currentUnpackr.int64AsType === 'string') {
384
395
  value = dataView.getBigInt64(position$1).toString();
396
+ } else if (currentUnpackr.int64AsType === 'auto') {
397
+ value = dataView.getBigInt64(position$1);
398
+ if (value>=BigInt(-2)<<BigInt(52)&&value<=BigInt(2)<<BigInt(52)) value=Number(value);
385
399
  } else
386
400
  value = dataView.getBigInt64(position$1);
387
401
  position$1 += 8;
@@ -911,9 +925,10 @@
911
925
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
912
926
  currentExtensions[0].noBuffer = true;
913
927
 
928
+ let glbl = typeof globalThis === 'object' ? globalThis : window;
914
929
  currentExtensions[0x65] = () => {
915
930
  let data = read();
916
- return (globalThis[data[0]] || Error)(data[1])
931
+ return (glbl[data[0]] || Error)(data[1])
917
932
  };
918
933
 
919
934
  currentExtensions[0x69] = (data) => {
@@ -957,7 +972,7 @@
957
972
  if (!typedArrayName)
958
973
  throw new Error('Could not find typed array for code ' + typeCode)
959
974
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
960
- return new globalThis[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
975
+ return new glbl[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
961
976
  };
962
977
  currentExtensions[0x78] = () => {
963
978
  let data = read();
@@ -1183,7 +1198,7 @@
1183
1198
  if (hasSharedUpdate)
1184
1199
  hasSharedUpdate = false;
1185
1200
  try {
1186
- if (packr.randomAccessStructure && value.constructor && value.constructor === Object)
1201
+ if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
1187
1202
  writeStruct(value);
1188
1203
  else
1189
1204
  pack(value);
@@ -1470,21 +1485,24 @@
1470
1485
  } else if (constructor === Array) {
1471
1486
  packArray(value);
1472
1487
  } else if (constructor === Map) {
1473
- length = value.size;
1474
- if (length < 0x10) {
1475
- target[position++] = 0x80 | length;
1476
- } else if (length < 0x10000) {
1477
- target[position++] = 0xde;
1478
- target[position++] = length >> 8;
1479
- target[position++] = length & 0xff;
1480
- } else {
1481
- target[position++] = 0xdf;
1482
- targetView.setUint32(position, length);
1483
- position += 4;
1484
- }
1485
- for (let [ key, entryValue ] of value) {
1486
- pack(key);
1487
- pack(entryValue);
1488
+ if (this.mapAsEmptyObject) target[position++] = 0x80;
1489
+ else {
1490
+ length = value.size;
1491
+ if (length < 0x10) {
1492
+ target[position++] = 0x80 | length;
1493
+ } else if (length < 0x10000) {
1494
+ target[position++] = 0xde;
1495
+ target[position++] = length >> 8;
1496
+ target[position++] = length & 0xff;
1497
+ } else {
1498
+ target[position++] = 0xdf;
1499
+ targetView.setUint32(position, length);
1500
+ position += 4;
1501
+ }
1502
+ for (let [key, entryValue] of value) {
1503
+ pack(key);
1504
+ pack(entryValue);
1505
+ }
1488
1506
  }
1489
1507
  } else {
1490
1508
  for (let i = 0, l = extensions.length; i < l; i++) {
@@ -1547,6 +1565,8 @@
1547
1565
  if (Array.isArray(value)) {
1548
1566
  packArray(value);
1549
1567
  } else {
1568
+ if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1569
+ return pack(value.toJSON());
1550
1570
  // no extension found, write as object
1551
1571
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1552
1572
  }
@@ -1867,6 +1887,7 @@
1867
1887
  }
1868
1888
  }, {
1869
1889
  pack(set, allocateForWrite, pack) {
1890
+ if (this.setAsEmptyObject) return pack({})
1870
1891
  let array = Array.from(set);
1871
1892
  let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1872
1893
  if (this.moreTypes) {