浏览代码

Fix switching DCs on start.

Painor 1 年之前
父节点
当前提交
1a47334ded
共有 7 个文件被更改,包括 39 次插入21 次删除
  1. 1 1
      gramjs/Version.ts
  2. 3 3
      gramjs/client/TelegramClient.ts
  3. 0 1
      gramjs/client/updates.ts
  4. 32 8
      gramjs/client/users.ts
  5. 2 2
      package-lock.json
  6. 1 1
      package.json
  7. 0 5
      publish_npm.js

+ 1 - 1
gramjs/Version.ts

@@ -1 +1 @@
-export const version = "2.18.4";
+export const version = "2.18.16";

+ 3 - 3
gramjs/client/TelegramClient.ts

@@ -1188,7 +1188,7 @@ export class TelegramClient extends TelegramBaseClient {
      * Generally this should only be used when there isn't a friendly method that does what you need.<br/>
      * Generally this should only be used when there isn't a friendly method that does what you need.<br/>
      * All available requests and types are found under the `Api.` namespace.
      * All available requests and types are found under the `Api.` namespace.
      * @param request - The request to send. this should be of type request.
      * @param request - The request to send. this should be of type request.
-     * @param sender - Optional sender to use to send the requests. defaults to main sender.
+     * @param dcId - Optional dc id to use when sending.
      * @return The response from Telegram.
      * @return The response from Telegram.
      * @example
      * @example
      * ```ts
      * ```ts
@@ -1202,9 +1202,9 @@ export class TelegramClient extends TelegramBaseClient {
      */
      */
     invoke<R extends Api.AnyRequest>(
     invoke<R extends Api.AnyRequest>(
         request: R,
         request: R,
-        sender?: MTProtoSender
+        dcId?: number
     ): Promise<R["__response"]> {
     ): Promise<R["__response"]> {
-        return userMethods.invoke(this, request, sender);
+        return userMethods.invoke(this, request, dcId);
     }
     }
 
 
     /**
     /**

+ 0 - 1
gramjs/client/updates.ts

@@ -251,7 +251,6 @@ export async function _updateLoop(client: TelegramClient) {
             if (client._sender!.isReconnecting || client._isSwitchingDc) {
             if (client._sender!.isReconnecting || client._isSwitchingDc) {
                 continue;
                 continue;
             }
             }
-            console.log("recoonecting");
             client._sender!.reconnect();
             client._sender!.reconnect();
         }
         }
 
 

+ 32 - 8
gramjs/client/users.ts

@@ -12,7 +12,7 @@ import { errors, utils } from "../";
 import type { TelegramClient } from "../";
 import type { TelegramClient } from "../";
 import bigInt from "big-integer";
 import bigInt from "big-integer";
 import { LogLevel } from "../extensions/Logger";
 import { LogLevel } from "../extensions/Logger";
-import { MTProtoSender } from "../network";
+import { RequestState } from "../network/RequestState";
 
 
 // UserMethods {
 // UserMethods {
 // region Invoking Telegram request
 // region Invoking Telegram request
@@ -21,14 +21,13 @@ import { MTProtoSender } from "../network";
 export async function invoke<R extends Api.AnyRequest>(
 export async function invoke<R extends Api.AnyRequest>(
     client: TelegramClient,
     client: TelegramClient,
     request: R,
     request: R,
-    sender?: MTProtoSender
+    dcId?: number
 ): Promise<R["__response"]> {
 ): Promise<R["__response"]> {
     if (request.classType !== "request") {
     if (request.classType !== "request") {
         throw new Error("You can only invoke MTProtoRequests");
         throw new Error("You can only invoke MTProtoRequests");
     }
     }
-    if (!sender) {
-        sender = client._sender;
-    }
+    const isExported = dcId !== undefined;
+    let sender = !isExported ? client._sender : await client.getSender(dcId);
     if (sender == undefined) {
     if (sender == undefined) {
         throw new Error(
         throw new Error(
             "Cannot send requests while disconnected. You need to call .connect()"
             "Cannot send requests while disconnected. You need to call .connect()"
@@ -39,13 +38,18 @@ export async function invoke<R extends Api.AnyRequest>(
 
 
     await request.resolve(client, utils);
     await request.resolve(client, utils);
     client._lastRequest = new Date().getTime();
     client._lastRequest = new Date().getTime();
-    let attempt: number;
+    const state = new RequestState(request);
+
+    let attempt: number = 0;
     for (attempt = 0; attempt < client._requestRetries; attempt++) {
     for (attempt = 0; attempt < client._requestRetries; attempt++) {
+        sender!.addStateToQueue(state);
+
         try {
         try {
-            const promise = sender.send(request);
-            const result = await promise;
+            const result = await state.promise;
+            state.finished.resolve();
             client.session.processEntities(result);
             client.session.processEntities(result);
             client._entityCache.add(result);
             client._entityCache.add(result);
+
             return result;
             return result;
         } catch (e: any) {
         } catch (e: any) {
             if (
             if (
@@ -67,6 +71,8 @@ export async function invoke<R extends Api.AnyRequest>(
                     );
                     );
                     await sleep(e.seconds * 1000);
                     await sleep(e.seconds * 1000);
                 } else {
                 } else {
+                    state.finished.resolve();
+
                     throw e;
                     throw e;
                 }
                 }
             } else if (
             } else if (
@@ -79,13 +85,31 @@ export async function invoke<R extends Api.AnyRequest>(
                     e instanceof errors.PhoneMigrateError ||
                     e instanceof errors.PhoneMigrateError ||
                     e instanceof errors.NetworkMigrateError;
                     e instanceof errors.NetworkMigrateError;
                 if (shouldRaise && (await client.isUserAuthorized())) {
                 if (shouldRaise && (await client.isUserAuthorized())) {
+                    state.finished.resolve();
+
                     throw e;
                     throw e;
                 }
                 }
                 await client._switchDC(e.newDc);
                 await client._switchDC(e.newDc);
+                sender =
+                    dcId === undefined
+                        ? client._sender
+                        : await client.getSender(dcId);
+            } else if (e instanceof errors.MsgWaitError) {
+                // We need to resend this after the old one was confirmed.
+                await state.isReady();
+
+                state.after = undefined;
+            } else if (e.message === "CONNECTION_NOT_INITED") {
+                await client.disconnect();
+                await sleep(2000);
+                await client.connect();
             } else {
             } else {
+                state.finished.resolve();
+
                 throw e;
                 throw e;
             }
             }
         }
         }
+        state.resetPromise();
     }
     }
     throw new Error(`Request was unsuccessful ${attempt} time(s)`);
     throw new Error(`Request was unsuccessful ${attempt} time(s)`);
 }
 }

+ 2 - 2
package-lock.json

@@ -1,12 +1,12 @@
 {
 {
   "name": "telegram",
   "name": "telegram",
-  "version": "2.17.8",
+  "version": "2.18.17",
   "lockfileVersion": 2,
   "lockfileVersion": 2,
   "requires": true,
   "requires": true,
   "packages": {
   "packages": {
     "": {
     "": {
       "name": "telegram",
       "name": "telegram",
-      "version": "2.17.8",
+      "version": "2.18.17",
       "license": "MIT",
       "license": "MIT",
       "dependencies": {
       "dependencies": {
         "@cryptography/aes": "^0.1.1",
         "@cryptography/aes": "^0.1.1",

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
 {
   "name": "telegram",
   "name": "telegram",
-  "version": "2.17.8",
+  "version": "2.18.20",
   "description": "NodeJS/Browser MTProto API Telegram client library,",
   "description": "NodeJS/Browser MTProto API Telegram client library,",
   "main": "index.js",
   "main": "index.js",
   "types": "index.d.ts",
   "types": "index.d.ts",

+ 0 - 5
publish_npm.js

@@ -152,11 +152,6 @@ npmi.on("close", (code) => {
       JSON.stringify(packageJSON, null, "  "),
       JSON.stringify(packageJSON, null, "  "),
       "utf8"
       "utf8"
     );
     );
-    fs.writeFileSync(
-      "gramjs/Version.ts",
-      `export const version = "${packageJSON.version}";`,
-      "utf8"
-    );
 
 
     const npmi = exec("npm i");
     const npmi = exec("npm i");
     npmi.on("close", (code) => {
     npmi.on("close", (code) => {