msgpackr 1.7.2 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -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,44 +808,44 @@
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
  }
817
827
  else
818
- throw new Error('Unknown extension type ' + type)``
828
+ throw new Error('Unknown extension type ' + type)
819
829
  }
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--;
842
+ position$1--;
833
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;
@@ -908,10 +918,10 @@
908
918
 
909
919
  currentExtensions[0x69] = (data) => {
910
920
  // id extension (for structured clones)
911
- let id = dataView.getUint32(position - 4);
921
+ let id = dataView.getUint32(position$1 - 4);
912
922
  if (!referenceMap)
913
923
  referenceMap = new Map();
914
- let token = src[position];
924
+ let token = src[position$1];
915
925
  let target;
916
926
  // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
917
927
  // ahead past references to record structure definitions
@@ -931,7 +941,7 @@
931
941
 
932
942
  currentExtensions[0x70] = (data) => {
933
943
  // pointer extension (for structured clones)
934
- let id = dataView.getUint32(position - 4);
944
+ let id = dataView.getUint32(position$1 - 4);
935
945
  let refEntry = referenceMap.get(id);
936
946
  refEntry.used = true;
937
947
  return refEntry.target
@@ -956,14 +966,14 @@
956
966
  const TEMP_BUNDLE = [];
957
967
  currentExtensions[0x62] = (data) => {
958
968
  let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
959
- let dataPosition = position;
960
- position += dataSize - data.length;
961
- bundledStrings = TEMP_BUNDLE;
962
- bundledStrings = [readOnlyJSString(), readOnlyJSString()];
963
- bundledStrings.position0 = 0;
964
- bundledStrings.position1 = 0;
965
- bundledStrings.postBundlePosition = position;
966
- 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;
967
977
  return read()
968
978
  };
969
979
 
@@ -987,12 +997,12 @@
987
997
 
988
998
  function saveState(callback) {
989
999
  let savedSrcEnd = srcEnd;
990
- let savedPosition = position;
1000
+ let savedPosition = position$1;
991
1001
  let savedSrcStringStart = srcStringStart;
992
1002
  let savedSrcStringEnd = srcStringEnd;
993
1003
  let savedSrcString = srcString;
994
1004
  let savedReferenceMap = referenceMap;
995
- let savedBundledStrings = bundledStrings;
1005
+ let savedBundledStrings = bundledStrings$1;
996
1006
 
997
1007
  // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
998
1008
  let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
@@ -1002,12 +1012,12 @@
1002
1012
  let savedSequentialMode = sequentialMode;
1003
1013
  let value = callback();
1004
1014
  srcEnd = savedSrcEnd;
1005
- position = savedPosition;
1015
+ position$1 = savedPosition;
1006
1016
  srcStringStart = savedSrcStringStart;
1007
1017
  srcStringEnd = savedSrcStringEnd;
1008
1018
  srcString = savedSrcString;
1009
1019
  referenceMap = savedReferenceMap;
1010
- bundledStrings = savedBundledStrings;
1020
+ bundledStrings$1 = savedBundledStrings;
1011
1021
  src = savedSrc;
1012
1022
  sequentialMode = savedSequentialMode;
1013
1023
  currentStructures = savedStructures;
@@ -1022,7 +1032,7 @@
1022
1032
  currentStructures = null;
1023
1033
  }
1024
1034
 
1025
- function addExtension(extension) {
1035
+ function addExtension$1(extension) {
1026
1036
  if (extension.unpack)
1027
1037
  currentExtensions[extension.type] = extension.unpack;
1028
1038
  else
@@ -1064,9 +1074,9 @@
1064
1074
  const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1065
1075
  let target, keysTarget;
1066
1076
  let targetView;
1067
- let position$1 = 0;
1077
+ let position = 0;
1068
1078
  let safeEnd;
1069
- let bundledStrings$1 = null;
1079
+ let bundledStrings = null;
1070
1080
  let writeStructSlots;
1071
1081
  const MAX_BUNDLE_SIZE = 0xf000;
1072
1082
  const hasNonLatin = /[\u0080-\uFFFF]/;
@@ -1118,25 +1128,25 @@
1118
1128
  this.pack = this.encode = function(value, encodeOptions) {
1119
1129
  if (!target) {
1120
1130
  target = new ByteArrayAllocate(8192);
1121
- targetView = target.dataView = new DataView(target.buffer, 0, 8192);
1122
- position$1 = 0;
1131
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
1132
+ position = 0;
1123
1133
  }
1124
1134
  safeEnd = target.length - 10;
1125
- if (safeEnd - position$1 < 0x800) {
1135
+ if (safeEnd - position < 0x800) {
1126
1136
  // don't start too close to the end,
1127
1137
  target = new ByteArrayAllocate(target.length);
1128
- targetView = target.dataView = new DataView(target.buffer, 0, target.length);
1138
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1129
1139
  safeEnd = target.length - 10;
1130
- position$1 = 0;
1140
+ position = 0;
1131
1141
  } else
1132
- position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1133
- start = position$1;
1142
+ position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1143
+ start = position;
1134
1144
  referenceMap = packr.structuredClone ? new Map() : null;
1135
1145
  if (packr.bundleStrings && typeof value !== 'string') {
1136
- bundledStrings$1 = [];
1137
- 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
1138
1148
  } else
1139
- bundledStrings$1 = null;
1149
+ bundledStrings = null;
1140
1150
  structures = packr.structures;
1141
1151
  if (structures) {
1142
1152
  if (structures.uninitialized)
@@ -1177,8 +1187,8 @@
1177
1187
  writeStruct(value);
1178
1188
  else
1179
1189
  pack(value);
1180
- let lastBundle = bundledStrings$1;
1181
- if (bundledStrings$1)
1190
+ let lastBundle = bundledStrings;
1191
+ if (bundledStrings)
1182
1192
  writeBundles(start, pack, 0);
1183
1193
  if (referenceMap && referenceMap.idsToInsert) {
1184
1194
  let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
@@ -1207,21 +1217,21 @@
1207
1217
  targetView.setUint32(lastBundle.position + start,
1208
1218
  targetView.getUint32(lastBundle.position + start) + incrementPosition);
1209
1219
  }
1210
- position$1 += idsToInsert.length * 6;
1211
- if (position$1 > safeEnd)
1212
- makeRoom(position$1);
1213
- packr.offset = position$1;
1214
- let serialized = insertIds(target.subarray(start, position$1), idsToInsert);
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);
1215
1225
  referenceMap = null;
1216
1226
  return serialized
1217
1227
  }
1218
- 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
1219
1229
  if (encodeOptions & REUSE_BUFFER_MODE) {
1220
1230
  target.start = start;
1221
- target.end = position$1;
1231
+ target.end = position;
1222
1232
  return target
1223
1233
  }
1224
- 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
1225
1235
  } finally {
1226
1236
  if (structures) {
1227
1237
  if (serializationsSinceTransitionRebuild < 10)
@@ -1244,7 +1254,7 @@
1244
1254
  }
1245
1255
  if (hasSharedUpdate && packr.saveStructures) {
1246
1256
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1247
- let returnBuffer = target.subarray(start, position$1);
1257
+ let returnBuffer = target.subarray(start, position);
1248
1258
  let newSharedData = prepareStructures(structures, packr);
1249
1259
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1250
1260
  // get updated structures and try again if the update failed
@@ -1255,47 +1265,64 @@
1255
1265
  }
1256
1266
  }
1257
1267
  if (encodeOptions & RESET_BUFFER_MODE)
1258
- position$1 = start;
1268
+ position = start;
1269
+ }
1270
+ };
1271
+ const packArray = (value) => {
1272
+ var length = value.length;
1273
+ if (length < 0x10) {
1274
+ target[position++] = 0x90 | length;
1275
+ } else if (length < 0x10000) {
1276
+ target[position++] = 0xdc;
1277
+ target[position++] = length >> 8;
1278
+ target[position++] = length & 0xff;
1279
+ } else {
1280
+ target[position++] = 0xdd;
1281
+ targetView.setUint32(position, length);
1282
+ position += 4;
1283
+ }
1284
+ for (let i = 0; i < length; i++) {
1285
+ pack(value[i]);
1259
1286
  }
1260
1287
  };
1261
1288
  const pack = (value) => {
1262
- if (position$1 > safeEnd)
1263
- target = makeRoom(position$1);
1289
+ if (position > safeEnd)
1290
+ target = makeRoom(position);
1264
1291
 
1265
1292
  var type = typeof value;
1266
1293
  var length;
1267
1294
  if (type === 'string') {
1268
1295
  let strLength = value.length;
1269
- if (bundledStrings$1 && strLength >= 4 && strLength < 0x1000) {
1270
- if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1296
+ if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
1297
+ if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1271
1298
  let extStart;
1272
- let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1273
- if (position$1 + maxBytes > safeEnd)
1274
- target = makeRoom(position$1 + maxBytes);
1299
+ let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
1300
+ if (position + maxBytes > safeEnd)
1301
+ target = makeRoom(position + maxBytes);
1275
1302
  let lastBundle;
1276
- if (bundledStrings$1.position) { // here we use the 0x62 extension to write the last bundle and reserve space for the reference pointer to the next/current bundle
1277
- lastBundle = bundledStrings$1;
1278
- target[position$1] = 0xc8; // ext 16
1279
- position$1 += 3; // reserve for the writing bundle size
1280
- target[position$1++] = 0x62; // 'b'
1281
- extStart = position$1 - start;
1282
- position$1 += 4; // reserve for writing bundle reference
1303
+ 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
1304
+ lastBundle = bundledStrings;
1305
+ target[position] = 0xc8; // ext 16
1306
+ position += 3; // reserve for the writing bundle size
1307
+ target[position++] = 0x62; // 'b'
1308
+ extStart = position - start;
1309
+ position += 4; // reserve for writing bundle reference
1283
1310
  writeBundles(start, pack, 0); // write the last bundles
1284
- targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1311
+ targetView.setUint16(extStart + start - 3, position - start - extStart);
1285
1312
  } 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)
1286
- target[position$1++] = 0xd6; // fixext 4
1287
- target[position$1++] = 0x62; // 'b'
1288
- extStart = position$1 - start;
1289
- position$1 += 4; // reserve for writing bundle reference
1313
+ target[position++] = 0xd6; // fixext 4
1314
+ target[position++] = 0x62; // 'b'
1315
+ extStart = position - start;
1316
+ position += 4; // reserve for writing bundle reference
1290
1317
  }
1291
- bundledStrings$1 = ['', '']; // create new ones
1292
- bundledStrings$1.previous = lastBundle;
1293
- bundledStrings$1.size = 0;
1294
- bundledStrings$1.position = extStart;
1318
+ bundledStrings = ['', '']; // create new ones
1319
+ bundledStrings.previous = lastBundle;
1320
+ bundledStrings.size = 0;
1321
+ bundledStrings.position = extStart;
1295
1322
  }
1296
1323
  let twoByte = hasNonLatin.test(value);
1297
- bundledStrings$1[twoByte ? 0 : 1] += value;
1298
- target[position$1++] = 0xc1;
1324
+ bundledStrings[twoByte ? 0 : 1] += value;
1325
+ target[position++] = 0xc1;
1299
1326
  pack(twoByte ? -strLength : strLength);
1300
1327
  return
1301
1328
  }
@@ -1311,11 +1338,11 @@
1311
1338
  headerSize = 5;
1312
1339
  }
1313
1340
  let maxBytes = strLength * 3;
1314
- if (position$1 + maxBytes > safeEnd)
1315
- target = makeRoom(position$1 + maxBytes);
1341
+ if (position + maxBytes > safeEnd)
1342
+ target = makeRoom(position + maxBytes);
1316
1343
 
1317
1344
  if (strLength < 0x40 || !encodeUtf8) {
1318
- let i, c1, c2, strPosition = position$1 + headerSize;
1345
+ let i, c1, c2, strPosition = position + headerSize;
1319
1346
  for (i = 0; i < strLength; i++) {
1320
1347
  c1 = value.charCodeAt(i);
1321
1348
  if (c1 < 0x80) {
@@ -1339,88 +1366,88 @@
1339
1366
  target[strPosition++] = c1 & 0x3f | 0x80;
1340
1367
  }
1341
1368
  }
1342
- length = strPosition - position$1 - headerSize;
1369
+ length = strPosition - position - headerSize;
1343
1370
  } else {
1344
- length = encodeUtf8(value, position$1 + headerSize);
1371
+ length = encodeUtf8(value, position + headerSize);
1345
1372
  }
1346
1373
 
1347
1374
  if (length < 0x20) {
1348
- target[position$1++] = 0xa0 | length;
1375
+ target[position++] = 0xa0 | length;
1349
1376
  } else if (length < 0x100) {
1350
1377
  if (headerSize < 2) {
1351
- target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1378
+ target.copyWithin(position + 2, position + 1, position + 1 + length);
1352
1379
  }
1353
- target[position$1++] = 0xd9;
1354
- target[position$1++] = length;
1380
+ target[position++] = 0xd9;
1381
+ target[position++] = length;
1355
1382
  } else if (length < 0x10000) {
1356
1383
  if (headerSize < 3) {
1357
- target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1384
+ target.copyWithin(position + 3, position + 2, position + 2 + length);
1358
1385
  }
1359
- target[position$1++] = 0xda;
1360
- target[position$1++] = length >> 8;
1361
- target[position$1++] = length & 0xff;
1386
+ target[position++] = 0xda;
1387
+ target[position++] = length >> 8;
1388
+ target[position++] = length & 0xff;
1362
1389
  } else {
1363
1390
  if (headerSize < 5) {
1364
- target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1391
+ target.copyWithin(position + 5, position + 3, position + 3 + length);
1365
1392
  }
1366
- target[position$1++] = 0xdb;
1367
- targetView.setUint32(position$1, length);
1368
- position$1 += 4;
1393
+ target[position++] = 0xdb;
1394
+ targetView.setUint32(position, length);
1395
+ position += 4;
1369
1396
  }
1370
- position$1 += length;
1397
+ position += length;
1371
1398
  } else if (type === 'number') {
1372
1399
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1373
1400
  // positive uint
1374
1401
  if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
1375
- target[position$1++] = value;
1402
+ target[position++] = value;
1376
1403
  } else if (value < 0x100) {
1377
- target[position$1++] = 0xcc;
1378
- target[position$1++] = value;
1404
+ target[position++] = 0xcc;
1405
+ target[position++] = value;
1379
1406
  } else if (value < 0x10000) {
1380
- target[position$1++] = 0xcd;
1381
- target[position$1++] = value >> 8;
1382
- target[position$1++] = value & 0xff;
1407
+ target[position++] = 0xcd;
1408
+ target[position++] = value >> 8;
1409
+ target[position++] = value & 0xff;
1383
1410
  } else {
1384
- target[position$1++] = 0xce;
1385
- targetView.setUint32(position$1, value);
1386
- position$1 += 4;
1411
+ target[position++] = 0xce;
1412
+ targetView.setUint32(position, value);
1413
+ position += 4;
1387
1414
  }
1388
1415
  } else if (value >> 0 === value) { // negative integer
1389
1416
  if (value >= -0x20) {
1390
- target[position$1++] = 0x100 + value;
1417
+ target[position++] = 0x100 + value;
1391
1418
  } else if (value >= -0x80) {
1392
- target[position$1++] = 0xd0;
1393
- target[position$1++] = value + 0x100;
1419
+ target[position++] = 0xd0;
1420
+ target[position++] = value + 0x100;
1394
1421
  } else if (value >= -0x8000) {
1395
- target[position$1++] = 0xd1;
1396
- targetView.setInt16(position$1, value);
1397
- position$1 += 2;
1422
+ target[position++] = 0xd1;
1423
+ targetView.setInt16(position, value);
1424
+ position += 2;
1398
1425
  } else {
1399
- target[position$1++] = 0xd2;
1400
- targetView.setInt32(position$1, value);
1401
- position$1 += 4;
1426
+ target[position++] = 0xd2;
1427
+ targetView.setInt32(position, value);
1428
+ position += 4;
1402
1429
  }
1403
1430
  } else {
1404
1431
  let useFloat32;
1405
1432
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1406
- target[position$1++] = 0xca;
1407
- targetView.setFloat32(position$1, value);
1433
+ target[position++] = 0xca;
1434
+ targetView.setFloat32(position, value);
1408
1435
  let xShifted;
1409
1436
  if (useFloat32 < 4 ||
1410
1437
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1411
- ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1412
- position$1 += 4;
1438
+ ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1439
+ position += 4;
1413
1440
  return
1414
1441
  } else
1415
- position$1--; // move back into position for writing a double
1442
+ position--; // move back into position for writing a double
1416
1443
  }
1417
- target[position$1++] = 0xcb;
1418
- targetView.setFloat64(position$1, value);
1419
- position$1 += 8;
1444
+ target[position++] = 0xcb;
1445
+ targetView.setFloat64(position, value);
1446
+ position += 8;
1420
1447
  }
1421
1448
  } else if (type === 'object') {
1422
1449
  if (!value)
1423
- target[position$1++] = 0xc0;
1450
+ target[position++] = 0xc0;
1424
1451
  else {
1425
1452
  if (referenceMap) {
1426
1453
  let referee = referenceMap.get(value);
@@ -1429,45 +1456,31 @@
1429
1456
  let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1430
1457
  referee.id = idsToInsert.push(referee);
1431
1458
  }
1432
- target[position$1++] = 0xd6; // fixext 4
1433
- target[position$1++] = 0x70; // "p" for pointer
1434
- targetView.setUint32(position$1, referee.id);
1435
- position$1 += 4;
1459
+ target[position++] = 0xd6; // fixext 4
1460
+ target[position++] = 0x70; // "p" for pointer
1461
+ targetView.setUint32(position, referee.id);
1462
+ position += 4;
1436
1463
  return
1437
1464
  } else
1438
- referenceMap.set(value, { offset: position$1 - start });
1465
+ referenceMap.set(value, { offset: position - start });
1439
1466
  }
1440
1467
  let constructor = value.constructor;
1441
1468
  if (constructor === Object) {
1442
1469
  writeObject(value, true);
1443
1470
  } else if (constructor === Array) {
1444
- length = value.length;
1445
- if (length < 0x10) {
1446
- target[position$1++] = 0x90 | length;
1447
- } else if (length < 0x10000) {
1448
- target[position$1++] = 0xdc;
1449
- target[position$1++] = length >> 8;
1450
- target[position$1++] = length & 0xff;
1451
- } else {
1452
- target[position$1++] = 0xdd;
1453
- targetView.setUint32(position$1, length);
1454
- position$1 += 4;
1455
- }
1456
- for (let i = 0; i < length; i++) {
1457
- pack(value[i]);
1458
- }
1471
+ packArray(value);
1459
1472
  } else if (constructor === Map) {
1460
1473
  length = value.size;
1461
1474
  if (length < 0x10) {
1462
- target[position$1++] = 0x80 | length;
1475
+ target[position++] = 0x80 | length;
1463
1476
  } else if (length < 0x10000) {
1464
- target[position$1++] = 0xde;
1465
- target[position$1++] = length >> 8;
1466
- target[position$1++] = length & 0xff;
1477
+ target[position++] = 0xde;
1478
+ target[position++] = length >> 8;
1479
+ target[position++] = length & 0xff;
1467
1480
  } else {
1468
- target[position$1++] = 0xdf;
1469
- targetView.setUint32(position$1, length);
1470
- position$1 += 4;
1481
+ target[position++] = 0xdf;
1482
+ targetView.setUint32(position, length);
1483
+ position += 4;
1471
1484
  }
1472
1485
  for (let [ key, entryValue ] of value) {
1473
1486
  pack(key);
@@ -1480,16 +1493,25 @@
1480
1493
  let extension = extensions[i];
1481
1494
  if (extension.write) {
1482
1495
  if (extension.type) {
1483
- target[position$1++] = 0xd4; // one byte "tag" extension
1484
- target[position$1++] = extension.type;
1485
- target[position$1++] = 0;
1496
+ target[position++] = 0xd4; // one byte "tag" extension
1497
+ target[position++] = extension.type;
1498
+ target[position++] = 0;
1499
+ }
1500
+ let writeResult = extension.write.call(this, value);
1501
+ if (writeResult === value) { // avoid infinite recursion
1502
+ if (Array.isArray(value)) {
1503
+ packArray(value);
1504
+ } else {
1505
+ writeObject(value);
1506
+ }
1507
+ } else {
1508
+ pack(writeResult);
1486
1509
  }
1487
- pack(extension.write.call(this, value));
1488
1510
  return
1489
1511
  }
1490
1512
  let currentTarget = target;
1491
1513
  let currentTargetView = targetView;
1492
- let currentPosition = position$1;
1514
+ let currentPosition = position;
1493
1515
  target = null;
1494
1516
  let result;
1495
1517
  try {
@@ -1497,11 +1519,11 @@
1497
1519
  // restore target and use it
1498
1520
  target = currentTarget;
1499
1521
  currentTarget = null;
1500
- position$1 += size;
1501
- if (position$1 > safeEnd)
1502
- makeRoom(position$1);
1522
+ position += size;
1523
+ if (position > safeEnd)
1524
+ makeRoom(position);
1503
1525
  return {
1504
- target, targetView, position: position$1 - size
1526
+ target, targetView, position: position - size
1505
1527
  }
1506
1528
  }, pack);
1507
1529
  } finally {
@@ -1509,50 +1531,55 @@
1509
1531
  if (currentTarget) {
1510
1532
  target = currentTarget;
1511
1533
  targetView = currentTargetView;
1512
- position$1 = currentPosition;
1534
+ position = currentPosition;
1513
1535
  safeEnd = target.length - 10;
1514
1536
  }
1515
1537
  }
1516
1538
  if (result) {
1517
- if (result.length + position$1 > safeEnd)
1518
- makeRoom(result.length + position$1);
1519
- position$1 = writeExtensionData(result, target, position$1, extension.type);
1539
+ if (result.length + position > safeEnd)
1540
+ makeRoom(result.length + position);
1541
+ position = writeExtensionData(result, target, position, extension.type);
1520
1542
  }
1521
1543
  return
1522
1544
  }
1523
1545
  }
1524
- // no extension found, write as object
1525
- writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1546
+ // check isArray after extensions, because extensions can extend Array
1547
+ if (Array.isArray(value)) {
1548
+ packArray(value);
1549
+ } else {
1550
+ // no extension found, write as object
1551
+ writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1552
+ }
1526
1553
  }
1527
1554
  }
1528
1555
  } else if (type === 'boolean') {
1529
- target[position$1++] = value ? 0xc3 : 0xc2;
1556
+ target[position++] = value ? 0xc3 : 0xc2;
1530
1557
  } else if (type === 'bigint') {
1531
1558
  if (value < (BigInt(1)<<BigInt(63)) && value >= -(BigInt(1)<<BigInt(63))) {
1532
1559
  // use a signed int as long as it fits
1533
- target[position$1++] = 0xd3;
1534
- targetView.setBigInt64(position$1, value);
1560
+ target[position++] = 0xd3;
1561
+ targetView.setBigInt64(position, value);
1535
1562
  } else if (value < (BigInt(1)<<BigInt(64)) && value > 0) {
1536
1563
  // if we can fit an unsigned int, use that
1537
- target[position$1++] = 0xcf;
1538
- targetView.setBigUint64(position$1, value);
1564
+ target[position++] = 0xcf;
1565
+ targetView.setBigUint64(position, value);
1539
1566
  } else {
1540
1567
  // overflow
1541
1568
  if (this.largeBigIntToFloat) {
1542
- target[position$1++] = 0xcb;
1543
- targetView.setFloat64(position$1, Number(value));
1569
+ target[position++] = 0xcb;
1570
+ targetView.setFloat64(position, Number(value));
1544
1571
  } else {
1545
1572
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1546
1573
  }
1547
1574
  }
1548
- position$1 += 8;
1575
+ position += 8;
1549
1576
  } else if (type === 'undefined') {
1550
1577
  if (this.encodeUndefinedAsNil)
1551
- target[position$1++] = 0xc0;
1578
+ target[position++] = 0xc0;
1552
1579
  else {
1553
- target[position$1++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1554
- target[position$1++] = 0;
1555
- target[position$1++] = 0;
1580
+ target[position++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1581
+ target[position++] = 0;
1582
+ target[position++] = 0;
1556
1583
  }
1557
1584
  } else if (type === 'function') {
1558
1585
  pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
@@ -1566,15 +1593,15 @@
1566
1593
  let keys = Object.keys(object);
1567
1594
  let length = keys.length;
1568
1595
  if (length < 0x10) {
1569
- target[position$1++] = 0x80 | length;
1596
+ target[position++] = 0x80 | length;
1570
1597
  } else if (length < 0x10000) {
1571
- target[position$1++] = 0xde;
1572
- target[position$1++] = length >> 8;
1573
- target[position$1++] = length & 0xff;
1598
+ target[position++] = 0xde;
1599
+ target[position++] = length >> 8;
1600
+ target[position++] = length & 0xff;
1574
1601
  } else {
1575
- target[position$1++] = 0xdf;
1576
- targetView.setUint32(position$1, length);
1577
- position$1 += 4;
1602
+ target[position++] = 0xdf;
1603
+ targetView.setUint32(position, length);
1604
+ position += 4;
1578
1605
  }
1579
1606
  let key;
1580
1607
  for (let i = 0; i < length; i++) {
@@ -1583,9 +1610,9 @@
1583
1610
  }
1584
1611
  } :
1585
1612
  (object, safePrototype) => {
1586
- target[position$1++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1587
- let objectOffset = position$1 - start;
1588
- position$1 += 2;
1613
+ target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1614
+ let objectOffset = position - start;
1615
+ position += 2;
1589
1616
  let size = 0;
1590
1617
  for (let key in object) {
1591
1618
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1600,7 +1627,7 @@
1600
1627
  (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)
1601
1628
  (object, safePrototype) => {
1602
1629
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1603
- let objectOffset = position$1++ - start;
1630
+ let objectOffset = position++ - start;
1604
1631
  let wroteKeys;
1605
1632
  for (let key in object) {
1606
1633
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1622,9 +1649,9 @@
1622
1649
  }
1623
1650
  transition = nextTransition;
1624
1651
  }
1625
- if (objectOffset + start + 1 == position$1) {
1652
+ if (objectOffset + start + 1 == position) {
1626
1653
  // first key, so we don't need to insert, we can just write record directly
1627
- position$1--;
1654
+ position--;
1628
1655
  newRecord(transition, keys, newTransitions);
1629
1656
  } else // otherwise we need to insert the record, moving existing data after the record
1630
1657
  insertNewRecord(transition, keys, objectOffset, newTransitions);
@@ -1656,10 +1683,10 @@
1656
1683
  let recordId = transition[RECORD_SYMBOL];
1657
1684
  if (recordId) {
1658
1685
  if (recordId >= 0x60 && useTwoByteRecords) {
1659
- target[position$1++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1660
- target[position$1++] = recordId >> 5;
1686
+ target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1687
+ target[position++] = recordId >> 5;
1661
1688
  } else
1662
- target[position$1++] = recordId;
1689
+ target[position++] = recordId;
1663
1690
  } else {
1664
1691
  newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
1665
1692
  }
@@ -1679,13 +1706,13 @@
1679
1706
  } else // faster handling for smaller buffers
1680
1707
  newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
1681
1708
  let newBuffer = new ByteArrayAllocate(newSize);
1682
- targetView = newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize);
1709
+ targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
1683
1710
  end = Math.min(end, target.length);
1684
1711
  if (target.copy)
1685
1712
  target.copy(newBuffer, 0, start, end);
1686
1713
  else
1687
1714
  newBuffer.set(target.slice(start, end));
1688
- position$1 -= start;
1715
+ position -= start;
1689
1716
  start = 0;
1690
1717
  safeEnd = newBuffer.length - 10;
1691
1718
  return target = newBuffer
@@ -1714,21 +1741,21 @@
1714
1741
  structures.sharedLength = recordId - 0x3f;
1715
1742
  hasSharedUpdate = true;
1716
1743
  if (highByte >= 0) {
1717
- target[position$1++] = (recordId & 0x1f) + 0x60;
1718
- target[position$1++] = highByte;
1744
+ target[position++] = (recordId & 0x1f) + 0x60;
1745
+ target[position++] = highByte;
1719
1746
  } else {
1720
- target[position$1++] = recordId;
1747
+ target[position++] = recordId;
1721
1748
  }
1722
1749
  } else {
1723
1750
  if (highByte >= 0) {
1724
- target[position$1++] = 0xd5; // fixext 2
1725
- target[position$1++] = 0x72; // "r" record defintion extension type
1726
- target[position$1++] = (recordId & 0x1f) + 0x60;
1727
- target[position$1++] = highByte;
1751
+ target[position++] = 0xd5; // fixext 2
1752
+ target[position++] = 0x72; // "r" record defintion extension type
1753
+ target[position++] = (recordId & 0x1f) + 0x60;
1754
+ target[position++] = highByte;
1728
1755
  } else {
1729
- target[position$1++] = 0xd4; // fixext 1
1730
- target[position$1++] = 0x72; // "r" record defintion extension type
1731
- target[position$1++] = recordId;
1756
+ target[position++] = 0xd4; // fixext 1
1757
+ target[position++] = 0x72; // "r" record defintion extension type
1758
+ target[position++] = recordId;
1732
1759
  }
1733
1760
 
1734
1761
  if (newTransitions)
@@ -1742,46 +1769,57 @@
1742
1769
  };
1743
1770
  const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
1744
1771
  let mainTarget = target;
1745
- let mainPosition = position$1;
1772
+ let mainPosition = position;
1746
1773
  let mainSafeEnd = safeEnd;
1747
1774
  let mainStart = start;
1748
1775
  target = keysTarget;
1749
- position$1 = 0;
1776
+ position = 0;
1750
1777
  start = 0;
1751
1778
  if (!target)
1752
1779
  keysTarget = target = new ByteArrayAllocate(8192);
1753
1780
  safeEnd = target.length - 10;
1754
1781
  newRecord(transition, keys, newTransitions);
1755
1782
  keysTarget = target;
1756
- let keysPosition = position$1;
1783
+ let keysPosition = position;
1757
1784
  target = mainTarget;
1758
- position$1 = mainPosition;
1785
+ position = mainPosition;
1759
1786
  safeEnd = mainSafeEnd;
1760
1787
  start = mainStart;
1761
1788
  if (keysPosition > 1) {
1762
- let newEnd = position$1 + keysPosition - 1;
1789
+ let newEnd = position + keysPosition - 1;
1763
1790
  if (newEnd > safeEnd)
1764
1791
  makeRoom(newEnd);
1765
1792
  let insertionPosition = insertionOffset + start;
1766
- target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position$1);
1793
+ target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position);
1767
1794
  target.set(keysTarget.slice(0, keysPosition), insertionPosition);
1768
- position$1 = newEnd;
1795
+ position = newEnd;
1769
1796
  } else {
1770
1797
  target[insertionOffset + start] = keysTarget[0];
1771
1798
  }
1772
1799
  };
1773
1800
  const writeStruct = (object, safePrototype) => {
1774
- let newPosition = writeStructSlots();
1801
+ let newPosition = writeStructSlots(object, target, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1802
+ if (notifySharedUpdate)
1803
+ return hasSharedUpdate = true;
1804
+ position = newPosition;
1805
+ if (start > 0) {
1806
+ pack(value);
1807
+ if (start == 0)
1808
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1809
+ } else
1810
+ pack(value);
1811
+ return position;
1812
+ }, this);
1775
1813
  if (newPosition === 0) // bail and go to a msgpack object
1776
1814
  return writeObject(object, true);
1777
- position$1 = newPosition;
1815
+ position = newPosition;
1778
1816
  };
1779
1817
  }
1780
1818
  useBuffer(buffer) {
1781
1819
  // this means we are finished using our own buffer and we can write over it safely
1782
1820
  target = buffer;
1783
1821
  targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1784
- position$1 = 0;
1822
+ position = 0;
1785
1823
  }
1786
1824
  clearSharedData() {
1787
1825
  if (this.structures)
@@ -1986,17 +2024,17 @@
1986
2024
  }
1987
2025
 
1988
2026
  function writeBundles(start, pack, incrementPosition) {
1989
- if (bundledStrings$1.length > 0) {
1990
- targetView.setUint32(bundledStrings$1.position + start, position$1 + incrementPosition - bundledStrings$1.position - start);
1991
- bundledStrings$1.stringsPosition = position$1 - start;
1992
- let writeStrings = bundledStrings$1;
1993
- bundledStrings$1 = null;
2027
+ if (bundledStrings.length > 0) {
2028
+ targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start);
2029
+ bundledStrings.stringsPosition = position - start;
2030
+ let writeStrings = bundledStrings;
2031
+ bundledStrings = null;
1994
2032
  pack(writeStrings[0]);
1995
2033
  pack(writeStrings[1]);
1996
2034
  }
1997
2035
  }
1998
2036
 
1999
- function addExtension$1(extension) {
2037
+ function addExtension(extension) {
2000
2038
  if (extension.Class) {
2001
2039
  if (!extension.pack && !extension.write)
2002
2040
  throw new Error('Extension has no pack or write function')
@@ -2005,7 +2043,7 @@
2005
2043
  extensionClasses.unshift(extension.Class);
2006
2044
  extensions.unshift(extension);
2007
2045
  }
2008
- addExtension(extension);
2046
+ addExtension$1(extension);
2009
2047
  }
2010
2048
  function prepareStructures(structures, packr) {
2011
2049
  structures.isCompatible = (existingStructures) => {
@@ -2124,7 +2162,7 @@
2124
2162
  exports.Packr = Packr;
2125
2163
  exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
2126
2164
  exports.Unpackr = Unpackr;
2127
- exports.addExtension = addExtension$1;
2165
+ exports.addExtension = addExtension;
2128
2166
  exports.clearSource = clearSource;
2129
2167
  exports.decode = decode;
2130
2168
  exports.decodeIter = decodeIter;
@@ -2138,7 +2176,5 @@
2138
2176
  exports.unpackMultiple = unpackMultiple;
2139
2177
  exports.useRecords = useRecords;
2140
2178
 
2141
- Object.defineProperty(exports, '__esModule', { value: true });
2142
-
2143
- })));
2179
+ }));
2144
2180
  //# sourceMappingURL=index.js.map