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) {
|
async storeRemoteSession(options) {
|
||||||
/* Compress & Store Session */
|
|
||||||
const pathExists = await this.isValidPath(this.userDataDir);
|
const pathExists = await this.isValidPath(this.userDataDir);
|
||||||
if (pathExists) {
|
if (!pathExists) return;
|
||||||
await this.compressSession();
|
|
||||||
|
let compressedSessionPath;
|
||||||
|
try {
|
||||||
|
compressedSessionPath = await this.compressSession();
|
||||||
await this.store.save({ session: path.join(this.dataPath, this.sessionName) });
|
await this.store.save({ session: path.join(this.dataPath, this.sessionName) });
|
||||||
await fs.promises.unlink(path.join(this.dataPath, `${this.sessionName}.zip`));
|
if(options && options.emit) this.client.emit(Events.REMOTE_SESSION_SAVED);
|
||||||
await fs.promises.rm(`${this.tempDir}`, {
|
} finally {
|
||||||
|
const paths = [
|
||||||
|
this.tempDir,
|
||||||
|
...(compressedSessionPath ? [compressedSessionPath] : [])
|
||||||
|
];
|
||||||
|
await Promise.allSettled(
|
||||||
|
paths.map((p) =>
|
||||||
|
fs.promises.rm(p, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
force: true,
|
force: true,
|
||||||
maxRetries: this.rmMaxRetries,
|
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() {
|
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 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 new Promise((resolve, reject) => {
|
||||||
await this.deleteMetadata();
|
out.once('close', resolve);
|
||||||
return new Promise((resolve, reject) => {
|
out.once('error', reject);
|
||||||
archive
|
archive.once('error', reject);
|
||||||
.directory(this.tempDir, false)
|
|
||||||
.on('error', err => reject(err))
|
|
||||||
.pipe(stream);
|
|
||||||
|
|
||||||
stream.on('close', () => resolve());
|
archive.pipe(out);
|
||||||
|
archive.directory(this.tempDir, false);
|
||||||
archive.finalize();
|
archive.finalize();
|
||||||
});
|
});
|
||||||
|
return outPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
async unCompressSession(compressedSessionPath) {
|
async unCompressSession(compressedSessionPath) {
|
||||||
|
|
@ -171,25 +187,16 @@ class RemoteAuth extends BaseAuthStrategy {
|
||||||
await fs.promises.unlink(compressedSessionPath);
|
await fs.promises.unlink(compressedSessionPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteMetadata() {
|
async copyByRequiredDirs(from, to) {
|
||||||
const sessionDirs = [this.tempDir, path.join(this.tempDir, 'Default')];
|
for (const d of this.requiredDirs) {
|
||||||
for (const dir of sessionDirs) {
|
const src = path.join(from, d);
|
||||||
const sessionFiles = await fs.promises.readdir(dir);
|
if (await this.isValidPath(src)) {
|
||||||
for (const element of sessionFiles) {
|
const dest = path.join(to, path.basename(src));
|
||||||
if (!this.requiredDirs.includes(element)) {
|
await fs.promises.cp(src, dest, {
|
||||||
const dirElement = path.join(dir, element);
|
|
||||||
const stats = await fs.promises.lstat(dirElement);
|
|
||||||
|
|
||||||
if (stats.isDirectory()) {
|
|
||||||
await fs.promises.rm(dirElement, {
|
|
||||||
recursive: true,
|
recursive: true,
|
||||||
force: true,
|
force: true,
|
||||||
maxRetries: this.rmMaxRetries,
|
errorOnExist: false
|
||||||
}).catch(() => {});
|
});
|
||||||
} else {
|
|
||||||
await fs.promises.unlink(dirElement).catch(() => {});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue