msgpackr 1.11.12 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/node.cjs CHANGED
@@ -31,7 +31,6 @@ const C1 = new C1Type();
31
31
  C1.name = 'MessagePack 0xC1';
32
32
  var sequentialMode = false;
33
33
  var inlineObjectReadThreshold = 2;
34
- var readStruct$1, onLoadedStructures$1, onSaveState;
35
34
 
36
35
  class Unpackr {
37
36
  constructor(options) {
@@ -61,10 +60,10 @@ class Unpackr {
61
60
  unpack(source, options) {
62
61
  if (src) {
63
62
  // re-entrant execution, save the state and restore it after we do this unpack
64
- return saveState$1(() => {
63
+ return saveState(() => {
65
64
  clearSource();
66
- return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
67
- })
65
+ return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options);
66
+ });
68
67
  }
69
68
  if (!source.buffer && source.constructor === ArrayBuffer)
70
69
  source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
@@ -90,14 +89,14 @@ class Unpackr {
90
89
  // if it doesn't have a buffer, maybe it is the wrong type of object
91
90
  src = null;
92
91
  if (source instanceof Uint8Array)
93
- throw error
94
- throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
92
+ throw error;
93
+ throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source));
95
94
  }
96
95
  if (this instanceof Unpackr) {
97
96
  currentUnpackr = this;
98
97
  if (this.structures) {
99
98
  currentStructures = this.structures;
100
- return checkedRead(options)
99
+ return checkedRead(options);
101
100
  } else if (!currentStructures || currentStructures.length > 0) {
102
101
  currentStructures = [];
103
102
  }
@@ -106,7 +105,7 @@ class Unpackr {
106
105
  if (!currentStructures || currentStructures.length > 0)
107
106
  currentStructures = [];
108
107
  }
109
- return checkedRead(options)
108
+ return checkedRead(options);
110
109
  }
111
110
  unpackMultiple(source, forEach) {
112
111
  let values, lastPosition = 0;
@@ -119,7 +118,7 @@ class Unpackr {
119
118
  while(position$1 < size) {
120
119
  lastPosition = position$1;
121
120
  if (forEach(checkedRead(), lastPosition, position$1) === false) {
122
- return
121
+ return;
123
122
  }
124
123
  }
125
124
  }
@@ -129,20 +128,20 @@ class Unpackr {
129
128
  lastPosition = position$1;
130
129
  values.push(checkedRead());
131
130
  }
132
- return values
131
+ return values;
133
132
  }
134
133
  } catch(error) {
135
134
  error.lastPosition = lastPosition;
136
135
  error.values = values;
137
- throw error
136
+ throw error;
138
137
  } finally {
139
138
  sequentialMode = false;
140
139
  clearSource();
141
140
  }
142
141
  }
143
142
  _mergeStructures(loadedStructures, existingStructures) {
144
- if (onLoadedStructures$1)
145
- loadedStructures = onLoadedStructures$1.call(this, loadedStructures);
143
+ if (this._onLoadedStructures)
144
+ loadedStructures = this._onLoadedStructures(loadedStructures);
146
145
  loadedStructures = loadedStructures || [];
147
146
  if (Object.isFrozen(loadedStructures))
148
147
  loadedStructures = loadedStructures.map(structure => structure.slice(0));
@@ -166,10 +165,10 @@ class Unpackr {
166
165
  }
167
166
  }
168
167
  }
169
- return this.structures = loadedStructures
168
+ return this.structures = loadedStructures;
170
169
  }
171
170
  decode(source, options) {
172
- return this.unpack(source, options)
171
+ return this.unpack(source, options);
173
172
  }
174
173
  }
175
174
  function checkedRead(options) {
@@ -180,8 +179,8 @@ function checkedRead(options) {
180
179
  currentStructures.length = sharedLength;
181
180
  }
182
181
  let result;
183
- if (currentUnpackr.randomAccessStructure && src[position$1] < 0x40 && src[position$1] >= 0x20 && readStruct$1) {
184
- result = readStruct$1(src, position$1, srcEnd, currentUnpackr);
182
+ if (currentUnpackr._readStruct && src[position$1] < 0x40 && src[position$1] >= 0x20) {
183
+ result = currentUnpackr._readStruct(src, position$1, srcEnd);
185
184
  src = null; // dispose of this so that recursive unpack calls don't save state
186
185
  if (!(options && options.lazy) && result)
187
186
  result = result.toJSON();
@@ -207,7 +206,7 @@ function checkedRead(options) {
207
206
  referenceMap = null;
208
207
  } else if (position$1 > srcEnd) {
209
208
  // over read
210
- throw new Error('Unexpected end of MessagePack data')
209
+ throw new Error('Unexpected end of MessagePack data');
211
210
  } else if (!sequentialMode) {
212
211
  let jsonView;
213
212
  try {
@@ -215,10 +214,10 @@ function checkedRead(options) {
215
214
  } catch(error) {
216
215
  jsonView = '(JSON view not available ' + error + ')';
217
216
  }
218
- throw new Error('Data read, but end of buffer not reached ' + jsonView)
217
+ throw new Error('Data read, but end of buffer not reached ' + jsonView);
219
218
  }
220
219
  // else more to read, but we are reading sequentially, so don't clear source yet
221
- return result
220
+ return result;
222
221
  } catch(error) {
223
222
  if (currentStructures && currentStructures.restoreStructures)
224
223
  restoreStructures();
@@ -226,7 +225,7 @@ function checkedRead(options) {
226
225
  if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position$1 > srcEnd) {
227
226
  error.incomplete = true;
228
227
  }
229
- throw error
228
+ throw error;
230
229
  }
231
230
  }
232
231
 
@@ -242,7 +241,7 @@ function read() {
242
241
  if (token < 0xa0) {
243
242
  if (token < 0x80) {
244
243
  if (token < 0x40)
245
- return token
244
+ return token;
246
245
  else {
247
246
  let structure = currentStructures[token & 0x3f] ||
248
247
  currentUnpackr.getStructures && loadStructures()[token & 0x3f];
@@ -250,9 +249,9 @@ function read() {
250
249
  if (!structure.read) {
251
250
  structure.read = createStructureReader(structure, token & 0x3f);
252
251
  }
253
- return structure.read()
252
+ return structure.read();
254
253
  } else
255
- return token
254
+ return token;
256
255
  }
257
256
  } else if (token < 0x90) {
258
257
  // map
@@ -265,13 +264,13 @@ function read() {
265
264
  key = '__proto_';
266
265
  object[key] = read();
267
266
  }
268
- return object
267
+ return object;
269
268
  } else {
270
269
  let map = new Map();
271
270
  for (let i = 0; i < token; i++) {
272
271
  map.set(read(), read());
273
272
  }
274
- return map
273
+ return map;
275
274
  }
276
275
  } else {
277
276
  token -= 0x90;
@@ -280,91 +279,91 @@ function read() {
280
279
  array[i] = read();
281
280
  }
282
281
  if (currentUnpackr.freezeData)
283
- return Object.freeze(array)
284
- return array
282
+ return Object.freeze(array);
283
+ return array;
285
284
  }
286
285
  } else if (token < 0xc0) {
287
286
  // fixstr
288
287
  let length = token - 0xa0;
289
288
  if (srcStringEnd >= position$1) {
290
- return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
289
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart);
291
290
  }
292
291
  if (srcStringEnd == 0 && srcEnd < 140) {
293
292
  // for small blocks, avoiding the overhead of the extract call is helpful
294
293
  let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
295
294
  if (string != null)
296
- return string
295
+ return string;
297
296
  }
298
- return readFixedString(length)
297
+ return readFixedString(length);
299
298
  } else {
300
299
  let value;
301
300
  switch (token) {
302
- case 0xc0: return null
301
+ case 0xc0: return null;
303
302
  case 0xc1:
304
303
  if (bundledStrings$1) {
305
304
  value = read(); // followed by the length of the string in characters (not bytes!)
306
305
  if (value > 0)
307
- return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value)
306
+ return bundledStrings$1[1].slice(bundledStrings$1.position1, bundledStrings$1.position1 += value);
308
307
  else
309
- return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value)
308
+ return bundledStrings$1[0].slice(bundledStrings$1.position0, bundledStrings$1.position0 -= value);
310
309
  }
311
310
  return C1; // "never-used", return special object to denote that
312
- case 0xc2: return false
313
- case 0xc3: return true
311
+ case 0xc2: return false;
312
+ case 0xc3: return true;
314
313
  case 0xc4:
315
314
  // bin 8
316
315
  value = src[position$1++];
317
316
  if (value === undefined)
318
- throw new Error('Unexpected end of buffer')
319
- return readBin(value)
317
+ throw new Error('Unexpected end of buffer');
318
+ return readBin(value);
320
319
  case 0xc5:
321
320
  // bin 16
322
321
  value = dataView.getUint16(position$1);
323
322
  position$1 += 2;
324
- return readBin(value)
323
+ return readBin(value);
325
324
  case 0xc6:
326
325
  // bin 32
327
326
  value = dataView.getUint32(position$1);
328
327
  position$1 += 4;
329
- return readBin(value)
328
+ return readBin(value);
330
329
  case 0xc7:
331
330
  // ext 8
332
- return readExt(src[position$1++])
331
+ return readExt(src[position$1++]);
333
332
  case 0xc8:
334
333
  // ext 16
335
334
  value = dataView.getUint16(position$1);
336
335
  position$1 += 2;
337
- return readExt(value)
336
+ return readExt(value);
338
337
  case 0xc9:
339
338
  // ext 32
340
339
  value = dataView.getUint32(position$1);
341
340
  position$1 += 4;
342
- return readExt(value)
341
+ return readExt(value);
343
342
  case 0xca:
344
343
  value = dataView.getFloat32(position$1);
345
344
  if (currentUnpackr.useFloat32 > 2) {
346
345
  // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
347
346
  let multiplier = mult10[((src[position$1] & 0x7f) << 1) | (src[position$1 + 1] >> 7)];
348
347
  position$1 += 4;
349
- return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
348
+ return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
350
349
  }
351
350
  position$1 += 4;
352
- return value
351
+ return value;
353
352
  case 0xcb:
354
353
  value = dataView.getFloat64(position$1);
355
354
  position$1 += 8;
356
- return value
355
+ return value;
357
356
  // uint handlers
358
357
  case 0xcc:
359
- return src[position$1++]
358
+ return src[position$1++];
360
359
  case 0xcd:
361
360
  value = dataView.getUint16(position$1);
362
361
  position$1 += 2;
363
- return value
362
+ return value;
364
363
  case 0xce:
365
364
  value = dataView.getUint32(position$1);
366
365
  position$1 += 4;
367
- return value
366
+ return value;
368
367
  case 0xcf:
369
368
  if (currentUnpackr.int64AsType === 'number') {
370
369
  value = dataView.getUint32(position$1) * 0x100000000;
@@ -377,19 +376,19 @@ function read() {
377
376
  } else
378
377
  value = dataView.getBigUint64(position$1);
379
378
  position$1 += 8;
380
- return value
379
+ return value;
381
380
 
382
381
  // int handlers
383
382
  case 0xd0:
384
- return dataView.getInt8(position$1++)
383
+ return dataView.getInt8(position$1++);
385
384
  case 0xd1:
386
385
  value = dataView.getInt16(position$1);
387
386
  position$1 += 2;
388
- return value
387
+ return value;
389
388
  case 0xd2:
390
389
  value = dataView.getInt32(position$1);
391
390
  position$1 += 4;
392
- return value
391
+ return value;
393
392
  case 0xd3:
394
393
  if (currentUnpackr.int64AsType === 'number') {
395
394
  value = dataView.getInt32(position$1) * 0x100000000;
@@ -402,96 +401,96 @@ function read() {
402
401
  } else
403
402
  value = dataView.getBigInt64(position$1);
404
403
  position$1 += 8;
405
- return value
404
+ return value;
406
405
 
407
406
  case 0xd4:
408
407
  // fixext 1
409
408
  value = src[position$1++];
410
409
  if (value == 0x72) {
411
- return recordDefinition(src[position$1++] & 0x3f)
410
+ return recordDefinition(src[position$1++] & 0x3f);
412
411
  } else {
413
412
  let extension = currentExtensions[value];
414
413
  if (extension) {
415
414
  if (extension.read) {
416
415
  position$1++; // skip filler byte
417
- return extension.read(read())
416
+ return extension.read(read());
418
417
  } else if (extension.noBuffer) {
419
418
  position$1++; // skip filler byte
420
- return extension()
419
+ return extension();
421
420
  } else
422
- return extension(src.subarray(position$1, ++position$1))
421
+ return extension(src.subarray(position$1, ++position$1));
423
422
  } else
424
- throw new Error('Unknown extension ' + value)
423
+ throw new Error('Unknown extension ' + value);
425
424
  }
426
425
  case 0xd5:
427
426
  // fixext 2
428
427
  value = src[position$1];
429
428
  if (value == 0x72) {
430
429
  position$1++;
431
- return recordDefinition(src[position$1++] & 0x3f, src[position$1++])
430
+ return recordDefinition(src[position$1++] & 0x3f, src[position$1++]);
432
431
  } else
433
- return readExt(2)
432
+ return readExt(2);
434
433
  case 0xd6:
435
434
  // fixext 4
436
- return readExt(4)
435
+ return readExt(4);
437
436
  case 0xd7:
438
437
  // fixext 8
439
- return readExt(8)
438
+ return readExt(8);
440
439
  case 0xd8:
441
440
  // fixext 16
442
- return readExt(16)
441
+ return readExt(16);
443
442
  case 0xd9:
444
443
  // str 8
445
444
  value = src[position$1++];
446
445
  if (srcStringEnd >= position$1) {
447
- return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
446
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
448
447
  }
449
- return readString8(value)
448
+ return readString8(value);
450
449
  case 0xda:
451
450
  // str 16
452
451
  value = dataView.getUint16(position$1);
453
452
  position$1 += 2;
454
453
  if (srcStringEnd >= position$1) {
455
- return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
454
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
456
455
  }
457
- return readString16(value)
456
+ return readString16(value);
458
457
  case 0xdb:
459
458
  // str 32
460
459
  value = dataView.getUint32(position$1);
461
460
  position$1 += 4;
462
461
  if (srcStringEnd >= position$1) {
463
- return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart)
462
+ return srcString.slice(position$1 - srcStringStart, (position$1 += value) - srcStringStart);
464
463
  }
465
- return readString32(value)
464
+ return readString32(value);
466
465
  case 0xdc:
467
466
  // array 16
468
467
  value = dataView.getUint16(position$1);
469
468
  position$1 += 2;
470
- return readArray(value)
469
+ return readArray(value);
471
470
  case 0xdd:
472
471
  // array 32
473
472
  value = dataView.getUint32(position$1);
474
473
  position$1 += 4;
475
- return readArray(value)
474
+ return readArray(value);
476
475
  case 0xde:
477
476
  // map 16
478
477
  value = dataView.getUint16(position$1);
479
478
  position$1 += 2;
480
- return readMap(value)
479
+ return readMap(value);
481
480
  case 0xdf:
482
481
  // map 32
483
482
  value = dataView.getUint32(position$1);
484
483
  position$1 += 4;
485
- return readMap(value)
484
+ return readMap(value);
486
485
  default: // negative int
487
486
  if (token >= 0xe0)
488
- return token - 0x100
487
+ return token - 0x100;
489
488
  if (token === undefined) {
490
489
  let error = new Error('Unexpected end of MessagePack data');
491
490
  error.incomplete = true;
492
- throw error
491
+ throw error;
493
492
  }
494
- throw new Error('Unknown MessagePack token ' + token)
493
+ throw new Error('Unknown MessagePack token ' + token);
495
494
 
496
495
  }
497
496
  }
@@ -512,7 +511,7 @@ function createStructureReader(structure, firstId) {
512
511
  }
513
512
  if (structure.highByte === 0)
514
513
  structure.read = createSecondByteReader(firstId, structure.read);
515
- return optimizedReadObject() // second byte is already read, if there is one so immediately read object
514
+ return optimizedReadObject(); // second byte is already read, if there is one so immediately read object
516
515
  }
517
516
  let object = {};
518
517
  for (let i = 0, l = structure.length; i < l; i++) {
@@ -523,38 +522,38 @@ function createStructureReader(structure, firstId) {
523
522
  }
524
523
  if (currentUnpackr.freezeData)
525
524
  return Object.freeze(object);
526
- return object
525
+ return object;
527
526
  }
528
527
  readObject.count = 0;
529
528
  if (structure.highByte === 0) {
530
- return createSecondByteReader(firstId, readObject)
529
+ return createSecondByteReader(firstId, readObject);
531
530
  }
532
- return readObject
531
+ return readObject;
533
532
  }
534
533
 
535
534
  const createSecondByteReader = (firstId, read0) => {
536
535
  return function() {
537
536
  let highByte = src[position$1++];
538
537
  if (highByte === 0)
539
- return read0()
538
+ return read0();
540
539
  let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
541
540
  let structure = currentStructures[id] || loadStructures()[id];
542
541
  if (!structure) {
543
- throw new Error('Record id is not defined for ' + id)
542
+ throw new Error('Record id is not defined for ' + id);
544
543
  }
545
544
  if (!structure.read)
546
545
  structure.read = createStructureReader(structure, firstId);
547
- return structure.read()
548
- }
546
+ return structure.read();
547
+ };
549
548
  };
550
549
 
551
550
  function loadStructures() {
552
- let loadedStructures = saveState$1(() => {
551
+ let loadedStructures = saveState(() => {
553
552
  // save the state in case getStructures modifies our buffer
554
553
  src = null;
555
- return currentUnpackr.getStructures()
554
+ return currentUnpackr.getStructures();
556
555
  });
557
- return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
556
+ return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures);
558
557
  }
559
558
 
560
559
  var readFixedString = readStringJS;
@@ -574,7 +573,7 @@ function setExtractor(extractStrings) {
574
573
  let string = strings[stringPosition++];
575
574
  if (string == null) {
576
575
  if (bundledStrings$1)
577
- return readStringJS(length)
576
+ return readStringJS(length);
578
577
  let byteOffset = src.byteOffset;
579
578
  let extraction = extractStrings(position$1 - headerLength + byteOffset, srcEnd + byteOffset, src.buffer);
580
579
  if (typeof extraction == 'string') {
@@ -586,30 +585,30 @@ function setExtractor(extractStrings) {
586
585
  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
587
586
  string = strings[0];
588
587
  if (string === undefined)
589
- throw new Error('Unexpected end of buffer')
588
+ throw new Error('Unexpected end of buffer');
590
589
  }
591
590
  }
592
591
  let srcStringLength = string.length;
593
592
  if (srcStringLength <= length) {
594
593
  position$1 += length;
595
- return string
594
+ return string;
596
595
  }
597
596
  srcString = string;
598
597
  srcStringStart = position$1;
599
598
  srcStringEnd = position$1 + srcStringLength;
600
599
  position$1 += length;
601
- return string.slice(0, length) // we know we just want the beginning
602
- }
600
+ return string.slice(0, length); // we know we just want the beginning
601
+ };
603
602
  }
604
603
  }
605
604
  function readStringJS(length) {
606
605
  let result;
607
606
  if (length < 16) {
608
607
  if (result = shortStringInJS(length))
609
- return result
608
+ return result;
610
609
  }
611
610
  if (length > 64 && decoder)
612
- return decoder.decode(src.subarray(position$1, position$1 += length))
611
+ return decoder.decode(src.subarray(position$1, position$1 += length));
613
612
  const end = position$1 + length;
614
613
  const units = [];
615
614
  result = '';
@@ -672,17 +671,7 @@ function readStringJS(length) {
672
671
  result += fromCharCode.apply(String, units);
673
672
  }
674
673
 
675
- return result
676
- }
677
- function readString(source, start, length) {
678
- let existingSrc = src;
679
- src = source;
680
- position$1 = start;
681
- try {
682
- return readStringJS(length);
683
- } finally {
684
- src = existingSrc;
685
- }
674
+ return result;
686
675
  }
687
676
 
688
677
  function readArray(length) {
@@ -691,8 +680,8 @@ function readArray(length) {
691
680
  array[i] = read();
692
681
  }
693
682
  if (currentUnpackr.freezeData)
694
- return Object.freeze(array)
695
- return array
683
+ return Object.freeze(array);
684
+ return array;
696
685
  }
697
686
 
698
687
  function readMap(length) {
@@ -704,13 +693,13 @@ function readMap(length) {
704
693
  key = '__proto_';
705
694
  object[key] = read();
706
695
  }
707
- return object
696
+ return object;
708
697
  } else {
709
698
  let map = new Map();
710
699
  for (let i = 0; i < length; i++) {
711
700
  map.set(read(), read());
712
701
  }
713
- return map
702
+ return map;
714
703
  }
715
704
  }
716
705
 
@@ -722,40 +711,40 @@ function longStringInJS(length) {
722
711
  const byte = src[position$1++];
723
712
  if ((byte & 0x80) > 0) {
724
713
  position$1 = start;
725
- return
714
+ return;
726
715
  }
727
716
  bytes[i] = byte;
728
717
  }
729
- return fromCharCode.apply(String, bytes)
718
+ return fromCharCode.apply(String, bytes);
730
719
  }
731
720
  function shortStringInJS(length) {
732
721
  if (length < 4) {
733
722
  if (length < 2) {
734
723
  if (length === 0)
735
- return ''
724
+ return '';
736
725
  else {
737
726
  let a = src[position$1++];
738
727
  if ((a & 0x80) > 1) {
739
728
  position$1 -= 1;
740
- return
729
+ return;
741
730
  }
742
- return fromCharCode(a)
731
+ return fromCharCode(a);
743
732
  }
744
733
  } else {
745
734
  let a = src[position$1++];
746
735
  let b = src[position$1++];
747
736
  if ((a & 0x80) > 0 || (b & 0x80) > 0) {
748
737
  position$1 -= 2;
749
- return
738
+ return;
750
739
  }
751
740
  if (length < 3)
752
- return fromCharCode(a, b)
741
+ return fromCharCode(a, b);
753
742
  let c = src[position$1++];
754
743
  if ((c & 0x80) > 0) {
755
744
  position$1 -= 3;
756
- return
745
+ return;
757
746
  }
758
- return fromCharCode(a, b, c)
747
+ return fromCharCode(a, b, c);
759
748
  }
760
749
  } else {
761
750
  let a = src[position$1++];
@@ -764,34 +753,34 @@ function shortStringInJS(length) {
764
753
  let d = src[position$1++];
765
754
  if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
766
755
  position$1 -= 4;
767
- return
756
+ return;
768
757
  }
769
758
  if (length < 6) {
770
759
  if (length === 4)
771
- return fromCharCode(a, b, c, d)
760
+ return fromCharCode(a, b, c, d);
772
761
  else {
773
762
  let e = src[position$1++];
774
763
  if ((e & 0x80) > 0) {
775
764
  position$1 -= 5;
776
- return
765
+ return;
777
766
  }
778
- return fromCharCode(a, b, c, d, e)
767
+ return fromCharCode(a, b, c, d, e);
779
768
  }
780
769
  } else if (length < 8) {
781
770
  let e = src[position$1++];
782
771
  let f = src[position$1++];
783
772
  if ((e & 0x80) > 0 || (f & 0x80) > 0) {
784
773
  position$1 -= 6;
785
- return
774
+ return;
786
775
  }
787
776
  if (length < 7)
788
- return fromCharCode(a, b, c, d, e, f)
777
+ return fromCharCode(a, b, c, d, e, f);
789
778
  let g = src[position$1++];
790
779
  if ((g & 0x80) > 0) {
791
780
  position$1 -= 7;
792
- return
781
+ return;
793
782
  }
794
- return fromCharCode(a, b, c, d, e, f, g)
783
+ return fromCharCode(a, b, c, d, e, f, g);
795
784
  } else {
796
785
  let e = src[position$1++];
797
786
  let f = src[position$1++];
@@ -799,34 +788,34 @@ function shortStringInJS(length) {
799
788
  let h = src[position$1++];
800
789
  if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
801
790
  position$1 -= 8;
802
- return
791
+ return;
803
792
  }
804
793
  if (length < 10) {
805
794
  if (length === 8)
806
- return fromCharCode(a, b, c, d, e, f, g, h)
795
+ return fromCharCode(a, b, c, d, e, f, g, h);
807
796
  else {
808
797
  let i = src[position$1++];
809
798
  if ((i & 0x80) > 0) {
810
799
  position$1 -= 9;
811
- return
800
+ return;
812
801
  }
813
- return fromCharCode(a, b, c, d, e, f, g, h, i)
802
+ return fromCharCode(a, b, c, d, e, f, g, h, i);
814
803
  }
815
804
  } else if (length < 12) {
816
805
  let i = src[position$1++];
817
806
  let j = src[position$1++];
818
807
  if ((i & 0x80) > 0 || (j & 0x80) > 0) {
819
808
  position$1 -= 10;
820
- return
809
+ return;
821
810
  }
822
811
  if (length < 11)
823
- return fromCharCode(a, b, c, d, e, f, g, h, i, j)
812
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j);
824
813
  let k = src[position$1++];
825
814
  if ((k & 0x80) > 0) {
826
815
  position$1 -= 11;
827
- return
816
+ return;
828
817
  }
829
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
818
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
830
819
  } else {
831
820
  let i = src[position$1++];
832
821
  let j = src[position$1++];
@@ -834,34 +823,34 @@ function shortStringInJS(length) {
834
823
  let l = src[position$1++];
835
824
  if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
836
825
  position$1 -= 12;
837
- return
826
+ return;
838
827
  }
839
828
  if (length < 14) {
840
829
  if (length === 12)
841
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
830
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
842
831
  else {
843
832
  let m = src[position$1++];
844
833
  if ((m & 0x80) > 0) {
845
834
  position$1 -= 13;
846
- return
835
+ return;
847
836
  }
848
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
837
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
849
838
  }
850
839
  } else {
851
840
  let m = src[position$1++];
852
841
  let n = src[position$1++];
853
842
  if ((m & 0x80) > 0 || (n & 0x80) > 0) {
854
843
  position$1 -= 14;
855
- return
844
+ return;
856
845
  }
857
846
  if (length < 15)
858
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
847
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
859
848
  let o = src[position$1++];
860
849
  if ((o & 0x80) > 0) {
861
850
  position$1 -= 15;
862
- return
851
+ return;
863
852
  }
864
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
853
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
865
854
  }
866
855
  }
867
856
  }
@@ -879,22 +868,22 @@ function readOnlyJSString() {
879
868
  case 0xd9:
880
869
  // str 8
881
870
  length = src[position$1++];
882
- break
871
+ break;
883
872
  case 0xda:
884
873
  // str 16
885
874
  length = dataView.getUint16(position$1);
886
875
  position$1 += 2;
887
- break
876
+ break;
888
877
  case 0xdb:
889
878
  // str 32
890
879
  length = dataView.getUint32(position$1);
891
880
  position$1 += 4;
892
- break
881
+ break;
893
882
  default:
894
- throw new Error('Expected string')
883
+ throw new Error('Expected string');
895
884
  }
896
885
  }
897
- return readStringJS(length)
886
+ return readStringJS(length);
898
887
  }
899
888
 
900
889
 
@@ -902,7 +891,7 @@ function readBin(length) {
902
891
  return currentUnpackr.copyBuffers ?
903
892
  // specifically use the copying slice (not the node one)
904
893
  Uint8Array.prototype.slice.call(src, position$1, position$1 += length) :
905
- src.subarray(position$1, position$1 += length)
894
+ src.subarray(position$1, position$1 += length);
906
895
  }
907
896
  function readExt(length) {
908
897
  let type = src[position$1++];
@@ -915,10 +904,10 @@ function readExt(length) {
915
904
  } finally {
916
905
  position$1 = end;
917
906
  }
918
- })
907
+ });
919
908
  }
920
909
  else
921
- throw new Error('Unknown extension type ' + type)
910
+ throw new Error('Unknown extension type ' + type);
922
911
  }
923
912
 
924
913
  var keyCache = new Array(4096);
@@ -928,12 +917,12 @@ function readKey() {
928
917
  // fixstr, potentially use key cache
929
918
  length = length - 0xa0;
930
919
  if (srcStringEnd >= position$1) // if it has been extracted, must use it (and faster anyway)
931
- return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart)
920
+ return srcString.slice(position$1 - srcStringStart, (position$1 += length) - srcStringStart);
932
921
  else if (!(srcStringEnd == 0 && srcEnd < 180))
933
- return readFixedString(length)
922
+ return readFixedString(length);
934
923
  } else { // not cacheable, go back and do a standard read
935
924
  position$1--;
936
- return asSafeString(read())
925
+ return asSafeString(read());
937
926
  }
938
927
  let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position$1) : length > 0 ? src[position$1] : 0)) & 0xfff;
939
928
  let entry = keyCache[key];
@@ -946,7 +935,7 @@ function readKey() {
946
935
  chunk = dataView.getUint32(checkPosition);
947
936
  if (chunk != entry[i++]) {
948
937
  checkPosition = 0x70000000;
949
- break
938
+ break;
950
939
  }
951
940
  checkPosition += 4;
952
941
  }
@@ -955,12 +944,12 @@ function readKey() {
955
944
  chunk = src[checkPosition++];
956
945
  if (chunk != entry[i++]) {
957
946
  checkPosition = 0x70000000;
958
- break
947
+ break;
959
948
  }
960
949
  }
961
950
  if (checkPosition === end) {
962
951
  position$1 = checkPosition;
963
- return entry.string
952
+ return entry.string;
964
953
  }
965
954
  end -= 3;
966
955
  checkPosition = position$1;
@@ -981,8 +970,8 @@ function readKey() {
981
970
  // for small blocks, avoiding the overhead of the extract call is helpful
982
971
  let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
983
972
  if (string != null)
984
- return entry.string = string
985
- return entry.string = readFixedString(length)
973
+ return entry.string = string;
974
+ return entry.string = readFixedString(length);
986
975
  }
987
976
 
988
977
  function asSafeString(property) {
@@ -1013,7 +1002,7 @@ const recordDefinition = (id, highByte) => {
1013
1002
  }
1014
1003
  currentStructures[id] = structure;
1015
1004
  structure.read = createStructureReader(structure, firstByte);
1016
- return structure.read()
1005
+ return structure.read();
1017
1006
  };
1018
1007
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
1019
1008
  currentExtensions[0].noBuffer = true;
@@ -1035,17 +1024,17 @@ currentExtensions[0x42] = data => {
1035
1024
  out <<= BigInt(64);
1036
1025
  out |= view.getBigUint64(i);
1037
1026
  }
1038
- return out
1027
+ return out;
1039
1028
  }
1040
1029
  // if (length === 8) return view.getBigUint64(start)
1041
1030
  let middle = start + (length >> 4 << 3);
1042
1031
  let left = decode(start, middle);
1043
1032
  let right = decode(middle, end);
1044
- return (left << BigInt((end - middle) * 8)) | right
1033
+ return (left << BigInt((end - middle) * 8)) | right;
1045
1034
  };
1046
1035
  head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength);
1047
1036
  }
1048
- return head
1037
+ return head;
1049
1038
  };
1050
1039
 
1051
1040
  let errors = {
@@ -1056,14 +1045,14 @@ currentExtensions[0x65] = () => {
1056
1045
  if (!errors[data[0]]) {
1057
1046
  let error = Error(data[1], { cause: data[2] });
1058
1047
  error.name = data[0];
1059
- return error
1048
+ return error;
1060
1049
  }
1061
- return errors[data[0]](data[1], { cause: data[2] })
1050
+ return errors[data[0]](data[1], { cause: data[2] });
1062
1051
  };
1063
1052
 
1064
1053
  currentExtensions[0x69] = (data) => {
1065
1054
  // id extension (for structured clones)
1066
- if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
1055
+ if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled');
1067
1056
  let id = dataView.getUint32(position$1 - 4);
1068
1057
  if (!referenceMap)
1069
1058
  referenceMap = new Map();
@@ -1084,7 +1073,7 @@ currentExtensions[0x69] = (data) => {
1084
1073
  let targetProperties = read(); // read the next value as the target object to id
1085
1074
  if (!refEntry.used) {
1086
1075
  // no cycle, can just use the returned read object
1087
- return refEntry.target = targetProperties // replace the placeholder with the real one
1076
+ return refEntry.target = targetProperties; // replace the placeholder with the real one
1088
1077
  } else {
1089
1078
  // there is a cycle, so we have to assign properties to original target
1090
1079
  Object.assign(target, targetProperties);
@@ -1095,16 +1084,16 @@ currentExtensions[0x69] = (data) => {
1095
1084
  for (let [k, v] of targetProperties.entries()) target.set(k, v);
1096
1085
  if (target instanceof Set)
1097
1086
  for (let i of Array.from(targetProperties)) target.add(i);
1098
- return target
1087
+ return target;
1099
1088
  };
1100
1089
 
1101
1090
  currentExtensions[0x70] = (data) => {
1102
1091
  // pointer extension (for structured clones)
1103
- if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
1092
+ if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled');
1104
1093
  let id = dataView.getUint32(position$1 - 4);
1105
1094
  let refEntry = referenceMap.get(id);
1106
1095
  refEntry.used = true;
1107
- return refEntry.target
1096
+ return refEntry.target;
1108
1097
  };
1109
1098
 
1110
1099
  currentExtensions[0x73] = () => new Set(read());
@@ -1119,15 +1108,15 @@ currentExtensions[0x74] = (data) => {
1119
1108
 
1120
1109
  let typedArrayName = typedArrays[typeCode];
1121
1110
  if (!typedArrayName) {
1122
- if (typeCode === 16) return buffer
1123
- if (typeCode === 17) return new DataView(buffer)
1124
- throw new Error('Could not find typed array for code ' + typeCode)
1111
+ if (typeCode === 16) return buffer;
1112
+ if (typeCode === 17) return new DataView(buffer);
1113
+ throw new Error('Could not find typed array for code ' + typeCode);
1125
1114
  }
1126
- return new glbl[typedArrayName](buffer)
1115
+ return new glbl[typedArrayName](buffer);
1127
1116
  };
1128
1117
  currentExtensions[0x78] = () => {
1129
1118
  let data = read();
1130
- return new RegExp(data[0], data[1])
1119
+ return new RegExp(data[0], data[1]);
1131
1120
  };
1132
1121
  const TEMP_BUNDLE = [];
1133
1122
  currentExtensions[0x62] = (data) => {
@@ -1140,30 +1129,30 @@ currentExtensions[0x62] = (data) => {
1140
1129
  bundledStrings$1.position1 = 0;
1141
1130
  bundledStrings$1.postBundlePosition = position$1;
1142
1131
  position$1 = dataPosition;
1143
- return read()
1132
+ return read();
1144
1133
  };
1145
1134
 
1146
1135
  currentExtensions[0xff] = (data) => {
1147
1136
  // 32-bit date extension
1148
1137
  if (data.length == 4)
1149
- return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
1138
+ return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000);
1150
1139
  else if (data.length == 8)
1151
1140
  return new Date(
1152
1141
  ((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
1153
- ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
1142
+ ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000);
1154
1143
  else if (data.length == 12)
1155
1144
  return new Date(
1156
1145
  ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
1157
- (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
1146
+ (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000);
1158
1147
  else
1159
- return new Date('invalid')
1148
+ return new Date('invalid');
1160
1149
  };
1161
1150
  // registration of bulk record definition?
1162
1151
  // currentExtensions[0x52] = () =>
1163
1152
 
1164
- function saveState$1(callback) {
1165
- if (onSaveState)
1166
- onSaveState();
1153
+ function saveState(callback) {
1154
+ if (currentUnpackr && currentUnpackr._onSaveState)
1155
+ currentUnpackr._onSaveState();
1167
1156
  let savedSrcEnd = srcEnd;
1168
1157
  let savedPosition = position$1;
1169
1158
  let savedStringPosition = stringPosition;
@@ -1196,7 +1185,7 @@ function saveState$1(callback) {
1196
1185
  currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
1197
1186
  currentUnpackr = savedPackr;
1198
1187
  dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
1199
- return value
1188
+ return value;
1200
1189
  }
1201
1190
  function clearSource() {
1202
1191
  src = null;
@@ -1231,30 +1220,28 @@ let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
1231
1220
  function roundFloat32(float32Number) {
1232
1221
  f32Array[0] = float32Number;
1233
1222
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1234
- return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1235
- }
1236
- function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1237
- readStruct$1 = updatedReadStruct;
1238
- onLoadedStructures$1 = loadedStructs;
1239
- onSaveState = saveState;
1223
+ return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
1240
1224
  }
1225
+ // Marker for downstream libraries (e.g. structon) to detect per-instance
1226
+ // struct-decoding hooks (this._readStruct, this._onLoadedStructures,
1227
+ // this._onSaveState). See `checkedRead` for the dispatch.
1228
+ Unpackr.SUPPORTS_STRUCT_HOOKS = true;
1241
1229
 
1242
- let textEncoder$1;
1230
+ let textEncoder;
1243
1231
  try {
1244
- textEncoder$1 = new TextEncoder();
1232
+ textEncoder = new TextEncoder();
1245
1233
  } catch (error) {}
1246
1234
  let extensions, extensionClasses;
1247
- const hasNodeBuffer$1 = typeof Buffer !== 'undefined';
1248
- const ByteArrayAllocate = hasNodeBuffer$1 ?
1249
- function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
1250
- const ByteArray = hasNodeBuffer$1 ? Buffer : Uint8Array;
1251
- const MAX_BUFFER_SIZE = hasNodeBuffer$1 ? 0x100000000 : 0x7fd00000;
1235
+ const hasNodeBuffer = typeof Buffer !== 'undefined';
1236
+ const ByteArrayAllocate = hasNodeBuffer ?
1237
+ function(length) { return Buffer.allocUnsafeSlow(length); } : Uint8Array;
1238
+ const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1239
+ const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1252
1240
  let target, keysTarget;
1253
1241
  let targetView;
1254
1242
  let position = 0;
1255
1243
  let safeEnd;
1256
1244
  let bundledStrings = null;
1257
- let writeStructSlots;
1258
1245
  const MAX_BUNDLE_SIZE = 0x5500; // maximum characters such that the encoded bytes fits in 16 bits.
1259
1246
  const hasNonLatin = /[\u0080-\uFFFF]/;
1260
1247
  const RECORD_SYMBOL = Symbol('record-id');
@@ -1267,10 +1254,10 @@ class Packr extends Unpackr {
1267
1254
  let structures;
1268
1255
  let referenceMap;
1269
1256
  let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1270
- return target.utf8Write(string, position, target.byteLength - position)
1271
- } : (textEncoder$1 && textEncoder$1.encodeInto) ?
1257
+ return target.utf8Write(string, position, target.byteLength - position);
1258
+ } : (textEncoder && textEncoder.encodeInto) ?
1272
1259
  function(string, position) {
1273
- return textEncoder$1.encodeInto(string, target.subarray(position)).written
1260
+ return textEncoder.encodeInto(string, target.subarray(position)).written;
1274
1261
  } : false;
1275
1262
 
1276
1263
  let packr = this;
@@ -1282,7 +1269,7 @@ class Packr extends Unpackr {
1282
1269
  if (maxSharedStructures == null)
1283
1270
  maxSharedStructures = hasSharedStructures ? 32 : 0;
1284
1271
  if (maxSharedStructures > 8160)
1285
- throw new Error('Maximum maxSharedStructure is 8160')
1272
+ throw new Error('Maximum maxSharedStructure is 8160');
1286
1273
  if (options.structuredClone && options.moreTypes == undefined) {
1287
1274
  this.moreTypes = true;
1288
1275
  }
@@ -1296,7 +1283,7 @@ class Packr extends Unpackr {
1296
1283
  let sharedLimitId = maxSharedStructures + 0x40;
1297
1284
  let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
1298
1285
  if (maxStructureId > 8256) {
1299
- throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
1286
+ throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192');
1300
1287
  }
1301
1288
  let recordIdsToRemove = [];
1302
1289
  let transitionsCount = 0;
@@ -1332,7 +1319,7 @@ class Packr extends Unpackr {
1332
1319
  let sharedLength = structures.sharedLength || 0;
1333
1320
  if (sharedLength > maxSharedStructures) {
1334
1321
  //if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
1335
- throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
1322
+ throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength);
1336
1323
  }
1337
1324
  if (!structures.transitions) {
1338
1325
  // rebuild our structure transitions
@@ -1340,7 +1327,7 @@ class Packr extends Unpackr {
1340
1327
  for (let i = 0; i < sharedLength; i++) {
1341
1328
  let keys = structures[i];
1342
1329
  if (!keys)
1343
- continue
1330
+ continue;
1344
1331
  let nextTransition, transition = structures.transitions;
1345
1332
  for (let j = 0, l = keys.length; j < l; j++) {
1346
1333
  let key = keys[j];
@@ -1362,7 +1349,7 @@ class Packr extends Unpackr {
1362
1349
  hasSharedUpdate = false;
1363
1350
  let encodingError;
1364
1351
  try {
1365
- if (packr.randomAccessStructure && value && typeof value === 'object') {
1352
+ if (packr._writeStruct && value && typeof value === 'object') {
1366
1353
  if (value.constructor === Object) writeStruct(value); // simple object
1367
1354
  else if (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass)) {
1368
1355
  // allow user classes, if they don't need special handling (but do use toJSON if available)
@@ -1406,15 +1393,15 @@ class Packr extends Unpackr {
1406
1393
  packr.offset = position;
1407
1394
  let serialized = insertIds(target.subarray(start, position), idsToInsert);
1408
1395
  referenceMap = null;
1409
- return serialized
1396
+ return serialized;
1410
1397
  }
1411
1398
  packr.offset = position; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1412
1399
  if (encodeOptions & REUSE_BUFFER_MODE) {
1413
1400
  target.start = start;
1414
1401
  target.end = position;
1415
- return target
1402
+ return target;
1416
1403
  }
1417
- return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1404
+ return target.subarray(start, position); // position can change if we call pack again in saveStructures, so we get the buffer now
1418
1405
  } catch(error) {
1419
1406
  encodingError = error;
1420
1407
  throw error;
@@ -1425,16 +1412,16 @@ class Packr extends Unpackr {
1425
1412
  let sharedLength = structures.sharedLength || 0;
1426
1413
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1427
1414
  let returnBuffer = target.subarray(start, position);
1428
- let newSharedData = prepareStructures$1(structures, packr);
1415
+ let newSharedData = (packr._prepareStructures || prepareStructures)(structures, packr);
1429
1416
  if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
1430
1417
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1431
1418
  // get updated structures and try again if the update failed
1432
- return packr.pack(value, encodeOptions)
1419
+ return packr.pack(value, encodeOptions);
1433
1420
  }
1434
1421
  packr.lastNamedStructuresLength = sharedLength;
1435
1422
  // don't keep large buffers around
1436
1423
  if (target.length > 0x40000000) target = null;
1437
- return returnBuffer
1424
+ return returnBuffer;
1438
1425
  }
1439
1426
  }
1440
1427
  }
@@ -1520,7 +1507,7 @@ class Packr extends Unpackr {
1520
1507
  bundledStrings[twoByte ? 0 : 1] += value;
1521
1508
  target[position++] = 0xc1;
1522
1509
  pack(twoByte ? -strLength : strLength);
1523
- return
1510
+ return;
1524
1511
  }
1525
1512
  let headerSize;
1526
1513
  // first we estimate the header size, so we can write to the correct location
@@ -1594,7 +1581,7 @@ class Packr extends Unpackr {
1594
1581
  } else if (type === 'number') {
1595
1582
  if (value >>> 0 === value) {// positive integer, 32-bit or less
1596
1583
  // positive uint
1597
- if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this.randomAccessStructure)) {
1584
+ if (value < 0x20 || (value < 0x80 && this.useRecords === false) || (value < 0x40 && !this._writeStruct)) {
1598
1585
  target[position++] = value;
1599
1586
  } else if (value < 0x100) {
1600
1587
  target[position++] = 0xcc;
@@ -1633,7 +1620,7 @@ class Packr extends Unpackr {
1633
1620
  // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1634
1621
  ((xShifted = value * mult10[((target[position] & 0x7f) << 1) | (target[position + 1] >> 7)]) >> 0) === xShifted) {
1635
1622
  position += 4;
1636
- return
1623
+ return;
1637
1624
  } else
1638
1625
  position--; // move back into position for writing a double
1639
1626
  }
@@ -1656,7 +1643,7 @@ class Packr extends Unpackr {
1656
1643
  target[position++] = 0x70; // "p" for pointer
1657
1644
  targetView.setUint32(position, referee.id);
1658
1645
  position += 4;
1659
- return
1646
+ return;
1660
1647
  } else
1661
1648
  referenceMap.set(value, { offset: position - start });
1662
1649
  }
@@ -1706,7 +1693,7 @@ class Packr extends Unpackr {
1706
1693
  } else {
1707
1694
  pack(writeResult);
1708
1695
  }
1709
- return
1696
+ return;
1710
1697
  }
1711
1698
  let currentTarget = target;
1712
1699
  let currentTargetView = targetView;
@@ -1723,7 +1710,7 @@ class Packr extends Unpackr {
1723
1710
  makeRoom(position);
1724
1711
  return {
1725
1712
  target, targetView, position: position - size
1726
- }
1713
+ };
1727
1714
  }, pack);
1728
1715
  } finally {
1729
1716
  // restore current target information (unless already restored)
@@ -1739,7 +1726,7 @@ class Packr extends Unpackr {
1739
1726
  makeRoom(result.length + position);
1740
1727
  position = writeExtensionData(result, target, position, extension.type);
1741
1728
  }
1742
- return
1729
+ return;
1743
1730
  }
1744
1731
  }
1745
1732
  // check isArray after extensions, because extensions can extend Array
@@ -1751,7 +1738,7 @@ class Packr extends Unpackr {
1751
1738
  const json = value.toJSON();
1752
1739
  // if for some reason value.toJSON returns itself it'll loop forever
1753
1740
  if (json !== value)
1754
- return pack(json)
1741
+ return pack(json);
1755
1742
  }
1756
1743
 
1757
1744
  // if there is a writeFunction, use it, otherwise just encode as undefined
@@ -1790,7 +1777,7 @@ class Packr extends Unpackr {
1790
1777
  let chunks = [];
1791
1778
  while (true) {
1792
1779
  chunks.push(value & mask);
1793
- if ((value >> BigInt(63)) === empty) break
1780
+ if ((value >> BigInt(63)) === empty) break;
1794
1781
  value >>= BigInt(64);
1795
1782
  }
1796
1783
 
@@ -1805,7 +1792,7 @@ class Packr extends Unpackr {
1805
1792
  string = '00' + string;
1806
1793
  }
1807
1794
 
1808
- if (hasNodeBuffer$1) {
1795
+ if (hasNodeBuffer) {
1809
1796
  array = Buffer.from(string, 'hex');
1810
1797
  } else {
1811
1798
  array = new Uint8Array(string.length / 2);
@@ -1822,11 +1809,11 @@ class Packr extends Unpackr {
1822
1809
  if (array.length + position > safeEnd)
1823
1810
  makeRoom(array.length + position);
1824
1811
  position = writeExtensionData(array, target, position, 0x42);
1825
- return
1812
+ return;
1826
1813
  } else {
1827
1814
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
1828
1815
  ' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
1829
- ' largeBigIntToString to convert to string')
1816
+ ' largeBigIntToString to convert to string');
1830
1817
  }
1831
1818
  }
1832
1819
  position += 8;
@@ -1839,7 +1826,7 @@ class Packr extends Unpackr {
1839
1826
  target[position++] = 0;
1840
1827
  }
1841
1828
  } else {
1842
- throw new Error('Unknown type: ' + type)
1829
+ throw new Error('Unknown type: ' + type);
1843
1830
  }
1844
1831
  };
1845
1832
 
@@ -1985,12 +1972,30 @@ class Packr extends Unpackr {
1985
1972
  checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1986
1973
  } : writeRecord;
1987
1974
 
1975
+ const writeStruct = (object) => {
1976
+ let newPosition = packr._writeStruct(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1977
+ if (notifySharedUpdate)
1978
+ return hasSharedUpdate = true;
1979
+ position = newPosition;
1980
+ let startTarget = target;
1981
+ pack(value);
1982
+ resetStructures();
1983
+ if (startTarget !== target) {
1984
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1985
+ }
1986
+ return position;
1987
+ });
1988
+ if (newPosition === 0) // bail and go to a msgpack object
1989
+ return writeObject(object);
1990
+ position = newPosition;
1991
+ };
1992
+
1988
1993
  const makeRoom = (end) => {
1989
1994
  let newSize;
1990
1995
  if (end > 0x1000000) {
1991
1996
  // special handling for really large buffers
1992
1997
  if ((end - start) > MAX_BUFFER_SIZE)
1993
- throw new Error('Packed buffer would be larger than maximum buffer size')
1998
+ throw new Error('Packed buffer would be larger than maximum buffer size');
1994
1999
  newSize = Math.min(MAX_BUFFER_SIZE,
1995
2000
  Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
1996
2001
  } else // faster handling for smaller buffers
@@ -2005,7 +2010,7 @@ class Packr extends Unpackr {
2005
2010
  position -= start;
2006
2011
  start = 0;
2007
2012
  safeEnd = newBuffer.length - 10;
2008
- return target = newBuffer
2013
+ return target = newBuffer;
2009
2014
  };
2010
2015
  const newRecord = (transition, keys, newTransitions) => {
2011
2016
  let recordId = structures.nextId;
@@ -2087,23 +2092,6 @@ class Packr extends Unpackr {
2087
2092
  target[insertionOffset + start] = keysTarget[0];
2088
2093
  }
2089
2094
  };
2090
- const writeStruct = (object) => {
2091
- let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
2092
- if (notifySharedUpdate)
2093
- return hasSharedUpdate = true;
2094
- position = newPosition;
2095
- let startTarget = target;
2096
- pack(value);
2097
- resetStructures();
2098
- if (startTarget !== target) {
2099
- return { position, targetView, target }; // indicate the buffer was re-allocated
2100
- }
2101
- return position;
2102
- }, this);
2103
- if (newPosition === 0) // bail and go to a msgpack object
2104
- return writeObject(object);
2105
- position = newPosition;
2106
- };
2107
2095
  }
2108
2096
  useBuffer(buffer) {
2109
2097
  // this means we are finished using our own buffer and we can write over it safely
@@ -2146,7 +2134,7 @@ extensions = [{
2146
2134
  } else if (isNaN(seconds)) {
2147
2135
  if (this.onInvalidDate) {
2148
2136
  allocateForWrite(0);
2149
- return pack(this.onInvalidDate())
2137
+ return pack(this.onInvalidDate());
2150
2138
  }
2151
2139
  // Intentionally invalid timestamp
2152
2140
  let { target, targetView, position} = allocateForWrite(3);
@@ -2167,7 +2155,7 @@ extensions = [{
2167
2155
  pack(set, allocateForWrite, pack) {
2168
2156
  if (this.setAsEmptyObject) {
2169
2157
  allocateForWrite(0);
2170
- return pack({})
2158
+ return pack({});
2171
2159
  }
2172
2160
  let array = Array.from(set);
2173
2161
  let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
@@ -2203,7 +2191,7 @@ extensions = [{
2203
2191
  if (this.moreTypes)
2204
2192
  writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
2205
2193
  else
2206
- writeBuffer(hasNodeBuffer$1 ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
2194
+ writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
2207
2195
  }
2208
2196
  }, {
2209
2197
  pack(typedArray, allocateForWrite) {
@@ -2218,7 +2206,7 @@ extensions = [{
2218
2206
  if (this.moreTypes)
2219
2207
  writeExtBuffer(arrayBuffer, 0x11, allocateForWrite);
2220
2208
  else
2221
- writeBuffer(hasNodeBuffer$1 ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
2209
+ writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
2222
2210
  }
2223
2211
  }, {
2224
2212
  pack(c1, allocateForWrite) { // specific 0xC1 object
@@ -2275,19 +2263,19 @@ function writeExtensionData(result, target, position, type) {
2275
2263
  switch (length) {
2276
2264
  case 1:
2277
2265
  target[position++] = 0xd4;
2278
- break
2266
+ break;
2279
2267
  case 2:
2280
2268
  target[position++] = 0xd5;
2281
- break
2269
+ break;
2282
2270
  case 4:
2283
2271
  target[position++] = 0xd6;
2284
- break
2272
+ break;
2285
2273
  case 8:
2286
2274
  target[position++] = 0xd7;
2287
- break
2275
+ break;
2288
2276
  case 16:
2289
2277
  target[position++] = 0xd8;
2290
- break
2278
+ break;
2291
2279
  default:
2292
2280
  if (length < 0x100) {
2293
2281
  target[position++] = 0xc7;
@@ -2307,7 +2295,7 @@ function writeExtensionData(result, target, position, type) {
2307
2295
  target[position++] = type;
2308
2296
  target.set(result, position);
2309
2297
  position += length;
2310
- return position
2298
+ return position;
2311
2299
  }
2312
2300
 
2313
2301
  function insertIds(serialized, idsToInsert) {
@@ -2329,7 +2317,7 @@ function insertIds(serialized, idsToInsert) {
2329
2317
  serialized[position++] = id & 0xff;
2330
2318
  lastEnd = offset;
2331
2319
  }
2332
- return serialized
2320
+ return serialized;
2333
2321
  }
2334
2322
 
2335
2323
  function writeBundles(start, pack, incrementPosition) {
@@ -2346,28 +2334,29 @@ function writeBundles(start, pack, incrementPosition) {
2346
2334
  function addExtension(extension) {
2347
2335
  if (extension.Class) {
2348
2336
  if (!extension.pack && !extension.write)
2349
- throw new Error('Extension has no pack or write function')
2337
+ throw new Error('Extension has no pack or write function');
2350
2338
  if (extension.pack && !extension.type)
2351
- throw new Error('Extension has no type (numeric code to identify the extension)')
2339
+ throw new Error('Extension has no type (numeric code to identify the extension)');
2352
2340
  extensionClasses.unshift(extension.Class);
2353
2341
  extensions.unshift(extension);
2354
2342
  }
2355
2343
  addExtension$1(extension);
2356
2344
  }
2357
- function prepareStructures$1(structures, packr) {
2345
+ function prepareStructures(structures, packr) {
2358
2346
  structures.isCompatible = (existingStructures) => {
2359
2347
  let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
2360
2348
  if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
2361
2349
  packr._mergeStructures(existingStructures);
2362
2350
  return compatible;
2363
2351
  };
2364
- return structures
2365
- }
2366
- function setWriteStructSlots(writeSlots, makeStructures) {
2367
- writeStructSlots = writeSlots;
2368
- prepareStructures$1 = makeStructures;
2352
+ return structures;
2369
2353
  }
2370
2354
 
2355
+ // Marker for downstream libraries (e.g. structon) to detect that this Packr
2356
+ // supports per-instance struct-encoding hooks (this._writeStruct,
2357
+ // this._prepareStructures). See `pack` for the dispatch.
2358
+ Packr.SUPPORTS_STRUCT_HOOKS = true;
2359
+
2371
2360
  let defaultPackr = new Packr({ useRecords: false });
2372
2361
  const pack = defaultPackr.pack;
2373
2362
  const encode = defaultPackr.pack;
@@ -2377,779 +2366,6 @@ const REUSE_BUFFER_MODE = 512;
2377
2366
  const RESET_BUFFER_MODE = 1024;
2378
2367
  const RESERVE_START_SPACE = 2048;
2379
2368
 
2380
- const ASCII = 3; // the MIBenum from https://www.iana.org/assignments/character-sets/character-sets.xhtml (and other character encodings could be referenced by MIBenum)
2381
- const NUMBER = 0;
2382
- const UTF8 = 2;
2383
- const OBJECT_DATA = 1;
2384
- const DATE = 16;
2385
- const TYPE_NAMES = ['num', 'object', 'string', 'ascii'];
2386
- TYPE_NAMES[DATE] = 'date';
2387
- const float32Headers = [false, true, true, false, false, true, true, false];
2388
- let evalSupported;
2389
- try {
2390
- new Function('');
2391
- evalSupported = true;
2392
- } catch(error) {
2393
- // if eval variants are not supported, do not create inline object readers ever
2394
- }
2395
-
2396
- let updatedPosition;
2397
- const hasNodeBuffer = typeof Buffer !== 'undefined';
2398
- let textEncoder, currentSource;
2399
- try {
2400
- textEncoder = new TextEncoder();
2401
- } catch (error) {}
2402
- const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
2403
- return target.utf8Write(string, position, target.byteLength - position)
2404
- } : (textEncoder && textEncoder.encodeInto) ?
2405
- function(target, string, position) {
2406
- return textEncoder.encodeInto(string, target.subarray(position)).written
2407
- } : false;
2408
- setWriteStructSlots(writeStruct, prepareStructures);
2409
- function writeStruct(object, target, encodingStart, position, structures, makeRoom, pack, packr) {
2410
- let typedStructs = packr.typedStructs || (packr.typedStructs = []);
2411
- // note that we rely on pack.js to load stored structures before we get to this point
2412
- let targetView = target.dataView;
2413
- let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
2414
- let safeEnd = target.length - 10;
2415
- let start = position;
2416
- if (position > safeEnd) {
2417
- target = makeRoom(position);
2418
- targetView = target.dataView;
2419
- position -= encodingStart;
2420
- start -= encodingStart;
2421
- refsStartPosition -= encodingStart;
2422
- encodingStart = 0;
2423
- safeEnd = target.length - 10;
2424
- }
2425
-
2426
- let refOffset, refPosition = refsStartPosition;
2427
-
2428
- let transition = typedStructs.transitions || (typedStructs.transitions = Object.create(null));
2429
- let nextId = typedStructs.nextId || typedStructs.length;
2430
- let headerSize =
2431
- nextId < 0xf ? 1 :
2432
- nextId < 0xf0 ? 2 :
2433
- nextId < 0xf000 ? 3 :
2434
- nextId < 0xf00000 ? 4 : 0;
2435
- if (headerSize === 0)
2436
- return 0;
2437
- position += headerSize;
2438
- let queuedReferences = [];
2439
- let usedAscii0;
2440
- let keyIndex = 0;
2441
- for (let key in object) {
2442
- let value = object[key];
2443
- let nextTransition = transition[key];
2444
- if (!nextTransition) {
2445
- transition[key] = nextTransition = {
2446
- key,
2447
- parent: transition,
2448
- enumerationOffset: 0,
2449
- ascii0: null,
2450
- ascii8: null,
2451
- num8: null,
2452
- string16: null,
2453
- object16: null,
2454
- num32: null,
2455
- float64: null,
2456
- date64: null
2457
- };
2458
- }
2459
- if (position > safeEnd) {
2460
- target = makeRoom(position);
2461
- targetView = target.dataView;
2462
- position -= encodingStart;
2463
- start -= encodingStart;
2464
- refsStartPosition -= encodingStart;
2465
- refPosition -= encodingStart;
2466
- encodingStart = 0;
2467
- safeEnd = target.length - 10;
2468
- }
2469
- switch (typeof value) {
2470
- case 'number':
2471
- let number = value;
2472
- // first check to see if we are using a lot of ids and should default to wide/common format
2473
- if (nextId < 200 || !nextTransition.num64) {
2474
- if (number >> 0 === number && number < 0x20000000 && number > -0x1f000000) {
2475
- if (number < 0xf6 && number >= 0 && (nextTransition.num8 && !(nextId > 200 && nextTransition.num32) || number < 0x20 && !nextTransition.num32)) {
2476
- transition = nextTransition.num8 || createTypeTransition(nextTransition, NUMBER, 1);
2477
- target[position++] = number;
2478
- } else {
2479
- transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2480
- targetView.setUint32(position, number, true);
2481
- position += 4;
2482
- }
2483
- break;
2484
- } else if (number < 0x100000000 && number >= -0x80000000) {
2485
- targetView.setFloat32(position, number, true);
2486
- if (float32Headers[target[position + 3] >>> 5]) {
2487
- let xShifted;
2488
- // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
2489
- if (((xShifted = number * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
2490
- transition = nextTransition.num32 || createTypeTransition(nextTransition, NUMBER, 4);
2491
- position += 4;
2492
- break;
2493
- }
2494
- }
2495
- }
2496
- }
2497
- transition = nextTransition.num64 || createTypeTransition(nextTransition, NUMBER, 8);
2498
- targetView.setFloat64(position, number, true);
2499
- position += 8;
2500
- break;
2501
- case 'string':
2502
- let strLength = value.length;
2503
- refOffset = refPosition - refsStartPosition;
2504
- if ((strLength << 2) + refPosition > safeEnd) {
2505
- target = makeRoom((strLength << 2) + refPosition);
2506
- targetView = target.dataView;
2507
- position -= encodingStart;
2508
- start -= encodingStart;
2509
- refsStartPosition -= encodingStart;
2510
- refPosition -= encodingStart;
2511
- encodingStart = 0;
2512
- safeEnd = target.length - 10;
2513
- }
2514
- if (strLength > ((0xff00 + refOffset) >> 2)) {
2515
- queuedReferences.push(key, value, position - start);
2516
- break;
2517
- }
2518
- let isNotAscii;
2519
- let strStart = refPosition;
2520
- if (strLength < 0x40) {
2521
- let i, c1, c2;
2522
- for (i = 0; i < strLength; i++) {
2523
- c1 = value.charCodeAt(i);
2524
- if (c1 < 0x80) {
2525
- target[refPosition++] = c1;
2526
- } else if (c1 < 0x800) {
2527
- isNotAscii = true;
2528
- target[refPosition++] = c1 >> 6 | 0xc0;
2529
- target[refPosition++] = c1 & 0x3f | 0x80;
2530
- } else if (
2531
- (c1 & 0xfc00) === 0xd800 &&
2532
- ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
2533
- ) {
2534
- isNotAscii = true;
2535
- c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
2536
- i++;
2537
- target[refPosition++] = c1 >> 18 | 0xf0;
2538
- target[refPosition++] = c1 >> 12 & 0x3f | 0x80;
2539
- target[refPosition++] = c1 >> 6 & 0x3f | 0x80;
2540
- target[refPosition++] = c1 & 0x3f | 0x80;
2541
- } else {
2542
- isNotAscii = true;
2543
- target[refPosition++] = c1 >> 12 | 0xe0;
2544
- target[refPosition++] = c1 >> 6 & 0x3f | 0x80;
2545
- target[refPosition++] = c1 & 0x3f | 0x80;
2546
- }
2547
- }
2548
- } else {
2549
- refPosition += encodeUtf8(target, value, refPosition);
2550
- isNotAscii = refPosition - strStart > strLength;
2551
- }
2552
- if (refOffset < 0xa0 || (refOffset < 0xf6 && (nextTransition.ascii8 || nextTransition.string8))) {
2553
- // short strings
2554
- if (isNotAscii) {
2555
- if (!(transition = nextTransition.string8)) {
2556
- if (typedStructs.length > 10 && (transition = nextTransition.ascii8)) {
2557
- // we can safely change ascii to utf8 in place since they are compatible
2558
- transition.__type = UTF8;
2559
- nextTransition.ascii8 = null;
2560
- nextTransition.string8 = transition;
2561
- pack(null, 0, true); // special call to notify that structures have been updated
2562
- } else {
2563
- transition = createTypeTransition(nextTransition, UTF8, 1);
2564
- }
2565
- }
2566
- } else if (refOffset === 0 && !usedAscii0) {
2567
- usedAscii0 = true;
2568
- transition = nextTransition.ascii0 || createTypeTransition(nextTransition, ASCII, 0);
2569
- break; // don't increment position
2570
- }// else ascii:
2571
- else if (!(transition = nextTransition.ascii8) && !(typedStructs.length > 10 && (transition = nextTransition.string8)))
2572
- transition = createTypeTransition(nextTransition, ASCII, 1);
2573
- target[position++] = refOffset;
2574
- } else {
2575
- // TODO: Enable ascii16 at some point, but get the logic right
2576
- //if (isNotAscii)
2577
- transition = nextTransition.string16 || createTypeTransition(nextTransition, UTF8, 2);
2578
- //else
2579
- //transition = nextTransition.ascii16 || createTypeTransition(nextTransition, ASCII, 2);
2580
- targetView.setUint16(position, refOffset, true);
2581
- position += 2;
2582
- }
2583
- break;
2584
- case 'object':
2585
- if (value) {
2586
- if (value.constructor === Date) {
2587
- transition = nextTransition.date64 || createTypeTransition(nextTransition, DATE, 8);
2588
- targetView.setFloat64(position, value.getTime(), true);
2589
- position += 8;
2590
- } else {
2591
- queuedReferences.push(key, value, keyIndex);
2592
- }
2593
- break;
2594
- } else { // null
2595
- nextTransition = anyType(nextTransition, position, targetView, -10); // match CBOR with this
2596
- if (nextTransition) {
2597
- transition = nextTransition;
2598
- position = updatedPosition;
2599
- } else queuedReferences.push(key, value, keyIndex);
2600
- }
2601
- break;
2602
- case 'boolean':
2603
- transition = nextTransition.num8 || nextTransition.ascii8 || createTypeTransition(nextTransition, NUMBER, 1);
2604
- target[position++] = value ? 0xf9 : 0xf8; // match CBOR with these
2605
- break;
2606
- case 'undefined':
2607
- nextTransition = anyType(nextTransition, position, targetView, -9); // match CBOR with this
2608
- if (nextTransition) {
2609
- transition = nextTransition;
2610
- position = updatedPosition;
2611
- } else queuedReferences.push(key, value, keyIndex);
2612
- break;
2613
- default:
2614
- queuedReferences.push(key, value, keyIndex);
2615
- }
2616
- keyIndex++;
2617
- }
2618
-
2619
- for (let i = 0, l = queuedReferences.length; i < l;) {
2620
- let key = queuedReferences[i++];
2621
- let value = queuedReferences[i++];
2622
- let propertyIndex = queuedReferences[i++];
2623
- let nextTransition = transition[key];
2624
- if (!nextTransition) {
2625
- transition[key] = nextTransition = {
2626
- key,
2627
- parent: transition,
2628
- enumerationOffset: propertyIndex - keyIndex,
2629
- ascii0: null,
2630
- ascii8: null,
2631
- num8: null,
2632
- string16: null,
2633
- object16: null,
2634
- num32: null,
2635
- float64: null
2636
- };
2637
- }
2638
- let newPosition;
2639
- if (value) {
2640
- /*if (typeof value === 'string') { // TODO: we could re-enable long strings
2641
- if (position + value.length * 3 > safeEnd) {
2642
- target = makeRoom(position + value.length * 3);
2643
- position -= start;
2644
- targetView = target.dataView;
2645
- start = 0;
2646
- }
2647
- newPosition = position + target.utf8Write(value, position, 0xffffffff);
2648
- } else { */
2649
- let size;
2650
- refOffset = refPosition - refsStartPosition;
2651
- if (refOffset < 0xff00) {
2652
- transition = nextTransition.object16;
2653
- if (transition)
2654
- size = 2;
2655
- else if ((transition = nextTransition.object32))
2656
- size = 4;
2657
- else {
2658
- transition = createTypeTransition(nextTransition, OBJECT_DATA, 2);
2659
- size = 2;
2660
- }
2661
- } else {
2662
- transition = nextTransition.object32 || createTypeTransition(nextTransition, OBJECT_DATA, 4);
2663
- size = 4;
2664
- }
2665
- newPosition = pack(value, refPosition);
2666
- //}
2667
- if (typeof newPosition === 'object') {
2668
- // re-allocated
2669
- refPosition = newPosition.position;
2670
- targetView = newPosition.targetView;
2671
- target = newPosition.target;
2672
- refsStartPosition -= encodingStart;
2673
- position -= encodingStart;
2674
- start -= encodingStart;
2675
- encodingStart = 0;
2676
- } else
2677
- refPosition = newPosition;
2678
- if (size === 2) {
2679
- targetView.setUint16(position, refOffset, true);
2680
- position += 2;
2681
- } else {
2682
- targetView.setUint32(position, refOffset, true);
2683
- position += 4;
2684
- }
2685
- } else { // null or undefined
2686
- transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
2687
- targetView.setInt16(position, value === null ? -10 : -9, true);
2688
- position += 2;
2689
- }
2690
- keyIndex++;
2691
- }
2692
-
2693
-
2694
- let recordId = transition[RECORD_SYMBOL];
2695
- if (recordId == null) {
2696
- recordId = packr.typedStructs.length;
2697
- let structure = [];
2698
- let nextTransition = transition;
2699
- let key, type;
2700
- while ((type = nextTransition.__type) !== undefined) {
2701
- let size = nextTransition.__size;
2702
- nextTransition = nextTransition.__parent;
2703
- key = nextTransition.key;
2704
- let property = [type, size, key];
2705
- if (nextTransition.enumerationOffset)
2706
- property.push(nextTransition.enumerationOffset);
2707
- structure.push(property);
2708
- nextTransition = nextTransition.parent;
2709
- }
2710
- structure.reverse();
2711
- transition[RECORD_SYMBOL] = recordId;
2712
- packr.typedStructs[recordId] = structure;
2713
- pack(null, 0, true); // special call to notify that structures have been updated
2714
- }
2715
-
2716
-
2717
- switch (headerSize) {
2718
- case 1:
2719
- if (recordId >= 0x10) return 0;
2720
- target[start] = recordId + 0x20;
2721
- break;
2722
- case 2:
2723
- if (recordId >= 0x100) return 0;
2724
- target[start] = 0x38;
2725
- target[start + 1] = recordId;
2726
- break;
2727
- case 3:
2728
- if (recordId >= 0x10000) return 0;
2729
- target[start] = 0x39;
2730
- targetView.setUint16(start + 1, recordId, true);
2731
- break;
2732
- case 4:
2733
- if (recordId >= 0x1000000) return 0;
2734
- targetView.setUint32(start, (recordId << 8) + 0x3a, true);
2735
- break;
2736
- }
2737
-
2738
- if (position < refsStartPosition) {
2739
- if (refsStartPosition === refPosition)
2740
- return position; // no refs
2741
- // adjust positioning
2742
- target.copyWithin(position, refsStartPosition, refPosition);
2743
- refPosition += position - refsStartPosition;
2744
- typedStructs.lastStringStart = position - start;
2745
- } else if (position > refsStartPosition) {
2746
- if (refsStartPosition === refPosition)
2747
- return position; // no refs
2748
- typedStructs.lastStringStart = position - start;
2749
- return writeStruct(object, target, encodingStart, start, structures, makeRoom, pack, packr);
2750
- }
2751
- return refPosition;
2752
- }
2753
- function anyType(transition, position, targetView, value) {
2754
- let nextTransition;
2755
- if ((nextTransition = transition.ascii8 || transition.num8)) {
2756
- targetView.setInt8(position, value, true);
2757
- updatedPosition = position + 1;
2758
- return nextTransition;
2759
- }
2760
- if ((nextTransition = transition.string16 || transition.object16)) {
2761
- targetView.setInt16(position, value, true);
2762
- updatedPosition = position + 2;
2763
- return nextTransition;
2764
- }
2765
- if (nextTransition = transition.num32) {
2766
- targetView.setUint32(position, 0xe0000100 + value, true);
2767
- updatedPosition = position + 4;
2768
- return nextTransition;
2769
- }
2770
- // transition.float64
2771
- if (nextTransition = transition.num64) {
2772
- targetView.setFloat64(position, NaN, true);
2773
- targetView.setInt8(position, value);
2774
- updatedPosition = position + 8;
2775
- return nextTransition;
2776
- }
2777
- updatedPosition = position;
2778
- // TODO: can we do an "any" type where we defer the decision?
2779
- return;
2780
- }
2781
- function createTypeTransition(transition, type, size) {
2782
- let typeName = TYPE_NAMES[type] + (size << 3);
2783
- let newTransition = transition[typeName] || (transition[typeName] = Object.create(null));
2784
- newTransition.__type = type;
2785
- newTransition.__size = size;
2786
- newTransition.__parent = transition;
2787
- return newTransition;
2788
- }
2789
- function onLoadedStructures(sharedData) {
2790
- if (!(sharedData instanceof Map))
2791
- return sharedData;
2792
- let typed = sharedData.get('typed') || [];
2793
- if (Object.isFrozen(typed))
2794
- typed = typed.map(structure => structure.slice(0));
2795
- let named = sharedData.get('named');
2796
- let transitions = Object.create(null);
2797
- for (let i = 0, l = typed.length; i < l; i++) {
2798
- let structure = typed[i];
2799
- let transition = transitions;
2800
- for (let [type, size, key] of structure) {
2801
- let nextTransition = transition[key];
2802
- if (!nextTransition) {
2803
- transition[key] = nextTransition = {
2804
- key,
2805
- parent: transition,
2806
- enumerationOffset: 0,
2807
- ascii0: null,
2808
- ascii8: null,
2809
- num8: null,
2810
- string16: null,
2811
- object16: null,
2812
- num32: null,
2813
- float64: null,
2814
- date64: null,
2815
- };
2816
- }
2817
- transition = createTypeTransition(nextTransition, type, size);
2818
- }
2819
- transition[RECORD_SYMBOL] = i;
2820
- }
2821
- typed.transitions = transitions;
2822
- this.typedStructs = typed;
2823
- this.lastTypedStructuresLength = typed.length;
2824
- return named;
2825
- }
2826
- var sourceSymbol = Symbol.for('source');
2827
- function readStruct(src, position, srcEnd, unpackr) {
2828
- let recordId = src[position++] - 0x20;
2829
- if (recordId >= 24) {
2830
- switch(recordId) {
2831
- case 24: recordId = src[position++]; break;
2832
- // little endian:
2833
- case 25: recordId = src[position++] + (src[position++] << 8); break;
2834
- case 26: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16); break;
2835
- case 27: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16) + (src[position++] << 24); break;
2836
- }
2837
- }
2838
- let structure = unpackr.typedStructs && unpackr.typedStructs[recordId];
2839
- if (!structure) {
2840
- // copy src buffer because getStructures will override it
2841
- src = Uint8Array.prototype.slice.call(src, position, srcEnd);
2842
- srcEnd -= position;
2843
- position = 0;
2844
- if (!unpackr.getStructures)
2845
- throw new Error(`Reference to shared structure ${recordId} without getStructures method`);
2846
- unpackr._mergeStructures(unpackr.getStructures());
2847
- if (!unpackr.typedStructs)
2848
- throw new Error('Could not find any shared typed structures');
2849
- unpackr.lastTypedStructuresLength = unpackr.typedStructs.length;
2850
- structure = unpackr.typedStructs[recordId];
2851
- if (!structure)
2852
- throw new Error('Could not find typed structure ' + recordId);
2853
- }
2854
- var construct = structure.construct;
2855
- var fullConstruct = structure.fullConstruct;
2856
- if (!construct) {
2857
- construct = structure.construct = function LazyObject() {
2858
- };
2859
- fullConstruct = structure.fullConstruct = function LoadedObject() {
2860
- };
2861
- fullConstruct.prototype = unpackr.structPrototype || {};
2862
- var prototype = construct.prototype = unpackr.structPrototype ? Object.create(unpackr.structPrototype) : {};
2863
- let properties = [];
2864
- let currentOffset = 0;
2865
- let lastRefProperty;
2866
- for (let i = 0, l = structure.length; i < l; i++) {
2867
- let definition = structure[i];
2868
- let [ type, size, key, enumerationOffset ] = definition;
2869
- if (key === '__proto__')
2870
- key = '__proto_';
2871
- let property = {
2872
- key,
2873
- offset: currentOffset,
2874
- };
2875
- if (enumerationOffset)
2876
- properties.splice(i + enumerationOffset, 0, property);
2877
- else
2878
- properties.push(property);
2879
- let getRef;
2880
- switch(size) { // TODO: Move into a separate function
2881
- case 0: getRef = () => 0; break;
2882
- case 1:
2883
- getRef = (source, position) => {
2884
- let ref = source.bytes[position + property.offset];
2885
- return ref >= 0xf6 ? toConstant(ref) : ref;
2886
- };
2887
- break;
2888
- case 2:
2889
- getRef = (source, position) => {
2890
- let src = source.bytes;
2891
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2892
- let ref = dataView.getUint16(position + property.offset, true);
2893
- return ref >= 0xff00 ? toConstant(ref & 0xff) : ref;
2894
- };
2895
- break;
2896
- case 4:
2897
- getRef = (source, position) => {
2898
- let src = source.bytes;
2899
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2900
- let ref = dataView.getUint32(position + property.offset, true);
2901
- return ref >= 0xffffff00 ? toConstant(ref & 0xff) : ref;
2902
- };
2903
- break;
2904
- }
2905
- property.getRef = getRef;
2906
- currentOffset += size;
2907
- let get;
2908
- switch(type) {
2909
- case ASCII:
2910
- if (lastRefProperty && !lastRefProperty.next)
2911
- lastRefProperty.next = property;
2912
- lastRefProperty = property;
2913
- property.multiGetCount = 0;
2914
- get = function(source) {
2915
- let src = source.bytes;
2916
- let position = source.position;
2917
- let refStart = currentOffset + position;
2918
- let ref = getRef(source, position);
2919
- if (typeof ref !== 'number') return ref;
2920
-
2921
- let end, next = property.next;
2922
- while(next) {
2923
- end = next.getRef(source, position);
2924
- if (typeof end === 'number')
2925
- break;
2926
- else
2927
- end = null;
2928
- next = next.next;
2929
- }
2930
- if (end == null)
2931
- end = source.bytesEnd - refStart;
2932
- if (source.srcString) {
2933
- return source.srcString.slice(ref, end);
2934
- }
2935
- /*if (property.multiGetCount > 0) {
2936
- let asciiEnd;
2937
- next = firstRefProperty;
2938
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2939
- do {
2940
- asciiEnd = dataView.getUint16(source.position + next.offset, true);
2941
- if (asciiEnd < 0xff00)
2942
- break;
2943
- else
2944
- asciiEnd = null;
2945
- } while((next = next.next));
2946
- if (asciiEnd == null)
2947
- asciiEnd = source.bytesEnd - refStart
2948
- source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
2949
- return source.srcString.slice(ref, end);
2950
- }
2951
- if (source.prevStringGet) {
2952
- source.prevStringGet.multiGetCount += 2;
2953
- } else {
2954
- source.prevStringGet = property;
2955
- property.multiGetCount--;
2956
- }*/
2957
- return readString(src, ref + refStart, end - ref);
2958
- //return src.toString('latin1', ref + refStart, end + refStart);
2959
- };
2960
- break;
2961
- case UTF8: case OBJECT_DATA:
2962
- if (lastRefProperty && !lastRefProperty.next)
2963
- lastRefProperty.next = property;
2964
- lastRefProperty = property;
2965
- get = function(source) {
2966
- let position = source.position;
2967
- let refStart = currentOffset + position;
2968
- let ref = getRef(source, position);
2969
- if (typeof ref !== 'number') return ref;
2970
- let src = source.bytes;
2971
- let end, next = property.next;
2972
- while(next) {
2973
- end = next.getRef(source, position);
2974
- if (typeof end === 'number')
2975
- break;
2976
- else
2977
- end = null;
2978
- next = next.next;
2979
- }
2980
- if (end == null)
2981
- end = source.bytesEnd - refStart;
2982
- if (type === UTF8) {
2983
- return src.toString('utf8', ref + refStart, end + refStart);
2984
- } else {
2985
- currentSource = source;
2986
- try {
2987
- return unpackr.unpack(src, { start: ref + refStart, end: end + refStart });
2988
- } finally {
2989
- currentSource = null;
2990
- }
2991
- }
2992
- };
2993
- break;
2994
- case NUMBER:
2995
- switch(size) {
2996
- case 4:
2997
- get = function (source) {
2998
- let src = source.bytes;
2999
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
3000
- let position = source.position + property.offset;
3001
- let value = dataView.getInt32(position, true);
3002
- if (value < 0x20000000) {
3003
- if (value > -0x1f000000)
3004
- return value;
3005
- if (value > -0x20000000)
3006
- return toConstant(value & 0xff);
3007
- }
3008
- let fValue = dataView.getFloat32(position, true);
3009
- // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
3010
- let multiplier = mult10[((src[position + 3] & 0x7f) << 1) | (src[position + 2] >> 7)];
3011
- return ((multiplier * fValue + (fValue > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
3012
- };
3013
- break;
3014
- case 8:
3015
- get = function (source) {
3016
- let src = source.bytes;
3017
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
3018
- let value = dataView.getFloat64(source.position + property.offset, true);
3019
- if (isNaN(value)) {
3020
- let byte = src[source.position + property.offset];
3021
- if (byte >= 0xf6)
3022
- return toConstant(byte);
3023
- }
3024
- return value;
3025
- };
3026
- break;
3027
- case 1:
3028
- get = function (source) {
3029
- let src = source.bytes;
3030
- let value = src[source.position + property.offset];
3031
- return value < 0xf6 ? value : toConstant(value);
3032
- };
3033
- break;
3034
- }
3035
- break;
3036
- case DATE:
3037
- get = function (source) {
3038
- let src = source.bytes;
3039
- let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
3040
- return new Date(dataView.getFloat64(source.position + property.offset, true));
3041
- };
3042
- break;
3043
-
3044
- }
3045
- property.get = get;
3046
- }
3047
- // TODO: load the srcString for faster string decoding on toJSON
3048
- if (evalSupported) {
3049
- let objectLiteralProperties = [];
3050
- let args = [];
3051
- let i = 0;
3052
- let hasInheritedProperties;
3053
- for (let property of properties) { // assign in enumeration order
3054
- if (unpackr.alwaysLazyProperty && unpackr.alwaysLazyProperty(property.key)) {
3055
- // these properties are not eagerly evaluated and this can be used for creating properties
3056
- // that are not serialized as JSON
3057
- hasInheritedProperties = true;
3058
- continue;
3059
- }
3060
- Object.defineProperty(prototype, property.key, { get: withSource(property.get), enumerable: true });
3061
- let valueFunction = 'v' + i++;
3062
- args.push(valueFunction);
3063
- objectLiteralProperties.push('o[' + JSON.stringify(property.key) + ']=' + valueFunction + '(s)');
3064
- }
3065
- if (hasInheritedProperties) {
3066
- objectLiteralProperties.push('__proto__:this');
3067
- }
3068
- let toObject = (new Function(...args, 'var c=this;return function(s){var o=new c();' + objectLiteralProperties.join(';') + ';return o;}')).apply(fullConstruct, properties.map(prop => prop.get));
3069
- Object.defineProperty(prototype, 'toJSON', {
3070
- value(omitUnderscoredProperties) {
3071
- return toObject.call(this, this[sourceSymbol]);
3072
- }
3073
- });
3074
- } else {
3075
- Object.defineProperty(prototype, 'toJSON', {
3076
- value(omitUnderscoredProperties) {
3077
- // return an enumerable object with own properties to JSON stringify
3078
- let resolved = {};
3079
- for (let i = 0, l = properties.length; i < l; i++) {
3080
- // TODO: check alwaysLazyProperty
3081
- let key = properties[i].key;
3082
-
3083
- resolved[key] = this[key];
3084
- }
3085
- return resolved;
3086
- },
3087
- // not enumerable or anything
3088
- });
3089
- }
3090
- }
3091
- var instance = new construct();
3092
- instance[sourceSymbol] = {
3093
- bytes: src,
3094
- position,
3095
- srcString: '',
3096
- bytesEnd: srcEnd
3097
- };
3098
- return instance;
3099
- }
3100
- function toConstant(code) {
3101
- switch(code) {
3102
- case 0xf6: return null;
3103
- case 0xf7: return undefined;
3104
- case 0xf8: return false;
3105
- case 0xf9: return true;
3106
- }
3107
- throw new Error('Unknown constant');
3108
- }
3109
- function withSource(get) {
3110
- return function() {
3111
- return get(this[sourceSymbol]);
3112
- }
3113
- }
3114
-
3115
- function saveState() {
3116
- if (currentSource) {
3117
- currentSource.bytes = Uint8Array.prototype.slice.call(currentSource.bytes, currentSource.position, currentSource.bytesEnd);
3118
- currentSource.position = 0;
3119
- currentSource.bytesEnd = currentSource.bytes.length;
3120
- }
3121
- }
3122
- function prepareStructures(structures, packr) {
3123
- if (packr.typedStructs) {
3124
- let structMap = new Map();
3125
- structMap.set('named', structures);
3126
- structMap.set('typed', packr.typedStructs);
3127
- structures = structMap;
3128
- }
3129
- let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
3130
- structures.isCompatible = existing => {
3131
- let compatible = true;
3132
- if (existing instanceof Map) {
3133
- let named = existing.get('named') || [];
3134
- if (named.length !== (packr.lastNamedStructuresLength || 0))
3135
- compatible = false;
3136
- let typed = existing.get('typed') || [];
3137
- if (typed.length !== lastTypedStructuresLength)
3138
- compatible = false;
3139
- } else if (existing instanceof Array || Array.isArray(existing)) {
3140
- if (existing.length !== (packr.lastNamedStructuresLength || 0))
3141
- compatible = false;
3142
- }
3143
- if (!compatible)
3144
- packr._mergeStructures(existing);
3145
- return compatible;
3146
- };
3147
- packr.lastTypedStructuresLength = packr.typedStructs && packr.typedStructs.length;
3148
- return structures;
3149
- }
3150
-
3151
- setReadStruct(readStruct, onLoadedStructures, saveState);
3152
-
3153
2369
  class PackrStream extends stream.Transform {
3154
2370
  constructor(options) {
3155
2371
  if (!options)
@@ -3188,12 +2404,12 @@ class UnpackrStream extends stream.Transform {
3188
2404
  let incompleteBuffer = chunk.slice(error.lastPosition);
3189
2405
  if (incompleteBuffer.length > this.maxIncompleteBufferSize) {
3190
2406
  this.incompleteBuffer = null;
3191
- return callback(new Error('Maximum incomplete buffer size exceeded'))
2407
+ return callback(new Error('Maximum incomplete buffer size exceeded'));
3192
2408
  }
3193
2409
  this.incompleteBuffer = incompleteBuffer;
3194
2410
  values = error.values;
3195
2411
  } else {
3196
- return callback(error)
2412
+ return callback(error);
3197
2413
  }
3198
2414
  }
3199
2415
  for (let value of values || []) {
@@ -3204,7 +2420,7 @@ class UnpackrStream extends stream.Transform {
3204
2420
  callback();
3205
2421
  }
3206
2422
  getNullValue() {
3207
- return Symbol.for(null)
2423
+ return Symbol.for(null);
3208
2424
  }
3209
2425
  }
3210
2426
 
@@ -3217,13 +2433,13 @@ class UnpackrStream extends stream.Transform {
3217
2433
  */
3218
2434
  function packIter (objectIterator, options = {}) {
3219
2435
  if (!objectIterator || typeof objectIterator !== 'object') {
3220
- throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
2436
+ throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable');
3221
2437
  } else if (typeof objectIterator[Symbol.iterator] === 'function') {
3222
- return packIterSync(objectIterator, options)
2438
+ return packIterSync(objectIterator, options);
3223
2439
  } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
3224
- return packIterAsync(objectIterator, options)
2440
+ return packIterAsync(objectIterator, options);
3225
2441
  } else {
3226
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
2442
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise');
3227
2443
  }
3228
2444
  }
3229
2445
 
@@ -3250,7 +2466,7 @@ async function * packIterAsync (objectIterator, options) {
3250
2466
  */
3251
2467
  function unpackIter (bufferIterator, options = {}) {
3252
2468
  if (!bufferIterator || typeof bufferIterator !== 'object') {
3253
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
2469
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise');
3254
2470
  }
3255
2471
 
3256
2472
  const unpackr = new Unpackr(options);
@@ -3270,10 +2486,10 @@ function unpackIter (bufferIterator, options = {}) {
3270
2486
  incomplete = chunk.slice(err.lastPosition);
3271
2487
  yields = err.values;
3272
2488
  } else {
3273
- throw err
2489
+ throw err;
3274
2490
  }
3275
2491
  }
3276
- return yields
2492
+ return yields;
3277
2493
  };
3278
2494
 
3279
2495
  if (typeof bufferIterator[Symbol.iterator] === 'function') {
@@ -3281,13 +2497,13 @@ function unpackIter (bufferIterator, options = {}) {
3281
2497
  for (const value of bufferIterator) {
3282
2498
  yield * parser(value);
3283
2499
  }
3284
- })()
2500
+ })();
3285
2501
  } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
3286
2502
  return (async function * iter () {
3287
2503
  for await (const value of bufferIterator) {
3288
2504
  yield * parser(value);
3289
2505
  }
3290
- })()
2506
+ })();
3291
2507
  }
3292
2508
  }
3293
2509
  const decodeIter = unpackIter;