@andrew_l/tl-pack 0.3.22 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_chunks/BinaryReader.d.mts +481 -0
- package/dist/_chunks/BinaryReader.d.mts.map +1 -0
- package/dist/_chunks/BinaryReader.mjs +1300 -0
- package/dist/_chunks/BinaryReader.mjs.map +1 -0
- package/dist/index.d.mts +3 -6
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +14 -21
- package/dist/index.mjs.map +1 -1
- package/dist/stream.d.mts +13 -15
- package/dist/stream.d.mts.map +1 -0
- package/dist/stream.mjs +61 -68
- package/dist/stream.mjs.map +1 -1
- package/package.json +10 -9
- package/dist/index.cjs +0 -38
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -29
- package/dist/index.d.ts +0 -29
- package/dist/shared/tl-pack.5CF-VZgL.cjs +0 -1663
- package/dist/shared/tl-pack.5CF-VZgL.cjs.map +0 -1
- package/dist/shared/tl-pack.B8uBXdlJ.mjs +0 -1651
- package/dist/shared/tl-pack.B8uBXdlJ.mjs.map +0 -1
- package/dist/shared/tl-pack.DM1wAuup.d.cts +0 -482
- package/dist/shared/tl-pack.DM1wAuup.d.mts +0 -482
- package/dist/shared/tl-pack.DM1wAuup.d.ts +0 -482
- package/dist/stream.cjs +0 -72
- package/dist/stream.cjs.map +0 -1
- package/dist/stream.d.cts +0 -21
- package/dist/stream.d.ts +0 -21
|
@@ -0,0 +1,1300 @@
|
|
|
1
|
+
import pako from "pako";
|
|
2
|
+
import { assert, crc32, isPlainObject, noop, textDecoder, textEncoder } from "@andrew_l/toolkit";
|
|
3
|
+
let CORE_TYPES = /* @__PURE__ */ function(CORE_TYPES) {
|
|
4
|
+
CORE_TYPES[CORE_TYPES["None"] = 0] = "None";
|
|
5
|
+
CORE_TYPES[CORE_TYPES["Binary"] = 1] = "Binary";
|
|
6
|
+
CORE_TYPES[CORE_TYPES["BoolFalse"] = 2] = "BoolFalse";
|
|
7
|
+
CORE_TYPES[CORE_TYPES["BoolTrue"] = 3] = "BoolTrue";
|
|
8
|
+
CORE_TYPES[CORE_TYPES["Null"] = 4] = "Null";
|
|
9
|
+
CORE_TYPES[CORE_TYPES["Date"] = 5] = "Date";
|
|
10
|
+
CORE_TYPES[CORE_TYPES["Vector"] = 6] = "Vector";
|
|
11
|
+
CORE_TYPES[CORE_TYPES["VectorDynamic"] = 7] = "VectorDynamic";
|
|
12
|
+
CORE_TYPES[CORE_TYPES["Int64"] = 22] = "Int64";
|
|
13
|
+
CORE_TYPES[CORE_TYPES["Int32"] = 8] = "Int32";
|
|
14
|
+
CORE_TYPES[CORE_TYPES["Int16"] = 9] = "Int16";
|
|
15
|
+
CORE_TYPES[CORE_TYPES["Int8"] = 10] = "Int8";
|
|
16
|
+
CORE_TYPES[CORE_TYPES["UInt64"] = 23] = "UInt64";
|
|
17
|
+
CORE_TYPES[CORE_TYPES["UInt32"] = 11] = "UInt32";
|
|
18
|
+
CORE_TYPES[CORE_TYPES["UInt16"] = 12] = "UInt16";
|
|
19
|
+
CORE_TYPES[CORE_TYPES["UInt8"] = 13] = "UInt8";
|
|
20
|
+
CORE_TYPES[CORE_TYPES["Float"] = 14] = "Float";
|
|
21
|
+
CORE_TYPES[CORE_TYPES["Double"] = 15] = "Double";
|
|
22
|
+
CORE_TYPES[CORE_TYPES["Map"] = 16] = "Map";
|
|
23
|
+
CORE_TYPES[CORE_TYPES["DictValue"] = 17] = "DictValue";
|
|
24
|
+
CORE_TYPES[CORE_TYPES["DictIndex"] = 18] = "DictIndex";
|
|
25
|
+
CORE_TYPES[CORE_TYPES["String"] = 19] = "String";
|
|
26
|
+
CORE_TYPES[CORE_TYPES["Repeat"] = 20] = "Repeat";
|
|
27
|
+
CORE_TYPES[CORE_TYPES["Checksum"] = 21] = "Checksum";
|
|
28
|
+
CORE_TYPES[CORE_TYPES["GZIP"] = 25] = "GZIP";
|
|
29
|
+
CORE_TYPES[CORE_TYPES["Structure"] = 26] = "Structure";
|
|
30
|
+
return CORE_TYPES;
|
|
31
|
+
}({});
|
|
32
|
+
const MAX_BUFFER_SIZE = 2144337920;
|
|
33
|
+
function createDictionary(values) {
|
|
34
|
+
return new Dictionary(values);
|
|
35
|
+
}
|
|
36
|
+
var Dictionary = class {
|
|
37
|
+
_count;
|
|
38
|
+
_wordToIndex;
|
|
39
|
+
_words;
|
|
40
|
+
_offset;
|
|
41
|
+
constructor(values, offset = 0) {
|
|
42
|
+
this._count = 0;
|
|
43
|
+
this._words = [];
|
|
44
|
+
this._wordToIndex = /* @__PURE__ */ new Map();
|
|
45
|
+
this._offset = offset;
|
|
46
|
+
if (Array.isArray(values) && values.length) values.forEach((word) => {
|
|
47
|
+
if (this._wordToIndex.has(word)) return;
|
|
48
|
+
this._wordToIndex.set(word, this._count++);
|
|
49
|
+
this._words.push(word);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
clear() {
|
|
53
|
+
this._count = 0;
|
|
54
|
+
this._words.length = 0;
|
|
55
|
+
this._wordToIndex.clear();
|
|
56
|
+
}
|
|
57
|
+
get size() {
|
|
58
|
+
return this._count;
|
|
59
|
+
}
|
|
60
|
+
maybeInsert(word) {
|
|
61
|
+
if (this._wordToIndex.has(word)) return null;
|
|
62
|
+
this._wordToIndex.set(word, this._count++);
|
|
63
|
+
this._words.push(word);
|
|
64
|
+
return this._count + this._offset;
|
|
65
|
+
}
|
|
66
|
+
getValue(index) {
|
|
67
|
+
return this._words[index - this._offset] ?? null;
|
|
68
|
+
}
|
|
69
|
+
getIndex(value) {
|
|
70
|
+
const idx = this._wordToIndex.get(value);
|
|
71
|
+
if (idx === void 0) return null;
|
|
72
|
+
return idx + this._offset;
|
|
73
|
+
}
|
|
74
|
+
hasValue(value) {
|
|
75
|
+
return this._wordToIndex.has(value);
|
|
76
|
+
}
|
|
77
|
+
hasIndex(index) {
|
|
78
|
+
return this._words[index - this._offset] !== void 0;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const noop$1 = Symbol();
|
|
82
|
+
const NO_CONSTRUCTOR = /* @__PURE__ */ new Set([
|
|
83
|
+
2,
|
|
84
|
+
3,
|
|
85
|
+
4
|
|
86
|
+
]);
|
|
87
|
+
const SUPPORT_COMPRESSION = /* @__PURE__ */ new Set([19]);
|
|
88
|
+
const NOOP_DICTIONARY$1 = new Dictionary();
|
|
89
|
+
var BinaryWriter = class BinaryWriter {
|
|
90
|
+
withGzip;
|
|
91
|
+
target;
|
|
92
|
+
dictionary;
|
|
93
|
+
dictionaryExtended;
|
|
94
|
+
extensions;
|
|
95
|
+
structures;
|
|
96
|
+
_last;
|
|
97
|
+
offsetChecksum;
|
|
98
|
+
_repeat;
|
|
99
|
+
offset;
|
|
100
|
+
constructor({ gzip = false, dictionary = NOOP_DICTIONARY$1, extensions, structures } = {}) {
|
|
101
|
+
this.offset = 0;
|
|
102
|
+
this.offsetChecksum = 0;
|
|
103
|
+
this.extensions = /* @__PURE__ */ new Map();
|
|
104
|
+
this.structures = /* @__PURE__ */ new Map();
|
|
105
|
+
this.withGzip = gzip;
|
|
106
|
+
this.target = byteArrayAllocate(8192);
|
|
107
|
+
this._last = noop$1;
|
|
108
|
+
if (extensions) for (const ext of extensions) this.extensions.set(ext.token, ext);
|
|
109
|
+
if (structures) for (const struct of structures) this.structures.set(struct.extension.token, struct);
|
|
110
|
+
if (Array.isArray(dictionary)) this.dictionary = new Dictionary(dictionary);
|
|
111
|
+
else this.dictionary = dictionary;
|
|
112
|
+
this.dictionaryExtended = new Dictionary(void 0, this.dictionary.size);
|
|
113
|
+
}
|
|
114
|
+
reset() {
|
|
115
|
+
this.offset = 0;
|
|
116
|
+
this.offsetChecksum = 0;
|
|
117
|
+
this._last = noop$1;
|
|
118
|
+
this._repeat = void 0;
|
|
119
|
+
this.dictionaryExtended.clear();
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
allocate(size) {
|
|
123
|
+
const position = this.offset + size;
|
|
124
|
+
if (this.safeEnd < position) this.makeRoom(position);
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
makeRoom(end) {
|
|
128
|
+
let start = 0;
|
|
129
|
+
let newSize = 0;
|
|
130
|
+
let target = this.target;
|
|
131
|
+
if (end > 16777216) {
|
|
132
|
+
if (end - start > 2144337920) throw new Error("Packed buffer would be larger than maximum buffer size");
|
|
133
|
+
newSize = Math.min(MAX_BUFFER_SIZE, Math.round(Math.max((end - start) * (end > 67108864 ? 1.25 : 2), 4194304) / 4096) * 4096);
|
|
134
|
+
} else newSize = (Math.max(end - start << 2, target.length - 1) >> 12) + 1 << 12;
|
|
135
|
+
const newBuffer = byteArrayAllocate(newSize);
|
|
136
|
+
end = Math.min(end, target.length);
|
|
137
|
+
newBuffer.set(target.slice(start, end));
|
|
138
|
+
this.target = newBuffer;
|
|
139
|
+
}
|
|
140
|
+
get safeEnd() {
|
|
141
|
+
return this.target.length - 10;
|
|
142
|
+
}
|
|
143
|
+
getBuffer() {
|
|
144
|
+
return this.target.subarray(0, this.offset);
|
|
145
|
+
}
|
|
146
|
+
writeByte(value) {
|
|
147
|
+
this.allocate(1);
|
|
148
|
+
this.target[this.offset++] = value;
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
writeBool(value) {
|
|
152
|
+
if (value) this.writeByte(3);
|
|
153
|
+
else this.writeByte(2);
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
writeNull() {
|
|
157
|
+
this.writeByte(4);
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
writeInt64(value, signed = true) {
|
|
161
|
+
this.allocate(8);
|
|
162
|
+
if (typeof value === "number") value = BigInt(value);
|
|
163
|
+
const low32 = Number(value & 4294967295n);
|
|
164
|
+
const high32 = Number(value >> 32n);
|
|
165
|
+
this.writeInt32(low32, signed);
|
|
166
|
+
this.writeInt32(high32, signed);
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
writeInt32(value, signed = true) {
|
|
170
|
+
this.allocate(4);
|
|
171
|
+
if (signed) {
|
|
172
|
+
this.target[this.offset++] = value;
|
|
173
|
+
this.target[this.offset++] = value >> 8;
|
|
174
|
+
this.target[this.offset++] = value >> 16;
|
|
175
|
+
this.target[this.offset++] = value >> 24;
|
|
176
|
+
} else {
|
|
177
|
+
this.target[this.offset++] = value;
|
|
178
|
+
this.target[this.offset++] = value >> 8;
|
|
179
|
+
this.target[this.offset++] = value >> 16;
|
|
180
|
+
this.target[this.offset++] = value >> 24;
|
|
181
|
+
}
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
writeInt16(value, signed = true) {
|
|
185
|
+
this.allocate(2);
|
|
186
|
+
if (signed) {
|
|
187
|
+
this.target[this.offset++] = value;
|
|
188
|
+
this.target[this.offset++] = value >> 8;
|
|
189
|
+
} else {
|
|
190
|
+
this.target[this.offset++] = value;
|
|
191
|
+
this.target[this.offset++] = value >> 8;
|
|
192
|
+
}
|
|
193
|
+
return this;
|
|
194
|
+
}
|
|
195
|
+
writeInt8(value, signed = true) {
|
|
196
|
+
this.allocate(1);
|
|
197
|
+
this.target[this.offset++] = value;
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
writeFloat(value) {
|
|
201
|
+
this.allocate(4);
|
|
202
|
+
float32[0] = value;
|
|
203
|
+
this.writeInt32(int32[0]);
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
writeDouble(value) {
|
|
207
|
+
this.allocate(8);
|
|
208
|
+
float64[0] = value;
|
|
209
|
+
this.writeInt32(int32[0], false);
|
|
210
|
+
this.writeInt32(int32[1], false);
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
writeDate(value) {
|
|
214
|
+
let timestamp = 0;
|
|
215
|
+
if (value instanceof Date) timestamp = value.getTime();
|
|
216
|
+
else if (typeof value === "number") timestamp = value;
|
|
217
|
+
this.writeDouble(timestamp);
|
|
218
|
+
return this;
|
|
219
|
+
}
|
|
220
|
+
writeString(value) {
|
|
221
|
+
const strLength = value.length;
|
|
222
|
+
let start = this.offset;
|
|
223
|
+
let require = strLength << 2;
|
|
224
|
+
if (require < 254) {
|
|
225
|
+
require += 1;
|
|
226
|
+
this.offset += 1;
|
|
227
|
+
} else {
|
|
228
|
+
require += 4;
|
|
229
|
+
this.offset += 4;
|
|
230
|
+
}
|
|
231
|
+
this.allocate(require);
|
|
232
|
+
const bytes = utf8Write(this.target, value, this.offset);
|
|
233
|
+
if (require < 254) this.target[start++] = bytes;
|
|
234
|
+
else {
|
|
235
|
+
this.target[start++] = 254;
|
|
236
|
+
this.target[start++] = bytes % 256;
|
|
237
|
+
this.target[start++] = (bytes >> 8) % 256;
|
|
238
|
+
this.target[start++] = (bytes >> 16) % 256;
|
|
239
|
+
}
|
|
240
|
+
this.offset += bytes;
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
writeChecksum(withConstructor = true) {
|
|
244
|
+
const bytes = this.target.subarray(this.offsetChecksum, this.offset);
|
|
245
|
+
let sum = 0;
|
|
246
|
+
for (const val of bytes) sum += val;
|
|
247
|
+
if (withConstructor) this.writeByte(21);
|
|
248
|
+
this.writeInt32(sum);
|
|
249
|
+
this.offsetChecksum = this.offset;
|
|
250
|
+
return this;
|
|
251
|
+
}
|
|
252
|
+
writeBytes(value) {
|
|
253
|
+
const length = value.length;
|
|
254
|
+
this.writeLength(length);
|
|
255
|
+
this.allocate(length);
|
|
256
|
+
this.target.set(value, this.offset);
|
|
257
|
+
this.offset += length;
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
writeLength(value) {
|
|
261
|
+
if (value < 254) {
|
|
262
|
+
this.allocate(1);
|
|
263
|
+
this.target[this.offset++] = value;
|
|
264
|
+
} else {
|
|
265
|
+
this.allocate(4);
|
|
266
|
+
this.target[this.offset++] = 254;
|
|
267
|
+
this.target[this.offset++] = value % 256;
|
|
268
|
+
this.target[this.offset++] = (value >> 8) % 256;
|
|
269
|
+
this.target[this.offset++] = (value >> 16) % 256;
|
|
270
|
+
}
|
|
271
|
+
return this;
|
|
272
|
+
}
|
|
273
|
+
writeVector(value) {
|
|
274
|
+
const length = value.length;
|
|
275
|
+
this.writeLength(length);
|
|
276
|
+
for (let i = 0; i < length; i++) if (value[i] === void 0) this.writeNull();
|
|
277
|
+
else this.writeObject(value[i]);
|
|
278
|
+
return this;
|
|
279
|
+
}
|
|
280
|
+
writeMap(object) {
|
|
281
|
+
for (const key in object) {
|
|
282
|
+
if (object[key] === void 0) continue;
|
|
283
|
+
this._last = noop$1;
|
|
284
|
+
this.wireDictionary(key);
|
|
285
|
+
this.writeObject(object[key]);
|
|
286
|
+
}
|
|
287
|
+
this.writeByte(0);
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
wireDictionary(value) {
|
|
291
|
+
let idx = null;
|
|
292
|
+
idx = this.dictionary.getIndex(value);
|
|
293
|
+
if (idx === null) idx = this.dictionaryExtended.getIndex(value);
|
|
294
|
+
if (idx === null) {
|
|
295
|
+
this.dictionaryExtended.maybeInsert(value);
|
|
296
|
+
this.writeCore(17, value);
|
|
297
|
+
} else this.writeCore(18, idx);
|
|
298
|
+
return this;
|
|
299
|
+
}
|
|
300
|
+
writeStructure(value) {
|
|
301
|
+
const ctor = value.constructor;
|
|
302
|
+
this.writeInt32(ctor.extension.token, false);
|
|
303
|
+
ctor.extension.encode.call(this, value.value);
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
306
|
+
writeGzip(value) {
|
|
307
|
+
const compressed = pako.deflateRaw(value, { level: 9 });
|
|
308
|
+
this.writeBytes(compressed);
|
|
309
|
+
return this;
|
|
310
|
+
}
|
|
311
|
+
encode(value) {
|
|
312
|
+
this.offset = 0;
|
|
313
|
+
this.offsetChecksum = 0;
|
|
314
|
+
this._last = noop$1;
|
|
315
|
+
this._repeat = void 0;
|
|
316
|
+
this.target = byteArrayAllocate(256);
|
|
317
|
+
this.writeObject(value);
|
|
318
|
+
return this.getBuffer();
|
|
319
|
+
}
|
|
320
|
+
startDynamicVector() {
|
|
321
|
+
this.writeByte(7);
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
endDynamicVector() {
|
|
325
|
+
this.writeByte(0);
|
|
326
|
+
return this;
|
|
327
|
+
}
|
|
328
|
+
_writeCustom(value) {
|
|
329
|
+
const start = this.offset;
|
|
330
|
+
this.allocate(1);
|
|
331
|
+
this.offset++;
|
|
332
|
+
let edgeExt;
|
|
333
|
+
for (const ext of this.extensions.values()) {
|
|
334
|
+
if (ext.token === -1) {
|
|
335
|
+
edgeExt = ext;
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
ext.encode.call(this, value);
|
|
339
|
+
if (start + 1 < this.offset) {
|
|
340
|
+
const end = this.offset;
|
|
341
|
+
this.offset = start;
|
|
342
|
+
this.writeByte(ext.token);
|
|
343
|
+
this.offset = end;
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
this.offset = start;
|
|
348
|
+
if (edgeExt) {
|
|
349
|
+
edgeExt.encode.call(this, value);
|
|
350
|
+
return start < this.offset;
|
|
351
|
+
}
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
writeObject(value) {
|
|
355
|
+
if (value === void 0) return this;
|
|
356
|
+
const constructorId = coreType(value);
|
|
357
|
+
if (constructorId === 0) {
|
|
358
|
+
if (this._writeCustom(value)) return this;
|
|
359
|
+
throw new TypeError(`Invalid core type of ${value}`);
|
|
360
|
+
}
|
|
361
|
+
if (this._last === value) this.writeRepeat();
|
|
362
|
+
else {
|
|
363
|
+
this._last = value;
|
|
364
|
+
this._repeat = void 0;
|
|
365
|
+
this.writeCore(constructorId, value);
|
|
366
|
+
}
|
|
367
|
+
return this;
|
|
368
|
+
}
|
|
369
|
+
writeObjectGzip(value) {
|
|
370
|
+
const writer = new BinaryWriter();
|
|
371
|
+
writer.extensions = this.extensions;
|
|
372
|
+
writer.dictionary = this.dictionary;
|
|
373
|
+
writer.structures = this.structures;
|
|
374
|
+
writer.dictionaryExtended = this.dictionaryExtended;
|
|
375
|
+
writer.writeObject(value);
|
|
376
|
+
this.writeCore(25, writer.getBuffer());
|
|
377
|
+
return this;
|
|
378
|
+
}
|
|
379
|
+
writeCore(constructorId, value) {
|
|
380
|
+
if (this.withGzip && SUPPORT_COMPRESSION.has(constructorId)) {
|
|
381
|
+
this.writeObjectGzip(value);
|
|
382
|
+
return this;
|
|
383
|
+
} else if (!NO_CONSTRUCTOR.has(constructorId)) this.writeByte(constructorId);
|
|
384
|
+
switch (constructorId) {
|
|
385
|
+
case 26: return this.writeStructure(value);
|
|
386
|
+
case 1: return this.writeBytes(value);
|
|
387
|
+
case 25: return this.writeGzip(value);
|
|
388
|
+
case 18: return this.writeLength(value);
|
|
389
|
+
case 17: return this.writeString(value);
|
|
390
|
+
case 2: return this.writeBool(value);
|
|
391
|
+
case 3: return this.writeBool(value);
|
|
392
|
+
case 5: return this.writeDate(value);
|
|
393
|
+
case 22: return this.writeInt64(value);
|
|
394
|
+
case 8: return this.writeInt32(value);
|
|
395
|
+
case 9: return this.writeInt16(value);
|
|
396
|
+
case 10: return this.writeInt8(value);
|
|
397
|
+
case 23: return this.writeInt64(value, false);
|
|
398
|
+
case 11: return this.writeInt32(value, false);
|
|
399
|
+
case 12: return this.writeInt16(value, false);
|
|
400
|
+
case 13: return this.writeInt8(value, false);
|
|
401
|
+
case 15: return this.writeDouble(value);
|
|
402
|
+
case 14: return this.writeFloat(value);
|
|
403
|
+
case 4: return this.writeNull();
|
|
404
|
+
case 19:
|
|
405
|
+
if (value.length <= 16) {
|
|
406
|
+
this.offset--;
|
|
407
|
+
return this.wireDictionary(value);
|
|
408
|
+
}
|
|
409
|
+
return this.writeString(value);
|
|
410
|
+
case 6: return this.writeVector(value);
|
|
411
|
+
case 16: return this.writeMap(value);
|
|
412
|
+
}
|
|
413
|
+
return this;
|
|
414
|
+
}
|
|
415
|
+
writeRepeat() {
|
|
416
|
+
if (!this._repeat) {
|
|
417
|
+
this.writeByte(20);
|
|
418
|
+
this._repeat = {
|
|
419
|
+
count: 0,
|
|
420
|
+
offset: this.offset
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
this.offset = this._repeat.offset;
|
|
424
|
+
this._repeat.count++;
|
|
425
|
+
this.writeLength(this._repeat.count);
|
|
426
|
+
return this;
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
const CONSTRUCTOR_OPTIONAL = 1644261036;
|
|
430
|
+
const CONSTRUCTOR_OPTIONAL_NULL = 570519212;
|
|
431
|
+
const TYPE_HANDLERS = {
|
|
432
|
+
["unknown"]: {
|
|
433
|
+
encode: function(value, key) {
|
|
434
|
+
this.writeObject(value[key]);
|
|
435
|
+
},
|
|
436
|
+
decode: function(result, key) {
|
|
437
|
+
result[key] = this.readObject();
|
|
438
|
+
},
|
|
439
|
+
estimatedSizeBytes: 0
|
|
440
|
+
},
|
|
441
|
+
[16]: {
|
|
442
|
+
encode: function(value, key) {
|
|
443
|
+
this.writeMap(value[key]);
|
|
444
|
+
},
|
|
445
|
+
decode: function(result, key) {
|
|
446
|
+
result[key] = this.readMap(false);
|
|
447
|
+
},
|
|
448
|
+
estimatedSizeBytes: 0
|
|
449
|
+
},
|
|
450
|
+
[1]: {
|
|
451
|
+
encode: function(value, key) {
|
|
452
|
+
this.writeBytes(value[key]);
|
|
453
|
+
},
|
|
454
|
+
decode: function(result, key) {
|
|
455
|
+
result[key] = this.readBytes();
|
|
456
|
+
},
|
|
457
|
+
estimatedSizeBytes: 0
|
|
458
|
+
},
|
|
459
|
+
[6]: {
|
|
460
|
+
encode: function(value, key) {
|
|
461
|
+
this.writeVector(value[key]);
|
|
462
|
+
},
|
|
463
|
+
decode: function(result, key) {
|
|
464
|
+
result[key] = this.readVector(false);
|
|
465
|
+
},
|
|
466
|
+
estimatedSizeBytes: 0
|
|
467
|
+
},
|
|
468
|
+
[Boolean.name]: {
|
|
469
|
+
encode: function(value, key) {
|
|
470
|
+
this.writeBool(value[key]);
|
|
471
|
+
},
|
|
472
|
+
decode: function(result, key) {
|
|
473
|
+
result[key] = this.readBool();
|
|
474
|
+
},
|
|
475
|
+
estimatedSizeBytes: 1
|
|
476
|
+
},
|
|
477
|
+
[10]: {
|
|
478
|
+
encode: function(value, key) {
|
|
479
|
+
this.writeInt8(value[key], true);
|
|
480
|
+
},
|
|
481
|
+
decode: function(result, key) {
|
|
482
|
+
result[key] = this.readInt8(true);
|
|
483
|
+
},
|
|
484
|
+
estimatedSizeBytes: 1
|
|
485
|
+
},
|
|
486
|
+
[9]: {
|
|
487
|
+
encode: function(value, key) {
|
|
488
|
+
this.writeInt16(value[key], true);
|
|
489
|
+
},
|
|
490
|
+
decode: function(result, key) {
|
|
491
|
+
result[key] = this.readInt16(true);
|
|
492
|
+
},
|
|
493
|
+
estimatedSizeBytes: 2
|
|
494
|
+
},
|
|
495
|
+
[8]: {
|
|
496
|
+
encode: function(value, key) {
|
|
497
|
+
this.writeInt32(value[key], true);
|
|
498
|
+
},
|
|
499
|
+
decode: function(result, key) {
|
|
500
|
+
result[key] = this.readInt32(true);
|
|
501
|
+
},
|
|
502
|
+
estimatedSizeBytes: 4
|
|
503
|
+
},
|
|
504
|
+
[22]: {
|
|
505
|
+
encode: function(value, key) {
|
|
506
|
+
this.writeInt64(value[key], true);
|
|
507
|
+
},
|
|
508
|
+
decode: function(result, key) {
|
|
509
|
+
result[key] = this.readInt64(true);
|
|
510
|
+
},
|
|
511
|
+
estimatedSizeBytes: 8
|
|
512
|
+
},
|
|
513
|
+
[13]: {
|
|
514
|
+
encode: function(value, key) {
|
|
515
|
+
this.writeInt8(value[key], false);
|
|
516
|
+
},
|
|
517
|
+
decode: function(result, key) {
|
|
518
|
+
result[key] = this.readInt8(false);
|
|
519
|
+
},
|
|
520
|
+
estimatedSizeBytes: 1
|
|
521
|
+
},
|
|
522
|
+
[12]: {
|
|
523
|
+
encode: function(value, key) {
|
|
524
|
+
this.writeInt16(value[key], false);
|
|
525
|
+
},
|
|
526
|
+
decode: function(result, key) {
|
|
527
|
+
result[key] = this.readInt16(false);
|
|
528
|
+
},
|
|
529
|
+
estimatedSizeBytes: 2
|
|
530
|
+
},
|
|
531
|
+
[11]: {
|
|
532
|
+
encode: function(value, key) {
|
|
533
|
+
this.writeInt32(value[key], false);
|
|
534
|
+
},
|
|
535
|
+
decode: function(result, key) {
|
|
536
|
+
result[key] = this.readInt32(false);
|
|
537
|
+
},
|
|
538
|
+
estimatedSizeBytes: 4
|
|
539
|
+
},
|
|
540
|
+
[23]: {
|
|
541
|
+
encode: function(value, key) {
|
|
542
|
+
this.writeInt64(value[key], false);
|
|
543
|
+
},
|
|
544
|
+
decode: function(result, key) {
|
|
545
|
+
result[key] = this.readInt64(false);
|
|
546
|
+
},
|
|
547
|
+
estimatedSizeBytes: 8
|
|
548
|
+
},
|
|
549
|
+
[15]: {
|
|
550
|
+
encode: function(value, key) {
|
|
551
|
+
this.writeDouble(value[key]);
|
|
552
|
+
},
|
|
553
|
+
decode: function(result, key) {
|
|
554
|
+
result[key] = this.readDouble();
|
|
555
|
+
},
|
|
556
|
+
estimatedSizeBytes: 8
|
|
557
|
+
},
|
|
558
|
+
[5]: {
|
|
559
|
+
encode: function(value, key) {
|
|
560
|
+
this.writeDate(value[key]);
|
|
561
|
+
},
|
|
562
|
+
decode: function(result, key) {
|
|
563
|
+
result[key] = this.readDate();
|
|
564
|
+
},
|
|
565
|
+
estimatedSizeBytes: 8
|
|
566
|
+
},
|
|
567
|
+
[19]: {
|
|
568
|
+
encode: function(value, key) {
|
|
569
|
+
this.writeString(value[key]);
|
|
570
|
+
},
|
|
571
|
+
decode: function(result, key) {
|
|
572
|
+
result[key] = this.readString();
|
|
573
|
+
},
|
|
574
|
+
estimatedSizeBytes: 0
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
TYPE_HANDLERS[Number.name] = TYPE_HANDLERS[15];
|
|
578
|
+
TYPE_HANDLERS[String.name] = TYPE_HANDLERS[19];
|
|
579
|
+
TYPE_HANDLERS[Object.name] = TYPE_HANDLERS[16];
|
|
580
|
+
TYPE_HANDLERS[Uint8Array.name] = TYPE_HANDLERS[1];
|
|
581
|
+
TYPE_HANDLERS[Array.name] = TYPE_HANDLERS[6];
|
|
582
|
+
TYPE_HANDLERS[Date.name] = TYPE_HANDLERS[5];
|
|
583
|
+
function compileStructure(name, version, properties, checksum) {
|
|
584
|
+
const encodeFns = [];
|
|
585
|
+
const decodeFns = [];
|
|
586
|
+
const structures = [];
|
|
587
|
+
const structureId = crc32(name) >>> 0;
|
|
588
|
+
let estimatedSizeBytes = 1;
|
|
589
|
+
encodeFns.push(function() {
|
|
590
|
+
this.writeByte(version);
|
|
591
|
+
});
|
|
592
|
+
decodeFns.push(function() {
|
|
593
|
+
const ver = this.readByte();
|
|
594
|
+
assert.ok(version === ver, `Structure ${structureId} version mismatch: expected ${version}, got ${ver}`);
|
|
595
|
+
});
|
|
596
|
+
const entries = Object.entries(properties);
|
|
597
|
+
const entriesLength = entries.length;
|
|
598
|
+
for (let i = 0; i < entriesLength; i++) {
|
|
599
|
+
const [key, prop] = entries[i];
|
|
600
|
+
const isRequired = prop.required === true;
|
|
601
|
+
const isArray = Array.isArray(prop.type);
|
|
602
|
+
const propType = Array.isArray(prop.type) ? prop.type[0] : prop.type;
|
|
603
|
+
if (isStructureType(propType)) {
|
|
604
|
+
encodeFns.push(createStructureEncoder(key, propType, isRequired, isArray));
|
|
605
|
+
decodeFns.push(createStructureDecoder(key, isRequired, isArray));
|
|
606
|
+
estimatedSizeBytes += propType.estimatedSizeBytes;
|
|
607
|
+
structures.push(propType);
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
const typeName = propType?.name || (propType === null ? "unknown" : propType);
|
|
611
|
+
const handler = TYPE_HANDLERS[typeName];
|
|
612
|
+
if (handler) {
|
|
613
|
+
encodeFns.push(createBoundEncoder(handler.encode, key, isRequired, isArray));
|
|
614
|
+
decodeFns.push(createBoundDecoder(handler.decode, key, isRequired, isArray));
|
|
615
|
+
estimatedSizeBytes += handler.estimatedSizeBytes;
|
|
616
|
+
} else throw new Error(`Unsupported property type: ${typeName || "unknown"}`);
|
|
617
|
+
}
|
|
618
|
+
if (checksum) {
|
|
619
|
+
estimatedSizeBytes += 4;
|
|
620
|
+
encodeFns.push(function() {
|
|
621
|
+
this.writeChecksum(false);
|
|
622
|
+
});
|
|
623
|
+
decodeFns.push(function() {
|
|
624
|
+
this.readChecksum(false);
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
return {
|
|
628
|
+
id: structureId,
|
|
629
|
+
encodeFns,
|
|
630
|
+
decodeFns,
|
|
631
|
+
structures,
|
|
632
|
+
estimatedSizeBytes
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
function isStructureType(type) {
|
|
636
|
+
return type?.prototype && Structure.prototype.isPrototypeOf(type.prototype);
|
|
637
|
+
}
|
|
638
|
+
function createStructureEncoder(key, StructureCtor, isRequired, isArray) {
|
|
639
|
+
return function(value) {
|
|
640
|
+
if (!(value[key] !== void 0 && value[key] !== null)) {
|
|
641
|
+
assert.ok(!isRequired, `Required property "${key}" is missing or null`);
|
|
642
|
+
if (value[key] === null) this.writeInt32(CONSTRUCTOR_OPTIONAL_NULL);
|
|
643
|
+
else this.writeInt32(CONSTRUCTOR_OPTIONAL);
|
|
644
|
+
} else if (isArray) {
|
|
645
|
+
const arr = value[key];
|
|
646
|
+
assert.array(arr, `Expected property "${key}" to be array.`);
|
|
647
|
+
this.writeLength(arr.length);
|
|
648
|
+
for (let idx = 0; idx < arr.length; idx++) this.writeStructure(arr[idx] instanceof Structure ? arr[idx] : new StructureCtor(arr[idx]));
|
|
649
|
+
} else this.writeStructure(value[key] instanceof Structure ? value[key] : new StructureCtor(value[key]));
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function createStructureDecoder(key, isRequired, isArray) {
|
|
653
|
+
return function(result) {
|
|
654
|
+
let shouldRead = true;
|
|
655
|
+
if (!isRequired) {
|
|
656
|
+
shouldRead = !readMaybeInt32(this, CONSTRUCTOR_OPTIONAL);
|
|
657
|
+
if (shouldRead && readMaybeInt32(this, CONSTRUCTOR_OPTIONAL_NULL)) {
|
|
658
|
+
result[key] = null;
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (shouldRead) if (isArray) {
|
|
663
|
+
const length = this.readLength();
|
|
664
|
+
const arrResult = Array.from({ length });
|
|
665
|
+
for (let idx = 0; idx < length; idx++) arrResult[idx] = this.readStructure(false);
|
|
666
|
+
result[key] = arrResult;
|
|
667
|
+
} else result[key] = this.readStructure(false);
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
function readMaybeInt32(reader, expectedValue) {
|
|
671
|
+
let result = false;
|
|
672
|
+
if (reader.length >= reader.offset + 4) {
|
|
673
|
+
result = reader.readInt32() === expectedValue;
|
|
674
|
+
if (!result) reader.offset -= 4;
|
|
675
|
+
}
|
|
676
|
+
return result;
|
|
677
|
+
}
|
|
678
|
+
function createBoundEncoder(encodeFn, key, isRequired, isArray) {
|
|
679
|
+
return function(value) {
|
|
680
|
+
if (!(value[key] !== void 0 && value[key] !== null)) {
|
|
681
|
+
assert.ok(!isRequired, `Required property "${key}" is missing or null`);
|
|
682
|
+
if (value[key] === null) this.writeInt32(CONSTRUCTOR_OPTIONAL_NULL);
|
|
683
|
+
else this.writeInt32(CONSTRUCTOR_OPTIONAL);
|
|
684
|
+
} else if (isArray) {
|
|
685
|
+
const arr = value[key];
|
|
686
|
+
assert.array(arr, `Expected property "${key}" to be array.`);
|
|
687
|
+
this.writeLength(arr.length);
|
|
688
|
+
for (let idx = 0; idx < arr.length; idx++) encodeFn.call(this, arr, idx);
|
|
689
|
+
} else encodeFn.call(this, value, key);
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
function createBoundDecoder(decodeFn, key, isRequired, isArray) {
|
|
693
|
+
return function(result) {
|
|
694
|
+
let shouldRead = true;
|
|
695
|
+
if (!isRequired) {
|
|
696
|
+
shouldRead = !readMaybeInt32(this, CONSTRUCTOR_OPTIONAL);
|
|
697
|
+
if (shouldRead && readMaybeInt32(this, CONSTRUCTOR_OPTIONAL_NULL)) {
|
|
698
|
+
result[key] = null;
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
if (shouldRead) if (isArray) {
|
|
703
|
+
const length = this.readLength();
|
|
704
|
+
const arrResult = Array.from({ length });
|
|
705
|
+
for (let idx = 0; idx < length; idx++) decodeFn.call(this, arrResult, idx);
|
|
706
|
+
result[key] = arrResult;
|
|
707
|
+
} else decodeFn.call(this, result, key);
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
function defineStructure({ name, properties, version, checksum = false }) {
|
|
711
|
+
const compiled = compileStructure(name, version, properties, checksum);
|
|
712
|
+
return class DefinedStructure extends Structure {
|
|
713
|
+
static estimatedSizeBytes = compiled.estimatedSizeBytes;
|
|
714
|
+
static structures = compiled.structures;
|
|
715
|
+
static extension = {
|
|
716
|
+
token: compiled.id,
|
|
717
|
+
encode(value) {
|
|
718
|
+
const fns = compiled.encodeFns;
|
|
719
|
+
const length = fns.length;
|
|
720
|
+
for (let i = 0; i < length; i++) fns[i].call(this, value);
|
|
721
|
+
},
|
|
722
|
+
decode() {
|
|
723
|
+
const result = {};
|
|
724
|
+
const fns = compiled.decodeFns;
|
|
725
|
+
const length = fns.length;
|
|
726
|
+
for (let i = 0; i < length; i++) fns[i].call(this, result);
|
|
727
|
+
return result;
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
var Structure = class {
|
|
733
|
+
value;
|
|
734
|
+
constructor(value) {
|
|
735
|
+
this.value = value;
|
|
736
|
+
}
|
|
737
|
+
toBuffer(options) {
|
|
738
|
+
const ctor = this.constructor;
|
|
739
|
+
const writer = new BinaryWriter(options);
|
|
740
|
+
ctor.extension.encode.call(writer, this.value);
|
|
741
|
+
return writer.getBuffer();
|
|
742
|
+
}
|
|
743
|
+
static fromBuffer(buffer, options = {}) {
|
|
744
|
+
const reader = new BinaryReader(buffer, {
|
|
745
|
+
...options,
|
|
746
|
+
structures: options.structures ? this.structures.concat(options.structures) : this.structures
|
|
747
|
+
});
|
|
748
|
+
return this.extension.decode.call(reader);
|
|
749
|
+
}
|
|
750
|
+
static estimatedSizeBytes = -1;
|
|
751
|
+
static structures = [];
|
|
752
|
+
static extension = {
|
|
753
|
+
token: 1,
|
|
754
|
+
encode: noop,
|
|
755
|
+
decode: noop
|
|
756
|
+
};
|
|
757
|
+
};
|
|
758
|
+
const fromCharCode = String.fromCharCode;
|
|
759
|
+
const int32 = /* @__PURE__ */ new Int32Array(2);
|
|
760
|
+
const float32 = new Float32Array(int32.buffer);
|
|
761
|
+
const float64 = new Float64Array(int32.buffer);
|
|
762
|
+
function byteArrayAllocate(length) {
|
|
763
|
+
return new Uint8Array(length);
|
|
764
|
+
}
|
|
765
|
+
function coreType(value) {
|
|
766
|
+
if (value instanceof Structure) return 26;
|
|
767
|
+
else if (value instanceof Uint8Array) return 1;
|
|
768
|
+
switch (typeof value) {
|
|
769
|
+
case "string": return 19;
|
|
770
|
+
case "boolean": return value ? 3 : 2;
|
|
771
|
+
case "bigint":
|
|
772
|
+
if (value >= 0n && value <= 18446744073709551615n) return 23;
|
|
773
|
+
else if (value >= -9223372036854775808n && value <= 9223372036854775807n) return 22;
|
|
774
|
+
return 0;
|
|
775
|
+
case "number":
|
|
776
|
+
if (Math.trunc(value) === value) {
|
|
777
|
+
if (value >= 0 && value <= 255) return 13;
|
|
778
|
+
else if (value >= 0 && value <= 65535) return 12;
|
|
779
|
+
else if (value >= 0 && value <= 4294967295) return 11;
|
|
780
|
+
else if (value >= -128 && value <= 127) return 10;
|
|
781
|
+
else if (value >= -32768 && value <= 32767) return 9;
|
|
782
|
+
else if (value >= -2147483648 && value <= 2147483647) return 8;
|
|
783
|
+
}
|
|
784
|
+
return 15;
|
|
785
|
+
case "object":
|
|
786
|
+
if (value === null) return 4;
|
|
787
|
+
if (value instanceof Date) return 5;
|
|
788
|
+
if (Array.isArray(value)) return 6;
|
|
789
|
+
if (isPlainObject(value)) return 16;
|
|
790
|
+
}
|
|
791
|
+
return 0;
|
|
792
|
+
}
|
|
793
|
+
function utf8Read(target, length, offset) {
|
|
794
|
+
let result;
|
|
795
|
+
if (length < 16) {
|
|
796
|
+
if (result = utf8ReadShort(target, length, offset)) return result;
|
|
797
|
+
}
|
|
798
|
+
if (length > 64) return textDecoder.decode(target.subarray(offset, offset += length));
|
|
799
|
+
const end = offset + length;
|
|
800
|
+
const units = [];
|
|
801
|
+
result = "";
|
|
802
|
+
while (offset < end) {
|
|
803
|
+
const byte1 = target[offset++];
|
|
804
|
+
if ((byte1 & 128) === 0) units.push(byte1);
|
|
805
|
+
else if ((byte1 & 224) === 192) {
|
|
806
|
+
const byte2 = target[offset++] & 63;
|
|
807
|
+
units.push((byte1 & 31) << 6 | byte2);
|
|
808
|
+
} else if ((byte1 & 240) === 224) {
|
|
809
|
+
const byte2 = target[offset++] & 63;
|
|
810
|
+
const byte3 = target[offset++] & 63;
|
|
811
|
+
units.push((byte1 & 31) << 12 | byte2 << 6 | byte3);
|
|
812
|
+
} else if ((byte1 & 248) === 240) {
|
|
813
|
+
const byte2 = target[offset++] & 63;
|
|
814
|
+
const byte3 = target[offset++] & 63;
|
|
815
|
+
const byte4 = target[offset++] & 63;
|
|
816
|
+
let unit = (byte1 & 7) << 18 | byte2 << 12 | byte3 << 6 | byte4;
|
|
817
|
+
if (unit > 65535) {
|
|
818
|
+
unit -= 65536;
|
|
819
|
+
units.push(unit >>> 10 & 1023 | 55296);
|
|
820
|
+
unit = 56320 | unit & 1023;
|
|
821
|
+
}
|
|
822
|
+
units.push(unit);
|
|
823
|
+
} else units.push(byte1);
|
|
824
|
+
if (units.length >= 4096) {
|
|
825
|
+
result += fromCharCode.apply(String, units);
|
|
826
|
+
units.length = 0;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
if (units.length > 0) result += fromCharCode.apply(String, units);
|
|
830
|
+
return result;
|
|
831
|
+
}
|
|
832
|
+
function utf8ReadShort(target, length, offset) {
|
|
833
|
+
if (length < 4) if (length < 2) if (length === 0) return "";
|
|
834
|
+
else {
|
|
835
|
+
let a = target[offset++];
|
|
836
|
+
if ((a & 128) > 1) {
|
|
837
|
+
offset -= 1;
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
return fromCharCode(a);
|
|
841
|
+
}
|
|
842
|
+
else {
|
|
843
|
+
let a = target[offset++];
|
|
844
|
+
let b = target[offset++];
|
|
845
|
+
if ((a & 128) > 0 || (b & 128) > 0) {
|
|
846
|
+
offset -= 2;
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (length < 3) return fromCharCode(a, b);
|
|
850
|
+
let c = target[offset++];
|
|
851
|
+
if ((c & 128) > 0) {
|
|
852
|
+
offset -= 3;
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
return fromCharCode(a, b, c);
|
|
856
|
+
}
|
|
857
|
+
else {
|
|
858
|
+
let a = target[offset++];
|
|
859
|
+
let b = target[offset++];
|
|
860
|
+
let c = target[offset++];
|
|
861
|
+
let d = target[offset++];
|
|
862
|
+
if ((a & 128) > 0 || (b & 128) > 0 || (c & 128) > 0 || (d & 128) > 0) {
|
|
863
|
+
offset -= 4;
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
if (length < 6) if (length === 4) return fromCharCode(a, b, c, d);
|
|
867
|
+
else {
|
|
868
|
+
let e = target[offset++];
|
|
869
|
+
if ((e & 128) > 0) {
|
|
870
|
+
offset -= 5;
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
return fromCharCode(a, b, c, d, e);
|
|
874
|
+
}
|
|
875
|
+
else if (length < 8) {
|
|
876
|
+
let e = target[offset++];
|
|
877
|
+
let f = target[offset++];
|
|
878
|
+
if ((e & 128) > 0 || (f & 128) > 0) {
|
|
879
|
+
offset -= 6;
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
if (length < 7) return fromCharCode(a, b, c, d, e, f);
|
|
883
|
+
let g = target[offset++];
|
|
884
|
+
if ((g & 128) > 0) {
|
|
885
|
+
offset -= 7;
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
return fromCharCode(a, b, c, d, e, f, g);
|
|
889
|
+
} else {
|
|
890
|
+
let e = target[offset++];
|
|
891
|
+
let f = target[offset++];
|
|
892
|
+
let g = target[offset++];
|
|
893
|
+
let h = target[offset++];
|
|
894
|
+
if ((e & 128) > 0 || (f & 128) > 0 || (g & 128) > 0 || (h & 128) > 0) {
|
|
895
|
+
offset -= 8;
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
if (length < 10) if (length === 8) return fromCharCode(a, b, c, d, e, f, g, h);
|
|
899
|
+
else {
|
|
900
|
+
let i = target[offset++];
|
|
901
|
+
if ((i & 128) > 0) {
|
|
902
|
+
offset -= 9;
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
906
|
+
}
|
|
907
|
+
else if (length < 12) {
|
|
908
|
+
let i = target[offset++];
|
|
909
|
+
let j = target[offset++];
|
|
910
|
+
if ((i & 128) > 0 || (j & 128) > 0) {
|
|
911
|
+
offset -= 10;
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
if (length < 11) return fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
915
|
+
let k = target[offset++];
|
|
916
|
+
if ((k & 128) > 0) {
|
|
917
|
+
offset -= 11;
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
921
|
+
} else {
|
|
922
|
+
let i = target[offset++];
|
|
923
|
+
let j = target[offset++];
|
|
924
|
+
let k = target[offset++];
|
|
925
|
+
let l = target[offset++];
|
|
926
|
+
if ((i & 128) > 0 || (j & 128) > 0 || (k & 128) > 0 || (l & 128) > 0) {
|
|
927
|
+
offset -= 12;
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
if (length < 14) if (length === 12) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
931
|
+
else {
|
|
932
|
+
let m = target[offset++];
|
|
933
|
+
if ((m & 128) > 0) {
|
|
934
|
+
offset -= 13;
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
938
|
+
}
|
|
939
|
+
else {
|
|
940
|
+
let m = target[offset++];
|
|
941
|
+
let n = target[offset++];
|
|
942
|
+
if ((m & 128) > 0 || (n & 128) > 0) {
|
|
943
|
+
offset -= 14;
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
if (length < 15) return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
947
|
+
let o = target[offset++];
|
|
948
|
+
if ((o & 128) > 0) {
|
|
949
|
+
offset -= 15;
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
const utf8Write = function(target, value, offset) {
|
|
959
|
+
return value.length < 64 ? utf8WriteShort(target, value, offset) : textEncoder.encodeInto(value, target.subarray(offset)).written;
|
|
960
|
+
};
|
|
961
|
+
const utf8WriteShort = (target, value, offset) => {
|
|
962
|
+
let i, c1, c2, strPosition = offset;
|
|
963
|
+
const strLength = value.length;
|
|
964
|
+
for (i = 0; i < strLength; i++) {
|
|
965
|
+
c1 = value.charCodeAt(i);
|
|
966
|
+
if (c1 < 128) target[strPosition++] = c1;
|
|
967
|
+
else if (c1 < 2048) {
|
|
968
|
+
target[strPosition++] = c1 >> 6 | 192;
|
|
969
|
+
target[strPosition++] = c1 & 63 | 128;
|
|
970
|
+
} else if ((c1 & 64512) === 55296 && ((c2 = value.charCodeAt(i + 1)) & 64512) === 56320) {
|
|
971
|
+
c1 = 65536 + ((c1 & 1023) << 10) + (c2 & 1023);
|
|
972
|
+
i++;
|
|
973
|
+
target[strPosition++] = c1 >> 18 | 240;
|
|
974
|
+
target[strPosition++] = c1 >> 12 & 63 | 128;
|
|
975
|
+
target[strPosition++] = c1 >> 6 & 63 | 128;
|
|
976
|
+
target[strPosition++] = c1 & 63 | 128;
|
|
977
|
+
} else {
|
|
978
|
+
target[strPosition++] = c1 >> 12 | 224;
|
|
979
|
+
target[strPosition++] = c1 >> 6 & 63 | 128;
|
|
980
|
+
target[strPosition++] = c1 & 63 | 128;
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
return strPosition - offset;
|
|
984
|
+
};
|
|
985
|
+
const NOOP_DICTIONARY = new Dictionary();
|
|
986
|
+
var BinaryReader = class BinaryReader {
|
|
987
|
+
target;
|
|
988
|
+
_last;
|
|
989
|
+
_lastObject;
|
|
990
|
+
dictionary;
|
|
991
|
+
dictionaryExtended;
|
|
992
|
+
extensions;
|
|
993
|
+
structures;
|
|
994
|
+
_repeat;
|
|
995
|
+
_checksumOffset;
|
|
996
|
+
offset;
|
|
997
|
+
length;
|
|
998
|
+
constructor(data, { dictionary = NOOP_DICTIONARY, extensions, structures } = {}) {
|
|
999
|
+
this.target = data;
|
|
1000
|
+
this.offset = 0;
|
|
1001
|
+
this._checksumOffset = 0;
|
|
1002
|
+
this.length = data.length;
|
|
1003
|
+
this.extensions = /* @__PURE__ */ new Map();
|
|
1004
|
+
this.structures = /* @__PURE__ */ new Map();
|
|
1005
|
+
if (extensions) for (const ext of extensions) this.extensions.set(ext.token, ext);
|
|
1006
|
+
if (structures) for (const struct of structures) this.structures.set(struct.extension.token, struct);
|
|
1007
|
+
if (Array.isArray(dictionary)) this.dictionary = new Dictionary(dictionary);
|
|
1008
|
+
else this.dictionary = dictionary;
|
|
1009
|
+
this.dictionaryExtended = new Dictionary(void 0, this.dictionary.size);
|
|
1010
|
+
}
|
|
1011
|
+
readByte() {
|
|
1012
|
+
this.assertRead(1);
|
|
1013
|
+
this._last = this.target[this.offset++];
|
|
1014
|
+
return this._last;
|
|
1015
|
+
}
|
|
1016
|
+
readInt64(signed = true) {
|
|
1017
|
+
const low32 = this.readInt32(signed);
|
|
1018
|
+
const high32 = this.readInt32(signed);
|
|
1019
|
+
this._last = BigInt(high32) << 32n | BigInt(low32);
|
|
1020
|
+
return this._last;
|
|
1021
|
+
}
|
|
1022
|
+
readInt32(signed = true) {
|
|
1023
|
+
this.assertRead(4);
|
|
1024
|
+
this._last = this.target[this.offset++] | this.target[this.offset++] << 8 | this.target[this.offset++] << 16 | this.target[this.offset++] << 24;
|
|
1025
|
+
if (!signed) this._last = this._last >>> 0;
|
|
1026
|
+
return this._last;
|
|
1027
|
+
}
|
|
1028
|
+
readInt16(signed = true) {
|
|
1029
|
+
this.assertRead(2);
|
|
1030
|
+
this._last = this.target[this.offset++] | this.target[this.offset++] << 8;
|
|
1031
|
+
if (signed) this._last = this._last << 16 >> 16;
|
|
1032
|
+
return this._last;
|
|
1033
|
+
}
|
|
1034
|
+
readInt8(signed = true) {
|
|
1035
|
+
this.assertRead(1);
|
|
1036
|
+
this._last = this.target[this.offset++];
|
|
1037
|
+
if (signed) this._last = this._last << 24 >> 24;
|
|
1038
|
+
return this._last;
|
|
1039
|
+
}
|
|
1040
|
+
readFloat() {
|
|
1041
|
+
this.assertRead(4);
|
|
1042
|
+
int32[0] = this.readInt32();
|
|
1043
|
+
this._last = float32[0];
|
|
1044
|
+
return this._last;
|
|
1045
|
+
}
|
|
1046
|
+
readDouble() {
|
|
1047
|
+
this.assertRead(8);
|
|
1048
|
+
int32[0] = this.readInt32();
|
|
1049
|
+
int32[1] = this.readInt32();
|
|
1050
|
+
this._last = float64[0];
|
|
1051
|
+
return this._last;
|
|
1052
|
+
}
|
|
1053
|
+
assertRead(length) {
|
|
1054
|
+
if (this.length < this.offset + +length) {
|
|
1055
|
+
const left = this.target.length - this.offset;
|
|
1056
|
+
const result = this.target.subarray(this.offset, this.offset + left);
|
|
1057
|
+
const err = /* @__PURE__ */ new Error(`No more data left to read (need ${length}, got ${left}: ${result}); last read ${this._last}`);
|
|
1058
|
+
err.incomplete = true;
|
|
1059
|
+
Error.captureStackTrace(err, this.assertRead);
|
|
1060
|
+
throw err;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
assertConstructor(constructorId) {
|
|
1064
|
+
const byte = this.readByte();
|
|
1065
|
+
if (byte !== constructorId) throw new Error(`Invalid constructor code, expected = ${CORE_TYPES[constructorId]}, got = ${CORE_TYPES[byte] || byte}, offset = ${this.offset - 1}`);
|
|
1066
|
+
}
|
|
1067
|
+
getBuffer() {
|
|
1068
|
+
return this.target;
|
|
1069
|
+
}
|
|
1070
|
+
readNull() {
|
|
1071
|
+
const value = this.readByte();
|
|
1072
|
+
if (value === 4) return null;
|
|
1073
|
+
throw new Error(`Invalid boolean code ${value.toString(16)}`);
|
|
1074
|
+
}
|
|
1075
|
+
readLength() {
|
|
1076
|
+
const firstByte = this.readByte();
|
|
1077
|
+
if (firstByte === 254) return this.readByte() | this.readByte() << 8 | this.readByte() << 16;
|
|
1078
|
+
return firstByte;
|
|
1079
|
+
}
|
|
1080
|
+
readAll() {
|
|
1081
|
+
const result = [];
|
|
1082
|
+
while (this.length > this.offset) result.push(this.readObject());
|
|
1083
|
+
return result;
|
|
1084
|
+
}
|
|
1085
|
+
readBytes() {
|
|
1086
|
+
const length = this.readLength();
|
|
1087
|
+
this.assertRead(length);
|
|
1088
|
+
const bytes = this.target.subarray(this.offset, this.offset + length);
|
|
1089
|
+
this.offset += bytes.length;
|
|
1090
|
+
this._last = bytes;
|
|
1091
|
+
return bytes;
|
|
1092
|
+
}
|
|
1093
|
+
readString() {
|
|
1094
|
+
const length = this.readLength();
|
|
1095
|
+
this.assertRead(length);
|
|
1096
|
+
const result = utf8Read(this.target, length, this.offset);
|
|
1097
|
+
this.offset += length;
|
|
1098
|
+
this._last = result;
|
|
1099
|
+
return result;
|
|
1100
|
+
}
|
|
1101
|
+
readBool() {
|
|
1102
|
+
const value = this.readByte();
|
|
1103
|
+
if (value === 3) return true;
|
|
1104
|
+
else if (value === 2) return false;
|
|
1105
|
+
else throw new Error(`Invalid boolean code ${value.toString(16)}`);
|
|
1106
|
+
}
|
|
1107
|
+
readDate() {
|
|
1108
|
+
const value = this.readDouble();
|
|
1109
|
+
return new Date(value);
|
|
1110
|
+
}
|
|
1111
|
+
readStructure(checkConstructor = true) {
|
|
1112
|
+
if (checkConstructor) this.assertConstructor(26);
|
|
1113
|
+
const structureId = this.readInt32(false);
|
|
1114
|
+
const struct = this.structures.get(structureId);
|
|
1115
|
+
if (!struct) throw new Error(`Unknown structure id = ${structureId}, offset = ${this.offset - 1}`);
|
|
1116
|
+
return struct.extension.decode.call(this);
|
|
1117
|
+
}
|
|
1118
|
+
readObject() {
|
|
1119
|
+
if (this._repeat) if (this._repeat.pool > 0) {
|
|
1120
|
+
--this._repeat.pool;
|
|
1121
|
+
return this._repeat.value;
|
|
1122
|
+
} else this._repeat = void 0;
|
|
1123
|
+
const constructorId = this.readByte();
|
|
1124
|
+
const ext = this.extensions.get(constructorId);
|
|
1125
|
+
let value;
|
|
1126
|
+
if (ext) value = ext.decode.call(this);
|
|
1127
|
+
else value = this._lastObject = this.readCore(constructorId);
|
|
1128
|
+
return value;
|
|
1129
|
+
}
|
|
1130
|
+
readObjectGzip() {
|
|
1131
|
+
const bytes = this.readGzip();
|
|
1132
|
+
const reader = new BinaryReader(bytes);
|
|
1133
|
+
reader.extensions = this.extensions;
|
|
1134
|
+
reader.dictionary = this.dictionary;
|
|
1135
|
+
reader.dictionaryExtended = this.dictionaryExtended;
|
|
1136
|
+
return reader.readObject();
|
|
1137
|
+
}
|
|
1138
|
+
readGzip() {
|
|
1139
|
+
return pako.inflateRaw(this.readBytes());
|
|
1140
|
+
}
|
|
1141
|
+
readCore(constructorId) {
|
|
1142
|
+
switch (constructorId) {
|
|
1143
|
+
case 0: return this.readObject();
|
|
1144
|
+
case 25: return this.readObjectGzip();
|
|
1145
|
+
case 3: return true;
|
|
1146
|
+
case 2: return false;
|
|
1147
|
+
case 6: return this.readVector(false);
|
|
1148
|
+
case 7: return this.readVectorDynamic(false);
|
|
1149
|
+
case 4: return null;
|
|
1150
|
+
case 1: return this.readBytes();
|
|
1151
|
+
case 19: return this.readString();
|
|
1152
|
+
case 5: return this.readDate();
|
|
1153
|
+
case 22: return this.readInt64();
|
|
1154
|
+
case 8: return this.readInt32();
|
|
1155
|
+
case 9: return this.readInt16();
|
|
1156
|
+
case 10: return this.readInt8();
|
|
1157
|
+
case 23: return this.readInt64(false);
|
|
1158
|
+
case 11: return this.readInt32(false);
|
|
1159
|
+
case 12: return this.readInt16(false);
|
|
1160
|
+
case 13: return this.readInt8(false);
|
|
1161
|
+
case 14: return this.readFloat();
|
|
1162
|
+
case 15: return this.readDouble();
|
|
1163
|
+
case 16: return this.readMap(false);
|
|
1164
|
+
case 21:
|
|
1165
|
+
this.readChecksum(false);
|
|
1166
|
+
return;
|
|
1167
|
+
case 26: return this.readStructure(false);
|
|
1168
|
+
case 18: {
|
|
1169
|
+
const idx = this.readLength();
|
|
1170
|
+
return this.getDictionaryValue(idx);
|
|
1171
|
+
}
|
|
1172
|
+
case 17: {
|
|
1173
|
+
const value = this.readString();
|
|
1174
|
+
this.dictionaryExtended.maybeInsert(value);
|
|
1175
|
+
return value;
|
|
1176
|
+
}
|
|
1177
|
+
case 20: {
|
|
1178
|
+
const size = this.readLength();
|
|
1179
|
+
this._repeat = {
|
|
1180
|
+
pool: size - 1,
|
|
1181
|
+
value: this._lastObject
|
|
1182
|
+
};
|
|
1183
|
+
return this._lastObject;
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
throw new Error(`Invalid constructor = ${CORE_TYPES[constructorId] || constructorId}, offset = ${this.offset - 1}`);
|
|
1187
|
+
}
|
|
1188
|
+
getDictionaryValue(index) {
|
|
1189
|
+
let value = null;
|
|
1190
|
+
if (this.dictionary) value = this.dictionary.getValue(index);
|
|
1191
|
+
if (value === null) value = this.dictionaryExtended.getValue(index);
|
|
1192
|
+
return value;
|
|
1193
|
+
}
|
|
1194
|
+
readDictionary() {
|
|
1195
|
+
const constructorId = this.readByte();
|
|
1196
|
+
let key = null;
|
|
1197
|
+
switch (constructorId) {
|
|
1198
|
+
case 18: {
|
|
1199
|
+
const idx = this.readLength();
|
|
1200
|
+
key = this.getDictionaryValue(idx);
|
|
1201
|
+
break;
|
|
1202
|
+
}
|
|
1203
|
+
case 17:
|
|
1204
|
+
key = this.readString();
|
|
1205
|
+
this.dictionaryExtended.maybeInsert(key);
|
|
1206
|
+
break;
|
|
1207
|
+
case 0:
|
|
1208
|
+
key = null;
|
|
1209
|
+
break;
|
|
1210
|
+
default: this.seek(-1);
|
|
1211
|
+
}
|
|
1212
|
+
return key;
|
|
1213
|
+
}
|
|
1214
|
+
readMap(checkConstructor = true) {
|
|
1215
|
+
if (checkConstructor) this.assertConstructor(16);
|
|
1216
|
+
const temp = {};
|
|
1217
|
+
let key = this.readDictionary();
|
|
1218
|
+
while (key !== null) {
|
|
1219
|
+
temp[key] = this.readObject();
|
|
1220
|
+
key = this.readDictionary();
|
|
1221
|
+
}
|
|
1222
|
+
return temp;
|
|
1223
|
+
}
|
|
1224
|
+
decode(value) {
|
|
1225
|
+
this.target = value;
|
|
1226
|
+
this._last = void 0;
|
|
1227
|
+
this._lastObject = void 0;
|
|
1228
|
+
this._repeat = void 0;
|
|
1229
|
+
this.offset = 0;
|
|
1230
|
+
this._checksumOffset = 0;
|
|
1231
|
+
this.length = value.length;
|
|
1232
|
+
return this.readObject();
|
|
1233
|
+
}
|
|
1234
|
+
readVector(checkConstructor = true) {
|
|
1235
|
+
if (checkConstructor) this.assertConstructor(6);
|
|
1236
|
+
const count = this.readLength();
|
|
1237
|
+
const temp = [];
|
|
1238
|
+
for (let i = 0; i < count; i++) temp.push(this.readObject());
|
|
1239
|
+
return temp;
|
|
1240
|
+
}
|
|
1241
|
+
readVectorDynamic(checkConstructor = true) {
|
|
1242
|
+
if (checkConstructor) this.assertConstructor(7);
|
|
1243
|
+
const temp = [];
|
|
1244
|
+
let complete = false;
|
|
1245
|
+
while (this.length > this.offset) {
|
|
1246
|
+
const constructorId = this.readByte();
|
|
1247
|
+
if (constructorId === 0) {
|
|
1248
|
+
complete = true;
|
|
1249
|
+
break;
|
|
1250
|
+
}
|
|
1251
|
+
const ext = this.extensions.get(constructorId);
|
|
1252
|
+
let value;
|
|
1253
|
+
if (ext) value = ext.decode.call(this);
|
|
1254
|
+
else value = this.readCore(constructorId);
|
|
1255
|
+
temp.push(value);
|
|
1256
|
+
}
|
|
1257
|
+
if (!complete) {
|
|
1258
|
+
const err = /* @__PURE__ */ new Error(`DynamicVector incomplete.`);
|
|
1259
|
+
err.incomplete = true;
|
|
1260
|
+
Error.captureStackTrace(err, this.readDictionary);
|
|
1261
|
+
throw err;
|
|
1262
|
+
}
|
|
1263
|
+
this._last = temp;
|
|
1264
|
+
return temp;
|
|
1265
|
+
}
|
|
1266
|
+
readChecksum(checkConstructor = true) {
|
|
1267
|
+
const offset = this.offset;
|
|
1268
|
+
if (checkConstructor) this.assertConstructor(21);
|
|
1269
|
+
const bytes = this.target.subarray(this._checksumOffset, offset);
|
|
1270
|
+
const checksum = this.readInt32();
|
|
1271
|
+
let sum = 0;
|
|
1272
|
+
for (const val of bytes) sum += val;
|
|
1273
|
+
if (checksum - sum !== 0) throw new Error(`Invalid checksum = ${checksum - sum}, offset = ${offset}`);
|
|
1274
|
+
this._checksumOffset = this.offset;
|
|
1275
|
+
}
|
|
1276
|
+
tellPosition() {
|
|
1277
|
+
return this.offset;
|
|
1278
|
+
}
|
|
1279
|
+
setPosition(position) {
|
|
1280
|
+
this.offset = position;
|
|
1281
|
+
}
|
|
1282
|
+
seek(offset) {
|
|
1283
|
+
this.offset += offset;
|
|
1284
|
+
}
|
|
1285
|
+
reset(data) {
|
|
1286
|
+
this.offset = 0;
|
|
1287
|
+
this._checksumOffset = 0;
|
|
1288
|
+
this._lastObject = void 0;
|
|
1289
|
+
this._last = void 0;
|
|
1290
|
+
this._repeat = void 0;
|
|
1291
|
+
this.dictionaryExtended.clear();
|
|
1292
|
+
if (data) {
|
|
1293
|
+
this.length = data.length;
|
|
1294
|
+
this.target = data;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
};
|
|
1298
|
+
export { BinaryReader, BinaryWriter, CORE_TYPES, MAX_BUFFER_SIZE, Structure, createDictionary, defineStructure };
|
|
1299
|
+
|
|
1300
|
+
//# sourceMappingURL=BinaryReader.mjs.map
|