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

Code before:

        await this.extractRemoteSession();

        this.client.options.puppeteer = {
            ...puppeteerOpts,
            userDataDir: dirPath
        };
    }

    async logout() {
        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 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);
        }
    }

Review comment (security):
Does this mean that if you close the client before this initial delay, you will start back up with a fresh new session and have to authenticate again? Is there something other than a fixed delay that we can use to know if it's "restorable" (e.g. some file present or something set on the client)?

Code after:
        const dirPath = path.join(this.dataPath, sessionDirName);

        if (puppeteerOpts.userDataDir && puppeteerOpts.userDataDir !== dirPath) {
            throw new Error('RemoteAuth is not compatible with a user-supplied userDataDir.');
        }

        this.userDataDir = dirPath;
        this.sessionName = sessionDirName;

        await this.extractRemoteSession();

        this.client.options.puppeteer = {
            ...puppeteerOpts,
            userDataDir: dirPath
        };
    }

    async logout() {
        await this.disconnect();
    }

    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) {