@andrew_l/tl-pack 0.1.82 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +49 -41
- 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.C2PEqnNg.d.cts +200 -0
- package/dist/shared/tl-pack.C2PEqnNg.d.mts +200 -0
- package/dist/shared/tl-pack.C2PEqnNg.d.ts +200 -0
- package/dist/shared/tl-pack.CkwM-fIb.mjs +1152 -0
- package/dist/shared/tl-pack.CkwM-fIb.mjs.map +1 -0
- package/dist/shared/tl-pack.DfL54MjP.cjs +1162 -0
- package/dist/shared/tl-pack.DfL54MjP.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 +50 -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
package/dist/BinaryReader.js
DELETED
|
@@ -1,412 +0,0 @@
|
|
|
1
|
-
import pako from 'pako';
|
|
2
|
-
import { CORE_TYPES } from './constants.js';
|
|
3
|
-
import { Dictionary } from './dictionary.js';
|
|
4
|
-
import { float32, float64, int32, utf8Read } from './helpers.js';
|
|
5
|
-
export class BinaryReader {
|
|
6
|
-
target;
|
|
7
|
-
_last;
|
|
8
|
-
_lastObject;
|
|
9
|
-
dictionary;
|
|
10
|
-
dictionaryExtended;
|
|
11
|
-
extensions;
|
|
12
|
-
_repeat;
|
|
13
|
-
offset;
|
|
14
|
-
length;
|
|
15
|
-
/**
|
|
16
|
-
* Small utility class to read binary data.
|
|
17
|
-
* @param data {Buffer}
|
|
18
|
-
*/
|
|
19
|
-
constructor(data, options) {
|
|
20
|
-
this.target = data;
|
|
21
|
-
this.offset = 0;
|
|
22
|
-
this.length = data.length;
|
|
23
|
-
this.extensions = new Map();
|
|
24
|
-
if (options && options.extensions) {
|
|
25
|
-
options.extensions.forEach((ext) => {
|
|
26
|
-
this.extensions.set(ext.token, ext);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
if (!options) {
|
|
30
|
-
this.dictionary = new Dictionary();
|
|
31
|
-
}
|
|
32
|
-
else if (options.dictionary instanceof Dictionary) {
|
|
33
|
-
this.dictionary = options.dictionary;
|
|
34
|
-
}
|
|
35
|
-
else if (Array.isArray(options.dictionary)) {
|
|
36
|
-
this.dictionary = new Dictionary(options.dictionary);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
this.dictionary = new Dictionary();
|
|
40
|
-
}
|
|
41
|
-
this.dictionaryExtended = new Dictionary(undefined, this.dictionary.size);
|
|
42
|
-
}
|
|
43
|
-
readByte() {
|
|
44
|
-
this.assertRead(1);
|
|
45
|
-
this._last = this.target[this.offset++];
|
|
46
|
-
return this._last;
|
|
47
|
-
}
|
|
48
|
-
readInt32(signed = true) {
|
|
49
|
-
this.assertRead(4);
|
|
50
|
-
this._last =
|
|
51
|
-
this.target[this.offset++] |
|
|
52
|
-
(this.target[this.offset++] << 8) |
|
|
53
|
-
(this.target[this.offset++] << 16) |
|
|
54
|
-
(this.target[this.offset++] << 24);
|
|
55
|
-
if (!signed) {
|
|
56
|
-
this._last = this._last >>> 0;
|
|
57
|
-
}
|
|
58
|
-
return this._last;
|
|
59
|
-
}
|
|
60
|
-
readInt16(signed = true) {
|
|
61
|
-
this.assertRead(2);
|
|
62
|
-
this._last = this.target[this.offset++] | (this.target[this.offset++] << 8);
|
|
63
|
-
if (signed) {
|
|
64
|
-
this._last = (this._last << 16) >> 16;
|
|
65
|
-
}
|
|
66
|
-
return this._last;
|
|
67
|
-
}
|
|
68
|
-
readInt8(signed = true) {
|
|
69
|
-
this.assertRead(1);
|
|
70
|
-
this._last = this.target[this.offset++];
|
|
71
|
-
if (signed) {
|
|
72
|
-
this._last = (this._last << 24) >> 24;
|
|
73
|
-
}
|
|
74
|
-
return this._last;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Reads a real floating point (4 bytes) value.
|
|
78
|
-
* @returns {number}
|
|
79
|
-
*/
|
|
80
|
-
readFloat() {
|
|
81
|
-
this.assertRead(4);
|
|
82
|
-
int32[0] = this.readInt32();
|
|
83
|
-
this._last = float32[0];
|
|
84
|
-
return this._last;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Reads a real floating point (8 bytes) value.
|
|
88
|
-
* @returns {BigInteger}
|
|
89
|
-
*/
|
|
90
|
-
readDouble() {
|
|
91
|
-
this.assertRead(8);
|
|
92
|
-
int32[0] = this.readInt32();
|
|
93
|
-
int32[1] = this.readInt32();
|
|
94
|
-
this._last = float64[0];
|
|
95
|
-
return this._last;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Read the given amount of bytes, or -1 to read all remaining.
|
|
99
|
-
* @param length {number}
|
|
100
|
-
*/
|
|
101
|
-
assertRead(length) {
|
|
102
|
-
if (this.length < this.offset + +length) {
|
|
103
|
-
const left = this.target.length - this.offset;
|
|
104
|
-
const result = this.target.subarray(this.offset, this.offset + left);
|
|
105
|
-
const err = new Error(`No more data left to read (need ${length}, got ${left}: ${result}); last read ${this._last}`);
|
|
106
|
-
err.incomplete = true;
|
|
107
|
-
Error.captureStackTrace(err, this.assertRead);
|
|
108
|
-
throw err;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
assertConstructor(constructorId) {
|
|
112
|
-
const byte = this.readByte();
|
|
113
|
-
if (byte !== constructorId) {
|
|
114
|
-
throw new Error(`Invalid constructor code, expected = ${CORE_TYPES[constructorId]}, got = ${CORE_TYPES[byte] || byte}, offset = ${this.offset - 1}`);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Gets the byte array representing the current buffer as a whole.
|
|
119
|
-
* @returns {Buffer}
|
|
120
|
-
*/
|
|
121
|
-
getBuffer() {
|
|
122
|
-
return this.target;
|
|
123
|
-
}
|
|
124
|
-
readNull() {
|
|
125
|
-
const value = this.readByte();
|
|
126
|
-
if (value === CORE_TYPES.Null) {
|
|
127
|
-
return null;
|
|
128
|
-
}
|
|
129
|
-
throw new Error(`Invalid boolean code ${value.toString(16)}`);
|
|
130
|
-
}
|
|
131
|
-
readLength() {
|
|
132
|
-
const firstByte = this.readByte();
|
|
133
|
-
if (firstByte === 254) {
|
|
134
|
-
return this.readByte() | (this.readByte() << 8) | (this.readByte() << 16);
|
|
135
|
-
}
|
|
136
|
-
return firstByte;
|
|
137
|
-
}
|
|
138
|
-
readAll() {
|
|
139
|
-
const result = [];
|
|
140
|
-
while (this.length > this.offset) {
|
|
141
|
-
result.push(this.readObject());
|
|
142
|
-
}
|
|
143
|
-
return result;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* @returns {Uint8Array | Buffer}
|
|
147
|
-
*/
|
|
148
|
-
readBytes() {
|
|
149
|
-
const length = this.readLength();
|
|
150
|
-
this.assertRead(length);
|
|
151
|
-
const bytes = this.target.subarray(this.offset, this.offset + length);
|
|
152
|
-
this.offset += bytes.length;
|
|
153
|
-
this._last = bytes;
|
|
154
|
-
return bytes;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Reads encoded string.
|
|
158
|
-
* @returns {string}
|
|
159
|
-
*/
|
|
160
|
-
readString() {
|
|
161
|
-
const length = this.readLength();
|
|
162
|
-
this.assertRead(length);
|
|
163
|
-
const result = utf8Read(this.target, length, this.offset);
|
|
164
|
-
this.offset += length;
|
|
165
|
-
this._last = result;
|
|
166
|
-
return result;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Reads a boolean value.
|
|
170
|
-
* @returns {boolean}
|
|
171
|
-
*/
|
|
172
|
-
readBool() {
|
|
173
|
-
const value = this.readByte();
|
|
174
|
-
if (value === CORE_TYPES.BoolTrue) {
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
else if (value === CORE_TYPES.BoolFalse) {
|
|
178
|
-
return false;
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
throw new Error(`Invalid boolean code ${value.toString(16)}`);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Reads and converts Unix time
|
|
186
|
-
* into a Javascript {Date} object.
|
|
187
|
-
* @returns {Date}
|
|
188
|
-
*/
|
|
189
|
-
readDate() {
|
|
190
|
-
const value = this.readDouble();
|
|
191
|
-
return new Date(value);
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Reads a object.
|
|
195
|
-
*/
|
|
196
|
-
readObject() {
|
|
197
|
-
if (this._repeat) {
|
|
198
|
-
if (this._repeat.pool > 0) {
|
|
199
|
-
--this._repeat.pool;
|
|
200
|
-
return this._repeat.value;
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
this._repeat = undefined;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
const constructorId = this.readByte();
|
|
207
|
-
const ext = this.extensions.get(constructorId);
|
|
208
|
-
let value;
|
|
209
|
-
if (ext) {
|
|
210
|
-
value = ext.decode.call(this);
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
value = this._lastObject = this.readCore(constructorId);
|
|
214
|
-
}
|
|
215
|
-
return value;
|
|
216
|
-
}
|
|
217
|
-
readObjectGzip() {
|
|
218
|
-
const bytes = this.readGzip();
|
|
219
|
-
const reader = new BinaryReader(bytes);
|
|
220
|
-
reader.extensions = this.extensions;
|
|
221
|
-
reader.dictionary = this.dictionary;
|
|
222
|
-
reader.dictionaryExtended = this.dictionaryExtended;
|
|
223
|
-
return reader.readObject();
|
|
224
|
-
}
|
|
225
|
-
readGzip() {
|
|
226
|
-
return pako.inflateRaw(this.readBytes());
|
|
227
|
-
}
|
|
228
|
-
readCore(constructorId) {
|
|
229
|
-
switch (constructorId) {
|
|
230
|
-
case CORE_TYPES.None:
|
|
231
|
-
return this.readObject();
|
|
232
|
-
case CORE_TYPES.GZIP:
|
|
233
|
-
return this.readObjectGzip();
|
|
234
|
-
case CORE_TYPES.BoolTrue:
|
|
235
|
-
return true;
|
|
236
|
-
case CORE_TYPES.BoolFalse:
|
|
237
|
-
return false;
|
|
238
|
-
case CORE_TYPES.Vector:
|
|
239
|
-
return this.readVector(false);
|
|
240
|
-
case CORE_TYPES.VectorDynamic:
|
|
241
|
-
return this.readVectorDynamic(false);
|
|
242
|
-
case CORE_TYPES.Null:
|
|
243
|
-
return null;
|
|
244
|
-
case CORE_TYPES.Binary:
|
|
245
|
-
return this.readBytes();
|
|
246
|
-
case CORE_TYPES.String:
|
|
247
|
-
return this.readString();
|
|
248
|
-
case CORE_TYPES.Date:
|
|
249
|
-
return this.readDate();
|
|
250
|
-
case CORE_TYPES.Int32:
|
|
251
|
-
return this.readInt32();
|
|
252
|
-
case CORE_TYPES.Int16:
|
|
253
|
-
return this.readInt16();
|
|
254
|
-
case CORE_TYPES.Int8:
|
|
255
|
-
return this.readInt8();
|
|
256
|
-
case CORE_TYPES.UInt32:
|
|
257
|
-
return this.readInt32(false);
|
|
258
|
-
case CORE_TYPES.UInt16:
|
|
259
|
-
return this.readInt16(false);
|
|
260
|
-
case CORE_TYPES.UInt8:
|
|
261
|
-
return this.readInt8(false);
|
|
262
|
-
case CORE_TYPES.Float:
|
|
263
|
-
return this.readFloat();
|
|
264
|
-
case CORE_TYPES.Double:
|
|
265
|
-
return this.readDouble();
|
|
266
|
-
case CORE_TYPES.Map:
|
|
267
|
-
return this.readMap(false);
|
|
268
|
-
case CORE_TYPES.DictIndex: {
|
|
269
|
-
const idx = this.readLength();
|
|
270
|
-
return this.getDictionaryValue(idx);
|
|
271
|
-
}
|
|
272
|
-
case CORE_TYPES.DictValue: {
|
|
273
|
-
const value = this.readString();
|
|
274
|
-
this.dictionaryExtended.maybeInsert(value);
|
|
275
|
-
return value;
|
|
276
|
-
}
|
|
277
|
-
case CORE_TYPES.Repeat: {
|
|
278
|
-
const size = this.readLength();
|
|
279
|
-
this._repeat = { pool: size - 1, value: this._lastObject };
|
|
280
|
-
return this._lastObject;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
throw new Error(`Invalid constructor = ${CORE_TYPES[constructorId] || constructorId}, offset = ${this.offset - 1}`);
|
|
284
|
-
}
|
|
285
|
-
getDictionaryValue(index) {
|
|
286
|
-
let value;
|
|
287
|
-
if (this.dictionary) {
|
|
288
|
-
value = this.dictionary.getValue(index);
|
|
289
|
-
}
|
|
290
|
-
if (value === undefined) {
|
|
291
|
-
value = this.dictionaryExtended.getValue(index);
|
|
292
|
-
}
|
|
293
|
-
return value;
|
|
294
|
-
}
|
|
295
|
-
readDictionary() {
|
|
296
|
-
const constructorId = this.readByte();
|
|
297
|
-
let key = null;
|
|
298
|
-
switch (constructorId) {
|
|
299
|
-
case CORE_TYPES.DictIndex: {
|
|
300
|
-
const idx = this.readLength();
|
|
301
|
-
key = this.getDictionaryValue(idx);
|
|
302
|
-
break;
|
|
303
|
-
}
|
|
304
|
-
case CORE_TYPES.DictValue: {
|
|
305
|
-
key = this.readString();
|
|
306
|
-
this.dictionaryExtended.maybeInsert(key);
|
|
307
|
-
break;
|
|
308
|
-
}
|
|
309
|
-
case CORE_TYPES.None: {
|
|
310
|
-
key = null;
|
|
311
|
-
break;
|
|
312
|
-
}
|
|
313
|
-
default: {
|
|
314
|
-
this.seek(-1);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
return key;
|
|
318
|
-
}
|
|
319
|
-
readMap(checkConstructor = true) {
|
|
320
|
-
if (checkConstructor) {
|
|
321
|
-
this.assertConstructor(CORE_TYPES.Map);
|
|
322
|
-
}
|
|
323
|
-
const temp = {};
|
|
324
|
-
let key = this.readDictionary();
|
|
325
|
-
while (key !== null) {
|
|
326
|
-
temp[key] = this.readObject();
|
|
327
|
-
key = this.readDictionary();
|
|
328
|
-
}
|
|
329
|
-
return temp;
|
|
330
|
-
}
|
|
331
|
-
decode(value) {
|
|
332
|
-
this.target = value;
|
|
333
|
-
this._last = undefined;
|
|
334
|
-
this._lastObject = undefined;
|
|
335
|
-
this._repeat = undefined;
|
|
336
|
-
this.offset = 0;
|
|
337
|
-
this.length = value.length;
|
|
338
|
-
return this.readObject();
|
|
339
|
-
}
|
|
340
|
-
/**
|
|
341
|
-
* Reads a vector (a list) of objects.
|
|
342
|
-
* @returns {any[]}
|
|
343
|
-
*/
|
|
344
|
-
readVector(checkConstructor = true) {
|
|
345
|
-
if (checkConstructor) {
|
|
346
|
-
this.assertConstructor(CORE_TYPES.Vector);
|
|
347
|
-
}
|
|
348
|
-
const count = this.readLength();
|
|
349
|
-
const temp = [];
|
|
350
|
-
for (let i = 0; i < count; i++) {
|
|
351
|
-
temp.push(this.readObject());
|
|
352
|
-
}
|
|
353
|
-
return temp;
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Reads a vector (a list) of objects.
|
|
357
|
-
* @returns {any[]}
|
|
358
|
-
*/
|
|
359
|
-
readVectorDynamic(checkConstructor = true) {
|
|
360
|
-
if (checkConstructor) {
|
|
361
|
-
this.assertConstructor(CORE_TYPES.VectorDynamic);
|
|
362
|
-
}
|
|
363
|
-
const temp = [];
|
|
364
|
-
let complete = false;
|
|
365
|
-
while (this.length > this.offset) {
|
|
366
|
-
const constructorId = this.readByte();
|
|
367
|
-
if (constructorId === CORE_TYPES.None) {
|
|
368
|
-
complete = true;
|
|
369
|
-
break;
|
|
370
|
-
}
|
|
371
|
-
const ext = this.extensions.get(constructorId);
|
|
372
|
-
let value;
|
|
373
|
-
if (ext) {
|
|
374
|
-
value = ext.decode.call(this);
|
|
375
|
-
}
|
|
376
|
-
else {
|
|
377
|
-
value = this.readCore(constructorId);
|
|
378
|
-
}
|
|
379
|
-
temp.push(value);
|
|
380
|
-
}
|
|
381
|
-
if (!complete) {
|
|
382
|
-
const err = new Error(`DynamicVector incomplete.`);
|
|
383
|
-
err.incomplete = true;
|
|
384
|
-
Error.captureStackTrace(err, this.readDictionary);
|
|
385
|
-
throw err;
|
|
386
|
-
}
|
|
387
|
-
this._last = temp;
|
|
388
|
-
return temp;
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Tells the current position on the stream.
|
|
392
|
-
* @returns {number}
|
|
393
|
-
*/
|
|
394
|
-
tellPosition() {
|
|
395
|
-
return this.offset;
|
|
396
|
-
}
|
|
397
|
-
/**
|
|
398
|
-
* Sets the current position on the stream.
|
|
399
|
-
* @param position
|
|
400
|
-
*/
|
|
401
|
-
setPosition(position) {
|
|
402
|
-
this.offset = position;
|
|
403
|
-
}
|
|
404
|
-
/**
|
|
405
|
-
* Seeks the stream position given an offset from the current position.
|
|
406
|
-
* The offset may be negative.
|
|
407
|
-
* @param offset
|
|
408
|
-
*/
|
|
409
|
-
seek(offset) {
|
|
410
|
-
this.offset += offset;
|
|
411
|
-
}
|
|
412
|
-
}
|
package/dist/BinaryWriter.d.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
import { Dictionary } from './dictionary.js';
|
|
3
|
-
import { TLExtension } from './extension.js';
|
|
4
|
-
export interface BinaryWriterOptions {
|
|
5
|
-
gzip?: boolean;
|
|
6
|
-
dictionary?: string[] | Dictionary;
|
|
7
|
-
extensions?: TLExtension[];
|
|
8
|
-
}
|
|
9
|
-
export declare class BinaryWriter {
|
|
10
|
-
private withGzip;
|
|
11
|
-
private target;
|
|
12
|
-
private dictionary?;
|
|
13
|
-
private dictionaryExtended;
|
|
14
|
-
private extensions;
|
|
15
|
-
private _last;
|
|
16
|
-
private _repeat?;
|
|
17
|
-
offset: number;
|
|
18
|
-
constructor(options?: BinaryWriterOptions);
|
|
19
|
-
allocate(size: number): void;
|
|
20
|
-
private makeRoom;
|
|
21
|
-
get safeEnd(): number;
|
|
22
|
-
getBuffer(): Buffer | Uint8Array;
|
|
23
|
-
writeByte(value: number): void;
|
|
24
|
-
writeBool(value: boolean): void;
|
|
25
|
-
writeNull(): void;
|
|
26
|
-
writeInt32(value: number, signed?: boolean): void;
|
|
27
|
-
writeInt16(value: number, signed?: boolean): void;
|
|
28
|
-
writeInt8(value: number, signed?: boolean): void;
|
|
29
|
-
writeFloat(value: number): void;
|
|
30
|
-
writeDouble(value: number): void;
|
|
31
|
-
writeDate(value: number | Date): void;
|
|
32
|
-
writeString(value: string): void;
|
|
33
|
-
writeBytes(value: Buffer | Uint8Array): void;
|
|
34
|
-
writeLength(value: number): void;
|
|
35
|
-
writeVector(value: Array<any>): void;
|
|
36
|
-
writeMap(object: Record<string, any>): void;
|
|
37
|
-
wireDictionary(value: string): void;
|
|
38
|
-
writeGzip(value: any): void;
|
|
39
|
-
encode(value: any): Buffer | Uint8Array;
|
|
40
|
-
startDynamicVector(): void;
|
|
41
|
-
endDynamicVector(): void;
|
|
42
|
-
private _writeCustom;
|
|
43
|
-
writeObject(value: any): void;
|
|
44
|
-
writeObjectGzip(value: any): void;
|
|
45
|
-
private writeCore;
|
|
46
|
-
private writeRepeat;
|
|
47
|
-
}
|