GramJs: Re-connect on CONNECTION_NOT_INITED error

This commit is contained in:
Alexander Zinchuk 2023-02-04 03:02:44 +01:00
parent e4c30ea969
commit 07b8e2ba6a
2 changed files with 29 additions and 4 deletions

View File

@ -22,7 +22,10 @@ const {
} = require('./auth');
const { downloadFile } = require('./downloadFile');
const { uploadFile } = require('./uploadFile');
const { updateTwoFaSettings, getTmpPassword } = require('./2fa');
const {
updateTwoFaSettings,
getTmpPassword,
} = require('./2fa');
const DEFAULT_DC_ID = 2;
const WEBDOCUMENT_DC_ID = 4;
@ -372,6 +375,24 @@ class TelegramClient {
await sender.disconnect();
}
async _cleanupExportedSenders(dcId) {
const promises = Object.values(this._exportedSenderPromises[dcId]);
if (!promises.length) {
return;
}
if (this.session.dcId !== dcId) {
this.session.setAuthKey(undefined, dcId);
}
this._exportedSenderPromises[dcId] = {};
await Promise.all(promises.map(async (promise) => {
const sender = await promise;
await sender.disconnect();
}));
}
async _connectSender(sender, dcId, isPremium = false) {
// if we don't already have an auth key we want to use normal DCs not -1
let hasAuthKey = Boolean(sender.authKey.getKey());

View File

@ -101,10 +101,14 @@ export async function downloadFile(
try {
return await downloadFile2(client, inputLocation, fileParams);
} catch (err: any) {
if (i === SENDER_RETRIES - 1 || !err.message.startsWith('SESSION_REVOKED')) {
if (
(err.message.startsWith('SESSION_REVOKED') || err.message.startsWith('CONNECTION_NOT_INITED'))
&& i < SENDER_RETRIES - 1
) {
await client._cleanupExportedSenders(dcId);
} else {
throw err;
}
await client._cleanupExportedSender(dcId);
}
}
@ -215,7 +219,7 @@ async function downloadFile2(
precise: isPrecise || undefined,
})),
sleep(SENDER_TIMEOUT).then(() => {
// if we're on the main DC we just cancel the download and let the user retry later.
// If we're on the main DC we just cancel the download and let the user retry later
if (dcId === client.session.dcId) {
return Promise.reject(new Error('USER_CANCELED'));
} else {