msgpackr 1.8.0 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1158 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.msgpackr = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ var decoder;
8
+ try {
9
+ decoder = new TextDecoder();
10
+ } catch(error) {}
11
+ var src;
12
+ var srcEnd;
13
+ var position = 0;
14
+ const EMPTY_ARRAY = [];
15
+ var strings = EMPTY_ARRAY;
16
+ var stringPosition = 0;
17
+ var currentUnpackr = {};
18
+ var currentStructures;
19
+ var srcString;
20
+ var srcStringStart = 0;
21
+ var srcStringEnd = 0;
22
+ var bundledStrings;
23
+ var referenceMap;
24
+ var currentExtensions = [];
25
+ var dataView;
26
+ var defaultOptions = {
27
+ useRecords: false,
28
+ mapsAsObjects: true
29
+ };
30
+ class C1Type {}
31
+ const C1 = new C1Type();
32
+ C1.name = 'MessagePack 0xC1';
33
+ var sequentialMode = false;
34
+ var inlineObjectReadThreshold = 2;
35
+ var readStruct, onLoadedStructures, onSaveState;
36
+ var BlockedFunction; // we use search and replace to change the next call to BlockedFunction to avoid CSP issues for
37
+ // no-eval build
38
+ try {
39
+ new BlockedFunction ('');
40
+ } catch(error) {
41
+ // if eval variants are not supported, do not create inline object readers ever
42
+ inlineObjectReadThreshold = Infinity;
43
+ }
44
+
45
+ class Unpackr {
46
+ constructor(options) {
47
+ if (options) {
48
+ if (options.useRecords === false && options.mapsAsObjects === undefined)
49
+ options.mapsAsObjects = true;
50
+ if (options.sequential && options.trusted !== false) {
51
+ options.trusted = true;
52
+ if (!options.structures && options.useRecords != false) {
53
+ options.structures = [];
54
+ if (!options.maxSharedStructures)
55
+ options.maxSharedStructures = 0;
56
+ }
57
+ }
58
+ if (options.structures)
59
+ options.structures.sharedLength = options.structures.length;
60
+ else if (options.getStructures) {
61
+ (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
62
+ options.structures.sharedLength = 0;
63
+ }
64
+ if (options.int64AsNumber) {
65
+ options.int64AsType = 'number';
66
+ }
67
+ }
68
+ Object.assign(this, options);
69
+ }
70
+ unpack(source, options) {
71
+ if (src) {
72
+ // re-entrant execution, save the state and restore it after we do this unpack
73
+ return saveState(() => {
74
+ clearSource();
75
+ return this ? this.unpack(source, options) : Unpackr.prototype.unpack.call(defaultOptions, source, options)
76
+ })
77
+ }
78
+ if (typeof options === 'object') {
79
+ srcEnd = options.end || source.length;
80
+ position = options.start || 0;
81
+ } else {
82
+ position = 0;
83
+ srcEnd = options > -1 ? options : source.length;
84
+ }
85
+ stringPosition = 0;
86
+ srcStringEnd = 0;
87
+ srcString = null;
88
+ strings = EMPTY_ARRAY;
89
+ bundledStrings = null;
90
+ src = source;
91
+ // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
92
+ // technique for getting data from a database where it can be copied into an existing buffer instead of creating
93
+ // new ones
94
+ try {
95
+ dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
96
+ } catch(error) {
97
+ // if it doesn't have a buffer, maybe it is the wrong type of object
98
+ src = null;
99
+ if (source instanceof Uint8Array)
100
+ throw error
101
+ throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
102
+ }
103
+ if (this instanceof Unpackr) {
104
+ currentUnpackr = this;
105
+ if (this.structures) {
106
+ currentStructures = this.structures;
107
+ return checkedRead(options)
108
+ } else if (!currentStructures || currentStructures.length > 0) {
109
+ currentStructures = [];
110
+ }
111
+ } else {
112
+ currentUnpackr = defaultOptions;
113
+ if (!currentStructures || currentStructures.length > 0)
114
+ currentStructures = [];
115
+ }
116
+ return checkedRead(options)
117
+ }
118
+ unpackMultiple(source, forEach) {
119
+ let values, lastPosition = 0;
120
+ try {
121
+ sequentialMode = true;
122
+ let size = source.length;
123
+ let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
124
+ if (forEach) {
125
+ forEach(value);
126
+ while(position < size) {
127
+ lastPosition = position;
128
+ if (forEach(checkedRead()) === false) {
129
+ return
130
+ }
131
+ }
132
+ }
133
+ else {
134
+ values = [ value ];
135
+ while(position < size) {
136
+ lastPosition = position;
137
+ values.push(checkedRead());
138
+ }
139
+ return values
140
+ }
141
+ } catch(error) {
142
+ error.lastPosition = lastPosition;
143
+ error.values = values;
144
+ throw error
145
+ } finally {
146
+ sequentialMode = false;
147
+ clearSource();
148
+ }
149
+ }
150
+ _mergeStructures(loadedStructures, existingStructures) {
151
+ if (onLoadedStructures)
152
+ loadedStructures = onLoadedStructures.call(this, loadedStructures);
153
+ loadedStructures = loadedStructures || [];
154
+ if (Object.isFrozen(loadedStructures))
155
+ loadedStructures = loadedStructures.map(structure => structure.slice(0));
156
+ for (let i = 0, l = loadedStructures.length; i < l; i++) {
157
+ let structure = loadedStructures[i];
158
+ if (structure) {
159
+ structure.isShared = true;
160
+ if (i >= 32)
161
+ structure.highByte = (i - 32) >> 5;
162
+ }
163
+ }
164
+ loadedStructures.sharedLength = loadedStructures.length;
165
+ for (let id in existingStructures || []) {
166
+ if (id >= 0) {
167
+ let structure = loadedStructures[id];
168
+ let existing = existingStructures[id];
169
+ if (existing) {
170
+ if (structure)
171
+ (loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure;
172
+ loadedStructures[id] = existing;
173
+ }
174
+ }
175
+ }
176
+ return this.structures = loadedStructures
177
+ }
178
+ decode(source, end) {
179
+ return this.unpack(source, end)
180
+ }
181
+ }
182
+ function getPosition() {
183
+ return position
184
+ }
185
+ function checkedRead(options) {
186
+ try {
187
+ if (!currentUnpackr.trusted && !sequentialMode) {
188
+ let sharedLength = currentStructures.sharedLength || 0;
189
+ if (sharedLength < currentStructures.length)
190
+ currentStructures.length = sharedLength;
191
+ }
192
+ let result;
193
+ if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
194
+ result = readStruct(src, position, srcEnd, currentUnpackr);
195
+ src = null; // dispose of this so that recursive unpack calls don't save state
196
+ if (!(options && options.lazy) && result)
197
+ result = result.toJSON();
198
+ position = srcEnd;
199
+ } else
200
+ result = read();
201
+ if (bundledStrings) { // bundled strings to skip past
202
+ position = bundledStrings.postBundlePosition;
203
+ bundledStrings = null;
204
+ }
205
+
206
+ if (position == srcEnd) {
207
+ // finished reading this source, cleanup references
208
+ if (currentStructures && currentStructures.restoreStructures)
209
+ restoreStructures();
210
+ currentStructures = null;
211
+ src = null;
212
+ if (referenceMap)
213
+ referenceMap = null;
214
+ } else if (position > srcEnd) {
215
+ // over read
216
+ throw new Error('Unexpected end of MessagePack data')
217
+ } else if (!sequentialMode) {
218
+ throw new Error('Data read, but end of buffer not reached ' + JSON.stringify(result).slice(0, 100))
219
+ }
220
+ // else more to read, but we are reading sequentially, so don't clear source yet
221
+ return result
222
+ } catch(error) {
223
+ if (currentStructures && currentStructures.restoreStructures)
224
+ restoreStructures();
225
+ clearSource();
226
+ if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
227
+ error.incomplete = true;
228
+ }
229
+ throw error
230
+ }
231
+ }
232
+
233
+ function restoreStructures() {
234
+ for (let id in currentStructures.restoreStructures) {
235
+ currentStructures[id] = currentStructures.restoreStructures[id];
236
+ }
237
+ currentStructures.restoreStructures = null;
238
+ }
239
+
240
+ function read() {
241
+ let token = src[position++];
242
+ if (token < 0xa0) {
243
+ if (token < 0x80) {
244
+ if (token < 0x40)
245
+ return token
246
+ else {
247
+ let structure = currentStructures[token & 0x3f] ||
248
+ currentUnpackr.getStructures && loadStructures()[token & 0x3f];
249
+ if (structure) {
250
+ if (!structure.read) {
251
+ structure.read = createStructureReader(structure, token & 0x3f);
252
+ }
253
+ return structure.read()
254
+ } else
255
+ return token
256
+ }
257
+ } else if (token < 0x90) {
258
+ // map
259
+ token -= 0x80;
260
+ if (currentUnpackr.mapsAsObjects) {
261
+ let object = {};
262
+ for (let i = 0; i < token; i++) {
263
+ let key = readKey();
264
+ if (key === '__proto__')
265
+ key = '__proto_';
266
+ object[key] = read();
267
+ }
268
+ return object
269
+ } else {
270
+ let map = new Map();
271
+ for (let i = 0; i < token; i++) {
272
+ map.set(read(), read());
273
+ }
274
+ return map
275
+ }
276
+ } else {
277
+ token -= 0x90;
278
+ let array = new Array(token);
279
+ for (let i = 0; i < token; i++) {
280
+ array[i] = read();
281
+ }
282
+ if (currentUnpackr.freezeData)
283
+ return Object.freeze(array)
284
+ return array
285
+ }
286
+ } else if (token < 0xc0) {
287
+ // fixstr
288
+ let length = token - 0xa0;
289
+ if (srcStringEnd >= position) {
290
+ return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
291
+ }
292
+ if (srcStringEnd == 0 && srcEnd < 140) {
293
+ // for small blocks, avoiding the overhead of the extract call is helpful
294
+ let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
295
+ if (string != null)
296
+ return string
297
+ }
298
+ return readFixedString(length)
299
+ } else {
300
+ let value;
301
+ switch (token) {
302
+ case 0xc0: return null
303
+ case 0xc1:
304
+ if (bundledStrings) {
305
+ value = read(); // followed by the length of the string in characters (not bytes!)
306
+ if (value > 0)
307
+ return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
308
+ else
309
+ return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
310
+ }
311
+ return C1; // "never-used", return special object to denote that
312
+ case 0xc2: return false
313
+ case 0xc3: return true
314
+ case 0xc4:
315
+ // bin 8
316
+ value = src[position++];
317
+ if (value === undefined)
318
+ throw new Error('Unexpected end of buffer')
319
+ return readBin(value)
320
+ case 0xc5:
321
+ // bin 16
322
+ value = dataView.getUint16(position);
323
+ position += 2;
324
+ return readBin(value)
325
+ case 0xc6:
326
+ // bin 32
327
+ value = dataView.getUint32(position);
328
+ position += 4;
329
+ return readBin(value)
330
+ case 0xc7:
331
+ // ext 8
332
+ return readExt(src[position++])
333
+ case 0xc8:
334
+ // ext 16
335
+ value = dataView.getUint16(position);
336
+ position += 2;
337
+ return readExt(value)
338
+ case 0xc9:
339
+ // ext 32
340
+ value = dataView.getUint32(position);
341
+ position += 4;
342
+ return readExt(value)
343
+ case 0xca:
344
+ value = dataView.getFloat32(position);
345
+ if (currentUnpackr.useFloat32 > 2) {
346
+ // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
347
+ let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
348
+ position += 4;
349
+ return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
350
+ }
351
+ position += 4;
352
+ return value
353
+ case 0xcb:
354
+ value = dataView.getFloat64(position);
355
+ position += 8;
356
+ return value
357
+ // uint handlers
358
+ case 0xcc:
359
+ return src[position++]
360
+ case 0xcd:
361
+ value = dataView.getUint16(position);
362
+ position += 2;
363
+ return value
364
+ case 0xce:
365
+ value = dataView.getUint32(position);
366
+ position += 4;
367
+ return value
368
+ case 0xcf:
369
+ if (currentUnpackr.int64AsType === 'number') {
370
+ value = dataView.getUint32(position) * 0x100000000;
371
+ value += dataView.getUint32(position + 4);
372
+ } else if (currentUnpackr.int64AsType === 'string') {
373
+ value = dataView.getBigUint64(position).toString();
374
+ } else
375
+ value = dataView.getBigUint64(position);
376
+ position += 8;
377
+ return value
378
+
379
+ // int handlers
380
+ case 0xd0:
381
+ return dataView.getInt8(position++)
382
+ case 0xd1:
383
+ value = dataView.getInt16(position);
384
+ position += 2;
385
+ return value
386
+ case 0xd2:
387
+ value = dataView.getInt32(position);
388
+ position += 4;
389
+ return value
390
+ case 0xd3:
391
+ if (currentUnpackr.int64AsType === 'number') {
392
+ value = dataView.getInt32(position) * 0x100000000;
393
+ value += dataView.getUint32(position + 4);
394
+ } else if (currentUnpackr.int64AsType === 'string') {
395
+ value = dataView.getBigInt64(position).toString();
396
+ } else
397
+ value = dataView.getBigInt64(position);
398
+ position += 8;
399
+ return value
400
+
401
+ case 0xd4:
402
+ // fixext 1
403
+ value = src[position++];
404
+ if (value == 0x72) {
405
+ return recordDefinition(src[position++] & 0x3f)
406
+ } else {
407
+ let extension = currentExtensions[value];
408
+ if (extension) {
409
+ if (extension.read) {
410
+ position++; // skip filler byte
411
+ return extension.read(read())
412
+ } else if (extension.noBuffer) {
413
+ position++; // skip filler byte
414
+ return extension()
415
+ } else
416
+ return extension(src.subarray(position, ++position))
417
+ } else
418
+ throw new Error('Unknown extension ' + value)
419
+ }
420
+ case 0xd5:
421
+ // fixext 2
422
+ value = src[position];
423
+ if (value == 0x72) {
424
+ position++;
425
+ return recordDefinition(src[position++] & 0x3f, src[position++])
426
+ } else
427
+ return readExt(2)
428
+ case 0xd6:
429
+ // fixext 4
430
+ return readExt(4)
431
+ case 0xd7:
432
+ // fixext 8
433
+ return readExt(8)
434
+ case 0xd8:
435
+ // fixext 16
436
+ return readExt(16)
437
+ case 0xd9:
438
+ // str 8
439
+ value = src[position++];
440
+ if (srcStringEnd >= position) {
441
+ return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
442
+ }
443
+ return readString8(value)
444
+ case 0xda:
445
+ // str 16
446
+ value = dataView.getUint16(position);
447
+ position += 2;
448
+ if (srcStringEnd >= position) {
449
+ return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
450
+ }
451
+ return readString16(value)
452
+ case 0xdb:
453
+ // str 32
454
+ value = dataView.getUint32(position);
455
+ position += 4;
456
+ if (srcStringEnd >= position) {
457
+ return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
458
+ }
459
+ return readString32(value)
460
+ case 0xdc:
461
+ // array 16
462
+ value = dataView.getUint16(position);
463
+ position += 2;
464
+ return readArray(value)
465
+ case 0xdd:
466
+ // array 32
467
+ value = dataView.getUint32(position);
468
+ position += 4;
469
+ return readArray(value)
470
+ case 0xde:
471
+ // map 16
472
+ value = dataView.getUint16(position);
473
+ position += 2;
474
+ return readMap(value)
475
+ case 0xdf:
476
+ // map 32
477
+ value = dataView.getUint32(position);
478
+ position += 4;
479
+ return readMap(value)
480
+ default: // negative int
481
+ if (token >= 0xe0)
482
+ return token - 0x100
483
+ if (token === undefined) {
484
+ let error = new Error('Unexpected end of MessagePack data');
485
+ error.incomplete = true;
486
+ throw error
487
+ }
488
+ throw new Error('Unknown MessagePack token ' + token)
489
+
490
+ }
491
+ }
492
+ }
493
+ const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
494
+ function createStructureReader(structure, firstId) {
495
+ function readObject() {
496
+ // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
497
+ if (readObject.count++ > inlineObjectReadThreshold) {
498
+ let readObject = structure.read = (new BlockedFunction ('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
499
+ '({' + structure.map(key => key === '__proto__' ? '__proto_:r()' : validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read);
500
+ if (structure.highByte === 0)
501
+ structure.read = createSecondByteReader(firstId, structure.read);
502
+ return readObject() // second byte is already read, if there is one so immediately read object
503
+ }
504
+ let object = {};
505
+ for (let i = 0, l = structure.length; i < l; i++) {
506
+ let key = structure[i];
507
+ if (key === '__proto__')
508
+ key = '__proto_';
509
+ object[key] = read();
510
+ }
511
+ if (currentUnpackr.freezeData)
512
+ return Object.freeze(object);
513
+ return object
514
+ }
515
+ readObject.count = 0;
516
+ if (structure.highByte === 0) {
517
+ return createSecondByteReader(firstId, readObject)
518
+ }
519
+ return readObject
520
+ }
521
+
522
+ const createSecondByteReader = (firstId, read0) => {
523
+ return function() {
524
+ let highByte = src[position++];
525
+ if (highByte === 0)
526
+ return read0()
527
+ let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
528
+ let structure = currentStructures[id] || loadStructures()[id];
529
+ if (!structure) {
530
+ throw new Error('Record id is not defined for ' + id)
531
+ }
532
+ if (!structure.read)
533
+ structure.read = createStructureReader(structure, firstId);
534
+ return structure.read()
535
+ }
536
+ };
537
+
538
+ function loadStructures() {
539
+ let loadedStructures = saveState(() => {
540
+ // save the state in case getStructures modifies our buffer
541
+ src = null;
542
+ return currentUnpackr.getStructures()
543
+ });
544
+ return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
545
+ }
546
+
547
+ var readFixedString = readStringJS;
548
+ var readString8 = readStringJS;
549
+ var readString16 = readStringJS;
550
+ var readString32 = readStringJS;
551
+ exports.isNativeAccelerationEnabled = false;
552
+
553
+ function setExtractor(extractStrings) {
554
+ exports.isNativeAccelerationEnabled = true;
555
+ readFixedString = readString(1);
556
+ readString8 = readString(2);
557
+ readString16 = readString(3);
558
+ readString32 = readString(5);
559
+ function readString(headerLength) {
560
+ return function readString(length) {
561
+ let string = strings[stringPosition++];
562
+ if (string == null) {
563
+ if (bundledStrings)
564
+ return readStringJS(length)
565
+ let extraction = extractStrings(position - headerLength, srcEnd, src);
566
+ if (typeof extraction == 'string') {
567
+ string = extraction;
568
+ strings = EMPTY_ARRAY;
569
+ } else {
570
+ strings = extraction;
571
+ stringPosition = 1;
572
+ 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
573
+ string = strings[0];
574
+ if (string === undefined)
575
+ throw new Error('Unexpected end of buffer')
576
+ }
577
+ }
578
+ let srcStringLength = string.length;
579
+ if (srcStringLength <= length) {
580
+ position += length;
581
+ return string
582
+ }
583
+ srcString = string;
584
+ srcStringStart = position;
585
+ srcStringEnd = position + srcStringLength;
586
+ position += length;
587
+ return string.slice(0, length) // we know we just want the beginning
588
+ }
589
+ }
590
+ }
591
+ function readStringJS(length) {
592
+ let result;
593
+ if (length < 16) {
594
+ if (result = shortStringInJS(length))
595
+ return result
596
+ }
597
+ if (length > 64 && decoder)
598
+ return decoder.decode(src.subarray(position, position += length))
599
+ const end = position + length;
600
+ const units = [];
601
+ result = '';
602
+ while (position < end) {
603
+ const byte1 = src[position++];
604
+ if ((byte1 & 0x80) === 0) {
605
+ // 1 byte
606
+ units.push(byte1);
607
+ } else if ((byte1 & 0xe0) === 0xc0) {
608
+ // 2 bytes
609
+ const byte2 = src[position++] & 0x3f;
610
+ units.push(((byte1 & 0x1f) << 6) | byte2);
611
+ } else if ((byte1 & 0xf0) === 0xe0) {
612
+ // 3 bytes
613
+ const byte2 = src[position++] & 0x3f;
614
+ const byte3 = src[position++] & 0x3f;
615
+ units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
616
+ } else if ((byte1 & 0xf8) === 0xf0) {
617
+ // 4 bytes
618
+ const byte2 = src[position++] & 0x3f;
619
+ const byte3 = src[position++] & 0x3f;
620
+ const byte4 = src[position++] & 0x3f;
621
+ let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
622
+ if (unit > 0xffff) {
623
+ unit -= 0x10000;
624
+ units.push(((unit >>> 10) & 0x3ff) | 0xd800);
625
+ unit = 0xdc00 | (unit & 0x3ff);
626
+ }
627
+ units.push(unit);
628
+ } else {
629
+ units.push(byte1);
630
+ }
631
+
632
+ if (units.length >= 0x1000) {
633
+ result += fromCharCode.apply(String, units);
634
+ units.length = 0;
635
+ }
636
+ }
637
+
638
+ if (units.length > 0) {
639
+ result += fromCharCode.apply(String, units);
640
+ }
641
+
642
+ return result
643
+ }
644
+ function readString(source, start, length) {
645
+ let existingSrc = src;
646
+ src = source;
647
+ position = start;
648
+ try {
649
+ return readStringJS(length);
650
+ } finally {
651
+ src = existingSrc;
652
+ }
653
+ }
654
+
655
+ function readArray(length) {
656
+ let array = new Array(length);
657
+ for (let i = 0; i < length; i++) {
658
+ array[i] = read();
659
+ }
660
+ if (currentUnpackr.freezeData)
661
+ return Object.freeze(array)
662
+ return array
663
+ }
664
+
665
+ function readMap(length) {
666
+ if (currentUnpackr.mapsAsObjects) {
667
+ let object = {};
668
+ for (let i = 0; i < length; i++) {
669
+ let key = readKey();
670
+ if (key === '__proto__')
671
+ key = '__proto_';
672
+ object[key] = read();
673
+ }
674
+ return object
675
+ } else {
676
+ let map = new Map();
677
+ for (let i = 0; i < length; i++) {
678
+ map.set(read(), read());
679
+ }
680
+ return map
681
+ }
682
+ }
683
+
684
+ var fromCharCode = String.fromCharCode;
685
+ function longStringInJS(length) {
686
+ let start = position;
687
+ let bytes = new Array(length);
688
+ for (let i = 0; i < length; i++) {
689
+ const byte = src[position++];
690
+ if ((byte & 0x80) > 0) {
691
+ position = start;
692
+ return
693
+ }
694
+ bytes[i] = byte;
695
+ }
696
+ return fromCharCode.apply(String, bytes)
697
+ }
698
+ function shortStringInJS(length) {
699
+ if (length < 4) {
700
+ if (length < 2) {
701
+ if (length === 0)
702
+ return ''
703
+ else {
704
+ let a = src[position++];
705
+ if ((a & 0x80) > 1) {
706
+ position -= 1;
707
+ return
708
+ }
709
+ return fromCharCode(a)
710
+ }
711
+ } else {
712
+ let a = src[position++];
713
+ let b = src[position++];
714
+ if ((a & 0x80) > 0 || (b & 0x80) > 0) {
715
+ position -= 2;
716
+ return
717
+ }
718
+ if (length < 3)
719
+ return fromCharCode(a, b)
720
+ let c = src[position++];
721
+ if ((c & 0x80) > 0) {
722
+ position -= 3;
723
+ return
724
+ }
725
+ return fromCharCode(a, b, c)
726
+ }
727
+ } else {
728
+ let a = src[position++];
729
+ let b = src[position++];
730
+ let c = src[position++];
731
+ let d = src[position++];
732
+ if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
733
+ position -= 4;
734
+ return
735
+ }
736
+ if (length < 6) {
737
+ if (length === 4)
738
+ return fromCharCode(a, b, c, d)
739
+ else {
740
+ let e = src[position++];
741
+ if ((e & 0x80) > 0) {
742
+ position -= 5;
743
+ return
744
+ }
745
+ return fromCharCode(a, b, c, d, e)
746
+ }
747
+ } else if (length < 8) {
748
+ let e = src[position++];
749
+ let f = src[position++];
750
+ if ((e & 0x80) > 0 || (f & 0x80) > 0) {
751
+ position -= 6;
752
+ return
753
+ }
754
+ if (length < 7)
755
+ return fromCharCode(a, b, c, d, e, f)
756
+ let g = src[position++];
757
+ if ((g & 0x80) > 0) {
758
+ position -= 7;
759
+ return
760
+ }
761
+ return fromCharCode(a, b, c, d, e, f, g)
762
+ } else {
763
+ let e = src[position++];
764
+ let f = src[position++];
765
+ let g = src[position++];
766
+ let h = src[position++];
767
+ if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
768
+ position -= 8;
769
+ return
770
+ }
771
+ if (length < 10) {
772
+ if (length === 8)
773
+ return fromCharCode(a, b, c, d, e, f, g, h)
774
+ else {
775
+ let i = src[position++];
776
+ if ((i & 0x80) > 0) {
777
+ position -= 9;
778
+ return
779
+ }
780
+ return fromCharCode(a, b, c, d, e, f, g, h, i)
781
+ }
782
+ } else if (length < 12) {
783
+ let i = src[position++];
784
+ let j = src[position++];
785
+ if ((i & 0x80) > 0 || (j & 0x80) > 0) {
786
+ position -= 10;
787
+ return
788
+ }
789
+ if (length < 11)
790
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j)
791
+ let k = src[position++];
792
+ if ((k & 0x80) > 0) {
793
+ position -= 11;
794
+ return
795
+ }
796
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
797
+ } else {
798
+ let i = src[position++];
799
+ let j = src[position++];
800
+ let k = src[position++];
801
+ let l = src[position++];
802
+ if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
803
+ position -= 12;
804
+ return
805
+ }
806
+ if (length < 14) {
807
+ if (length === 12)
808
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
809
+ else {
810
+ let m = src[position++];
811
+ if ((m & 0x80) > 0) {
812
+ position -= 13;
813
+ return
814
+ }
815
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
816
+ }
817
+ } else {
818
+ let m = src[position++];
819
+ let n = src[position++];
820
+ if ((m & 0x80) > 0 || (n & 0x80) > 0) {
821
+ position -= 14;
822
+ return
823
+ }
824
+ if (length < 15)
825
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
826
+ let o = src[position++];
827
+ if ((o & 0x80) > 0) {
828
+ position -= 15;
829
+ return
830
+ }
831
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
832
+ }
833
+ }
834
+ }
835
+ }
836
+ }
837
+
838
+ function readOnlyJSString() {
839
+ let token = src[position++];
840
+ let length;
841
+ if (token < 0xc0) {
842
+ // fixstr
843
+ length = token - 0xa0;
844
+ } else {
845
+ switch(token) {
846
+ case 0xd9:
847
+ // str 8
848
+ length = src[position++];
849
+ break
850
+ case 0xda:
851
+ // str 16
852
+ length = dataView.getUint16(position);
853
+ position += 2;
854
+ break
855
+ case 0xdb:
856
+ // str 32
857
+ length = dataView.getUint32(position);
858
+ position += 4;
859
+ break
860
+ default:
861
+ throw new Error('Expected string')
862
+ }
863
+ }
864
+ return readStringJS(length)
865
+ }
866
+
867
+
868
+ function readBin(length) {
869
+ return currentUnpackr.copyBuffers ?
870
+ // specifically use the copying slice (not the node one)
871
+ Uint8Array.prototype.slice.call(src, position, position += length) :
872
+ src.subarray(position, position += length)
873
+ }
874
+ function readExt(length) {
875
+ let type = src[position++];
876
+ if (currentExtensions[type]) {
877
+ let end;
878
+ return currentExtensions[type](src.subarray(position, end = (position += length)), (readPosition) => {
879
+ position = readPosition;
880
+ try {
881
+ return read();
882
+ } finally {
883
+ position = end;
884
+ }
885
+ })
886
+ }
887
+ else
888
+ throw new Error('Unknown extension type ' + type)
889
+ }
890
+
891
+ var keyCache = new Array(4096);
892
+ function readKey() {
893
+ let length = src[position++];
894
+ if (length >= 0xa0 && length < 0xc0) {
895
+ // fixstr, potentially use key cache
896
+ length = length - 0xa0;
897
+ if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
898
+ return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
899
+ else if (!(srcStringEnd == 0 && srcEnd < 180))
900
+ return readFixedString(length)
901
+ } else { // not cacheable, go back and do a standard read
902
+ position--;
903
+ return read().toString()
904
+ }
905
+ let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
906
+ let entry = keyCache[key];
907
+ let checkPosition = position;
908
+ let end = position + length - 3;
909
+ let chunk;
910
+ let i = 0;
911
+ if (entry && entry.bytes == length) {
912
+ while (checkPosition < end) {
913
+ chunk = dataView.getUint32(checkPosition);
914
+ if (chunk != entry[i++]) {
915
+ checkPosition = 0x70000000;
916
+ break
917
+ }
918
+ checkPosition += 4;
919
+ }
920
+ end += 3;
921
+ while (checkPosition < end) {
922
+ chunk = src[checkPosition++];
923
+ if (chunk != entry[i++]) {
924
+ checkPosition = 0x70000000;
925
+ break
926
+ }
927
+ }
928
+ if (checkPosition === end) {
929
+ position = checkPosition;
930
+ return entry.string
931
+ }
932
+ end -= 3;
933
+ checkPosition = position;
934
+ }
935
+ entry = [];
936
+ keyCache[key] = entry;
937
+ entry.bytes = length;
938
+ while (checkPosition < end) {
939
+ chunk = dataView.getUint32(checkPosition);
940
+ entry.push(chunk);
941
+ checkPosition += 4;
942
+ }
943
+ end += 3;
944
+ while (checkPosition < end) {
945
+ chunk = src[checkPosition++];
946
+ entry.push(chunk);
947
+ }
948
+ // for small blocks, avoiding the overhead of the extract call is helpful
949
+ let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
950
+ if (string != null)
951
+ return entry.string = string
952
+ return entry.string = readFixedString(length)
953
+ }
954
+
955
+ // the registration of the record definition extension (as "r")
956
+ const recordDefinition = (id, highByte) => {
957
+ let structure = read().map(property => property.toString()); // ensure that all keys are strings and that the array is mutable
958
+ let firstByte = id;
959
+ if (highByte !== undefined) {
960
+ id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
961
+ structure.highByte = highByte;
962
+ }
963
+ let existingStructure = currentStructures[id];
964
+ if (existingStructure && existingStructure.isShared) {
965
+ (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
966
+ }
967
+ currentStructures[id] = structure;
968
+ structure.read = createStructureReader(structure, firstByte);
969
+ return structure.read()
970
+ };
971
+ currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
972
+ currentExtensions[0].noBuffer = true;
973
+
974
+ currentExtensions[0x65] = () => {
975
+ let data = read();
976
+ return (globalThis[data[0]] || Error)(data[1])
977
+ };
978
+
979
+ currentExtensions[0x69] = (data) => {
980
+ // id extension (for structured clones)
981
+ let id = dataView.getUint32(position - 4);
982
+ if (!referenceMap)
983
+ referenceMap = new Map();
984
+ let token = src[position];
985
+ let target;
986
+ // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
987
+ // ahead past references to record structure definitions
988
+ if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
989
+ target = [];
990
+ else
991
+ target = {};
992
+
993
+ let refEntry = { target }; // a placeholder object
994
+ referenceMap.set(id, refEntry);
995
+ let targetProperties = read(); // read the next value as the target object to id
996
+ if (refEntry.used) // there is a cycle, so we have to assign properties to original target
997
+ return Object.assign(target, targetProperties)
998
+ refEntry.target = targetProperties; // the placeholder wasn't used, replace with the deserialized one
999
+ return targetProperties // no cycle, can just use the returned read object
1000
+ };
1001
+
1002
+ currentExtensions[0x70] = (data) => {
1003
+ // pointer extension (for structured clones)
1004
+ let id = dataView.getUint32(position - 4);
1005
+ let refEntry = referenceMap.get(id);
1006
+ refEntry.used = true;
1007
+ return refEntry.target
1008
+ };
1009
+
1010
+ currentExtensions[0x73] = () => new Set(read());
1011
+
1012
+ const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
1013
+
1014
+ currentExtensions[0x74] = (data) => {
1015
+ let typeCode = data[0];
1016
+ let typedArrayName = typedArrays[typeCode];
1017
+ if (!typedArrayName)
1018
+ throw new Error('Could not find typed array for code ' + typeCode)
1019
+ // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
1020
+ return new globalThis[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
1021
+ };
1022
+ currentExtensions[0x78] = () => {
1023
+ let data = read();
1024
+ return new RegExp(data[0], data[1])
1025
+ };
1026
+ const TEMP_BUNDLE = [];
1027
+ currentExtensions[0x62] = (data) => {
1028
+ let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
1029
+ let dataPosition = position;
1030
+ position += dataSize - data.length;
1031
+ bundledStrings = TEMP_BUNDLE;
1032
+ bundledStrings = [readOnlyJSString(), readOnlyJSString()];
1033
+ bundledStrings.position0 = 0;
1034
+ bundledStrings.position1 = 0;
1035
+ bundledStrings.postBundlePosition = position;
1036
+ position = dataPosition;
1037
+ return read()
1038
+ };
1039
+
1040
+ currentExtensions[0xff] = (data) => {
1041
+ // 32-bit date extension
1042
+ if (data.length == 4)
1043
+ return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
1044
+ else if (data.length == 8)
1045
+ return new Date(
1046
+ ((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
1047
+ ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
1048
+ else if (data.length == 12)// TODO: Implement support for negative
1049
+ return new Date(
1050
+ ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
1051
+ (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
1052
+ else
1053
+ return new Date('invalid')
1054
+ }; // notepack defines extension 0 to mean undefined, so use that as the default here
1055
+ // registration of bulk record definition?
1056
+ // currentExtensions[0x52] = () =>
1057
+
1058
+ function saveState(callback) {
1059
+ if (onSaveState)
1060
+ onSaveState();
1061
+ let savedSrcEnd = srcEnd;
1062
+ let savedPosition = position;
1063
+ let savedStringPosition = stringPosition;
1064
+ let savedSrcStringStart = srcStringStart;
1065
+ let savedSrcStringEnd = srcStringEnd;
1066
+ let savedSrcString = srcString;
1067
+ let savedStrings = strings;
1068
+ let savedReferenceMap = referenceMap;
1069
+ let savedBundledStrings = bundledStrings;
1070
+
1071
+ // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1072
+ let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
1073
+ let savedStructures = currentStructures;
1074
+ let savedStructuresContents = currentStructures.slice(0, currentStructures.length);
1075
+ let savedPackr = currentUnpackr;
1076
+ let savedSequentialMode = sequentialMode;
1077
+ let value = callback();
1078
+ srcEnd = savedSrcEnd;
1079
+ position = savedPosition;
1080
+ stringPosition = savedStringPosition;
1081
+ srcStringStart = savedSrcStringStart;
1082
+ srcStringEnd = savedSrcStringEnd;
1083
+ srcString = savedSrcString;
1084
+ strings = savedStrings;
1085
+ referenceMap = savedReferenceMap;
1086
+ bundledStrings = savedBundledStrings;
1087
+ src = savedSrc;
1088
+ sequentialMode = savedSequentialMode;
1089
+ currentStructures = savedStructures;
1090
+ currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
1091
+ currentUnpackr = savedPackr;
1092
+ dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
1093
+ return value
1094
+ }
1095
+ function clearSource() {
1096
+ src = null;
1097
+ referenceMap = null;
1098
+ currentStructures = null;
1099
+ }
1100
+
1101
+ function addExtension(extension) {
1102
+ if (extension.unpack)
1103
+ currentExtensions[extension.type] = extension.unpack;
1104
+ else
1105
+ currentExtensions[extension.type] = extension;
1106
+ }
1107
+
1108
+ const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1109
+ for (let i = 0; i < 256; i++) {
1110
+ mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
1111
+ }
1112
+ const Decoder = Unpackr;
1113
+ var defaultUnpackr = new Unpackr({ useRecords: false });
1114
+ const unpack = defaultUnpackr.unpack;
1115
+ const unpackMultiple = defaultUnpackr.unpackMultiple;
1116
+ const decode = defaultUnpackr.unpack;
1117
+ const FLOAT32_OPTIONS = {
1118
+ NEVER: 0,
1119
+ ALWAYS: 1,
1120
+ DECIMAL_ROUND: 3,
1121
+ DECIMAL_FIT: 4
1122
+ };
1123
+ let f32Array = new Float32Array(1);
1124
+ let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
1125
+ function roundFloat32(float32Number) {
1126
+ f32Array[0] = float32Number;
1127
+ let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1128
+ return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1129
+ }
1130
+ function setReadStruct(updatedReadStruct, loadedStructs, saveState) {
1131
+ readStruct = updatedReadStruct;
1132
+ onLoadedStructures = loadedStructs;
1133
+ onSaveState = saveState;
1134
+ }
1135
+
1136
+ exports.C1 = C1;
1137
+ exports.C1Type = C1Type;
1138
+ exports.Decoder = Decoder;
1139
+ exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
1140
+ exports.Unpackr = Unpackr;
1141
+ exports.addExtension = addExtension;
1142
+ exports.checkedRead = checkedRead;
1143
+ exports.clearSource = clearSource;
1144
+ exports.decode = decode;
1145
+ exports.getPosition = getPosition;
1146
+ exports.loadStructures = loadStructures;
1147
+ exports.mult10 = mult10;
1148
+ exports.read = read;
1149
+ exports.readString = readString;
1150
+ exports.roundFloat32 = roundFloat32;
1151
+ exports.setExtractor = setExtractor;
1152
+ exports.setReadStruct = setReadStruct;
1153
+ exports.typedArrays = typedArrays;
1154
+ exports.unpack = unpack;
1155
+ exports.unpackMultiple = unpackMultiple;
1156
+
1157
+ }));
1158
+ //# sourceMappingURL=unpack-no-eval.cjs.map