msgpackr 1.5.3 → 1.5.6

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
@@ -32,6 +32,13 @@ class C1Type {}
32
32
  const C1 = new C1Type();
33
33
  C1.name = 'MessagePack 0xC1';
34
34
  var sequentialMode = false;
35
+ var inlineObjectReadThreshold = 2;
36
+ try {
37
+ new Function('');
38
+ } catch(error) {
39
+ // if eval variants are not supported, do not create inline object readers ever
40
+ inlineObjectReadThreshold = Infinity;
41
+ }
35
42
 
36
43
  class Unpackr {
37
44
  constructor(options) {
@@ -442,7 +449,7 @@ const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
442
449
  function createStructureReader(structure, firstId) {
443
450
  function readObject() {
444
451
  // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
445
- if (readObject.count++ > 2) {
452
+ if (readObject.count++ > inlineObjectReadThreshold) {
446
453
  let readObject = structure.read = (new Function('r', 'return function(){return {' + structure.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}}'))(read);
447
454
  if (structure.highByte === 0)
448
455
  structure.read = createSecondByteReader(firstId, structure.read);
@@ -1069,7 +1076,6 @@ class Packr extends Unpackr {
1069
1076
  super(options);
1070
1077
  this.offset = 0;
1071
1078
  let start;
1072
- let sharedStructures;
1073
1079
  let hasSharedUpdate;
1074
1080
  let structures;
1075
1081
  let referenceMap;
@@ -1091,10 +1097,13 @@ class Packr extends Unpackr {
1091
1097
  maxSharedStructures = hasSharedStructures ? 32 : 0;
1092
1098
  if (maxSharedStructures > 8160)
1093
1099
  throw new Error('Maximum maxSharedStructure is 8160')
1100
+ if (options.structuredClone && options.moreTypes == undefined) {
1101
+ options.moreTypes = true;
1102
+ }
1094
1103
  let maxOwnStructures = options.maxOwnStructures;
1095
1104
  if (maxOwnStructures == null)
1096
1105
  maxOwnStructures = hasSharedStructures ? 32 : 64;
1097
- if (isSequential && !options.saveStructures)
1106
+ if (!this.structures && options.useRecords != false)
1098
1107
  this.structures = [];
1099
1108
  // two byte record ids for shared structures
1100
1109
  let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
@@ -1129,23 +1138,23 @@ class Packr extends Unpackr {
1129
1138
  bundledStrings$1.size = Infinity; // force a new bundle start on first string
1130
1139
  } else
1131
1140
  bundledStrings$1 = null;
1132
- sharedStructures = packr.structures;
1133
- if (sharedStructures) {
1134
- if (sharedStructures.uninitialized)
1135
- sharedStructures = packr._mergeStructures(packr.getStructures());
1136
- let sharedLength = sharedStructures.sharedLength || 0;
1141
+ structures = packr.structures;
1142
+ if (structures) {
1143
+ if (structures.uninitialized)
1144
+ structures = packr._mergeStructures(packr.getStructures());
1145
+ let sharedLength = structures.sharedLength || 0;
1137
1146
  if (sharedLength > maxSharedStructures) {
1138
- //if (maxSharedStructures <= 32 && sharedStructures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
1139
- throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + sharedStructures.sharedLength)
1147
+ //if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
1148
+ throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
1140
1149
  }
1141
- if (!sharedStructures.transitions) {
1150
+ if (!structures.transitions) {
1142
1151
  // rebuild our structure transitions
1143
- sharedStructures.transitions = Object.create(null);
1152
+ structures.transitions = Object.create(null);
1144
1153
  for (let i = 0; i < sharedLength; i++) {
1145
- let keys = sharedStructures[i];
1154
+ let keys = structures[i];
1146
1155
  if (!keys)
1147
1156
  continue
1148
- let nextTransition, transition = sharedStructures.transitions;
1157
+ let nextTransition, transition = structures.transitions;
1149
1158
  for (let j = 0, l = keys.length; j < l; j++) {
1150
1159
  let key = keys[j];
1151
1160
  nextTransition = transition[key];
@@ -1159,12 +1168,11 @@ class Packr extends Unpackr {
1159
1168
  lastSharedStructuresLength = sharedLength;
1160
1169
  }
1161
1170
  if (!isSequential) {
1162
- sharedStructures.nextId = sharedLength + 0x40;
1171
+ structures.nextId = sharedLength + 0x40;
1163
1172
  }
1164
1173
  }
1165
1174
  if (hasSharedUpdate)
1166
1175
  hasSharedUpdate = false;
1167
- structures = sharedStructures || (packr.structures = []);
1168
1176
  try {
1169
1177
  pack(value);
1170
1178
  if (bundledStrings$1) {
@@ -1187,14 +1195,15 @@ class Packr extends Unpackr {
1187
1195
  }
1188
1196
  return target.subarray(start, position$1) // position can change if we call pack again in saveStructures, so we get the buffer now
1189
1197
  } finally {
1190
- if (sharedStructures) {
1198
+ if (structures) {
1191
1199
  if (serializationsSinceTransitionRebuild < 10)
1192
1200
  serializationsSinceTransitionRebuild++;
1193
- if (sharedStructures.length > maxSharedStructures)
1194
- sharedStructures.length = maxSharedStructures;
1201
+ let sharedLength = structures.sharedLength || maxSharedStructures;
1202
+ if (structures.length > sharedLength)
1203
+ structures.length = sharedLength;
1195
1204
  if (transitionsCount > 10000) {
1196
1205
  // force a rebuild occasionally after a lot of transitions so it can get cleaned up
1197
- sharedStructures.transitions = null;
1206
+ structures.transitions = null;
1198
1207
  serializationsSinceTransitionRebuild = 0;
1199
1208
  transitionsCount = 0;
1200
1209
  if (recordIdsToRemove.length > 0)
@@ -1206,13 +1215,9 @@ class Packr extends Unpackr {
1206
1215
  recordIdsToRemove = [];
1207
1216
  }
1208
1217
  if (hasSharedUpdate && packr.saveStructures) {
1209
- let sharedLength = sharedStructures.sharedLength || maxSharedStructures;
1210
- if (sharedStructures.length > sharedLength) {
1211
- sharedStructures = sharedStructures.slice(0, sharedLength);
1212
- }
1213
1218
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1214
1219
  let returnBuffer = target.subarray(start, position$1);
1215
- if (packr.saveStructures(sharedStructures, lastSharedStructuresLength) === false) {
1220
+ if (packr.saveStructures(structures, lastSharedStructuresLength) === false) {
1216
1221
  // get updated structures and try again if the update failed
1217
1222
  packr._mergeStructures(packr.getStructures());
1218
1223
  return packr.pack(value)
@@ -1786,8 +1791,8 @@ extensions = [{
1786
1791
  }, {
1787
1792
  pack(set, allocateForWrite, pack) {
1788
1793
  let array = Array.from(set);
1789
- let { target, position} = allocateForWrite(this.structuredClone ? 3 : 0);
1790
- if (this.structuredClone) {
1794
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1795
+ if (this.moreTypes) {
1791
1796
  target[position++] = 0xd4;
1792
1797
  target[position++] = 0x73; // 's' for Set
1793
1798
  target[position++] = 0;
@@ -1796,8 +1801,8 @@ extensions = [{
1796
1801
  }
1797
1802
  }, {
1798
1803
  pack(error, allocateForWrite, pack) {
1799
- let { target, position} = allocateForWrite(this.structuredClone ? 3 : 0);
1800
- if (this.structuredClone) {
1804
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1805
+ if (this.moreTypes) {
1801
1806
  target[position++] = 0xd4;
1802
1807
  target[position++] = 0x65; // 'e' for error
1803
1808
  target[position++] = 0;
@@ -1806,8 +1811,8 @@ extensions = [{
1806
1811
  }
1807
1812
  }, {
1808
1813
  pack(regex, allocateForWrite, pack) {
1809
- let { target, position} = allocateForWrite(this.structuredClone ? 3 : 0);
1810
- if (this.structuredClone) {
1814
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1815
+ if (this.moreTypes) {
1811
1816
  target[position++] = 0xd4;
1812
1817
  target[position++] = 0x78; // 'x' for regeXp
1813
1818
  target[position++] = 0;
@@ -1816,7 +1821,7 @@ extensions = [{
1816
1821
  }
1817
1822
  }, {
1818
1823
  pack(arrayBuffer, allocateForWrite) {
1819
- if (this.structuredClone)
1824
+ if (this.moreTypes)
1820
1825
  writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
1821
1826
  else
1822
1827
  writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
@@ -1824,7 +1829,7 @@ extensions = [{
1824
1829
  }, {
1825
1830
  pack(typedArray, allocateForWrite) {
1826
1831
  let constructor = typedArray.constructor;
1827
- if (constructor !== ByteArray && this.structuredClone)
1832
+ if (constructor !== ByteArray && this.moreTypes)
1828
1833
  writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite);
1829
1834
  else
1830
1835
  writeBuffer(typedArray, allocateForWrite);
@@ -2119,8 +2124,7 @@ function tryRequire(moduleId) {
2119
2124
  let require$1 = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('node.cjs', document.baseURI).href)));
2120
2125
  return require$1(moduleId)
2121
2126
  } catch (error) {
2122
- if (typeof window != 'undefined')
2123
- console.warn('For browser usage, directly use msgpackr/unpack or msgpackr/pack modules. ' + error.message.split('\n')[0]);
2127
+ // native module is optional
2124
2128
  }
2125
2129
  }
2126
2130