@oxidezap/baileyrs 0.2.0 → 0.2.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.
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rejection Baileys code expects, or the original error untouched.
|
|
3
|
+
*
|
|
4
|
+
* Narrow on purpose: only the engine's own `no-recipient-device` is rewritten,
|
|
5
|
+
* so every other failure still reaches the caller with the shape the bridge
|
|
6
|
+
* gave it.
|
|
7
|
+
*/
|
|
8
|
+
export declare const asAllEncryptionsFailed: (error: unknown) => unknown;
|
|
9
|
+
/** Run a send, translating only that one rejection. */
|
|
10
|
+
export declare const sendReportingUpstreamFailure: <T>(send: () => Promise<T>) => Promise<T>;
|
|
11
|
+
//# sourceMappingURL=all-encryptions-failed.d.ts.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Boom } from '../Utils/boom.js';
|
|
2
|
+
/**
|
|
3
|
+
* A send that produced no ciphertext for its recipient.
|
|
4
|
+
*
|
|
5
|
+
* Upstream Baileys drops a device it cannot encrypt for and, when that leaves
|
|
6
|
+
* nothing, throws `Boom('All encryptions failed', { statusCode: 500 })`
|
|
7
|
+
* (`Socket/messages-send.ts`, in `createParticipantNodes`). The engine reached
|
|
8
|
+
* the same conclusion later: until `whatsapp-rust-bridge` 0.13.0 it returned a
|
|
9
|
+
* message id for a send that was never transmitted, and it now rejects with
|
|
10
|
+
* `no-recipient-device` instead.
|
|
11
|
+
*
|
|
12
|
+
* Same condition, different spelling, so it is translated here rather than
|
|
13
|
+
* left for the caller: Baileys code that branches on `statusCode === 500` or
|
|
14
|
+
* matches the message keeps working, and `attempted` rides along as extra
|
|
15
|
+
* data upstream has no way to carry.
|
|
16
|
+
*/
|
|
17
|
+
const NO_RECIPIENT_DEVICE = 'no-recipient-device';
|
|
18
|
+
/** How many recipient devices the engine tried, when it said. */
|
|
19
|
+
const attemptedFrom = (error) => {
|
|
20
|
+
const { attempted } = error;
|
|
21
|
+
return typeof attempted === 'number' ? attempted : undefined;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The rejection Baileys code expects, or the original error untouched.
|
|
25
|
+
*
|
|
26
|
+
* Narrow on purpose: only the engine's own `no-recipient-device` is rewritten,
|
|
27
|
+
* so every other failure still reaches the caller with the shape the bridge
|
|
28
|
+
* gave it.
|
|
29
|
+
*/
|
|
30
|
+
export const asAllEncryptionsFailed = (error) => {
|
|
31
|
+
if (typeof error !== 'object' || error === null)
|
|
32
|
+
return error;
|
|
33
|
+
if (error.kind !== NO_RECIPIENT_DEVICE)
|
|
34
|
+
return error;
|
|
35
|
+
return new Boom('All encryptions failed', {
|
|
36
|
+
statusCode: 500,
|
|
37
|
+
data: { attempted: attemptedFrom(error) }
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
/** Run a send, translating only that one rejection. */
|
|
41
|
+
export const sendReportingUpstreamFailure = async (send) => {
|
|
42
|
+
try {
|
|
43
|
+
return await send();
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw asAllEncryptionsFailed(error);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=all-encryptions-failed.js.map
|
package/lib/Socket/messages.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sendReportingUpstreamFailure } from '../Compatibility/all-encryptions-failed.js';
|
|
1
2
|
import { sendDroppingDerivedNodes } from '../Compatibility/derived-stanza-nodes.js';
|
|
2
3
|
import { encodeProtoCompat } from '../Compatibility/encode-proto.js';
|
|
3
4
|
import { planMessageRelay } from '../Compatibility/message-relay.js';
|
|
@@ -69,14 +70,10 @@ export const makeMessageMethods = (ctx) => ({
|
|
|
69
70
|
return fullMsg;
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
|
-
let msgId;
|
|
73
73
|
const msgBytes = encodeProtoCompat('Message', msg);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
else {
|
|
78
|
-
msgId = await client.sendMessageBytes(jid, msgBytes);
|
|
79
|
-
}
|
|
74
|
+
const msgId = await sendReportingUpstreamFailure(() => jid === 'status@broadcast' && options?.statusJidList?.length
|
|
75
|
+
? client.sendStatusMessageBytes(msgBytes, options.statusJidList)
|
|
76
|
+
: client.sendMessageBytes(jid, msgBytes));
|
|
80
77
|
fullMsg.key.id = msgId || fullMsg.key.id;
|
|
81
78
|
// Local echo of the message we just sent. Suppressed when
|
|
82
79
|
// `emitOwnEvents=false` so callers that explicitly opted out of seeing
|
|
@@ -150,9 +147,9 @@ export const makeMessageMethods = (ctx) => ({
|
|
|
150
147
|
// either way.
|
|
151
148
|
const drop = (tag) => ctx.logger.debug({ jid, messageId: plan.messageId, tag }, 'dropped an additionalNodes entry the engine derives from the message');
|
|
152
149
|
if (plan.kind === 'status') {
|
|
153
|
-
return sendDroppingDerivedNodes(plan.nodes, nodes => client.sendStatusMessageBytesWithOptions(bytes, plan.recipients, plan.messageId, nodes, plan.refreshDevices), drop);
|
|
150
|
+
return sendReportingUpstreamFailure(() => sendDroppingDerivedNodes(plan.nodes, nodes => client.sendStatusMessageBytesWithOptions(bytes, plan.recipients, plan.messageId, nodes, plan.refreshDevices), drop));
|
|
154
151
|
}
|
|
155
|
-
return sendDroppingDerivedNodes(plan.nodes, nodes => client.relayMessageBytesWithOptions(jid, bytes, plan.messageId, nodes, plan.refreshGroupMetadata, plan.refreshDevices), drop);
|
|
152
|
+
return sendReportingUpstreamFailure(() => sendDroppingDerivedNodes(plan.nodes, nodes => client.relayMessageBytesWithOptions(jid, bytes, plan.messageId, nodes, plan.refreshGroupMetadata, plan.refreshDevices), drop));
|
|
156
153
|
},
|
|
157
154
|
readMessages: async (keys) => {
|
|
158
155
|
const receiptKeys = receiptMessageKeys(keys);
|
package/lib/Socket/transport.js
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* `WebSocket.send()` takes a `BufferSource`, which is `ArrayBufferView<ArrayBuffer>
|
|
3
|
+
* | ArrayBuffer` — a `SharedArrayBuffer`-backed view is outside that contract.
|
|
4
|
+
* The bridge hands us the wider `Uint8Array<ArrayBufferLike>`, so the backing
|
|
5
|
+
* buffer has to be discriminated at runtime; this predicate is what carries that
|
|
6
|
+
* runtime check into the type system.
|
|
7
|
+
*/
|
|
8
|
+
const isArrayBufferBacked = (data) => data.buffer instanceof ArrayBuffer;
|
|
9
|
+
/**
|
|
10
|
+
* Narrows an outgoing frame to what `WebSocket.send()` accepts.
|
|
11
|
+
*
|
|
12
|
+
* The `ArrayBuffer` case returns the caller's view as-is. Rebuilding it — the
|
|
13
|
+
* `new Uint8Array(data.buffer, data.byteOffset, data.byteLength)` this used to
|
|
14
|
+
* do — produced a view over the same buffer at the same offset and length: an
|
|
15
|
+
* allocation per outgoing frame that existed only to restate the type. It also
|
|
16
|
+
* did not detach the view from WASM memory growth, so returning the original
|
|
17
|
+
* gives the payload exactly the lifetime it had before; both are aliases of the
|
|
18
|
+
* same linear memory, consumed synchronously by the `ws.send()` on the next line.
|
|
19
|
+
*
|
|
20
|
+
* The `SharedArrayBuffer` case still copies: that one is not a type formality,
|
|
21
|
+
* the send API genuinely cannot take a view over shared memory.
|
|
22
|
+
*/
|
|
23
|
+
const asWebSocketPayload = (data) => isArrayBufferBacked(data) ? data : new Uint8Array(data);
|
|
4
24
|
export const makeTransport = (config) => {
|
|
5
25
|
const { waWebSocketUrl, logger } = config;
|
|
6
26
|
let ws;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oxidezap/baileyrs",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.2",
|
|
5
5
|
"description": "A Rust-powered WhatsApp Web library for JavaScript, with a Baileys-compatible API",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"whatsapp",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
},
|
|
83
83
|
"dependencies": {
|
|
84
84
|
"@hapi/boom": "^9.1.4",
|
|
85
|
-
"@oxidezap/whatsapp-rust-bridge": "0.
|
|
85
|
+
"@oxidezap/whatsapp-rust-bridge": "0.13.0",
|
|
86
86
|
"long": "^5.3.2",
|
|
87
87
|
"pino": "^10.3.1",
|
|
88
88
|
"protobufjs": "^7.6.5"
|