From bfa0a90a124ee0c88f9f1b3a50e78f36423bc54a Mon Sep 17 00:00:00 2001 From: Skippy <82155626+skippyyy@users.noreply.github.com> Date: Sun, 25 Dec 2022 11:07:35 +0200 Subject: [PATCH] Fix double call to refreshSession() on 403 --- src/chatgpt-api-browser.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/chatgpt-api-browser.ts b/src/chatgpt-api-browser.ts index aae85cf..55590a1 100644 --- a/src/chatgpt-api-browser.ts +++ b/src/chatgpt-api-browser.ts @@ -32,6 +32,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { protected _browser: Browser protected _page: Page protected _proxyServer: string + protected _isRefreshing: boolean /** * Creates a new client for automating the ChatGPT webapp. @@ -95,6 +96,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { this._nopechaKey = nopechaKey this._executablePath = executablePath this._proxyServer = proxyServer + this._isRefreshing = false if (!this._email) { const error = new types.ChatGPTError('ChatGPT invalid email') @@ -280,7 +282,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { } if (url.endsWith('/conversation')) { - if (status === 403) { + if (status === 403 && !this._isRefreshing) { await this.refreshSession() } } else if (url.endsWith('api/auth/session')) { @@ -327,6 +329,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { * Attempts to handle 403 errors by refreshing the page. */ async refreshSession() { + this._isRefreshing = true console.log(`ChatGPT "${this._email}" session expired (403); refreshing...`) try { if (!this._minimize) { @@ -372,6 +375,8 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI { `ChatGPT "${this._email}" error refreshing session`, err.toString() ) + } finally { + this._isRefreshing = false } }