UNPKG

wwebjs-firebase-storage

Version:

A remote authentication plugin for whatsapp-web.js, using Firebase Storage to securely store WhatsApp multi-device session data.

63 lines (62 loc) 2.38 kB
import { deleteObject, getBytes, getMetadata, ref, uploadBytes, } from 'firebase/storage'; import { createWriteStream, openAsBlob } from 'node:fs'; export { initializeApp } from 'firebase/app'; export { getStorage } from 'firebase/storage'; /** * @class FirebaseStorageStore * @param {Object} options - Options for the store * @param {FirebaseStorage} options.firebaseStorage - A Firebase Storage instance * @param {string} options.sessionPath - Path inside the storage bucket to store the session files * @returns {FirebaseStorageStore} * @example * import { Client, RemoteAuth } from 'whatsapp-web.js'; * const client = new Client({ * authStrategy: new RemoteAuth({ * store: new FirebaseStorageStore({ * firebaseStorage: getStorage(app), * sessionPath: 'sessions-whatsapp-web.js', // save in a sub-directory * }), * backupSyncIntervalMs: 600000, // 10 minutes * }), * ... // other options * }); */ export class FirebaseStorageStore { fbStorage; sessionPath; constructor({ firebaseStorage, sessionPath, }) { this.fbStorage = firebaseStorage; this.sessionPath = sessionPath; } async sessionExists(options) { try { await getMetadata(ref(this.fbStorage, this.sessionPath ? `${this.sessionPath.replace(/\\/g, '/')}/${options.session}.zip` : `${options.session}.zip`)); return true; } catch (error) { return false; } } async save(options) { await uploadBytes(ref(this.fbStorage, this.sessionPath ? `${this.sessionPath.replace(/\\/g, '/')}/${options.session}.zip` : `${options.session}.zip`), await openAsBlob(`${options.session}.zip`), { contentType: 'application/zip', }); } async extract(options) { createWriteStream(options.path, { autoClose: true, encoding: 'binary', }).write(Buffer.from(await getBytes(ref(this.fbStorage, this.sessionPath ? `${this.sessionPath.replace(/\\/g, '/')}/${options.session}.zip` : `${options.session}.zip`)))); } async delete(options) { return deleteObject(ref(this.fbStorage, this.sessionPath ? `${this.sessionPath.replace(/\\/g, '/')}/${options.session}.zip` : `${options.session}.zip`)); } }