@tgsnake/skema 1.13.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.
- package/LICENSE +1 -1
- package/README.md +124 -15
- package/dist/mod.d.ts +10 -0
- package/dist/mod.js +10 -0
- package/dist/src/Logger.d.ts +20 -0
- package/dist/src/Logger.js +23 -0
- package/dist/src/deps.d.ts +29 -0
- package/dist/src/deps.js +34 -0
- package/dist/src/errors/Base.d.ts +53 -0
- package/dist/src/errors/Base.js +114 -0
- package/dist/src/errors/Client.d.ts +69 -0
- package/dist/src/errors/Client.js +69 -0
- package/dist/src/errors/File.d.ts +89 -0
- package/dist/src/errors/File.js +99 -0
- package/dist/src/errors/RpcError.d.ts +115 -0
- package/dist/src/errors/RpcError.js +226 -0
- package/dist/src/errors/SecretChat.d.ts +78 -0
- package/dist/src/errors/SecretChat.js +88 -0
- package/dist/src/errors/WebSocket.d.ts +70 -0
- package/dist/src/errors/WebSocket.js +80 -0
- package/dist/{errors → src/errors}/exceptions/All.d.ts +14 -0
- package/dist/{errors → src/errors}/exceptions/All.js +15 -5
- package/dist/{errors → src/errors}/exceptions/BadRequest400.d.ts +15 -1
- package/dist/src/errors/exceptions/BadRequest400.js +2683 -0
- package/dist/{errors → src/errors}/exceptions/Flood420.d.ts +15 -1
- package/dist/{errors → src/errors}/exceptions/Flood420.js +25 -24
- package/dist/{errors → src/errors}/exceptions/Forbidden403.d.ts +15 -1
- package/dist/src/errors/exceptions/Forbidden403.js +243 -0
- package/dist/{errors → src/errors}/exceptions/InternalServerError500.d.ts +15 -1
- package/dist/src/errors/exceptions/InternalServerError500.js +255 -0
- package/dist/{errors → src/errors}/exceptions/NotAcceptable406.d.ts +15 -1
- package/dist/{errors → src/errors}/exceptions/NotAcceptable406.js +51 -76
- package/dist/src/errors/exceptions/NotFound404.d.ts +27 -0
- package/dist/src/errors/exceptions/NotFound404.js +27 -0
- package/dist/src/errors/exceptions/SeeOther303.d.ts +39 -0
- package/dist/src/errors/exceptions/SeeOther303.js +39 -0
- package/dist/src/errors/exceptions/ServiceUnavailable503.d.ts +31 -0
- package/dist/src/errors/exceptions/ServiceUnavailable503.js +31 -0
- package/dist/{errors → src/errors}/exceptions/Unauthorized401.d.ts +15 -1
- package/dist/src/errors/exceptions/Unauthorized401.js +55 -0
- package/dist/src/errors/exceptions/index.d.ts +24 -0
- package/dist/src/errors/exceptions/index.js +24 -0
- package/dist/src/errors/index.d.ts +142 -0
- package/dist/src/errors/index.js +178 -0
- package/dist/src/helpers.d.ts +102 -0
- package/dist/src/helpers.js +176 -0
- package/dist/src/index.d.ts +12 -0
- package/dist/src/index.js +12 -0
- package/dist/{raw → src/raw}/All.d.ts +14 -0
- package/dist/{raw → src/raw}/All.js +15 -4
- package/dist/{raw → src/raw}/Raw.d.ts +17205 -3
- package/dist/{raw → src/raw}/Raw.js +44035 -21917
- package/dist/src/raw/UpdateSecretChat.d.ts +186 -0
- package/dist/src/raw/UpdateSecretChat.js +215 -0
- package/dist/src/raw/core/GzipPacked.d.ts +63 -0
- package/dist/src/raw/core/GzipPacked.js +76 -0
- package/dist/src/raw/core/Message.d.ts +68 -0
- package/dist/src/raw/core/Message.js +105 -0
- package/dist/src/raw/core/MsgContainer.d.ts +56 -0
- package/dist/src/raw/core/MsgContainer.js +77 -0
- package/dist/src/raw/core/TLObject.d.ts +108 -0
- package/dist/src/raw/core/TLObject.js +209 -0
- package/dist/src/raw/core/index.d.ts +19 -0
- package/dist/src/raw/core/index.js +19 -0
- package/dist/src/raw/core/primitive/Bool.d.ts +104 -0
- package/dist/src/raw/core/primitive/Bool.js +120 -0
- package/dist/src/raw/core/primitive/Bytes.d.ts +45 -0
- package/dist/src/raw/core/primitive/Bytes.js +76 -0
- package/dist/src/raw/core/primitive/Double.d.ts +37 -0
- package/dist/src/raw/core/primitive/Double.js +51 -0
- package/dist/src/raw/core/primitive/Float.d.ts +37 -0
- package/dist/src/raw/core/primitive/Float.js +51 -0
- package/dist/src/raw/core/primitive/Int.d.ts +144 -0
- package/dist/src/raw/core/primitive/Int.js +237 -0
- package/dist/src/raw/core/primitive/String.d.ts +37 -0
- package/dist/src/raw/core/primitive/String.js +42 -0
- package/dist/src/raw/core/primitive/Vector.d.ts +56 -0
- package/dist/src/raw/core/primitive/Vector.js +93 -0
- package/dist/src/raw/core/primitive/index.d.ts +20 -0
- package/dist/src/raw/core/primitive/index.js +20 -0
- package/dist/src/raw/index.d.ts +17 -0
- package/dist/src/raw/index.js +17 -0
- package/package.json +54 -50
- package/dist/Logger.d.ts +0 -3
- package/dist/Logger.js +0 -9
- package/dist/deps.d.ts +0 -8
- package/dist/deps.js +0 -19
- package/dist/errors/Base.d.ts +0 -9
- package/dist/errors/Base.js +0 -50
- package/dist/errors/Client.d.ts +0 -21
- package/dist/errors/Client.js +0 -29
- package/dist/errors/File.d.ts +0 -13
- package/dist/errors/File.js +0 -30
- package/dist/errors/RpcError.d.ts +0 -22
- package/dist/errors/RpcError.js +0 -120
- package/dist/errors/SecretChat.d.ts +0 -13
- package/dist/errors/SecretChat.js +0 -30
- package/dist/errors/WebSocket.d.ts +0 -13
- package/dist/errors/WebSocket.js +0 -30
- package/dist/errors/exceptions/BadRequest400.js +0 -3352
- package/dist/errors/exceptions/Forbidden403.js +0 -290
- package/dist/errors/exceptions/InternalServerError500.js +0 -305
- package/dist/errors/exceptions/NotFound404.d.ts +0 -13
- package/dist/errors/exceptions/NotFound404.js +0 -19
- package/dist/errors/exceptions/SeeOther303.d.ts +0 -25
- package/dist/errors/exceptions/SeeOther303.js +0 -34
- package/dist/errors/exceptions/ServiceUnavailable503.d.ts +0 -17
- package/dist/errors/exceptions/ServiceUnavailable503.js +0 -24
- package/dist/errors/exceptions/Unauthorized401.js +0 -54
- package/dist/errors/exceptions/index.d.ts +0 -10
- package/dist/errors/exceptions/index.js +0 -48
- package/dist/errors/index.d.ts +0 -31
- package/dist/errors/index.js +0 -111
- package/dist/helpers.d.ts +0 -6
- package/dist/helpers.js +0 -85
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -34
- package/dist/raw/UpdateSecretChat.d.ts +0 -43
- package/dist/raw/UpdateSecretChat.js +0 -91
- package/dist/raw/core/GzipPacked.d.ts +0 -9
- package/dist/raw/core/GzipPacked.js +0 -59
- package/dist/raw/core/Message.d.ts +0 -12
- package/dist/raw/core/Message.js +0 -80
- package/dist/raw/core/MsgContainer.d.ts +0 -10
- package/dist/raw/core/MsgContainer.js +0 -68
- package/dist/raw/core/TLObject.d.ts +0 -18
- package/dist/raw/core/TLObject.js +0 -108
- package/dist/raw/core/index.d.ts +0 -6
- package/dist/raw/core/index.js +0 -46
- package/dist/raw/core/primitive/Bool.d.ts +0 -19
- package/dist/raw/core/primitive/Bool.js +0 -41
- package/dist/raw/core/primitive/Bytes.d.ts +0 -6
- package/dist/raw/core/primitive/Bytes.js +0 -41
- package/dist/raw/core/primitive/Double.d.ts +0 -6
- package/dist/raw/core/primitive/Double.js +0 -24
- package/dist/raw/core/primitive/Float.d.ts +0 -6
- package/dist/raw/core/primitive/Float.js +0 -24
- package/dist/raw/core/primitive/Int.d.ts +0 -22
- package/dist/raw/core/primitive/Int.js +0 -122
- package/dist/raw/core/primitive/String.d.ts +0 -6
- package/dist/raw/core/primitive/String.js +0 -15
- package/dist/raw/core/primitive/Vector.d.ts +0 -8
- package/dist/raw/core/primitive/Vector.js +0 -49
- package/dist/raw/core/primitive/index.d.ts +0 -7
- package/dist/raw/core/primitive/index.js +0 -22
- package/dist/raw/index.d.ts +0 -4
- package/dist/raw/index.js +0 -17
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,37 +1,146 @@
|
|
|
1
1
|
# Skema 🚀
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
[](https://jsr.io/@tgsnake/skema) [](https://www.npmjs.com/package/@tgsnake/skema) [](https://www.npmjs.com/package/@tgsnake/skema) [](https://github.com/tgsnake/skema/blob/main/LICENSE)
|
|
4
|
+
|
|
5
|
+
**Skema** compiles the Telegram TL (Type Language) Schema and Telegram RPC Errors Schema into highly usable JavaScript classes with first-class TypeScript support.
|
|
6
|
+
|
|
7
|
+
This project was originally based on the source code from [`@tgsnake/core`](https://github.com/tgsnake/core) 🐍.
|
|
8
|
+
|
|
9
|
+
---
|
|
5
10
|
|
|
6
11
|
<center>
|
|
7
12
|
<b>Layer 225</b>
|
|
8
13
|
</center>
|
|
9
14
|
|
|
10
|
-
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## ⚠️ Requirements & Compatibility in v2.0.0
|
|
18
|
+
|
|
19
|
+
> [!IMPORTANT]
|
|
20
|
+
> Starting with the major release **v2.0.0**, `@tgsnake/skema` is compiled as a pure ECMAScript Module (ESM). Since Node.js 20 introduced native support for `require(ESM)` under experimental flags (and subsequent stabilizing in newer versions), we fully transitioned to ESM. So, the minimum Node.js version required for installation and execution is **Node.js 22**. Ensure your runtime matches or exceeds this version before installing the package.
|
|
21
|
+
|
|
22
|
+
---
|
|
11
23
|
|
|
12
|
-
|
|
13
|
-
- Handles Telegram error types
|
|
14
|
-
- Easy integration into your TypeScript or JavaScript projects
|
|
24
|
+
## ✨ Features
|
|
15
25
|
|
|
16
|
-
|
|
26
|
+
- Up-to-date with Telegram's compiled TL Schema.
|
|
27
|
+
- **Bi-directional Serialization**: Clean and efficient API to serialize TL schema classes into binary `Buffer` structures (`.write()`) and parse them back (`.read()`).
|
|
28
|
+
- **Comprehensive Error Mapping**: Built-in classes for standard Telegram `RPCError` exceptions, custom security mismatches, websocket timeouts, and MTProto packet notifications.
|
|
29
|
+
- **Out-of-the-box Type Safety**: Fully typed modules providing precise autocompletion for Telegram functions, requests, and constructors.
|
|
30
|
+
- **Multi-Runtime Support**: Compatible with modern ESM environments across Node.js, Deno, Bun, and Browser.
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## 📦 Installation
|
|
35
|
+
|
|
36
|
+
Install `@tgsnake/skema` using your preferred package manager:
|
|
17
37
|
|
|
18
38
|
```bash
|
|
39
|
+
# Using npm
|
|
19
40
|
npm install @tgsnake/skema
|
|
41
|
+
|
|
42
|
+
# Using yarn
|
|
43
|
+
yarn add @tgsnake/skema
|
|
44
|
+
|
|
45
|
+
# Using pnpm
|
|
46
|
+
pnpm add @tgsnake/skema
|
|
47
|
+
|
|
48
|
+
# Using Bun
|
|
49
|
+
bun add @tgsnake/skema
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Deno / JSR (Native Deno Import)
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Add the dependency via JSR
|
|
56
|
+
deno add jsr:@tgsnake/skema
|
|
20
57
|
```
|
|
21
58
|
|
|
22
|
-
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## 🛠️ Usage Guide
|
|
62
|
+
|
|
63
|
+
### Constructing Schema Objects
|
|
64
|
+
|
|
65
|
+
You can construct Telegram MTProto API requests and constructors using the `Raw` namespace.
|
|
23
66
|
|
|
24
67
|
```ts
|
|
25
68
|
import { Raw } from '@tgsnake/skema';
|
|
26
69
|
|
|
27
|
-
//
|
|
28
|
-
const
|
|
29
|
-
|
|
70
|
+
// Create a SendMessage request parameters object
|
|
71
|
+
const sendMessageRequest = new Raw.messages.SendMessage({
|
|
72
|
+
peer: new Raw.InputPeerSelf(),
|
|
73
|
+
message: 'Hello from Skema! 🐍',
|
|
74
|
+
randomId: 1234567890n, // BigInt support for long/int128/int256 types
|
|
30
75
|
});
|
|
76
|
+
|
|
77
|
+
console.log(sendMessageRequest.className); // "messages.SendMessage"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Catching & Handling Errors
|
|
81
|
+
|
|
82
|
+
Skema includes robust exceptions mapping Telegram errors to digestible error classes.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { RPCError, Exceptions } from '@tgsnake/skema';
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
// E.g., MTProto request execution logic...
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error instanceof RPCError) {
|
|
91
|
+
console.error(`RPC Error [${error.code}]: ${error.message} (${error.description})`);
|
|
92
|
+
|
|
93
|
+
// Check for specific error types
|
|
94
|
+
if (error instanceof Exceptions.BadRequest.PeerIdInvalid) {
|
|
95
|
+
console.error('The provided peer ID is invalid.');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
31
99
|
```
|
|
32
100
|
|
|
33
|
-
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## 🗃️ Key Exports & Architecture
|
|
104
|
+
|
|
105
|
+
Below is a breakdown of the primary entry points and structures exported by `@tgsnake/skema`:
|
|
106
|
+
|
|
107
|
+
### 🌐 Namespaces & Schema Engines
|
|
108
|
+
|
|
109
|
+
- **`Raw`**: Contains the entire Telegram TL Schema constructors and requests. All types conform to MTProto Layer 225.
|
|
110
|
+
- `Raw.Layer`: The active layer number.
|
|
111
|
+
- `Raw.HighestSCLayer`: The highest secret chat layer supported.
|
|
112
|
+
|
|
113
|
+
### 🏗️ Core Classes
|
|
114
|
+
|
|
115
|
+
- **`TLObject`**: The base class for all compiled types. Implements common helper functions (`read`, `write`, `toJSON`, `toString`).
|
|
116
|
+
- **`Message`**: Represents an individual MTProto envelope message encapsulating a payload, `msg_id`, `seq_no`, and length.
|
|
117
|
+
- **`MsgContainer`**: A container class handling transport multiplexing of multiple MTProto messages.
|
|
118
|
+
- **`GzipPacked`**: Helper utility handling compression and decompression of zlib payloads inside MTProto communications.
|
|
119
|
+
|
|
120
|
+
### 🧰 Math & Buffer Helpers
|
|
121
|
+
|
|
122
|
+
- **`bufferToBigint(buffer: Buffer)`**: Parses a binary buffer into a BigInt.
|
|
123
|
+
- **`bigintToBuffer(number: bigint, padding?: number)`**: Converts a BigInt to its equivalent binary buffer.
|
|
124
|
+
- **`mod(n: number, m: number)`** / **`bigIntMod(n: bigint, m: bigint)`**: Standard modulus helpers.
|
|
125
|
+
|
|
126
|
+
### 🚨 Exceptional Errors Mappings
|
|
127
|
+
|
|
128
|
+
- **`RPCError`**: Base class representing standard Telegram RPC failures.
|
|
129
|
+
- **`BadMsgNotification`**: Raised when the MTProto server rejects a message envelope structure (such as incorrect server salt or out-of-sync message ID).
|
|
130
|
+
- **`SecurityError`** / **`SecurityCheckMismatch`**: Raised when cryptographic assertions, signature checks, or padding validations fail during keys derivation.
|
|
131
|
+
- **`CDNFileHashMismatch`**: Raised if a file chunk downloaded from a Telegram CDN fails checksum verification.
|
|
132
|
+
- **`TimeoutError`**: Raised when a task exceeds its execution time boundary.
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## 🤔 Why Skema?
|
|
137
|
+
|
|
138
|
+
1. **Eliminates Boilerplate**: Automates the tedious, error-prone manual serialization layout and serialization rules of MTProto schemas.
|
|
139
|
+
2. **Type Safety**: Write codebase with confidence using fully generated TypeScript definition files that precisely mirror the official Telegram Type Language layers.
|
|
140
|
+
3. **Optimized for Modern Environments**: Native ESM and modern Node.js features support, making it lightweight and highly performant.
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## 📄 License
|
|
34
145
|
|
|
35
|
-
|
|
36
|
-
- Type-safe: full TypeScript support
|
|
37
|
-
- Reduces boilerplate and manual schema handling
|
|
146
|
+
This project is licensed under the [MIT License](https://github.com/tgsnake/skema/blob/main/LICENSE) - Copyright (C) 2026 tgsnake.
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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 * from './src/index.js';
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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 * from './src/index.js';
|
|
@@ -0,0 +1,20 @@
|
|
|
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 { Logger } from './deps.js';
|
|
11
|
+
/**
|
|
12
|
+
* The internal logger instance for the `@tgsnake/skema` package.
|
|
13
|
+
* Configure with debug level by default to output internal protocol logs.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* This logger wraps the `@tgsnake/log` utility. It is configured to output debug
|
|
17
|
+
* and more severe messages for tracing MTProto serialization and deserialization.
|
|
18
|
+
*/
|
|
19
|
+
declare const log: Logger;
|
|
20
|
+
export { log as Logger };
|
|
@@ -0,0 +1,23 @@
|
|
|
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 { Logger } from './deps.js';
|
|
11
|
+
/**
|
|
12
|
+
* The internal logger instance for the `@tgsnake/skema` package.
|
|
13
|
+
* Configure with debug level by default to output internal protocol logs.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* This logger wraps the `@tgsnake/log` utility. It is configured to output debug
|
|
17
|
+
* and more severe messages for tracing MTProto serialization and deserialization.
|
|
18
|
+
*/
|
|
19
|
+
const log = new Logger({
|
|
20
|
+
name: '@tgsnake/skema',
|
|
21
|
+
level: ['debug'],
|
|
22
|
+
});
|
|
23
|
+
export { log as Logger };
|
|
@@ -0,0 +1,29 @@
|
|
|
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 { BytesIO } from '@tgsnake/bytesio';
|
|
11
|
+
export { Logger } from '@tgsnake/log';
|
|
12
|
+
export { gzipSync, gunzipSync } from 'browserify-zlib';
|
|
13
|
+
export { Buffer } from 'buffer';
|
|
14
|
+
import { inspect as nodeInspect } from 'util';
|
|
15
|
+
import bigInt from 'big-integer';
|
|
16
|
+
/**
|
|
17
|
+
* Identifies the runtime platform environment.
|
|
18
|
+
* Can be 'Deno', 'Bun', 'Browser', or 'Node'.
|
|
19
|
+
* @type {'Deno' | 'Bun' | 'Browser' | 'Node'}
|
|
20
|
+
*/
|
|
21
|
+
export declare const platform: string;
|
|
22
|
+
/**
|
|
23
|
+
* Platform-independent utility function to deeply inspect objects.
|
|
24
|
+
* Uses `Deno.inspect` in Deno environment, otherwise falls back to Node's `util.inspect`.
|
|
25
|
+
*
|
|
26
|
+
* @type {typeof nodeInspect}
|
|
27
|
+
*/
|
|
28
|
+
export declare const inspect: typeof nodeInspect;
|
|
29
|
+
export { bigInt };
|
package/dist/src/deps.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
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 { BytesIO } from '@tgsnake/bytesio';
|
|
11
|
+
export { Logger } from '@tgsnake/log';
|
|
12
|
+
export { gzipSync, gunzipSync } from 'browserify-zlib';
|
|
13
|
+
export { Buffer } from 'buffer';
|
|
14
|
+
import { inspect as nodeInspect } from 'util';
|
|
15
|
+
import bigInt from 'big-integer';
|
|
16
|
+
// Other platform compatibility
|
|
17
|
+
// After this line, we will provide some compatibility for Deno, Bun, and Browser so that the same code can run in both environments without modification.
|
|
18
|
+
const isDeno = 'Deno' in globalThis;
|
|
19
|
+
const isBun = 'Bun' in globalThis;
|
|
20
|
+
const isBrowser = !isDeno && !isBun && typeof window !== 'undefined'; // browser compatibility
|
|
21
|
+
/**
|
|
22
|
+
* Identifies the runtime platform environment.
|
|
23
|
+
* Can be 'Deno', 'Bun', 'Browser', or 'Node'.
|
|
24
|
+
* @type {'Deno' | 'Bun' | 'Browser' | 'Node'}
|
|
25
|
+
*/
|
|
26
|
+
export const platform = isDeno ? 'Deno' : isBun ? 'Bun' : isBrowser ? 'Browser' : 'Node';
|
|
27
|
+
/**
|
|
28
|
+
* Platform-independent utility function to deeply inspect objects.
|
|
29
|
+
* Uses `Deno.inspect` in Deno environment, otherwise falls back to Node's `util.inspect`.
|
|
30
|
+
*
|
|
31
|
+
* @type {typeof nodeInspect}
|
|
32
|
+
*/
|
|
33
|
+
export const inspect = nodeInspect;
|
|
34
|
+
export { bigInt };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base custom error class for the entire tgsnake library.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* This class inherits from the standard JavaScript `Error` object.
|
|
6
|
+
* It provides uniform JSON serialization (`toJSON`), string serialization (`toString`),
|
|
7
|
+
* and platform-agnostic runtime object inspector support for Node.js (`util.inspect`)
|
|
8
|
+
* and Deno console utilities.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* class MyCustomError extends BaseError {
|
|
13
|
+
* constructor(msg: string) {
|
|
14
|
+
* super(msg);
|
|
15
|
+
* this.name = "MyCustomError";
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class BaseError extends Error {
|
|
21
|
+
/**
|
|
22
|
+
* The message string describing this error.
|
|
23
|
+
*/
|
|
24
|
+
message: string;
|
|
25
|
+
/**
|
|
26
|
+
* Detailed human-readable description offering troubleshooting steps or context.
|
|
27
|
+
*/
|
|
28
|
+
description?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Constructs a new instance of the BaseError class.
|
|
31
|
+
*
|
|
32
|
+
* @param message - An optional brief message describing the error.
|
|
33
|
+
*/
|
|
34
|
+
constructor(message?: string);
|
|
35
|
+
/**
|
|
36
|
+
* Serializes the error object to a clean JSON-compatible representation.
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* Correctly stringifies extremely large fields (e.g. bigints) that are normally not
|
|
40
|
+
* serializable in standard `JSON.stringify` calls.
|
|
41
|
+
*
|
|
42
|
+
* @returns A plain object containing error properties and the stack trace.
|
|
43
|
+
*/
|
|
44
|
+
toJSON(): {
|
|
45
|
+
[key: string]: unknown;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Formats the error into a structured string.
|
|
49
|
+
*
|
|
50
|
+
* @returns A string starting with the constructor name followed by a pretty JSON payload.
|
|
51
|
+
*/
|
|
52
|
+
toString(): string;
|
|
53
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
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 { inspect } from '../deps.js';
|
|
11
|
+
/**
|
|
12
|
+
* Base custom error class for the entire tgsnake library.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* This class inherits from the standard JavaScript `Error` object.
|
|
16
|
+
* It provides uniform JSON serialization (`toJSON`), string serialization (`toString`),
|
|
17
|
+
* and platform-agnostic runtime object inspector support for Node.js (`util.inspect`)
|
|
18
|
+
* and Deno console utilities.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* class MyCustomError extends BaseError {
|
|
23
|
+
* constructor(msg: string) {
|
|
24
|
+
* super(msg);
|
|
25
|
+
* this.name = "MyCustomError";
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export class BaseError extends Error {
|
|
31
|
+
/**
|
|
32
|
+
* The message string describing this error.
|
|
33
|
+
*/
|
|
34
|
+
message;
|
|
35
|
+
/**
|
|
36
|
+
* Detailed human-readable description offering troubleshooting steps or context.
|
|
37
|
+
*/
|
|
38
|
+
description;
|
|
39
|
+
/**
|
|
40
|
+
* Constructs a new instance of the BaseError class.
|
|
41
|
+
*
|
|
42
|
+
* @param message - An optional brief message describing the error.
|
|
43
|
+
*/
|
|
44
|
+
constructor(message) {
|
|
45
|
+
super(message);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Custom inspect handler for Node.js console output (`util.inspect`).
|
|
49
|
+
* Omits private properties (prefixed with `_`) to keep debug prints clean.
|
|
50
|
+
*
|
|
51
|
+
* @ignore
|
|
52
|
+
* @returns An object containing enumerable public properties to display.
|
|
53
|
+
*/
|
|
54
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
55
|
+
const toPrint = {
|
|
56
|
+
_: this.constructor.name,
|
|
57
|
+
};
|
|
58
|
+
for (const key in this) {
|
|
59
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
60
|
+
const value = this[key];
|
|
61
|
+
if (!key.startsWith('_')) {
|
|
62
|
+
toPrint[key] = value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
Object.setPrototypeOf(toPrint, {
|
|
67
|
+
stack: this.stack,
|
|
68
|
+
});
|
|
69
|
+
return toPrint;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Custom inspect handler for Deno runtime terminal output (`Deno.inspect`).
|
|
73
|
+
* Wraps the Node.js inspect output and enables terminal colors.
|
|
74
|
+
*
|
|
75
|
+
* @ignore
|
|
76
|
+
* @returns The colorized, formatted representation of the error.
|
|
77
|
+
*/
|
|
78
|
+
[Symbol.for('Deno.customInspect')]() {
|
|
79
|
+
// @ts-ignore: Deno custom inspect
|
|
80
|
+
return String(inspect(this[Symbol.for('nodejs.util.inspect.custom')](), { colors: true }));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Serializes the error object to a clean JSON-compatible representation.
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* Correctly stringifies extremely large fields (e.g. bigints) that are normally not
|
|
87
|
+
* serializable in standard `JSON.stringify` calls.
|
|
88
|
+
*
|
|
89
|
+
* @returns A plain object containing error properties and the stack trace.
|
|
90
|
+
*/
|
|
91
|
+
toJSON() {
|
|
92
|
+
const toPrint = {
|
|
93
|
+
_: this.constructor.name,
|
|
94
|
+
stack: this.stack,
|
|
95
|
+
};
|
|
96
|
+
for (const key in this) {
|
|
97
|
+
if (Object.prototype.hasOwnProperty.call(this, key)) {
|
|
98
|
+
const value = this[key];
|
|
99
|
+
if (!key.startsWith('_')) {
|
|
100
|
+
toPrint[key] = typeof value === 'bigint' ? String(value) : value;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return toPrint;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Formats the error into a structured string.
|
|
108
|
+
*
|
|
109
|
+
* @returns A string starting with the constructor name followed by a pretty JSON payload.
|
|
110
|
+
*/
|
|
111
|
+
toString() {
|
|
112
|
+
return `[constructor of ${this.constructor.name}] ${JSON.stringify(this, null, 2)}`;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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 { BaseError } from './Base.js';
|
|
11
|
+
/**
|
|
12
|
+
* Error thrown when a client request is attempted while the Telegram MTProto client is completely disconnected.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* To resolve this error, ensure that `client.connect()` or equivalent initialization routines are run
|
|
16
|
+
* and completed successfully before initiating requests to the Telegram API.
|
|
17
|
+
*
|
|
18
|
+
* @extends BaseError
|
|
19
|
+
*/
|
|
20
|
+
export declare class ClientDisconnected extends BaseError {
|
|
21
|
+
message: string;
|
|
22
|
+
description: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Error thrown when the Telegram client completely fails to establish a connection to the data center servers.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* This occurs when the network connection retries exceed the user-defined maximum attempt threshold.
|
|
29
|
+
*
|
|
30
|
+
* @extends BaseError
|
|
31
|
+
*/
|
|
32
|
+
export declare class ClientFailed extends BaseError {
|
|
33
|
+
message: string;
|
|
34
|
+
description: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Error thrown when an attempt is made to call connect or start on an already connected Telegram client.
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* Re-initiating a connection while the socket/channel is already fully active is not permitted.
|
|
41
|
+
*
|
|
42
|
+
* @extends BaseError
|
|
43
|
+
*/
|
|
44
|
+
export declare class ClientReady extends BaseError {
|
|
45
|
+
message: string;
|
|
46
|
+
description: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Error thrown when an attempt is made to disconnect or stop an already disconnected Telegram client.
|
|
50
|
+
*
|
|
51
|
+
* @extends BaseError
|
|
52
|
+
*/
|
|
53
|
+
export declare class ClientNotReady extends BaseError {
|
|
54
|
+
message: string;
|
|
55
|
+
description: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Error thrown when the MTProto session authentication key is missing or not provided.
|
|
59
|
+
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* This commonly happens if the client is run in an environment expecting an existing session file
|
|
62
|
+
* or session string, but no authorization parameters or credentials are supplied.
|
|
63
|
+
*
|
|
64
|
+
* @extends BaseError
|
|
65
|
+
*/
|
|
66
|
+
export declare class AuthKeyMissing extends BaseError {
|
|
67
|
+
message: string;
|
|
68
|
+
description: string;
|
|
69
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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 { BaseError } from './Base.js';
|
|
11
|
+
/**
|
|
12
|
+
* Error thrown when a client request is attempted while the Telegram MTProto client is completely disconnected.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* To resolve this error, ensure that `client.connect()` or equivalent initialization routines are run
|
|
16
|
+
* and completed successfully before initiating requests to the Telegram API.
|
|
17
|
+
*
|
|
18
|
+
* @extends BaseError
|
|
19
|
+
*/
|
|
20
|
+
export class ClientDisconnected extends BaseError {
|
|
21
|
+
message = "Can't send request to telegram when client is unconnected.";
|
|
22
|
+
description = 'The provided telegram client is unconnected, make sure to start the telegram client first before sending request.';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Error thrown when the Telegram client completely fails to establish a connection to the data center servers.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* This occurs when the network connection retries exceed the user-defined maximum attempt threshold.
|
|
29
|
+
*
|
|
30
|
+
* @extends BaseError
|
|
31
|
+
*/
|
|
32
|
+
export class ClientFailed extends BaseError {
|
|
33
|
+
message = 'Client failed to connect to server.';
|
|
34
|
+
description = 'The provided telegram client failed to connect to the telegram data center server. Attempts to connect to the telegram server have exceeded the specified maximum limit.';
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Error thrown when an attempt is made to call connect or start on an already connected Telegram client.
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* Re-initiating a connection while the socket/channel is already fully active is not permitted.
|
|
41
|
+
*
|
|
42
|
+
* @extends BaseError
|
|
43
|
+
*/
|
|
44
|
+
export class ClientReady extends BaseError {
|
|
45
|
+
message = 'Client is already connected to server.';
|
|
46
|
+
description = 'The provided telegram client has been already connected to the telegram data center server.';
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Error thrown when an attempt is made to disconnect or stop an already disconnected Telegram client.
|
|
50
|
+
*
|
|
51
|
+
* @extends BaseError
|
|
52
|
+
*/
|
|
53
|
+
export class ClientNotReady extends BaseError {
|
|
54
|
+
message = 'Client is already disconnected to server.';
|
|
55
|
+
description = 'The provided telegram client has been already disconnected to the telegram data center server.';
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Error thrown when the MTProto session authentication key is missing or not provided.
|
|
59
|
+
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* This commonly happens if the client is run in an environment expecting an existing session file
|
|
62
|
+
* or session string, but no authorization parameters or credentials are supplied.
|
|
63
|
+
*
|
|
64
|
+
* @extends BaseError
|
|
65
|
+
*/
|
|
66
|
+
export class AuthKeyMissing extends BaseError {
|
|
67
|
+
message = 'Auth key unavailable';
|
|
68
|
+
description = 'Auth key is unavailable, this can happen because at when the client is run, the user does not provide information to login.';
|
|
69
|
+
}
|