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

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

    async deleteRemoteSession() {
        try {

Review comment (suggestion):
Users may want to handle these errors differently than simply logging. I would suggest either re-throwing or wrapping them in a new error type.

Code after:
    }

    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) {
            await fs.promises.rm(this.userDataDir, {
                recursive: true,
                force: true
            }).catch(() => {});