msgpackr 1.6.1 → 1.6.3

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