msgpackr 1.6.0 → 1.7.0-alpha1

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
@@ -5,1976 +5,2256 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var stream = require('stream');
6
6
  var module$1 = require('module');
7
7
 
8
- var decoder;
9
- try {
10
- decoder = new TextDecoder();
11
- } catch(error) {}
12
- var src;
13
- var srcEnd;
14
- var position = 0;
15
- const EMPTY_ARRAY = [];
16
- var strings = EMPTY_ARRAY;
17
- var stringPosition = 0;
18
- var currentUnpackr = {};
19
- var currentStructures;
20
- var srcString;
21
- var srcStringStart = 0;
22
- var srcStringEnd = 0;
23
- var bundledStrings;
24
- var referenceMap;
25
- var currentExtensions = [];
26
- var dataView;
27
- var defaultOptions = {
28
- useRecords: false,
29
- mapsAsObjects: true
30
- };
31
- class C1Type {}
32
- const C1 = new C1Type();
33
- C1.name = 'MessagePack 0xC1';
34
- var sequentialMode = false;
35
- var inlineObjectReadThreshold = 2;
36
- try {
37
- new Function('');
38
- } catch(error) {
39
- // if eval variants are not supported, do not create inline object readers ever
40
- inlineObjectReadThreshold = Infinity;
41
- }
42
-
43
- class Unpackr {
44
- constructor(options) {
45
- if (options) {
46
- if (options.useRecords === false && options.mapsAsObjects === undefined)
47
- options.mapsAsObjects = true;
48
- if (options.structures)
49
- options.structures.sharedLength = options.structures.length;
50
- else if (options.getStructures) {
51
- (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
52
- options.structures.sharedLength = 0;
53
- }
54
- }
55
- Object.assign(this, options);
56
- }
57
- unpack(source, end) {
58
- if (src) {
59
- // re-entrant execution, save the state and restore it after we do this unpack
60
- return saveState(() => {
61
- clearSource();
62
- return this ? this.unpack(source, end) : Unpackr.prototype.unpack.call(defaultOptions, source, end)
63
- })
64
- }
65
- srcEnd = end > -1 ? end : source.length;
66
- position = 0;
67
- stringPosition = 0;
68
- srcStringEnd = 0;
69
- srcString = null;
70
- strings = EMPTY_ARRAY;
71
- bundledStrings = null;
72
- src = source;
73
- // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
74
- // technique for getting data from a database where it can be copied into an existing buffer instead of creating
75
- // new ones
76
- try {
77
- dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
78
- } catch(error) {
79
- // if it doesn't have a buffer, maybe it is the wrong type of object
80
- src = null;
81
- if (source instanceof Uint8Array)
82
- throw error
83
- throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
84
- }
85
- if (this instanceof Unpackr) {
86
- currentUnpackr = this;
87
- if (this.structures) {
88
- currentStructures = this.structures;
89
- return checkedRead()
90
- } else if (!currentStructures || currentStructures.length > 0) {
91
- currentStructures = [];
92
- }
93
- } else {
94
- currentUnpackr = defaultOptions;
95
- if (!currentStructures || currentStructures.length > 0)
96
- currentStructures = [];
97
- }
98
- return checkedRead()
99
- }
100
- unpackMultiple(source, forEach) {
101
- let values, lastPosition = 0;
102
- try {
103
- sequentialMode = true;
104
- let size = source.length;
105
- let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
106
- if (forEach) {
107
- forEach(value);
108
- while(position < size) {
109
- lastPosition = position;
110
- if (forEach(checkedRead()) === false) {
111
- return
112
- }
113
- }
114
- }
115
- else {
116
- values = [ value ];
117
- while(position < size) {
118
- lastPosition = position;
119
- values.push(checkedRead());
120
- }
121
- return values
122
- }
123
- } catch(error) {
124
- error.lastPosition = lastPosition;
125
- error.values = values;
126
- throw error
127
- } finally {
128
- sequentialMode = false;
129
- clearSource();
130
- }
131
- }
132
- _mergeStructures(loadedStructures, existingStructures) {
133
- loadedStructures = loadedStructures || [];
134
- for (let i = 0, l = loadedStructures.length; i < l; i++) {
135
- let structure = loadedStructures[i];
136
- if (structure) {
137
- structure.isShared = true;
138
- if (i >= 32)
139
- structure.highByte = (i - 32) >> 5;
140
- }
141
- }
142
- loadedStructures.sharedLength = loadedStructures.length;
143
- for (let id in existingStructures || []) {
144
- if (id >= 0) {
145
- let structure = loadedStructures[id];
146
- let existing = existingStructures[id];
147
- if (existing) {
148
- if (structure)
149
- (loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure;
150
- loadedStructures[id] = existing;
151
- }
152
- }
153
- }
154
- return this.structures = loadedStructures
155
- }
156
- decode(source, end) {
157
- return this.unpack(source, end)
158
- }
159
- }
160
- function checkedRead() {
161
- try {
162
- if (!currentUnpackr.trusted && !sequentialMode) {
163
- let sharedLength = currentStructures.sharedLength || 0;
164
- if (sharedLength < currentStructures.length)
165
- currentStructures.length = sharedLength;
166
- }
167
- let result = read();
168
- if (bundledStrings) // bundled strings to skip past
169
- position = bundledStrings.postBundlePosition;
170
-
171
- if (position == srcEnd) {
172
- // finished reading this source, cleanup references
173
- if (currentStructures.restoreStructures)
174
- restoreStructures();
175
- currentStructures = null;
176
- src = null;
177
- if (referenceMap)
178
- referenceMap = null;
179
- } else if (position > srcEnd) {
180
- // over read
181
- let error = new Error('Unexpected end of MessagePack data');
182
- error.incomplete = true;
183
- throw error
184
- } else if (!sequentialMode) {
185
- throw new Error('Data read, but end of buffer not reached')
186
- }
187
- // else more to read, but we are reading sequentially, so don't clear source yet
188
- return result
189
- } catch(error) {
190
- if (currentStructures.restoreStructures)
191
- restoreStructures();
192
- clearSource();
193
- if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer')) {
194
- error.incomplete = true;
195
- }
196
- throw error
197
- }
198
- }
199
-
200
- function restoreStructures() {
201
- for (let id in currentStructures.restoreStructures) {
202
- currentStructures[id] = currentStructures.restoreStructures[id];
203
- }
204
- currentStructures.restoreStructures = null;
205
- }
206
-
207
- function read() {
208
- let token = src[position++];
209
- if (token < 0xa0) {
210
- if (token < 0x80) {
211
- if (token < 0x40)
212
- return token
213
- else {
214
- let structure = currentStructures[token & 0x3f] ||
215
- currentUnpackr.getStructures && loadStructures()[token & 0x3f];
216
- if (structure) {
217
- if (!structure.read) {
218
- structure.read = createStructureReader(structure, token & 0x3f);
219
- }
220
- return structure.read()
221
- } else
222
- return token
223
- }
224
- } else if (token < 0x90) {
225
- // map
226
- token -= 0x80;
227
- if (currentUnpackr.mapsAsObjects) {
228
- let object = {};
229
- for (let i = 0; i < token; i++) {
230
- object[readKey()] = read();
231
- }
232
- return object
233
- } else {
234
- let map = new Map();
235
- for (let i = 0; i < token; i++) {
236
- map.set(read(), read());
237
- }
238
- return map
239
- }
240
- } else {
241
- token -= 0x90;
242
- let array = new Array(token);
243
- for (let i = 0; i < token; i++) {
244
- array[i] = read();
245
- }
246
- return array
247
- }
248
- } else if (token < 0xc0) {
249
- // fixstr
250
- let length = token - 0xa0;
251
- if (srcStringEnd >= position) {
252
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
253
- }
254
- if (srcStringEnd == 0 && srcEnd < 140) {
255
- // for small blocks, avoiding the overhead of the extract call is helpful
256
- let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
257
- if (string != null)
258
- return string
259
- }
260
- return readFixedString(length)
261
- } else {
262
- let value;
263
- switch (token) {
264
- case 0xc0: return null
265
- case 0xc1:
266
- if (bundledStrings) {
267
- value = read(); // followed by the length of the string in characters (not bytes!)
268
- if (value > 0)
269
- return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
270
- else
271
- return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
272
- }
273
- return C1; // "never-used", return special object to denote that
274
- case 0xc2: return false
275
- case 0xc3: return true
276
- case 0xc4:
277
- // bin 8
278
- return readBin(src[position++])
279
- case 0xc5:
280
- // bin 16
281
- value = dataView.getUint16(position);
282
- position += 2;
283
- return readBin(value)
284
- case 0xc6:
285
- // bin 32
286
- value = dataView.getUint32(position);
287
- position += 4;
288
- return readBin(value)
289
- case 0xc7:
290
- // ext 8
291
- return readExt(src[position++])
292
- case 0xc8:
293
- // ext 16
294
- value = dataView.getUint16(position);
295
- position += 2;
296
- return readExt(value)
297
- case 0xc9:
298
- // ext 32
299
- value = dataView.getUint32(position);
300
- position += 4;
301
- return readExt(value)
302
- case 0xca:
303
- value = dataView.getFloat32(position);
304
- if (currentUnpackr.useFloat32 > 2) {
305
- // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
306
- let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
307
- position += 4;
308
- return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
309
- }
310
- position += 4;
311
- return value
312
- case 0xcb:
313
- value = dataView.getFloat64(position);
314
- position += 8;
315
- return value
316
- // uint handlers
317
- case 0xcc:
318
- return src[position++]
319
- case 0xcd:
320
- value = dataView.getUint16(position);
321
- position += 2;
322
- return value
323
- case 0xce:
324
- value = dataView.getUint32(position);
325
- position += 4;
326
- return value
327
- case 0xcf:
328
- if (currentUnpackr.int64AsNumber) {
329
- value = dataView.getUint32(position) * 0x100000000;
330
- value += dataView.getUint32(position + 4);
331
- } else
332
- value = dataView.getBigUint64(position);
333
- position += 8;
334
- return value
335
-
336
- // int handlers
337
- case 0xd0:
338
- return dataView.getInt8(position++)
339
- case 0xd1:
340
- value = dataView.getInt16(position);
341
- position += 2;
342
- return value
343
- case 0xd2:
344
- value = dataView.getInt32(position);
345
- position += 4;
346
- return value
347
- case 0xd3:
348
- if (currentUnpackr.int64AsNumber) {
349
- value = dataView.getInt32(position) * 0x100000000;
350
- value += dataView.getUint32(position + 4);
351
- } else
352
- value = dataView.getBigInt64(position);
353
- position += 8;
354
- return value
355
-
356
- case 0xd4:
357
- // fixext 1
358
- value = src[position++];
359
- if (value == 0x72) {
360
- return recordDefinition(src[position++] & 0x3f)
361
- } else {
362
- let extension = currentExtensions[value];
363
- if (extension) {
364
- if (extension.read) {
365
- position++; // skip filler byte
366
- return extension.read(read())
367
- } else if (extension.noBuffer) {
368
- position++; // skip filler byte
369
- return extension()
370
- } else
371
- return extension(src.subarray(position, ++position))
372
- } else
373
- throw new Error('Unknown extension ' + value)
374
- }
375
- case 0xd5:
376
- // fixext 2
377
- value = src[position];
378
- if (value == 0x72) {
379
- position++;
380
- return recordDefinition(src[position++] & 0x3f, src[position++])
381
- } else
382
- return readExt(2)
383
- case 0xd6:
384
- // fixext 4
385
- return readExt(4)
386
- case 0xd7:
387
- // fixext 8
388
- return readExt(8)
389
- case 0xd8:
390
- // fixext 16
391
- return readExt(16)
392
- case 0xd9:
393
- // str 8
394
- value = src[position++];
395
- if (srcStringEnd >= position) {
396
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
397
- }
398
- return readString8(value)
399
- case 0xda:
400
- // str 16
401
- value = dataView.getUint16(position);
402
- position += 2;
403
- if (srcStringEnd >= position) {
404
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
405
- }
406
- return readString16(value)
407
- case 0xdb:
408
- // str 32
409
- value = dataView.getUint32(position);
410
- position += 4;
411
- if (srcStringEnd >= position) {
412
- return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
413
- }
414
- return readString32(value)
415
- case 0xdc:
416
- // array 16
417
- value = dataView.getUint16(position);
418
- position += 2;
419
- return readArray(value)
420
- case 0xdd:
421
- // array 32
422
- value = dataView.getUint32(position);
423
- position += 4;
424
- return readArray(value)
425
- case 0xde:
426
- // map 16
427
- value = dataView.getUint16(position);
428
- position += 2;
429
- return readMap(value)
430
- case 0xdf:
431
- // map 32
432
- value = dataView.getUint32(position);
433
- position += 4;
434
- return readMap(value)
435
- default: // negative int
436
- if (token >= 0xe0)
437
- return token - 0x100
438
- if (token === undefined) {
439
- let error = new Error('Unexpected end of MessagePack data');
440
- error.incomplete = true;
441
- throw error
442
- }
443
- throw new Error('Unknown MessagePack token ' + token)
444
-
445
- }
446
- }
447
- }
448
- const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
449
- function createStructureReader(structure, firstId) {
450
- function readObject() {
451
- // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
452
- if (readObject.count++ > inlineObjectReadThreshold) {
453
- let readObject = structure.read = (new Function('r', 'return function(){return {' + structure.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}}'))(read);
454
- if (structure.highByte === 0)
455
- structure.read = createSecondByteReader(firstId, structure.read);
456
- return readObject() // second byte is already read, if there is one so immediately read object
457
- }
458
- let object = {};
459
- for (let i = 0, l = structure.length; i < l; i++) {
460
- let key = structure[i];
461
- object[key] = read();
462
- }
463
- return object
464
- }
465
- readObject.count = 0;
466
- if (structure.highByte === 0) {
467
- return createSecondByteReader(firstId, readObject)
468
- }
469
- return readObject
470
- }
471
-
472
- const createSecondByteReader = (firstId, read0) => {
473
- return function() {
474
- let highByte = src[position++];
475
- if (highByte === 0)
476
- return read0()
477
- let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
478
- let structure = currentStructures[id] || loadStructures()[id];
479
- if (!structure) {
480
- throw new Error('Record id is not defined for ' + id)
481
- }
482
- if (!structure.read)
483
- structure.read = createStructureReader(structure, firstId);
484
- return structure.read()
485
- }
486
- };
487
-
488
- function loadStructures() {
489
- let loadedStructures = saveState(() => {
490
- // save the state in case getStructures modifies our buffer
491
- src = null;
492
- return currentUnpackr.getStructures()
493
- });
494
- return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
495
- }
496
-
497
- var readFixedString = readStringJS;
498
- var readString8 = readStringJS;
499
- var readString16 = readStringJS;
500
- var readString32 = readStringJS;
501
- exports.isNativeAccelerationEnabled = false;
502
-
503
- function setExtractor(extractStrings) {
504
- exports.isNativeAccelerationEnabled = true;
505
- readFixedString = readString(1);
506
- readString8 = readString(2);
507
- readString16 = readString(3);
508
- readString32 = readString(5);
509
- function readString(headerLength) {
510
- return function readString(length) {
511
- let string = strings[stringPosition++];
512
- if (string == null) {
513
- if (bundledStrings)
514
- return readStringJS(length)
515
- let extraction = extractStrings(position - headerLength, srcEnd, src);
516
- if (typeof extraction == 'string') {
517
- string = extraction;
518
- strings = EMPTY_ARRAY;
519
- } else {
520
- strings = extraction;
521
- stringPosition = 1;
522
- 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
523
- string = strings[0];
524
- if (string === undefined)
525
- throw new Error('Unexpected end of buffer')
526
- }
527
- }
528
- let srcStringLength = string.length;
529
- if (srcStringLength <= length) {
530
- position += length;
531
- return string
532
- }
533
- srcString = string;
534
- srcStringStart = position;
535
- srcStringEnd = position + srcStringLength;
536
- position += length;
537
- return string.slice(0, length) // we know we just want the beginning
538
- }
539
- }
540
- }
541
- function readStringJS(length) {
542
- let result;
543
- if (length < 16) {
544
- if (result = shortStringInJS(length))
545
- return result
546
- }
547
- if (length > 64 && decoder)
548
- return decoder.decode(src.subarray(position, position += length))
549
- const end = position + length;
550
- const units = [];
551
- result = '';
552
- while (position < end) {
553
- const byte1 = src[position++];
554
- if ((byte1 & 0x80) === 0) {
555
- // 1 byte
556
- units.push(byte1);
557
- } else if ((byte1 & 0xe0) === 0xc0) {
558
- // 2 bytes
559
- const byte2 = src[position++] & 0x3f;
560
- units.push(((byte1 & 0x1f) << 6) | byte2);
561
- } else if ((byte1 & 0xf0) === 0xe0) {
562
- // 3 bytes
563
- const byte2 = src[position++] & 0x3f;
564
- const byte3 = src[position++] & 0x3f;
565
- units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
566
- } else if ((byte1 & 0xf8) === 0xf0) {
567
- // 4 bytes
568
- const byte2 = src[position++] & 0x3f;
569
- const byte3 = src[position++] & 0x3f;
570
- const byte4 = src[position++] & 0x3f;
571
- let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
572
- if (unit > 0xffff) {
573
- unit -= 0x10000;
574
- units.push(((unit >>> 10) & 0x3ff) | 0xd800);
575
- unit = 0xdc00 | (unit & 0x3ff);
576
- }
577
- units.push(unit);
578
- } else {
579
- units.push(byte1);
580
- }
581
-
582
- if (units.length >= 0x1000) {
583
- result += fromCharCode.apply(String, units);
584
- units.length = 0;
585
- }
586
- }
587
-
588
- if (units.length > 0) {
589
- result += fromCharCode.apply(String, units);
590
- }
591
-
592
- return result
593
- }
594
-
595
- function readArray(length) {
596
- let array = new Array(length);
597
- for (let i = 0; i < length; i++) {
598
- array[i] = read();
599
- }
600
- return array
601
- }
602
-
603
- function readMap(length) {
604
- if (currentUnpackr.mapsAsObjects) {
605
- let object = {};
606
- for (let i = 0; i < length; i++) {
607
- object[readKey()] = read();
608
- }
609
- return object
610
- } else {
611
- let map = new Map();
612
- for (let i = 0; i < length; i++) {
613
- map.set(read(), read());
614
- }
615
- return map
616
- }
617
- }
618
-
619
- var fromCharCode = String.fromCharCode;
620
- function longStringInJS(length) {
621
- let start = position;
622
- let bytes = new Array(length);
623
- for (let i = 0; i < length; i++) {
624
- const byte = src[position++];
625
- if ((byte & 0x80) > 0) {
626
- position = start;
627
- return
628
- }
629
- bytes[i] = byte;
630
- }
631
- return fromCharCode.apply(String, bytes)
632
- }
633
- function shortStringInJS(length) {
634
- if (length < 4) {
635
- if (length < 2) {
636
- if (length === 0)
637
- return ''
638
- else {
639
- let a = src[position++];
640
- if ((a & 0x80) > 1) {
641
- position -= 1;
642
- return
643
- }
644
- return fromCharCode(a)
645
- }
646
- } else {
647
- let a = src[position++];
648
- let b = src[position++];
649
- if ((a & 0x80) > 0 || (b & 0x80) > 0) {
650
- position -= 2;
651
- return
652
- }
653
- if (length < 3)
654
- return fromCharCode(a, b)
655
- let c = src[position++];
656
- if ((c & 0x80) > 0) {
657
- position -= 3;
658
- return
659
- }
660
- return fromCharCode(a, b, c)
661
- }
662
- } else {
663
- let a = src[position++];
664
- let b = src[position++];
665
- let c = src[position++];
666
- let d = src[position++];
667
- if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
668
- position -= 4;
669
- return
670
- }
671
- if (length < 6) {
672
- if (length === 4)
673
- return fromCharCode(a, b, c, d)
674
- else {
675
- let e = src[position++];
676
- if ((e & 0x80) > 0) {
677
- position -= 5;
678
- return
679
- }
680
- return fromCharCode(a, b, c, d, e)
681
- }
682
- } else if (length < 8) {
683
- let e = src[position++];
684
- let f = src[position++];
685
- if ((e & 0x80) > 0 || (f & 0x80) > 0) {
686
- position -= 6;
687
- return
688
- }
689
- if (length < 7)
690
- return fromCharCode(a, b, c, d, e, f)
691
- let g = src[position++];
692
- if ((g & 0x80) > 0) {
693
- position -= 7;
694
- return
695
- }
696
- return fromCharCode(a, b, c, d, e, f, g)
697
- } else {
698
- let e = src[position++];
699
- let f = src[position++];
700
- let g = src[position++];
701
- let h = src[position++];
702
- if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
703
- position -= 8;
704
- return
705
- }
706
- if (length < 10) {
707
- if (length === 8)
708
- return fromCharCode(a, b, c, d, e, f, g, h)
709
- else {
710
- let i = src[position++];
711
- if ((i & 0x80) > 0) {
712
- position -= 9;
713
- return
714
- }
715
- return fromCharCode(a, b, c, d, e, f, g, h, i)
716
- }
717
- } else if (length < 12) {
718
- let i = src[position++];
719
- let j = src[position++];
720
- if ((i & 0x80) > 0 || (j & 0x80) > 0) {
721
- position -= 10;
722
- return
723
- }
724
- if (length < 11)
725
- return fromCharCode(a, b, c, d, e, f, g, h, i, j)
726
- let k = src[position++];
727
- if ((k & 0x80) > 0) {
728
- position -= 11;
729
- return
730
- }
731
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
732
- } else {
733
- let i = src[position++];
734
- let j = src[position++];
735
- let k = src[position++];
736
- let l = src[position++];
737
- if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
738
- position -= 12;
739
- return
740
- }
741
- if (length < 14) {
742
- if (length === 12)
743
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
744
- else {
745
- let m = src[position++];
746
- if ((m & 0x80) > 0) {
747
- position -= 13;
748
- return
749
- }
750
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
751
- }
752
- } else {
753
- let m = src[position++];
754
- let n = src[position++];
755
- if ((m & 0x80) > 0 || (n & 0x80) > 0) {
756
- position -= 14;
757
- return
758
- }
759
- if (length < 15)
760
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
761
- let o = src[position++];
762
- if ((o & 0x80) > 0) {
763
- position -= 15;
764
- return
765
- }
766
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
767
- }
768
- }
769
- }
770
- }
771
- }
772
-
773
- function readOnlyJSString() {
774
- let token = src[position++];
775
- let length;
776
- if (token < 0xc0) {
777
- // fixstr
778
- length = token - 0xa0;
779
- } else {
780
- switch(token) {
781
- case 0xd9:
782
- // str 8
783
- length = src[position++];
784
- break
785
- case 0xda:
786
- // str 16
787
- length = dataView.getUint16(position);
788
- position += 2;
789
- break
790
- case 0xdb:
791
- // str 32
792
- length = dataView.getUint32(position);
793
- position += 4;
794
- break
795
- default:
796
- throw new Error('Expected string')
797
- }
798
- }
799
- return readStringJS(length)
800
- }
801
-
802
-
803
- function readBin(length) {
804
- return currentUnpackr.copyBuffers ?
805
- // specifically use the copying slice (not the node one)
806
- Uint8Array.prototype.slice.call(src, position, position += length) :
807
- src.subarray(position, position += length)
808
- }
809
- function readExt(length) {
810
- let type = src[position++];
811
- if (currentExtensions[type]) {
812
- return currentExtensions[type](src.subarray(position, position += length))
813
- }
814
- else
815
- throw new Error('Unknown extension type ' + type)
816
- }
817
-
818
- var keyCache = new Array(4096);
819
- function readKey() {
820
- let length = src[position++];
821
- if (length >= 0xa0 && length < 0xc0) {
822
- // fixstr, potentially use key cache
823
- length = length - 0xa0;
824
- if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
825
- return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
826
- else if (!(srcStringEnd == 0 && srcEnd < 180))
827
- return readFixedString(length)
828
- } else { // not cacheable, go back and do a standard read
829
- position--;
830
- return read()
831
- }
832
- let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
833
- let entry = keyCache[key];
834
- let checkPosition = position;
835
- let end = position + length - 3;
836
- let chunk;
837
- let i = 0;
838
- if (entry && entry.bytes == length) {
839
- while (checkPosition < end) {
840
- chunk = dataView.getUint32(checkPosition);
841
- if (chunk != entry[i++]) {
842
- checkPosition = 0x70000000;
843
- break
844
- }
845
- checkPosition += 4;
846
- }
847
- end += 3;
848
- while (checkPosition < end) {
849
- chunk = src[checkPosition++];
850
- if (chunk != entry[i++]) {
851
- checkPosition = 0x70000000;
852
- break
853
- }
854
- }
855
- if (checkPosition === end) {
856
- position = checkPosition;
857
- return entry.string
858
- }
859
- end -= 3;
860
- checkPosition = position;
861
- }
862
- entry = [];
863
- keyCache[key] = entry;
864
- entry.bytes = length;
865
- while (checkPosition < end) {
866
- chunk = dataView.getUint32(checkPosition);
867
- entry.push(chunk);
868
- checkPosition += 4;
869
- }
870
- end += 3;
871
- while (checkPosition < end) {
872
- chunk = src[checkPosition++];
873
- entry.push(chunk);
874
- }
875
- // for small blocks, avoiding the overhead of the extract call is helpful
876
- let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
877
- if (string != null)
878
- return entry.string = string
879
- return entry.string = readFixedString(length)
880
- }
881
-
882
- // the registration of the record definition extension (as "r")
883
- const recordDefinition = (id, highByte) => {
884
- var structure = read();
885
- let firstByte = id;
886
- if (highByte !== undefined) {
887
- id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
888
- structure.highByte = highByte;
889
- }
890
- let existingStructure = currentStructures[id];
891
- if (existingStructure && existingStructure.isShared) {
892
- (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
893
- }
894
- currentStructures[id] = structure;
895
- structure.read = createStructureReader(structure, firstByte);
896
- return structure.read()
897
- };
898
- currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
899
- currentExtensions[0].noBuffer = true;
900
-
901
- currentExtensions[0x65] = () => {
902
- let data = read();
903
- return (globalThis[data[0]] || Error)(data[1])
904
- };
905
-
906
- currentExtensions[0x69] = (data) => {
907
- // id extension (for structured clones)
908
- let id = dataView.getUint32(position - 4);
909
- if (!referenceMap)
910
- referenceMap = new Map();
911
- let token = src[position];
912
- let target;
913
- // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
914
- // ahead past references to record structure definitions
915
- if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
916
- target = [];
917
- else
918
- target = {};
919
-
920
- let refEntry = { target }; // a placeholder object
921
- referenceMap.set(id, refEntry);
922
- let targetProperties = read(); // read the next value as the target object to id
923
- if (refEntry.used) // there is a cycle, so we have to assign properties to original target
924
- return Object.assign(target, targetProperties)
925
- refEntry.target = targetProperties; // the placeholder wasn't used, replace with the deserialized one
926
- return targetProperties // no cycle, can just use the returned read object
927
- };
928
-
929
- currentExtensions[0x70] = (data) => {
930
- // pointer extension (for structured clones)
931
- let id = dataView.getUint32(position - 4);
932
- let refEntry = referenceMap.get(id);
933
- refEntry.used = true;
934
- return refEntry.target
935
- };
936
-
937
- currentExtensions[0x73] = () => new Set(read());
938
-
939
- const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
940
-
941
- currentExtensions[0x74] = (data) => {
942
- let typeCode = data[0];
943
- let typedArrayName = typedArrays[typeCode];
944
- if (!typedArrayName)
945
- throw new Error('Could not find typed array for code ' + typeCode)
946
- // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
947
- return new globalThis[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
948
- };
949
- currentExtensions[0x78] = () => {
950
- let data = read();
951
- return new RegExp(data[0], data[1])
952
- };
953
- const TEMP_BUNDLE = [];
954
- currentExtensions[0x62] = (data) => {
955
- let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
956
- let dataPosition = position;
957
- position += dataSize - data.length;
958
- bundledStrings = TEMP_BUNDLE;
959
- bundledStrings = [readOnlyJSString(), readOnlyJSString()];
960
- bundledStrings.position0 = 0;
961
- bundledStrings.position1 = 0;
962
- bundledStrings.postBundlePosition = position;
963
- position = dataPosition;
964
- return read()
965
- };
966
-
967
- currentExtensions[0xff] = (data) => {
968
- // 32-bit date extension
969
- if (data.length == 4)
970
- return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
971
- else if (data.length == 8)
972
- return new Date(
973
- ((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
974
- ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
975
- else if (data.length == 12)// TODO: Implement support for negative
976
- return new Date(
977
- ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
978
- (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
979
- else
980
- return new Date('invalid')
981
- }; // notepack defines extension 0 to mean undefined, so use that as the default here
982
- // registration of bulk record definition?
983
- // currentExtensions[0x52] = () =>
984
-
985
- function saveState(callback) {
986
- let savedSrcEnd = srcEnd;
987
- let savedPosition = position;
988
- let savedStringPosition = stringPosition;
989
- let savedSrcStringStart = srcStringStart;
990
- let savedSrcStringEnd = srcStringEnd;
991
- let savedSrcString = srcString;
992
- let savedStrings = strings;
993
- let savedReferenceMap = referenceMap;
994
- let savedBundledStrings = bundledStrings;
995
-
996
- // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
997
- let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
998
- let savedStructures = currentStructures;
999
- let savedStructuresContents = currentStructures.slice(0, currentStructures.length);
1000
- let savedPackr = currentUnpackr;
1001
- let savedSequentialMode = sequentialMode;
1002
- let value = callback();
1003
- srcEnd = savedSrcEnd;
1004
- position = savedPosition;
1005
- stringPosition = savedStringPosition;
1006
- srcStringStart = savedSrcStringStart;
1007
- srcStringEnd = savedSrcStringEnd;
1008
- srcString = savedSrcString;
1009
- strings = savedStrings;
1010
- referenceMap = savedReferenceMap;
1011
- bundledStrings = savedBundledStrings;
1012
- src = savedSrc;
1013
- sequentialMode = savedSequentialMode;
1014
- currentStructures = savedStructures;
1015
- currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
1016
- currentUnpackr = savedPackr;
1017
- dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
1018
- return value
1019
- }
1020
- function clearSource() {
1021
- src = null;
1022
- referenceMap = null;
1023
- currentStructures = null;
1024
- }
1025
-
1026
- function addExtension(extension) {
1027
- if (extension.unpack)
1028
- currentExtensions[extension.type] = extension.unpack;
1029
- else
1030
- currentExtensions[extension.type] = extension;
1031
- }
1032
-
1033
- const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1034
- for (let i = 0; i < 256; i++) {
1035
- mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
1036
- }
1037
- const Decoder = Unpackr;
1038
- var defaultUnpackr = new Unpackr({ useRecords: false });
1039
- const unpack = defaultUnpackr.unpack;
1040
- const unpackMultiple = defaultUnpackr.unpackMultiple;
1041
- const decode = defaultUnpackr.unpack;
1042
- const FLOAT32_OPTIONS = {
1043
- NEVER: 0,
1044
- ALWAYS: 1,
1045
- DECIMAL_ROUND: 3,
1046
- DECIMAL_FIT: 4
1047
- };
1048
- let f32Array = new Float32Array(1);
1049
- let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
1050
- function roundFloat32(float32Number) {
1051
- f32Array[0] = float32Number;
1052
- let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1053
- return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
8
+ var decoder;
9
+ try {
10
+ decoder = new TextDecoder();
11
+ } catch(error) {}
12
+ var src;
13
+ var srcEnd;
14
+ var position = 0;
15
+ const EMPTY_ARRAY = [];
16
+ var strings = EMPTY_ARRAY;
17
+ var stringPosition = 0;
18
+ var currentUnpackr = {};
19
+ var currentStructures;
20
+ var srcString;
21
+ var srcStringStart = 0;
22
+ var srcStringEnd = 0;
23
+ var bundledStrings;
24
+ var referenceMap;
25
+ var currentExtensions = [];
26
+ var dataView;
27
+ var defaultOptions = {
28
+ useRecords: false,
29
+ mapsAsObjects: true
30
+ };
31
+ class C1Type {}
32
+ const C1 = new C1Type();
33
+ C1.name = 'MessagePack 0xC1';
34
+ var sequentialMode = false;
35
+ var inlineObjectReadThreshold = 2;
36
+ var readStruct;
37
+ try {
38
+ new Function('');
39
+ } catch(error) {
40
+ // if eval variants are not supported, do not create inline object readers ever
41
+ inlineObjectReadThreshold = Infinity;
1054
42
  }
1055
43
 
1056
- let textEncoder;
1057
- try {
1058
- textEncoder = new TextEncoder();
1059
- } catch (error) {}
1060
- let extensions, extensionClasses;
1061
- const hasNodeBuffer = typeof Buffer !== 'undefined';
1062
- const ByteArrayAllocate = hasNodeBuffer ?
1063
- function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
1064
- const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1065
- const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1066
- let target, keysTarget;
1067
- let targetView;
1068
- let position$1 = 0;
1069
- let safeEnd;
1070
- let bundledStrings$1 = null;
1071
- const MAX_BUNDLE_SIZE = 0xf000;
1072
- const hasNonLatin = /[\u0080-\uFFFF]/;
1073
- const RECORD_SYMBOL = Symbol('record-id');
1074
- class Packr extends Unpackr {
1075
- constructor(options) {
1076
- super(options);
1077
- this.offset = 0;
1078
- let start;
1079
- let hasSharedUpdate;
1080
- let structures;
1081
- let referenceMap;
1082
- let lastSharedStructuresLength = 0;
1083
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1084
- return target.utf8Write(string, position, 0xffffffff)
1085
- } : (textEncoder && textEncoder.encodeInto) ?
1086
- function(string, position) {
1087
- return textEncoder.encodeInto(string, target.subarray(position)).written
1088
- } : false;
1089
-
1090
- let packr = this;
1091
- if (!options)
1092
- options = {};
1093
- let isSequential = options && options.sequential;
1094
- let hasSharedStructures = options.structures || options.saveStructures;
1095
- let maxSharedStructures = options.maxSharedStructures;
1096
- if (maxSharedStructures == null)
1097
- maxSharedStructures = hasSharedStructures ? 32 : 0;
1098
- if (maxSharedStructures > 8160)
1099
- throw new Error('Maximum maxSharedStructure is 8160')
1100
- if (options.structuredClone && options.moreTypes == undefined) {
1101
- options.moreTypes = true;
1102
- }
1103
- let maxOwnStructures = options.maxOwnStructures;
1104
- if (maxOwnStructures == null)
1105
- maxOwnStructures = hasSharedStructures ? 32 : 64;
1106
- if (!this.structures && options.useRecords != false)
1107
- this.structures = [];
1108
- // two byte record ids for shared structures
1109
- let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1110
- let sharedLimitId = maxSharedStructures + 0x40;
1111
- let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
1112
- if (maxStructureId > 8256) {
1113
- throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
1114
- }
1115
- let recordIdsToRemove = [];
1116
- let transitionsCount = 0;
1117
- let serializationsSinceTransitionRebuild = 0;
1118
-
1119
- this.pack = this.encode = function(value, encodeOptions) {
1120
- if (!target) {
1121
- target = new ByteArrayAllocate(8192);
1122
- targetView = new DataView(target.buffer, 0, 8192);
1123
- position$1 = 0;
1124
- }
1125
- safeEnd = target.length - 10;
1126
- if (safeEnd - position$1 < 0x800) {
1127
- // don't start too close to the end,
1128
- target = new ByteArrayAllocate(target.length);
1129
- targetView = new DataView(target.buffer, 0, target.length);
1130
- safeEnd = target.length - 10;
1131
- position$1 = 0;
1132
- } else
1133
- position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1134
- start = position$1;
1135
- referenceMap = packr.structuredClone ? new Map() : null;
1136
- if (packr.bundleStrings && typeof value !== 'string') {
1137
- bundledStrings$1 = [];
1138
- bundledStrings$1.size = Infinity; // force a new bundle start on first string
1139
- } else
1140
- bundledStrings$1 = null;
1141
- structures = packr.structures;
1142
- if (structures) {
1143
- if (structures.uninitialized)
1144
- structures = packr._mergeStructures(packr.getStructures());
1145
- let sharedLength = structures.sharedLength || 0;
1146
- if (sharedLength > maxSharedStructures) {
1147
- //if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
1148
- throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
1149
- }
1150
- if (!structures.transitions) {
1151
- // rebuild our structure transitions
1152
- structures.transitions = Object.create(null);
1153
- for (let i = 0; i < sharedLength; i++) {
1154
- let keys = structures[i];
1155
- if (!keys)
1156
- continue
1157
- let nextTransition, transition = structures.transitions;
1158
- for (let j = 0, l = keys.length; j < l; j++) {
1159
- let key = keys[j];
1160
- nextTransition = transition[key];
1161
- if (!nextTransition) {
1162
- nextTransition = transition[key] = Object.create(null);
1163
- }
1164
- transition = nextTransition;
1165
- }
1166
- transition[RECORD_SYMBOL] = i + 0x40;
1167
- }
1168
- lastSharedStructuresLength = sharedLength;
1169
- }
1170
- if (!isSequential) {
1171
- structures.nextId = sharedLength + 0x40;
1172
- }
1173
- }
1174
- if (hasSharedUpdate)
1175
- hasSharedUpdate = false;
1176
- try {
1177
- pack(value);
1178
- if (bundledStrings$1) {
1179
- writeBundles(start, pack);
1180
- }
1181
- packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1182
- if (referenceMap && referenceMap.idsToInsert) {
1183
- position$1 += referenceMap.idsToInsert.length * 6;
1184
- if (position$1 > safeEnd)
1185
- makeRoom(position$1);
1186
- packr.offset = position$1;
1187
- let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
1188
- referenceMap = null;
1189
- return serialized
1190
- }
1191
- if (encodeOptions & REUSE_BUFFER_MODE) {
1192
- target.start = start;
1193
- target.end = position$1;
1194
- return target
1195
- }
1196
- return target.subarray(start, position$1) // position can change if we call pack again in saveStructures, so we get the buffer now
1197
- } finally {
1198
- if (structures) {
1199
- if (serializationsSinceTransitionRebuild < 10)
1200
- serializationsSinceTransitionRebuild++;
1201
- let sharedLength = structures.sharedLength || maxSharedStructures;
1202
- if (structures.length > sharedLength)
1203
- structures.length = sharedLength;
1204
- if (transitionsCount > 10000) {
1205
- // force a rebuild occasionally after a lot of transitions so it can get cleaned up
1206
- structures.transitions = null;
1207
- serializationsSinceTransitionRebuild = 0;
1208
- transitionsCount = 0;
1209
- if (recordIdsToRemove.length > 0)
1210
- recordIdsToRemove = [];
1211
- } else if (recordIdsToRemove.length > 0 && !isSequential) {
1212
- for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
1213
- recordIdsToRemove[i][RECORD_SYMBOL] = 0;
1214
- }
1215
- recordIdsToRemove = [];
1216
- }
1217
- if (hasSharedUpdate && packr.saveStructures) {
1218
- // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1219
- let returnBuffer = target.subarray(start, position$1);
1220
- if (packr.saveStructures(structures, lastSharedStructuresLength) === false) {
1221
- // get updated structures and try again if the update failed
1222
- packr._mergeStructures(packr.getStructures());
1223
- return packr.pack(value)
1224
- }
1225
- lastSharedStructuresLength = sharedLength;
1226
- return returnBuffer
1227
- }
1228
- }
1229
- if (encodeOptions & RESET_BUFFER_MODE)
1230
- position$1 = start;
1231
- }
1232
- };
1233
- const pack = (value) => {
1234
- if (position$1 > safeEnd)
1235
- target = makeRoom(position$1);
1236
-
1237
- var type = typeof value;
1238
- var length;
1239
- if (type === 'string') {
1240
- let strLength = value.length;
1241
- if (bundledStrings$1 && strLength >= 4 && strLength < 0x1000) {
1242
- if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1243
- let extStart;
1244
- let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1245
- if (position$1 + maxBytes > safeEnd)
1246
- target = makeRoom(position$1 + maxBytes);
1247
- if (bundledStrings$1.position) { // here we use the 0x62 extension to write the last bundle and reserve sapce for the reference pointer to the next/current bundle
1248
- target[position$1] = 0xc8; // ext 16
1249
- position$1 += 3; // reserve for the writing bundle size
1250
- target[position$1++] = 0x62; // 'b'
1251
- extStart = position$1 - start;
1252
- position$1 += 4; // reserve for writing bundle reference
1253
- writeBundles(start, pack); // write the last bundles
1254
- targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1255
- } else { // here we use the 0x62 extension just to reserve the space for the reference pointer to the bundle (will be updated once the bundle is written)
1256
- target[position$1++] = 0xd6; // fixext 4
1257
- target[position$1++] = 0x62; // 'b'
1258
- extStart = position$1 - start;
1259
- position$1 += 4; // reserve for writing bundle reference
1260
- }
1261
- bundledStrings$1 = ['', '']; // create new ones
1262
- bundledStrings$1.size = 0;
1263
- bundledStrings$1.position = extStart;
1264
- }
1265
- let twoByte = hasNonLatin.test(value);
1266
- bundledStrings$1[twoByte ? 0 : 1] += value;
1267
- target[position$1++] = 0xc1;
1268
- pack(twoByte ? -strLength : strLength);
1269
- return
1270
- }
1271
- let headerSize;
1272
- // first we estimate the header size, so we can write to the correct location
1273
- if (strLength < 0x20) {
1274
- headerSize = 1;
1275
- } else if (strLength < 0x100) {
1276
- headerSize = 2;
1277
- } else if (strLength < 0x10000) {
1278
- headerSize = 3;
1279
- } else {
1280
- headerSize = 5;
1281
- }
1282
- let maxBytes = strLength * 3;
1283
- if (position$1 + maxBytes > safeEnd)
1284
- target = makeRoom(position$1 + maxBytes);
1285
-
1286
- if (strLength < 0x40 || !encodeUtf8) {
1287
- let i, c1, c2, strPosition = position$1 + headerSize;
1288
- for (i = 0; i < strLength; i++) {
1289
- c1 = value.charCodeAt(i);
1290
- if (c1 < 0x80) {
1291
- target[strPosition++] = c1;
1292
- } else if (c1 < 0x800) {
1293
- target[strPosition++] = c1 >> 6 | 0xc0;
1294
- target[strPosition++] = c1 & 0x3f | 0x80;
1295
- } else if (
1296
- (c1 & 0xfc00) === 0xd800 &&
1297
- ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
1298
- ) {
1299
- c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
1300
- i++;
1301
- target[strPosition++] = c1 >> 18 | 0xf0;
1302
- target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
1303
- target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
1304
- target[strPosition++] = c1 & 0x3f | 0x80;
1305
- } else {
1306
- target[strPosition++] = c1 >> 12 | 0xe0;
1307
- target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
1308
- target[strPosition++] = c1 & 0x3f | 0x80;
1309
- }
1310
- }
1311
- length = strPosition - position$1 - headerSize;
1312
- } else {
1313
- length = encodeUtf8(value, position$1 + headerSize);
1314
- }
1315
-
1316
- if (length < 0x20) {
1317
- target[position$1++] = 0xa0 | length;
1318
- } else if (length < 0x100) {
1319
- if (headerSize < 2) {
1320
- target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1321
- }
1322
- target[position$1++] = 0xd9;
1323
- target[position$1++] = length;
1324
- } else if (length < 0x10000) {
1325
- if (headerSize < 3) {
1326
- target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1327
- }
1328
- target[position$1++] = 0xda;
1329
- target[position$1++] = length >> 8;
1330
- target[position$1++] = length & 0xff;
1331
- } else {
1332
- if (headerSize < 5) {
1333
- target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1334
- }
1335
- target[position$1++] = 0xdb;
1336
- targetView.setUint32(position$1, length);
1337
- position$1 += 4;
1338
- }
1339
- position$1 += length;
1340
- } else if (type === 'number') {
1341
- if (value >>> 0 === value) {// positive integer, 32-bit or less
1342
- // positive uint
1343
- if (value < 0x40) {
1344
- target[position$1++] = value;
1345
- } else if (value < 0x100) {
1346
- target[position$1++] = 0xcc;
1347
- target[position$1++] = value;
1348
- } else if (value < 0x10000) {
1349
- target[position$1++] = 0xcd;
1350
- target[position$1++] = value >> 8;
1351
- target[position$1++] = value & 0xff;
1352
- } else {
1353
- target[position$1++] = 0xce;
1354
- targetView.setUint32(position$1, value);
1355
- position$1 += 4;
1356
- }
1357
- } else if (value >> 0 === value) { // negative integer
1358
- if (value >= -0x20) {
1359
- target[position$1++] = 0x100 + value;
1360
- } else if (value >= -0x80) {
1361
- target[position$1++] = 0xd0;
1362
- target[position$1++] = value + 0x100;
1363
- } else if (value >= -0x8000) {
1364
- target[position$1++] = 0xd1;
1365
- targetView.setInt16(position$1, value);
1366
- position$1 += 2;
1367
- } else {
1368
- target[position$1++] = 0xd2;
1369
- targetView.setInt32(position$1, value);
1370
- position$1 += 4;
1371
- }
1372
- } else {
1373
- let useFloat32;
1374
- if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1375
- target[position$1++] = 0xca;
1376
- targetView.setFloat32(position$1, value);
1377
- let xShifted;
1378
- if (useFloat32 < 4 ||
1379
- // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1380
- ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1381
- position$1 += 4;
1382
- return
1383
- } else
1384
- position$1--; // move back into position for writing a double
1385
- }
1386
- target[position$1++] = 0xcb;
1387
- targetView.setFloat64(position$1, value);
1388
- position$1 += 8;
1389
- }
1390
- } else if (type === 'object') {
1391
- if (!value)
1392
- target[position$1++] = 0xc0;
1393
- else {
1394
- if (referenceMap) {
1395
- let referee = referenceMap.get(value);
1396
- if (referee) {
1397
- if (!referee.id) {
1398
- let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1399
- referee.id = idsToInsert.push(referee);
1400
- }
1401
- target[position$1++] = 0xd6; // fixext 4
1402
- target[position$1++] = 0x70; // "p" for pointer
1403
- targetView.setUint32(position$1, referee.id);
1404
- position$1 += 4;
1405
- return
1406
- } else
1407
- referenceMap.set(value, { offset: position$1 - start });
1408
- }
1409
- let constructor = value.constructor;
1410
- if (constructor === Object) {
1411
- writeObject(value, true);
1412
- } else if (constructor === Array) {
1413
- length = value.length;
1414
- if (length < 0x10) {
1415
- target[position$1++] = 0x90 | length;
1416
- } else if (length < 0x10000) {
1417
- target[position$1++] = 0xdc;
1418
- target[position$1++] = length >> 8;
1419
- target[position$1++] = length & 0xff;
1420
- } else {
1421
- target[position$1++] = 0xdd;
1422
- targetView.setUint32(position$1, length);
1423
- position$1 += 4;
1424
- }
1425
- for (let i = 0; i < length; i++) {
1426
- pack(value[i]);
1427
- }
1428
- } else if (constructor === Map) {
1429
- length = value.size;
1430
- if (length < 0x10) {
1431
- target[position$1++] = 0x80 | length;
1432
- } else if (length < 0x10000) {
1433
- target[position$1++] = 0xde;
1434
- target[position$1++] = length >> 8;
1435
- target[position$1++] = length & 0xff;
1436
- } else {
1437
- target[position$1++] = 0xdf;
1438
- targetView.setUint32(position$1, length);
1439
- position$1 += 4;
1440
- }
1441
- for (let [ key, entryValue ] of value) {
1442
- pack(key);
1443
- pack(entryValue);
1444
- }
1445
- } else {
1446
- for (let i = 0, l = extensions.length; i < l; i++) {
1447
- let extensionClass = extensionClasses[i];
1448
- if (value instanceof extensionClass) {
1449
- let extension = extensions[i];
1450
- if (extension.write) {
1451
- if (extension.type) {
1452
- target[position$1++] = 0xd4; // one byte "tag" extension
1453
- target[position$1++] = extension.type;
1454
- target[position$1++] = 0;
1455
- }
1456
- pack(extension.write.call(this, value));
1457
- return
1458
- }
1459
- let currentTarget = target;
1460
- let currentTargetView = targetView;
1461
- let currentPosition = position$1;
1462
- target = null;
1463
- let result;
1464
- try {
1465
- result = extension.pack.call(this, value, (size) => {
1466
- // restore target and use it
1467
- target = currentTarget;
1468
- currentTarget = null;
1469
- position$1 += size;
1470
- if (position$1 > safeEnd)
1471
- makeRoom(position$1);
1472
- return {
1473
- target, targetView, position: position$1 - size
1474
- }
1475
- }, pack);
1476
- } finally {
1477
- // restore current target information (unless already restored)
1478
- if (currentTarget) {
1479
- target = currentTarget;
1480
- targetView = currentTargetView;
1481
- position$1 = currentPosition;
1482
- safeEnd = target.length - 10;
1483
- }
1484
- }
1485
- if (result) {
1486
- if (result.length + position$1 > safeEnd)
1487
- makeRoom(result.length + position$1);
1488
- position$1 = writeExtensionData(result, target, position$1, extension.type);
1489
- }
1490
- return
1491
- }
1492
- }
1493
- // no extension found, write as object
1494
- writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1495
- }
1496
- }
1497
- } else if (type === 'boolean') {
1498
- target[position$1++] = value ? 0xc3 : 0xc2;
1499
- } else if (type === 'bigint') {
1500
- if (value < (BigInt(1)<<BigInt(63)) && value >= -(BigInt(1)<<BigInt(63))) {
1501
- // use a signed int as long as it fits
1502
- target[position$1++] = 0xd3;
1503
- targetView.setBigInt64(position$1, value);
1504
- } else if (value < (BigInt(1)<<BigInt(64)) && value > 0) {
1505
- // if we can fit an unsigned int, use that
1506
- target[position$1++] = 0xcf;
1507
- targetView.setBigUint64(position$1, value);
1508
- } else {
1509
- // overflow
1510
- if (this.largeBigIntToFloat) {
1511
- target[position$1++] = 0xcb;
1512
- targetView.setFloat64(position$1, Number(value));
1513
- } else {
1514
- throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1515
- }
1516
- }
1517
- position$1 += 8;
1518
- } else if (type === 'undefined') {
1519
- if (this.encodeUndefinedAsNil)
1520
- target[position$1++] = 0xc0;
1521
- else {
1522
- target[position$1++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1523
- target[position$1++] = 0;
1524
- target[position$1++] = 0;
1525
- }
1526
- } else if (type === 'function') {
1527
- pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
1528
- } else {
1529
- throw new Error('Unknown type: ' + type)
1530
- }
1531
- };
1532
-
1533
- const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
1534
- // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1535
- let keys = Object.keys(object);
1536
- let length = keys.length;
1537
- if (length < 0x10) {
1538
- target[position$1++] = 0x80 | length;
1539
- } else if (length < 0x10000) {
1540
- target[position$1++] = 0xde;
1541
- target[position$1++] = length >> 8;
1542
- target[position$1++] = length & 0xff;
1543
- } else {
1544
- target[position$1++] = 0xdf;
1545
- targetView.setUint32(position$1, length);
1546
- position$1 += 4;
1547
- }
1548
- let key;
1549
- for (let i = 0; i < length; i++) {
1550
- pack(key = keys[i]);
1551
- pack(object[key]);
1552
- }
1553
- } :
1554
- (object, safePrototype) => {
1555
- target[position$1++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1556
- let objectOffset = position$1 - start;
1557
- position$1 += 2;
1558
- let size = 0;
1559
- for (let key in object) {
1560
- if (safePrototype || object.hasOwnProperty(key)) {
1561
- pack(key);
1562
- pack(object[key]);
1563
- size++;
1564
- }
1565
- }
1566
- target[objectOffset++ + start] = size >> 8;
1567
- target[objectOffset + start] = size & 0xff;
1568
- } :
1569
- (options.progressiveRecords && !useTwoByteRecords) ? // this is about 2% faster for highly stable structures, since it only requires one for-in loop (but much more expensive when new structure needs to be written)
1570
- (object, safePrototype) => {
1571
- let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1572
- let objectOffset = position$1++ - start;
1573
- let wroteKeys;
1574
- for (let key in object) {
1575
- if (safePrototype || object.hasOwnProperty(key)) {
1576
- nextTransition = transition[key];
1577
- if (nextTransition)
1578
- transition = nextTransition;
1579
- else {
1580
- // record doesn't exist, create full new record and insert it
1581
- let keys = Object.keys(object);
1582
- let lastTransition = transition;
1583
- transition = structures.transitions;
1584
- let newTransitions = 0;
1585
- for (let i = 0, l = keys.length; i < l; i++) {
1586
- let key = keys[i];
1587
- nextTransition = transition[key];
1588
- if (!nextTransition) {
1589
- nextTransition = transition[key] = Object.create(null);
1590
- newTransitions++;
1591
- }
1592
- transition = nextTransition;
1593
- }
1594
- if (objectOffset + start + 1 == position$1) {
1595
- // first key, so we don't need to insert, we can just write record directly
1596
- position$1--;
1597
- newRecord(transition, keys, newTransitions);
1598
- } else // otherwise we need to insert the record, moving existing data after the record
1599
- insertNewRecord(transition, keys, objectOffset, newTransitions);
1600
- wroteKeys = true;
1601
- transition = lastTransition[key];
1602
- }
1603
- pack(object[key]);
1604
- }
1605
- }
1606
- if (!wroteKeys) {
1607
- let recordId = transition[RECORD_SYMBOL];
1608
- if (recordId)
1609
- target[objectOffset + start] = recordId;
1610
- else
1611
- insertNewRecord(transition, Object.keys(object), objectOffset, 0);
1612
- }
1613
- } :
1614
- (object, safePrototype) => {
1615
- let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1616
- let newTransitions = 0;
1617
- for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
1618
- nextTransition = transition[key];
1619
- if (!nextTransition) {
1620
- nextTransition = transition[key] = Object.create(null);
1621
- newTransitions++;
1622
- }
1623
- transition = nextTransition;
1624
- }
1625
- let recordId = transition[RECORD_SYMBOL];
1626
- if (recordId) {
1627
- if (recordId >= 0x60 && useTwoByteRecords) {
1628
- target[position$1++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1629
- target[position$1++] = recordId >> 5;
1630
- } else
1631
- target[position$1++] = recordId;
1632
- } else {
1633
- newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
1634
- }
1635
- // now write the values
1636
- for (let key in object)
1637
- if (safePrototype || object.hasOwnProperty(key))
1638
- pack(object[key]);
1639
- };
1640
- const makeRoom = (end) => {
1641
- let newSize;
1642
- if (end > 0x1000000) {
1643
- // special handling for really large buffers
1644
- if ((end - start) > MAX_BUFFER_SIZE)
1645
- throw new Error('Packed buffer would be larger than maximum buffer size')
1646
- newSize = Math.min(MAX_BUFFER_SIZE,
1647
- Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
1648
- } else // faster handling for smaller buffers
1649
- newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
1650
- let newBuffer = new ByteArrayAllocate(newSize);
1651
- targetView = new DataView(newBuffer.buffer, 0, newSize);
1652
- end = Math.min(end, target.length);
1653
- if (target.copy)
1654
- target.copy(newBuffer, 0, start, end);
1655
- else
1656
- newBuffer.set(target.slice(start, end));
1657
- position$1 -= start;
1658
- start = 0;
1659
- safeEnd = newBuffer.length - 10;
1660
- return target = newBuffer
1661
- };
1662
- const newRecord = (transition, keys, newTransitions) => {
1663
- let recordId = structures.nextId;
1664
- if (!recordId)
1665
- recordId = 0x40;
1666
- if (recordId < sharedLimitId && this.shouldShareStructure && !this.shouldShareStructure(keys)) {
1667
- recordId = structures.nextOwnId;
1668
- if (!(recordId < maxStructureId))
1669
- recordId = sharedLimitId;
1670
- structures.nextOwnId = recordId + 1;
1671
- } else {
1672
- if (recordId >= maxStructureId)// cycle back around
1673
- recordId = sharedLimitId;
1674
- structures.nextId = recordId + 1;
1675
- }
1676
- let highByte = keys.highByte = recordId >= 0x60 && useTwoByteRecords ? (recordId - 0x60) >> 5 : -1;
1677
- transition[RECORD_SYMBOL] = recordId;
1678
- transition.__keys__ = keys;
1679
- structures[recordId - 0x40] = keys;
1680
-
1681
- if (recordId < sharedLimitId) {
1682
- keys.isShared = true;
1683
- structures.sharedLength = recordId - 0x3f;
1684
- hasSharedUpdate = true;
1685
- if (highByte >= 0) {
1686
- target[position$1++] = (recordId & 0x1f) + 0x60;
1687
- target[position$1++] = highByte;
1688
- } else {
1689
- target[position$1++] = recordId;
1690
- }
1691
- } else {
1692
- if (highByte >= 0) {
1693
- target[position$1++] = 0xd5; // fixext 2
1694
- target[position$1++] = 0x72; // "r" record defintion extension type
1695
- target[position$1++] = (recordId & 0x1f) + 0x60;
1696
- target[position$1++] = highByte;
1697
- } else {
1698
- target[position$1++] = 0xd4; // fixext 1
1699
- target[position$1++] = 0x72; // "r" record defintion extension type
1700
- target[position$1++] = recordId;
1701
- }
1702
-
1703
- if (newTransitions)
1704
- transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
1705
- // record the removal of the id, we can maintain our shared structure
1706
- if (recordIdsToRemove.length >= maxOwnStructures)
1707
- recordIdsToRemove.shift()[RECORD_SYMBOL] = 0; // we are cycling back through, and have to remove old ones
1708
- recordIdsToRemove.push(transition);
1709
- pack(keys);
1710
- }
1711
- };
1712
- const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
1713
- let mainTarget = target;
1714
- let mainPosition = position$1;
1715
- let mainSafeEnd = safeEnd;
1716
- let mainStart = start;
1717
- target = keysTarget;
1718
- position$1 = 0;
1719
- start = 0;
1720
- if (!target)
1721
- keysTarget = target = new ByteArrayAllocate(8192);
1722
- safeEnd = target.length - 10;
1723
- newRecord(transition, keys, newTransitions);
1724
- keysTarget = target;
1725
- let keysPosition = position$1;
1726
- target = mainTarget;
1727
- position$1 = mainPosition;
1728
- safeEnd = mainSafeEnd;
1729
- start = mainStart;
1730
- if (keysPosition > 1) {
1731
- let newEnd = position$1 + keysPosition - 1;
1732
- if (newEnd > safeEnd)
1733
- makeRoom(newEnd);
1734
- let insertionPosition = insertionOffset + start;
1735
- target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position$1);
1736
- target.set(keysTarget.slice(0, keysPosition), insertionPosition);
1737
- position$1 = newEnd;
1738
- } else {
1739
- target[insertionOffset + start] = keysTarget[0];
1740
- }
1741
- };
1742
- }
1743
- useBuffer(buffer) {
1744
- // this means we are finished using our own buffer and we can write over it safely
1745
- target = buffer;
1746
- targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1747
- position$1 = 0;
1748
- }
1749
- clearSharedData() {
1750
- if (this.structures)
1751
- this.structures = [];
1752
- }
1753
- }
1754
-
1755
- extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, C1Type ];
1756
- extensions = [{
1757
- pack(date, allocateForWrite, pack) {
1758
- let seconds = date.getTime() / 1000;
1759
- if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
1760
- // Timestamp 32
1761
- let { target, targetView, position} = allocateForWrite(6);
1762
- target[position++] = 0xd6;
1763
- target[position++] = 0xff;
1764
- targetView.setUint32(position, seconds);
1765
- } else if (seconds > 0 && seconds < 0x100000000) {
1766
- // Timestamp 64
1767
- let { target, targetView, position} = allocateForWrite(10);
1768
- target[position++] = 0xd7;
1769
- target[position++] = 0xff;
1770
- targetView.setUint32(position, date.getMilliseconds() * 4000000 + ((seconds / 1000 / 0x100000000) >> 0));
1771
- targetView.setUint32(position + 4, seconds);
1772
- } else if (isNaN(seconds)) {
1773
- if (this.onInvalidDate) {
1774
- allocateForWrite(0);
1775
- return pack(this.onInvalidDate())
1776
- }
1777
- // Intentionally invalid timestamp
1778
- let { target, targetView, position} = allocateForWrite(3);
1779
- target[position++] = 0xd4;
1780
- target[position++] = 0xff;
1781
- target[position++] = 0xff;
1782
- } else {
1783
- // Timestamp 96
1784
- let { target, targetView, position} = allocateForWrite(15);
1785
- target[position++] = 0xc7;
1786
- target[position++] = 12;
1787
- target[position++] = 0xff;
1788
- targetView.setUint32(position, date.getMilliseconds() * 1000000);
1789
- targetView.setBigInt64(position + 4, BigInt(Math.floor(seconds)));
1790
- }
1791
- }
1792
- }, {
1793
- pack(set, allocateForWrite, pack) {
1794
- let array = Array.from(set);
1795
- let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1796
- if (this.moreTypes) {
1797
- target[position++] = 0xd4;
1798
- target[position++] = 0x73; // 's' for Set
1799
- target[position++] = 0;
1800
- }
1801
- pack(array);
1802
- }
1803
- }, {
1804
- pack(error, allocateForWrite, pack) {
1805
- let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1806
- if (this.moreTypes) {
1807
- target[position++] = 0xd4;
1808
- target[position++] = 0x65; // 'e' for error
1809
- target[position++] = 0;
1810
- }
1811
- pack([ error.name, error.message ]);
1812
- }
1813
- }, {
1814
- pack(regex, allocateForWrite, pack) {
1815
- let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1816
- if (this.moreTypes) {
1817
- target[position++] = 0xd4;
1818
- target[position++] = 0x78; // 'x' for regeXp
1819
- target[position++] = 0;
1820
- }
1821
- pack([ regex.source, regex.flags ]);
1822
- }
1823
- }, {
1824
- pack(arrayBuffer, allocateForWrite) {
1825
- if (this.moreTypes)
1826
- writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
1827
- else
1828
- writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
1829
- }
1830
- }, {
1831
- pack(typedArray, allocateForWrite) {
1832
- let constructor = typedArray.constructor;
1833
- if (constructor !== ByteArray && this.moreTypes)
1834
- writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite);
1835
- else
1836
- writeBuffer(typedArray, allocateForWrite);
1837
- }
1838
- }, {
1839
- pack(c1, allocateForWrite) { // specific 0xC1 object
1840
- let { target, position} = allocateForWrite(1);
1841
- target[position] = 0xc1;
1842
- }
1843
- }];
1844
-
1845
- function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
1846
- let length = typedArray.byteLength;
1847
- if (length + 1 < 0x100) {
1848
- var { target, position } = allocateForWrite(4 + length);
1849
- target[position++] = 0xc7;
1850
- target[position++] = length + 1;
1851
- } else if (length + 1 < 0x10000) {
1852
- var { target, position } = allocateForWrite(5 + length);
1853
- target[position++] = 0xc8;
1854
- target[position++] = (length + 1) >> 8;
1855
- target[position++] = (length + 1) & 0xff;
1856
- } else {
1857
- var { target, position, targetView } = allocateForWrite(7 + length);
1858
- target[position++] = 0xc9;
1859
- targetView.setUint32(position, length + 1); // plus one for the type byte
1860
- position += 4;
1861
- }
1862
- target[position++] = 0x74; // "t" for typed array
1863
- target[position++] = type;
1864
- target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position);
1865
- }
1866
- function writeBuffer(buffer, allocateForWrite) {
1867
- let length = buffer.byteLength;
1868
- var target, position;
1869
- if (length < 0x100) {
1870
- var { target, position } = allocateForWrite(length + 2);
1871
- target[position++] = 0xc4;
1872
- target[position++] = length;
1873
- } else if (length < 0x10000) {
1874
- var { target, position } = allocateForWrite(length + 3);
1875
- target[position++] = 0xc5;
1876
- target[position++] = length >> 8;
1877
- target[position++] = length & 0xff;
1878
- } else {
1879
- var { target, position, targetView } = allocateForWrite(length + 5);
1880
- target[position++] = 0xc6;
1881
- targetView.setUint32(position, length);
1882
- position += 4;
1883
- }
1884
- target.set(buffer, position);
1885
- }
1886
-
1887
- function writeExtensionData(result, target, position, type) {
1888
- let length = result.length;
1889
- switch (length) {
1890
- case 1:
1891
- target[position++] = 0xd4;
1892
- break
1893
- case 2:
1894
- target[position++] = 0xd5;
1895
- break
1896
- case 4:
1897
- target[position++] = 0xd6;
1898
- break
1899
- case 8:
1900
- target[position++] = 0xd7;
1901
- break
1902
- case 16:
1903
- target[position++] = 0xd8;
1904
- break
1905
- default:
1906
- if (length < 0x100) {
1907
- target[position++] = 0xc7;
1908
- target[position++] = length;
1909
- } else if (length < 0x10000) {
1910
- target[position++] = 0xc8;
1911
- target[position++] = length >> 8;
1912
- target[position++] = length & 0xff;
1913
- } else {
1914
- target[position++] = 0xc9;
1915
- target[position++] = length >> 24;
1916
- target[position++] = (length >> 16) & 0xff;
1917
- target[position++] = (length >> 8) & 0xff;
1918
- target[position++] = length & 0xff;
1919
- }
1920
- }
1921
- target[position++] = type;
1922
- target.set(result, position);
1923
- position += length;
1924
- return position
1925
- }
1926
-
1927
- function insertIds(serialized, idsToInsert) {
1928
- // insert the ids that need to be referenced for structured clones
1929
- let nextId;
1930
- let distanceToMove = idsToInsert.length * 6;
1931
- let lastEnd = serialized.length - distanceToMove;
1932
- idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1933
- while (nextId = idsToInsert.pop()) {
1934
- let offset = nextId.offset;
1935
- let id = nextId.id;
1936
- serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
1937
- distanceToMove -= 6;
1938
- let position = offset + distanceToMove;
1939
- serialized[position++] = 0xd6;
1940
- serialized[position++] = 0x69; // 'i'
1941
- serialized[position++] = id >> 24;
1942
- serialized[position++] = (id >> 16) & 0xff;
1943
- serialized[position++] = (id >> 8) & 0xff;
1944
- serialized[position++] = id & 0xff;
1945
- lastEnd = offset;
1946
- }
1947
- return serialized
1948
- }
1949
-
1950
- function writeBundles(start, pack) {
1951
- targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start);
1952
- let writeStrings = bundledStrings$1;
1953
- bundledStrings$1 = null;
1954
- pack(writeStrings[0]);
1955
- pack(writeStrings[1]);
1956
- }
1957
-
1958
- function addExtension$1(extension) {
1959
- if (extension.Class) {
1960
- if (!extension.pack && !extension.write)
1961
- throw new Error('Extension has no pack or write function')
1962
- if (extension.pack && !extension.type)
1963
- throw new Error('Extension has no type (numeric code to identify the extension)')
1964
- extensionClasses.unshift(extension.Class);
1965
- extensions.unshift(extension);
1966
- }
1967
- addExtension(extension);
1968
- }
1969
-
1970
- let defaultPackr = new Packr({ useRecords: false });
1971
- const pack = defaultPackr.pack;
1972
- const encode = defaultPackr.pack;
1973
- const Encoder = Packr;
1974
- const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
1975
- const REUSE_BUFFER_MODE = 512;
44
+ class Unpackr {
45
+ constructor(options) {
46
+ if (options) {
47
+ if (options.useRecords === false && options.mapsAsObjects === undefined)
48
+ options.mapsAsObjects = true;
49
+ if (options.sequential && options.trusted !== false) {
50
+ options.trusted = true;
51
+ if (!options.structures && options.useRecords != false) {
52
+ options.structures = [];
53
+ if (!options.maxSharedStructures)
54
+ options.maxSharedStructures = 0;
55
+ }
56
+ }
57
+ if (options.structures)
58
+ options.structures.sharedLength = options.structures.length;
59
+ else if (options.getStructures) {
60
+ (options.structures = []).uninitialized = true; // this is what we use to denote an uninitialized structures
61
+ options.structures.sharedLength = 0;
62
+ }
63
+ }
64
+ Object.assign(this, options);
65
+ }
66
+ unpack(source, end) {
67
+ if (src) {
68
+ // re-entrant execution, save the state and restore it after we do this unpack
69
+ return saveState(() => {
70
+ clearSource();
71
+ return this ? this.unpack(source, end) : Unpackr.prototype.unpack.call(defaultOptions, source, end)
72
+ })
73
+ }
74
+ srcEnd = end > -1 ? end : source.length;
75
+ position = 0;
76
+ stringPosition = 0;
77
+ srcStringEnd = 0;
78
+ srcString = null;
79
+ strings = EMPTY_ARRAY;
80
+ bundledStrings = null;
81
+ src = source;
82
+ // this provides cached access to the data view for a buffer if it is getting reused, which is a recommend
83
+ // technique for getting data from a database where it can be copied into an existing buffer instead of creating
84
+ // new ones
85
+ try {
86
+ dataView = source.dataView || (source.dataView = new DataView(source.buffer, source.byteOffset, source.byteLength));
87
+ } catch(error) {
88
+ // if it doesn't have a buffer, maybe it is the wrong type of object
89
+ src = null;
90
+ if (source instanceof Uint8Array)
91
+ throw error
92
+ throw new Error('Source must be a Uint8Array or Buffer but was a ' + ((source && typeof source == 'object') ? source.constructor.name : typeof source))
93
+ }
94
+ if (this instanceof Unpackr) {
95
+ currentUnpackr = this;
96
+ if (this.structures) {
97
+ currentStructures = this.structures;
98
+ return checkedRead()
99
+ } else if (!currentStructures || currentStructures.length > 0) {
100
+ currentStructures = [];
101
+ }
102
+ } else {
103
+ currentUnpackr = defaultOptions;
104
+ if (!currentStructures || currentStructures.length > 0)
105
+ currentStructures = [];
106
+ }
107
+ return checkedRead()
108
+ }
109
+ unpackMultiple(source, forEach) {
110
+ let values, lastPosition = 0;
111
+ try {
112
+ sequentialMode = true;
113
+ let size = source.length;
114
+ let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
115
+ if (forEach) {
116
+ forEach(value);
117
+ while(position < size) {
118
+ lastPosition = position;
119
+ if (forEach(checkedRead()) === false) {
120
+ return
121
+ }
122
+ }
123
+ }
124
+ else {
125
+ values = [ value ];
126
+ while(position < size) {
127
+ lastPosition = position;
128
+ values.push(checkedRead());
129
+ }
130
+ return values
131
+ }
132
+ } catch(error) {
133
+ error.lastPosition = lastPosition;
134
+ error.values = values;
135
+ throw error
136
+ } finally {
137
+ sequentialMode = false;
138
+ clearSource();
139
+ }
140
+ }
141
+ _mergeStructures(loadedStructures, existingStructures) {
142
+ loadedStructures = loadedStructures || [];
143
+ for (let i = 0, l = loadedStructures.length; i < l; i++) {
144
+ let structure = loadedStructures[i];
145
+ if (structure) {
146
+ structure.isShared = true;
147
+ if (i >= 32)
148
+ structure.highByte = (i - 32) >> 5;
149
+ }
150
+ }
151
+ loadedStructures.sharedLength = loadedStructures.length;
152
+ for (let id in existingStructures || []) {
153
+ if (id >= 0) {
154
+ let structure = loadedStructures[id];
155
+ let existing = existingStructures[id];
156
+ if (existing) {
157
+ if (structure)
158
+ (loadedStructures.restoreStructures || (loadedStructures.restoreStructures = []))[id] = structure;
159
+ loadedStructures[id] = existing;
160
+ }
161
+ }
162
+ }
163
+ return this.structures = loadedStructures
164
+ }
165
+ decode(source, end) {
166
+ return this.unpack(source, end)
167
+ }
168
+ }
169
+ function checkedRead() {
170
+ try {
171
+ if (!currentUnpackr.trusted && !sequentialMode) {
172
+ let sharedLength = currentStructures.sharedLength || 0;
173
+ if (sharedLength < currentStructures.length)
174
+ currentStructures.length = sharedLength;
175
+ }
176
+ let result;
177
+ if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && readStruct) {
178
+ let id = (src[position++] << 8) + src[position++];
179
+ result = readStruct(src, position, srcEnd, currentStructures[id - 0x40] || loadStructures()[id - 0x40], currentUnpackr);
180
+ position = srcEnd;
181
+ } else
182
+ result = read();
183
+ if (bundledStrings) // bundled strings to skip past
184
+ position = bundledStrings.postBundlePosition;
185
+
186
+ if (position == srcEnd) {
187
+ // finished reading this source, cleanup references
188
+ if (currentStructures.restoreStructures)
189
+ restoreStructures();
190
+ currentStructures = null;
191
+ src = null;
192
+ if (referenceMap)
193
+ referenceMap = null;
194
+ } else if (position > srcEnd) {
195
+ // over read
196
+ throw new Error('Unexpected end of MessagePack data')
197
+ } else if (!sequentialMode) {
198
+ throw new Error('Data read, but end of buffer not reached ' + JSON.stringify(result).slice(0, 100))
199
+ }
200
+ // else more to read, but we are reading sequentially, so don't clear source yet
201
+ return result
202
+ } catch(error) {
203
+ if (currentStructures.restoreStructures)
204
+ restoreStructures();
205
+ clearSource();
206
+ if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
207
+ error.incomplete = true;
208
+ }
209
+ throw error
210
+ }
211
+ }
212
+
213
+ function restoreStructures() {
214
+ for (let id in currentStructures.restoreStructures) {
215
+ currentStructures[id] = currentStructures.restoreStructures[id];
216
+ }
217
+ currentStructures.restoreStructures = null;
218
+ }
219
+
220
+ function read() {
221
+ let token = src[position++];
222
+ if (token < 0xa0) {
223
+ if (token < 0x80) {
224
+ if (token < 0x40)
225
+ return token
226
+ else {
227
+ let structure = currentStructures[token & 0x3f] ||
228
+ currentUnpackr.getStructures && loadStructures()[token & 0x3f];
229
+ if (structure) {
230
+ if (!structure.read) {
231
+ structure.read = createStructureReader(structure, token & 0x3f);
232
+ }
233
+ return structure.read()
234
+ } else
235
+ return token
236
+ }
237
+ } else if (token < 0x90) {
238
+ // map
239
+ token -= 0x80;
240
+ if (currentUnpackr.mapsAsObjects) {
241
+ let object = {};
242
+ for (let i = 0; i < token; i++) {
243
+ object[readKey()] = read();
244
+ }
245
+ return object
246
+ } else {
247
+ let map = new Map();
248
+ for (let i = 0; i < token; i++) {
249
+ map.set(read(), read());
250
+ }
251
+ return map
252
+ }
253
+ } else {
254
+ token -= 0x90;
255
+ let array = new Array(token);
256
+ for (let i = 0; i < token; i++) {
257
+ array[i] = read();
258
+ }
259
+ if (currentUnpackr.freezeData)
260
+ return Object.freeze(array)
261
+ return array
262
+ }
263
+ } else if (token < 0xc0) {
264
+ // fixstr
265
+ let length = token - 0xa0;
266
+ if (srcStringEnd >= position) {
267
+ return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
268
+ }
269
+ if (srcStringEnd == 0 && srcEnd < 140) {
270
+ // for small blocks, avoiding the overhead of the extract call is helpful
271
+ let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
272
+ if (string != null)
273
+ return string
274
+ }
275
+ return readFixedString(length)
276
+ } else {
277
+ let value;
278
+ switch (token) {
279
+ case 0xc0: return null
280
+ case 0xc1:
281
+ if (bundledStrings) {
282
+ value = read(); // followed by the length of the string in characters (not bytes!)
283
+ if (value > 0)
284
+ return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
285
+ else
286
+ return bundledStrings[0].slice(bundledStrings.position0, bundledStrings.position0 -= value)
287
+ }
288
+ return C1; // "never-used", return special object to denote that
289
+ case 0xc2: return false
290
+ case 0xc3: return true
291
+ case 0xc4:
292
+ // bin 8
293
+ value = src[position++];
294
+ if (value === undefined)
295
+ throw new Error('Unexpected end of buffer')
296
+ return readBin(value)
297
+ case 0xc5:
298
+ // bin 16
299
+ value = dataView.getUint16(position);
300
+ position += 2;
301
+ return readBin(value)
302
+ case 0xc6:
303
+ // bin 32
304
+ value = dataView.getUint32(position);
305
+ position += 4;
306
+ return readBin(value)
307
+ case 0xc7:
308
+ // ext 8
309
+ return readExt(src[position++])
310
+ case 0xc8:
311
+ // ext 16
312
+ value = dataView.getUint16(position);
313
+ position += 2;
314
+ return readExt(value)
315
+ case 0xc9:
316
+ // ext 32
317
+ value = dataView.getUint32(position);
318
+ position += 4;
319
+ return readExt(value)
320
+ case 0xca:
321
+ value = dataView.getFloat32(position);
322
+ if (currentUnpackr.useFloat32 > 2) {
323
+ // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
324
+ let multiplier = mult10[((src[position] & 0x7f) << 1) | (src[position + 1] >> 7)];
325
+ position += 4;
326
+ return ((multiplier * value + (value > 0 ? 0.5 : -0.5)) >> 0) / multiplier
327
+ }
328
+ position += 4;
329
+ return value
330
+ case 0xcb:
331
+ value = dataView.getFloat64(position);
332
+ position += 8;
333
+ return value
334
+ // uint handlers
335
+ case 0xcc:
336
+ return src[position++]
337
+ case 0xcd:
338
+ value = dataView.getUint16(position);
339
+ position += 2;
340
+ return value
341
+ case 0xce:
342
+ value = dataView.getUint32(position);
343
+ position += 4;
344
+ return value
345
+ case 0xcf:
346
+ if (currentUnpackr.int64AsNumber) {
347
+ value = dataView.getUint32(position) * 0x100000000;
348
+ value += dataView.getUint32(position + 4);
349
+ } else
350
+ value = dataView.getBigUint64(position);
351
+ position += 8;
352
+ return value
353
+
354
+ // int handlers
355
+ case 0xd0:
356
+ return dataView.getInt8(position++)
357
+ case 0xd1:
358
+ value = dataView.getInt16(position);
359
+ position += 2;
360
+ return value
361
+ case 0xd2:
362
+ value = dataView.getInt32(position);
363
+ position += 4;
364
+ return value
365
+ case 0xd3:
366
+ if (currentUnpackr.int64AsNumber) {
367
+ value = dataView.getInt32(position) * 0x100000000;
368
+ value += dataView.getUint32(position + 4);
369
+ } else
370
+ value = dataView.getBigInt64(position);
371
+ position += 8;
372
+ return value
373
+
374
+ case 0xd4:
375
+ // fixext 1
376
+ value = src[position++];
377
+ if (value == 0x72) {
378
+ return recordDefinition(src[position++] & 0x3f)
379
+ } else {
380
+ let extension = currentExtensions[value];
381
+ if (extension) {
382
+ if (extension.read) {
383
+ position++; // skip filler byte
384
+ return extension.read(read())
385
+ } else if (extension.noBuffer) {
386
+ position++; // skip filler byte
387
+ return extension()
388
+ } else
389
+ return extension(src.subarray(position, ++position))
390
+ } else
391
+ throw new Error('Unknown extension ' + value)
392
+ }
393
+ case 0xd5:
394
+ // fixext 2
395
+ value = src[position];
396
+ if (value == 0x72) {
397
+ position++;
398
+ return recordDefinition(src[position++] & 0x3f, src[position++])
399
+ } else
400
+ return readExt(2)
401
+ case 0xd6:
402
+ // fixext 4
403
+ return readExt(4)
404
+ case 0xd7:
405
+ // fixext 8
406
+ return readExt(8)
407
+ case 0xd8:
408
+ // fixext 16
409
+ return readExt(16)
410
+ case 0xd9:
411
+ // str 8
412
+ value = src[position++];
413
+ if (srcStringEnd >= position) {
414
+ return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
415
+ }
416
+ return readString8(value)
417
+ case 0xda:
418
+ // str 16
419
+ value = dataView.getUint16(position);
420
+ position += 2;
421
+ if (srcStringEnd >= position) {
422
+ return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
423
+ }
424
+ return readString16(value)
425
+ case 0xdb:
426
+ // str 32
427
+ value = dataView.getUint32(position);
428
+ position += 4;
429
+ if (srcStringEnd >= position) {
430
+ return srcString.slice(position - srcStringStart, (position += value) - srcStringStart)
431
+ }
432
+ return readString32(value)
433
+ case 0xdc:
434
+ // array 16
435
+ value = dataView.getUint16(position);
436
+ position += 2;
437
+ return readArray(value)
438
+ case 0xdd:
439
+ // array 32
440
+ value = dataView.getUint32(position);
441
+ position += 4;
442
+ return readArray(value)
443
+ case 0xde:
444
+ // map 16
445
+ value = dataView.getUint16(position);
446
+ position += 2;
447
+ return readMap(value)
448
+ case 0xdf:
449
+ // map 32
450
+ value = dataView.getUint32(position);
451
+ position += 4;
452
+ return readMap(value)
453
+ default: // negative int
454
+ if (token >= 0xe0)
455
+ return token - 0x100
456
+ if (token === undefined) {
457
+ let error = new Error('Unexpected end of MessagePack data');
458
+ error.incomplete = true;
459
+ throw error
460
+ }
461
+ throw new Error('Unknown MessagePack token ' + token)
462
+
463
+ }
464
+ }
465
+ }
466
+ const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
467
+ function createStructureReader(structure, firstId) {
468
+ function readObject() {
469
+ // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
470
+ if (readObject.count++ > inlineObjectReadThreshold) {
471
+ let readObject = structure.read = (new Function('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
472
+ '({' + structure.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read);
473
+ if (structure.highByte === 0)
474
+ structure.read = createSecondByteReader(firstId, structure.read);
475
+ return readObject() // second byte is already read, if there is one so immediately read object
476
+ }
477
+ let object = {};
478
+ for (let i = 0, l = structure.length; i < l; i++) {
479
+ let key = structure[i];
480
+ object[key] = read();
481
+ }
482
+ if (currentUnpackr.freezeData)
483
+ return Object.freeze(object);
484
+ return object
485
+ }
486
+ readObject.count = 0;
487
+ if (structure.highByte === 0) {
488
+ return createSecondByteReader(firstId, readObject)
489
+ }
490
+ return readObject
491
+ }
492
+
493
+ const createSecondByteReader = (firstId, read0) => {
494
+ return function() {
495
+ let highByte = src[position++];
496
+ if (highByte === 0)
497
+ return read0()
498
+ let id = firstId < 32 ? -(firstId + (highByte << 5)) : firstId + (highByte << 5);
499
+ let structure = currentStructures[id] || loadStructures()[id];
500
+ if (!structure) {
501
+ throw new Error('Record id is not defined for ' + id)
502
+ }
503
+ if (!structure.read)
504
+ structure.read = createStructureReader(structure, firstId);
505
+ return structure.read()
506
+ }
507
+ };
508
+
509
+ function loadStructures() {
510
+ let loadedStructures = saveState(() => {
511
+ // save the state in case getStructures modifies our buffer
512
+ src = null;
513
+ return currentUnpackr.getStructures()
514
+ });
515
+ return currentStructures = currentUnpackr._mergeStructures(loadedStructures, currentStructures)
516
+ }
517
+
518
+ var readFixedString = readStringJS;
519
+ var readString8 = readStringJS;
520
+ var readString16 = readStringJS;
521
+ var readString32 = readStringJS;
522
+ exports.isNativeAccelerationEnabled = false;
523
+
524
+ function setExtractor(extractStrings) {
525
+ exports.isNativeAccelerationEnabled = true;
526
+ readFixedString = readString(1);
527
+ readString8 = readString(2);
528
+ readString16 = readString(3);
529
+ readString32 = readString(5);
530
+ function readString(headerLength) {
531
+ return function readString(length) {
532
+ let string = strings[stringPosition++];
533
+ if (string == null) {
534
+ if (bundledStrings)
535
+ return readStringJS(length)
536
+ let extraction = extractStrings(position - headerLength, srcEnd, src);
537
+ if (typeof extraction == 'string') {
538
+ string = extraction;
539
+ strings = EMPTY_ARRAY;
540
+ } else {
541
+ strings = extraction;
542
+ stringPosition = 1;
543
+ 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
544
+ string = strings[0];
545
+ if (string === undefined)
546
+ throw new Error('Unexpected end of buffer')
547
+ }
548
+ }
549
+ let srcStringLength = string.length;
550
+ if (srcStringLength <= length) {
551
+ position += length;
552
+ return string
553
+ }
554
+ srcString = string;
555
+ srcStringStart = position;
556
+ srcStringEnd = position + srcStringLength;
557
+ position += length;
558
+ return string.slice(0, length) // we know we just want the beginning
559
+ }
560
+ }
561
+ }
562
+ function readStringJS(length) {
563
+ let result;
564
+ if (length < 16) {
565
+ if (result = shortStringInJS(length))
566
+ return result
567
+ }
568
+ if (length > 64 && decoder)
569
+ return decoder.decode(src.subarray(position, position += length))
570
+ const end = position + length;
571
+ const units = [];
572
+ result = '';
573
+ while (position < end) {
574
+ const byte1 = src[position++];
575
+ if ((byte1 & 0x80) === 0) {
576
+ // 1 byte
577
+ units.push(byte1);
578
+ } else if ((byte1 & 0xe0) === 0xc0) {
579
+ // 2 bytes
580
+ const byte2 = src[position++] & 0x3f;
581
+ units.push(((byte1 & 0x1f) << 6) | byte2);
582
+ } else if ((byte1 & 0xf0) === 0xe0) {
583
+ // 3 bytes
584
+ const byte2 = src[position++] & 0x3f;
585
+ const byte3 = src[position++] & 0x3f;
586
+ units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
587
+ } else if ((byte1 & 0xf8) === 0xf0) {
588
+ // 4 bytes
589
+ const byte2 = src[position++] & 0x3f;
590
+ const byte3 = src[position++] & 0x3f;
591
+ const byte4 = src[position++] & 0x3f;
592
+ let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
593
+ if (unit > 0xffff) {
594
+ unit -= 0x10000;
595
+ units.push(((unit >>> 10) & 0x3ff) | 0xd800);
596
+ unit = 0xdc00 | (unit & 0x3ff);
597
+ }
598
+ units.push(unit);
599
+ } else {
600
+ units.push(byte1);
601
+ }
602
+
603
+ if (units.length >= 0x1000) {
604
+ result += fromCharCode.apply(String, units);
605
+ units.length = 0;
606
+ }
607
+ }
608
+
609
+ if (units.length > 0) {
610
+ result += fromCharCode.apply(String, units);
611
+ }
612
+
613
+ return result
614
+ }
615
+
616
+ function readArray(length) {
617
+ let array = new Array(length);
618
+ for (let i = 0; i < length; i++) {
619
+ array[i] = read();
620
+ }
621
+ if (currentUnpackr.freezeData)
622
+ return Object.freeze(array)
623
+ return array
624
+ }
625
+
626
+ function readMap(length) {
627
+ if (currentUnpackr.mapsAsObjects) {
628
+ let object = {};
629
+ for (let i = 0; i < length; i++) {
630
+ object[readKey()] = read();
631
+ }
632
+ return object
633
+ } else {
634
+ let map = new Map();
635
+ for (let i = 0; i < length; i++) {
636
+ map.set(read(), read());
637
+ }
638
+ return map
639
+ }
640
+ }
641
+
642
+ var fromCharCode = String.fromCharCode;
643
+ function longStringInJS(length) {
644
+ let start = position;
645
+ let bytes = new Array(length);
646
+ for (let i = 0; i < length; i++) {
647
+ const byte = src[position++];
648
+ if ((byte & 0x80) > 0) {
649
+ position = start;
650
+ return
651
+ }
652
+ bytes[i] = byte;
653
+ }
654
+ return fromCharCode.apply(String, bytes)
655
+ }
656
+ function shortStringInJS(length) {
657
+ if (length < 4) {
658
+ if (length < 2) {
659
+ if (length === 0)
660
+ return ''
661
+ else {
662
+ let a = src[position++];
663
+ if ((a & 0x80) > 1) {
664
+ position -= 1;
665
+ return
666
+ }
667
+ return fromCharCode(a)
668
+ }
669
+ } else {
670
+ let a = src[position++];
671
+ let b = src[position++];
672
+ if ((a & 0x80) > 0 || (b & 0x80) > 0) {
673
+ position -= 2;
674
+ return
675
+ }
676
+ if (length < 3)
677
+ return fromCharCode(a, b)
678
+ let c = src[position++];
679
+ if ((c & 0x80) > 0) {
680
+ position -= 3;
681
+ return
682
+ }
683
+ return fromCharCode(a, b, c)
684
+ }
685
+ } else {
686
+ let a = src[position++];
687
+ let b = src[position++];
688
+ let c = src[position++];
689
+ let d = src[position++];
690
+ if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
691
+ position -= 4;
692
+ return
693
+ }
694
+ if (length < 6) {
695
+ if (length === 4)
696
+ return fromCharCode(a, b, c, d)
697
+ else {
698
+ let e = src[position++];
699
+ if ((e & 0x80) > 0) {
700
+ position -= 5;
701
+ return
702
+ }
703
+ return fromCharCode(a, b, c, d, e)
704
+ }
705
+ } else if (length < 8) {
706
+ let e = src[position++];
707
+ let f = src[position++];
708
+ if ((e & 0x80) > 0 || (f & 0x80) > 0) {
709
+ position -= 6;
710
+ return
711
+ }
712
+ if (length < 7)
713
+ return fromCharCode(a, b, c, d, e, f)
714
+ let g = src[position++];
715
+ if ((g & 0x80) > 0) {
716
+ position -= 7;
717
+ return
718
+ }
719
+ return fromCharCode(a, b, c, d, e, f, g)
720
+ } else {
721
+ let e = src[position++];
722
+ let f = src[position++];
723
+ let g = src[position++];
724
+ let h = src[position++];
725
+ if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
726
+ position -= 8;
727
+ return
728
+ }
729
+ if (length < 10) {
730
+ if (length === 8)
731
+ return fromCharCode(a, b, c, d, e, f, g, h)
732
+ else {
733
+ let i = src[position++];
734
+ if ((i & 0x80) > 0) {
735
+ position -= 9;
736
+ return
737
+ }
738
+ return fromCharCode(a, b, c, d, e, f, g, h, i)
739
+ }
740
+ } else if (length < 12) {
741
+ let i = src[position++];
742
+ let j = src[position++];
743
+ if ((i & 0x80) > 0 || (j & 0x80) > 0) {
744
+ position -= 10;
745
+ return
746
+ }
747
+ if (length < 11)
748
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j)
749
+ let k = src[position++];
750
+ if ((k & 0x80) > 0) {
751
+ position -= 11;
752
+ return
753
+ }
754
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k)
755
+ } else {
756
+ let i = src[position++];
757
+ let j = src[position++];
758
+ let k = src[position++];
759
+ let l = src[position++];
760
+ if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
761
+ position -= 12;
762
+ return
763
+ }
764
+ if (length < 14) {
765
+ if (length === 12)
766
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l)
767
+ else {
768
+ let m = src[position++];
769
+ if ((m & 0x80) > 0) {
770
+ position -= 13;
771
+ return
772
+ }
773
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m)
774
+ }
775
+ } else {
776
+ let m = src[position++];
777
+ let n = src[position++];
778
+ if ((m & 0x80) > 0 || (n & 0x80) > 0) {
779
+ position -= 14;
780
+ return
781
+ }
782
+ if (length < 15)
783
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
784
+ let o = src[position++];
785
+ if ((o & 0x80) > 0) {
786
+ position -= 15;
787
+ return
788
+ }
789
+ return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
790
+ }
791
+ }
792
+ }
793
+ }
794
+ }
795
+
796
+ function readOnlyJSString() {
797
+ let token = src[position++];
798
+ let length;
799
+ if (token < 0xc0) {
800
+ // fixstr
801
+ length = token - 0xa0;
802
+ } else {
803
+ switch(token) {
804
+ case 0xd9:
805
+ // str 8
806
+ length = src[position++];
807
+ break
808
+ case 0xda:
809
+ // str 16
810
+ length = dataView.getUint16(position);
811
+ position += 2;
812
+ break
813
+ case 0xdb:
814
+ // str 32
815
+ length = dataView.getUint32(position);
816
+ position += 4;
817
+ break
818
+ default:
819
+ throw new Error('Expected string')
820
+ }
821
+ }
822
+ return readStringJS(length)
823
+ }
824
+
825
+
826
+ function readBin(length) {
827
+ return currentUnpackr.copyBuffers ?
828
+ // specifically use the copying slice (not the node one)
829
+ Uint8Array.prototype.slice.call(src, position, position += length) :
830
+ src.subarray(position, position += length)
831
+ }
832
+ function readExt(length) {
833
+ let type = src[position++];
834
+ if (currentExtensions[type]) {
835
+ return currentExtensions[type](src.subarray(position, position += length))
836
+ }
837
+ else
838
+ throw new Error('Unknown extension type ' + type)
839
+ }
840
+
841
+ var keyCache = new Array(4096);
842
+ function readKey() {
843
+ let length = src[position++];
844
+ if (length >= 0xa0 && length < 0xc0) {
845
+ // fixstr, potentially use key cache
846
+ length = length - 0xa0;
847
+ if (srcStringEnd >= position) // if it has been extracted, must use it (and faster anyway)
848
+ return srcString.slice(position - srcStringStart, (position += length) - srcStringStart)
849
+ else if (!(srcStringEnd == 0 && srcEnd < 180))
850
+ return readFixedString(length)
851
+ } else { // not cacheable, go back and do a standard read
852
+ position--;
853
+ return read()
854
+ }
855
+ let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
856
+ let entry = keyCache[key];
857
+ let checkPosition = position;
858
+ let end = position + length - 3;
859
+ let chunk;
860
+ let i = 0;
861
+ if (entry && entry.bytes == length) {
862
+ while (checkPosition < end) {
863
+ chunk = dataView.getUint32(checkPosition);
864
+ if (chunk != entry[i++]) {
865
+ checkPosition = 0x70000000;
866
+ break
867
+ }
868
+ checkPosition += 4;
869
+ }
870
+ end += 3;
871
+ while (checkPosition < end) {
872
+ chunk = src[checkPosition++];
873
+ if (chunk != entry[i++]) {
874
+ checkPosition = 0x70000000;
875
+ break
876
+ }
877
+ }
878
+ if (checkPosition === end) {
879
+ position = checkPosition;
880
+ return entry.string
881
+ }
882
+ end -= 3;
883
+ checkPosition = position;
884
+ }
885
+ entry = [];
886
+ keyCache[key] = entry;
887
+ entry.bytes = length;
888
+ while (checkPosition < end) {
889
+ chunk = dataView.getUint32(checkPosition);
890
+ entry.push(chunk);
891
+ checkPosition += 4;
892
+ }
893
+ end += 3;
894
+ while (checkPosition < end) {
895
+ chunk = src[checkPosition++];
896
+ entry.push(chunk);
897
+ }
898
+ // for small blocks, avoiding the overhead of the extract call is helpful
899
+ let string = length < 16 ? shortStringInJS(length) : longStringInJS(length);
900
+ if (string != null)
901
+ return entry.string = string
902
+ return entry.string = readFixedString(length)
903
+ }
904
+
905
+ // the registration of the record definition extension (as "r")
906
+ const recordDefinition = (id, highByte) => {
907
+ var structure = read();
908
+ let firstByte = id;
909
+ if (highByte !== undefined) {
910
+ id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
911
+ structure.highByte = highByte;
912
+ }
913
+ let existingStructure = currentStructures[id];
914
+ if (existingStructure && existingStructure.isShared) {
915
+ (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
916
+ }
917
+ currentStructures[id] = structure;
918
+ structure.read = createStructureReader(structure, firstByte);
919
+ return structure.read()
920
+ };
921
+ currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
922
+ currentExtensions[0].noBuffer = true;
923
+
924
+ currentExtensions[0x65] = () => {
925
+ let data = read();
926
+ return (globalThis[data[0]] || Error)(data[1])
927
+ };
928
+
929
+ currentExtensions[0x69] = (data) => {
930
+ // id extension (for structured clones)
931
+ let id = dataView.getUint32(position - 4);
932
+ if (!referenceMap)
933
+ referenceMap = new Map();
934
+ let token = src[position];
935
+ let target;
936
+ // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
937
+ // ahead past references to record structure definitions
938
+ if (token >= 0x90 && token < 0xa0 || token == 0xdc || token == 0xdd)
939
+ target = [];
940
+ else
941
+ target = {};
942
+
943
+ let refEntry = { target }; // a placeholder object
944
+ referenceMap.set(id, refEntry);
945
+ let targetProperties = read(); // read the next value as the target object to id
946
+ if (refEntry.used) // there is a cycle, so we have to assign properties to original target
947
+ return Object.assign(target, targetProperties)
948
+ refEntry.target = targetProperties; // the placeholder wasn't used, replace with the deserialized one
949
+ return targetProperties // no cycle, can just use the returned read object
950
+ };
951
+
952
+ currentExtensions[0x70] = (data) => {
953
+ // pointer extension (for structured clones)
954
+ let id = dataView.getUint32(position - 4);
955
+ let refEntry = referenceMap.get(id);
956
+ refEntry.used = true;
957
+ return refEntry.target
958
+ };
959
+
960
+ currentExtensions[0x73] = () => new Set(read());
961
+
962
+ const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
963
+
964
+ currentExtensions[0x74] = (data) => {
965
+ let typeCode = data[0];
966
+ let typedArrayName = typedArrays[typeCode];
967
+ if (!typedArrayName)
968
+ throw new Error('Could not find typed array for code ' + typeCode)
969
+ // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
970
+ return new globalThis[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
971
+ };
972
+ currentExtensions[0x78] = () => {
973
+ let data = read();
974
+ return new RegExp(data[0], data[1])
975
+ };
976
+ const TEMP_BUNDLE = [];
977
+ currentExtensions[0x62] = (data) => {
978
+ let dataSize = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
979
+ let dataPosition = position;
980
+ position += dataSize - data.length;
981
+ bundledStrings = TEMP_BUNDLE;
982
+ bundledStrings = [readOnlyJSString(), readOnlyJSString()];
983
+ bundledStrings.position0 = 0;
984
+ bundledStrings.position1 = 0;
985
+ bundledStrings.postBundlePosition = position;
986
+ position = dataPosition;
987
+ return read()
988
+ };
989
+
990
+ currentExtensions[0xff] = (data) => {
991
+ // 32-bit date extension
992
+ if (data.length == 4)
993
+ return new Date((data[0] * 0x1000000 + (data[1] << 16) + (data[2] << 8) + data[3]) * 1000)
994
+ else if (data.length == 8)
995
+ return new Date(
996
+ ((data[0] << 22) + (data[1] << 14) + (data[2] << 6) + (data[3] >> 2)) / 1000000 +
997
+ ((data[3] & 0x3) * 0x100000000 + data[4] * 0x1000000 + (data[5] << 16) + (data[6] << 8) + data[7]) * 1000)
998
+ else if (data.length == 12)// TODO: Implement support for negative
999
+ return new Date(
1000
+ ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]) / 1000000 +
1001
+ (((data[4] & 0x80) ? -0x1000000000000 : 0) + data[6] * 0x10000000000 + data[7] * 0x100000000 + data[8] * 0x1000000 + (data[9] << 16) + (data[10] << 8) + data[11]) * 1000)
1002
+ else
1003
+ return new Date('invalid')
1004
+ }; // notepack defines extension 0 to mean undefined, so use that as the default here
1005
+ // registration of bulk record definition?
1006
+ // currentExtensions[0x52] = () =>
1007
+
1008
+ function saveState(callback) {
1009
+ let savedSrcEnd = srcEnd;
1010
+ let savedPosition = position;
1011
+ let savedStringPosition = stringPosition;
1012
+ let savedSrcStringStart = srcStringStart;
1013
+ let savedSrcStringEnd = srcStringEnd;
1014
+ let savedSrcString = srcString;
1015
+ let savedStrings = strings;
1016
+ let savedReferenceMap = referenceMap;
1017
+ let savedBundledStrings = bundledStrings;
1018
+
1019
+ // TODO: We may need to revisit this if we do more external calls to user code (since it could be slow)
1020
+ let savedSrc = new Uint8Array(src.slice(0, srcEnd)); // we copy the data in case it changes while external data is processed
1021
+ let savedStructures = currentStructures;
1022
+ let savedStructuresContents = currentStructures.slice(0, currentStructures.length);
1023
+ let savedPackr = currentUnpackr;
1024
+ let savedSequentialMode = sequentialMode;
1025
+ let value = callback();
1026
+ srcEnd = savedSrcEnd;
1027
+ position = savedPosition;
1028
+ stringPosition = savedStringPosition;
1029
+ srcStringStart = savedSrcStringStart;
1030
+ srcStringEnd = savedSrcStringEnd;
1031
+ srcString = savedSrcString;
1032
+ strings = savedStrings;
1033
+ referenceMap = savedReferenceMap;
1034
+ bundledStrings = savedBundledStrings;
1035
+ src = savedSrc;
1036
+ sequentialMode = savedSequentialMode;
1037
+ currentStructures = savedStructures;
1038
+ currentStructures.splice(0, currentStructures.length, ...savedStructuresContents);
1039
+ currentUnpackr = savedPackr;
1040
+ dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
1041
+ return value
1042
+ }
1043
+ function clearSource() {
1044
+ src = null;
1045
+ referenceMap = null;
1046
+ currentStructures = null;
1047
+ }
1048
+
1049
+ function addExtension(extension) {
1050
+ if (extension.unpack)
1051
+ currentExtensions[extension.type] = extension.unpack;
1052
+ else
1053
+ currentExtensions[extension.type] = extension;
1054
+ }
1055
+
1056
+ const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1057
+ for (let i = 0; i < 256; i++) {
1058
+ mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
1059
+ }
1060
+ const Decoder = Unpackr;
1061
+ var defaultUnpackr = new Unpackr({ useRecords: false });
1062
+ const unpack = defaultUnpackr.unpack;
1063
+ const unpackMultiple = defaultUnpackr.unpackMultiple;
1064
+ const decode = defaultUnpackr.unpack;
1065
+ const FLOAT32_OPTIONS = {
1066
+ NEVER: 0,
1067
+ ALWAYS: 1,
1068
+ DECIMAL_ROUND: 3,
1069
+ DECIMAL_FIT: 4
1070
+ };
1071
+ let f32Array = new Float32Array(1);
1072
+ let u8Array = new Uint8Array(f32Array.buffer, 0, 4);
1073
+ function roundFloat32(float32Number) {
1074
+ f32Array[0] = float32Number;
1075
+ let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1076
+ return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier
1077
+ }
1078
+ function setReadStruct(func) {
1079
+ readStruct = func;
1080
+ }
1081
+
1082
+ let textEncoder;
1083
+ try {
1084
+ textEncoder = new TextEncoder();
1085
+ } catch (error) {}
1086
+ let extensions, extensionClasses;
1087
+ const hasNodeBuffer = typeof Buffer !== 'undefined';
1088
+ const ByteArrayAllocate = hasNodeBuffer ?
1089
+ function(length) { return Buffer.allocUnsafeSlow(length) } : Uint8Array;
1090
+ const ByteArray = hasNodeBuffer ? Buffer : Uint8Array;
1091
+ const MAX_BUFFER_SIZE = hasNodeBuffer ? 0x100000000 : 0x7fd00000;
1092
+ let target, keysTarget;
1093
+ let targetView;
1094
+ let position$1 = 0;
1095
+ let safeEnd;
1096
+ let bundledStrings$1 = null;
1097
+ let writeStructSlots;
1098
+ const MAX_BUNDLE_SIZE = 0xf000;
1099
+ const hasNonLatin = /[\u0080-\uFFFF]/;
1100
+ const RECORD_SYMBOL = Symbol('record-id');
1101
+ class Packr extends Unpackr {
1102
+ constructor(options) {
1103
+ super(options);
1104
+ this.offset = 0;
1105
+ let start;
1106
+ let hasSharedUpdate;
1107
+ let structures;
1108
+ let referenceMap;
1109
+ let lastSharedStructuresLength = 0;
1110
+ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1111
+ return target.utf8Write(string, position, 0xffffffff)
1112
+ } : (textEncoder && textEncoder.encodeInto) ?
1113
+ function(string, position) {
1114
+ return textEncoder.encodeInto(string, target.subarray(position)).written
1115
+ } : false;
1116
+
1117
+ let packr = this;
1118
+ if (!options)
1119
+ options = {};
1120
+ let isSequential = options && options.sequential;
1121
+ let hasSharedStructures = options.structures || options.saveStructures;
1122
+ let maxSharedStructures = options.maxSharedStructures;
1123
+ if (maxSharedStructures == null)
1124
+ maxSharedStructures = hasSharedStructures ? 32 : 0;
1125
+ if (maxSharedStructures > 8160)
1126
+ throw new Error('Maximum maxSharedStructure is 8160')
1127
+ if (options.structuredClone && options.moreTypes == undefined) {
1128
+ options.moreTypes = true;
1129
+ }
1130
+ let maxOwnStructures = options.maxOwnStructures;
1131
+ if (maxOwnStructures == null)
1132
+ maxOwnStructures = hasSharedStructures ? 32 : 64;
1133
+ if (!this.structures && options.useRecords != false)
1134
+ this.structures = [];
1135
+ // two byte record ids for shared structures
1136
+ let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1137
+ let sharedLimitId = maxSharedStructures + 0x40;
1138
+ let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
1139
+ if (maxStructureId > 8256) {
1140
+ throw new Error('Maximum maxSharedStructure + maxOwnStructure is 8192')
1141
+ }
1142
+ let recordIdsToRemove = [];
1143
+ let transitionsCount = 0;
1144
+ let serializationsSinceTransitionRebuild = 0;
1145
+
1146
+ this.pack = this.encode = function(value, encodeOptions) {
1147
+ if (!target) {
1148
+ target = new ByteArrayAllocate(8192);
1149
+ targetView = target.dataView = new DataView(target.buffer, 0, 8192);
1150
+ position$1 = 0;
1151
+ }
1152
+ safeEnd = target.length - 10;
1153
+ if (safeEnd - position$1 < 0x800) {
1154
+ // don't start too close to the end,
1155
+ target = new ByteArrayAllocate(target.length);
1156
+ targetView = target.dataView = new DataView(target.buffer, 0, target.length);
1157
+ safeEnd = target.length - 10;
1158
+ position$1 = 0;
1159
+ } else
1160
+ position$1 = (position$1 + 7) & 0x7ffffff8; // Word align to make any future copying of this buffer faster
1161
+ start = position$1;
1162
+ referenceMap = packr.structuredClone ? new Map() : null;
1163
+ if (packr.bundleStrings && typeof value !== 'string') {
1164
+ bundledStrings$1 = [];
1165
+ bundledStrings$1.size = Infinity; // force a new bundle start on first string
1166
+ } else
1167
+ bundledStrings$1 = null;
1168
+ structures = packr.structures;
1169
+ if (structures) {
1170
+ if (structures.uninitialized)
1171
+ structures = packr._mergeStructures(packr.getStructures());
1172
+ let sharedLength = structures.sharedLength || 0;
1173
+ if (sharedLength > maxSharedStructures) {
1174
+ //if (maxSharedStructures <= 32 && structures.sharedLength > 32) // TODO: could support this, but would need to update the limit ids
1175
+ throw new Error('Shared structures is larger than maximum shared structures, try increasing maxSharedStructures to ' + structures.sharedLength)
1176
+ }
1177
+ if (!structures.transitions) {
1178
+ // rebuild our structure transitions
1179
+ structures.transitions = Object.create(null);
1180
+ for (let i = 0; i < sharedLength; i++) {
1181
+ let keys = structures[i];
1182
+ if (!keys)
1183
+ continue
1184
+ let nextTransition, transition = structures.transitions;
1185
+ for (let j = 0, l = keys.length; j < l; j++) {
1186
+ let key = keys[j];
1187
+ nextTransition = transition[key];
1188
+ if (!nextTransition) {
1189
+ nextTransition = transition[key] = Object.create(null);
1190
+ }
1191
+ transition = nextTransition;
1192
+ }
1193
+ transition[RECORD_SYMBOL] = i + 0x40;
1194
+ }
1195
+ lastSharedStructuresLength = sharedLength;
1196
+ }
1197
+ if (!isSequential) {
1198
+ structures.nextId = sharedLength + 0x40;
1199
+ }
1200
+ }
1201
+ if (hasSharedUpdate)
1202
+ hasSharedUpdate = false;
1203
+ try {
1204
+ if (packr.randomAccessStructure)
1205
+ writeStruct(value);
1206
+ else
1207
+ pack(value);
1208
+ if (bundledStrings$1) {
1209
+ writeBundles(start, pack);
1210
+ }
1211
+ packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1212
+ if (referenceMap && referenceMap.idsToInsert) {
1213
+ position$1 += referenceMap.idsToInsert.length * 6;
1214
+ if (position$1 > safeEnd)
1215
+ makeRoom(position$1);
1216
+ packr.offset = position$1;
1217
+ let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
1218
+ referenceMap = null;
1219
+ return serialized
1220
+ }
1221
+ if (encodeOptions & REUSE_BUFFER_MODE) {
1222
+ target.start = start;
1223
+ target.end = position$1;
1224
+ return target
1225
+ }
1226
+ return target.subarray(start, position$1) // position can change if we call pack again in saveStructures, so we get the buffer now
1227
+ } finally {
1228
+ if (structures) {
1229
+ if (serializationsSinceTransitionRebuild < 10)
1230
+ serializationsSinceTransitionRebuild++;
1231
+ let sharedLength = structures.sharedLength || maxSharedStructures;
1232
+ if (structures.length > sharedLength)
1233
+ structures.length = sharedLength;
1234
+ if (transitionsCount > 10000) {
1235
+ // force a rebuild occasionally after a lot of transitions so it can get cleaned up
1236
+ structures.transitions = null;
1237
+ serializationsSinceTransitionRebuild = 0;
1238
+ transitionsCount = 0;
1239
+ if (recordIdsToRemove.length > 0)
1240
+ recordIdsToRemove = [];
1241
+ } else if (recordIdsToRemove.length > 0 && !isSequential) {
1242
+ for (let i = 0, l = recordIdsToRemove.length; i < l; i++) {
1243
+ recordIdsToRemove[i][RECORD_SYMBOL] = 0;
1244
+ }
1245
+ recordIdsToRemove = [];
1246
+ }
1247
+ if (hasSharedUpdate && packr.saveStructures) {
1248
+ // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1249
+ let returnBuffer = target.subarray(start, position$1);
1250
+ if (packr.saveStructures(structures, lastSharedStructuresLength) === false) {
1251
+ // get updated structures and try again if the update failed
1252
+ packr._mergeStructures(packr.getStructures());
1253
+ return packr.pack(value)
1254
+ }
1255
+ lastSharedStructuresLength = sharedLength;
1256
+ return returnBuffer
1257
+ }
1258
+ }
1259
+ if (encodeOptions & RESET_BUFFER_MODE)
1260
+ position$1 = start;
1261
+ }
1262
+ };
1263
+ const pack = (value) => {
1264
+ if (position$1 > safeEnd)
1265
+ target = makeRoom(position$1);
1266
+
1267
+ var type = typeof value;
1268
+ var length;
1269
+ if (type === 'string') {
1270
+ let strLength = value.length;
1271
+ if (bundledStrings$1 && strLength >= 4 && strLength < 0x1000) {
1272
+ if ((bundledStrings$1.size += strLength) > MAX_BUNDLE_SIZE) {
1273
+ let extStart;
1274
+ let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1275
+ if (position$1 + maxBytes > safeEnd)
1276
+ target = makeRoom(position$1 + maxBytes);
1277
+ if (bundledStrings$1.position) { // here we use the 0x62 extension to write the last bundle and reserve sapce for the reference pointer to the next/current bundle
1278
+ target[position$1] = 0xc8; // ext 16
1279
+ position$1 += 3; // reserve for the writing bundle size
1280
+ target[position$1++] = 0x62; // 'b'
1281
+ extStart = position$1 - start;
1282
+ position$1 += 4; // reserve for writing bundle reference
1283
+ writeBundles(start, pack); // write the last bundles
1284
+ targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1285
+ } else { // here we use the 0x62 extension just to reserve the space for the reference pointer to the bundle (will be updated once the bundle is written)
1286
+ target[position$1++] = 0xd6; // fixext 4
1287
+ target[position$1++] = 0x62; // 'b'
1288
+ extStart = position$1 - start;
1289
+ position$1 += 4; // reserve for writing bundle reference
1290
+ }
1291
+ bundledStrings$1 = ['', '']; // create new ones
1292
+ bundledStrings$1.size = 0;
1293
+ bundledStrings$1.position = extStart;
1294
+ }
1295
+ let twoByte = hasNonLatin.test(value);
1296
+ bundledStrings$1[twoByte ? 0 : 1] += value;
1297
+ target[position$1++] = 0xc1;
1298
+ pack(twoByte ? -strLength : strLength);
1299
+ return
1300
+ }
1301
+ let headerSize;
1302
+ // first we estimate the header size, so we can write to the correct location
1303
+ if (strLength < 0x20) {
1304
+ headerSize = 1;
1305
+ } else if (strLength < 0x100) {
1306
+ headerSize = 2;
1307
+ } else if (strLength < 0x10000) {
1308
+ headerSize = 3;
1309
+ } else {
1310
+ headerSize = 5;
1311
+ }
1312
+ let maxBytes = strLength * 3;
1313
+ if (position$1 + maxBytes > safeEnd)
1314
+ target = makeRoom(position$1 + maxBytes);
1315
+
1316
+ if (strLength < 0x40 || !encodeUtf8) {
1317
+ let i, c1, c2, strPosition = position$1 + headerSize;
1318
+ for (i = 0; i < strLength; i++) {
1319
+ c1 = value.charCodeAt(i);
1320
+ if (c1 < 0x80) {
1321
+ target[strPosition++] = c1;
1322
+ } else if (c1 < 0x800) {
1323
+ target[strPosition++] = c1 >> 6 | 0xc0;
1324
+ target[strPosition++] = c1 & 0x3f | 0x80;
1325
+ } else if (
1326
+ (c1 & 0xfc00) === 0xd800 &&
1327
+ ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00
1328
+ ) {
1329
+ c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
1330
+ i++;
1331
+ target[strPosition++] = c1 >> 18 | 0xf0;
1332
+ target[strPosition++] = c1 >> 12 & 0x3f | 0x80;
1333
+ target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
1334
+ target[strPosition++] = c1 & 0x3f | 0x80;
1335
+ } else {
1336
+ target[strPosition++] = c1 >> 12 | 0xe0;
1337
+ target[strPosition++] = c1 >> 6 & 0x3f | 0x80;
1338
+ target[strPosition++] = c1 & 0x3f | 0x80;
1339
+ }
1340
+ }
1341
+ length = strPosition - position$1 - headerSize;
1342
+ } else {
1343
+ length = encodeUtf8(value, position$1 + headerSize);
1344
+ }
1345
+
1346
+ if (length < 0x20) {
1347
+ target[position$1++] = 0xa0 | length;
1348
+ } else if (length < 0x100) {
1349
+ if (headerSize < 2) {
1350
+ target.copyWithin(position$1 + 2, position$1 + 1, position$1 + 1 + length);
1351
+ }
1352
+ target[position$1++] = 0xd9;
1353
+ target[position$1++] = length;
1354
+ } else if (length < 0x10000) {
1355
+ if (headerSize < 3) {
1356
+ target.copyWithin(position$1 + 3, position$1 + 2, position$1 + 2 + length);
1357
+ }
1358
+ target[position$1++] = 0xda;
1359
+ target[position$1++] = length >> 8;
1360
+ target[position$1++] = length & 0xff;
1361
+ } else {
1362
+ if (headerSize < 5) {
1363
+ target.copyWithin(position$1 + 5, position$1 + 3, position$1 + 3 + length);
1364
+ }
1365
+ target[position$1++] = 0xdb;
1366
+ targetView.setUint32(position$1, length);
1367
+ position$1 += 4;
1368
+ }
1369
+ position$1 += length;
1370
+ } else if (type === 'number') {
1371
+ if (value >>> 0 === value) {// positive integer, 32-bit or less
1372
+ // positive uint
1373
+ if (value < 0x40 || (value < 0x80 && this.useRecords === false)) {
1374
+ target[position$1++] = value;
1375
+ } else if (value < 0x100) {
1376
+ target[position$1++] = 0xcc;
1377
+ target[position$1++] = value;
1378
+ } else if (value < 0x10000) {
1379
+ target[position$1++] = 0xcd;
1380
+ target[position$1++] = value >> 8;
1381
+ target[position$1++] = value & 0xff;
1382
+ } else {
1383
+ target[position$1++] = 0xce;
1384
+ targetView.setUint32(position$1, value);
1385
+ position$1 += 4;
1386
+ }
1387
+ } else if (value >> 0 === value) { // negative integer
1388
+ if (value >= -0x20) {
1389
+ target[position$1++] = 0x100 + value;
1390
+ } else if (value >= -0x80) {
1391
+ target[position$1++] = 0xd0;
1392
+ target[position$1++] = value + 0x100;
1393
+ } else if (value >= -0x8000) {
1394
+ target[position$1++] = 0xd1;
1395
+ targetView.setInt16(position$1, value);
1396
+ position$1 += 2;
1397
+ } else {
1398
+ target[position$1++] = 0xd2;
1399
+ targetView.setInt32(position$1, value);
1400
+ position$1 += 4;
1401
+ }
1402
+ } else {
1403
+ let useFloat32;
1404
+ if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
1405
+ target[position$1++] = 0xca;
1406
+ targetView.setFloat32(position$1, value);
1407
+ let xShifted;
1408
+ if (useFloat32 < 4 ||
1409
+ // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
1410
+ ((xShifted = value * mult10[((target[position$1] & 0x7f) << 1) | (target[position$1 + 1] >> 7)]) >> 0) === xShifted) {
1411
+ position$1 += 4;
1412
+ return
1413
+ } else
1414
+ position$1--; // move back into position for writing a double
1415
+ }
1416
+ target[position$1++] = 0xcb;
1417
+ targetView.setFloat64(position$1, value);
1418
+ position$1 += 8;
1419
+ }
1420
+ } else if (type === 'object') {
1421
+ if (!value)
1422
+ target[position$1++] = 0xc0;
1423
+ else {
1424
+ if (referenceMap) {
1425
+ let referee = referenceMap.get(value);
1426
+ if (referee) {
1427
+ if (!referee.id) {
1428
+ let idsToInsert = referenceMap.idsToInsert || (referenceMap.idsToInsert = []);
1429
+ referee.id = idsToInsert.push(referee);
1430
+ }
1431
+ target[position$1++] = 0xd6; // fixext 4
1432
+ target[position$1++] = 0x70; // "p" for pointer
1433
+ targetView.setUint32(position$1, referee.id);
1434
+ position$1 += 4;
1435
+ return
1436
+ } else
1437
+ referenceMap.set(value, { offset: position$1 - start });
1438
+ }
1439
+ let constructor = value.constructor;
1440
+ if (constructor === Object) {
1441
+ writeObject(value, true);
1442
+ } else if (constructor === Array) {
1443
+ length = value.length;
1444
+ if (length < 0x10) {
1445
+ target[position$1++] = 0x90 | length;
1446
+ } else if (length < 0x10000) {
1447
+ target[position$1++] = 0xdc;
1448
+ target[position$1++] = length >> 8;
1449
+ target[position$1++] = length & 0xff;
1450
+ } else {
1451
+ target[position$1++] = 0xdd;
1452
+ targetView.setUint32(position$1, length);
1453
+ position$1 += 4;
1454
+ }
1455
+ for (let i = 0; i < length; i++) {
1456
+ pack(value[i]);
1457
+ }
1458
+ } else if (constructor === Map) {
1459
+ length = value.size;
1460
+ if (length < 0x10) {
1461
+ target[position$1++] = 0x80 | length;
1462
+ } else if (length < 0x10000) {
1463
+ target[position$1++] = 0xde;
1464
+ target[position$1++] = length >> 8;
1465
+ target[position$1++] = length & 0xff;
1466
+ } else {
1467
+ target[position$1++] = 0xdf;
1468
+ targetView.setUint32(position$1, length);
1469
+ position$1 += 4;
1470
+ }
1471
+ for (let [ key, entryValue ] of value) {
1472
+ pack(key);
1473
+ pack(entryValue);
1474
+ }
1475
+ } else {
1476
+ for (let i = 0, l = extensions.length; i < l; i++) {
1477
+ let extensionClass = extensionClasses[i];
1478
+ if (value instanceof extensionClass) {
1479
+ let extension = extensions[i];
1480
+ if (extension.write) {
1481
+ if (extension.type) {
1482
+ target[position$1++] = 0xd4; // one byte "tag" extension
1483
+ target[position$1++] = extension.type;
1484
+ target[position$1++] = 0;
1485
+ }
1486
+ pack(extension.write.call(this, value));
1487
+ return
1488
+ }
1489
+ let currentTarget = target;
1490
+ let currentTargetView = targetView;
1491
+ let currentPosition = position$1;
1492
+ target = null;
1493
+ let result;
1494
+ try {
1495
+ result = extension.pack.call(this, value, (size) => {
1496
+ // restore target and use it
1497
+ target = currentTarget;
1498
+ currentTarget = null;
1499
+ position$1 += size;
1500
+ if (position$1 > safeEnd)
1501
+ makeRoom(position$1);
1502
+ return {
1503
+ target, targetView, position: position$1 - size
1504
+ }
1505
+ }, pack);
1506
+ } finally {
1507
+ // restore current target information (unless already restored)
1508
+ if (currentTarget) {
1509
+ target = currentTarget;
1510
+ targetView = currentTargetView;
1511
+ position$1 = currentPosition;
1512
+ safeEnd = target.length - 10;
1513
+ }
1514
+ }
1515
+ if (result) {
1516
+ if (result.length + position$1 > safeEnd)
1517
+ makeRoom(result.length + position$1);
1518
+ position$1 = writeExtensionData(result, target, position$1, extension.type);
1519
+ }
1520
+ return
1521
+ }
1522
+ }
1523
+ // no extension found, write as object
1524
+ writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1525
+ }
1526
+ }
1527
+ } else if (type === 'boolean') {
1528
+ target[position$1++] = value ? 0xc3 : 0xc2;
1529
+ } else if (type === 'bigint') {
1530
+ if (value < (BigInt(1)<<BigInt(63)) && value >= -(BigInt(1)<<BigInt(63))) {
1531
+ // use a signed int as long as it fits
1532
+ target[position$1++] = 0xd3;
1533
+ targetView.setBigInt64(position$1, value);
1534
+ } else if (value < (BigInt(1)<<BigInt(64)) && value > 0) {
1535
+ // if we can fit an unsigned int, use that
1536
+ target[position$1++] = 0xcf;
1537
+ targetView.setBigUint64(position$1, value);
1538
+ } else {
1539
+ // overflow
1540
+ if (this.largeBigIntToFloat) {
1541
+ target[position$1++] = 0xcb;
1542
+ targetView.setFloat64(position$1, Number(value));
1543
+ } else {
1544
+ throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1545
+ }
1546
+ }
1547
+ position$1 += 8;
1548
+ } else if (type === 'undefined') {
1549
+ if (this.encodeUndefinedAsNil)
1550
+ target[position$1++] = 0xc0;
1551
+ else {
1552
+ target[position$1++] = 0xd4; // a number of implementations use fixext1 with type 0, data 0 to denote undefined, so we follow suite
1553
+ target[position$1++] = 0;
1554
+ target[position$1++] = 0;
1555
+ }
1556
+ } else if (type === 'function') {
1557
+ pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
1558
+ } else {
1559
+ throw new Error('Unknown type: ' + type)
1560
+ }
1561
+ };
1562
+
1563
+ const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
1564
+ // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1565
+ let keys = Object.keys(object);
1566
+ let length = keys.length;
1567
+ if (length < 0x10) {
1568
+ target[position$1++] = 0x80 | length;
1569
+ } else if (length < 0x10000) {
1570
+ target[position$1++] = 0xde;
1571
+ target[position$1++] = length >> 8;
1572
+ target[position$1++] = length & 0xff;
1573
+ } else {
1574
+ target[position$1++] = 0xdf;
1575
+ targetView.setUint32(position$1, length);
1576
+ position$1 += 4;
1577
+ }
1578
+ let key;
1579
+ for (let i = 0; i < length; i++) {
1580
+ pack(key = keys[i]);
1581
+ pack(object[key]);
1582
+ }
1583
+ } :
1584
+ (object, safePrototype) => {
1585
+ target[position$1++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1586
+ let objectOffset = position$1 - start;
1587
+ position$1 += 2;
1588
+ let size = 0;
1589
+ for (let key in object) {
1590
+ if (safePrototype || object.hasOwnProperty(key)) {
1591
+ pack(key);
1592
+ pack(object[key]);
1593
+ size++;
1594
+ }
1595
+ }
1596
+ target[objectOffset++ + start] = size >> 8;
1597
+ target[objectOffset + start] = size & 0xff;
1598
+ } :
1599
+ (options.progressiveRecords && !useTwoByteRecords) ? // this is about 2% faster for highly stable structures, since it only requires one for-in loop (but much more expensive when new structure needs to be written)
1600
+ (object, safePrototype) => {
1601
+ let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1602
+ let objectOffset = position$1++ - start;
1603
+ let wroteKeys;
1604
+ for (let key in object) {
1605
+ if (safePrototype || object.hasOwnProperty(key)) {
1606
+ nextTransition = transition[key];
1607
+ if (nextTransition)
1608
+ transition = nextTransition;
1609
+ else {
1610
+ // record doesn't exist, create full new record and insert it
1611
+ let keys = Object.keys(object);
1612
+ let lastTransition = transition;
1613
+ transition = structures.transitions;
1614
+ let newTransitions = 0;
1615
+ for (let i = 0, l = keys.length; i < l; i++) {
1616
+ let key = keys[i];
1617
+ nextTransition = transition[key];
1618
+ if (!nextTransition) {
1619
+ nextTransition = transition[key] = Object.create(null);
1620
+ newTransitions++;
1621
+ }
1622
+ transition = nextTransition;
1623
+ }
1624
+ if (objectOffset + start + 1 == position$1) {
1625
+ // first key, so we don't need to insert, we can just write record directly
1626
+ position$1--;
1627
+ newRecord(transition, keys, newTransitions);
1628
+ } else // otherwise we need to insert the record, moving existing data after the record
1629
+ insertNewRecord(transition, keys, objectOffset, newTransitions);
1630
+ wroteKeys = true;
1631
+ transition = lastTransition[key];
1632
+ }
1633
+ pack(object[key]);
1634
+ }
1635
+ }
1636
+ if (!wroteKeys) {
1637
+ let recordId = transition[RECORD_SYMBOL];
1638
+ if (recordId)
1639
+ target[objectOffset + start] = recordId;
1640
+ else
1641
+ insertNewRecord(transition, Object.keys(object), objectOffset, 0);
1642
+ }
1643
+ } :
1644
+ (object, safePrototype) => {
1645
+ let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1646
+ let newTransitions = 0;
1647
+ for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
1648
+ nextTransition = transition[key];
1649
+ if (!nextTransition) {
1650
+ nextTransition = transition[key] = Object.create(null);
1651
+ newTransitions++;
1652
+ }
1653
+ transition = nextTransition;
1654
+ }
1655
+ let recordId = transition[RECORD_SYMBOL];
1656
+ if (recordId) {
1657
+ if (recordId >= 0x60 && useTwoByteRecords) {
1658
+ target[position$1++] = ((recordId -= 0x60) & 0x1f) + 0x60;
1659
+ target[position$1++] = recordId >> 5;
1660
+ } else
1661
+ target[position$1++] = recordId;
1662
+ } else {
1663
+ newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions);
1664
+ }
1665
+ // now write the values
1666
+ for (let key in object)
1667
+ if (safePrototype || object.hasOwnProperty(key))
1668
+ pack(object[key]);
1669
+ };
1670
+ const makeRoom = (end) => {
1671
+ let newSize;
1672
+ if (end > 0x1000000) {
1673
+ // special handling for really large buffers
1674
+ if ((end - start) > MAX_BUFFER_SIZE)
1675
+ throw new Error('Packed buffer would be larger than maximum buffer size')
1676
+ newSize = Math.min(MAX_BUFFER_SIZE,
1677
+ Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) * 0x1000);
1678
+ } else // faster handling for smaller buffers
1679
+ newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
1680
+ let newBuffer = new ByteArrayAllocate(newSize);
1681
+ targetView = newBuffer.dataView = new DataView(newBuffer.buffer, 0, newSize);
1682
+ end = Math.min(end, target.length);
1683
+ if (target.copy)
1684
+ target.copy(newBuffer, 0, start, end);
1685
+ else
1686
+ newBuffer.set(target.slice(start, end));
1687
+ position$1 -= start;
1688
+ start = 0;
1689
+ safeEnd = newBuffer.length - 10;
1690
+ return target = newBuffer
1691
+ };
1692
+ const newRecord = (transition, keys, newTransitions) => {
1693
+ let recordId = structures.nextId;
1694
+ if (!recordId)
1695
+ recordId = 0x40;
1696
+ if (recordId < sharedLimitId && this.shouldShareStructure && !this.shouldShareStructure(keys)) {
1697
+ recordId = structures.nextOwnId;
1698
+ if (!(recordId < maxStructureId))
1699
+ recordId = sharedLimitId;
1700
+ structures.nextOwnId = recordId + 1;
1701
+ } else {
1702
+ if (recordId >= maxStructureId)// cycle back around
1703
+ recordId = sharedLimitId;
1704
+ structures.nextId = recordId + 1;
1705
+ }
1706
+ let highByte = keys.highByte = recordId >= 0x60 && useTwoByteRecords ? (recordId - 0x60) >> 5 : -1;
1707
+ transition[RECORD_SYMBOL] = recordId;
1708
+ transition.__keys__ = keys;
1709
+ structures[recordId - 0x40] = keys;
1710
+
1711
+ if (recordId < sharedLimitId) {
1712
+ keys.isShared = true;
1713
+ structures.sharedLength = recordId - 0x3f;
1714
+ hasSharedUpdate = true;
1715
+ if (highByte >= 0) {
1716
+ target[position$1++] = (recordId & 0x1f) + 0x60;
1717
+ target[position$1++] = highByte;
1718
+ } else {
1719
+ target[position$1++] = recordId;
1720
+ }
1721
+ } else {
1722
+ if (highByte >= 0) {
1723
+ target[position$1++] = 0xd5; // fixext 2
1724
+ target[position$1++] = 0x72; // "r" record defintion extension type
1725
+ target[position$1++] = (recordId & 0x1f) + 0x60;
1726
+ target[position$1++] = highByte;
1727
+ } else {
1728
+ target[position$1++] = 0xd4; // fixext 1
1729
+ target[position$1++] = 0x72; // "r" record defintion extension type
1730
+ target[position$1++] = recordId;
1731
+ }
1732
+
1733
+ if (newTransitions)
1734
+ transitionsCount += serializationsSinceTransitionRebuild * newTransitions;
1735
+ // record the removal of the id, we can maintain our shared structure
1736
+ if (recordIdsToRemove.length >= maxOwnStructures)
1737
+ recordIdsToRemove.shift()[RECORD_SYMBOL] = 0; // we are cycling back through, and have to remove old ones
1738
+ recordIdsToRemove.push(transition);
1739
+ pack(keys);
1740
+ }
1741
+ };
1742
+ const insertNewRecord = (transition, keys, insertionOffset, newTransitions) => {
1743
+ let mainTarget = target;
1744
+ let mainPosition = position$1;
1745
+ let mainSafeEnd = safeEnd;
1746
+ let mainStart = start;
1747
+ target = keysTarget;
1748
+ position$1 = 0;
1749
+ start = 0;
1750
+ if (!target)
1751
+ keysTarget = target = new ByteArrayAllocate(8192);
1752
+ safeEnd = target.length - 10;
1753
+ newRecord(transition, keys, newTransitions);
1754
+ keysTarget = target;
1755
+ let keysPosition = position$1;
1756
+ target = mainTarget;
1757
+ position$1 = mainPosition;
1758
+ safeEnd = mainSafeEnd;
1759
+ start = mainStart;
1760
+ if (keysPosition > 1) {
1761
+ let newEnd = position$1 + keysPosition - 1;
1762
+ if (newEnd > safeEnd)
1763
+ makeRoom(newEnd);
1764
+ let insertionPosition = insertionOffset + start;
1765
+ target.copyWithin(insertionPosition + keysPosition, insertionPosition + 1, position$1);
1766
+ target.set(keysTarget.slice(0, keysPosition), insertionPosition);
1767
+ position$1 = newEnd;
1768
+ } else {
1769
+ target[insertionOffset + start] = keysTarget[0];
1770
+ }
1771
+ };
1772
+ const writeStruct = (object, safePrototype) => {
1773
+ let newPosition = writeStructSlots(object, target, position$1, structures, makeRoom, (value, newPosition) => {
1774
+ position$1 = newPosition;
1775
+ if (start > 0) {
1776
+ pack(value);
1777
+ if (start == 0)
1778
+ return { position: position$1, targetView }; // indicate the buffer was re-allocated
1779
+ } else
1780
+ pack(value);
1781
+ return position$1;
1782
+ });
1783
+ if (newPosition === 0) // bail and go to a msgpack object
1784
+ return writeObject(object, true);
1785
+ position$1 = newPosition;
1786
+ };
1787
+ }
1788
+ useBuffer(buffer) {
1789
+ // this means we are finished using our own buffer and we can write over it safely
1790
+ target = buffer;
1791
+ targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1792
+ position$1 = 0;
1793
+ }
1794
+ clearSharedData() {
1795
+ if (this.structures)
1796
+ this.structures = [];
1797
+ }
1798
+ }
1799
+
1800
+ extensionClasses = [ Date, Set, Error, RegExp, ArrayBuffer, Object.getPrototypeOf(Uint8Array.prototype).constructor /*TypedArray*/, C1Type ];
1801
+ extensions = [{
1802
+ pack(date, allocateForWrite, pack) {
1803
+ let seconds = date.getTime() / 1000;
1804
+ if ((this.useTimestamp32 || date.getMilliseconds() === 0) && seconds >= 0 && seconds < 0x100000000) {
1805
+ // Timestamp 32
1806
+ let { target, targetView, position} = allocateForWrite(6);
1807
+ target[position++] = 0xd6;
1808
+ target[position++] = 0xff;
1809
+ targetView.setUint32(position, seconds);
1810
+ } else if (seconds > 0 && seconds < 0x100000000) {
1811
+ // Timestamp 64
1812
+ let { target, targetView, position} = allocateForWrite(10);
1813
+ target[position++] = 0xd7;
1814
+ target[position++] = 0xff;
1815
+ targetView.setUint32(position, date.getMilliseconds() * 4000000 + ((seconds / 1000 / 0x100000000) >> 0));
1816
+ targetView.setUint32(position + 4, seconds);
1817
+ } else if (isNaN(seconds)) {
1818
+ if (this.onInvalidDate) {
1819
+ allocateForWrite(0);
1820
+ return pack(this.onInvalidDate())
1821
+ }
1822
+ // Intentionally invalid timestamp
1823
+ let { target, targetView, position} = allocateForWrite(3);
1824
+ target[position++] = 0xd4;
1825
+ target[position++] = 0xff;
1826
+ target[position++] = 0xff;
1827
+ } else {
1828
+ // Timestamp 96
1829
+ let { target, targetView, position} = allocateForWrite(15);
1830
+ target[position++] = 0xc7;
1831
+ target[position++] = 12;
1832
+ target[position++] = 0xff;
1833
+ targetView.setUint32(position, date.getMilliseconds() * 1000000);
1834
+ targetView.setBigInt64(position + 4, BigInt(Math.floor(seconds)));
1835
+ }
1836
+ }
1837
+ }, {
1838
+ pack(set, allocateForWrite, pack) {
1839
+ let array = Array.from(set);
1840
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1841
+ if (this.moreTypes) {
1842
+ target[position++] = 0xd4;
1843
+ target[position++] = 0x73; // 's' for Set
1844
+ target[position++] = 0;
1845
+ }
1846
+ pack(array);
1847
+ }
1848
+ }, {
1849
+ pack(error, allocateForWrite, pack) {
1850
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1851
+ if (this.moreTypes) {
1852
+ target[position++] = 0xd4;
1853
+ target[position++] = 0x65; // 'e' for error
1854
+ target[position++] = 0;
1855
+ }
1856
+ pack([ error.name, error.message ]);
1857
+ }
1858
+ }, {
1859
+ pack(regex, allocateForWrite, pack) {
1860
+ let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1861
+ if (this.moreTypes) {
1862
+ target[position++] = 0xd4;
1863
+ target[position++] = 0x78; // 'x' for regeXp
1864
+ target[position++] = 0;
1865
+ }
1866
+ pack([ regex.source, regex.flags ]);
1867
+ }
1868
+ }, {
1869
+ pack(arrayBuffer, allocateForWrite) {
1870
+ if (this.moreTypes)
1871
+ writeExtBuffer(arrayBuffer, 0x10, allocateForWrite);
1872
+ else
1873
+ writeBuffer(hasNodeBuffer ? Buffer.from(arrayBuffer) : new Uint8Array(arrayBuffer), allocateForWrite);
1874
+ }
1875
+ }, {
1876
+ pack(typedArray, allocateForWrite) {
1877
+ let constructor = typedArray.constructor;
1878
+ if (constructor !== ByteArray && this.moreTypes)
1879
+ writeExtBuffer(typedArray, typedArrays.indexOf(constructor.name), allocateForWrite);
1880
+ else
1881
+ writeBuffer(typedArray, allocateForWrite);
1882
+ }
1883
+ }, {
1884
+ pack(c1, allocateForWrite) { // specific 0xC1 object
1885
+ let { target, position} = allocateForWrite(1);
1886
+ target[position] = 0xc1;
1887
+ }
1888
+ }];
1889
+
1890
+ function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
1891
+ let length = typedArray.byteLength;
1892
+ if (length + 1 < 0x100) {
1893
+ var { target, position } = allocateForWrite(4 + length);
1894
+ target[position++] = 0xc7;
1895
+ target[position++] = length + 1;
1896
+ } else if (length + 1 < 0x10000) {
1897
+ var { target, position } = allocateForWrite(5 + length);
1898
+ target[position++] = 0xc8;
1899
+ target[position++] = (length + 1) >> 8;
1900
+ target[position++] = (length + 1) & 0xff;
1901
+ } else {
1902
+ var { target, position, targetView } = allocateForWrite(7 + length);
1903
+ target[position++] = 0xc9;
1904
+ targetView.setUint32(position, length + 1); // plus one for the type byte
1905
+ position += 4;
1906
+ }
1907
+ target[position++] = 0x74; // "t" for typed array
1908
+ target[position++] = type;
1909
+ target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position);
1910
+ }
1911
+ function writeBuffer(buffer, allocateForWrite) {
1912
+ let length = buffer.byteLength;
1913
+ var target, position;
1914
+ if (length < 0x100) {
1915
+ var { target, position } = allocateForWrite(length + 2);
1916
+ target[position++] = 0xc4;
1917
+ target[position++] = length;
1918
+ } else if (length < 0x10000) {
1919
+ var { target, position } = allocateForWrite(length + 3);
1920
+ target[position++] = 0xc5;
1921
+ target[position++] = length >> 8;
1922
+ target[position++] = length & 0xff;
1923
+ } else {
1924
+ var { target, position, targetView } = allocateForWrite(length + 5);
1925
+ target[position++] = 0xc6;
1926
+ targetView.setUint32(position, length);
1927
+ position += 4;
1928
+ }
1929
+ target.set(buffer, position);
1930
+ }
1931
+
1932
+ function writeExtensionData(result, target, position, type) {
1933
+ let length = result.length;
1934
+ switch (length) {
1935
+ case 1:
1936
+ target[position++] = 0xd4;
1937
+ break
1938
+ case 2:
1939
+ target[position++] = 0xd5;
1940
+ break
1941
+ case 4:
1942
+ target[position++] = 0xd6;
1943
+ break
1944
+ case 8:
1945
+ target[position++] = 0xd7;
1946
+ break
1947
+ case 16:
1948
+ target[position++] = 0xd8;
1949
+ break
1950
+ default:
1951
+ if (length < 0x100) {
1952
+ target[position++] = 0xc7;
1953
+ target[position++] = length;
1954
+ } else if (length < 0x10000) {
1955
+ target[position++] = 0xc8;
1956
+ target[position++] = length >> 8;
1957
+ target[position++] = length & 0xff;
1958
+ } else {
1959
+ target[position++] = 0xc9;
1960
+ target[position++] = length >> 24;
1961
+ target[position++] = (length >> 16) & 0xff;
1962
+ target[position++] = (length >> 8) & 0xff;
1963
+ target[position++] = length & 0xff;
1964
+ }
1965
+ }
1966
+ target[position++] = type;
1967
+ target.set(result, position);
1968
+ position += length;
1969
+ return position
1970
+ }
1971
+
1972
+ function insertIds(serialized, idsToInsert) {
1973
+ // insert the ids that need to be referenced for structured clones
1974
+ let nextId;
1975
+ let distanceToMove = idsToInsert.length * 6;
1976
+ let lastEnd = serialized.length - distanceToMove;
1977
+ idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1978
+ while (nextId = idsToInsert.pop()) {
1979
+ let offset = nextId.offset;
1980
+ let id = nextId.id;
1981
+ serialized.copyWithin(offset + distanceToMove, offset, lastEnd);
1982
+ distanceToMove -= 6;
1983
+ let position = offset + distanceToMove;
1984
+ serialized[position++] = 0xd6;
1985
+ serialized[position++] = 0x69; // 'i'
1986
+ serialized[position++] = id >> 24;
1987
+ serialized[position++] = (id >> 16) & 0xff;
1988
+ serialized[position++] = (id >> 8) & 0xff;
1989
+ serialized[position++] = id & 0xff;
1990
+ lastEnd = offset;
1991
+ }
1992
+ return serialized
1993
+ }
1994
+
1995
+ function writeBundles(start, pack) {
1996
+ if (bundledStrings$1.length > 0) {
1997
+ targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start);
1998
+ let writeStrings = bundledStrings$1;
1999
+ bundledStrings$1 = null;
2000
+ pack(writeStrings[0]);
2001
+ pack(writeStrings[1]);
2002
+ }
2003
+ }
2004
+
2005
+ function addExtension$1(extension) {
2006
+ if (extension.Class) {
2007
+ if (!extension.pack && !extension.write)
2008
+ throw new Error('Extension has no pack or write function')
2009
+ if (extension.pack && !extension.type)
2010
+ throw new Error('Extension has no type (numeric code to identify the extension)')
2011
+ extensionClasses.unshift(extension.Class);
2012
+ extensions.unshift(extension);
2013
+ }
2014
+ addExtension(extension);
2015
+ }
2016
+ function setWriteStructSlots(func) {
2017
+ writeStructSlots = func;
2018
+ }
2019
+
2020
+ let defaultPackr = new Packr({ useRecords: false });
2021
+ const pack = defaultPackr.pack;
2022
+ const encode = defaultPackr.pack;
2023
+ const Encoder = Packr;
2024
+ const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS;
2025
+ const REUSE_BUFFER_MODE = 512;
1976
2026
  const RESET_BUFFER_MODE = 1024;
1977
2027
 
2028
+ // first four bits
2029
+ const hasNonLatin$1 = /[\u0080-\uFFFF]/;
2030
+ const float32Headers = [false, true, true, false, false, true, true, false];
2031
+ setWriteStructSlots(writeStruct);
2032
+ function writeStruct(object, target, position, structures, makeRoom, pack) {
2033
+ let transition = structures.transitions || false;
2034
+ let start = position;
2035
+ position += 4;
2036
+ let queuedReferences = [];
2037
+ let uint32 = target.uint32 || (target.uint32 = new Uint32Array(target.buffer));
2038
+ let targetView = target.dataView;
2039
+ let encoded;
2040
+ let stringData = '';
2041
+ let safeEnd = target.length - 10;
2042
+ for (let key in object) {
2043
+ let nextTransition = transition[key];
2044
+ if (!nextTransition) {
2045
+ return 0; // bail
2046
+ //nextTransition = transition[key] = Object.create(null)
2047
+ //newTransitions++
2048
+ }
2049
+ if (position > safeEnd) {
2050
+ let newPosition = position - start;
2051
+ target = makeRoom(position);
2052
+ position = newPosition;
2053
+ start = 0;
2054
+ safeEnd = target.length - 10;
2055
+ }
2056
+ transition = nextTransition;
2057
+ let value = object[key];
2058
+ switch (typeof value) {
2059
+ case 'number':
2060
+ if (value >>> 0 === value && value < 0x20000000) {
2061
+ encoded = value;
2062
+ break;
2063
+ } else if (value < 0x100000000 && value >= -0x80000000) {
2064
+ targetView.setFloat32(position, value, true);
2065
+ if (float32Headers[target[position + 3] >>> 5]) {
2066
+ let xShifted;
2067
+ // this checks for rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
2068
+ if (((xShifted = value * mult10[((target[position + 3] & 0x7f) << 1) | (target[position + 2] >> 7)]) >> 0) === xShifted) {
2069
+ position += 4;
2070
+ continue;
2071
+ }
2072
+ }
2073
+ }
2074
+ // fall back to msgpack encoding
2075
+ queuedReferences.push(value, position - start);
2076
+ position += 4;
2077
+ continue;
2078
+ case 'string':
2079
+ if (hasNonLatin$1.test(value)) {
2080
+ queuedReferences.push(value, position - start);
2081
+ position += 4;
2082
+ continue;
2083
+ }
2084
+ if (value.length < 4) { // we can inline really small strings
2085
+ encoded = 0xf8000000 + (value.length << 24) + (value.charCodeAt(0) << 16) + (value.charCodeAt(1) << 8) + (value.charCodeAt(2) || 0);
2086
+ // TODO: determining remaining and make max value be a ratio of that (probably 1/256th)
2087
+ } else if (value.length < 256 && stringData.length < 61440) {
2088
+ // bundle these strings
2089
+ encoded = 0x60000000 | (value.length << 16) | stringData.length;
2090
+ stringData += value;
2091
+ } else { // else queue it
2092
+ queuedReferences.push(value, position - start);
2093
+ position += 4;
2094
+ continue;
2095
+ }
2096
+ break;
2097
+ case 'object':
2098
+ if (value) {
2099
+ queuedReferences.push(value, position - start);
2100
+ position += 4;
2101
+ continue;
2102
+ } else { // null
2103
+ encoded = 0xe0000000;
2104
+ }
2105
+ break;
2106
+ case 'boolean':
2107
+ encoded = value ? 0xe3000000 : 0xe2000000;
2108
+ break;
2109
+ case 'undefined':
2110
+ encoded = 0xe1000000;
2111
+ break;
2112
+ }
2113
+ targetView.setUint32(position, encoded, true);
2114
+ position += 4;
2115
+ }
2116
+ let recordId = transition[RECORD_SYMBOL];
2117
+ if (!(recordId < 1024)) {
2118
+ // for now just punt and go back to writeObject
2119
+ return 0;
2120
+ // newRecord(transition, transition.__keys__ || Object.keys(object), newTransitions, true)
2121
+ }
2122
+ let stringLength = stringData.length;
2123
+ if (stringData) {
2124
+ if (position + stringLength > safeEnd) {
2125
+ target = makeRoom(position + stringLength);
2126
+ }
2127
+ position += target.latin1Write(stringData, position, 0xffffffff);
2128
+ }
2129
+ target[start] = recordId >> 8;
2130
+ target[start + 1] = recordId & 0xff;
2131
+ target[start + 2] = stringLength >> 8;
2132
+ target[start + 3] = stringLength & 0xff;
2133
+ let queued32BitReferences;
2134
+ for (let i = 0, l = queuedReferences.length; i < l;) {
2135
+ let value = queuedReferences[i++];
2136
+ let slotOffset = queuedReferences[i++] + start;
2137
+ let offset = position - slotOffset;
2138
+ if (offset < 0x1f000000) {
2139
+ targetView.setUint32(slotOffset, 0x80000000 | (offset), true);
2140
+ } else {
2141
+ if (!queued32BitReferences)
2142
+ queued32BitReferences = [];
2143
+ queued32BitReferences.push({slotOffset, offset: position - start});
2144
+ }
2145
+ let newPosition = pack(value, position);
2146
+ if (typeof newPosition === 'object') {
2147
+ // re-allocated
2148
+ position = newPosition.position;
2149
+ targetView = newPosition.targetView;
2150
+ start = 0;
2151
+ } else
2152
+ position = newPosition;
2153
+ }
2154
+ if (queued32BitReferences) {
2155
+ // TODO: makeRoom
2156
+ for (let i = 0, l = queued32BitReferences.length; i < l; i++) {
2157
+ let ref = queued32BitReferences[i];
2158
+ targetView.setUint32(ref.slotOffset, 0xa0000000 - ((l - i) << 2), true);
2159
+ targetView.setUint32(position, ref.offset, true);
2160
+ position += 4;
2161
+ }
2162
+ }
2163
+
2164
+ return position;
2165
+ }
2166
+ var sourceSymbol = Symbol('source');
2167
+ function readStruct$1(src, position, srcEnd, structure, unpackr) {
2168
+ var stringLength = (src[position++] << 8) | src[position++];
2169
+ var construct = structure.construct;
2170
+ var srcString;
2171
+ if (!construct) {
2172
+ construct = structure.construct = function() {
2173
+ };
2174
+ var prototype = construct.prototype;
2175
+ Object.defineProperty(prototype, 'toJSON', {
2176
+ get() {
2177
+ // return an enumerable object with own properties to JSON stringify
2178
+ let resolved = {};
2179
+ for (let i = 0, l = structure.length; i < l; i++) {
2180
+ let key = structure[i];
2181
+ resolved[key] = this[key];
2182
+ }
2183
+ return resolved;
2184
+ },
2185
+ // not enumerable or anything
2186
+ });
2187
+ for (let i = 0, l = structure.length; i < l; i++) {
2188
+ let key = structure[i];
2189
+ Object.defineProperty(prototype, key, {
2190
+ get() {
2191
+ let source = this[sourceSymbol];
2192
+ let src = source.src;
2193
+ //let uint32 = src.uint32 || (src.uint32 = new Uint32Array(src.buffer, src.byteOffset, src.byteLength));
2194
+ let dataView = src.dataView || (src.dataView = new DataView(src.buffer, src.byteOffset, src.byteLength));
2195
+ let position = source.position + (i << 2);
2196
+ let value = dataView.getUint32(position, true);
2197
+ let start;
2198
+ switch (value >>> 29) {
2199
+ case 0:
2200
+ return value;
2201
+ case 3:
2202
+ if (value & 0x10000000) {
2203
+ start = (value & 0xffff) + position;
2204
+ return src.toString('utf8', start, start + ((value >> 16) & 0x7ff));
2205
+ } else {
2206
+ if (!srcString) {
2207
+ start = source.position + (l << 2);
2208
+ srcString = src.toString('latin1', start, start + stringLength);
2209
+ }
2210
+ start = value & 0xffff;
2211
+ return srcString.slice(start, start + ((value >> 16) & 0x7ff));
2212
+ }
2213
+ case 4:
2214
+ start = (0x1fffffff & value) + position;
2215
+ let end = srcEnd;
2216
+ for (let next = i + 1; next < l; next++) {
2217
+ position = source.position + (next << 2);
2218
+ let nextValue = dataView.getUint32(position, true); if ((nextValue & 0xe0000000) == -0x80000000) {
2219
+ end = (0x1fffffff & nextValue) + position;
2220
+ break;
2221
+ }
2222
+ }
2223
+ return unpackr.unpack(src.slice(start, end));
2224
+ case 1: case 2: case 5: case 6:
2225
+ let fValue = dataView.getFloat32(position, true);
2226
+ // this does rounding of numbers that were encoded in 32-bit float to nearest significant decimal digit that could be preserved
2227
+ let multiplier = mult10[((src[position + 3] & 0x7f) << 1) | (src[position + 2] >> 7)];
2228
+ return ((multiplier * fValue + (fValue > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
2229
+ case 7:
2230
+ switch((value >> 24) & 0x1f) {
2231
+ case 0: return null;
2232
+ case 1: return undefined;
2233
+ case 2: return false;
2234
+ case 3: return true;
2235
+ case 8: return dataView.getFloat64(position + (value & 0x3ffffff), true);
2236
+ case 0x18: return '';
2237
+ case 0x19: return String.fromCharCode((value >> 16) & 0xff);
2238
+ case 0x20: return String.fromCharCode((value >> 16) & 0xff, (value >> 8) & 0xff);
2239
+ case 0x21: return String.fromCharCode((value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff);
2240
+ default: throw new Error('Unknown constant');
2241
+ }
2242
+ }
2243
+ },
2244
+ enumerable: true,
2245
+ });
2246
+ }
2247
+ }
2248
+ var instance = new construct();
2249
+ instance[sourceSymbol] = {
2250
+ src,
2251
+ uint32: src.uint32,
2252
+ position,
2253
+ };
2254
+ return instance;
2255
+ }
2256
+ setReadStruct(readStruct$1);
2257
+
1978
2258
  class PackrStream extends stream.Transform {
1979
2259
  constructor(options) {
1980
2260
  if (!options)
@@ -2113,23 +2393,23 @@ function unpackIter (bufferIterator, options = {}) {
2113
2393
  const decodeIter = unpackIter;
2114
2394
  const encodeIter = packIter;
2115
2395
 
2116
- const useRecords = false;
2117
- const mapsAsObjects = true;
2118
-
2119
- const nativeAccelerationDisabled = process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
2120
-
2121
- if (!nativeAccelerationDisabled) {
2122
- let extractor;
2123
- try {
2124
- if (typeof require == 'function')
2125
- extractor = require('msgpackr-extract');
2126
- else
2127
- extractor = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('node.cjs', document.baseURI).href)))('msgpackr-extract');
2128
- if (extractor)
2129
- setExtractor(extractor.extractStrings);
2130
- } catch (error) {
2131
- // native module is optional
2132
- }
2396
+ const useRecords = false;
2397
+ const mapsAsObjects = true;
2398
+
2399
+ const nativeAccelerationDisabled = process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
2400
+
2401
+ if (!nativeAccelerationDisabled) {
2402
+ let extractor;
2403
+ try {
2404
+ if (typeof require == 'function')
2405
+ extractor = require('msgpackr-extract');
2406
+ else
2407
+ extractor = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('node.cjs', document.baseURI).href)))('msgpackr-extract');
2408
+ if (extractor)
2409
+ setExtractor(extractor.extractStrings);
2410
+ } catch (error) {
2411
+ // native module is optional
2412
+ }
2133
2413
  }
2134
2414
 
2135
2415
  exports.ALWAYS = ALWAYS;