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/test.js CHANGED
@@ -1,22 +1,22 @@
1
- (function (msgpackr, chai, inspector, fs) {
1
+ (function (chai, stream, module, fs) {
2
2
  'use strict';
3
3
 
4
- chai = chai && Object.prototype.hasOwnProperty.call(chai, 'default') ? chai['default'] : chai;
5
- inspector = inspector && Object.prototype.hasOwnProperty.call(inspector, 'default') ? inspector['default'] : inspector;
6
-
7
4
  var decoder;
8
5
  try {
9
6
  decoder = new TextDecoder();
10
7
  } catch(error) {}
11
8
  var src;
12
9
  var srcEnd;
13
- var position = 0;
10
+ var position$1 = 0;
11
+ const EMPTY_ARRAY = [];
12
+ var strings = EMPTY_ARRAY;
13
+ var stringPosition = 0;
14
14
  var currentUnpackr = {};
15
15
  var currentStructures;
16
16
  var srcString;
17
17
  var srcStringStart = 0;
18
18
  var srcStringEnd = 0;
19
- var bundledStrings;
19
+ var bundledStrings$1;
20
20
  var referenceMap;
21
21
  var currentExtensions = [];
22
22
  var dataView;
@@ -29,7 +29,8 @@
29
29
  C1.name = 'MessagePack 0xC1';
30
30
  var sequentialMode = false;
31
31
  var inlineObjectReadThreshold = 2;
32
- var readStruct, onLoadedStructures, onSaveState;
32
+ var readStruct$1, onLoadedStructures$1, onSaveState;
33
+ // no-eval build
33
34
  try {
34
35
  new Function('');
35
36
  } catch(error) {
@@ -37,7 +38,7 @@
37
38
  inlineObjectReadThreshold = Infinity;
38
39
  }
39
40
 
40
- class Unpackr {
41
+ let Unpackr$1 = class Unpackr {
41
42
  constructor(options) {
42
43
  if (options) {
43
44
  if (options.useRecords === false && options.mapsAsObjects === undefined)
@@ -56,27 +57,32 @@
56
57
  (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
57
58
  options.structures.sharedLength = 0;
58
59
  }
60
+ if (options.int64AsNumber) {
61
+ options.int64AsType = 'number';
62
+ }
59
63
  }
60
64
  Object.assign(this, options);
61
65
  }
62
66
  unpack(source, options) {
63
67
  if (src) {
64
68
  // re-entrant execution, save the state and restore it after we do this unpack
65
- return saveState(() => {
69
+ return saveState$1(() => {
66
70
  clearSource();
67
- return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
71
+ return this ? this.unpack(source, options) : Unpackr$1.prototype.unpack.call(defaultOptions, source, options)
68
72
  })
69
73
  }
70
74
  if (typeof options === 'object') {
71
75
  srcEnd = options.end || source.length;
72
- position = options.start || 0;
76
+ position$1 = options.start || 0;
73
77
  } else {
74
- position = 0;
78
+ position$1 = 0;
75
79
  srcEnd = options > -1 ? options : source.length;
76
80
  }
81
+ stringPosition = 0;
77
82
  srcStringEnd = 0;
78
83
  srcString = null;
79
- bundledStrings = null;
84
+ strings = EMPTY_ARRAY;
85
+ bundledStrings$1 = null;
80
86
  src = source;
81
87
  // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
82
88
  // technique for getting data from a database where it can be copied into an existing buffer instead of creating
@@ -90,7 +96,7 @@
90
96
  throw error
91
97
  throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
92
98
  }
93
- if (this instanceof Unpackr) {
99
+ if (this instanceof Unpackr$1) {
94
100
  currentUnpackr = this;
95
101
  if (this.structures) {
96
102
  currentStructures = this.structures;
@@ -113,8 +119,8 @@
113
119
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
114
120
  if (forEach) {
115
121
  forEach(value);
116
- while(position < size) {
117
- lastPosition = position;
122
+ while(position$1 < size) {
123
+ lastPosition = position$1;
118
124
  if (forEach(checkedRead()) === false) {
119
125
  return
120
126
  }
@@ -122,8 +128,8 @@
122
128
  }
123
129
  else {
124
130
  values = [ value ];
125
- while(position < size) {
126
- lastPosition = position;
131
+ while(position$1 < size) {
132
+ lastPosition = position$1;
127
133
  values.push(checkedRead());
128
134
  }
129
135
  return values
@@ -138,8 +144,8 @@
138
144
  }
139
145
  }
140
146
  _mergeStructures(loadedStructures, existingStructures) {
141
- if (onLoadedStructures)
142
- loadedStructures = onLoadedStructures.call(this, loadedStructures);
147
+ if (onLoadedStructures$1)
148
+ loadedStructures = onLoadedStructures$1.call(this, loadedStructures);
143
149
  loadedStructures = loadedStructures || [];
144
150
  if (Object.isFrozen(loadedStructures))
145
151
  loadedStructures = loadedStructures.map(structure => structure.slice(0));
@@ -168,7 +174,7 @@
168
174
  decode(source, end) {
169
175
  return this.unpack(source, end)
170
176
  }
171
- }
177
+ };
172
178
  function checkedRead(options) {
173
179
  try {
174
180
  if (!currentUnpackr.trusted && !sequentialMode) {
@@ -177,18 +183,20 @@
177
183
  currentStructures.length = sharedLength;
178
184
  }
179
185
  let result;
180
- if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
181
- result = readStruct(src, position, srcEnd, currentUnpackr);
186
+ if (currentUnpackr.randomAccessStructure && src[position$1] < 0x40 && src[position$1] >= 0x20 && readStruct$1) {
187
+ result = readStruct$1(src, position$1, srcEnd, currentUnpackr);
182
188
  src = null; // dispose of this so that recursive unpack calls don't save state
183
189
  if (!(options && options.lazy) && result)
184
190
  result = result.toJSON();
185
- position = srcEnd;
191
+ position$1 = srcEnd;
186
192
  } else
187
193
  result = read();
188
- if (bundledStrings) // bundled strings to skip past
189
- position = bundledStrings.postBundlePosition;
194
+ if (bundledStrings$1) { // bundled strings to skip past
195
+ position$1 = bundledStrings$1.postBundlePosition;
196
+ bundledStrings$1 = null;
197
+ }
190
198
 
191
- if (position == srcEnd) {
199
+ if (position$1 == srcEnd) {
192
200
  // finished reading this source, cleanup references
193
201
  if (currentStructures && currentStructures.restoreStructures)
194
202
  restoreStructures();
@@ -196,7 +204,7 @@
196
204
  src = null;
197
205
  if (referenceMap)
198
206
  referenceMap = null;
199
- } else if (position > srcEnd) {
207
+ } else if (position$1 > srcEnd) {
200
208
  // over read
201
209
  throw new Error('Unexpected end of MessagePack data')
202
210
  } else if (!sequentialMode) {
@@ -208,7 +216,7 @@
208
216
  if (currentStructures && currentStructures.restoreStructures)
209
217
  restoreStructures();
210
218
  clearSource();
211
- if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
219
+ if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
212
220
  error.incomplete = true;
213
221
  }
214
222
  throw error
@@ -223,7 +231,7 @@
223
231
  }
224
232
 
225
233
  function read() {
226
- let token = src[position++];
234
+ let token = src[position$1++];
227
235
  if (token < 0xa0) {
228
236
  if (token < 0x80) {
229
237
  if (token < 0x40)
@@ -271,8 +279,8 @@
271
279
  } else if (token < 0xc0) {
272
280
  // fixstr
273
281
  let length = token - 0xa0;
274
- if (srcStringEnd >= position) {
275
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
282
+ if (srcStringEnd >= position$1) {
283
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
276
284
  }
277
285
  if (srcStringEnd == 0 && srcEnd < 140) {
278
286
  // for small blocks, avoiding the overhead of the extract call is helpful
@@ -286,124 +294,128 @@
286
294
  switch (token) {
287
295
  case 0xc0: return null
288
296
  case 0xc1:
289
- if (bundledStrings) {
297
+ if (bundledStrings$1) {
290
298
  value = read(); // followed by the length of the string in characters (not bytes!)
291
299
  if (value > 0)
292
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
300
+ return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
293
301
  else
294
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
302
+ return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
295
303
  }
296
304
  return C1; // "never-used", return special object to denote that
297
305
  case 0xc2: return false
298
306
  case 0xc3: return true
299
307
  case 0xc4:
300
308
  // bin 8
301
- value = src[position++];
309
+ value = src[position$1++];
302
310
  if (value === undefined)
303
311
  throw new Error('Unexpected end of buffer')
304
312
  return readBin(value)
305
313
  case 0xc5:
306
314
  // bin 16
307
- value = dataView.getUint16(position);
308
- position += 2;
315
+ value = dataView.getUint16(position$1);
316
+ position$1 += 2;
309
317
  return readBin(value)
310
318
  case 0xc6:
311
319
  // bin 32
312
- value = dataView.getUint32(position);
313
- position += 4;
320
+ value = dataView.getUint32(position$1);
321
+ position$1 += 4;
314
322
  return readBin(value)
315
323
  case 0xc7:
316
324
  // ext 8
317
- return readExt(src[position++])
325
+ return readExt(src[position$1++])
318
326
  case 0xc8:
319
327
  // ext 16
320
- value = dataView.getUint16(position);
321
- position += 2;
328
+ value = dataView.getUint16(position$1);
329
+ position$1 += 2;
322
330
  return readExt(value)
323
331
  case 0xc9:
324
332
  // ext 32
325
- value = dataView.getUint32(position);
326
- position += 4;
333
+ value = dataView.getUint32(position$1);
334
+ position$1 += 4;
327
335
  return readExt(value)
328
336
  case 0xca:
329
- value = dataView.getFloat32(position);
337
+ value = dataView.getFloat32(position$1);
330
338
  if (currentUnpackr.useFloat32 > 2) {
331
339
  // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
332
- let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
333
- position += 4;
340
+ let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
341
+ position$1 += 4;
334
342
  return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
335
343
  }
336
- position += 4;
344
+ position$1 += 4;
337
345
  return value
338
346
  case 0xcb:
339
- value = dataView.getFloat64(position);
340
- position += 8;
347
+ value = dataView.getFloat64(position$1);
348
+ position$1 += 8;
341
349
  return value
342
350
  // uint handlers
343
351
  case 0xcc:
344
- return src[position++]
352
+ return src[position$1++]
345
353
  case 0xcd:
346
- value = dataView.getUint16(position);
347
- position += 2;
354
+ value = dataView.getUint16(position$1);
355
+ position$1 += 2;
348
356
  return value
349
357
  case 0xce:
350
- value = dataView.getUint32(position);
351
- position += 4;
358
+ value = dataView.getUint32(position$1);
359
+ position$1 += 4;
352
360
  return value
353
361
  case 0xcf:
354
- if (currentUnpackr.int64AsNumber) {
355
- value = dataView.getUint32(position) * 0x100000000;
356
- value += dataView.getUint32(position + 4);
362
+ if (currentUnpackr.int64AsType === 'number') {
363
+ value = dataView.getUint32(position$1) * 0x100000000;
364
+ value += dataView.getUint32(position$1 + 4);
365
+ } else if (currentUnpackr.int64AsType === 'string') {
366
+ value = dataView.getBigUint64(position$1).toString();
357
367
  } else
358
- value = dataView.getBigUint64(position);
359
- position += 8;
368
+ value = dataView.getBigUint64(position$1);
369
+ position$1 += 8;
360
370
  return value
361
371
 
362
372
  // int handlers
363
373
  case 0xd0:
364
- return dataView.getInt8(position++)
374
+ return dataView.getInt8(position$1++)
365
375
  case 0xd1:
366
- value = dataView.getInt16(position);
367
- position += 2;
376
+ value = dataView.getInt16(position$1);
377
+ position$1 += 2;
368
378
  return value
369
379
  case 0xd2:
370
- value = dataView.getInt32(position);
371
- position += 4;
380
+ value = dataView.getInt32(position$1);
381
+ position$1 += 4;
372
382
  return value
373
383
  case 0xd3:
374
- if (currentUnpackr.int64AsNumber) {
375
- value = dataView.getInt32(position) * 0x100000000;
376
- value += dataView.getUint32(position + 4);
384
+ if (currentUnpackr.int64AsType === 'number') {
385
+ value = dataView.getInt32(position$1) * 0x100000000;
386
+ value += dataView.getUint32(position$1 + 4);
387
+ } else if (currentUnpackr.int64AsType === 'string') {
388
+ value = dataView.getBigInt64(position$1).toString();
377
389
  } else
378
- value = dataView.getBigInt64(position);
379
- position += 8;
390
+ value = dataView.getBigInt64(position$1);
391
+ position$1 += 8;
380
392
  return value
381
393
 
382
394
  case 0xd4:
383
395
  // fixext 1
384
- value = src[position++];
396
+ value = src[position$1++];
385
397
  if (value == 0x72) {
386
- return recordDefinition(src[position++] & 0x3f)
398
+ return recordDefinition(src[position$1++] & 0x3f)
387
399
  } else {
388
400
  let extension = currentExtensions[value];
389
401
  if (extension) {
390
402
  if (extension.read) {
391
- position++; // skip filler byte
403
+ position$1++; // skip filler byte
392
404
  return extension.read(read())
393
405
  } else if (extension.noBuffer) {
394
- position++; // skip filler byte
406
+ position$1++; // skip filler byte
395
407
  return extension()
396
408
  } else
397
- return extension(src.subarray(position, ++position))
409
+ return extension(src.subarray(position$1, ++position$1))
398
410
  } else
399
411
  throw new Error('Unknown extension ' + value)
400
412
  }
401
413
  case 0xd5:
402
414
  // fixext 2
403
- value = src[position];
415
+ value = src[position$1];
404
416
  if (value == 0x72) {
405
- position++;
406
- return recordDefinition(src[position++] & 0x3f, src[position++])
417
+ position$1++;
418
+ return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
407
419
  } else
408
420
  return readExt(2)
409
421
  case 0xd6:
@@ -417,46 +429,46 @@
417
429
  return readExt(16)
418
430
  case 0xd9:
419
431
  // str 8
420
- value = src[position++];
421
- if (srcStringEnd >= position) {
422
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
432
+ value = src[position$1++];
433
+ if (srcStringEnd >= position$1) {
434
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
423
435
  }
424
436
  return readString8(value)
425
437
  case 0xda:
426
438
  // str 16
427
- value = dataView.getUint16(position);
428
- position += 2;
429
- if (srcStringEnd >= position) {
430
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
439
+ value = dataView.getUint16(position$1);
440
+ position$1 += 2;
441
+ if (srcStringEnd >= position$1) {
442
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
431
443
  }
432
444
  return readString16(value)
433
445
  case 0xdb:
434
446
  // str 32
435
- value = dataView.getUint32(position);
436
- position += 4;
437
- if (srcStringEnd >= position) {
438
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
447
+ value = dataView.getUint32(position$1);
448
+ position$1 += 4;
449
+ if (srcStringEnd >= position$1) {
450
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
439
451
  }
440
452
  return readString32(value)
441
453
  case 0xdc:
442
454
  // array 16
443
- value = dataView.getUint16(position);
444
- position += 2;
455
+ value = dataView.getUint16(position$1);
456
+ position$1 += 2;
445
457
  return readArray(value)
446
458
  case 0xdd:
447
459
  // array 32
448
- value = dataView.getUint32(position);
449
- position += 4;
460
+ value = dataView.getUint32(position$1);
461
+ position$1 += 4;
450
462
  return readArray(value)
451
463
  case 0xde:
452
464
  // map 16
453
- value = dataView.getUint16(position);
454
- position += 2;
465
+ value = dataView.getUint16(position$1);
466
+ position$1 += 2;
455
467
  return readMap(value)
456
468
  case 0xdf:
457
469
  // map 32
458
- value = dataView.getUint32(position);
459
- position += 4;
470
+ value = dataView.getUint32(position$1);
471
+ position$1 += 4;
460
472
  return readMap(value)
461
473
  default: // negative int
462
474
  if (token >= 0xe0)
@@ -502,7 +514,7 @@
502
514
 
503
515
  const createSecondByteReader = (firstId, read0) => {
504
516
  return function() {
505
- let highByte = src[position++];
517
+ let highByte = src[position$1++];
506
518
  if (highByte === 0)
507
519
  return read0()
508
520
  let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
@@ -517,7 +529,7 @@
517
529
  };
518
530
 
519
531
  function loadStructures() {
520
- let loadedStructures = saveState(() => {
532
+ let loadedStructures = saveState$1(() => {
521
533
  // save the state in case getStructures modifies our buffer
522
534
  src = null;
523
535
  return currentUnpackr.getStructures()
@@ -529,6 +541,44 @@
529
541
  var readString8 = readStringJS;
530
542
  var readString16 = readStringJS;
531
543
  var readString32 = readStringJS;
544
+
545
+ function setExtractor(extractStrings) {
546
+ readFixedString = readString(1);
547
+ readString8 = readString(2);
548
+ readString16 = readString(3);
549
+ readString32 = readString(5);
550
+ function readString(headerLength) {
551
+ return function readString(length) {
552
+ let string = strings[stringPosition++];
553
+ if (string == null) {
554
+ if (bundledStrings$1)
555
+ return readStringJS(length)
556
+ let extraction = extractStrings(position$1 - headerLength, srcEnd, src);
557
+ if (typeof extraction == 'string') {
558
+ string = extraction;
559
+ strings = EMPTY_ARRAY;
560
+ } else {
561
+ strings = extraction;
562
+ stringPosition = 1;
563
+ srcStringEnd = 1; // even if a utf-8 string was decoded, must indicate we are in the midst of extracted strings and can't skip strings
564
+ string = strings[0];
565
+ if (string === undefined)
566
+ throw new Error('Unexpected end of buffer')
567
+ }
568
+ }
569
+ let srcStringLength = string.length;
570
+ if (srcStringLength <= length) {
571
+ position$1 += length;
572
+ return string
573
+ }
574
+ srcString = string;
575
+ srcStringStart = position$1;
576
+ srcStringEnd = position$1 + srcStringLength;
577
+ position$1 += length;
578
+ return string.slice(0, length) // we know we just want the beginning
579
+ }
580
+ }
581
+ }
532
582
  function readStringJS(length) {
533
583
  let result;
534
584
  if (length < 16) {
@@ -536,29 +586,29 @@
536
586
  return result
537
587
  }
538
588
  if (length > 64 && decoder)
539
- return decoder.decode(src.subarray(position, position += length))
540
- const end = position + length;
589
+ return decoder.decode(src.subarray(position$1, position$1 += length))
590
+ const end = position$1 + length;
541
591
  const units = [];
542
592
  result = '';
543
- while (position < end) {
544
- const byte1 = src[position++];
593
+ while (position$1 < end) {
594
+ const byte1 = src[position$1++];
545
595
  if ((byte1 & 0x80) === 0) {
546
596
  // 1 byte
547
597
  units.push(byte1);
548
598
  } else if ((byte1 & 0xe0) === 0xc0) {
549
599
  // 2 bytes
550
- const byte2 = src[position++] & 0x3f;
600
+ const byte2 = src[position$1++] & 0x3f;
551
601
  units.push(((byte1 & 0x1f) << 6) | byte2);
552
602
  } else if ((byte1 & 0xf0) === 0xe0) {
553
603
  // 3 bytes
554
- const byte2 = src[position++] & 0x3f;
555
- const byte3 = src[position++] & 0x3f;
604
+ const byte2 = src[position$1++] & 0x3f;
605
+ const byte3 = src[position$1++] & 0x3f;
556
606
  units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
557
607
  } else if ((byte1 & 0xf8) === 0xf0) {
558
608
  // 4 bytes
559
- const byte2 = src[position++] & 0x3f;
560
- const byte3 = src[position++] & 0x3f;
561
- const byte4 = src[position++] & 0x3f;
609
+ const byte2 = src[position$1++] & 0x3f;
610
+ const byte3 = src[position$1++] & 0x3f;
611
+ const byte4 = src[position$1++] & 0x3f;
562
612
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
563
613
  if (unit > 0xffff) {
564
614
  unit -= 0x10000;
@@ -585,7 +635,7 @@
585
635
  function readString(source, start, length) {
586
636
  let existingSrc = src;
587
637
  src = source;
588
- position = start;
638
+ position$1 = start;
589
639
  try {
590
640
  return readStringJS(length);
591
641
  } finally {
@@ -624,12 +674,12 @@
624
674
 
625
675
  var fromCharCode = String.fromCharCode;
626
676
  function longStringInJS(length) {
627
- let start = position;
677
+ let start = position$1;
628
678
  let bytes = new Array(length);
629
679
  for (let i = 0; i < length; i++) {
630
- const byte = src[position++];
680
+ const byte = src[position$1++];
631
681
  if ((byte & 0x80) > 0) {
632
- position = start;
682
+ position$1 = start;
633
683
  return
634
684
  }
635
685
  bytes[i] = byte;
@@ -642,131 +692,131 @@
642
692
  if (length === 0)
643
693
  return ''
644
694
  else {
645
- let a = src[position++];
695
+ let a = src[position$1++];
646
696
  if ((a & 0x80) > 1) {
647
- position -= 1;
697
+ position$1 -= 1;
648
698
  return
649
699
  }
650
700
  return fromCharCode(a)
651
701
  }
652
702
  } else {
653
- let a = src[position++];
654
- let b = src[position++];
703
+ let a = src[position$1++];
704
+ let b = src[position$1++];
655
705
  if ((a & 0x80) > 0 || (b & 0x80) > 0) {
656
- position -= 2;
706
+ position$1 -= 2;
657
707
  return
658
708
  }
659
709
  if (length < 3)
660
710
  return fromCharCode(a, b)
661
- let c = src[position++];
711
+ let c = src[position$1++];
662
712
  if ((c & 0x80) > 0) {
663
- position -= 3;
713
+ position$1 -= 3;
664
714
  return
665
715
  }
666
716
  return fromCharCode(a, b, c)
667
717
  }
668
718
  } else {
669
- let a = src[position++];
670
- let b = src[position++];
671
- let c = src[position++];
672
- let d = src[position++];
719
+ let a = src[position$1++];
720
+ let b = src[position$1++];
721
+ let c = src[position$1++];
722
+ let d = src[position$1++];
673
723
  if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
674
- position -= 4;
724
+ position$1 -= 4;
675
725
  return
676
726
  }
677
727
  if (length < 6) {
678
728
  if (length === 4)
679
729
  return fromCharCode(a, b, c, d)
680
730
  else {
681
- let e = src[position++];
731
+ let e = src[position$1++];
682
732
  if ((e & 0x80) > 0) {
683
- position -= 5;
733
+ position$1 -= 5;
684
734
  return
685
735
  }
686
736
  return fromCharCode(a, b, c, d, e)
687
737
  }
688
738
  } else if (length < 8) {
689
- let e = src[position++];
690
- let f = src[position++];
739
+ let e = src[position$1++];
740
+ let f = src[position$1++];
691
741
  if ((e & 0x80) > 0 || (f & 0x80) > 0) {
692
- position -= 6;
742
+ position$1 -= 6;
693
743
  return
694
744
  }
695
745
  if (length < 7)
696
746
  return fromCharCode(a, b, c, d, e, f)
697
- let g = src[position++];
747
+ let g = src[position$1++];
698
748
  if ((g & 0x80) > 0) {
699
- position -= 7;
749
+ position$1 -= 7;
700
750
  return
701
751
  }
702
752
  return fromCharCode(a, b, c, d, e, f, g)
703
753
  } else {
704
- let e = src[position++];
705
- let f = src[position++];
706
- let g = src[position++];
707
- let h = src[position++];
754
+ let e = src[position$1++];
755
+ let f = src[position$1++];
756
+ let g = src[position$1++];
757
+ let h = src[position$1++];
708
758
  if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
709
- position -= 8;
759
+ position$1 -= 8;
710
760
  return
711
761
  }
712
762
  if (length < 10) {
713
763
  if (length === 8)
714
764
  return fromCharCode(a, b, c, d, e, f, g, h)
715
765
  else {
716
- let i = src[position++];
766
+ let i = src[position$1++];
717
767
  if ((i & 0x80) > 0) {
718
- position -= 9;
768
+ position$1 -= 9;
719
769
  return
720
770
  }
721
771
  return fromCharCode(a, b, c, d, e, f, g, h, i)
722
772
  }
723
773
  } else if (length < 12) {
724
- let i = src[position++];
725
- let j = src[position++];
774
+ let i = src[position$1++];
775
+ let j = src[position$1++];
726
776
  if ((i & 0x80) > 0 || (j & 0x80) > 0) {
727
- position -= 10;
777
+ position$1 -= 10;
728
778
  return
729
779
  }
730
780
  if (length < 11)
731
781
  return fromCharCode(a, b, c, d, e, f, g, h, i, j)
732
- let k = src[position++];
782
+ let k = src[position$1++];
733
783
  if ((k & 0x80) > 0) {
734
- position -= 11;
784
+ position$1 -= 11;
735
785
  return
736
786
  }
737
787
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
738
788
  } else {
739
- let i = src[position++];
740
- let j = src[position++];
741
- let k = src[position++];
742
- let l = src[position++];
789
+ let i = src[position$1++];
790
+ let j = src[position$1++];
791
+ let k = src[position$1++];
792
+ let l = src[position$1++];
743
793
  if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
744
- position -= 12;
794
+ position$1 -= 12;
745
795
  return
746
796
  }
747
797
  if (length < 14) {
748
798
  if (length === 12)
749
799
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
750
800
  else {
751
- let m = src[position++];
801
+ let m = src[position$1++];
752
802
  if ((m & 0x80) > 0) {
753
- position -= 13;
803
+ position$1 -= 13;
754
804
  return
755
805
  }
756
806
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
757
807
  }
758
808
  } else {
759
- let m = src[position++];
760
- let n = src[position++];
809
+ let m = src[position$1++];
810
+ let n = src[position$1++];
761
811
  if ((m & 0x80) > 0 || (n & 0x80) > 0) {
762
- position -= 14;
812
+ position$1 -= 14;
763
813
  return
764
814
  }
765
815
  if (length < 15)
766
816
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
767
- let o = src[position++];
817
+ let o = src[position$1++];
768
818
  if ((o & 0x80) > 0) {
769
- position -= 15;
819
+ position$1 -= 15;
770
820
  return
771
821
  }
772
822
  return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
@@ -777,7 +827,7 @@
777
827
  }
778
828
 
779
829
  function readOnlyJSString() {
780
- let token = src[position++];
830
+ let token = src[position$1++];
781
831
  let length;
782
832
  if (token < 0xc0) {
783
833
  // fixstr
@@ -786,17 +836,17 @@
786
836
  switch(token) {
787
837
  case 0xd9:
788
838
  // str 8
789
- length = src[position++];
839
+ length = src[position$1++];
790
840
  break
791
841
  case 0xda:
792
842
  // str 16
793
- length = dataView.getUint16(position);
794
- position += 2;
843
+ length = dataView.getUint16(position$1);
844
+ position$1 += 2;
795
845
  break
796
846
  case 0xdb:
797
847
  // str 32
798
- length = dataView.getUint32(position);
799
- position += 4;
848
+ length = dataView.getUint32(position$1);
849
+ position$1 += 4;
800
850
  break
801
851
  default:
802
852
  throw new Error('Expected string')
@@ -809,44 +859,44 @@
809
859
  function readBin(length) {
810
860
  return currentUnpackr.copyBuffers ?
811
861
  // specifically use the copying slice (not the node one)
812
- Uint8Array.prototype.slice.call(src, position, position += length) :
813
- src.subarray(position, position += length)
862
+ Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
863
+ src.subarray(position$1, position$1 += length)
814
864
  }
815
865
  function readExt(length) {
816
- let type = src[position++];
866
+ let type = src[position$1++];
817
867
  if (currentExtensions[type]) {
818
868
  let end;
819
- return currentExtensions[type](src.subarray(position, end = (position += length)), (readPosition) => {
820
- position = readPosition;
869
+ return currentExtensions[type](src.subarray(position$1, end = (position$1 += length)), (readPosition) => {
870
+ position$1 = readPosition;
821
871
  try {
822
872
  return read();
823
873
  } finally {
824
- position = end;
874
+ position$1 = end;
825
875
  }
826
876
  })
827
877
  }
828
878
  else
829
- throw new Error('Unknown extension type ' + type)``
879
+ throw new Error('Unknown extension type ' + type)
830
880
  }
831
881
 
832
882
  var keyCache = new Array(4096);
833
883
  function readKey() {
834
- let length = src[position++];
884
+ let length = src[position$1++];
835
885
  if (length >= 0xa0 && length < 0xc0) {
836
886
  // fixstr, potentially use key cache
837
887
  length = length - 0xa0;
838
- if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
839
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
888
+ if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
889
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
840
890
  else if (!(srcStringEnd == 0 && srcEnd < 180))
841
891
  return readFixedString(length)
842
892
  } else { // not cacheable, go back and do a standard read
843
- position--;
893
+ position$1--;
844
894
  return read().toString()
845
895
  }
846
- let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
896
+ let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
847
897
  let entry = keyCache[key];
848
- let checkPosition = position;
849
- let end = position + length - 3;
898
+ let checkPosition = position$1;
899
+ let end = position$1 + length - 3;
850
900
  let chunk;
851
901
  let i = 0;
852
902
  if (entry && entry.bytes == length) {
@@ -867,11 +917,11 @@
867
917
  }
868
918
  }
869
919
  if (checkPosition === end) {
870
- position = checkPosition;
920
+ position$1 = checkPosition;
871
921
  return entry.string
872
922
  }
873
923
  end -= 3;
874
- checkPosition = position;
924
+ checkPosition = position$1;
875
925
  }
876
926
  entry = [];
877
927
  keyCache[key] = entry;
@@ -919,10 +969,10 @@
919
969
 
920
970
  currentExtensions[0x69] = (data) => {
921
971
  // id extension (for structured clones)
922
- let id = dataView.getUint32(position - 4);
972
+ let id = dataView.getUint32(position$1 - 4);
923
973
  if (!referenceMap)
924
974
  referenceMap = new Map();
925
- let token = src[position];
975
+ let token = src[position$1];
926
976
  let target;
927
977
  // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
928
978
  // ahead past references to record structure definitions
@@ -942,7 +992,7 @@
942
992
 
943
993
  currentExtensions[0x70] = (data) => {
944
994
  // pointer extension (for structured clones)
945
- let id = dataView.getUint32(position - 4);
995
+ let id = dataView.getUint32(position$1 - 4);
946
996
  let refEntry = referenceMap.get(id);
947
997
  refEntry.used = true;
948
998
  return refEntry.target
@@ -967,14 +1017,14 @@
967
1017
  const TEMP_BUNDLE = [];
968
1018
  currentExtensions[0x62] = (data) => {
969
1019
  let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
970
- let dataPosition = position;
971
- position += dataSize - data.length;
972
- bundledStrings = TEMP_BUNDLE;
973
- bundledStrings = [readOnlyJSString(), readOnlyJSString()];
974
- bundledStrings.position0 = 0;
975
- bundledStrings.position1 = 0;
976
- bundledStrings.postBundlePosition = position;
977
- position = dataPosition;
1020
+ let dataPosition = position$1;
1021
+ position$1 += dataSize - data.length;
1022
+ bundledStrings$1 = TEMP_BUNDLE;
1023
+ bundledStrings$1 = [readOnlyJSString(), readOnlyJSString()];
1024
+ bundledStrings$1.position0 = 0;
1025
+ bundledStrings$1.position1 = 0;
1026
+ bundledStrings$1.postBundlePosition = position$1;
1027
+ position$1 = dataPosition;
978
1028
  return read()
979
1029
  };
980
1030
 
@@ -996,16 +1046,18 @@
996
1046
  // registration of bulk record definition?
997
1047
  // currentExtensions[0x52] = () =>
998
1048
 
999
- function saveState(callback) {
1049
+ function saveState$1(callback) {
1000
1050
  if (onSaveState)
1001
1051
  onSaveState();
1002
1052
  let savedSrcEnd = srcEnd;
1003
- let savedPosition = position;
1053
+ let savedPosition = position$1;
1054
+ let savedStringPosition = stringPosition;
1004
1055
  let savedSrcStringStart = srcStringStart;
1005
1056
  let savedSrcStringEnd = srcStringEnd;
1006
1057
  let savedSrcString = srcString;
1058
+ let savedStrings = strings;
1007
1059
  let savedReferenceMap = referenceMap;
1008
- let savedBundledStrings = bundledStrings;
1060
+ let savedBundledStrings = bundledStrings$1;
1009
1061
 
1010
1062
  // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1011
1063
  let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
@@ -1015,12 +1067,14 @@
1015
1067
  let savedSequentialMode = sequentialMode;
1016
1068
  let value = callback();
1017
1069
  srcEnd = savedSrcEnd;
1018
- position = savedPosition;
1070
+ position$1 = savedPosition;
1071
+ stringPosition = savedStringPosition;
1019
1072
  srcStringStart = savedSrcStringStart;
1020
1073
  srcStringEnd = savedSrcStringEnd;
1021
1074
  srcString = savedSrcString;
1075
+ strings = savedStrings;
1022
1076
  referenceMap = savedReferenceMap;
1023
- bundledStrings = savedBundledStrings;
1077
+ bundledStrings$1 = savedBundledStrings;
1024
1078
  src = savedSrc;
1025
1079
  sequentialMode = savedSequentialMode;
1026
1080
  currentStructures = savedStructures;
@@ -1035,37 +1089,60 @@
1035
1089
  currentStructures = null;
1036
1090
  }
1037
1091
 
1092
+ function addExtension$2(extension) {
1093
+ if (extension.unpack)
1094
+ currentExtensions[extension.type] = extension.unpack;
1095
+ else
1096
+ currentExtensions[extension.type] = extension;
1097
+ }
1098
+
1038
1099
  const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1039
1100
  for (let i = 0; i < 256; i++) {
1040
1101
  mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
1041
1102
  }
1042
- var defaultUnpackr = new Unpackr({ useRecords: false });
1103
+ var defaultUnpackr = new Unpackr$1({ useRecords: false });
1104
+ const unpack$1 = defaultUnpackr.unpack;
1105
+ const unpackMultiple$1 = defaultUnpackr.unpackMultiple;
1106
+ defaultUnpackr.unpack;
1107
+ const FLOAT32_OPTIONS = {
1108
+ NEVER: 0,
1109
+ ALWAYS: 1,
1110
+ DECIMAL_ROUND: 3,
1111
+ DECIMAL_FIT: 4
1112
+ };
1113
+ let f32Array = new Float32Array(1);
1114
+ let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
1115
+ function roundFloat32$1(float32Number) {
1116
+ f32Array[0] = float32Number;
1117
+ let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1118
+ return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1119
+ }
1043
1120
  function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1044
- readStruct = updatedReadStruct;
1045
- onLoadedStructures = loadedStructs;
1121
+ readStruct$1 = updatedReadStruct;
1122
+ onLoadedStructures$1 = loadedStructs;
1046
1123
  onSaveState = saveState;
1047
1124
  }
1048
1125
 
1049
- let textEncoder;
1126
+ let textEncoder$1;
1050
1127
  try {
1051
- textEncoder = new TextEncoder();
1128
+ textEncoder$1 = new TextEncoder();
1052
1129
  } catch (error) {}
1053
1130
  let extensions, extensionClasses;
1054
- const hasNodeBuffer = typeof Buffer !== 'undefined';
1055
- const ByteArrayAllocate = hasNodeBuffer ?
1131
+ const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
1132
+ const ByteArrayAllocate = hasNodeBuffer$1 ?
1056
1133
  function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
1057
- const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1058
- const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1134
+ const ByteArray = hasNodeBuffer$1 ? Buffer : Uint8Array;
1135
+ const MAX_BUFFER_SIZE = hasNodeBuffer$1 ? 0x100000000 : 0x7fd00000;
1059
1136
  let target, keysTarget;
1060
1137
  let targetView;
1061
- let position$1 = 0;
1138
+ let position = 0;
1062
1139
  let safeEnd;
1063
- let bundledStrings$1 = null;
1140
+ let bundledStrings = null;
1064
1141
  let writeStructSlots;
1065
1142
  const MAX_BUNDLE_SIZE = 0xf000;
1066
1143
  const hasNonLatin = /[\u0080-\uFFFF]/;
1067
1144
  const RECORD_SYMBOL = Symbol('record-id');
1068
- class Packr extends Unpackr {
1145
+ let Packr$1 = class Packr extends Unpackr$1 {
1069
1146
  constructor(options) {
1070
1147
  super(options);
1071
1148
  this.offset = 0;
@@ -1075,9 +1152,9 @@
1075
1152
  let referenceMap;
1076
1153
  let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1077
1154
  return target.utf8Write(string, position, 0xffffffff)
1078
- } : (textEncoder && textEncoder.encodeInto) ?
1155
+ } : (textEncoder$1 && textEncoder$1.encodeInto) ?
1079
1156
  function(string, position) {
1080
- return textEncoder.encodeInto(string, target.subarray(position)).written
1157
+ return textEncoder$1.encodeInto(string, target.subarray(position)).written
1081
1158
  } : false;
1082
1159
 
1083
1160
  let packr = this;
@@ -1112,25 +1189,25 @@
1112
1189
  this.pack = this.encode = function(value, encodeOptions) {
1113
1190
  if (!target) {
1114
1191
  target = new ByteArrayAllocate(8192);
1115
- targetView = target.dataView = new DataView(target.buffer, 0, 8192);
1116
- position$1 = 0;
1192
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, 8192));
1193
+ position = 0;
1117
1194
  }
1118
1195
  safeEnd = target.length - 10;
1119
- if (safeEnd - position$1 < 0x800) {
1196
+ if (safeEnd - position < 0x800) {
1120
1197
  // don't start too close to the end,
1121
1198
  target = new ByteArrayAllocate(target.length);
1122
- targetView = target.dataView = new DataView(target.buffer, 0, target.length);
1199
+ targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1123
1200
  safeEnd = target.length - 10;
1124
- position$1 = 0;
1201
+ position = 0;
1125
1202
  } else
1126
- position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1127
- start = position$1;
1203
+ position = (position + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1204
+ start = position;
1128
1205
  referenceMap = packr.structuredClone ? new Map() : null;
1129
1206
  if (packr.bundleStrings && typeof value !== 'string') {
1130
- bundledStrings$1 = [];
1131
- bundledStrings$1.size = Infinity; // force a new bundle start on first string
1207
+ bundledStrings = [];
1208
+ bundledStrings.size = Infinity; // force a new bundle start on first string
1132
1209
  } else
1133
- bundledStrings$1 = null;
1210
+ bundledStrings = null;
1134
1211
  structures = packr.structures;
1135
1212
  if (structures) {
1136
1213
  if (structures.uninitialized)
@@ -1171,8 +1248,8 @@
1171
1248
  writeStruct(value);
1172
1249
  else
1173
1250
  pack(value);
1174
- let lastBundle = bundledStrings$1;
1175
- if (bundledStrings$1)
1251
+ let lastBundle = bundledStrings;
1252
+ if (bundledStrings)
1176
1253
  writeBundles(start, pack, 0);
1177
1254
  if (referenceMap && referenceMap.idsToInsert) {
1178
1255
  let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
@@ -1201,21 +1278,21 @@
1201
1278
  targetView.setUint32(lastBundle.position + start,
1202
1279
  targetView.getUint32(lastBundle.position + start) + incrementPosition);
1203
1280
  }
1204
- position$1 += idsToInsert.length * 6;
1205
- if (position$1 > safeEnd)
1206
- makeRoom(position$1);
1207
- packr.offset = position$1;
1208
- let serialized = insertIds(target.subarray(start, position$1), idsToInsert);
1281
+ position += idsToInsert.length * 6;
1282
+ if (position > safeEnd)
1283
+ makeRoom(position);
1284
+ packr.offset = position;
1285
+ let serialized = insertIds(target.subarray(start, position), idsToInsert);
1209
1286
  referenceMap = null;
1210
1287
  return serialized
1211
1288
  }
1212
- packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1289
+ packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1213
1290
  if (encodeOptions & REUSE_BUFFER_MODE) {
1214
1291
  target.start = start;
1215
- target.end = position$1;
1292
+ target.end = position;
1216
1293
  return target
1217
1294
  }
1218
- return target.subarray(start, position$1) // position can change if we call pack again in saveStructures, so we get the buffer now
1295
+ return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1219
1296
  } finally {
1220
1297
  if (structures) {
1221
1298
  if (serializationsSinceTransitionRebuild < 10)
@@ -1238,8 +1315,8 @@
1238
1315
  }
1239
1316
  if (hasSharedUpdate && packr.saveStructures) {
1240
1317
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1241
- let returnBuffer = target.subarray(start, position$1);
1242
- let newSharedData = prepareStructures(structures, packr);
1318
+ let returnBuffer = target.subarray(start, position);
1319
+ let newSharedData = prepareStructures$1(structures, packr);
1243
1320
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1244
1321
  // get updated structures and try again if the update failed
1245
1322
  return packr.pack(value)
@@ -1249,47 +1326,64 @@
1249
1326
  }
1250
1327
  }
1251
1328
  if (encodeOptions & RESET_BUFFER_MODE)
1252
- position$1 = start;
1329
+ position = start;
1330
+ }
1331
+ };
1332
+ const packArray = (value) => {
1333
+ var length = value.length;
1334
+ if (length < 0x10) {
1335
+ target[position++] = 0x90 | length;
1336
+ } else if (length < 0x10000) {
1337
+ target[position++] = 0xdc;
1338
+ target[position++] = length >> 8;
1339
+ target[position++] = length & 0xff;
1340
+ } else {
1341
+ target[position++] = 0xdd;
1342
+ targetView.setUint32(position, length);
1343
+ position += 4;
1344
+ }
1345
+ for (let i = 0; i < length; i++) {
1346
+ pack(value[i]);
1253
1347
  }
1254
1348
  };
1255
1349
  const pack = (value) => {
1256
- if (position$1 > safeEnd)
1257
- target = makeRoom(position$1);
1350
+ if (position > safeEnd)
1351
+ target = makeRoom(position);
1258
1352
 
1259
1353
  var type = typeof value;
1260
1354
  var length;
1261
1355
  if (type === 'string') {
1262
1356
  let strLength = value.length;
1263
- if (bundledStrings$1 && strLength >= 4 && strLength < 0x1000) {
1264
- if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1357
+ if (bundledStrings && strLength >= 4 && strLength < 0x1000) {
1358
+ if ((bundledStrings.size += strLength) > MAX_BUNDLE_SIZE) {
1265
1359
  let extStart;
1266
- let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1267
- if (position$1 + maxBytes > safeEnd)
1268
- target = makeRoom(position$1 + maxBytes);
1360
+ let maxBytes = (bundledStrings[0] ? bundledStrings[0].length * 3 + bundledStrings[1].length : 0) + 10;
1361
+ if (position + maxBytes > safeEnd)
1362
+ target = makeRoom(position + maxBytes);
1269
1363
  let lastBundle;
1270
- 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
1271
- lastBundle = bundledStrings$1;
1272
- target[position$1] = 0xc8; // ext 16
1273
- position$1 += 3; // reserve for the writing bundle size
1274
- target[position$1++] = 0x62; // 'b'
1275
- extStart = position$1 - start;
1276
- position$1 += 4; // reserve for writing bundle reference
1364
+ 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
1365
+ lastBundle = bundledStrings;
1366
+ target[position] = 0xc8; // ext 16
1367
+ position += 3; // reserve for the writing bundle size
1368
+ target[position++] = 0x62; // 'b'
1369
+ extStart = position - start;
1370
+ position += 4; // reserve for writing bundle reference
1277
1371
  writeBundles(start, pack, 0); // write the last bundles
1278
- targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1372
+ targetView.setUint16(extStart + start - 3, position - start - extStart);
1279
1373
  } 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)
1280
- target[position$1++] = 0xd6; // fixext 4
1281
- target[position$1++] = 0x62; // 'b'
1282
- extStart = position$1 - start;
1283
- position$1 += 4; // reserve for writing bundle reference
1374
+ target[position++] = 0xd6; // fixext 4
1375
+ target[position++] = 0x62; // 'b'
1376
+ extStart = position - start;
1377
+ position += 4; // reserve for writing bundle reference
1284
1378
  }
1285
- bundledStrings$1 = ['', '']; // create new ones
1286
- bundledStrings$1.previous = lastBundle;
1287
- bundledStrings$1.size = 0;
1288
- bundledStrings$1.position = extStart;
1379
+ bundledStrings = ['', '']; // create new ones
1380
+ bundledStrings.previous = lastBundle;
1381
+ bundledStrings.size = 0;
1382
+ bundledStrings.position = extStart;
1289
1383
  }
1290
1384
  let twoByte = hasNonLatin.test(value);
1291
- bundledStrings$1[twoByte ? 0 : 1] += value;
1292
- target[position$1++] = 0xc1;
1385
+ bundledStrings[twoByte ? 0 : 1] += value;
1386
+ target[position++] = 0xc1;
1293
1387
  pack(twoByte ? -strLength : strLength);
1294
1388
  return
1295
1389
  }
@@ -1305,11 +1399,11 @@
1305
1399
  headerSize = 5;
1306
1400
  }
1307
1401
  let maxBytes = strLength * 3;
1308
- if (position$1 + maxBytes > safeEnd)
1309
- target = makeRoom(position$1 + maxBytes);
1402
+ if (position + maxBytes > safeEnd)
1403
+ target = makeRoom(position + maxBytes);
1310
1404
 
1311
1405
  if (strLength < 0x40 || !encodeUtf8) {
1312
- let i, c1, c2, strPosition = position$1 + headerSize;
1406
+ let i, c1, c2, strPosition = position + headerSize;
1313
1407
  for (i = 0; i < strLength; i++) {
1314
1408
  c1 = value.charCodeAt(i);
1315
1409
  if (c1 < 0x80) {
@@ -1333,88 +1427,88 @@
1333
1427
  target[strPosition++] = c1 & 0x3f | 0x80;
1334
1428
  }
1335
1429
  }
1336
- length = strPosition - position$1 - headerSize;
1430
+ length = strPosition - position - headerSize;
1337
1431
  } else {
1338
- length = encodeUtf8(value, position$1 + headerSize);
1432
+ length = encodeUtf8(value, position + headerSize);
1339
1433
  }
1340
1434
 
1341
1435
  if (length < 0x20) {
1342
- target[position$1++] = 0xa0 | length;
1436
+ target[position++] = 0xa0 | length;
1343
1437
  } else if (length < 0x100) {
1344
1438
  if (headerSize < 2) {
1345
- target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1439
+ target.copyWithin(position + 2, position + 1, position + 1 + length);
1346
1440
  }
1347
- target[position$1++] = 0xd9;
1348
- target[position$1++] = length;
1441
+ target[position++] = 0xd9;
1442
+ target[position++] = length;
1349
1443
  } else if (length < 0x10000) {
1350
1444
  if (headerSize < 3) {
1351
- target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1445
+ target.copyWithin(position + 3, position + 2, position + 2 + length);
1352
1446
  }
1353
- target[position$1++] = 0xda;
1354
- target[position$1++] = length >> 8;
1355
- target[position$1++] = length & 0xff;
1447
+ target[position++] = 0xda;
1448
+ target[position++] = length >> 8;
1449
+ target[position++] = length & 0xff;
1356
1450
  } else {
1357
1451
  if (headerSize < 5) {
1358
- target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1452
+ target.copyWithin(position + 5, position + 3, position + 3 + length);
1359
1453
  }
1360
- target[position$1++] = 0xdb;
1361
- targetView.setUint32(position$1, length);
1362
- position$1 += 4;
1454
+ target[position++] = 0xdb;
1455
+ targetView.setUint32(position, length);
1456
+ position += 4;
1363
1457
  }
1364
- position$1 += length;
1458
+ position += length;
1365
1459
  } else if (type === 'number') {
1366
1460
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1367
1461
  // positive uint
1368
1462
  if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
1369
- target[position$1++] = value;
1463
+ target[position++] = value;
1370
1464
  } else if (value < 0x100) {
1371
- target[position$1++] = 0xcc;
1372
- target[position$1++] = value;
1465
+ target[position++] = 0xcc;
1466
+ target[position++] = value;
1373
1467
  } else if (value < 0x10000) {
1374
- target[position$1++] = 0xcd;
1375
- target[position$1++] = value >> 8;
1376
- target[position$1++] = value & 0xff;
1468
+ target[position++] = 0xcd;
1469
+ target[position++] = value >> 8;
1470
+ target[position++] = value & 0xff;
1377
1471
  } else {
1378
- target[position$1++] = 0xce;
1379
- targetView.setUint32(position$1, value);
1380
- position$1 += 4;
1472
+ target[position++] = 0xce;
1473
+ targetView.setUint32(position, value);
1474
+ position += 4;
1381
1475
  }
1382
1476
  } else if (value >> 0 === value) { // negative integer
1383
1477
  if (value >= -0x20) {
1384
- target[position$1++] = 0x100 + value;
1478
+ target[position++] = 0x100 + value;
1385
1479
  } else if (value >= -0x80) {
1386
- target[position$1++] = 0xd0;
1387
- target[position$1++] = value + 0x100;
1480
+ target[position++] = 0xd0;
1481
+ target[position++] = value + 0x100;
1388
1482
  } else if (value >= -0x8000) {
1389
- target[position$1++] = 0xd1;
1390
- targetView.setInt16(position$1, value);
1391
- position$1 += 2;
1483
+ target[position++] = 0xd1;
1484
+ targetView.setInt16(position, value);
1485
+ position += 2;
1392
1486
  } else {
1393
- target[position$1++] = 0xd2;
1394
- targetView.setInt32(position$1, value);
1395
- position$1 += 4;
1487
+ target[position++] = 0xd2;
1488
+ targetView.setInt32(position, value);
1489
+ position += 4;
1396
1490
  }
1397
1491
  } else {
1398
1492
  let useFloat32;
1399
1493
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1400
- target[position$1++] = 0xca;
1401
- targetView.setFloat32(position$1, value);
1494
+ target[position++] = 0xca;
1495
+ targetView.setFloat32(position, value);
1402
1496
  let xShifted;
1403
1497
  if (useFloat32 < 4 ||
1404
1498
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1405
- ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1406
- position$1 += 4;
1499
+ ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1500
+ position += 4;
1407
1501
  return
1408
1502
  } else
1409
- position$1--; // move back into position for writing a double
1503
+ position--; // move back into position for writing a double
1410
1504
  }
1411
- target[position$1++] = 0xcb;
1412
- targetView.setFloat64(position$1, value);
1413
- position$1 += 8;
1505
+ target[position++] = 0xcb;
1506
+ targetView.setFloat64(position, value);
1507
+ position += 8;
1414
1508
  }
1415
1509
  } else if (type === 'object') {
1416
1510
  if (!value)
1417
- target[position$1++] = 0xc0;
1511
+ target[position++] = 0xc0;
1418
1512
  else {
1419
1513
  if (referenceMap) {
1420
1514
  let referee = referenceMap.get(value);
@@ -1423,45 +1517,31 @@
1423
1517
  let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1424
1518
  referee.id = idsToInsert.push(referee);
1425
1519
  }
1426
- target[position$1++] = 0xd6; // fixext 4
1427
- target[position$1++] = 0x70; // "p" for pointer
1428
- targetView.setUint32(position$1, referee.id);
1429
- position$1 += 4;
1520
+ target[position++] = 0xd6; // fixext 4
1521
+ target[position++] = 0x70; // "p" for pointer
1522
+ targetView.setUint32(position, referee.id);
1523
+ position += 4;
1430
1524
  return
1431
1525
  } else
1432
- referenceMap.set(value, { offset: position$1 - start });
1526
+ referenceMap.set(value, { offset: position - start });
1433
1527
  }
1434
1528
  let constructor = value.constructor;
1435
1529
  if (constructor === Object) {
1436
1530
  writeObject(value, true);
1437
1531
  } else if (constructor === Array) {
1438
- length = value.length;
1439
- if (length < 0x10) {
1440
- target[position$1++] = 0x90 | length;
1441
- } else if (length < 0x10000) {
1442
- target[position$1++] = 0xdc;
1443
- target[position$1++] = length >> 8;
1444
- target[position$1++] = length & 0xff;
1445
- } else {
1446
- target[position$1++] = 0xdd;
1447
- targetView.setUint32(position$1, length);
1448
- position$1 += 4;
1449
- }
1450
- for (let i = 0; i < length; i++) {
1451
- pack(value[i]);
1452
- }
1532
+ packArray(value);
1453
1533
  } else if (constructor === Map) {
1454
1534
  length = value.size;
1455
1535
  if (length < 0x10) {
1456
- target[position$1++] = 0x80 | length;
1536
+ target[position++] = 0x80 | length;
1457
1537
  } else if (length < 0x10000) {
1458
- target[position$1++] = 0xde;
1459
- target[position$1++] = length >> 8;
1460
- target[position$1++] = length & 0xff;
1538
+ target[position++] = 0xde;
1539
+ target[position++] = length >> 8;
1540
+ target[position++] = length & 0xff;
1461
1541
  } else {
1462
- target[position$1++] = 0xdf;
1463
- targetView.setUint32(position$1, length);
1464
- position$1 += 4;
1542
+ target[position++] = 0xdf;
1543
+ targetView.setUint32(position, length);
1544
+ position += 4;
1465
1545
  }
1466
1546
  for (let [ key, entryValue ] of value) {
1467
1547
  pack(key);
@@ -1474,16 +1554,25 @@
1474
1554
  let extension = extensions[i];
1475
1555
  if (extension.write) {
1476
1556
  if (extension.type) {
1477
- target[position$1++] = 0xd4; // one byte "tag" extension
1478
- target[position$1++] = extension.type;
1479
- target[position$1++] = 0;
1557
+ target[position++] = 0xd4; // one byte "tag" extension
1558
+ target[position++] = extension.type;
1559
+ target[position++] = 0;
1560
+ }
1561
+ let writeResult = extension.write.call(this, value);
1562
+ if (writeResult === value) { // avoid infinite recursion
1563
+ if (Array.isArray(value)) {
1564
+ packArray(value);
1565
+ } else {
1566
+ writeObject(value);
1567
+ }
1568
+ } else {
1569
+ pack(writeResult);
1480
1570
  }
1481
- pack(extension.write.call(this, value));
1482
1571
  return
1483
1572
  }
1484
1573
  let currentTarget = target;
1485
1574
  let currentTargetView = targetView;
1486
- let currentPosition = position$1;
1575
+ let currentPosition = position;
1487
1576
  target = null;
1488
1577
  let result;
1489
1578
  try {
@@ -1491,11 +1580,11 @@
1491
1580
  // restore target and use it
1492
1581
  target = currentTarget;
1493
1582
  currentTarget = null;
1494
- position$1 += size;
1495
- if (position$1 > safeEnd)
1496
- makeRoom(position$1);
1583
+ position += size;
1584
+ if (position > safeEnd)
1585
+ makeRoom(position);
1497
1586
  return {
1498
- target, targetView, position: position$1 - size
1587
+ target, targetView, position: position - size
1499
1588
  }
1500
1589
  }, pack);
1501
1590
  } finally {
@@ -1503,50 +1592,55 @@
1503
1592
  if (currentTarget) {
1504
1593
  target = currentTarget;
1505
1594
  targetView = currentTargetView;
1506
- position$1 = currentPosition;
1595
+ position = currentPosition;
1507
1596
  safeEnd = target.length - 10;
1508
1597
  }
1509
1598
  }
1510
1599
  if (result) {
1511
- if (result.length + position$1 > safeEnd)
1512
- makeRoom(result.length + position$1);
1513
- position$1 = writeExtensionData(result, target, position$1, extension.type);
1600
+ if (result.length + position > safeEnd)
1601
+ makeRoom(result.length + position);
1602
+ position = writeExtensionData(result, target, position, extension.type);
1514
1603
  }
1515
1604
  return
1516
1605
  }
1517
1606
  }
1518
- // no extension found, write as object
1519
- writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1607
+ // check isArray after extensions, because extensions can extend Array
1608
+ if (Array.isArray(value)) {
1609
+ packArray(value);
1610
+ } else {
1611
+ // no extension found, write as object
1612
+ writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1613
+ }
1520
1614
  }
1521
1615
  }
1522
1616
  } else if (type === 'boolean') {
1523
- target[position$1++] = value ? 0xc3 : 0xc2;
1617
+ target[position++] = value ? 0xc3 : 0xc2;
1524
1618
  } else if (type === 'bigint') {
1525
1619
  if (value < (BigInt(1)<<BigInt(63)) && value >= -(BigInt(1)<<BigInt(63))) {
1526
1620
  // use a signed int as long as it fits
1527
- target[position$1++] = 0xd3;
1528
- targetView.setBigInt64(position$1, value);
1621
+ target[position++] = 0xd3;
1622
+ targetView.setBigInt64(position, value);
1529
1623
  } else if (value < (BigInt(1)<<BigInt(64)) && value > 0) {
1530
1624
  // if we can fit an unsigned int, use that
1531
- target[position$1++] = 0xcf;
1532
- targetView.setBigUint64(position$1, value);
1625
+ target[position++] = 0xcf;
1626
+ targetView.setBigUint64(position, value);
1533
1627
  } else {
1534
1628
  // overflow
1535
1629
  if (this.largeBigIntToFloat) {
1536
- target[position$1++] = 0xcb;
1537
- targetView.setFloat64(position$1, Number(value));
1630
+ target[position++] = 0xcb;
1631
+ targetView.setFloat64(position, Number(value));
1538
1632
  } else {
1539
1633
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1540
1634
  }
1541
1635
  }
1542
- position$1 += 8;
1636
+ position += 8;
1543
1637
  } else if (type === 'undefined') {
1544
1638
  if (this.encodeUndefinedAsNil)
1545
- target[position$1++] = 0xc0;
1639
+ target[position++] = 0xc0;
1546
1640
  else {
1547
- target[position$1++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1548
- target[position$1++] = 0;
1549
- target[position$1++] = 0;
1641
+ target[position++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1642
+ target[position++] = 0;
1643
+ target[position++] = 0;
1550
1644
  }
1551
1645
  } else if (type === 'function') {
1552
1646
  pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
@@ -1560,15 +1654,15 @@
1560
1654
  let keys = Object.keys(object);
1561
1655
  let length = keys.length;
1562
1656
  if (length < 0x10) {
1563
- target[position$1++] = 0x80 | length;
1657
+ target[position++] = 0x80 | length;
1564
1658
  } else if (length < 0x10000) {
1565
- target[position$1++] = 0xde;
1566
- target[position$1++] = length >> 8;
1567
- target[position$1++] = length & 0xff;
1659
+ target[position++] = 0xde;
1660
+ target[position++] = length >> 8;
1661
+ target[position++] = length & 0xff;
1568
1662
  } else {
1569
- target[position$1++] = 0xdf;
1570
- targetView.setUint32(position$1, length);
1571
- position$1 += 4;
1663
+ target[position++] = 0xdf;
1664
+ targetView.setUint32(position, length);
1665
+ position += 4;
1572
1666
  }
1573
1667
  let key;
1574
1668
  for (let i = 0; i < length; i++) {
@@ -1577,9 +1671,9 @@
1577
1671
  }
1578
1672
  } :
1579
1673
  (object, safePrototype) => {
1580
- target[position$1++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1581
- let objectOffset = position$1 - start;
1582
- position$1 += 2;
1674
+ target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1675
+ let objectOffset = position - start;
1676
+ position += 2;
1583
1677
  let size = 0;
1584
1678
  for (let key in object) {
1585
1679
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1594,7 +1688,7 @@
1594
1688
  (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)
1595
1689
  (object, safePrototype) => {
1596
1690
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1597
- let objectOffset = position$1++ - start;
1691
+ let objectOffset = position++ - start;
1598
1692
  let wroteKeys;
1599
1693
  for (let key in object) {
1600
1694
  if (safePrototype || object.hasOwnProperty(key)) {
@@ -1616,9 +1710,9 @@
1616
1710
  }
1617
1711
  transition = nextTransition;
1618
1712
  }
1619
- if (objectOffset + start + 1 == position$1) {
1713
+ if (objectOffset + start + 1 == position) {
1620
1714
  // first key, so we don't need to insert, we can just write record directly
1621
- position$1--;
1715
+ position--;
1622
1716
  newRecord(transition, keys, newTransitions);
1623
1717
  } else // otherwise we need to insert the record, moving existing data after the record
1624
1718
  insertNewRecord(transition, keys, objectOffset, newTransitions);
@@ -1650,10 +1744,10 @@
1650
1744
  let recordId = transition[RECORD_SYMBOL];
1651
1745
  if (recordId) {
1652
1746
  if (recordId >= 0x60 && useTwoByteRecords) {
1653
- target[position$1++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1654
- target[position$1++] = recordId >> 5;
1747
+ target[position++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1748
+ target[position++] = recordId >> 5;
1655
1749
  } else
1656
- target[position$1++] = recordId;
1750
+ target[position++] = recordId;
1657
1751
  } else {
1658
1752
  newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
1659
1753
  }
@@ -1673,13 +1767,13 @@
1673
1767
  } else // faster handling for smaller buffers
1674
1768
  newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
1675
1769
  let newBuffer = new ByteArrayAllocate(newSize);
1676
- targetView = newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize);
1770
+ targetView = newBuffer.dataView || (newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize));
1677
1771
  end = Math.min(end, target.length);
1678
1772
  if (target.copy)
1679
1773
  target.copy(newBuffer, 0, start, end);
1680
1774
  else
1681
1775
  newBuffer.set(target.slice(start, end));
1682
- position$1 -= start;
1776
+ position -= start;
1683
1777
  start = 0;
1684
1778
  safeEnd = newBuffer.length - 10;
1685
1779
  return target = newBuffer
@@ -1708,21 +1802,21 @@
1708
1802
  structures.sharedLength = recordId - 0x3f;
1709
1803
  hasSharedUpdate = true;
1710
1804
  if (highByte >= 0) {
1711
- target[position$1++] = (recordId & 0x1f) + 0x60;
1712
- target[position$1++] = highByte;
1805
+ target[position++] = (recordId & 0x1f) + 0x60;
1806
+ target[position++] = highByte;
1713
1807
  } else {
1714
- target[position$1++] = recordId;
1808
+ target[position++] = recordId;
1715
1809
  }
1716
1810
  } else {
1717
1811
  if (highByte >= 0) {
1718
- target[position$1++] = 0xd5; // fixext 2
1719
- target[position$1++] = 0x72; // "r" record defintion extension type
1720
- target[position$1++] = (recordId & 0x1f) + 0x60;
1721
- target[position$1++] = highByte;
1812
+ target[position++] = 0xd5; // fixext 2
1813
+ target[position++] = 0x72; // "r" record defintion extension type
1814
+ target[position++] = (recordId & 0x1f) + 0x60;
1815
+ target[position++] = highByte;
1722
1816
  } else {
1723
- target[position$1++] = 0xd4; // fixext 1
1724
- target[position$1++] = 0x72; // "r" record defintion extension type
1725
- target[position$1++] = recordId;
1817
+ target[position++] = 0xd4; // fixext 1
1818
+ target[position++] = 0x72; // "r" record defintion extension type
1819
+ target[position++] = recordId;
1726
1820
  }
1727
1821
 
1728
1822
  if (newTransitions)
@@ -1736,57 +1830,57 @@
1736
1830
  };
1737
1831
  const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
1738
1832
  let mainTarget = target;
1739
- let mainPosition = position$1;
1833
+ let mainPosition = position;
1740
1834
  let mainSafeEnd = safeEnd;
1741
1835
  let mainStart = start;
1742
1836
  target = keysTarget;
1743
- position$1 = 0;
1837
+ position = 0;
1744
1838
  start = 0;
1745
1839
  if (!target)
1746
1840
  keysTarget = target = new ByteArrayAllocate(8192);
1747
1841
  safeEnd = target.length - 10;
1748
1842
  newRecord(transition, keys, newTransitions);
1749
1843
  keysTarget = target;
1750
- let keysPosition = position$1;
1844
+ let keysPosition = position;
1751
1845
  target = mainTarget;
1752
- position$1 = mainPosition;
1846
+ position = mainPosition;
1753
1847
  safeEnd = mainSafeEnd;
1754
1848
  start = mainStart;
1755
1849
  if (keysPosition > 1) {
1756
- let newEnd = position$1 + keysPosition - 1;
1850
+ let newEnd = position + keysPosition - 1;
1757
1851
  if (newEnd > safeEnd)
1758
1852
  makeRoom(newEnd);
1759
1853
  let insertionPosition = insertionOffset + start;
1760
- target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position$1);
1854
+ target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position);
1761
1855
  target.set(keysTarget.slice(0, keysPosition), insertionPosition);
1762
- position$1 = newEnd;
1856
+ position = newEnd;
1763
1857
  } else {
1764
1858
  target[insertionOffset + start] = keysTarget[0];
1765
1859
  }
1766
1860
  };
1767
1861
  const writeStruct = (object, safePrototype) => {
1768
- let newPosition = writeStructSlots(object, target, position$1, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1862
+ let newPosition = writeStructSlots(object, target, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1769
1863
  if (notifySharedUpdate)
1770
1864
  return hasSharedUpdate = true;
1771
- position$1 = newPosition;
1865
+ position = newPosition;
1772
1866
  if (start > 0) {
1773
1867
  pack(value);
1774
1868
  if (start == 0)
1775
- return { position: position$1, targetView, target }; // indicate the buffer was re-allocated
1869
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1776
1870
  } else
1777
1871
  pack(value);
1778
- return position$1;
1872
+ return position;
1779
1873
  }, this);
1780
1874
  if (newPosition === 0) // bail and go to a msgpack object
1781
1875
  return writeObject(object, true);
1782
- position$1 = newPosition;
1876
+ position = newPosition;
1783
1877
  };
1784
1878
  }
1785
1879
  useBuffer(buffer) {
1786
1880
  // this means we are finished using our own buffer and we can write over it safely
1787
1881
  target = buffer;
1788
1882
  targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1789
- position$1 = 0;
1883
+ position = 0;
1790
1884
  }
1791
1885
  clearSharedData() {
1792
1886
  if (this.structures)
@@ -1794,7 +1888,7 @@
1794
1888
  if (this.typedStructs)
1795
1889
  this.typedStructs = [];
1796
1890
  }
1797
- }
1891
+ };
1798
1892
 
1799
1893
  extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, C1Type ];
1800
1894
  extensions = [{
@@ -1869,7 +1963,7 @@
1869
1963
  if (this.moreTypes)
1870
1964
  writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
1871
1965
  else
1872
- writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
1966
+ writeBuffer(hasNodeBuffer$1 ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
1873
1967
  }
1874
1968
  }, {
1875
1969
  pack(typedArray, allocateForWrite) {
@@ -1991,16 +2085,28 @@
1991
2085
  }
1992
2086
 
1993
2087
  function writeBundles(start, pack, incrementPosition) {
1994
- if (bundledStrings$1.length > 0) {
1995
- targetView.setUint32(bundledStrings$1.position + start, position$1 + incrementPosition - bundledStrings$1.position - start);
1996
- bundledStrings$1.stringsPosition = position$1 - start;
1997
- let writeStrings = bundledStrings$1;
1998
- bundledStrings$1 = null;
2088
+ if (bundledStrings.length > 0) {
2089
+ targetView.setUint32(bundledStrings.position + start, position + incrementPosition - bundledStrings.position - start);
2090
+ bundledStrings.stringsPosition = position - start;
2091
+ let writeStrings = bundledStrings;
2092
+ bundledStrings = null;
1999
2093
  pack(writeStrings[0]);
2000
2094
  pack(writeStrings[1]);
2001
2095
  }
2002
2096
  }
2003
- function prepareStructures(structures, packr) {
2097
+
2098
+ function addExtension$1(extension) {
2099
+ if (extension.Class) {
2100
+ if (!extension.pack && !extension.write)
2101
+ throw new Error('Extension has no pack or write function')
2102
+ if (extension.pack && !extension.type)
2103
+ throw new Error('Extension has no type (numeric code to identify the extension)')
2104
+ extensionClasses.unshift(extension.Class);
2105
+ extensions.unshift(extension);
2106
+ }
2107
+ addExtension$2(extension);
2108
+ }
2109
+ function prepareStructures$1(structures, packr) {
2004
2110
  structures.isCompatible = (existingStructures) => {
2005
2111
  let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
2006
2112
  if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
@@ -2011,10 +2117,12 @@
2011
2117
  }
2012
2118
  function setWriteStructSlots(writeSlots, makeStructures) {
2013
2119
  writeStructSlots = writeSlots;
2014
- prepareStructures = makeStructures;
2120
+ prepareStructures$1 = makeStructures;
2015
2121
  }
2016
2122
 
2017
- let defaultPackr = new Packr({ useRecords: false });
2123
+ let defaultPackr = new Packr$1({ useRecords: false });
2124
+ const pack$1 = defaultPackr.pack;
2125
+ defaultPackr.pack;
2018
2126
  const REUSE_BUFFER_MODE = 512;
2019
2127
  const RESET_BUFFER_MODE = 1024;
2020
2128
 
@@ -2035,18 +2143,18 @@
2035
2143
  }
2036
2144
 
2037
2145
  let updatedPosition;
2038
- const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
2039
- let textEncoder$1, currentSource;
2146
+ const hasNodeBuffer = typeof Buffer !== 'undefined';
2147
+ let textEncoder, currentSource;
2040
2148
  try {
2041
- textEncoder$1 = new TextEncoder();
2149
+ textEncoder = new TextEncoder();
2042
2150
  } catch (error) {}
2043
- const encodeUtf8 = hasNodeBuffer$1 ? function(target, string, position) {
2151
+ const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
2044
2152
  return target.utf8Write(string, position, 0xffffffff)
2045
- } : (textEncoder$1 && textEncoder$1.encodeInto) ?
2153
+ } : (textEncoder && textEncoder.encodeInto) ?
2046
2154
  function(target, string, position) {
2047
- return textEncoder$1.encodeInto(string, target.subarray(position)).written
2155
+ return textEncoder.encodeInto(string, target.subarray(position)).written
2048
2156
  } : false;
2049
- setWriteStructSlots(writeStruct, prepareStructures$1);
2157
+ setWriteStructSlots(writeStruct, prepareStructures);
2050
2158
  function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
2051
2159
  let typedStructs = packr.typedStructs || (packr.typedStructs = []);
2052
2160
  // note that we rely on pack.js to load stored structures before we get to this point
@@ -2139,9 +2247,9 @@
2139
2247
  case 'string':
2140
2248
  let strLength = value.length;
2141
2249
  refOffset = refPosition - refsStartPosition;
2142
- if ((strLength << 2) + position > safeEnd) {
2250
+ if ((strLength << 2) + refPosition > safeEnd) {
2143
2251
  let lastStart = start;
2144
- target = makeRoom(refPosition);
2252
+ target = makeRoom((strLength << 2) + refPosition);
2145
2253
  targetView = target.dataView;
2146
2254
  position -= lastStart;
2147
2255
  refsStartPosition -= lastStart;
@@ -2421,7 +2529,7 @@
2421
2529
  newTransition.__parent = transition;
2422
2530
  return newTransition;
2423
2531
  }
2424
- function onLoadedStructures$1(sharedData) {
2532
+ function onLoadedStructures(sharedData) {
2425
2533
  if (!(sharedData instanceof Map))
2426
2534
  return sharedData;
2427
2535
  let typed = sharedData.get('typed') || [];
@@ -2459,7 +2567,7 @@
2459
2567
  return named;
2460
2568
  }
2461
2569
  var sourceSymbol = Symbol.for('source');
2462
- function readStruct$1(src, position, srcEnd, unpackr) {
2570
+ function readStruct(src, position, srcEnd, unpackr) {
2463
2571
  let recordId = src[position++] - 0x20;
2464
2572
  if (recordId >= 24) {
2465
2573
  switch(recordId) {
@@ -2730,14 +2838,14 @@
2730
2838
  }
2731
2839
  }
2732
2840
 
2733
- function saveState$1() {
2841
+ function saveState() {
2734
2842
  if (currentSource) {
2735
2843
  currentSource.bytes = Uint8Array.prototype.slice.call(currentSource.bytes, currentSource.position, currentSource.bytesEnd);
2736
2844
  currentSource.position = 0;
2737
2845
  currentSource.bytesEnd = currentSource.bytes.length;
2738
2846
  }
2739
2847
  }
2740
- function prepareStructures$1(structures, packr) {
2848
+ function prepareStructures(structures, packr) {
2741
2849
  if (packr.typedStructs) {
2742
2850
  let structMap = new Map();
2743
2851
  structMap.set('named', structures);
@@ -2754,7 +2862,7 @@
2754
2862
  let typed = existing.get('typed') || [];
2755
2863
  if (typed.length !== lastTypedStructuresLength)
2756
2864
  compatible = false;
2757
- } else if (existing instanceof Array) {
2865
+ } else if (existing instanceof Array || Array.isArray(existing)) {
2758
2866
  if (existing.length !== (packr.lastNamedStructuresLength || 0))
2759
2867
  compatible = false;
2760
2868
  }
@@ -2766,7 +2874,23 @@
2766
2874
  return structures;
2767
2875
  }
2768
2876
 
2769
- setReadStruct(readStruct$1, onLoadedStructures$1, saveState$1);
2877
+ setReadStruct(readStruct, onLoadedStructures, saveState);
2878
+
2879
+ const nativeAccelerationDisabled = process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
2880
+
2881
+ if (!nativeAccelerationDisabled) {
2882
+ let extractor;
2883
+ try {
2884
+ if (typeof require == 'function')
2885
+ extractor = require('msgpackr-extract');
2886
+ else
2887
+ extractor = module.createRequire((document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href))('msgpackr-extract');
2888
+ if (extractor)
2889
+ setExtractor(extractor.extractStrings);
2890
+ } catch (error) {
2891
+ // native module is optional
2892
+ }
2893
+ }
2770
2894
 
2771
2895
  let allSampleData = [];
2772
2896
  for (let i = 1; i < 6; i++) {
@@ -2795,49 +2919,63 @@
2795
2919
  //if (typeof chai === 'undefined') { chai = require('chai') }
2796
2920
  var assert = chai.assert;
2797
2921
  //if (typeof msgpackr === 'undefined') { msgpackr = require('..') }
2798
- var Packr$1 = msgpackr.Packr;
2799
- var Unpackr$1 = msgpackr.Unpackr;
2800
- var unpack = msgpackr.unpack;
2801
- var unpackMultiple = msgpackr.unpackMultiple;
2802
- var roundFloat32 = msgpackr.roundFloat32;
2803
- var pack = msgpackr.pack;
2804
- var DECIMAL_FIT = msgpackr.FLOAT32_OPTIONS.DECIMAL_FIT;
2805
-
2806
- var addExtension = msgpackr.addExtension;
2922
+ var Packr = Packr$1;
2923
+ var Unpackr = Unpackr$1;
2924
+ var unpack = unpack$1;
2925
+ var unpackMultiple = unpackMultiple$1;
2926
+ var roundFloat32 = roundFloat32$1;
2927
+ var pack = pack$1;
2928
+ var DECIMAL_FIT = FLOAT32_OPTIONS.DECIMAL_FIT;
2929
+
2930
+ var addExtension = addExtension$1;
2807
2931
  var zlib = tryRequire('zlib');
2808
- var deflateSync = zlib.deflateSync;
2809
- var inflateSync = zlib.inflateSync;
2810
- var deflateSync = zlib.brotliCompressSync;
2811
- var inflateSync = zlib.brotliDecompressSync;
2812
- var constants = zlib.constants;
2932
+ zlib.deflateSync;
2933
+ zlib.inflateSync;
2934
+ zlib.brotliCompressSync;
2935
+ zlib.brotliDecompressSync;
2936
+ zlib.constants;
2813
2937
 
2814
2938
  var ITERATIONS = 4000;
2815
2939
 
2816
- suite('msgpackr basic tests', function(){
2817
- test('pack/unpack data', function(){
2940
+ class ExtendArray extends Array {
2941
+ }
2942
+
2943
+ class ExtendArray2 extends Array {
2944
+ }
2945
+
2946
+ class ExtendArray3 extends Array {
2947
+ }
2948
+
2949
+
2950
+ class ExtendObject {
2951
+ }
2952
+
2953
+
2954
+ suite('msgpackr basic tests', function() {
2955
+ test('pack/unpack data', function () {
2818
2956
  var data = {
2819
2957
  data: [
2820
- { a: 1, name: 'one', type: 'odd', isOdd: true },
2821
- { a: 2, name: 'two', type: 'even'},
2822
- { a: 3, name: 'three', type: 'odd', isOdd: true },
2823
- { a: 4, name: 'four', type: 'even'},
2824
- { a: 5, name: 'five', type: 'odd', isOdd: true },
2825
- { a: 6, name: 'six', type: 'even', isOdd: null }
2958
+ {a: 1, name: 'one', type: 'odd', isOdd: true},
2959
+ {a: 2, name: 'two', type: 'even'},
2960
+ {a: 3, name: 'three', type: 'odd', isOdd: true},
2961
+ {a: 4, name: 'four', type: 'even'},
2962
+ {a: 5, name: 'five', type: 'odd', isOdd: true},
2963
+ {a: 6, name: 'six', type: 'even', isOdd: null}
2826
2964
  ],
2827
2965
  description: 'some names',
2828
2966
  types: ['odd', 'even'],
2829
2967
  convertEnumToNum: [
2830
- { prop: 'test' },
2831
- { prop: 'test' },
2832
- { prop: 'test' },
2833
- { prop: 1 },
2834
- { prop: 2 },
2835
- { prop: [undefined] },
2836
- { prop: null }
2968
+ {prop: 'test'},
2969
+ {prop: 'test'},
2970
+ {prop: 'test'},
2971
+ {prop: 1},
2972
+ {prop: 2},
2973
+ {prop: [undefined]},
2974
+ {prop: null}
2837
2975
  ]
2838
2976
  };
2839
2977
  let structures = [];
2840
- let packr = new Packr$1({ structures });
2978
+ let packr = new Packr({structures});
2841
2979
  var serialized = packr.pack(data);
2842
2980
  serialized = packr.pack(data);
2843
2981
  serialized = packr.pack(data);
@@ -2845,12 +2983,12 @@
2845
2983
  assert.deepEqual(deserialized, data);
2846
2984
  });
2847
2985
 
2848
- test('mixed structures', function(){
2849
- let data1 = { a: 1, b: 2, c: 3 };
2850
- let data2 = { a: 1, b: 2, d: 4 };
2851
- let data3 = { a: 1, b: 2, e: 5 };
2986
+ test('mixed structures', function () {
2987
+ let data1 = {a: 1, b: 2, c: 3};
2988
+ let data2 = {a: 1, b: 2, d: 4};
2989
+ let data3 = {a: 1, b: 2, e: 5};
2852
2990
  let structures = [];
2853
- let packr = new Packr$1({ structures });
2991
+ let packr = new Packr({structures});
2854
2992
  var serialized = packr.pack(data1);
2855
2993
  var deserialized = packr.unpack(serialized);
2856
2994
  assert.deepEqual(deserialized, data1);
@@ -2862,7 +3000,7 @@
2862
3000
  assert.deepEqual(deserialized, data3);
2863
3001
  });
2864
3002
 
2865
- test('mixed array', function(){
3003
+ test('mixed array', function () {
2866
3004
  var data = [
2867
3005
  'one',
2868
3006
  'two',
@@ -2874,31 +3012,34 @@
2874
3012
  'three',
2875
3013
  'three',
2876
3014
  'one', [
2877
- 3, -5, -50, -400,1.3, -5.3, true
3015
+ 3, -5, -50, -400, 1.3, -5.3, true
2878
3016
  ]
2879
3017
  ];
2880
3018
  let structures = [];
2881
- let packr = new Packr$1({ structures });
3019
+ let packr = new Packr({structures});
2882
3020
  var serialized = packr.pack(data);
2883
3021
  var deserialized = packr.unpack(serialized);
2884
3022
  assert.deepEqual(deserialized, data);
2885
3023
  });
2886
3024
 
2887
- test('255 chars', function() {
3025
+ test('255 chars', function () {
2888
3026
  const data = 'RRZG9A6I7xupPeOZhxcOcioFsuhszGOdyDUcbRf4Zef2kdPIfC9RaLO4jTM5JhuZvTsF09fbRHMGtqk7YAgu3vespeTe9l61ziZ6VrMnYu2CamK96wCkmz0VUXyqaiUoTPgzk414LS9yYrd5uh7w18ksJF5SlC2e91rukWvNqAZJjYN3jpkqHNOFchCwFrhbxq2Lrv1kSJPYCx9blRg2hGmYqTbElLTZHv20iNqwZeQbRMgSBPT6vnbCBPnOh1W';
2889
3027
  var serialized = pack(data);
2890
3028
  var deserialized = unpack(serialized);
2891
3029
  assert.equal(deserialized, data);
2892
3030
  });
2893
- test('pack/unpack varying data with random access structures', function() {
3031
+ test('pack/unpack varying data with random access structures', function () {
2894
3032
  let structures = [];
2895
- let packr = new Packr$1({ structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
3033
+ let packr = new Packr({
3034
+ structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
2896
3035
  }, getStructures() {
2897
3036
  console.log('getStructures');
2898
- } });
3037
+ }
3038
+ });
2899
3039
  for (let i = 0; i < 20; i++) {
2900
3040
  let data = {};
2901
- let props = ['foo', 'bar', 'a', 'b', 'c','name', 'age', 'd'];
3041
+ let props = ['foo', 'bar', 'a', 'b', 'c', 'name', 'age', 'd'];
3042
+
2902
3043
  function makeString() {
2903
3044
  let str = '';
2904
3045
  while (random() < 0.9) {
@@ -2906,6 +3047,7 @@
2906
3047
  }
2907
3048
  return str;
2908
3049
  }
3050
+
2909
3051
  for (let i = 0; i < random() * 20; i++) {
2910
3052
  data[props[Math.floor(random() * 8)]] =
2911
3053
  random() < 0.3 ? Math.floor(random() * 400) / 2 :
@@ -2914,7 +3056,7 @@
2914
3056
  var serialized = packr.pack(data);
2915
3057
  var deserialized = packr.unpack(serialized);
2916
3058
  for (let key in deserialized) {
2917
- let a = deserialized[key];
3059
+ deserialized[key];
2918
3060
  }
2919
3061
  assert.deepEqual(deserialized, data);
2920
3062
  }
@@ -2922,7 +3064,7 @@
2922
3064
 
2923
3065
  for (let sampleData of allSampleData) {
2924
3066
  let snippet = JSON.stringify(sampleData).slice(0, 20) + '...';
2925
- test('pack/unpack sample data ' + snippet, function(){
3067
+ test('pack/unpack sample data ' + snippet, function () {
2926
3068
  var data = sampleData;
2927
3069
  var serialized = pack(data);
2928
3070
  var deserialized = unpack(serialized);
@@ -2931,16 +3073,28 @@
2931
3073
  var deserialized = unpack(serialized);
2932
3074
  assert.deepEqual(deserialized, data);
2933
3075
  });
2934
- test('pack/unpack sample data with random access structures ' + snippet, function() {
3076
+ test('pack/unpack sample data with Uint8Array encoding' + snippet, function () {
3077
+ var data = sampleData;
3078
+ var serialized = pack(data);
3079
+ serialized = new Uint8Array(serialized);
3080
+ var deserialized = unpack(serialized);
3081
+ assert.deepEqual(deserialized, data);
3082
+ var serialized = pack(data);
3083
+ var deserialized = unpack(serialized);
3084
+ assert.deepEqual(deserialized, data);
3085
+ });
3086
+ test('pack/unpack sample data with random access structures ' + snippet, function () {
2935
3087
  var data = sampleData;
2936
3088
  let structures = [];
2937
- let packr = new Packr$1({ structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
2938
- }, getStructures() {
2939
- console.log('getStructures');
2940
- } });
3089
+ let packr = new Packr({
3090
+ structures, useRecords: true, randomAccessStructure: true, freezeData: true, saveStructures(structures) {
3091
+ }, getStructures() {
3092
+ console.log('getStructures');
3093
+ }
3094
+ });
2941
3095
  for (let i = 0; i < 20; i++) {
2942
3096
  var serialized = packr.pack(data);
2943
- var deserialized = packr.unpack(serialized, { lazy: true });
3097
+ var deserialized = packr.unpack(serialized, {lazy: true});
2944
3098
  var copied = {};
2945
3099
  for (let key in deserialized) {
2946
3100
  copied[key] = deserialized[key];
@@ -2948,26 +3102,26 @@
2948
3102
  assert.deepEqual(copied, data);
2949
3103
  }
2950
3104
  });
2951
- test('pack/unpack sample data with bundled strings ' + snippet, function(){
3105
+ test('pack/unpack sample data with bundled strings ' + snippet, function () {
2952
3106
  var data = sampleData;
2953
- let packr = new Packr$1({ /*structures,*/ useRecords: false, bundleStrings: true });
3107
+ let packr = new Packr({ /*structures,*/ useRecords: false, bundleStrings: true});
2954
3108
  var serialized = packr.pack(data);
2955
3109
  var deserialized = packr.unpack(serialized);
2956
3110
  assert.deepEqual(deserialized, data);
2957
3111
  });
2958
3112
  }
2959
3113
 
2960
- test('pack/unpack empty data with bundled strings', function(){
3114
+ test('pack/unpack empty data with bundled strings', function () {
2961
3115
  var data = {};
2962
- let packr = new Packr$1({ bundleStrings: true });
3116
+ let packr = new Packr({bundleStrings: true});
2963
3117
  var serialized = packr.pack(data);
2964
3118
  var deserialized = packr.unpack(serialized);
2965
3119
  assert.deepEqual(deserialized, data);
2966
3120
  });
2967
- test('pack/unpack sequential data', function(){
2968
- var data = { foo:1, bar: 2 };
2969
- let packr = new Packr$1({ sequential: true });
2970
- let unpackr = new Unpackr$1({ sequential: true });
3121
+ test('pack/unpack sequential data', function () {
3122
+ var data = {foo: 1, bar: 2};
3123
+ let packr = new Packr({sequential: true});
3124
+ let unpackr = new Unpackr({sequential: true});
2971
3125
  var serialized = packr.pack(data);
2972
3126
  var deserialized = unpackr.unpack(serialized);
2973
3127
  assert.deepEqual(deserialized, data);
@@ -2975,6 +3129,22 @@
2975
3129
  var deserialized = unpackr.unpack(serialized);
2976
3130
  assert.deepEqual(deserialized, data);
2977
3131
  });
3132
+ test('pack/unpack with bundled strings and sequential', function () {
3133
+ const options = {
3134
+ bundleStrings: true,
3135
+ sequential: true,
3136
+ };
3137
+
3138
+ const packer = new Packr(options);
3139
+ const unpacker = new Packr(options);
3140
+
3141
+ const data = {data: 42}; // key length >= 4
3142
+
3143
+ unpacker.unpackMultiple(Buffer.concat([
3144
+ packer.pack(data),
3145
+ packer.pack(data)
3146
+ ]));
3147
+ });
2978
3148
  if (typeof Buffer != 'undefined')
2979
3149
  test('replace data', function(){
2980
3150
  var data1 = {
@@ -3028,7 +3198,7 @@
3028
3198
  prop2: 'more string',
3029
3199
  num: 3,
3030
3200
  };
3031
- let packr = new Packr$1();
3201
+ let packr = new Packr();
3032
3202
  addExtension({
3033
3203
  Class: Extended,
3034
3204
  type: 11,
@@ -3048,6 +3218,73 @@
3048
3218
  assert.deepEqual(data, deserialized);
3049
3219
  assert.equal(deserialized.extendedInstance.getDouble(), 8);
3050
3220
  });
3221
+
3222
+ test('extended Array class read/write', function(){
3223
+ var instance = new ExtendArray();
3224
+ instance.push(0);
3225
+ instance.push(1);
3226
+ instance.push(2);
3227
+ var data = {
3228
+ prop1: 'has multi-byte: ᾜ',
3229
+ extendedInstance: instance,
3230
+ prop2: 'more string',
3231
+ num: 3,
3232
+ };
3233
+ new Packr();
3234
+ addExtension({
3235
+ Class: ExtendArray,
3236
+ type: 12,
3237
+ read: function(data) {
3238
+ Object.setPrototypeOf(data, ExtendArray.prototype);
3239
+ return data
3240
+ },
3241
+ write: function(instance) {
3242
+ return [...instance]
3243
+ }
3244
+ });
3245
+ var serialized = pack(data);
3246
+ var deserialized = unpack(serialized);
3247
+ assert.strictEqual(Object.getPrototypeOf(deserialized.extendedInstance), ExtendArray.prototype);
3248
+ assert.deepEqual(data, deserialized);
3249
+ });
3250
+
3251
+
3252
+ test('unregistered extended Array class read/write', function(){
3253
+ var instance = new ExtendArray2();
3254
+ instance.push(0);
3255
+ instance.push(1);
3256
+ instance.push(2);
3257
+ var data = {
3258
+ prop1: 'has multi-byte: ᾜ',
3259
+ extendedInstance: instance,
3260
+ prop2: 'more string',
3261
+ num: 3,
3262
+ };
3263
+ new Packr();
3264
+ var serialized = pack(data);
3265
+ var deserialized = unpack(serialized);
3266
+ assert.strictEqual(Object.getPrototypeOf(deserialized.extendedInstance), Array.prototype);
3267
+ assert.deepEqual(data, deserialized);
3268
+ });
3269
+
3270
+
3271
+ test('unregistered extended Object class read/write', function(){
3272
+ var instance = new ExtendObject();
3273
+ instance.test1 = "string";
3274
+ instance.test2 = 3421321;
3275
+ var data = {
3276
+ prop1: 'has multi-byte: ᾜ',
3277
+ extendedInstance: instance,
3278
+ prop2: 'more string',
3279
+ num: 3,
3280
+ };
3281
+ new Packr();
3282
+ var serialized = pack(data);
3283
+ var deserialized = unpack(serialized);
3284
+ assert.strictEqual(Object.getPrototypeOf(deserialized.extendedInstance), Object.prototype);
3285
+ assert.deepEqual(data, deserialized);
3286
+ });
3287
+
3051
3288
  test('extended class pack/unpack custom size', function(){
3052
3289
  function TestClass() {
3053
3290
 
@@ -3082,7 +3319,7 @@
3082
3319
  prop2: 'more string',
3083
3320
  num: 3,
3084
3321
  };
3085
- let packr = new Packr$1();
3322
+ new Packr();
3086
3323
  addExtension({
3087
3324
  Class: Extended,
3088
3325
  type: 12,
@@ -3101,12 +3338,75 @@
3101
3338
  assert.deepEqual(data, deserialized);
3102
3339
  assert.equal(deserialized.extendedInstance.getDouble(), 8);
3103
3340
  });
3341
+ test('extended class return self', function(){
3342
+ function Extended() {
3343
+
3344
+ }
3345
+ Extended.prototype.getDouble = function() {
3346
+ return this.value * 2
3347
+ };
3348
+ var instance = new Extended();
3349
+ instance.value = 4;
3350
+ instance.string = 'decode this: ᾜ';
3351
+ var data = {
3352
+ prop1: 'has multi-byte: ᾜ',
3353
+ extendedInstance: instance,
3354
+ prop2: 'more string',
3355
+ num: 3,
3356
+ };
3357
+ new Packr();
3358
+ addExtension({
3359
+ Class: Extended,
3360
+ type: 13,
3361
+ read: function(data) {
3362
+ Object.setPrototypeOf(data, Extended.prototype);
3363
+ return data
3364
+ },
3365
+ write: function(data) {
3366
+ return data
3367
+ }
3368
+ });
3369
+ var serialized = pack(data);
3370
+ var deserialized = unpack(serialized);
3371
+ assert.deepEqual(data, deserialized);
3372
+ assert.strictEqual(Object.getPrototypeOf(deserialized.extendedInstance), Extended.prototype);
3373
+ assert.equal(deserialized.extendedInstance.getDouble(), 8);
3374
+ });
3375
+ test('extended Array class return self', function(){
3376
+ var instance = new ExtendArray3();
3377
+ instance.push(0);
3378
+ instance.push('has multi-byte: ᾜ');
3379
+ var data = {
3380
+ prop1: 'has multi-byte: ᾜ',
3381
+ extendedInstance: instance,
3382
+ prop2: 'more string',
3383
+ num: 3,
3384
+ };
3385
+ new Packr();
3386
+ addExtension({
3387
+ Class: ExtendArray3,
3388
+ type: 14,
3389
+ read: function(data) {
3390
+ Object.setPrototypeOf(data, ExtendArray3.prototype);
3391
+ return data
3392
+ },
3393
+ write: function(data) {
3394
+ return data
3395
+ }
3396
+ });
3397
+ var serialized = pack(data);
3398
+ var deserialized = unpack(serialized);
3399
+ assert.deepEqual(data, deserialized);
3400
+ assert.strictEqual(Object.getPrototypeOf(deserialized.extendedInstance), ExtendArray3.prototype);
3401
+ assert.equal(deserialized.extendedInstance[0], 0);
3402
+ });
3403
+
3104
3404
  test('proto handling', function() {
3105
3405
  var objectWithProto = JSON.parse('{"__proto__":{"foo":3}}');
3106
3406
  var decoded = unpack(pack(objectWithProto));
3107
3407
  assert(!decoded.foo);
3108
3408
  var objectsWithProto = [objectWithProto, objectWithProto, objectWithProto, objectWithProto, objectWithProto, objectWithProto];
3109
- let packr = new Packr$1();
3409
+ let packr = new Packr();
3110
3410
  var decoded = packr.unpack(packr.pack(objectsWithProto));
3111
3411
  for (let object of decoded) {
3112
3412
  assert(!decoded.foo);
@@ -3138,7 +3438,7 @@
3138
3438
  object.children[1] = object;
3139
3439
  object.children[2] = object.children[0];
3140
3440
  object.childrenAgain = object.children;
3141
- let packr = new Packr$1({
3441
+ let packr = new Packr({
3142
3442
  moreTypes: true,
3143
3443
  structuredClone: true,
3144
3444
  });
@@ -3163,7 +3463,7 @@
3163
3463
  float32Array: fa,
3164
3464
  uint16Array: new Uint16Array([3,4])
3165
3465
  };
3166
- let packr = new Packr$1({
3466
+ let packr = new Packr({
3167
3467
  moreTypes: true,
3168
3468
  structuredClone: true,
3169
3469
  });
@@ -3181,8 +3481,8 @@
3181
3481
  });
3182
3482
  test('big bundledStrings', function() {
3183
3483
  const MSGPACK_OPTIONS = {bundleStrings: true};
3184
- const packer = new Packr$1(MSGPACK_OPTIONS);
3185
- const unpacker = new Unpackr$1(MSGPACK_OPTIONS);
3484
+ const packer = new Packr(MSGPACK_OPTIONS);
3485
+ const unpacker = new Unpackr(MSGPACK_OPTIONS);
3186
3486
 
3187
3487
  const payload = {
3188
3488
  output: [
@@ -3199,7 +3499,7 @@
3199
3499
  assert.equal(deserialized.output[0].url, payload.output[0].url);
3200
3500
  });
3201
3501
  test('structured clone with bundled strings', function() {
3202
- const packer = new Packr$1({
3502
+ const packer = new Packr({
3203
3503
  structuredClone: true, // both options must be enabled
3204
3504
  bundleStrings: true,
3205
3505
  });
@@ -3230,10 +3530,10 @@
3230
3530
  });
3231
3531
 
3232
3532
  test('separate instances', function() {
3233
- const packr = new Packr$1({
3533
+ const packr = new Packr({
3234
3534
  structures: [['m', 'e'], ['action', 'share']]
3235
3535
  });
3236
- const packr2 = new Packr$1({
3536
+ const packr2 = new Packr({
3237
3537
  structures: [['m', 'e'], ['action', 'share']]
3238
3538
  });
3239
3539
  let packed = packr.pack([{m: 1, e: 2}, {action: 3, share: 4}]);
@@ -3248,7 +3548,7 @@
3248
3548
  }
3249
3549
  let structures = [];
3250
3550
  let savedStructures;
3251
- let packr = new Packr$1({
3551
+ let packr = new Packr({
3252
3552
  structures,
3253
3553
  saveStructures(structures) {
3254
3554
  savedStructures = structures;
@@ -3259,7 +3559,7 @@
3259
3559
  var deserialized = packr.unpack(serializedWith32);
3260
3560
  assert.deepEqual(deserialized, data);
3261
3561
  structures = structures.slice(0, 32);
3262
- packr = new Packr$1({
3562
+ packr = new Packr({
3263
3563
  structures,
3264
3564
  maxSharedStructures: 100,
3265
3565
  saveStructures(structures) {
@@ -3269,7 +3569,7 @@
3269
3569
  deserialized = packr.unpack(serializedWith32);
3270
3570
  assert.deepEqual(deserialized, data);
3271
3571
  structures = structures.slice(0, 32);
3272
- packr = new Packr$1({
3572
+ packr = new Packr({
3273
3573
  structures,
3274
3574
  maxSharedStructures: 100,
3275
3575
  saveStructures(structures) {
@@ -3295,7 +3595,7 @@
3295
3595
  structures.push(['a' + i]);
3296
3596
  }
3297
3597
  const structures2 = [...structures];
3298
- const packr = new Packr$1({
3598
+ const packr = new Packr({
3299
3599
  getStructures() {
3300
3600
  return structures
3301
3601
  },
@@ -3303,7 +3603,7 @@
3303
3603
  },
3304
3604
  maxSharedStructures: 100
3305
3605
  });
3306
- const packr2 = new Packr$1({
3606
+ const packr2 = new Packr({
3307
3607
  getStructures() {
3308
3608
  return structures2
3309
3609
  },
@@ -3353,7 +3653,7 @@
3353
3653
  ancient: new Date(-3532219539133),
3354
3654
  invalidDate: new Date('invalid')
3355
3655
  };
3356
- let packr = new Packr$1();
3656
+ let packr = new Packr();
3357
3657
  var serialized = packr.pack(data);
3358
3658
  var deserialized = packr.unpack(serialized);
3359
3659
  assert.equal(deserialized.map.get(4), 'four');
@@ -3375,7 +3675,7 @@
3375
3675
  date: new Date(1532219539011),
3376
3676
  invalidDate: new Date('invalid')
3377
3677
  };
3378
- let packr = new Packr$1({
3678
+ let packr = new Packr({
3379
3679
  mapsAsObjects: true,
3380
3680
  useTimestamp32: true,
3381
3681
  onInvalidDate: () => 'Custom invalid date'
@@ -3431,7 +3731,7 @@
3431
3731
  c: 0.00000000000352501,
3432
3732
  d: 3252.77,
3433
3733
  };
3434
- let packr = new Packr$1({
3734
+ let packr = new Packr({
3435
3735
  useFloat32: DECIMAL_FIT
3436
3736
  });
3437
3737
  var serialized = packr.pack(data);
@@ -3439,17 +3739,50 @@
3439
3739
  var deserialized = packr.unpack(serialized);
3440
3740
  assert.deepEqual(deserialized, data);
3441
3741
  });
3742
+ test('int64/uint64 should be bigints by default', function() {
3743
+ var data = {
3744
+ a: 325283295382932843n
3745
+ };
3746
+
3747
+ let packr = new Packr();
3748
+ var serialized = packr.pack(data);
3749
+ var deserialized = packr.unpack(serialized);
3750
+ assert.deepEqual(deserialized.a, 325283295382932843n);
3751
+ });
3442
3752
  test('bigint to float', function() {
3443
3753
  var data = {
3444
3754
  a: 325283295382932843n
3445
3755
  };
3446
- let packr = new Packr$1({
3756
+ let packr = new Packr({
3757
+ int64AsType: 'number'
3758
+ });
3759
+ var serialized = packr.pack(data);
3760
+ var deserialized = packr.unpack(serialized);
3761
+ assert.deepEqual(deserialized.a, 325283295382932843);
3762
+ });
3763
+ test('int64AsNumber compatibility', function() {
3764
+ // https://github.com/kriszyp/msgpackr/pull/85
3765
+ var data = {
3766
+ a: 325283295382932843n
3767
+ };
3768
+ let packr = new Packr({
3447
3769
  int64AsNumber: true
3448
3770
  });
3449
3771
  var serialized = packr.pack(data);
3450
3772
  var deserialized = packr.unpack(serialized);
3451
3773
  assert.deepEqual(deserialized.a, 325283295382932843);
3452
3774
  });
3775
+ test('bigint to string', function() {
3776
+ var data = {
3777
+ a: 325283295382932843n,
3778
+ };
3779
+ let packr = new Packr({
3780
+ int64AsType: 'string'
3781
+ });
3782
+ var serialized = packr.pack(data);
3783
+ var deserialized = packr.unpack(serialized);
3784
+ assert.deepEqual(deserialized.a, '325283295382932843');
3785
+ });
3453
3786
  test('numbers', function(){
3454
3787
  var data = {
3455
3788
  bigEncodable: 48978578104322,
@@ -3482,7 +3815,7 @@
3482
3815
  tooBig: 2n**66n
3483
3816
  };
3484
3817
  assert.throws(function(){ serialized = pack(tooBigInt); });
3485
- let packr = new Packr$1({
3818
+ let packr = new Packr({
3486
3819
  largeBigIntToFloat: true
3487
3820
  });
3488
3821
  serialized = packr.pack(tooBigInt);
@@ -3549,14 +3882,14 @@
3549
3882
  var serialized = JSON.stringify(data);
3550
3883
  console.log('JSON size', serialized.length);
3551
3884
  for (var i = 0; i < ITERATIONS; i++) {
3552
- var deserialized = JSON.parse(serialized);
3885
+ JSON.parse(serialized);
3553
3886
  }
3554
3887
  });
3555
3888
  test('performance JSON.stringify', function() {
3556
3889
  var data = sampleData;
3557
3890
  this.timeout(10000);
3558
3891
  for (var i = 0; i < ITERATIONS; i++) {
3559
- var serialized = JSON.stringify(data);
3892
+ JSON.stringify(data);
3560
3893
  }
3561
3894
  });
3562
3895
  test('performance unpack', function() {
@@ -3565,29 +3898,29 @@
3565
3898
  let structures = [];
3566
3899
  var serialized = pack(data);
3567
3900
  console.log('MessagePack size', serialized.length);
3568
- let packr = new Packr$1({ structures, bundleStrings: false });
3901
+ let packr = new Packr({ structures, bundleStrings: false });
3569
3902
  var serialized = packr.pack(data);
3570
3903
  console.log('msgpackr w/ record ext size', serialized.length);
3571
3904
  for (var i = 0; i < ITERATIONS; i++) {
3572
- var deserialized = packr.unpack(serialized);
3905
+ packr.unpack(serialized);
3573
3906
  }
3574
3907
  });
3575
3908
  test('performance pack', function() {
3576
3909
  var data = sampleData;
3577
3910
  this.timeout(10000);
3578
3911
  let structures = [];
3579
- let packr = new Packr$1({ structures, bundleStrings: false });
3912
+ let packr = new Packr({ structures, bundleStrings: false });
3580
3913
  let buffer = typeof Buffer != 'undefined' ? Buffer.alloc(0x10000) : new Uint8Array(0x10000);
3581
3914
 
3582
3915
  for (var i = 0; i < ITERATIONS; i++) {
3583
3916
  //serialized = pack(data, { shared: sharedStructure })
3584
3917
  packr.useBuffer(buffer);
3585
- var serialized = packr.pack(data);
3918
+ packr.pack(data);
3586
3919
  //var serializedGzip = deflateSync(serialized)
3587
3920
  }
3588
3921
  //console.log('serialized', serialized.length, global.propertyComparisons)
3589
3922
  });
3590
3923
  });
3591
3924
 
3592
- }(msgpackr, chai, inspector, fs));
3925
+ })(chai, null, module, fs);
3593
3926
  //# sourceMappingURL=test.js.map