@tgsnake/skema 1.13.0 → 2.0.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.
Files changed (147) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +124 -15
  3. package/dist/mod.d.ts +10 -0
  4. package/dist/mod.js +10 -0
  5. package/dist/src/Logger.d.ts +20 -0
  6. package/dist/src/Logger.js +23 -0
  7. package/dist/src/deps.d.ts +28 -0
  8. package/dist/src/deps.js +34 -0
  9. package/dist/src/errors/Base.d.ts +53 -0
  10. package/dist/src/errors/Base.js +114 -0
  11. package/dist/src/errors/Client.d.ts +69 -0
  12. package/dist/src/errors/Client.js +69 -0
  13. package/dist/src/errors/File.d.ts +89 -0
  14. package/dist/src/errors/File.js +99 -0
  15. package/dist/src/errors/RpcError.d.ts +115 -0
  16. package/dist/src/errors/RpcError.js +226 -0
  17. package/dist/src/errors/SecretChat.d.ts +78 -0
  18. package/dist/src/errors/SecretChat.js +88 -0
  19. package/dist/src/errors/WebSocket.d.ts +70 -0
  20. package/dist/src/errors/WebSocket.js +80 -0
  21. package/dist/{errors → src/errors}/exceptions/All.d.ts +14 -0
  22. package/dist/{errors → src/errors}/exceptions/All.js +15 -5
  23. package/dist/{errors → src/errors}/exceptions/BadRequest400.d.ts +15 -1
  24. package/dist/src/errors/exceptions/BadRequest400.js +2683 -0
  25. package/dist/{errors → src/errors}/exceptions/Flood420.d.ts +15 -1
  26. package/dist/{errors → src/errors}/exceptions/Flood420.js +25 -24
  27. package/dist/{errors → src/errors}/exceptions/Forbidden403.d.ts +15 -1
  28. package/dist/src/errors/exceptions/Forbidden403.js +243 -0
  29. package/dist/{errors → src/errors}/exceptions/InternalServerError500.d.ts +15 -1
  30. package/dist/src/errors/exceptions/InternalServerError500.js +255 -0
  31. package/dist/{errors → src/errors}/exceptions/NotAcceptable406.d.ts +15 -1
  32. package/dist/{errors → src/errors}/exceptions/NotAcceptable406.js +51 -76
  33. package/dist/src/errors/exceptions/NotFound404.d.ts +27 -0
  34. package/dist/src/errors/exceptions/NotFound404.js +27 -0
  35. package/dist/src/errors/exceptions/SeeOther303.d.ts +39 -0
  36. package/dist/src/errors/exceptions/SeeOther303.js +39 -0
  37. package/dist/src/errors/exceptions/ServiceUnavailable503.d.ts +31 -0
  38. package/dist/src/errors/exceptions/ServiceUnavailable503.js +31 -0
  39. package/dist/{errors → src/errors}/exceptions/Unauthorized401.d.ts +15 -1
  40. package/dist/src/errors/exceptions/Unauthorized401.js +55 -0
  41. package/dist/src/errors/exceptions/index.d.ts +24 -0
  42. package/dist/src/errors/exceptions/index.js +24 -0
  43. package/dist/src/errors/index.d.ts +142 -0
  44. package/dist/src/errors/index.js +178 -0
  45. package/dist/src/helpers.d.ts +102 -0
  46. package/dist/src/helpers.js +176 -0
  47. package/dist/src/index.d.ts +12 -0
  48. package/dist/src/index.js +12 -0
  49. package/dist/{raw → src/raw}/All.d.ts +14 -0
  50. package/dist/{raw → src/raw}/All.js +15 -4
  51. package/dist/{raw → src/raw}/Raw.d.ts +17205 -3
  52. package/dist/{raw → src/raw}/Raw.js +44035 -21917
  53. package/dist/src/raw/UpdateSecretChat.d.ts +186 -0
  54. package/dist/src/raw/UpdateSecretChat.js +215 -0
  55. package/dist/src/raw/core/GzipPacked.d.ts +63 -0
  56. package/dist/src/raw/core/GzipPacked.js +76 -0
  57. package/dist/src/raw/core/Message.d.ts +68 -0
  58. package/dist/src/raw/core/Message.js +105 -0
  59. package/dist/src/raw/core/MsgContainer.d.ts +56 -0
  60. package/dist/src/raw/core/MsgContainer.js +77 -0
  61. package/dist/src/raw/core/TLObject.d.ts +108 -0
  62. package/dist/src/raw/core/TLObject.js +209 -0
  63. package/dist/src/raw/core/index.d.ts +19 -0
  64. package/dist/src/raw/core/index.js +19 -0
  65. package/dist/src/raw/core/primitive/Bool.d.ts +104 -0
  66. package/dist/src/raw/core/primitive/Bool.js +120 -0
  67. package/dist/src/raw/core/primitive/Bytes.d.ts +45 -0
  68. package/dist/src/raw/core/primitive/Bytes.js +76 -0
  69. package/dist/src/raw/core/primitive/Double.d.ts +37 -0
  70. package/dist/src/raw/core/primitive/Double.js +51 -0
  71. package/dist/src/raw/core/primitive/Float.d.ts +37 -0
  72. package/dist/src/raw/core/primitive/Float.js +51 -0
  73. package/dist/src/raw/core/primitive/Int.d.ts +144 -0
  74. package/dist/src/raw/core/primitive/Int.js +237 -0
  75. package/dist/src/raw/core/primitive/String.d.ts +37 -0
  76. package/dist/src/raw/core/primitive/String.js +42 -0
  77. package/dist/src/raw/core/primitive/Vector.d.ts +56 -0
  78. package/dist/src/raw/core/primitive/Vector.js +93 -0
  79. package/dist/src/raw/core/primitive/index.d.ts +20 -0
  80. package/dist/src/raw/core/primitive/index.js +20 -0
  81. package/dist/src/raw/index.d.ts +17 -0
  82. package/dist/src/raw/index.js +17 -0
  83. package/package.json +13 -11
  84. package/dist/Logger.d.ts +0 -3
  85. package/dist/Logger.js +0 -9
  86. package/dist/deps.d.ts +0 -8
  87. package/dist/deps.js +0 -19
  88. package/dist/errors/Base.d.ts +0 -9
  89. package/dist/errors/Base.js +0 -50
  90. package/dist/errors/Client.d.ts +0 -21
  91. package/dist/errors/Client.js +0 -29
  92. package/dist/errors/File.d.ts +0 -13
  93. package/dist/errors/File.js +0 -30
  94. package/dist/errors/RpcError.d.ts +0 -22
  95. package/dist/errors/RpcError.js +0 -120
  96. package/dist/errors/SecretChat.d.ts +0 -13
  97. package/dist/errors/SecretChat.js +0 -30
  98. package/dist/errors/WebSocket.d.ts +0 -13
  99. package/dist/errors/WebSocket.js +0 -30
  100. package/dist/errors/exceptions/BadRequest400.js +0 -3352
  101. package/dist/errors/exceptions/Forbidden403.js +0 -290
  102. package/dist/errors/exceptions/InternalServerError500.js +0 -305
  103. package/dist/errors/exceptions/NotFound404.d.ts +0 -13
  104. package/dist/errors/exceptions/NotFound404.js +0 -19
  105. package/dist/errors/exceptions/SeeOther303.d.ts +0 -25
  106. package/dist/errors/exceptions/SeeOther303.js +0 -34
  107. package/dist/errors/exceptions/ServiceUnavailable503.d.ts +0 -17
  108. package/dist/errors/exceptions/ServiceUnavailable503.js +0 -24
  109. package/dist/errors/exceptions/Unauthorized401.js +0 -54
  110. package/dist/errors/exceptions/index.d.ts +0 -10
  111. package/dist/errors/exceptions/index.js +0 -48
  112. package/dist/errors/index.d.ts +0 -31
  113. package/dist/errors/index.js +0 -111
  114. package/dist/helpers.d.ts +0 -6
  115. package/dist/helpers.js +0 -85
  116. package/dist/index.d.ts +0 -3
  117. package/dist/index.js +0 -34
  118. package/dist/raw/UpdateSecretChat.d.ts +0 -43
  119. package/dist/raw/UpdateSecretChat.js +0 -91
  120. package/dist/raw/core/GzipPacked.d.ts +0 -9
  121. package/dist/raw/core/GzipPacked.js +0 -59
  122. package/dist/raw/core/Message.d.ts +0 -12
  123. package/dist/raw/core/Message.js +0 -80
  124. package/dist/raw/core/MsgContainer.d.ts +0 -10
  125. package/dist/raw/core/MsgContainer.js +0 -68
  126. package/dist/raw/core/TLObject.d.ts +0 -18
  127. package/dist/raw/core/TLObject.js +0 -108
  128. package/dist/raw/core/index.d.ts +0 -6
  129. package/dist/raw/core/index.js +0 -46
  130. package/dist/raw/core/primitive/Bool.d.ts +0 -19
  131. package/dist/raw/core/primitive/Bool.js +0 -41
  132. package/dist/raw/core/primitive/Bytes.d.ts +0 -6
  133. package/dist/raw/core/primitive/Bytes.js +0 -41
  134. package/dist/raw/core/primitive/Double.d.ts +0 -6
  135. package/dist/raw/core/primitive/Double.js +0 -24
  136. package/dist/raw/core/primitive/Float.d.ts +0 -6
  137. package/dist/raw/core/primitive/Float.js +0 -24
  138. package/dist/raw/core/primitive/Int.d.ts +0 -22
  139. package/dist/raw/core/primitive/Int.js +0 -122
  140. package/dist/raw/core/primitive/String.d.ts +0 -6
  141. package/dist/raw/core/primitive/String.js +0 -15
  142. package/dist/raw/core/primitive/Vector.d.ts +0 -8
  143. package/dist/raw/core/primitive/Vector.js +0 -49
  144. package/dist/raw/core/primitive/index.d.ts +0 -7
  145. package/dist/raw/core/primitive/index.js +0 -22
  146. package/dist/raw/index.d.ts +0 -4
  147. package/dist/raw/index.js +0 -17
@@ -0,0 +1,105 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software: you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ import { TLObject } from './TLObject.js';
11
+ import { BytesIO, Buffer } from '../../deps.js';
12
+ import * as Primitive from './primitive/index.js';
13
+ /**
14
+ * Utility helper converting a 64-bit integer into a little-endian byte representation.
15
+ *
16
+ * @param value - The bigint value to convert.
17
+ * @returns A Buffer representing the little-endian bytes of the bigint.
18
+ */
19
+ function toBytes(value) {
20
+ const bytesArray = [];
21
+ for (let i = 0; i < 8; i++) {
22
+ let shift = value >> BigInt(8 * i);
23
+ shift &= BigInt(255);
24
+ bytesArray[i] = Number(String(shift));
25
+ }
26
+ return Buffer.from(bytesArray);
27
+ }
28
+ /**
29
+ * Represents a standard MTProto transport container message.
30
+ *
31
+ * @remarks
32
+ * Every message transmitted or received over the MTProto protocol is wrapped in a `Message` envelope.
33
+ * The envelope includes metadata crucial for routing and ordering: the message ID (which indicates
34
+ * client/server time synchronization), the message sequence number, the body byte length, and the actual
35
+ * serialized payload/body TLObject.
36
+ *
37
+ * @extends TLObject
38
+ */
39
+ export class Message extends TLObject {
40
+ /**
41
+ * The unique class ID identifier representing `Message` (0x5bb8e511).
42
+ */
43
+ static ID = 0x5bb8e511; // hex(crc32(b"message msg_id:long seqno:int bytes:int body:Object = Message"))
44
+ /**
45
+ * The unique 64-bit message identifier, representing the creation time of the message in seconds multiplied by 2^32.
46
+ */
47
+ msgId;
48
+ /**
49
+ * The message sequence number, indicating the order of the message in the session.
50
+ */
51
+ seqNo;
52
+ /**
53
+ * The length of the serialized message body in bytes.
54
+ */
55
+ length;
56
+ /**
57
+ * The inner body deserialized as a `TLObject`.
58
+ */
59
+ body;
60
+ /**
61
+ * Constructs a new Message envelope.
62
+ *
63
+ * @param body - The inner body payload of the message.
64
+ * @param msgId - The 64-bit message identifier.
65
+ * @param seqNo - The session message sequence number.
66
+ * @param length - The byte length of the body.
67
+ */
68
+ constructor(body, msgId, seqNo, length) {
69
+ super();
70
+ this.className = 'Message';
71
+ this._slots = ['body', 'msgId', 'seqNo', 'length'];
72
+ this.msgId = msgId;
73
+ this.seqNo = seqNo;
74
+ this.length = length;
75
+ this.body = body;
76
+ }
77
+ /**
78
+ * Reads and deserializes a `Message` envelope from a binary stream.
79
+ *
80
+ * @param data - The `BytesIO` stream containing the serialized message envelope.
81
+ * @param _args - Unused additional parameters.
82
+ * @returns A promise resolving to the deserialized `Message` instance.
83
+ */
84
+ static async read(data, ..._args) {
85
+ const msgId = await Primitive.Long.read(data);
86
+ const seqNo = await Primitive.Int.read(data);
87
+ const length = await Primitive.Int.read(data);
88
+ const body = data.read(length);
89
+ return new Message(await TLObject.read(new BytesIO(body)), msgId, seqNo, length);
90
+ }
91
+ /**
92
+ * Serializes the message envelope and its body into a raw binary buffer.
93
+ *
94
+ * @override
95
+ * @returns A Buffer representing the complete message envelope structure.
96
+ */
97
+ write() {
98
+ const bytes = new BytesIO();
99
+ bytes.write(toBytes(this.msgId));
100
+ bytes.write(Primitive.Int.write(this.seqNo));
101
+ bytes.write(Primitive.Int.write(this.length));
102
+ bytes.write(this.body.write());
103
+ return Buffer.from(bytes.buffer);
104
+ }
105
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software: you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ import { TLObject } from './TLObject.js';
11
+ import { BytesIO, Buffer } from '../../deps.js';
12
+ import { Message } from './Message.js';
13
+ /**
14
+ * Represents a standard MTProto Message Container (`msg_container`).
15
+ *
16
+ * @remarks
17
+ * A message container is a core MTProto concept allowing multiple independent messages (requests or updates)
18
+ * to be grouped together and sent within a single network transaction. This reduces overhead and enables
19
+ * parallel execution or efficient state synchronization between client and server.
20
+ *
21
+ * @extends TLObject
22
+ */
23
+ export declare class MsgContainer extends TLObject {
24
+ /**
25
+ * The unique class ID identifier representing `MsgContainer` (0x73f1f8dc).
26
+ */
27
+ static ID: number;
28
+ /**
29
+ * The list of MTProto `Message` envelopes packaged inside this container.
30
+ */
31
+ messages: Array<Message>;
32
+ /**
33
+ * Constructs a new MsgContainer.
34
+ *
35
+ * @param messages - An array of `Message` instances to package.
36
+ */
37
+ constructor(messages: Array<Message>);
38
+ /**
39
+ * Reads, unpacks, and deserializes a `MsgContainer` from a binary stream.
40
+ *
41
+ * @remarks
42
+ * Reads the message count prefix, then iterates sequentially to deserialize each nested `Message` envelope.
43
+ *
44
+ * @param data - The `BytesIO` stream containing the serialized message container.
45
+ * @param _args - Unused additional parameters.
46
+ * @returns A promise resolving to the deserialized `MsgContainer` instance.
47
+ */
48
+ static read(data: BytesIO, ..._args: Array<any>): Promise<MsgContainer>;
49
+ /**
50
+ * Serializes the container header, message counts, and nested envelopes into a raw binary buffer.
51
+ *
52
+ * @override
53
+ * @returns A Buffer containing the serialized representation of the message container.
54
+ */
55
+ write(): Buffer;
56
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software: you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ import { TLObject } from './TLObject.js';
11
+ import { BytesIO, Buffer } from '../../deps.js';
12
+ import * as Primitive from './primitive/index.js';
13
+ import { Message } from './Message.js';
14
+ /**
15
+ * Represents a standard MTProto Message Container (`msg_container`).
16
+ *
17
+ * @remarks
18
+ * A message container is a core MTProto concept allowing multiple independent messages (requests or updates)
19
+ * to be grouped together and sent within a single network transaction. This reduces overhead and enables
20
+ * parallel execution or efficient state synchronization between client and server.
21
+ *
22
+ * @extends TLObject
23
+ */
24
+ export class MsgContainer extends TLObject {
25
+ /**
26
+ * The unique class ID identifier representing `MsgContainer` (0x73f1f8dc).
27
+ */
28
+ static ID = 0x73f1f8dc;
29
+ /**
30
+ * The list of MTProto `Message` envelopes packaged inside this container.
31
+ */
32
+ messages;
33
+ /**
34
+ * Constructs a new MsgContainer.
35
+ *
36
+ * @param messages - An array of `Message` instances to package.
37
+ */
38
+ constructor(messages) {
39
+ super();
40
+ this._slots = ['messages'];
41
+ this.className = 'MsgContainer';
42
+ this.messages = messages;
43
+ }
44
+ /**
45
+ * Reads, unpacks, and deserializes a `MsgContainer` from a binary stream.
46
+ *
47
+ * @remarks
48
+ * Reads the message count prefix, then iterates sequentially to deserialize each nested `Message` envelope.
49
+ *
50
+ * @param data - The `BytesIO` stream containing the serialized message container.
51
+ * @param _args - Unused additional parameters.
52
+ * @returns A promise resolving to the deserialized `MsgContainer` instance.
53
+ */
54
+ static async read(data, ..._args) {
55
+ const count = await Primitive.Int.read(data);
56
+ const messages = [];
57
+ for (let i = 0; i < count; i++) {
58
+ messages.push(await Message.read(data));
59
+ }
60
+ return new MsgContainer(messages);
61
+ }
62
+ /**
63
+ * Serializes the container header, message counts, and nested envelopes into a raw binary buffer.
64
+ *
65
+ * @override
66
+ * @returns A Buffer containing the serialized representation of the message container.
67
+ */
68
+ write() {
69
+ const bytes = new BytesIO();
70
+ bytes.write(Primitive.Int.write(MsgContainer.ID, false));
71
+ bytes.write(Primitive.Int.write(this.messages.length));
72
+ for (const message of this.messages) {
73
+ bytes.write(message.write());
74
+ }
75
+ return Buffer.from(bytes.buffer);
76
+ }
77
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software: you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ import { BytesIO, Buffer } from '../../deps.js';
11
+ /**
12
+ * Base abstract class for all Type Language (TL) schema objects within the MTProto protocol.
13
+ *
14
+ * @remarks
15
+ * In the Telegram MTProto protocol, messages and API payloads are defined in a custom ID-based schema language
16
+ * called Type Language (TL). `TLObject` is the root class providing fundamental binary serialization (`write`)
17
+ * and deserialization (`read`) interfaces, alongside custom console print formatters and standard JSON serializers.
18
+ */
19
+ export declare class TLObject {
20
+ /**
21
+ * The internal slot list storing serialization field property names.
22
+ *
23
+ * @internal
24
+ */
25
+ _slots: Array<string>;
26
+ /**
27
+ * A dynamic reference to the current subclass constructor.
28
+ * Enables late static binding for instance methods to access static helper operations.
29
+ */
30
+ cls: any;
31
+ /**
32
+ * The unique 32-bit integer identifier (CRC32 checksum) of this TL constructor.
33
+ */
34
+ constructorId: number;
35
+ /**
36
+ * The unique 32-bit integer identifier of the parent/abstract type this TL subclass represents.
37
+ */
38
+ subclassOfId: number;
39
+ /**
40
+ * The canonical string name of this class representation.
41
+ */
42
+ className: string;
43
+ /**
44
+ * The underlying classification type (e.g. `request`, `constructor`, `function`).
45
+ */
46
+ classType: string;
47
+ /**
48
+ * Constructs a new TLObject instance, initializing slots and late-bound constructors.
49
+ */
50
+ constructor();
51
+ /**
52
+ * Reads and deserializes a generic TLObject from a binary stream.
53
+ *
54
+ * @remarks
55
+ * This method first reads the leading 32-bit little-endian integer from the stream, which is
56
+ * treated as the unique `constructorId` (or Class ID). It resolves the constructor class dynamically
57
+ * from the global TL object map (`AllTLObject`) and delegates parsing to the subclass's static `read` method.
58
+ *
59
+ * @param data - The `BytesIO` buffer stream containing the serialized TLObject.
60
+ * @param args - Additional arguments passed down to the resolved subclass's static read implementation.
61
+ * @returns A promise resolving to the parsed class instance.
62
+ */
63
+ static read(data: BytesIO, ...args: Array<any>): Promise<any>;
64
+ /**
65
+ * Serializes constructor arguments into a raw binary buffer.
66
+ *
67
+ * @remarks
68
+ * This base implementation acts as an abstract stub returning an empty Buffer. Subclasses override
69
+ * this static method to handle specific binary serialization layouts.
70
+ *
71
+ * @param _args - Arguments to be serialized.
72
+ * @returns An empty `Buffer` of size 0.
73
+ */
74
+ static write(..._args: Array<any>): Buffer;
75
+ /**
76
+ * Deserializes a binary stream into this specific TLObject instance.
77
+ *
78
+ * @param data - The `BytesIO` buffer stream containing the serialized TLObject.
79
+ * @param args - Additional arguments passed down to the read parser.
80
+ * @returns A promise resolving to the parsed instance.
81
+ */
82
+ read(data: BytesIO, ...args: Array<any>): Promise<any>;
83
+ /**
84
+ * Serializes the current class instance into a raw binary buffer.
85
+ *
86
+ * @param args - Arguments passed down to the writer.
87
+ * @returns A Buffer containing the serialized representation of this object.
88
+ */
89
+ write(...args: Array<any>): Buffer;
90
+ /**
91
+ * Serializes the TLObject instance to a clean JSON-compatible representation.
92
+ *
93
+ * @remarks
94
+ * Correctly stringifies extremely large fields (e.g. bigints) that are normally not
95
+ * serializable in standard `JSON.stringify` calls.
96
+ *
97
+ * @returns A plain object containing class attributes and values.
98
+ */
99
+ toJSON(): {
100
+ [key: string]: any;
101
+ };
102
+ /**
103
+ * Formats the TLObject into a structured string.
104
+ *
105
+ * @returns A string starting with the constructor name followed by a pretty JSON payload.
106
+ */
107
+ toString(): string;
108
+ }
@@ -0,0 +1,209 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software: you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ import { AllTLObject } from '../All.js';
11
+ import { Buffer, inspect } from '../../deps.js';
12
+ import { Logger } from '../../Logger.js';
13
+ import { Raw, Message, GzipPacked, Primitive, MsgContainer } from '../index.js';
14
+ function getModule(name) {
15
+ if (!name) {
16
+ throw new Error("name of module can't be undefined");
17
+ }
18
+ if (name === 'Message') {
19
+ return Message;
20
+ }
21
+ else if (name === 'GzipPacked') {
22
+ return GzipPacked;
23
+ }
24
+ else if (name === 'MsgContainer') {
25
+ return MsgContainer;
26
+ }
27
+ else if (name.startsWith('Primitive')) {
28
+ return Primitive[name.split('.')[1]];
29
+ }
30
+ else {
31
+ const split = name.split('.');
32
+ if (split.length == 3) {
33
+ return Raw[split[1]][split[2]];
34
+ }
35
+ return Raw[split[1]];
36
+ }
37
+ }
38
+ /**
39
+ * Base abstract class for all Type Language (TL) schema objects within the MTProto protocol.
40
+ *
41
+ * @remarks
42
+ * In the Telegram MTProto protocol, messages and API payloads are defined in a custom ID-based schema language
43
+ * called Type Language (TL). `TLObject` is the root class providing fundamental binary serialization (`write`)
44
+ * and deserialization (`read`) interfaces, alongside custom console print formatters and standard JSON serializers.
45
+ */
46
+ export class TLObject {
47
+ /**
48
+ * The internal slot list storing serialization field property names.
49
+ *
50
+ * @internal
51
+ */
52
+ _slots;
53
+ /**
54
+ * A dynamic reference to the current subclass constructor.
55
+ * Enables late static binding for instance methods to access static helper operations.
56
+ */
57
+ cls = this.constructor;
58
+ /**
59
+ * The unique 32-bit integer identifier (CRC32 checksum) of this TL constructor.
60
+ */
61
+ constructorId;
62
+ /**
63
+ * The unique 32-bit integer identifier of the parent/abstract type this TL subclass represents.
64
+ */
65
+ subclassOfId;
66
+ /**
67
+ * The canonical string name of this class representation.
68
+ */
69
+ className;
70
+ /**
71
+ * The underlying classification type (e.g. `request`, `constructor`, `function`).
72
+ */
73
+ classType;
74
+ /**
75
+ * Constructs a new TLObject instance, initializing slots and late-bound constructors.
76
+ */
77
+ constructor() {
78
+ this._slots = [];
79
+ this.constructorId = this.cls.ID ?? 0;
80
+ this.className = 'TLObject';
81
+ }
82
+ /**
83
+ * Reads and deserializes a generic TLObject from a binary stream.
84
+ *
85
+ * @remarks
86
+ * This method first reads the leading 32-bit little-endian integer from the stream, which is
87
+ * treated as the unique `constructorId` (or Class ID). It resolves the constructor class dynamically
88
+ * from the global TL object map (`AllTLObject`) and delegates parsing to the subclass's static `read` method.
89
+ *
90
+ * @param data - The `BytesIO` buffer stream containing the serialized TLObject.
91
+ * @param args - Additional arguments passed down to the resolved subclass's static read implementation.
92
+ * @returns A promise resolving to the parsed class instance.
93
+ */
94
+ static async read(data, ...args) {
95
+ const id = data.readUInt32LE(4);
96
+ Logger.debug(`[10] Reading TLObject with id: ${id.toString(16)} (${AllTLObject[id]})`);
97
+ const _class = getModule(AllTLObject[id]);
98
+ return await _class.read(data, ...args);
99
+ }
100
+ /**
101
+ * Serializes constructor arguments into a raw binary buffer.
102
+ *
103
+ * @remarks
104
+ * This base implementation acts as an abstract stub returning an empty Buffer. Subclasses override
105
+ * this static method to handle specific binary serialization layouts.
106
+ *
107
+ * @param _args - Arguments to be serialized.
108
+ * @returns An empty `Buffer` of size 0.
109
+ */
110
+ static write(..._args) {
111
+ return Buffer.alloc(0);
112
+ }
113
+ /**
114
+ * Deserializes a binary stream into this specific TLObject instance.
115
+ *
116
+ * @param data - The `BytesIO` buffer stream containing the serialized TLObject.
117
+ * @param args - Additional arguments passed down to the read parser.
118
+ * @returns A promise resolving to the parsed instance.
119
+ */
120
+ read(data, ...args) {
121
+ return this.cls.read(data, ...args);
122
+ }
123
+ /**
124
+ * Serializes the current class instance into a raw binary buffer.
125
+ *
126
+ * @param args - Arguments passed down to the writer.
127
+ * @returns A Buffer containing the serialized representation of this object.
128
+ */
129
+ write(...args) {
130
+ return this.cls.write(...args);
131
+ }
132
+ /**
133
+ * Custom inspect handler for Node.js console output (`util.inspect`).
134
+ * Omits private properties (prefixed with `_`) and structural metadata to keep console outputs clean.
135
+ *
136
+ * @ignore
137
+ * @returns An object containing enumerable public properties to display.
138
+ */
139
+ [Symbol.for('nodejs.util.inspect.custom')]() {
140
+ const toPrint = {
141
+ _: this.className,
142
+ };
143
+ const ignore = ['slots', 'className', 'constructorId', 'subclassOfId', 'classType', 'cls'];
144
+ for (const key in this) {
145
+ if (Object.prototype.hasOwnProperty.call(this, key)) {
146
+ const value = this[key];
147
+ if (ignore.includes(key))
148
+ continue;
149
+ if (!key.startsWith('_') && value !== undefined && value !== null) {
150
+ toPrint[key] = value;
151
+ }
152
+ }
153
+ }
154
+ return toPrint;
155
+ }
156
+ /**
157
+ * Custom inspect handler for Deno runtime terminal output (`Deno.inspect`).
158
+ * Wraps the Node.js inspect output and enables terminal colors.
159
+ *
160
+ * @ignore
161
+ * @returns The colorized, formatted representation of the TLObject.
162
+ */
163
+ [Symbol.for('Deno.customInspect')]() {
164
+ // @ts-ignore: deno compatibility
165
+ return String(inspect(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
166
+ }
167
+ /**
168
+ * Serializes the TLObject instance to a clean JSON-compatible representation.
169
+ *
170
+ * @remarks
171
+ * Correctly stringifies extremely large fields (e.g. bigints) that are normally not
172
+ * serializable in standard `JSON.stringify` calls.
173
+ *
174
+ * @returns A plain object containing class attributes and values.
175
+ */
176
+ toJSON() {
177
+ const toPrint = {
178
+ _: this.className,
179
+ };
180
+ const ignore = ['slots', 'className', 'constructorId', 'subclassOfId', 'classType', 'cls'];
181
+ for (const key in this) {
182
+ if (Object.prototype.hasOwnProperty.call(this, key)) {
183
+ const value = this[key];
184
+ if (ignore.includes(key))
185
+ continue;
186
+ if (!key.startsWith('_') && value !== undefined && value !== null) {
187
+ if (typeof value === 'bigint') {
188
+ toPrint[key] = String(value);
189
+ }
190
+ else if (Array.isArray(value)) {
191
+ toPrint[key] = value.map((v) => (typeof v === 'bigint' ? String(v) : v));
192
+ }
193
+ else {
194
+ toPrint[key] = value;
195
+ }
196
+ }
197
+ }
198
+ }
199
+ return toPrint;
200
+ }
201
+ /**
202
+ * Formats the TLObject into a structured string.
203
+ *
204
+ * @returns A string starting with the constructor name followed by a pretty JSON payload.
205
+ */
206
+ toString() {
207
+ return `[constructor of ${this.className}] ${JSON.stringify(this, null, 2)}`;
208
+ }
209
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software: you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ /**
11
+ * Re-exports core MTProto primitive builders, zlib compression packer,
12
+ * message envelopes, message containers, and the base TLObject class.
13
+ */
14
+ import * as Primitive from './primitive/index.js';
15
+ export { Primitive };
16
+ export { TLObject } from './TLObject.js';
17
+ export { GzipPacked } from './GzipPacked.js';
18
+ export { Message } from './Message.js';
19
+ export { MsgContainer } from './MsgContainer.js';
@@ -0,0 +1,19 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software: you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ /**
11
+ * Re-exports core MTProto primitive builders, zlib compression packer,
12
+ * message envelopes, message containers, and the base TLObject class.
13
+ */
14
+ import * as Primitive from './primitive/index.js';
15
+ export { Primitive };
16
+ export { TLObject } from './TLObject.js';
17
+ export { GzipPacked } from './GzipPacked.js';
18
+ export { Message } from './Message.js';
19
+ export { MsgContainer } from './MsgContainer.js';