@andrew_l/tl-pack 0.1.8 → 0.1.9
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/LICENSE +21 -0
- package/README.md +5 -5
- package/dist/index.cjs +36 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +28 -0
- package/dist/index.d.ts +28 -5
- package/dist/index.mjs +28 -0
- package/dist/index.mjs.map +1 -0
- package/dist/shared/tl-pack.CdcwN4dG.d.cts +195 -0
- package/dist/shared/tl-pack.CdcwN4dG.d.mts +195 -0
- package/dist/shared/tl-pack.CdcwN4dG.d.ts +195 -0
- package/dist/shared/tl-pack.DnNse4P6.mjs +1112 -0
- package/dist/shared/tl-pack.DnNse4P6.mjs.map +1 -0
- package/dist/shared/tl-pack.VfbARqWq.cjs +1122 -0
- package/dist/shared/tl-pack.VfbARqWq.cjs.map +1 -0
- package/dist/stream.cjs +72 -0
- package/dist/stream.cjs.map +1 -0
- package/dist/stream.d.cts +20 -0
- package/dist/stream.d.mts +20 -0
- package/dist/stream.d.ts +8 -8
- package/dist/stream.mjs +69 -0
- package/dist/stream.mjs.map +1 -0
- package/package.json +42 -53
- package/dist/BinaryReader.d.ts +0 -109
- package/dist/BinaryReader.js +0 -412
- package/dist/BinaryWriter.d.ts +0 -47
- package/dist/BinaryWriter.js +0 -385
- package/dist/constants.d.ts +0 -26
- package/dist/constants.js +0 -27
- package/dist/dictionary.d.ts +0 -17
- package/dist/dictionary.js +0 -51
- package/dist/extension.d.ts +0 -13
- package/dist/extension.js +0 -13
- package/dist/helpers.d.ts +0 -11
- package/dist/helpers.js +0 -321
- package/dist/index.js +0 -5
- package/dist/stream.js +0 -67
|
@@ -0,0 +1,1112 @@
|
|
|
1
|
+
import pako from 'pako';
|
|
2
|
+
import { isObject } from '@andrew_l/toolkit';
|
|
3
|
+
|
|
4
|
+
var CORE_TYPES = /* @__PURE__ */ ((CORE_TYPES2) => {
|
|
5
|
+
CORE_TYPES2[CORE_TYPES2["None"] = 0] = "None";
|
|
6
|
+
CORE_TYPES2[CORE_TYPES2["Binary"] = 1] = "Binary";
|
|
7
|
+
CORE_TYPES2[CORE_TYPES2["BoolFalse"] = 2] = "BoolFalse";
|
|
8
|
+
CORE_TYPES2[CORE_TYPES2["BoolTrue"] = 3] = "BoolTrue";
|
|
9
|
+
CORE_TYPES2[CORE_TYPES2["Null"] = 4] = "Null";
|
|
10
|
+
CORE_TYPES2[CORE_TYPES2["Date"] = 5] = "Date";
|
|
11
|
+
CORE_TYPES2[CORE_TYPES2["Vector"] = 6] = "Vector";
|
|
12
|
+
CORE_TYPES2[CORE_TYPES2["VectorDynamic"] = 7] = "VectorDynamic";
|
|
13
|
+
CORE_TYPES2[CORE_TYPES2["Int32"] = 8] = "Int32";
|
|
14
|
+
CORE_TYPES2[CORE_TYPES2["Int16"] = 9] = "Int16";
|
|
15
|
+
CORE_TYPES2[CORE_TYPES2["Int8"] = 10] = "Int8";
|
|
16
|
+
CORE_TYPES2[CORE_TYPES2["UInt32"] = 11] = "UInt32";
|
|
17
|
+
CORE_TYPES2[CORE_TYPES2["UInt16"] = 12] = "UInt16";
|
|
18
|
+
CORE_TYPES2[CORE_TYPES2["UInt8"] = 13] = "UInt8";
|
|
19
|
+
CORE_TYPES2[CORE_TYPES2["Float"] = 14] = "Float";
|
|
20
|
+
CORE_TYPES2[CORE_TYPES2["Double"] = 15] = "Double";
|
|
21
|
+
CORE_TYPES2[CORE_TYPES2["Map"] = 16] = "Map";
|
|
22
|
+
CORE_TYPES2[CORE_TYPES2["DictValue"] = 17] = "DictValue";
|
|
23
|
+
CORE_TYPES2[CORE_TYPES2["DictIndex"] = 18] = "DictIndex";
|
|
24
|
+
CORE_TYPES2[CORE_TYPES2["String"] = 19] = "String";
|
|
25
|
+
CORE_TYPES2[CORE_TYPES2["Repeat"] = 20] = "Repeat";
|
|
26
|
+
CORE_TYPES2[CORE_TYPES2["GZIP"] = 25] = "GZIP";
|
|
27
|
+
return CORE_TYPES2;
|
|
28
|
+
})(CORE_TYPES || {});
|
|
29
|
+
const MAX_BUFFER_SIZE = 2144337920;
|
|
30
|
+
|
|
31
|
+
function createDictionary(values) {
|
|
32
|
+
return new Dictionary(values);
|
|
33
|
+
}
|
|
34
|
+
class Dictionary {
|
|
35
|
+
_count = 0;
|
|
36
|
+
_wordToIndex;
|
|
37
|
+
_words;
|
|
38
|
+
_offset;
|
|
39
|
+
constructor(values, offset = 0) {
|
|
40
|
+
this._words = [];
|
|
41
|
+
this._wordToIndex = /* @__PURE__ */ new Map();
|
|
42
|
+
this._offset = offset;
|
|
43
|
+
if (Array.isArray(values) && values.length) {
|
|
44
|
+
values.forEach((word) => {
|
|
45
|
+
if (this._wordToIndex.has(word)) return;
|
|
46
|
+
this._wordToIndex.set(word, this._count++);
|
|
47
|
+
this._words.push(word);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
get size() {
|
|
52
|
+
return this._count;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns inserted index or nothing
|
|
56
|
+
*/
|
|
57
|
+
maybeInsert(word) {
|
|
58
|
+
if (this._wordToIndex.has(word)) return null;
|
|
59
|
+
this._wordToIndex.set(word, this._count++);
|
|
60
|
+
this._words.push(word);
|
|
61
|
+
return this._count + this._offset;
|
|
62
|
+
}
|
|
63
|
+
getValue(index) {
|
|
64
|
+
return this._words[index - this._offset] ?? null;
|
|
65
|
+
}
|
|
66
|
+
getIndex(value) {
|
|
67
|
+
const idx = this._wordToIndex.get(value);
|
|
68
|
+
if (idx === void 0) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
return idx + this._offset;
|
|
72
|
+
}
|
|
73
|
+
hasValue(value) {
|
|
74
|
+
return this._wordToIndex.has(value);
|
|
75
|
+
}
|
|
76
|
+
hasIndex(index) {
|
|
77
|
+
return this._words[index - this._offset] !== void 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const encoder = new TextEncoder();
|
|
82
|
+
const decoder = new TextDecoder();
|
|
83
|
+
const fromCharCode = String.fromCharCode;
|
|
84
|
+
const int32 = new Int32Array(2);
|
|
85
|
+
const float32 = new Float32Array(int32.buffer);
|
|
86
|
+
const float64 = new Float64Array(int32.buffer);
|
|
87
|
+
function byteArrayAllocate(length) {
|
|
88
|
+
return new Uint8Array(length);
|
|
89
|
+
}
|
|
90
|
+
function coreType(value) {
|
|
91
|
+
switch (typeof value) {
|
|
92
|
+
case "string": {
|
|
93
|
+
return CORE_TYPES.String;
|
|
94
|
+
}
|
|
95
|
+
case "boolean": {
|
|
96
|
+
return value ? CORE_TYPES.BoolTrue : CORE_TYPES.BoolFalse;
|
|
97
|
+
}
|
|
98
|
+
case "number": {
|
|
99
|
+
if (Math.trunc(value) === value) {
|
|
100
|
+
if (value >= 0 && value <= 255) {
|
|
101
|
+
return CORE_TYPES.UInt8;
|
|
102
|
+
} else if (value >= 0 && value <= 65535) {
|
|
103
|
+
return CORE_TYPES.UInt16;
|
|
104
|
+
} else if (value >= 0 && value <= 4294967295) {
|
|
105
|
+
return CORE_TYPES.UInt32;
|
|
106
|
+
} else if (value >= -128 && value <= 127) {
|
|
107
|
+
return CORE_TYPES.Int8;
|
|
108
|
+
} else if (value >= -32768 && value <= 32767) {
|
|
109
|
+
return CORE_TYPES.Int16;
|
|
110
|
+
} else if (value >= -2147483648 && value <= 2147483647) {
|
|
111
|
+
return CORE_TYPES.Int32;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return CORE_TYPES.Double;
|
|
115
|
+
}
|
|
116
|
+
case "object": {
|
|
117
|
+
if (value === null) return CORE_TYPES.Null;
|
|
118
|
+
if (value instanceof Date) {
|
|
119
|
+
return CORE_TYPES.Date;
|
|
120
|
+
}
|
|
121
|
+
if (Array.isArray(value)) {
|
|
122
|
+
return CORE_TYPES.Vector;
|
|
123
|
+
}
|
|
124
|
+
if (isObject(value)) {
|
|
125
|
+
return CORE_TYPES.Map;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return CORE_TYPES.None;
|
|
130
|
+
}
|
|
131
|
+
function utf8Read(target, length, offset) {
|
|
132
|
+
let result;
|
|
133
|
+
if (length < 16) {
|
|
134
|
+
if (result = utf8ReadShort(target, length, offset)) return result;
|
|
135
|
+
}
|
|
136
|
+
if (length > 64 && decoder)
|
|
137
|
+
return decoder.decode(target.subarray(offset, offset += length));
|
|
138
|
+
const end = offset + length;
|
|
139
|
+
const units = [];
|
|
140
|
+
result = "";
|
|
141
|
+
while (offset < end) {
|
|
142
|
+
const byte1 = target[offset++];
|
|
143
|
+
if ((byte1 & 128) === 0) {
|
|
144
|
+
units.push(byte1);
|
|
145
|
+
} else if ((byte1 & 224) === 192) {
|
|
146
|
+
const byte2 = target[offset++] & 63;
|
|
147
|
+
units.push((byte1 & 31) << 6 | byte2);
|
|
148
|
+
} else if ((byte1 & 240) === 224) {
|
|
149
|
+
const byte2 = target[offset++] & 63;
|
|
150
|
+
const byte3 = target[offset++] & 63;
|
|
151
|
+
units.push((byte1 & 31) << 12 | byte2 << 6 | byte3);
|
|
152
|
+
} else if ((byte1 & 248) === 240) {
|
|
153
|
+
const byte2 = target[offset++] & 63;
|
|
154
|
+
const byte3 = target[offset++] & 63;
|
|
155
|
+
const byte4 = target[offset++] & 63;
|
|
156
|
+
let unit = (byte1 & 7) << 18 | byte2 << 12 | byte3 << 6 | byte4;
|
|
157
|
+
if (unit > 65535) {
|
|
158
|
+
unit -= 65536;
|
|
159
|
+
units.push(unit >>> 10 & 1023 | 55296);
|
|
160
|
+
unit = 56320 | unit & 1023;
|
|
161
|
+
}
|
|
162
|
+
units.push(unit);
|
|
163
|
+
} else {
|
|
164
|
+
units.push(byte1);
|
|
165
|
+
}
|
|
166
|
+
if (units.length >= 4096) {
|
|
167
|
+
result += fromCharCode.apply(String, units);
|
|
168
|
+
units.length = 0;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (units.length > 0) {
|
|
172
|
+
result += fromCharCode.apply(String, units);
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
function utf8ReadShort(target, length, offset) {
|
|
177
|
+
if (length < 4) {
|
|
178
|
+
if (length < 2) {
|
|
179
|
+
if (length === 0) return "";
|
|
180
|
+
else {
|
|
181
|
+
let a = target[offset++];
|
|
182
|
+
if ((a & 128) > 1) {
|
|
183
|
+
offset -= 1;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
return fromCharCode(a);
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
let a = target[offset++];
|
|
190
|
+
let b = target[offset++];
|
|
191
|
+
if ((a & 128) > 0 || (b & 128) > 0) {
|
|
192
|
+
offset -= 2;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (length < 3) return fromCharCode(a, b);
|
|
196
|
+
let c = target[offset++];
|
|
197
|
+
if ((c & 128) > 0) {
|
|
198
|
+
offset -= 3;
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
return fromCharCode(a, b, c);
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
let a = target[offset++];
|
|
205
|
+
let b = target[offset++];
|
|
206
|
+
let c = target[offset++];
|
|
207
|
+
let d = target[offset++];
|
|
208
|
+
if ((a & 128) > 0 || (b & 128) > 0 || (c & 128) > 0 || (d & 128) > 0) {
|
|
209
|
+
offset -= 4;
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (length < 6) {
|
|
213
|
+
if (length === 4) return fromCharCode(a, b, c, d);
|
|
214
|
+
else {
|
|
215
|
+
let e = target[offset++];
|
|
216
|
+
if ((e & 128) > 0) {
|
|
217
|
+
offset -= 5;
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
return fromCharCode(a, b, c, d, e);
|
|
221
|
+
}
|
|
222
|
+
} else if (length < 8) {
|
|
223
|
+
let e = target[offset++];
|
|
224
|
+
let f = target[offset++];
|
|
225
|
+
if ((e & 128) > 0 || (f & 128) > 0) {
|
|
226
|
+
offset -= 6;
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (length < 7) return fromCharCode(a, b, c, d, e, f);
|
|
230
|
+
let g = target[offset++];
|
|
231
|
+
if ((g & 128) > 0) {
|
|
232
|
+
offset -= 7;
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
return fromCharCode(a, b, c, d, e, f, g);
|
|
236
|
+
} else {
|
|
237
|
+
let e = target[offset++];
|
|
238
|
+
let f = target[offset++];
|
|
239
|
+
let g = target[offset++];
|
|
240
|
+
let h = target[offset++];
|
|
241
|
+
if ((e & 128) > 0 || (f & 128) > 0 || (g & 128) > 0 || (h & 128) > 0) {
|
|
242
|
+
offset -= 8;
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (length < 10) {
|
|
246
|
+
if (length === 8) return fromCharCode(a, b, c, d, e, f, g, h);
|
|
247
|
+
else {
|
|
248
|
+
let i = target[offset++];
|
|
249
|
+
if ((i & 128) > 0) {
|
|
250
|
+
offset -= 9;
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
254
|
+
}
|
|
255
|
+
} else if (length < 12) {
|
|
256
|
+
let i = target[offset++];
|
|
257
|
+
let j = target[offset++];
|
|
258
|
+
if ((i & 128) > 0 || (j & 128) > 0) {
|
|
259
|
+
offset -= 10;
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (length < 11) return fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
263
|
+
let k = target[offset++];
|
|
264
|
+
if ((k & 128) > 0) {
|
|
265
|
+
offset -= 11;
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
269
|
+
} else {
|
|
270
|
+
let i = target[offset++];
|
|
271
|
+
let j = target[offset++];
|
|
272
|
+
let k = target[offset++];
|
|
273
|
+
let l = target[offset++];
|
|
274
|
+
if ((i & 128) > 0 || (j & 128) > 0 || (k & 128) > 0 || (l & 128) > 0) {
|
|
275
|
+
offset -= 12;
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (length < 14) {
|
|
279
|
+
if (length === 12)
|
|
280
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
281
|
+
else {
|
|
282
|
+
let m = target[offset++];
|
|
283
|
+
if ((m & 128) > 0) {
|
|
284
|
+
offset -= 13;
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
288
|
+
}
|
|
289
|
+
} else {
|
|
290
|
+
let m = target[offset++];
|
|
291
|
+
let n = target[offset++];
|
|
292
|
+
if ((m & 128) > 0 || (n & 128) > 0) {
|
|
293
|
+
offset -= 14;
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (length < 15)
|
|
297
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
298
|
+
let o = target[offset++];
|
|
299
|
+
if ((o & 128) > 0) {
|
|
300
|
+
offset -= 15;
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const utf8Write = function(target, value, offset) {
|
|
310
|
+
return value.length < 64 ? utf8WriteShort(target, value, offset) : encoder.encodeInto(value, target.subarray(offset)).written;
|
|
311
|
+
};
|
|
312
|
+
const utf8WriteShort = (target, value, offset) => {
|
|
313
|
+
let i, c1, c2, strPosition = offset;
|
|
314
|
+
const strLength = value.length;
|
|
315
|
+
for (i = 0; i < strLength; i++) {
|
|
316
|
+
c1 = value.charCodeAt(i);
|
|
317
|
+
if (c1 < 128) {
|
|
318
|
+
target[strPosition++] = c1;
|
|
319
|
+
} else if (c1 < 2048) {
|
|
320
|
+
target[strPosition++] = c1 >> 6 | 192;
|
|
321
|
+
target[strPosition++] = c1 & 63 | 128;
|
|
322
|
+
} else if ((c1 & 64512) === 55296 && ((c2 = value.charCodeAt(i + 1)) & 64512) === 56320) {
|
|
323
|
+
c1 = 65536 + ((c1 & 1023) << 10) + (c2 & 1023);
|
|
324
|
+
i++;
|
|
325
|
+
target[strPosition++] = c1 >> 18 | 240;
|
|
326
|
+
target[strPosition++] = c1 >> 12 & 63 | 128;
|
|
327
|
+
target[strPosition++] = c1 >> 6 & 63 | 128;
|
|
328
|
+
target[strPosition++] = c1 & 63 | 128;
|
|
329
|
+
} else {
|
|
330
|
+
target[strPosition++] = c1 >> 12 | 224;
|
|
331
|
+
target[strPosition++] = c1 >> 6 & 63 | 128;
|
|
332
|
+
target[strPosition++] = c1 & 63 | 128;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return strPosition - offset;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
class BinaryReader {
|
|
339
|
+
target;
|
|
340
|
+
_last;
|
|
341
|
+
_lastObject;
|
|
342
|
+
dictionary;
|
|
343
|
+
dictionaryExtended;
|
|
344
|
+
extensions;
|
|
345
|
+
_repeat;
|
|
346
|
+
offset;
|
|
347
|
+
length;
|
|
348
|
+
/**
|
|
349
|
+
* Small utility class to read binary data.
|
|
350
|
+
*/
|
|
351
|
+
constructor(data, options) {
|
|
352
|
+
this.target = data;
|
|
353
|
+
this.offset = 0;
|
|
354
|
+
this.length = data.length;
|
|
355
|
+
this.extensions = /* @__PURE__ */ new Map();
|
|
356
|
+
if (options && options.extensions) {
|
|
357
|
+
options.extensions.forEach((ext) => {
|
|
358
|
+
this.extensions.set(ext.token, ext);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
if (!options) {
|
|
362
|
+
this.dictionary = new Dictionary();
|
|
363
|
+
} else if (options.dictionary instanceof Dictionary) {
|
|
364
|
+
this.dictionary = options.dictionary;
|
|
365
|
+
} else if (Array.isArray(options.dictionary)) {
|
|
366
|
+
this.dictionary = new Dictionary(options.dictionary);
|
|
367
|
+
} else {
|
|
368
|
+
this.dictionary = new Dictionary();
|
|
369
|
+
}
|
|
370
|
+
this.dictionaryExtended = new Dictionary(void 0, this.dictionary.size);
|
|
371
|
+
}
|
|
372
|
+
readByte() {
|
|
373
|
+
this.assertRead(1);
|
|
374
|
+
this._last = this.target[this.offset++];
|
|
375
|
+
return this._last;
|
|
376
|
+
}
|
|
377
|
+
readInt32(signed = true) {
|
|
378
|
+
this.assertRead(4);
|
|
379
|
+
this._last = this.target[this.offset++] | this.target[this.offset++] << 8 | this.target[this.offset++] << 16 | this.target[this.offset++] << 24;
|
|
380
|
+
if (!signed) {
|
|
381
|
+
this._last = this._last >>> 0;
|
|
382
|
+
}
|
|
383
|
+
return this._last;
|
|
384
|
+
}
|
|
385
|
+
readInt16(signed = true) {
|
|
386
|
+
this.assertRead(2);
|
|
387
|
+
this._last = this.target[this.offset++] | this.target[this.offset++] << 8;
|
|
388
|
+
if (signed) {
|
|
389
|
+
this._last = this._last << 16 >> 16;
|
|
390
|
+
}
|
|
391
|
+
return this._last;
|
|
392
|
+
}
|
|
393
|
+
readInt8(signed = true) {
|
|
394
|
+
this.assertRead(1);
|
|
395
|
+
this._last = this.target[this.offset++];
|
|
396
|
+
if (signed) {
|
|
397
|
+
this._last = this._last << 24 >> 24;
|
|
398
|
+
}
|
|
399
|
+
return this._last;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Reads a real floating point (4 bytes) value.
|
|
403
|
+
* @returns {number}
|
|
404
|
+
*/
|
|
405
|
+
readFloat() {
|
|
406
|
+
this.assertRead(4);
|
|
407
|
+
int32[0] = this.readInt32();
|
|
408
|
+
this._last = float32[0];
|
|
409
|
+
return this._last;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Reads a real floating point (8 bytes) value.
|
|
413
|
+
* @returns {BigInteger}
|
|
414
|
+
*/
|
|
415
|
+
readDouble() {
|
|
416
|
+
this.assertRead(8);
|
|
417
|
+
int32[0] = this.readInt32();
|
|
418
|
+
int32[1] = this.readInt32();
|
|
419
|
+
this._last = float64[0];
|
|
420
|
+
return this._last;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Read the given amount of bytes, or -1 to read all remaining.
|
|
424
|
+
* @param length {number}
|
|
425
|
+
*/
|
|
426
|
+
assertRead(length) {
|
|
427
|
+
if (this.length < this.offset + +length) {
|
|
428
|
+
const left = this.target.length - this.offset;
|
|
429
|
+
const result = this.target.subarray(this.offset, this.offset + left);
|
|
430
|
+
const err = new Error(
|
|
431
|
+
`No more data left to read (need ${length}, got ${left}: ${result}); last read ${this._last}`
|
|
432
|
+
);
|
|
433
|
+
err.incomplete = true;
|
|
434
|
+
Error.captureStackTrace(err, this.assertRead);
|
|
435
|
+
throw err;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
assertConstructor(constructorId) {
|
|
439
|
+
const byte = this.readByte();
|
|
440
|
+
if (byte !== constructorId) {
|
|
441
|
+
throw new Error(
|
|
442
|
+
`Invalid constructor code, expected = ${CORE_TYPES[constructorId]}, got = ${CORE_TYPES[byte] || byte}, offset = ${this.offset - 1}`
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Gets the byte array representing the current buffer as a whole.
|
|
448
|
+
*/
|
|
449
|
+
getBuffer() {
|
|
450
|
+
return this.target;
|
|
451
|
+
}
|
|
452
|
+
readNull() {
|
|
453
|
+
const value = this.readByte();
|
|
454
|
+
if (value === CORE_TYPES.Null) {
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
throw new Error(`Invalid boolean code ${value.toString(16)}`);
|
|
458
|
+
}
|
|
459
|
+
readLength() {
|
|
460
|
+
const firstByte = this.readByte();
|
|
461
|
+
if (firstByte === 254) {
|
|
462
|
+
return this.readByte() | this.readByte() << 8 | this.readByte() << 16;
|
|
463
|
+
}
|
|
464
|
+
return firstByte;
|
|
465
|
+
}
|
|
466
|
+
readAll() {
|
|
467
|
+
const result = [];
|
|
468
|
+
while (this.length > this.offset) {
|
|
469
|
+
result.push(this.readObject());
|
|
470
|
+
}
|
|
471
|
+
return result;
|
|
472
|
+
}
|
|
473
|
+
readBytes() {
|
|
474
|
+
const length = this.readLength();
|
|
475
|
+
this.assertRead(length);
|
|
476
|
+
const bytes = this.target.subarray(this.offset, this.offset + length);
|
|
477
|
+
this.offset += bytes.length;
|
|
478
|
+
this._last = bytes;
|
|
479
|
+
return bytes;
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Reads encoded string.
|
|
483
|
+
*/
|
|
484
|
+
readString() {
|
|
485
|
+
const length = this.readLength();
|
|
486
|
+
this.assertRead(length);
|
|
487
|
+
const result = utf8Read(this.target, length, this.offset);
|
|
488
|
+
this.offset += length;
|
|
489
|
+
this._last = result;
|
|
490
|
+
return result;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Reads a boolean value.
|
|
494
|
+
*/
|
|
495
|
+
readBool() {
|
|
496
|
+
const value = this.readByte();
|
|
497
|
+
if (value === CORE_TYPES.BoolTrue) {
|
|
498
|
+
return true;
|
|
499
|
+
} else if (value === CORE_TYPES.BoolFalse) {
|
|
500
|
+
return false;
|
|
501
|
+
} else {
|
|
502
|
+
throw new Error(`Invalid boolean code ${value.toString(16)}`);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Reads and converts Unix time
|
|
507
|
+
* into a Javascript {Date} object.
|
|
508
|
+
*/
|
|
509
|
+
readDate() {
|
|
510
|
+
const value = this.readDouble();
|
|
511
|
+
return new Date(value);
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Reads a object.
|
|
515
|
+
*/
|
|
516
|
+
readObject() {
|
|
517
|
+
if (this._repeat) {
|
|
518
|
+
if (this._repeat.pool > 0) {
|
|
519
|
+
--this._repeat.pool;
|
|
520
|
+
return this._repeat.value;
|
|
521
|
+
} else {
|
|
522
|
+
this._repeat = void 0;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
const constructorId = this.readByte();
|
|
526
|
+
const ext = this.extensions.get(constructorId);
|
|
527
|
+
let value;
|
|
528
|
+
if (ext) {
|
|
529
|
+
value = ext.decode.call(this);
|
|
530
|
+
} else {
|
|
531
|
+
value = this._lastObject = this.readCore(constructorId);
|
|
532
|
+
}
|
|
533
|
+
return value;
|
|
534
|
+
}
|
|
535
|
+
readObjectGzip() {
|
|
536
|
+
const bytes = this.readGzip();
|
|
537
|
+
const reader = new BinaryReader(bytes);
|
|
538
|
+
reader.extensions = this.extensions;
|
|
539
|
+
reader.dictionary = this.dictionary;
|
|
540
|
+
reader.dictionaryExtended = this.dictionaryExtended;
|
|
541
|
+
return reader.readObject();
|
|
542
|
+
}
|
|
543
|
+
readGzip() {
|
|
544
|
+
return pako.inflateRaw(this.readBytes());
|
|
545
|
+
}
|
|
546
|
+
readCore(constructorId) {
|
|
547
|
+
switch (constructorId) {
|
|
548
|
+
case CORE_TYPES.None:
|
|
549
|
+
return this.readObject();
|
|
550
|
+
case CORE_TYPES.GZIP:
|
|
551
|
+
return this.readObjectGzip();
|
|
552
|
+
case CORE_TYPES.BoolTrue:
|
|
553
|
+
return true;
|
|
554
|
+
case CORE_TYPES.BoolFalse:
|
|
555
|
+
return false;
|
|
556
|
+
case CORE_TYPES.Vector:
|
|
557
|
+
return this.readVector(false);
|
|
558
|
+
case CORE_TYPES.VectorDynamic:
|
|
559
|
+
return this.readVectorDynamic(false);
|
|
560
|
+
case CORE_TYPES.Null:
|
|
561
|
+
return null;
|
|
562
|
+
case CORE_TYPES.Binary:
|
|
563
|
+
return this.readBytes();
|
|
564
|
+
case CORE_TYPES.String:
|
|
565
|
+
return this.readString();
|
|
566
|
+
case CORE_TYPES.Date:
|
|
567
|
+
return this.readDate();
|
|
568
|
+
case CORE_TYPES.Int32:
|
|
569
|
+
return this.readInt32();
|
|
570
|
+
case CORE_TYPES.Int16:
|
|
571
|
+
return this.readInt16();
|
|
572
|
+
case CORE_TYPES.Int8:
|
|
573
|
+
return this.readInt8();
|
|
574
|
+
case CORE_TYPES.UInt32:
|
|
575
|
+
return this.readInt32(false);
|
|
576
|
+
case CORE_TYPES.UInt16:
|
|
577
|
+
return this.readInt16(false);
|
|
578
|
+
case CORE_TYPES.UInt8:
|
|
579
|
+
return this.readInt8(false);
|
|
580
|
+
case CORE_TYPES.Float:
|
|
581
|
+
return this.readFloat();
|
|
582
|
+
case CORE_TYPES.Double:
|
|
583
|
+
return this.readDouble();
|
|
584
|
+
case CORE_TYPES.Map:
|
|
585
|
+
return this.readMap(false);
|
|
586
|
+
case CORE_TYPES.DictIndex: {
|
|
587
|
+
const idx = this.readLength();
|
|
588
|
+
return this.getDictionaryValue(idx);
|
|
589
|
+
}
|
|
590
|
+
case CORE_TYPES.DictValue: {
|
|
591
|
+
const value = this.readString();
|
|
592
|
+
this.dictionaryExtended.maybeInsert(value);
|
|
593
|
+
return value;
|
|
594
|
+
}
|
|
595
|
+
case CORE_TYPES.Repeat: {
|
|
596
|
+
const size = this.readLength();
|
|
597
|
+
this._repeat = { pool: size - 1, value: this._lastObject };
|
|
598
|
+
return this._lastObject;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
throw new Error(
|
|
602
|
+
`Invalid constructor = ${CORE_TYPES[constructorId] || constructorId}, offset = ${this.offset - 1}`
|
|
603
|
+
);
|
|
604
|
+
}
|
|
605
|
+
getDictionaryValue(index) {
|
|
606
|
+
let value = null;
|
|
607
|
+
if (this.dictionary) {
|
|
608
|
+
value = this.dictionary.getValue(index);
|
|
609
|
+
}
|
|
610
|
+
if (value === void 0) {
|
|
611
|
+
value = this.dictionaryExtended.getValue(index);
|
|
612
|
+
}
|
|
613
|
+
return value;
|
|
614
|
+
}
|
|
615
|
+
readDictionary() {
|
|
616
|
+
const constructorId = this.readByte();
|
|
617
|
+
let key = null;
|
|
618
|
+
switch (constructorId) {
|
|
619
|
+
case CORE_TYPES.DictIndex: {
|
|
620
|
+
const idx = this.readLength();
|
|
621
|
+
key = this.getDictionaryValue(idx);
|
|
622
|
+
break;
|
|
623
|
+
}
|
|
624
|
+
case CORE_TYPES.DictValue: {
|
|
625
|
+
key = this.readString();
|
|
626
|
+
this.dictionaryExtended.maybeInsert(key);
|
|
627
|
+
break;
|
|
628
|
+
}
|
|
629
|
+
case CORE_TYPES.None: {
|
|
630
|
+
key = null;
|
|
631
|
+
break;
|
|
632
|
+
}
|
|
633
|
+
default: {
|
|
634
|
+
this.seek(-1);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
return key;
|
|
638
|
+
}
|
|
639
|
+
readMap(checkConstructor = true) {
|
|
640
|
+
if (checkConstructor) {
|
|
641
|
+
this.assertConstructor(CORE_TYPES.Map);
|
|
642
|
+
}
|
|
643
|
+
const temp = {};
|
|
644
|
+
let key = this.readDictionary();
|
|
645
|
+
while (key !== null) {
|
|
646
|
+
temp[key] = this.readObject();
|
|
647
|
+
key = this.readDictionary();
|
|
648
|
+
}
|
|
649
|
+
return temp;
|
|
650
|
+
}
|
|
651
|
+
decode(value) {
|
|
652
|
+
this.target = value;
|
|
653
|
+
this._last = void 0;
|
|
654
|
+
this._lastObject = void 0;
|
|
655
|
+
this._repeat = void 0;
|
|
656
|
+
this.offset = 0;
|
|
657
|
+
this.length = value.length;
|
|
658
|
+
return this.readObject();
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Reads a vector (a list) of objects.
|
|
662
|
+
*/
|
|
663
|
+
readVector(checkConstructor = true) {
|
|
664
|
+
if (checkConstructor) {
|
|
665
|
+
this.assertConstructor(CORE_TYPES.Vector);
|
|
666
|
+
}
|
|
667
|
+
const count = this.readLength();
|
|
668
|
+
const temp = [];
|
|
669
|
+
for (let i = 0; i < count; i++) {
|
|
670
|
+
temp.push(this.readObject());
|
|
671
|
+
}
|
|
672
|
+
return temp;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Reads a vector (a list) of objects.
|
|
676
|
+
*/
|
|
677
|
+
readVectorDynamic(checkConstructor = true) {
|
|
678
|
+
if (checkConstructor) {
|
|
679
|
+
this.assertConstructor(CORE_TYPES.VectorDynamic);
|
|
680
|
+
}
|
|
681
|
+
const temp = [];
|
|
682
|
+
let complete = false;
|
|
683
|
+
while (this.length > this.offset) {
|
|
684
|
+
const constructorId = this.readByte();
|
|
685
|
+
if (constructorId === CORE_TYPES.None) {
|
|
686
|
+
complete = true;
|
|
687
|
+
break;
|
|
688
|
+
}
|
|
689
|
+
const ext = this.extensions.get(constructorId);
|
|
690
|
+
let value;
|
|
691
|
+
if (ext) {
|
|
692
|
+
value = ext.decode.call(this);
|
|
693
|
+
} else {
|
|
694
|
+
value = this.readCore(constructorId);
|
|
695
|
+
}
|
|
696
|
+
temp.push(value);
|
|
697
|
+
}
|
|
698
|
+
if (!complete) {
|
|
699
|
+
const err = new Error(`DynamicVector incomplete.`);
|
|
700
|
+
err.incomplete = true;
|
|
701
|
+
Error.captureStackTrace(err, this.readDictionary);
|
|
702
|
+
throw err;
|
|
703
|
+
}
|
|
704
|
+
this._last = temp;
|
|
705
|
+
return temp;
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Tells the current position on the stream.
|
|
709
|
+
*/
|
|
710
|
+
tellPosition() {
|
|
711
|
+
return this.offset;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Sets the current position on the stream.
|
|
715
|
+
*/
|
|
716
|
+
setPosition(position) {
|
|
717
|
+
this.offset = position;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Seeks the stream position given an offset from the current position.
|
|
721
|
+
* The offset may be negative.
|
|
722
|
+
*/
|
|
723
|
+
seek(offset) {
|
|
724
|
+
this.offset += offset;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
const noop = Symbol();
|
|
729
|
+
const NO_CONSTRUCTOR = /* @__PURE__ */ new Set([
|
|
730
|
+
CORE_TYPES.BoolFalse,
|
|
731
|
+
CORE_TYPES.BoolTrue,
|
|
732
|
+
CORE_TYPES.Null
|
|
733
|
+
]);
|
|
734
|
+
const SUPPORT_COMPRESSION = /* @__PURE__ */ new Set([CORE_TYPES.String]);
|
|
735
|
+
class BinaryWriter {
|
|
736
|
+
withGzip;
|
|
737
|
+
target;
|
|
738
|
+
dictionary;
|
|
739
|
+
dictionaryExtended;
|
|
740
|
+
extensions;
|
|
741
|
+
_last = noop;
|
|
742
|
+
_repeat;
|
|
743
|
+
offset;
|
|
744
|
+
constructor(options) {
|
|
745
|
+
this.offset = 0;
|
|
746
|
+
this.extensions = /* @__PURE__ */ new Map();
|
|
747
|
+
this.withGzip = !!options && !!options.gzip;
|
|
748
|
+
this.target = byteArrayAllocate(8192);
|
|
749
|
+
if (options && options.extensions) {
|
|
750
|
+
options.extensions.forEach((ext) => {
|
|
751
|
+
this.extensions.set(ext.token, ext);
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
if (!options) {
|
|
755
|
+
this.dictionary = new Dictionary();
|
|
756
|
+
} else if (options.dictionary instanceof Dictionary) {
|
|
757
|
+
this.dictionary = options.dictionary;
|
|
758
|
+
} else if (Array.isArray(options.dictionary)) {
|
|
759
|
+
this.dictionary = new Dictionary(options.dictionary);
|
|
760
|
+
} else {
|
|
761
|
+
this.dictionary = new Dictionary();
|
|
762
|
+
}
|
|
763
|
+
this.dictionaryExtended = new Dictionary(void 0, this.dictionary.size);
|
|
764
|
+
}
|
|
765
|
+
allocate(size) {
|
|
766
|
+
const position = this.offset + size;
|
|
767
|
+
if (this.safeEnd < position) {
|
|
768
|
+
this.makeRoom(position);
|
|
769
|
+
}
|
|
770
|
+
return this;
|
|
771
|
+
}
|
|
772
|
+
makeRoom(end) {
|
|
773
|
+
let start = 0;
|
|
774
|
+
let newSize = 0;
|
|
775
|
+
let target = this.target;
|
|
776
|
+
if (end > 16777216) {
|
|
777
|
+
if (end - start > MAX_BUFFER_SIZE)
|
|
778
|
+
throw new Error(
|
|
779
|
+
"Packed buffer would be larger than maximum buffer size"
|
|
780
|
+
);
|
|
781
|
+
newSize = Math.min(
|
|
782
|
+
MAX_BUFFER_SIZE,
|
|
783
|
+
Math.round(
|
|
784
|
+
Math.max((end - start) * (end > 67108864 ? 1.25 : 2), 4194304) / 4096
|
|
785
|
+
) * 4096
|
|
786
|
+
);
|
|
787
|
+
} else {
|
|
788
|
+
newSize = (Math.max(end - start << 2, target.length - 1) >> 12) + 1 << 12;
|
|
789
|
+
}
|
|
790
|
+
const newBuffer = byteArrayAllocate(newSize);
|
|
791
|
+
end = Math.min(end, target.length);
|
|
792
|
+
newBuffer.set(target.slice(start, end));
|
|
793
|
+
this.target = newBuffer;
|
|
794
|
+
}
|
|
795
|
+
get safeEnd() {
|
|
796
|
+
return this.target.length - 10;
|
|
797
|
+
}
|
|
798
|
+
getBuffer() {
|
|
799
|
+
return this.target.subarray(0, this.offset);
|
|
800
|
+
}
|
|
801
|
+
writeByte(value) {
|
|
802
|
+
this.allocate(1);
|
|
803
|
+
this.target[this.offset++] = value;
|
|
804
|
+
return this;
|
|
805
|
+
}
|
|
806
|
+
writeBool(value) {
|
|
807
|
+
if (value) {
|
|
808
|
+
this.writeByte(CORE_TYPES.BoolTrue);
|
|
809
|
+
} else {
|
|
810
|
+
this.writeByte(CORE_TYPES.BoolFalse);
|
|
811
|
+
}
|
|
812
|
+
return this;
|
|
813
|
+
}
|
|
814
|
+
writeNull() {
|
|
815
|
+
this.writeByte(CORE_TYPES.Null);
|
|
816
|
+
return this;
|
|
817
|
+
}
|
|
818
|
+
writeInt32(value, signed = true) {
|
|
819
|
+
this.allocate(4);
|
|
820
|
+
if (signed) {
|
|
821
|
+
this.target[this.offset++] = value;
|
|
822
|
+
this.target[this.offset++] = value >> 8;
|
|
823
|
+
this.target[this.offset++] = value >> 16;
|
|
824
|
+
this.target[this.offset++] = value >> 24;
|
|
825
|
+
} else {
|
|
826
|
+
this.target[this.offset++] = value;
|
|
827
|
+
this.target[this.offset++] = value >> 8;
|
|
828
|
+
this.target[this.offset++] = value >> 16;
|
|
829
|
+
this.target[this.offset++] = value >> 24;
|
|
830
|
+
}
|
|
831
|
+
return this;
|
|
832
|
+
}
|
|
833
|
+
writeInt16(value, signed = true) {
|
|
834
|
+
this.allocate(2);
|
|
835
|
+
if (signed) {
|
|
836
|
+
this.target[this.offset++] = value;
|
|
837
|
+
this.target[this.offset++] = value >> 8;
|
|
838
|
+
} else {
|
|
839
|
+
this.target[this.offset++] = value;
|
|
840
|
+
this.target[this.offset++] = value >> 8;
|
|
841
|
+
}
|
|
842
|
+
return this;
|
|
843
|
+
}
|
|
844
|
+
writeInt8(value, signed = true) {
|
|
845
|
+
this.allocate(1);
|
|
846
|
+
this.target[this.offset++] = value;
|
|
847
|
+
return this;
|
|
848
|
+
}
|
|
849
|
+
writeFloat(value) {
|
|
850
|
+
this.allocate(4);
|
|
851
|
+
float32[0] = value;
|
|
852
|
+
this.writeInt32(int32[0]);
|
|
853
|
+
return this;
|
|
854
|
+
}
|
|
855
|
+
writeDouble(value) {
|
|
856
|
+
this.allocate(8);
|
|
857
|
+
float64[0] = value;
|
|
858
|
+
this.writeInt32(int32[0], false);
|
|
859
|
+
this.writeInt32(int32[1], false);
|
|
860
|
+
return this;
|
|
861
|
+
}
|
|
862
|
+
writeDate(value) {
|
|
863
|
+
let timestamp = 0;
|
|
864
|
+
if (value instanceof Date) {
|
|
865
|
+
timestamp = value.getTime();
|
|
866
|
+
} else if (typeof value === "number") {
|
|
867
|
+
timestamp = value;
|
|
868
|
+
}
|
|
869
|
+
this.writeDouble(timestamp);
|
|
870
|
+
return this;
|
|
871
|
+
}
|
|
872
|
+
writeString(value) {
|
|
873
|
+
const strLength = value.length;
|
|
874
|
+
let start = this.offset;
|
|
875
|
+
let require = strLength << 2;
|
|
876
|
+
if (require < 254) {
|
|
877
|
+
require += 1;
|
|
878
|
+
this.offset += 1;
|
|
879
|
+
} else {
|
|
880
|
+
require += 4;
|
|
881
|
+
this.offset += 4;
|
|
882
|
+
}
|
|
883
|
+
this.allocate(require);
|
|
884
|
+
const bytes = utf8Write(this.target, value, this.offset);
|
|
885
|
+
if (require < 254) {
|
|
886
|
+
this.target[start++] = bytes;
|
|
887
|
+
} else {
|
|
888
|
+
this.target[start++] = 254;
|
|
889
|
+
this.target[start++] = bytes % 256;
|
|
890
|
+
this.target[start++] = (bytes >> 8) % 256;
|
|
891
|
+
this.target[start++] = (bytes >> 16) % 256;
|
|
892
|
+
}
|
|
893
|
+
this.offset += bytes;
|
|
894
|
+
return this;
|
|
895
|
+
}
|
|
896
|
+
writeBytes(value) {
|
|
897
|
+
const length = value.length;
|
|
898
|
+
this.writeLength(length);
|
|
899
|
+
this.allocate(length);
|
|
900
|
+
this.target.set(value, this.offset);
|
|
901
|
+
this.offset += length;
|
|
902
|
+
return this;
|
|
903
|
+
}
|
|
904
|
+
writeLength(value) {
|
|
905
|
+
if (value < 254) {
|
|
906
|
+
this.allocate(1);
|
|
907
|
+
this.target[this.offset++] = value;
|
|
908
|
+
} else {
|
|
909
|
+
this.allocate(4);
|
|
910
|
+
this.target[this.offset++] = 254;
|
|
911
|
+
this.target[this.offset++] = value % 256;
|
|
912
|
+
this.target[this.offset++] = (value >> 8) % 256;
|
|
913
|
+
this.target[this.offset++] = (value >> 16) % 256;
|
|
914
|
+
}
|
|
915
|
+
return this;
|
|
916
|
+
}
|
|
917
|
+
writeVector(value) {
|
|
918
|
+
const length = value.length;
|
|
919
|
+
this.writeLength(length);
|
|
920
|
+
for (let i = 0; i < length; i++) {
|
|
921
|
+
if (value[i] === void 0) {
|
|
922
|
+
this.writeNull();
|
|
923
|
+
} else {
|
|
924
|
+
this.writeObject(value[i]);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
writeMap(object) {
|
|
930
|
+
for (const key in object) {
|
|
931
|
+
if (object[key] === void 0) continue;
|
|
932
|
+
this._last = noop;
|
|
933
|
+
this.wireDictionary(key);
|
|
934
|
+
this.writeObject(object[key]);
|
|
935
|
+
}
|
|
936
|
+
this.writeByte(CORE_TYPES.None);
|
|
937
|
+
return this;
|
|
938
|
+
}
|
|
939
|
+
wireDictionary(value) {
|
|
940
|
+
let idx = null;
|
|
941
|
+
if (this.dictionary) {
|
|
942
|
+
idx = this.dictionary.getIndex(value);
|
|
943
|
+
}
|
|
944
|
+
if (idx === null) {
|
|
945
|
+
idx = this.dictionaryExtended.getIndex(value);
|
|
946
|
+
}
|
|
947
|
+
if (idx === null) {
|
|
948
|
+
this.dictionaryExtended.maybeInsert(value);
|
|
949
|
+
this.writeCore(CORE_TYPES.DictValue, value);
|
|
950
|
+
} else {
|
|
951
|
+
this.writeCore(CORE_TYPES.DictIndex, idx);
|
|
952
|
+
}
|
|
953
|
+
return this;
|
|
954
|
+
}
|
|
955
|
+
writeGzip(value) {
|
|
956
|
+
const compressed = pako.deflateRaw(value, { level: 9 });
|
|
957
|
+
this.writeBytes(compressed);
|
|
958
|
+
return this;
|
|
959
|
+
}
|
|
960
|
+
encode(value) {
|
|
961
|
+
this.offset = 0;
|
|
962
|
+
this._last = noop;
|
|
963
|
+
this._repeat = void 0;
|
|
964
|
+
this.target = byteArrayAllocate(256);
|
|
965
|
+
this.writeObject(value);
|
|
966
|
+
return this.getBuffer();
|
|
967
|
+
}
|
|
968
|
+
startDynamicVector() {
|
|
969
|
+
this.writeByte(CORE_TYPES.VectorDynamic);
|
|
970
|
+
return this;
|
|
971
|
+
}
|
|
972
|
+
endDynamicVector() {
|
|
973
|
+
this.writeByte(CORE_TYPES.None);
|
|
974
|
+
return this;
|
|
975
|
+
}
|
|
976
|
+
_writeCustom(value) {
|
|
977
|
+
const start = this.offset;
|
|
978
|
+
this.allocate(1);
|
|
979
|
+
this.offset++;
|
|
980
|
+
let edgeExt;
|
|
981
|
+
for (const ext of this.extensions.values()) {
|
|
982
|
+
if (ext.token === -1) {
|
|
983
|
+
edgeExt = ext;
|
|
984
|
+
continue;
|
|
985
|
+
}
|
|
986
|
+
ext.encode.call(this, value);
|
|
987
|
+
const processed = start + 1 < this.offset;
|
|
988
|
+
if (processed) {
|
|
989
|
+
const end = this.offset;
|
|
990
|
+
this.offset = start;
|
|
991
|
+
this.writeByte(ext.token);
|
|
992
|
+
this.offset = end;
|
|
993
|
+
return true;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
this.offset = start;
|
|
997
|
+
if (edgeExt) {
|
|
998
|
+
edgeExt.encode.call(this, value);
|
|
999
|
+
return start < this.offset;
|
|
1000
|
+
}
|
|
1001
|
+
return false;
|
|
1002
|
+
}
|
|
1003
|
+
writeObject(value) {
|
|
1004
|
+
if (value === void 0) return this;
|
|
1005
|
+
const constructorId = coreType(value);
|
|
1006
|
+
if (constructorId === CORE_TYPES.None) {
|
|
1007
|
+
if (this._writeCustom(value)) {
|
|
1008
|
+
return this;
|
|
1009
|
+
}
|
|
1010
|
+
throw new TypeError(`Invalid core type of ${value}`);
|
|
1011
|
+
}
|
|
1012
|
+
if (this._last === value) {
|
|
1013
|
+
this.writeRepeat();
|
|
1014
|
+
} else {
|
|
1015
|
+
this._last = value;
|
|
1016
|
+
this._repeat = void 0;
|
|
1017
|
+
this.writeCore(constructorId, value);
|
|
1018
|
+
}
|
|
1019
|
+
return this;
|
|
1020
|
+
}
|
|
1021
|
+
writeObjectGzip(value) {
|
|
1022
|
+
const writer = new BinaryWriter();
|
|
1023
|
+
writer.extensions = this.extensions;
|
|
1024
|
+
writer.dictionary = this.dictionary;
|
|
1025
|
+
writer.dictionaryExtended = this.dictionaryExtended;
|
|
1026
|
+
writer.writeObject(value);
|
|
1027
|
+
this.writeCore(CORE_TYPES.GZIP, writer.getBuffer());
|
|
1028
|
+
return this;
|
|
1029
|
+
}
|
|
1030
|
+
writeCore(constructorId, value) {
|
|
1031
|
+
if (this.withGzip && SUPPORT_COMPRESSION.has(constructorId)) {
|
|
1032
|
+
this.writeObjectGzip(value);
|
|
1033
|
+
return this;
|
|
1034
|
+
} else if (!NO_CONSTRUCTOR.has(constructorId)) {
|
|
1035
|
+
this.writeByte(constructorId);
|
|
1036
|
+
}
|
|
1037
|
+
switch (constructorId) {
|
|
1038
|
+
case CORE_TYPES.GZIP: {
|
|
1039
|
+
return this.writeGzip(value);
|
|
1040
|
+
}
|
|
1041
|
+
case CORE_TYPES.DictIndex: {
|
|
1042
|
+
return this.writeLength(value);
|
|
1043
|
+
}
|
|
1044
|
+
case CORE_TYPES.DictValue: {
|
|
1045
|
+
return this.writeString(value);
|
|
1046
|
+
}
|
|
1047
|
+
case CORE_TYPES.BoolFalse: {
|
|
1048
|
+
return this.writeBool(value);
|
|
1049
|
+
}
|
|
1050
|
+
case CORE_TYPES.BoolTrue: {
|
|
1051
|
+
return this.writeBool(value);
|
|
1052
|
+
}
|
|
1053
|
+
case CORE_TYPES.Date: {
|
|
1054
|
+
return this.writeDate(value);
|
|
1055
|
+
}
|
|
1056
|
+
case CORE_TYPES.Int32: {
|
|
1057
|
+
return this.writeInt32(value);
|
|
1058
|
+
}
|
|
1059
|
+
case CORE_TYPES.Int16: {
|
|
1060
|
+
return this.writeInt16(value);
|
|
1061
|
+
}
|
|
1062
|
+
case CORE_TYPES.Int8: {
|
|
1063
|
+
return this.writeInt8(value);
|
|
1064
|
+
}
|
|
1065
|
+
case CORE_TYPES.UInt32: {
|
|
1066
|
+
return this.writeInt32(value, false);
|
|
1067
|
+
}
|
|
1068
|
+
case CORE_TYPES.UInt16: {
|
|
1069
|
+
return this.writeInt16(value, false);
|
|
1070
|
+
}
|
|
1071
|
+
case CORE_TYPES.UInt8: {
|
|
1072
|
+
return this.writeInt8(value, false);
|
|
1073
|
+
}
|
|
1074
|
+
case CORE_TYPES.Double: {
|
|
1075
|
+
return this.writeDouble(value);
|
|
1076
|
+
}
|
|
1077
|
+
case CORE_TYPES.Float: {
|
|
1078
|
+
return this.writeFloat(value);
|
|
1079
|
+
}
|
|
1080
|
+
case CORE_TYPES.Null: {
|
|
1081
|
+
return this.writeNull();
|
|
1082
|
+
}
|
|
1083
|
+
case CORE_TYPES.String: {
|
|
1084
|
+
if (value.length <= 16) {
|
|
1085
|
+
this.offset--;
|
|
1086
|
+
return this.wireDictionary(value);
|
|
1087
|
+
}
|
|
1088
|
+
return this.writeString(value);
|
|
1089
|
+
}
|
|
1090
|
+
case CORE_TYPES.Vector: {
|
|
1091
|
+
return this.writeVector(value);
|
|
1092
|
+
}
|
|
1093
|
+
case CORE_TYPES.Map: {
|
|
1094
|
+
return this.writeMap(value);
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
return this;
|
|
1098
|
+
}
|
|
1099
|
+
writeRepeat() {
|
|
1100
|
+
if (!this._repeat) {
|
|
1101
|
+
this.writeByte(CORE_TYPES.Repeat);
|
|
1102
|
+
this._repeat = { count: 0, offset: this.offset };
|
|
1103
|
+
}
|
|
1104
|
+
this.offset = this._repeat.offset;
|
|
1105
|
+
this._repeat.count++;
|
|
1106
|
+
this.writeLength(this._repeat.count);
|
|
1107
|
+
return this;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
export { BinaryWriter as B, CORE_TYPES as C, MAX_BUFFER_SIZE as M, BinaryReader as a, createDictionary as c };
|
|
1112
|
+
//# sourceMappingURL=tl-pack.DnNse4P6.mjs.map
|