2021-11-23 00:03:59 +03:00
|
|
|
import Long from 'long'
|
|
|
|
|
2024-02-23 00:24:21 +03:00
|
|
|
import { ITlPlatform } from './platform.js'
|
2023-10-16 19:23:53 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
const TWO_PWR_32_DBL = (1 << 16) * (1 << 16)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mapping of TL object names to writer functions.
|
|
|
|
*/
|
2023-06-05 03:30:48 +03:00
|
|
|
// avoid unnecessary type complexity
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-07-20 22:07:07 +03:00
|
|
|
export type TlWriterMap = Record<string, (w: any, val: any) => void> & {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
_bare?: Record<number, (w: any, val: any) => void>
|
2023-09-20 18:37:26 +03:00
|
|
|
_staticSize: Record<string, number>
|
2023-07-20 22:07:07 +03:00
|
|
|
}
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* Counter of the required number of bytes to encode a given object.
|
|
|
|
*
|
|
|
|
* Used as a pre-pass before using {@link TlBinaryWriter}
|
|
|
|
* to avoid unnecessary allocations.
|
|
|
|
*/
|
2021-11-23 00:03:59 +03:00
|
|
|
export class TlSerializationCounter {
|
|
|
|
count = 0
|
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* @param objectMap Writers map
|
|
|
|
*/
|
2024-02-23 00:24:21 +03:00
|
|
|
constructor(
|
|
|
|
readonly objectMap: TlWriterMap,
|
|
|
|
) {}
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* Count bytes required to serialize the given object.
|
|
|
|
*
|
|
|
|
* @param objectMap Writers map
|
|
|
|
* @param obj Object to count bytes for
|
|
|
|
*/
|
2024-02-28 00:33:23 +03:00
|
|
|
static countNeededBytes(objectMap: TlWriterMap, obj: { _: string }): number {
|
|
|
|
const cnt = new TlSerializationCounter(objectMap)
|
2021-11-23 00:03:59 +03:00
|
|
|
cnt.object(obj)
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
return cnt.count
|
|
|
|
}
|
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* Count overhead in bytes for the given number of bytes when
|
|
|
|
* encoded as `bytes` TL type.
|
|
|
|
*
|
|
|
|
* @param size Number of bytes
|
|
|
|
*/
|
2021-11-23 00:03:59 +03:00
|
|
|
static countBytesOverhead(size: number): number {
|
|
|
|
let res = 0
|
|
|
|
|
|
|
|
let padding
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
if (size <= 253) {
|
|
|
|
res += 1
|
|
|
|
padding = (size + 1) % 4
|
|
|
|
} else {
|
|
|
|
res += 4
|
|
|
|
padding = size % 4
|
|
|
|
}
|
|
|
|
|
|
|
|
if (padding > 0) res += 4 - padding
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean(): void {
|
|
|
|
this.count += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
double(): void {
|
|
|
|
this.count += 8
|
|
|
|
}
|
|
|
|
|
|
|
|
float(): void {
|
|
|
|
this.count += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
int128(): void {
|
|
|
|
this.count += 16
|
|
|
|
}
|
|
|
|
|
|
|
|
int256(): void {
|
|
|
|
this.count += 32
|
|
|
|
}
|
|
|
|
|
|
|
|
int(): void {
|
|
|
|
this.count += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
uint(): void {
|
|
|
|
this.count += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
int53(): void {
|
|
|
|
this.count += 8
|
|
|
|
}
|
|
|
|
|
|
|
|
long(): void {
|
|
|
|
this.count += 8
|
|
|
|
}
|
|
|
|
|
|
|
|
null(): void {
|
|
|
|
this.count += 4
|
|
|
|
}
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
raw(val: Uint8Array): void {
|
|
|
|
this.count += val.byteLength
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
bytes(val: Uint8Array): void {
|
2023-09-24 01:32:22 +03:00
|
|
|
this.count += TlSerializationCounter.countBytesOverhead(val.length) + val.length
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
string(val: string): void {
|
2024-02-28 00:33:23 +03:00
|
|
|
const length = TlBinaryWriter.platform.utf8ByteLength(val)
|
2021-11-23 00:03:59 +03:00
|
|
|
this.count += TlSerializationCounter.countBytesOverhead(length) + length
|
|
|
|
}
|
|
|
|
|
2022-08-24 23:37:26 +03:00
|
|
|
object(obj: { _: string }): void {
|
2021-11-23 00:03:59 +03:00
|
|
|
if (!this.objectMap[obj._]) throw new Error(`Unknown object ${obj._}`)
|
|
|
|
this.objectMap[obj._](this, obj)
|
|
|
|
}
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
vector(fn: (item: unknown) => void, items: unknown[]): void {
|
2021-11-23 00:03:59 +03:00
|
|
|
this.count += 8
|
|
|
|
items.forEach((it) => fn.call(this, it))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* Writer for TL objects.
|
|
|
|
*/
|
2021-11-23 00:03:59 +03:00
|
|
|
export class TlBinaryWriter {
|
2024-02-28 00:33:23 +03:00
|
|
|
static platform: ITlPlatform
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
readonly dataView: DataView
|
|
|
|
readonly uint8View: Uint8Array
|
2022-08-29 16:22:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current position in the buffer.
|
|
|
|
*/
|
2021-11-23 00:03:59 +03:00
|
|
|
pos: number
|
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* @param objectMap Writers map
|
|
|
|
* @param buffer Buffer to write to
|
|
|
|
* @param start Position to start writing at
|
|
|
|
*/
|
2023-10-06 21:15:52 +03:00
|
|
|
constructor(
|
|
|
|
readonly objectMap: TlWriterMap | undefined,
|
2023-10-16 19:23:53 +03:00
|
|
|
data: ArrayBuffer,
|
2023-10-06 21:15:52 +03:00
|
|
|
start = 0,
|
|
|
|
) {
|
2023-10-16 19:23:53 +03:00
|
|
|
if (ArrayBuffer.isView(data)) {
|
|
|
|
this.pos = start
|
|
|
|
this.dataView = new DataView(data.buffer, data.byteOffset, data.byteLength)
|
|
|
|
this.uint8View = new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
|
|
|
|
} else {
|
|
|
|
this.pos = start
|
|
|
|
this.dataView = new DataView(data)
|
|
|
|
this.uint8View = new Uint8Array(data)
|
|
|
|
}
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* Create a new writer with the given size.
|
|
|
|
*
|
|
|
|
* @param objectMap Writers map
|
|
|
|
* @param size Size of the writer's buffer
|
|
|
|
*/
|
2024-02-28 00:33:23 +03:00
|
|
|
static alloc(objectMap: TlWriterMap | undefined, size: number): TlBinaryWriter {
|
|
|
|
return new TlBinaryWriter(objectMap, new ArrayBuffer(size))
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* Create a new writer without objects map for manual usage
|
|
|
|
*
|
2023-10-16 19:23:53 +03:00
|
|
|
* @param buffer Buffer to write to, or its size
|
2022-08-29 16:22:57 +03:00
|
|
|
* @param start Position to start writing at
|
|
|
|
*/
|
2024-02-28 00:33:23 +03:00
|
|
|
static manual(buffer: ArrayBuffer | number, start = 0): TlBinaryWriter {
|
2023-10-16 19:23:53 +03:00
|
|
|
if (typeof buffer === 'number') buffer = new ArrayBuffer(buffer)
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2024-02-28 00:33:23 +03:00
|
|
|
return new TlBinaryWriter(undefined, buffer, start)
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* Serialize a single object
|
|
|
|
*
|
|
|
|
* @param objectMap Writers map
|
|
|
|
* @param obj Object to serialize
|
|
|
|
* @param knownSize In case the size is known, pass it here
|
|
|
|
*/
|
2024-02-23 00:24:21 +03:00
|
|
|
static serializeObject(
|
|
|
|
objectMap: TlWriterMap,
|
|
|
|
obj: { _: string },
|
|
|
|
knownSize = -1,
|
|
|
|
): Uint8Array {
|
2023-07-20 22:07:07 +03:00
|
|
|
if (knownSize === -1) {
|
2024-02-23 00:24:21 +03:00
|
|
|
knownSize =
|
2024-02-28 00:33:23 +03:00
|
|
|
objectMap._staticSize[obj._] || TlSerializationCounter.countNeededBytes(objectMap, obj)
|
2023-07-20 22:07:07 +03:00
|
|
|
}
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2024-02-28 00:33:23 +03:00
|
|
|
const writer = TlBinaryWriter.alloc(objectMap, knownSize)
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
writer.object(obj)
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
return writer.uint8View
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int(val: number): void {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.dataView.setInt32(this.pos, val, true)
|
2021-11-23 00:03:59 +03:00
|
|
|
this.pos += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
uint(val: number): void {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.dataView.setUint32(this.pos, val, true)
|
2021-11-23 00:03:59 +03:00
|
|
|
this.pos += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
int53(val: number): void {
|
|
|
|
// inlined fromNumber from Long
|
2023-10-16 19:23:53 +03:00
|
|
|
this.dataView.setInt32(this.pos, val % TWO_PWR_32_DBL | 0, true)
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
if (val < 0) {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.dataView.setInt32(this.pos + 4, (val / TWO_PWR_32_DBL - 1) | 0, true)
|
2021-11-23 00:03:59 +03:00
|
|
|
} else {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.dataView.setInt32(this.pos + 4, (val / TWO_PWR_32_DBL) | 0, true)
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this.pos += 8
|
|
|
|
}
|
|
|
|
|
|
|
|
null(): void {
|
|
|
|
this.uint(0x56730bcc)
|
|
|
|
}
|
|
|
|
|
|
|
|
long(val: Long): void {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.dataView.setInt32(this.pos, val.low, true)
|
|
|
|
this.dataView.setInt32(this.pos + 4, val.high, true)
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
this.pos += 8
|
|
|
|
}
|
|
|
|
|
|
|
|
float(val: number): void {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.dataView.setFloat32(this.pos, val, true)
|
2021-11-23 00:03:59 +03:00
|
|
|
this.pos += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
double(val: number): void {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.dataView.setFloat64(this.pos, val, true)
|
2021-11-23 00:03:59 +03:00
|
|
|
this.pos += 8
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean(val: boolean): void {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.dataView.setInt32(this.pos, val ? 0x997275b5 : 0xbc799737, true)
|
2021-11-23 00:03:59 +03:00
|
|
|
this.pos += 4
|
|
|
|
}
|
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* Write raw bytes to the buffer
|
|
|
|
* @param val Buffer to write
|
|
|
|
*/
|
2023-10-16 19:23:53 +03:00
|
|
|
raw(val: Uint8Array): void {
|
|
|
|
this.uint8View.set(val, this.pos)
|
|
|
|
this.pos += val.byteLength
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
int128(val: Uint8Array): void {
|
|
|
|
if (val.byteLength !== 16) throw new Error('Invalid int128 length')
|
|
|
|
this.raw(val)
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
int256(val: Uint8Array): void {
|
|
|
|
if (val.byteLength !== 32) throw new Error('Invalid int256 length')
|
|
|
|
this.raw(val)
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
bytes(val: Uint8Array): void {
|
|
|
|
const length = val.byteLength
|
2021-11-23 00:03:59 +03:00
|
|
|
let padding
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
if (length <= 253) {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.uint8View[this.pos++] = length
|
2021-11-23 00:03:59 +03:00
|
|
|
padding = (length + 1) % 4
|
|
|
|
} else {
|
2023-10-16 19:23:53 +03:00
|
|
|
this.uint8View[this.pos++] = 254
|
|
|
|
this.uint8View[this.pos++] = length & 0xff
|
|
|
|
this.uint8View[this.pos++] = (length >> 8) & 0xff
|
|
|
|
this.uint8View[this.pos++] = (length >> 16) & 0xff
|
2021-11-23 00:03:59 +03:00
|
|
|
padding = length % 4
|
|
|
|
}
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
this.uint8View.set(val, this.pos)
|
|
|
|
this.pos += length
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
if (padding > 0) {
|
|
|
|
padding = 4 - padding
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
while (padding--) this.uint8View[this.pos++] = 0
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string(val: string): void {
|
2024-02-28 00:33:23 +03:00
|
|
|
this.bytes(TlBinaryWriter.platform.utf8Encode(val))
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
2023-06-05 03:30:48 +03:00
|
|
|
// hot path, avoid additional runtime checks
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
object(obj: any): void {
|
2021-11-23 00:03:59 +03:00
|
|
|
const fn = this.objectMap![obj._]
|
|
|
|
if (!fn) throw new Error(`Unknown object ${obj._}`)
|
|
|
|
fn(this, obj)
|
|
|
|
}
|
|
|
|
|
2023-09-24 01:32:22 +03:00
|
|
|
vector(fn: (item: unknown, bare?: boolean) => void, val: unknown[], bare?: boolean): void {
|
2021-11-23 00:03:59 +03:00
|
|
|
if (!bare) this.uint(0x1cb5c415)
|
|
|
|
this.uint(val.length)
|
|
|
|
|
|
|
|
val.forEach((it) => fn.call(this, it, bare))
|
|
|
|
}
|
|
|
|
|
2022-08-29 16:22:57 +03:00
|
|
|
/**
|
|
|
|
* Get the resulting buffer
|
|
|
|
*/
|
2023-10-16 19:23:53 +03:00
|
|
|
result(): Uint8Array {
|
|
|
|
return this.uint8View.subarray(0, this.pos)
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
}
|