Repository: pedroslopez/whatsapp-web.js
File: src/authStrategies/RemoteAuth.js (JavaScript)

Code before:
        clearInterval(this.backupSync);
    }

    async authReady() {
        const sessionExists = await this.store.sessionExists({session: this.sessionName});
        if(!sessionExists) {
            await this.delay(30000); /* Initial delay sync required for session to be stable enough to recover */
            await this.storeRemoteSession();
        }
        var self = this;
        this.backupSync = setInterval(async function () {
            await self.storeRemoteSession();
        }, this.backupSyncMs);
    }

    async storeRemoteSession() {
        try {
            const sessionExists = await this.store.sessionExists({session: this.sessionName});
            if (sessionExists) {
                await this.store.delete({session: this.sessionName});
            }
            /* Compress & Store Session */
            const pathExists = await this.isValidPath(this.userDataDir);
            if (pathExists) {
                await this.compressSession();
                await this.store.save({session: this.sessionName});
                await fs.promises.unlink(`${this.sessionName}.zip`);
            }
        } catch (error) {
            console.log('RemoteAuth Error => storeRemoteSession: ', error);
        }
    }

    async extractRemoteSession() {
        try {
            const sessionExists = await this.store.sessionExists({session: this.sessionName});
            const pathExists = await this.isValidPath(this.userDataDir);
            if (pathExists) {
                await fs.promises.rm(this.userDataDir, {
                    recursive: true,
                    force: true
                });
            }
            if (sessionExists) {
                await this.store.extract({session: this.sessionName});
                await this.unCompressSession();
                await fs.promises.unlink(`RemoteAuth-${this.sessionName}.zip`);
            }
        } catch (error) {
            console.log('RemoteAuth Error => extractRemoteSession: ', error);
        }

Review comment (security):
If the interface between the authstrategy and the store will be a file, we should be more explicit about where that file is located. I would suggest passing a filepath directly instead of determining it implicitly from within the store

Code after:
    }

    async destroy() {
        clearInterval(this.backupSync);
    }

    async disconnect() {
        await this.deleteRemoteSession();

        let pathExists = await this.isValidPath(this.userDataDir);
        if (pathExists) {
            await fs.promises.rm(this.userDataDir, {
                recursive: true,
                force: true
            }).catch(() => {});
        }
        clearInterval(this.backupSync);
    }

    async afterAuthReady() {
        const sessionExists = await this.store.sessionExists({session: this.sessionName});
        if(!sessionExists) {
            await this.delay(60000); /* Initial delay sync required for session to be stable enough to recover */
            await this.storeRemoteSession({emit: true});
        }
        var self = this;
        this.backupSync = setInterval(async function () {
            await self.storeRemoteSession();
        }, this.backupSyncIntervalMs);
    }

    async storeRemoteSession(options) {
        /* Compress & Store Session */
        const pathExists = await this.isValidPath(this.userDataDir);
        if (pathExists) {
            await this.compressSession();
            await this.store.save({session: this.sessionName});
            await fs.promises.unlink(`${this.sessionName}.zip`);
            await fs.promises.rm(`${this.tempDir}`, {
                recursive: true,
                force: true
            }).catch(() => {});
            if(options && options.emit) this.client.emit(Events.REMOTE_SESSION_SAVED);
        }
    }

    async extractRemoteSession() {
        const pathExists = await this.isValidPath(this.userDataDir);
        const compressedSessionPath = `${this.sessionName}.zip`;
        const sessionExists = await this.store.sessionExists({session: this.sessionName});
        if (pathExists) {