Add max retries for fs.promises.rm (#3351)
This commit is contained in:
parent
5df83b030d
commit
f56d25f1c4
3 changed files with 19 additions and 9 deletions
6
index.d.ts
vendored
6
index.d.ts
vendored
|
|
@ -538,7 +538,8 @@ declare namespace WAWebJS {
|
||||||
public dataPath?: string;
|
public dataPath?: string;
|
||||||
constructor(options?: {
|
constructor(options?: {
|
||||||
clientId?: string,
|
clientId?: string,
|
||||||
dataPath?: string
|
dataPath?: string,
|
||||||
|
rmMaxRetries?: number
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -552,7 +553,8 @@ declare namespace WAWebJS {
|
||||||
store: Store,
|
store: Store,
|
||||||
clientId?: string,
|
clientId?: string,
|
||||||
dataPath?: string,
|
dataPath?: string,
|
||||||
backupSyncIntervalMs: number
|
backupSyncIntervalMs: number,
|
||||||
|
rmMaxRetries?: number
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,10 @@ const BaseAuthStrategy = require('./BaseAuthStrategy');
|
||||||
* @param {object} options - options
|
* @param {object} options - options
|
||||||
* @param {string} options.clientId - Client id to distinguish instances if you are using multiple, otherwise keep null if you are using only one instance
|
* @param {string} options.clientId - Client id to distinguish instances if you are using multiple, otherwise keep null if you are using only one instance
|
||||||
* @param {string} options.dataPath - Change the default path for saving session files, default is: "./.wwebjs_auth/"
|
* @param {string} options.dataPath - Change the default path for saving session files, default is: "./.wwebjs_auth/"
|
||||||
|
* @param {number} options.rmMaxRetries - Sets the maximum number of retries for removing the session directory
|
||||||
*/
|
*/
|
||||||
class LocalAuth extends BaseAuthStrategy {
|
class LocalAuth extends BaseAuthStrategy {
|
||||||
constructor({ clientId, dataPath }={}) {
|
constructor({ clientId, dataPath, rmMaxRetries }={}) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
const idRegex = /^[-_\w]+$/i;
|
const idRegex = /^[-_\w]+$/i;
|
||||||
|
|
@ -21,6 +22,7 @@ class LocalAuth extends BaseAuthStrategy {
|
||||||
|
|
||||||
this.dataPath = path.resolve(dataPath || './.wwebjs_auth/');
|
this.dataPath = path.resolve(dataPath || './.wwebjs_auth/');
|
||||||
this.clientId = clientId;
|
this.clientId = clientId;
|
||||||
|
this.rmMaxRetries = rmMaxRetries ?? 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
async beforeBrowserInitialized() {
|
async beforeBrowserInitialized() {
|
||||||
|
|
@ -44,7 +46,7 @@ class LocalAuth extends BaseAuthStrategy {
|
||||||
|
|
||||||
async logout() {
|
async logout() {
|
||||||
if (this.userDataDir) {
|
if (this.userDataDir) {
|
||||||
await fs.promises.rm(this.userDataDir, { recursive: true, force: true })
|
await fs.promises.rm(this.userDataDir, { recursive: true, force: true, maxRetries: this.rmMaxRetries })
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
throw new Error(e);
|
throw new Error(e);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,10 @@ const BaseAuthStrategy = require('./BaseAuthStrategy');
|
||||||
* @param {string} options.clientId - Client id to distinguish instances if you are using multiple, otherwise keep null if you are using only one instance
|
* @param {string} options.clientId - Client id to distinguish instances if you are using multiple, otherwise keep null if you are using only one instance
|
||||||
* @param {string} options.dataPath - Change the default path for saving session files, default is: "./.wwebjs_auth/"
|
* @param {string} options.dataPath - Change the default path for saving session files, default is: "./.wwebjs_auth/"
|
||||||
* @param {number} options.backupSyncIntervalMs - Sets the time interval for periodic session backups. Accepts values starting from 60000ms {1 minute}
|
* @param {number} options.backupSyncIntervalMs - Sets the time interval for periodic session backups. Accepts values starting from 60000ms {1 minute}
|
||||||
|
* @param {number} options.rmMaxRetries - Sets the maximum number of retries for removing the session directory
|
||||||
*/
|
*/
|
||||||
class RemoteAuth extends BaseAuthStrategy {
|
class RemoteAuth extends BaseAuthStrategy {
|
||||||
constructor({ clientId, dataPath, store, backupSyncIntervalMs } = {}) {
|
constructor({ clientId, dataPath, store, backupSyncIntervalMs, rmMaxRetries } = {}) {
|
||||||
if (!fs && !unzipper && !archiver) throw new Error('Optional Dependencies [fs-extra, unzipper, archiver] are required to use RemoteAuth. Make sure to run npm install correctly and remove the --no-optional flag');
|
if (!fs && !unzipper && !archiver) throw new Error('Optional Dependencies [fs-extra, unzipper, archiver] are required to use RemoteAuth. Make sure to run npm install correctly and remove the --no-optional flag');
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
|
@ -43,6 +44,7 @@ class RemoteAuth extends BaseAuthStrategy {
|
||||||
this.dataPath = path.resolve(dataPath || './.wwebjs_auth/');
|
this.dataPath = path.resolve(dataPath || './.wwebjs_auth/');
|
||||||
this.tempDir = `${this.dataPath}/wwebjs_temp_session_${this.clientId}`;
|
this.tempDir = `${this.dataPath}/wwebjs_temp_session_${this.clientId}`;
|
||||||
this.requiredDirs = ['Default', 'IndexedDB', 'Local Storage']; /* => Required Files & Dirs in WWebJS to restore session */
|
this.requiredDirs = ['Default', 'IndexedDB', 'Local Storage']; /* => Required Files & Dirs in WWebJS to restore session */
|
||||||
|
this.rmMaxRetries = rmMaxRetries ?? 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
async beforeBrowserInitialized() {
|
async beforeBrowserInitialized() {
|
||||||
|
|
@ -80,7 +82,8 @@ class RemoteAuth extends BaseAuthStrategy {
|
||||||
if (pathExists) {
|
if (pathExists) {
|
||||||
await fs.promises.rm(this.userDataDir, {
|
await fs.promises.rm(this.userDataDir, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
force: true
|
force: true,
|
||||||
|
maxRetries: this.rmMaxRetries,
|
||||||
}).catch(() => {});
|
}).catch(() => {});
|
||||||
}
|
}
|
||||||
clearInterval(this.backupSync);
|
clearInterval(this.backupSync);
|
||||||
|
|
@ -107,7 +110,8 @@ class RemoteAuth extends BaseAuthStrategy {
|
||||||
await fs.promises.unlink(`${this.sessionName}.zip`);
|
await fs.promises.unlink(`${this.sessionName}.zip`);
|
||||||
await fs.promises.rm(`${this.tempDir}`, {
|
await fs.promises.rm(`${this.tempDir}`, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
force: true
|
force: true,
|
||||||
|
maxRetries: this.rmMaxRetries,
|
||||||
}).catch(() => {});
|
}).catch(() => {});
|
||||||
if(options && options.emit) this.client.emit(Events.REMOTE_SESSION_SAVED);
|
if(options && options.emit) this.client.emit(Events.REMOTE_SESSION_SAVED);
|
||||||
}
|
}
|
||||||
|
|
@ -120,7 +124,8 @@ class RemoteAuth extends BaseAuthStrategy {
|
||||||
if (pathExists) {
|
if (pathExists) {
|
||||||
await fs.promises.rm(this.userDataDir, {
|
await fs.promises.rm(this.userDataDir, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
force: true
|
force: true,
|
||||||
|
maxRetries: this.rmMaxRetries,
|
||||||
}).catch(() => {});
|
}).catch(() => {});
|
||||||
}
|
}
|
||||||
if (sessionExists) {
|
if (sessionExists) {
|
||||||
|
|
@ -177,7 +182,8 @@ class RemoteAuth extends BaseAuthStrategy {
|
||||||
if (stats.isDirectory()) {
|
if (stats.isDirectory()) {
|
||||||
await fs.promises.rm(dirElement, {
|
await fs.promises.rm(dirElement, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
force: true
|
force: true,
|
||||||
|
maxRetries: this.rmMaxRetries,
|
||||||
}).catch(() => {});
|
}).catch(() => {});
|
||||||
} else {
|
} else {
|
||||||
await fs.promises.unlink(dirElement).catch(() => {});
|
await fs.promises.unlink(dirElement).catch(() => {});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue