@andrew_l/tl-pack 0.2.15 → 0.2.17

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.
@@ -0,0 +1,470 @@
1
+ import { Data } from '@andrew_l/toolkit';
2
+
3
+ declare function createDictionary(values?: string[]): Dictionary;
4
+ declare class Dictionary {
5
+ private _count;
6
+ private _wordToIndex;
7
+ private _words;
8
+ private _offset;
9
+ constructor(values?: string[], offset?: number);
10
+ get size(): number;
11
+ /**
12
+ * Returns inserted index or nothing
13
+ */
14
+ maybeInsert(word: string): number | null;
15
+ getValue(index: number): string | null;
16
+ getIndex(value: string): number | null;
17
+ hasValue(value: string): boolean;
18
+ hasIndex(index: number): boolean;
19
+ }
20
+
21
+ type EncodeHandler<T = any> = (this: BinaryWriter, value: T) => void;
22
+ type DecodeHandler<T = any> = (this: BinaryReader) => T;
23
+ interface TLExtension<T = any> {
24
+ token: number;
25
+ encode: EncodeHandler<T>;
26
+ decode: DecodeHandler<T>;
27
+ }
28
+ declare function createExtension(token: number, { encode, decode }: {
29
+ encode: EncodeHandler;
30
+ decode: DecodeHandler;
31
+ }): TLExtension;
32
+
33
+ interface BinaryWriterOptions {
34
+ gzip?: boolean;
35
+ dictionary?: string[] | Dictionary;
36
+ extensions?: TLExtension[];
37
+ structures?: Structure.Constructor[];
38
+ }
39
+ declare class BinaryWriter {
40
+ private withGzip;
41
+ private target;
42
+ private dictionary;
43
+ private dictionaryExtended;
44
+ private extensions;
45
+ private structures;
46
+ private _last;
47
+ private offsetChecksum;
48
+ private _repeat?;
49
+ offset: number;
50
+ constructor({ gzip, dictionary, extensions, structures, }?: BinaryWriterOptions);
51
+ /**
52
+ * Reset internal state
53
+ */
54
+ reset(): this;
55
+ allocate(size: number): this;
56
+ private makeRoom;
57
+ get safeEnd(): number;
58
+ getBuffer(): Uint8Array;
59
+ writeByte(value: number): this;
60
+ writeBool(value: boolean): this;
61
+ writeNull(): this;
62
+ writeInt32(value: number, signed?: boolean): this;
63
+ writeInt16(value: number, signed?: boolean): this;
64
+ writeInt8(value: number, signed?: boolean): this;
65
+ writeFloat(value: number): this;
66
+ writeDouble(value: number): this;
67
+ writeDate(value: number | Date): this;
68
+ writeString(value: string): this;
69
+ writeChecksum(withConstructor?: boolean): this;
70
+ writeBytes(value: Uint8Array): this;
71
+ writeLength(value: number): this;
72
+ writeVector(value: Array<any>): this;
73
+ writeMap(object: Record<string, any>): this;
74
+ wireDictionary(value: string): this;
75
+ writeStructure(value: Structure): this;
76
+ writeGzip(value: Uint8Array | ArrayBuffer): this;
77
+ encode(value: any): Uint8Array;
78
+ startDynamicVector(): this;
79
+ endDynamicVector(): this;
80
+ private _writeCustom;
81
+ writeObject(value: any): this;
82
+ writeObjectGzip(value: any): this;
83
+ private writeCore;
84
+ private writeRepeat;
85
+ }
86
+
87
+ declare enum CORE_TYPES {
88
+ None = 0,
89
+ Binary = 1,
90
+ BoolFalse = 2,
91
+ BoolTrue = 3,
92
+ Null = 4,
93
+ Date = 5,
94
+ Vector = 6,
95
+ VectorDynamic = 7,
96
+ Int32 = 8,
97
+ Int16 = 9,
98
+ Int8 = 10,
99
+ UInt32 = 11,
100
+ UInt16 = 12,
101
+ UInt8 = 13,
102
+ Float = 14,
103
+ Double = 15,
104
+ Map = 16,
105
+ DictValue = 17,
106
+ DictIndex = 18,
107
+ String = 19,
108
+ Repeat = 20,
109
+ Checksum = 21,
110
+ GZIP = 25,
111
+ Structure = 26
112
+ }
113
+ declare const MAX_BUFFER_SIZE = 2144337920;
114
+
115
+ interface DefineStructureOptions<Props extends Structure.ObjectPropsOptions> {
116
+ /**
117
+ * Unique name of binary structure
118
+ */
119
+ readonly name: string;
120
+ /**
121
+ * Version of binary structure
122
+ */
123
+ readonly version: number;
124
+ /**
125
+ * Binary structure properties
126
+ */
127
+ readonly properties: Props;
128
+ /**
129
+ * Write checksum to verify decoded data
130
+ * @default false
131
+ */
132
+ readonly checksum?: boolean;
133
+ }
134
+ /**
135
+ * Create binary structure definition with type safety and performance optimization
136
+ *
137
+ * @example
138
+ * // Define a user structure
139
+ * const User = defineStructure({
140
+ * name: 'User',
141
+ * version: 1,
142
+ * checksum: true,
143
+ * properties: {
144
+ * id: { type: Number, required: true },
145
+ * name: { type: String, required: true },
146
+ * email: { type: String, required: false },
147
+ * isActive: { type: Boolean, required: true },
148
+ * createdAt: { type: Date, required: true },
149
+ * tags: { type: Array, required: false }
150
+ * }
151
+ * });
152
+ *
153
+ * // Create and serialize a user
154
+ * const user = new User({
155
+ * id: 123,
156
+ * name: 'John Doe',
157
+ * email: 'john@example.com',
158
+ * isActive: true,
159
+ * createdAt: new Date(),
160
+ * tags: ['admin', 'verified']
161
+ * });
162
+ *
163
+ * const buffer = user.toBuffer();
164
+ * const restored = User.fromBuffer(buffer);
165
+ *
166
+ * @example
167
+ * // Define nested structures
168
+ * const Address = defineStructure({
169
+ * name: 'Address',
170
+ * version: 1,
171
+ * properties: {
172
+ * street: { type: String, required: true },
173
+ * city: { type: String, required: true },
174
+ * zipCode: { type: String, required: true }
175
+ * }
176
+ * });
177
+ *
178
+ * const Person = defineStructure({
179
+ * name: 'Person',
180
+ * version: 1,
181
+ * properties: {
182
+ * name: { type: String, required: true },
183
+ * address: { type: Address, required: false }
184
+ * }
185
+ * });
186
+ *
187
+ * @example
188
+ * // Using with complex data types
189
+ * const GameState = defineStructure({
190
+ * name: 'GameState',
191
+ * version: 2,
192
+ * checksum: true,
193
+ * properties: {
194
+ * playerData: { type: Object, required: true }, // Untrusted data
195
+ * screenshot: { type: Uint8Array, required: false }, // Binary data
196
+ * timestamp: { type: Date, required: true },
197
+ * metadata: { type: Object, required: false }
198
+ * }
199
+ * });
200
+ *
201
+ * @group Main
202
+ */
203
+ declare function defineStructure<PropsOptions extends Structure.ObjectPropsOptions, T extends Data = Structure.ExtractPropTypes<PropsOptions>>({ name, properties, version, checksum, }: DefineStructureOptions<PropsOptions>): Structure.Constructor<T>;
204
+ declare class Structure<T extends Data = Data> implements Structure.Structure<T> {
205
+ readonly value: T;
206
+ constructor(value: T);
207
+ toBuffer(options?: BinaryWriterOptions): Uint8Array;
208
+ static fromBuffer(buffer: Uint8Array, options?: BinaryReaderOptions): Data;
209
+ static readonly estimatedSizeBytes: number;
210
+ static readonly structures: Structure.Constructor[];
211
+ static readonly extension: TLExtension;
212
+ }
213
+ declare namespace Structure {
214
+ export interface Options<T extends Data = Data> {
215
+ readonly encode: EncodeHandler<T>;
216
+ readonly decode: DecodeHandler<T>;
217
+ }
218
+ export interface Structure<T extends Data = Data> {
219
+ /**
220
+ * The structured data value
221
+ * @type {T}
222
+ * @readonly
223
+ *
224
+ * @example
225
+ * const user = new UserStruct({ id: 1, name: 'Alice' });
226
+ * console.log(user.value.name); // 'Alice'
227
+ */
228
+ readonly value: T;
229
+ /**
230
+ * Serializes the structure to a binary buffer
231
+ *
232
+ * @param {BinaryWriterOptions} [options] - Writer configuration options
233
+ * @returns {Uint8Array} Serialized binary data
234
+ *
235
+ * @example
236
+ * const user = new User({ id: 1, name: 'Alice' });
237
+ * const buffer = user.toBuffer();
238
+ * console.log(buffer.length); // Size in bytes
239
+ *
240
+ * @example
241
+ * // With custom writer options
242
+ * const buffer = user.toBuffer({
243
+ * initialSize: 1024,
244
+ * growthFactor: 2
245
+ * });
246
+ */
247
+ toBuffer(options?: BinaryWriterOptions): Uint8Array;
248
+ }
249
+ /**
250
+ * Base Structure class for binary serialization with type safety
251
+ *
252
+ * @template T - Data type extending Data interface
253
+ *
254
+ * @example
255
+ * // Direct usage (not recommended, use defineStructure instead)
256
+ * class CustomStruct extends Structure<{id: number, name: string}> {
257
+ * // Custom implementation
258
+ * }
259
+ *
260
+ * @example
261
+ * // Typical usage through defineStructure
262
+ * const MyStruct = defineStructure({
263
+ * name: 'MyStruct',
264
+ * version: 1,
265
+ * properties: { id: { type: Number, required: true } }
266
+ * });
267
+ *
268
+ * const instance = new MyStruct({ id: 42 });
269
+ * console.log(instance.value.id); // 42
270
+ */
271
+ export interface Constructor<T extends Data = any> {
272
+ /**
273
+ * Creates a new Structure instance
274
+ *
275
+ * @param {T} value - The data to structure
276
+ *
277
+ * @example
278
+ * const data = { id: 123, name: 'Test' };
279
+ * const struct = new MyStructure(data);
280
+ */
281
+ new (value: T): Structure<T>;
282
+ /**
283
+ * Deserializes a structure from a binary buffer
284
+ *
285
+ * @param {Uint8Array} buffer - Binary data to deserialize
286
+ * @param {BinaryReaderOptions} [options] - Reader configuration options
287
+ * @returns {Data} Deserialized structure data
288
+ *
289
+ * @example
290
+ * const buffer = user.toBuffer();
291
+ * const restored = User.fromBuffer(buffer);
292
+ * console.log(restored.id); // Original user ID
293
+ */
294
+ fromBuffer(buffer: Uint8Array, options?: BinaryReaderOptions): T;
295
+ /**
296
+ * Estimated size of bytes
297
+ */
298
+ readonly estimatedSizeBytes: number;
299
+ /**
300
+ * Nested structures defining encoding/decoding behavior
301
+ */
302
+ readonly structures: Constructor[];
303
+ /**
304
+ * Structure extension defining encoding/decoding behavior
305
+ */
306
+ readonly extension: TLExtension;
307
+ }
308
+ type PropConstructor<T = any> = {
309
+ new (...args: any[]): T & {};
310
+ } | {
311
+ (): T;
312
+ } | PropMethod<T>;
313
+ type PropMethod<T, TConstructor = any> = [T] extends [
314
+ ((...args: any) => any) | undefined
315
+ ] ? {
316
+ new (): TConstructor;
317
+ (): T;
318
+ readonly prototype: TConstructor;
319
+ } : never;
320
+ type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>;
321
+ type RequiredKeys<T> = {
322
+ [K in keyof T]: T[K] extends {
323
+ required: true;
324
+ } ? K : never;
325
+ }[keyof T];
326
+ type Prop<T> = PropOptions<T> | PropType<T>;
327
+ type CoreInt = CORE_TYPES.Int8 | CORE_TYPES.Int16 | CORE_TYPES.Int32 | CORE_TYPES.UInt8 | CORE_TYPES.UInt16 | CORE_TYPES.UInt32 | CORE_TYPES.Float | CORE_TYPES.Double;
328
+ type InferPropType<T, NullAsAny = true> = [T] extends [null] ? NullAsAny extends true ? any : null : [T] extends [{
329
+ type: null;
330
+ }] ? any : [T] extends [{
331
+ type: [null];
332
+ }] ? any[] : [T] extends [{
333
+ type: ObjectConstructor | CORE_TYPES.Map;
334
+ }] ? Record<string, any> : [T] extends [{
335
+ type: CoreInt;
336
+ }] ? number : [T] extends [{
337
+ type: CORE_TYPES.String;
338
+ }] ? string : [T] extends [{
339
+ type: CORE_TYPES.Vector;
340
+ }] ? unknown[] : [T] extends [{
341
+ type: CORE_TYPES.Binary;
342
+ }] ? Uint8Array : [T] extends [{
343
+ type: BooleanConstructor;
344
+ }] ? boolean : [T] extends [{
345
+ type: DateConstructor | CORE_TYPES.Date;
346
+ }] ? Date : [T] extends [{
347
+ type: Constructor<infer U>;
348
+ }] ? U : [T] extends [{
349
+ type: [infer U];
350
+ }] ? U extends DateConstructor ? Date[] : U extends Constructor<infer V> ? V[] : InferPropType<U, false>[] : [T] extends [{
351
+ type: (infer U)[];
352
+ }] ? U extends DateConstructor ? Date | InferPropType<U, false> : InferPropType<U, false> : [T] extends [Prop<infer V>] ? V : T;
353
+ export type ObjectPropsOptions<P = Data> = {
354
+ readonly [K in keyof P]: PropOptions<P[K]>;
355
+ };
356
+ export interface PropOptions<T = any> {
357
+ type: PropType<T> | null | [null];
358
+ required?: boolean;
359
+ }
360
+ export type PropType<T> = PropConstructor<T> | [PropConstructor<T>] | CORE_TYPES | [CORE_TYPES];
361
+ export type ExtractType<T extends Constructor> = T extends Constructor<infer U> ? U : never;
362
+ export type ExtractPropTypes<O> = {
363
+ [K in keyof Pick<O, RequiredKeys<O>>]: O[K] extends {
364
+ default: any;
365
+ } ? Exclude<InferPropType<O[K]>, undefined> : InferPropType<O[K]>;
366
+ } & {
367
+ [K in keyof Pick<O, OptionalKeys<O>>]?: InferPropType<O[K]> | null;
368
+ };
369
+ export { };
370
+ }
371
+
372
+ interface BinaryReaderOptions {
373
+ dictionary?: string[] | Dictionary;
374
+ extensions?: TLExtension[];
375
+ structures?: Structure.Constructor[];
376
+ }
377
+ declare class BinaryReader {
378
+ private target;
379
+ private _last?;
380
+ private _lastObject?;
381
+ private dictionary?;
382
+ private dictionaryExtended;
383
+ private extensions;
384
+ private structures;
385
+ private _repeat?;
386
+ private _checksumOffset;
387
+ offset: number;
388
+ length: number;
389
+ /**
390
+ * Small utility class to read binary data.
391
+ */
392
+ constructor(data: Uint8Array, { dictionary, extensions, structures, }?: BinaryReaderOptions);
393
+ readByte(): number;
394
+ readInt32(signed?: boolean): number;
395
+ readInt16(signed?: boolean): number;
396
+ readInt8(signed?: boolean): number;
397
+ /**
398
+ * Reads a real floating point (4 bytes) value.
399
+ * @returns {number}
400
+ */
401
+ readFloat(): number;
402
+ /**
403
+ * Reads a real floating point (8 bytes) value.
404
+ * @returns {BigInteger}
405
+ */
406
+ readDouble(): number;
407
+ /**
408
+ * Throws error if provided length cannot be read from buffer
409
+ * @param length {number}
410
+ */
411
+ assertRead(length: number): void;
412
+ assertConstructor(constructorId: CORE_TYPES): void;
413
+ /**
414
+ * Gets the byte array representing the current buffer as a whole.
415
+ */
416
+ getBuffer(): Uint8Array;
417
+ readNull(): null;
418
+ readLength(): number;
419
+ readAll(): any[];
420
+ readBytes(): Uint8Array;
421
+ /**
422
+ * Reads encoded string.
423
+ */
424
+ readString(): string;
425
+ /**
426
+ * Reads a boolean value.
427
+ */
428
+ readBool(): boolean;
429
+ /**
430
+ * Reads and converts Unix time
431
+ * into a Javascript {Date} object.
432
+ */
433
+ readDate(): Date;
434
+ readStructure<T extends Data = Data>(checkConstructor?: boolean): T;
435
+ /**
436
+ * Reads a object.
437
+ */
438
+ readObject(): any;
439
+ readObjectGzip(): any;
440
+ readGzip(): any;
441
+ private readCore;
442
+ getDictionaryValue(index: number): string | null;
443
+ readDictionary(): null | string;
444
+ readMap(checkConstructor?: boolean): Record<string, any>;
445
+ decode<T = any>(value: Uint8Array): T;
446
+ /**
447
+ * Reads a vector (a list) of objects.
448
+ */
449
+ readVector<T = any>(checkConstructor?: boolean): T[];
450
+ /**
451
+ * Reads a vector (a list) of objects.
452
+ */
453
+ readVectorDynamic<T>(checkConstructor?: boolean): T[];
454
+ readChecksum(checkConstructor?: boolean): void;
455
+ /**
456
+ * Tells the current position on the stream.
457
+ */
458
+ tellPosition(): number;
459
+ /**
460
+ * Sets the current position on the stream.
461
+ */
462
+ setPosition(position: number): void;
463
+ /**
464
+ * Seeks the stream position given an offset from the current position.
465
+ * The offset may be negative.
466
+ */
467
+ seek(offset: number): void;
468
+ }
469
+
470
+ export { type BinaryWriterOptions as B, CORE_TYPES as C, type DecodeHandler as D, type EncodeHandler as E, MAX_BUFFER_SIZE as M, Structure as S, type TLExtension as T, type BinaryReaderOptions as a, BinaryReader as b, BinaryWriter as c, createDictionary as d, createExtension as e, type DefineStructureOptions as f, defineStructure as g };