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