Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/use-multi-file-auth-state.js
1129 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.useMultiFileAuthState = void 0;3const promises_1 = require("fs/promises");4const path_1 = require("path");5const WAProto_1 = require("../../WAProto");6const auth_utils_1 = require("./auth-utils");7const generics_1 = require("./generics");8/**9* stores the full authentication state in a single folder.10* Far more efficient than singlefileauthstate11*12* Again, I wouldn't endorse this for any production level use other than perhaps a bot.13* Would recommend writing an auth state for use with a proper SQL or No-SQL DB14* */15const useMultiFileAuthState = async (folder) => {16const writeData = (data, file) => {17return (0, promises_1.writeFile)((0, path_1.join)(folder, fixFileName(file)), JSON.stringify(data, generics_1.BufferJSON.replacer));18};19const readData = async (file) => {20try {21const data = await (0, promises_1.readFile)((0, path_1.join)(folder, fixFileName(file)), { encoding: 'utf-8' });22return JSON.parse(data, generics_1.BufferJSON.reviver);23}24catch (error) {25return null;26}27};28const removeData = async (file) => {29try {30await (0, promises_1.unlink)(fixFileName(file));31}32catch (_a) {33}34};35const folderInfo = await (0, promises_1.stat)(folder).catch(() => { });36if (folderInfo) {37if (!folderInfo.isDirectory()) {38throw new Error(`found something that is not a directory at ${folder}, either delete it or specify a different location`);39}40}41else {42await (0, promises_1.mkdir)(folder, { recursive: true });43}44const fixFileName = (file) => { var _a; return (_a = file === null || file === void 0 ? void 0 : file.replace(/\//g, '__')) === null || _a === void 0 ? void 0 : _a.replace(/:/g, '-'); };45const creds = await readData('creds.json') || (0, auth_utils_1.initAuthCreds)();46return {47state: {48creds,49keys: {50get: async (type, ids) => {51const data = {};52await Promise.all(ids.map(async (id) => {53let value = await readData(`${type}-${id}.json`);54if (type === 'app-state-sync-key' && value) {55value = WAProto_1.proto.Message.AppStateSyncKeyData.fromObject(value);56}57data[id] = value;58}));59return data;60},61set: async (data) => {62const tasks = [];63for (const category in data) {64for (const id in data[category]) {65const value = data[category][id];66const file = `${category}-${id}.json`;67tasks.push(value ? writeData(value, file) : removeData(file));68}69}70await Promise.all(tasks);71}72}73},74saveCreds: () => {75return writeData(creds, 'creds.json');76}77};78};79exports.useMultiFileAuthState = useMultiFileAuthState;808182