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/node.cjs CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var stream = require('stream');
6
4
  var module$1 = require('module');
7
5
 
@@ -11,7 +9,7 @@ try {
11
9
  } catch(error) {}
12
10
  var src;
13
11
  var srcEnd;
14
- var position = 0;
12
+ var position$1 = 0;
15
13
  const EMPTY_ARRAY = [];
16
14
  var strings = EMPTY_ARRAY;
17
15
  var stringPosition = 0;
@@ -20,7 +18,7 @@ var currentStructures;
20
18
  var srcString;
21
19
  var srcStringStart = 0;
22
20
  var srcStringEnd = 0;
23
- var bundledStrings;
21
+ var bundledStrings$1;
24
22
  var referenceMap;
25
23
  var currentExtensions = [];
26
24
  var dataView;
@@ -33,7 +31,8 @@ const C1 = new C1Type();
33
31
  C1.name = 'MessagePack 0xC1';
34
32
  var sequentialMode = false;
35
33
  var inlineObjectReadThreshold = 2;
36
- var readStruct, onLoadedStructures, onSaveState;
34
+ var readStruct$1, onLoadedStructures$1, onSaveState;
35
+ // no-eval build
37
36
  try {
38
37
  new Function('');
39
38
  } catch(error) {
@@ -60,29 +59,32 @@ class Unpackr {
60
59
  (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
61
60
  options.structures.sharedLength = 0;
62
61
  }
62
+ if (options.int64AsNumber) {
63
+ options.int64AsType = 'number';
64
+ }
63
65
  }
64
66
  Object.assign(this, options);
65
67
  }
66
68
  unpack(source, options) {
67
69
  if (src) {
68
70
  // re-entrant execution, save the state and restore it after we do this unpack
69
- return saveState(() => {
71
+ return saveState$1(() => {
70
72
  clearSource();
71
73
  return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
72
74
  })
73
75
  }
74
76
  if (typeof options === 'object') {
75
77
  srcEnd = options.end || source.length;
76
- position = options.start || 0;
78
+ position$1 = options.start || 0;
77
79
  } else {
78
- position = 0;
80
+ position$1 = 0;
79
81
  srcEnd = options > -1 ? options : source.length;
80
82
  }
81
83
  stringPosition = 0;
82
84
  srcStringEnd = 0;
83
85
  srcString = null;
84
86
  strings = EMPTY_ARRAY;
85
- bundledStrings = null;
87
+ bundledStrings$1 = null;
86
88
  src = source;
87
89
  // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
88
90
  // technique for getting data from a database where it can be copied into an existing buffer instead of creating
@@ -119,8 +121,8 @@ class Unpackr {
119
121
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
120
122
  if (forEach) {
121
123
  forEach(value);
122
- while(position < size) {
123
- lastPosition = position;
124
+ while(position$1 < size) {
125
+ lastPosition = position$1;
124
126
  if (forEach(checkedRead()) === false) {
125
127
  return
126
128
  }
@@ -128,8 +130,8 @@ class Unpackr {
128
130
  }
129
131
  else {
130
132
  values = [ value ];
131
- while(position < size) {
132
- lastPosition = position;
133
+ while(position$1 < size) {
134
+ lastPosition = position$1;
133
135
  values.push(checkedRead());
134
136
  }
135
137
  return values
@@ -144,8 +146,8 @@ class Unpackr {
144
146
  }
145
147
  }
146
148
  _mergeStructures(loadedStructures, existingStructures) {
147
- if (onLoadedStructures)
148
- loadedStructures = onLoadedStructures.call(this, loadedStructures);
149
+ if (onLoadedStructures$1)
150
+ loadedStructures = onLoadedStructures$1.call(this, loadedStructures);
149
151
  loadedStructures = loadedStructures || [];
150
152
  if (Object.isFrozen(loadedStructures))
151
153
  loadedStructures = loadedStructures.map(structure => structure.slice(0));
@@ -183,18 +185,20 @@ function checkedRead(options) {
183
185
  currentStructures.length = sharedLength;
184
186
  }
185
187
  let result;
186
- if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
187
- result = readStruct(src, position, srcEnd, currentUnpackr);
188
+ if (currentUnpackr.randomAccessStructure && src[position$1] < 0x40 && src[position$1] >= 0x20 && readStruct$1) {
189
+ result = readStruct$1(src, position$1, srcEnd, currentUnpackr);
188
190
  src = null; // dispose of this so that recursive unpack calls don't save state
189
191
  if (!(options && options.lazy) && result)
190
192
  result = result.toJSON();
191
- position = srcEnd;
193
+ position$1 = srcEnd;
192
194
  } else
193
195
  result = read();
194
- if (bundledStrings) // bundled strings to skip past
195
- position = bundledStrings.postBundlePosition;
196
+ if (bundledStrings$1) { // bundled strings to skip past
197
+ position$1 = bundledStrings$1.postBundlePosition;
198
+ bundledStrings$1 = null;
199
+ }
196
200
 
197
- if (position == srcEnd) {
201
+ if (position$1 == srcEnd) {
198
202
  // finished reading this source, cleanup references
199
203
  if (currentStructures && currentStructures.restoreStructures)
200
204
  restoreStructures();
@@ -202,7 +206,7 @@ function checkedRead(options) {
202
206
  src = null;
203
207
  if (referenceMap)
204
208
  referenceMap = null;
205
- } else if (position > srcEnd) {
209
+ } else if (position$1 > srcEnd) {
206
210
  // over read
207
211
  throw new Error('Unexpected end of MessagePack data')
208
212
  } else if (!sequentialMode) {
@@ -214,7 +218,7 @@ function checkedRead(options) {
214
218
  if (currentStructures && currentStructures.restoreStructures)
215
219
  restoreStructures();
216
220
  clearSource();
217
- if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
221
+ if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
218
222
  error.incomplete = true;
219
223
  }
220
224
  throw error
@@ -229,7 +233,7 @@ function restoreStructures() {
229
233
  }
230
234
 
231
235
  function read() {
232
- let token = src[position++];
236
+ let token = src[position$1++];
233
237
  if (token < 0xa0) {
234
238
  if (token < 0x80) {
235
239
  if (token < 0x40)
@@ -277,8 +281,8 @@ function read() {
277
281
  } else if (token < 0xc0) {
278
282
  // fixstr
279
283
  let length = token - 0xa0;
280
- if (srcStringEnd >= position) {
281
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
284
+ if (srcStringEnd >= position$1) {
285
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
282
286
  }
283
287
  if (srcStringEnd == 0 && srcEnd < 140) {
284
288
  // for small blocks, avoiding the overhead of the extract call is helpful
@@ -292,124 +296,128 @@ function read() {
292
296
  switch (token) {
293
297
  case 0xc0: return null
294
298
  case 0xc1:
295
- if (bundledStrings) {
299
+ if (bundledStrings$1) {
296
300
  value = read(); // followed by the length of the string in characters (not bytes!)
297
301
  if (value > 0)
298
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
302
+ return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
299
303
  else
300
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
304
+ return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
301
305
  }
302
306
  return C1; // "never-used", return special object to denote that
303
307
  case 0xc2: return false
304
308
  case 0xc3: return true
305
309
  case 0xc4:
306
310
  // bin 8
307
- value = src[position++];
311
+ value = src[position$1++];
308
312
  if (value === undefined)
309
313
  throw new Error('Unexpected end of buffer')
310
314
  return readBin(value)
311
315
  case 0xc5:
312
316
  // bin 16
313
- value = dataView.getUint16(position);
314
- position += 2;
317
+ value = dataView.getUint16(position$1);
318
+ position$1 += 2;
315
319
  return readBin(value)
316
320
  case 0xc6:
317
321
  // bin 32
318
- value = dataView.getUint32(position);
319
- position += 4;
322
+ value = dataView.getUint32(position$1);
323
+ position$1 += 4;
320
324
  return readBin(value)
321
325
  case 0xc7:
322
326
  // ext 8
323
- return readExt(src[position++])
327
+ return readExt(src[position$1++])
324
328
  case 0xc8:
325
329
  // ext 16
326
- value = dataView.getUint16(position);
327
- position += 2;
330
+ value = dataView.getUint16(position$1);
331
+ position$1 += 2;
328
332
  return readExt(value)
329
333
  case 0xc9:
330
334
  // ext 32
331
- value = dataView.getUint32(position);
332
- position += 4;
335
+ value = dataView.getUint32(position$1);
336
+ position$1 += 4;
333
337
  return readExt(value)
334
338
  case 0xca:
335
- value = dataView.getFloat32(position);
339
+ value = dataView.getFloat32(position$1);
336
340
  if (currentUnpackr.useFloat32 > 2) {
337
341
  // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
338
- let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
339
- position += 4;
342
+ let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
343
+ position$1 += 4;
340
344
  return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
341
345
  }
342
- position += 4;
346
+ position$1 += 4;
343
347
  return value
344
348
  case 0xcb:
345
- value = dataView.getFloat64(position);
346
- position += 8;
349
+ value = dataView.getFloat64(position$1);
350
+ position$1 += 8;
347
351
  return value
348
352
  // uint handlers
349
353
  case 0xcc:
350
- return src[position++]
354
+ return src[position$1++]
351
355
  case 0xcd:
352
- value = dataView.getUint16(position);
353
- position += 2;
356
+ value = dataView.getUint16(position$1);
357
+ position$1 += 2;
354
358
  return value
355
359
  case 0xce:
356
- value = dataView.getUint32(position);
357
- position += 4;
360
+ value = dataView.getUint32(position$1);
361
+ position$1 += 4;
358
362
  return value
359
363
  case 0xcf:
360
- if (currentUnpackr.int64AsNumber) {
361
- value = dataView.getUint32(position) * 0x100000000;
362
- value += dataView.getUint32(position + 4);
364
+ if (currentUnpackr.int64AsType === 'number') {
365
+ value = dataView.getUint32(position$1) * 0x100000000;
366
+ value += dataView.getUint32(position$1 + 4);
367
+ } else if (currentUnpackr.int64AsType === 'string') {
368
+ value = dataView.getBigUint64(position$1).toString();
363
369
  } else
364
- value = dataView.getBigUint64(position);
365
- position += 8;
370
+ value = dataView.getBigUint64(position$1);
371
+ position$1 += 8;
366
372
  return value
367
373
 
368
374
  // int handlers
369
375
  case 0xd0:
370
- return dataView.getInt8(position++)
376
+ return dataView.getInt8(position$1++)
371
377
  case 0xd1:
372
- value = dataView.getInt16(position);
373
- position += 2;
378
+ value = dataView.getInt16(position$1);
379
+ position$1 += 2;
374
380
  return value
375
381
  case 0xd2:
376
- value = dataView.getInt32(position);
377
- position += 4;
382
+ value = dataView.getInt32(position$1);
383
+ position$1 += 4;
378
384
  return value
379
385
  case 0xd3:
380
- if (currentUnpackr.int64AsNumber) {
381
- value = dataView.getInt32(position) * 0x100000000;
382
- value += dataView.getUint32(position + 4);
386
+ if (currentUnpackr.int64AsType === 'number') {
387
+ value = dataView.getInt32(position$1) * 0x100000000;
388
+ value += dataView.getUint32(position$1 + 4);
389
+ } else if (currentUnpackr.int64AsType === 'string') {
390
+ value = dataView.getBigInt64(position$1).toString();
383
391
  } else
384
- value = dataView.getBigInt64(position);
385
- position += 8;
392
+ value = dataView.getBigInt64(position$1);
393
+ position$1 += 8;
386
394
  return value
387
395
 
388
396
  case 0xd4:
389
397
  // fixext 1
390
- value = src[position++];
398
+ value = src[position$1++];
391
399
  if (value == 0x72) {
392
- return recordDefinition(src[position++] & 0x3f)
400
+ return recordDefinition(src[position$1++] & 0x3f)
393
401
  } else {
394
402
  let extension = currentExtensions[value];
395
403
  if (extension) {
396
404
  if (extension.read) {
397
- position++; // skip filler byte
405
+ position$1++; // skip filler byte
398
406
  return extension.read(read())
399
407
  } else if (extension.noBuffer) {
400
- position++; // skip filler byte
408
+ position$1++; // skip filler byte
401
409
  return extension()
402
410
  } else
403
- return extension(src.subarray(position, ++position))
411
+ return extension(src.subarray(position$1, ++position$1))
404
412
  } else
405
413
  throw new Error('Unknown extension ' + value)
406
414
  }
407
415
  case 0xd5:
408
416
  // fixext 2
409
- value = src[position];
417
+ value = src[position$1];
410
418
  if (value == 0x72) {
411
- position++;
412
- return recordDefinition(src[position++] & 0x3f, src[position++])
419
+ position$1++;
420
+ return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
413
421
  } else
414
422
  return readExt(2)
415
423
  case 0xd6:
@@ -423,46 +431,46 @@ function read() {
423
431
  return readExt(16)
424
432
  case 0xd9:
425
433
  // str 8
426
- value = src[position++];
427
- if (srcStringEnd >= position) {
428
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
434
+ value = src[position$1++];
435
+ if (srcStringEnd >= position$1) {
436
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
429
437
  }
430
438
  return readString8(value)
431
439
  case 0xda:
432
440
  // str 16
433
- value = dataView.getUint16(position);
434
- position += 2;
435
- if (srcStringEnd >= position) {
436
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
441
+ value = dataView.getUint16(position$1);
442
+ position$1 += 2;
443
+ if (srcStringEnd >= position$1) {
444
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
437
445
  }
438
446
  return readString16(value)
439
447
  case 0xdb:
440
448
  // str 32
441
- value = dataView.getUint32(position);
442
- position += 4;
443
- if (srcStringEnd >= position) {
444
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
449
+ value = dataView.getUint32(position$1);
450
+ position$1 += 4;
451
+ if (srcStringEnd >= position$1) {
452
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
445
453
  }
446
454
  return readString32(value)
447
455
  case 0xdc:
448
456
  // array 16
449
- value = dataView.getUint16(position);
450
- position += 2;
457
+ value = dataView.getUint16(position$1);
458
+ position$1 += 2;
451
459
  return readArray(value)
452
460
  case 0xdd:
453
461
  // array 32
454
- value = dataView.getUint32(position);
455
- position += 4;
462
+ value = dataView.getUint32(position$1);
463
+ position$1 += 4;
456
464
  return readArray(value)
457
465
  case 0xde:
458
466
  // map 16
459
- value = dataView.getUint16(position);
460
- position += 2;
467
+ value = dataView.getUint16(position$1);
468
+ position$1 += 2;
461
469
  return readMap(value)
462
470
  case 0xdf:
463
471
  // map 32
464
- value = dataView.getUint32(position);
465
- position += 4;
472
+ value = dataView.getUint32(position$1);
473
+ position$1 += 4;
466
474
  return readMap(value)
467
475
  default: // negative int
468
476
  if (token >= 0xe0)
@@ -508,7 +516,7 @@ function createStructureReader(structure, firstId) {
508
516
 
509
517
  const createSecondByteReader = (firstId, read0) => {
510
518
  return function() {
511
- let highByte = src[position++];
519
+ let highByte = src[position$1++];
512
520
  if (highByte === 0)
513
521
  return read0()
514
522
  let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
@@ -523,7 +531,7 @@ const createSecondByteReader = (firstId, read0) => {
523
531
  };
524
532
 
525
533
  function loadStructures() {
526
- let loadedStructures = saveState(() => {
534
+ let loadedStructures = saveState$1(() => {
527
535
  // save the state in case getStructures modifies our buffer
528
536
  src = null;
529
537
  return currentUnpackr.getStructures()
@@ -547,9 +555,9 @@ function setExtractor(extractStrings) {
547
555
  return function readString(length) {
548
556
  let string = strings[stringPosition++];
549
557
  if (string == null) {
550
- if (bundledStrings)
558
+ if (bundledStrings$1)
551
559
  return readStringJS(length)
552
- let extraction = extractStrings(position - headerLength, srcEnd, src);
560
+ let extraction = extractStrings(position$1 - headerLength, srcEnd, src);
553
561
  if (typeof extraction == 'string') {
554
562
  string = extraction;
555
563
  strings = EMPTY_ARRAY;
@@ -564,13 +572,13 @@ function setExtractor(extractStrings) {
564
572
  }
565
573
  let srcStringLength = string.length;
566
574
  if (srcStringLength <= length) {
567
- position += length;
575
+ position$1 += length;
568
576
  return string
569
577
  }
570
578
  srcString = string;
571
- srcStringStart = position;
572
- srcStringEnd = position + srcStringLength;
573
- position += length;
579
+ srcStringStart = position$1;
580
+ srcStringEnd = position$1 + srcStringLength;
581
+ position$1 += length;
574
582
  return string.slice(0, length) // we know we just want the beginning
575
583
  }
576
584
  }
@@ -582,29 +590,29 @@ function readStringJS(length) {
582
590
  return result
583
591
  }
584
592
  if (length > 64 && decoder)
585
- return decoder.decode(src.subarray(position, position += length))
586
- const end = position + length;
593
+ return decoder.decode(src.subarray(position$1, position$1 += length))
594
+ const end = position$1 + length;
587
595
  const units = [];
588
596
  result = '';
589
- while (position < end) {
590
- const byte1 = src[position++];
597
+ while (position$1 < end) {
598
+ const byte1 = src[position$1++];
591
599
  if ((byte1 & 0x80) === 0) {
592
600
  // 1 byte
593
601
  units.push(byte1);
594
602
  } else if ((byte1 & 0xe0) === 0xc0) {
595
603
  // 2 bytes
596
- const byte2 = src[position++] & 0x3f;
604
+ const byte2 = src[position$1++] & 0x3f;
597
605
  units.push(((byte1 & 0x1f) << 6) | byte2);
598
606
  } else if ((byte1 & 0xf0) === 0xe0) {
599
607
  // 3 bytes
600
- const byte2 = src[position++] & 0x3f;
601
- const byte3 = src[position++] & 0x3f;
608
+ const byte2 = src[position$1++] & 0x3f;
609
+ const byte3 = src[position$1++] & 0x3f;
602
610
  units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
603
611
  } else if ((byte1 & 0xf8) === 0xf0) {
604
612
  // 4 bytes
605
- const byte2 = src[position++] & 0x3f;
606
- const byte3 = src[position++] & 0x3f;
607
- const byte4 = src[position++] & 0x3f;
613
+ const byte2 = src[position$1++] & 0x3f;
614
+ const byte3 = src[position$1++] & 0x3f;
615
+ const byte4 = src[position$1++] & 0x3f;
608
616
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
609
617
  if (unit > 0xffff) {
610
618
  unit -= 0x10000;
@@ -631,7 +639,7 @@ function readStringJS(length) {
631
639
  function readString(source, start, length) {
632
640
  let existingSrc = src;
633
641
  src = source;
634
- position = start;
642
+ position$1 = start;
635
643
  try {
636
644
  return readStringJS(length);
637
645
  } finally {
@@ -670,12 +678,12 @@ function readMap(length) {
670
678
 
671
679
  var fromCharCode = String.fromCharCode;
672
680
  function longStringInJS(length) {
673
- let start = position;
681
+ let start = position$1;
674
682
  let bytes = new Array(length);
675
683
  for (let i = 0; i < length; i++) {
676
- const byte = src[position++];
684
+ const byte = src[position$1++];
677
685
  if ((byte & 0x80) > 0) {
678
- position = start;
686
+ position$1 = start;
679
687
  return
680
688
  }
681
689
  bytes[i] = byte;
@@ -688,131 +696,131 @@ function shortStringInJS(length) {
688
696
  if (length === 0)
689
697
  return ''
690
698
  else {
691
- let a = src[position++];
699
+ let a = src[position$1++];
692
700
  if ((a & 0x80) > 1) {
693
- position -= 1;
701
+ position$1 -= 1;
694
702
  return
695
703
  }
696
704
  return fromCharCode(a)
697
705
  }
698
706
  } else {
699
- let a = src[position++];
700
- let b = src[position++];
707
+ let a = src[position$1++];
708
+ let b = src[position$1++];
701
709
  if ((a & 0x80) > 0 || (b & 0x80) > 0) {
702
- position -= 2;
710
+ position$1 -= 2;
703
711
  return
704
712
  }
705
713
  if (length < 3)
706
714
  return fromCharCode(a, b)
707
- let c = src[position++];
715
+ let c = src[position$1++];
708
716
  if ((c & 0x80) > 0) {
709
- position -= 3;
717
+ position$1 -= 3;
710
718
  return
711
719
  }
712
720
  return fromCharCode(a, b, c)
713
721
  }
714
722
  } else {
715
- let a = src[position++];
716
- let b = src[position++];
717
- let c = src[position++];
718
- let d = src[position++];
723
+ let a = src[position$1++];
724
+ let b = src[position$1++];
725
+ let c = src[position$1++];
726
+ let d = src[position$1++];
719
727
  if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
720
- position -= 4;
728
+ position$1 -= 4;
721
729
  return
722
730
  }
723
731
  if (length < 6) {
724
732
  if (length === 4)
725
733
  return fromCharCode(a, b, c, d)
726
734
  else {
727
- let e = src[position++];
735
+ let e = src[position$1++];
728
736
  if ((e & 0x80) > 0) {
729
- position -= 5;
737
+ position$1 -= 5;
730
738
  return
731
739
  }
732
740
  return fromCharCode(a, b, c, d, e)
733
741
  }
734
742
  } else if (length < 8) {
735
- let e = src[position++];
736
- let f = src[position++];
743
+ let e = src[position$1++];
744
+ let f = src[position$1++];
737
745
  if ((e & 0x80) > 0 || (f & 0x80) > 0) {
738
- position -= 6;
746
+ position$1 -= 6;
739
747
  return
740
748
  }
741
749
  if (length < 7)
742
750
  return fromCharCode(a, b, c, d, e, f)
743
- let g = src[position++];
751
+ let g = src[position$1++];
744
752
  if ((g & 0x80) > 0) {
745
- position -= 7;
753
+ position$1 -= 7;
746
754
  return
747
755
  }
748
756
  return fromCharCode(a, b, c, d, e, f, g)
749
757
  } else {
750
- let e = src[position++];
751
- let f = src[position++];
752
- let g = src[position++];
753
- let h = src[position++];
758
+ let e = src[position$1++];
759
+ let f = src[position$1++];
760
+ let g = src[position$1++];
761
+ let h = src[position$1++];
754
762
  if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
755
- position -= 8;
763
+ position$1 -= 8;
756
764
  return
757
765
  }
758
766
  if (length < 10) {
759
767
  if (length === 8)
760
768
  return fromCharCode(a, b, c, d, e, f, g, h)
761
769
  else {
762
- let i = src[position++];
770
+ let i = src[position$1++];
763
771
  if ((i & 0x80) > 0) {
764
- position -= 9;
772
+ position$1 -= 9;
765
773
  return
766
774
  }
767
775
  return fromCharCode(a, b, c, d, e, f, g, h, i)
768
776
  }
769
777
  } else if (length < 12) {
770
- let i = src[position++];
771
- let j = src[position++];
778
+ let i = src[position$1++];
779
+ let j = src[position$1++];
772
780
  if ((i & 0x80) > 0 || (j & 0x80) > 0) {
773
- position -= 10;
781
+ position$1 -= 10;
774
782
  return
775
783
  }
776
784
  if (length < 11)
777
785
  return fromCharCode(a, b, c, d, e, f, g, h, i, j)
778
- let k = src[position++];
786
+ let k = src[position$1++];
779
787
  if ((k & 0x80) > 0) {
780
- position -= 11;
788
+ position$1 -= 11;
781
789
  return
782
790
  }
783
791
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
784
792
  } else {
785
- let i = src[position++];
786
- let j = src[position++];
787
- let k = src[position++];
788
- let l = src[position++];
793
+ let i = src[position$1++];
794
+ let j = src[position$1++];
795
+ let k = src[position$1++];
796
+ let l = src[position$1++];
789
797
  if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
790
- position -= 12;
798
+ position$1 -= 12;
791
799
  return
792
800
  }
793
801
  if (length < 14) {
794
802
  if (length === 12)
795
803
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
796
804
  else {
797
- let m = src[position++];
805
+ let m = src[position$1++];
798
806
  if ((m & 0x80) > 0) {
799
- position -= 13;
807
+ position$1 -= 13;
800
808
  return
801
809
  }
802
810
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
803
811
  }
804
812
  } else {
805
- let m = src[position++];
806
- let n = src[position++];
813
+ let m = src[position$1++];
814
+ let n = src[position$1++];
807
815
  if ((m & 0x80) > 0 || (n & 0x80) > 0) {
808
- position -= 14;
816
+ position$1 -= 14;
809
817
  return
810
818
  }
811
819
  if (length < 15)
812
820
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
813
- let o = src[position++];
821
+ let o = src[position$1++];
814
822
  if ((o & 0x80) > 0) {
815
- position -= 15;
823
+ position$1 -= 15;
816
824
  return
817
825
  }
818
826
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
@@ -823,7 +831,7 @@ function shortStringInJS(length) {
823
831
  }
824
832
 
825
833
  function readOnlyJSString() {
826
- let token = src[position++];
834
+ let token = src[position$1++];
827
835
  let length;
828
836
  if (token < 0xc0) {
829
837
  // fixstr
@@ -832,17 +840,17 @@ function readOnlyJSString() {
832
840
  switch(token) {
833
841
  case 0xd9:
834
842
  // str 8
835
- length = src[position++];
843
+ length = src[position$1++];
836
844
  break
837
845
  case 0xda:
838
846
  // str 16
839
- length = dataView.getUint16(position);
840
- position += 2;
847
+ length = dataView.getUint16(position$1);
848
+ position$1 += 2;
841
849
  break
842
850
  case 0xdb:
843
851
  // str 32
844
- length = dataView.getUint32(position);
845
- position += 4;
852
+ length = dataView.getUint32(position$1);
853
+ position$1 += 4;
846
854
  break
847
855
  default:
848
856
  throw new Error('Expected string')
@@ -855,44 +863,44 @@ function readOnlyJSString() {
855
863
  function readBin(length) {
856
864
  return currentUnpackr.copyBuffers ?
857
865
  // specifically use the copying slice (not the node one)
858
- Uint8Array.prototype.slice.call(src, position, position += length) :
859
- src.subarray(position, position += length)
866
+ Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
867
+ src.subarray(position$1, position$1 += length)
860
868
  }
861
869
  function readExt(length) {
862
- let type = src[position++];
870
+ let type = src[position$1++];
863
871
  if (currentExtensions[type]) {
864
872
  let end;
865
- return currentExtensions[type](src.subarray(position, end = (position += length)), (readPosition) => {
866
- position = readPosition;
873
+ return currentExtensions[type](src.subarray(position$1, end = (position$1 += length)), (readPosition) => {
874
+ position$1 = readPosition;
867
875
  try {
868
876
  return read();
869
877
  } finally {
870
- position = end;
878
+ position$1 = end;
871
879
  }
872
880
  })
873
881
  }
874
882
  else
875
- throw new Error('Unknown extension type ' + type)``
883
+ throw new Error('Unknown extension type ' + type)
876
884
  }
877
885
 
878
886
  var keyCache = new Array(4096);
879
887
  function readKey() {
880
- let length = src[position++];
888
+ let length = src[position$1++];
881
889
  if (length >= 0xa0 && length < 0xc0) {
882
890
  // fixstr, potentially use key cache
883
891
  length = length - 0xa0;
884
- if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
885
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
892
+ if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
893
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
886
894
  else if (!(srcStringEnd == 0 && srcEnd < 180))
887
895
  return readFixedString(length)
888
896
  } else { // not cacheable, go back and do a standard read
889
- position--;
897
+ position$1--;
890
898
  return read().toString()
891
899
  }
892
- let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
900
+ let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
893
901
  let entry = keyCache[key];
894
- let checkPosition = position;
895
- let end = position + length - 3;
902
+ let checkPosition = position$1;
903
+ let end = position$1 + length - 3;
896
904
  let chunk;
897
905
  let i = 0;
898
906
  if (entry && entry.bytes == length) {
@@ -913,11 +921,11 @@ function readKey() {
913
921
  }
914
922
  }
915
923
  if (checkPosition === end) {
916
- position = checkPosition;
924
+ position$1 = checkPosition;
917
925
  return entry.string
918
926
  }
919
927
  end -= 3;
920
- checkPosition = position;
928
+ checkPosition = position$1;
921
929
  }
922
930
  entry = [];
923
931
  keyCache[key] = entry;
@@ -965,10 +973,10 @@ currentExtensions[0x65] = () => {
965
973
 
966
974
  currentExtensions[0x69] = (data) => {
967
975
  // id extension (for structured clones)
968
- let id = dataView.getUint32(position - 4);
976
+ let id = dataView.getUint32(position$1 - 4);
969
977
  if (!referenceMap)
970
978
  referenceMap = new Map();
971
- let token = src[position];
979
+ let token = src[position$1];
972
980
  let target;
973
981
  // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
974
982
  // ahead past references to record structure definitions
@@ -988,7 +996,7 @@ currentExtensions[0x69] = (data) => {
988
996
 
989
997
  currentExtensions[0x70] = (data) => {
990
998
  // pointer extension (for structured clones)
991
- let id = dataView.getUint32(position - 4);
999
+ let id = dataView.getUint32(position$1 - 4);
992
1000
  let refEntry = referenceMap.get(id);
993
1001
  refEntry.used = true;
994
1002
  return refEntry.target
@@ -1013,14 +1021,14 @@ currentExtensions[0x78] = () => {
1013
1021
  const TEMP_BUNDLE = [];
1014
1022
  currentExtensions[0x62] = (data) => {
1015
1023
  let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
1016
- let dataPosition = position;
1017
- position += dataSize - data.length;
1018
- bundledStrings = TEMP_BUNDLE;
1019
- bundledStrings = [readOnlyJSString(), readOnlyJSString()];
1020
- bundledStrings.position0 = 0;
1021
- bundledStrings.position1 = 0;
1022
- bundledStrings.postBundlePosition = position;
1023
- position = dataPosition;
1024
+ let dataPosition = position$1;
1025
+ position$1 += dataSize - data.length;
1026
+ bundledStrings$1 = TEMP_BUNDLE;
1027
+ bundledStrings$1 = [readOnlyJSString(), readOnlyJSString()];
1028
+ bundledStrings$1.position0 = 0;
1029
+ bundledStrings$1.position1 = 0;
1030
+ bundledStrings$1.postBundlePosition = position$1;
1031
+ position$1 = dataPosition;
1024
1032
  return read()
1025
1033
  };
1026
1034
 
@@ -1042,18 +1050,18 @@ currentExtensions[0xff] = (data) => {
1042
1050
  // registration of bulk record definition?
1043
1051
  // currentExtensions[0x52] = () =>
1044
1052
 
1045
- function saveState(callback) {
1053
+ function saveState$1(callback) {
1046
1054
  if (onSaveState)
1047
1055
  onSaveState();
1048
1056
  let savedSrcEnd = srcEnd;
1049
- let savedPosition = position;
1057
+ let savedPosition = position$1;
1050
1058
  let savedStringPosition = stringPosition;
1051
1059
  let savedSrcStringStart = srcStringStart;
1052
1060
  let savedSrcStringEnd = srcStringEnd;
1053
1061
  let savedSrcString = srcString;
1054
1062
  let savedStrings = strings;
1055
1063
  let savedReferenceMap = referenceMap;
1056
- let savedBundledStrings = bundledStrings;
1064
+ let savedBundledStrings = bundledStrings$1;
1057
1065
 
1058
1066
  // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1059
1067
  let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
@@ -1063,14 +1071,14 @@ function saveState(callback) {
1063
1071
  let savedSequentialMode = sequentialMode;
1064
1072
  let value = callback();
1065
1073
  srcEnd = savedSrcEnd;
1066
- position = savedPosition;
1074
+ position$1 = savedPosition;
1067
1075
  stringPosition = savedStringPosition;
1068
1076
  srcStringStart = savedSrcStringStart;
1069
1077
  srcStringEnd = savedSrcStringEnd;
1070
1078
  srcString = savedSrcString;
1071
1079
  strings = savedStrings;
1072
1080
  referenceMap = savedReferenceMap;
1073
- bundledStrings = savedBundledStrings;
1081
+ bundledStrings$1 = savedBundledStrings;
1074
1082
  src = savedSrc;
1075
1083
  sequentialMode = savedSequentialMode;
1076
1084
  currentStructures = savedStructures;
@@ -1085,7 +1093,7 @@ function clearSource() {
1085
1093
  currentStructures = null;
1086
1094
  }
1087
1095
 
1088
- function addExtension(extension) {
1096
+ function addExtension$1(extension) {
1089
1097
  if (extension.unpack)
1090
1098
  currentExtensions[extension.type] = extension.unpack;
1091
1099
  else
@@ -1115,26 +1123,26 @@ function roundFloat32(float32Number) {
1115
1123
  return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1116
1124
  }
1117
1125
  function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1118
- readStruct = updatedReadStruct;
1119
- onLoadedStructures = loadedStructs;
1126
+ readStruct$1 = updatedReadStruct;
1127
+ onLoadedStructures$1 = loadedStructs;
1120
1128
  onSaveState = saveState;
1121
1129
  }
1122
1130
 
1123
- let textEncoder;
1131
+ let textEncoder$1;
1124
1132
  try {
1125
- textEncoder = new TextEncoder();
1133
+ textEncoder$1 = new TextEncoder();
1126
1134
  } catch (error) {}
1127
1135
  let extensions, extensionClasses;
1128
- const hasNodeBuffer = typeof Buffer !== 'undefined';
1129
- const ByteArrayAllocate = hasNodeBuffer ?
1136
+ const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
1137
+ const ByteArrayAllocate = hasNodeBuffer$1 ?
1130
1138
  function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
1131
- const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1132
- const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1139
+ const ByteArray = hasNodeBuffer$1 ? Buffer : Uint8Array;
1140
+ const MAX_BUFFER_SIZE = hasNodeBuffer$1 ? 0x100000000 : 0x7fd00000;
1133
1141
  let target, keysTarget;
1134
1142
  let targetView;
1135
- let position$1 = 0;
1143
+ let position = 0;
1136
1144
  let safeEnd;
1137
- let bundledStrings$1 = null;
1145
+ let bundledStrings = null;
1138
1146
  let writeStructSlots;
1139
1147
  const MAX_BUNDLE_SIZE = 0xf000;
1140
1148
  const hasNonLatin = /[\u0080-\uFFFF]/;
@@ -1149,9 +1157,9 @@ class Packr extends Unpackr {
1149
1157
  let referenceMap;
1150
1158
  let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1151
1159
  return target.utf8Write(string, position, 0xffffffff)
1152
- } : (textEncoder && textEncoder.encodeInto) ?
1160
+ } : (textEncoder$1 && textEncoder$1.encodeInto) ?
1153
1161
  function(string, position) {
1154
- return textEncoder.encodeInto(string, target.subarray(position)).written
1162
+ return textEncoder$1.encodeInto(string, target.subarray(position)).written
1155
1163
  } : false;
1156
1164
 
1157
1165
  let packr = this;
@@ -1186,25 +1194,25 @@ class Packr extends Unpackr {
1186
1194
  this.pack = this.encode = function(value, encodeOptions) {
1187
1195
  if (!target) {
1188
1196
  target = new ByteArrayAllocate(8192);
1189
- targetView = target.dataView = new DataView(target.buffer, 0, 8192);
1190
- position$1 = 0;
1197
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
1198
+ position = 0;
1191
1199
  }
1192
1200
  safeEnd = target.length - 10;
1193
- if (safeEnd - position$1 < 0x800) {
1201
+ if (safeEnd - position < 0x800) {
1194
1202
  // don't start too close to the end,
1195
1203
  target = new ByteArrayAllocate(target.length);
1196
- targetView = target.dataView = new DataView(target.buffer, 0, target.length);
1204
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1197
1205
  safeEnd = target.length - 10;
1198
- position$1 = 0;
1206
+ position = 0;
1199
1207
  } else
1200
- position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1201
- start = position$1;
1208
+ position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1209
+ start = position;
1202
1210
  referenceMap = packr.structuredClone ? new Map() : null;
1203
1211
  if (packr.bundleStrings && typeof value !== 'string') {
1204
- bundledStrings$1 = [];
1205
- bundledStrings$1.size = Infinity; // force a new bundle start on first string
1212
+ bundledStrings = [];
1213
+ bundledStrings.size = Infinity; // force a new bundle start on first string
1206
1214
  } else
1207
- bundledStrings$1 = null;
1215
+ bundledStrings = null;
1208
1216
  structures = packr.structures;
1209
1217
  if (structures) {
1210
1218
  if (structures.uninitialized)
@@ -1245,8 +1253,8 @@ class Packr extends Unpackr {
1245
1253
  writeStruct(value);
1246
1254
  else
1247
1255
  pack(value);
1248
- let lastBundle = bundledStrings$1;
1249
- if (bundledStrings$1)
1256
+ let lastBundle = bundledStrings;
1257
+ if (bundledStrings)
1250
1258
  writeBundles(start, pack, 0);
1251
1259
  if (referenceMap && referenceMap.idsToInsert) {
1252
1260
  let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
@@ -1275,21 +1283,21 @@ class Packr extends Unpackr {
1275
1283
  targetView.setUint32(lastBundle.position + start,
1276
1284
  targetView.getUint32(lastBundle.position + start) + incrementPosition);
1277
1285
  }
1278
- position$1 += idsToInsert.length * 6;
1279
- if (position$1 > safeEnd)
1280
- makeRoom(position$1);
1281
- packr.offset = position$1;
1282
- let serialized = insertIds(target.subarray(start, position$1), idsToInsert);
1286
+ position += idsToInsert.length * 6;
1287
+ if (position > safeEnd)
1288
+ makeRoom(position);
1289
+ packr.offset = position;
1290
+ let serialized = insertIds(target.subarray(start, position), idsToInsert);
1283
1291
  referenceMap = null;
1284
1292
  return serialized
1285
1293
  }
1286
- packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1294
+ packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1287
1295
  if (encodeOptions & REUSE_BUFFER_MODE) {
1288
1296
  target.start = start;
1289
- target.end = position$1;
1297
+ target.end = position;
1290
1298
  return target
1291
1299
  }
1292
- return target.subarray(start, position$1) // position can change if we call pack again in saveStructures, so we get the buffer now
1300
+ return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1293
1301
  } finally {
1294
1302
  if (structures) {
1295
1303
  if (serializationsSinceTransitionRebuild < 10)
@@ -1312,8 +1320,8 @@ class Packr extends Unpackr {
1312
1320
  }
1313
1321
  if (hasSharedUpdate && packr.saveStructures) {
1314
1322
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1315
- let returnBuffer = target.subarray(start, position$1);
1316
- let newSharedData = prepareStructures(structures, packr);
1323
+ let returnBuffer = target.subarray(start, position);
1324
+ let newSharedData = prepareStructures$1(structures, packr);
1317
1325
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1318
1326
  // get updated structures and try again if the update failed
1319
1327
  return packr.pack(value)
@@ -1323,47 +1331,64 @@ class Packr extends Unpackr {
1323
1331
  }
1324
1332
  }
1325
1333
  if (encodeOptions & RESET_BUFFER_MODE)
1326
- position$1 = start;
1334
+ position = start;
1335
+ }
1336
+ };
1337
+ const packArray = (value) => {
1338
+ var length = value.length;
1339
+ if (length < 0x10) {
1340
+ target[position++] = 0x90 | length;
1341
+ } else if (length < 0x10000) {
1342
+ target[position++] = 0xdc;
1343
+ target[position++] = length >> 8;
1344
+ target[position++] = length & 0xff;
1345
+ } else {
1346
+ target[position++] = 0xdd;
1347
+ targetView.setUint32(position, length);
1348
+ position += 4;
1349
+ }
1350
+ for (let i = 0; i < length; i++) {
1351
+ pack(value[i]);
1327
1352
  }
1328
1353
  };
1329
1354
  const pack = (value) => {
1330
- if (position$1 > safeEnd)
1331
- target = makeRoom(position$1);
1355
+ if (position > safeEnd)
1356
+ target = makeRoom(position);
1332
1357
 
1333
1358
  var type = typeof value;
1334
1359
  var length;
1335
1360
  if (type === 'string') {
1336
1361
  let strLength = value.length;
1337
- if (bundledStrings$1 && strLength >= 4 && strLength < 0x1000) {
1338
- if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1362
+ if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
1363
+ if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1339
1364
  let extStart;
1340
- let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1341
- if (position$1 + maxBytes > safeEnd)
1342
- target = makeRoom(position$1 + maxBytes);
1365
+ let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
1366
+ if (position + maxBytes > safeEnd)
1367
+ target = makeRoom(position + maxBytes);
1343
1368
  let lastBundle;
1344
- 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
1345
- lastBundle = bundledStrings$1;
1346
- target[position$1] = 0xc8; // ext 16
1347
- position$1 += 3; // reserve for the writing bundle size
1348
- target[position$1++] = 0x62; // 'b'
1349
- extStart = position$1 - start;
1350
- position$1 += 4; // reserve for writing bundle reference
1369
+ 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
1370
+ lastBundle = bundledStrings;
1371
+ target[position] = 0xc8; // ext 16
1372
+ position += 3; // reserve for the writing bundle size
1373
+ target[position++] = 0x62; // 'b'
1374
+ extStart = position - start;
1375
+ position += 4; // reserve for writing bundle reference
1351
1376
  writeBundles(start, pack, 0); // write the last bundles
1352
- targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1377
+ targetView.setUint16(extStart + start - 3, position - start - extStart);
1353
1378
  } 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)
1354
- target[position$1++] = 0xd6; // fixext 4
1355
- target[position$1++] = 0x62; // 'b'
1356
- extStart = position$1 - start;
1357
- position$1 += 4; // reserve for writing bundle reference
1379
+ target[position++] = 0xd6; // fixext 4
1380
+ target[position++] = 0x62; // 'b'
1381
+ extStart = position - start;
1382
+ position += 4; // reserve for writing bundle reference
1358
1383
  }
1359
- bundledStrings$1 = ['', '']; // create new ones
1360
- bundledStrings$1.previous = lastBundle;
1361
- bundledStrings$1.size = 0;
1362
- bundledStrings$1.position = extStart;
1384
+ bundledStrings = ['', '']; // create new ones
1385
+ bundledStrings.previous = lastBundle;
1386
+ bundledStrings.size = 0;
1387
+ bundledStrings.position = extStart;
1363
1388
  }
1364
1389
  let twoByte = hasNonLatin.test(value);
1365
- bundledStrings$1[twoByte ? 0 : 1] += value;
1366
- target[position$1++] = 0xc1;
1390
+ bundledStrings[twoByte ? 0 : 1] += value;
1391
+ target[position++] = 0xc1;
1367
1392
  pack(twoByte ? -strLength : strLength);
1368
1393
  return
1369
1394
  }
@@ -1379,11 +1404,11 @@ class Packr extends Unpackr {
1379
1404
  headerSize = 5;
1380
1405
  }
1381
1406
  let maxBytes = strLength * 3;
1382
- if (position$1 + maxBytes > safeEnd)
1383
- target = makeRoom(position$1 + maxBytes);
1407
+ if (position + maxBytes > safeEnd)
1408
+ target = makeRoom(position + maxBytes);
1384
1409
 
1385
1410
  if (strLength < 0x40 || !encodeUtf8) {
1386
- let i, c1, c2, strPosition = position$1 + headerSize;
1411
+ let i, c1, c2, strPosition = position + headerSize;
1387
1412
  for (i = 0; i < strLength; i++) {
1388
1413
  c1 = value.charCodeAt(i);
1389
1414
  if (c1 < 0x80) {
@@ -1407,88 +1432,88 @@ class Packr extends Unpackr {
1407
1432
  target[strPosition++] = c1 & 0x3f | 0x80;
1408
1433
  }
1409
1434
  }
1410
- length = strPosition - position$1 - headerSize;
1435
+ length = strPosition - position - headerSize;
1411
1436
  } else {
1412
- length = encodeUtf8(value, position$1 + headerSize);
1437
+ length = encodeUtf8(value, position + headerSize);
1413
1438
  }
1414
1439
 
1415
1440
  if (length < 0x20) {
1416
- target[position$1++] = 0xa0 | length;
1441
+ target[position++] = 0xa0 | length;
1417
1442
  } else if (length < 0x100) {
1418
1443
  if (headerSize < 2) {
1419
- target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1444
+ target.copyWithin(position + 2, position + 1, position + 1 + length);
1420
1445
  }
1421
- target[position$1++] = 0xd9;
1422
- target[position$1++] = length;
1446
+ target[position++] = 0xd9;
1447
+ target[position++] = length;
1423
1448
  } else if (length < 0x10000) {
1424
1449
  if (headerSize < 3) {
1425
- target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1450
+ target.copyWithin(position + 3, position + 2, position + 2 + length);
1426
1451
  }
1427
- target[position$1++] = 0xda;
1428
- target[position$1++] = length >> 8;
1429
- target[position$1++] = length & 0xff;
1452
+ target[position++] = 0xda;
1453
+ target[position++] = length >> 8;
1454
+ target[position++] = length & 0xff;
1430
1455
  } else {
1431
1456
  if (headerSize < 5) {
1432
- target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1457
+ target.copyWithin(position + 5, position + 3, position + 3 + length);
1433
1458
  }
1434
- target[position$1++] = 0xdb;
1435
- targetView.setUint32(position$1, length);
1436
- position$1 += 4;
1459
+ target[position++] = 0xdb;
1460
+ targetView.setUint32(position, length);
1461
+ position += 4;
1437
1462
  }
1438
- position$1 += length;
1463
+ position += length;
1439
1464
  } else if (type === 'number') {
1440
1465
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1441
1466
  // positive uint
1442
1467
  if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
1443
- target[position$1++] = value;
1468
+ target[position++] = value;
1444
1469
  } else if (value < 0x100) {
1445
- target[position$1++] = 0xcc;
1446
- target[position$1++] = value;
1470
+ target[position++] = 0xcc;
1471
+ target[position++] = value;
1447
1472
  } else if (value < 0x10000) {
1448
- target[position$1++] = 0xcd;
1449
- target[position$1++] = value >> 8;
1450
- target[position$1++] = value & 0xff;
1473
+ target[position++] = 0xcd;
1474
+ target[position++] = value >> 8;
1475
+ target[position++] = value & 0xff;
1451
1476
  } else {
1452
- target[position$1++] = 0xce;
1453
- targetView.setUint32(position$1, value);
1454
- position$1 += 4;
1477
+ target[position++] = 0xce;
1478
+ targetView.setUint32(position, value);
1479
+ position += 4;
1455
1480
  }
1456
1481
  } else if (value >> 0 === value) { // negative integer
1457
1482
  if (value >= -0x20) {
1458
- target[position$1++] = 0x100 + value;
1483
+ target[position++] = 0x100 + value;
1459
1484
  } else if (value >= -0x80) {
1460
- target[position$1++] = 0xd0;
1461
- target[position$1++] = value + 0x100;
1485
+ target[position++] = 0xd0;
1486
+ target[position++] = value + 0x100;
1462
1487
  } else if (value >= -0x8000) {
1463
- target[position$1++] = 0xd1;
1464
- targetView.setInt16(position$1, value);
1465
- position$1 += 2;
1488
+ target[position++] = 0xd1;
1489
+ targetView.setInt16(position, value);
1490
+ position += 2;
1466
1491
  } else {
1467
- target[position$1++] = 0xd2;
1468
- targetView.setInt32(position$1, value);
1469
- position$1 += 4;
1492
+ target[position++] = 0xd2;
1493
+ targetView.setInt32(position, value);
1494
+ position += 4;
1470
1495
  }
1471
1496
  } else {
1472
1497
  let useFloat32;
1473
1498
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1474
- target[position$1++] = 0xca;
1475
- targetView.setFloat32(position$1, value);
1499
+ target[position++] = 0xca;
1500
+ targetView.setFloat32(position, value);
1476
1501
  let xShifted;
1477
1502
  if (useFloat32 < 4 ||
1478
1503
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1479
- ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1480
- position$1 += 4;
1504
+ ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1505
+ position += 4;
1481
1506
  return
1482
1507
  } else
1483
- position$1--; // move back into position for writing a double
1508
+ position--; // move back into position for writing a double
1484
1509
  }
1485
- target[position$1++] = 0xcb;
1486
- targetView.setFloat64(position$1, value);
1487
- position$1 += 8;
1510
+ target[position++] = 0xcb;
1511
+ targetView.setFloat64(position, value);
1512
+ position += 8;
1488
1513
  }
1489
1514
  } else if (type === 'object') {
1490
1515
  if (!value)
1491
- target[position$1++] = 0xc0;
1516
+ target[position++] = 0xc0;
1492
1517
  else {
1493
1518
  if (referenceMap) {
1494
1519
  let referee = referenceMap.get(value);
@@ -1497,45 +1522,31 @@ class Packr extends Unpackr {
1497
1522
  let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1498
1523
  referee.id = idsToInsert.push(referee);
1499
1524
  }
1500
- target[position$1++] = 0xd6; // fixext 4
1501
- target[position$1++] = 0x70; // "p" for pointer
1502
- targetView.setUint32(position$1, referee.id);
1503
- position$1 += 4;
1525
+ target[position++] = 0xd6; // fixext 4
1526
+ target[position++] = 0x70; // "p" for pointer
1527
+ targetView.setUint32(position, referee.id);
1528
+ position += 4;
1504
1529
  return
1505
1530
  } else
1506
- referenceMap.set(value, { offset: position$1 - start });
1531
+ referenceMap.set(value, { offset: position - start });
1507
1532
  }
1508
1533
  let constructor = value.constructor;
1509
1534
  if (constructor === Object) {
1510
1535
  writeObject(value, true);
1511
1536
  } else if (constructor === Array) {
1512
- length = value.length;
1513
- if (length < 0x10) {
1514
- target[position$1++] = 0x90 | length;
1515
- } else if (length < 0x10000) {
1516
- target[position$1++] = 0xdc;
1517
- target[position$1++] = length >> 8;
1518
- target[position$1++] = length & 0xff;
1519
- } else {
1520
- target[position$1++] = 0xdd;
1521
- targetView.setUint32(position$1, length);
1522
- position$1 += 4;
1523
- }
1524
- for (let i = 0; i < length; i++) {
1525
- pack(value[i]);
1526
- }
1537
+ packArray(value);
1527
1538
  } else if (constructor === Map) {
1528
1539
  length = value.size;
1529
1540
  if (length < 0x10) {
1530
- target[position$1++] = 0x80 | length;
1541
+ target[position++] = 0x80 | length;
1531
1542
  } else if (length < 0x10000) {
1532
- target[position$1++] = 0xde;
1533
- target[position$1++] = length >> 8;
1534
- target[position$1++] = length & 0xff;
1543
+ target[position++] = 0xde;
1544
+ target[position++] = length >> 8;
1545
+ target[position++] = length & 0xff;
1535
1546
  } else {
1536
- target[position$1++] = 0xdf;
1537
- targetView.setUint32(position$1, length);
1538
- position$1 += 4;
1547
+ target[position++] = 0xdf;
1548
+ targetView.setUint32(position, length);
1549
+ position += 4;
1539
1550
  }
1540
1551
  for (let [ key, entryValue ] of value) {
1541
1552
  pack(key);
@@ -1548,16 +1559,25 @@ class Packr extends Unpackr {
1548
1559
  let extension = extensions[i];
1549
1560
  if (extension.write) {
1550
1561
  if (extension.type) {
1551
- target[position$1++] = 0xd4; // one byte "tag" extension
1552
- target[position$1++] = extension.type;
1553
- target[position$1++] = 0;
1562
+ target[position++] = 0xd4; // one byte "tag" extension
1563
+ target[position++] = extension.type;
1564
+ target[position++] = 0;
1565
+ }
1566
+ let writeResult = extension.write.call(this, value);
1567
+ if (writeResult === value) { // avoid infinite recursion
1568
+ if (Array.isArray(value)) {
1569
+ packArray(value);
1570
+ } else {
1571
+ writeObject(value);
1572
+ }
1573
+ } else {
1574
+ pack(writeResult);
1554
1575
  }
1555
- pack(extension.write.call(this, value));
1556
1576
  return
1557
1577
  }
1558
1578
  let currentTarget = target;
1559
1579
  let currentTargetView = targetView;
1560
- let currentPosition = position$1;
1580
+ let currentPosition = position;
1561
1581
  target = null;
1562
1582
  let result;
1563
1583
  try {
@@ -1565,11 +1585,11 @@ class Packr extends Unpackr {
1565
1585
  // restore target and use it
1566
1586
  target = currentTarget;
1567
1587
  currentTarget = null;
1568
- position$1 += size;
1569
- if (position$1 > safeEnd)
1570
- makeRoom(position$1);
1588
+ position += size;
1589
+ if (position > safeEnd)
1590
+ makeRoom(position);
1571
1591
  return {
1572
- target, targetView, position: position$1 - size
1592
+ target, targetView, position: position - size
1573
1593
  }
1574
1594
  }, pack);
1575
1595
  } finally {
@@ -1577,50 +1597,55 @@ class Packr extends Unpackr {
1577
1597
  if (currentTarget) {
1578
1598
  target = currentTarget;
1579
1599
  targetView = currentTargetView;
1580
- position$1 = currentPosition;
1600
+ position = currentPosition;
1581
1601
  safeEnd = target.length - 10;
1582
1602
  }
1583
1603
  }
1584
1604
  if (result) {
1585
- if (result.length + position$1 > safeEnd)
1586
- makeRoom(result.length + position$1);
1587
- position$1 = writeExtensionData(result, target, position$1, extension.type);
1605
+ if (result.length + position > safeEnd)
1606
+ makeRoom(result.length + position);
1607
+ position = writeExtensionData(result, target, position, extension.type);
1588
1608
  }
1589
1609
  return
1590
1610
  }
1591
1611
  }
1592
- // no extension found, write as object
1593
- writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1612
+ // check isArray after extensions, because extensions can extend Array
1613
+ if (Array.isArray(value)) {
1614
+ packArray(value);
1615
+ } else {
1616
+ // no extension found, write as object
1617
+ writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1618
+ }
1594
1619
  }
1595
1620
  }
1596
1621
  } else if (type === 'boolean') {
1597
- target[position$1++] = value ? 0xc3 : 0xc2;
1622
+ target[position++] = value ? 0xc3 : 0xc2;
1598
1623
  } else if (type === 'bigint') {
1599
1624
  if (value < (BigInt(1)<<BigInt(63)) && value >= -(BigInt(1)<<BigInt(63))) {
1600
1625
  // use a signed int as long as it fits
1601
- target[position$1++] = 0xd3;
1602
- targetView.setBigInt64(position$1, value);
1626
+ target[position++] = 0xd3;
1627
+ targetView.setBigInt64(position, value);
1603
1628
  } else if (value < (BigInt(1)<<BigInt(64)) && value > 0) {
1604
1629
  // if we can fit an unsigned int, use that
1605
- target[position$1++] = 0xcf;
1606
- targetView.setBigUint64(position$1, value);
1630
+ target[position++] = 0xcf;
1631
+ targetView.setBigUint64(position, value);
1607
1632
  } else {
1608
1633
  // overflow
1609
1634
  if (this.largeBigIntToFloat) {
1610
- target[position$1++] = 0xcb;
1611
- targetView.setFloat64(position$1, Number(value));
1635
+ target[position++] = 0xcb;
1636
+ targetView.setFloat64(position, Number(value));
1612
1637
  } else {
1613
1638
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1614
1639
  }
1615
1640
  }
1616
- position$1 += 8;
1641
+ position += 8;
1617
1642
  } else if (type === 'undefined') {
1618
1643
  if (this.encodeUndefinedAsNil)
1619
- target[position$1++] = 0xc0;
1644
+ target[position++] = 0xc0;
1620
1645
  else {
1621
- target[position$1++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1622
- target[position$1++] = 0;
1623
- target[position$1++] = 0;
1646
+ target[position++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1647
+ target[position++] = 0;
1648
+ target[position++] = 0;
1624
1649
  }
1625
1650
  } else if (type === 'function') {
1626
1651
  pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
@@ -1634,15 +1659,15 @@ class Packr extends Unpackr {
1634
1659
  let keys = Object.keys(object);
1635
1660
  let length = keys.length;
1636
1661
  if (length < 0x10) {
1637
- target[position$1++] = 0x80 | length;
1662
+ target[position++] = 0x80 | length;
1638
1663
  } else if (length < 0x10000) {
1639
- target[position$1++] = 0xde;
1640
- target[position$1++] = length >> 8;
1641
- target[position$1++] = length & 0xff;
1664
+ target[position++] = 0xde;
1665
+ target[position++] = length >> 8;
1666
+ target[position++] = length & 0xff;
1642
1667
  } else {
1643
- target[position$1++] = 0xdf;
1644
- targetView.setUint32(position$1, length);
1645
- position$1 += 4;
1668
+ target[position++] = 0xdf;
1669
+ targetView.setUint32(position, length);
1670
+ position += 4;
1646
1671
  }
1647
1672
  let key;
1648
1673
  for (let i = 0; i < length; i++) {
@@ -1651,9 +1676,9 @@ class Packr extends Unpackr {
1651
1676
  }
1652
1677
  } :
1653
1678
  (object, safePrototype) => {
1654
- target[position$1++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1655
- let objectOffset = position$1 - start;
1656
- position$1 += 2;
1679
+ target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1680
+ let objectOffset = position - start;
1681
+ position += 2;
1657
1682
  let size = 0;
1658
1683
  for (let key in object) {
1659
1684
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1668,7 +1693,7 @@ class Packr extends Unpackr {
1668
1693
  (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)
1669
1694
  (object, safePrototype) => {
1670
1695
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1671
- let objectOffset = position$1++ - start;
1696
+ let objectOffset = position++ - start;
1672
1697
  let wroteKeys;
1673
1698
  for (let key in object) {
1674
1699
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1690,9 +1715,9 @@ class Packr extends Unpackr {
1690
1715
  }
1691
1716
  transition = nextTransition;
1692
1717
  }
1693
- if (objectOffset + start + 1 == position$1) {
1718
+ if (objectOffset + start + 1 == position) {
1694
1719
  // first key, so we don't need to insert, we can just write record directly
1695
- position$1--;
1720
+ position--;
1696
1721
  newRecord(transition, keys, newTransitions);
1697
1722
  } else // otherwise we need to insert the record, moving existing data after the record
1698
1723
  insertNewRecord(transition, keys, objectOffset, newTransitions);
@@ -1724,10 +1749,10 @@ class Packr extends Unpackr {
1724
1749
  let recordId = transition[RECORD_SYMBOL];
1725
1750
  if (recordId) {
1726
1751
  if (recordId >= 0x60 && useTwoByteRecords) {
1727
- target[position$1++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1728
- target[position$1++] = recordId >> 5;
1752
+ target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1753
+ target[position++] = recordId >> 5;
1729
1754
  } else
1730
- target[position$1++] = recordId;
1755
+ target[position++] = recordId;
1731
1756
  } else {
1732
1757
  newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
1733
1758
  }
@@ -1747,13 +1772,13 @@ class Packr extends Unpackr {
1747
1772
  } else // faster handling for smaller buffers
1748
1773
  newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
1749
1774
  let newBuffer = new ByteArrayAllocate(newSize);
1750
- targetView = newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize);
1775
+ targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
1751
1776
  end = Math.min(end, target.length);
1752
1777
  if (target.copy)
1753
1778
  target.copy(newBuffer, 0, start, end);
1754
1779
  else
1755
1780
  newBuffer.set(target.slice(start, end));
1756
- position$1 -= start;
1781
+ position -= start;
1757
1782
  start = 0;
1758
1783
  safeEnd = newBuffer.length - 10;
1759
1784
  return target = newBuffer
@@ -1782,21 +1807,21 @@ class Packr extends Unpackr {
1782
1807
  structures.sharedLength = recordId - 0x3f;
1783
1808
  hasSharedUpdate = true;
1784
1809
  if (highByte >= 0) {
1785
- target[position$1++] = (recordId & 0x1f) + 0x60;
1786
- target[position$1++] = highByte;
1810
+ target[position++] = (recordId & 0x1f) + 0x60;
1811
+ target[position++] = highByte;
1787
1812
  } else {
1788
- target[position$1++] = recordId;
1813
+ target[position++] = recordId;
1789
1814
  }
1790
1815
  } else {
1791
1816
  if (highByte >= 0) {
1792
- target[position$1++] = 0xd5; // fixext 2
1793
- target[position$1++] = 0x72; // "r" record defintion extension type
1794
- target[position$1++] = (recordId & 0x1f) + 0x60;
1795
- target[position$1++] = highByte;
1817
+ target[position++] = 0xd5; // fixext 2
1818
+ target[position++] = 0x72; // "r" record defintion extension type
1819
+ target[position++] = (recordId & 0x1f) + 0x60;
1820
+ target[position++] = highByte;
1796
1821
  } else {
1797
- target[position$1++] = 0xd4; // fixext 1
1798
- target[position$1++] = 0x72; // "r" record defintion extension type
1799
- target[position$1++] = recordId;
1822
+ target[position++] = 0xd4; // fixext 1
1823
+ target[position++] = 0x72; // "r" record defintion extension type
1824
+ target[position++] = recordId;
1800
1825
  }
1801
1826
 
1802
1827
  if (newTransitions)
@@ -1810,57 +1835,57 @@ class Packr extends Unpackr {
1810
1835
  };
1811
1836
  const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
1812
1837
  let mainTarget = target;
1813
- let mainPosition = position$1;
1838
+ let mainPosition = position;
1814
1839
  let mainSafeEnd = safeEnd;
1815
1840
  let mainStart = start;
1816
1841
  target = keysTarget;
1817
- position$1 = 0;
1842
+ position = 0;
1818
1843
  start = 0;
1819
1844
  if (!target)
1820
1845
  keysTarget = target = new ByteArrayAllocate(8192);
1821
1846
  safeEnd = target.length - 10;
1822
1847
  newRecord(transition, keys, newTransitions);
1823
1848
  keysTarget = target;
1824
- let keysPosition = position$1;
1849
+ let keysPosition = position;
1825
1850
  target = mainTarget;
1826
- position$1 = mainPosition;
1851
+ position = mainPosition;
1827
1852
  safeEnd = mainSafeEnd;
1828
1853
  start = mainStart;
1829
1854
  if (keysPosition > 1) {
1830
- let newEnd = position$1 + keysPosition - 1;
1855
+ let newEnd = position + keysPosition - 1;
1831
1856
  if (newEnd > safeEnd)
1832
1857
  makeRoom(newEnd);
1833
1858
  let insertionPosition = insertionOffset + start;
1834
- target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position$1);
1859
+ target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position);
1835
1860
  target.set(keysTarget.slice(0, keysPosition), insertionPosition);
1836
- position$1 = newEnd;
1861
+ position = newEnd;
1837
1862
  } else {
1838
1863
  target[insertionOffset + start] = keysTarget[0];
1839
1864
  }
1840
1865
  };
1841
1866
  const writeStruct = (object, safePrototype) => {
1842
- let newPosition = writeStructSlots(object, target, position$1, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1867
+ let newPosition = writeStructSlots(object, target, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1843
1868
  if (notifySharedUpdate)
1844
1869
  return hasSharedUpdate = true;
1845
- position$1 = newPosition;
1870
+ position = newPosition;
1846
1871
  if (start > 0) {
1847
1872
  pack(value);
1848
1873
  if (start == 0)
1849
- return { position: position$1, targetView, target }; // indicate the buffer was re-allocated
1874
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1850
1875
  } else
1851
1876
  pack(value);
1852
- return position$1;
1877
+ return position;
1853
1878
  }, this);
1854
1879
  if (newPosition === 0) // bail and go to a msgpack object
1855
1880
  return writeObject(object, true);
1856
- position$1 = newPosition;
1881
+ position = newPosition;
1857
1882
  };
1858
1883
  }
1859
1884
  useBuffer(buffer) {
1860
1885
  // this means we are finished using our own buffer and we can write over it safely
1861
1886
  target = buffer;
1862
1887
  targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1863
- position$1 = 0;
1888
+ position = 0;
1864
1889
  }
1865
1890
  clearSharedData() {
1866
1891
  if (this.structures)
@@ -1943,7 +1968,7 @@ extensions = [{
1943
1968
  if (this.moreTypes)
1944
1969
  writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
1945
1970
  else
1946
- writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
1971
+ writeBuffer(hasNodeBuffer$1 ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
1947
1972
  }
1948
1973
  }, {
1949
1974
  pack(typedArray, allocateForWrite) {
@@ -2065,17 +2090,17 @@ function insertIds(serialized, idsToInsert) {
2065
2090
  }
2066
2091
 
2067
2092
  function writeBundles(start, pack, incrementPosition) {
2068
- if (bundledStrings$1.length > 0) {
2069
- targetView.setUint32(bundledStrings$1.position + start, position$1 + incrementPosition - bundledStrings$1.position - start);
2070
- bundledStrings$1.stringsPosition = position$1 - start;
2071
- let writeStrings = bundledStrings$1;
2072
- bundledStrings$1 = null;
2093
+ if (bundledStrings.length > 0) {
2094
+ targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start);
2095
+ bundledStrings.stringsPosition = position - start;
2096
+ let writeStrings = bundledStrings;
2097
+ bundledStrings = null;
2073
2098
  pack(writeStrings[0]);
2074
2099
  pack(writeStrings[1]);
2075
2100
  }
2076
2101
  }
2077
2102
 
2078
- function addExtension$1(extension) {
2103
+ function addExtension(extension) {
2079
2104
  if (extension.Class) {
2080
2105
  if (!extension.pack && !extension.write)
2081
2106
  throw new Error('Extension has no pack or write function')
@@ -2084,9 +2109,9 @@ function addExtension$1(extension) {
2084
2109
  extensionClasses.unshift(extension.Class);
2085
2110
  extensions.unshift(extension);
2086
2111
  }
2087
- addExtension(extension);
2112
+ addExtension$1(extension);
2088
2113
  }
2089
- function prepareStructures(structures, packr) {
2114
+ function prepareStructures$1(structures, packr) {
2090
2115
  structures.isCompatible = (existingStructures) => {
2091
2116
  let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
2092
2117
  if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
@@ -2097,7 +2122,7 @@ function prepareStructures(structures, packr) {
2097
2122
  }
2098
2123
  function setWriteStructSlots(writeSlots, makeStructures) {
2099
2124
  writeStructSlots = writeSlots;
2100
- prepareStructures = makeStructures;
2125
+ prepareStructures$1 = makeStructures;
2101
2126
  }
2102
2127
 
2103
2128
  let defaultPackr = new Packr({ useRecords: false });
@@ -2125,18 +2150,18 @@ try {
2125
2150
  }
2126
2151
 
2127
2152
  let updatedPosition;
2128
- const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
2129
- let textEncoder$1, currentSource;
2153
+ const hasNodeBuffer = typeof Buffer !== 'undefined';
2154
+ let textEncoder, currentSource;
2130
2155
  try {
2131
- textEncoder$1 = new TextEncoder();
2156
+ textEncoder = new TextEncoder();
2132
2157
  } catch (error) {}
2133
- const encodeUtf8 = hasNodeBuffer$1 ? function(target, string, position) {
2158
+ const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
2134
2159
  return target.utf8Write(string, position, 0xffffffff)
2135
- } : (textEncoder$1 && textEncoder$1.encodeInto) ?
2160
+ } : (textEncoder && textEncoder.encodeInto) ?
2136
2161
  function(target, string, position) {
2137
- return textEncoder$1.encodeInto(string, target.subarray(position)).written
2162
+ return textEncoder.encodeInto(string, target.subarray(position)).written
2138
2163
  } : false;
2139
- setWriteStructSlots(writeStruct, prepareStructures$1);
2164
+ setWriteStructSlots(writeStruct, prepareStructures);
2140
2165
  function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
2141
2166
  let typedStructs = packr.typedStructs || (packr.typedStructs = []);
2142
2167
  // note that we rely on pack.js to load stored structures before we get to this point
@@ -2229,9 +2254,9 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2229
2254
  case 'string':
2230
2255
  let strLength = value.length;
2231
2256
  refOffset = refPosition - refsStartPosition;
2232
- if ((strLength << 2) + position > safeEnd) {
2257
+ if ((strLength << 2) + refPosition > safeEnd) {
2233
2258
  let lastStart = start;
2234
- target = makeRoom(refPosition);
2259
+ target = makeRoom((strLength << 2) + refPosition);
2235
2260
  targetView = target.dataView;
2236
2261
  position -= lastStart;
2237
2262
  refsStartPosition -= lastStart;
@@ -2511,7 +2536,7 @@ function createTypeTransition(transition, type, size) {
2511
2536
  newTransition.__parent = transition;
2512
2537
  return newTransition;
2513
2538
  }
2514
- function onLoadedStructures$1(sharedData) {
2539
+ function onLoadedStructures(sharedData) {
2515
2540
  if (!(sharedData instanceof Map))
2516
2541
  return sharedData;
2517
2542
  let typed = sharedData.get('typed') || [];
@@ -2549,7 +2574,7 @@ function onLoadedStructures$1(sharedData) {
2549
2574
  return named;
2550
2575
  }
2551
2576
  var sourceSymbol = Symbol.for('source');
2552
- function readStruct$1(src, position, srcEnd, unpackr) {
2577
+ function readStruct(src, position, srcEnd, unpackr) {
2553
2578
  let recordId = src[position++] - 0x20;
2554
2579
  if (recordId >= 24) {
2555
2580
  switch(recordId) {
@@ -2820,14 +2845,14 @@ function withSource(get) {
2820
2845
  }
2821
2846
  }
2822
2847
 
2823
- function saveState$1() {
2848
+ function saveState() {
2824
2849
  if (currentSource) {
2825
2850
  currentSource.bytes = Uint8Array.prototype.slice.call(currentSource.bytes, currentSource.position, currentSource.bytesEnd);
2826
2851
  currentSource.position = 0;
2827
2852
  currentSource.bytesEnd = currentSource.bytes.length;
2828
2853
  }
2829
2854
  }
2830
- function prepareStructures$1(structures, packr) {
2855
+ function prepareStructures(structures, packr) {
2831
2856
  if (packr.typedStructs) {
2832
2857
  let structMap = new Map();
2833
2858
  structMap.set('named', structures);
@@ -2844,7 +2869,7 @@ function prepareStructures$1(structures, packr) {
2844
2869
  let typed = existing.get('typed') || [];
2845
2870
  if (typed.length !== lastTypedStructuresLength)
2846
2871
  compatible = false;
2847
- } else if (existing instanceof Array) {
2872
+ } else if (existing instanceof Array || Array.isArray(existing)) {
2848
2873
  if (existing.length !== (packr.lastNamedStructuresLength || 0))
2849
2874
  compatible = false;
2850
2875
  }
@@ -2856,7 +2881,7 @@ function prepareStructures$1(structures, packr) {
2856
2881
  return structures;
2857
2882
  }
2858
2883
 
2859
- setReadStruct(readStruct$1, onLoadedStructures$1, saveState$1);
2884
+ setReadStruct(readStruct, onLoadedStructures, saveState);
2860
2885
 
2861
2886
  class PackrStream extends stream.Transform {
2862
2887
  constructor(options) {
@@ -3029,7 +3054,7 @@ exports.Packr = Packr;
3029
3054
  exports.PackrStream = PackrStream;
3030
3055
  exports.Unpackr = Unpackr;
3031
3056
  exports.UnpackrStream = UnpackrStream;
3032
- exports.addExtension = addExtension$1;
3057
+ exports.addExtension = addExtension;
3033
3058
  exports.clearSource = clearSource;
3034
3059
  exports.decode = decode;
3035
3060
  exports.decodeIter = decodeIter;