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