@mtcute/core 0.30.2 → 0.31.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/client.cjs +73 -0
- package/client.js +73 -0
- package/highlevel/client.d.cts +344 -114
- package/highlevel/client.d.ts +344 -114
- package/highlevel/methods/auth/sign-in-qr.cjs +2 -0
- package/highlevel/methods/auth/sign-in-qr.js +2 -0
- package/highlevel/methods/auth/sign-in-qr.test.d.cts +1 -0
- package/highlevel/methods/auth/sign-in-qr.test.d.ts +1 -0
- package/highlevel/methods/chats/join-chat.cjs +1 -1
- package/highlevel/methods/chats/join-chat.d.cts +2 -1
- package/highlevel/methods/chats/join-chat.d.ts +2 -1
- package/highlevel/methods/chats/join-chat.js +1 -1
- package/highlevel/methods/chats/transfer-chat-ownership.cjs +6 -17
- package/highlevel/methods/chats/transfer-chat-ownership.js +6 -17
- package/highlevel/methods/communities/ban-community-participant.cjs +30 -0
- package/highlevel/methods/communities/ban-community-participant.d.cts +20 -0
- package/highlevel/methods/communities/ban-community-participant.d.ts +20 -0
- package/highlevel/methods/communities/ban-community-participant.js +25 -0
- package/highlevel/methods/communities/create-community.cjs +29 -0
- package/highlevel/methods/communities/create-community.d.cts +22 -0
- package/highlevel/methods/communities/create-community.d.ts +22 -0
- package/highlevel/methods/communities/create-community.js +24 -0
- package/highlevel/methods/communities/get-community-link-requests.cjs +27 -0
- package/highlevel/methods/communities/get-community-link-requests.d.cts +17 -0
- package/highlevel/methods/communities/get-community-link-requests.d.ts +17 -0
- package/highlevel/methods/communities/get-community-link-requests.js +22 -0
- package/highlevel/methods/communities/get-community-participant-chats.cjs +24 -0
- package/highlevel/methods/communities/get-community-participant-chats.d.cts +17 -0
- package/highlevel/methods/communities/get-community-participant-chats.d.ts +17 -0
- package/highlevel/methods/communities/get-community-participant-chats.js +19 -0
- package/highlevel/methods/communities/get-joined-communities.cjs +21 -0
- package/highlevel/methods/communities/get-joined-communities.d.cts +6 -0
- package/highlevel/methods/communities/get-joined-communities.d.ts +6 -0
- package/highlevel/methods/communities/get-joined-communities.js +16 -0
- package/highlevel/methods/communities/hide-community-link-request.cjs +30 -0
- package/highlevel/methods/communities/hide-community-link-request.d.cts +22 -0
- package/highlevel/methods/communities/hide-community-link-request.d.ts +22 -0
- package/highlevel/methods/communities/hide-community-link-request.js +25 -0
- package/highlevel/methods/communities/link-community-peer.cjs +32 -0
- package/highlevel/methods/communities/link-community-peer.d.cts +22 -0
- package/highlevel/methods/communities/link-community-peer.d.ts +22 -0
- package/highlevel/methods/communities/link-community-peer.js +27 -0
- package/highlevel/methods/communities/toggle-community-collapsed.cjs +18 -0
- package/highlevel/methods/communities/toggle-community-collapsed.d.cts +16 -0
- package/highlevel/methods/communities/toggle-community-collapsed.d.ts +16 -0
- package/highlevel/methods/communities/toggle-community-collapsed.js +13 -0
- package/highlevel/methods/ephemeral/delete-ephemeral-message.cjs +20 -0
- package/highlevel/methods/ephemeral/delete-ephemeral-message.d.cts +13 -0
- package/highlevel/methods/ephemeral/delete-ephemeral-message.d.ts +13 -0
- package/highlevel/methods/ephemeral/delete-ephemeral-message.js +15 -0
- package/highlevel/methods/ephemeral/edit-ephemeral-message.cjs +33 -0
- package/highlevel/methods/ephemeral/edit-ephemeral-message.d.cts +25 -0
- package/highlevel/methods/ephemeral/edit-ephemeral-message.d.ts +25 -0
- package/highlevel/methods/ephemeral/edit-ephemeral-message.js +28 -0
- package/highlevel/methods/ephemeral/find-in-update.cjs +27 -0
- package/highlevel/methods/ephemeral/find-in-update.d.cts +8 -0
- package/highlevel/methods/ephemeral/find-in-update.d.ts +8 -0
- package/highlevel/methods/ephemeral/find-in-update.js +22 -0
- package/highlevel/methods/ephemeral/get-ephemeral-callback-answer.cjs +31 -0
- package/highlevel/methods/ephemeral/get-ephemeral-callback-answer.d.cts +29 -0
- package/highlevel/methods/ephemeral/get-ephemeral-callback-answer.d.ts +29 -0
- package/highlevel/methods/ephemeral/get-ephemeral-callback-answer.js +26 -0
- package/highlevel/methods/ephemeral/send-ephemeral-message.cjs +38 -0
- package/highlevel/methods/ephemeral/send-ephemeral-message.d.cts +29 -0
- package/highlevel/methods/ephemeral/send-ephemeral-message.d.ts +29 -0
- package/highlevel/methods/ephemeral/send-ephemeral-message.js +33 -0
- package/highlevel/methods/files/download-iterable.cjs +4 -2
- package/highlevel/methods/files/download-iterable.js +4 -2
- package/highlevel/methods/files/download-iterable.test.d.cts +1 -0
- package/highlevel/methods/files/download-iterable.test.d.ts +1 -0
- package/highlevel/methods/files/download-stream.cjs +5 -6
- package/highlevel/methods/files/download-stream.js +5 -6
- package/highlevel/methods/forums/create-forum-topic.d.cts +7 -0
- package/highlevel/methods/forums/create-forum-topic.d.ts +7 -0
- package/highlevel/methods/forums/delete-forum-topic-history.d.cts +1 -1
- package/highlevel/methods/forums/delete-forum-topic-history.d.ts +1 -1
- package/highlevel/methods/forums/edit-forum-topic.cjs +1 -1
- package/highlevel/methods/forums/edit-forum-topic.d.cts +2 -2
- package/highlevel/methods/forums/edit-forum-topic.d.ts +2 -2
- package/highlevel/methods/forums/edit-forum-topic.js +1 -1
- package/highlevel/methods/forums/get-forum-topics-by-id.cjs +5 -1
- package/highlevel/methods/forums/get-forum-topics-by-id.d.cts +9 -1
- package/highlevel/methods/forums/get-forum-topics-by-id.d.ts +9 -1
- package/highlevel/methods/forums/get-forum-topics-by-id.js +5 -1
- package/highlevel/methods/forums/get-forum-topics-by-id.test.d.cts +1 -0
- package/highlevel/methods/forums/get-forum-topics-by-id.test.d.ts +1 -0
- package/highlevel/methods/forums/get-forum-topics.cjs +1 -1
- package/highlevel/methods/forums/get-forum-topics.js +1 -1
- package/highlevel/methods/forums/toggle-forum-topic-closed.d.cts +1 -1
- package/highlevel/methods/forums/toggle-forum-topic-closed.d.ts +1 -1
- package/highlevel/methods/forums/toggle-forum-topic-pinned.d.cts +1 -1
- package/highlevel/methods/forums/toggle-forum-topic-pinned.d.ts +1 -1
- package/highlevel/methods/forums/toggle-general-topic-hidden.cjs +2 -2
- package/highlevel/methods/forums/toggle-general-topic-hidden.js +3 -3
- package/highlevel/methods/messages/send-common.d.cts +7 -1
- package/highlevel/methods/messages/send-common.d.ts +7 -1
- package/highlevel/methods/messages/send-text.cjs +70 -4
- package/highlevel/methods/messages/send-text.js +70 -4
- package/highlevel/methods/users/resolve-peer.cjs +11 -1
- package/highlevel/methods/users/resolve-peer.js +11 -1
- package/highlevel/methods.d.cts +16 -0
- package/highlevel/methods.d.ts +16 -0
- package/highlevel/storage/service/peers.cjs +25 -0
- package/highlevel/storage/service/peers.js +25 -0
- package/highlevel/storage/service/peers.test.d.cts +1 -0
- package/highlevel/storage/service/peers.test.d.ts +1 -0
- package/highlevel/types/conversation.cjs +8 -0
- package/highlevel/types/conversation.d.cts +1 -0
- package/highlevel/types/conversation.d.ts +1 -0
- package/highlevel/types/conversation.js +10 -2
- package/highlevel/types/conversation.test.d.cts +1 -0
- package/highlevel/types/conversation.test.d.ts +1 -0
- package/highlevel/types/messages/ephemeral-message.cjs +103 -0
- package/highlevel/types/messages/ephemeral-message.d.cts +55 -0
- package/highlevel/types/messages/ephemeral-message.d.ts +55 -0
- package/highlevel/types/messages/ephemeral-message.js +98 -0
- package/highlevel/types/messages/index.d.cts +1 -0
- package/highlevel/types/messages/index.d.ts +1 -0
- package/highlevel/types/messages/message-action.cjs +6 -0
- package/highlevel/types/messages/message-action.d.cts +8 -1
- package/highlevel/types/messages/message-action.d.ts +8 -1
- package/highlevel/types/messages/message-action.js +6 -0
- package/highlevel/types/messages/replied-message.cjs +5 -1
- package/highlevel/types/messages/replied-message.d.cts +5 -1
- package/highlevel/types/messages/replied-message.d.ts +5 -1
- package/highlevel/types/messages/replied-message.js +5 -1
- package/highlevel/types/peers/chat.cjs +23 -3
- package/highlevel/types/peers/chat.d.cts +8 -3
- package/highlevel/types/peers/chat.d.ts +8 -3
- package/highlevel/types/peers/chat.js +24 -4
- package/highlevel/types/peers/community-peer.cjs +60 -0
- package/highlevel/types/peers/community-peer.d.cts +37 -0
- package/highlevel/types/peers/community-peer.d.ts +37 -0
- package/highlevel/types/peers/community-peer.js +55 -0
- package/highlevel/types/peers/full-chat.cjs +20 -5
- package/highlevel/types/peers/full-chat.d.cts +5 -0
- package/highlevel/types/peers/full-chat.d.ts +5 -0
- package/highlevel/types/peers/full-chat.js +20 -5
- package/highlevel/types/peers/index.d.cts +1 -0
- package/highlevel/types/peers/index.d.ts +1 -0
- package/highlevel/types/peers/peer.cjs +2 -0
- package/highlevel/types/peers/peer.js +2 -0
- package/highlevel/types/peers/user.cjs +6 -0
- package/highlevel/types/peers/user.d.cts +2 -0
- package/highlevel/types/peers/user.d.ts +2 -0
- package/highlevel/types/peers/user.js +6 -0
- package/highlevel/types/updates/delete-ephemeral-messages-update.cjs +27 -0
- package/highlevel/types/updates/delete-ephemeral-messages-update.d.cts +15 -0
- package/highlevel/types/updates/delete-ephemeral-messages-update.d.ts +15 -0
- package/highlevel/types/updates/delete-ephemeral-messages-update.js +22 -0
- package/highlevel/types/updates/ephemeral-callback-query.cjs +61 -0
- package/highlevel/types/updates/ephemeral-callback-query.d.cts +38 -0
- package/highlevel/types/updates/ephemeral-callback-query.d.ts +38 -0
- package/highlevel/types/updates/ephemeral-callback-query.js +56 -0
- package/highlevel/types/updates/index.d.cts +16 -2
- package/highlevel/types/updates/index.d.ts +16 -2
- package/highlevel/types/updates/parse-update.cjs +12 -1
- package/highlevel/types/updates/parse-update.js +12 -1
- package/highlevel/updates/manager.cjs +31 -13
- package/highlevel/updates/manager.d.cts +1 -0
- package/highlevel/updates/manager.d.ts +1 -0
- package/highlevel/updates/manager.js +31 -13
- package/highlevel/updates/manager.test.d.cts +1 -0
- package/highlevel/updates/manager.test.d.ts +1 -0
- package/highlevel/utils/stream-utils.cjs +8 -4
- package/highlevel/utils/stream-utils.js +8 -4
- package/highlevel/utils/stream-utils.test.d.cts +1 -0
- package/highlevel/utils/stream-utils.test.d.ts +1 -0
- package/highlevel/utils/walk-page-blocks.cjs +4 -0
- package/highlevel/utils/walk-page-blocks.js +4 -0
- package/index.cjs +10 -0
- package/index.js +11 -1
- package/methods.cjs +27 -0
- package/methods.js +27 -0
- package/network/client.cjs +4 -3
- package/network/client.d.cts +1 -0
- package/network/client.d.ts +1 -0
- package/network/client.js +4 -3
- package/network/middlewares/flood-waiter.cjs +2 -2
- package/network/middlewares/flood-waiter.js +2 -2
- package/network/middlewares/internal-errors.cjs +1 -1
- package/network/middlewares/internal-errors.js +1 -1
- package/network/middlewares/media-throttle.cjs +1 -1
- package/network/middlewares/media-throttle.js +1 -1
- package/network/mtproxy/index.cjs +4 -3
- package/network/mtproxy/index.d.cts +2 -2
- package/network/mtproxy/index.d.ts +2 -2
- package/network/mtproxy/index.js +4 -3
- package/network/network-manager.cjs +44 -16
- package/network/network-manager.d.cts +10 -3
- package/network/network-manager.d.ts +10 -3
- package/network/network-manager.js +45 -17
- package/network/network-manager.test.d.cts +1 -0
- package/network/network-manager.test.d.ts +1 -0
- package/network/persistent-connection.cjs +10 -2
- package/network/persistent-connection.js +10 -2
- package/network/session-connection.cjs +3 -0
- package/network/session-connection.js +3 -0
- package/network/transports/abstract.cjs +16 -0
- package/network/transports/abstract.d.cts +8 -2
- package/network/transports/abstract.d.ts +8 -2
- package/network/transports/abstract.js +17 -1
- package/network/transports/obfuscated.cjs +3 -1
- package/network/transports/obfuscated.js +3 -1
- package/package.json +6 -6
- package/tl/api-schema.json +1 -1
- package/tl/binary/reader.cjs +90 -15
- package/tl/binary/reader.js +90 -15
- package/tl/binary/writer.cjs +385 -80
- package/tl/binary/writer.js +385 -80
- package/tl/compat/reader.cjs +11 -0
- package/tl/compat/reader.js +11 -0
- package/tl/index.d.cts +12380 -4272
- package/tl/index.d.ts +12380 -4272
- package/tl/inner-tl.cjs +70 -20
- package/tl/inner-tl.js +70 -20
- package/tl/raw-errors.json +1 -1
- package/utils/abort-signal.cjs +1 -12
- package/utils/abort-signal.js +1 -12
- package/utils/abort-signal.test.d.cts +1 -0
- package/utils/abort-signal.test.d.ts +1 -0
- package/utils/binary/compat.cjs +14 -0
- package/utils/binary/compat.js +14 -0
- package/utils/early-timer.cjs +3 -11
- package/utils/early-timer.js +3 -11
- package/utils/early-timer.test.d.cts +1 -0
- package/utils/early-timer.test.d.ts +1 -0
- package/utils/function-utils.cjs +9 -4
- package/utils/function-utils.js +9 -4
- package/utils/links/bots.cjs +3 -0
- package/utils/links/bots.js +3 -0
- package/utils/peer-utils.cjs +8 -0
- package/utils/peer-utils.js +8 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defaultReconnectionStrategy } from "@fuman/net";
|
|
2
|
-
import { asNonNull, Deferred, LruMap, composeMiddlewares } from "@fuman/utils";
|
|
2
|
+
import { asNonNull, Deferred, noop, LruMap, composeMiddlewares } from "@fuman/utils";
|
|
3
3
|
import { dropUndefined } from "../utils/misc-utils.js";
|
|
4
4
|
import { isTlRpcError, assertTypeIs } from "../utils/type-assertions.js";
|
|
5
5
|
import "@mtcute/tl-runtime";
|
|
@@ -220,7 +220,7 @@ class NetworkManager {
|
|
|
220
220
|
_: "initConnection",
|
|
221
221
|
deviceModel,
|
|
222
222
|
systemVersion: "1.0",
|
|
223
|
-
appVersion: "0.
|
|
223
|
+
appVersion: "0.31.0",
|
|
224
224
|
systemLangCode: "en",
|
|
225
225
|
langPack: "",
|
|
226
226
|
// "langPacks are for official apps only"
|
|
@@ -272,34 +272,45 @@ class NetworkManager {
|
|
|
272
272
|
return { main, media };
|
|
273
273
|
}
|
|
274
274
|
_resetOnNetworkChange;
|
|
275
|
+
_destroyed = false;
|
|
276
|
+
_teardownAbort = new AbortController();
|
|
277
|
+
/** Signal that is aborted when the network manager is disconnected or destroyed */
|
|
278
|
+
get teardownSignal() {
|
|
279
|
+
return this._teardownAbort.signal;
|
|
280
|
+
}
|
|
281
|
+
_onPrimaryUsable = () => this.params.onUsable();
|
|
282
|
+
_onPrimaryWait = () => this.params.onConnecting();
|
|
283
|
+
_onPrimaryUpdate = (update) => this._updateHandler(update, false);
|
|
275
284
|
_switchPrimaryDc(dc) {
|
|
276
285
|
if (this._primaryDc && this._primaryDc !== dc) {
|
|
277
286
|
this._primaryDc.setIsPrimary(false);
|
|
287
|
+
this._primaryDc.main.onUsable.remove(this._onPrimaryUsable);
|
|
288
|
+
this._primaryDc.main.onWait.remove(this._onPrimaryWait);
|
|
289
|
+
this._primaryDc.main.onUpdate.remove(this._onPrimaryUpdate);
|
|
278
290
|
}
|
|
279
291
|
this._primaryDc = dc;
|
|
280
292
|
dc.setIsPrimary(true);
|
|
281
293
|
this.params.onConnecting();
|
|
282
|
-
dc.main.onUsable.
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
dc.main.onWait.add(
|
|
287
|
-
|
|
288
|
-
this.params.onConnecting();
|
|
289
|
-
});
|
|
290
|
-
dc.main.onUpdate.add((update) => {
|
|
291
|
-
this._updateHandler(update, false);
|
|
292
|
-
});
|
|
294
|
+
dc.main.onUsable.remove(this._onPrimaryUsable);
|
|
295
|
+
dc.main.onWait.remove(this._onPrimaryWait);
|
|
296
|
+
dc.main.onUpdate.remove(this._onPrimaryUpdate);
|
|
297
|
+
dc.main.onUsable.add(this._onPrimaryUsable);
|
|
298
|
+
dc.main.onWait.add(this._onPrimaryWait);
|
|
299
|
+
dc.main.onUpdate.add(this._onPrimaryUpdate);
|
|
293
300
|
return dc.loadKeys().then(() => dc.main.ensureConnected());
|
|
294
301
|
}
|
|
295
302
|
_dcCreationPromise = /* @__PURE__ */ new Map();
|
|
296
303
|
async _getOtherDc(dcId) {
|
|
304
|
+
if (this._destroyed) {
|
|
305
|
+
throw new MtcuteError("Network manager is destroyed");
|
|
306
|
+
}
|
|
297
307
|
if (!this._dcConnections.has(dcId)) {
|
|
298
308
|
if (this._dcCreationPromise.has(dcId)) {
|
|
299
309
|
await this._dcCreationPromise.get(dcId);
|
|
300
310
|
return asNonNull(this._dcConnections.get(dcId));
|
|
301
311
|
}
|
|
302
312
|
const promise = new Deferred();
|
|
313
|
+
promise.promise.catch(noop);
|
|
303
314
|
this._dcCreationPromise.set(dcId, promise.promise);
|
|
304
315
|
this._log.debug("creating new DC %d", dcId);
|
|
305
316
|
try {
|
|
@@ -316,7 +327,7 @@ class NetworkManager {
|
|
|
316
327
|
this._log.debug("dc %d was not created: %e", dcId, e);
|
|
317
328
|
promise.reject(e);
|
|
318
329
|
this._dcCreationPromise.delete(dcId);
|
|
319
|
-
|
|
330
|
+
throw e;
|
|
320
331
|
}
|
|
321
332
|
}
|
|
322
333
|
return this._dcConnections.get(dcId);
|
|
@@ -327,9 +338,15 @@ class NetworkManager {
|
|
|
327
338
|
* @param defaultDcs Default DCs to connect to
|
|
328
339
|
*/
|
|
329
340
|
async connect(defaultDcs) {
|
|
341
|
+
if (this._destroyed) {
|
|
342
|
+
throw new MtcuteError("Network manager is destroyed");
|
|
343
|
+
}
|
|
330
344
|
if (defaultDcs.main.id !== defaultDcs.media.id) {
|
|
331
345
|
throw new MtArgumentError("Default DCs must be the same");
|
|
332
346
|
}
|
|
347
|
+
if (this._teardownAbort.signal.aborted) {
|
|
348
|
+
this._teardownAbort = new AbortController();
|
|
349
|
+
}
|
|
333
350
|
if (this._dcConnections.has(defaultDcs.main.id)) {
|
|
334
351
|
throw new MtArgumentError("DC manager already exists");
|
|
335
352
|
}
|
|
@@ -368,11 +385,12 @@ class NetworkManager {
|
|
|
368
385
|
}
|
|
369
386
|
assertTypeIs("auth.importAuthorization", res, "auth.authorization");
|
|
370
387
|
promise.resolve();
|
|
371
|
-
this._pendingExports.delete(manager.dcId);
|
|
372
388
|
} catch (e) {
|
|
373
389
|
this._log.warn("failed to export auth to dc %d: %s", manager.dcId, e);
|
|
374
390
|
promise.reject(e);
|
|
375
391
|
throw e;
|
|
392
|
+
} finally {
|
|
393
|
+
this._pendingExports.delete(manager.dcId);
|
|
376
394
|
}
|
|
377
395
|
}
|
|
378
396
|
setIsPremium(isPremium) {
|
|
@@ -442,6 +460,9 @@ class NetworkManager {
|
|
|
442
460
|
}
|
|
443
461
|
}
|
|
444
462
|
async changePrimaryDc(newDc) {
|
|
463
|
+
if (this._destroyed) {
|
|
464
|
+
throw new MtcuteError("Network manager is destroyed");
|
|
465
|
+
}
|
|
445
466
|
if (newDc === this._primaryDc?.dcId) return;
|
|
446
467
|
const options = await this._findDcOptions(newDc);
|
|
447
468
|
if (!this._dcConnections.has(newDc)) {
|
|
@@ -574,13 +595,20 @@ class NetworkManager {
|
|
|
574
595
|
if (!this._primaryDc) throw new MtcuteError("Not connected to any DC");
|
|
575
596
|
return this._primaryDc.dcId;
|
|
576
597
|
}
|
|
577
|
-
async
|
|
598
|
+
async disconnect() {
|
|
599
|
+
this._teardownAbort.abort();
|
|
600
|
+
this._primaryDc = void 0;
|
|
578
601
|
for (const dc of this._dcConnections.values()) {
|
|
579
602
|
await dc.destroy();
|
|
580
603
|
}
|
|
581
604
|
this._dcConnections.clear();
|
|
582
|
-
this.config.onUpdated.remove(this._onConfigChanged);
|
|
583
605
|
this._resetOnNetworkChange?.();
|
|
606
|
+
this._resetOnNetworkChange = void 0;
|
|
607
|
+
}
|
|
608
|
+
async destroy() {
|
|
609
|
+
this._destroyed = true;
|
|
610
|
+
await this.disconnect();
|
|
611
|
+
this.config.onUpdated.remove(this._onConfigChanged);
|
|
584
612
|
}
|
|
585
613
|
getMtprotoMessageId() {
|
|
586
614
|
return this._primaryDc.main._connections[0]._session.getMessageId();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -23,7 +23,7 @@ class PersistentConnection {
|
|
|
23
23
|
}
|
|
24
24
|
this._onInactivityTimeout = this._onInactivityTimeout.bind(this);
|
|
25
25
|
this._fuman = new net.PersistentConnection({
|
|
26
|
-
connect: async (dc) => {
|
|
26
|
+
connect: async (dc, abortSignal) => {
|
|
27
27
|
if (this._floodControl) {
|
|
28
28
|
if (!this._connectAbort || this._connectAbort.signal.aborted) {
|
|
29
29
|
this._connectAbort = new AbortController();
|
|
@@ -33,7 +33,7 @@ class PersistentConnection {
|
|
|
33
33
|
}
|
|
34
34
|
this._updateLogPrefix();
|
|
35
35
|
this.log.debug("connecting to %j", dc);
|
|
36
|
-
return params.transport.connect(dc);
|
|
36
|
+
return params.transport.connect(dc, abortSignal);
|
|
37
37
|
},
|
|
38
38
|
strategy: params.reconnectionStrategy,
|
|
39
39
|
onOpen: this._onOpen.bind(this),
|
|
@@ -139,10 +139,18 @@ class PersistentConnection {
|
|
|
139
139
|
this._fuman.connect(this.params.dc);
|
|
140
140
|
}
|
|
141
141
|
connect() {
|
|
142
|
+
if (this._destroyed) {
|
|
143
|
+
this.log.warn("tried to connect a destroyed connection, ignoring");
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
142
146
|
this._fuman.connect(this.params.dc);
|
|
143
147
|
this._inactive = false;
|
|
144
148
|
}
|
|
145
149
|
reconnect() {
|
|
150
|
+
if (this._destroyed) {
|
|
151
|
+
this.log.warn("tried to reconnect a destroyed connection, ignoring");
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
146
154
|
if (this._disconnectedManually) {
|
|
147
155
|
this._disconnectedManually = false;
|
|
148
156
|
this.connect();
|
|
@@ -16,7 +16,7 @@ class PersistentConnection {
|
|
|
16
16
|
}
|
|
17
17
|
this._onInactivityTimeout = this._onInactivityTimeout.bind(this);
|
|
18
18
|
this._fuman = new PersistentConnection$1({
|
|
19
|
-
connect: async (dc) => {
|
|
19
|
+
connect: async (dc, abortSignal) => {
|
|
20
20
|
if (this._floodControl) {
|
|
21
21
|
if (!this._connectAbort || this._connectAbort.signal.aborted) {
|
|
22
22
|
this._connectAbort = new AbortController();
|
|
@@ -26,7 +26,7 @@ class PersistentConnection {
|
|
|
26
26
|
}
|
|
27
27
|
this._updateLogPrefix();
|
|
28
28
|
this.log.debug("connecting to %j", dc);
|
|
29
|
-
return params.transport.connect(dc);
|
|
29
|
+
return params.transport.connect(dc, abortSignal);
|
|
30
30
|
},
|
|
31
31
|
strategy: params.reconnectionStrategy,
|
|
32
32
|
onOpen: this._onOpen.bind(this),
|
|
@@ -132,10 +132,18 @@ class PersistentConnection {
|
|
|
132
132
|
this._fuman.connect(this.params.dc);
|
|
133
133
|
}
|
|
134
134
|
connect() {
|
|
135
|
+
if (this._destroyed) {
|
|
136
|
+
this.log.warn("tried to connect a destroyed connection, ignoring");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
135
139
|
this._fuman.connect(this.params.dc);
|
|
136
140
|
this._inactive = false;
|
|
137
141
|
}
|
|
138
142
|
reconnect() {
|
|
143
|
+
if (this._destroyed) {
|
|
144
|
+
this.log.warn("tried to reconnect a destroyed connection, ignoring");
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
139
147
|
if (this._disconnectedManually) {
|
|
140
148
|
this._disconnectedManually = false;
|
|
141
149
|
this.connect();
|
|
@@ -1008,6 +1008,9 @@ class SessionConnection extends persistentConnection.PersistentConnection {
|
|
|
1008
1008
|
}
|
|
1009
1009
|
}
|
|
1010
1010
|
sendRpc(request, timeout, abortSignal, chainId) {
|
|
1011
|
+
if (this._destroyed) {
|
|
1012
|
+
return Promise.reject(new errors.MtcuteError("Connection destroyed"));
|
|
1013
|
+
}
|
|
1011
1014
|
if (this._usable && this.params.inactivityTimeout) {
|
|
1012
1015
|
this._rescheduleInactivity();
|
|
1013
1016
|
}
|
|
@@ -1001,6 +1001,9 @@ class SessionConnection extends PersistentConnection {
|
|
|
1001
1001
|
}
|
|
1002
1002
|
}
|
|
1003
1003
|
sendRpc(request, timeout, abortSignal, chainId) {
|
|
1004
|
+
if (this._destroyed) {
|
|
1005
|
+
return Promise.reject(new MtcuteError("Connection destroyed"));
|
|
1006
|
+
}
|
|
1004
1007
|
if (this._usable && this.params.inactivityTimeout) {
|
|
1005
1008
|
this._rescheduleInactivity();
|
|
1006
1009
|
}
|
|
@@ -5,6 +5,21 @@ if (typeof globalThis !== "undefined" && !globalThis._MTCUTE_CJS_DEPRECATION_WAR
|
|
|
5
5
|
}
|
|
6
6
|
"use strict";
|
|
7
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
|
+
async function performHandshakeWithAbort(conn, abortSignal, handshake) {
|
|
9
|
+
if (abortSignal.aborted) {
|
|
10
|
+
conn.close();
|
|
11
|
+
throw abortSignal.reason;
|
|
12
|
+
}
|
|
13
|
+
const onAbort = () => conn.close();
|
|
14
|
+
abortSignal.addEventListener("abort", onAbort);
|
|
15
|
+
try {
|
|
16
|
+
await handshake();
|
|
17
|
+
} finally {
|
|
18
|
+
abortSignal.removeEventListener("abort", onAbort);
|
|
19
|
+
}
|
|
20
|
+
abortSignal.throwIfAborted();
|
|
21
|
+
return conn;
|
|
22
|
+
}
|
|
8
23
|
class TransportError extends Error {
|
|
9
24
|
code;
|
|
10
25
|
constructor(code) {
|
|
@@ -13,3 +28,4 @@ class TransportError extends Error {
|
|
|
13
28
|
}
|
|
14
29
|
}
|
|
15
30
|
exports.TransportError = TransportError;
|
|
31
|
+
exports.performHandshakeWithAbort = performHandshakeWithAbort;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IFrameDecoder, IFrameEncoder } from '@fuman/io';
|
|
1
|
+
import { IClosable, IFrameDecoder, IFrameEncoder } from '@fuman/io';
|
|
2
2
|
import { IConnection } from '@fuman/net';
|
|
3
3
|
import { tl } from '../../tl/index.js';
|
|
4
4
|
import { MaybePromise } from '../../types/index.js';
|
|
@@ -18,9 +18,15 @@ export interface ITelegramConnection extends IConnection<any, any> {
|
|
|
18
18
|
}
|
|
19
19
|
export interface TelegramTransport {
|
|
20
20
|
setup?(crypto: ICryptoProvider, log: Logger): void;
|
|
21
|
-
connect: (dc: BasicDcOption) => Promise<ITelegramConnection>;
|
|
21
|
+
connect: (dc: BasicDcOption, abortSignal: AbortSignal) => Promise<ITelegramConnection>;
|
|
22
22
|
packetCodec: (dc: BasicDcOption) => IPacketCodec;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Run a post-connect handshake, closing the connection if `abortSignal`
|
|
26
|
+
* is aborted before it completes (which in turn makes any pending
|
|
27
|
+
* reads/writes inside the handshake reject)
|
|
28
|
+
*/
|
|
29
|
+
export declare function performHandshakeWithAbort<Connection extends IClosable>(conn: Connection, abortSignal: AbortSignal, handshake: () => Promise<void>): Promise<Connection>;
|
|
24
30
|
/**
|
|
25
31
|
* Interface declaring handling of received packets.
|
|
26
32
|
* When receiving a packet, its content is sent to feed(),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IFrameDecoder, IFrameEncoder } from '@fuman/io';
|
|
1
|
+
import { IClosable, IFrameDecoder, IFrameEncoder } from '@fuman/io';
|
|
2
2
|
import { IConnection } from '@fuman/net';
|
|
3
3
|
import { tl } from '../../tl/index.js';
|
|
4
4
|
import { MaybePromise } from '../../types/index.js';
|
|
@@ -18,9 +18,15 @@ export interface ITelegramConnection extends IConnection<any, any> {
|
|
|
18
18
|
}
|
|
19
19
|
export interface TelegramTransport {
|
|
20
20
|
setup?(crypto: ICryptoProvider, log: Logger): void;
|
|
21
|
-
connect: (dc: BasicDcOption) => Promise<ITelegramConnection>;
|
|
21
|
+
connect: (dc: BasicDcOption, abortSignal: AbortSignal) => Promise<ITelegramConnection>;
|
|
22
22
|
packetCodec: (dc: BasicDcOption) => IPacketCodec;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Run a post-connect handshake, closing the connection if `abortSignal`
|
|
26
|
+
* is aborted before it completes (which in turn makes any pending
|
|
27
|
+
* reads/writes inside the handshake reject)
|
|
28
|
+
*/
|
|
29
|
+
export declare function performHandshakeWithAbort<Connection extends IClosable>(conn: Connection, abortSignal: AbortSignal, handshake: () => Promise<void>): Promise<Connection>;
|
|
24
30
|
/**
|
|
25
31
|
* Interface declaring handling of received packets.
|
|
26
32
|
* When receiving a packet, its content is sent to feed(),
|
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
async function performHandshakeWithAbort(conn, abortSignal, handshake) {
|
|
2
|
+
if (abortSignal.aborted) {
|
|
3
|
+
conn.close();
|
|
4
|
+
throw abortSignal.reason;
|
|
5
|
+
}
|
|
6
|
+
const onAbort = () => conn.close();
|
|
7
|
+
abortSignal.addEventListener("abort", onAbort);
|
|
8
|
+
try {
|
|
9
|
+
await handshake();
|
|
10
|
+
} finally {
|
|
11
|
+
abortSignal.removeEventListener("abort", onAbort);
|
|
12
|
+
}
|
|
13
|
+
abortSignal.throwIfAborted();
|
|
14
|
+
return conn;
|
|
15
|
+
}
|
|
1
16
|
class TransportError extends Error {
|
|
2
17
|
code;
|
|
3
18
|
constructor(code) {
|
|
@@ -6,5 +21,6 @@ class TransportError extends Error {
|
|
|
6
21
|
}
|
|
7
22
|
}
|
|
8
23
|
export {
|
|
9
|
-
TransportError
|
|
24
|
+
TransportError,
|
|
25
|
+
performHandshakeWithAbort
|
|
10
26
|
};
|
|
@@ -74,7 +74,9 @@ class ObfuscatedPacketCodec {
|
|
|
74
74
|
const into = this._decodeBuf.writeSync(reader.available);
|
|
75
75
|
into.set(this._decryptor.process(reader.readSync(reader.available)));
|
|
76
76
|
}
|
|
77
|
-
|
|
77
|
+
const packet = await this._inner.decode(this._decodeBuf, eof);
|
|
78
|
+
this._decodeBuf.reclaim();
|
|
79
|
+
return packet;
|
|
78
80
|
}
|
|
79
81
|
reset() {
|
|
80
82
|
this._inner.reset();
|
|
@@ -67,7 +67,9 @@ class ObfuscatedPacketCodec {
|
|
|
67
67
|
const into = this._decodeBuf.writeSync(reader.available);
|
|
68
68
|
into.set(this._decryptor.process(reader.readSync(reader.available)));
|
|
69
69
|
}
|
|
70
|
-
|
|
70
|
+
const packet = await this._inner.decode(this._decodeBuf, eof);
|
|
71
|
+
this._decodeBuf.reclaim();
|
|
72
|
+
return packet;
|
|
71
73
|
}
|
|
72
74
|
reset() {
|
|
73
75
|
this._inner.reset();
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mtcute/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.31.0",
|
|
5
5
|
"description": "Type-safe library for MTProto (Telegram API)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"scripts": {},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@fuman/io": "0.0.
|
|
10
|
-
"@fuman/net": "0.0.
|
|
11
|
-
"@fuman/utils": "0.0.
|
|
12
|
-
"@mtcute/file-id": "^0.
|
|
13
|
-
"@mtcute/tl-runtime": "^0.
|
|
9
|
+
"@fuman/io": "0.0.21",
|
|
10
|
+
"@fuman/net": "0.0.21",
|
|
11
|
+
"@fuman/utils": "0.0.21",
|
|
12
|
+
"@mtcute/file-id": "^0.31.0",
|
|
13
|
+
"@mtcute/tl-runtime": "^0.31.0",
|
|
14
14
|
"@types/events": "3.0.0",
|
|
15
15
|
"long": "^5.3.2"
|
|
16
16
|
},
|