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

Code before:
        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);
        }
    }

    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});

Review comment (bug):
Deleting the old session from the remote store worries me a little, since something could go wrong when storing the latest session and we would be left without a backup. 

Could we delete it after we know the latest has been successfully persisted?

Code after:
            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) {
        /* 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);
        }
    }