@mtcute/core 0.2.3 → 0.3.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/cjs/base-client.d.ts +33 -166
- package/cjs/base-client.js +69 -45
- package/cjs/base-client.js.map +1 -1
- package/cjs/base-client.types.d.ts +153 -0
- package/cjs/base-client.types.js +3 -0
- package/cjs/base-client.types.js.map +1 -0
- package/cjs/index.d.ts +1 -0
- package/cjs/index.js +1 -0
- package/cjs/index.js.map +1 -1
- package/cjs/network/mtproto-session.d.ts +10 -2
- package/cjs/network/mtproto-session.js +33 -3
- package/cjs/network/mtproto-session.js.map +1 -1
- package/cjs/network/multi-session-connection.d.ts +1 -1
- package/cjs/network/multi-session-connection.js +4 -3
- package/cjs/network/multi-session-connection.js.map +1 -1
- package/cjs/network/network-manager.d.ts +23 -0
- package/cjs/network/network-manager.js +28 -8
- package/cjs/network/network-manager.js.map +1 -1
- package/cjs/network/server-salt.d.ts +12 -0
- package/cjs/network/server-salt.js +45 -0
- package/cjs/network/server-salt.js.map +1 -0
- package/cjs/network/session-connection.d.ts +6 -1
- package/cjs/network/session-connection.js +179 -54
- package/cjs/network/session-connection.js.map +1 -1
- package/cjs/storage/abstract.d.ts +12 -1
- package/cjs/storage/abstract.js.map +1 -1
- package/cjs/storage/idb.d.ts +3 -1
- package/cjs/storage/idb.js +17 -0
- package/cjs/storage/idb.js.map +1 -1
- package/cjs/storage/index.d.ts +1 -0
- package/cjs/storage/index.js +1 -0
- package/cjs/storage/index.js.map +1 -1
- package/cjs/storage/json.js +1 -0
- package/cjs/storage/json.js.map +1 -1
- package/cjs/storage/memory.d.ts +5 -2
- package/cjs/storage/memory.js +16 -2
- package/cjs/storage/memory.js.map +1 -1
- package/cjs/storage/memory.test.js +2 -1
- package/cjs/storage/memory.test.js.map +1 -1
- package/cjs/utils/logger.js +8 -2
- package/cjs/utils/logger.js.map +1 -1
- package/cjs/utils/logger.test.js +12 -0
- package/cjs/utils/logger.test.js.map +1 -1
- package/cjs/utils/long-utils.d.ts +5 -0
- package/cjs/utils/long-utils.js +13 -1
- package/cjs/utils/long-utils.js.map +1 -1
- package/cjs/utils/sorted-array.js +1 -0
- package/cjs/utils/sorted-array.js.map +1 -1
- package/cjs/utils/type-assertions.d.ts +1 -0
- package/cjs/utils/type-assertions.js +7 -1
- package/cjs/utils/type-assertions.js.map +1 -1
- package/esm/base-client.d.ts +33 -166
- package/esm/base-client.js +69 -45
- package/esm/base-client.js.map +1 -1
- package/esm/base-client.types.d.ts +153 -0
- package/esm/base-client.types.js +2 -0
- package/esm/base-client.types.js.map +1 -0
- package/esm/index.d.ts +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/network/mtproto-session.d.ts +10 -2
- package/esm/network/mtproto-session.js +34 -4
- package/esm/network/mtproto-session.js.map +1 -1
- package/esm/network/multi-session-connection.d.ts +1 -1
- package/esm/network/multi-session-connection.js +4 -3
- package/esm/network/multi-session-connection.js.map +1 -1
- package/esm/network/network-manager.d.ts +23 -0
- package/esm/network/network-manager.js +29 -9
- package/esm/network/network-manager.js.map +1 -1
- package/esm/network/server-salt.d.ts +12 -0
- package/esm/network/server-salt.js +38 -0
- package/esm/network/server-salt.js.map +1 -0
- package/esm/network/session-connection.d.ts +6 -1
- package/esm/network/session-connection.js +179 -54
- package/esm/network/session-connection.js.map +1 -1
- package/esm/storage/abstract.d.ts +12 -1
- package/esm/storage/abstract.js.map +1 -1
- package/esm/storage/idb.d.ts +3 -1
- package/esm/storage/idb.js +17 -0
- package/esm/storage/idb.js.map +1 -1
- package/esm/storage/index.d.ts +1 -0
- package/esm/storage/index.js +1 -0
- package/esm/storage/index.js.map +1 -1
- package/esm/storage/json.js +1 -0
- package/esm/storage/json.js.map +1 -1
- package/esm/storage/memory.d.ts +5 -2
- package/esm/storage/memory.js +16 -2
- package/esm/storage/memory.js.map +1 -1
- package/esm/storage/memory.test.js +2 -1
- package/esm/storage/memory.test.js.map +1 -1
- package/esm/utils/logger.js +8 -2
- package/esm/utils/logger.js.map +1 -1
- package/esm/utils/logger.test.js +12 -0
- package/esm/utils/logger.test.js.map +1 -1
- package/esm/utils/long-utils.d.ts +5 -0
- package/esm/utils/long-utils.js +11 -0
- package/esm/utils/long-utils.js.map +1 -1
- package/esm/utils/sorted-array.js +1 -0
- package/esm/utils/sorted-array.js.map +1 -1
- package/esm/utils/type-assertions.d.ts +1 -0
- package/esm/utils/type-assertions.js +5 -0
- package/esm/utils/type-assertions.js.map +1 -1
- package/package.json +4 -4
package/esm/base-client.js
CHANGED
|
@@ -12,63 +12,59 @@ import { createControllablePromise, defaultCryptoProviderFactory, defaultProduct
|
|
|
12
12
|
* to make RPC calls and receive low-level updates.
|
|
13
13
|
*/
|
|
14
14
|
export class BaseTelegramClient extends EventEmitter {
|
|
15
|
-
constructor(
|
|
15
|
+
constructor(params) {
|
|
16
16
|
super();
|
|
17
|
+
this.params = params;
|
|
17
18
|
/** Unix timestamp when the last update was received */
|
|
18
19
|
this._lastUpdateTime = 0;
|
|
19
20
|
this._config = new ConfigManager(() => this.call({ _: 'help.getConfig' }));
|
|
20
21
|
// not really connected, but rather "connect() was called"
|
|
21
22
|
this._connected = false;
|
|
23
|
+
this._emitError = console.error.bind(console);
|
|
22
24
|
this.log = new LogManager('client');
|
|
23
25
|
this._beforeStorageSave = [];
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
throw new Error('apiId must be a number or a numeric string!');
|
|
26
|
+
if (params.logLevel !== undefined) {
|
|
27
|
+
this.log.level = params.logLevel;
|
|
27
28
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.storage = opts.storage;
|
|
33
|
-
this._apiHash = opts.apiHash;
|
|
34
|
-
this._useIpv6 = Boolean(opts.useIpv6);
|
|
35
|
-
this._testMode = Boolean(opts.testMode);
|
|
36
|
-
let dc = opts.defaultDcs;
|
|
29
|
+
this.crypto = (params.crypto ?? defaultCryptoProviderFactory)();
|
|
30
|
+
this.storage = params.storage;
|
|
31
|
+
this._testMode = Boolean(params.testMode);
|
|
32
|
+
let dc = params.defaultDcs;
|
|
37
33
|
if (!dc) {
|
|
38
|
-
if (
|
|
39
|
-
dc =
|
|
34
|
+
if (params.testMode) {
|
|
35
|
+
dc = params.useIpv6 ? defaultTestIpv6Dc : defaultTestDc;
|
|
40
36
|
}
|
|
41
37
|
else {
|
|
42
|
-
dc =
|
|
38
|
+
dc = params.useIpv6 ? defaultProductionIpv6Dc : defaultProductionDc;
|
|
43
39
|
}
|
|
44
40
|
}
|
|
45
41
|
this._defaultDcs = dc;
|
|
46
|
-
this._niceStacks =
|
|
47
|
-
this._layer =
|
|
48
|
-
this._readerMap =
|
|
49
|
-
this._writerMap =
|
|
42
|
+
this._niceStacks = params.niceStacks ?? true;
|
|
43
|
+
this._layer = params.overrideLayer ?? tl.LAYER;
|
|
44
|
+
this._readerMap = params.readerMap ?? defaultReaderMap;
|
|
45
|
+
this._writerMap = params.writerMap ?? defaultWriterMap;
|
|
50
46
|
this.network = new NetworkManager({
|
|
51
|
-
apiId,
|
|
47
|
+
apiId: params.apiId,
|
|
52
48
|
crypto: this.crypto,
|
|
53
|
-
disableUpdates:
|
|
54
|
-
initConnectionOptions:
|
|
49
|
+
disableUpdates: params.disableUpdates ?? false,
|
|
50
|
+
initConnectionOptions: params.initConnectionOptions,
|
|
55
51
|
layer: this._layer,
|
|
56
52
|
log: this.log,
|
|
57
53
|
readerMap: this._readerMap,
|
|
58
54
|
writerMap: this._writerMap,
|
|
59
|
-
reconnectionStrategy:
|
|
55
|
+
reconnectionStrategy: params.reconnectionStrategy,
|
|
60
56
|
storage: this.storage,
|
|
61
|
-
testMode:
|
|
62
|
-
transport:
|
|
57
|
+
testMode: Boolean(params.testMode),
|
|
58
|
+
transport: params.transport,
|
|
63
59
|
_emitError: this._emitError.bind(this),
|
|
64
|
-
floodSleepThreshold:
|
|
65
|
-
maxRetryCount:
|
|
60
|
+
floodSleepThreshold: params.floodSleepThreshold ?? 10000,
|
|
61
|
+
maxRetryCount: params.maxRetryCount ?? 5,
|
|
66
62
|
isPremium: false,
|
|
67
|
-
useIpv6: Boolean(
|
|
63
|
+
useIpv6: Boolean(params.useIpv6),
|
|
68
64
|
keepAliveAction: this._keepAliveAction.bind(this),
|
|
69
|
-
enableErrorReporting:
|
|
65
|
+
enableErrorReporting: params.enableErrorReporting ?? false,
|
|
70
66
|
onUsable: () => this.emit('usable'),
|
|
71
|
-
...(
|
|
67
|
+
...(params.network ?? {}),
|
|
72
68
|
}, this._config);
|
|
73
69
|
this.storage.setup?.(this.log, this._readerMap, this._writerMap);
|
|
74
70
|
}
|
|
@@ -110,7 +106,7 @@ export class BaseTelegramClient extends EventEmitter {
|
|
|
110
106
|
this._defaultDcs = primaryDc;
|
|
111
107
|
const defaultDcAuthKey = await this.storage.getAuthKeyFor(this._defaultDcs.main.id);
|
|
112
108
|
if ((this._importForce || !defaultDcAuthKey) && this._importFrom) {
|
|
113
|
-
const data =
|
|
109
|
+
const data = this._importFrom;
|
|
114
110
|
if (data.testMode !== this._testMode) {
|
|
115
111
|
throw new Error('This session string is not for the current backend. ' +
|
|
116
112
|
`Session is ${data.testMode ? 'test' : 'prod'}, but the client is ${this._testMode ? 'test' : 'prod'}`);
|
|
@@ -169,6 +165,40 @@ export class BaseTelegramClient extends EventEmitter {
|
|
|
169
165
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
170
166
|
return res;
|
|
171
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Create a Proxy that will call all methods with given call parameters
|
|
170
|
+
* (see {@link RpcCallOptions}})
|
|
171
|
+
*
|
|
172
|
+
* This is useful when you don't call `call()` directly, but rather
|
|
173
|
+
* use high-level API provided by `@mtcute/client`, for example:
|
|
174
|
+
*
|
|
175
|
+
* ```ts
|
|
176
|
+
* const client = new TelegramClient(...)
|
|
177
|
+
*
|
|
178
|
+
* const someone = await client
|
|
179
|
+
* .withCallParams({ timeout: 500 })
|
|
180
|
+
* .getUsers(...)
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
withCallParams(params) {
|
|
184
|
+
return new Proxy(this, {
|
|
185
|
+
get(target, prop, receiver) {
|
|
186
|
+
if (prop === 'call') {
|
|
187
|
+
return (message, paramsCustom) => target.call(message, {
|
|
188
|
+
...params,
|
|
189
|
+
...paramsCustom,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
return Reflect.get(target, prop, receiver);
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Shorthand for `withCallParams({ abortSignal })`
|
|
198
|
+
*/
|
|
199
|
+
withAbortSignal(signal) {
|
|
200
|
+
return this.withCallParams({ abortSignal: signal });
|
|
201
|
+
}
|
|
172
202
|
/**
|
|
173
203
|
* Change transport for the client.
|
|
174
204
|
*
|
|
@@ -192,21 +222,15 @@ export class BaseTelegramClient extends EventEmitter {
|
|
|
192
222
|
* this was connection-related error.
|
|
193
223
|
*/
|
|
194
224
|
onError(handler) {
|
|
195
|
-
this.
|
|
225
|
+
this._emitError = handler;
|
|
196
226
|
}
|
|
197
227
|
notifyLoggedIn(auth) {
|
|
198
228
|
this.network.notifyLoggedIn(auth);
|
|
199
229
|
this.emit('logged_in', auth);
|
|
200
230
|
}
|
|
201
|
-
_emitError(err, connection) {
|
|
202
|
-
if (this._onError) {
|
|
203
|
-
this._onError(err, connection);
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
console.error(err);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
231
|
/**
|
|
232
|
+
* **ADVANCED**
|
|
233
|
+
*
|
|
210
234
|
* Adds all peers from a given object to entity cache in storage.
|
|
211
235
|
*/
|
|
212
236
|
async _cachePeersFrom(obj) {
|
|
@@ -296,18 +320,18 @@ export class BaseTelegramClient extends EventEmitter {
|
|
|
296
320
|
/**
|
|
297
321
|
* Request the session to be imported from the given session string.
|
|
298
322
|
*
|
|
299
|
-
* Note that the
|
|
300
|
-
* instead, it will be imported
|
|
323
|
+
* Note that the session will not be imported right away,
|
|
324
|
+
* instead, it will be imported once `connect()` is called
|
|
301
325
|
*
|
|
302
326
|
* Also note that the session will only be imported in case
|
|
303
327
|
* the storage is missing authorization (i.e. does not contain
|
|
304
|
-
* auth key for the primary DC), otherwise it will be ignored (unless `force).
|
|
328
|
+
* auth key for the primary DC), otherwise it will be ignored (unless `force`).
|
|
305
329
|
*
|
|
306
330
|
* @param session Session string to import
|
|
307
331
|
* @param force Whether to overwrite existing session
|
|
308
332
|
*/
|
|
309
333
|
importSession(session, force = false) {
|
|
310
|
-
this._importFrom = session;
|
|
334
|
+
this._importFrom = typeof session === 'string' ? readStringSession(this._readerMap, session) : session;
|
|
311
335
|
this._importForce = force;
|
|
312
336
|
}
|
|
313
337
|
}
|
package/esm/base-client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-client.js","sourceRoot":"","sources":["../../src/base-client.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,OAAO,YAAY,MAAM,QAAQ,CAAA;AACjC,OAAO,IAAI,MAAM,MAAM,CAAA;AAEvB,OAAO,EAAE,EAAE,EAAE,MAAM,YAAY,CAAA;AAC/B,OAAO,EAAE,aAAa,IAAI,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAC/E,OAAO,EAAE,aAAa,IAAI,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAG/E,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAE3D,OAAO,EAAE,cAAc,EAA6C,MAAM,8BAA8B,CAAA;AAIxG,OAAO,EAEH,yBAAyB,EAEzB,4BAA4B,EAC5B,mBAAmB,EACnB,uBAAuB,EACvB,aAAa,EACb,iBAAiB,EACjB,eAAe,EAEf,UAAU,EACV,iBAAiB,EACjB,mBAAmB,EACnB,kBAAkB,GACrB,MAAM,kBAAkB,CAAA;AAwKzB;;;GAGG;AACH,MAAM,OA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;QAED,OAAO,KAAK,CAAA;IAChB,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,aAAa;QACf,MAAM,UAAU,GAAG,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC,IAAI,IAAI,CAAC,WAAW,CAAA;QAE3E,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;QACpE,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;QAE1D,OAAO,kBAAkB,CAAC,IAAI,CAAC,UAAU,EAAE;YACvC,OAAO,EAAE,CAAC;YACV,IAAI,EAAE,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;YAClC,QAAQ,EAAE,IAAI,CAAC,SAAS;YACxB,UAAU;YACV,OAAO;SACV,CAAC,CAAA;IACN,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,aAAa,CAAC,OAAe,EAAE,KAAK,GAAG,KAAK;QACxC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAA;QAC1B,IAAI,CAAC,YAAY,GAAG,KAAK,CAAA;IAC7B,CAAC;CACJ","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport EventEmitter from 'events'\nimport Long from 'long'\n\nimport { tl } from '@mtcute/tl'\nimport { __tlReaderMap as defaultReaderMap } from '@mtcute/tl/binary/reader.js'\nimport { __tlWriterMap as defaultWriterMap } from '@mtcute/tl/binary/writer.js'\nimport { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime'\n\nimport { ConfigManager } from './network/config-manager.js'\nimport { ReconnectionStrategy, SessionConnection, TransportFactory } from './network/index.js'\nimport { NetworkManager, NetworkManagerExtraParams, RpcCallOptions } from './network/network-manager.js'\nimport { PersistentConnectionParams } from './network/persistent-connection.js'\nimport { ITelegramStorage } from './storage/index.js'\nimport { MustEqual } from './types/index.js'\nimport {\n ControllablePromise,\n createControllablePromise,\n CryptoProviderFactory,\n defaultCryptoProviderFactory,\n defaultProductionDc,\n defaultProductionIpv6Dc,\n defaultTestDc,\n defaultTestIpv6Dc,\n getAllPeersFrom,\n ICryptoProvider,\n LogManager,\n readStringSession,\n toggleChannelIdMark,\n writeStringSession,\n} from './utils/index.js'\n\n/** Options for {@link BaseTelegramClient} */\nexport interface BaseTelegramClientOptions {\n /**\n * API ID from my.telegram.org\n */\n apiId: number | string\n /**\n * API hash from my.telegram.org\n */\n apiHash: string\n\n /**\n * Storage to use for this client.\n */\n storage: ITelegramStorage\n\n /**\n * Cryptography provider factory to allow delegating\n * crypto to native addon, worker, etc.\n */\n crypto?: CryptoProviderFactory\n\n /**\n * Whether to use IPv6 datacenters\n * (IPv6 will be preferred when choosing a DC by id)\n * (default: false)\n */\n useIpv6?: boolean\n\n /**\n * Primary DC to use for initial connection.\n * This does not mean this will be the only DC used,\n * nor that this DC will actually be primary, this only\n * determines the first DC the library will try to connect to.\n * Can be used to connect to other networks (like test DCs).\n *\n * When session already contains primary DC, this parameter is ignored.\n *\n * @default Production DC 2.\n */\n defaultDcs?: ITelegramStorage.DcOptions\n\n /**\n * Whether to connect to test servers.\n *\n * If passed, {@link defaultDc} defaults to Test DC 2.\n *\n * **Must** be passed if using test servers, even if\n * you passed custom {@link defaultDc}\n */\n testMode?: boolean\n\n /**\n * Additional options for initConnection call.\n * `apiId` and `query` are not available and will be ignored.\n * Omitted values will be filled with defaults\n */\n initConnectionOptions?: Partial<Omit<tl.RawInitConnectionRequest, 'apiId' | 'query'>>\n\n /**\n * Transport factory to use in the client.\n *\n * @default platform-specific transport: WebSocket on the web, TCP in node\n */\n transport?: TransportFactory\n\n /**\n * Reconnection strategy.\n *\n * @default simple reconnection strategy: first 0ms, then up to 5s (increasing by 1s)\n */\n reconnectionStrategy?: ReconnectionStrategy<PersistentConnectionParams>\n\n /**\n * Maximum duration of a flood_wait that will be waited automatically.\n * Flood waits above this threshold will throw a FloodWaitError.\n * Set to 0 to disable. Can be overridden with `throwFlood` parameter in call() params\n *\n * @default 10000\n */\n floodSleepThreshold?: number\n\n /**\n * Maximum number of retries when calling RPC methods.\n * Call is retried when InternalError or FloodWaitError is encountered.\n * Can be set to Infinity.\n *\n * @default 5\n */\n maxRetryCount?: number\n\n /**\n * If true, every single API call will be wrapped with `tl.invokeWithoutUpdates`,\n * effectively disabling the server-sent events for the clients.\n * May be useful in some cases.\n *\n * Note that this only wraps calls made with `.call()` within the primary\n * connection. Additional connections and direct `.sendForResult()` calls\n * must be wrapped manually.\n *\n * @default false\n */\n disableUpdates?: boolean\n\n /**\n * mtcute can send all unknown RPC errors to [danog](https://github.com/danog)'s\n * [error reporting service](https://rpc.pwrtelegram.xyz/).\n *\n * This is fully anonymous (except maybe IP) and is only used to improve the library\n * and developer experience for everyone working with MTProto. This is fully opt-in,\n * and if you're too paranoid, you can disable it by manually passing `enableErrorReporting: false` to the client.\n *\n * @default false\n */\n enableErrorReporting?: boolean\n\n /**\n * If true, RPC errors will have a stack trace of the initial `.call()`\n * or `.sendForResult()` call position, which drastically improves\n * debugging experience.<br>\n * If false, they will have a stack trace of mtcute internals.\n *\n * Internally this creates a stack capture before every RPC call\n * and stores it until the result is received. This might\n * use a lot more memory than normal, thus can be disabled here.\n *\n * @default true\n */\n niceStacks?: boolean\n\n /**\n * Extra parameters for {@link NetworkManager}\n */\n network?: NetworkManagerExtraParams\n\n /**\n * Set logging level for the client.\n *\n * See static members of {@link LogManager} for possible values.\n */\n logLevel?: number\n\n /**\n * **EXPERT USE ONLY!**\n *\n * Override TL layer used for the connection.\n *\n * **Does not** change the schema used.\n */\n overrideLayer?: number\n\n /**\n * **EXPERT USE ONLY**\n *\n * Override reader map used for the connection.\n */\n readerMap?: TlReaderMap\n\n /**\n * **EXPERT USE ONLY**\n *\n * Override writer map used for the connection.\n */\n writerMap?: TlWriterMap\n}\n\n/**\n * Basic Telegram client that only implements the bare minimum\n * to make RPC calls and receive low-level updates.\n */\nexport class BaseTelegramClient extends EventEmitter {\n /**\n * Crypto provider taken from {@link BaseTelegramClientOptions.crypto}\n */\n readonly crypto: ICryptoProvider\n\n /**\n * Telegram storage taken from {@link BaseTelegramClientOptions.storage}\n */\n readonly storage: ITelegramStorage\n\n /**\n * API hash taken from {@link BaseTelegramClientOptions.apiHash}\n */\n protected readonly _apiHash: string\n\n /**\n * \"Use IPv6\" taken from {@link BaseTelegramClientOptions.useIpv6}\n */\n protected readonly _useIpv6: boolean\n\n /**\n * \"Test mode\" taken from {@link BaseTelegramClientOptions.testMode}\n */\n protected readonly _testMode: boolean\n\n /**\n * Primary DCs taken from {@link BaseTelegramClientOptions.defaultDcs},\n * loaded from session or changed by other means (like redirecting).\n */\n protected _defaultDcs: ITelegramStorage.DcOptions\n\n private _niceStacks: boolean\n /** TL layer used by the client */\n readonly _layer: number\n /** TL readers map used by the client */\n readonly _readerMap: TlReaderMap\n /** TL writers map used by the client */\n readonly _writerMap: TlWriterMap\n\n /** Unix timestamp when the last update was received */\n protected _lastUpdateTime = 0\n\n readonly _config = new ConfigManager(() => this.call({ _: 'help.getConfig' }))\n\n // not really connected, but rather \"connect() was called\"\n private _connected: ControllablePromise<void> | boolean = false\n\n private _onError?: (err: unknown, connection?: SessionConnection) => void\n\n private _importFrom?: string\n private _importForce?: boolean\n\n readonly log = new LogManager('client')\n readonly network: NetworkManager\n\n constructor(opts: BaseTelegramClientOptions) {\n super()\n\n const apiId = typeof opts.apiId === 'string' ? parseInt(opts.apiId) : opts.apiId\n\n if (isNaN(apiId)) {\n throw new Error('apiId must be a number or a numeric string!')\n }\n\n if (opts.logLevel !== undefined) {\n this.log.level = opts.logLevel\n }\n\n this.crypto = (opts.crypto ?? defaultCryptoProviderFactory)()\n\n this.storage = opts.storage\n this._apiHash = opts.apiHash\n this._useIpv6 = Boolean(opts.useIpv6)\n this._testMode = Boolean(opts.testMode)\n\n let dc = opts.defaultDcs\n\n if (!dc) {\n if (this._testMode) {\n dc = this._useIpv6 ? defaultTestIpv6Dc : defaultTestDc\n } else {\n dc = this._useIpv6 ? defaultProductionIpv6Dc : defaultProductionDc\n }\n }\n\n this._defaultDcs = dc\n this._niceStacks = opts.niceStacks ?? true\n\n this._layer = opts.overrideLayer ?? tl.LAYER\n this._readerMap = opts.readerMap ?? defaultReaderMap\n this._writerMap = opts.writerMap ?? defaultWriterMap\n\n this.network = new NetworkManager(\n {\n apiId,\n crypto: this.crypto,\n disableUpdates: opts.disableUpdates ?? false,\n initConnectionOptions: opts.initConnectionOptions,\n layer: this._layer,\n log: this.log,\n readerMap: this._readerMap,\n writerMap: this._writerMap,\n reconnectionStrategy: opts.reconnectionStrategy,\n storage: this.storage,\n testMode: this._testMode,\n transport: opts.transport,\n _emitError: this._emitError.bind(this),\n floodSleepThreshold: opts.floodSleepThreshold ?? 10000,\n maxRetryCount: opts.maxRetryCount ?? 5,\n isPremium: false,\n useIpv6: Boolean(opts.useIpv6),\n keepAliveAction: this._keepAliveAction.bind(this),\n enableErrorReporting: opts.enableErrorReporting ?? false,\n onUsable: () => this.emit('usable'),\n ...(opts.network ?? {}),\n },\n this._config,\n )\n\n this.storage.setup?.(this.log, this._readerMap, this._writerMap)\n }\n\n protected _keepAliveAction(): void {\n this.emit('keep_alive')\n }\n\n protected async _loadStorage(): Promise<void> {\n await this.storage.load?.()\n }\n\n _beforeStorageSave: (() => Promise<void>)[] = []\n\n beforeStorageSave(cb: () => Promise<void>): void {\n this._beforeStorageSave.push(cb)\n }\n\n offBeforeStorageSave(cb: () => Promise<void>): void {\n this._beforeStorageSave = this._beforeStorageSave.filter((x) => x !== cb)\n }\n\n async saveStorage(): Promise<void> {\n for (const cb of this._beforeStorageSave) {\n await cb()\n }\n await this.storage.save?.()\n }\n\n /**\n * Initialize the connection to the primary DC.\n *\n * You shouldn't usually call this method directly as it is called\n * implicitly the first time you call {@link call}.\n */\n async connect(): Promise<void> {\n if (this._connected) {\n // avoid double-connect\n await this._connected\n\n return\n }\n\n const promise = (this._connected = createControllablePromise())\n\n await this.crypto.initialize?.()\n await this._loadStorage()\n const primaryDc = await this.storage.getDefaultDcs()\n if (primaryDc !== null) this._defaultDcs = primaryDc\n\n const defaultDcAuthKey = await this.storage.getAuthKeyFor(this._defaultDcs.main.id)\n\n if ((this._importForce || !defaultDcAuthKey) && this._importFrom) {\n const data = readStringSession(this._readerMap, this._importFrom)\n\n if (data.testMode !== this._testMode) {\n throw new Error(\n 'This session string is not for the current backend. ' +\n `Session is ${data.testMode ? 'test' : 'prod'}, but the client is ${\n this._testMode ? 'test' : 'prod'\n }`,\n )\n }\n\n this._defaultDcs = data.primaryDcs\n await this.storage.setDefaultDcs(data.primaryDcs)\n\n if (data.self) {\n await this.storage.setSelf(data.self)\n }\n\n // await this.primaryConnection.setupKeys(data.authKey)\n await this.storage.setAuthKeyFor(data.primaryDcs.main.id, data.authKey)\n\n await this.saveStorage()\n }\n\n this.emit('before_connect')\n\n this.network\n .connect(this._defaultDcs)\n .then(() => {\n promise.resolve()\n this._connected = true\n })\n .catch((err: Error) => this._emitError(err))\n }\n\n /**\n * Close all connections and finalize the client.\n */\n async close(): Promise<void> {\n this.emit('before_close')\n\n this._config.destroy()\n this.network.destroy()\n\n await this.saveStorage()\n await this.storage.destroy?.()\n\n this.emit('closed')\n }\n\n /**\n * Make an RPC call to the primary DC.\n * This method handles DC migration, flood waits and retries automatically.\n *\n * If you want more low-level control, use\n * `primaryConnection.sendForResult()` (which is what this method wraps)\n *\n * This method is still quite low-level and you shouldn't use this\n * when using high-level API provided by `@mtcute/client`.\n *\n * @param message RPC method to call\n * @param params Additional call parameters\n */\n async call<T extends tl.RpcMethod>(\n message: MustEqual<T, tl.RpcMethod>,\n params?: RpcCallOptions,\n ): Promise<tl.RpcCallReturn[T['_']]> {\n if (this._connected !== true) {\n await this.connect()\n }\n\n const stack = this._niceStacks ? new Error().stack : undefined\n\n const res = await this.network.call(message, params, stack)\n\n if (await this._cachePeersFrom(res)) {\n await this.saveStorage()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return res\n }\n\n /**\n * Change transport for the client.\n *\n * Can be used, for example, to change proxy at runtime\n *\n * This effectively calls `changeTransport()` on\n * `primaryConnection` and all additional connections.\n *\n * @param factory New transport factory\n */\n changeTransport(factory: TransportFactory): void {\n this.network.changeTransport(factory)\n }\n\n /**\n * Register an error handler for the client\n *\n * @param handler\n * Error handler. Called with one or two parameters.\n * The first one is always the error, and the second is\n * the connection in which the error has occurred, in case\n * this was connection-related error.\n */\n onError(handler: (err: unknown, connection?: SessionConnection) => void): void {\n this._onError = handler\n }\n\n notifyLoggedIn(auth: tl.auth.RawAuthorization): void {\n this.network.notifyLoggedIn(auth)\n this.emit('logged_in', auth)\n }\n\n _emitError(err: unknown, connection?: SessionConnection): void {\n if (this._onError) {\n this._onError(err, connection)\n } else {\n console.error(err)\n }\n }\n\n /**\n * Adds all peers from a given object to entity cache in storage.\n */\n async _cachePeersFrom(obj: object): Promise<boolean> {\n const parsedPeers: ITelegramStorage.PeerInfo[] = []\n\n let count = 0\n\n for (const peer of getAllPeersFrom(obj as tl.TlObject)) {\n if ((peer as any).min) {\n // no point in caching min peers as we can't use them\n continue\n }\n\n count += 1\n\n switch (peer._) {\n case 'user':\n if (!peer.accessHash) {\n this.log.warn('received user without access hash: %j', peer)\n continue\n }\n\n parsedPeers.push({\n id: peer.id,\n accessHash: peer.accessHash,\n username: peer.username?.toLowerCase(),\n phone: peer.phone,\n type: 'user',\n full: peer,\n })\n break\n case 'chat':\n case 'chatForbidden':\n parsedPeers.push({\n id: -peer.id,\n accessHash: Long.ZERO,\n type: 'chat',\n full: peer,\n })\n break\n case 'channel':\n case 'channelForbidden':\n if (!peer.accessHash) {\n this.log.warn('received user without access hash: %j', peer)\n continue\n }\n parsedPeers.push({\n id: toggleChannelIdMark(peer.id),\n accessHash: peer.accessHash,\n username: peer._ === 'channel' ? peer.username?.toLowerCase() : undefined,\n type: 'channel',\n full: peer,\n })\n break\n }\n }\n\n if (count > 0) {\n await this.storage.updatePeers(parsedPeers)\n this.log.debug('cached %d peers', count)\n\n return true\n }\n\n return false\n }\n\n /**\n * Export current session to a single *LONG* string, containing\n * all the needed information.\n *\n * > **Warning!** Anyone with this string will be able\n * > to authorize as you and do anything. Treat this\n * > as your password, and never give it away!\n * >\n * > In case you have accidentally leaked this string,\n * > make sure to revoke this session in account settings:\n * > \"Privacy & Security\" > \"Active sessions\" >\n * > find the one containing `mtcute` > Revoke,\n * > or, in case this is a bot, revoke bot token\n * > with [@BotFather](//t.me/botfather)\n */\n async exportSession(): Promise<string> {\n const primaryDcs = (await this.storage.getDefaultDcs()) ?? this._defaultDcs\n\n const authKey = await this.storage.getAuthKeyFor(primaryDcs.main.id)\n if (!authKey) throw new Error('Auth key is not ready yet')\n\n return writeStringSession(this._writerMap, {\n version: 2,\n self: await this.storage.getSelf(),\n testMode: this._testMode,\n primaryDcs,\n authKey,\n })\n }\n\n /**\n * Request the session to be imported from the given session string.\n *\n * Note that the string will not be parsed and imported right away,\n * instead, it will be imported when `connect()` is called\n *\n * Also note that the session will only be imported in case\n * the storage is missing authorization (i.e. does not contain\n * auth key for the primary DC), otherwise it will be ignored (unless `force).\n *\n * @param session Session string to import\n * @param force Whether to overwrite existing session\n */\n importSession(session: string, force = false): void {\n this._importFrom = session\n this._importForce = force\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"base-client.js","sourceRoot":"","sources":["../../src/base-client.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,OAAO,YAAY,MAAM,QAAQ,CAAA;AACjC,OAAO,IAAI,MAAM,MAAM,CAAA;AAEvB,OAAO,EAAE,EAAE,EAAE,MAAM,YAAY,CAAA;AAC/B,OAAO,EAAE,aAAa,IAAI,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAC/E,OAAO,EAAE,aAAa,IAAI,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAI/E,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAE3D,OAAO,E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B,CAAC;CACJ","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport EventEmitter from 'events'\nimport Long from 'long'\n\nimport { tl } from '@mtcute/tl'\nimport { __tlReaderMap as defaultReaderMap } from '@mtcute/tl/binary/reader.js'\nimport { __tlWriterMap as defaultWriterMap } from '@mtcute/tl/binary/writer.js'\nimport { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime'\n\nimport { BaseTelegramClientOptions } from './base-client.types.js'\nimport { ConfigManager } from './network/config-manager.js'\nimport { SessionConnection, TransportFactory } from './network/index.js'\nimport { NetworkManager, RpcCallOptions } from './network/network-manager.js'\nimport { ITelegramStorage } from './storage/index.js'\nimport { MustEqual } from './types/index.js'\nimport {\n ControllablePromise,\n createControllablePromise,\n defaultCryptoProviderFactory,\n defaultProductionDc,\n defaultProductionIpv6Dc,\n defaultTestDc,\n defaultTestIpv6Dc,\n getAllPeersFrom,\n ICryptoProvider,\n LogManager,\n readStringSession,\n StringSessionData,\n toggleChannelIdMark,\n writeStringSession,\n} from './utils/index.js'\n\n/**\n * Basic Telegram client that only implements the bare minimum\n * to make RPC calls and receive low-level updates.\n */\nexport class BaseTelegramClient extends EventEmitter {\n /**\n * Crypto provider taken from {@link BaseTelegramClientOptions.crypto}\n */\n readonly crypto: ICryptoProvider\n\n /**\n * Telegram storage taken from {@link BaseTelegramClientOptions.storage}\n */\n readonly storage: ITelegramStorage\n\n /**\n * \"Test mode\" taken from {@link BaseTelegramClientOptions.testMode}\n */\n protected readonly _testMode: boolean\n\n /**\n * Primary DCs taken from {@link BaseTelegramClientOptions.defaultDcs},\n * loaded from session or changed by other means (like redirecting).\n */\n protected _defaultDcs: ITelegramStorage.DcOptions\n\n private _niceStacks: boolean\n /** TL layer used by the client */\n readonly _layer: number\n /** TL readers map used by the client */\n readonly _readerMap: TlReaderMap\n /** TL writers map used by the client */\n readonly _writerMap: TlWriterMap\n\n /** Unix timestamp when the last update was received */\n protected _lastUpdateTime = 0\n\n readonly _config = new ConfigManager(() => this.call({ _: 'help.getConfig' }))\n\n // not really connected, but rather \"connect() was called\"\n private _connected: ControllablePromise<void> | boolean = false\n\n _emitError: (err: unknown, connection?: SessionConnection) => void = console.error.bind(console)\n\n private _importFrom?: StringSessionData\n private _importForce?: boolean\n\n readonly log = new LogManager('client')\n readonly network: NetworkManager\n\n constructor(readonly params: BaseTelegramClientOptions) {\n super()\n\n if (params.logLevel !== undefined) {\n this.log.level = params.logLevel\n }\n\n this.crypto = (params.crypto ?? defaultCryptoProviderFactory)()\n this.storage = params.storage\n this._testMode = Boolean(params.testMode)\n\n let dc = params.defaultDcs\n\n if (!dc) {\n if (params.testMode) {\n dc = params.useIpv6 ? defaultTestIpv6Dc : defaultTestDc\n } else {\n dc = params.useIpv6 ? defaultProductionIpv6Dc : defaultProductionDc\n }\n }\n\n this._defaultDcs = dc\n this._niceStacks = params.niceStacks ?? true\n\n this._layer = params.overrideLayer ?? tl.LAYER\n this._readerMap = params.readerMap ?? defaultReaderMap\n this._writerMap = params.writerMap ?? defaultWriterMap\n\n this.network = new NetworkManager(\n {\n apiId: params.apiId,\n crypto: this.crypto,\n disableUpdates: params.disableUpdates ?? false,\n initConnectionOptions: params.initConnectionOptions,\n layer: this._layer,\n log: this.log,\n readerMap: this._readerMap,\n writerMap: this._writerMap,\n reconnectionStrategy: params.reconnectionStrategy,\n storage: this.storage,\n testMode: Boolean(params.testMode),\n transport: params.transport,\n _emitError: this._emitError.bind(this),\n floodSleepThreshold: params.floodSleepThreshold ?? 10000,\n maxRetryCount: params.maxRetryCount ?? 5,\n isPremium: false,\n useIpv6: Boolean(params.useIpv6),\n keepAliveAction: this._keepAliveAction.bind(this),\n enableErrorReporting: params.enableErrorReporting ?? false,\n onUsable: () => this.emit('usable'),\n ...(params.network ?? {}),\n },\n this._config,\n )\n\n this.storage.setup?.(this.log, this._readerMap, this._writerMap)\n }\n\n protected _keepAliveAction(): void {\n this.emit('keep_alive')\n }\n\n protected async _loadStorage(): Promise<void> {\n await this.storage.load?.()\n }\n\n _beforeStorageSave: (() => Promise<void>)[] = []\n\n beforeStorageSave(cb: () => Promise<void>): void {\n this._beforeStorageSave.push(cb)\n }\n\n offBeforeStorageSave(cb: () => Promise<void>): void {\n this._beforeStorageSave = this._beforeStorageSave.filter((x) => x !== cb)\n }\n\n async saveStorage(): Promise<void> {\n for (const cb of this._beforeStorageSave) {\n await cb()\n }\n await this.storage.save?.()\n }\n\n /**\n * Initialize the connection to the primary DC.\n *\n * You shouldn't usually call this method directly as it is called\n * implicitly the first time you call {@link call}.\n */\n async connect(): Promise<void> {\n if (this._connected) {\n // avoid double-connect\n await this._connected\n\n return\n }\n\n const promise = (this._connected = createControllablePromise())\n\n await this.crypto.initialize?.()\n await this._loadStorage()\n const primaryDc = await this.storage.getDefaultDcs()\n if (primaryDc !== null) this._defaultDcs = primaryDc\n\n const defaultDcAuthKey = await this.storage.getAuthKeyFor(this._defaultDcs.main.id)\n\n if ((this._importForce || !defaultDcAuthKey) && this._importFrom) {\n const data = this._importFrom\n\n if (data.testMode !== this._testMode) {\n throw new Error(\n 'This session string is not for the current backend. ' +\n `Session is ${data.testMode ? 'test' : 'prod'}, but the client is ${\n this._testMode ? 'test' : 'prod'\n }`,\n )\n }\n\n this._defaultDcs = data.primaryDcs\n await this.storage.setDefaultDcs(data.primaryDcs)\n\n if (data.self) {\n await this.storage.setSelf(data.self)\n }\n\n // await this.primaryConnection.setupKeys(data.authKey)\n await this.storage.setAuthKeyFor(data.primaryDcs.main.id, data.authKey)\n\n await this.saveStorage()\n }\n\n this.emit('before_connect')\n\n this.network\n .connect(this._defaultDcs)\n .then(() => {\n promise.resolve()\n this._connected = true\n })\n .catch((err: Error) => this._emitError(err))\n }\n\n /**\n * Close all connections and finalize the client.\n */\n async close(): Promise<void> {\n this.emit('before_close')\n\n this._config.destroy()\n this.network.destroy()\n\n await this.saveStorage()\n await this.storage.destroy?.()\n\n this.emit('closed')\n }\n\n /**\n * Make an RPC call to the primary DC.\n * This method handles DC migration, flood waits and retries automatically.\n *\n * If you want more low-level control, use\n * `primaryConnection.sendForResult()` (which is what this method wraps)\n *\n * This method is still quite low-level and you shouldn't use this\n * when using high-level API provided by `@mtcute/client`.\n *\n * @param message RPC method to call\n * @param params Additional call parameters\n */\n async call<T extends tl.RpcMethod>(\n message: MustEqual<T, tl.RpcMethod>,\n params?: RpcCallOptions,\n ): Promise<tl.RpcCallReturn[T['_']]> {\n if (this._connected !== true) {\n await this.connect()\n }\n\n const stack = this._niceStacks ? new Error().stack : undefined\n\n const res = await this.network.call(message, params, stack)\n\n if (await this._cachePeersFrom(res)) {\n await this.saveStorage()\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return res\n }\n\n /**\n * Create a Proxy that will call all methods with given call parameters\n * (see {@link RpcCallOptions}})\n *\n * This is useful when you don't call `call()` directly, but rather\n * use high-level API provided by `@mtcute/client`, for example:\n *\n * ```ts\n * const client = new TelegramClient(...)\n *\n * const someone = await client\n * .withCallParams({ timeout: 500 })\n * .getUsers(...)\n * ```\n */\n withCallParams(params: RpcCallOptions): this {\n return new Proxy(this, {\n get(target, prop, receiver) {\n if (prop === 'call') {\n return (message: tl.RpcMethod, paramsCustom?: RpcCallOptions) =>\n target.call(message, {\n ...params,\n ...paramsCustom,\n })\n }\n\n return Reflect.get(target, prop, receiver)\n },\n })\n }\n\n /**\n * Shorthand for `withCallParams({ abortSignal })`\n */\n withAbortSignal(signal: AbortSignal): this {\n return this.withCallParams({ abortSignal: signal })\n }\n\n /**\n * Change transport for the client.\n *\n * Can be used, for example, to change proxy at runtime\n *\n * This effectively calls `changeTransport()` on\n * `primaryConnection` and all additional connections.\n *\n * @param factory New transport factory\n */\n changeTransport(factory: TransportFactory): void {\n this.network.changeTransport(factory)\n }\n\n /**\n * Register an error handler for the client\n *\n * @param handler\n * Error handler. Called with one or two parameters.\n * The first one is always the error, and the second is\n * the connection in which the error has occurred, in case\n * this was connection-related error.\n */\n onError(handler: (err: unknown, connection?: SessionConnection) => void): void {\n this._emitError = handler\n }\n\n notifyLoggedIn(auth: tl.auth.RawAuthorization): void {\n this.network.notifyLoggedIn(auth)\n this.emit('logged_in', auth)\n }\n\n /**\n * **ADVANCED**\n *\n * Adds all peers from a given object to entity cache in storage.\n */\n async _cachePeersFrom(obj: object): Promise<boolean> {\n const parsedPeers: ITelegramStorage.PeerInfo[] = []\n\n let count = 0\n\n for (const peer of getAllPeersFrom(obj as tl.TlObject)) {\n if ((peer as any).min) {\n // no point in caching min peers as we can't use them\n continue\n }\n\n count += 1\n\n switch (peer._) {\n case 'user':\n if (!peer.accessHash) {\n this.log.warn('received user without access hash: %j', peer)\n continue\n }\n\n parsedPeers.push({\n id: peer.id,\n accessHash: peer.accessHash,\n username: peer.username?.toLowerCase(),\n phone: peer.phone,\n type: 'user',\n full: peer,\n })\n break\n case 'chat':\n case 'chatForbidden':\n parsedPeers.push({\n id: -peer.id,\n accessHash: Long.ZERO,\n type: 'chat',\n full: peer,\n })\n break\n case 'channel':\n case 'channelForbidden':\n if (!peer.accessHash) {\n this.log.warn('received user without access hash: %j', peer)\n continue\n }\n parsedPeers.push({\n id: toggleChannelIdMark(peer.id),\n accessHash: peer.accessHash,\n username: peer._ === 'channel' ? peer.username?.toLowerCase() : undefined,\n type: 'channel',\n full: peer,\n })\n break\n }\n }\n\n if (count > 0) {\n await this.storage.updatePeers(parsedPeers)\n this.log.debug('cached %d peers', count)\n\n return true\n }\n\n return false\n }\n\n /**\n * Export current session to a single *LONG* string, containing\n * all the needed information.\n *\n * > **Warning!** Anyone with this string will be able\n * > to authorize as you and do anything. Treat this\n * > as your password, and never give it away!\n * >\n * > In case you have accidentally leaked this string,\n * > make sure to revoke this session in account settings:\n * > \"Privacy & Security\" > \"Active sessions\" >\n * > find the one containing `mtcute` > Revoke,\n * > or, in case this is a bot, revoke bot token\n * > with [@BotFather](//t.me/botfather)\n */\n async exportSession(): Promise<string> {\n const primaryDcs = (await this.storage.getDefaultDcs()) ?? this._defaultDcs\n\n const authKey = await this.storage.getAuthKeyFor(primaryDcs.main.id)\n if (!authKey) throw new Error('Auth key is not ready yet')\n\n return writeStringSession(this._writerMap, {\n version: 2,\n self: await this.storage.getSelf(),\n testMode: this._testMode,\n primaryDcs,\n authKey,\n })\n }\n\n /**\n * Request the session to be imported from the given session string.\n *\n * Note that the session will not be imported right away,\n * instead, it will be imported once `connect()` is called\n *\n * Also note that the session will only be imported in case\n * the storage is missing authorization (i.e. does not contain\n * auth key for the primary DC), otherwise it will be ignored (unless `force`).\n *\n * @param session Session string to import\n * @param force Whether to overwrite existing session\n */\n importSession(session: string | StringSessionData, force = false): void {\n this._importFrom = typeof session === 'string' ? readStringSession(this._readerMap, session) : session\n this._importForce = force\n }\n}\n"]}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { tl } from '@mtcute/tl';
|
|
2
|
+
import { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime';
|
|
3
|
+
import { NetworkManagerExtraParams, ReconnectionStrategy, TransportFactory } from './network/index.js';
|
|
4
|
+
import { PersistentConnectionParams } from './network/persistent-connection.js';
|
|
5
|
+
import { ITelegramStorage } from './storage/abstract.js';
|
|
6
|
+
import { CryptoProviderFactory } from './utils/index.js';
|
|
7
|
+
/** Options for {@link BaseTelegramClient} */
|
|
8
|
+
export interface BaseTelegramClientOptions {
|
|
9
|
+
/**
|
|
10
|
+
* API ID from my.telegram.org
|
|
11
|
+
*/
|
|
12
|
+
apiId: number;
|
|
13
|
+
/**
|
|
14
|
+
* API hash from my.telegram.org
|
|
15
|
+
*/
|
|
16
|
+
apiHash: string;
|
|
17
|
+
/**
|
|
18
|
+
* Storage to use for this client.
|
|
19
|
+
*/
|
|
20
|
+
storage: ITelegramStorage;
|
|
21
|
+
/**
|
|
22
|
+
* Cryptography provider factory to allow delegating
|
|
23
|
+
* crypto to native addon, worker, etc.
|
|
24
|
+
*/
|
|
25
|
+
crypto?: CryptoProviderFactory;
|
|
26
|
+
/**
|
|
27
|
+
* Whether to use IPv6 datacenters
|
|
28
|
+
* (IPv6 will be preferred when choosing a DC by id)
|
|
29
|
+
* (default: false)
|
|
30
|
+
*/
|
|
31
|
+
useIpv6?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Primary DC to use for initial connection.
|
|
34
|
+
* This does not mean this will be the only DC used,
|
|
35
|
+
* nor that this DC will actually be primary, this only
|
|
36
|
+
* determines the first DC the library will try to connect to.
|
|
37
|
+
* Can be used to connect to other networks (like test DCs).
|
|
38
|
+
*
|
|
39
|
+
* When session already contains primary DC, this parameter is ignored.
|
|
40
|
+
*
|
|
41
|
+
* @default Production DC 2.
|
|
42
|
+
*/
|
|
43
|
+
defaultDcs?: ITelegramStorage.DcOptions;
|
|
44
|
+
/**
|
|
45
|
+
* Whether to connect to test servers.
|
|
46
|
+
*
|
|
47
|
+
* If passed, {@link defaultDc} defaults to Test DC 2.
|
|
48
|
+
*
|
|
49
|
+
* **Must** be passed if using test servers, even if
|
|
50
|
+
* you passed custom {@link defaultDc}
|
|
51
|
+
*/
|
|
52
|
+
testMode?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Additional options for initConnection call.
|
|
55
|
+
* `apiId` and `query` are not available and will be ignored.
|
|
56
|
+
* Omitted values will be filled with defaults
|
|
57
|
+
*/
|
|
58
|
+
initConnectionOptions?: Partial<Omit<tl.RawInitConnectionRequest, 'apiId' | 'query'>>;
|
|
59
|
+
/**
|
|
60
|
+
* Transport factory to use in the client.
|
|
61
|
+
*
|
|
62
|
+
* @default platform-specific transport: WebSocket on the web, TCP in node
|
|
63
|
+
*/
|
|
64
|
+
transport?: TransportFactory;
|
|
65
|
+
/**
|
|
66
|
+
* Reconnection strategy.
|
|
67
|
+
*
|
|
68
|
+
* @default simple reconnection strategy: first 0ms, then up to 5s (increasing by 1s)
|
|
69
|
+
*/
|
|
70
|
+
reconnectionStrategy?: ReconnectionStrategy<PersistentConnectionParams>;
|
|
71
|
+
/**
|
|
72
|
+
* Maximum duration of a flood_wait that will be waited automatically.
|
|
73
|
+
* Flood waits above this threshold will throw a FloodWaitError.
|
|
74
|
+
* Set to 0 to disable. Can be overridden with `throwFlood` parameter in call() params
|
|
75
|
+
*
|
|
76
|
+
* @default 10000
|
|
77
|
+
*/
|
|
78
|
+
floodSleepThreshold?: number;
|
|
79
|
+
/**
|
|
80
|
+
* Maximum number of retries when calling RPC methods.
|
|
81
|
+
* Call is retried when InternalError or FloodWaitError is encountered.
|
|
82
|
+
* Can be set to Infinity.
|
|
83
|
+
*
|
|
84
|
+
* @default 5
|
|
85
|
+
*/
|
|
86
|
+
maxRetryCount?: number;
|
|
87
|
+
/**
|
|
88
|
+
* If true, every single API call will be wrapped with `tl.invokeWithoutUpdates`,
|
|
89
|
+
* effectively disabling the server-sent events for the clients.
|
|
90
|
+
* May be useful in some cases.
|
|
91
|
+
*
|
|
92
|
+
* Note that this only wraps calls made with `.call()` within the primary
|
|
93
|
+
* connection. Additional connections and direct `.sendForResult()` calls
|
|
94
|
+
* must be wrapped manually.
|
|
95
|
+
*
|
|
96
|
+
* @default false
|
|
97
|
+
*/
|
|
98
|
+
disableUpdates?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* mtcute can send all unknown RPC errors to [danog](https://github.com/danog)'s
|
|
101
|
+
* [error reporting service](https://rpc.pwrtelegram.xyz/).
|
|
102
|
+
*
|
|
103
|
+
* This is fully anonymous (except maybe IP) and is only used to improve the library
|
|
104
|
+
* and developer experience for everyone working with MTProto. This is fully opt-in,
|
|
105
|
+
* and if you're too paranoid, you can disable it by manually passing `enableErrorReporting: false` to the client.
|
|
106
|
+
*
|
|
107
|
+
* @default false
|
|
108
|
+
*/
|
|
109
|
+
enableErrorReporting?: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* If true, RPC errors will have a stack trace of the initial `.call()`
|
|
112
|
+
* or `.sendForResult()` call position, which drastically improves
|
|
113
|
+
* debugging experience.<br>
|
|
114
|
+
* If false, they will have a stack trace of mtcute internals.
|
|
115
|
+
*
|
|
116
|
+
* Internally this creates a stack capture before every RPC call
|
|
117
|
+
* and stores it until the result is received. This might
|
|
118
|
+
* use a lot more memory than normal, thus can be disabled here.
|
|
119
|
+
*
|
|
120
|
+
* @default true
|
|
121
|
+
*/
|
|
122
|
+
niceStacks?: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Extra parameters for {@link NetworkManager}
|
|
125
|
+
*/
|
|
126
|
+
network?: NetworkManagerExtraParams;
|
|
127
|
+
/**
|
|
128
|
+
* Set logging level for the client.
|
|
129
|
+
*
|
|
130
|
+
* See static members of {@link LogManager} for possible values.
|
|
131
|
+
*/
|
|
132
|
+
logLevel?: number;
|
|
133
|
+
/**
|
|
134
|
+
* **EXPERT USE ONLY!**
|
|
135
|
+
*
|
|
136
|
+
* Override TL layer used for the connection.
|
|
137
|
+
*
|
|
138
|
+
* **Does not** change the schema used.
|
|
139
|
+
*/
|
|
140
|
+
overrideLayer?: number;
|
|
141
|
+
/**
|
|
142
|
+
* **EXPERT USE ONLY**
|
|
143
|
+
*
|
|
144
|
+
* Override reader map used for the connection.
|
|
145
|
+
*/
|
|
146
|
+
readerMap?: TlReaderMap;
|
|
147
|
+
/**
|
|
148
|
+
* **EXPERT USE ONLY**
|
|
149
|
+
*
|
|
150
|
+
* Override writer map used for the connection.
|
|
151
|
+
*/
|
|
152
|
+
writerMap?: TlWriterMap;
|
|
153
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-client.types.js","sourceRoot":"","sources":["../../src/base-client.types.ts"],"names":[],"mappings":"","sourcesContent":["import { tl } from '@mtcute/tl'\nimport { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime'\n\nimport { NetworkManagerExtraParams, ReconnectionStrategy, TransportFactory } from './network/index.js'\nimport { PersistentConnectionParams } from './network/persistent-connection.js'\nimport { ITelegramStorage } from './storage/abstract.js'\nimport { CryptoProviderFactory } from './utils/index.js'\n\n/** Options for {@link BaseTelegramClient} */\nexport interface BaseTelegramClientOptions {\n /**\n * API ID from my.telegram.org\n */\n apiId: number\n /**\n * API hash from my.telegram.org\n */\n apiHash: string\n\n /**\n * Storage to use for this client.\n */\n storage: ITelegramStorage\n\n /**\n * Cryptography provider factory to allow delegating\n * crypto to native addon, worker, etc.\n */\n crypto?: CryptoProviderFactory\n\n /**\n * Whether to use IPv6 datacenters\n * (IPv6 will be preferred when choosing a DC by id)\n * (default: false)\n */\n useIpv6?: boolean\n\n /**\n * Primary DC to use for initial connection.\n * This does not mean this will be the only DC used,\n * nor that this DC will actually be primary, this only\n * determines the first DC the library will try to connect to.\n * Can be used to connect to other networks (like test DCs).\n *\n * When session already contains primary DC, this parameter is ignored.\n *\n * @default Production DC 2.\n */\n defaultDcs?: ITelegramStorage.DcOptions\n\n /**\n * Whether to connect to test servers.\n *\n * If passed, {@link defaultDc} defaults to Test DC 2.\n *\n * **Must** be passed if using test servers, even if\n * you passed custom {@link defaultDc}\n */\n testMode?: boolean\n\n /**\n * Additional options for initConnection call.\n * `apiId` and `query` are not available and will be ignored.\n * Omitted values will be filled with defaults\n */\n initConnectionOptions?: Partial<Omit<tl.RawInitConnectionRequest, 'apiId' | 'query'>>\n\n /**\n * Transport factory to use in the client.\n *\n * @default platform-specific transport: WebSocket on the web, TCP in node\n */\n transport?: TransportFactory\n\n /**\n * Reconnection strategy.\n *\n * @default simple reconnection strategy: first 0ms, then up to 5s (increasing by 1s)\n */\n reconnectionStrategy?: ReconnectionStrategy<PersistentConnectionParams>\n\n /**\n * Maximum duration of a flood_wait that will be waited automatically.\n * Flood waits above this threshold will throw a FloodWaitError.\n * Set to 0 to disable. Can be overridden with `throwFlood` parameter in call() params\n *\n * @default 10000\n */\n floodSleepThreshold?: number\n\n /**\n * Maximum number of retries when calling RPC methods.\n * Call is retried when InternalError or FloodWaitError is encountered.\n * Can be set to Infinity.\n *\n * @default 5\n */\n maxRetryCount?: number\n\n /**\n * If true, every single API call will be wrapped with `tl.invokeWithoutUpdates`,\n * effectively disabling the server-sent events for the clients.\n * May be useful in some cases.\n *\n * Note that this only wraps calls made with `.call()` within the primary\n * connection. Additional connections and direct `.sendForResult()` calls\n * must be wrapped manually.\n *\n * @default false\n */\n disableUpdates?: boolean\n\n /**\n * mtcute can send all unknown RPC errors to [danog](https://github.com/danog)'s\n * [error reporting service](https://rpc.pwrtelegram.xyz/).\n *\n * This is fully anonymous (except maybe IP) and is only used to improve the library\n * and developer experience for everyone working with MTProto. This is fully opt-in,\n * and if you're too paranoid, you can disable it by manually passing `enableErrorReporting: false` to the client.\n *\n * @default false\n */\n enableErrorReporting?: boolean\n\n /**\n * If true, RPC errors will have a stack trace of the initial `.call()`\n * or `.sendForResult()` call position, which drastically improves\n * debugging experience.<br>\n * If false, they will have a stack trace of mtcute internals.\n *\n * Internally this creates a stack capture before every RPC call\n * and stores it until the result is received. This might\n * use a lot more memory than normal, thus can be disabled here.\n *\n * @default true\n */\n niceStacks?: boolean\n\n /**\n * Extra parameters for {@link NetworkManager}\n */\n network?: NetworkManagerExtraParams\n\n /**\n * Set logging level for the client.\n *\n * See static members of {@link LogManager} for possible values.\n */\n logLevel?: number\n\n /**\n * **EXPERT USE ONLY!**\n *\n * Override TL layer used for the connection.\n *\n * **Does not** change the schema used.\n */\n overrideLayer?: number\n\n /**\n * **EXPERT USE ONLY**\n *\n * Override reader map used for the connection.\n */\n readerMap?: TlReaderMap\n\n /**\n * **EXPERT USE ONLY**\n *\n * Override writer map used for the connection.\n */\n writerMap?: TlWriterMap\n}\n"]}
|
package/esm/index.d.ts
CHANGED
package/esm/index.js
CHANGED
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,oBAAoB,CAAA;AAClC,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,uBAAuB,CAAA;AAErC,cAAc;AACd,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,cAAc;AACd,OAAO,EAAE,IAAI,EAAE,CAAA","sourcesContent":["export * from './base-client.js'\nexport * from './network/index.js'\nexport * from './storage/index.js'\nexport * from './types/index.js'\nexport * from './utils/peer-utils.js'\n\n/** @hidden */\nexport { mtp, tl } from '@mtcute/tl'\nimport Long from 'long'\n/** @hidden */\nexport { Long }\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,wBAAwB,CAAA;AACtC,cAAc,oBAAoB,CAAA;AAClC,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,uBAAuB,CAAA;AAErC,cAAc;AACd,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,cAAc;AACd,OAAO,EAAE,IAAI,EAAE,CAAA","sourcesContent":["export * from './base-client.js'\nexport * from './base-client.types.js'\nexport * from './network/index.js'\nexport * from './storage/index.js'\nexport * from './types/index.js'\nexport * from './utils/peer-utils.js'\n\n/** @hidden */\nexport { mtp, tl } from '@mtcute/tl'\nimport Long from 'long'\n/** @hidden */\nexport { Long }\n"]}
|
|
@@ -4,12 +4,15 @@ import { mtp, tl } from '@mtcute/tl';
|
|
|
4
4
|
import { TlBinaryWriter, TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime';
|
|
5
5
|
import { ControllablePromise, Deque, ICryptoProvider, Logger, LongMap, LruSet, SortedArray } from '../utils/index.js';
|
|
6
6
|
import { AuthKey } from './auth-key.js';
|
|
7
|
+
import { ServerSaltManager } from './server-salt.js';
|
|
7
8
|
export interface PendingRpc {
|
|
8
9
|
method: string;
|
|
9
10
|
data: Uint8Array;
|
|
10
11
|
promise: ControllablePromise;
|
|
11
12
|
stack?: string;
|
|
12
13
|
gzipOverhead?: number;
|
|
14
|
+
chainId?: string | number;
|
|
15
|
+
invokeAfter?: Long;
|
|
13
16
|
sent?: boolean;
|
|
14
17
|
done?: boolean;
|
|
15
18
|
msgId?: Long;
|
|
@@ -63,6 +66,7 @@ export declare class MtprotoSession {
|
|
|
63
66
|
readonly log: Logger;
|
|
64
67
|
readonly _readerMap: TlReaderMap;
|
|
65
68
|
readonly _writerMap: TlWriterMap;
|
|
69
|
+
readonly _salts: ServerSaltManager;
|
|
66
70
|
_sessionId: Long;
|
|
67
71
|
_authKey: AuthKey;
|
|
68
72
|
_authKeyTemp: AuthKey;
|
|
@@ -70,7 +74,6 @@ export declare class MtprotoSession {
|
|
|
70
74
|
_timeOffset: number;
|
|
71
75
|
_lastMessageId: Long;
|
|
72
76
|
_seqNo: number;
|
|
73
|
-
serverSalt: Long;
|
|
74
77
|
recentOutgoingMsgIds: LruSet<Long>;
|
|
75
78
|
recentIncomingMsgIds: LruSet<Long>;
|
|
76
79
|
queuedRpc: Deque<PendingRpc>;
|
|
@@ -79,6 +82,8 @@ export declare class MtprotoSession {
|
|
|
79
82
|
queuedResendReq: Long[];
|
|
80
83
|
queuedCancelReq: Long[];
|
|
81
84
|
getStateSchedule: SortedArray<PendingRpc>;
|
|
85
|
+
chains: Map<string | number, Long>;
|
|
86
|
+
chainsPendingFails: Map<string | number, SortedArray<PendingRpc>>;
|
|
82
87
|
pendingMessages: LongMap<PendingMessage>;
|
|
83
88
|
destroySessionIdToMsgId: LongMap<Long>;
|
|
84
89
|
lastPingRtt: number;
|
|
@@ -90,7 +95,7 @@ export declare class MtprotoSession {
|
|
|
90
95
|
next429Timeout: number;
|
|
91
96
|
current429Timeout?: NodeJS.Timeout;
|
|
92
97
|
next429ResetTimeout?: NodeJS.Timeout;
|
|
93
|
-
constructor(_crypto: ICryptoProvider, log: Logger, _readerMap: TlReaderMap, _writerMap: TlWriterMap);
|
|
98
|
+
constructor(_crypto: ICryptoProvider, log: Logger, _readerMap: TlReaderMap, _writerMap: TlWriterMap, _salts: ServerSaltManager);
|
|
94
99
|
get hasPendingMessages(): boolean;
|
|
95
100
|
/**
|
|
96
101
|
* Reset session by resetting auth key(s) and session state
|
|
@@ -113,4 +118,7 @@ export declare class MtprotoSession {
|
|
|
113
118
|
writeMessage(writer: TlBinaryWriter, content: tl.TlObject | mtp.TlObject | Uint8Array, isContentRelated?: boolean): Long;
|
|
114
119
|
onTransportFlood(callback: () => void): number | undefined;
|
|
115
120
|
resetLastPing(withTime?: boolean): void;
|
|
121
|
+
addToChain(chainId: string | number, msgId: Long): Long | undefined;
|
|
122
|
+
removeFromChain(chainId: string | number, msgId: Long): void;
|
|
123
|
+
getPendingChainedFails(chainId: string | number): SortedArray<PendingRpc>;
|
|
116
124
|
}
|