msgpackr 1.7.1 → 1.8.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
@@ -1,22 +1,22 @@
1
- (function (msgpackr, chai, inspector, fs) {
1
+ (function (chai, stream, module, fs) {
2
2
  'use strict';
3
3
 
4
- chai = chai && Object.prototype.hasOwnProperty.call(chai, 'default') ? chai['default'] : chai;
5
- inspector = inspector && Object.prototype.hasOwnProperty.call(inspector, 'default') ? inspector['default'] : inspector;
6
-
7
4
  var decoder;
8
5
  try {
9
6
  decoder = new TextDecoder();
10
7
  } catch(error) {}
11
8
  var src;
12
9
  var srcEnd;
13
- var position = 0;
10
+ var position$1 = 0;
11
+ const EMPTY_ARRAY = [];
12
+ var strings = EMPTY_ARRAY;
13
+ var stringPosition = 0;
14
14
  var currentUnpackr = {};
15
15
  var currentStructures;
16
16
  var srcString;
17
17
  var srcStringStart = 0;
18
18
  var srcStringEnd = 0;
19
- var bundledStrings;
19
+ var bundledStrings$1;
20
20
  var referenceMap;
21
21
  var currentExtensions = [];
22
22
  var dataView;
@@ -29,7 +29,8 @@
29
29
  C1.name = 'MessagePack 0xC1';
30
30
  var sequentialMode = false;
31
31
  var inlineObjectReadThreshold = 2;
32
- var readStruct, onLoadedStructures, onSaveState;
32
+ var readStruct$1, onLoadedStructures$1, onSaveState;
33
+ // no-eval build
33
34
  try {
34
35
  new Function('');
35
36
  } catch(error) {
@@ -37,7 +38,7 @@
37
38
  inlineObjectReadThreshold = Infinity;
38
39
  }
39
40
 
40
- class Unpackr {
41
+ let Unpackr$1 = class Unpackr {
41
42
  constructor(options) {
42
43
  if (options) {
43
44
  if (options.useRecords === false && options.mapsAsObjects === undefined)
@@ -56,27 +57,32 @@
56
57
  (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
57
58
  options.structures.sharedLength = 0;
58
59
  }
60
+ if (options.int64AsNumber) {
61
+ options.int64AsType = 'number';
62
+ }
59
63
  }
60
64
  Object.assign(this, options);
61
65
  }
62
66
  unpack(source, options) {
63
67
  if (src) {
64
68
  // re-entrant execution, save the state and restore it after we do this unpack
65
- return saveState(() => {
69
+ return saveState$1(() => {
66
70
  clearSource();
67
- return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
71
+ return this ? this.unpack(source, options) : Unpackr$1.prototype.unpack.call(defaultOptions, source, options)
68
72
  })
69
73
  }
70
74
  if (typeof options === 'object') {
71
75
  srcEnd = options.end || source.length;
72
- position = options.start || 0;
76
+ position$1 = options.start || 0;
73
77
  } else {
74
- position = 0;
78
+ position$1 = 0;
75
79
  srcEnd = options > -1 ? options : source.length;
76
80
  }
81
+ stringPosition = 0;
77
82
  srcStringEnd = 0;
78
83
  srcString = null;
79
- bundledStrings = null;
84
+ strings = EMPTY_ARRAY;
85
+ bundledStrings$1 = null;
80
86
  src = source;
81
87
  // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
82
88
  // technique for getting data from a database where it can be copied into an existing buffer instead of creating
@@ -90,7 +96,7 @@
90
96
  throw error
91
97
  throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
92
98
  }
93
- if (this instanceof Unpackr) {
99
+ if (this instanceof Unpackr$1) {
94
100
  currentUnpackr = this;
95
101
  if (this.structures) {
96
102
  currentStructures = this.structures;
@@ -113,8 +119,8 @@
113
119
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
114
120
  if (forEach) {
115
121
  forEach(value);
116
- while(position < size) {
117
- lastPosition = position;
122
+ while(position$1 < size) {
123
+ lastPosition = position$1;
118
124
  if (forEach(checkedRead()) === false) {
119
125
  return
120
126
  }
@@ -122,8 +128,8 @@
122
128
  }
123
129
  else {
124
130
  values = [ value ];
125
- while(position < size) {
126
- lastPosition = position;
131
+ while(position$1 < size) {
132
+ lastPosition = position$1;
127
133
  values.push(checkedRead());
128
134
  }
129
135
  return values
@@ -138,8 +144,8 @@
138
144
  }
139
145
  }
140
146
  _mergeStructures(loadedStructures, existingStructures) {
141
- if (onLoadedStructures)
142
- loadedStructures = onLoadedStructures.call(this, loadedStructures);
147
+ if (onLoadedStructures$1)
148
+ loadedStructures = onLoadedStructures$1.call(this, loadedStructures);
143
149
  loadedStructures = loadedStructures || [];
144
150
  if (Object.isFrozen(loadedStructures))
145
151
  loadedStructures = loadedStructures.map(structure => structure.slice(0));
@@ -168,7 +174,7 @@
168
174
  decode(source, end) {
169
175
  return this.unpack(source, end)
170
176
  }
171
- }
177
+ };
172
178
  function checkedRead(options) {
173
179
  try {
174
180
  if (!currentUnpackr.trusted && !sequentialMode) {
@@ -177,18 +183,20 @@
177
183
  currentStructures.length = sharedLength;
178
184
  }
179
185
  let result;
180
- if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
181
- result = readStruct(src, position, srcEnd, currentUnpackr);
186
+ if (currentUnpackr.randomAccessStructure && src[position$1] < 0x40 && src[position$1] >= 0x20 && readStruct$1) {
187
+ result = readStruct$1(src, position$1, srcEnd, currentUnpackr);
182
188
  src = null; // dispose of this so that recursive unpack calls don't save state
183
189
  if (!(options && options.lazy) && result)
184
190
  result = result.toJSON();
185
- position = srcEnd;
191
+ position$1 = srcEnd;
186
192
  } else
187
193
  result = read();
188
- if (bundledStrings) // bundled strings to skip past
189
- position = bundledStrings.postBundlePosition;
194
+ if (bundledStrings$1) { // bundled strings to skip past
195
+ position$1 = bundledStrings$1.postBundlePosition;
196
+ bundledStrings$1 = null;
197
+ }
190
198
 
191
- if (position == srcEnd) {
199
+ if (position$1 == srcEnd) {
192
200
  // finished reading this source, cleanup references
193
201
  if (currentStructures && currentStructures.restoreStructures)
194
202
  restoreStructures();
@@ -196,7 +204,7 @@
196
204
  src = null;
197
205
  if (referenceMap)
198
206
  referenceMap = null;
199
- } else if (position > srcEnd) {
207
+ } else if (position$1 > srcEnd) {
200
208
  // over read
201
209
  throw new Error('Unexpected end of MessagePack data')
202
210
  } else if (!sequentialMode) {
@@ -208,7 +216,7 @@
208
216
  if (currentStructures && currentStructures.restoreStructures)
209
217
  restoreStructures();
210
218
  clearSource();
211
- if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
219
+ if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
212
220
  error.incomplete = true;
213
221
  }
214
222
  throw error
@@ -223,7 +231,7 @@
223
231
  }
224
232
 
225
233
  function read() {
226
- let token = src[position++];
234
+ let token = src[position$1++];
227
235
  if (token < 0xa0) {
228
236
  if (token < 0x80) {
229
237
  if (token < 0x40)
@@ -271,8 +279,8 @@
271
279
  } else if (token < 0xc0) {
272
280
  // fixstr
273
281
  let length = token - 0xa0;
274
- if (srcStringEnd >= position) {
275
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
282
+ if (srcStringEnd >= position$1) {
283
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
276
284
  }
277
285
  if (srcStringEnd == 0 && srcEnd < 140) {
278
286
  // for small blocks, avoiding the overhead of the extract call is helpful
@@ -286,124 +294,128 @@
286
294
  switch (token) {
287
295
  case 0xc0: return null
288
296
  case 0xc1:
289
- if (bundledStrings) {
297
+ if (bundledStrings$1) {
290
298
  value = read(); // followed by the length of the string in characters (not bytes!)
291
299
  if (value > 0)
292
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
300
+ return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
293
301
  else
294
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
302
+ return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
295
303
  }
296
304
  return C1; // "never-used", return special object to denote that
297
305
  case 0xc2: return false
298
306
  case 0xc3: return true
299
307
  case 0xc4:
300
308
  // bin 8
301
- value = src[position++];
309
+ value = src[position$1++];
302
310
  if (value === undefined)
303
311
  throw new Error('Unexpected end of buffer')
304
312
  return readBin(value)
305
313
  case 0xc5:
306
314
  // bin 16
307
- value = dataView.getUint16(position);
308
- position += 2;
315
+ value = dataView.getUint16(position$1);
316
+ position$1 += 2;
309
317
  return readBin(value)
310
318
  case 0xc6:
311
319
  // bin 32
312
- value = dataView.getUint32(position);
313
- position += 4;
320
+ value = dataView.getUint32(position$1);
321
+ position$1 += 4;
314
322
  return readBin(value)
315
323
  case 0xc7:
316
324
  // ext 8
317
- return readExt(src[position++])
325
+ return readExt(src[position$1++])
318
326
  case 0xc8:
319
327
  // ext 16
320
- value = dataView.getUint16(position);
321
- position += 2;
328
+ value = dataView.getUint16(position$1);
329
+ position$1 += 2;
322
330
  return readExt(value)
323
331
  case 0xc9:
324
332
  // ext 32
325
- value = dataView.getUint32(position);
326
- position += 4;
333
+ value = dataView.getUint32(position$1);
334
+ position$1 += 4;
327
335
  return readExt(value)
328
336
  case 0xca:
329
- value = dataView.getFloat32(position);
337
+ value = dataView.getFloat32(position$1);
330
338
  if (currentUnpackr.useFloat32 > 2) {
331
339
  // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
332
- let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
333
- position += 4;
340
+ let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
341
+ position$1 += 4;
334
342
  return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
335
343
  }
336
- position += 4;
344
+ position$1 += 4;
337
345
  return value
338
346
  case 0xcb:
339
- value = dataView.getFloat64(position);
340
- position += 8;
347
+ value = dataView.getFloat64(position$1);
348
+ position$1 += 8;
341
349
  return value
342
350
  // uint handlers
343
351
  case 0xcc:
344
- return src[position++]
352
+ return src[position$1++]
345
353
  case 0xcd:
346
- value = dataView.getUint16(position);
347
- position += 2;
354
+ value = dataView.getUint16(position$1);
355
+ position$1 += 2;
348
356
  return value
349
357
  case 0xce:
350
- value = dataView.getUint32(position);
351
- position += 4;
358
+ value = dataView.getUint32(position$1);
359
+ position$1 += 4;
352
360
  return value
353
361
  case 0xcf:
354
- if (currentUnpackr.int64AsNumber) {
355
- value = dataView.getUint32(position) * 0x100000000;
356
- value += dataView.getUint32(position + 4);
362
+ if (currentUnpackr.int64AsType === 'number') {
363
+ value = dataView.getUint32(position$1) * 0x100000000;
364
+ value += dataView.getUint32(position$1 + 4);
365
+ } else if (currentUnpackr.int64AsType === 'string') {
366
+ value = dataView.getBigUint64(position$1).toString();
357
367
  } else
358
- value = dataView.getBigUint64(position);
359
- position += 8;
368
+ value = dataView.getBigUint64(position$1);
369
+ position$1 += 8;
360
370
  return value
361
371
 
362
372
  // int handlers
363
373
  case 0xd0:
364
- return dataView.getInt8(position++)
374
+ return dataView.getInt8(position$1++)
365
375
  case 0xd1:
366
- value = dataView.getInt16(position);
367
- position += 2;
376
+ value = dataView.getInt16(position$1);
377
+ position$1 += 2;
368
378
  return value
369
379
  case 0xd2:
370
- value = dataView.getInt32(position);
371
- position += 4;
380
+ value = dataView.getInt32(position$1);
381
+ position$1 += 4;
372
382
  return value
373
383
  case 0xd3:
374
- if (currentUnpackr.int64AsNumber) {
375
- value = dataView.getInt32(position) * 0x100000000;
376
- value += dataView.getUint32(position + 4);
384
+ if (currentUnpackr.int64AsType === 'number') {
385
+ value = dataView.getInt32(position$1) * 0x100000000;
386
+ value += dataView.getUint32(position$1 + 4);
387
+ } else if (currentUnpackr.int64AsType === 'string') {
388
+ value = dataView.getBigInt64(position$1).toString();
377
389
  } else
378
- value = dataView.getBigInt64(position);
379
- position += 8;
390
+ value = dataView.getBigInt64(position$1);
391
+ position$1 += 8;
380
392
  return value
381
393
 
382
394
  case 0xd4:
383
395
  // fixext 1
384
- value = src[position++];
396
+ value = src[position$1++];
385
397
  if (value == 0x72) {
386
- return recordDefinition(src[position++] & 0x3f)
398
+ return recordDefinition(src[position$1++] & 0x3f)
387
399
  } else {
388
400
  let extension = currentExtensions[value];
389
401
  if (extension) {
390
402
  if (extension.read) {
391
- position++; // skip filler byte
403
+ position$1++; // skip filler byte
392
404
  return extension.read(read())
393
405
  } else if (extension.noBuffer) {
394
- position++; // skip filler byte
406
+ position$1++; // skip filler byte
395
407
  return extension()
396
408
  } else
397
- return extension(src.subarray(position, ++position))
409
+ return extension(src.subarray(position$1, ++position$1))
398
410
  } else
399
411
  throw new Error('Unknown extension ' + value)
400
412
  }
401
413
  case 0xd5:
402
414
  // fixext 2
403
- value = src[position];
415
+ value = src[position$1];
404
416
  if (value == 0x72) {
405
- position++;
406
- return recordDefinition(src[position++] & 0x3f, src[position++])
417
+ position$1++;
418
+ return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
407
419
  } else
408
420
  return readExt(2)
409
421
  case 0xd6:
@@ -417,46 +429,46 @@
417
429
  return readExt(16)
418
430
  case 0xd9:
419
431
  // str 8
420
- value = src[position++];
421
- if (srcStringEnd >= position) {
422
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
432
+ value = src[position$1++];
433
+ if (srcStringEnd >= position$1) {
434
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
423
435
  }
424
436
  return readString8(value)
425
437
  case 0xda:
426
438
  // str 16
427
- value = dataView.getUint16(position);
428
- position += 2;
429
- if (srcStringEnd >= position) {
430
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
439
+ value = dataView.getUint16(position$1);
440
+ position$1 += 2;
441
+ if (srcStringEnd >= position$1) {
442
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
431
443
  }
432
444
  return readString16(value)
433
445
  case 0xdb:
434
446
  // str 32
435
- value = dataView.getUint32(position);
436
- position += 4;
437
- if (srcStringEnd >= position) {
438
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
447
+ value = dataView.getUint32(position$1);
448
+ position$1 += 4;
449
+ if (srcStringEnd >= position$1) {
450
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
439
451
  }
440
452
  return readString32(value)
441
453
  case 0xdc:
442
454
  // array 16
443
- value = dataView.getUint16(position);
444
- position += 2;
455
+ value = dataView.getUint16(position$1);
456
+ position$1 += 2;
445
457
  return readArray(value)
446
458
  case 0xdd:
447
459
  // array 32
448
- value = dataView.getUint32(position);
449
- position += 4;
460
+ value = dataView.getUint32(position$1);
461
+ position$1 += 4;
450
462
  return readArray(value)
451
463
  case 0xde:
452
464
  // map 16
453
- value = dataView.getUint16(position);
454
- position += 2;
465
+ value = dataView.getUint16(position$1);
466
+ position$1 += 2;
455
467
  return readMap(value)
456
468
  case 0xdf:
457
469
  // map 32
458
- value = dataView.getUint32(position);
459
- position += 4;
470
+ value = dataView.getUint32(position$1);
471
+ position$1 += 4;
460
472
  return readMap(value)
461
473
  default: // negative int
462
474
  if (token >= 0xe0)
@@ -502,7 +514,7 @@
502
514
 
503
515
  const createSecondByteReader = (firstId, read0) => {
504
516
  return function() {
505
- let highByte = src[position++];
517
+ let highByte = src[position$1++];
506
518
  if (highByte === 0)
507
519
  return read0()
508
520
  let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
@@ -517,7 +529,7 @@
517
529
  };
518
530
 
519
531
  function loadStructures() {
520
- let loadedStructures = saveState(() => {
532
+ let loadedStructures = saveState$1(() => {
521
533
  // save the state in case getStructures modifies our buffer
522
534
  src = null;
523
535
  return currentUnpackr.getStructures()
@@ -529,6 +541,44 @@
529
541
  var readString8 = readStringJS;
530
542
  var readString16 = readStringJS;
531
543
  var readString32 = readStringJS;
544
+
545
+ function setExtractor(extractStrings) {
546
+ readFixedString = readString(1);
547
+ readString8 = readString(2);
548
+ readString16 = readString(3);
549
+ readString32 = readString(5);
550
+ function readString(headerLength) {
551
+ return function readString(length) {
552
+ let string = strings[stringPosition++];
553
+ if (string == null) {
554
+ if (bundledStrings$1)
555
+ return readStringJS(length)
556
+ let extraction = extractStrings(position$1 - headerLength, srcEnd, src);
557
+ if (typeof extraction == 'string') {
558
+ string = extraction;
559
+ strings = EMPTY_ARRAY;
560
+ } else {
561
+ strings = extraction;
562
+ stringPosition = 1;
563
+ srcStringEnd = 1; // even if a utf-8 string was decoded, must indicate we are in the midst of extracted strings and can't skip strings
564
+ string = strings[0];
565
+ if (string === undefined)
566
+ throw new Error('Unexpected end of buffer')
567
+ }
568
+ }
569
+ let srcStringLength = string.length;
570
+ if (srcStringLength <= length) {
571
+ position$1 += length;
572
+ return string
573
+ }
574
+ srcString = string;
575
+ srcStringStart = position$1;
576
+ srcStringEnd = position$1 + srcStringLength;
577
+ position$1 += length;
578
+ return string.slice(0, length) // we know we just want the beginning
579
+ }
580
+ }
581
+ }
532
582
  function readStringJS(length) {
533
583
  let result;
534
584
  if (length < 16) {
@@ -536,29 +586,29 @@
536
586
  return result
537
587
  }
538
588
  if (length > 64 && decoder)
539
- return decoder.decode(src.subarray(position, position += length))
540
- const end = position + length;
589
+ return decoder.decode(src.subarray(position$1, position$1 += length))
590
+ const end = position$1 + length;
541
591
  const units = [];
542
592
  result = '';
543
- while (position < end) {
544
- const byte1 = src[position++];
593
+ while (position$1 < end) {
594
+ const byte1 = src[position$1++];
545
595
  if ((byte1 & 0x80) === 0) {
546
596
  // 1 byte
547
597
  units.push(byte1);
548
598
  } else if ((byte1 & 0xe0) === 0xc0) {
549
599
  // 2 bytes
550
- const byte2 = src[position++] & 0x3f;
600
+ const byte2 = src[position$1++] & 0x3f;
551
601
  units.push(((byte1 & 0x1f) << 6) | byte2);
552
602
  } else if ((byte1 & 0xf0) === 0xe0) {
553
603
  // 3 bytes
554
- const byte2 = src[position++] & 0x3f;
555
- const byte3 = src[position++] & 0x3f;
604
+ const byte2 = src[position$1++] & 0x3f;
605
+ const byte3 = src[position$1++] & 0x3f;
556
606
  units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
557
607
  } else if ((byte1 & 0xf8) === 0xf0) {
558
608
  // 4 bytes
559
- const byte2 = src[position++] & 0x3f;
560
- const byte3 = src[position++] & 0x3f;
561
- const byte4 = src[position++] & 0x3f;
609
+ const byte2 = src[position$1++] & 0x3f;
610
+ const byte3 = src[position$1++] & 0x3f;
611
+ const byte4 = src[position$1++] & 0x3f;
562
612
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
563
613
  if (unit > 0xffff) {
564
614
  unit -= 0x10000;
@@ -585,7 +635,7 @@
585
635
  function readString(source, start, length) {
586
636
  let existingSrc = src;
587
637
  src = source;
588
- position = start;
638
+ position$1 = start;
589
639
  try {
590
640
  return readStringJS(length);
591
641
  } finally {
@@ -624,12 +674,12 @@
624
674
 
625
675
  var fromCharCode = String.fromCharCode;
626
676
  function longStringInJS(length) {
627
- let start = position;
677
+ let start = position$1;
628
678
  let bytes = new Array(length);
629
679
  for (let i = 0; i < length; i++) {
630
- const byte = src[position++];
680
+ const byte = src[position$1++];
631
681
  if ((byte & 0x80) > 0) {
632
- position = start;
682
+ position$1 = start;
633
683
  return
634
684
  }
635
685
  bytes[i] = byte;
@@ -642,131 +692,131 @@
642
692
  if (length === 0)
643
693
  return ''
644
694
  else {
645
- let a = src[position++];
695
+ let a = src[position$1++];
646
696
  if ((a & 0x80) > 1) {
647
- position -= 1;
697
+ position$1 -= 1;
648
698
  return
649
699
  }
650
700
  return fromCharCode(a)
651
701
  }
652
702
  } else {
653
- let a = src[position++];
654
- let b = src[position++];
703
+ let a = src[position$1++];
704
+ let b = src[position$1++];
655
705
  if ((a & 0x80) > 0 || (b & 0x80) > 0) {
656
- position -= 2;
706
+ position$1 -= 2;
657
707
  return
658
708
  }
659
709
  if (length < 3)
660
710
  return fromCharCode(a, b)
661
- let c = src[position++];
711
+ let c = src[position$1++];
662
712
  if ((c & 0x80) > 0) {
663
- position -= 3;
713
+ position$1 -= 3;
664
714
  return
665
715
  }
666
716
  return fromCharCode(a, b, c)
667
717
  }
668
718
  } else {
669
- let a = src[position++];
670
- let b = src[position++];
671
- let c = src[position++];
672
- let d = src[position++];
719
+ let a = src[position$1++];
720
+ let b = src[position$1++];
721
+ let c = src[position$1++];
722
+ let d = src[position$1++];
673
723
  if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
674
- position -= 4;
724
+ position$1 -= 4;
675
725
  return
676
726
  }
677
727
  if (length < 6) {
678
728
  if (length === 4)
679
729
  return fromCharCode(a, b, c, d)
680
730
  else {
681
- let e = src[position++];
731
+ let e = src[position$1++];
682
732
  if ((e & 0x80) > 0) {
683
- position -= 5;
733
+ position$1 -= 5;
684
734
  return
685
735
  }
686
736
  return fromCharCode(a, b, c, d, e)
687
737
  }
688
738
  } else if (length < 8) {
689
- let e = src[position++];
690
- let f = src[position++];
739
+ let e = src[position$1++];
740
+ let f = src[position$1++];
691
741
  if ((e & 0x80) > 0 || (f & 0x80) > 0) {
692
- position -= 6;
742
+ position$1 -= 6;
693
743
  return
694
744
  }
695
745
  if (length < 7)
696
746
  return fromCharCode(a, b, c, d, e, f)
697
- let g = src[position++];
747
+ let g = src[position$1++];
698
748
  if ((g & 0x80) > 0) {
699
- position -= 7;
749
+ position$1 -= 7;
700
750
  return
701
751
  }
702
752
  return fromCharCode(a, b, c, d, e, f, g)
703
753
  } else {
704
- let e = src[position++];
705
- let f = src[position++];
706
- let g = src[position++];
707
- let h = src[position++];
754
+ let e = src[position$1++];
755
+ let f = src[position$1++];
756
+ let g = src[position$1++];
757
+ let h = src[position$1++];
708
758
  if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
709
- position -= 8;
759
+ position$1 -= 8;
710
760
  return
711
761
  }
712
762
  if (length < 10) {
713
763
  if (length === 8)
714
764
  return fromCharCode(a, b, c, d, e, f, g, h)
715
765
  else {
716
- let i = src[position++];
766
+ let i = src[position$1++];
717
767
  if ((i & 0x80) > 0) {
718
- position -= 9;
768
+ position$1 -= 9;
719
769
  return
720
770
  }
721
771
  return fromCharCode(a, b, c, d, e, f, g, h, i)
722
772
  }
723
773
  } else if (length < 12) {
724
- let i = src[position++];
725
- let j = src[position++];
774
+ let i = src[position$1++];
775
+ let j = src[position$1++];
726
776
  if ((i & 0x80) > 0 || (j & 0x80) > 0) {
727
- position -= 10;
777
+ position$1 -= 10;
728
778
  return
729
779
  }
730
780
  if (length < 11)
731
781
  return fromCharCode(a, b, c, d, e, f, g, h, i, j)
732
- let k = src[position++];
782
+ let k = src[position$1++];
733
783
  if ((k & 0x80) > 0) {
734
- position -= 11;
784
+ position$1 -= 11;
735
785
  return
736
786
  }
737
787
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
738
788
  } else {
739
- let i = src[position++];
740
- let j = src[position++];
741
- let k = src[position++];
742
- let l = src[position++];
789
+ let i = src[position$1++];
790
+ let j = src[position$1++];
791
+ let k = src[position$1++];
792
+ let l = src[position$1++];
743
793
  if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
744
- position -= 12;
794
+ position$1 -= 12;
745
795
  return
746
796
  }
747
797
  if (length < 14) {
748
798
  if (length === 12)
749
799
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
750
800
  else {
751
- let m = src[position++];
801
+ let m = src[position$1++];
752
802
  if ((m & 0x80) > 0) {
753
- position -= 13;
803
+ position$1 -= 13;
754
804
  return
755
805
  }
756
806
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
757
807
  }
758
808
  } else {
759
- let m = src[position++];
760
- let n = src[position++];
809
+ let m = src[position$1++];
810
+ let n = src[position$1++];
761
811
  if ((m & 0x80) > 0 || (n & 0x80) > 0) {
762
- position -= 14;
812
+ position$1 -= 14;
763
813
  return
764
814
  }
765
815
  if (length < 15)
766
816
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
767
- let o = src[position++];
817
+ let o = src[position$1++];
768
818
  if ((o & 0x80) > 0) {
769
- position -= 15;
819
+ position$1 -= 15;
770
820
  return
771
821
  }
772
822
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
@@ -777,7 +827,7 @@
777
827
  }
778
828
 
779
829
  function readOnlyJSString() {
780
- let token = src[position++];
830
+ let token = src[position$1++];
781
831
  let length;
782
832
  if (token < 0xc0) {
783
833
  // fixstr
@@ -786,17 +836,17 @@
786
836
  switch(token) {
787
837
  case 0xd9:
788
838
  // str 8
789
- length = src[position++];
839
+ length = src[position$1++];
790
840
  break
791
841
  case 0xda:
792
842
  // str 16
793
- length = dataView.getUint16(position);
794
- position += 2;
843
+ length = dataView.getUint16(position$1);
844
+ position$1 += 2;
795
845
  break
796
846
  case 0xdb:
797
847
  // str 32
798
- length = dataView.getUint32(position);
799
- position += 4;
848
+ length = dataView.getUint32(position$1);
849
+ position$1 += 4;
800
850
  break
801
851
  default:
802
852
  throw new Error('Expected string')
@@ -809,19 +859,19 @@
809
859
  function readBin(length) {
810
860
  return currentUnpackr.copyBuffers ?
811
861
  // specifically use the copying slice (not the node one)
812
- Uint8Array.prototype.slice.call(src, position, position += length) :
813
- src.subarray(position, position += length)
862
+ Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
863
+ src.subarray(position$1, position$1 += length)
814
864
  }
815
865
  function readExt(length) {
816
- let type = src[position++];
866
+ let type = src[position$1++];
817
867
  if (currentExtensions[type]) {
818
868
  let end;
819
- return currentExtensions[type](src.subarray(position, end = (position += length)), (readPosition) => {
820
- position = readPosition;
869
+ return currentExtensions[type](src.subarray(position$1, end = (position$1 += length)), (readPosition) => {
870
+ position$1 = readPosition;
821
871
  try {
822
872
  return read();
823
873
  } finally {
824
- position = end;
874
+ position$1 = end;
825
875
  }
826
876
  })
827
877
  }
@@ -831,22 +881,22 @@
831
881
 
832
882
  var keyCache = new Array(4096);
833
883
  function readKey() {
834
- let length = src[position++];
884
+ let length = src[position$1++];
835
885
  if (length >= 0xa0 && length < 0xc0) {
836
886
  // fixstr, potentially use key cache
837
887
  length = length - 0xa0;
838
- if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
839
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
888
+ if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
889
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
840
890
  else if (!(srcStringEnd == 0 && srcEnd < 180))
841
891
  return readFixedString(length)
842
892
  } else { // not cacheable, go back and do a standard read
843
- position--;
844
- return read()
893
+ position$1--;
894
+ return read().toString()
845
895
  }
846
- let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
896
+ let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
847
897
  let entry = keyCache[key];
848
- let checkPosition = position;
849
- let end = position + length - 3;
898
+ let checkPosition = position$1;
899
+ let end = position$1 + length - 3;
850
900
  let chunk;
851
901
  let i = 0;
852
902
  if (entry && entry.bytes == length) {
@@ -867,11 +917,11 @@
867
917
  }
868
918
  }
869
919
  if (checkPosition === end) {
870
- position = checkPosition;
920
+ position$1 = checkPosition;
871
921
  return entry.string
872
922
  }
873
923
  end -= 3;
874
- checkPosition = position;
924
+ checkPosition = position$1;
875
925
  }
876
926
  entry = [];
877
927
  keyCache[key] = entry;
@@ -895,16 +945,7 @@
895
945
 
896
946
  // the registration of the record definition extension (as "r")
897
947
  const recordDefinition = (id, highByte) => {
898
- let structure;
899
- if (currentUnpackr.freezeData) {
900
- currentUnpackr.freezeData = false;
901
- try {
902
- structure = read();
903
- } finally {
904
- currentUnpackr.freezeData = true;
905
- }
906
- } else
907
- structure = read();
948
+ let structure = read().map(property => property.toString()); // ensure that all keys are strings and that the array is mutable
908
949
  let firstByte = id;
909
950
  if (highByte !== undefined) {
910
951
  id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
@@ -928,10 +969,10 @@
928
969
 
929
970
  currentExtensions[0x69] = (data) => {
930
971
  // id extension (for structured clones)
931
- let id = dataView.getUint32(position - 4);
972
+ let id = dataView.getUint32(position$1 - 4);
932
973
  if (!referenceMap)
933
974
  referenceMap = new Map();
934
- let token = src[position];
975
+ let token = src[position$1];
935
976
  let target;
936
977
  // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
937
978
  // ahead past references to record structure definitions
@@ -951,7 +992,7 @@
951
992
 
952
993
  currentExtensions[0x70] = (data) => {
953
994
  // pointer extension (for structured clones)
954
- let id = dataView.getUint32(position - 4);
995
+ let id = dataView.getUint32(position$1 - 4);
955
996
  let refEntry = referenceMap.get(id);
956
997
  refEntry.used = true;
957
998
  return refEntry.target
@@ -976,14 +1017,14 @@
976
1017
  const TEMP_BUNDLE = [];
977
1018
  currentExtensions[0x62] = (data) => {
978
1019
  let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
979
- let dataPosition = position;
980
- position += dataSize - data.length;
981
- bundledStrings = TEMP_BUNDLE;
982
- bundledStrings = [readOnlyJSString(), readOnlyJSString()];
983
- bundledStrings.position0 = 0;
984
- bundledStrings.position1 = 0;
985
- bundledStrings.postBundlePosition = position;
986
- position = dataPosition;
1020
+ let dataPosition = position$1;
1021
+ position$1 += dataSize - data.length;
1022
+ bundledStrings$1 = TEMP_BUNDLE;
1023
+ bundledStrings$1 = [readOnlyJSString(), readOnlyJSString()];
1024
+ bundledStrings$1.position0 = 0;
1025
+ bundledStrings$1.position1 = 0;
1026
+ bundledStrings$1.postBundlePosition = position$1;
1027
+ position$1 = dataPosition;
987
1028
  return read()
988
1029
  };
989
1030
 
@@ -1005,16 +1046,18 @@
1005
1046
  // registration of bulk record definition?
1006
1047
  // currentExtensions[0x52] = () =>
1007
1048
 
1008
- function saveState(callback) {
1049
+ function saveState$1(callback) {
1009
1050
  if (onSaveState)
1010
1051
  onSaveState();
1011
1052
  let savedSrcEnd = srcEnd;
1012
- let savedPosition = position;
1053
+ let savedPosition = position$1;
1054
+ let savedStringPosition = stringPosition;
1013
1055
  let savedSrcStringStart = srcStringStart;
1014
1056
  let savedSrcStringEnd = srcStringEnd;
1015
1057
  let savedSrcString = srcString;
1058
+ let savedStrings = strings;
1016
1059
  let savedReferenceMap = referenceMap;
1017
- let savedBundledStrings = bundledStrings;
1060
+ let savedBundledStrings = bundledStrings$1;
1018
1061
 
1019
1062
  // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1020
1063
  let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
@@ -1024,12 +1067,14 @@
1024
1067
  let savedSequentialMode = sequentialMode;
1025
1068
  let value = callback();
1026
1069
  srcEnd = savedSrcEnd;
1027
- position = savedPosition;
1070
+ position$1 = savedPosition;
1071
+ stringPosition = savedStringPosition;
1028
1072
  srcStringStart = savedSrcStringStart;
1029
1073
  srcStringEnd = savedSrcStringEnd;
1030
1074
  srcString = savedSrcString;
1075
+ strings = savedStrings;
1031
1076
  referenceMap = savedReferenceMap;
1032
- bundledStrings = savedBundledStrings;
1077
+ bundledStrings$1 = savedBundledStrings;
1033
1078
  src = savedSrc;
1034
1079
  sequentialMode = savedSequentialMode;
1035
1080
  currentStructures = savedStructures;
@@ -1044,37 +1089,60 @@
1044
1089
  currentStructures = null;
1045
1090
  }
1046
1091
 
1092
+ function addExtension$2(extension) {
1093
+ if (extension.unpack)
1094
+ currentExtensions[extension.type] = extension.unpack;
1095
+ else
1096
+ currentExtensions[extension.type] = extension;
1097
+ }
1098
+
1047
1099
  const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1048
1100
  for (let i = 0; i < 256; i++) {
1049
1101
  mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
1050
1102
  }
1051
- var defaultUnpackr = new Unpackr({ useRecords: false });
1103
+ var defaultUnpackr = new Unpackr$1({ useRecords: false });
1104
+ const unpack$1 = defaultUnpackr.unpack;
1105
+ const unpackMultiple$1 = defaultUnpackr.unpackMultiple;
1106
+ defaultUnpackr.unpack;
1107
+ const FLOAT32_OPTIONS = {
1108
+ NEVER: 0,
1109
+ ALWAYS: 1,
1110
+ DECIMAL_ROUND: 3,
1111
+ DECIMAL_FIT: 4
1112
+ };
1113
+ let f32Array = new Float32Array(1);
1114
+ let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
1115
+ function roundFloat32$1(float32Number) {
1116
+ f32Array[0] = float32Number;
1117
+ let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1118
+ return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1119
+ }
1052
1120
  function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1053
- readStruct = updatedReadStruct;
1054
- onLoadedStructures = loadedStructs;
1121
+ readStruct$1 = updatedReadStruct;
1122
+ onLoadedStructures$1 = loadedStructs;
1055
1123
  onSaveState = saveState;
1056
1124
  }
1057
1125
 
1058
- let textEncoder;
1126
+ let textEncoder$1;
1059
1127
  try {
1060
- textEncoder = new TextEncoder();
1128
+ textEncoder$1 = new TextEncoder();
1061
1129
  } catch (error) {}
1062
1130
  let extensions, extensionClasses;
1063
- const hasNodeBuffer = typeof Buffer !== 'undefined';
1064
- const ByteArrayAllocate = hasNodeBuffer ?
1131
+ const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
1132
+ const ByteArrayAllocate = hasNodeBuffer$1 ?
1065
1133
  function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
1066
- const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1067
- const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1134
+ const ByteArray = hasNodeBuffer$1 ? Buffer : Uint8Array;
1135
+ const MAX_BUFFER_SIZE = hasNodeBuffer$1 ? 0x100000000 : 0x7fd00000;
1068
1136
  let target, keysTarget;
1069
1137
  let targetView;
1070
- let position$1 = 0;
1138
+ let position = 0;
1071
1139
  let safeEnd;
1072
- let bundledStrings$1 = null;
1140
+ let bundledStrings = null;
1073
1141
  let writeStructSlots;
1074
1142
  const MAX_BUNDLE_SIZE = 0xf000;
1075
1143
  const hasNonLatin = /[\u0080-\uFFFF]/;
1076
1144
  const RECORD_SYMBOL = Symbol('record-id');
1077
- class Packr extends Unpackr {
1145
+ let Packr$1 = class Packr extends Unpackr$1 {
1078
1146
  constructor(options) {
1079
1147
  super(options);
1080
1148
  this.offset = 0;
@@ -1084,9 +1152,9 @@
1084
1152
  let referenceMap;
1085
1153
  let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1086
1154
  return target.utf8Write(string, position, 0xffffffff)
1087
- } : (textEncoder && textEncoder.encodeInto) ?
1155
+ } : (textEncoder$1 && textEncoder$1.encodeInto) ?
1088
1156
  function(string, position) {
1089
- return textEncoder.encodeInto(string, target.subarray(position)).written
1157
+ return textEncoder$1.encodeInto(string, target.subarray(position)).written
1090
1158
  } : false;
1091
1159
 
1092
1160
  let packr = this;
@@ -1121,25 +1189,25 @@
1121
1189
  this.pack = this.encode = function(value, encodeOptions) {
1122
1190
  if (!target) {
1123
1191
  target = new ByteArrayAllocate(8192);
1124
- targetView = target.dataView = new DataView(target.buffer, 0, 8192);
1125
- position$1 = 0;
1192
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
1193
+ position = 0;
1126
1194
  }
1127
1195
  safeEnd = target.length - 10;
1128
- if (safeEnd - position$1 < 0x800) {
1196
+ if (safeEnd - position < 0x800) {
1129
1197
  // don't start too close to the end,
1130
1198
  target = new ByteArrayAllocate(target.length);
1131
- targetView = target.dataView = new DataView(target.buffer, 0, target.length);
1199
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1132
1200
  safeEnd = target.length - 10;
1133
- position$1 = 0;
1201
+ position = 0;
1134
1202
  } else
1135
- position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1136
- start = position$1;
1203
+ position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1204
+ start = position;
1137
1205
  referenceMap = packr.structuredClone ? new Map() : null;
1138
1206
  if (packr.bundleStrings && typeof value !== 'string') {
1139
- bundledStrings$1 = [];
1140
- bundledStrings$1.size = Infinity; // force a new bundle start on first string
1207
+ bundledStrings = [];
1208
+ bundledStrings.size = Infinity; // force a new bundle start on first string
1141
1209
  } else
1142
- bundledStrings$1 = null;
1210
+ bundledStrings = null;
1143
1211
  structures = packr.structures;
1144
1212
  if (structures) {
1145
1213
  if (structures.uninitialized)
@@ -1180,27 +1248,51 @@
1180
1248
  writeStruct(value);
1181
1249
  else
1182
1250
  pack(value);
1251
+ let lastBundle = bundledStrings;
1252
+ if (bundledStrings)
1253
+ writeBundles(start, pack, 0);
1183
1254
  if (referenceMap && referenceMap.idsToInsert) {
1184
- let incrementPosition = referenceMap.idsToInsert.length * 6;
1185
- if (bundledStrings$1)
1186
- writeBundles(start, pack, incrementPosition);
1187
- position$1 += incrementPosition;
1188
- if (position$1 > safeEnd)
1189
- makeRoom(position$1);
1190
- packr.offset = position$1;
1191
- let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
1255
+ let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1256
+ let i = idsToInsert.length;
1257
+ let incrementPosition = -1;
1258
+ while (lastBundle && i > 0) {
1259
+ let insertionPoint = idsToInsert[--i].offset + start;
1260
+ if (insertionPoint < (lastBundle.stringsPosition + start) && incrementPosition === -1)
1261
+ incrementPosition = 0;
1262
+ if (insertionPoint > (lastBundle.position + start)) {
1263
+ if (incrementPosition >= 0)
1264
+ incrementPosition += 6;
1265
+ } else {
1266
+ if (incrementPosition >= 0) {
1267
+ // update the bundle reference now
1268
+ targetView.setUint32(lastBundle.position + start,
1269
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1270
+ incrementPosition = -1; // reset
1271
+ }
1272
+ lastBundle = lastBundle.previous;
1273
+ i++;
1274
+ }
1275
+ }
1276
+ if (incrementPosition >= 0 && lastBundle) {
1277
+ // update the bundle reference now
1278
+ targetView.setUint32(lastBundle.position + start,
1279
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1280
+ }
1281
+ position += idsToInsert.length * 6;
1282
+ if (position > safeEnd)
1283
+ makeRoom(position);
1284
+ packr.offset = position;
1285
+ let serialized = insertIds(target.subarray(start, position), idsToInsert);
1192
1286
  referenceMap = null;
1193
1287
  return serialized
1194
1288
  }
1195
- if (bundledStrings$1)
1196
- writeBundles(start, pack, 0);
1197
- packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1289
+ packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1198
1290
  if (encodeOptions & REUSE_BUFFER_MODE) {
1199
1291
  target.start = start;
1200
- target.end = position$1;
1292
+ target.end = position;
1201
1293
  return target
1202
1294
  }
1203
- return target.subarray(start, position$1) // position can change if we call pack again in saveStructures, so we get the buffer now
1295
+ return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1204
1296
  } finally {
1205
1297
  if (structures) {
1206
1298
  if (serializationsSinceTransitionRebuild < 10)
@@ -1223,8 +1315,8 @@
1223
1315
  }
1224
1316
  if (hasSharedUpdate && packr.saveStructures) {
1225
1317
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1226
- let returnBuffer = target.subarray(start, position$1);
1227
- let newSharedData = prepareStructures(structures, packr);
1318
+ let returnBuffer = target.subarray(start, position);
1319
+ let newSharedData = prepareStructures$1(structures, packr);
1228
1320
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1229
1321
  // get updated structures and try again if the update failed
1230
1322
  return packr.pack(value)
@@ -1234,44 +1326,47 @@
1234
1326
  }
1235
1327
  }
1236
1328
  if (encodeOptions & RESET_BUFFER_MODE)
1237
- position$1 = start;
1329
+ position = start;
1238
1330
  }
1239
1331
  };
1240
1332
  const pack = (value) => {
1241
- if (position$1 > safeEnd)
1242
- target = makeRoom(position$1);
1333
+ if (position > safeEnd)
1334
+ target = makeRoom(position);
1243
1335
 
1244
1336
  var type = typeof value;
1245
1337
  var length;
1246
1338
  if (type === 'string') {
1247
1339
  let strLength = value.length;
1248
- if (bundledStrings$1 && strLength >= 4 && strLength < 0x1000) {
1249
- if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1340
+ if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
1341
+ if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1250
1342
  let extStart;
1251
- let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1252
- if (position$1 + maxBytes > safeEnd)
1253
- target = makeRoom(position$1 + maxBytes);
1254
- if (bundledStrings$1.position) { // here we use the 0x62 extension to write the last bundle and reserve sapce for the reference pointer to the next/current bundle
1255
- target[position$1] = 0xc8; // ext 16
1256
- position$1 += 3; // reserve for the writing bundle size
1257
- target[position$1++] = 0x62; // 'b'
1258
- extStart = position$1 - start;
1259
- position$1 += 4; // reserve for writing bundle reference
1260
- writeBundles(start, pack); // write the last bundles
1261
- targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1343
+ let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
1344
+ if (position + maxBytes > safeEnd)
1345
+ target = makeRoom(position + maxBytes);
1346
+ let lastBundle;
1347
+ if (bundledStrings.position) { // here we use the 0x62 extension to write the last bundle and reserve space for the reference pointer to the next/current bundle
1348
+ lastBundle = bundledStrings;
1349
+ target[position] = 0xc8; // ext 16
1350
+ position += 3; // reserve for the writing bundle size
1351
+ target[position++] = 0x62; // 'b'
1352
+ extStart = position - start;
1353
+ position += 4; // reserve for writing bundle reference
1354
+ writeBundles(start, pack, 0); // write the last bundles
1355
+ targetView.setUint16(extStart + start - 3, position - start - extStart);
1262
1356
  } else { // here we use the 0x62 extension just to reserve the space for the reference pointer to the bundle (will be updated once the bundle is written)
1263
- target[position$1++] = 0xd6; // fixext 4
1264
- target[position$1++] = 0x62; // 'b'
1265
- extStart = position$1 - start;
1266
- position$1 += 4; // reserve for writing bundle reference
1357
+ target[position++] = 0xd6; // fixext 4
1358
+ target[position++] = 0x62; // 'b'
1359
+ extStart = position - start;
1360
+ position += 4; // reserve for writing bundle reference
1267
1361
  }
1268
- bundledStrings$1 = ['', '']; // create new ones
1269
- bundledStrings$1.size = 0;
1270
- bundledStrings$1.position = extStart;
1362
+ bundledStrings = ['', '']; // create new ones
1363
+ bundledStrings.previous = lastBundle;
1364
+ bundledStrings.size = 0;
1365
+ bundledStrings.position = extStart;
1271
1366
  }
1272
1367
  let twoByte = hasNonLatin.test(value);
1273
- bundledStrings$1[twoByte ? 0 : 1] += value;
1274
- target[position$1++] = 0xc1;
1368
+ bundledStrings[twoByte ? 0 : 1] += value;
1369
+ target[position++] = 0xc1;
1275
1370
  pack(twoByte ? -strLength : strLength);
1276
1371
  return
1277
1372
  }
@@ -1287,11 +1382,11 @@
1287
1382
  headerSize = 5;
1288
1383
  }
1289
1384
  let maxBytes = strLength * 3;
1290
- if (position$1 + maxBytes > safeEnd)
1291
- target = makeRoom(position$1 + maxBytes);
1385
+ if (position + maxBytes > safeEnd)
1386
+ target = makeRoom(position + maxBytes);
1292
1387
 
1293
1388
  if (strLength < 0x40 || !encodeUtf8) {
1294
- let i, c1, c2, strPosition = position$1 + headerSize;
1389
+ let i, c1, c2, strPosition = position + headerSize;
1295
1390
  for (i = 0; i < strLength; i++) {
1296
1391
  c1 = value.charCodeAt(i);
1297
1392
  if (c1 < 0x80) {
@@ -1315,88 +1410,88 @@
1315
1410
  target[strPosition++] = c1 & 0x3f | 0x80;
1316
1411
  }
1317
1412
  }
1318
- length = strPosition - position$1 - headerSize;
1413
+ length = strPosition - position - headerSize;
1319
1414
  } else {
1320
- length = encodeUtf8(value, position$1 + headerSize);
1415
+ length = encodeUtf8(value, position + headerSize);
1321
1416
  }
1322
1417
 
1323
1418
  if (length < 0x20) {
1324
- target[position$1++] = 0xa0 | length;
1419
+ target[position++] = 0xa0 | length;
1325
1420
  } else if (length < 0x100) {
1326
1421
  if (headerSize < 2) {
1327
- target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1422
+ target.copyWithin(position + 2, position + 1, position + 1 + length);
1328
1423
  }
1329
- target[position$1++] = 0xd9;
1330
- target[position$1++] = length;
1424
+ target[position++] = 0xd9;
1425
+ target[position++] = length;
1331
1426
  } else if (length < 0x10000) {
1332
1427
  if (headerSize < 3) {
1333
- target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1428
+ target.copyWithin(position + 3, position + 2, position + 2 + length);
1334
1429
  }
1335
- target[position$1++] = 0xda;
1336
- target[position$1++] = length >> 8;
1337
- target[position$1++] = length & 0xff;
1430
+ target[position++] = 0xda;
1431
+ target[position++] = length >> 8;
1432
+ target[position++] = length & 0xff;
1338
1433
  } else {
1339
1434
  if (headerSize < 5) {
1340
- target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1435
+ target.copyWithin(position + 5, position + 3, position + 3 + length);
1341
1436
  }
1342
- target[position$1++] = 0xdb;
1343
- targetView.setUint32(position$1, length);
1344
- position$1 += 4;
1437
+ target[position++] = 0xdb;
1438
+ targetView.setUint32(position, length);
1439
+ position += 4;
1345
1440
  }
1346
- position$1 += length;
1441
+ position += length;
1347
1442
  } else if (type === 'number') {
1348
1443
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1349
1444
  // positive uint
1350
1445
  if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
1351
- target[position$1++] = value;
1446
+ target[position++] = value;
1352
1447
  } else if (value < 0x100) {
1353
- target[position$1++] = 0xcc;
1354
- target[position$1++] = value;
1448
+ target[position++] = 0xcc;
1449
+ target[position++] = value;
1355
1450
  } else if (value < 0x10000) {
1356
- target[position$1++] = 0xcd;
1357
- target[position$1++] = value >> 8;
1358
- target[position$1++] = value & 0xff;
1451
+ target[position++] = 0xcd;
1452
+ target[position++] = value >> 8;
1453
+ target[position++] = value & 0xff;
1359
1454
  } else {
1360
- target[position$1++] = 0xce;
1361
- targetView.setUint32(position$1, value);
1362
- position$1 += 4;
1455
+ target[position++] = 0xce;
1456
+ targetView.setUint32(position, value);
1457
+ position += 4;
1363
1458
  }
1364
1459
  } else if (value >> 0 === value) { // negative integer
1365
1460
  if (value >= -0x20) {
1366
- target[position$1++] = 0x100 + value;
1461
+ target[position++] = 0x100 + value;
1367
1462
  } else if (value >= -0x80) {
1368
- target[position$1++] = 0xd0;
1369
- target[position$1++] = value + 0x100;
1463
+ target[position++] = 0xd0;
1464
+ target[position++] = value + 0x100;
1370
1465
  } else if (value >= -0x8000) {
1371
- target[position$1++] = 0xd1;
1372
- targetView.setInt16(position$1, value);
1373
- position$1 += 2;
1466
+ target[position++] = 0xd1;
1467
+ targetView.setInt16(position, value);
1468
+ position += 2;
1374
1469
  } else {
1375
- target[position$1++] = 0xd2;
1376
- targetView.setInt32(position$1, value);
1377
- position$1 += 4;
1470
+ target[position++] = 0xd2;
1471
+ targetView.setInt32(position, value);
1472
+ position += 4;
1378
1473
  }
1379
1474
  } else {
1380
1475
  let useFloat32;
1381
1476
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1382
- target[position$1++] = 0xca;
1383
- targetView.setFloat32(position$1, value);
1477
+ target[position++] = 0xca;
1478
+ targetView.setFloat32(position, value);
1384
1479
  let xShifted;
1385
1480
  if (useFloat32 < 4 ||
1386
1481
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1387
- ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1388
- position$1 += 4;
1482
+ ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1483
+ position += 4;
1389
1484
  return
1390
1485
  } else
1391
- position$1--; // move back into position for writing a double
1486
+ position--; // move back into position for writing a double
1392
1487
  }
1393
- target[position$1++] = 0xcb;
1394
- targetView.setFloat64(position$1, value);
1395
- position$1 += 8;
1488
+ target[position++] = 0xcb;
1489
+ targetView.setFloat64(position, value);
1490
+ position += 8;
1396
1491
  }
1397
1492
  } else if (type === 'object') {
1398
1493
  if (!value)
1399
- target[position$1++] = 0xc0;
1494
+ target[position++] = 0xc0;
1400
1495
  else {
1401
1496
  if (referenceMap) {
1402
1497
  let referee = referenceMap.get(value);
@@ -1405,29 +1500,29 @@
1405
1500
  let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1406
1501
  referee.id = idsToInsert.push(referee);
1407
1502
  }
1408
- target[position$1++] = 0xd6; // fixext 4
1409
- target[position$1++] = 0x70; // "p" for pointer
1410
- targetView.setUint32(position$1, referee.id);
1411
- position$1 += 4;
1503
+ target[position++] = 0xd6; // fixext 4
1504
+ target[position++] = 0x70; // "p" for pointer
1505
+ targetView.setUint32(position, referee.id);
1506
+ position += 4;
1412
1507
  return
1413
1508
  } else
1414
- referenceMap.set(value, { offset: position$1 - start });
1509
+ referenceMap.set(value, { offset: position - start });
1415
1510
  }
1416
1511
  let constructor = value.constructor;
1417
1512
  if (constructor === Object) {
1418
1513
  writeObject(value, true);
1419
- } else if (constructor === Array) {
1514
+ } else if (constructor === Array || Array.isArray(value)) {
1420
1515
  length = value.length;
1421
1516
  if (length < 0x10) {
1422
- target[position$1++] = 0x90 | length;
1517
+ target[position++] = 0x90 | length;
1423
1518
  } else if (length < 0x10000) {
1424
- target[position$1++] = 0xdc;
1425
- target[position$1++] = length >> 8;
1426
- target[position$1++] = length & 0xff;
1519
+ target[position++] = 0xdc;
1520
+ target[position++] = length >> 8;
1521
+ target[position++] = length & 0xff;
1427
1522
  } else {
1428
- target[position$1++] = 0xdd;
1429
- targetView.setUint32(position$1, length);
1430
- position$1 += 4;
1523
+ target[position++] = 0xdd;
1524
+ targetView.setUint32(position, length);
1525
+ position += 4;
1431
1526
  }
1432
1527
  for (let i = 0; i < length; i++) {
1433
1528
  pack(value[i]);
@@ -1435,15 +1530,15 @@
1435
1530
  } else if (constructor === Map) {
1436
1531
  length = value.size;
1437
1532
  if (length < 0x10) {
1438
- target[position$1++] = 0x80 | length;
1533
+ target[position++] = 0x80 | length;
1439
1534
  } else if (length < 0x10000) {
1440
- target[position$1++] = 0xde;
1441
- target[position$1++] = length >> 8;
1442
- target[position$1++] = length & 0xff;
1535
+ target[position++] = 0xde;
1536
+ target[position++] = length >> 8;
1537
+ target[position++] = length & 0xff;
1443
1538
  } else {
1444
- target[position$1++] = 0xdf;
1445
- targetView.setUint32(position$1, length);
1446
- position$1 += 4;
1539
+ target[position++] = 0xdf;
1540
+ targetView.setUint32(position, length);
1541
+ position += 4;
1447
1542
  }
1448
1543
  for (let [ key, entryValue ] of value) {
1449
1544
  pack(key);
@@ -1456,16 +1551,16 @@
1456
1551
  let extension = extensions[i];
1457
1552
  if (extension.write) {
1458
1553
  if (extension.type) {
1459
- target[position$1++] = 0xd4; // one byte "tag" extension
1460
- target[position$1++] = extension.type;
1461
- target[position$1++] = 0;
1554
+ target[position++] = 0xd4; // one byte "tag" extension
1555
+ target[position++] = extension.type;
1556
+ target[position++] = 0;
1462
1557
  }
1463
1558
  pack(extension.write.call(this, value));
1464
1559
  return
1465
1560
  }
1466
1561
  let currentTarget = target;
1467
1562
  let currentTargetView = targetView;
1468
- let currentPosition = position$1;
1563
+ let currentPosition = position;
1469
1564
  target = null;
1470
1565
  let result;
1471
1566
  try {
@@ -1473,11 +1568,11 @@
1473
1568
  // restore target and use it
1474
1569
  target = currentTarget;
1475
1570
  currentTarget = null;
1476
- position$1 += size;
1477
- if (position$1 > safeEnd)
1478
- makeRoom(position$1);
1571
+ position += size;
1572
+ if (position > safeEnd)
1573
+ makeRoom(position);
1479
1574
  return {
1480
- target, targetView, position: position$1 - size
1575
+ target, targetView, position: position - size
1481
1576
  }
1482
1577
  }, pack);
1483
1578
  } finally {
@@ -1485,14 +1580,14 @@
1485
1580
  if (currentTarget) {
1486
1581
  target = currentTarget;
1487
1582
  targetView = currentTargetView;
1488
- position$1 = currentPosition;
1583
+ position = currentPosition;
1489
1584
  safeEnd = target.length - 10;
1490
1585
  }
1491
1586
  }
1492
1587
  if (result) {
1493
- if (result.length + position$1 > safeEnd)
1494
- makeRoom(result.length + position$1);
1495
- position$1 = writeExtensionData(result, target, position$1, extension.type);
1588
+ if (result.length + position > safeEnd)
1589
+ makeRoom(result.length + position);
1590
+ position = writeExtensionData(result, target, position, extension.type);
1496
1591
  }
1497
1592
  return
1498
1593
  }
@@ -1502,33 +1597,33 @@
1502
1597
  }
1503
1598
  }
1504
1599
  } else if (type === 'boolean') {
1505
- target[position$1++] = value ? 0xc3 : 0xc2;
1600
+ target[position++] = value ? 0xc3 : 0xc2;
1506
1601
  } else if (type === 'bigint') {
1507
1602
  if (value < (BigInt(1)<<BigInt(63)) && value >= -(BigInt(1)<<BigInt(63))) {
1508
1603
  // use a signed int as long as it fits
1509
- target[position$1++] = 0xd3;
1510
- targetView.setBigInt64(position$1, value);
1604
+ target[position++] = 0xd3;
1605
+ targetView.setBigInt64(position, value);
1511
1606
  } else if (value < (BigInt(1)<<BigInt(64)) && value > 0) {
1512
1607
  // if we can fit an unsigned int, use that
1513
- target[position$1++] = 0xcf;
1514
- targetView.setBigUint64(position$1, value);
1608
+ target[position++] = 0xcf;
1609
+ targetView.setBigUint64(position, value);
1515
1610
  } else {
1516
1611
  // overflow
1517
1612
  if (this.largeBigIntToFloat) {
1518
- target[position$1++] = 0xcb;
1519
- targetView.setFloat64(position$1, Number(value));
1613
+ target[position++] = 0xcb;
1614
+ targetView.setFloat64(position, Number(value));
1520
1615
  } else {
1521
1616
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1522
1617
  }
1523
1618
  }
1524
- position$1 += 8;
1619
+ position += 8;
1525
1620
  } else if (type === 'undefined') {
1526
1621
  if (this.encodeUndefinedAsNil)
1527
- target[position$1++] = 0xc0;
1622
+ target[position++] = 0xc0;
1528
1623
  else {
1529
- target[position$1++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1530
- target[position$1++] = 0;
1531
- target[position$1++] = 0;
1624
+ target[position++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1625
+ target[position++] = 0;
1626
+ target[position++] = 0;
1532
1627
  }
1533
1628
  } else if (type === 'function') {
1534
1629
  pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
@@ -1542,15 +1637,15 @@
1542
1637
  let keys = Object.keys(object);
1543
1638
  let length = keys.length;
1544
1639
  if (length < 0x10) {
1545
- target[position$1++] = 0x80 | length;
1640
+ target[position++] = 0x80 | length;
1546
1641
  } else if (length < 0x10000) {
1547
- target[position$1++] = 0xde;
1548
- target[position$1++] = length >> 8;
1549
- target[position$1++] = length & 0xff;
1642
+ target[position++] = 0xde;
1643
+ target[position++] = length >> 8;
1644
+ target[position++] = length & 0xff;
1550
1645
  } else {
1551
- target[position$1++] = 0xdf;
1552
- targetView.setUint32(position$1, length);
1553
- position$1 += 4;
1646
+ target[position++] = 0xdf;
1647
+ targetView.setUint32(position, length);
1648
+ position += 4;
1554
1649
  }
1555
1650
  let key;
1556
1651
  for (let i = 0; i < length; i++) {
@@ -1559,9 +1654,9 @@
1559
1654
  }
1560
1655
  } :
1561
1656
  (object, safePrototype) => {
1562
- target[position$1++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1563
- let objectOffset = position$1 - start;
1564
- position$1 += 2;
1657
+ target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1658
+ let objectOffset = position - start;
1659
+ position += 2;
1565
1660
  let size = 0;
1566
1661
  for (let key in object) {
1567
1662
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1576,7 +1671,7 @@
1576
1671
  (options.progressiveRecords && !useTwoByteRecords) ? // this is about 2% faster for highly stable structures, since it only requires one for-in loop (but much more expensive when new structure needs to be written)
1577
1672
  (object, safePrototype) => {
1578
1673
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1579
- let objectOffset = position$1++ - start;
1674
+ let objectOffset = position++ - start;
1580
1675
  let wroteKeys;
1581
1676
  for (let key in object) {
1582
1677
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1598,9 +1693,9 @@
1598
1693
  }
1599
1694
  transition = nextTransition;
1600
1695
  }
1601
- if (objectOffset + start + 1 == position$1) {
1696
+ if (objectOffset + start + 1 == position) {
1602
1697
  // first key, so we don't need to insert, we can just write record directly
1603
- position$1--;
1698
+ position--;
1604
1699
  newRecord(transition, keys, newTransitions);
1605
1700
  } else // otherwise we need to insert the record, moving existing data after the record
1606
1701
  insertNewRecord(transition, keys, objectOffset, newTransitions);
@@ -1632,10 +1727,10 @@
1632
1727
  let recordId = transition[RECORD_SYMBOL];
1633
1728
  if (recordId) {
1634
1729
  if (recordId >= 0x60 && useTwoByteRecords) {
1635
- target[position$1++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1636
- target[position$1++] = recordId >> 5;
1730
+ target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1731
+ target[position++] = recordId >> 5;
1637
1732
  } else
1638
- target[position$1++] = recordId;
1733
+ target[position++] = recordId;
1639
1734
  } else {
1640
1735
  newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
1641
1736
  }
@@ -1655,13 +1750,13 @@
1655
1750
  } else // faster handling for smaller buffers
1656
1751
  newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
1657
1752
  let newBuffer = new ByteArrayAllocate(newSize);
1658
- targetView = newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize);
1753
+ targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
1659
1754
  end = Math.min(end, target.length);
1660
1755
  if (target.copy)
1661
1756
  target.copy(newBuffer, 0, start, end);
1662
1757
  else
1663
1758
  newBuffer.set(target.slice(start, end));
1664
- position$1 -= start;
1759
+ position -= start;
1665
1760
  start = 0;
1666
1761
  safeEnd = newBuffer.length - 10;
1667
1762
  return target = newBuffer
@@ -1690,21 +1785,21 @@
1690
1785
  structures.sharedLength = recordId - 0x3f;
1691
1786
  hasSharedUpdate = true;
1692
1787
  if (highByte >= 0) {
1693
- target[position$1++] = (recordId & 0x1f) + 0x60;
1694
- target[position$1++] = highByte;
1788
+ target[position++] = (recordId & 0x1f) + 0x60;
1789
+ target[position++] = highByte;
1695
1790
  } else {
1696
- target[position$1++] = recordId;
1791
+ target[position++] = recordId;
1697
1792
  }
1698
1793
  } else {
1699
1794
  if (highByte >= 0) {
1700
- target[position$1++] = 0xd5; // fixext 2
1701
- target[position$1++] = 0x72; // "r" record defintion extension type
1702
- target[position$1++] = (recordId & 0x1f) + 0x60;
1703
- target[position$1++] = highByte;
1795
+ target[position++] = 0xd5; // fixext 2
1796
+ target[position++] = 0x72; // "r" record defintion extension type
1797
+ target[position++] = (recordId & 0x1f) + 0x60;
1798
+ target[position++] = highByte;
1704
1799
  } else {
1705
- target[position$1++] = 0xd4; // fixext 1
1706
- target[position$1++] = 0x72; // "r" record defintion extension type
1707
- target[position$1++] = recordId;
1800
+ target[position++] = 0xd4; // fixext 1
1801
+ target[position++] = 0x72; // "r" record defintion extension type
1802
+ target[position++] = recordId;
1708
1803
  }
1709
1804
 
1710
1805
  if (newTransitions)
@@ -1718,57 +1813,57 @@
1718
1813
  };
1719
1814
  const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
1720
1815
  let mainTarget = target;
1721
- let mainPosition = position$1;
1816
+ let mainPosition = position;
1722
1817
  let mainSafeEnd = safeEnd;
1723
1818
  let mainStart = start;
1724
1819
  target = keysTarget;
1725
- position$1 = 0;
1820
+ position = 0;
1726
1821
  start = 0;
1727
1822
  if (!target)
1728
1823
  keysTarget = target = new ByteArrayAllocate(8192);
1729
1824
  safeEnd = target.length - 10;
1730
1825
  newRecord(transition, keys, newTransitions);
1731
1826
  keysTarget = target;
1732
- let keysPosition = position$1;
1827
+ let keysPosition = position;
1733
1828
  target = mainTarget;
1734
- position$1 = mainPosition;
1829
+ position = mainPosition;
1735
1830
  safeEnd = mainSafeEnd;
1736
1831
  start = mainStart;
1737
1832
  if (keysPosition > 1) {
1738
- let newEnd = position$1 + keysPosition - 1;
1833
+ let newEnd = position + keysPosition - 1;
1739
1834
  if (newEnd > safeEnd)
1740
1835
  makeRoom(newEnd);
1741
1836
  let insertionPosition = insertionOffset + start;
1742
- target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position$1);
1837
+ target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position);
1743
1838
  target.set(keysTarget.slice(0, keysPosition), insertionPosition);
1744
- position$1 = newEnd;
1839
+ position = newEnd;
1745
1840
  } else {
1746
1841
  target[insertionOffset + start] = keysTarget[0];
1747
1842
  }
1748
1843
  };
1749
1844
  const writeStruct = (object, safePrototype) => {
1750
- let newPosition = writeStructSlots(object, target, position$1, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1845
+ let newPosition = writeStructSlots(object, target, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1751
1846
  if (notifySharedUpdate)
1752
1847
  return hasSharedUpdate = true;
1753
- position$1 = newPosition;
1848
+ position = newPosition;
1754
1849
  if (start > 0) {
1755
1850
  pack(value);
1756
1851
  if (start == 0)
1757
- return { position: position$1, targetView, target }; // indicate the buffer was re-allocated
1852
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1758
1853
  } else
1759
1854
  pack(value);
1760
- return position$1;
1855
+ return position;
1761
1856
  }, this);
1762
1857
  if (newPosition === 0) // bail and go to a msgpack object
1763
1858
  return writeObject(object, true);
1764
- position$1 = newPosition;
1859
+ position = newPosition;
1765
1860
  };
1766
1861
  }
1767
1862
  useBuffer(buffer) {
1768
1863
  // this means we are finished using our own buffer and we can write over it safely
1769
1864
  target = buffer;
1770
1865
  targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1771
- position$1 = 0;
1866
+ position = 0;
1772
1867
  }
1773
1868
  clearSharedData() {
1774
1869
  if (this.structures)
@@ -1776,7 +1871,7 @@
1776
1871
  if (this.typedStructs)
1777
1872
  this.typedStructs = [];
1778
1873
  }
1779
- }
1874
+ };
1780
1875
 
1781
1876
  extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, C1Type ];
1782
1877
  extensions = [{
@@ -1851,7 +1946,7 @@
1851
1946
  if (this.moreTypes)
1852
1947
  writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
1853
1948
  else
1854
- writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
1949
+ writeBuffer(hasNodeBuffer$1 ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
1855
1950
  }
1856
1951
  }, {
1857
1952
  pack(typedArray, allocateForWrite) {
@@ -1955,7 +2050,6 @@
1955
2050
  let nextId;
1956
2051
  let distanceToMove = idsToInsert.length * 6;
1957
2052
  let lastEnd = serialized.length - distanceToMove;
1958
- idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1959
2053
  while (nextId = idsToInsert.pop()) {
1960
2054
  let offset = nextId.offset;
1961
2055
  let id = nextId.id;
@@ -1974,15 +2068,28 @@
1974
2068
  }
1975
2069
 
1976
2070
  function writeBundles(start, pack, incrementPosition) {
1977
- if (bundledStrings$1.length > 0) {
1978
- targetView.setUint32(bundledStrings$1.position + start, position$1 + incrementPosition - bundledStrings$1.position - start);
1979
- let writeStrings = bundledStrings$1;
1980
- bundledStrings$1 = null;
2071
+ if (bundledStrings.length > 0) {
2072
+ targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start);
2073
+ bundledStrings.stringsPosition = position - start;
2074
+ let writeStrings = bundledStrings;
2075
+ bundledStrings = null;
1981
2076
  pack(writeStrings[0]);
1982
2077
  pack(writeStrings[1]);
1983
2078
  }
1984
2079
  }
1985
- function prepareStructures(structures, packr) {
2080
+
2081
+ function addExtension$1(extension) {
2082
+ if (extension.Class) {
2083
+ if (!extension.pack && !extension.write)
2084
+ throw new Error('Extension has no pack or write function')
2085
+ if (extension.pack && !extension.type)
2086
+ throw new Error('Extension has no type (numeric code to identify the extension)')
2087
+ extensionClasses.unshift(extension.Class);
2088
+ extensions.unshift(extension);
2089
+ }
2090
+ addExtension$2(extension);
2091
+ }
2092
+ function prepareStructures$1(structures, packr) {
1986
2093
  structures.isCompatible = (existingStructures) => {
1987
2094
  let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
1988
2095
  if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
@@ -1993,10 +2100,12 @@
1993
2100
  }
1994
2101
  function setWriteStructSlots(writeSlots, makeStructures) {
1995
2102
  writeStructSlots = writeSlots;
1996
- prepareStructures = makeStructures;
2103
+ prepareStructures$1 = makeStructures;
1997
2104
  }
1998
2105
 
1999
- let defaultPackr = new Packr({ useRecords: false });
2106
+ let defaultPackr = new Packr$1({ useRecords: false });
2107
+ const pack$1 = defaultPackr.pack;
2108
+ defaultPackr.pack;
2000
2109
  const REUSE_BUFFER_MODE = 512;
2001
2110
  const RESET_BUFFER_MODE = 1024;
2002
2111
 
@@ -2017,18 +2126,18 @@
2017
2126
  }
2018
2127
 
2019
2128
  let updatedPosition;
2020
- const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
2021
- let textEncoder$1, currentSource;
2129
+ const hasNodeBuffer = typeof Buffer !== 'undefined';
2130
+ let textEncoder, currentSource;
2022
2131
  try {
2023
- textEncoder$1 = new TextEncoder();
2132
+ textEncoder = new TextEncoder();
2024
2133
  } catch (error) {}
2025
- const encodeUtf8 = hasNodeBuffer$1 ? function(target, string, position) {
2134
+ const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
2026
2135
  return target.utf8Write(string, position, 0xffffffff)
2027
- } : (textEncoder$1 && textEncoder$1.encodeInto) ?
2136
+ } : (textEncoder && textEncoder.encodeInto) ?
2028
2137
  function(target, string, position) {
2029
- return textEncoder$1.encodeInto(string, target.subarray(position)).written
2138
+ return textEncoder.encodeInto(string, target.subarray(position)).written
2030
2139
  } : false;
2031
- setWriteStructSlots(writeStruct, prepareStructures$1);
2140
+ setWriteStructSlots(writeStruct, prepareStructures);
2032
2141
  function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
2033
2142
  let typedStructs = packr.typedStructs || (packr.typedStructs = []);
2034
2143
  // note that we rely on pack.js to load stored structures before we get to this point
@@ -2121,9 +2230,9 @@
2121
2230
  case 'string':
2122
2231
  let strLength = value.length;
2123
2232
  refOffset = refPosition - refsStartPosition;
2124
- if ((strLength << 2) + position > safeEnd) {
2233
+ if ((strLength << 2) + refPosition > safeEnd) {
2125
2234
  let lastStart = start;
2126
- target = makeRoom(refPosition);
2235
+ target = makeRoom((strLength << 2) + refPosition);
2127
2236
  targetView = target.dataView;
2128
2237
  position -= lastStart;
2129
2238
  refsStartPosition -= lastStart;
@@ -2403,7 +2512,7 @@
2403
2512
  newTransition.__parent = transition;
2404
2513
  return newTransition;
2405
2514
  }
2406
- function onLoadedStructures$1(sharedData) {
2515
+ function onLoadedStructures(sharedData) {
2407
2516
  if (!(sharedData instanceof Map))
2408
2517
  return sharedData;
2409
2518
  let typed = sharedData.get('typed') || [];
@@ -2441,7 +2550,7 @@
2441
2550
  return named;
2442
2551
  }
2443
2552
  var sourceSymbol = Symbol.for('source');
2444
- function readStruct$1(src, position, srcEnd, unpackr) {
2553
+ function readStruct(src, position, srcEnd, unpackr) {
2445
2554
  let recordId = src[position++] - 0x20;
2446
2555
  if (recordId >= 24) {
2447
2556
  switch(recordId) {
@@ -2712,14 +2821,14 @@
2712
2821
  }
2713
2822
  }
2714
2823
 
2715
- function saveState$1() {
2824
+ function saveState() {
2716
2825
  if (currentSource) {
2717
2826
  currentSource.bytes = Uint8Array.prototype.slice.call(currentSource.bytes, currentSource.position, currentSource.bytesEnd);
2718
2827
  currentSource.position = 0;
2719
2828
  currentSource.bytesEnd = currentSource.bytes.length;
2720
2829
  }
2721
2830
  }
2722
- function prepareStructures$1(structures, packr) {
2831
+ function prepareStructures(structures, packr) {
2723
2832
  if (packr.typedStructs) {
2724
2833
  let structMap = new Map();
2725
2834
  structMap.set('named', structures);
@@ -2736,7 +2845,7 @@
2736
2845
  let typed = existing.get('typed') || [];
2737
2846
  if (typed.length !== lastTypedStructuresLength)
2738
2847
  compatible = false;
2739
- } else if (existing instanceof Array) {
2848
+ } else if (existing instanceof Array || Array.isArray(existing)) {
2740
2849
  if (existing.length !== (packr.lastNamedStructuresLength || 0))
2741
2850
  compatible = false;
2742
2851
  }
@@ -2748,7 +2857,23 @@
2748
2857
  return structures;
2749
2858
  }
2750
2859
 
2751
- setReadStruct(readStruct$1, onLoadedStructures$1, saveState$1);
2860
+ setReadStruct(readStruct, onLoadedStructures, saveState);
2861
+
2862
+ const nativeAccelerationDisabled = process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
2863
+
2864
+ if (!nativeAccelerationDisabled) {
2865
+ let extractor;
2866
+ try {
2867
+ if (typeof require == 'function')
2868
+ extractor = require('msgpackr-extract');
2869
+ else
2870
+ extractor = module.createRequire((document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href))('msgpackr-extract');
2871
+ if (extractor)
2872
+ setExtractor(extractor.extractStrings);
2873
+ } catch (error) {
2874
+ // native module is optional
2875
+ }
2876
+ }
2752
2877
 
2753
2878
  let allSampleData = [];
2754
2879
  for (let i = 1; i < 6; i++) {
@@ -2777,49 +2902,49 @@
2777
2902
  //if (typeof chai === 'undefined') { chai = require('chai') }
2778
2903
  var assert = chai.assert;
2779
2904
  //if (typeof msgpackr === 'undefined') { msgpackr = require('..') }
2780
- var Packr$1 = msgpackr.Packr;
2781
- var Unpackr$1 = msgpackr.Unpackr;
2782
- var unpack = msgpackr.unpack;
2783
- var unpackMultiple = msgpackr.unpackMultiple;
2784
- var roundFloat32 = msgpackr.roundFloat32;
2785
- var pack = msgpackr.pack;
2786
- var DECIMAL_FIT = msgpackr.FLOAT32_OPTIONS.DECIMAL_FIT;
2787
-
2788
- var addExtension = msgpackr.addExtension;
2905
+ var Packr = Packr$1;
2906
+ var Unpackr = Unpackr$1;
2907
+ var unpack = unpack$1;
2908
+ var unpackMultiple = unpackMultiple$1;
2909
+ var roundFloat32 = roundFloat32$1;
2910
+ var pack = pack$1;
2911
+ var DECIMAL_FIT = FLOAT32_OPTIONS.DECIMAL_FIT;
2912
+
2913
+ var addExtension = addExtension$1;
2789
2914
  var zlib = tryRequire('zlib');
2790
- var deflateSync = zlib.deflateSync;
2791
- var inflateSync = zlib.inflateSync;
2792
- var deflateSync = zlib.brotliCompressSync;
2793
- var inflateSync = zlib.brotliDecompressSync;
2794
- var constants = zlib.constants;
2915
+ zlib.deflateSync;
2916
+ zlib.inflateSync;
2917
+ zlib.brotliCompressSync;
2918
+ zlib.brotliDecompressSync;
2919
+ zlib.constants;
2795
2920
 
2796
2921
  var ITERATIONS = 4000;
2797
2922
 
2798
- suite('msgpackr basic tests', function(){
2799
- test('pack/unpack data', function(){
2923
+ suite('msgpackr basic tests', function() {
2924
+ test('pack/unpack data', function () {
2800
2925
  var data = {
2801
2926
  data: [
2802
- { a: 1, name: 'one', type: 'odd', isOdd: true },
2803
- { a: 2, name: 'two', type: 'even'},
2804
- { a: 3, name: 'three', type: 'odd', isOdd: true },
2805
- { a: 4, name: 'four', type: 'even'},
2806
- { a: 5, name: 'five', type: 'odd', isOdd: true },
2807
- { a: 6, name: 'six', type: 'even', isOdd: null }
2927
+ {a: 1, name: 'one', type: 'odd', isOdd: true},
2928
+ {a: 2, name: 'two', type: 'even'},
2929
+ {a: 3, name: 'three', type: 'odd', isOdd: true},
2930
+ {a: 4, name: 'four', type: 'even'},
2931
+ {a: 5, name: 'five', type: 'odd', isOdd: true},
2932
+ {a: 6, name: 'six', type: 'even', isOdd: null}
2808
2933
  ],
2809
2934
  description: 'some names',
2810
2935
  types: ['odd', 'even'],
2811
2936
  convertEnumToNum: [
2812
- { prop: 'test' },
2813
- { prop: 'test' },
2814
- { prop: 'test' },
2815
- { prop: 1 },
2816
- { prop: 2 },
2817
- { prop: [undefined] },
2818
- { prop: null }
2937
+ {prop: 'test'},
2938
+ {prop: 'test'},
2939
+ {prop: 'test'},
2940
+ {prop: 1},
2941
+ {prop: 2},
2942
+ {prop: [undefined]},
2943
+ {prop: null}
2819
2944
  ]
2820
2945
  };
2821
2946
  let structures = [];
2822
- let packr = new Packr$1({ structures });
2947
+ let packr = new Packr({structures});
2823
2948
  var serialized = packr.pack(data);
2824
2949
  serialized = packr.pack(data);
2825
2950
  serialized = packr.pack(data);
@@ -2827,12 +2952,12 @@
2827
2952
  assert.deepEqual(deserialized, data);
2828
2953
  });
2829
2954
 
2830
- test('mixed structures', function(){
2831
- let data1 = { a: 1, b: 2, c: 3 };
2832
- let data2 = { a: 1, b: 2, d: 4 };
2833
- let data3 = { a: 1, b: 2, e: 5 };
2955
+ test('mixed structures', function () {
2956
+ let data1 = {a: 1, b: 2, c: 3};
2957
+ let data2 = {a: 1, b: 2, d: 4};
2958
+ let data3 = {a: 1, b: 2, e: 5};
2834
2959
  let structures = [];
2835
- let packr = new Packr$1({ structures });
2960
+ let packr = new Packr({structures});
2836
2961
  var serialized = packr.pack(data1);
2837
2962
  var deserialized = packr.unpack(serialized);
2838
2963
  assert.deepEqual(deserialized, data1);
@@ -2844,7 +2969,7 @@
2844
2969
  assert.deepEqual(deserialized, data3);
2845
2970
  });
2846
2971
 
2847
- test('mixed array', function(){
2972
+ test('mixed array', function () {
2848
2973
  var data = [
2849
2974
  'one',
2850
2975
  'two',
@@ -2856,31 +2981,34 @@
2856
2981
  'three',
2857
2982
  'three',
2858
2983
  'one', [
2859
- 3, -5, -50, -400,1.3, -5.3, true
2984
+ 3, -5, -50, -400, 1.3, -5.3, true
2860
2985
  ]
2861
2986
  ];
2862
2987
  let structures = [];
2863
- let packr = new Packr$1({ structures });
2988
+ let packr = new Packr({structures});
2864
2989
  var serialized = packr.pack(data);
2865
2990
  var deserialized = packr.unpack(serialized);
2866
2991
  assert.deepEqual(deserialized, data);
2867
2992
  });
2868
2993
 
2869
- test('255 chars', function() {
2994
+ test('255 chars', function () {
2870
2995
  const data = 'RRZG9A6I7xupPeOZhxcOcioFsuhszGOdyDUcbRf4Zef2kdPIfC9RaLO4jTM5JhuZvTsF09fbRHMGtqk7YAgu3vespeTe9l61ziZ6VrMnYu2CamK96wCkmz0VUXyqaiUoTPgzk414LS9yYrd5uh7w18ksJF5SlC2e91rukWvNqAZJjYN3jpkqHNOFchCwFrhbxq2Lrv1kSJPYCx9blRg2hGmYqTbElLTZHv20iNqwZeQbRMgSBPT6vnbCBPnOh1W';
2871
2996
  var serialized = pack(data);
2872
2997
  var deserialized = unpack(serialized);
2873
2998
  assert.equal(deserialized, data);
2874
2999
  });
2875
- test('pack/unpack varying data with random access structures', function() {
3000
+ test('pack/unpack varying data with random access structures', function () {
2876
3001
  let structures = [];
2877
- let packr = new Packr$1({ structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
3002
+ let packr = new Packr({
3003
+ structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
2878
3004
  }, getStructures() {
2879
3005
  console.log('getStructures');
2880
- } });
3006
+ }
3007
+ });
2881
3008
  for (let i = 0; i < 20; i++) {
2882
3009
  let data = {};
2883
- let props = ['foo', 'bar', 'a', 'b', 'c','name', 'age', 'd'];
3010
+ let props = ['foo', 'bar', 'a', 'b', 'c', 'name', 'age', 'd'];
3011
+
2884
3012
  function makeString() {
2885
3013
  let str = '';
2886
3014
  while (random() < 0.9) {
@@ -2888,6 +3016,7 @@
2888
3016
  }
2889
3017
  return str;
2890
3018
  }
3019
+
2891
3020
  for (let i = 0; i < random() * 20; i++) {
2892
3021
  data[props[Math.floor(random() * 8)]] =
2893
3022
  random() < 0.3 ? Math.floor(random() * 400) / 2 :
@@ -2896,7 +3025,7 @@
2896
3025
  var serialized = packr.pack(data);
2897
3026
  var deserialized = packr.unpack(serialized);
2898
3027
  for (let key in deserialized) {
2899
- let a = deserialized[key];
3028
+ deserialized[key];
2900
3029
  }
2901
3030
  assert.deepEqual(deserialized, data);
2902
3031
  }
@@ -2904,25 +3033,37 @@
2904
3033
 
2905
3034
  for (let sampleData of allSampleData) {
2906
3035
  let snippet = JSON.stringify(sampleData).slice(0, 20) + '...';
2907
- test('pack/unpack sample data ' + snippet, function(){
3036
+ test('pack/unpack sample data ' + snippet, function () {
3037
+ var data = sampleData;
3038
+ var serialized = pack(data);
3039
+ var deserialized = unpack(serialized);
3040
+ assert.deepEqual(deserialized, data);
3041
+ var serialized = pack(data);
3042
+ var deserialized = unpack(serialized);
3043
+ assert.deepEqual(deserialized, data);
3044
+ });
3045
+ test('pack/unpack sample data with Uint8Array encoding' + snippet, function () {
2908
3046
  var data = sampleData;
2909
3047
  var serialized = pack(data);
3048
+ serialized = new Uint8Array(serialized);
2910
3049
  var deserialized = unpack(serialized);
2911
3050
  assert.deepEqual(deserialized, data);
2912
3051
  var serialized = pack(data);
2913
3052
  var deserialized = unpack(serialized);
2914
3053
  assert.deepEqual(deserialized, data);
2915
3054
  });
2916
- test('pack/unpack sample data with random access structures ' + snippet, function() {
3055
+ test('pack/unpack sample data with random access structures ' + snippet, function () {
2917
3056
  var data = sampleData;
2918
3057
  let structures = [];
2919
- let packr = new Packr$1({ structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
2920
- }, getStructures() {
2921
- console.log('getStructures');
2922
- } });
3058
+ let packr = new Packr({
3059
+ structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
3060
+ }, getStructures() {
3061
+ console.log('getStructures');
3062
+ }
3063
+ });
2923
3064
  for (let i = 0; i < 20; i++) {
2924
3065
  var serialized = packr.pack(data);
2925
- var deserialized = packr.unpack(serialized, { lazy: true });
3066
+ var deserialized = packr.unpack(serialized, {lazy: true});
2926
3067
  var copied = {};
2927
3068
  for (let key in deserialized) {
2928
3069
  copied[key] = deserialized[key];
@@ -2930,26 +3071,26 @@
2930
3071
  assert.deepEqual(copied, data);
2931
3072
  }
2932
3073
  });
2933
- test('pack/unpack sample data with bundled strings ' + snippet, function(){
3074
+ test('pack/unpack sample data with bundled strings ' + snippet, function () {
2934
3075
  var data = sampleData;
2935
- let packr = new Packr$1({ /*structures,*/ useRecords: false, bundleStrings: true });
3076
+ let packr = new Packr({ /*structures,*/ useRecords: false, bundleStrings: true});
2936
3077
  var serialized = packr.pack(data);
2937
3078
  var deserialized = packr.unpack(serialized);
2938
3079
  assert.deepEqual(deserialized, data);
2939
3080
  });
2940
3081
  }
2941
3082
 
2942
- test('pack/unpack empty data with bundled strings', function(){
3083
+ test('pack/unpack empty data with bundled strings', function () {
2943
3084
  var data = {};
2944
- let packr = new Packr$1({ bundleStrings: true });
3085
+ let packr = new Packr({bundleStrings: true});
2945
3086
  var serialized = packr.pack(data);
2946
3087
  var deserialized = packr.unpack(serialized);
2947
3088
  assert.deepEqual(deserialized, data);
2948
3089
  });
2949
- test('pack/unpack sequential data', function(){
2950
- var data = { foo:1, bar: 2 };
2951
- let packr = new Packr$1({ sequential: true });
2952
- let unpackr = new Unpackr$1({ sequential: true });
3090
+ test('pack/unpack sequential data', function () {
3091
+ var data = {foo: 1, bar: 2};
3092
+ let packr = new Packr({sequential: true});
3093
+ let unpackr = new Unpackr({sequential: true});
2953
3094
  var serialized = packr.pack(data);
2954
3095
  var deserialized = unpackr.unpack(serialized);
2955
3096
  assert.deepEqual(deserialized, data);
@@ -2957,6 +3098,22 @@
2957
3098
  var deserialized = unpackr.unpack(serialized);
2958
3099
  assert.deepEqual(deserialized, data);
2959
3100
  });
3101
+ test('pack/unpack with bundled strings and sequential', function () {
3102
+ const options = {
3103
+ bundleStrings: true,
3104
+ sequential: true,
3105
+ };
3106
+
3107
+ const packer = new Packr(options);
3108
+ const unpacker = new Packr(options);
3109
+
3110
+ const data = {data: 42}; // key length >= 4
3111
+
3112
+ unpacker.unpackMultiple(Buffer.concat([
3113
+ packer.pack(data),
3114
+ packer.pack(data)
3115
+ ]));
3116
+ });
2960
3117
  if (typeof Buffer != 'undefined')
2961
3118
  test('replace data', function(){
2962
3119
  var data1 = {
@@ -3010,7 +3167,7 @@
3010
3167
  prop2: 'more string',
3011
3168
  num: 3,
3012
3169
  };
3013
- let packr = new Packr$1();
3170
+ let packr = new Packr();
3014
3171
  addExtension({
3015
3172
  Class: Extended,
3016
3173
  type: 11,
@@ -3064,7 +3221,7 @@
3064
3221
  prop2: 'more string',
3065
3222
  num: 3,
3066
3223
  };
3067
- let packr = new Packr$1();
3224
+ new Packr();
3068
3225
  addExtension({
3069
3226
  Class: Extended,
3070
3227
  type: 12,
@@ -3088,7 +3245,7 @@
3088
3245
  var decoded = unpack(pack(objectWithProto));
3089
3246
  assert(!decoded.foo);
3090
3247
  var objectsWithProto = [objectWithProto, objectWithProto, objectWithProto, objectWithProto, objectWithProto, objectWithProto];
3091
- let packr = new Packr$1();
3248
+ let packr = new Packr();
3092
3249
  var decoded = packr.unpack(packr.pack(objectsWithProto));
3093
3250
  for (let object of decoded) {
3094
3251
  assert(!decoded.foo);
@@ -3120,7 +3277,7 @@
3120
3277
  object.children[1] = object;
3121
3278
  object.children[2] = object.children[0];
3122
3279
  object.childrenAgain = object.children;
3123
- let packr = new Packr$1({
3280
+ let packr = new Packr({
3124
3281
  moreTypes: true,
3125
3282
  structuredClone: true,
3126
3283
  });
@@ -3145,7 +3302,7 @@
3145
3302
  float32Array: fa,
3146
3303
  uint16Array: new Uint16Array([3,4])
3147
3304
  };
3148
- let packr = new Packr$1({
3305
+ let packr = new Packr({
3149
3306
  moreTypes: true,
3150
3307
  structuredClone: true,
3151
3308
  });
@@ -3161,22 +3318,46 @@
3161
3318
  assert.equal(deserialized.uint16Array[0], 3);
3162
3319
  assert.equal(deserialized.uint16Array[1], 4);
3163
3320
  });
3321
+ test('big bundledStrings', function() {
3322
+ const MSGPACK_OPTIONS = {bundleStrings: true};
3323
+ const packer = new Packr(MSGPACK_OPTIONS);
3324
+ const unpacker = new Unpackr(MSGPACK_OPTIONS);
3325
+
3326
+ const payload = {
3327
+ output: [
3328
+ {
3329
+ url: 'https://www.example.com/',
3330
+ },
3331
+ ],
3332
+ };
3164
3333
 
3334
+ for (let i = 0; i < 10000; i++) {
3335
+ payload.output.push(payload.output[0]);
3336
+ }
3337
+ let deserialized = unpacker.unpack(packer.pack(payload));
3338
+ assert.equal(deserialized.output[0].url, payload.output[0].url);
3339
+ });
3165
3340
  test('structured clone with bundled strings', function() {
3166
- const packer = new Packr$1({
3341
+ const packer = new Packr({
3167
3342
  structuredClone: true, // both options must be enabled
3168
3343
  bundleStrings: true,
3169
3344
  });
3170
3345
 
3171
3346
  const v = {};
3172
3347
 
3173
- const shared = {
3348
+ let shared = {
3174
3349
  name1: v,
3175
- name2: v, // one key has to be named `data`
3350
+ name2: v,
3176
3351
  };
3177
3352
 
3178
3353
  let deserialized = packer.unpack(packer.pack(shared));
3179
3354
  assert.equal(deserialized.name1, deserialized.name2);
3355
+
3356
+ shared = {};
3357
+ shared.aaaa = shared; // key length >= 4
3358
+
3359
+ deserialized = packer.unpack(packer.pack(shared));
3360
+ assert.equal(deserialized.aaaa, deserialized);
3180
3361
  });
3181
3362
 
3182
3363
  test('object without prototype', function(){
@@ -3188,10 +3369,10 @@
3188
3369
  });
3189
3370
 
3190
3371
  test('separate instances', function() {
3191
- const packr = new Packr$1({
3372
+ const packr = new Packr({
3192
3373
  structures: [['m', 'e'], ['action', 'share']]
3193
3374
  });
3194
- const packr2 = new Packr$1({
3375
+ const packr2 = new Packr({
3195
3376
  structures: [['m', 'e'], ['action', 'share']]
3196
3377
  });
3197
3378
  let packed = packr.pack([{m: 1, e: 2}, {action: 3, share: 4}]);
@@ -3206,7 +3387,7 @@
3206
3387
  }
3207
3388
  let structures = [];
3208
3389
  let savedStructures;
3209
- let packr = new Packr$1({
3390
+ let packr = new Packr({
3210
3391
  structures,
3211
3392
  saveStructures(structures) {
3212
3393
  savedStructures = structures;
@@ -3217,7 +3398,7 @@
3217
3398
  var deserialized = packr.unpack(serializedWith32);
3218
3399
  assert.deepEqual(deserialized, data);
3219
3400
  structures = structures.slice(0, 32);
3220
- packr = new Packr$1({
3401
+ packr = new Packr({
3221
3402
  structures,
3222
3403
  maxSharedStructures: 100,
3223
3404
  saveStructures(structures) {
@@ -3227,7 +3408,7 @@
3227
3408
  deserialized = packr.unpack(serializedWith32);
3228
3409
  assert.deepEqual(deserialized, data);
3229
3410
  structures = structures.slice(0, 32);
3230
- packr = new Packr$1({
3411
+ packr = new Packr({
3231
3412
  structures,
3232
3413
  maxSharedStructures: 100,
3233
3414
  saveStructures(structures) {
@@ -3253,7 +3434,7 @@
3253
3434
  structures.push(['a' + i]);
3254
3435
  }
3255
3436
  const structures2 = [...structures];
3256
- const packr = new Packr$1({
3437
+ const packr = new Packr({
3257
3438
  getStructures() {
3258
3439
  return structures
3259
3440
  },
@@ -3261,7 +3442,7 @@
3261
3442
  },
3262
3443
  maxSharedStructures: 100
3263
3444
  });
3264
- const packr2 = new Packr$1({
3445
+ const packr2 = new Packr({
3265
3446
  getStructures() {
3266
3447
  return structures2
3267
3448
  },
@@ -3311,7 +3492,7 @@
3311
3492
  ancient: new Date(-3532219539133),
3312
3493
  invalidDate: new Date('invalid')
3313
3494
  };
3314
- let packr = new Packr$1();
3495
+ let packr = new Packr();
3315
3496
  var serialized = packr.pack(data);
3316
3497
  var deserialized = packr.unpack(serialized);
3317
3498
  assert.equal(deserialized.map.get(4), 'four');
@@ -3333,7 +3514,7 @@
3333
3514
  date: new Date(1532219539011),
3334
3515
  invalidDate: new Date('invalid')
3335
3516
  };
3336
- let packr = new Packr$1({
3517
+ let packr = new Packr({
3337
3518
  mapsAsObjects: true,
3338
3519
  useTimestamp32: true,
3339
3520
  onInvalidDate: () => 'Custom invalid date'
@@ -3389,7 +3570,7 @@
3389
3570
  c: 0.00000000000352501,
3390
3571
  d: 3252.77,
3391
3572
  };
3392
- let packr = new Packr$1({
3573
+ let packr = new Packr({
3393
3574
  useFloat32: DECIMAL_FIT
3394
3575
  });
3395
3576
  var serialized = packr.pack(data);
@@ -3397,17 +3578,50 @@
3397
3578
  var deserialized = packr.unpack(serialized);
3398
3579
  assert.deepEqual(deserialized, data);
3399
3580
  });
3581
+ test('int64/uint64 should be bigints by default', function() {
3582
+ var data = {
3583
+ a: 325283295382932843n
3584
+ };
3585
+
3586
+ let packr = new Packr();
3587
+ var serialized = packr.pack(data);
3588
+ var deserialized = packr.unpack(serialized);
3589
+ assert.deepEqual(deserialized.a, 325283295382932843n);
3590
+ });
3400
3591
  test('bigint to float', function() {
3401
3592
  var data = {
3402
3593
  a: 325283295382932843n
3403
3594
  };
3404
- let packr = new Packr$1({
3595
+ let packr = new Packr({
3596
+ int64AsType: 'number'
3597
+ });
3598
+ var serialized = packr.pack(data);
3599
+ var deserialized = packr.unpack(serialized);
3600
+ assert.deepEqual(deserialized.a, 325283295382932843);
3601
+ });
3602
+ test('int64AsNumber compatibility', function() {
3603
+ // https://github.com/kriszyp/msgpackr/pull/85
3604
+ var data = {
3605
+ a: 325283295382932843n
3606
+ };
3607
+ let packr = new Packr({
3405
3608
  int64AsNumber: true
3406
3609
  });
3407
3610
  var serialized = packr.pack(data);
3408
3611
  var deserialized = packr.unpack(serialized);
3409
3612
  assert.deepEqual(deserialized.a, 325283295382932843);
3410
3613
  });
3614
+ test('bigint to string', function() {
3615
+ var data = {
3616
+ a: 325283295382932843n,
3617
+ };
3618
+ let packr = new Packr({
3619
+ int64AsType: 'string'
3620
+ });
3621
+ var serialized = packr.pack(data);
3622
+ var deserialized = packr.unpack(serialized);
3623
+ assert.deepEqual(deserialized.a, '325283295382932843');
3624
+ });
3411
3625
  test('numbers', function(){
3412
3626
  var data = {
3413
3627
  bigEncodable: 48978578104322,
@@ -3440,7 +3654,7 @@
3440
3654
  tooBig: 2n**66n
3441
3655
  };
3442
3656
  assert.throws(function(){ serialized = pack(tooBigInt); });
3443
- let packr = new Packr$1({
3657
+ let packr = new Packr({
3444
3658
  largeBigIntToFloat: true
3445
3659
  });
3446
3660
  serialized = packr.pack(tooBigInt);
@@ -3507,14 +3721,14 @@
3507
3721
  var serialized = JSON.stringify(data);
3508
3722
  console.log('JSON size', serialized.length);
3509
3723
  for (var i = 0; i < ITERATIONS; i++) {
3510
- var deserialized = JSON.parse(serialized);
3724
+ JSON.parse(serialized);
3511
3725
  }
3512
3726
  });
3513
3727
  test('performance JSON.stringify', function() {
3514
3728
  var data = sampleData;
3515
3729
  this.timeout(10000);
3516
3730
  for (var i = 0; i < ITERATIONS; i++) {
3517
- var serialized = JSON.stringify(data);
3731
+ JSON.stringify(data);
3518
3732
  }
3519
3733
  });
3520
3734
  test('performance unpack', function() {
@@ -3523,29 +3737,29 @@
3523
3737
  let structures = [];
3524
3738
  var serialized = pack(data);
3525
3739
  console.log('MessagePack size', serialized.length);
3526
- let packr = new Packr$1({ structures, bundleStrings: false });
3740
+ let packr = new Packr({ structures, bundleStrings: false });
3527
3741
  var serialized = packr.pack(data);
3528
3742
  console.log('msgpackr w/ record ext size', serialized.length);
3529
3743
  for (var i = 0; i < ITERATIONS; i++) {
3530
- var deserialized = packr.unpack(serialized);
3744
+ packr.unpack(serialized);
3531
3745
  }
3532
3746
  });
3533
3747
  test('performance pack', function() {
3534
3748
  var data = sampleData;
3535
3749
  this.timeout(10000);
3536
3750
  let structures = [];
3537
- let packr = new Packr$1({ structures, bundleStrings: false });
3751
+ let packr = new Packr({ structures, bundleStrings: false });
3538
3752
  let buffer = typeof Buffer != 'undefined' ? Buffer.alloc(0x10000) : new Uint8Array(0x10000);
3539
3753
 
3540
3754
  for (var i = 0; i < ITERATIONS; i++) {
3541
3755
  //serialized = pack(data, { shared: sharedStructure })
3542
3756
  packr.useBuffer(buffer);
3543
- var serialized = packr.pack(data);
3757
+ packr.pack(data);
3544
3758
  //var serializedGzip = deflateSync(serialized)
3545
3759
  }
3546
3760
  //console.log('serialized', serialized.length, global.propertyComparisons)
3547
3761
  });
3548
3762
  });
3549
3763
 
3550
- }(msgpackr, chai, inspector, fs));
3764
+ })(chai, null, module, fs);
3551
3765
  //# sourceMappingURL=test.js.map