msgpackr 1.8.0 → 1.8.2

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
@@ -116,7 +116,7 @@
116
116
  let size = source.length;
117
117
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
118
118
  if (forEach) {
119
- forEach(value);
119
+ if (forEach(value) === false) return;
120
120
  while(position$1 < size) {
121
121
  lastPosition = position$1;
122
122
  if (forEach(checkedRead()) === false) {
@@ -1107,7 +1107,7 @@
1107
1107
  if (maxSharedStructures > 8160)
1108
1108
  throw new Error('Maximum maxSharedStructure is 8160')
1109
1109
  if (options.structuredClone && options.moreTypes == undefined) {
1110
- options.moreTypes = true;
1110
+ this.moreTypes = true;
1111
1111
  }
1112
1112
  let maxOwnStructures = options.maxOwnStructures;
1113
1113
  if (maxOwnStructures == null)
@@ -1268,6 +1268,23 @@
1268
1268
  position = start;
1269
1269
  }
1270
1270
  };
1271
+ const packArray = (value) => {
1272
+ var length = value.length;
1273
+ if (length < 0x10) {
1274
+ target[position++] = 0x90 | length;
1275
+ } else if (length < 0x10000) {
1276
+ target[position++] = 0xdc;
1277
+ target[position++] = length >> 8;
1278
+ target[position++] = length & 0xff;
1279
+ } else {
1280
+ target[position++] = 0xdd;
1281
+ targetView.setUint32(position, length);
1282
+ position += 4;
1283
+ }
1284
+ for (let i = 0; i < length; i++) {
1285
+ pack(value[i]);
1286
+ }
1287
+ };
1271
1288
  const pack = (value) => {
1272
1289
  if (position > safeEnd)
1273
1290
  target = makeRoom(position);
@@ -1450,22 +1467,8 @@
1450
1467
  let constructor = value.constructor;
1451
1468
  if (constructor === Object) {
1452
1469
  writeObject(value, true);
1453
- } else if (constructor === Array || Array.isArray(value)) {
1454
- length = value.length;
1455
- if (length < 0x10) {
1456
- target[position++] = 0x90 | length;
1457
- } else if (length < 0x10000) {
1458
- target[position++] = 0xdc;
1459
- target[position++] = length >> 8;
1460
- target[position++] = length & 0xff;
1461
- } else {
1462
- target[position++] = 0xdd;
1463
- targetView.setUint32(position, length);
1464
- position += 4;
1465
- }
1466
- for (let i = 0; i < length; i++) {
1467
- pack(value[i]);
1468
- }
1470
+ } else if (constructor === Array) {
1471
+ packArray(value);
1469
1472
  } else if (constructor === Map) {
1470
1473
  length = value.size;
1471
1474
  if (length < 0x10) {
@@ -1494,7 +1497,16 @@
1494
1497
  target[position++] = extension.type;
1495
1498
  target[position++] = 0;
1496
1499
  }
1497
- pack(extension.write.call(this, value));
1500
+ let writeResult = extension.write.call(this, value);
1501
+ if (writeResult === value) { // avoid infinite recursion
1502
+ if (Array.isArray(value)) {
1503
+ packArray(value);
1504
+ } else {
1505
+ writeObject(value);
1506
+ }
1507
+ } else {
1508
+ pack(writeResult);
1509
+ }
1498
1510
  return
1499
1511
  }
1500
1512
  let currentTarget = target;
@@ -1531,8 +1543,13 @@
1531
1543
  return
1532
1544
  }
1533
1545
  }
1534
- // no extension found, write as object
1535
- writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1546
+ // check isArray after extensions, because extensions can extend Array
1547
+ if (Array.isArray(value)) {
1548
+ packArray(value);
1549
+ } else {
1550
+ // no extension found, write as object
1551
+ writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1552
+ }
1536
1553
  }
1537
1554
  }
1538
1555
  } else if (type === 'boolean') {
@@ -1785,12 +1802,11 @@
1785
1802
  if (notifySharedUpdate)
1786
1803
  return hasSharedUpdate = true;
1787
1804
  position = newPosition;
1788
- if (start > 0) {
1789
- pack(value);
1790
- if (start == 0)
1791
- return { position, targetView, target }; // indicate the buffer was re-allocated
1792
- } else
1793
- pack(value);
1805
+ let startTarget = target;
1806
+ pack(value);
1807
+ if (startTarget !== target) {
1808
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1809
+ }
1794
1810
  return position;
1795
1811
  }, this);
1796
1812
  if (newPosition === 0) // bail and go to a msgpack object