@rizzxdc/baileys 8.0.10 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -2
- package/README.md +107 -245
- package/WAProto/index.d.ts +15795 -0
- package/WAProto/index.js +25587 -4867
- package/lib/Defaults/index.d.ts +147 -0
- package/lib/Defaults/index.js +134 -104
- package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
- package/lib/Signal/Group/ciphertext-message.js +9 -11
- package/lib/Signal/Group/group-session-builder.d.ts +8 -0
- package/lib/Signal/Group/group-session-builder.js +5 -8
- package/lib/Signal/Group/group_cipher.d.ts +11 -0
- package/lib/Signal/Group/group_cipher.js +72 -75
- package/lib/Signal/Group/index.d.ts +12 -0
- package/lib/Signal/Group/index.js +11 -13
- package/lib/Signal/Group/keyhelper.d.ts +7 -0
- package/lib/Signal/Group/keyhelper.js +11 -14
- package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
- package/lib/Signal/Group/sender-chain-key.js +23 -26
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +58 -61
- package/lib/Signal/Group/sender-key-message.d.ts +19 -0
- package/lib/Signal/Group/sender-key-message.js +61 -64
- package/lib/Signal/Group/sender-key-name.d.ts +12 -0
- package/lib/Signal/Group/sender-key-name.js +39 -44
- package/lib/Signal/Group/sender-key-record.d.ts +13 -0
- package/lib/Signal/Group/sender-key-record.js +35 -38
- package/lib/Signal/Group/sender-key-state.d.ts +17 -0
- package/lib/Signal/Group/sender-key-state.js +76 -79
- package/lib/Signal/Group/sender-message-key.d.ts +12 -0
- package/lib/Signal/Group/sender-message-key.js +23 -26
- package/lib/Signal/libsignal.d.ts +55 -0
- package/lib/Signal/libsignal.js +414 -308
- package/lib/Signal/lid-mapping.d.ts +21 -0
- package/lib/Signal/lid-mapping.js +270 -149
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.js +2 -4
- package/lib/Socket/Client/types.d.ts +6 -0
- package/lib/Socket/Client/types.js +8 -11
- package/lib/Socket/Client/websocket.d.ts +13 -0
- package/lib/Socket/Client/websocket.js +51 -50
- package/lib/Socket/business.d.ts +395 -0
- package/lib/Socket/business.js +357 -355
- package/lib/Socket/chats.d.ts +137 -0
- package/lib/Socket/chats.js +1195 -867
- package/lib/Socket/communities.d.ts +562 -0
- package/lib/Socket/communities.js +416 -398
- package/lib/Socket/dugong.js +8 -1
- package/lib/Socket/groups.d.ts +286 -0
- package/lib/Socket/groups.js +336 -301
- package/lib/Socket/index.d.ts +530 -0
- package/lib/Socket/index.js +8 -13
- package/lib/Socket/messages-recv.d.ts +334 -0
- package/lib/Socket/messages-recv.js +1729 -1121
- package/lib/Socket/messages-send.d.ts +330 -0
- package/lib/Socket/messages-send.js +684 -462
- package/lib/Socket/mex.d.ts +2 -0
- package/lib/Socket/mex.js +36 -40
- package/lib/Socket/newsletter.d.ts +284 -0
- package/lib/Socket/newsletter.js +247 -237
- package/lib/Socket/socket.d.ts +61 -0
- package/lib/Socket/socket.js +968 -790
- package/lib/Store/index.d.ts +4 -0
- package/lib/Store/index.js +4 -6
- package/lib/Store/make-cache-manager-store.js +2 -2
- package/lib/Store/make-in-memory-store.d.ts +63 -0
- package/lib/Store/make-in-memory-store.js +414 -284
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +75 -76
- package/lib/Store/object-repository.d.ts +11 -0
- package/lib/Store/object-repository.js +21 -23
- package/lib/Types/Auth.d.ts +2 -0
- package/lib/Types/Auth.js +0 -2
- package/lib/Types/Bussines.d.ts +2 -0
- package/lib/Types/Bussines.js +0 -2
- package/lib/Types/Call.d.ts +2 -0
- package/lib/Types/Call.js +0 -2
- package/lib/Types/Chat.d.ts +2 -0
- package/lib/Types/Chat.js +5 -7
- package/lib/Types/Contact.d.ts +2 -0
- package/lib/Types/Contact.js +0 -2
- package/lib/Types/Events.d.ts +2 -0
- package/lib/Types/Events.js +1 -3
- package/lib/Types/GroupMetadata.d.ts +2 -0
- package/lib/Types/GroupMetadata.js +0 -2
- package/lib/Types/Label.d.ts +3 -0
- package/lib/Types/Label.js +21 -22
- package/lib/Types/LabelAssociation.d.ts +3 -0
- package/lib/Types/LabelAssociation.js +3 -4
- package/lib/Types/Message.d.ts +12 -0
- package/lib/Types/Message.js +11 -6
- package/lib/Types/Mex.d.ts +3 -0
- package/lib/Types/Mex.js +38 -0
- package/lib/Types/Product.d.ts +2 -0
- package/lib/Types/Product.js +0 -2
- package/lib/Types/RichType.d.ts +3 -0
- package/lib/Types/RichType.js +22 -0
- package/lib/Types/Signal.d.ts +2 -0
- package/lib/Types/Signal.js +1 -3
- package/lib/Types/Socket.d.ts +2 -0
- package/lib/Types/Socket.js +1 -4
- package/lib/Types/State.d.ts +6 -0
- package/lib/Types/State.js +52 -8
- package/lib/Types/USync.d.ts +2 -0
- package/lib/Types/USync.js +1 -3
- package/lib/Types/index.d.ts +14 -0
- package/lib/Types/index.js +22 -25
- package/lib/Utils/auth-utils.d.ts +58 -0
- package/lib/Utils/auth-utils.js +285 -203
- package/lib/Utils/browser-utils.d.ts +9 -0
- package/lib/Utils/browser-utils.js +21 -19
- package/lib/Utils/business.d.ts +50 -0
- package/lib/Utils/business.js +206 -209
- package/lib/Utils/chat-utils.d.ts +409 -0
- package/lib/Utils/chat-utils.js +825 -706
- package/lib/Utils/companion-reg-client-utils.d.ts +5 -0
- package/lib/Utils/companion-reg-client-utils.js +39 -0
- package/lib/Utils/crypto.d.ts +40 -0
- package/lib/Utils/crypto.js +88 -101
- package/lib/Utils/decode-wa-message.d.ts +84 -0
- package/lib/Utils/decode-wa-message.js +287 -238
- package/lib/Utils/event-buffer.d.ts +13 -0
- package/lib/Utils/event-buffer.js +602 -509
- package/lib/Utils/generics.d.ts +64 -0
- package/lib/Utils/generics.js +334 -295
- package/lib/Utils/history.d.ts +76 -0
- package/lib/Utils/history.js +125 -79
- package/lib/Utils/identity-change-handler.d.ts +14 -0
- package/lib/Utils/identity-change-handler.js +49 -0
- package/lib/Utils/index.d.ts +25 -0
- package/lib/Utils/index.js +25 -21
- package/lib/Utils/link-preview.d.ts +13 -0
- package/lib/Utils/link-preview.js +73 -70
- package/lib/Utils/logger.d.ts +3 -0
- package/lib/Utils/logger.js +1 -4
- package/lib/Utils/lt-hash.d.ts +7 -0
- package/lib/Utils/lt-hash.js +7 -45
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/make-mutex.js +27 -31
- package/lib/Utils/message-retry-manager.d.ts +80 -0
- package/lib/Utils/message-retry-manager.js +165 -14
- package/lib/Utils/messages-media.d.ts +137 -0
- package/lib/Utils/messages-media.js +768 -572
- package/lib/Utils/messages.d.ts +46 -0
- package/lib/Utils/messages.js +1082 -130
- package/lib/Utils/noise-handler.d.ts +21 -0
- package/lib/Utils/noise-handler.js +191 -135
- package/lib/Utils/offline-node-processor.d.ts +10 -0
- package/lib/Utils/offline-node-processor.js +39 -0
- package/lib/Utils/pre-key-manager.d.ts +26 -0
- package/lib/Utils/pre-key-manager.js +96 -76
- package/lib/Utils/process-message.d.ts +84 -0
- package/lib/Utils/process-message.js +718 -311
- package/lib/Utils/reporting-utils.d.ts +13 -0
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/rich-message-utils.d.ts +49 -0
- package/lib/Utils/rich-message-utils.js +257 -0
- package/lib/Utils/signal.d.ts +83 -0
- package/lib/Utils/signal.js +174 -135
- package/lib/Utils/stanza-ack.d.ts +17 -0
- package/lib/Utils/stanza-ack.js +37 -0
- package/lib/Utils/sync-action-utils.d.ts +3 -0
- package/lib/Utils/sync-action-utils.js +48 -0
- package/lib/Utils/tc-token-utils.d.ts +30 -0
- package/lib/Utils/tc-token-utils.js +162 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +11 -0
- package/lib/Utils/use-multi-file-auth-state.js +104 -95
- package/lib/Utils/use-single-file-auth-state.d.ts +11 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
- package/lib/Utils/use-sqlite-auth-state.js +108 -0
- package/lib/Utils/validate-connection.d.ts +44 -0
- package/lib/Utils/validate-connection.js +182 -180
- package/lib/WABinary/constants.d.ts +62 -0
- package/lib/WABinary/constants.js +1455 -1292
- package/lib/WABinary/decode.d.ts +10 -0
- package/lib/WABinary/decode.js +249 -228
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/encode.js +207 -206
- package/lib/WABinary/generic-utils.d.ts +77 -0
- package/lib/WABinary/generic-utils.js +135 -99
- package/lib/WABinary/index.d.ts +6 -0
- package/lib/WABinary/index.js +5 -7
- package/lib/WABinary/jid-utils.d.ts +29 -0
- package/lib/WABinary/jid-utils.js +80 -78
- package/lib/WABinary/types.d.ts +2 -0
- package/lib/WABinary/types.js +1 -3
- package/lib/WAM/BinaryInfo.d.ts +8 -0
- package/lib/WAM/BinaryInfo.js +7 -9
- package/lib/WAM/constants.d.ts +34927 -0
- package/lib/WAM/constants.js +22847 -22848
- package/lib/WAM/encode.d.ts +2 -0
- package/lib/WAM/encode.js +129 -134
- package/lib/WAM/index.d.ts +4 -0
- package/lib/WAM/index.js +3 -5
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +37 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +48 -27
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -48
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -26
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -35
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +39 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +46 -49
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +15 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +23 -25
- package/lib/WAUSync/Protocols/index.d.ts +6 -0
- package/lib/WAUSync/Protocols/index.js +5 -6
- package/lib/WAUSync/USyncQuery.d.ts +21 -0
- package/lib/WAUSync/USyncQuery.js +92 -85
- package/lib/WAUSync/USyncUser.d.ts +17 -0
- package/lib/WAUSync/USyncUser.js +28 -22
- package/lib/WAUSync/index.d.ts +4 -0
- package/lib/WAUSync/index.js +3 -5
- package/lib/index.d.ts +12 -0
- package/lib/index.js +31 -19
- package/package.json +12 -6
- package/lib/Types/Newsletter.js +0 -33
|
@@ -1,111 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
import { mkdir, readFile, stat, unlink, writeFile } from
|
|
3
|
-
import {
|
|
4
|
-
import { proto } from
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { Mutex } from 'async-mutex';
|
|
2
|
+
import { mkdir, readFile, stat, unlink, writeFile } from 'fs/promises';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { proto } from '../../WAProto/index.js';
|
|
5
|
+
import { initAuthCreds } from './auth-utils.js';
|
|
6
|
+
import { BufferJSON } from './generics.js';
|
|
7
|
+
// We need to lock files due to the fact that we are using async functions to read and write files
|
|
8
|
+
// https://github.com/WhiskeySockets/Baileys/issues/794
|
|
9
|
+
// https://github.com/nodejs/node/issues/26338
|
|
10
|
+
// Use a Map to store mutexes for each file path
|
|
9
11
|
const fileLocks = new Map();
|
|
12
|
+
// Get or create a mutex for a specific file path
|
|
10
13
|
const getFileLock = (path) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
let mutex = fileLocks.get(path);
|
|
15
|
+
if (!mutex) {
|
|
16
|
+
mutex = new Mutex();
|
|
17
|
+
fileLocks.set(path, mutex);
|
|
18
|
+
}
|
|
19
|
+
return mutex;
|
|
17
20
|
};
|
|
18
|
-
|
|
21
|
+
/**
|
|
22
|
+
* stores the full authentication state in a single folder.
|
|
23
|
+
* Far more efficient than singlefileauthstate
|
|
24
|
+
*
|
|
25
|
+
* Again, I wouldn't endorse this for any production level use other than perhaps a bot.
|
|
26
|
+
* Would recommend writing an auth state for use with a proper SQL or No-SQL DB
|
|
27
|
+
* */
|
|
19
28
|
export const useMultiFileAuthState = async (folder) => {
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const filePath = join(folder, fixFileName(file));
|
|
35
|
-
const mutex = getFileLock(filePath);
|
|
36
|
-
return await mutex.acquire().then(async (release) => {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
const writeData = async (data, file) => {
|
|
31
|
+
const filePath = join(folder, fixFileName(file));
|
|
32
|
+
const mutex = getFileLock(filePath);
|
|
33
|
+
return mutex.acquire().then(async (release) => {
|
|
34
|
+
try {
|
|
35
|
+
await writeFile(filePath, JSON.stringify(data, BufferJSON.replacer));
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
release();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
const readData = async (file) => {
|
|
37
43
|
try {
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
const filePath = join(folder, fixFileName(file));
|
|
45
|
+
const mutex = getFileLock(filePath);
|
|
46
|
+
return await mutex.acquire().then(async (release) => {
|
|
47
|
+
try {
|
|
48
|
+
const data = await readFile(filePath, { encoding: 'utf-8' });
|
|
49
|
+
return JSON.parse(data, BufferJSON.reviver);
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
release();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
40
55
|
}
|
|
41
|
-
|
|
42
|
-
|
|
56
|
+
catch (error) {
|
|
57
|
+
return null;
|
|
43
58
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
catch (error) {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
const removeData = async (file) => {
|
|
51
|
-
try {
|
|
52
|
-
const filePath = join(folder, fixFileName(file));
|
|
53
|
-
const mutex = getFileLock(filePath);
|
|
54
|
-
return mutex.acquire().then(async (release) => {
|
|
59
|
+
};
|
|
60
|
+
const removeData = async (file) => {
|
|
55
61
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
const filePath = join(folder, fixFileName(file));
|
|
63
|
+
const mutex = getFileLock(filePath);
|
|
64
|
+
return mutex.acquire().then(async (release) => {
|
|
65
|
+
try {
|
|
66
|
+
await unlink(filePath);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
release();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
59
74
|
}
|
|
60
|
-
|
|
61
|
-
|
|
75
|
+
catch { }
|
|
76
|
+
};
|
|
77
|
+
const folderInfo = await stat(folder).catch(() => { });
|
|
78
|
+
if (folderInfo) {
|
|
79
|
+
if (!folderInfo.isDirectory()) {
|
|
80
|
+
throw new Error(`found something that is not a directory at ${folder}, either delete it or specify a different location`);
|
|
62
81
|
}
|
|
63
|
-
});
|
|
64
82
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const folderInfo = await stat(folder).catch(() => { });
|
|
68
|
-
if (folderInfo) {
|
|
69
|
-
if (!folderInfo.isDirectory()) {
|
|
70
|
-
throw new Error(`found something that is not a directory at ${folder}, either delete it or specify a different location`);
|
|
83
|
+
else {
|
|
84
|
+
await mkdir(folder, { recursive: true });
|
|
71
85
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
const fixFileName = (file) => file?.replace(/\//g, '__')?.replace(/:/g, '-');
|
|
87
|
+
const creds = (await readData('creds.json')) || initAuthCreds();
|
|
88
|
+
return {
|
|
89
|
+
state: {
|
|
90
|
+
creds,
|
|
91
|
+
keys: {
|
|
92
|
+
get: async (type, ids) => {
|
|
93
|
+
const data = {};
|
|
94
|
+
await Promise.all(ids.map(async (id) => {
|
|
95
|
+
let value = await readData(`${type}-${id}.json`);
|
|
96
|
+
if (type === 'app-state-sync-key' && value) {
|
|
97
|
+
value = proto.Message.AppStateSyncKeyData.fromObject(value);
|
|
98
|
+
}
|
|
99
|
+
data[id] = value;
|
|
100
|
+
}));
|
|
101
|
+
return data;
|
|
102
|
+
},
|
|
103
|
+
set: async (data) => {
|
|
104
|
+
const tasks = [];
|
|
105
|
+
for (const category in data) {
|
|
106
|
+
for (const id in data[category]) {
|
|
107
|
+
const value = data[category][id];
|
|
108
|
+
const file = `${category}-${id}.json`;
|
|
109
|
+
tasks.push(value ? writeData(value, file) : removeData(file));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
await Promise.all(tasks);
|
|
113
|
+
}
|
|
88
114
|
}
|
|
89
|
-
data[id] = value;
|
|
90
|
-
}));
|
|
91
|
-
return data;
|
|
92
115
|
},
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
for (const category in data) {
|
|
96
|
-
for (const id in data[category]) {
|
|
97
|
-
const value = data[category][id];
|
|
98
|
-
const file = `${category}-${id}.json`;
|
|
99
|
-
tasks.push(value ? writeData(value, file) : removeData(file));
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
await Promise.all(tasks);
|
|
116
|
+
saveCreds: async () => {
|
|
117
|
+
return writeData(creds, 'creds.json');
|
|
103
118
|
}
|
|
104
|
-
|
|
105
|
-
},
|
|
106
|
-
saveCreds: async () => {
|
|
107
|
-
return writeData(creds, "creds.json");
|
|
108
|
-
}
|
|
109
|
-
};
|
|
119
|
+
};
|
|
110
120
|
};
|
|
111
|
-
//=======================================================//
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function useSingleFileAuthState(fileName: any): Promise<{
|
|
2
|
+
state: {
|
|
3
|
+
creds: any;
|
|
4
|
+
keys: {
|
|
5
|
+
get: (type: any, ids: any) => {};
|
|
6
|
+
set: (data: any) => void;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
saveCreds: () => void;
|
|
10
|
+
}>;
|
|
11
|
+
//# sourceMappingURL=use-single-file-auth-state.d.ts.map
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { readFile, rename, stat, writeFile } from 'fs/promises';
|
|
2
|
+
import { DEFAULT_CACHE_TTLS } from '../Defaults/index.js';
|
|
3
|
+
import { proto } from '../../WAProto/index.js';
|
|
4
|
+
import { initAuthCreds } from './auth-utils.js';
|
|
5
|
+
import { BufferJSON } from './generics.js';
|
|
6
|
+
import { LRUCache } from 'lru-cache';
|
|
7
|
+
import { Mutex } from 'async-mutex';
|
|
8
|
+
// Lia@Changes 25-03-26 --- Add useSingleFileAuthState with integrated cache
|
|
9
|
+
const FLUSH_TIMEOUT_MS = 3000;
|
|
10
|
+
// Lia@Changes 22-04-26 --- Enhanced useSingleFileAuthState with LRUCache
|
|
11
|
+
export const useSingleFileAuthState = async (fileName) => {
|
|
12
|
+
const cache = new LRUCache({
|
|
13
|
+
max: 15000,
|
|
14
|
+
ttl: 1000 * DEFAULT_CACHE_TTLS.SIGNAL_STORE,
|
|
15
|
+
updateAgeOnGet: false,
|
|
16
|
+
updateAgeOnHas: false,
|
|
17
|
+
ttlAutopurge: true
|
|
18
|
+
});
|
|
19
|
+
// Lia@Changes 26-04-26 --- Add mutex for prevent race condition
|
|
20
|
+
const mutex = new Mutex();
|
|
21
|
+
let fileData = {};
|
|
22
|
+
let isLoaded = false;
|
|
23
|
+
let flushTimeout = null;
|
|
24
|
+
const loadKey = async () => {
|
|
25
|
+
return await mutex.runExclusive(async () => {
|
|
26
|
+
if (isLoaded)
|
|
27
|
+
return;
|
|
28
|
+
try {
|
|
29
|
+
const data = JSON.parse(await readFile(fileName, 'utf-8'), BufferJSON.reviver);
|
|
30
|
+
fileData = data || {};
|
|
31
|
+
for (const [keyName, value] of Object.entries(fileData)) {
|
|
32
|
+
cache.set(keyName, value);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
fileData = {};
|
|
37
|
+
}
|
|
38
|
+
isLoaded = true;
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
const flushKey = () => {
|
|
42
|
+
if (flushTimeout)
|
|
43
|
+
return;
|
|
44
|
+
flushTimeout = setTimeout(async () => {
|
|
45
|
+
flushTimeout = null;
|
|
46
|
+
await mutex.runExclusive(async () => {
|
|
47
|
+
try {
|
|
48
|
+
const tempFile = fileName + '.temp';
|
|
49
|
+
await writeFile(tempFile, JSON.stringify(fileData, BufferJSON.replacer));
|
|
50
|
+
await rename(tempFile, fileName);
|
|
51
|
+
}
|
|
52
|
+
catch { }
|
|
53
|
+
});
|
|
54
|
+
}, FLUSH_TIMEOUT_MS);
|
|
55
|
+
};
|
|
56
|
+
const writeKey = (keyName, value) => {
|
|
57
|
+
cache.set(keyName, value);
|
|
58
|
+
fileData[keyName] = value;
|
|
59
|
+
flushKey();
|
|
60
|
+
};
|
|
61
|
+
const removeKey = (keyName) => {
|
|
62
|
+
cache.delete(keyName);
|
|
63
|
+
delete fileData[keyName];
|
|
64
|
+
flushKey();
|
|
65
|
+
};
|
|
66
|
+
const fileInfo = await stat(fileName).catch(() => null);
|
|
67
|
+
if (!fileInfo) {
|
|
68
|
+
await writeFile(fileName, '{}');
|
|
69
|
+
}
|
|
70
|
+
else if (!fileInfo.isFile()) {
|
|
71
|
+
throw new Error(`found something that is not a file at ${fileName}, either delete it or specify a different location`);
|
|
72
|
+
}
|
|
73
|
+
await loadKey();
|
|
74
|
+
const creds = fileData['creds'] || initAuthCreds();
|
|
75
|
+
return {
|
|
76
|
+
state: {
|
|
77
|
+
creds,
|
|
78
|
+
keys: {
|
|
79
|
+
get: (type, ids) => {
|
|
80
|
+
const data = {};
|
|
81
|
+
for (const id of ids) {
|
|
82
|
+
const keyName = type + id;
|
|
83
|
+
let value = cache.get(keyName);
|
|
84
|
+
if (value === undefined && fileData[keyName] !== undefined) {
|
|
85
|
+
value = fileData[keyName];
|
|
86
|
+
cache.set(keyName, value);
|
|
87
|
+
}
|
|
88
|
+
if (type === 'app-state-sync-key' && value) {
|
|
89
|
+
value = proto.Message.AppStateSyncKeyData.fromObject(value);
|
|
90
|
+
}
|
|
91
|
+
data[id] = value;
|
|
92
|
+
}
|
|
93
|
+
return data;
|
|
94
|
+
},
|
|
95
|
+
set: (data) => {
|
|
96
|
+
for (const category in data) {
|
|
97
|
+
for (const id in data[category]) {
|
|
98
|
+
const keyName = category + id;
|
|
99
|
+
const value = data[category][id];
|
|
100
|
+
value ? writeKey(keyName, value) : removeKey(keyName);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
saveCreds: () => writeKey('creds', creds)
|
|
107
|
+
};
|
|
108
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function useSqliteAuthState(opts: any): Promise<{
|
|
2
|
+
state: {
|
|
3
|
+
creds: any;
|
|
4
|
+
keys: {
|
|
5
|
+
get: (type: any, ids: any) => Promise<{}>;
|
|
6
|
+
set: (data: any) => Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
saveCreds: () => Promise<void>;
|
|
10
|
+
}>;
|
|
11
|
+
//# sourceMappingURL=use-sqlite-auth-state.d.ts.map
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { mkdir, readFile, stat, unlink, writeFile } from 'fs/promises';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { proto } from '../../WAProto/index.js';
|
|
4
|
+
import { initAuthCreds } from './auth-utils.js';
|
|
5
|
+
import { BufferJSON } from './generics.js';
|
|
6
|
+
async function loadBetterSqlite3() {
|
|
7
|
+
try {
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
const mod = (await import('better-sqlite3'));
|
|
10
|
+
return mod.default ?? mod;
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
const helpful = new Error('`better-sqlite3` is required for `useSqliteAuthState`. Install it as a peer dependency: `npm install better-sqlite3` (or `yarn add better-sqlite3`).');
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
helpful.cause = err;
|
|
16
|
+
throw helpful;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const CREDS_ROW_KEY = '__creds__';
|
|
20
|
+
const CREATE_SCHEMA_SQL = `
|
|
21
|
+
CREATE TABLE IF NOT EXISTS creds (
|
|
22
|
+
key TEXT PRIMARY KEY,
|
|
23
|
+
value TEXT NOT NULL
|
|
24
|
+
);
|
|
25
|
+
CREATE TABLE IF NOT EXISTS signal_keys (
|
|
26
|
+
type TEXT NOT NULL,
|
|
27
|
+
id TEXT NOT NULL,
|
|
28
|
+
value TEXT NOT NULL,
|
|
29
|
+
PRIMARY KEY (type, id)
|
|
30
|
+
);
|
|
31
|
+
CREATE INDEX IF NOT EXISTS signal_keys_type_idx ON signal_keys(type);
|
|
32
|
+
`;
|
|
33
|
+
export async function useSqliteAuthState(opts) {
|
|
34
|
+
let db;
|
|
35
|
+
if (opts.database) {
|
|
36
|
+
db = opts.database;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const Database = await loadBetterSqlite3();
|
|
40
|
+
db = new Database(opts.dbPath);
|
|
41
|
+
}
|
|
42
|
+
// WAL mode allows concurrent reads alongside a single writer; matches
|
|
43
|
+
// what SQLite recommends for read-heavy workloads with sporadic writes.
|
|
44
|
+
db.pragma('journal_mode = WAL');
|
|
45
|
+
db.pragma('synchronous = NORMAL');
|
|
46
|
+
db.exec(CREATE_SCHEMA_SQL);
|
|
47
|
+
const stmts = {
|
|
48
|
+
credsSelect: db.prepare('SELECT value FROM creds WHERE key = ?'),
|
|
49
|
+
credsUpsert: db.prepare('INSERT INTO creds (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value'),
|
|
50
|
+
keySelect: db.prepare('SELECT value FROM signal_keys WHERE type = ? AND id = ?'),
|
|
51
|
+
keyUpsert: db.prepare('INSERT INTO signal_keys (type, id, value) VALUES (?, ?, ?) ON CONFLICT(type, id) DO UPDATE SET value = excluded.value'),
|
|
52
|
+
keyDelete: db.prepare('DELETE FROM signal_keys WHERE type = ? AND id = ?'),
|
|
53
|
+
keyListIds: db.prepare('SELECT id FROM signal_keys WHERE type = ?'),
|
|
54
|
+
keyList: db.prepare('SELECT id, value FROM signal_keys WHERE type = ?'),
|
|
55
|
+
clearKeys: db.prepare('DELETE FROM signal_keys')
|
|
56
|
+
};
|
|
57
|
+
const loadCreds = () => {
|
|
58
|
+
const row = stmts.credsSelect.get(CREDS_ROW_KEY);
|
|
59
|
+
if (!row)
|
|
60
|
+
return initAuthCreds();
|
|
61
|
+
return JSON.parse(row.value, BufferJSON.reviver);
|
|
62
|
+
};
|
|
63
|
+
const persistCreds = (creds) => {
|
|
64
|
+
stmts.credsUpsert.run(CREDS_ROW_KEY, JSON.stringify(creds, BufferJSON.replacer));
|
|
65
|
+
};
|
|
66
|
+
const creds = loadCreds();
|
|
67
|
+
return {
|
|
68
|
+
state: {
|
|
69
|
+
creds,
|
|
70
|
+
keys: {
|
|
71
|
+
get: async (type, ids) => {
|
|
72
|
+
const data = {};
|
|
73
|
+
for (const id of ids) {
|
|
74
|
+
const row = stmts.keySelect.get(type, id);
|
|
75
|
+
if (row) {
|
|
76
|
+
let value = JSON.parse(row.value, BufferJSON.reviver);
|
|
77
|
+
if (type === 'app-state-sync-key' && value) {
|
|
78
|
+
value = proto.Message.AppStateSyncKeyData.fromObject(value);
|
|
79
|
+
}
|
|
80
|
+
data[id] = value;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return data;
|
|
84
|
+
},
|
|
85
|
+
set: async (data) => {
|
|
86
|
+
const writeTx = db.transaction(() => {
|
|
87
|
+
for (const category in data) {
|
|
88
|
+
for (const id in data[category]) {
|
|
89
|
+
const value = data[category][id];
|
|
90
|
+
if (value) {
|
|
91
|
+
const stringified = JSON.stringify(value, BufferJSON.replacer);
|
|
92
|
+
stmts.keyUpsert.run(category, id, stringified);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
stmts.keyDelete.run(category, id);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
writeTx();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
saveCreds: async () => {
|
|
105
|
+
persistCreds(creds);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export function generateLoginNode(userJid: any, config: any): proto.ClientPayload;
|
|
2
|
+
export function generateRegistrationNode({ registrationId, signedPreKey, signedIdentityKey }: {
|
|
3
|
+
registrationId: any;
|
|
4
|
+
signedPreKey: any;
|
|
5
|
+
signedIdentityKey: any;
|
|
6
|
+
}, config: any): proto.ClientPayload;
|
|
7
|
+
export function configureSuccessfulPairing(stanza: any, { advSecretKey, signedIdentityKey, signalIdentities }: {
|
|
8
|
+
advSecretKey: any;
|
|
9
|
+
signedIdentityKey: any;
|
|
10
|
+
signalIdentities: any;
|
|
11
|
+
}): {
|
|
12
|
+
creds: {
|
|
13
|
+
account: proto.ADVSignedDeviceIdentity;
|
|
14
|
+
me: {
|
|
15
|
+
id: any;
|
|
16
|
+
name: any;
|
|
17
|
+
lid: any;
|
|
18
|
+
};
|
|
19
|
+
signalIdentities: any[];
|
|
20
|
+
platform: any;
|
|
21
|
+
};
|
|
22
|
+
reply: {
|
|
23
|
+
tag: string;
|
|
24
|
+
attrs: {
|
|
25
|
+
to: string;
|
|
26
|
+
type: string;
|
|
27
|
+
id: any;
|
|
28
|
+
};
|
|
29
|
+
content: {
|
|
30
|
+
tag: string;
|
|
31
|
+
attrs: {};
|
|
32
|
+
content: {
|
|
33
|
+
tag: string;
|
|
34
|
+
attrs: {
|
|
35
|
+
'key-index': string;
|
|
36
|
+
};
|
|
37
|
+
content: any;
|
|
38
|
+
}[];
|
|
39
|
+
}[];
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export function encodeSignedDeviceIdentity(account: any, includeSignatureKey: any): any;
|
|
43
|
+
import { proto } from '../../WAProto/index.js';
|
|
44
|
+
//# sourceMappingURL=validate-connection.d.ts.map
|