Skip to content

Commit 882ef52

Browse files
matheusmartinsInspermatheusmartinsInsper
authored andcommitted
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
1 parent d6131d7 commit 882ef52

File tree

3 files changed

+78
-61
lines changed

3 files changed

+78
-61
lines changed

package-lock.json

Lines changed: 41 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@aws-sdk/client-sqs": "^3.723.0",
5454
"@hapi/boom": "^10.0.1",
5555
"@paralleldrive/cuid2": "^2.2.2",
56-
"@prisma/client": "^6.1.0",
56+
"@prisma/client": "^5.22.0",
5757
"@sentry/node": "^8.47.0",
5858
"amqplib": "^0.10.5",
5959
"axios": "^1.7.9",
@@ -81,7 +81,7 @@
8181
"node-cache": "^5.1.2",
8282
"node-cron": "^3.0.3",
8383
"pino": "^8.11.0",
84-
"prisma": "^6.1.0",
84+
"prisma": "^5.22.0",
8585
"pusher": "^5.2.0",
8686
"qrcode": "^1.5.4",
8787
"qrcode-terminal": "^0.12.0",

src/utils/use-multi-file-auth-state-prisma.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from
55
import fs from 'fs/promises';
66
import path from 'path';
77

8-
// const fixFileName = (file: string): string | undefined => {
9-
// if (!file) {
10-
// return undefined;
11-
// }
12-
// const replacedSlash = file.replace(/\//g, '__');
13-
// const replacedColon = replacedSlash.replace(/:/g, '-');
14-
// return replacedColon;
15-
// };
8+
const fixFileName = (file: string): string | undefined => {
9+
if (!file) {
10+
return undefined;
11+
}
12+
const replacedSlash = file.replace(/\//g, '__');
13+
const replacedColon = replacedSlash.replace(/:/g, '-');
14+
return replacedColon;
15+
};
1616

1717
export async function keyExists(sessionId: string): Promise<any> {
1818
try {
@@ -63,14 +63,14 @@ async function deleteAuthKey(sessionId: string): Promise<any> {
6363
}
6464
}
6565

66-
// async function fileExists(file: string): Promise<any> {
67-
// try {
68-
// const stat = await fs.stat(file);
69-
// if (stat.isFile()) return true;
70-
// } catch (error) {
71-
// return;
72-
// }
73-
// }
66+
async function fileExists(file: string): Promise<any> {
67+
try {
68+
const stat = await fs.stat(file);
69+
if (stat.isFile()) return true;
70+
} catch (error) {
71+
return;
72+
}
73+
}
7474

7575
export default async function useMultiFileAuthStatePrisma(
7676
sessionId: string,
@@ -80,16 +80,19 @@ export default async function useMultiFileAuthStatePrisma(
8080
saveCreds: () => Promise<void>;
8181
}> {
8282
const localFolder = path.join(INSTANCE_DIR, sessionId);
83-
// const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
83+
const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
8484
await fs.mkdir(localFolder, { recursive: true });
8585

8686
async function writeData(data: any, key: string): Promise<any> {
8787
const dataString = JSON.stringify(data, BufferJSON.replacer);
8888

8989
if (key != 'creds') {
90-
return await cache.hSet(sessionId, key, data);
91-
// await fs.writeFile(localFile(key), dataString);
92-
// return;
90+
if (process.env.CACHE_REDIS_ENABLED === 'true') {
91+
return await cache.hSet(sessionId, key, data);
92+
} else {
93+
await fs.writeFile(localFile(key), dataString);
94+
return;
95+
}
9396
}
9497
await saveKey(sessionId, dataString);
9598
return;
@@ -100,9 +103,13 @@ export default async function useMultiFileAuthStatePrisma(
100103
let rawData;
101104

102105
if (key != 'creds') {
103-
return await cache.hGet(sessionId, key);
104-
// if (!(await fileExists(localFile(key)))) return null;
105-
// rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' });
106+
if (process.env.CACHE_REDIS_ENABLED === 'true') {
107+
return await cache.hGet(sessionId, key);
108+
} else {
109+
if (!(await fileExists(localFile(key)))) return null;
110+
rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' });
111+
return JSON.parse(rawData, BufferJSON.reviver);
112+
}
106113
} else {
107114
rawData = await getAuthKey(sessionId);
108115
}
@@ -117,8 +124,11 @@ export default async function useMultiFileAuthStatePrisma(
117124
async function removeData(key: string): Promise<any> {
118125
try {
119126
if (key != 'creds') {
120-
return await cache.hDelete(sessionId, key);
121-
// await fs.unlink(localFile(key));
127+
if (process.env.CACHE_REDIS_ENABLED === 'true') {
128+
return await cache.hDelete(sessionId, key);
129+
} else {
130+
await fs.unlink(localFile(key));
131+
}
122132
} else {
123133
await deleteAuthKey(sessionId);
124134
}

0 commit comments

Comments
 (0)