@oxidezap/baileyrs 0.3.6 → 0.3.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.
Files changed (130) hide show
  1. package/README.md +67 -8
  2. package/lib/Bridge/history-sync-wire.d.ts +2 -2
  3. package/lib/Bridge/history-sync-wire.js +4 -4
  4. package/lib/Bridge/schema.js +7 -1
  5. package/lib/Bridge/types.d.ts +7 -1
  6. package/lib/Compatibility/auth-state.d.ts +30 -0
  7. package/lib/Compatibility/auth-state.js +96 -0
  8. package/lib/Compatibility/encode-proto-core.d.ts +31 -0
  9. package/lib/Compatibility/encode-proto-core.js +51 -0
  10. package/lib/Compatibility/encode-proto.d.ts +0 -19
  11. package/lib/Compatibility/encode-proto.js +3 -35
  12. package/lib/Compatibility/host-auth-state.d.ts +6 -0
  13. package/lib/Compatibility/host-auth-state.js +162 -0
  14. package/lib/Compatibility/internal/auth-state.d.ts +3 -7
  15. package/lib/Compatibility/internal/auth-state.js +34 -1
  16. package/lib/Compatibility/internal/host-auth-state.d.ts +4 -0
  17. package/lib/Compatibility/internal/host-auth-state.js +14 -0
  18. package/lib/Compatibility/legacy-store/device.js +5 -5
  19. package/lib/Compatibility/message-ids.d.ts +17 -0
  20. package/lib/Compatibility/message-ids.js +47 -0
  21. package/lib/Compatibility/message-relay.d.ts +2 -2
  22. package/lib/Compatibility/message-relay.js +4 -4
  23. package/lib/Compatibility/proto-runtime.d.ts +5 -0
  24. package/lib/Compatibility/proto-runtime.js +214 -32
  25. package/lib/Compatibility/socket-results.d.ts +1 -1
  26. package/lib/Compatibility/tagged-message-waiter.d.ts +5 -2
  27. package/lib/Compatibility/tagged-message-waiter.js +5 -5
  28. package/lib/Compatibility/usync/adapter.d.ts +1 -1
  29. package/lib/Compatibility/websocket-client.d.ts +11 -3
  30. package/lib/Compatibility/websocket-client.js +84 -11
  31. package/lib/Defaults/index.d.ts +8 -8
  32. package/lib/Defaults/index.js +8 -8
  33. package/lib/Media/content.d.ts +28 -0
  34. package/lib/Media/content.js +129 -0
  35. package/lib/Media/core.d.ts +34 -0
  36. package/lib/Media/core.js +120 -0
  37. package/lib/Media/mutations.d.ts +24 -0
  38. package/lib/Media/mutations.js +38 -0
  39. package/lib/Runtime/aes-gcm.d.ts +4 -0
  40. package/lib/Runtime/aes-gcm.js +11 -0
  41. package/lib/Runtime/bridge.d.ts +44 -0
  42. package/lib/Runtime/bridge.js +76 -0
  43. package/lib/Runtime/buffer.d.ts +9 -0
  44. package/lib/Runtime/buffer.js +22 -0
  45. package/lib/Runtime/bytes.d.ts +61 -0
  46. package/lib/Runtime/bytes.js +270 -0
  47. package/lib/Runtime/host.d.ts +22 -0
  48. package/lib/Runtime/host.js +43 -0
  49. package/lib/Runtime/long.d.ts +33 -0
  50. package/lib/Runtime/long.js +64 -0
  51. package/lib/Runtime/node-media-processors.d.ts +4 -0
  52. package/lib/Runtime/node-media-processors.js +29 -0
  53. package/lib/Runtime/node-media.d.ts +28 -0
  54. package/lib/Runtime/node-media.js +115 -0
  55. package/lib/Runtime/node.d.ts +14 -0
  56. package/lib/Runtime/node.js +81 -0
  57. package/lib/Runtime/paths.d.ts +4 -0
  58. package/lib/Runtime/paths.js +7 -0
  59. package/lib/Runtime/stream.d.ts +11 -0
  60. package/lib/Runtime/stream.js +25 -0
  61. package/lib/Runtime/types.d.ts +98 -0
  62. package/lib/Runtime/types.js +13 -0
  63. package/lib/Socket/core.d.ts +312 -0
  64. package/lib/Socket/core.js +1122 -0
  65. package/lib/Socket/events-core.d.ts +75 -0
  66. package/lib/Socket/events-core.js +1058 -0
  67. package/lib/Socket/events.d.ts +1 -63
  68. package/lib/Socket/events.js +4 -1047
  69. package/lib/Socket/groups.js +4 -3
  70. package/lib/Socket/index.d.ts +521 -103
  71. package/lib/Socket/index.js +10 -1007
  72. package/lib/Socket/messages-core.d.ts +41 -0
  73. package/lib/Socket/messages-core.js +270 -0
  74. package/lib/Socket/messages.d.ts +9 -36
  75. package/lib/Socket/messages.js +3 -260
  76. package/lib/Socket/newsletter.js +3 -3
  77. package/lib/Socket/profile.js +1 -1
  78. package/lib/Socket/terminal-close-reporter.d.ts +2 -0
  79. package/lib/Socket/terminal-close-reporter.js +3 -3
  80. package/lib/Socket/transport.d.ts +2 -0
  81. package/lib/Socket/transport.js +16 -1
  82. package/lib/Socket/types.d.ts +2 -0
  83. package/lib/Types/Auth.d.ts +11 -0
  84. package/lib/Types/Message.d.ts +1 -1
  85. package/lib/Types/Socket.d.ts +2 -2
  86. package/lib/Types/index.d.ts +6 -1
  87. package/lib/Utils/browser-utils.d.ts +4 -0
  88. package/lib/Utils/browser-utils.js +53 -2
  89. package/lib/Utils/crypto.d.ts +1 -0
  90. package/lib/Utils/crypto.js +1 -0
  91. package/lib/Utils/event-buffer.d.ts +6 -1
  92. package/lib/Utils/event-buffer.js +28 -27
  93. package/lib/Utils/link-preview.d.ts +4 -3
  94. package/lib/Utils/logger.d.ts +3 -0
  95. package/lib/Utils/logger.js +35 -6
  96. package/lib/Utils/messages-core.d.ts +174 -0
  97. package/lib/Utils/messages-core.js +1042 -0
  98. package/lib/Utils/messages-media-core.d.ts +73 -0
  99. package/lib/Utils/messages-media-core.js +319 -0
  100. package/lib/Utils/messages-media.d.ts +1 -72
  101. package/lib/Utils/messages-media.js +9 -358
  102. package/lib/Utils/messages.d.ts +1 -166
  103. package/lib/Utils/messages.js +7 -1040
  104. package/lib/Utils/optional-peer.js +14 -5
  105. package/lib/Utils/process-history-message-core.d.ts +36 -0
  106. package/lib/Utils/process-history-message-core.js +187 -0
  107. package/lib/Utils/process-history-message.d.ts +5 -43
  108. package/lib/Utils/process-history-message.js +11 -197
  109. package/lib/Utils/process-message-core.d.ts +29 -0
  110. package/lib/Utils/process-message-core.js +112 -0
  111. package/lib/Utils/process-message.d.ts +4 -24
  112. package/lib/Utils/process-message.js +6 -111
  113. package/lib/WABinary/generic-utils.js +7 -5
  114. package/lib/host-shared.d.ts +74 -0
  115. package/lib/host-shared.js +15 -0
  116. package/lib/host-socket-operations.d.ts +3 -0
  117. package/lib/host-socket-operations.js +2 -0
  118. package/lib/host-surface.d.ts +21 -0
  119. package/lib/host-surface.js +35 -0
  120. package/lib/host-types.d.ts +356 -0
  121. package/lib/host-types.js +2 -0
  122. package/lib/host.d.ts +33 -0
  123. package/lib/host.js +31 -0
  124. package/lib/node-surface.d.ts +9 -0
  125. package/lib/node-surface.js +15 -0
  126. package/lib/surface.d.ts +5 -0
  127. package/lib/surface.js +6 -0
  128. package/lib/wasm.d.ts +27 -0
  129. package/lib/wasm.js +27 -0
  130. package/package.json +18 -6
@@ -0,0 +1,112 @@
1
+ import { WAMessageStubType } from '../Types/index.js';
2
+ import { proto } from '../WAProto/runtime.js';
3
+ import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidStatusBroadcast, jidDecode, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
4
+ import { Boom } from './boom.js';
5
+ import { getContentType, normalizeMessageContent } from '../Media/content.js';
6
+ const REAL_MSG_STUB_TYPES = new Set([
7
+ WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
8
+ WAMessageStubType.CALL_MISSED_GROUP_VOICE,
9
+ WAMessageStubType.CALL_MISSED_VIDEO,
10
+ WAMessageStubType.CALL_MISSED_VOICE
11
+ ]);
12
+ const REAL_MSG_REQ_ME_STUB_TYPES = new Set([WAMessageStubType.GROUP_PARTICIPANT_ADD]);
13
+ /** Whether an envelope carries user-visible content rather than a control message. */
14
+ export const isRealMessage = (message) => {
15
+ const normalizedContent = normalizeMessageContent(message.message);
16
+ const hasSomeContent = !!getContentType(normalizedContent);
17
+ return ((!!normalizedContent ||
18
+ REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
19
+ REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)) &&
20
+ hasSomeContent &&
21
+ !normalizedContent?.protocolMessage &&
22
+ !normalizedContent?.reactionMessage &&
23
+ !normalizedContent?.pollUpdateMessage);
24
+ };
25
+ export const shouldIncrementChatUnread = (message) => !message.key.fromMe && !message.messageStubType;
26
+ /**
27
+ * WhatsApp Web's rules, which upstream Baileys does not always follow.
28
+ *
29
+ * **A field with nothing to normalise stays absent.** `WAWebMsgKey` only assigns
30
+ * the participant it was given — `h !== void 0 && (this.participant = h)` — and
31
+ * builds the message's identity by joining the parts that are present, so an
32
+ * empty string there would change what the key serialises to. Upstream runs
33
+ * every field through `jidNormalizedUser`, which answers `''` for a jid it was
34
+ * not given, and so writes `participant: ''` onto every direct-message key.
35
+ * Matching that would mean copying a bug into the key handed back to callers.
36
+ *
37
+ * **A hosted jid is re-encoded onto its plain server**, and the fallback that
38
+ * used to keep `@hosted` when the user part was empty is gone. WA Web accepts
39
+ * hosted only as `<digits>:99@hosted` (`WAWebWidValidator`), and every jid that
40
+ * reached the old fallback is one it rejects outright — so there is no
41
+ * behaviour to preserve there, and agreeing with upstream costs nothing. Valid
42
+ * hosted jids already agreed.
43
+ */
44
+ const normalizeMessageJid = (jid) => {
45
+ if (!jid)
46
+ return undefined;
47
+ const hostedPn = isHostedPnUser(jid);
48
+ if (!hostedPn && !isHostedLidUser(jid))
49
+ return jidNormalizedUser(jid);
50
+ return jidEncode(jidDecode(jid)?.user ?? null, hostedPn ? 's.whatsapp.net' : 'lid');
51
+ };
52
+ /** Normalize device/hosted JIDs and nested reaction/poll keys in place. */
53
+ export const cleanMessage = (message, meId, meLid) => {
54
+ // Normalise the fields that are there; do not add the ones that are not.
55
+ //
56
+ // Writing the result unconditionally put the property on the key even when
57
+ // there was nothing to normalise, so a direct-message key — which carries no
58
+ // participant — still gained one holding `undefined`, and `Object.keys` and a
59
+ // spread saw it. `WAWebMsgKey` assigns the participant it was given and
60
+ // leaves the key without one otherwise, and that absence is load-bearing
61
+ // there: it joins the parts that are present into the id the message is
62
+ // stored under.
63
+ //
64
+ // A field the caller did provide is normalised even when it comes back with
65
+ // nothing, which is the empty string upstream writes — they provided it, so
66
+ // it is theirs to have normalised, and leaving a `null` in place would only
67
+ // swap one falsy spelling for another.
68
+ for (const field of ['remoteJid', 'participant']) {
69
+ if (!Object.hasOwn(message.key, field) && message.key[field] == null)
70
+ continue;
71
+ message.key[field] = normalizeMessageJid(message.key[field]) ?? '';
72
+ }
73
+ const content = normalizeMessageContent(message.message);
74
+ if (content?.reactionMessage)
75
+ normaliseKey(content.reactionMessage.key);
76
+ if (content?.pollUpdateMessage)
77
+ normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
78
+ function normaliseKey(msgKey) {
79
+ if (!message.key.fromMe) {
80
+ msgKey.fromMe = !msgKey.fromMe
81
+ ? areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId) ||
82
+ areJidsSameUser(msgKey.participant || msgKey.remoteJid, meLid)
83
+ : false;
84
+ msgKey.remoteJid = message.key.remoteJid;
85
+ msgKey.participant || (msgKey.participant = message.key.participant);
86
+ }
87
+ }
88
+ };
89
+ /** Return the conversation JID represented by a message key. */
90
+ export const getChatId = ({ remoteJid, participant, fromMe }) => {
91
+ if (!remoteJid) {
92
+ throw new Boom('Cannot derive chat id: message key is missing remoteJid', {
93
+ data: { remoteJid, participant, fromMe }
94
+ });
95
+ }
96
+ if (isJidBroadcast(remoteJid) && !isJidStatusBroadcast(remoteJid) && !fromMe) {
97
+ if (!participant) {
98
+ throw new Boom('Cannot derive chat id: broadcast message key is missing participant', {
99
+ data: { remoteJid, fromMe }
100
+ });
101
+ }
102
+ return participant;
103
+ }
104
+ return remoteJid;
105
+ };
106
+ export function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid }, crypto) {
107
+ return proto.Message.PollVoteMessage.decode(crypto.decryptPollVotePayload(encPayload, encIv, pollEncKey, pollMsgId, pollCreatorJid, voterJid));
108
+ }
109
+ export function decryptEventResponse({ encPayload, encIv }, { eventCreatorJid, eventMsgId, eventEncKey, responderJid }, crypto) {
110
+ return proto.Message.EventResponseMessage.decode(crypto.decryptEventResponsePayload(encPayload, encIv, eventEncKey, eventMsgId, eventCreatorJid, responderJid));
111
+ }
112
+ //# sourceMappingURL=process-message-core.js.map
@@ -1,25 +1,5 @@
1
- import type { WAMessage, WAMessageKey } from '../Types/index.js';
2
- import { proto } from '../WAProto/runtime.js';
3
- /** Whether an envelope carries user-visible content rather than a control message. */
4
- export declare const isRealMessage: (message: WAMessage) => boolean;
5
- export declare const shouldIncrementChatUnread: (message: WAMessage) => boolean;
6
- /** Normalize device/hosted JIDs and nested reaction/poll keys in place. */
7
- export declare const cleanMessage: (message: WAMessage, meId: string, meLid: string) => void;
8
- /** Return the conversation JID represented by a message key. */
9
- export declare const getChatId: ({ remoteJid, participant, fromMe }: WAMessageKey) => string;
10
- type PollContext = {
11
- pollCreatorJid: string;
12
- pollMsgId: string;
13
- pollEncKey: Uint8Array;
14
- voterJid: string;
15
- };
16
- type EventContext = {
17
- eventCreatorJid: string;
18
- eventMsgId: string;
19
- eventEncKey: Uint8Array;
20
- responderJid: string;
21
- };
22
- export declare function decryptPollVote({ encPayload, encIv }: proto.Message.IPollEncValue, { pollCreatorJid, pollMsgId, pollEncKey, voterJid }: PollContext): proto.Message.PollVoteMessage;
23
- export declare function decryptEventResponse({ encPayload, encIv }: proto.Message.IPollEncValue, { eventCreatorJid, eventMsgId, eventEncKey, responderJid }: EventContext): proto.Message.EventResponseMessage;
24
- export {};
1
+ import { cleanMessage, decryptEventResponse as decryptEventResponseCore, decryptPollVote as decryptPollVoteCore, getChatId, isRealMessage, shouldIncrementChatUnread } from './process-message-core.js';
2
+ export { cleanMessage, getChatId, isRealMessage, shouldIncrementChatUnread };
3
+ export declare const decryptPollVote: (...args: [Parameters<typeof decryptPollVoteCore>[0], Parameters<typeof decryptPollVoteCore>[1]]) => import("@oxidezap/whatsapp-rust-bridge/proto-types").proto.Message.PollVoteMessage;
4
+ export declare const decryptEventResponse: (...args: [Parameters<typeof decryptEventResponseCore>[0], Parameters<typeof decryptEventResponseCore>[1]]) => import("@oxidezap/whatsapp-rust-bridge/proto-types").proto.Message.EventResponseMessage;
25
5
  //# sourceMappingURL=process-message.d.ts.map
@@ -1,113 +1,8 @@
1
- import { WAMessageStubType } from '../Types/index.js';
2
- import { proto } from '../WAProto/runtime.js';
1
+ /** Node-facing message helpers; crypto defaults to the auto-initializing bridge root. */
3
2
  import { decryptEventResponsePayload, decryptPollVotePayload } from '@oxidezap/whatsapp-rust-bridge';
4
- import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidStatusBroadcast, jidDecode, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
5
- import { Boom } from './boom.js';
6
- import { getContentType, normalizeMessageContent } from './messages.js';
7
- const REAL_MSG_STUB_TYPES = new Set([
8
- WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
9
- WAMessageStubType.CALL_MISSED_GROUP_VOICE,
10
- WAMessageStubType.CALL_MISSED_VIDEO,
11
- WAMessageStubType.CALL_MISSED_VOICE
12
- ]);
13
- const REAL_MSG_REQ_ME_STUB_TYPES = new Set([WAMessageStubType.GROUP_PARTICIPANT_ADD]);
14
- /** Whether an envelope carries user-visible content rather than a control message. */
15
- export const isRealMessage = (message) => {
16
- const normalizedContent = normalizeMessageContent(message.message);
17
- const hasSomeContent = !!getContentType(normalizedContent);
18
- return ((!!normalizedContent ||
19
- REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
20
- REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)) &&
21
- hasSomeContent &&
22
- !normalizedContent?.protocolMessage &&
23
- !normalizedContent?.reactionMessage &&
24
- !normalizedContent?.pollUpdateMessage);
25
- };
26
- export const shouldIncrementChatUnread = (message) => !message.key.fromMe && !message.messageStubType;
27
- /**
28
- * WhatsApp Web's rules, which upstream Baileys does not always follow.
29
- *
30
- * **A field with nothing to normalise stays absent.** `WAWebMsgKey` only assigns
31
- * the participant it was given — `h !== void 0 && (this.participant = h)` — and
32
- * builds the message's identity by joining the parts that are present, so an
33
- * empty string there would change what the key serialises to. Upstream runs
34
- * every field through `jidNormalizedUser`, which answers `''` for a jid it was
35
- * not given, and so writes `participant: ''` onto every direct-message key.
36
- * Matching that would mean copying a bug into the key handed back to callers.
37
- *
38
- * **A hosted jid is re-encoded onto its plain server**, and the fallback that
39
- * used to keep `@hosted` when the user part was empty is gone. WA Web accepts
40
- * hosted only as `<digits>:99@hosted` (`WAWebWidValidator`), and every jid that
41
- * reached the old fallback is one it rejects outright — so there is no
42
- * behaviour to preserve there, and agreeing with upstream costs nothing. Valid
43
- * hosted jids already agreed.
44
- */
45
- const normalizeMessageJid = (jid) => {
46
- if (!jid)
47
- return undefined;
48
- const hostedPn = isHostedPnUser(jid);
49
- if (!hostedPn && !isHostedLidUser(jid))
50
- return jidNormalizedUser(jid);
51
- return jidEncode(jidDecode(jid)?.user ?? null, hostedPn ? 's.whatsapp.net' : 'lid');
52
- };
53
- /** Normalize device/hosted JIDs and nested reaction/poll keys in place. */
54
- export const cleanMessage = (message, meId, meLid) => {
55
- // Normalise the fields that are there; do not add the ones that are not.
56
- //
57
- // Writing the result unconditionally put the property on the key even when
58
- // there was nothing to normalise, so a direct-message key — which carries no
59
- // participant — still gained one holding `undefined`, and `Object.keys` and a
60
- // spread saw it. `WAWebMsgKey` assigns the participant it was given and
61
- // leaves the key without one otherwise, and that absence is load-bearing
62
- // there: it joins the parts that are present into the id the message is
63
- // stored under.
64
- //
65
- // A field the caller did provide is normalised even when it comes back with
66
- // nothing, which is the empty string upstream writes — they provided it, so
67
- // it is theirs to have normalised, and leaving a `null` in place would only
68
- // swap one falsy spelling for another.
69
- for (const field of ['remoteJid', 'participant']) {
70
- if (!Object.hasOwn(message.key, field) && message.key[field] == null)
71
- continue;
72
- message.key[field] = normalizeMessageJid(message.key[field]) ?? '';
73
- }
74
- const content = normalizeMessageContent(message.message);
75
- if (content?.reactionMessage)
76
- normaliseKey(content.reactionMessage.key);
77
- if (content?.pollUpdateMessage)
78
- normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
79
- function normaliseKey(msgKey) {
80
- if (!message.key.fromMe) {
81
- msgKey.fromMe = !msgKey.fromMe
82
- ? areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId) ||
83
- areJidsSameUser(msgKey.participant || msgKey.remoteJid, meLid)
84
- : false;
85
- msgKey.remoteJid = message.key.remoteJid;
86
- msgKey.participant || (msgKey.participant = message.key.participant);
87
- }
88
- }
89
- };
90
- /** Return the conversation JID represented by a message key. */
91
- export const getChatId = ({ remoteJid, participant, fromMe }) => {
92
- if (!remoteJid) {
93
- throw new Boom('Cannot derive chat id: message key is missing remoteJid', {
94
- data: { remoteJid, participant, fromMe }
95
- });
96
- }
97
- if (isJidBroadcast(remoteJid) && !isJidStatusBroadcast(remoteJid) && !fromMe) {
98
- if (!participant) {
99
- throw new Boom('Cannot derive chat id: broadcast message key is missing participant', {
100
- data: { remoteJid, fromMe }
101
- });
102
- }
103
- return participant;
104
- }
105
- return remoteJid;
106
- };
107
- export function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid }) {
108
- return proto.Message.PollVoteMessage.decode(decryptPollVotePayload(encPayload, encIv, pollEncKey, pollMsgId, pollCreatorJid, voterJid));
109
- }
110
- export function decryptEventResponse({ encPayload, encIv }, { eventCreatorJid, eventMsgId, eventEncKey, responderJid }) {
111
- return proto.Message.EventResponseMessage.decode(decryptEventResponsePayload(encPayload, encIv, eventEncKey, eventMsgId, eventCreatorJid, responderJid));
112
- }
3
+ import { cleanMessage, decryptEventResponse as decryptEventResponseCore, decryptPollVote as decryptPollVoteCore, getChatId, isRealMessage, shouldIncrementChatUnread } from './process-message-core.js';
4
+ const nodeCrypto = { decryptPollVotePayload, decryptEventResponsePayload };
5
+ export { cleanMessage, getChatId, isRealMessage, shouldIncrementChatUnread };
6
+ export const decryptPollVote = (...args) => decryptPollVoteCore(args[0], args[1], nodeCrypto);
7
+ export const decryptEventResponse = (...args) => decryptEventResponseCore(args[0], args[1], nodeCrypto);
113
8
  //# sourceMappingURL=process-message.js.map
@@ -4,7 +4,7 @@
4
4
  * same `BinaryNode` shape (`{ tag, attrs, content }`), so the
5
5
  * implementations are byte-identical.
6
6
  */
7
- import { Buffer } from 'node:buffer';
7
+ import { hexEncode } from '../Runtime/bytes.js';
8
8
  import { Boom } from '../Utils/boom.js';
9
9
  import { proto } from '../WAProto/runtime.js';
10
10
  export const getBinaryNodeChildren = (node, childTag) => {
@@ -22,13 +22,15 @@ export const getBinaryNodeChild = (node, childTag) => {
22
22
  export const getAllBinaryNodeChildren = ({ content }) => Array.isArray(content) ? content : [];
23
23
  export const getBinaryNodeChildBuffer = (node, childTag) => {
24
24
  const child = getBinaryNodeChild(node, childTag)?.content;
25
- if (Buffer.isBuffer(child) || child instanceof Uint8Array)
25
+ // Buffer subclasses Uint8Array, so one check covers both; anything else
26
+ // (string content, nested nodes) is genuinely not bytes.
27
+ if (child instanceof Uint8Array)
26
28
  return child;
27
29
  };
28
30
  export const getBinaryNodeChildString = (node, childTag) => {
29
31
  const child = getBinaryNodeChild(node, childTag)?.content;
30
- if (Buffer.isBuffer(child) || child instanceof Uint8Array)
31
- return Buffer.from(child).toString('utf8');
32
+ if (child instanceof Uint8Array)
33
+ return new TextDecoder('utf-8', { fatal: false, ignoreBOM: true }).decode(child);
32
34
  if (typeof child === 'string')
33
35
  return child;
34
36
  };
@@ -92,7 +94,7 @@ export function binaryNodeToString(node, i = 0) {
92
94
  if (typeof node === 'string')
93
95
  return tabs(i) + node;
94
96
  if (node instanceof Uint8Array)
95
- return tabs(i) + Buffer.from(node).toString('hex');
97
+ return tabs(i) + hexEncode(node);
96
98
  if (Array.isArray(node))
97
99
  return node.map(child => tabs(i + 1) + binaryNodeToString(child, i + 1)).join('\n');
98
100
  const children = binaryNodeToString(node.content, i + 1);
@@ -0,0 +1,74 @@
1
+ import type { HostMediaCryptoRuntime } from './host-types.js';
2
+ export type BinaryNode = {
3
+ tag: string;
4
+ attrs: Record<string, string>;
5
+ content?: BinaryNode[] | string | Uint8Array;
6
+ };
7
+ export type WAMessageKey = {
8
+ remoteJid?: string | null;
9
+ fromMe?: boolean | null;
10
+ id?: string | null;
11
+ participant?: string | null;
12
+ };
13
+ export type WAMessage = {
14
+ key: WAMessageKey;
15
+ message?: Record<string, unknown> | null;
16
+ messageTimestamp?: number | {
17
+ low: number;
18
+ high: number;
19
+ unsigned?: boolean;
20
+ } | null;
21
+ pushName?: string | null;
22
+ [key: string]: unknown;
23
+ };
24
+ export type Chat = {
25
+ id?: string;
26
+ [key: string]: unknown;
27
+ };
28
+ export type Contact = {
29
+ id: string;
30
+ [key: string]: unknown;
31
+ };
32
+ export type MediaType = 'audio' | 'document' | 'gif' | 'image' | 'ppic' | 'product' | 'ptt' | 'sticker' | 'video' | 'thumbnail-document' | 'thumbnail-image' | 'thumbnail-video' | 'thumbnail-link' | 'md-msg-hist' | 'md-app-state' | 'product-catalog-image' | 'payment-bg-image' | 'ptv' | 'biz-cover-photo';
33
+ export type MediaDecryptionKeyInfo = {
34
+ iv: Uint8Array;
35
+ cipherKey: Uint8Array;
36
+ macKey?: Uint8Array;
37
+ };
38
+ export type MediaRetryUpdate = {
39
+ key: WAMessageKey;
40
+ error?: unknown;
41
+ media?: {
42
+ ciphertext: Uint8Array;
43
+ iv: Uint8Array;
44
+ };
45
+ };
46
+ export type DisconnectReason = number;
47
+ export declare const DisconnectReason: {
48
+ [code: number]: string;
49
+ connectionClosed: number;
50
+ connectionLost: number;
51
+ connectionReplaced: number;
52
+ timedOut: number;
53
+ loggedOut: number;
54
+ badSession: number;
55
+ restartRequired: number;
56
+ multideviceMismatch: number;
57
+ forbidden: number;
58
+ unavailableService: number;
59
+ };
60
+ export declare const hkdfInfoKey: (type: MediaType) => string;
61
+ export declare const getMediaKeys: (buffer: Uint8Array | string | null | undefined, mediaType: MediaType, runtime?: HostMediaCryptoRuntime) => Promise<MediaDecryptionKeyInfo>;
62
+ export declare const encodeBase64EncodedStringForUpload: (value: string) => string;
63
+ export declare const mediaMessageSHA256B64: (message: Record<string, unknown>) => string | null | undefined;
64
+ export declare const DEF_MEDIA_HOST: string;
65
+ export declare const getUrlFromDirectPath: (directPath: string, host?: string) => string;
66
+ export declare const extensionForMediaMessage: (message: Record<string, unknown>) => string;
67
+ export declare const encryptMediaRetryRequest: (key: WAMessageKey, mediaKey: Uint8Array, meId: string, runtime?: HostMediaCryptoRuntime) => BinaryNode;
68
+ export declare const getStatusCodeForMediaRetry: (code: number) => 200 | 412 | 404 | 418;
69
+ export declare const decodeMediaRetryNode: (node: BinaryNode) => MediaRetryUpdate;
70
+ export declare const decryptMediaRetryData: (data: {
71
+ ciphertext: Uint8Array;
72
+ iv: Uint8Array;
73
+ }, mediaKey: Uint8Array, msgId: string, runtime?: HostMediaCryptoRuntime) => unknown;
74
+ //# sourceMappingURL=host-shared.d.ts.map
@@ -0,0 +1,15 @@
1
+ import { DisconnectReason as socketDisconnectReason } from './Types/index.js';
2
+ import * as mediaCore from './Media/core.js';
3
+ export const DisconnectReason = socketDisconnectReason;
4
+ export const hkdfInfoKey = (type) => mediaCore.hkdfInfoKey(type);
5
+ export const getMediaKeys = (buffer, mediaType, runtime) => mediaCore.getMediaKeys(buffer, mediaType, runtime);
6
+ export const encodeBase64EncodedStringForUpload = (value) => mediaCore.encodeBase64EncodedStringForUpload(value);
7
+ export const mediaMessageSHA256B64 = (message) => mediaCore.mediaMessageSHA256B64(message);
8
+ export const DEF_MEDIA_HOST = mediaCore.DEF_MEDIA_HOST;
9
+ export const getUrlFromDirectPath = (directPath, host) => mediaCore.getUrlFromDirectPath(directPath, host);
10
+ export const extensionForMediaMessage = (message) => mediaCore.extensionForMediaMessage(message);
11
+ export const encryptMediaRetryRequest = (key, mediaKey, meId, runtime) => mediaCore.encryptMediaRetryRequest(key, mediaKey, meId, runtime);
12
+ export const getStatusCodeForMediaRetry = (code) => mediaCore.getStatusCodeForMediaRetry(code);
13
+ export const decodeMediaRetryNode = (node) => mediaCore.decodeMediaRetryNode(node);
14
+ export const decryptMediaRetryData = (data, mediaKey, msgId, runtime) => mediaCore.decryptMediaRetryData(data, mediaKey, msgId, runtime);
15
+ //# sourceMappingURL=host-shared.js.map
@@ -0,0 +1,3 @@
1
+ /** Callable keys exposed by the host socket. Kept Node-type-free for /host declarations. */
2
+ export type HostSocketOperationName = 'addChatLabel' | 'addLabel' | 'addMessageLabel' | 'addOrEditContact' | 'addOrEditQuickReply' | 'appPatch' | 'archiveChat' | 'assertSessions' | 'chatModify' | 'cleanDirtyBits' | 'communityAcceptInvite' | 'communityAcceptInviteV4' | 'communityCreate' | 'communityCreateGroup' | 'communityFetchAllParticipating' | 'communityFetchLinkedGroups' | 'communityGetInviteInfo' | 'communityInviteCode' | 'communityJoinApprovalMode' | 'communityLeave' | 'communityLinkGroup' | 'communityMemberAddMode' | 'communityMetadata' | 'communityParticipantsUpdate' | 'communityRequestParticipantsList' | 'communityRequestParticipantsUpdate' | 'communityRevokeInvite' | 'communityRevokeInviteV4' | 'communitySettingUpdate' | 'communityToggleEphemeral' | 'communityUnlinkGroup' | 'communityUpdateDescription' | 'communityUpdateSubject' | 'createCallLink' | 'createParticipantNodes' | 'digestKeyBundle' | 'downloadMedia' | 'end' | 'executeUSyncQuery' | 'fetchAccountReachoutTimelock' | 'fetchBlocklist' | 'fetchDisappearingDuration' | 'fetchMessageHistory' | 'fetchNewChatMessageCap' | 'fetchPrivacySettings' | 'fetchReachoutTimelock' | 'fetchStatus' | 'fetchUserInfo' | 'generateMessageTag' | 'getBotListV2' | 'getBusinessProfile' | 'getCatalog' | 'getCollections' | 'getJid' | 'getLid' | 'getMediaHost' | 'getOrderDetails' | 'getPushName' | 'getUSyncDevices' | 'groupAcceptInvite' | 'groupAcceptInviteV4' | 'groupCreate' | 'groupFetchAllParticipating' | 'groupGetInviteInfo' | 'groupInviteCode' | 'groupJoinApprovalMode' | 'groupLeave' | 'groupMemberAddMode' | 'groupMetadata' | 'groupParticipantsUpdate' | 'groupRequestParticipantsList' | 'groupRequestParticipantsUpdate' | 'groupRevokeInvite' | 'groupRevokeInviteV4' | 'groupSettingUpdate' | 'groupToggleEphemeral' | 'groupUpdateDescription' | 'groupUpdateSubject' | 'issuePrivacyTokens' | 'logout' | 'muteChat' | 'newsletterAdminCount' | 'newsletterChangeOwner' | 'newsletterCreate' | 'newsletterDelete' | 'newsletterDemote' | 'newsletterFetchMessages' | 'newsletterFollow' | 'newsletterMetadata' | 'newsletterMute' | 'newsletterReactMessage' | 'newsletterRemovePicture' | 'newsletterSubscribe' | 'newsletterSubscribers' | 'newsletterUnfollow' | 'newsletterUnmute' | 'newsletterUnsubscribe' | 'newsletterUpdate' | 'newsletterUpdateDescription' | 'newsletterUpdateName' | 'newsletterUpdatePicture' | 'onUnexpectedError' | 'onWhatsApp' | 'pinChat' | 'presenceSubscribe' | 'productCreate' | 'productDelete' | 'productUpdate' | 'profilePictureUrl' | 'query' | 'readMessages' | 'refreshMediaConn' | 'registerSocketEndHandler' | 'rejectCall' | 'relayMessage' | 'removeChatLabel' | 'removeContact' | 'removeCoverPhoto' | 'removeMessageLabel' | 'removeProfilePicture' | 'removeQuickReply' | 'requestPairingCode' | 'requestPlaceholderResend' | 'resyncAppState' | 'rotateSignedPreKey' | 'sendChatState' | 'sendMessage' | 'sendMessageAck' | 'sendNode' | 'sendPeerDataOperationMessage' | 'sendPresence' | 'sendPresenceUpdate' | 'sendRawMessage' | 'sendReceipt' | 'sendReceipts' | 'sendRetryRequest' | 'sendStatusMessage' | 'sendUnifiedSession' | 'setAutoReconnect' | 'setPushName' | 'star' | 'starMessage' | 'subscribeNewsletterUpdates' | 'updateBlockStatus' | 'updateBussinesProfile' | 'updateCallPrivacy' | 'updateCoverPhoto' | 'updateDefaultDisappearingMode' | 'updateGroupsAddPrivacy' | 'updateLastSeenPrivacy' | 'updateMediaMessage' | 'updateMemberLabel' | 'updateMessagesPrivacy' | 'updateOnlinePrivacy' | 'updatePrivacySetting' | 'updateProfileName' | 'updateProfilePicture' | 'updateProfilePicturePrivacy' | 'updateProfileStatus' | 'updateReadReceiptsPrivacy' | 'updateServerTimeOffset' | 'updateStatusPrivacy' | 'uploadPreKeys' | 'uploadPreKeysToServerIfRequired' | 'upsertMessage' | 'waUploadToServer' | 'waitForConnectionUpdate' | 'waitForMessage' | 'waitForSocketOpen';
3
+ //# sourceMappingURL=host-socket-operations.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=host-socket-operations.js.map
@@ -0,0 +1,21 @@
1
+ import type { HostLoggerSink, HostMediaCryptoRuntime, HostRuntime, HostSocketConfig, HostStoreCallbacks, HostWASocket } from './host-types.js';
2
+ export * from './host-types.js';
3
+ export declare const useMemoryStore: (options?: {
4
+ native?: boolean;
5
+ }) => HostStoreCallbacks;
6
+ export { createAuthenticationState, initHostAuthCreds } from './Compatibility/host-auth-state.js';
7
+ export declare const setPlatformInfo: (info: {
8
+ os: string;
9
+ release: string;
10
+ }) => void;
11
+ export declare function setLoggerSink(sink: HostLoggerSink | undefined): void;
12
+ export type HostSocketFactory = (config: HostSocketConfig) => HostWASocket;
13
+ /** Bind bridge media crypto and randomness to a caller-supplied host runtime. */
14
+ export declare const makeMediaCryptoRuntime: (runtime: Pick<HostRuntime, 'bridge' | 'randomBytes'>) => HostMediaCryptoRuntime;
15
+ /** Build a host socket factory from explicitly supplied host capabilities. */
16
+ export declare const createWASocketFactory: (runtime: HostRuntime) => HostSocketFactory;
17
+ /** Host-bound socket factory: global WebSocket/fetch/WebCrypto and caller-owned WASM. */
18
+ export declare const makeHostWASocket: HostSocketFactory;
19
+ export declare const makeWASocket: HostSocketFactory;
20
+ export default makeWASocket;
21
+ //# sourceMappingURL=host-surface.d.ts.map
@@ -0,0 +1,35 @@
1
+ // Host surface: deliberately separate declarations from the Node-shaped Types barrel.
2
+ import { createWASocketFactory as createFactory } from './Socket/core.js';
3
+ import { hostLoggerSink, hostRuntime } from './Runtime/host.js';
4
+ import { makeMediaCryptoRuntime as bindMediaCryptoRuntime } from './Runtime/bridge.js';
5
+ import { useMemoryStore as makeMemoryStore } from './Utils/use-memory-store.js';
6
+ export * from './host-types.js';
7
+ export const useMemoryStore = (options) => makeMemoryStore(options);
8
+ export { createAuthenticationState, initHostAuthCreds } from './Compatibility/host-auth-state.js';
9
+ export const setPlatformInfo = (info) => {
10
+ hostRuntime.platformInfo = () => ({ ...info });
11
+ };
12
+ export function setLoggerSink(sink) {
13
+ if (!sink) {
14
+ hostRuntime.loggerSink = hostLoggerSink;
15
+ return;
16
+ }
17
+ hostRuntime.loggerSink = (line, delivered) => {
18
+ if (sink.length >= 2)
19
+ sink(line, delivered ?? (() => { }));
20
+ else {
21
+ ;
22
+ sink(line);
23
+ delivered?.();
24
+ }
25
+ };
26
+ }
27
+ /** Bind bridge media crypto and randomness to a caller-supplied host runtime. */
28
+ export const makeMediaCryptoRuntime = (runtime) => bindMediaCryptoRuntime(runtime);
29
+ /** Build a host socket factory from explicitly supplied host capabilities. */
30
+ export const createWASocketFactory = (runtime) => createFactory(runtime);
31
+ /** Host-bound socket factory: global WebSocket/fetch/WebCrypto and caller-owned WASM. */
32
+ export const makeHostWASocket = createWASocketFactory(hostRuntime);
33
+ export const makeWASocket = makeHostWASocket;
34
+ export default makeWASocket;
35
+ //# sourceMappingURL=host-surface.js.map