@vanzxy/baileys 1.4.0 → 1.4.2
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.
Potentially problematic release.
This version of @vanzxy/baileys might be problematic. Click here for more details.
- package/cjs/WAProto/index.js +106053 -0
- package/cjs/lib/Defaults/index.js +168 -0
- package/cjs/lib/Signal/Group/ciphertext-message.js +16 -0
- package/cjs/lib/Signal/Group/group-session-builder.js +67 -0
- package/cjs/lib/Signal/Group/group_cipher.js +86 -0
- package/cjs/lib/Signal/Group/index.js +58 -0
- package/cjs/lib/Signal/Group/keyhelper.js +56 -0
- package/cjs/lib/Signal/Group/sender-chain-key.js +30 -0
- package/cjs/lib/Signal/Group/sender-key-distribution-message.js +67 -0
- package/cjs/lib/Signal/Group/sender-key-message.js +70 -0
- package/cjs/lib/Signal/Group/sender-key-name.js +52 -0
- package/cjs/lib/Signal/Group/sender-key-record.js +45 -0
- package/cjs/lib/Signal/Group/sender-key-state.js +88 -0
- package/cjs/lib/Signal/Group/sender-message-key.js +30 -0
- package/cjs/lib/Signal/libsignal.js +467 -0
- package/cjs/lib/Signal/lid-mapping.js +281 -0
- package/cjs/lib/Socket/Client/index.js +19 -0
- package/cjs/lib/Socket/Client/types.js +15 -0
- package/cjs/lib/Socket/Client/websocket.js +61 -0
- package/cjs/lib/Socket/business.js +384 -0
- package/cjs/lib/Socket/chats.js +1257 -0
- package/cjs/lib/Socket/communities.js +442 -0
- package/cjs/lib/Socket/dugong.js +753 -0
- package/cjs/lib/Socket/groups.js +360 -0
- package/cjs/lib/Socket/index.js +19 -0
- package/cjs/lib/Socket/messages-recv.js +1790 -0
- package/cjs/lib/Socket/messages-send.js +1390 -0
- package/cjs/lib/Socket/mex.js +46 -0
- package/cjs/lib/Socket/newsletter.js +340 -0
- package/cjs/lib/Socket/socket.js +1007 -0
- package/cjs/lib/Store/index.js +20 -0
- package/cjs/lib/Store/make-in-memory-store.js +438 -0
- package/cjs/lib/Store/make-ordered-dictionary.js +82 -0
- package/cjs/lib/Store/object-repository.js +28 -0
- package/cjs/lib/Types/Auth.js +3 -0
- package/cjs/lib/Types/Bussines.js +3 -0
- package/cjs/lib/Types/Call.js +3 -0
- package/cjs/lib/Types/Chat.js +11 -0
- package/cjs/lib/Types/Contact.js +3 -0
- package/cjs/lib/Types/Events.js +4 -0
- package/cjs/lib/Types/GroupMetadata.js +3 -0
- package/cjs/lib/Types/Label.js +26 -0
- package/cjs/lib/Types/LabelAssociation.js +8 -0
- package/cjs/lib/Types/Message.js +19 -0
- package/cjs/lib/Types/Mex.js +40 -0
- package/cjs/lib/Types/Product.js +3 -0
- package/cjs/lib/Types/RichType.js +24 -0
- package/cjs/lib/Types/Signal.js +4 -0
- package/cjs/lib/Types/Socket.js +4 -0
- package/cjs/lib/Types/State.js +54 -0
- package/cjs/lib/Types/USync.js +4 -0
- package/cjs/lib/Types/index.js +42 -0
- package/cjs/lib/Utils/MessageBuilder.js +2164 -0
- package/cjs/lib/Utils/auth-utils.js +312 -0
- package/cjs/lib/Utils/browser-utils.js +32 -0
- package/cjs/lib/Utils/business.js +245 -0
- package/cjs/lib/Utils/chat-utils.js +937 -0
- package/cjs/lib/Utils/companion-reg-client-utils.js +45 -0
- package/cjs/lib/Utils/crypto.js +196 -0
- package/cjs/lib/Utils/decode-wa-message.js +324 -0
- package/cjs/lib/Utils/event-buffer.js +643 -0
- package/cjs/lib/Utils/generics.js +429 -0
- package/cjs/lib/Utils/history.js +151 -0
- package/cjs/lib/Utils/identity-change-handler.js +56 -0
- package/cjs/lib/Utils/index.js +42 -0
- package/cjs/lib/Utils/link-preview.js +122 -0
- package/cjs/lib/Utils/logger.js +8 -0
- package/cjs/lib/Utils/lt-hash.js +62 -0
- package/cjs/lib/Utils/make-mutex.js +38 -0
- package/cjs/lib/Utils/message-retry-manager.js +267 -0
- package/cjs/lib/Utils/messages-media.js +933 -0
- package/cjs/lib/Utils/messages.js +2175 -0
- package/cjs/lib/Utils/noise-handler.js +205 -0
- package/cjs/lib/Utils/offline-node-processor.js +43 -0
- package/cjs/lib/Utils/pre-key-manager.js +113 -0
- package/cjs/lib/Utils/process-message.js +772 -0
- package/cjs/lib/Utils/reporting-utils.js +263 -0
- package/cjs/lib/Utils/rich-message-utils.js +439 -0
- package/cjs/lib/Utils/signal.js +214 -0
- package/cjs/lib/Utils/stanza-ack.js +41 -0
- package/cjs/lib/Utils/sync-action-utils.js +54 -0
- package/cjs/lib/Utils/tc-token-utils.js +174 -0
- package/cjs/lib/Utils/use-multi-file-auth-state.js +125 -0
- package/cjs/lib/Utils/use-single-file-auth-state.js +121 -0
- package/cjs/lib/Utils/use-sqlite-auth-state.js +182 -0
- package/cjs/lib/Utils/validate-connection.js +210 -0
- package/cjs/lib/WABinary/constants.js +1470 -0
- package/cjs/lib/WABinary/decode.js +301 -0
- package/cjs/lib/WABinary/encode.js +257 -0
- package/cjs/lib/WABinary/generic-utils.js +274 -0
- package/cjs/lib/WABinary/index.js +22 -0
- package/cjs/lib/WABinary/jid-utils.js +114 -0
- package/cjs/lib/WABinary/types.js +37 -0
- package/cjs/lib/WAM/BinaryInfo.js +14 -0
- package/cjs/lib/WAM/constants.js +22856 -0
- package/cjs/lib/WAM/encode.js +154 -0
- package/cjs/lib/WAM/index.js +20 -0
- package/cjs/lib/WAUSync/Protocols/USyncContactProtocol.js +56 -0
- package/cjs/lib/WAUSync/Protocols/USyncDeviceProtocol.js +67 -0
- package/cjs/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
- package/cjs/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/cjs/lib/WAUSync/Protocols/USyncUsernameProtocol.js +29 -0
- package/cjs/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +55 -0
- package/cjs/lib/WAUSync/Protocols/UsyncLIDProtocol.js +33 -0
- package/cjs/lib/WAUSync/Protocols/index.js +24 -0
- package/cjs/lib/WAUSync/USyncQuery.js +126 -0
- package/cjs/lib/WAUSync/USyncUser.js +35 -0
- package/cjs/lib/WAUSync/index.js +20 -0
- package/cjs/lib/index.js +34 -0
- package/cjs/package.json +1 -0
- package/lib/Socket/messages-send.js +4 -3
- package/lib/Utils/messages.js +4 -1
- package/package.json +16 -5
- package/postinstall-banner.js +3 -2
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeNoiseHandler = void 0;
|
|
4
|
+
const boom_1 = require("@hapi/boom");
|
|
5
|
+
const index_js_1 = require("../../WAProto/index.js");
|
|
6
|
+
const index_js_2 = require("../Defaults/index.js");
|
|
7
|
+
const index_js_3 = require("../WABinary/index.js");
|
|
8
|
+
const crypto_js_1 = require("./crypto.js");
|
|
9
|
+
const IV_LENGTH = 12;
|
|
10
|
+
const EMPTY_BUFFER = Buffer.alloc(0);
|
|
11
|
+
const generateIV = (counter) => {
|
|
12
|
+
const iv = new ArrayBuffer(IV_LENGTH);
|
|
13
|
+
new DataView(iv).setUint32(8, counter);
|
|
14
|
+
return new Uint8Array(iv);
|
|
15
|
+
};
|
|
16
|
+
class TransportState {
|
|
17
|
+
constructor(encKey, decKey) {
|
|
18
|
+
this.encKey = encKey;
|
|
19
|
+
this.decKey = decKey;
|
|
20
|
+
this.readCounter = 0;
|
|
21
|
+
this.writeCounter = 0;
|
|
22
|
+
this.iv = new Uint8Array(IV_LENGTH);
|
|
23
|
+
}
|
|
24
|
+
encrypt(plaintext) {
|
|
25
|
+
const c = this.writeCounter++;
|
|
26
|
+
this.iv[8] = (c >>> 24) & 0xff;
|
|
27
|
+
this.iv[9] = (c >>> 16) & 0xff;
|
|
28
|
+
this.iv[10] = (c >>> 8) & 0xff;
|
|
29
|
+
this.iv[11] = c & 0xff;
|
|
30
|
+
return (0, crypto_js_1.aesEncryptGCM)(plaintext, this.encKey, this.iv, EMPTY_BUFFER);
|
|
31
|
+
}
|
|
32
|
+
decrypt(ciphertext) {
|
|
33
|
+
const c = this.readCounter++;
|
|
34
|
+
this.iv[8] = (c >>> 24) & 0xff;
|
|
35
|
+
this.iv[9] = (c >>> 16) & 0xff;
|
|
36
|
+
this.iv[10] = (c >>> 8) & 0xff;
|
|
37
|
+
this.iv[11] = c & 0xff;
|
|
38
|
+
return (0, crypto_js_1.aesDecryptGCM)(ciphertext, this.decKey, this.iv, EMPTY_BUFFER);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const makeNoiseHandler = ({ keyPair: { private: privateKey, public: publicKey }, NOISE_HEADER, logger, routingInfo }) => {
|
|
42
|
+
logger = logger.child({ class: 'ns' });
|
|
43
|
+
const data = Buffer.from(index_js_2.NOISE_MODE);
|
|
44
|
+
let hash = data.byteLength === 32 ? data : (0, crypto_js_1.sha256)(data);
|
|
45
|
+
let salt = hash;
|
|
46
|
+
let encKey = hash;
|
|
47
|
+
let decKey = hash;
|
|
48
|
+
let counter = 0;
|
|
49
|
+
let sentIntro = false;
|
|
50
|
+
let inBytes = Buffer.alloc(0);
|
|
51
|
+
let transport = null;
|
|
52
|
+
let isWaitingForTransport = false;
|
|
53
|
+
let pendingOnFrame = null;
|
|
54
|
+
let introHeader;
|
|
55
|
+
if (routingInfo) {
|
|
56
|
+
introHeader = Buffer.alloc(7 + routingInfo.byteLength + NOISE_HEADER.length);
|
|
57
|
+
introHeader.write('ED', 0, 'utf8');
|
|
58
|
+
introHeader.writeUint8(0, 2);
|
|
59
|
+
introHeader.writeUint8(1, 3);
|
|
60
|
+
introHeader.writeUint8(routingInfo.byteLength >> 16, 4);
|
|
61
|
+
introHeader.writeUint16BE(routingInfo.byteLength & 65535, 5);
|
|
62
|
+
introHeader.set(routingInfo, 7);
|
|
63
|
+
introHeader.set(NOISE_HEADER, 7 + routingInfo.byteLength);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
introHeader = Buffer.from(NOISE_HEADER);
|
|
67
|
+
}
|
|
68
|
+
const authenticate = (data) => {
|
|
69
|
+
if (!transport) {
|
|
70
|
+
hash = (0, crypto_js_1.sha256)(Buffer.concat([hash, data]));
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const encrypt = (plaintext) => {
|
|
74
|
+
if (transport) {
|
|
75
|
+
return transport.encrypt(plaintext);
|
|
76
|
+
}
|
|
77
|
+
const result = (0, crypto_js_1.aesEncryptGCM)(plaintext, encKey, generateIV(counter++), hash);
|
|
78
|
+
authenticate(result);
|
|
79
|
+
return result;
|
|
80
|
+
};
|
|
81
|
+
const decrypt = (ciphertext) => {
|
|
82
|
+
if (transport) {
|
|
83
|
+
return transport.decrypt(ciphertext);
|
|
84
|
+
}
|
|
85
|
+
const result = (0, crypto_js_1.aesDecryptGCM)(ciphertext, decKey, generateIV(counter++), hash);
|
|
86
|
+
authenticate(ciphertext);
|
|
87
|
+
return result;
|
|
88
|
+
};
|
|
89
|
+
const localHKDF = (data) => {
|
|
90
|
+
const key = (0, crypto_js_1.hkdf)(Buffer.from(data), 64, { salt, info: '' });
|
|
91
|
+
return [key.subarray(0, 32), key.subarray(32)];
|
|
92
|
+
};
|
|
93
|
+
const mixIntoKey = (data) => {
|
|
94
|
+
const [write, read] = localHKDF(data);
|
|
95
|
+
salt = write;
|
|
96
|
+
encKey = read;
|
|
97
|
+
decKey = read;
|
|
98
|
+
counter = 0;
|
|
99
|
+
};
|
|
100
|
+
const finishInit = async () => {
|
|
101
|
+
isWaitingForTransport = true;
|
|
102
|
+
const [write, read] = localHKDF(new Uint8Array(0));
|
|
103
|
+
transport = new TransportState(write, read);
|
|
104
|
+
isWaitingForTransport = false;
|
|
105
|
+
logger.trace('Noise handler transitioned to Transport state');
|
|
106
|
+
if (pendingOnFrame) {
|
|
107
|
+
logger.trace({ length: inBytes.length }, 'Flushing buffered frames after transport ready');
|
|
108
|
+
await processData(pendingOnFrame);
|
|
109
|
+
pendingOnFrame = null;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const processData = async (onFrame) => {
|
|
113
|
+
let size;
|
|
114
|
+
while (true) {
|
|
115
|
+
if (inBytes.length < 3)
|
|
116
|
+
return;
|
|
117
|
+
size = (inBytes[0] << 16) | (inBytes[1] << 8) | inBytes[2];
|
|
118
|
+
if (inBytes.length < size + 3)
|
|
119
|
+
return;
|
|
120
|
+
let frame = inBytes.subarray(3, size + 3);
|
|
121
|
+
inBytes = inBytes.subarray(size + 3);
|
|
122
|
+
if (transport) {
|
|
123
|
+
const result = transport.decrypt(frame);
|
|
124
|
+
frame = await (0, index_js_3.decodeBinaryNode)(result);
|
|
125
|
+
}
|
|
126
|
+
if (logger.level === 'trace') {
|
|
127
|
+
logger.trace({ msg: frame?.attrs?.id }, 'recv frame');
|
|
128
|
+
}
|
|
129
|
+
onFrame(frame);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
authenticate(NOISE_HEADER);
|
|
133
|
+
authenticate(publicKey);
|
|
134
|
+
return {
|
|
135
|
+
encrypt,
|
|
136
|
+
decrypt,
|
|
137
|
+
authenticate,
|
|
138
|
+
mixIntoKey,
|
|
139
|
+
finishInit,
|
|
140
|
+
processHandshake: ({ serverHello }, noiseKey) => {
|
|
141
|
+
authenticate(serverHello.ephemeral);
|
|
142
|
+
mixIntoKey(crypto_js_1.Curve.sharedKey(privateKey, serverHello.ephemeral));
|
|
143
|
+
const decStaticContent = decrypt(serverHello.static);
|
|
144
|
+
mixIntoKey(crypto_js_1.Curve.sharedKey(privateKey, decStaticContent));
|
|
145
|
+
const certDecoded = decrypt(serverHello.payload);
|
|
146
|
+
const { intermediate: certIntermediate, leaf } = index_js_1.proto.CertChain.decode(certDecoded);
|
|
147
|
+
// leaf
|
|
148
|
+
if (!leaf?.details || !leaf?.signature) {
|
|
149
|
+
throw new boom_1.Boom('invalid noise leaf certificate', { statusCode: 400 });
|
|
150
|
+
}
|
|
151
|
+
if (!certIntermediate?.details || !certIntermediate?.signature) {
|
|
152
|
+
throw new boom_1.Boom('invalid noise intermediate certificate', { statusCode: 400 });
|
|
153
|
+
}
|
|
154
|
+
const details = index_js_1.proto.CertChain.NoiseCertificate.Details.decode(certIntermediate.details);
|
|
155
|
+
const { issuerSerial } = details;
|
|
156
|
+
const verify = crypto_js_1.Curve.verify(details.key, leaf.details, leaf.signature);
|
|
157
|
+
const verifyIntermediate = crypto_js_1.Curve.verify(index_js_2.WA_CERT_DETAILS.PUBLIC_KEY, certIntermediate.details, certIntermediate.signature);
|
|
158
|
+
if (!verify) {
|
|
159
|
+
throw new boom_1.Boom('noise certificate signature invalid', { statusCode: 400 });
|
|
160
|
+
}
|
|
161
|
+
if (!verifyIntermediate) {
|
|
162
|
+
throw new boom_1.Boom('noise intermediate certificate signature invalid', { statusCode: 400 });
|
|
163
|
+
}
|
|
164
|
+
if (issuerSerial !== index_js_2.WA_CERT_DETAILS.SERIAL) {
|
|
165
|
+
throw new boom_1.Boom('certification match failed', { statusCode: 400 });
|
|
166
|
+
}
|
|
167
|
+
const keyEnc = encrypt(noiseKey.public);
|
|
168
|
+
mixIntoKey(crypto_js_1.Curve.sharedKey(noiseKey.private, serverHello.ephemeral));
|
|
169
|
+
return keyEnc;
|
|
170
|
+
},
|
|
171
|
+
encodeFrame: (data) => {
|
|
172
|
+
if (transport) {
|
|
173
|
+
data = transport.encrypt(data);
|
|
174
|
+
}
|
|
175
|
+
const dataLen = data.byteLength;
|
|
176
|
+
const introSize = sentIntro ? 0 : introHeader.length;
|
|
177
|
+
const frame = Buffer.allocUnsafe(introSize + 3 + dataLen);
|
|
178
|
+
if (!sentIntro) {
|
|
179
|
+
frame.set(introHeader);
|
|
180
|
+
sentIntro = true;
|
|
181
|
+
}
|
|
182
|
+
frame[introSize] = (dataLen >>> 16) & 0xff;
|
|
183
|
+
frame[introSize + 1] = (dataLen >>> 8) & 0xff;
|
|
184
|
+
frame[introSize + 2] = dataLen & 0xff;
|
|
185
|
+
frame.set(data, introSize + 3);
|
|
186
|
+
return frame;
|
|
187
|
+
},
|
|
188
|
+
decodeFrame: async (newData, onFrame) => {
|
|
189
|
+
if (isWaitingForTransport) {
|
|
190
|
+
inBytes = Buffer.concat([inBytes, newData]);
|
|
191
|
+
pendingOnFrame = onFrame;
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (inBytes.length === 0) {
|
|
195
|
+
inBytes = Buffer.from(newData);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
inBytes = Buffer.concat([inBytes, newData]);
|
|
199
|
+
}
|
|
200
|
+
await processData(onFrame);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
exports.makeNoiseHandler = makeNoiseHandler;
|
|
205
|
+
//# sourceMappingURL=noise-handler.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeOfflineNodeProcessor = makeOfflineNodeProcessor;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a processor for offline stanza nodes that:
|
|
6
|
+
* - Queues nodes for sequential processing
|
|
7
|
+
* - Yields to the event loop periodically to avoid blocking
|
|
8
|
+
* - Catches handler errors to prevent the processing loop from crashing
|
|
9
|
+
*/
|
|
10
|
+
function makeOfflineNodeProcessor(nodeProcessorMap, deps, batchSize = 10) {
|
|
11
|
+
const nodes = [];
|
|
12
|
+
let isProcessing = false;
|
|
13
|
+
const enqueue = (type, node) => {
|
|
14
|
+
nodes.push({ type, node });
|
|
15
|
+
if (isProcessing) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
isProcessing = true;
|
|
19
|
+
const promise = async () => {
|
|
20
|
+
let processedInBatch = 0;
|
|
21
|
+
while (nodes.length && deps.isWsOpen()) {
|
|
22
|
+
const { type, node } = nodes.shift();
|
|
23
|
+
const nodeProcessor = nodeProcessorMap.get(type);
|
|
24
|
+
if (!nodeProcessor) {
|
|
25
|
+
deps.onUnexpectedError(new Error(`unknown offline node type: ${type}`), 'processing offline node');
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
await nodeProcessor(node).catch(err => deps.onUnexpectedError(err, `processing offline ${type}`));
|
|
29
|
+
processedInBatch++;
|
|
30
|
+
// Yield to event loop after processing a batch
|
|
31
|
+
// This prevents blocking the event loop for too long when there are many offline nodes
|
|
32
|
+
if (processedInBatch >= batchSize) {
|
|
33
|
+
processedInBatch = 0;
|
|
34
|
+
await deps.yieldToEventLoop();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
isProcessing = false;
|
|
38
|
+
};
|
|
39
|
+
promise().catch(error => deps.onUnexpectedError(error, 'processing offline nodes'));
|
|
40
|
+
};
|
|
41
|
+
return { enqueue };
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=offline-node-processor.js.map
|
|
@@ -0,0 +1,113 @@
|
|
|
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.PreKeyManager = void 0;
|
|
7
|
+
const p_queue_1 = __importDefault(require("p-queue"));
|
|
8
|
+
/**
|
|
9
|
+
* Manages pre-key operations with proper concurrency control
|
|
10
|
+
*/
|
|
11
|
+
class PreKeyManager {
|
|
12
|
+
constructor(store, logger) {
|
|
13
|
+
this.store = store;
|
|
14
|
+
this.logger = logger;
|
|
15
|
+
this.queues = new Map();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Get or create a queue for a specific key type
|
|
19
|
+
*/
|
|
20
|
+
getQueue(keyType) {
|
|
21
|
+
if (!this.queues.has(keyType)) {
|
|
22
|
+
this.queues.set(keyType, new p_queue_1.default({ concurrency: 1 }));
|
|
23
|
+
}
|
|
24
|
+
return this.queues.get(keyType);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Process pre-key operations (updates and deletions)
|
|
28
|
+
*/
|
|
29
|
+
async processOperations(data, keyType, transactionCache, mutations, isInTransaction) {
|
|
30
|
+
const keyData = data[keyType];
|
|
31
|
+
if (!keyData)
|
|
32
|
+
return;
|
|
33
|
+
return this.getQueue(keyType).add(async () => {
|
|
34
|
+
// Ensure structures exist
|
|
35
|
+
transactionCache[keyType] = transactionCache[keyType] || {};
|
|
36
|
+
mutations[keyType] = mutations[keyType] || {};
|
|
37
|
+
// Separate deletions from updates
|
|
38
|
+
const deletions = [];
|
|
39
|
+
const updates = {};
|
|
40
|
+
for (const keyId in keyData) {
|
|
41
|
+
if (keyData[keyId] === null) {
|
|
42
|
+
deletions.push(keyId);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
updates[keyId] = keyData[keyId];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// Process updates (no validation needed)
|
|
49
|
+
if (Object.keys(updates).length > 0) {
|
|
50
|
+
Object.assign(transactionCache[keyType], updates);
|
|
51
|
+
Object.assign(mutations[keyType], updates);
|
|
52
|
+
}
|
|
53
|
+
// Process deletions with validation
|
|
54
|
+
if (deletions.length > 0) {
|
|
55
|
+
await this.processDeletions(keyType, deletions, transactionCache, mutations, isInTransaction);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Process deletions with validation
|
|
61
|
+
*/
|
|
62
|
+
async processDeletions(keyType, ids, transactionCache, mutations, isInTransaction) {
|
|
63
|
+
if (isInTransaction) {
|
|
64
|
+
// In transaction, only allow deletion if key exists in cache
|
|
65
|
+
for (const keyId of ids) {
|
|
66
|
+
if (transactionCache[keyType]?.[keyId]) {
|
|
67
|
+
transactionCache[keyType][keyId] = null;
|
|
68
|
+
mutations[keyType][keyId] = null;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
this.logger.warn(`Skipping deletion of non-existent ${keyType} in transaction: ${keyId}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
// Outside transaction, validate against store
|
|
77
|
+
const existingKeys = await this.store.get(keyType, ids);
|
|
78
|
+
for (const keyId of ids) {
|
|
79
|
+
if (existingKeys[keyId]) {
|
|
80
|
+
transactionCache[keyType][keyId] = null;
|
|
81
|
+
mutations[keyType][keyId] = null;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Validate and process pre-key deletions outside transactions
|
|
91
|
+
*/
|
|
92
|
+
async validateDeletions(data, keyType) {
|
|
93
|
+
const keyData = data[keyType];
|
|
94
|
+
if (!keyData)
|
|
95
|
+
return;
|
|
96
|
+
return this.getQueue(keyType).add(async () => {
|
|
97
|
+
// Find all deletion requests
|
|
98
|
+
const deletionIds = Object.keys(keyData).filter(id => keyData[id] === null);
|
|
99
|
+
if (deletionIds.length === 0)
|
|
100
|
+
return;
|
|
101
|
+
// Validate deletions
|
|
102
|
+
const existingKeys = await this.store.get(keyType, deletionIds);
|
|
103
|
+
for (const keyId of deletionIds) {
|
|
104
|
+
if (!existingKeys[keyId]) {
|
|
105
|
+
this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`);
|
|
106
|
+
delete data[keyType][keyId];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.PreKeyManager = PreKeyManager;
|
|
113
|
+
//# sourceMappingURL=pre-key-manager.js.map
|