@andrew_l/tl-pack 0.3.22 → 0.4.0

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.
@@ -1,1663 +0,0 @@
1
- 'use strict';
2
-
3
- const pako = require('pako');
4
- const toolkit = require('@andrew_l/toolkit');
5
-
6
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
7
-
8
- const pako__default = /*#__PURE__*/_interopDefaultCompat(pako);
9
-
10
- var CORE_TYPES = /* @__PURE__ */ ((CORE_TYPES2) => {
11
- CORE_TYPES2[CORE_TYPES2["None"] = 0] = "None";
12
- CORE_TYPES2[CORE_TYPES2["Binary"] = 1] = "Binary";
13
- CORE_TYPES2[CORE_TYPES2["BoolFalse"] = 2] = "BoolFalse";
14
- CORE_TYPES2[CORE_TYPES2["BoolTrue"] = 3] = "BoolTrue";
15
- CORE_TYPES2[CORE_TYPES2["Null"] = 4] = "Null";
16
- CORE_TYPES2[CORE_TYPES2["Date"] = 5] = "Date";
17
- CORE_TYPES2[CORE_TYPES2["Vector"] = 6] = "Vector";
18
- CORE_TYPES2[CORE_TYPES2["VectorDynamic"] = 7] = "VectorDynamic";
19
- CORE_TYPES2[CORE_TYPES2["Int64"] = 22] = "Int64";
20
- CORE_TYPES2[CORE_TYPES2["Int32"] = 8] = "Int32";
21
- CORE_TYPES2[CORE_TYPES2["Int16"] = 9] = "Int16";
22
- CORE_TYPES2[CORE_TYPES2["Int8"] = 10] = "Int8";
23
- CORE_TYPES2[CORE_TYPES2["UInt64"] = 23] = "UInt64";
24
- CORE_TYPES2[CORE_TYPES2["UInt32"] = 11] = "UInt32";
25
- CORE_TYPES2[CORE_TYPES2["UInt16"] = 12] = "UInt16";
26
- CORE_TYPES2[CORE_TYPES2["UInt8"] = 13] = "UInt8";
27
- CORE_TYPES2[CORE_TYPES2["Float"] = 14] = "Float";
28
- CORE_TYPES2[CORE_TYPES2["Double"] = 15] = "Double";
29
- CORE_TYPES2[CORE_TYPES2["Map"] = 16] = "Map";
30
- CORE_TYPES2[CORE_TYPES2["DictValue"] = 17] = "DictValue";
31
- CORE_TYPES2[CORE_TYPES2["DictIndex"] = 18] = "DictIndex";
32
- CORE_TYPES2[CORE_TYPES2["String"] = 19] = "String";
33
- CORE_TYPES2[CORE_TYPES2["Repeat"] = 20] = "Repeat";
34
- CORE_TYPES2[CORE_TYPES2["Checksum"] = 21] = "Checksum";
35
- CORE_TYPES2[CORE_TYPES2["GZIP"] = 25] = "GZIP";
36
- CORE_TYPES2[CORE_TYPES2["Structure"] = 26] = "Structure";
37
- return CORE_TYPES2;
38
- })(CORE_TYPES || {});
39
- const MAX_BUFFER_SIZE = 2144337920;
40
-
41
- function createDictionary(values) {
42
- return new Dictionary(values);
43
- }
44
- class Dictionary {
45
- _count;
46
- _wordToIndex;
47
- _words;
48
- _offset;
49
- constructor(values, offset = 0) {
50
- this._count = 0;
51
- this._words = [];
52
- this._wordToIndex = /* @__PURE__ */ new Map();
53
- this._offset = offset;
54
- if (Array.isArray(values) && values.length) {
55
- values.forEach((word) => {
56
- if (this._wordToIndex.has(word)) return;
57
- this._wordToIndex.set(word, this._count++);
58
- this._words.push(word);
59
- });
60
- }
61
- }
62
- clear() {
63
- this._count = 0;
64
- this._words.length = 0;
65
- this._wordToIndex.clear();
66
- }
67
- get size() {
68
- return this._count;
69
- }
70
- /**
71
- * Returns inserted index or nothing
72
- */
73
- maybeInsert(word) {
74
- if (this._wordToIndex.has(word)) return null;
75
- this._wordToIndex.set(word, this._count++);
76
- this._words.push(word);
77
- return this._count + this._offset;
78
- }
79
- getValue(index) {
80
- return this._words[index - this._offset] ?? null;
81
- }
82
- getIndex(value) {
83
- const idx = this._wordToIndex.get(value);
84
- if (idx === void 0) {
85
- return null;
86
- }
87
- return idx + this._offset;
88
- }
89
- hasValue(value) {
90
- return this._wordToIndex.has(value);
91
- }
92
- hasIndex(index) {
93
- return this._words[index - this._offset] !== void 0;
94
- }
95
- }
96
-
97
- const noop = Symbol();
98
- const NO_CONSTRUCTOR = /* @__PURE__ */ new Set([
99
- CORE_TYPES.BoolFalse,
100
- CORE_TYPES.BoolTrue,
101
- CORE_TYPES.Null
102
- ]);
103
- const SUPPORT_COMPRESSION = /* @__PURE__ */ new Set([CORE_TYPES.String]);
104
- const NOOP_DICTIONARY$1 = new Dictionary();
105
- class BinaryWriter {
106
- withGzip;
107
- target;
108
- dictionary;
109
- dictionaryExtended;
110
- extensions;
111
- structures;
112
- _last;
113
- offsetChecksum;
114
- _repeat;
115
- offset;
116
- constructor({
117
- gzip = false,
118
- dictionary = NOOP_DICTIONARY$1,
119
- extensions,
120
- structures
121
- } = {}) {
122
- this.offset = 0;
123
- this.offsetChecksum = 0;
124
- this.extensions = /* @__PURE__ */ new Map();
125
- this.structures = /* @__PURE__ */ new Map();
126
- this.withGzip = gzip;
127
- this.target = byteArrayAllocate(8192);
128
- this._last = noop;
129
- if (extensions) {
130
- for (const ext of extensions) {
131
- this.extensions.set(ext.token, ext);
132
- }
133
- }
134
- if (structures) {
135
- for (const struct of structures) {
136
- this.structures.set(struct.extension.token, struct);
137
- }
138
- }
139
- if (Array.isArray(dictionary)) {
140
- this.dictionary = new Dictionary(dictionary);
141
- } else {
142
- this.dictionary = dictionary;
143
- }
144
- this.dictionaryExtended = new Dictionary(void 0, this.dictionary.size);
145
- }
146
- /**
147
- * Reset internal state
148
- */
149
- reset() {
150
- this.offset = 0;
151
- this.offsetChecksum = 0;
152
- this._last = noop;
153
- this._repeat = void 0;
154
- this.dictionaryExtended.clear();
155
- return this;
156
- }
157
- allocate(size) {
158
- const position = this.offset + size;
159
- if (this.safeEnd < position) {
160
- this.makeRoom(position);
161
- }
162
- return this;
163
- }
164
- makeRoom(end) {
165
- let start = 0;
166
- let newSize = 0;
167
- let target = this.target;
168
- if (end > 16777216) {
169
- if (end - start > MAX_BUFFER_SIZE)
170
- throw new Error(
171
- "Packed buffer would be larger than maximum buffer size"
172
- );
173
- newSize = Math.min(
174
- MAX_BUFFER_SIZE,
175
- Math.round(
176
- Math.max((end - start) * (end > 67108864 ? 1.25 : 2), 4194304) / 4096
177
- ) * 4096
178
- );
179
- } else {
180
- newSize = (Math.max(end - start << 2, target.length - 1) >> 12) + 1 << 12;
181
- }
182
- const newBuffer = byteArrayAllocate(newSize);
183
- end = Math.min(end, target.length);
184
- newBuffer.set(target.slice(start, end));
185
- this.target = newBuffer;
186
- }
187
- get safeEnd() {
188
- return this.target.length - 10;
189
- }
190
- getBuffer() {
191
- return this.target.subarray(0, this.offset);
192
- }
193
- writeByte(value) {
194
- this.allocate(1);
195
- this.target[this.offset++] = value;
196
- return this;
197
- }
198
- writeBool(value) {
199
- if (value) {
200
- this.writeByte(CORE_TYPES.BoolTrue);
201
- } else {
202
- this.writeByte(CORE_TYPES.BoolFalse);
203
- }
204
- return this;
205
- }
206
- writeNull() {
207
- this.writeByte(CORE_TYPES.Null);
208
- return this;
209
- }
210
- writeInt64(value, signed = true) {
211
- this.allocate(8);
212
- if (typeof value === "number") value = BigInt(value);
213
- const low32 = Number(value & 0xffffffffn);
214
- const high32 = Number(value >> 32n);
215
- this.writeInt32(low32, signed);
216
- this.writeInt32(high32, signed);
217
- return this;
218
- }
219
- writeInt32(value, signed = true) {
220
- this.allocate(4);
221
- if (signed) {
222
- this.target[this.offset++] = value;
223
- this.target[this.offset++] = value >> 8;
224
- this.target[this.offset++] = value >> 16;
225
- this.target[this.offset++] = value >> 24;
226
- } else {
227
- this.target[this.offset++] = value;
228
- this.target[this.offset++] = value >> 8;
229
- this.target[this.offset++] = value >> 16;
230
- this.target[this.offset++] = value >> 24;
231
- }
232
- return this;
233
- }
234
- writeInt16(value, signed = true) {
235
- this.allocate(2);
236
- if (signed) {
237
- this.target[this.offset++] = value;
238
- this.target[this.offset++] = value >> 8;
239
- } else {
240
- this.target[this.offset++] = value;
241
- this.target[this.offset++] = value >> 8;
242
- }
243
- return this;
244
- }
245
- writeInt8(value, signed = true) {
246
- this.allocate(1);
247
- this.target[this.offset++] = value;
248
- return this;
249
- }
250
- writeFloat(value) {
251
- this.allocate(4);
252
- float32[0] = value;
253
- this.writeInt32(int32[0]);
254
- return this;
255
- }
256
- writeDouble(value) {
257
- this.allocate(8);
258
- float64[0] = value;
259
- this.writeInt32(int32[0], false);
260
- this.writeInt32(int32[1], false);
261
- return this;
262
- }
263
- writeDate(value) {
264
- let timestamp = 0;
265
- if (value instanceof Date) {
266
- timestamp = value.getTime();
267
- } else if (typeof value === "number") {
268
- timestamp = value;
269
- }
270
- this.writeDouble(timestamp);
271
- return this;
272
- }
273
- writeString(value) {
274
- const strLength = value.length;
275
- let start = this.offset;
276
- let require = strLength << 2;
277
- if (require < 254) {
278
- require += 1;
279
- this.offset += 1;
280
- } else {
281
- require += 4;
282
- this.offset += 4;
283
- }
284
- this.allocate(require);
285
- const bytes = utf8Write(this.target, value, this.offset);
286
- if (require < 254) {
287
- this.target[start++] = bytes;
288
- } else {
289
- this.target[start++] = 254;
290
- this.target[start++] = bytes % 256;
291
- this.target[start++] = (bytes >> 8) % 256;
292
- this.target[start++] = (bytes >> 16) % 256;
293
- }
294
- this.offset += bytes;
295
- return this;
296
- }
297
- writeChecksum(withConstructor = true) {
298
- const bytes = this.target.subarray(this.offsetChecksum, this.offset);
299
- let sum = 0;
300
- for (const val of bytes) {
301
- sum += val;
302
- }
303
- if (withConstructor) {
304
- this.writeByte(CORE_TYPES.Checksum);
305
- }
306
- this.writeInt32(sum);
307
- this.offsetChecksum = this.offset;
308
- return this;
309
- }
310
- writeBytes(value) {
311
- const length = value.length;
312
- this.writeLength(length);
313
- this.allocate(length);
314
- this.target.set(value, this.offset);
315
- this.offset += length;
316
- return this;
317
- }
318
- writeLength(value) {
319
- if (value < 254) {
320
- this.allocate(1);
321
- this.target[this.offset++] = value;
322
- } else {
323
- this.allocate(4);
324
- this.target[this.offset++] = 254;
325
- this.target[this.offset++] = value % 256;
326
- this.target[this.offset++] = (value >> 8) % 256;
327
- this.target[this.offset++] = (value >> 16) % 256;
328
- }
329
- return this;
330
- }
331
- writeVector(value) {
332
- const length = value.length;
333
- this.writeLength(length);
334
- for (let i = 0; i < length; i++) {
335
- if (value[i] === void 0) {
336
- this.writeNull();
337
- } else {
338
- this.writeObject(value[i]);
339
- }
340
- }
341
- return this;
342
- }
343
- writeMap(object) {
344
- for (const key in object) {
345
- if (object[key] === void 0) continue;
346
- this._last = noop;
347
- this.wireDictionary(key);
348
- this.writeObject(object[key]);
349
- }
350
- this.writeByte(CORE_TYPES.None);
351
- return this;
352
- }
353
- wireDictionary(value) {
354
- let idx = null;
355
- idx = this.dictionary.getIndex(value);
356
- if (idx === null) {
357
- idx = this.dictionaryExtended.getIndex(value);
358
- }
359
- if (idx === null) {
360
- this.dictionaryExtended.maybeInsert(value);
361
- this.writeCore(CORE_TYPES.DictValue, value);
362
- } else {
363
- this.writeCore(CORE_TYPES.DictIndex, idx);
364
- }
365
- return this;
366
- }
367
- writeStructure(value) {
368
- const ctor = value.constructor;
369
- this.writeInt32(ctor.extension.token, false);
370
- ctor.extension.encode.call(this, value.value);
371
- return this;
372
- }
373
- writeGzip(value) {
374
- const compressed = pako__default.deflateRaw(value, { level: 9 });
375
- this.writeBytes(compressed);
376
- return this;
377
- }
378
- encode(value) {
379
- this.offset = 0;
380
- this.offsetChecksum = 0;
381
- this._last = noop;
382
- this._repeat = void 0;
383
- this.target = byteArrayAllocate(256);
384
- this.writeObject(value);
385
- return this.getBuffer();
386
- }
387
- startDynamicVector() {
388
- this.writeByte(CORE_TYPES.VectorDynamic);
389
- return this;
390
- }
391
- endDynamicVector() {
392
- this.writeByte(CORE_TYPES.None);
393
- return this;
394
- }
395
- _writeCustom(value) {
396
- const start = this.offset;
397
- this.allocate(1);
398
- this.offset++;
399
- let edgeExt;
400
- for (const ext of this.extensions.values()) {
401
- if (ext.token === -1) {
402
- edgeExt = ext;
403
- continue;
404
- }
405
- ext.encode.call(this, value);
406
- const processed = start + 1 < this.offset;
407
- if (processed) {
408
- const end = this.offset;
409
- this.offset = start;
410
- this.writeByte(ext.token);
411
- this.offset = end;
412
- return true;
413
- }
414
- }
415
- this.offset = start;
416
- if (edgeExt) {
417
- edgeExt.encode.call(this, value);
418
- return start < this.offset;
419
- }
420
- return false;
421
- }
422
- writeObject(value) {
423
- if (value === void 0) return this;
424
- const constructorId = coreType(value);
425
- if (constructorId === CORE_TYPES.None) {
426
- if (this._writeCustom(value)) {
427
- return this;
428
- }
429
- throw new TypeError(`Invalid core type of ${value}`);
430
- }
431
- if (this._last === value) {
432
- this.writeRepeat();
433
- } else {
434
- this._last = value;
435
- this._repeat = void 0;
436
- this.writeCore(constructorId, value);
437
- }
438
- return this;
439
- }
440
- writeObjectGzip(value) {
441
- const writer = new BinaryWriter();
442
- writer.extensions = this.extensions;
443
- writer.dictionary = this.dictionary;
444
- writer.structures = this.structures;
445
- writer.dictionaryExtended = this.dictionaryExtended;
446
- writer.writeObject(value);
447
- this.writeCore(CORE_TYPES.GZIP, writer.getBuffer());
448
- return this;
449
- }
450
- writeCore(constructorId, value) {
451
- if (this.withGzip && SUPPORT_COMPRESSION.has(constructorId)) {
452
- this.writeObjectGzip(value);
453
- return this;
454
- } else if (!NO_CONSTRUCTOR.has(constructorId)) {
455
- this.writeByte(constructorId);
456
- }
457
- switch (constructorId) {
458
- case CORE_TYPES.Structure: {
459
- return this.writeStructure(value);
460
- }
461
- case CORE_TYPES.Binary: {
462
- return this.writeBytes(value);
463
- }
464
- case CORE_TYPES.GZIP: {
465
- return this.writeGzip(value);
466
- }
467
- case CORE_TYPES.DictIndex: {
468
- return this.writeLength(value);
469
- }
470
- case CORE_TYPES.DictValue: {
471
- return this.writeString(value);
472
- }
473
- case CORE_TYPES.BoolFalse: {
474
- return this.writeBool(value);
475
- }
476
- case CORE_TYPES.BoolTrue: {
477
- return this.writeBool(value);
478
- }
479
- case CORE_TYPES.Date: {
480
- return this.writeDate(value);
481
- }
482
- case CORE_TYPES.Int64: {
483
- return this.writeInt64(value);
484
- }
485
- case CORE_TYPES.Int32: {
486
- return this.writeInt32(value);
487
- }
488
- case CORE_TYPES.Int16: {
489
- return this.writeInt16(value);
490
- }
491
- case CORE_TYPES.Int8: {
492
- return this.writeInt8(value);
493
- }
494
- case CORE_TYPES.UInt64: {
495
- return this.writeInt64(value, false);
496
- }
497
- case CORE_TYPES.UInt32: {
498
- return this.writeInt32(value, false);
499
- }
500
- case CORE_TYPES.UInt16: {
501
- return this.writeInt16(value, false);
502
- }
503
- case CORE_TYPES.UInt8: {
504
- return this.writeInt8(value, false);
505
- }
506
- case CORE_TYPES.Double: {
507
- return this.writeDouble(value);
508
- }
509
- case CORE_TYPES.Float: {
510
- return this.writeFloat(value);
511
- }
512
- case CORE_TYPES.Null: {
513
- return this.writeNull();
514
- }
515
- case CORE_TYPES.String: {
516
- if (value.length <= 16) {
517
- this.offset--;
518
- return this.wireDictionary(value);
519
- }
520
- return this.writeString(value);
521
- }
522
- case CORE_TYPES.Vector: {
523
- return this.writeVector(value);
524
- }
525
- case CORE_TYPES.Map: {
526
- return this.writeMap(value);
527
- }
528
- }
529
- return this;
530
- }
531
- writeRepeat() {
532
- if (!this._repeat) {
533
- this.writeByte(CORE_TYPES.Repeat);
534
- this._repeat = { count: 0, offset: this.offset };
535
- }
536
- this.offset = this._repeat.offset;
537
- this._repeat.count++;
538
- this.writeLength(this._repeat.count);
539
- return this;
540
- }
541
- }
542
-
543
- const CONSTRUCTOR_OPTIONAL = 1644261036;
544
- const CONSTRUCTOR_OPTIONAL_NULL = 570519212;
545
- const TYPE_HANDLERS = {
546
- ["unknown"]: {
547
- encode: function(value, key) {
548
- this.writeObject(value[key]);
549
- },
550
- decode: function(result, key) {
551
- result[key] = this.readObject();
552
- },
553
- estimatedSizeBytes: 0
554
- },
555
- [CORE_TYPES.Map]: {
556
- encode: function(value, key) {
557
- this.writeMap(value[key]);
558
- },
559
- decode: function(result, key) {
560
- result[key] = this.readMap(false);
561
- },
562
- estimatedSizeBytes: 0
563
- },
564
- [CORE_TYPES.Binary]: {
565
- encode: function(value, key) {
566
- this.writeBytes(value[key]);
567
- },
568
- decode: function(result, key) {
569
- result[key] = this.readBytes();
570
- },
571
- estimatedSizeBytes: 0
572
- },
573
- [CORE_TYPES.Vector]: {
574
- encode: function(value, key) {
575
- this.writeVector(value[key]);
576
- },
577
- decode: function(result, key) {
578
- result[key] = this.readVector(false);
579
- },
580
- estimatedSizeBytes: 0
581
- },
582
- [Boolean.name]: {
583
- encode: function(value, key) {
584
- this.writeBool(value[key]);
585
- },
586
- decode: function(result, key) {
587
- result[key] = this.readBool();
588
- },
589
- estimatedSizeBytes: 1
590
- },
591
- [CORE_TYPES.Int8]: {
592
- encode: function(value, key) {
593
- this.writeInt8(value[key], true);
594
- },
595
- decode: function(result, key) {
596
- result[key] = this.readInt8(true);
597
- },
598
- estimatedSizeBytes: 1
599
- },
600
- [CORE_TYPES.Int16]: {
601
- encode: function(value, key) {
602
- this.writeInt16(value[key], true);
603
- },
604
- decode: function(result, key) {
605
- result[key] = this.readInt16(true);
606
- },
607
- estimatedSizeBytes: 2
608
- },
609
- [CORE_TYPES.Int32]: {
610
- encode: function(value, key) {
611
- this.writeInt32(value[key], true);
612
- },
613
- decode: function(result, key) {
614
- result[key] = this.readInt32(true);
615
- },
616
- estimatedSizeBytes: 4
617
- },
618
- [CORE_TYPES.Int64]: {
619
- encode: function(value, key) {
620
- this.writeInt64(value[key], true);
621
- },
622
- decode: function(result, key) {
623
- result[key] = this.readInt64(true);
624
- },
625
- estimatedSizeBytes: 8
626
- },
627
- [CORE_TYPES.UInt8]: {
628
- encode: function(value, key) {
629
- this.writeInt8(value[key], false);
630
- },
631
- decode: function(result, key) {
632
- result[key] = this.readInt8(false);
633
- },
634
- estimatedSizeBytes: 1
635
- },
636
- [CORE_TYPES.UInt16]: {
637
- encode: function(value, key) {
638
- this.writeInt16(value[key], false);
639
- },
640
- decode: function(result, key) {
641
- result[key] = this.readInt16(false);
642
- },
643
- estimatedSizeBytes: 2
644
- },
645
- [CORE_TYPES.UInt32]: {
646
- encode: function(value, key) {
647
- this.writeInt32(value[key], false);
648
- },
649
- decode: function(result, key) {
650
- result[key] = this.readInt32(false);
651
- },
652
- estimatedSizeBytes: 4
653
- },
654
- [CORE_TYPES.UInt64]: {
655
- encode: function(value, key) {
656
- this.writeInt64(value[key], false);
657
- },
658
- decode: function(result, key) {
659
- result[key] = this.readInt64(false);
660
- },
661
- estimatedSizeBytes: 8
662
- },
663
- [CORE_TYPES.Double]: {
664
- encode: function(value, key) {
665
- this.writeDouble(value[key]);
666
- },
667
- decode: function(result, key) {
668
- result[key] = this.readDouble();
669
- },
670
- estimatedSizeBytes: 8
671
- },
672
- [CORE_TYPES.Date]: {
673
- encode: function(value, key) {
674
- this.writeDate(value[key]);
675
- },
676
- decode: function(result, key) {
677
- result[key] = this.readDate();
678
- },
679
- estimatedSizeBytes: 8
680
- },
681
- [CORE_TYPES.String]: {
682
- encode: function(value, key) {
683
- this.writeString(value[key]);
684
- },
685
- decode: function(result, key) {
686
- result[key] = this.readString();
687
- },
688
- estimatedSizeBytes: 0
689
- }
690
- };
691
- TYPE_HANDLERS[Number.name] = TYPE_HANDLERS[CORE_TYPES.Double];
692
- TYPE_HANDLERS[String.name] = TYPE_HANDLERS[CORE_TYPES.String];
693
- TYPE_HANDLERS[Object.name] = TYPE_HANDLERS[CORE_TYPES.Map];
694
- TYPE_HANDLERS[Uint8Array.name] = TYPE_HANDLERS[CORE_TYPES.Binary];
695
- TYPE_HANDLERS[Array.name] = TYPE_HANDLERS[CORE_TYPES.Vector];
696
- TYPE_HANDLERS[Date.name] = TYPE_HANDLERS[CORE_TYPES.Date];
697
- function compileStructure(name, version, properties, checksum) {
698
- const encodeFns = [];
699
- const decodeFns = [];
700
- const structures = [];
701
- const structureId = toolkit.crc32(name) >>> 0;
702
- let estimatedSizeBytes = 1;
703
- encodeFns.push(function() {
704
- this.writeByte(version);
705
- });
706
- decodeFns.push(function() {
707
- const ver = this.readByte();
708
- toolkit.assert.ok(
709
- version === ver,
710
- `Structure ${structureId} version mismatch: expected ${version}, got ${ver}`
711
- );
712
- });
713
- const entries = Object.entries(properties);
714
- const entriesLength = entries.length;
715
- for (let i = 0; i < entriesLength; i++) {
716
- const [key, prop] = entries[i];
717
- const isRequired = prop.required === true;
718
- const isArray = Array.isArray(prop.type);
719
- const propType = Array.isArray(prop.type) ? prop.type[0] : prop.type;
720
- if (isStructureType(propType)) {
721
- encodeFns.push(
722
- createStructureEncoder(key, propType, isRequired, isArray)
723
- );
724
- decodeFns.push(createStructureDecoder(key, isRequired, isArray));
725
- estimatedSizeBytes += propType.estimatedSizeBytes;
726
- structures.push(propType);
727
- continue;
728
- }
729
- const typeName = propType?.name || (propType === null ? "unknown" : propType);
730
- const handler = TYPE_HANDLERS[typeName];
731
- if (handler) {
732
- encodeFns.push(
733
- createBoundEncoder(handler.encode, key, isRequired, isArray)
734
- );
735
- decodeFns.push(
736
- createBoundDecoder(handler.decode, key, isRequired, isArray)
737
- );
738
- estimatedSizeBytes += handler.estimatedSizeBytes;
739
- } else {
740
- throw new Error(`Unsupported property type: ${typeName || "unknown"}`);
741
- }
742
- }
743
- if (checksum) {
744
- estimatedSizeBytes += 4;
745
- encodeFns.push(function() {
746
- this.writeChecksum(false);
747
- });
748
- decodeFns.push(function() {
749
- this.readChecksum(false);
750
- });
751
- }
752
- const compiled = {
753
- id: structureId,
754
- encodeFns,
755
- decodeFns,
756
- structures,
757
- estimatedSizeBytes
758
- };
759
- return compiled;
760
- }
761
- function isStructureType(type) {
762
- return type?.prototype && Structure.prototype.isPrototypeOf(type.prototype);
763
- }
764
- function createStructureEncoder(key, StructureCtor, isRequired, isArray) {
765
- return function(value) {
766
- const hasValue = value[key] !== void 0 && value[key] !== null;
767
- if (!hasValue) {
768
- toolkit.assert.ok(!isRequired, `Required property "${key}" is missing or null`);
769
- if (value[key] === null) {
770
- this.writeInt32(CONSTRUCTOR_OPTIONAL_NULL);
771
- } else {
772
- this.writeInt32(CONSTRUCTOR_OPTIONAL);
773
- }
774
- } else if (isArray) {
775
- const arr = value[key];
776
- toolkit.assert.array(arr, `Expected property "${key}" to be array.`);
777
- this.writeLength(arr.length);
778
- for (let idx = 0; idx < arr.length; idx++) {
779
- this.writeStructure(
780
- arr[idx] instanceof Structure ? arr[idx] : new StructureCtor(arr[idx])
781
- );
782
- }
783
- } else {
784
- this.writeStructure(
785
- value[key] instanceof Structure ? value[key] : new StructureCtor(value[key])
786
- );
787
- }
788
- };
789
- }
790
- function createStructureDecoder(key, isRequired, isArray) {
791
- return function(result) {
792
- let shouldRead = true;
793
- if (!isRequired) {
794
- shouldRead = !readMaybeInt32(this, CONSTRUCTOR_OPTIONAL);
795
- if (shouldRead && readMaybeInt32(this, CONSTRUCTOR_OPTIONAL_NULL)) {
796
- result[key] = null;
797
- return;
798
- }
799
- }
800
- if (shouldRead) {
801
- if (isArray) {
802
- const length = this.readLength();
803
- const arrResult = Array.from({ length });
804
- for (let idx = 0; idx < length; idx++) {
805
- arrResult[idx] = this.readStructure(false);
806
- }
807
- result[key] = arrResult;
808
- } else {
809
- result[key] = this.readStructure(false);
810
- }
811
- }
812
- };
813
- }
814
- function readMaybeInt32(reader, expectedValue) {
815
- let result = false;
816
- if (reader.length >= reader.offset + 4) {
817
- result = reader.readInt32() === expectedValue;
818
- if (!result) {
819
- reader.offset -= 4;
820
- }
821
- }
822
- return result;
823
- }
824
- function createBoundEncoder(encodeFn, key, isRequired, isArray) {
825
- return function(value) {
826
- const hasValue = value[key] !== void 0 && value[key] !== null;
827
- if (!hasValue) {
828
- toolkit.assert.ok(!isRequired, `Required property "${key}" is missing or null`);
829
- if (value[key] === null) {
830
- this.writeInt32(CONSTRUCTOR_OPTIONAL_NULL);
831
- } else {
832
- this.writeInt32(CONSTRUCTOR_OPTIONAL);
833
- }
834
- } else if (isArray) {
835
- const arr = value[key];
836
- toolkit.assert.array(arr, `Expected property "${key}" to be array.`);
837
- this.writeLength(arr.length);
838
- for (let idx = 0; idx < arr.length; idx++) {
839
- encodeFn.call(this, arr, idx);
840
- }
841
- } else {
842
- encodeFn.call(this, value, key);
843
- }
844
- };
845
- }
846
- function createBoundDecoder(decodeFn, key, isRequired, isArray) {
847
- return function(result) {
848
- let shouldRead = true;
849
- if (!isRequired) {
850
- shouldRead = !readMaybeInt32(this, CONSTRUCTOR_OPTIONAL);
851
- if (shouldRead && readMaybeInt32(this, CONSTRUCTOR_OPTIONAL_NULL)) {
852
- result[key] = null;
853
- return;
854
- }
855
- }
856
- if (shouldRead) {
857
- if (isArray) {
858
- const length = this.readLength();
859
- const arrResult = Array.from({ length });
860
- for (let idx = 0; idx < length; idx++) {
861
- decodeFn.call(this, arrResult, idx);
862
- }
863
- result[key] = arrResult;
864
- } else {
865
- decodeFn.call(this, result, key);
866
- }
867
- }
868
- };
869
- }
870
- function defineStructure({
871
- name,
872
- properties,
873
- version,
874
- checksum = false
875
- }) {
876
- const compiled = compileStructure(name, version, properties, checksum);
877
- return class DefinedStructure extends Structure {
878
- static estimatedSizeBytes = compiled.estimatedSizeBytes;
879
- static structures = compiled.structures;
880
- static extension = {
881
- token: compiled.id,
882
- encode(value) {
883
- const fns = compiled.encodeFns;
884
- const length = fns.length;
885
- for (let i = 0; i < length; i++) {
886
- fns[i].call(this, value);
887
- }
888
- },
889
- decode() {
890
- const result = {};
891
- const fns = compiled.decodeFns;
892
- const length = fns.length;
893
- for (let i = 0; i < length; i++) {
894
- fns[i].call(this, result);
895
- }
896
- return result;
897
- }
898
- };
899
- };
900
- }
901
- class Structure {
902
- value;
903
- constructor(value) {
904
- this.value = value;
905
- }
906
- toBuffer(options) {
907
- const ctor = this.constructor;
908
- const writer = new BinaryWriter(options);
909
- ctor.extension.encode.call(writer, this.value);
910
- return writer.getBuffer();
911
- }
912
- static fromBuffer(buffer, options = {}) {
913
- const reader = new BinaryReader(buffer, {
914
- ...options,
915
- structures: options.structures ? this.structures.concat(options.structures) : this.structures
916
- });
917
- return this.extension.decode.call(reader);
918
- }
919
- static estimatedSizeBytes = -1;
920
- static structures = [];
921
- static extension = {
922
- token: CORE_TYPES.Binary,
923
- encode: toolkit.noop,
924
- decode: toolkit.noop
925
- };
926
- }
927
-
928
- const fromCharCode = String.fromCharCode;
929
- const int32 = new Int32Array(2);
930
- const float32 = new Float32Array(int32.buffer);
931
- const float64 = new Float64Array(int32.buffer);
932
- function byteArrayAllocate(length) {
933
- return new Uint8Array(length);
934
- }
935
- function coreType(value) {
936
- if (value instanceof Structure) {
937
- return CORE_TYPES.Structure;
938
- } else if (value instanceof Uint8Array) {
939
- return CORE_TYPES.Binary;
940
- }
941
- switch (typeof value) {
942
- case "string": {
943
- return CORE_TYPES.String;
944
- }
945
- case "boolean": {
946
- return value ? CORE_TYPES.BoolTrue : CORE_TYPES.BoolFalse;
947
- }
948
- case "bigint": {
949
- if (value >= 0n && value <= 0xffffffffffffffffn) {
950
- return CORE_TYPES.UInt64;
951
- } else if (value >= -0x8000000000000000n && value <= 0x7fffffffffffffffn) {
952
- return CORE_TYPES.Int64;
953
- }
954
- return CORE_TYPES.None;
955
- }
956
- case "number": {
957
- if (Math.trunc(value) === value) {
958
- if (value >= 0 && value <= 255) {
959
- return CORE_TYPES.UInt8;
960
- } else if (value >= 0 && value <= 65535) {
961
- return CORE_TYPES.UInt16;
962
- } else if (value >= 0 && value <= 4294967295) {
963
- return CORE_TYPES.UInt32;
964
- } else if (value >= -128 && value <= 127) {
965
- return CORE_TYPES.Int8;
966
- } else if (value >= -32768 && value <= 32767) {
967
- return CORE_TYPES.Int16;
968
- } else if (value >= -2147483648 && value <= 2147483647) {
969
- return CORE_TYPES.Int32;
970
- }
971
- }
972
- return CORE_TYPES.Double;
973
- }
974
- case "object": {
975
- if (value === null) return CORE_TYPES.Null;
976
- if (value instanceof Date) {
977
- return CORE_TYPES.Date;
978
- }
979
- if (Array.isArray(value)) {
980
- return CORE_TYPES.Vector;
981
- }
982
- if (toolkit.isPlainObject(value)) {
983
- return CORE_TYPES.Map;
984
- }
985
- }
986
- }
987
- return CORE_TYPES.None;
988
- }
989
- function utf8Read(target, length, offset) {
990
- let result;
991
- if (length < 16) {
992
- if (result = utf8ReadShort(target, length, offset)) return result;
993
- }
994
- if (length > 64)
995
- return toolkit.textDecoder.decode(target.subarray(offset, offset += length));
996
- const end = offset + length;
997
- const units = [];
998
- result = "";
999
- while (offset < end) {
1000
- const byte1 = target[offset++];
1001
- if ((byte1 & 128) === 0) {
1002
- units.push(byte1);
1003
- } else if ((byte1 & 224) === 192) {
1004
- const byte2 = target[offset++] & 63;
1005
- units.push((byte1 & 31) << 6 | byte2);
1006
- } else if ((byte1 & 240) === 224) {
1007
- const byte2 = target[offset++] & 63;
1008
- const byte3 = target[offset++] & 63;
1009
- units.push((byte1 & 31) << 12 | byte2 << 6 | byte3);
1010
- } else if ((byte1 & 248) === 240) {
1011
- const byte2 = target[offset++] & 63;
1012
- const byte3 = target[offset++] & 63;
1013
- const byte4 = target[offset++] & 63;
1014
- let unit = (byte1 & 7) << 18 | byte2 << 12 | byte3 << 6 | byte4;
1015
- if (unit > 65535) {
1016
- unit -= 65536;
1017
- units.push(unit >>> 10 & 1023 | 55296);
1018
- unit = 56320 | unit & 1023;
1019
- }
1020
- units.push(unit);
1021
- } else {
1022
- units.push(byte1);
1023
- }
1024
- if (units.length >= 4096) {
1025
- result += fromCharCode.apply(String, units);
1026
- units.length = 0;
1027
- }
1028
- }
1029
- if (units.length > 0) {
1030
- result += fromCharCode.apply(String, units);
1031
- }
1032
- return result;
1033
- }
1034
- function utf8ReadShort(target, length, offset) {
1035
- if (length < 4) {
1036
- if (length < 2) {
1037
- if (length === 0) return "";
1038
- else {
1039
- let a = target[offset++];
1040
- if ((a & 128) > 1) {
1041
- offset -= 1;
1042
- return;
1043
- }
1044
- return fromCharCode(a);
1045
- }
1046
- } else {
1047
- let a = target[offset++];
1048
- let b = target[offset++];
1049
- if ((a & 128) > 0 || (b & 128) > 0) {
1050
- offset -= 2;
1051
- return;
1052
- }
1053
- if (length < 3) return fromCharCode(a, b);
1054
- let c = target[offset++];
1055
- if ((c & 128) > 0) {
1056
- offset -= 3;
1057
- return;
1058
- }
1059
- return fromCharCode(a, b, c);
1060
- }
1061
- } else {
1062
- let a = target[offset++];
1063
- let b = target[offset++];
1064
- let c = target[offset++];
1065
- let d = target[offset++];
1066
- if ((a & 128) > 0 || (b & 128) > 0 || (c & 128) > 0 || (d & 128) > 0) {
1067
- offset -= 4;
1068
- return;
1069
- }
1070
- if (length < 6) {
1071
- if (length === 4) return fromCharCode(a, b, c, d);
1072
- else {
1073
- let e = target[offset++];
1074
- if ((e & 128) > 0) {
1075
- offset -= 5;
1076
- return;
1077
- }
1078
- return fromCharCode(a, b, c, d, e);
1079
- }
1080
- } else if (length < 8) {
1081
- let e = target[offset++];
1082
- let f = target[offset++];
1083
- if ((e & 128) > 0 || (f & 128) > 0) {
1084
- offset -= 6;
1085
- return;
1086
- }
1087
- if (length < 7) return fromCharCode(a, b, c, d, e, f);
1088
- let g = target[offset++];
1089
- if ((g & 128) > 0) {
1090
- offset -= 7;
1091
- return;
1092
- }
1093
- return fromCharCode(a, b, c, d, e, f, g);
1094
- } else {
1095
- let e = target[offset++];
1096
- let f = target[offset++];
1097
- let g = target[offset++];
1098
- let h = target[offset++];
1099
- if ((e & 128) > 0 || (f & 128) > 0 || (g & 128) > 0 || (h & 128) > 0) {
1100
- offset -= 8;
1101
- return;
1102
- }
1103
- if (length < 10) {
1104
- if (length === 8) return fromCharCode(a, b, c, d, e, f, g, h);
1105
- else {
1106
- let i = target[offset++];
1107
- if ((i & 128) > 0) {
1108
- offset -= 9;
1109
- return;
1110
- }
1111
- return fromCharCode(a, b, c, d, e, f, g, h, i);
1112
- }
1113
- } else if (length < 12) {
1114
- let i = target[offset++];
1115
- let j = target[offset++];
1116
- if ((i & 128) > 0 || (j & 128) > 0) {
1117
- offset -= 10;
1118
- return;
1119
- }
1120
- if (length < 11) return fromCharCode(a, b, c, d, e, f, g, h, i, j);
1121
- let k = target[offset++];
1122
- if ((k & 128) > 0) {
1123
- offset -= 11;
1124
- return;
1125
- }
1126
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
1127
- } else {
1128
- let i = target[offset++];
1129
- let j = target[offset++];
1130
- let k = target[offset++];
1131
- let l = target[offset++];
1132
- if ((i & 128) > 0 || (j & 128) > 0 || (k & 128) > 0 || (l & 128) > 0) {
1133
- offset -= 12;
1134
- return;
1135
- }
1136
- if (length < 14) {
1137
- if (length === 12)
1138
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
1139
- else {
1140
- let m = target[offset++];
1141
- if ((m & 128) > 0) {
1142
- offset -= 13;
1143
- return;
1144
- }
1145
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
1146
- }
1147
- } else {
1148
- let m = target[offset++];
1149
- let n = target[offset++];
1150
- if ((m & 128) > 0 || (n & 128) > 0) {
1151
- offset -= 14;
1152
- return;
1153
- }
1154
- if (length < 15)
1155
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
1156
- let o = target[offset++];
1157
- if ((o & 128) > 0) {
1158
- offset -= 15;
1159
- return;
1160
- }
1161
- return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
1162
- }
1163
- }
1164
- }
1165
- }
1166
- }
1167
- const utf8Write = function(target, value, offset) {
1168
- return value.length < 64 ? utf8WriteShort(target, value, offset) : toolkit.textEncoder.encodeInto(value, target.subarray(offset)).written;
1169
- };
1170
- const utf8WriteShort = (target, value, offset) => {
1171
- let i, c1, c2, strPosition = offset;
1172
- const strLength = value.length;
1173
- for (i = 0; i < strLength; i++) {
1174
- c1 = value.charCodeAt(i);
1175
- if (c1 < 128) {
1176
- target[strPosition++] = c1;
1177
- } else if (c1 < 2048) {
1178
- target[strPosition++] = c1 >> 6 | 192;
1179
- target[strPosition++] = c1 & 63 | 128;
1180
- } else if ((c1 & 64512) === 55296 && ((c2 = value.charCodeAt(i + 1)) & 64512) === 56320) {
1181
- c1 = 65536 + ((c1 & 1023) << 10) + (c2 & 1023);
1182
- i++;
1183
- target[strPosition++] = c1 >> 18 | 240;
1184
- target[strPosition++] = c1 >> 12 & 63 | 128;
1185
- target[strPosition++] = c1 >> 6 & 63 | 128;
1186
- target[strPosition++] = c1 & 63 | 128;
1187
- } else {
1188
- target[strPosition++] = c1 >> 12 | 224;
1189
- target[strPosition++] = c1 >> 6 & 63 | 128;
1190
- target[strPosition++] = c1 & 63 | 128;
1191
- }
1192
- }
1193
- return strPosition - offset;
1194
- };
1195
-
1196
- const NOOP_DICTIONARY = new Dictionary();
1197
- class BinaryReader {
1198
- target;
1199
- _last;
1200
- _lastObject;
1201
- dictionary;
1202
- dictionaryExtended;
1203
- extensions;
1204
- structures;
1205
- _repeat;
1206
- _checksumOffset;
1207
- offset;
1208
- length;
1209
- /**
1210
- * Small utility class to read binary data.
1211
- */
1212
- constructor(data, {
1213
- dictionary = NOOP_DICTIONARY,
1214
- extensions,
1215
- structures
1216
- } = {}) {
1217
- this.target = data;
1218
- this.offset = 0;
1219
- this._checksumOffset = 0;
1220
- this.length = data.length;
1221
- this.extensions = /* @__PURE__ */ new Map();
1222
- this.structures = /* @__PURE__ */ new Map();
1223
- if (extensions) {
1224
- for (const ext of extensions) {
1225
- this.extensions.set(ext.token, ext);
1226
- }
1227
- }
1228
- if (structures) {
1229
- for (const struct of structures) {
1230
- this.structures.set(struct.extension.token, struct);
1231
- }
1232
- }
1233
- if (Array.isArray(dictionary)) {
1234
- this.dictionary = new Dictionary(dictionary);
1235
- } else {
1236
- this.dictionary = dictionary;
1237
- }
1238
- this.dictionaryExtended = new Dictionary(void 0, this.dictionary.size);
1239
- }
1240
- readByte() {
1241
- this.assertRead(1);
1242
- this._last = this.target[this.offset++];
1243
- return this._last;
1244
- }
1245
- readInt64(signed = true) {
1246
- const low32 = this.readInt32(signed);
1247
- const high32 = this.readInt32(signed);
1248
- this._last = BigInt(high32) << 32n | BigInt(low32);
1249
- return this._last;
1250
- }
1251
- readInt32(signed = true) {
1252
- this.assertRead(4);
1253
- this._last = this.target[this.offset++] | this.target[this.offset++] << 8 | this.target[this.offset++] << 16 | this.target[this.offset++] << 24;
1254
- if (!signed) {
1255
- this._last = this._last >>> 0;
1256
- }
1257
- return this._last;
1258
- }
1259
- readInt16(signed = true) {
1260
- this.assertRead(2);
1261
- this._last = this.target[this.offset++] | this.target[this.offset++] << 8;
1262
- if (signed) {
1263
- this._last = this._last << 16 >> 16;
1264
- }
1265
- return this._last;
1266
- }
1267
- readInt8(signed = true) {
1268
- this.assertRead(1);
1269
- this._last = this.target[this.offset++];
1270
- if (signed) {
1271
- this._last = this._last << 24 >> 24;
1272
- }
1273
- return this._last;
1274
- }
1275
- /**
1276
- * Reads a real floating point (4 bytes) value.
1277
- * @returns {number}
1278
- */
1279
- readFloat() {
1280
- this.assertRead(4);
1281
- int32[0] = this.readInt32();
1282
- this._last = float32[0];
1283
- return this._last;
1284
- }
1285
- /**
1286
- * Reads a real floating point (8 bytes) value.
1287
- */
1288
- readDouble() {
1289
- this.assertRead(8);
1290
- int32[0] = this.readInt32();
1291
- int32[1] = this.readInt32();
1292
- this._last = float64[0];
1293
- return this._last;
1294
- }
1295
- /**
1296
- * Throws error if provided length cannot be read from buffer
1297
- * @param length {number}
1298
- */
1299
- assertRead(length) {
1300
- if (this.length < this.offset + +length) {
1301
- const left = this.target.length - this.offset;
1302
- const result = this.target.subarray(this.offset, this.offset + left);
1303
- const err = new Error(
1304
- `No more data left to read (need ${length}, got ${left}: ${result}); last read ${this._last}`
1305
- );
1306
- err.incomplete = true;
1307
- Error.captureStackTrace(err, this.assertRead);
1308
- throw err;
1309
- }
1310
- }
1311
- assertConstructor(constructorId) {
1312
- const byte = this.readByte();
1313
- if (byte !== constructorId) {
1314
- throw new Error(
1315
- `Invalid constructor code, expected = ${CORE_TYPES[constructorId]}, got = ${CORE_TYPES[byte] || byte}, offset = ${this.offset - 1}`
1316
- );
1317
- }
1318
- }
1319
- /**
1320
- * Gets the byte array representing the current buffer as a whole.
1321
- */
1322
- getBuffer() {
1323
- return this.target;
1324
- }
1325
- readNull() {
1326
- const value = this.readByte();
1327
- if (value === CORE_TYPES.Null) {
1328
- return null;
1329
- }
1330
- throw new Error(`Invalid boolean code ${value.toString(16)}`);
1331
- }
1332
- readLength() {
1333
- const firstByte = this.readByte();
1334
- if (firstByte === 254) {
1335
- return this.readByte() | this.readByte() << 8 | this.readByte() << 16;
1336
- }
1337
- return firstByte;
1338
- }
1339
- readAll() {
1340
- const result = [];
1341
- while (this.length > this.offset) {
1342
- result.push(this.readObject());
1343
- }
1344
- return result;
1345
- }
1346
- readBytes() {
1347
- const length = this.readLength();
1348
- this.assertRead(length);
1349
- const bytes = this.target.subarray(this.offset, this.offset + length);
1350
- this.offset += bytes.length;
1351
- this._last = bytes;
1352
- return bytes;
1353
- }
1354
- /**
1355
- * Reads encoded string.
1356
- */
1357
- readString() {
1358
- const length = this.readLength();
1359
- this.assertRead(length);
1360
- const result = utf8Read(this.target, length, this.offset);
1361
- this.offset += length;
1362
- this._last = result;
1363
- return result;
1364
- }
1365
- /**
1366
- * Reads a boolean value.
1367
- */
1368
- readBool() {
1369
- const value = this.readByte();
1370
- if (value === CORE_TYPES.BoolTrue) {
1371
- return true;
1372
- } else if (value === CORE_TYPES.BoolFalse) {
1373
- return false;
1374
- } else {
1375
- throw new Error(`Invalid boolean code ${value.toString(16)}`);
1376
- }
1377
- }
1378
- /**
1379
- * Reads and converts Unix time
1380
- * into a Javascript {Date} object.
1381
- */
1382
- readDate() {
1383
- const value = this.readDouble();
1384
- return new Date(value);
1385
- }
1386
- readStructure(checkConstructor = true) {
1387
- if (checkConstructor) {
1388
- this.assertConstructor(CORE_TYPES.Structure);
1389
- }
1390
- const structureId = this.readInt32(false);
1391
- const struct = this.structures.get(structureId);
1392
- if (!struct) {
1393
- throw new Error(
1394
- `Unknown structure id = ${structureId}, offset = ${this.offset - 1}`
1395
- );
1396
- }
1397
- return struct.extension.decode.call(this);
1398
- }
1399
- /**
1400
- * Reads a object.
1401
- */
1402
- readObject() {
1403
- if (this._repeat) {
1404
- if (this._repeat.pool > 0) {
1405
- --this._repeat.pool;
1406
- return this._repeat.value;
1407
- } else {
1408
- this._repeat = void 0;
1409
- }
1410
- }
1411
- const constructorId = this.readByte();
1412
- const ext = this.extensions.get(constructorId);
1413
- let value;
1414
- if (ext) {
1415
- value = ext.decode.call(this);
1416
- } else {
1417
- value = this._lastObject = this.readCore(constructorId);
1418
- }
1419
- return value;
1420
- }
1421
- readObjectGzip() {
1422
- const bytes = this.readGzip();
1423
- const reader = new BinaryReader(bytes);
1424
- reader.extensions = this.extensions;
1425
- reader.dictionary = this.dictionary;
1426
- reader.dictionaryExtended = this.dictionaryExtended;
1427
- return reader.readObject();
1428
- }
1429
- readGzip() {
1430
- return pako__default.inflateRaw(this.readBytes());
1431
- }
1432
- readCore(constructorId) {
1433
- switch (constructorId) {
1434
- case CORE_TYPES.None:
1435
- return this.readObject();
1436
- case CORE_TYPES.GZIP:
1437
- return this.readObjectGzip();
1438
- case CORE_TYPES.BoolTrue:
1439
- return true;
1440
- case CORE_TYPES.BoolFalse:
1441
- return false;
1442
- case CORE_TYPES.Vector:
1443
- return this.readVector(false);
1444
- case CORE_TYPES.VectorDynamic:
1445
- return this.readVectorDynamic(false);
1446
- case CORE_TYPES.Null:
1447
- return null;
1448
- case CORE_TYPES.Binary:
1449
- return this.readBytes();
1450
- case CORE_TYPES.String:
1451
- return this.readString();
1452
- case CORE_TYPES.Date:
1453
- return this.readDate();
1454
- case CORE_TYPES.Int64:
1455
- return this.readInt64();
1456
- case CORE_TYPES.Int32:
1457
- return this.readInt32();
1458
- case CORE_TYPES.Int16:
1459
- return this.readInt16();
1460
- case CORE_TYPES.Int8:
1461
- return this.readInt8();
1462
- case CORE_TYPES.UInt64:
1463
- return this.readInt64(false);
1464
- case CORE_TYPES.UInt32:
1465
- return this.readInt32(false);
1466
- case CORE_TYPES.UInt16:
1467
- return this.readInt16(false);
1468
- case CORE_TYPES.UInt8:
1469
- return this.readInt8(false);
1470
- case CORE_TYPES.Float:
1471
- return this.readFloat();
1472
- case CORE_TYPES.Double:
1473
- return this.readDouble();
1474
- case CORE_TYPES.Map:
1475
- return this.readMap(false);
1476
- case CORE_TYPES.Checksum:
1477
- return void this.readChecksum(false);
1478
- case CORE_TYPES.Structure:
1479
- return this.readStructure(false);
1480
- case CORE_TYPES.DictIndex: {
1481
- const idx = this.readLength();
1482
- return this.getDictionaryValue(idx);
1483
- }
1484
- case CORE_TYPES.DictValue: {
1485
- const value = this.readString();
1486
- this.dictionaryExtended.maybeInsert(value);
1487
- return value;
1488
- }
1489
- case CORE_TYPES.Repeat: {
1490
- const size = this.readLength();
1491
- this._repeat = { pool: size - 1, value: this._lastObject };
1492
- return this._lastObject;
1493
- }
1494
- }
1495
- throw new Error(
1496
- `Invalid constructor = ${CORE_TYPES[constructorId] || constructorId}, offset = ${this.offset - 1}`
1497
- );
1498
- }
1499
- getDictionaryValue(index) {
1500
- let value = null;
1501
- if (this.dictionary) {
1502
- value = this.dictionary.getValue(index);
1503
- }
1504
- if (value === null) {
1505
- value = this.dictionaryExtended.getValue(index);
1506
- }
1507
- return value;
1508
- }
1509
- readDictionary() {
1510
- const constructorId = this.readByte();
1511
- let key = null;
1512
- switch (constructorId) {
1513
- case CORE_TYPES.DictIndex: {
1514
- const idx = this.readLength();
1515
- key = this.getDictionaryValue(idx);
1516
- break;
1517
- }
1518
- case CORE_TYPES.DictValue: {
1519
- key = this.readString();
1520
- this.dictionaryExtended.maybeInsert(key);
1521
- break;
1522
- }
1523
- case CORE_TYPES.None: {
1524
- key = null;
1525
- break;
1526
- }
1527
- default: {
1528
- this.seek(-1);
1529
- }
1530
- }
1531
- return key;
1532
- }
1533
- readMap(checkConstructor = true) {
1534
- if (checkConstructor) {
1535
- this.assertConstructor(CORE_TYPES.Map);
1536
- }
1537
- const temp = {};
1538
- let key = this.readDictionary();
1539
- while (key !== null) {
1540
- temp[key] = this.readObject();
1541
- key = this.readDictionary();
1542
- }
1543
- return temp;
1544
- }
1545
- decode(value) {
1546
- this.target = value;
1547
- this._last = void 0;
1548
- this._lastObject = void 0;
1549
- this._repeat = void 0;
1550
- this.offset = 0;
1551
- this._checksumOffset = 0;
1552
- this.length = value.length;
1553
- return this.readObject();
1554
- }
1555
- /**
1556
- * Reads a vector (a list) of objects.
1557
- */
1558
- readVector(checkConstructor = true) {
1559
- if (checkConstructor) {
1560
- this.assertConstructor(CORE_TYPES.Vector);
1561
- }
1562
- const count = this.readLength();
1563
- const temp = [];
1564
- for (let i = 0; i < count; i++) {
1565
- temp.push(this.readObject());
1566
- }
1567
- return temp;
1568
- }
1569
- /**
1570
- * Reads a vector (a list) of objects.
1571
- */
1572
- readVectorDynamic(checkConstructor = true) {
1573
- if (checkConstructor) {
1574
- this.assertConstructor(CORE_TYPES.VectorDynamic);
1575
- }
1576
- const temp = [];
1577
- let complete = false;
1578
- while (this.length > this.offset) {
1579
- const constructorId = this.readByte();
1580
- if (constructorId === CORE_TYPES.None) {
1581
- complete = true;
1582
- break;
1583
- }
1584
- const ext = this.extensions.get(constructorId);
1585
- let value;
1586
- if (ext) {
1587
- value = ext.decode.call(this);
1588
- } else {
1589
- value = this.readCore(constructorId);
1590
- }
1591
- temp.push(value);
1592
- }
1593
- if (!complete) {
1594
- const err = new Error(`DynamicVector incomplete.`);
1595
- err.incomplete = true;
1596
- Error.captureStackTrace(err, this.readDictionary);
1597
- throw err;
1598
- }
1599
- this._last = temp;
1600
- return temp;
1601
- }
1602
- readChecksum(checkConstructor = true) {
1603
- const offset = this.offset;
1604
- if (checkConstructor) {
1605
- this.assertConstructor(CORE_TYPES.Checksum);
1606
- }
1607
- const bytes = this.target.subarray(this._checksumOffset, offset);
1608
- const checksum = this.readInt32();
1609
- let sum = 0;
1610
- for (const val of bytes) {
1611
- sum += val;
1612
- }
1613
- if (checksum - sum !== 0) {
1614
- throw new Error(
1615
- `Invalid checksum = ${checksum - sum}, offset = ${offset}`
1616
- );
1617
- }
1618
- this._checksumOffset = this.offset;
1619
- }
1620
- /**
1621
- * Tells the current position on the stream.
1622
- */
1623
- tellPosition() {
1624
- return this.offset;
1625
- }
1626
- /**
1627
- * Sets the current position on the stream.
1628
- */
1629
- setPosition(position) {
1630
- this.offset = position;
1631
- }
1632
- /**
1633
- * Seeks the stream position given an offset from the current position.
1634
- * The offset may be negative.
1635
- */
1636
- seek(offset) {
1637
- this.offset += offset;
1638
- }
1639
- /**
1640
- * Sets the current buffer and reset initial state.
1641
- */
1642
- reset(data) {
1643
- this.offset = 0;
1644
- this._checksumOffset = 0;
1645
- this._lastObject = void 0;
1646
- this._last = void 0;
1647
- this._repeat = void 0;
1648
- this.dictionaryExtended.clear();
1649
- if (data) {
1650
- this.length = data.length;
1651
- this.target = data;
1652
- }
1653
- }
1654
- }
1655
-
1656
- exports.BinaryReader = BinaryReader;
1657
- exports.BinaryWriter = BinaryWriter;
1658
- exports.CORE_TYPES = CORE_TYPES;
1659
- exports.MAX_BUFFER_SIZE = MAX_BUFFER_SIZE;
1660
- exports.Structure = Structure;
1661
- exports.createDictionary = createDictionary;
1662
- exports.defineStructure = defineStructure;
1663
- //# sourceMappingURL=tl-pack.5CF-VZgL.cjs.map