@andrew_l/tl-pack 0.1.81 → 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/dist/helpers.js DELETED
@@ -1,321 +0,0 @@
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.js DELETED
@@ -1,5 +0,0 @@
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/stream.js DELETED
@@ -1,67 +0,0 @@
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
- }