copy only requiredDirs and make storeRemoteSession safer (#3744)
* copy only requiredDirs and make storeRemoteSession safer * replace fs-extra copy with native fs cp function to avoid unhandled exceptions bug in fs-extra * add recursive option to cp for folders copy * fix: correct indentation in storeRemoteSession method Fixed ESLint indentation errors (lines 105-127) by changing from 2-space to 4-space indentation to match project standards. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Fix formatting of session save object in RemoteAuth --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
42d0c40e2e
commit
98cdced13f
1 changed files with 46 additions and 39 deletions
|
|
@ -102,18 +102,28 @@ class RemoteAuth extends BaseAuthStrategy {
|
|||
}
|
||||
|
||||
async storeRemoteSession(options) {
|
||||
/* Compress & Store Session */
|
||||
const pathExists = await this.isValidPath(this.userDataDir);
|
||||
if (pathExists) {
|
||||
await this.compressSession();
|
||||
if (!pathExists) return;
|
||||
|
||||
let compressedSessionPath;
|
||||
try {
|
||||
compressedSessionPath = await this.compressSession();
|
||||
await this.store.save({ session: path.join(this.dataPath, this.sessionName) });
|
||||
await fs.promises.unlink(path.join(this.dataPath, `${this.sessionName}.zip`));
|
||||
await fs.promises.rm(`${this.tempDir}`, {
|
||||
if(options && options.emit) this.client.emit(Events.REMOTE_SESSION_SAVED);
|
||||
} finally {
|
||||
const paths = [
|
||||
this.tempDir,
|
||||
...(compressedSessionPath ? [compressedSessionPath] : [])
|
||||
];
|
||||
await Promise.allSettled(
|
||||
paths.map((p) =>
|
||||
fs.promises.rm(p, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: this.rmMaxRetries,
|
||||
}).catch(() => {});
|
||||
if(options && options.emit) this.client.emit(Events.REMOTE_SESSION_SAVED);
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -142,20 +152,26 @@ class RemoteAuth extends BaseAuthStrategy {
|
|||
}
|
||||
|
||||
async compressSession() {
|
||||
const stageDefaultPath = path.join(this.tempDir, 'Default');
|
||||
const userDataDefaultPath = path.join(this.userDataDir, 'Default');
|
||||
|
||||
await fs.emptyDir(stageDefaultPath);
|
||||
await this.copyByRequiredDirs(userDataDefaultPath, stageDefaultPath);
|
||||
|
||||
const archive = archiver('zip');
|
||||
const stream = fs.createWriteStream(path.join(this.dataPath, `${this.sessionName}.zip`));
|
||||
const outPath = path.join(this.dataPath, `${this.sessionName}.zip`);
|
||||
const out = fs.createWriteStream(outPath);
|
||||
|
||||
await fs.copy(this.userDataDir, this.tempDir).catch(() => {});
|
||||
await this.deleteMetadata();
|
||||
return new Promise((resolve, reject) => {
|
||||
archive
|
||||
.directory(this.tempDir, false)
|
||||
.on('error', err => reject(err))
|
||||
.pipe(stream);
|
||||
await new Promise((resolve, reject) => {
|
||||
out.once('close', resolve);
|
||||
out.once('error', reject);
|
||||
archive.once('error', reject);
|
||||
|
||||
stream.on('close', () => resolve());
|
||||
archive.pipe(out);
|
||||
archive.directory(this.tempDir, false);
|
||||
archive.finalize();
|
||||
});
|
||||
return outPath;
|
||||
}
|
||||
|
||||
async unCompressSession(compressedSessionPath) {
|
||||
|
|
@ -171,25 +187,16 @@ class RemoteAuth extends BaseAuthStrategy {
|
|||
await fs.promises.unlink(compressedSessionPath);
|
||||
}
|
||||
|
||||
async deleteMetadata() {
|
||||
const sessionDirs = [this.tempDir, path.join(this.tempDir, 'Default')];
|
||||
for (const dir of sessionDirs) {
|
||||
const sessionFiles = await fs.promises.readdir(dir);
|
||||
for (const element of sessionFiles) {
|
||||
if (!this.requiredDirs.includes(element)) {
|
||||
const dirElement = path.join(dir, element);
|
||||
const stats = await fs.promises.lstat(dirElement);
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
await fs.promises.rm(dirElement, {
|
||||
async copyByRequiredDirs(from, to) {
|
||||
for (const d of this.requiredDirs) {
|
||||
const src = path.join(from, d);
|
||||
if (await this.isValidPath(src)) {
|
||||
const dest = path.join(to, path.basename(src));
|
||||
await fs.promises.cp(src, dest, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: this.rmMaxRetries,
|
||||
}).catch(() => {});
|
||||
} else {
|
||||
await fs.promises.unlink(dirElement).catch(() => {});
|
||||
}
|
||||
}
|
||||
errorOnExist: false
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue