@andrew_l/tl-pack 0.1.9 → 0.1.81
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
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { CORE_TYPES, HAS_NODE_BUFFER } from './constants.js';
|
|
2
|
+
const encoder = new TextEncoder();
|
|
3
|
+
const decoder = new TextDecoder();
|
|
4
|
+
const fromCharCode = String.fromCharCode;
|
|
5
|
+
export const int32 = new Int32Array(2);
|
|
6
|
+
export const float32 = new Float32Array(int32.buffer);
|
|
7
|
+
export const float64 = new Float64Array(int32.buffer);
|
|
8
|
+
export function byteArrayAllocate(length) {
|
|
9
|
+
if (HAS_NODE_BUFFER) {
|
|
10
|
+
return Buffer.allocUnsafeSlow(length);
|
|
11
|
+
}
|
|
12
|
+
return new Uint8Array(length);
|
|
13
|
+
}
|
|
14
|
+
export function coreType(value) {
|
|
15
|
+
switch (typeof value) {
|
|
16
|
+
case 'string': {
|
|
17
|
+
return CORE_TYPES.String;
|
|
18
|
+
}
|
|
19
|
+
case 'boolean': {
|
|
20
|
+
return value ? CORE_TYPES.BoolTrue : CORE_TYPES.BoolFalse;
|
|
21
|
+
}
|
|
22
|
+
case 'number': {
|
|
23
|
+
if (value >> 0 === value) {
|
|
24
|
+
if (value >= 0 && value <= 0xff) {
|
|
25
|
+
return CORE_TYPES.UInt8;
|
|
26
|
+
}
|
|
27
|
+
else if (value >= 0 && value <= 0xffff) {
|
|
28
|
+
return CORE_TYPES.UInt16;
|
|
29
|
+
}
|
|
30
|
+
else if (value >= 0 && value <= 0xffffffff) {
|
|
31
|
+
return CORE_TYPES.UInt32;
|
|
32
|
+
}
|
|
33
|
+
else if (value >= -0x80 && value <= 0x7f) {
|
|
34
|
+
return CORE_TYPES.Int8;
|
|
35
|
+
}
|
|
36
|
+
else if (value >= -0x8000 && value <= 0x7fff) {
|
|
37
|
+
return CORE_TYPES.Int16;
|
|
38
|
+
}
|
|
39
|
+
else if (value >= -0x80000000 && value <= 0x7fffffff) {
|
|
40
|
+
return CORE_TYPES.Int32;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return CORE_TYPES.Double;
|
|
44
|
+
}
|
|
45
|
+
case 'object': {
|
|
46
|
+
if (value === null)
|
|
47
|
+
return CORE_TYPES.Null;
|
|
48
|
+
if (value instanceof Date) {
|
|
49
|
+
return CORE_TYPES.Date;
|
|
50
|
+
}
|
|
51
|
+
if (Array.isArray(value)) {
|
|
52
|
+
return CORE_TYPES.Vector;
|
|
53
|
+
}
|
|
54
|
+
if (isPlainObject(value)) {
|
|
55
|
+
return CORE_TYPES.Map;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return CORE_TYPES.None;
|
|
60
|
+
}
|
|
61
|
+
export function utf8Read(target, length, offset) {
|
|
62
|
+
let result;
|
|
63
|
+
if (length < 16) {
|
|
64
|
+
if ((result = utf8ReadShort(target, length, offset)))
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
if (length > 64 && decoder)
|
|
68
|
+
return decoder.decode(target.subarray(offset, (offset += length)));
|
|
69
|
+
const end = offset + length;
|
|
70
|
+
const units = [];
|
|
71
|
+
result = '';
|
|
72
|
+
while (offset < end) {
|
|
73
|
+
const byte1 = target[offset++];
|
|
74
|
+
if ((byte1 & 0x80) === 0) {
|
|
75
|
+
// 1 byte
|
|
76
|
+
units.push(byte1);
|
|
77
|
+
}
|
|
78
|
+
else if ((byte1 & 0xe0) === 0xc0) {
|
|
79
|
+
// 2 bytes
|
|
80
|
+
const byte2 = target[offset++] & 0x3f;
|
|
81
|
+
units.push(((byte1 & 0x1f) << 6) | byte2);
|
|
82
|
+
}
|
|
83
|
+
else if ((byte1 & 0xf0) === 0xe0) {
|
|
84
|
+
// 3 bytes
|
|
85
|
+
const byte2 = target[offset++] & 0x3f;
|
|
86
|
+
const byte3 = target[offset++] & 0x3f;
|
|
87
|
+
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
|
|
88
|
+
}
|
|
89
|
+
else if ((byte1 & 0xf8) === 0xf0) {
|
|
90
|
+
// 4 bytes
|
|
91
|
+
const byte2 = target[offset++] & 0x3f;
|
|
92
|
+
const byte3 = target[offset++] & 0x3f;
|
|
93
|
+
const byte4 = target[offset++] & 0x3f;
|
|
94
|
+
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
|
|
95
|
+
if (unit > 0xffff) {
|
|
96
|
+
unit -= 0x10000;
|
|
97
|
+
units.push(((unit >>> 10) & 0x3ff) | 0xd800);
|
|
98
|
+
unit = 0xdc00 | (unit & 0x3ff);
|
|
99
|
+
}
|
|
100
|
+
units.push(unit);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
units.push(byte1);
|
|
104
|
+
}
|
|
105
|
+
if (units.length >= 0x1000) {
|
|
106
|
+
result += fromCharCode.apply(String, units);
|
|
107
|
+
units.length = 0;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (units.length > 0) {
|
|
111
|
+
result += fromCharCode.apply(String, units);
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
export function utf8ReadShort(target, length, offset) {
|
|
116
|
+
if (length < 4) {
|
|
117
|
+
if (length < 2) {
|
|
118
|
+
if (length === 0)
|
|
119
|
+
return '';
|
|
120
|
+
else {
|
|
121
|
+
let a = target[offset++];
|
|
122
|
+
if ((a & 0x80) > 1) {
|
|
123
|
+
offset -= 1;
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
return fromCharCode(a);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
let a = target[offset++];
|
|
131
|
+
let b = target[offset++];
|
|
132
|
+
if ((a & 0x80) > 0 || (b & 0x80) > 0) {
|
|
133
|
+
offset -= 2;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (length < 3)
|
|
137
|
+
return fromCharCode(a, b);
|
|
138
|
+
let c = target[offset++];
|
|
139
|
+
if ((c & 0x80) > 0) {
|
|
140
|
+
offset -= 3;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
return fromCharCode(a, b, c);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
let a = target[offset++];
|
|
148
|
+
let b = target[offset++];
|
|
149
|
+
let c = target[offset++];
|
|
150
|
+
let d = target[offset++];
|
|
151
|
+
if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
|
|
152
|
+
offset -= 4;
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (length < 6) {
|
|
156
|
+
if (length === 4)
|
|
157
|
+
return fromCharCode(a, b, c, d);
|
|
158
|
+
else {
|
|
159
|
+
let e = target[offset++];
|
|
160
|
+
if ((e & 0x80) > 0) {
|
|
161
|
+
offset -= 5;
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
return fromCharCode(a, b, c, d, e);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else if (length < 8) {
|
|
168
|
+
let e = target[offset++];
|
|
169
|
+
let f = target[offset++];
|
|
170
|
+
if ((e & 0x80) > 0 || (f & 0x80) > 0) {
|
|
171
|
+
offset -= 6;
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (length < 7)
|
|
175
|
+
return fromCharCode(a, b, c, d, e, f);
|
|
176
|
+
let g = target[offset++];
|
|
177
|
+
if ((g & 0x80) > 0) {
|
|
178
|
+
offset -= 7;
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
return fromCharCode(a, b, c, d, e, f, g);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
let e = target[offset++];
|
|
185
|
+
let f = target[offset++];
|
|
186
|
+
let g = target[offset++];
|
|
187
|
+
let h = target[offset++];
|
|
188
|
+
if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
|
|
189
|
+
offset -= 8;
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (length < 10) {
|
|
193
|
+
if (length === 8)
|
|
194
|
+
return fromCharCode(a, b, c, d, e, f, g, h);
|
|
195
|
+
else {
|
|
196
|
+
let i = target[offset++];
|
|
197
|
+
if ((i & 0x80) > 0) {
|
|
198
|
+
offset -= 9;
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (length < 12) {
|
|
205
|
+
let i = target[offset++];
|
|
206
|
+
let j = target[offset++];
|
|
207
|
+
if ((i & 0x80) > 0 || (j & 0x80) > 0) {
|
|
208
|
+
offset -= 10;
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (length < 11)
|
|
212
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
213
|
+
let k = target[offset++];
|
|
214
|
+
if ((k & 0x80) > 0) {
|
|
215
|
+
offset -= 11;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
let i = target[offset++];
|
|
222
|
+
let j = target[offset++];
|
|
223
|
+
let k = target[offset++];
|
|
224
|
+
let l = target[offset++];
|
|
225
|
+
if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
|
|
226
|
+
offset -= 12;
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (length < 14) {
|
|
230
|
+
if (length === 12)
|
|
231
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
232
|
+
else {
|
|
233
|
+
let m = target[offset++];
|
|
234
|
+
if ((m & 0x80) > 0) {
|
|
235
|
+
offset -= 13;
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
let m = target[offset++];
|
|
243
|
+
let n = target[offset++];
|
|
244
|
+
if ((m & 0x80) > 0 || (n & 0x80) > 0) {
|
|
245
|
+
offset -= 14;
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
if (length < 15)
|
|
249
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
250
|
+
let o = target[offset++];
|
|
251
|
+
if ((o & 0x80) > 0) {
|
|
252
|
+
offset -= 15;
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
export const utf8Write = HAS_NODE_BUFFER
|
|
262
|
+
? function (target, value, offset) {
|
|
263
|
+
return value.length < 0x40
|
|
264
|
+
? utf8WriteShort(target, value, offset)
|
|
265
|
+
: target.utf8Write(value, offset, 0xffffffff);
|
|
266
|
+
}
|
|
267
|
+
: function (target, value, offset) {
|
|
268
|
+
return value.length < 0x40
|
|
269
|
+
? utf8WriteShort(target, value, offset)
|
|
270
|
+
: encoder.encodeInto(value, target.subarray(offset)).written;
|
|
271
|
+
};
|
|
272
|
+
export const utf8WriteShort = (target, value, offset) => {
|
|
273
|
+
let i, c1, c2, strPosition = offset;
|
|
274
|
+
const strLength = value.length;
|
|
275
|
+
for (i = 0; i < strLength; i++) {
|
|
276
|
+
c1 = value.charCodeAt(i);
|
|
277
|
+
if (c1 < 0x80) {
|
|
278
|
+
target[strPosition++] = c1;
|
|
279
|
+
}
|
|
280
|
+
else if (c1 < 0x800) {
|
|
281
|
+
target[strPosition++] = (c1 >> 6) | 0xc0;
|
|
282
|
+
target[strPosition++] = (c1 & 0x3f) | 0x80;
|
|
283
|
+
}
|
|
284
|
+
else if ((c1 & 0xfc00) === 0xd800 && ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00) {
|
|
285
|
+
c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
|
|
286
|
+
i++;
|
|
287
|
+
target[strPosition++] = (c1 >> 18) | 0xf0;
|
|
288
|
+
target[strPosition++] = ((c1 >> 12) & 0x3f) | 0x80;
|
|
289
|
+
target[strPosition++] = ((c1 >> 6) & 0x3f) | 0x80;
|
|
290
|
+
target[strPosition++] = (c1 & 0x3f) | 0x80;
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
target[strPosition++] = (c1 >> 12) | 0xe0;
|
|
294
|
+
target[strPosition++] = ((c1 >> 6) & 0x3f) | 0x80;
|
|
295
|
+
target[strPosition++] = (c1 & 0x3f) | 0x80;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return strPosition - offset;
|
|
299
|
+
};
|
|
300
|
+
/** @ts-ignore */
|
|
301
|
+
const isObject = (val) => toString.call(val) === '[object Object]';
|
|
302
|
+
function isPlainObject(value) {
|
|
303
|
+
let ctor, prot;
|
|
304
|
+
if (!isObject(value))
|
|
305
|
+
return false;
|
|
306
|
+
// If it has modified constructor
|
|
307
|
+
ctor = value.constructor;
|
|
308
|
+
if (ctor === undefined)
|
|
309
|
+
return true;
|
|
310
|
+
// If it has modified prototype
|
|
311
|
+
prot = ctor.prototype;
|
|
312
|
+
if (isObject(prot) === false)
|
|
313
|
+
return false;
|
|
314
|
+
// If constructor does not have an Object-specific method
|
|
315
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
316
|
+
if (prot.hasOwnProperty('isPrototypeOf') === false) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
// Most likely a plain Object
|
|
320
|
+
return true;
|
|
321
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,28 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* const buffer = tlEncode(new Date(0));
|
|
9
|
-
*
|
|
10
|
-
* console.log(buffer); // Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0])
|
|
11
|
-
*
|
|
12
|
-
* @group Main
|
|
13
|
-
*/
|
|
14
|
-
declare function tlEncode(value: unknown, opts?: BinaryWriterOptions): Uint8Array;
|
|
15
|
-
/**
|
|
16
|
-
* Decode value from `Uint8Array`
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* const buffer = new Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
20
|
-
* const value = tlDecode(buffer);
|
|
21
|
-
*
|
|
22
|
-
* console.log(value); // Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)
|
|
23
|
-
*
|
|
24
|
-
* @group Main
|
|
25
|
-
*/
|
|
26
|
-
declare function tlDecode<T = any>(buffer: Uint8Array, opts?: BinaryReaderOptions): T;
|
|
27
|
-
|
|
28
|
-
export { BinaryReaderOptions, BinaryWriterOptions, tlDecode, tlEncode };
|
|
1
|
+
export * from './BinaryWriter.js';
|
|
2
|
+
export * from './BinaryReader.js';
|
|
3
|
+
export * from './constants.js';
|
|
4
|
+
export * from './extension.js';
|
|
5
|
+
export * from './dictionary.js';
|
package/dist/index.js
ADDED
package/dist/stream.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
3
|
+
import { Transform, type TransformCallback, type TransformOptions } from 'stream';
|
|
4
|
+
import { BinaryWriter, BinaryWriterOptions } from './BinaryWriter.js';
|
|
5
|
+
import { BinaryReader } from './BinaryReader.js';
|
|
6
|
+
export interface TLEncodeOptions extends BinaryWriterOptions {
|
|
5
7
|
streamOptions?: TransformOptions;
|
|
6
8
|
}
|
|
7
|
-
declare class TLEncode extends Transform {
|
|
9
|
+
export declare class TLEncode extends Transform {
|
|
8
10
|
writer: BinaryWriter;
|
|
9
11
|
count: number;
|
|
10
12
|
constructor(options?: TLEncodeOptions);
|
|
11
13
|
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void;
|
|
12
14
|
}
|
|
13
|
-
declare class TLDecode extends Transform {
|
|
15
|
+
export declare class TLDecode extends Transform {
|
|
14
16
|
reader: BinaryReader;
|
|
15
17
|
private incompleteBuffer;
|
|
16
18
|
constructor(options?: TransformOptions);
|
|
17
19
|
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void;
|
|
18
20
|
}
|
|
19
|
-
|
|
20
|
-
export { TLDecode, TLEncode, type TLEncodeOptions };
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Transform } from 'stream';
|
|
2
|
+
import { BinaryWriter } from './BinaryWriter.js';
|
|
3
|
+
import { BinaryReader } from './BinaryReader.js';
|
|
4
|
+
import { CORE_TYPES } from './constants.js';
|
|
5
|
+
export class TLEncode extends Transform {
|
|
6
|
+
writer;
|
|
7
|
+
count;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
const opts = options || {};
|
|
10
|
+
opts.streamOptions = { writableObjectMode: true, ...(opts.streamOptions || {}) };
|
|
11
|
+
super(opts.streamOptions);
|
|
12
|
+
const writer = new BinaryWriter(options);
|
|
13
|
+
const customFlush = opts.streamOptions.flush;
|
|
14
|
+
const VECTOR_TYPES = new Uint8Array(2);
|
|
15
|
+
VECTOR_TYPES[0] = CORE_TYPES.VectorDynamic;
|
|
16
|
+
VECTOR_TYPES[1] = CORE_TYPES.None;
|
|
17
|
+
// push a byte about dynamic vector starting
|
|
18
|
+
this.push(VECTOR_TYPES.subarray(0, 1));
|
|
19
|
+
this._flush = (callback) => {
|
|
20
|
+
// push a byte about dynamic vector ending
|
|
21
|
+
this.push(VECTOR_TYPES.subarray(1, 2));
|
|
22
|
+
if (customFlush) {
|
|
23
|
+
customFlush.call(this, callback);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
callback();
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
this.writer = writer;
|
|
30
|
+
this.count = 0;
|
|
31
|
+
}
|
|
32
|
+
_transform(chunk, encoding, callback) {
|
|
33
|
+
const buff = this.writer.encode(chunk);
|
|
34
|
+
this.push(buff);
|
|
35
|
+
this.count++;
|
|
36
|
+
callback();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export class TLDecode extends Transform {
|
|
40
|
+
reader;
|
|
41
|
+
incompleteBuffer;
|
|
42
|
+
constructor(options) {
|
|
43
|
+
if (!options)
|
|
44
|
+
options = {};
|
|
45
|
+
options.objectMode = true;
|
|
46
|
+
super(options);
|
|
47
|
+
this.incompleteBuffer = null;
|
|
48
|
+
this.reader = new BinaryReader(new Uint8Array(8192));
|
|
49
|
+
}
|
|
50
|
+
_transform(chunk, encoding, callback) {
|
|
51
|
+
if (this.incompleteBuffer) {
|
|
52
|
+
chunk = Buffer.concat([this.incompleteBuffer, chunk]);
|
|
53
|
+
this.incompleteBuffer = null;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const value = this.reader.decode(chunk);
|
|
57
|
+
return callback(null, value);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
if (err?.incomplete) {
|
|
61
|
+
this.incompleteBuffer = chunk;
|
|
62
|
+
return callback();
|
|
63
|
+
}
|
|
64
|
+
return callback(err);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
package/package.json
CHANGED
|
@@ -1,43 +1,54 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
2
|
+
"name": "@andrew_l/tl-pack",
|
|
3
|
+
"version": "0.1.81",
|
|
4
|
+
"description": "Binary serialization library",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "nodemon src/playground.ts",
|
|
8
|
+
"build": "rm -rf dist && npx tsc"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"tl",
|
|
12
|
+
"pack",
|
|
13
|
+
"binary",
|
|
14
|
+
"buffer",
|
|
15
|
+
"serialization",
|
|
16
|
+
"deserialization"
|
|
17
|
+
],
|
|
18
|
+
"author": "Andrew L.",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/**"
|
|
22
|
+
],
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/men232/tl-pack.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/men232/tl-pack/issues"
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"import": "./dist/index.js",
|
|
35
|
+
"types": "./dist/index.d.ts"
|
|
36
|
+
},
|
|
37
|
+
"./stream": {
|
|
38
|
+
"import": "./dist/stream.js",
|
|
39
|
+
"types": "./dist/stream.d.ts"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^20.3.2",
|
|
44
|
+
"@types/pako": "^2.0.0",
|
|
45
|
+
"eslint-config-prettier": "^8.8.0",
|
|
46
|
+
"nodemon": "^2.0.22",
|
|
47
|
+
"prettier": "^2.8.8",
|
|
48
|
+
"ts-node": "^10.9.1",
|
|
49
|
+
"typescript": "^5.1.5"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"pako": "^2.1.0"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Andrew L. <andrew.io.dev@gmail.com>
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/index.cjs
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const BinaryWriter = require('./shared/tl-pack.VfbARqWq.cjs');
|
|
4
|
-
const toolkit = require('@andrew_l/toolkit');
|
|
5
|
-
require('pako');
|
|
6
|
-
|
|
7
|
-
function createExtension(token, { encode, decode }) {
|
|
8
|
-
toolkit.assert.ok(Math.trunc(token) === token, " Token must be integer value.");
|
|
9
|
-
toolkit.assert.ok(
|
|
10
|
-
token >= 0 && token <= 255,
|
|
11
|
-
"Token must be a 8 bit number. (0 - 255)"
|
|
12
|
-
);
|
|
13
|
-
toolkit.assert.ok(token >= 35, "Tokens from 0 to 34 reserved.");
|
|
14
|
-
return {
|
|
15
|
-
token,
|
|
16
|
-
encode,
|
|
17
|
-
decode
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function tlEncode(value, opts) {
|
|
22
|
-
return new BinaryWriter.BinaryWriter(opts).writeObject(value).getBuffer();
|
|
23
|
-
}
|
|
24
|
-
function tlDecode(buffer, opts) {
|
|
25
|
-
return new BinaryWriter.BinaryReader(buffer, opts).readObject();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
exports.BinaryReader = BinaryWriter.BinaryReader;
|
|
29
|
-
exports.BinaryWriter = BinaryWriter.BinaryWriter;
|
|
30
|
-
exports.CORE_TYPES = BinaryWriter.CORE_TYPES;
|
|
31
|
-
exports.MAX_BUFFER_SIZE = BinaryWriter.MAX_BUFFER_SIZE;
|
|
32
|
-
exports.createDictionary = BinaryWriter.createDictionary;
|
|
33
|
-
exports.createExtension = createExtension;
|
|
34
|
-
exports.tlDecode = tlDecode;
|
|
35
|
-
exports.tlEncode = tlEncode;
|
|
36
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/extension.ts","../src/index.ts"],"sourcesContent":["import { assert } from '@andrew_l/toolkit';\nimport type { BinaryReader } from './BinaryReader';\nimport type { BinaryWriter } from './BinaryWriter';\n\nexport type EncodeHandler = (this: BinaryWriter, value: any) => void;\n\nexport type DecodeHandler = (this: BinaryReader) => any;\n\nexport interface TLExtension {\n token: number;\n encode: EncodeHandler;\n decode: DecodeHandler;\n}\n\nexport function createExtension(\n token: number,\n { encode, decode }: { encode: EncodeHandler; decode: DecodeHandler },\n): TLExtension {\n assert.ok(Math.trunc(token) === token, ' Token must be integer value.');\n\n assert.ok(\n token >= 0 && token <= 255,\n 'Token must be a 8 bit number. (0 - 255)',\n );\n\n assert.ok(token >= 35, 'Tokens from 0 to 34 reserved.');\n\n return {\n token,\n encode,\n decode,\n };\n}\n","import { BinaryReader, type BinaryReaderOptions } from './BinaryReader';\nimport { BinaryWriter, type BinaryWriterOptions } from './BinaryWriter';\n\nexport { BinaryReader, type BinaryReaderOptions } from './BinaryReader';\nexport { BinaryWriter, type BinaryWriterOptions } from './BinaryWriter';\nexport * from './constants';\nexport { createDictionary } from './dictionary';\nexport {\n createExtension,\n type DecodeHandler,\n type EncodeHandler,\n type TLExtension,\n} from './extension';\n\n/**\n * Encode any value into `Uint8Array`\n *\n * @example\n * const buffer = tlEncode(new Date(0));\n *\n * console.log(buffer); // Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0])\n *\n * @group Main\n */\nexport function tlEncode(\n value: unknown,\n opts?: BinaryWriterOptions,\n): Uint8Array {\n return new BinaryWriter(opts).writeObject(value).getBuffer();\n}\n\n/**\n * Decode value from `Uint8Array`\n *\n * @example\n * const buffer = new Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0]);\n * const value = tlDecode(buffer);\n *\n * console.log(value); // Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)\n *\n * @group Main\n */\nexport function tlDecode<T = any>(\n buffer: Uint8Array,\n opts?: BinaryReaderOptions,\n): T {\n return new BinaryReader(buffer, opts).readObject();\n}\n"],"names":["assert","BinaryWriter","BinaryReader"],"mappings":";;;;;;AAcO,SAAS,eACd,CAAA,KAAA,EACA,EAAE,MAAA,EAAQ,QACG,EAAA;AACb,EAAAA,cAAA,CAAO,GAAG,IAAK,CAAA,KAAA,CAAM,KAAK,CAAA,KAAM,OAAO,+BAA+B,CAAA,CAAA;AAEtE,EAAOA,cAAA,CAAA,EAAA;AAAA,IACL,KAAA,IAAS,KAAK,KAAS,IAAA,GAAA;AAAA,IACvB,yCAAA;AAAA,GACF,CAAA;AAEA,EAAOA,cAAA,CAAA,EAAA,CAAG,KAAS,IAAA,EAAA,EAAI,+BAA+B,CAAA,CAAA;AAEtD,EAAO,OAAA;AAAA,IACL,KAAA;AAAA,IACA,MAAA;AAAA,IACA,MAAA;AAAA,GACF,CAAA;AACF;;ACRgB,SAAA,QAAA,CACd,OACA,IACY,EAAA;AACZ,EAAA,OAAO,IAAIC,yBAAa,CAAA,IAAI,EAAE,WAAY,CAAA,KAAK,EAAE,SAAU,EAAA,CAAA;AAC7D,CAAA;AAagB,SAAA,QAAA,CACd,QACA,IACG,EAAA;AACH,EAAA,OAAO,IAAIC,yBAAA,CAAa,MAAQ,EAAA,IAAI,EAAE,UAAW,EAAA,CAAA;AACnD;;;;;;;;;;;"}
|
package/dist/index.d.cts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { B as BinaryWriterOptions, a as BinaryReaderOptions } from './shared/tl-pack.CdcwN4dG.cjs';
|
|
2
|
-
export { b as BinaryReader, c as BinaryWriter, C as CORE_TYPES, D as DecodeHandler, E as EncodeHandler, M as MAX_BUFFER_SIZE, T as TLExtension, d as createDictionary, e as createExtension } from './shared/tl-pack.CdcwN4dG.cjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Encode any value into `Uint8Array`
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* const buffer = tlEncode(new Date(0));
|
|
9
|
-
*
|
|
10
|
-
* console.log(buffer); // Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0])
|
|
11
|
-
*
|
|
12
|
-
* @group Main
|
|
13
|
-
*/
|
|
14
|
-
declare function tlEncode(value: unknown, opts?: BinaryWriterOptions): Uint8Array;
|
|
15
|
-
/**
|
|
16
|
-
* Decode value from `Uint8Array`
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* const buffer = new Uint8Array([5, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
20
|
-
* const value = tlDecode(buffer);
|
|
21
|
-
*
|
|
22
|
-
* console.log(value); // Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)
|
|
23
|
-
*
|
|
24
|
-
* @group Main
|
|
25
|
-
*/
|
|
26
|
-
declare function tlDecode<T = any>(buffer: Uint8Array, opts?: BinaryReaderOptions): T;
|
|
27
|
-
|
|
28
|
-
export { BinaryReaderOptions, BinaryWriterOptions, tlDecode, tlEncode };
|