mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-13 15:14:49 -06:00
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:
parent
169d2f797b
commit
79b1c6bb1c
4
package-lock.json
generated
4
package-lock.json
generated
@ -4752,8 +4752,8 @@
|
||||
"integrity": "sha512-OnAYlL5b7LEkALw87fUVafQw5rVR9RjwGd4KUwNQ6DrrNmaVaUCgLipfVlzrPQ4tWOR9P0IXGNOx50jYCCdSJg=="
|
||||
},
|
||||
"node_modules/baileys": {
|
||||
"version": "6.7.9",
|
||||
"resolved": "git+ssh://git@github.com/EvolutionAPI/Baileys.git#2140e16a9214067bf4cd408e88c231c24636a9e9",
|
||||
"version": "6.7.10",
|
||||
"resolved": "git+ssh://git@github.com/EvolutionAPI/Baileys.git#3d42781a00eb6d1b0f31f825a65b5e75708b9a89",
|
||||
"dependencies": {
|
||||
"@adiwajshing/keyed-db": "^0.2.4",
|
||||
"@hapi/boom": "^9.1.3",
|
||||
|
@ -5,14 +5,14 @@ import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
// const fixFileName = (file: string): string | undefined => {
|
||||
// if (!file) {
|
||||
// return undefined;
|
||||
// }
|
||||
// const replacedSlash = file.replace(/\//g, '__');
|
||||
// const replacedColon = replacedSlash.replace(/:/g, '-');
|
||||
// return replacedColon;
|
||||
// };
|
||||
const fixFileName = (file: string): string | undefined => {
|
||||
if (!file) {
|
||||
return undefined;
|
||||
}
|
||||
const replacedSlash = file.replace(/\//g, '__');
|
||||
const replacedColon = replacedSlash.replace(/:/g, '-');
|
||||
return replacedColon;
|
||||
};
|
||||
|
||||
export async function keyExists(sessionId: string): Promise<any> {
|
||||
try {
|
||||
@ -63,14 +63,14 @@ async function deleteAuthKey(sessionId: string): Promise<any> {
|
||||
}
|
||||
}
|
||||
|
||||
// async function fileExists(file: string): Promise<any> {
|
||||
// try {
|
||||
// const stat = await fs.stat(file);
|
||||
// if (stat.isFile()) return true;
|
||||
// } catch (error) {
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
async function fileExists(file: string): Promise<any> {
|
||||
try {
|
||||
const stat = await fs.stat(file);
|
||||
if (stat.isFile()) return true;
|
||||
} catch (error) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export default async function useMultiFileAuthStatePrisma(
|
||||
sessionId: string,
|
||||
@ -80,16 +80,19 @@ export default async function useMultiFileAuthStatePrisma(
|
||||
saveCreds: () => Promise<void>;
|
||||
}> {
|
||||
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 });
|
||||
|
||||
async function writeData(data: any, key: string): Promise<any> {
|
||||
const dataString = JSON.stringify(data, BufferJSON.replacer);
|
||||
|
||||
if (key != 'creds') {
|
||||
return await cache.hSet(sessionId, key, data);
|
||||
// await fs.writeFile(localFile(key), dataString);
|
||||
// return;
|
||||
if (process.env.CACHE_REDIS_ENABLED === 'true') {
|
||||
return await cache.hSet(sessionId, key, data);
|
||||
} else {
|
||||
await fs.writeFile(localFile(key), dataString);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await saveKey(sessionId, dataString);
|
||||
return;
|
||||
@ -100,9 +103,13 @@ export default async function useMultiFileAuthStatePrisma(
|
||||
let rawData;
|
||||
|
||||
if (key != 'creds') {
|
||||
return await cache.hGet(sessionId, key);
|
||||
// if (!(await fileExists(localFile(key)))) return null;
|
||||
// rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' });
|
||||
if (process.env.CACHE_REDIS_ENABLED === 'true') {
|
||||
return await cache.hGet(sessionId, key);
|
||||
} else {
|
||||
if (!(await fileExists(localFile(key)))) return null;
|
||||
rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' });
|
||||
return JSON.parse(rawData, BufferJSON.reviver);
|
||||
}
|
||||
} else {
|
||||
rawData = await getAuthKey(sessionId);
|
||||
}
|
||||
@ -117,8 +124,11 @@ export default async function useMultiFileAuthStatePrisma(
|
||||
async function removeData(key: string): Promise<any> {
|
||||
try {
|
||||
if (key != 'creds') {
|
||||
return await cache.hDelete(sessionId, key);
|
||||
// await fs.unlink(localFile(key));
|
||||
if (process.env.CACHE_REDIS_ENABLED === 'true') {
|
||||
return await cache.hDelete(sessionId, key);
|
||||
} else {
|
||||
await fs.unlink(localFile(key));
|
||||
}
|
||||
} else {
|
||||
await deleteAuthKey(sessionId);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user