@aseppxyzz12/baileys 1.1.8
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 +25 -0
- package/README.md +2579 -0
- package/WAProto/AICommon/AICommon.js +19396 -0
- package/WAProto/AICommon/AICommon.proto +820 -0
- package/WAProto/Adv/Adv.js +1137 -0
- package/WAProto/Adv/Adv.proto +42 -0
- package/WAProto/BotMetadata/BotMetadata.js +8975 -0
- package/WAProto/BotMetadata/BotMetadata.proto +484 -0
- package/WAProto/Cert/Cert.js +893 -0
- package/WAProto/Cert/Cert.proto +30 -0
- package/WAProto/ChatLockSettings/ChatLockSettings.js +673 -0
- package/WAProto/ChatLockSettings/ChatLockSettings.proto +9 -0
- package/WAProto/CompanionReg/CompanionReg.js +2457 -0
- package/WAProto/CompanionReg/CompanionReg.proto +103 -0
- package/WAProto/DeviceCapabilities/DeviceCapabilities.js +846 -0
- package/WAProto/DeviceCapabilities/DeviceCapabilities.proto +38 -0
- package/WAProto/E2E/E2E.js +78728 -0
- package/WAProto/E2E/E2E.proto +1970 -0
- package/WAProto/Ephemeral/Ephemeral.js +143 -0
- package/WAProto/Ephemeral/Ephemeral.proto +7 -0
- package/WAProto/HistorySync/HistorySync.js +95931 -0
- package/WAProto/HistorySync/HistorySync.proto +229 -0
- package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.js +414 -0
- package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.proto +13 -0
- package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.js +980 -0
- package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.proto +38 -0
- package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.js +81185 -0
- package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.proto +88 -0
- package/WAProto/MmsRetry/MmsRetry.js +310 -0
- package/WAProto/MmsRetry/MmsRetry.proto +19 -0
- package/WAProto/Protocol/Protocol.js +399 -0
- package/WAProto/Protocol/Protocol.proto +22 -0
- package/WAProto/Reporting/Reporting.js +535 -0
- package/WAProto/Reporting/Reporting.proto +22 -0
- package/WAProto/ServerSync/ServerSync.js +1830 -0
- package/WAProto/ServerSync/ServerSync.proto +70 -0
- package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.js +3267 -0
- package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.proto +95 -0
- package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.js +1140 -0
- package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.proto +44 -0
- package/WAProto/StatusAttributions/StatusAttributions.js +1604 -0
- package/WAProto/StatusAttributions/StatusAttributions.proto +89 -0
- package/WAProto/SyncAction/SyncAction.js +17424 -0
- package/WAProto/SyncAction/SyncAction.proto +663 -0
- package/WAProto/UserPassword/UserPassword.js +544 -0
- package/WAProto/UserPassword/UserPassword.proto +28 -0
- package/WAProto/VnameCert/VnameCert.js +1466 -0
- package/WAProto/VnameCert/VnameCert.proto +65 -0
- package/WAProto/Wa6/Wa6.js +4601 -0
- package/WAProto/Wa6/Wa6.proto +241 -0
- package/WAProto/Web/Web.js +90819 -0
- package/WAProto/Web/Web.proto +605 -0
- package/WAProto/index.js +30 -0
- package/engine-requirements.js +9 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/connection.js +39 -0
- package/lib/Defaults/constants.js +50 -0
- package/lib/Defaults/history.js +13 -0
- package/lib/Defaults/index.js +36 -0
- package/lib/Defaults/media.js +43 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Defaults/prefix.js +12 -0
- package/lib/Function/Download/tiktok.js +19 -0
- package/lib/Function/Tools/bypass.js +19 -0
- package/lib/Function/index.js +13 -0
- package/lib/Signal/Group/ciphertext-message.js +14 -0
- package/lib/Signal/Group/group-session-builder.js +46 -0
- package/lib/Signal/Group/group_cipher.js +104 -0
- package/lib/Signal/Group/index.js +42 -0
- package/lib/Signal/Group/keyhelper.js +21 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.js +34 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +78 -0
- package/lib/Signal/Group/sender-key-name.js +49 -0
- package/lib/Signal/Group/sender-key-record.js +45 -0
- package/lib/Signal/Group/sender-key-state.js +100 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +364 -0
- package/lib/Signal/lid-mapping.js +164 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.js +31 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/types.js +12 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/Client/websocket.js +67 -0
- package/lib/Socket/business.js +291 -0
- package/lib/Socket/chats.js +874 -0
- package/lib/Socket/community.js +454 -0
- package/lib/Socket/dugong.js +658 -0
- package/lib/Socket/groups.js +357 -0
- package/lib/Socket/index.js +13 -0
- package/lib/Socket/messages-recv.js +1509 -0
- package/lib/Socket/messages-send.js +1505 -0
- package/lib/Socket/mex.js +54 -0
- package/lib/Socket/newsletter.js +171 -0
- package/lib/Socket/registration.js +167 -0
- package/lib/Socket/rich-content.js +279 -0
- package/lib/Socket/socket.js +905 -0
- package/lib/Socket/username.js +157 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +37 -0
- package/lib/Store/keyed-db.js +114 -0
- package/lib/Store/make-cache-manager-store.js +77 -0
- package/lib/Store/make-in-memory-store.js +423 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.js +26 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +10 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +4 -0
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Mex.js +40 -0
- package/lib/Types/MexUpdates.js +15 -0
- package/lib/Types/Newsletter.js +32 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +9 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +60 -0
- package/lib/Utils/auth-utils.js +256 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/bot-toolkit.js +455 -0
- package/lib/Utils/browser-utils.js +29 -0
- package/lib/Utils/business.js +233 -0
- package/lib/Utils/chat-utils.js +904 -0
- package/lib/Utils/companion-reg-client-utils.js +41 -0
- package/lib/Utils/crypto.js +144 -0
- package/lib/Utils/decode-wa-message.js +305 -0
- package/lib/Utils/event-buffer.js +555 -0
- package/lib/Utils/generics.js +411 -0
- package/lib/Utils/history.js +100 -0
- package/lib/Utils/identity-change-handler.js +53 -0
- package/lib/Utils/index.js +57 -0
- package/lib/Utils/link-preview.js +76 -0
- package/lib/Utils/logger.js +4 -0
- package/lib/Utils/lt-hash.js +45 -0
- package/lib/Utils/make-mutex.js +33 -0
- package/lib/Utils/message-composer.js +289 -0
- package/lib/Utils/message-retry-manager.js +134 -0
- package/lib/Utils/messages-media.js +806 -0
- package/lib/Utils/messages.js +1908 -0
- package/lib/Utils/noise-handler.js +157 -0
- package/lib/Utils/offline-node-processor.js +42 -0
- package/lib/Utils/pre-key-manager.js +86 -0
- package/lib/Utils/process-message.js +814 -0
- package/lib/Utils/reporting-utils.js +264 -0
- package/lib/Utils/signal.js +183 -0
- package/lib/Utils/stanza-ack.js +36 -0
- package/lib/Utils/sync-action-utils.js +52 -0
- package/lib/Utils/tc-token-utils.js +167 -0
- package/lib/Utils/use-multi-file-auth-state.js +104 -0
- package/lib/Utils/validate-connection.js +229 -0
- package/lib/Utils/vialeys-event-stream.js +41 -0
- package/lib/WABinary/constants.js +1308 -0
- package/lib/WABinary/decode.js +233 -0
- package/lib/WABinary/encode.js +212 -0
- package/lib/WABinary/generic-utils.js +124 -0
- package/lib/WABinary/index.js +36 -0
- package/lib/WABinary/jid-utils.js +101 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +12 -0
- package/lib/WAM/constants.js +20491 -0
- package/lib/WAM/encode.js +148 -0
- package/lib/WAM/index.js +34 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +62 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +25 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +38 -0
- package/lib/WAUSync/USyncQuery.js +97 -0
- package/lib/WAUSync/USyncUser.js +33 -0
- package/lib/WAUSync/index.js +34 -0
- package/lib/index.js +113 -0
- package/lib/messages-send.js +1496 -0
- package/package.json +100 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const XWAPaths = {
|
|
4
|
+
CREATE: "xwa2_newsletter_create",
|
|
5
|
+
SUBSCRIBERS: "xwa2_newsletter_subscribers",
|
|
6
|
+
VIEW: "xwa2_newsletter_view",
|
|
7
|
+
METADATA: "xwa2_newsletter",
|
|
8
|
+
UPDATE: "xwa2_newsletter_update",
|
|
9
|
+
ADMIN_COUNT: "xwa2_newsletter_admin",
|
|
10
|
+
MUTE_V2: "xwa2_newsletter_mute_v2",
|
|
11
|
+
UNMUTE_V2: "xwa2_newsletter_unmute_v2",
|
|
12
|
+
FOLLOW: "xwa2_newsletter_follow",
|
|
13
|
+
UNFOLLOW: "xwa2_newsletter_unfollow",
|
|
14
|
+
CHANGE_OWNER: "xwa2_newsletter_change_owner",
|
|
15
|
+
DEMOTE: "xwa2_newsletter_demote",
|
|
16
|
+
DELETE_V2: "xwa2_newsletter_delete_v2",
|
|
17
|
+
};
|
|
18
|
+
const QueryIds = {
|
|
19
|
+
CREATE: "8823471724422422",
|
|
20
|
+
UPDATE_METADATA: "24250201037901610",
|
|
21
|
+
METADATA: "6563316087068696",
|
|
22
|
+
SUBSCRIBERS: "9783111038412085",
|
|
23
|
+
FOLLOW: "7871414976211147",
|
|
24
|
+
UNFOLLOW: "7238632346214362",
|
|
25
|
+
MUTE: "29766401636284406",
|
|
26
|
+
UNMUTE: "9864994326891137",
|
|
27
|
+
ADMIN_COUNT: "7130823597031706",
|
|
28
|
+
CHANGE_OWNER: "7341777602580933",
|
|
29
|
+
DEMOTE: "6551828931592903",
|
|
30
|
+
DELETE: "30062808666639665",
|
|
31
|
+
};
|
|
32
|
+
module.exports = { XWAPaths: XWAPaths, QueryIds: QueryIds };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
let desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () {
|
|
12
|
+
return m[k];
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(o, k2, desc);
|
|
17
|
+
}
|
|
18
|
+
: function (o, m, k, k2) {
|
|
19
|
+
if (k2 === undefined) k2 = k;
|
|
20
|
+
o[k2] = m[k];
|
|
21
|
+
});
|
|
22
|
+
const __exportStar =
|
|
23
|
+
(this && this.__exportStar) ||
|
|
24
|
+
function (m, exports) {
|
|
25
|
+
for (var p in m) {
|
|
26
|
+
if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) {
|
|
27
|
+
__createBinding(exports, m, p);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.DisconnectReason = null;
|
|
33
|
+
__exportStar(require("./Auth"), exports);
|
|
34
|
+
__exportStar(require("./Bussines"), exports);
|
|
35
|
+
__exportStar(require("./Chat"), exports);
|
|
36
|
+
__exportStar(require("./Contact"), exports);
|
|
37
|
+
__exportStar(require("./GroupMetadata"), exports);
|
|
38
|
+
__exportStar(require("./State"), exports);
|
|
39
|
+
__exportStar(require("./MexUpdates"), exports);
|
|
40
|
+
__exportStar(require("./Message"), exports);
|
|
41
|
+
__exportStar(require("./Newsletter"), exports);
|
|
42
|
+
__exportStar(require("./Mex"), exports);
|
|
43
|
+
__exportStar(require("./Socket"), exports);
|
|
44
|
+
__exportStar(require("./Events"), exports);
|
|
45
|
+
__exportStar(require("./Product"), exports);
|
|
46
|
+
__exportStar(require("./Call"), exports);
|
|
47
|
+
__exportStar(require("./Signal"), exports);
|
|
48
|
+
const DisconnectReason = {
|
|
49
|
+
connectionClosed: 428,
|
|
50
|
+
connectionLost: 408,
|
|
51
|
+
connectionReplaced: 440,
|
|
52
|
+
timedOut: 408,
|
|
53
|
+
loggedOut: 401,
|
|
54
|
+
badSession: 500,
|
|
55
|
+
restartRequired: 515,
|
|
56
|
+
multideviceMismatch: 411,
|
|
57
|
+
forbidden: 403,
|
|
58
|
+
unavailableService: 503,
|
|
59
|
+
};
|
|
60
|
+
exports.DisconnectReason = DisconnectReason;
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const { randomBytes: randomBytes } = require("crypto");
|
|
4
|
+
const { default: PQueue } = require("p-queue");
|
|
5
|
+
const { default: NodeCache } = require("@cacheable/node-cache");
|
|
6
|
+
const { DEFAULT_CACHE_TTLS: DEFAULT_CACHE_TTLS } = require("../Defaults/constants");
|
|
7
|
+
const { AsyncLocalStorage: AsyncLocalStorage } = require("async_hooks");
|
|
8
|
+
const { LRUCache: LRUCache } = require("lru-cache");
|
|
9
|
+
const { Mutex: Mutex } = require("async-mutex");
|
|
10
|
+
const { Curve: Curve, signedKeyPair: signedKeyPair } = require("./crypto");
|
|
11
|
+
const { delay: delay, generateRegistrationId: generateRegistrationId } = require("./generics");
|
|
12
|
+
const { PreKeyManager: PreKeyManager } = require("./pre-key-manager");
|
|
13
|
+
function makeCacheableSignalKeyStore(store, logger, _cache) {
|
|
14
|
+
const cache =
|
|
15
|
+
_cache ||
|
|
16
|
+
new NodeCache({
|
|
17
|
+
stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE,
|
|
18
|
+
useClones: false,
|
|
19
|
+
deleteOnExpire: true,
|
|
20
|
+
});
|
|
21
|
+
const cacheMutex = new Mutex();
|
|
22
|
+
function getUniqueId(type, id) {
|
|
23
|
+
return `${type}.${id}`;
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
async get(type, ids) {
|
|
27
|
+
return cacheMutex.runExclusive(async () => {
|
|
28
|
+
const data = {};
|
|
29
|
+
const idsToFetch = [];
|
|
30
|
+
for (const id of ids) {
|
|
31
|
+
const item = cache.get(getUniqueId(type, id));
|
|
32
|
+
if (typeof item !== "undefined") {
|
|
33
|
+
data[id] = item;
|
|
34
|
+
} else {
|
|
35
|
+
idsToFetch.push(id);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (idsToFetch.length) {
|
|
39
|
+
logger?.trace({ items: idsToFetch.length }, "loading from store");
|
|
40
|
+
const fetched = await store.get(type, idsToFetch);
|
|
41
|
+
for (const id of idsToFetch) {
|
|
42
|
+
const item = fetched[id];
|
|
43
|
+
if (item) {
|
|
44
|
+
data[id] = item;
|
|
45
|
+
cache.set(getUniqueId(type, id), item);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return data;
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
async set(data) {
|
|
53
|
+
return cacheMutex.runExclusive(async () => {
|
|
54
|
+
let keys = 0;
|
|
55
|
+
for (const type in data) {
|
|
56
|
+
for (const id in data[type]) {
|
|
57
|
+
await cache.set(getUniqueId(type, id), data[type][id]);
|
|
58
|
+
keys += 1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
logger?.trace({ keys: keys }, "updated cache");
|
|
62
|
+
await store.set(data);
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
async clear() {
|
|
66
|
+
await cache.flushAll();
|
|
67
|
+
await store.clear?.call(store);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const addTransactionCapability = (
|
|
72
|
+
state,
|
|
73
|
+
logger,
|
|
74
|
+
{ maxCommitRetries: maxCommitRetries, delayBetweenTriesMs: delayBetweenTriesMs }
|
|
75
|
+
) => {
|
|
76
|
+
const txStorage = new AsyncLocalStorage();
|
|
77
|
+
const keyQueues = new Map();
|
|
78
|
+
const txMutexes = new Map();
|
|
79
|
+
const txMutexRefCounts = new Map();
|
|
80
|
+
const preKeyManager = new PreKeyManager(state, logger);
|
|
81
|
+
function getQueue(key) {
|
|
82
|
+
if (!keyQueues.has(key)) {
|
|
83
|
+
keyQueues.set(key, new PQueue({ concurrency: 1 }));
|
|
84
|
+
}
|
|
85
|
+
return keyQueues.get(key);
|
|
86
|
+
}
|
|
87
|
+
function getTxMutex(key) {
|
|
88
|
+
if (!txMutexes.has(key)) {
|
|
89
|
+
txMutexes.set(key, new Mutex());
|
|
90
|
+
txMutexRefCounts.set(key, 0);
|
|
91
|
+
}
|
|
92
|
+
return txMutexes.get(key);
|
|
93
|
+
}
|
|
94
|
+
function acquireTxMutexRef(key) {
|
|
95
|
+
const count = txMutexRefCounts.get(key) ?? 0;
|
|
96
|
+
txMutexRefCounts.set(key, count + 1);
|
|
97
|
+
}
|
|
98
|
+
function releaseTxMutexRef(key) {
|
|
99
|
+
const count = (txMutexRefCounts.get(key) ?? 1) - 1;
|
|
100
|
+
txMutexRefCounts.set(key, count);
|
|
101
|
+
if (count <= 0) {
|
|
102
|
+
const mutex = txMutexes.get(key);
|
|
103
|
+
if (mutex && !mutex.isLocked()) {
|
|
104
|
+
txMutexes.delete(key);
|
|
105
|
+
txMutexRefCounts.delete(key);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function isInTransaction() {
|
|
110
|
+
return !!txStorage.getStore();
|
|
111
|
+
}
|
|
112
|
+
async function commitWithRetry(mutations) {
|
|
113
|
+
if (Object.keys(mutations).length === 0) {
|
|
114
|
+
logger.trace("no mutations in transaction");
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
logger.trace("committing transaction");
|
|
118
|
+
for (let attempt = 0; attempt < maxCommitRetries; attempt++) {
|
|
119
|
+
try {
|
|
120
|
+
await state.set(mutations);
|
|
121
|
+
logger.trace(
|
|
122
|
+
{ mutationCount: Object.keys(mutations).length },
|
|
123
|
+
"committed transaction"
|
|
124
|
+
);
|
|
125
|
+
return;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
const retriesLeft = maxCommitRetries - attempt - 1;
|
|
128
|
+
logger.warn(`failed to commit mutations, retries left=${retriesLeft}`);
|
|
129
|
+
if (retriesLeft === 0) {
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
await delay(delayBetweenTriesMs);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
get: async (type, ids) => {
|
|
138
|
+
const ctx = txStorage.getStore();
|
|
139
|
+
if (!ctx) {
|
|
140
|
+
return state.get(type, ids);
|
|
141
|
+
}
|
|
142
|
+
const cached = ctx.cache[type] || {};
|
|
143
|
+
const missing = ids.filter((id) => !(id in cached));
|
|
144
|
+
if (missing.length > 0) {
|
|
145
|
+
ctx.dbQueries++;
|
|
146
|
+
logger.trace(
|
|
147
|
+
{ type: type, count: missing.length },
|
|
148
|
+
"fetching missing keys in transaction"
|
|
149
|
+
);
|
|
150
|
+
const fetched = await getTxMutex(type).runExclusive(() => state.get(type, missing));
|
|
151
|
+
ctx.cache[type] = ctx.cache[type] || {};
|
|
152
|
+
Object.assign(ctx.cache[type], fetched);
|
|
153
|
+
}
|
|
154
|
+
const result = {};
|
|
155
|
+
for (const id of ids) {
|
|
156
|
+
const value = ctx.cache[type]?.[id];
|
|
157
|
+
if (value !== undefined && value !== null) {
|
|
158
|
+
result[id] = value;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
},
|
|
163
|
+
set: async (data) => {
|
|
164
|
+
const ctx = txStorage.getStore();
|
|
165
|
+
if (!ctx) {
|
|
166
|
+
const types = Object.keys(data);
|
|
167
|
+
for (const type_ of types) {
|
|
168
|
+
const type = type_;
|
|
169
|
+
if (type === "pre-key") {
|
|
170
|
+
await preKeyManager.validateDeletions(data, type);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
await Promise.all(
|
|
174
|
+
types.map((type) =>
|
|
175
|
+
getQueue(type).add(async () => {
|
|
176
|
+
const typeData = { [type]: data[type] };
|
|
177
|
+
await state.set(typeData);
|
|
178
|
+
})
|
|
179
|
+
)
|
|
180
|
+
);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
logger.trace({ types: Object.keys(data) }, "caching in transaction");
|
|
184
|
+
for (const key_ in data) {
|
|
185
|
+
const key = key_;
|
|
186
|
+
ctx.cache[key] = ctx.cache[key] || {};
|
|
187
|
+
ctx.mutations[key] = ctx.mutations[key] || {};
|
|
188
|
+
if (key === "pre-key") {
|
|
189
|
+
await preKeyManager.processOperations(
|
|
190
|
+
data,
|
|
191
|
+
key,
|
|
192
|
+
ctx.cache,
|
|
193
|
+
ctx.mutations,
|
|
194
|
+
true
|
|
195
|
+
);
|
|
196
|
+
} else {
|
|
197
|
+
Object.assign(ctx.cache[key], data[key]);
|
|
198
|
+
Object.assign(ctx.mutations[key], data[key]);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
isInTransaction: isInTransaction,
|
|
203
|
+
transaction: async (work, key) => {
|
|
204
|
+
const existing = txStorage.getStore();
|
|
205
|
+
if (existing) {
|
|
206
|
+
logger.trace("reusing existing transaction context");
|
|
207
|
+
return work();
|
|
208
|
+
}
|
|
209
|
+
const mutex = getTxMutex(key);
|
|
210
|
+
acquireTxMutexRef(key);
|
|
211
|
+
try {
|
|
212
|
+
return await mutex.runExclusive(async () => {
|
|
213
|
+
const ctx = { cache: {}, mutations: {}, dbQueries: 0 };
|
|
214
|
+
logger.trace("entering transaction");
|
|
215
|
+
try {
|
|
216
|
+
const result = await txStorage.run(ctx, work);
|
|
217
|
+
await commitWithRetry(ctx.mutations);
|
|
218
|
+
logger.trace({ dbQueries: ctx.dbQueries }, "transaction completed");
|
|
219
|
+
return result;
|
|
220
|
+
} catch (error) {
|
|
221
|
+
logger.error({ error: error }, "transaction failed, rolling back");
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
} finally {
|
|
226
|
+
releaseTxMutexRef(key);
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
const initAuthCreds = () => {
|
|
232
|
+
const identityKey = Curve.generateKeyPair();
|
|
233
|
+
return {
|
|
234
|
+
noiseKey: Curve.generateKeyPair(),
|
|
235
|
+
pairingEphemeralKeyPair: Curve.generateKeyPair(),
|
|
236
|
+
signedIdentityKey: identityKey,
|
|
237
|
+
signedPreKey: signedKeyPair(identityKey, 1),
|
|
238
|
+
registrationId: generateRegistrationId(),
|
|
239
|
+
advSecretKey: randomBytes(32).toString("base64"),
|
|
240
|
+
processedHistoryMessages: [],
|
|
241
|
+
nextPreKeyId: 1,
|
|
242
|
+
firstUnuploadedPreKeyId: 1,
|
|
243
|
+
accountSyncCounter: 0,
|
|
244
|
+
accountSettings: { unarchiveChats: false },
|
|
245
|
+
registered: false,
|
|
246
|
+
pairingCode: undefined,
|
|
247
|
+
lastPropHash: undefined,
|
|
248
|
+
routingInfo: undefined,
|
|
249
|
+
additionalData: undefined,
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
module.exports = {
|
|
253
|
+
makeCacheableSignalKeyStore: makeCacheableSignalKeyStore,
|
|
254
|
+
addTransactionCapability: addTransactionCapability,
|
|
255
|
+
initAuthCreds: initAuthCreds,
|
|
256
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.readAndEmitEventStream = exports.captureEventStream = void 0;
|
|
7
|
+
const events_1 = __importDefault(require("events"));
|
|
8
|
+
const fs_1 = require("fs");
|
|
9
|
+
const promises_1 = require("fs/promises");
|
|
10
|
+
const readline_1 = require("readline");
|
|
11
|
+
const generics_1 = require("./generics");
|
|
12
|
+
const make_mutex_1 = require("./make-mutex");
|
|
13
|
+
/**
|
|
14
|
+
* Captures events from a baileys event emitter & stores them in a file
|
|
15
|
+
* @param ev The event emitter to read events from
|
|
16
|
+
* @param filename File to save to
|
|
17
|
+
*/
|
|
18
|
+
const captureEventStream = (ev, filename) => {
|
|
19
|
+
const oldEmit = ev.emit;
|
|
20
|
+
// write mutex so data is appended in order
|
|
21
|
+
const writeMutex = (0, make_mutex_1.makeMutex)();
|
|
22
|
+
// monkey patch eventemitter to capture all events
|
|
23
|
+
ev.emit = function (...args) {
|
|
24
|
+
const content = JSON.stringify({ timestamp: Date.now(), event: args[0], data: args[1] }) + '\n';
|
|
25
|
+
const result = oldEmit.apply(ev, args);
|
|
26
|
+
writeMutex.mutex(async () => {
|
|
27
|
+
await (0, promises_1.writeFile)(filename, content, { flag: 'a' });
|
|
28
|
+
});
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
exports.captureEventStream = captureEventStream;
|
|
33
|
+
/**
|
|
34
|
+
* Read event file and emit events from there
|
|
35
|
+
* @param filename filename containing event data
|
|
36
|
+
* @param delayIntervalMs delay between each event emit
|
|
37
|
+
*/
|
|
38
|
+
const readAndEmitEventStream = (filename, delayIntervalMs = 0) => {
|
|
39
|
+
const ev = new events_1.default();
|
|
40
|
+
const fireEvents = async () => {
|
|
41
|
+
// from: https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js
|
|
42
|
+
const fileStream = (0, fs_1.createReadStream)(filename);
|
|
43
|
+
const rl = (0, readline_1.createInterface)({
|
|
44
|
+
input: fileStream,
|
|
45
|
+
crlfDelay: Infinity
|
|
46
|
+
});
|
|
47
|
+
// Note: we use the crlfDelay option to recognize all instances of CR LF
|
|
48
|
+
// ('\r\n') in input.txt as a single line break.
|
|
49
|
+
for await (const line of rl) {
|
|
50
|
+
if (line) {
|
|
51
|
+
const { event, data } = JSON.parse(line);
|
|
52
|
+
ev.emit(event, data);
|
|
53
|
+
delayIntervalMs && await (0, generics_1.delay)(delayIntervalMs);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
fileStream.close();
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
ev,
|
|
60
|
+
task: fireEvents()
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
exports.readAndEmitEventStream = readAndEmitEventStream;
|