@tgsnake/skema 1.12.0 → 2.0.0-browser.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 +125 -16
  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 +29 -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 +47 -5
  50. package/dist/{raw → src/raw}/All.js +48 -9
  51. package/dist/{raw → src/raw}/Raw.d.ts +17546 -9
  52. package/dist/{raw → src/raw}/Raw.js +44899 -21694
  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 +54 -50
  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,142 @@
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
+ export * as Exceptions from './exceptions/index.js';
11
+ export * as ClientError from './Client.js';
12
+ export * as WSError from './WebSocket.js';
13
+ export * as SecretChatError from './SecretChat.js';
14
+ export * as FileErrors from './File.js';
15
+ export { RPCError, UnknownError } from './RpcError.js';
16
+ import { BaseError } from './Base.js';
17
+ /**
18
+ * Error thrown when a promise or task execution exceeds a specified timeout threshold.
19
+ *
20
+ * @extends BaseError
21
+ * @example
22
+ * ```typescript
23
+ * throw new TimeoutError(5000);
24
+ * ```
25
+ */
26
+ export declare class TimeoutError extends BaseError {
27
+ /**
28
+ * The duration in milliseconds that was allowed before the timeout triggered.
29
+ */
30
+ timeout: number;
31
+ /**
32
+ * Constructs a new TimeoutError instance.
33
+ *
34
+ * @param timeout - The timeout limit in milliseconds.
35
+ */
36
+ constructor(timeout: number);
37
+ }
38
+ /**
39
+ * Error thrown when the client encounters a class constructor where it was expecting an executable function.
40
+ *
41
+ * @extends BaseError
42
+ * @example
43
+ * ```typescript
44
+ * throw new NotAFunctionClass('MyClass');
45
+ * ```
46
+ */
47
+ export declare class NotAFunctionClass extends BaseError {
48
+ message: string;
49
+ description: string;
50
+ /**
51
+ * Constructs a new NotAFunctionClass instance.
52
+ *
53
+ * @param className - The name of the invalid class.
54
+ */
55
+ constructor(className: string);
56
+ }
57
+ /**
58
+ * Error raised when the server responds with a bad message notification.
59
+ *
60
+ * @remarks
61
+ * Bad message notifications indicate structural, sequence, or time-sync errors in client messages.
62
+ * Common errors include out-of-sync system time, invalid sequence numbers (`seq_no`), or salt expiration.
63
+ *
64
+ * @extends BaseError
65
+ * @example
66
+ * ```typescript
67
+ * throw new BadMsgNotification(16);
68
+ * ```
69
+ */
70
+ export declare class BadMsgNotification extends BaseError {
71
+ /**
72
+ * Constructs a new BadMsgNotification instance.
73
+ *
74
+ * @param code - The bad message error code returned by the Telegram MTProto server.
75
+ */
76
+ constructor(code: number);
77
+ }
78
+ /**
79
+ * Custom error thrown when a security check, hash mismatch, or signature verification fails.
80
+ *
81
+ * @extends BaseError
82
+ * @example
83
+ * ```typescript
84
+ * SecurityError.check(user.isAuthenticated, "User must be authenticated");
85
+ * ```
86
+ */
87
+ export declare class SecurityError extends BaseError {
88
+ /**
89
+ * Constructs a new SecurityError instance.
90
+ *
91
+ * @param description - Detailed context of the security violation.
92
+ */
93
+ constructor(description?: string);
94
+ /**
95
+ * Asserts that a condition is true, throwing a SecurityError if it is not.
96
+ *
97
+ * @param cond - The boolean condition to evaluate.
98
+ * @param description - Detailed error message if assertion fails.
99
+ * @throws {SecurityError} If the condition is false.
100
+ */
101
+ static check(cond: boolean, description?: string): void;
102
+ }
103
+ /**
104
+ * Error raised when an assertion check for key, signature, or padding lengths fails.
105
+ *
106
+ * @extends SecurityError
107
+ * @example
108
+ * ```typescript
109
+ * SecurityCheckMismatch.check(serverNonce === expectedNonce, 'Nonce mismatch');
110
+ * ```
111
+ */
112
+ export declare class SecurityCheckMismatch extends SecurityError {
113
+ message: string;
114
+ /**
115
+ * Asserts that a condition is true, throwing a SecurityCheckMismatch if it is not.
116
+ *
117
+ * @param cond - The boolean condition to evaluate.
118
+ * @param description - Detailed error message if assertion fails.
119
+ * @throws {SecurityCheckMismatch} If the condition is false.
120
+ */
121
+ static check(cond: boolean, description?: string): void;
122
+ }
123
+ /**
124
+ * Error raised when the checksum or hash verification of a file downloaded from a Telegram CDN fails.
125
+ *
126
+ * @extends SecurityError
127
+ * @example
128
+ * ```typescript
129
+ * CDNFileHashMismatch.check(downloadedHash === correctHash, 'CDN file hash is corrupted');
130
+ * ```
131
+ */
132
+ export declare class CDNFileHashMismatch extends SecurityError {
133
+ message: string;
134
+ /**
135
+ * Asserts that a condition is true, throwing a CDNFileHashMismatch if it is not.
136
+ *
137
+ * @param cond - The boolean condition to evaluate.
138
+ * @param description - Detailed error message if assertion fails.
139
+ * @throws {CDNFileHashMismatch} If the condition is false.
140
+ */
141
+ static check(cond: boolean, description?: string): void;
142
+ }
@@ -0,0 +1,178 @@
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
+ export * as Exceptions from './exceptions/index.js';
11
+ export * as ClientError from './Client.js';
12
+ export * as WSError from './WebSocket.js';
13
+ export * as SecretChatError from './SecretChat.js';
14
+ export * as FileErrors from './File.js';
15
+ export { RPCError, UnknownError } from './RpcError.js';
16
+ import { BaseError } from './Base.js';
17
+ /**
18
+ * Error thrown when a promise or task execution exceeds a specified timeout threshold.
19
+ *
20
+ * @extends BaseError
21
+ * @example
22
+ * ```typescript
23
+ * throw new TimeoutError(5000);
24
+ * ```
25
+ */
26
+ export class TimeoutError extends BaseError {
27
+ /**
28
+ * The duration in milliseconds that was allowed before the timeout triggered.
29
+ */
30
+ timeout;
31
+ /**
32
+ * Constructs a new TimeoutError instance.
33
+ *
34
+ * @param timeout - The timeout limit in milliseconds.
35
+ */
36
+ constructor(timeout) {
37
+ super();
38
+ this.message = `Running timeout after ${timeout} ms`;
39
+ this.timeout = timeout;
40
+ this.description = `The function is running too long, until it reaches the time limit that has been given.`;
41
+ }
42
+ }
43
+ /**
44
+ * Error thrown when the client encounters a class constructor where it was expecting an executable function.
45
+ *
46
+ * @extends BaseError
47
+ * @example
48
+ * ```typescript
49
+ * throw new NotAFunctionClass('MyClass');
50
+ * ```
51
+ */
52
+ export class NotAFunctionClass extends BaseError {
53
+ message = '{value} is not a function.';
54
+ description = "The provided class {value} is not a function constructor, can't sending request with that class.";
55
+ /**
56
+ * Constructs a new NotAFunctionClass instance.
57
+ *
58
+ * @param className - The name of the invalid class.
59
+ */
60
+ constructor(className) {
61
+ super();
62
+ this.message = this.message.replace('{value}', className);
63
+ this.description = this.description.replace('{value}', className);
64
+ }
65
+ }
66
+ /**
67
+ * Error raised when the server responds with a bad message notification.
68
+ *
69
+ * @remarks
70
+ * Bad message notifications indicate structural, sequence, or time-sync errors in client messages.
71
+ * Common errors include out-of-sync system time, invalid sequence numbers (`seq_no`), or salt expiration.
72
+ *
73
+ * @extends BaseError
74
+ * @example
75
+ * ```typescript
76
+ * throw new BadMsgNotification(16);
77
+ * ```
78
+ */
79
+ export class BadMsgNotification extends BaseError {
80
+ /**
81
+ * Constructs a new BadMsgNotification instance.
82
+ *
83
+ * @param code - The bad message error code returned by the Telegram MTProto server.
84
+ */
85
+ constructor(code) {
86
+ const description = {
87
+ 16: 'The msg_id is too low, the client time has to be synchronized.',
88
+ 17: 'The msg_id is too high, the client time has to be synchronized.',
89
+ 18: 'Incorrect two lower order of the msg_id bits, the server expects the client message\nmsg_id to be divisible by 4.',
90
+ 19: 'The container msg_id is the same as the msg_id of a previously received message.',
91
+ 20: 'The message is too old, it cannot be verified by the server.',
92
+ 32: 'The msg_seqno is too low.',
93
+ 33: 'The msg_seqno is too high.',
94
+ 34: 'An even msg_seqno was expected, but an odd one was received.',
95
+ 35: 'An odd msg_seqno was expected, but an even one was received.',
96
+ 48: 'Incorrect server salt.',
97
+ 64: 'Invalid container.',
98
+ };
99
+ super(`[${code}] ${description[code] ?? 'Unknown Error'}`);
100
+ }
101
+ }
102
+ /**
103
+ * Custom error thrown when a security check, hash mismatch, or signature verification fails.
104
+ *
105
+ * @extends BaseError
106
+ * @example
107
+ * ```typescript
108
+ * SecurityError.check(user.isAuthenticated, "User must be authenticated");
109
+ * ```
110
+ */
111
+ export class SecurityError extends BaseError {
112
+ /**
113
+ * Constructs a new SecurityError instance.
114
+ *
115
+ * @param description - Detailed context of the security violation.
116
+ */
117
+ constructor(description) {
118
+ super();
119
+ this.description = description;
120
+ }
121
+ /**
122
+ * Asserts that a condition is true, throwing a SecurityError if it is not.
123
+ *
124
+ * @param cond - The boolean condition to evaluate.
125
+ * @param description - Detailed error message if assertion fails.
126
+ * @throws {SecurityError} If the condition is false.
127
+ */
128
+ static check(cond, description) {
129
+ if (!cond)
130
+ throw new SecurityError(description);
131
+ }
132
+ }
133
+ /**
134
+ * Error raised when an assertion check for key, signature, or padding lengths fails.
135
+ *
136
+ * @extends SecurityError
137
+ * @example
138
+ * ```typescript
139
+ * SecurityCheckMismatch.check(serverNonce === expectedNonce, 'Nonce mismatch');
140
+ * ```
141
+ */
142
+ export class SecurityCheckMismatch extends SecurityError {
143
+ message = 'A security check mismatch has occurred.';
144
+ /**
145
+ * Asserts that a condition is true, throwing a SecurityCheckMismatch if it is not.
146
+ *
147
+ * @param cond - The boolean condition to evaluate.
148
+ * @param description - Detailed error message if assertion fails.
149
+ * @throws {SecurityCheckMismatch} If the condition is false.
150
+ */
151
+ static check(cond, description) {
152
+ if (!cond)
153
+ throw new SecurityCheckMismatch(description);
154
+ }
155
+ }
156
+ /**
157
+ * Error raised when the checksum or hash verification of a file downloaded from a Telegram CDN fails.
158
+ *
159
+ * @extends SecurityError
160
+ * @example
161
+ * ```typescript
162
+ * CDNFileHashMismatch.check(downloadedHash === correctHash, 'CDN file hash is corrupted');
163
+ * ```
164
+ */
165
+ export class CDNFileHashMismatch extends SecurityError {
166
+ message = 'A CDN file hash mismatch has occurred.';
167
+ /**
168
+ * Asserts that a condition is true, throwing a CDNFileHashMismatch if it is not.
169
+ *
170
+ * @param cond - The boolean condition to evaluate.
171
+ * @param description - Detailed error message if assertion fails.
172
+ * @throws {CDNFileHashMismatch} If the condition is false.
173
+ */
174
+ static check(cond, description) {
175
+ if (!cond)
176
+ throw new CDNFileHashMismatch(description);
177
+ }
178
+ }
@@ -0,0 +1,102 @@
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 { Buffer } from './deps.js';
11
+ /**
12
+ * Converts a JS native `bigint` value into a Node.js `Buffer` with custom byte-padding, endianness, and sign representation.
13
+ *
14
+ * @remarks
15
+ * This utility handles both signed and unsigned numeric translations.
16
+ * For signed negative bigints, it computes the manual two's complement transformation
17
+ * in either little-endian or big-endian formats to match Telegram MTProto specifications.
18
+ *
19
+ * @param int - The bigint value to serialize into the buffer.
20
+ * @param padding - The absolute target size of the output buffer in bytes.
21
+ * @param litte - Set to `true` for little-endian encoding; set to `false` for big-endian. Defaults to `true`.
22
+ * @param signed - Set to `true` to allow signed integers and two's complement representation. Defaults to `false`.
23
+ * @returns A new `Buffer` representation of the bigint value.
24
+ *
25
+ * @throws {Error} If the bigint requires more bytes to represent than the specified padding size.
26
+ * @throws {Error} If the bigint is negative and `signed` is set to `false`.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * // Unsigned little-endian 4-byte padding
31
+ * const buf = bigintToBuffer(BigInt(42), 4, true, false);
32
+ * ```
33
+ */
34
+ export declare function bigintToBuffer(int: bigint, padding: number, litte?: boolean, signed?: boolean): Buffer;
35
+ /**
36
+ * Computes base-`x` raised to exponent-`y`, with an optional modular reduction `z`.
37
+ *
38
+ * @remarks
39
+ * Uses an efficient binary exponentiation algorithm (square-and-multiply) to handle
40
+ * extremely large values of `x`, `y`, and `z` securely without precision loss.
41
+ *
42
+ * @param x - The base bigint value.
43
+ * @param y - The exponent bigint value (must be non-negative if modulus is specified).
44
+ * @param z - Optional modulus bigint value.
45
+ * @returns If `z` is not defined: `x ** y`. If `z` is defined: `(x ** y) % z`.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const result = bigIntPow(BigInt(5), BigInt(3), BigInt(7)); // (5 ** 3) % 7 = 6
50
+ * ```
51
+ */
52
+ export declare function bigIntPow(x: bigint, y: bigint, z?: bigint): bigint;
53
+ /**
54
+ * Computes the true mathematical modulo of two JS standard `number`s, ensuring a non-negative result.
55
+ *
56
+ * @remarks
57
+ * In standard JavaScript, the `%` operator computes the remainder, which can result in a negative
58
+ * number if the dividend `n` is negative. This helper function corrects that behavior to return
59
+ * a mathematically sound positive modulo in the range `[0, m)`.
60
+ *
61
+ * @param n - The dividend.
62
+ * @param m - The divisor (must be a positive, non-zero number).
63
+ * @returns The positive remainder representing `n mod m`.
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * mod(5, 3); // returns 2
68
+ * mod(-1, 3); // returns 2 (whereas -1 % 3 returns -1)
69
+ * ```
70
+ */
71
+ export declare function mod(n: number, m: number): number;
72
+ /**
73
+ * Computes the true mathematical modulo of two `bigint` values, ensuring a non-negative result.
74
+ *
75
+ * @remarks
76
+ * Similar to {@link mod}, this function implements correct mathematical modulo for large bigints
77
+ * so that any negative dividend `n` resolves into a positive remainder modulo `m`.
78
+ *
79
+ * @param n - The bigint dividend.
80
+ * @param m - The bigint divisor.
81
+ * @returns The positive bigint remainder representing `n mod m`.
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * bigIntMod(BigInt(-5), BigInt(3)); // returns 1n
86
+ * ```
87
+ */
88
+ export declare function bigIntMod(n: bigint, m: bigint): bigint;
89
+ /**
90
+ * Decodes a Node.js `Buffer` back into a JS native `BigInt` value.
91
+ *
92
+ * @param buffer - The source Buffer containing binary representation of the integer.
93
+ * @param little - Set to `true` if the buffer is encoded in little-endian format; set to `false` if big-endian. Defaults to `true`.
94
+ * @param signed - Set to `true` if the buffer contains a signed two's complement integer. Defaults to `false`.
95
+ * @returns A standard native `bigint` value decoded from the bytes.
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * const val = bufferToBigint(Buffer.from([0x2a, 0x00]), true, false); // 42n
100
+ * ```
101
+ */
102
+ export declare function bufferToBigint(buffer: Buffer, little?: boolean, signed?: boolean): bigint;
@@ -0,0 +1,176 @@
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 { bigInt, Buffer } from './deps.js';
11
+ // https://github.com/gram-js/gramjs/blob/b99879464cd1114d89b333c5d929610780c4b003/gramjs/Helpers.ts#L13
12
+ /**
13
+ * Converts a JS native `bigint` value into a Node.js `Buffer` with custom byte-padding, endianness, and sign representation.
14
+ *
15
+ * @remarks
16
+ * This utility handles both signed and unsigned numeric translations.
17
+ * For signed negative bigints, it computes the manual two's complement transformation
18
+ * in either little-endian or big-endian formats to match Telegram MTProto specifications.
19
+ *
20
+ * @param int - The bigint value to serialize into the buffer.
21
+ * @param padding - The absolute target size of the output buffer in bytes.
22
+ * @param litte - Set to `true` for little-endian encoding; set to `false` for big-endian. Defaults to `true`.
23
+ * @param signed - Set to `true` to allow signed integers and two's complement representation. Defaults to `false`.
24
+ * @returns A new `Buffer` representation of the bigint value.
25
+ *
26
+ * @throws {Error} If the bigint requires more bytes to represent than the specified padding size.
27
+ * @throws {Error} If the bigint is negative and `signed` is set to `false`.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * // Unsigned little-endian 4-byte padding
32
+ * const buf = bigintToBuffer(BigInt(42), 4, true, false);
33
+ * ```
34
+ */
35
+ export function bigintToBuffer(int, padding, litte = true, signed = false) {
36
+ const bigintLength = int.toString(2).length;
37
+ const bytes = Math.ceil(bigintLength / 8);
38
+ if (padding < bytes) {
39
+ throw new Error("Too big, Can't convert it to buffer with that padding.");
40
+ }
41
+ if (!signed && int < BigInt(0)) {
42
+ throw new Error('Too small, can convert it when unsigned.');
43
+ }
44
+ let isBellow = false;
45
+ if (int < BigInt(0)) {
46
+ isBellow = true;
47
+ int = int * BigInt(-1);
48
+ }
49
+ const hex = int.toString(16).padStart(padding * 2, '0');
50
+ let buffer = Buffer.from(hex, 'hex');
51
+ if (litte)
52
+ buffer = buffer.reverse();
53
+ if (isBellow && signed) {
54
+ if (litte) {
55
+ let isReminder = false;
56
+ if (buffer[0])
57
+ buffer[0] -= 1;
58
+ for (let b = 0; b < Buffer.byteLength(buffer); b++) {
59
+ if (!buffer[b]) {
60
+ isReminder = true;
61
+ continue;
62
+ }
63
+ if (isReminder) {
64
+ buffer[b] -= 1;
65
+ isReminder = false;
66
+ }
67
+ buffer[b] = 255 - buffer[b];
68
+ }
69
+ }
70
+ else {
71
+ buffer[Buffer.byteLength(buffer) - 1] =
72
+ 256 - buffer[Buffer.byteLength(buffer) - 1];
73
+ for (let b = 0; b < Buffer.byteLength(buffer); b++) {
74
+ buffer[b] = 255 - buffer[b];
75
+ }
76
+ }
77
+ }
78
+ return buffer;
79
+ }
80
+ /**
81
+ * Computes base-`x` raised to exponent-`y`, with an optional modular reduction `z`.
82
+ *
83
+ * @remarks
84
+ * Uses an efficient binary exponentiation algorithm (square-and-multiply) to handle
85
+ * extremely large values of `x`, `y`, and `z` securely without precision loss.
86
+ *
87
+ * @param x - The base bigint value.
88
+ * @param y - The exponent bigint value (must be non-negative if modulus is specified).
89
+ * @param z - Optional modulus bigint value.
90
+ * @returns If `z` is not defined: `x ** y`. If `z` is defined: `(x ** y) % z`.
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * const result = bigIntPow(BigInt(5), BigInt(3), BigInt(7)); // (5 ** 3) % 7 = 6
95
+ * ```
96
+ */
97
+ export function bigIntPow(x, y, z) {
98
+ if (z === undefined) {
99
+ return x ** y;
100
+ }
101
+ else {
102
+ let result = BigInt(1);
103
+ while (y > BigInt(0)) {
104
+ if (bigIntMod(y, BigInt(2)) === BigInt(1)) {
105
+ result = bigIntMod(result * x, z);
106
+ }
107
+ y = y >> BigInt(1);
108
+ x = bigIntMod(x * x, z);
109
+ }
110
+ return result;
111
+ }
112
+ }
113
+ // https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
114
+ /**
115
+ * Computes the true mathematical modulo of two JS standard `number`s, ensuring a non-negative result.
116
+ *
117
+ * @remarks
118
+ * In standard JavaScript, the `%` operator computes the remainder, which can result in a negative
119
+ * number if the dividend `n` is negative. This helper function corrects that behavior to return
120
+ * a mathematically sound positive modulo in the range `[0, m)`.
121
+ *
122
+ * @param n - The dividend.
123
+ * @param m - The divisor (must be a positive, non-zero number).
124
+ * @returns The positive remainder representing `n mod m`.
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * mod(5, 3); // returns 2
129
+ * mod(-1, 3); // returns 2 (whereas -1 % 3 returns -1)
130
+ * ```
131
+ */
132
+ export function mod(n, m) {
133
+ return ((n % m) + m) % m;
134
+ }
135
+ /**
136
+ * Computes the true mathematical modulo of two `bigint` values, ensuring a non-negative result.
137
+ *
138
+ * @remarks
139
+ * Similar to {@link mod}, this function implements correct mathematical modulo for large bigints
140
+ * so that any negative dividend `n` resolves into a positive remainder modulo `m`.
141
+ *
142
+ * @param n - The bigint dividend.
143
+ * @param m - The bigint divisor.
144
+ * @returns The positive bigint remainder representing `n mod m`.
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * bigIntMod(BigInt(-5), BigInt(3)); // returns 1n
149
+ * ```
150
+ */
151
+ export function bigIntMod(n, m) {
152
+ return ((n % m) + m) % m;
153
+ }
154
+ /**
155
+ * Decodes a Node.js `Buffer` back into a JS native `BigInt` value.
156
+ *
157
+ * @param buffer - The source Buffer containing binary representation of the integer.
158
+ * @param little - Set to `true` if the buffer is encoded in little-endian format; set to `false` if big-endian. Defaults to `true`.
159
+ * @param signed - Set to `true` if the buffer contains a signed two's complement integer. Defaults to `false`.
160
+ * @returns A standard native `bigint` value decoded from the bytes.
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * const val = bufferToBigint(Buffer.from([0x2a, 0x00]), true, false); // 42n
165
+ * ```
166
+ */
167
+ export function bufferToBigint(buffer, little = true, signed = false) {
168
+ const length = Buffer.byteLength(buffer);
169
+ const value = little ? buffer.reverse().toString('hex') : buffer.toString('hex');
170
+ const _bigint = bigInt(value, 16);
171
+ let bigint = BigInt(String(_bigint));
172
+ if (signed && Math.floor(bigint.toString(2).length / 8) >= length) {
173
+ bigint = bigint - bigIntPow(BigInt(2), BigInt(length * 8));
174
+ }
175
+ return BigInt(bigint);
176
+ }
@@ -0,0 +1,12 @@
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
+ export { Raw, AllTLObject, Primitive, TLObject, GzipPacked, Message, MsgContainer, SecretChatMessage, SecretChatMessageService, UpdateSecretChatMessage, TypeTGenerate, } from './raw/index.js';
11
+ export { bufferToBigint, bigIntMod, bigIntPow, bigintToBuffer, mod } from './helpers.js';
12
+ export { BadMsgNotification, CDNFileHashMismatch, ClientError, Exceptions, FileErrors, NotAFunctionClass, RPCError, SecretChatError, SecurityCheckMismatch, SecurityError, TimeoutError, UnknownError, WSError, } from './errors/index.js';
@@ -0,0 +1,12 @@
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
+ export { Raw, AllTLObject, Primitive, TLObject, GzipPacked, Message, MsgContainer, SecretChatMessage, SecretChatMessageService, UpdateSecretChatMessage, } from './raw/index.js';
11
+ export { bufferToBigint, bigIntMod, bigIntPow, bigintToBuffer, mod } from './helpers.js';
12
+ export { BadMsgNotification, CDNFileHashMismatch, ClientError, Exceptions, FileErrors, NotAFunctionClass, RPCError, SecretChatError, SecurityCheckMismatch, SecurityError, TimeoutError, UnknownError, WSError, } from './errors/index.js';