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/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
3
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = global || self, factory(global.msgpackr = {}));
5
- }(this, (function (exports) { 'use strict';
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.msgpackr = {}));
5
+ })(this, (function (exports) { 'use strict';
6
6
 
7
7
  var decoder;
8
8
  try {
@@ -10,13 +10,13 @@
10
10
  } catch(error) {}
11
11
  var src;
12
12
  var srcEnd;
13
- var position = 0;
13
+ var position$1 = 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;
@@ -30,6 +30,7 @@
30
30
  var sequentialMode = false;
31
31
  var inlineObjectReadThreshold = 2;
32
32
  var readStruct;
33
+ // no-eval build
33
34
  try {
34
35
  new Function('');
35
36
  } catch(error) {
@@ -56,6 +57,9 @@
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
  }
@@ -69,14 +73,14 @@
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
  }
77
81
  srcStringEnd = 0;
78
82
  srcString = null;
79
- bundledStrings = null;
83
+ bundledStrings$1 = null;
80
84
  src = source;
81
85
  // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
82
86
  // technique for getting data from a database where it can be copied into an existing buffer instead of creating
@@ -113,8 +117,8 @@
113
117
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
114
118
  if (forEach) {
115
119
  forEach(value);
116
- while(position < size) {
117
- lastPosition = position;
120
+ while(position$1 < size) {
121
+ lastPosition = position$1;
118
122
  if (forEach(checkedRead()) === false) {
119
123
  return
120
124
  }
@@ -122,8 +126,8 @@
122
126
  }
123
127
  else {
124
128
  values = [ value ];
125
- while(position < size) {
126
- lastPosition = position;
129
+ while(position$1 < size) {
130
+ lastPosition = position$1;
127
131
  values.push(checkedRead());
128
132
  }
129
133
  return values
@@ -175,18 +179,20 @@
175
179
  currentStructures.length = sharedLength;
176
180
  }
177
181
  let result;
178
- if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
179
- result = readStruct(src, position, srcEnd, currentUnpackr);
182
+ if (currentUnpackr.randomAccessStructure && src[position$1] < 0x40 && src[position$1] >= 0x20 && readStruct) {
183
+ result = readStruct(src, position$1, srcEnd, currentUnpackr);
180
184
  src = null; // dispose of this so that recursive unpack calls don't save state
181
185
  if (!(options && options.lazy) && result)
182
186
  result = result.toJSON();
183
- position = srcEnd;
187
+ position$1 = srcEnd;
184
188
  } else
185
189
  result = read();
186
- if (bundledStrings) // bundled strings to skip past
187
- position = bundledStrings.postBundlePosition;
190
+ if (bundledStrings$1) { // bundled strings to skip past
191
+ position$1 = bundledStrings$1.postBundlePosition;
192
+ bundledStrings$1 = null;
193
+ }
188
194
 
189
- if (position == srcEnd) {
195
+ if (position$1 == srcEnd) {
190
196
  // finished reading this source, cleanup references
191
197
  if (currentStructures && currentStructures.restoreStructures)
192
198
  restoreStructures();
@@ -194,7 +200,7 @@
194
200
  src = null;
195
201
  if (referenceMap)
196
202
  referenceMap = null;
197
- } else if (position > srcEnd) {
203
+ } else if (position$1 > srcEnd) {
198
204
  // over read
199
205
  throw new Error('Unexpected end of MessagePack data')
200
206
  } else if (!sequentialMode) {
@@ -206,7 +212,7 @@
206
212
  if (currentStructures && currentStructures.restoreStructures)
207
213
  restoreStructures();
208
214
  clearSource();
209
- if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
215
+ if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
210
216
  error.incomplete = true;
211
217
  }
212
218
  throw error
@@ -221,7 +227,7 @@
221
227
  }
222
228
 
223
229
  function read() {
224
- let token = src[position++];
230
+ let token = src[position$1++];
225
231
  if (token < 0xa0) {
226
232
  if (token < 0x80) {
227
233
  if (token < 0x40)
@@ -269,8 +275,8 @@
269
275
  } else if (token < 0xc0) {
270
276
  // fixstr
271
277
  let length = token - 0xa0;
272
- if (srcStringEnd >= position) {
273
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
278
+ if (srcStringEnd >= position$1) {
279
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
274
280
  }
275
281
  if (srcStringEnd == 0 && srcEnd < 140) {
276
282
  // for small blocks, avoiding the overhead of the extract call is helpful
@@ -284,124 +290,128 @@
284
290
  switch (token) {
285
291
  case 0xc0: return null
286
292
  case 0xc1:
287
- if (bundledStrings) {
293
+ if (bundledStrings$1) {
288
294
  value = read(); // followed by the length of the string in characters (not bytes!)
289
295
  if (value > 0)
290
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
296
+ return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
291
297
  else
292
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
298
+ return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
293
299
  }
294
300
  return C1; // "never-used", return special object to denote that
295
301
  case 0xc2: return false
296
302
  case 0xc3: return true
297
303
  case 0xc4:
298
304
  // bin 8
299
- value = src[position++];
305
+ value = src[position$1++];
300
306
  if (value === undefined)
301
307
  throw new Error('Unexpected end of buffer')
302
308
  return readBin(value)
303
309
  case 0xc5:
304
310
  // bin 16
305
- value = dataView.getUint16(position);
306
- position += 2;
311
+ value = dataView.getUint16(position$1);
312
+ position$1 += 2;
307
313
  return readBin(value)
308
314
  case 0xc6:
309
315
  // bin 32
310
- value = dataView.getUint32(position);
311
- position += 4;
316
+ value = dataView.getUint32(position$1);
317
+ position$1 += 4;
312
318
  return readBin(value)
313
319
  case 0xc7:
314
320
  // ext 8
315
- return readExt(src[position++])
321
+ return readExt(src[position$1++])
316
322
  case 0xc8:
317
323
  // ext 16
318
- value = dataView.getUint16(position);
319
- position += 2;
324
+ value = dataView.getUint16(position$1);
325
+ position$1 += 2;
320
326
  return readExt(value)
321
327
  case 0xc9:
322
328
  // ext 32
323
- value = dataView.getUint32(position);
324
- position += 4;
329
+ value = dataView.getUint32(position$1);
330
+ position$1 += 4;
325
331
  return readExt(value)
326
332
  case 0xca:
327
- value = dataView.getFloat32(position);
333
+ value = dataView.getFloat32(position$1);
328
334
  if (currentUnpackr.useFloat32 > 2) {
329
335
  // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
330
- let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
331
- position += 4;
336
+ let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
337
+ position$1 += 4;
332
338
  return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
333
339
  }
334
- position += 4;
340
+ position$1 += 4;
335
341
  return value
336
342
  case 0xcb:
337
- value = dataView.getFloat64(position);
338
- position += 8;
343
+ value = dataView.getFloat64(position$1);
344
+ position$1 += 8;
339
345
  return value
340
346
  // uint handlers
341
347
  case 0xcc:
342
- return src[position++]
348
+ return src[position$1++]
343
349
  case 0xcd:
344
- value = dataView.getUint16(position);
345
- position += 2;
350
+ value = dataView.getUint16(position$1);
351
+ position$1 += 2;
346
352
  return value
347
353
  case 0xce:
348
- value = dataView.getUint32(position);
349
- position += 4;
354
+ value = dataView.getUint32(position$1);
355
+ position$1 += 4;
350
356
  return value
351
357
  case 0xcf:
352
- if (currentUnpackr.int64AsNumber) {
353
- value = dataView.getUint32(position) * 0x100000000;
354
- value += dataView.getUint32(position + 4);
358
+ if (currentUnpackr.int64AsType === 'number') {
359
+ value = dataView.getUint32(position$1) * 0x100000000;
360
+ value += dataView.getUint32(position$1 + 4);
361
+ } else if (currentUnpackr.int64AsType === 'string') {
362
+ value = dataView.getBigUint64(position$1).toString();
355
363
  } else
356
- value = dataView.getBigUint64(position);
357
- position += 8;
364
+ value = dataView.getBigUint64(position$1);
365
+ position$1 += 8;
358
366
  return value
359
367
 
360
368
  // int handlers
361
369
  case 0xd0:
362
- return dataView.getInt8(position++)
370
+ return dataView.getInt8(position$1++)
363
371
  case 0xd1:
364
- value = dataView.getInt16(position);
365
- position += 2;
372
+ value = dataView.getInt16(position$1);
373
+ position$1 += 2;
366
374
  return value
367
375
  case 0xd2:
368
- value = dataView.getInt32(position);
369
- position += 4;
376
+ value = dataView.getInt32(position$1);
377
+ position$1 += 4;
370
378
  return value
371
379
  case 0xd3:
372
- if (currentUnpackr.int64AsNumber) {
373
- value = dataView.getInt32(position) * 0x100000000;
374
- value += dataView.getUint32(position + 4);
380
+ if (currentUnpackr.int64AsType === 'number') {
381
+ value = dataView.getInt32(position$1) * 0x100000000;
382
+ value += dataView.getUint32(position$1 + 4);
383
+ } else if (currentUnpackr.int64AsType === 'string') {
384
+ value = dataView.getBigInt64(position$1).toString();
375
385
  } else
376
- value = dataView.getBigInt64(position);
377
- position += 8;
386
+ value = dataView.getBigInt64(position$1);
387
+ position$1 += 8;
378
388
  return value
379
389
 
380
390
  case 0xd4:
381
391
  // fixext 1
382
- value = src[position++];
392
+ value = src[position$1++];
383
393
  if (value == 0x72) {
384
- return recordDefinition(src[position++] & 0x3f)
394
+ return recordDefinition(src[position$1++] & 0x3f)
385
395
  } else {
386
396
  let extension = currentExtensions[value];
387
397
  if (extension) {
388
398
  if (extension.read) {
389
- position++; // skip filler byte
399
+ position$1++; // skip filler byte
390
400
  return extension.read(read())
391
401
  } else if (extension.noBuffer) {
392
- position++; // skip filler byte
402
+ position$1++; // skip filler byte
393
403
  return extension()
394
404
  } else
395
- return extension(src.subarray(position, ++position))
405
+ return extension(src.subarray(position$1, ++position$1))
396
406
  } else
397
407
  throw new Error('Unknown extension ' + value)
398
408
  }
399
409
  case 0xd5:
400
410
  // fixext 2
401
- value = src[position];
411
+ value = src[position$1];
402
412
  if (value == 0x72) {
403
- position++;
404
- return recordDefinition(src[position++] & 0x3f, src[position++])
413
+ position$1++;
414
+ return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
405
415
  } else
406
416
  return readExt(2)
407
417
  case 0xd6:
@@ -415,46 +425,46 @@
415
425
  return readExt(16)
416
426
  case 0xd9:
417
427
  // str 8
418
- value = src[position++];
419
- if (srcStringEnd >= position) {
420
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
428
+ value = src[position$1++];
429
+ if (srcStringEnd >= position$1) {
430
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
421
431
  }
422
432
  return readString8(value)
423
433
  case 0xda:
424
434
  // str 16
425
- value = dataView.getUint16(position);
426
- position += 2;
427
- if (srcStringEnd >= position) {
428
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
435
+ value = dataView.getUint16(position$1);
436
+ position$1 += 2;
437
+ if (srcStringEnd >= position$1) {
438
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
429
439
  }
430
440
  return readString16(value)
431
441
  case 0xdb:
432
442
  // str 32
433
- value = dataView.getUint32(position);
434
- position += 4;
435
- if (srcStringEnd >= position) {
436
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
443
+ value = dataView.getUint32(position$1);
444
+ position$1 += 4;
445
+ if (srcStringEnd >= position$1) {
446
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
437
447
  }
438
448
  return readString32(value)
439
449
  case 0xdc:
440
450
  // array 16
441
- value = dataView.getUint16(position);
442
- position += 2;
451
+ value = dataView.getUint16(position$1);
452
+ position$1 += 2;
443
453
  return readArray(value)
444
454
  case 0xdd:
445
455
  // array 32
446
- value = dataView.getUint32(position);
447
- position += 4;
456
+ value = dataView.getUint32(position$1);
457
+ position$1 += 4;
448
458
  return readArray(value)
449
459
  case 0xde:
450
460
  // map 16
451
- value = dataView.getUint16(position);
452
- position += 2;
461
+ value = dataView.getUint16(position$1);
462
+ position$1 += 2;
453
463
  return readMap(value)
454
464
  case 0xdf:
455
465
  // map 32
456
- value = dataView.getUint32(position);
457
- position += 4;
466
+ value = dataView.getUint32(position$1);
467
+ position$1 += 4;
458
468
  return readMap(value)
459
469
  default: // negative int
460
470
  if (token >= 0xe0)
@@ -500,7 +510,7 @@
500
510
 
501
511
  const createSecondByteReader = (firstId, read0) => {
502
512
  return function() {
503
- let highByte = src[position++];
513
+ let highByte = src[position$1++];
504
514
  if (highByte === 0)
505
515
  return read0()
506
516
  let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
@@ -535,29 +545,29 @@
535
545
  return result
536
546
  }
537
547
  if (length > 64 && decoder)
538
- return decoder.decode(src.subarray(position, position += length))
539
- const end = position + length;
548
+ return decoder.decode(src.subarray(position$1, position$1 += length))
549
+ const end = position$1 + length;
540
550
  const units = [];
541
551
  result = '';
542
- while (position < end) {
543
- const byte1 = src[position++];
552
+ while (position$1 < end) {
553
+ const byte1 = src[position$1++];
544
554
  if ((byte1 & 0x80) === 0) {
545
555
  // 1 byte
546
556
  units.push(byte1);
547
557
  } else if ((byte1 & 0xe0) === 0xc0) {
548
558
  // 2 bytes
549
- const byte2 = src[position++] & 0x3f;
559
+ const byte2 = src[position$1++] & 0x3f;
550
560
  units.push(((byte1 & 0x1f) << 6) | byte2);
551
561
  } else if ((byte1 & 0xf0) === 0xe0) {
552
562
  // 3 bytes
553
- const byte2 = src[position++] & 0x3f;
554
- const byte3 = src[position++] & 0x3f;
563
+ const byte2 = src[position$1++] & 0x3f;
564
+ const byte3 = src[position$1++] & 0x3f;
555
565
  units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
556
566
  } else if ((byte1 & 0xf8) === 0xf0) {
557
567
  // 4 bytes
558
- const byte2 = src[position++] & 0x3f;
559
- const byte3 = src[position++] & 0x3f;
560
- const byte4 = src[position++] & 0x3f;
568
+ const byte2 = src[position$1++] & 0x3f;
569
+ const byte3 = src[position$1++] & 0x3f;
570
+ const byte4 = src[position$1++] & 0x3f;
561
571
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
562
572
  if (unit > 0xffff) {
563
573
  unit -= 0x10000;
@@ -613,12 +623,12 @@
613
623
 
614
624
  var fromCharCode = String.fromCharCode;
615
625
  function longStringInJS(length) {
616
- let start = position;
626
+ let start = position$1;
617
627
  let bytes = new Array(length);
618
628
  for (let i = 0; i < length; i++) {
619
- const byte = src[position++];
629
+ const byte = src[position$1++];
620
630
  if ((byte & 0x80) > 0) {
621
- position = start;
631
+ position$1 = start;
622
632
  return
623
633
  }
624
634
  bytes[i] = byte;
@@ -631,131 +641,131 @@
631
641
  if (length === 0)
632
642
  return ''
633
643
  else {
634
- let a = src[position++];
644
+ let a = src[position$1++];
635
645
  if ((a & 0x80) > 1) {
636
- position -= 1;
646
+ position$1 -= 1;
637
647
  return
638
648
  }
639
649
  return fromCharCode(a)
640
650
  }
641
651
  } else {
642
- let a = src[position++];
643
- let b = src[position++];
652
+ let a = src[position$1++];
653
+ let b = src[position$1++];
644
654
  if ((a & 0x80) > 0 || (b & 0x80) > 0) {
645
- position -= 2;
655
+ position$1 -= 2;
646
656
  return
647
657
  }
648
658
  if (length < 3)
649
659
  return fromCharCode(a, b)
650
- let c = src[position++];
660
+ let c = src[position$1++];
651
661
  if ((c & 0x80) > 0) {
652
- position -= 3;
662
+ position$1 -= 3;
653
663
  return
654
664
  }
655
665
  return fromCharCode(a, b, c)
656
666
  }
657
667
  } else {
658
- let a = src[position++];
659
- let b = src[position++];
660
- let c = src[position++];
661
- let d = src[position++];
668
+ let a = src[position$1++];
669
+ let b = src[position$1++];
670
+ let c = src[position$1++];
671
+ let d = src[position$1++];
662
672
  if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
663
- position -= 4;
673
+ position$1 -= 4;
664
674
  return
665
675
  }
666
676
  if (length < 6) {
667
677
  if (length === 4)
668
678
  return fromCharCode(a, b, c, d)
669
679
  else {
670
- let e = src[position++];
680
+ let e = src[position$1++];
671
681
  if ((e & 0x80) > 0) {
672
- position -= 5;
682
+ position$1 -= 5;
673
683
  return
674
684
  }
675
685
  return fromCharCode(a, b, c, d, e)
676
686
  }
677
687
  } else if (length < 8) {
678
- let e = src[position++];
679
- let f = src[position++];
688
+ let e = src[position$1++];
689
+ let f = src[position$1++];
680
690
  if ((e & 0x80) > 0 || (f & 0x80) > 0) {
681
- position -= 6;
691
+ position$1 -= 6;
682
692
  return
683
693
  }
684
694
  if (length < 7)
685
695
  return fromCharCode(a, b, c, d, e, f)
686
- let g = src[position++];
696
+ let g = src[position$1++];
687
697
  if ((g & 0x80) > 0) {
688
- position -= 7;
698
+ position$1 -= 7;
689
699
  return
690
700
  }
691
701
  return fromCharCode(a, b, c, d, e, f, g)
692
702
  } else {
693
- let e = src[position++];
694
- let f = src[position++];
695
- let g = src[position++];
696
- let h = src[position++];
703
+ let e = src[position$1++];
704
+ let f = src[position$1++];
705
+ let g = src[position$1++];
706
+ let h = src[position$1++];
697
707
  if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
698
- position -= 8;
708
+ position$1 -= 8;
699
709
  return
700
710
  }
701
711
  if (length < 10) {
702
712
  if (length === 8)
703
713
  return fromCharCode(a, b, c, d, e, f, g, h)
704
714
  else {
705
- let i = src[position++];
715
+ let i = src[position$1++];
706
716
  if ((i & 0x80) > 0) {
707
- position -= 9;
717
+ position$1 -= 9;
708
718
  return
709
719
  }
710
720
  return fromCharCode(a, b, c, d, e, f, g, h, i)
711
721
  }
712
722
  } else if (length < 12) {
713
- let i = src[position++];
714
- let j = src[position++];
723
+ let i = src[position$1++];
724
+ let j = src[position$1++];
715
725
  if ((i & 0x80) > 0 || (j & 0x80) > 0) {
716
- position -= 10;
726
+ position$1 -= 10;
717
727
  return
718
728
  }
719
729
  if (length < 11)
720
730
  return fromCharCode(a, b, c, d, e, f, g, h, i, j)
721
- let k = src[position++];
731
+ let k = src[position$1++];
722
732
  if ((k & 0x80) > 0) {
723
- position -= 11;
733
+ position$1 -= 11;
724
734
  return
725
735
  }
726
736
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
727
737
  } else {
728
- let i = src[position++];
729
- let j = src[position++];
730
- let k = src[position++];
731
- let l = src[position++];
738
+ let i = src[position$1++];
739
+ let j = src[position$1++];
740
+ let k = src[position$1++];
741
+ let l = src[position$1++];
732
742
  if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
733
- position -= 12;
743
+ position$1 -= 12;
734
744
  return
735
745
  }
736
746
  if (length < 14) {
737
747
  if (length === 12)
738
748
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
739
749
  else {
740
- let m = src[position++];
750
+ let m = src[position$1++];
741
751
  if ((m & 0x80) > 0) {
742
- position -= 13;
752
+ position$1 -= 13;
743
753
  return
744
754
  }
745
755
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
746
756
  }
747
757
  } else {
748
- let m = src[position++];
749
- let n = src[position++];
758
+ let m = src[position$1++];
759
+ let n = src[position$1++];
750
760
  if ((m & 0x80) > 0 || (n & 0x80) > 0) {
751
- position -= 14;
761
+ position$1 -= 14;
752
762
  return
753
763
  }
754
764
  if (length < 15)
755
765
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
756
- let o = src[position++];
766
+ let o = src[position$1++];
757
767
  if ((o & 0x80) > 0) {
758
- position -= 15;
768
+ position$1 -= 15;
759
769
  return
760
770
  }
761
771
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
@@ -766,7 +776,7 @@
766
776
  }
767
777
 
768
778
  function readOnlyJSString() {
769
- let token = src[position++];
779
+ let token = src[position$1++];
770
780
  let length;
771
781
  if (token < 0xc0) {
772
782
  // fixstr
@@ -775,17 +785,17 @@
775
785
  switch(token) {
776
786
  case 0xd9:
777
787
  // str 8
778
- length = src[position++];
788
+ length = src[position$1++];
779
789
  break
780
790
  case 0xda:
781
791
  // str 16
782
- length = dataView.getUint16(position);
783
- position += 2;
792
+ length = dataView.getUint16(position$1);
793
+ position$1 += 2;
784
794
  break
785
795
  case 0xdb:
786
796
  // str 32
787
- length = dataView.getUint32(position);
788
- position += 4;
797
+ length = dataView.getUint32(position$1);
798
+ position$1 += 4;
789
799
  break
790
800
  default:
791
801
  throw new Error('Expected string')
@@ -798,19 +808,19 @@
798
808
  function readBin(length) {
799
809
  return currentUnpackr.copyBuffers ?
800
810
  // specifically use the copying slice (not the node one)
801
- Uint8Array.prototype.slice.call(src, position, position += length) :
802
- src.subarray(position, position += length)
811
+ Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
812
+ src.subarray(position$1, position$1 += length)
803
813
  }
804
814
  function readExt(length) {
805
- let type = src[position++];
815
+ let type = src[position$1++];
806
816
  if (currentExtensions[type]) {
807
817
  let end;
808
- return currentExtensions[type](src.subarray(position, end = (position += length)), (readPosition) => {
809
- position = readPosition;
818
+ return currentExtensions[type](src.subarray(position$1, end = (position$1 += length)), (readPosition) => {
819
+ position$1 = readPosition;
810
820
  try {
811
821
  return read();
812
822
  } finally {
813
- position = end;
823
+ position$1 = end;
814
824
  }
815
825
  })
816
826
  }
@@ -820,22 +830,22 @@
820
830
 
821
831
  var keyCache = new Array(4096);
822
832
  function readKey() {
823
- let length = src[position++];
833
+ let length = src[position$1++];
824
834
  if (length >= 0xa0 && length < 0xc0) {
825
835
  // fixstr, potentially use key cache
826
836
  length = length - 0xa0;
827
- if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
828
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
837
+ if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
838
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
829
839
  else if (!(srcStringEnd == 0 && srcEnd < 180))
830
840
  return readFixedString(length)
831
841
  } else { // not cacheable, go back and do a standard read
832
- position--;
833
- return read()
842
+ position$1--;
843
+ return read().toString()
834
844
  }
835
- let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
845
+ let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
836
846
  let entry = keyCache[key];
837
- let checkPosition = position;
838
- let end = position + length - 3;
847
+ let checkPosition = position$1;
848
+ let end = position$1 + length - 3;
839
849
  let chunk;
840
850
  let i = 0;
841
851
  if (entry && entry.bytes == length) {
@@ -856,11 +866,11 @@
856
866
  }
857
867
  }
858
868
  if (checkPosition === end) {
859
- position = checkPosition;
869
+ position$1 = checkPosition;
860
870
  return entry.string
861
871
  }
862
872
  end -= 3;
863
- checkPosition = position;
873
+ checkPosition = position$1;
864
874
  }
865
875
  entry = [];
866
876
  keyCache[key] = entry;
@@ -884,16 +894,7 @@
884
894
 
885
895
  // the registration of the record definition extension (as "r")
886
896
  const recordDefinition = (id, highByte) => {
887
- let structure;
888
- if (currentUnpackr.freezeData) {
889
- currentUnpackr.freezeData = false;
890
- try {
891
- structure = read();
892
- } finally {
893
- currentUnpackr.freezeData = true;
894
- }
895
- } else
896
- structure = read();
897
+ let structure = read().map(property => property.toString()); // ensure that all keys are strings and that the array is mutable
897
898
  let firstByte = id;
898
899
  if (highByte !== undefined) {
899
900
  id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
@@ -917,10 +918,10 @@
917
918
 
918
919
  currentExtensions[0x69] = (data) => {
919
920
  // id extension (for structured clones)
920
- let id = dataView.getUint32(position - 4);
921
+ let id = dataView.getUint32(position$1 - 4);
921
922
  if (!referenceMap)
922
923
  referenceMap = new Map();
923
- let token = src[position];
924
+ let token = src[position$1];
924
925
  let target;
925
926
  // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
926
927
  // ahead past references to record structure definitions
@@ -940,7 +941,7 @@
940
941
 
941
942
  currentExtensions[0x70] = (data) => {
942
943
  // pointer extension (for structured clones)
943
- let id = dataView.getUint32(position - 4);
944
+ let id = dataView.getUint32(position$1 - 4);
944
945
  let refEntry = referenceMap.get(id);
945
946
  refEntry.used = true;
946
947
  return refEntry.target
@@ -965,14 +966,14 @@
965
966
  const TEMP_BUNDLE = [];
966
967
  currentExtensions[0x62] = (data) => {
967
968
  let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
968
- let dataPosition = position;
969
- position += dataSize - data.length;
970
- bundledStrings = TEMP_BUNDLE;
971
- bundledStrings = [readOnlyJSString(), readOnlyJSString()];
972
- bundledStrings.position0 = 0;
973
- bundledStrings.position1 = 0;
974
- bundledStrings.postBundlePosition = position;
975
- position = dataPosition;
969
+ let dataPosition = position$1;
970
+ position$1 += dataSize - data.length;
971
+ bundledStrings$1 = TEMP_BUNDLE;
972
+ bundledStrings$1 = [readOnlyJSString(), readOnlyJSString()];
973
+ bundledStrings$1.position0 = 0;
974
+ bundledStrings$1.position1 = 0;
975
+ bundledStrings$1.postBundlePosition = position$1;
976
+ position$1 = dataPosition;
976
977
  return read()
977
978
  };
978
979
 
@@ -996,12 +997,12 @@
996
997
 
997
998
  function saveState(callback) {
998
999
  let savedSrcEnd = srcEnd;
999
- let savedPosition = position;
1000
+ let savedPosition = position$1;
1000
1001
  let savedSrcStringStart = srcStringStart;
1001
1002
  let savedSrcStringEnd = srcStringEnd;
1002
1003
  let savedSrcString = srcString;
1003
1004
  let savedReferenceMap = referenceMap;
1004
- let savedBundledStrings = bundledStrings;
1005
+ let savedBundledStrings = bundledStrings$1;
1005
1006
 
1006
1007
  // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1007
1008
  let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
@@ -1011,12 +1012,12 @@
1011
1012
  let savedSequentialMode = sequentialMode;
1012
1013
  let value = callback();
1013
1014
  srcEnd = savedSrcEnd;
1014
- position = savedPosition;
1015
+ position$1 = savedPosition;
1015
1016
  srcStringStart = savedSrcStringStart;
1016
1017
  srcStringEnd = savedSrcStringEnd;
1017
1018
  srcString = savedSrcString;
1018
1019
  referenceMap = savedReferenceMap;
1019
- bundledStrings = savedBundledStrings;
1020
+ bundledStrings$1 = savedBundledStrings;
1020
1021
  src = savedSrc;
1021
1022
  sequentialMode = savedSequentialMode;
1022
1023
  currentStructures = savedStructures;
@@ -1031,7 +1032,7 @@
1031
1032
  currentStructures = null;
1032
1033
  }
1033
1034
 
1034
- function addExtension(extension) {
1035
+ function addExtension$1(extension) {
1035
1036
  if (extension.unpack)
1036
1037
  currentExtensions[extension.type] = extension.unpack;
1037
1038
  else
@@ -1073,9 +1074,9 @@
1073
1074
  const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1074
1075
  let target, keysTarget;
1075
1076
  let targetView;
1076
- let position$1 = 0;
1077
+ let position = 0;
1077
1078
  let safeEnd;
1078
- let bundledStrings$1 = null;
1079
+ let bundledStrings = null;
1079
1080
  let writeStructSlots;
1080
1081
  const MAX_BUNDLE_SIZE = 0xf000;
1081
1082
  const hasNonLatin = /[\u0080-\uFFFF]/;
@@ -1127,25 +1128,25 @@
1127
1128
  this.pack = this.encode = function(value, encodeOptions) {
1128
1129
  if (!target) {
1129
1130
  target = new ByteArrayAllocate(8192);
1130
- targetView = target.dataView = new DataView(target.buffer, 0, 8192);
1131
- position$1 = 0;
1131
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
1132
+ position = 0;
1132
1133
  }
1133
1134
  safeEnd = target.length - 10;
1134
- if (safeEnd - position$1 < 0x800) {
1135
+ if (safeEnd - position < 0x800) {
1135
1136
  // don't start too close to the end,
1136
1137
  target = new ByteArrayAllocate(target.length);
1137
- targetView = target.dataView = new DataView(target.buffer, 0, target.length);
1138
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1138
1139
  safeEnd = target.length - 10;
1139
- position$1 = 0;
1140
+ position = 0;
1140
1141
  } else
1141
- position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1142
- start = position$1;
1142
+ position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1143
+ start = position;
1143
1144
  referenceMap = packr.structuredClone ? new Map() : null;
1144
1145
  if (packr.bundleStrings && typeof value !== 'string') {
1145
- bundledStrings$1 = [];
1146
- bundledStrings$1.size = Infinity; // force a new bundle start on first string
1146
+ bundledStrings = [];
1147
+ bundledStrings.size = Infinity; // force a new bundle start on first string
1147
1148
  } else
1148
- bundledStrings$1 = null;
1149
+ bundledStrings = null;
1149
1150
  structures = packr.structures;
1150
1151
  if (structures) {
1151
1152
  if (structures.uninitialized)
@@ -1186,27 +1187,51 @@
1186
1187
  writeStruct(value);
1187
1188
  else
1188
1189
  pack(value);
1190
+ let lastBundle = bundledStrings;
1191
+ if (bundledStrings)
1192
+ writeBundles(start, pack, 0);
1189
1193
  if (referenceMap && referenceMap.idsToInsert) {
1190
- let incrementPosition = referenceMap.idsToInsert.length * 6;
1191
- if (bundledStrings$1)
1192
- writeBundles(start, pack, incrementPosition);
1193
- position$1 += incrementPosition;
1194
- if (position$1 > safeEnd)
1195
- makeRoom(position$1);
1196
- packr.offset = position$1;
1197
- let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
1194
+ let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1195
+ let i = idsToInsert.length;
1196
+ let incrementPosition = -1;
1197
+ while (lastBundle && i > 0) {
1198
+ let insertionPoint = idsToInsert[--i].offset + start;
1199
+ if (insertionPoint < (lastBundle.stringsPosition + start) && incrementPosition === -1)
1200
+ incrementPosition = 0;
1201
+ if (insertionPoint > (lastBundle.position + start)) {
1202
+ if (incrementPosition >= 0)
1203
+ incrementPosition += 6;
1204
+ } else {
1205
+ if (incrementPosition >= 0) {
1206
+ // update the bundle reference now
1207
+ targetView.setUint32(lastBundle.position + start,
1208
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1209
+ incrementPosition = -1; // reset
1210
+ }
1211
+ lastBundle = lastBundle.previous;
1212
+ i++;
1213
+ }
1214
+ }
1215
+ if (incrementPosition >= 0 && lastBundle) {
1216
+ // update the bundle reference now
1217
+ targetView.setUint32(lastBundle.position + start,
1218
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1219
+ }
1220
+ position += idsToInsert.length * 6;
1221
+ if (position > safeEnd)
1222
+ makeRoom(position);
1223
+ packr.offset = position;
1224
+ let serialized = insertIds(target.subarray(start, position), idsToInsert);
1198
1225
  referenceMap = null;
1199
1226
  return serialized
1200
1227
  }
1201
- if (bundledStrings$1)
1202
- writeBundles(start, pack, 0);
1203
- packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1228
+ packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1204
1229
  if (encodeOptions & REUSE_BUFFER_MODE) {
1205
1230
  target.start = start;
1206
- target.end = position$1;
1231
+ target.end = position;
1207
1232
  return target
1208
1233
  }
1209
- return target.subarray(start, position$1) // position can change if we call pack again in saveStructures, so we get the buffer now
1234
+ return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1210
1235
  } finally {
1211
1236
  if (structures) {
1212
1237
  if (serializationsSinceTransitionRebuild < 10)
@@ -1229,7 +1254,7 @@
1229
1254
  }
1230
1255
  if (hasSharedUpdate && packr.saveStructures) {
1231
1256
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1232
- let returnBuffer = target.subarray(start, position$1);
1257
+ let returnBuffer = target.subarray(start, position);
1233
1258
  let newSharedData = prepareStructures(structures, packr);
1234
1259
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1235
1260
  // get updated structures and try again if the update failed
@@ -1240,44 +1265,47 @@
1240
1265
  }
1241
1266
  }
1242
1267
  if (encodeOptions & RESET_BUFFER_MODE)
1243
- position$1 = start;
1268
+ position = start;
1244
1269
  }
1245
1270
  };
1246
1271
  const pack = (value) => {
1247
- if (position$1 > safeEnd)
1248
- target = makeRoom(position$1);
1272
+ if (position > safeEnd)
1273
+ target = makeRoom(position);
1249
1274
 
1250
1275
  var type = typeof value;
1251
1276
  var length;
1252
1277
  if (type === 'string') {
1253
1278
  let strLength = value.length;
1254
- if (bundledStrings$1 && strLength >= 4 && strLength < 0x1000) {
1255
- if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1279
+ if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
1280
+ if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1256
1281
  let extStart;
1257
- let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1258
- if (position$1 + maxBytes > safeEnd)
1259
- target = makeRoom(position$1 + maxBytes);
1260
- 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
1261
- target[position$1] = 0xc8; // ext 16
1262
- position$1 += 3; // reserve for the writing bundle size
1263
- target[position$1++] = 0x62; // 'b'
1264
- extStart = position$1 - start;
1265
- position$1 += 4; // reserve for writing bundle reference
1266
- writeBundles(start, pack); // write the last bundles
1267
- targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1282
+ let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
1283
+ if (position + maxBytes > safeEnd)
1284
+ target = makeRoom(position + maxBytes);
1285
+ let lastBundle;
1286
+ 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
1287
+ lastBundle = bundledStrings;
1288
+ target[position] = 0xc8; // ext 16
1289
+ position += 3; // reserve for the writing bundle size
1290
+ target[position++] = 0x62; // 'b'
1291
+ extStart = position - start;
1292
+ position += 4; // reserve for writing bundle reference
1293
+ writeBundles(start, pack, 0); // write the last bundles
1294
+ targetView.setUint16(extStart + start - 3, position - start - extStart);
1268
1295
  } 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)
1269
- target[position$1++] = 0xd6; // fixext 4
1270
- target[position$1++] = 0x62; // 'b'
1271
- extStart = position$1 - start;
1272
- position$1 += 4; // reserve for writing bundle reference
1296
+ target[position++] = 0xd6; // fixext 4
1297
+ target[position++] = 0x62; // 'b'
1298
+ extStart = position - start;
1299
+ position += 4; // reserve for writing bundle reference
1273
1300
  }
1274
- bundledStrings$1 = ['', '']; // create new ones
1275
- bundledStrings$1.size = 0;
1276
- bundledStrings$1.position = extStart;
1301
+ bundledStrings = ['', '']; // create new ones
1302
+ bundledStrings.previous = lastBundle;
1303
+ bundledStrings.size = 0;
1304
+ bundledStrings.position = extStart;
1277
1305
  }
1278
1306
  let twoByte = hasNonLatin.test(value);
1279
- bundledStrings$1[twoByte ? 0 : 1] += value;
1280
- target[position$1++] = 0xc1;
1307
+ bundledStrings[twoByte ? 0 : 1] += value;
1308
+ target[position++] = 0xc1;
1281
1309
  pack(twoByte ? -strLength : strLength);
1282
1310
  return
1283
1311
  }
@@ -1293,11 +1321,11 @@
1293
1321
  headerSize = 5;
1294
1322
  }
1295
1323
  let maxBytes = strLength * 3;
1296
- if (position$1 + maxBytes > safeEnd)
1297
- target = makeRoom(position$1 + maxBytes);
1324
+ if (position + maxBytes > safeEnd)
1325
+ target = makeRoom(position + maxBytes);
1298
1326
 
1299
1327
  if (strLength < 0x40 || !encodeUtf8) {
1300
- let i, c1, c2, strPosition = position$1 + headerSize;
1328
+ let i, c1, c2, strPosition = position + headerSize;
1301
1329
  for (i = 0; i < strLength; i++) {
1302
1330
  c1 = value.charCodeAt(i);
1303
1331
  if (c1 < 0x80) {
@@ -1321,88 +1349,88 @@
1321
1349
  target[strPosition++] = c1 & 0x3f | 0x80;
1322
1350
  }
1323
1351
  }
1324
- length = strPosition - position$1 - headerSize;
1352
+ length = strPosition - position - headerSize;
1325
1353
  } else {
1326
- length = encodeUtf8(value, position$1 + headerSize);
1354
+ length = encodeUtf8(value, position + headerSize);
1327
1355
  }
1328
1356
 
1329
1357
  if (length < 0x20) {
1330
- target[position$1++] = 0xa0 | length;
1358
+ target[position++] = 0xa0 | length;
1331
1359
  } else if (length < 0x100) {
1332
1360
  if (headerSize < 2) {
1333
- target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1361
+ target.copyWithin(position + 2, position + 1, position + 1 + length);
1334
1362
  }
1335
- target[position$1++] = 0xd9;
1336
- target[position$1++] = length;
1363
+ target[position++] = 0xd9;
1364
+ target[position++] = length;
1337
1365
  } else if (length < 0x10000) {
1338
1366
  if (headerSize < 3) {
1339
- target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1367
+ target.copyWithin(position + 3, position + 2, position + 2 + length);
1340
1368
  }
1341
- target[position$1++] = 0xda;
1342
- target[position$1++] = length >> 8;
1343
- target[position$1++] = length & 0xff;
1369
+ target[position++] = 0xda;
1370
+ target[position++] = length >> 8;
1371
+ target[position++] = length & 0xff;
1344
1372
  } else {
1345
1373
  if (headerSize < 5) {
1346
- target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1374
+ target.copyWithin(position + 5, position + 3, position + 3 + length);
1347
1375
  }
1348
- target[position$1++] = 0xdb;
1349
- targetView.setUint32(position$1, length);
1350
- position$1 += 4;
1376
+ target[position++] = 0xdb;
1377
+ targetView.setUint32(position, length);
1378
+ position += 4;
1351
1379
  }
1352
- position$1 += length;
1380
+ position += length;
1353
1381
  } else if (type === 'number') {
1354
1382
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1355
1383
  // positive uint
1356
1384
  if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
1357
- target[position$1++] = value;
1385
+ target[position++] = value;
1358
1386
  } else if (value < 0x100) {
1359
- target[position$1++] = 0xcc;
1360
- target[position$1++] = value;
1387
+ target[position++] = 0xcc;
1388
+ target[position++] = value;
1361
1389
  } else if (value < 0x10000) {
1362
- target[position$1++] = 0xcd;
1363
- target[position$1++] = value >> 8;
1364
- target[position$1++] = value & 0xff;
1390
+ target[position++] = 0xcd;
1391
+ target[position++] = value >> 8;
1392
+ target[position++] = value & 0xff;
1365
1393
  } else {
1366
- target[position$1++] = 0xce;
1367
- targetView.setUint32(position$1, value);
1368
- position$1 += 4;
1394
+ target[position++] = 0xce;
1395
+ targetView.setUint32(position, value);
1396
+ position += 4;
1369
1397
  }
1370
1398
  } else if (value >> 0 === value) { // negative integer
1371
1399
  if (value >= -0x20) {
1372
- target[position$1++] = 0x100 + value;
1400
+ target[position++] = 0x100 + value;
1373
1401
  } else if (value >= -0x80) {
1374
- target[position$1++] = 0xd0;
1375
- target[position$1++] = value + 0x100;
1402
+ target[position++] = 0xd0;
1403
+ target[position++] = value + 0x100;
1376
1404
  } else if (value >= -0x8000) {
1377
- target[position$1++] = 0xd1;
1378
- targetView.setInt16(position$1, value);
1379
- position$1 += 2;
1405
+ target[position++] = 0xd1;
1406
+ targetView.setInt16(position, value);
1407
+ position += 2;
1380
1408
  } else {
1381
- target[position$1++] = 0xd2;
1382
- targetView.setInt32(position$1, value);
1383
- position$1 += 4;
1409
+ target[position++] = 0xd2;
1410
+ targetView.setInt32(position, value);
1411
+ position += 4;
1384
1412
  }
1385
1413
  } else {
1386
1414
  let useFloat32;
1387
1415
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1388
- target[position$1++] = 0xca;
1389
- targetView.setFloat32(position$1, value);
1416
+ target[position++] = 0xca;
1417
+ targetView.setFloat32(position, value);
1390
1418
  let xShifted;
1391
1419
  if (useFloat32 < 4 ||
1392
1420
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1393
- ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1394
- position$1 += 4;
1421
+ ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1422
+ position += 4;
1395
1423
  return
1396
1424
  } else
1397
- position$1--; // move back into position for writing a double
1425
+ position--; // move back into position for writing a double
1398
1426
  }
1399
- target[position$1++] = 0xcb;
1400
- targetView.setFloat64(position$1, value);
1401
- position$1 += 8;
1427
+ target[position++] = 0xcb;
1428
+ targetView.setFloat64(position, value);
1429
+ position += 8;
1402
1430
  }
1403
1431
  } else if (type === 'object') {
1404
1432
  if (!value)
1405
- target[position$1++] = 0xc0;
1433
+ target[position++] = 0xc0;
1406
1434
  else {
1407
1435
  if (referenceMap) {
1408
1436
  let referee = referenceMap.get(value);
@@ -1411,29 +1439,29 @@
1411
1439
  let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1412
1440
  referee.id = idsToInsert.push(referee);
1413
1441
  }
1414
- target[position$1++] = 0xd6; // fixext 4
1415
- target[position$1++] = 0x70; // "p" for pointer
1416
- targetView.setUint32(position$1, referee.id);
1417
- position$1 += 4;
1442
+ target[position++] = 0xd6; // fixext 4
1443
+ target[position++] = 0x70; // "p" for pointer
1444
+ targetView.setUint32(position, referee.id);
1445
+ position += 4;
1418
1446
  return
1419
1447
  } else
1420
- referenceMap.set(value, { offset: position$1 - start });
1448
+ referenceMap.set(value, { offset: position - start });
1421
1449
  }
1422
1450
  let constructor = value.constructor;
1423
1451
  if (constructor === Object) {
1424
1452
  writeObject(value, true);
1425
- } else if (constructor === Array) {
1453
+ } else if (constructor === Array || Array.isArray(value)) {
1426
1454
  length = value.length;
1427
1455
  if (length < 0x10) {
1428
- target[position$1++] = 0x90 | length;
1456
+ target[position++] = 0x90 | length;
1429
1457
  } else if (length < 0x10000) {
1430
- target[position$1++] = 0xdc;
1431
- target[position$1++] = length >> 8;
1432
- target[position$1++] = length & 0xff;
1458
+ target[position++] = 0xdc;
1459
+ target[position++] = length >> 8;
1460
+ target[position++] = length & 0xff;
1433
1461
  } else {
1434
- target[position$1++] = 0xdd;
1435
- targetView.setUint32(position$1, length);
1436
- position$1 += 4;
1462
+ target[position++] = 0xdd;
1463
+ targetView.setUint32(position, length);
1464
+ position += 4;
1437
1465
  }
1438
1466
  for (let i = 0; i < length; i++) {
1439
1467
  pack(value[i]);
@@ -1441,15 +1469,15 @@
1441
1469
  } else if (constructor === Map) {
1442
1470
  length = value.size;
1443
1471
  if (length < 0x10) {
1444
- target[position$1++] = 0x80 | length;
1472
+ target[position++] = 0x80 | length;
1445
1473
  } else if (length < 0x10000) {
1446
- target[position$1++] = 0xde;
1447
- target[position$1++] = length >> 8;
1448
- target[position$1++] = length & 0xff;
1474
+ target[position++] = 0xde;
1475
+ target[position++] = length >> 8;
1476
+ target[position++] = length & 0xff;
1449
1477
  } else {
1450
- target[position$1++] = 0xdf;
1451
- targetView.setUint32(position$1, length);
1452
- position$1 += 4;
1478
+ target[position++] = 0xdf;
1479
+ targetView.setUint32(position, length);
1480
+ position += 4;
1453
1481
  }
1454
1482
  for (let [ key, entryValue ] of value) {
1455
1483
  pack(key);
@@ -1462,16 +1490,16 @@
1462
1490
  let extension = extensions[i];
1463
1491
  if (extension.write) {
1464
1492
  if (extension.type) {
1465
- target[position$1++] = 0xd4; // one byte "tag" extension
1466
- target[position$1++] = extension.type;
1467
- target[position$1++] = 0;
1493
+ target[position++] = 0xd4; // one byte "tag" extension
1494
+ target[position++] = extension.type;
1495
+ target[position++] = 0;
1468
1496
  }
1469
1497
  pack(extension.write.call(this, value));
1470
1498
  return
1471
1499
  }
1472
1500
  let currentTarget = target;
1473
1501
  let currentTargetView = targetView;
1474
- let currentPosition = position$1;
1502
+ let currentPosition = position;
1475
1503
  target = null;
1476
1504
  let result;
1477
1505
  try {
@@ -1479,11 +1507,11 @@
1479
1507
  // restore target and use it
1480
1508
  target = currentTarget;
1481
1509
  currentTarget = null;
1482
- position$1 += size;
1483
- if (position$1 > safeEnd)
1484
- makeRoom(position$1);
1510
+ position += size;
1511
+ if (position > safeEnd)
1512
+ makeRoom(position);
1485
1513
  return {
1486
- target, targetView, position: position$1 - size
1514
+ target, targetView, position: position - size
1487
1515
  }
1488
1516
  }, pack);
1489
1517
  } finally {
@@ -1491,14 +1519,14 @@
1491
1519
  if (currentTarget) {
1492
1520
  target = currentTarget;
1493
1521
  targetView = currentTargetView;
1494
- position$1 = currentPosition;
1522
+ position = currentPosition;
1495
1523
  safeEnd = target.length - 10;
1496
1524
  }
1497
1525
  }
1498
1526
  if (result) {
1499
- if (result.length + position$1 > safeEnd)
1500
- makeRoom(result.length + position$1);
1501
- position$1 = writeExtensionData(result, target, position$1, extension.type);
1527
+ if (result.length + position > safeEnd)
1528
+ makeRoom(result.length + position);
1529
+ position = writeExtensionData(result, target, position, extension.type);
1502
1530
  }
1503
1531
  return
1504
1532
  }
@@ -1508,33 +1536,33 @@
1508
1536
  }
1509
1537
  }
1510
1538
  } else if (type === 'boolean') {
1511
- target[position$1++] = value ? 0xc3 : 0xc2;
1539
+ target[position++] = value ? 0xc3 : 0xc2;
1512
1540
  } else if (type === 'bigint') {
1513
1541
  if (value < (BigInt(1)<<BigInt(63)) && value >= -(BigInt(1)<<BigInt(63))) {
1514
1542
  // use a signed int as long as it fits
1515
- target[position$1++] = 0xd3;
1516
- targetView.setBigInt64(position$1, value);
1543
+ target[position++] = 0xd3;
1544
+ targetView.setBigInt64(position, value);
1517
1545
  } else if (value < (BigInt(1)<<BigInt(64)) && value > 0) {
1518
1546
  // if we can fit an unsigned int, use that
1519
- target[position$1++] = 0xcf;
1520
- targetView.setBigUint64(position$1, value);
1547
+ target[position++] = 0xcf;
1548
+ targetView.setBigUint64(position, value);
1521
1549
  } else {
1522
1550
  // overflow
1523
1551
  if (this.largeBigIntToFloat) {
1524
- target[position$1++] = 0xcb;
1525
- targetView.setFloat64(position$1, Number(value));
1552
+ target[position++] = 0xcb;
1553
+ targetView.setFloat64(position, Number(value));
1526
1554
  } else {
1527
1555
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1528
1556
  }
1529
1557
  }
1530
- position$1 += 8;
1558
+ position += 8;
1531
1559
  } else if (type === 'undefined') {
1532
1560
  if (this.encodeUndefinedAsNil)
1533
- target[position$1++] = 0xc0;
1561
+ target[position++] = 0xc0;
1534
1562
  else {
1535
- target[position$1++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1536
- target[position$1++] = 0;
1537
- target[position$1++] = 0;
1563
+ target[position++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1564
+ target[position++] = 0;
1565
+ target[position++] = 0;
1538
1566
  }
1539
1567
  } else if (type === 'function') {
1540
1568
  pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
@@ -1548,15 +1576,15 @@
1548
1576
  let keys = Object.keys(object);
1549
1577
  let length = keys.length;
1550
1578
  if (length < 0x10) {
1551
- target[position$1++] = 0x80 | length;
1579
+ target[position++] = 0x80 | length;
1552
1580
  } else if (length < 0x10000) {
1553
- target[position$1++] = 0xde;
1554
- target[position$1++] = length >> 8;
1555
- target[position$1++] = length & 0xff;
1581
+ target[position++] = 0xde;
1582
+ target[position++] = length >> 8;
1583
+ target[position++] = length & 0xff;
1556
1584
  } else {
1557
- target[position$1++] = 0xdf;
1558
- targetView.setUint32(position$1, length);
1559
- position$1 += 4;
1585
+ target[position++] = 0xdf;
1586
+ targetView.setUint32(position, length);
1587
+ position += 4;
1560
1588
  }
1561
1589
  let key;
1562
1590
  for (let i = 0; i < length; i++) {
@@ -1565,9 +1593,9 @@
1565
1593
  }
1566
1594
  } :
1567
1595
  (object, safePrototype) => {
1568
- target[position$1++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1569
- let objectOffset = position$1 - start;
1570
- position$1 += 2;
1596
+ target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1597
+ let objectOffset = position - start;
1598
+ position += 2;
1571
1599
  let size = 0;
1572
1600
  for (let key in object) {
1573
1601
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1582,7 +1610,7 @@
1582
1610
  (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)
1583
1611
  (object, safePrototype) => {
1584
1612
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1585
- let objectOffset = position$1++ - start;
1613
+ let objectOffset = position++ - start;
1586
1614
  let wroteKeys;
1587
1615
  for (let key in object) {
1588
1616
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1604,9 +1632,9 @@
1604
1632
  }
1605
1633
  transition = nextTransition;
1606
1634
  }
1607
- if (objectOffset + start + 1 == position$1) {
1635
+ if (objectOffset + start + 1 == position) {
1608
1636
  // first key, so we don't need to insert, we can just write record directly
1609
- position$1--;
1637
+ position--;
1610
1638
  newRecord(transition, keys, newTransitions);
1611
1639
  } else // otherwise we need to insert the record, moving existing data after the record
1612
1640
  insertNewRecord(transition, keys, objectOffset, newTransitions);
@@ -1638,10 +1666,10 @@
1638
1666
  let recordId = transition[RECORD_SYMBOL];
1639
1667
  if (recordId) {
1640
1668
  if (recordId >= 0x60 && useTwoByteRecords) {
1641
- target[position$1++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1642
- target[position$1++] = recordId >> 5;
1669
+ target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1670
+ target[position++] = recordId >> 5;
1643
1671
  } else
1644
- target[position$1++] = recordId;
1672
+ target[position++] = recordId;
1645
1673
  } else {
1646
1674
  newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
1647
1675
  }
@@ -1661,13 +1689,13 @@
1661
1689
  } else // faster handling for smaller buffers
1662
1690
  newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
1663
1691
  let newBuffer = new ByteArrayAllocate(newSize);
1664
- targetView = newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize);
1692
+ targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
1665
1693
  end = Math.min(end, target.length);
1666
1694
  if (target.copy)
1667
1695
  target.copy(newBuffer, 0, start, end);
1668
1696
  else
1669
1697
  newBuffer.set(target.slice(start, end));
1670
- position$1 -= start;
1698
+ position -= start;
1671
1699
  start = 0;
1672
1700
  safeEnd = newBuffer.length - 10;
1673
1701
  return target = newBuffer
@@ -1696,21 +1724,21 @@
1696
1724
  structures.sharedLength = recordId - 0x3f;
1697
1725
  hasSharedUpdate = true;
1698
1726
  if (highByte >= 0) {
1699
- target[position$1++] = (recordId & 0x1f) + 0x60;
1700
- target[position$1++] = highByte;
1727
+ target[position++] = (recordId & 0x1f) + 0x60;
1728
+ target[position++] = highByte;
1701
1729
  } else {
1702
- target[position$1++] = recordId;
1730
+ target[position++] = recordId;
1703
1731
  }
1704
1732
  } else {
1705
1733
  if (highByte >= 0) {
1706
- target[position$1++] = 0xd5; // fixext 2
1707
- target[position$1++] = 0x72; // "r" record defintion extension type
1708
- target[position$1++] = (recordId & 0x1f) + 0x60;
1709
- target[position$1++] = highByte;
1734
+ target[position++] = 0xd5; // fixext 2
1735
+ target[position++] = 0x72; // "r" record defintion extension type
1736
+ target[position++] = (recordId & 0x1f) + 0x60;
1737
+ target[position++] = highByte;
1710
1738
  } else {
1711
- target[position$1++] = 0xd4; // fixext 1
1712
- target[position$1++] = 0x72; // "r" record defintion extension type
1713
- target[position$1++] = recordId;
1739
+ target[position++] = 0xd4; // fixext 1
1740
+ target[position++] = 0x72; // "r" record defintion extension type
1741
+ target[position++] = recordId;
1714
1742
  }
1715
1743
 
1716
1744
  if (newTransitions)
@@ -1724,46 +1752,57 @@
1724
1752
  };
1725
1753
  const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
1726
1754
  let mainTarget = target;
1727
- let mainPosition = position$1;
1755
+ let mainPosition = position;
1728
1756
  let mainSafeEnd = safeEnd;
1729
1757
  let mainStart = start;
1730
1758
  target = keysTarget;
1731
- position$1 = 0;
1759
+ position = 0;
1732
1760
  start = 0;
1733
1761
  if (!target)
1734
1762
  keysTarget = target = new ByteArrayAllocate(8192);
1735
1763
  safeEnd = target.length - 10;
1736
1764
  newRecord(transition, keys, newTransitions);
1737
1765
  keysTarget = target;
1738
- let keysPosition = position$1;
1766
+ let keysPosition = position;
1739
1767
  target = mainTarget;
1740
- position$1 = mainPosition;
1768
+ position = mainPosition;
1741
1769
  safeEnd = mainSafeEnd;
1742
1770
  start = mainStart;
1743
1771
  if (keysPosition > 1) {
1744
- let newEnd = position$1 + keysPosition - 1;
1772
+ let newEnd = position + keysPosition - 1;
1745
1773
  if (newEnd > safeEnd)
1746
1774
  makeRoom(newEnd);
1747
1775
  let insertionPosition = insertionOffset + start;
1748
- target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position$1);
1776
+ target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position);
1749
1777
  target.set(keysTarget.slice(0, keysPosition), insertionPosition);
1750
- position$1 = newEnd;
1778
+ position = newEnd;
1751
1779
  } else {
1752
1780
  target[insertionOffset + start] = keysTarget[0];
1753
1781
  }
1754
1782
  };
1755
1783
  const writeStruct = (object, safePrototype) => {
1756
- let newPosition = writeStructSlots();
1784
+ let newPosition = writeStructSlots(object, target, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1785
+ if (notifySharedUpdate)
1786
+ return hasSharedUpdate = true;
1787
+ position = newPosition;
1788
+ if (start > 0) {
1789
+ pack(value);
1790
+ if (start == 0)
1791
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1792
+ } else
1793
+ pack(value);
1794
+ return position;
1795
+ }, this);
1757
1796
  if (newPosition === 0) // bail and go to a msgpack object
1758
1797
  return writeObject(object, true);
1759
- position$1 = newPosition;
1798
+ position = newPosition;
1760
1799
  };
1761
1800
  }
1762
1801
  useBuffer(buffer) {
1763
1802
  // this means we are finished using our own buffer and we can write over it safely
1764
1803
  target = buffer;
1765
1804
  targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1766
- position$1 = 0;
1805
+ position = 0;
1767
1806
  }
1768
1807
  clearSharedData() {
1769
1808
  if (this.structures)
@@ -1950,7 +1989,6 @@
1950
1989
  let nextId;
1951
1990
  let distanceToMove = idsToInsert.length * 6;
1952
1991
  let lastEnd = serialized.length - distanceToMove;
1953
- idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1954
1992
  while (nextId = idsToInsert.pop()) {
1955
1993
  let offset = nextId.offset;
1956
1994
  let id = nextId.id;
@@ -1969,16 +2007,17 @@
1969
2007
  }
1970
2008
 
1971
2009
  function writeBundles(start, pack, incrementPosition) {
1972
- if (bundledStrings$1.length > 0) {
1973
- targetView.setUint32(bundledStrings$1.position + start, position$1 + incrementPosition - bundledStrings$1.position - start);
1974
- let writeStrings = bundledStrings$1;
1975
- bundledStrings$1 = null;
2010
+ if (bundledStrings.length > 0) {
2011
+ targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start);
2012
+ bundledStrings.stringsPosition = position - start;
2013
+ let writeStrings = bundledStrings;
2014
+ bundledStrings = null;
1976
2015
  pack(writeStrings[0]);
1977
2016
  pack(writeStrings[1]);
1978
2017
  }
1979
2018
  }
1980
2019
 
1981
- function addExtension$1(extension) {
2020
+ function addExtension(extension) {
1982
2021
  if (extension.Class) {
1983
2022
  if (!extension.pack && !extension.write)
1984
2023
  throw new Error('Extension has no pack or write function')
@@ -1987,7 +2026,7 @@
1987
2026
  extensionClasses.unshift(extension.Class);
1988
2027
  extensions.unshift(extension);
1989
2028
  }
1990
- addExtension(extension);
2029
+ addExtension$1(extension);
1991
2030
  }
1992
2031
  function prepareStructures(structures, packr) {
1993
2032
  structures.isCompatible = (existingStructures) => {
@@ -2106,7 +2145,7 @@
2106
2145
  exports.Packr = Packr;
2107
2146
  exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
2108
2147
  exports.Unpackr = Unpackr;
2109
- exports.addExtension = addExtension$1;
2148
+ exports.addExtension = addExtension;
2110
2149
  exports.clearSource = clearSource;
2111
2150
  exports.decode = decode;
2112
2151
  exports.decodeIter = decodeIter;
@@ -2120,7 +2159,5 @@
2120
2159
  exports.unpackMultiple = unpackMultiple;
2121
2160
  exports.useRecords = useRecords;
2122
2161
 
2123
- Object.defineProperty(exports, '__esModule', { value: true });
2124
-
2125
- })));
2162
+ }));
2126
2163
  //# sourceMappingURL=index.js.map