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.
@@ -32,7 +32,6 @@
32
32
  C1.name = 'MessagePack 0xC1';
33
33
  var sequentialMode = false;
34
34
  var inlineObjectReadThreshold = 2;
35
- var readStruct, onLoadedStructures, onSaveState;
36
35
  var BlockedFunction; // we use search and replace to change the next call to BlockedFunction to avoid CSP issues for
37
36
 
38
37
  class Unpackr {
@@ -65,8 +64,8 @@
65
64
  // re-entrant execution, save the state and restore it after we do this unpack
66
65
  return saveState(() => {
67
66
  clearSource();
68
- return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
69
- })
67
+ return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options);
68
+ });
70
69
  }
71
70
  if (!source.buffer && source.constructor === ArrayBuffer)
72
71
  source = typeof Buffer !== 'undefined' ? Buffer.from(source) : new Uint8Array(source);
@@ -92,14 +91,14 @@
92
91
  // if it doesn't have a buffer, maybe it is the wrong type of object
93
92
  src = null;
94
93
  if (source instanceof Uint8Array)
95
- throw error
96
- throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
94
+ throw error;
95
+ throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source));
97
96
  }
98
97
  if (this instanceof Unpackr) {
99
98
  currentUnpackr = this;
100
99
  if (this.structures) {
101
100
  currentStructures = this.structures;
102
- return checkedRead(options)
101
+ return checkedRead(options);
103
102
  } else if (!currentStructures || currentStructures.length > 0) {
104
103
  currentStructures = [];
105
104
  }
@@ -108,7 +107,7 @@
108
107
  if (!currentStructures || currentStructures.length > 0)
109
108
  currentStructures = [];
110
109
  }
111
- return checkedRead(options)
110
+ return checkedRead(options);
112
111
  }
113
112
  unpackMultiple(source, forEach) {
114
113
  let values, lastPosition = 0;
@@ -121,7 +120,7 @@
121
120
  while(position < size) {
122
121
  lastPosition = position;
123
122
  if (forEach(checkedRead(), lastPosition, position) === false) {
124
- return
123
+ return;
125
124
  }
126
125
  }
127
126
  }
@@ -131,20 +130,20 @@
131
130
  lastPosition = position;
132
131
  values.push(checkedRead());
133
132
  }
134
- return values
133
+ return values;
135
134
  }
136
135
  } catch(error) {
137
136
  error.lastPosition = lastPosition;
138
137
  error.values = values;
139
- throw error
138
+ throw error;
140
139
  } finally {
141
140
  sequentialMode = false;
142
141
  clearSource();
143
142
  }
144
143
  }
145
144
  _mergeStructures(loadedStructures, existingStructures) {
146
- if (onLoadedStructures)
147
- loadedStructures = onLoadedStructures.call(this, loadedStructures);
145
+ if (this._onLoadedStructures)
146
+ loadedStructures = this._onLoadedStructures(loadedStructures);
148
147
  loadedStructures = loadedStructures || [];
149
148
  if (Object.isFrozen(loadedStructures))
150
149
  loadedStructures = loadedStructures.map(structure => structure.slice(0));
@@ -168,14 +167,14 @@
168
167
  }
169
168
  }
170
169
  }
171
- return this.structures = loadedStructures
170
+ return this.structures = loadedStructures;
172
171
  }
173
172
  decode(source, options) {
174
- return this.unpack(source, options)
173
+ return this.unpack(source, options);
175
174
  }
176
175
  }
177
176
  function getPosition() {
178
- return position
177
+ return position;
179
178
  }
180
179
  function checkedRead(options) {
181
180
  try {
@@ -185,8 +184,8 @@
185
184
  currentStructures.length = sharedLength;
186
185
  }
187
186
  let result;
188
- if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
189
- result = readStruct(src, position, srcEnd, currentUnpackr);
187
+ if (currentUnpackr._readStruct && src[position] < 0x40 && src[position] >= 0x20) {
188
+ result = currentUnpackr._readStruct(src, position, srcEnd);
190
189
  src = null; // dispose of this so that recursive unpack calls don't save state
191
190
  if (!(options && options.lazy) && result)
192
191
  result = result.toJSON();
@@ -212,7 +211,7 @@
212
211
  referenceMap = null;
213
212
  } else if (position > srcEnd) {
214
213
  // over read
215
- throw new Error('Unexpected end of MessagePack data')
214
+ throw new Error('Unexpected end of MessagePack data');
216
215
  } else if (!sequentialMode) {
217
216
  let jsonView;
218
217
  try {
@@ -220,10 +219,10 @@
220
219
  } catch(error) {
221
220
  jsonView = '(JSON view not available ' + error + ')';
222
221
  }
223
- throw new Error('Data read, but end of buffer not reached ' + jsonView)
222
+ throw new Error('Data read, but end of buffer not reached ' + jsonView);
224
223
  }
225
224
  // else more to read, but we are reading sequentially, so don't clear source yet
226
- return result
225
+ return result;
227
226
  } catch(error) {
228
227
  if (currentStructures && currentStructures.restoreStructures)
229
228
  restoreStructures();
@@ -231,7 +230,7 @@
231
230
  if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
232
231
  error.incomplete = true;
233
232
  }
234
- throw error
233
+ throw error;
235
234
  }
236
235
  }
237
236
 
@@ -247,7 +246,7 @@
247
246
  if (token < 0xa0) {
248
247
  if (token < 0x80) {
249
248
  if (token < 0x40)
250
- return token
249
+ return token;
251
250
  else {
252
251
  let structure = currentStructures[token & 0x3f] ||
253
252
  currentUnpackr.getStructures && loadStructures()[token & 0x3f];
@@ -255,9 +254,9 @@
255
254
  if (!structure.read) {
256
255
  structure.read = createStructureReader(structure, token & 0x3f);
257
256
  }
258
- return structure.read()
257
+ return structure.read();
259
258
  } else
260
- return token
259
+ return token;
261
260
  }
262
261
  } else if (token < 0x90) {
263
262
  // map
@@ -270,13 +269,13 @@
270
269
  key = '__proto_';
271
270
  object[key] = read();
272
271
  }
273
- return object
272
+ return object;
274
273
  } else {
275
274
  let map = new Map();
276
275
  for (let i = 0; i < token; i++) {
277
276
  map.set(read(), read());
278
277
  }
279
- return map
278
+ return map;
280
279
  }
281
280
  } else {
282
281
  token -= 0x90;
@@ -285,91 +284,91 @@
285
284
  array[i] = read();
286
285
  }
287
286
  if (currentUnpackr.freezeData)
288
- return Object.freeze(array)
289
- return array
287
+ return Object.freeze(array);
288
+ return array;
290
289
  }
291
290
  } else if (token < 0xc0) {
292
291
  // fixstr
293
292
  let length = token - 0xa0;
294
293
  if (srcStringEnd >= position) {
295
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
294
+ return srcString.slice(position - srcStringStart, (position += length) - srcStringStart);
296
295
  }
297
296
  if (srcStringEnd == 0 && srcEnd < 140) {
298
297
  // for small blocks, avoiding the overhead of the extract call is helpful
299
298
  let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
300
299
  if (string != null)
301
- return string
300
+ return string;
302
301
  }
303
- return readFixedString(length)
302
+ return readFixedString(length);
304
303
  } else {
305
304
  let value;
306
305
  switch (token) {
307
- case 0xc0: return null
306
+ case 0xc0: return null;
308
307
  case 0xc1:
309
308
  if (bundledStrings) {
310
309
  value = read(); // followed by the length of the string in characters (not bytes!)
311
310
  if (value > 0)
312
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
311
+ return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value);
313
312
  else
314
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
313
+ return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value);
315
314
  }
316
315
  return C1; // "never-used", return special object to denote that
317
- case 0xc2: return false
318
- case 0xc3: return true
316
+ case 0xc2: return false;
317
+ case 0xc3: return true;
319
318
  case 0xc4:
320
319
  // bin 8
321
320
  value = src[position++];
322
321
  if (value === undefined)
323
- throw new Error('Unexpected end of buffer')
324
- return readBin(value)
322
+ throw new Error('Unexpected end of buffer');
323
+ return readBin(value);
325
324
  case 0xc5:
326
325
  // bin 16
327
326
  value = dataView.getUint16(position);
328
327
  position += 2;
329
- return readBin(value)
328
+ return readBin(value);
330
329
  case 0xc6:
331
330
  // bin 32
332
331
  value = dataView.getUint32(position);
333
332
  position += 4;
334
- return readBin(value)
333
+ return readBin(value);
335
334
  case 0xc7:
336
335
  // ext 8
337
- return readExt(src[position++])
336
+ return readExt(src[position++]);
338
337
  case 0xc8:
339
338
  // ext 16
340
339
  value = dataView.getUint16(position);
341
340
  position += 2;
342
- return readExt(value)
341
+ return readExt(value);
343
342
  case 0xc9:
344
343
  // ext 32
345
344
  value = dataView.getUint32(position);
346
345
  position += 4;
347
- return readExt(value)
346
+ return readExt(value);
348
347
  case 0xca:
349
348
  value = dataView.getFloat32(position);
350
349
  if (currentUnpackr.useFloat32 > 2) {
351
350
  // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
352
351
  let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
353
352
  position += 4;
354
- return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
353
+ return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
355
354
  }
356
355
  position += 4;
357
- return value
356
+ return value;
358
357
  case 0xcb:
359
358
  value = dataView.getFloat64(position);
360
359
  position += 8;
361
- return value
360
+ return value;
362
361
  // uint handlers
363
362
  case 0xcc:
364
- return src[position++]
363
+ return src[position++];
365
364
  case 0xcd:
366
365
  value = dataView.getUint16(position);
367
366
  position += 2;
368
- return value
367
+ return value;
369
368
  case 0xce:
370
369
  value = dataView.getUint32(position);
371
370
  position += 4;
372
- return value
371
+ return value;
373
372
  case 0xcf:
374
373
  if (currentUnpackr.int64AsType === 'number') {
375
374
  value = dataView.getUint32(position) * 0x100000000;
@@ -382,19 +381,19 @@
382
381
  } else
383
382
  value = dataView.getBigUint64(position);
384
383
  position += 8;
385
- return value
384
+ return value;
386
385
 
387
386
  // int handlers
388
387
  case 0xd0:
389
- return dataView.getInt8(position++)
388
+ return dataView.getInt8(position++);
390
389
  case 0xd1:
391
390
  value = dataView.getInt16(position);
392
391
  position += 2;
393
- return value
392
+ return value;
394
393
  case 0xd2:
395
394
  value = dataView.getInt32(position);
396
395
  position += 4;
397
- return value
396
+ return value;
398
397
  case 0xd3:
399
398
  if (currentUnpackr.int64AsType === 'number') {
400
399
  value = dataView.getInt32(position) * 0x100000000;
@@ -407,96 +406,96 @@
407
406
  } else
408
407
  value = dataView.getBigInt64(position);
409
408
  position += 8;
410
- return value
409
+ return value;
411
410
 
412
411
  case 0xd4:
413
412
  // fixext 1
414
413
  value = src[position++];
415
414
  if (value == 0x72) {
416
- return recordDefinition(src[position++] & 0x3f)
415
+ return recordDefinition(src[position++] & 0x3f);
417
416
  } else {
418
417
  let extension = currentExtensions[value];
419
418
  if (extension) {
420
419
  if (extension.read) {
421
420
  position++; // skip filler byte
422
- return extension.read(read())
421
+ return extension.read(read());
423
422
  } else if (extension.noBuffer) {
424
423
  position++; // skip filler byte
425
- return extension()
424
+ return extension();
426
425
  } else
427
- return extension(src.subarray(position, ++position))
426
+ return extension(src.subarray(position, ++position));
428
427
  } else
429
- throw new Error('Unknown extension ' + value)
428
+ throw new Error('Unknown extension ' + value);
430
429
  }
431
430
  case 0xd5:
432
431
  // fixext 2
433
432
  value = src[position];
434
433
  if (value == 0x72) {
435
434
  position++;
436
- return recordDefinition(src[position++] & 0x3f, src[position++])
435
+ return recordDefinition(src[position++] & 0x3f, src[position++]);
437
436
  } else
438
- return readExt(2)
437
+ return readExt(2);
439
438
  case 0xd6:
440
439
  // fixext 4
441
- return readExt(4)
440
+ return readExt(4);
442
441
  case 0xd7:
443
442
  // fixext 8
444
- return readExt(8)
443
+ return readExt(8);
445
444
  case 0xd8:
446
445
  // fixext 16
447
- return readExt(16)
446
+ return readExt(16);
448
447
  case 0xd9:
449
448
  // str 8
450
449
  value = src[position++];
451
450
  if (srcStringEnd >= position) {
452
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
451
+ return srcString.slice(position - srcStringStart, (position += value) - srcStringStart);
453
452
  }
454
- return readString8(value)
453
+ return readString8(value);
455
454
  case 0xda:
456
455
  // str 16
457
456
  value = dataView.getUint16(position);
458
457
  position += 2;
459
458
  if (srcStringEnd >= position) {
460
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
459
+ return srcString.slice(position - srcStringStart, (position += value) - srcStringStart);
461
460
  }
462
- return readString16(value)
461
+ return readString16(value);
463
462
  case 0xdb:
464
463
  // str 32
465
464
  value = dataView.getUint32(position);
466
465
  position += 4;
467
466
  if (srcStringEnd >= position) {
468
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
467
+ return srcString.slice(position - srcStringStart, (position += value) - srcStringStart);
469
468
  }
470
- return readString32(value)
469
+ return readString32(value);
471
470
  case 0xdc:
472
471
  // array 16
473
472
  value = dataView.getUint16(position);
474
473
  position += 2;
475
- return readArray(value)
474
+ return readArray(value);
476
475
  case 0xdd:
477
476
  // array 32
478
477
  value = dataView.getUint32(position);
479
478
  position += 4;
480
- return readArray(value)
479
+ return readArray(value);
481
480
  case 0xde:
482
481
  // map 16
483
482
  value = dataView.getUint16(position);
484
483
  position += 2;
485
- return readMap(value)
484
+ return readMap(value);
486
485
  case 0xdf:
487
486
  // map 32
488
487
  value = dataView.getUint32(position);
489
488
  position += 4;
490
- return readMap(value)
489
+ return readMap(value);
491
490
  default: // negative int
492
491
  if (token >= 0xe0)
493
- return token - 0x100
492
+ return token - 0x100;
494
493
  if (token === undefined) {
495
494
  let error = new Error('Unexpected end of MessagePack data');
496
495
  error.incomplete = true;
497
- throw error
496
+ throw error;
498
497
  }
499
- throw new Error('Unknown MessagePack token ' + token)
498
+ throw new Error('Unknown MessagePack token ' + token);
500
499
 
501
500
  }
502
501
  }
@@ -517,7 +516,7 @@
517
516
  }
518
517
  if (structure.highByte === 0)
519
518
  structure.read = createSecondByteReader(firstId, structure.read);
520
- return optimizedReadObject() // second byte is already read, if there is one so immediately read object
519
+ return optimizedReadObject(); // second byte is already read, if there is one so immediately read object
521
520
  }
522
521
  let object = {};
523
522
  for (let i = 0, l = structure.length; i < l; i++) {
@@ -528,38 +527,38 @@
528
527
  }
529
528
  if (currentUnpackr.freezeData)
530
529
  return Object.freeze(object);
531
- return object
530
+ return object;
532
531
  }
533
532
  readObject.count = 0;
534
533
  if (structure.highByte === 0) {
535
- return createSecondByteReader(firstId, readObject)
534
+ return createSecondByteReader(firstId, readObject);
536
535
  }
537
- return readObject
536
+ return readObject;
538
537
  }
539
538
 
540
539
  const createSecondByteReader = (firstId, read0) => {
541
540
  return function() {
542
541
  let highByte = src[position++];
543
542
  if (highByte === 0)
544
- return read0()
543
+ return read0();
545
544
  let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
546
545
  let structure = currentStructures[id] || loadStructures()[id];
547
546
  if (!structure) {
548
- throw new Error('Record id is not defined for ' + id)
547
+ throw new Error('Record id is not defined for ' + id);
549
548
  }
550
549
  if (!structure.read)
551
550
  structure.read = createStructureReader(structure, firstId);
552
- return structure.read()
553
- }
551
+ return structure.read();
552
+ };
554
553
  };
555
554
 
556
555
  function loadStructures() {
557
556
  let loadedStructures = saveState(() => {
558
557
  // save the state in case getStructures modifies our buffer
559
558
  src = null;
560
- return currentUnpackr.getStructures()
559
+ return currentUnpackr.getStructures();
561
560
  });
562
- return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
561
+ return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures);
563
562
  }
564
563
 
565
564
  var readFixedString = readStringJS;
@@ -579,7 +578,7 @@
579
578
  let string = strings[stringPosition++];
580
579
  if (string == null) {
581
580
  if (bundledStrings)
582
- return readStringJS(length)
581
+ return readStringJS(length);
583
582
  let byteOffset = src.byteOffset;
584
583
  let extraction = extractStrings(position - headerLength + byteOffset, srcEnd + byteOffset, src.buffer);
585
584
  if (typeof extraction == 'string') {
@@ -591,30 +590,30 @@
591
590
  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
592
591
  string = strings[0];
593
592
  if (string === undefined)
594
- throw new Error('Unexpected end of buffer')
593
+ throw new Error('Unexpected end of buffer');
595
594
  }
596
595
  }
597
596
  let srcStringLength = string.length;
598
597
  if (srcStringLength <= length) {
599
598
  position += length;
600
- return string
599
+ return string;
601
600
  }
602
601
  srcString = string;
603
602
  srcStringStart = position;
604
603
  srcStringEnd = position + srcStringLength;
605
604
  position += length;
606
- return string.slice(0, length) // we know we just want the beginning
607
- }
605
+ return string.slice(0, length); // we know we just want the beginning
606
+ };
608
607
  }
609
608
  }
610
609
  function readStringJS(length) {
611
610
  let result;
612
611
  if (length < 16) {
613
612
  if (result = shortStringInJS(length))
614
- return result
613
+ return result;
615
614
  }
616
615
  if (length > 64 && decoder)
617
- return decoder.decode(src.subarray(position, position += length))
616
+ return decoder.decode(src.subarray(position, position += length));
618
617
  const end = position + length;
619
618
  const units = [];
620
619
  result = '';
@@ -677,7 +676,7 @@
677
676
  result += fromCharCode.apply(String, units);
678
677
  }
679
678
 
680
- return result
679
+ return result;
681
680
  }
682
681
  function readString(source, start, length) {
683
682
  let existingSrc = src;
@@ -696,8 +695,8 @@
696
695
  array[i] = read();
697
696
  }
698
697
  if (currentUnpackr.freezeData)
699
- return Object.freeze(array)
700
- return array
698
+ return Object.freeze(array);
699
+ return array;
701
700
  }
702
701
 
703
702
  function readMap(length) {
@@ -709,13 +708,13 @@
709
708
  key = '__proto_';
710
709
  object[key] = read();
711
710
  }
712
- return object
711
+ return object;
713
712
  } else {
714
713
  let map = new Map();
715
714
  for (let i = 0; i < length; i++) {
716
715
  map.set(read(), read());
717
716
  }
718
- return map
717
+ return map;
719
718
  }
720
719
  }
721
720
 
@@ -727,40 +726,40 @@
727
726
  const byte = src[position++];
728
727
  if ((byte & 0x80) > 0) {
729
728
  position = start;
730
- return
729
+ return;
731
730
  }
732
731
  bytes[i] = byte;
733
732
  }
734
- return fromCharCode.apply(String, bytes)
733
+ return fromCharCode.apply(String, bytes);
735
734
  }
736
735
  function shortStringInJS(length) {
737
736
  if (length < 4) {
738
737
  if (length < 2) {
739
738
  if (length === 0)
740
- return ''
739
+ return '';
741
740
  else {
742
741
  let a = src[position++];
743
742
  if ((a & 0x80) > 1) {
744
743
  position -= 1;
745
- return
744
+ return;
746
745
  }
747
- return fromCharCode(a)
746
+ return fromCharCode(a);
748
747
  }
749
748
  } else {
750
749
  let a = src[position++];
751
750
  let b = src[position++];
752
751
  if ((a & 0x80) > 0 || (b & 0x80) > 0) {
753
752
  position -= 2;
754
- return
753
+ return;
755
754
  }
756
755
  if (length < 3)
757
- return fromCharCode(a, b)
756
+ return fromCharCode(a, b);
758
757
  let c = src[position++];
759
758
  if ((c & 0x80) > 0) {
760
759
  position -= 3;
761
- return
760
+ return;
762
761
  }
763
- return fromCharCode(a, b, c)
762
+ return fromCharCode(a, b, c);
764
763
  }
765
764
  } else {
766
765
  let a = src[position++];
@@ -769,34 +768,34 @@
769
768
  let d = src[position++];
770
769
  if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
771
770
  position -= 4;
772
- return
771
+ return;
773
772
  }
774
773
  if (length < 6) {
775
774
  if (length === 4)
776
- return fromCharCode(a, b, c, d)
775
+ return fromCharCode(a, b, c, d);
777
776
  else {
778
777
  let e = src[position++];
779
778
  if ((e & 0x80) > 0) {
780
779
  position -= 5;
781
- return
780
+ return;
782
781
  }
783
- return fromCharCode(a, b, c, d, e)
782
+ return fromCharCode(a, b, c, d, e);
784
783
  }
785
784
  } else if (length < 8) {
786
785
  let e = src[position++];
787
786
  let f = src[position++];
788
787
  if ((e & 0x80) > 0 || (f & 0x80) > 0) {
789
788
  position -= 6;
790
- return
789
+ return;
791
790
  }
792
791
  if (length < 7)
793
- return fromCharCode(a, b, c, d, e, f)
792
+ return fromCharCode(a, b, c, d, e, f);
794
793
  let g = src[position++];
795
794
  if ((g & 0x80) > 0) {
796
795
  position -= 7;
797
- return
796
+ return;
798
797
  }
799
- return fromCharCode(a, b, c, d, e, f, g)
798
+ return fromCharCode(a, b, c, d, e, f, g);
800
799
  } else {
801
800
  let e = src[position++];
802
801
  let f = src[position++];
@@ -804,34 +803,34 @@
804
803
  let h = src[position++];
805
804
  if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
806
805
  position -= 8;
807
- return
806
+ return;
808
807
  }
809
808
  if (length < 10) {
810
809
  if (length === 8)
811
- return fromCharCode(a, b, c, d, e, f, g, h)
810
+ return fromCharCode(a, b, c, d, e, f, g, h);
812
811
  else {
813
812
  let i = src[position++];
814
813
  if ((i & 0x80) > 0) {
815
814
  position -= 9;
816
- return
815
+ return;
817
816
  }
818
- return fromCharCode(a, b, c, d, e, f, g, h, i)
817
+ return fromCharCode(a, b, c, d, e, f, g, h, i);
819
818
  }
820
819
  } else if (length < 12) {
821
820
  let i = src[position++];
822
821
  let j = src[position++];
823
822
  if ((i & 0x80) > 0 || (j & 0x80) > 0) {
824
823
  position -= 10;
825
- return
824
+ return;
826
825
  }
827
826
  if (length < 11)
828
- return fromCharCode(a, b, c, d, e, f, g, h, i, j)
827
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j);
829
828
  let k = src[position++];
830
829
  if ((k & 0x80) > 0) {
831
830
  position -= 11;
832
- return
831
+ return;
833
832
  }
834
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
833
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
835
834
  } else {
836
835
  let i = src[position++];
837
836
  let j = src[position++];
@@ -839,34 +838,34 @@
839
838
  let l = src[position++];
840
839
  if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
841
840
  position -= 12;
842
- return
841
+ return;
843
842
  }
844
843
  if (length < 14) {
845
844
  if (length === 12)
846
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
845
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
847
846
  else {
848
847
  let m = src[position++];
849
848
  if ((m & 0x80) > 0) {
850
849
  position -= 13;
851
- return
850
+ return;
852
851
  }
853
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
852
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
854
853
  }
855
854
  } else {
856
855
  let m = src[position++];
857
856
  let n = src[position++];
858
857
  if ((m & 0x80) > 0 || (n & 0x80) > 0) {
859
858
  position -= 14;
860
- return
859
+ return;
861
860
  }
862
861
  if (length < 15)
863
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
862
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
864
863
  let o = src[position++];
865
864
  if ((o & 0x80) > 0) {
866
865
  position -= 15;
867
- return
866
+ return;
868
867
  }
869
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
868
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
870
869
  }
871
870
  }
872
871
  }
@@ -884,22 +883,22 @@
884
883
  case 0xd9:
885
884
  // str 8
886
885
  length = src[position++];
887
- break
886
+ break;
888
887
  case 0xda:
889
888
  // str 16
890
889
  length = dataView.getUint16(position);
891
890
  position += 2;
892
- break
891
+ break;
893
892
  case 0xdb:
894
893
  // str 32
895
894
  length = dataView.getUint32(position);
896
895
  position += 4;
897
- break
896
+ break;
898
897
  default:
899
- throw new Error('Expected string')
898
+ throw new Error('Expected string');
900
899
  }
901
900
  }
902
- return readStringJS(length)
901
+ return readStringJS(length);
903
902
  }
904
903
 
905
904
 
@@ -907,7 +906,7 @@
907
906
  return currentUnpackr.copyBuffers ?
908
907
  // specifically use the copying slice (not the node one)
909
908
  Uint8Array.prototype.slice.call(src, position, position += length) :
910
- src.subarray(position, position += length)
909
+ src.subarray(position, position += length);
911
910
  }
912
911
  function readExt(length) {
913
912
  let type = src[position++];
@@ -920,10 +919,10 @@
920
919
  } finally {
921
920
  position = end;
922
921
  }
923
- })
922
+ });
924
923
  }
925
924
  else
926
- throw new Error('Unknown extension type ' + type)
925
+ throw new Error('Unknown extension type ' + type);
927
926
  }
928
927
 
929
928
  var keyCache = new Array(4096);
@@ -933,12 +932,12 @@
933
932
  // fixstr, potentially use key cache
934
933
  length = length - 0xa0;
935
934
  if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
936
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
935
+ return srcString.slice(position - srcStringStart, (position += length) - srcStringStart);
937
936
  else if (!(srcStringEnd == 0 && srcEnd < 180))
938
- return readFixedString(length)
937
+ return readFixedString(length);
939
938
  } else { // not cacheable, go back and do a standard read
940
939
  position--;
941
- return asSafeString(read())
940
+ return asSafeString(read());
942
941
  }
943
942
  let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
944
943
  let entry = keyCache[key];
@@ -951,7 +950,7 @@
951
950
  chunk = dataView.getUint32(checkPosition);
952
951
  if (chunk != entry[i++]) {
953
952
  checkPosition = 0x70000000;
954
- break
953
+ break;
955
954
  }
956
955
  checkPosition += 4;
957
956
  }
@@ -960,12 +959,12 @@
960
959
  chunk = src[checkPosition++];
961
960
  if (chunk != entry[i++]) {
962
961
  checkPosition = 0x70000000;
963
- break
962
+ break;
964
963
  }
965
964
  }
966
965
  if (checkPosition === end) {
967
966
  position = checkPosition;
968
- return entry.string
967
+ return entry.string;
969
968
  }
970
969
  end -= 3;
971
970
  checkPosition = position;
@@ -986,8 +985,8 @@
986
985
  // for small blocks, avoiding the overhead of the extract call is helpful
987
986
  let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
988
987
  if (string != null)
989
- return entry.string = string
990
- return entry.string = readFixedString(length)
988
+ return entry.string = string;
989
+ return entry.string = readFixedString(length);
991
990
  }
992
991
 
993
992
  function asSafeString(property) {
@@ -1018,7 +1017,7 @@
1018
1017
  }
1019
1018
  currentStructures[id] = structure;
1020
1019
  structure.read = createStructureReader(structure, firstByte);
1021
- return structure.read()
1020
+ return structure.read();
1022
1021
  };
1023
1022
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
1024
1023
  currentExtensions[0].noBuffer = true;
@@ -1040,17 +1039,17 @@
1040
1039
  out <<= BigInt(64);
1041
1040
  out |= view.getBigUint64(i);
1042
1041
  }
1043
- return out
1042
+ return out;
1044
1043
  }
1045
1044
  // if (length === 8) return view.getBigUint64(start)
1046
1045
  let middle = start + (length >> 4 << 3);
1047
1046
  let left = decode(start, middle);
1048
1047
  let right = decode(middle, end);
1049
- return (left << BigInt((end - middle) * 8)) | right
1048
+ return (left << BigInt((end - middle) * 8)) | right;
1050
1049
  };
1051
1050
  head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength);
1052
1051
  }
1053
- return head
1052
+ return head;
1054
1053
  };
1055
1054
 
1056
1055
  let errors = {
@@ -1061,14 +1060,14 @@
1061
1060
  if (!errors[data[0]]) {
1062
1061
  let error = Error(data[1], { cause: data[2] });
1063
1062
  error.name = data[0];
1064
- return error
1063
+ return error;
1065
1064
  }
1066
- return errors[data[0]](data[1], { cause: data[2] })
1065
+ return errors[data[0]](data[1], { cause: data[2] });
1067
1066
  };
1068
1067
 
1069
1068
  currentExtensions[0x69] = (data) => {
1070
1069
  // id extension (for structured clones)
1071
- if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
1070
+ if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled');
1072
1071
  let id = dataView.getUint32(position - 4);
1073
1072
  if (!referenceMap)
1074
1073
  referenceMap = new Map();
@@ -1089,7 +1088,7 @@
1089
1088
  let targetProperties = read(); // read the next value as the target object to id
1090
1089
  if (!refEntry.used) {
1091
1090
  // no cycle, can just use the returned read object
1092
- return refEntry.target = targetProperties // replace the placeholder with the real one
1091
+ return refEntry.target = targetProperties; // replace the placeholder with the real one
1093
1092
  } else {
1094
1093
  // there is a cycle, so we have to assign properties to original target
1095
1094
  Object.assign(target, targetProperties);
@@ -1100,16 +1099,16 @@
1100
1099
  for (let [k, v] of targetProperties.entries()) target.set(k, v);
1101
1100
  if (target instanceof Set)
1102
1101
  for (let i of Array.from(targetProperties)) target.add(i);
1103
- return target
1102
+ return target;
1104
1103
  };
1105
1104
 
1106
1105
  currentExtensions[0x70] = (data) => {
1107
1106
  // pointer extension (for structured clones)
1108
- if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled')
1107
+ if (currentUnpackr.structuredClone === false) throw new Error('Structured clone extension is disabled');
1109
1108
  let id = dataView.getUint32(position - 4);
1110
1109
  let refEntry = referenceMap.get(id);
1111
1110
  refEntry.used = true;
1112
- return refEntry.target
1111
+ return refEntry.target;
1113
1112
  };
1114
1113
 
1115
1114
  currentExtensions[0x73] = () => new Set(read());
@@ -1124,15 +1123,15 @@
1124
1123
 
1125
1124
  let typedArrayName = typedArrays[typeCode];
1126
1125
  if (!typedArrayName) {
1127
- if (typeCode === 16) return buffer
1128
- if (typeCode === 17) return new DataView(buffer)
1129
- throw new Error('Could not find typed array for code ' + typeCode)
1126
+ if (typeCode === 16) return buffer;
1127
+ if (typeCode === 17) return new DataView(buffer);
1128
+ throw new Error('Could not find typed array for code ' + typeCode);
1130
1129
  }
1131
- return new glbl[typedArrayName](buffer)
1130
+ return new glbl[typedArrayName](buffer);
1132
1131
  };
1133
1132
  currentExtensions[0x78] = () => {
1134
1133
  let data = read();
1135
- return new RegExp(data[0], data[1])
1134
+ return new RegExp(data[0], data[1]);
1136
1135
  };
1137
1136
  const TEMP_BUNDLE = [];
1138
1137
  currentExtensions[0x62] = (data) => {
@@ -1145,30 +1144,30 @@
1145
1144
  bundledStrings.position1 = 0;
1146
1145
  bundledStrings.postBundlePosition = position;
1147
1146
  position = dataPosition;
1148
- return read()
1147
+ return read();
1149
1148
  };
1150
1149
 
1151
1150
  currentExtensions[0xff] = (data) => {
1152
1151
  // 32-bit date extension
1153
1152
  if (data.length == 4)
1154
- return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
1153
+ return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000);
1155
1154
  else if (data.length == 8)
1156
1155
  return new Date(
1157
1156
  ((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
1158
- ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
1157
+ ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000);
1159
1158
  else if (data.length == 12)
1160
1159
  return new Date(
1161
1160
  ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
1162
- (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
1161
+ (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000);
1163
1162
  else
1164
- return new Date('invalid')
1163
+ return new Date('invalid');
1165
1164
  };
1166
1165
  // registration of bulk record definition?
1167
1166
  // currentExtensions[0x52] = () =>
1168
1167
 
1169
1168
  function saveState(callback) {
1170
- if (onSaveState)
1171
- onSaveState();
1169
+ if (currentUnpackr && currentUnpackr._onSaveState)
1170
+ currentUnpackr._onSaveState();
1172
1171
  let savedSrcEnd = srcEnd;
1173
1172
  let savedPosition = position;
1174
1173
  let savedStringPosition = stringPosition;
@@ -1201,7 +1200,7 @@
1201
1200
  currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
1202
1201
  currentUnpackr = savedPackr;
1203
1202
  dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
1204
- return value
1203
+ return value;
1205
1204
  }
1206
1205
  function clearSource() {
1207
1206
  src = null;
@@ -1236,13 +1235,12 @@
1236
1235
  function roundFloat32(float32Number) {
1237
1236
  f32Array[0] = float32Number;
1238
1237
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1239
- return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1240
- }
1241
- function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1242
- readStruct = updatedReadStruct;
1243
- onLoadedStructures = loadedStructs;
1244
- onSaveState = saveState;
1238
+ return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
1245
1239
  }
1240
+ // Marker for downstream libraries (e.g. structon) to detect per-instance
1241
+ // struct-decoding hooks (this._readStruct, this._onLoadedStructures,
1242
+ // this._onSaveState). See `checkedRead` for the dispatch.
1243
+ Unpackr.SUPPORTS_STRUCT_HOOKS = true;
1246
1244
 
1247
1245
  exports.C1 = C1;
1248
1246
  exports.C1Type = C1Type;
@@ -1260,7 +1258,6 @@
1260
1258
  exports.readString = readString;
1261
1259
  exports.roundFloat32 = roundFloat32;
1262
1260
  exports.setExtractor = setExtractor;
1263
- exports.setReadStruct = setReadStruct;
1264
1261
  exports.typedArrays = typedArrays;
1265
1262
  exports.unpack = unpack;
1266
1263
  exports.unpackMultiple = unpackMultiple;