feat: Add configurable file/cache storage for authentication state

- Update Baileys package to version 6.7.10
- Implement conditional storage mechanism for authentication state
- Add support for file-based or Redis cache storage based on environment configuration
- Re-enable previously commented out file handling utility functions
This commit is contained in:
Davidson Gomes 2025-01-31 17:38:42 -03:00
parent 169d2f797b
commit 79b1c6bb1c
2 changed files with 37 additions and 27 deletions

4
package-lock.json generated
View File

@ -4752,8 +4752,8 @@
"integrity": "sha512-OnAYlL5b7LEkALw87fUVafQw5rVR9RjwGd4KUwNQ6DrrNmaVaUCgLipfVlzrPQ4tWOR9P0IXGNOx50jYCCdSJg==" "integrity": "sha512-OnAYlL5b7LEkALw87fUVafQw5rVR9RjwGd4KUwNQ6DrrNmaVaUCgLipfVlzrPQ4tWOR9P0IXGNOx50jYCCdSJg=="
}, },
"node_modules/baileys": { "node_modules/baileys": {
"version": "6.7.9", "version": "6.7.10",
"resolved": "git+ssh://git@github.com/EvolutionAPI/Baileys.git#2140e16a9214067bf4cd408e88c231c24636a9e9", "resolved": "git+ssh://git@github.com/EvolutionAPI/Baileys.git#3d42781a00eb6d1b0f31f825a65b5e75708b9a89",
"dependencies": { "dependencies": {
"@adiwajshing/keyed-db": "^0.2.4", "@adiwajshing/keyed-db": "^0.2.4",
"@hapi/boom": "^9.1.3", "@hapi/boom": "^9.1.3",

View File

@ -5,14 +5,14 @@ import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from
import fs from 'fs/promises'; import fs from 'fs/promises';
import path from 'path'; import path from 'path';
// const fixFileName = (file: string): string | undefined => { const fixFileName = (file: string): string | undefined => {
// if (!file) { if (!file) {
// return undefined; return undefined;
// } }
// const replacedSlash = file.replace(/\//g, '__'); const replacedSlash = file.replace(/\//g, '__');
// const replacedColon = replacedSlash.replace(/:/g, '-'); const replacedColon = replacedSlash.replace(/:/g, '-');
// return replacedColon; return replacedColon;
// }; };
export async function keyExists(sessionId: string): Promise<any> { export async function keyExists(sessionId: string): Promise<any> {
try { try {
@ -63,14 +63,14 @@ async function deleteAuthKey(sessionId: string): Promise<any> {
} }
} }
// async function fileExists(file: string): Promise<any> { async function fileExists(file: string): Promise<any> {
// try { try {
// const stat = await fs.stat(file); const stat = await fs.stat(file);
// if (stat.isFile()) return true; if (stat.isFile()) return true;
// } catch (error) { } catch (error) {
// return; return;
// } }
// } }
export default async function useMultiFileAuthStatePrisma( export default async function useMultiFileAuthStatePrisma(
sessionId: string, sessionId: string,
@ -80,16 +80,19 @@ export default async function useMultiFileAuthStatePrisma(
saveCreds: () => Promise<void>; saveCreds: () => Promise<void>;
}> { }> {
const localFolder = path.join(INSTANCE_DIR, sessionId); const localFolder = path.join(INSTANCE_DIR, sessionId);
// const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json'); const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
await fs.mkdir(localFolder, { recursive: true }); await fs.mkdir(localFolder, { recursive: true });
async function writeData(data: any, key: string): Promise<any> { async function writeData(data: any, key: string): Promise<any> {
const dataString = JSON.stringify(data, BufferJSON.replacer); const dataString = JSON.stringify(data, BufferJSON.replacer);
if (key != 'creds') { if (key != 'creds') {
return await cache.hSet(sessionId, key, data); if (process.env.CACHE_REDIS_ENABLED === 'true') {
// await fs.writeFile(localFile(key), dataString); return await cache.hSet(sessionId, key, data);
// return; } else {
await fs.writeFile(localFile(key), dataString);
return;
}
} }
await saveKey(sessionId, dataString); await saveKey(sessionId, dataString);
return; return;
@ -100,9 +103,13 @@ export default async function useMultiFileAuthStatePrisma(
let rawData; let rawData;
if (key != 'creds') { if (key != 'creds') {
return await cache.hGet(sessionId, key); if (process.env.CACHE_REDIS_ENABLED === 'true') {
// if (!(await fileExists(localFile(key)))) return null; return await cache.hGet(sessionId, key);
// rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' }); } else {
if (!(await fileExists(localFile(key)))) return null;
rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' });
return JSON.parse(rawData, BufferJSON.reviver);
}
} else { } else {
rawData = await getAuthKey(sessionId); rawData = await getAuthKey(sessionId);
} }
@ -117,8 +124,11 @@ export default async function useMultiFileAuthStatePrisma(
async function removeData(key: string): Promise<any> { async function removeData(key: string): Promise<any> {
try { try {
if (key != 'creds') { if (key != 'creds') {
return await cache.hDelete(sessionId, key); if (process.env.CACHE_REDIS_ENABLED === 'true') {
// await fs.unlink(localFile(key)); return await cache.hDelete(sessionId, key);
} else {
await fs.unlink(localFile(key));
}
} else { } else {
await deleteAuthKey(sessionId); await deleteAuthKey(sessionId);
} }