@baileys-md/baileys 10.1.0 → 11.0.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 +1 -1
- package/README.md +1303 -2
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +4633 -0
- package/WAProto/fix-imports.js +29 -0
- package/WAProto/index.js +13516 -4182
- package/lib/Defaults/baileys-version.js +1 -0
- package/lib/Defaults/index.js +51 -72
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group_cipher.js +94 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.js +19 -0
- package/lib/Signal/Group/queue-job.js +54 -0
- package/lib/Signal/Group/sender-chain-key.js +32 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-message.js +67 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-record.js +50 -0
- package/lib/Signal/Group/sender-key-state.js +96 -0
- package/{WASignalGroup/sender_message_key.js → lib/Signal/Group/sender-message-key.js} +4 -16
- package/lib/Signal/libsignal.js +41 -61
- package/lib/Socket/Client/index.js +3 -19
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/websocket.js +50 -0
- package/lib/Socket/business.js +37 -42
- package/lib/Socket/chats.js +194 -187
- package/lib/Socket/communities.js +351 -0
- package/lib/Socket/groups.js +87 -90
- package/lib/Socket/index.js +7 -8
- package/lib/Socket/messages-recv.js +360 -335
- package/lib/Socket/messages-send.js +156 -279
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/newsletter.js +144 -213
- package/lib/Socket/socket.js +128 -161
- package/lib/Socket/usync.js +19 -26
- package/lib/Types/Auth.js +2 -2
- package/lib/Types/Call.js +2 -2
- package/lib/Types/Chat.js +8 -4
- package/lib/Types/Contact.js +2 -2
- package/lib/Types/Events.js +2 -2
- package/lib/Types/GroupMetadata.js +2 -2
- package/lib/Types/Label.js +3 -5
- package/lib/Types/LabelAssociation.js +3 -5
- package/lib/Types/Message.js +7 -7
- package/lib/Types/Newsletter.js +30 -17
- package/lib/Types/Product.js +2 -2
- package/lib/Types/Signal.js +2 -2
- package/lib/Types/Socket.js +3 -2
- package/lib/Types/State.js +2 -2
- package/lib/Types/USync.js +2 -2
- package/lib/Types/index.js +15 -31
- package/lib/Utils/auth-utils.js +31 -47
- package/lib/Utils/baileys-event-stream.js +15 -22
- package/lib/Utils/business.js +66 -69
- package/lib/Utils/chat-utils.js +200 -195
- package/lib/Utils/crypto.js +70 -85
- package/lib/Utils/decode-wa-message.js +47 -51
- package/lib/Utils/event-buffer.js +36 -46
- package/lib/Utils/generics.js +116 -188
- package/lib/Utils/history.js +37 -46
- package/lib/Utils/index.js +19 -33
- package/lib/Utils/link-preview.js +14 -55
- package/lib/Utils/logger.js +3 -7
- package/lib/Utils/lt-hash.js +23 -26
- package/lib/{Store → Utils}/make-in-memory-store.js +19 -27
- package/lib/Utils/make-mutex.js +7 -10
- package/lib/{Store → Utils}/make-ordered-dictionary.js +1 -3
- package/lib/Utils/messages-media.js +236 -368
- package/lib/Utils/messages.js +278 -510
- package/lib/Utils/noise-handler.js +22 -31
- package/lib/{Store → Utils}/object-repository.js +1 -4
- package/lib/Utils/process-message.js +144 -148
- package/lib/Utils/signal.js +71 -64
- package/lib/Utils/use-multi-file-auth-state.js +112 -84
- package/lib/Utils/validate-connection.js +72 -115
- package/lib/WABinary/constants.js +1281 -20
- package/lib/WABinary/decode.js +15 -52
- package/lib/WABinary/encode.js +14 -48
- package/lib/WABinary/generic-utils.js +31 -39
- package/lib/WABinary/index.js +6 -21
- package/lib/WABinary/jid-utils.js +23 -40
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.js +2 -5
- package/lib/WAM/constants.js +2257 -2366
- package/lib/WAM/encode.js +17 -21
- package/lib/WAM/index.js +4 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -11
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +3 -6
- package/lib/WAUSync/Protocols/index.js +5 -20
- package/lib/WAUSync/USyncQuery.js +34 -32
- package/lib/WAUSync/USyncUser.js +2 -5
- package/lib/WAUSync/index.js +4 -19
- package/lib/index.js +11 -33
- package/package.json +25 -54
- package/WASignalGroup/GroupProtocol.js +0 -1697
- package/WASignalGroup/ciphertext_message.js +0 -16
- package/WASignalGroup/group_cipher.js +0 -120
- package/WASignalGroup/group_session_builder.js +0 -46
- package/WASignalGroup/index.js +0 -5
- package/WASignalGroup/keyhelper.js +0 -21
- package/WASignalGroup/protobufs.js +0 -3
- package/WASignalGroup/queue_job.js +0 -69
- package/WASignalGroup/sender_chain_key.js +0 -50
- package/WASignalGroup/sender_key_distribution_message.js +0 -78
- package/WASignalGroup/sender_key_message.js +0 -92
- package/WASignalGroup/sender_key_name.js +0 -70
- package/WASignalGroup/sender_key_record.js +0 -56
- package/WASignalGroup/sender_key_state.js +0 -129
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/registration.js +0 -166
- package/lib/Store/index.js +0 -8
- package/lib/Store/make-cache-manager-store.js +0 -83
- package/lib/Store/make-mongo-store.js +0 -567
package/lib/Utils/signal.js
CHANGED
|
@@ -1,96 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { KEY_BUNDLE_TYPE } from '../Defaults/index.js';
|
|
2
|
+
import { assertNodeErrorFree, getBinaryNodeChild, getBinaryNodeChildBuffer, getBinaryNodeChildren, getBinaryNodeChildUInt, jidDecode, S_WHATSAPP_NET } from '../WABinary/index.js';
|
|
3
|
+
import { Curve, generateSignalPubKey } from './crypto.js';
|
|
4
|
+
import { encodeBigEndian } from './generics.js';
|
|
5
|
+
function chunk(array, size) {
|
|
6
|
+
const chunks = [];
|
|
7
|
+
for (let i = 0; i < array.length; i += size) {
|
|
8
|
+
chunks.push(array.slice(i, i + size));
|
|
9
|
+
}
|
|
10
|
+
return chunks;
|
|
11
|
+
}
|
|
12
|
+
export const createSignalIdentity = (wid, accountSignatureKey) => {
|
|
9
13
|
return {
|
|
10
14
|
identifier: { name: wid, deviceId: 0 },
|
|
11
|
-
identifierKey:
|
|
15
|
+
identifierKey: generateSignalPubKey(accountSignatureKey)
|
|
12
16
|
};
|
|
13
17
|
};
|
|
14
|
-
|
|
15
|
-
const getPreKeys = async ({ get }, min, limit) => {
|
|
18
|
+
export const getPreKeys = async ({ get }, min, limit) => {
|
|
16
19
|
const idList = [];
|
|
17
20
|
for (let id = min; id < limit; id++) {
|
|
18
21
|
idList.push(id.toString());
|
|
19
22
|
}
|
|
20
23
|
return get('pre-key', idList);
|
|
21
24
|
};
|
|
22
|
-
|
|
23
|
-
const generateOrGetPreKeys = (creds, range) => {
|
|
25
|
+
export const generateOrGetPreKeys = (creds, range) => {
|
|
24
26
|
const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId;
|
|
25
27
|
const remaining = range - avaliable;
|
|
26
28
|
const lastPreKeyId = creds.nextPreKeyId + remaining - 1;
|
|
27
29
|
const newPreKeys = {};
|
|
28
30
|
if (remaining > 0) {
|
|
29
31
|
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
|
|
30
|
-
newPreKeys[i] =
|
|
32
|
+
newPreKeys[i] = Curve.generateKeyPair();
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
35
|
return {
|
|
34
36
|
newPreKeys,
|
|
35
37
|
lastPreKeyId,
|
|
36
|
-
preKeysRange: [creds.firstUnuploadedPreKeyId, range]
|
|
38
|
+
preKeysRange: [creds.firstUnuploadedPreKeyId, range]
|
|
37
39
|
};
|
|
38
40
|
};
|
|
39
|
-
|
|
40
|
-
const xmppSignedPreKey = (key) => ({
|
|
41
|
+
export const xmppSignedPreKey = (key) => ({
|
|
41
42
|
tag: 'skey',
|
|
42
43
|
attrs: {},
|
|
43
44
|
content: [
|
|
44
|
-
{ tag: 'id', attrs: {}, content:
|
|
45
|
+
{ tag: 'id', attrs: {}, content: encodeBigEndian(key.keyId, 3) },
|
|
45
46
|
{ tag: 'value', attrs: {}, content: key.keyPair.public },
|
|
46
47
|
{ tag: 'signature', attrs: {}, content: key.signature }
|
|
47
48
|
]
|
|
48
49
|
});
|
|
49
|
-
|
|
50
|
-
const xmppPreKey = (pair, id) => ({
|
|
50
|
+
export const xmppPreKey = (pair, id) => ({
|
|
51
51
|
tag: 'key',
|
|
52
52
|
attrs: {},
|
|
53
53
|
content: [
|
|
54
|
-
{ tag: 'id', attrs: {}, content:
|
|
54
|
+
{ tag: 'id', attrs: {}, content: encodeBigEndian(id, 3) },
|
|
55
55
|
{ tag: 'value', attrs: {}, content: pair.public }
|
|
56
56
|
]
|
|
57
57
|
});
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
export const parseAndInjectE2ESessions = async (node, repository) => {
|
|
59
|
+
const extractKey = (key) => key
|
|
60
|
+
? {
|
|
61
|
+
keyId: getBinaryNodeChildUInt(key, 'id', 3),
|
|
62
|
+
publicKey: generateSignalPubKey(getBinaryNodeChildBuffer(key, 'value')),
|
|
63
|
+
signature: getBinaryNodeChildBuffer(key, 'signature')
|
|
64
|
+
}
|
|
65
|
+
: undefined;
|
|
66
|
+
const nodes = getBinaryNodeChildren(getBinaryNodeChild(node, 'list'), 'user');
|
|
66
67
|
for (const node of nodes) {
|
|
67
|
-
|
|
68
|
+
assertNodeErrorFree(node);
|
|
69
|
+
}
|
|
70
|
+
// Most of the work in repository.injectE2ESession is CPU intensive, not IO
|
|
71
|
+
// So Promise.all doesn't really help here,
|
|
72
|
+
// but blocks even loop if we're using it inside keys.transaction, and it makes it "sync" actually
|
|
73
|
+
// This way we chunk it in smaller parts and between those parts we can yield to the event loop
|
|
74
|
+
// It's rare case when you need to E2E sessions for so many users, but it's possible
|
|
75
|
+
const chunkSize = 100;
|
|
76
|
+
const chunks = chunk(nodes, chunkSize);
|
|
77
|
+
for (const nodesChunk of chunks) {
|
|
78
|
+
await Promise.all(nodesChunk.map(async (node) => {
|
|
79
|
+
const signedKey = getBinaryNodeChild(node, 'skey');
|
|
80
|
+
const key = getBinaryNodeChild(node, 'key');
|
|
81
|
+
const identity = getBinaryNodeChildBuffer(node, 'identity');
|
|
82
|
+
const jid = node.attrs.jid;
|
|
83
|
+
const registrationId = getBinaryNodeChildUInt(node, 'registration', 4);
|
|
84
|
+
await repository.injectE2ESession({
|
|
85
|
+
jid,
|
|
86
|
+
session: {
|
|
87
|
+
registrationId: registrationId,
|
|
88
|
+
identityKey: generateSignalPubKey(identity),
|
|
89
|
+
signedPreKey: extractKey(signedKey),
|
|
90
|
+
preKey: extractKey(key)
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}));
|
|
68
94
|
}
|
|
69
|
-
await Promise.all(nodes.map(async (node) => {
|
|
70
|
-
const signedKey = (0, WABinary_1.getBinaryNodeChild)(node, 'skey');
|
|
71
|
-
const key = (0, WABinary_1.getBinaryNodeChild)(node, 'key');
|
|
72
|
-
const identity = (0, WABinary_1.getBinaryNodeChildBuffer)(node, 'identity');
|
|
73
|
-
const jid = node.attrs.jid;
|
|
74
|
-
const registrationId = (0, WABinary_1.getBinaryNodeChildUInt)(node, 'registration', 4);
|
|
75
|
-
await repository.injectE2ESession({
|
|
76
|
-
jid,
|
|
77
|
-
session: {
|
|
78
|
-
registrationId: registrationId,
|
|
79
|
-
identityKey: (0, crypto_1.generateSignalPubKey)(identity),
|
|
80
|
-
signedPreKey: extractKey(signedKey),
|
|
81
|
-
preKey: extractKey(key)
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}));
|
|
85
95
|
};
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
const { user: myUser, device: myDevice } = (0, WABinary_1.jidDecode)(myJid);
|
|
96
|
+
export const extractDeviceJids = (result, myJid, excludeZeroDevices) => {
|
|
97
|
+
const { user: myUser, device: myDevice } = jidDecode(myJid);
|
|
89
98
|
const extracted = [];
|
|
90
99
|
for (const userResult of result) {
|
|
91
100
|
const { devices, id } = userResult;
|
|
92
|
-
const { user } =
|
|
93
|
-
const deviceList = devices
|
|
101
|
+
const { user } = jidDecode(id);
|
|
102
|
+
const deviceList = devices?.deviceList;
|
|
94
103
|
if (Array.isArray(deviceList)) {
|
|
95
104
|
for (const { id: device, keyIndex } of deviceList) {
|
|
96
105
|
if ((!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
|
|
@@ -104,40 +113,38 @@ const extractDeviceJids = (result, myJid, excludeZeroDevices) => {
|
|
|
104
113
|
}
|
|
105
114
|
return extracted;
|
|
106
115
|
};
|
|
107
|
-
exports.extractDeviceJids = extractDeviceJids;
|
|
108
116
|
/**
|
|
109
117
|
* get the next N keys for upload or processing
|
|
110
118
|
* @param count number of pre-keys to get or generate
|
|
111
119
|
*/
|
|
112
|
-
const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
113
|
-
const { newPreKeys, lastPreKeyId, preKeysRange } =
|
|
120
|
+
export const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
121
|
+
const { newPreKeys, lastPreKeyId, preKeysRange } = generateOrGetPreKeys(creds, count);
|
|
114
122
|
const update = {
|
|
115
123
|
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
116
124
|
firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1)
|
|
117
125
|
};
|
|
118
126
|
await keys.set({ 'pre-key': newPreKeys });
|
|
119
|
-
const preKeys = await
|
|
127
|
+
const preKeys = await getPreKeys(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1]);
|
|
120
128
|
return { update, preKeys };
|
|
121
129
|
};
|
|
122
|
-
|
|
123
|
-
const getNextPreKeysNode = async (state, count) => {
|
|
130
|
+
export const getNextPreKeysNode = async (state, count) => {
|
|
124
131
|
const { creds } = state;
|
|
125
|
-
const { update, preKeys } = await
|
|
132
|
+
const { update, preKeys } = await getNextPreKeys(state, count);
|
|
126
133
|
const node = {
|
|
127
134
|
tag: 'iq',
|
|
128
135
|
attrs: {
|
|
129
136
|
xmlns: 'encrypt',
|
|
130
137
|
type: 'set',
|
|
131
|
-
to:
|
|
138
|
+
to: S_WHATSAPP_NET
|
|
132
139
|
},
|
|
133
140
|
content: [
|
|
134
|
-
{ tag: 'registration', attrs: {}, content:
|
|
135
|
-
{ tag: 'type', attrs: {}, content:
|
|
141
|
+
{ tag: 'registration', attrs: {}, content: encodeBigEndian(creds.registrationId) },
|
|
142
|
+
{ tag: 'type', attrs: {}, content: KEY_BUNDLE_TYPE },
|
|
136
143
|
{ tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
|
|
137
|
-
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k =>
|
|
138
|
-
|
|
144
|
+
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => xmppPreKey(preKeys[+k], +k)) },
|
|
145
|
+
xmppSignedPreKey(creds.signedPreKey)
|
|
139
146
|
]
|
|
140
147
|
};
|
|
141
148
|
return { update, node };
|
|
142
149
|
};
|
|
143
|
-
|
|
150
|
+
//# sourceMappingURL=signal.js.map
|
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
|
11
|
+
const fileLocks = new Map();
|
|
12
|
+
// Get or create a mutex for a specific file path
|
|
13
|
+
const getFileLock = (path) => {
|
|
14
|
+
let mutex = fileLocks.get(path);
|
|
15
|
+
if (!mutex) {
|
|
16
|
+
mutex = new Mutex();
|
|
17
|
+
fileLocks.set(path, mutex);
|
|
18
|
+
}
|
|
19
|
+
return mutex;
|
|
6
20
|
};
|
|
7
|
-
Object.defineProperty(exports, "__esModule", {
|
|
8
|
-
value: true
|
|
9
|
-
});
|
|
10
|
-
exports.useMultiFileAuthState = void 0;
|
|
11
|
-
const fs = require("fs");
|
|
12
|
-
const path_1 = require("path");
|
|
13
|
-
const WAProto_1 = require("../../WAProto");
|
|
14
|
-
const auth_utils_1 = require("./auth-utils");
|
|
15
|
-
const generics_1 = require("./generics");
|
|
16
21
|
/**
|
|
17
22
|
* stores the full authentication state in a single folder.
|
|
18
23
|
* Far more efficient than singlefileauthstate
|
|
@@ -20,74 +25,97 @@ const generics_1 = require("./generics");
|
|
|
20
25
|
* Again, I wouldn't endorse this for any production level use other than perhaps a bot.
|
|
21
26
|
* Would recommend writing an auth state for use with a proper SQL or No-SQL DB
|
|
22
27
|
* */
|
|
23
|
-
const useMultiFileAuthState = async (folder) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
28
|
+
export const useMultiFileAuthState = async (folder) => {
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
const writeData = async (data, file) => {
|
|
31
|
+
const filePath = await join(folder, fixFileName(file));
|
|
32
|
+
const mutex = await getFileLock(filePath);
|
|
33
|
+
return await mutex.acquire().then(async (release) => {
|
|
34
|
+
try {
|
|
35
|
+
await writeFile(filePath, JSON.stringify(data, BufferJSON.replacer));
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
await release();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
const readData = async (file) => {
|
|
43
|
+
try {
|
|
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
|
+
});
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const removeData = async (file) => {
|
|
61
|
+
try {
|
|
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
|
+
});
|
|
74
|
+
}
|
|
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`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
await mkdir(folder, { recursive: true });
|
|
85
|
+
}
|
|
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 ? await writeData(value, file) : await removeData(file));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
await Promise.all(tasks);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
saveCreds: async () => {
|
|
117
|
+
return await writeData(creds, 'creds.json');
|
|
118
|
+
}
|
|
119
|
+
};
|
|
92
120
|
};
|
|
93
|
-
|
|
121
|
+
//# sourceMappingURL=use-multi-file-auth-state.js.map
|