UNPKG

@andrew_l/tl-pack

Version:

![license](https://img.shields.io/npm/l/%40andrew_l%2Ftl-pack) <!-- omit in toc --> ![npm version](https://img.shields.io/npm/v/%40andrew_l%2Ftl-pack) <!-- omit in toc --> ![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40andrew_l%2Ftl-pack

201 lines (195 loc) 5.66 kB
declare enum CORE_TYPES { None = 0, Binary = 1, BoolFalse = 2, BoolTrue = 3, Null = 4, Date = 5, Vector = 6, VectorDynamic = 7, Int32 = 8, Int16 = 9, Int8 = 10, UInt32 = 11, UInt16 = 12, UInt8 = 13, Float = 14, Double = 15, Map = 16, DictValue = 17, DictIndex = 18, String = 19, Repeat = 20, Checksum = 21, GZIP = 25 } declare const MAX_BUFFER_SIZE = 2144337920; declare function createDictionary(values?: string[]): Dictionary; declare class Dictionary { private _count; private _wordToIndex; private _words; private _offset; constructor(values?: string[], offset?: number); get size(): number; /** * Returns inserted index or nothing */ maybeInsert(word: string): number | null; getValue(index: number): string | null; getIndex(value: string): number | null; hasValue(value: string): boolean; hasIndex(index: number): boolean; } interface BinaryWriterOptions { gzip?: boolean; dictionary?: string[] | Dictionary; extensions?: TLExtension[]; } declare class BinaryWriter { private withGzip; private target; private dictionary?; private dictionaryExtended; private extensions; private _last; private _checksumOffset; private _repeat?; offset: number; constructor(options?: BinaryWriterOptions); allocate(size: number): this; private makeRoom; get safeEnd(): number; getBuffer(): Uint8Array; writeByte(value: number): this; writeBool(value: boolean): this; writeNull(): this; writeInt32(value: number, signed?: boolean): this; writeInt16(value: number, signed?: boolean): this; writeInt8(value: number, signed?: boolean): this; writeFloat(value: number): this; writeDouble(value: number): this; writeDate(value: number | Date): this; writeString(value: string): this; writeChecksum(withConstructor?: boolean): this; writeBytes(value: Uint8Array): this; writeLength(value: number): this; writeVector(value: Array<any>): this; writeMap(object: Record<string, any>): this; wireDictionary(value: string): this; writeGzip(value: Uint8Array | ArrayBuffer): this; encode(value: any): Uint8Array; startDynamicVector(): this; endDynamicVector(): this; private _writeCustom; writeObject(value: any): this; writeObjectGzip(value: any): this; private writeCore; private writeRepeat; } type EncodeHandler = (this: BinaryWriter, value: any) => void; type DecodeHandler = (this: BinaryReader) => any; interface TLExtension { token: number; encode: EncodeHandler; decode: DecodeHandler; } declare function createExtension(token: number, { encode, decode }: { encode: EncodeHandler; decode: DecodeHandler; }): TLExtension; interface BinaryReaderOptions { dictionary?: string[] | Dictionary; extensions?: TLExtension[]; } declare class BinaryReader { private target; private _last?; private _lastObject?; private dictionary?; private dictionaryExtended; private extensions; private _repeat?; private _checksumOffset; offset: number; length: number; /** * Small utility class to read binary data. */ constructor(data: Uint8Array, options?: BinaryReaderOptions); readByte(): number; readInt32(signed?: boolean): number; readInt16(signed?: boolean): number; readInt8(signed?: boolean): number; /** * Reads a real floating point (4 bytes) value. * @returns {number} */ readFloat(): number; /** * Reads a real floating point (8 bytes) value. * @returns {BigInteger} */ readDouble(): number; /** * Read the given amount of bytes, or -1 to read all remaining. * @param length {number} */ assertRead(length: number): void; assertConstructor(constructorId: CORE_TYPES): void; /** * Gets the byte array representing the current buffer as a whole. */ getBuffer(): Uint8Array; readNull(): null; readLength(): number; readAll(): any[]; readBytes(): Uint8Array; /** * Reads encoded string. */ readString(): string; /** * Reads a boolean value. */ readBool(): boolean; /** * Reads and converts Unix time * into a Javascript {Date} object. */ readDate(): Date; /** * Reads a object. */ readObject(): any; readObjectGzip(): any; readGzip(): any; private readCore; getDictionaryValue(index: number): string | null; readDictionary(): null | string; readMap(checkConstructor?: boolean): Record<string, any>; decode<T = any>(value: Uint8Array): T; /** * Reads a vector (a list) of objects. */ readVector<T = any>(checkConstructor?: boolean): T[]; /** * Reads a vector (a list) of objects. */ readVectorDynamic<T>(checkConstructor?: boolean): T[]; readChecksum(checkConstructor?: boolean): void; /** * Tells the current position on the stream. */ tellPosition(): number; /** * Sets the current position on the stream. */ setPosition(position: number): void; /** * Seeks the stream position given an offset from the current position. * The offset may be negative. */ seek(offset: number): void; } export { type BinaryWriterOptions as B, CORE_TYPES as C, type DecodeHandler as D, type EncodeHandler as E, MAX_BUFFER_SIZE as M, type TLExtension as T, type BinaryReaderOptions as a, BinaryReader as b, BinaryWriter as c, createDictionary as d, createExtension as e };