@andrew_l/tl-pack 0.1.9 → 0.1.82
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/README.md +5 -5
- package/dist/BinaryReader.d.ts +109 -0
- package/dist/BinaryReader.js +412 -0
- package/dist/BinaryWriter.d.ts +47 -0
- package/dist/BinaryWriter.js +385 -0
- package/dist/constants.d.ts +26 -0
- package/dist/constants.js +27 -0
- package/dist/dictionary.d.ts +17 -0
- package/dist/dictionary.js +51 -0
- package/dist/extension.d.ts +13 -0
- package/dist/extension.js +13 -0
- package/dist/helpers.d.ts +11 -0
- package/dist/helpers.js +321 -0
- package/dist/index.d.ts +5 -28
- package/dist/index.js +5 -0
- package/dist/stream.d.ts +8 -8
- package/dist/stream.js +67 -0
- package/package.json +53 -42
- package/LICENSE +0 -21
- package/dist/index.cjs +0 -36
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -28
- package/dist/index.d.mts +0 -28
- package/dist/index.mjs +0 -28
- package/dist/index.mjs.map +0 -1
- package/dist/shared/tl-pack.CdcwN4dG.d.cts +0 -195
- package/dist/shared/tl-pack.CdcwN4dG.d.mts +0 -195
- package/dist/shared/tl-pack.CdcwN4dG.d.ts +0 -195
- package/dist/shared/tl-pack.DnNse4P6.mjs +0 -1112
- package/dist/shared/tl-pack.DnNse4P6.mjs.map +0 -1
- package/dist/shared/tl-pack.VfbARqWq.cjs +0 -1122
- package/dist/shared/tl-pack.VfbARqWq.cjs.map +0 -1
- package/dist/stream.cjs +0 -72
- package/dist/stream.cjs.map +0 -1
- package/dist/stream.d.cts +0 -20
- package/dist/stream.d.mts +0 -20
- package/dist/stream.mjs +0 -69
- package/dist/stream.mjs.map +0 -1
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import pako from 'pako';
|
|
2
|
+
import { CORE_TYPES, MAX_BUFFER_SIZE } from './constants.js';
|
|
3
|
+
import { Dictionary } from './dictionary.js';
|
|
4
|
+
import { byteArrayAllocate, coreType, float32, float64, int32, utf8Write, } from './helpers.js';
|
|
5
|
+
const noop = Symbol();
|
|
6
|
+
const NO_CONSTRUCTOR = new Set([CORE_TYPES.BoolFalse, CORE_TYPES.BoolTrue, CORE_TYPES.Null]);
|
|
7
|
+
const SUPPORT_COMPRESSION = new Set([CORE_TYPES.String]);
|
|
8
|
+
export class BinaryWriter {
|
|
9
|
+
withGzip;
|
|
10
|
+
target;
|
|
11
|
+
dictionary;
|
|
12
|
+
dictionaryExtended;
|
|
13
|
+
extensions;
|
|
14
|
+
_last = noop;
|
|
15
|
+
_repeat;
|
|
16
|
+
offset;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
this.offset = 0;
|
|
19
|
+
this.extensions = new Map();
|
|
20
|
+
this.withGzip = !!options && !!options.gzip;
|
|
21
|
+
this.target = byteArrayAllocate(8192);
|
|
22
|
+
if (options && options.extensions) {
|
|
23
|
+
options.extensions.forEach((ext) => {
|
|
24
|
+
this.extensions.set(ext.token, ext);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (!options) {
|
|
28
|
+
this.dictionary = new Dictionary();
|
|
29
|
+
}
|
|
30
|
+
else if (options.dictionary instanceof Dictionary) {
|
|
31
|
+
this.dictionary = options.dictionary;
|
|
32
|
+
}
|
|
33
|
+
else if (Array.isArray(options.dictionary)) {
|
|
34
|
+
this.dictionary = new Dictionary(options.dictionary);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
this.dictionary = new Dictionary();
|
|
38
|
+
}
|
|
39
|
+
this.dictionaryExtended = new Dictionary(undefined, this.dictionary.size);
|
|
40
|
+
}
|
|
41
|
+
allocate(size) {
|
|
42
|
+
const position = this.offset + size;
|
|
43
|
+
if (this.safeEnd < position) {
|
|
44
|
+
this.makeRoom(position);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
makeRoom(end) {
|
|
48
|
+
let start = 0;
|
|
49
|
+
let newSize = 0;
|
|
50
|
+
let target = this.target;
|
|
51
|
+
if (end > 0x1000000) {
|
|
52
|
+
// special handling for really large buffers
|
|
53
|
+
if (end - start > MAX_BUFFER_SIZE)
|
|
54
|
+
throw new Error('Packed buffer would be larger than maximum buffer size');
|
|
55
|
+
newSize = Math.min(MAX_BUFFER_SIZE, Math.round(Math.max((end - start) * (end > 0x4000000 ? 1.25 : 2), 0x400000) / 0x1000) *
|
|
56
|
+
0x1000);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// faster handling for smaller buffers
|
|
60
|
+
newSize = ((Math.max((end - start) << 2, target.length - 1) >> 12) + 1) << 12;
|
|
61
|
+
}
|
|
62
|
+
const newBuffer = byteArrayAllocate(newSize);
|
|
63
|
+
end = Math.min(end, target.length);
|
|
64
|
+
if ('copy' in target) {
|
|
65
|
+
target.copy(newBuffer, 0, start, end);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
newBuffer.set(target.slice(start, end));
|
|
69
|
+
}
|
|
70
|
+
this.target = newBuffer;
|
|
71
|
+
}
|
|
72
|
+
get safeEnd() {
|
|
73
|
+
return this.target.length - 10;
|
|
74
|
+
}
|
|
75
|
+
getBuffer() {
|
|
76
|
+
return this.target.subarray(0, this.offset);
|
|
77
|
+
}
|
|
78
|
+
writeByte(value) {
|
|
79
|
+
this.allocate(1);
|
|
80
|
+
this.target[this.offset++] = value;
|
|
81
|
+
}
|
|
82
|
+
writeBool(value) {
|
|
83
|
+
if (value) {
|
|
84
|
+
this.writeByte(CORE_TYPES.BoolTrue);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
this.writeByte(CORE_TYPES.BoolFalse);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
writeNull() {
|
|
91
|
+
this.writeByte(CORE_TYPES.Null);
|
|
92
|
+
}
|
|
93
|
+
writeInt32(value, signed = true) {
|
|
94
|
+
this.allocate(4);
|
|
95
|
+
if (signed) {
|
|
96
|
+
this.target[this.offset++] = value;
|
|
97
|
+
this.target[this.offset++] = value >> 8;
|
|
98
|
+
this.target[this.offset++] = value >> 16;
|
|
99
|
+
this.target[this.offset++] = value >> 24;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
this.target[this.offset++] = value;
|
|
103
|
+
this.target[this.offset++] = value >> 8;
|
|
104
|
+
this.target[this.offset++] = value >> 16;
|
|
105
|
+
this.target[this.offset++] = value >> 24;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
writeInt16(value, signed = true) {
|
|
109
|
+
this.allocate(2);
|
|
110
|
+
if (signed) {
|
|
111
|
+
this.target[this.offset++] = value;
|
|
112
|
+
this.target[this.offset++] = value >> 8;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
this.target[this.offset++] = value;
|
|
116
|
+
this.target[this.offset++] = value >> 8;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
writeInt8(value, signed = true) {
|
|
120
|
+
this.allocate(1);
|
|
121
|
+
this.target[this.offset++] = value;
|
|
122
|
+
}
|
|
123
|
+
writeFloat(value) {
|
|
124
|
+
this.allocate(4);
|
|
125
|
+
float32[0] = value;
|
|
126
|
+
this.writeInt32(int32[0]);
|
|
127
|
+
}
|
|
128
|
+
writeDouble(value) {
|
|
129
|
+
this.allocate(8);
|
|
130
|
+
float64[0] = value;
|
|
131
|
+
this.writeInt32(int32[0], false);
|
|
132
|
+
this.writeInt32(int32[1], false);
|
|
133
|
+
}
|
|
134
|
+
writeDate(value) {
|
|
135
|
+
let timestamp = 0;
|
|
136
|
+
if (value instanceof Date) {
|
|
137
|
+
timestamp = value.getTime();
|
|
138
|
+
}
|
|
139
|
+
else if (typeof value === 'number') {
|
|
140
|
+
timestamp = value;
|
|
141
|
+
}
|
|
142
|
+
this.writeDouble(timestamp);
|
|
143
|
+
}
|
|
144
|
+
writeString(value) {
|
|
145
|
+
// const compressed = pako.deflateRaw(value, { level: 9 });
|
|
146
|
+
// this.writeBytes(compressed);
|
|
147
|
+
const strLength = value.length;
|
|
148
|
+
let start = this.offset;
|
|
149
|
+
let require = strLength << 2;
|
|
150
|
+
if (require < 254) {
|
|
151
|
+
require += 1;
|
|
152
|
+
this.offset += 1;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
require += 4;
|
|
156
|
+
this.offset += 4;
|
|
157
|
+
}
|
|
158
|
+
this.allocate(require);
|
|
159
|
+
const bytes = utf8Write(this.target, value, this.offset);
|
|
160
|
+
if (require < 254) {
|
|
161
|
+
this.target[start++] = bytes;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
this.target[start++] = 254;
|
|
165
|
+
this.target[start++] = bytes % 256;
|
|
166
|
+
this.target[start++] = (bytes >> 8) % 256;
|
|
167
|
+
this.target[start++] = (bytes >> 16) % 256;
|
|
168
|
+
}
|
|
169
|
+
this.offset += bytes;
|
|
170
|
+
}
|
|
171
|
+
writeBytes(value) {
|
|
172
|
+
const length = value.length;
|
|
173
|
+
this.writeLength(length);
|
|
174
|
+
this.allocate(length);
|
|
175
|
+
this.target.set(value, this.offset);
|
|
176
|
+
this.offset += length;
|
|
177
|
+
}
|
|
178
|
+
writeLength(value) {
|
|
179
|
+
if (value < 254) {
|
|
180
|
+
this.allocate(1);
|
|
181
|
+
this.target[this.offset++] = value;
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
this.allocate(4);
|
|
185
|
+
this.target[this.offset++] = 254;
|
|
186
|
+
this.target[this.offset++] = value % 256;
|
|
187
|
+
this.target[this.offset++] = (value >> 8) % 256;
|
|
188
|
+
this.target[this.offset++] = (value >> 16) % 256;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
writeVector(value) {
|
|
192
|
+
const length = value.length;
|
|
193
|
+
this.writeLength(length);
|
|
194
|
+
for (let i = 0; i < length; i++) {
|
|
195
|
+
if (value[i] === undefined) {
|
|
196
|
+
this.writeNull();
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
this.writeObject(value[i]);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
writeMap(object) {
|
|
204
|
+
for (const key in object) {
|
|
205
|
+
if (object[key] === undefined)
|
|
206
|
+
continue;
|
|
207
|
+
this._last = noop;
|
|
208
|
+
this.wireDictionary(key);
|
|
209
|
+
this.writeObject(object[key]);
|
|
210
|
+
}
|
|
211
|
+
this.writeByte(CORE_TYPES.None);
|
|
212
|
+
}
|
|
213
|
+
wireDictionary(value) {
|
|
214
|
+
let idx;
|
|
215
|
+
if (this.dictionary) {
|
|
216
|
+
idx = this.dictionary.getIndex(value);
|
|
217
|
+
}
|
|
218
|
+
if (idx === undefined) {
|
|
219
|
+
idx = this.dictionaryExtended.getIndex(value);
|
|
220
|
+
}
|
|
221
|
+
if (idx === undefined) {
|
|
222
|
+
this.dictionaryExtended.maybeInsert(value);
|
|
223
|
+
this.writeCore(CORE_TYPES.DictValue, value);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
this.writeCore(CORE_TYPES.DictIndex, idx);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
writeGzip(value) {
|
|
230
|
+
const compressed = pako.deflateRaw(value, { level: 9 });
|
|
231
|
+
this.writeBytes(compressed);
|
|
232
|
+
}
|
|
233
|
+
encode(value) {
|
|
234
|
+
this.offset = 0;
|
|
235
|
+
this._last = noop;
|
|
236
|
+
this._repeat = undefined;
|
|
237
|
+
this.target = byteArrayAllocate(256);
|
|
238
|
+
this.writeObject(value);
|
|
239
|
+
return this.getBuffer();
|
|
240
|
+
}
|
|
241
|
+
startDynamicVector() {
|
|
242
|
+
this.writeByte(CORE_TYPES.VectorDynamic);
|
|
243
|
+
}
|
|
244
|
+
endDynamicVector() {
|
|
245
|
+
this.writeByte(CORE_TYPES.None);
|
|
246
|
+
}
|
|
247
|
+
_writeCustom(value) {
|
|
248
|
+
const start = this.offset;
|
|
249
|
+
this.allocate(1);
|
|
250
|
+
this.offset++;
|
|
251
|
+
let edgeExt;
|
|
252
|
+
for (const ext of this.extensions.values()) {
|
|
253
|
+
if (ext.token === -1) {
|
|
254
|
+
edgeExt = ext;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
ext.encode.call(this, value);
|
|
258
|
+
const processed = start + 1 < this.offset;
|
|
259
|
+
if (processed) {
|
|
260
|
+
const end = this.offset;
|
|
261
|
+
this.offset = start;
|
|
262
|
+
this.writeByte(ext.token);
|
|
263
|
+
this.offset = end;
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
this.offset = start;
|
|
268
|
+
if (edgeExt) {
|
|
269
|
+
edgeExt.encode.call(this, value);
|
|
270
|
+
return start < this.offset;
|
|
271
|
+
}
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
writeObject(value) {
|
|
275
|
+
if (value === undefined)
|
|
276
|
+
return;
|
|
277
|
+
const constructorId = coreType(value);
|
|
278
|
+
// console.log('write', {
|
|
279
|
+
// offset: this.offset,
|
|
280
|
+
// constructorId: CORE_TYPES[constructorId],
|
|
281
|
+
// value: String(value),
|
|
282
|
+
// });
|
|
283
|
+
if (constructorId === CORE_TYPES.None) {
|
|
284
|
+
if (this._writeCustom(value)) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
throw new TypeError(`Invalid core type of ${value}`);
|
|
288
|
+
}
|
|
289
|
+
if (this._last === value) {
|
|
290
|
+
this.writeRepeat();
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
this._last = value;
|
|
294
|
+
this._repeat = undefined;
|
|
295
|
+
this.writeCore(constructorId, value);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
writeObjectGzip(value) {
|
|
299
|
+
const writer = new BinaryWriter();
|
|
300
|
+
writer.extensions = this.extensions;
|
|
301
|
+
writer.dictionary = this.dictionary;
|
|
302
|
+
writer.dictionaryExtended = this.dictionaryExtended;
|
|
303
|
+
writer.writeObject(value);
|
|
304
|
+
this.writeCore(CORE_TYPES.GZIP, writer.getBuffer());
|
|
305
|
+
}
|
|
306
|
+
writeCore(constructorId, value) {
|
|
307
|
+
if (this.withGzip && SUPPORT_COMPRESSION.has(constructorId)) {
|
|
308
|
+
this.writeObjectGzip(value);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
else if (!NO_CONSTRUCTOR.has(constructorId)) {
|
|
312
|
+
this.writeByte(constructorId);
|
|
313
|
+
}
|
|
314
|
+
switch (constructorId) {
|
|
315
|
+
case CORE_TYPES.GZIP: {
|
|
316
|
+
return this.writeGzip(value);
|
|
317
|
+
}
|
|
318
|
+
case CORE_TYPES.DictIndex: {
|
|
319
|
+
return this.writeLength(value);
|
|
320
|
+
}
|
|
321
|
+
case CORE_TYPES.DictValue: {
|
|
322
|
+
return this.writeString(value);
|
|
323
|
+
}
|
|
324
|
+
case CORE_TYPES.BoolFalse: {
|
|
325
|
+
return this.writeBool(value);
|
|
326
|
+
}
|
|
327
|
+
case CORE_TYPES.BoolTrue: {
|
|
328
|
+
return this.writeBool(value);
|
|
329
|
+
}
|
|
330
|
+
case CORE_TYPES.Date: {
|
|
331
|
+
return this.writeDate(value);
|
|
332
|
+
}
|
|
333
|
+
case CORE_TYPES.Int32: {
|
|
334
|
+
return this.writeInt32(value);
|
|
335
|
+
}
|
|
336
|
+
case CORE_TYPES.Int16: {
|
|
337
|
+
return this.writeInt16(value);
|
|
338
|
+
}
|
|
339
|
+
case CORE_TYPES.Int8: {
|
|
340
|
+
return this.writeInt8(value);
|
|
341
|
+
}
|
|
342
|
+
case CORE_TYPES.UInt32: {
|
|
343
|
+
return this.writeInt32(value, false);
|
|
344
|
+
}
|
|
345
|
+
case CORE_TYPES.UInt16: {
|
|
346
|
+
return this.writeInt16(value, false);
|
|
347
|
+
}
|
|
348
|
+
case CORE_TYPES.UInt8: {
|
|
349
|
+
return this.writeInt8(value, false);
|
|
350
|
+
}
|
|
351
|
+
case CORE_TYPES.Double: {
|
|
352
|
+
return this.writeDouble(value);
|
|
353
|
+
}
|
|
354
|
+
case CORE_TYPES.Float: {
|
|
355
|
+
return this.writeFloat(value);
|
|
356
|
+
}
|
|
357
|
+
case CORE_TYPES.Null: {
|
|
358
|
+
return this.writeNull();
|
|
359
|
+
}
|
|
360
|
+
case CORE_TYPES.String: {
|
|
361
|
+
// write short strings into dictionary
|
|
362
|
+
if (value.length <= 0x10) {
|
|
363
|
+
this.offset--;
|
|
364
|
+
return this.wireDictionary(value);
|
|
365
|
+
}
|
|
366
|
+
return this.writeString(value);
|
|
367
|
+
}
|
|
368
|
+
case CORE_TYPES.Vector: {
|
|
369
|
+
return this.writeVector(value);
|
|
370
|
+
}
|
|
371
|
+
case CORE_TYPES.Map: {
|
|
372
|
+
return this.writeMap(value);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
writeRepeat() {
|
|
377
|
+
if (!this._repeat) {
|
|
378
|
+
this.writeByte(CORE_TYPES.Repeat);
|
|
379
|
+
this._repeat = { count: 0, offset: this.offset };
|
|
380
|
+
}
|
|
381
|
+
this.offset = this._repeat.offset;
|
|
382
|
+
this._repeat.count++;
|
|
383
|
+
this.writeLength(this._repeat.count);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare enum CORE_TYPES {
|
|
2
|
+
None = 0,
|
|
3
|
+
Binary = 1,
|
|
4
|
+
BoolFalse = 2,
|
|
5
|
+
BoolTrue = 3,
|
|
6
|
+
Null = 4,
|
|
7
|
+
Date = 5,
|
|
8
|
+
Vector = 6,
|
|
9
|
+
VectorDynamic = 7,
|
|
10
|
+
Int32 = 8,
|
|
11
|
+
Int16 = 9,
|
|
12
|
+
Int8 = 10,
|
|
13
|
+
UInt32 = 11,
|
|
14
|
+
UInt16 = 12,
|
|
15
|
+
UInt8 = 13,
|
|
16
|
+
Float = 14,
|
|
17
|
+
Double = 15,
|
|
18
|
+
Map = 16,
|
|
19
|
+
DictValue = 17,
|
|
20
|
+
DictIndex = 18,
|
|
21
|
+
String = 19,
|
|
22
|
+
Repeat = 20,
|
|
23
|
+
GZIP = 25
|
|
24
|
+
}
|
|
25
|
+
export declare const HAS_NODE_BUFFER: boolean;
|
|
26
|
+
export declare const MAX_BUFFER_SIZE: number;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export var CORE_TYPES;
|
|
2
|
+
(function (CORE_TYPES) {
|
|
3
|
+
CORE_TYPES[CORE_TYPES["None"] = 0] = "None";
|
|
4
|
+
CORE_TYPES[CORE_TYPES["Binary"] = 1] = "Binary";
|
|
5
|
+
CORE_TYPES[CORE_TYPES["BoolFalse"] = 2] = "BoolFalse";
|
|
6
|
+
CORE_TYPES[CORE_TYPES["BoolTrue"] = 3] = "BoolTrue";
|
|
7
|
+
CORE_TYPES[CORE_TYPES["Null"] = 4] = "Null";
|
|
8
|
+
CORE_TYPES[CORE_TYPES["Date"] = 5] = "Date";
|
|
9
|
+
CORE_TYPES[CORE_TYPES["Vector"] = 6] = "Vector";
|
|
10
|
+
CORE_TYPES[CORE_TYPES["VectorDynamic"] = 7] = "VectorDynamic";
|
|
11
|
+
CORE_TYPES[CORE_TYPES["Int32"] = 8] = "Int32";
|
|
12
|
+
CORE_TYPES[CORE_TYPES["Int16"] = 9] = "Int16";
|
|
13
|
+
CORE_TYPES[CORE_TYPES["Int8"] = 10] = "Int8";
|
|
14
|
+
CORE_TYPES[CORE_TYPES["UInt32"] = 11] = "UInt32";
|
|
15
|
+
CORE_TYPES[CORE_TYPES["UInt16"] = 12] = "UInt16";
|
|
16
|
+
CORE_TYPES[CORE_TYPES["UInt8"] = 13] = "UInt8";
|
|
17
|
+
CORE_TYPES[CORE_TYPES["Float"] = 14] = "Float";
|
|
18
|
+
CORE_TYPES[CORE_TYPES["Double"] = 15] = "Double";
|
|
19
|
+
CORE_TYPES[CORE_TYPES["Map"] = 16] = "Map";
|
|
20
|
+
CORE_TYPES[CORE_TYPES["DictValue"] = 17] = "DictValue";
|
|
21
|
+
CORE_TYPES[CORE_TYPES["DictIndex"] = 18] = "DictIndex";
|
|
22
|
+
CORE_TYPES[CORE_TYPES["String"] = 19] = "String";
|
|
23
|
+
CORE_TYPES[CORE_TYPES["Repeat"] = 20] = "Repeat";
|
|
24
|
+
CORE_TYPES[CORE_TYPES["GZIP"] = 25] = "GZIP";
|
|
25
|
+
})(CORE_TYPES || (CORE_TYPES = {}));
|
|
26
|
+
export const HAS_NODE_BUFFER = typeof Buffer !== 'undefined';
|
|
27
|
+
export const MAX_BUFFER_SIZE = HAS_NODE_BUFFER ? 0x100000000 : 0x7fd00000;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare function createDictionary(values?: string[]): Dictionary;
|
|
2
|
+
export declare class Dictionary {
|
|
3
|
+
private _count;
|
|
4
|
+
private _map;
|
|
5
|
+
private _index;
|
|
6
|
+
private _offset;
|
|
7
|
+
constructor(values?: string[], offset?: number);
|
|
8
|
+
get size(): number;
|
|
9
|
+
/**
|
|
10
|
+
* Returns inserted index or nothing
|
|
11
|
+
*/
|
|
12
|
+
maybeInsert(word: string): number | undefined;
|
|
13
|
+
getValue(index: number): string | undefined;
|
|
14
|
+
getIndex(value: string): number | undefined;
|
|
15
|
+
hasValue(value: string): boolean;
|
|
16
|
+
hasIndex(index: number): boolean;
|
|
17
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export function createDictionary(values) {
|
|
2
|
+
return new Dictionary(values);
|
|
3
|
+
}
|
|
4
|
+
export class Dictionary {
|
|
5
|
+
_count = 0;
|
|
6
|
+
_map;
|
|
7
|
+
_index;
|
|
8
|
+
_offset;
|
|
9
|
+
constructor(values, offset = 0) {
|
|
10
|
+
this._index = [];
|
|
11
|
+
this._map = new Map();
|
|
12
|
+
this._offset = offset;
|
|
13
|
+
if (Array.isArray(values) && values.length) {
|
|
14
|
+
values.forEach((word) => {
|
|
15
|
+
if (this._map.has(word))
|
|
16
|
+
return;
|
|
17
|
+
this._map.set(word, this._count++);
|
|
18
|
+
this._index.push(word);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
get size() {
|
|
23
|
+
return this._count;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns inserted index or nothing
|
|
27
|
+
*/
|
|
28
|
+
maybeInsert(word) {
|
|
29
|
+
if (this._map.has(word))
|
|
30
|
+
return;
|
|
31
|
+
this._map.set(word, this._count++);
|
|
32
|
+
this._index.push(word);
|
|
33
|
+
return this._count + this._offset;
|
|
34
|
+
}
|
|
35
|
+
getValue(index) {
|
|
36
|
+
return this._index[index - this._offset];
|
|
37
|
+
}
|
|
38
|
+
getIndex(value) {
|
|
39
|
+
const idx = this._map.get(value);
|
|
40
|
+
if (idx === undefined) {
|
|
41
|
+
return idx;
|
|
42
|
+
}
|
|
43
|
+
return idx + this._offset;
|
|
44
|
+
}
|
|
45
|
+
hasValue(value) {
|
|
46
|
+
return this._map.has(value);
|
|
47
|
+
}
|
|
48
|
+
hasIndex(index) {
|
|
49
|
+
return this._index[index - this._offset] !== undefined;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { BinaryReader } from './BinaryReader.js';
|
|
2
|
+
import type { BinaryWriter } from './BinaryWriter.js';
|
|
3
|
+
export type EncodeHandler = (this: BinaryWriter, value: any) => void;
|
|
4
|
+
export type DecodeHandler = (this: BinaryReader) => any;
|
|
5
|
+
export interface TLExtension {
|
|
6
|
+
token: number;
|
|
7
|
+
encode: EncodeHandler;
|
|
8
|
+
decode: DecodeHandler;
|
|
9
|
+
}
|
|
10
|
+
export declare function createExtension(token: number, { encode, decode }: {
|
|
11
|
+
encode: EncodeHandler;
|
|
12
|
+
decode: DecodeHandler;
|
|
13
|
+
}): TLExtension;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function createExtension(token, { encode, decode }) {
|
|
2
|
+
if (token !== -1 && (token > 254 || token < 0 || token << 0 !== token)) {
|
|
3
|
+
throw new TypeError('Token must be a 8 bit number');
|
|
4
|
+
}
|
|
5
|
+
if (token !== -1 && token < 35) {
|
|
6
|
+
throw new TypeError('Tokens reserved from 0 to 34');
|
|
7
|
+
}
|
|
8
|
+
return {
|
|
9
|
+
token,
|
|
10
|
+
encode,
|
|
11
|
+
decode,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { CORE_TYPES } from './constants.js';
|
|
3
|
+
export declare const int32: Int32Array;
|
|
4
|
+
export declare const float32: Float32Array;
|
|
5
|
+
export declare const float64: Float64Array;
|
|
6
|
+
export declare function byteArrayAllocate(length: number): Buffer | Uint8Array;
|
|
7
|
+
export declare function coreType(value: any): CORE_TYPES;
|
|
8
|
+
export declare function utf8Read(target: Buffer | Uint8Array, length: number, offset: number): string;
|
|
9
|
+
export declare function utf8ReadShort(target: Buffer | Uint8Array, length: number, offset: number): string | undefined;
|
|
10
|
+
export declare const utf8Write: (target: any, value: string, offset: number) => number;
|
|
11
|
+
export declare const utf8WriteShort: (target: any, value: string, offset: number) => number;
|