@oxidezap/baileyrs 0.2.1 → 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
@@ -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
- if (jid === 'status@broadcast' && options?.statusJidList?.length) {
75
- msgId = await client.sendStatusMessageBytes(msgBytes, options.statusJidList);
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oxidezap/baileyrs",
3
3
  "type": "module",
4
- "version": "0.2.1",
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.12.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"