Skip to content

Commit 79b1c6b

Browse files
committed
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 169d2f7 commit 79b1c6b

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

package-lock.json

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

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)