@badzz88/baileys 8.4.7 → 8.5.0

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 (144) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1055 -337
  3. package/WAProto/index.js +5 -5
  4. package/lib/Defaults/index.js +187 -127
  5. package/lib/Defaults/phonenumber-mcc.json +223 -0
  6. package/lib/Signal/Group/ciphertext-message.js +14 -11
  7. package/lib/Signal/Group/group-session-builder.js +91 -29
  8. package/lib/Signal/Group/group_cipher.js +88 -81
  9. package/lib/Signal/Group/index.js +136 -12
  10. package/lib/Signal/Group/keyhelper.js +70 -15
  11. package/lib/Signal/Group/sender-chain-key.js +31 -25
  12. package/lib/Signal/Group/sender-key-distribution-message.js +65 -62
  13. package/lib/Signal/Group/sender-key-message.js +68 -65
  14. package/lib/Signal/Group/sender-key-name.js +43 -41
  15. package/lib/Signal/Group/sender-key-record.js +43 -40
  16. package/lib/Signal/Group/sender-key-state.js +96 -83
  17. package/lib/Signal/Group/sender-message-key.js +29 -25
  18. package/lib/Signal/libsignal.js +483 -424
  19. package/lib/Signal/lid-mapping.js +261 -276
  20. package/lib/Socket/Client/index.js +30 -3
  21. package/lib/Socket/Client/types.js +12 -10
  22. package/lib/Socket/Client/websocket.js +61 -53
  23. package/lib/Socket/aigroups.js +240 -0
  24. package/lib/Socket/business.js +422 -379
  25. package/lib/Socket/chats.js +1816 -1193
  26. package/lib/Socket/communities.js +549 -431
  27. package/lib/Socket/graphql.js +656 -0
  28. package/lib/Socket/groups.js +764 -374
  29. package/lib/Socket/index.js +38 -11
  30. package/lib/Socket/interactive-handler.js +522 -0
  31. package/lib/Socket/interop.js +549 -0
  32. package/lib/Socket/luxu.js +6 -5
  33. package/lib/Socket/managed-account.js +183 -0
  34. package/lib/Socket/messages-recv.js +2439 -1915
  35. package/lib/Socket/messages-send.js +1642 -1242
  36. package/lib/Socket/mex.js +59 -40
  37. package/lib/Socket/newsletter.js +812 -249
  38. package/lib/Socket/privacy.js +449 -0
  39. package/lib/Socket/registration.js +427 -0
  40. package/lib/Socket/socket.js +1077 -965
  41. package/lib/Socket/text-router.js +83 -0
  42. package/lib/Socket/username.js +237 -140
  43. package/lib/Store/index.js +36 -10
  44. package/lib/Store/make-cache-manager-store.js +85 -80
  45. package/lib/Store/make-in-memory-store.js +591 -185
  46. package/lib/Store/make-ordered-dictionary.js +78 -72
  47. package/lib/Store/object-repository.js +25 -28
  48. package/lib/Types/Auth.js +38 -2
  49. package/lib/Types/Bussines.js +2 -2
  50. package/lib/Types/Call.js +2 -2
  51. package/lib/Types/Chat.js +4 -8
  52. package/lib/Types/Contact.js +2 -2
  53. package/lib/Types/Events.js +2 -2
  54. package/lib/Types/GroupMetadata.js +2 -2
  55. package/lib/Types/Label.js +26 -24
  56. package/lib/Types/LabelAssociation.js +8 -6
  57. package/lib/Types/Message.js +95 -11
  58. package/lib/Types/Mex.js +112 -37
  59. package/lib/Types/Newsletter.js +121 -0
  60. package/lib/Types/Product.js +2 -2
  61. package/lib/Types/Signal.js +2 -2
  62. package/lib/Types/Socket.js +2 -3
  63. package/lib/Types/State.js +70 -56
  64. package/lib/Types/USync.js +2 -2
  65. package/lib/Types/index.js +55 -26
  66. package/lib/Utils/auth-utils.js +292 -288
  67. package/lib/Utils/browser-utils.js +113 -48
  68. package/lib/Utils/business.js +240 -224
  69. package/lib/Utils/chat-utils.js +1205 -869
  70. package/lib/Utils/command-loader.d.ts +31 -0
  71. package/lib/Utils/command-loader.js +100 -0
  72. package/lib/Utils/companion-reg-client-utils.js +41 -34
  73. package/lib/Utils/consumer-application.js +107 -0
  74. package/lib/Utils/crypto.js +144 -107
  75. package/lib/Utils/curve25519-js.js +275 -0
  76. package/lib/Utils/decode-wa-message.js +518 -308
  77. package/lib/Utils/event-buffer.js +596 -611
  78. package/lib/Utils/generics.js +514 -354
  79. package/lib/Utils/group-history.js +60 -0
  80. package/lib/Utils/history.js +244 -134
  81. package/lib/Utils/identity-change-handler.js +51 -49
  82. package/lib/Utils/index.js +57 -23
  83. package/lib/Utils/jid-display-normalization.js +218 -0
  84. package/lib/Utils/link-preview.js +135 -77
  85. package/lib/Utils/logger.js +9 -3
  86. package/lib/Utils/lt-hash.js +37 -6
  87. package/lib/Utils/make-mutex.js +36 -33
  88. package/lib/Utils/message-composer.js +439 -233
  89. package/lib/Utils/message-retry-manager.js +265 -260
  90. package/lib/Utils/messages-media.js +855 -747
  91. package/lib/Utils/messages.js +2528 -884
  92. package/lib/Utils/meta-ai-msmsg.js +262 -0
  93. package/lib/Utils/native-bridge.js +82 -0
  94. package/lib/Utils/noise-handler.js +195 -200
  95. package/lib/Utils/offline-node-processor.js +35 -33
  96. package/lib/Utils/pre-key-manager.js +103 -102
  97. package/lib/Utils/process-message.js +978 -560
  98. package/lib/Utils/reporting-utils.js +257 -253
  99. package/lib/Utils/session-pool.d.ts +16 -0
  100. package/lib/Utils/session-pool.js +94 -0
  101. package/lib/Utils/signal.js +218 -195
  102. package/lib/Utils/stanza-ack.js +31 -29
  103. package/lib/Utils/sticker.d.ts +17 -0
  104. package/lib/Utils/sticker.js +145 -0
  105. package/lib/Utils/sync-action-utils.js +50 -45
  106. package/lib/Utils/tc-token-utils.js +137 -139
  107. package/lib/Utils/use-multi-file-auth-state.js +109 -109
  108. package/lib/Utils/validate-connection.js +218 -202
  109. package/lib/Utils/view-once-cache.d.ts +9 -0
  110. package/lib/Utils/view-once-cache.js +79 -0
  111. package/lib/Utils/voip-rekey.js +22 -0
  112. package/lib/WABinary/constants.js +1302 -1299
  113. package/lib/WABinary/decode.js +343 -262
  114. package/lib/WABinary/encode.js +12 -4
  115. package/lib/WABinary/generic-utils.js +223 -187
  116. package/lib/WABinary/index.js +33 -6
  117. package/lib/WABinary/jid-utils.js +351 -88
  118. package/lib/WABinary/types.js +2 -2
  119. package/lib/WAM/BinaryInfo.js +12 -9
  120. package/lib/WAM/constants.js +39486 -22853
  121. package/lib/WAM/encode.js +132 -140
  122. package/lib/WAM/index.js +31 -4
  123. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +55 -0
  124. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +43 -0
  125. package/lib/WAUSync/Protocols/USyncContactProtocol.js +53 -51
  126. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +56 -53
  127. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -26
  128. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +59 -0
  129. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +31 -0
  130. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
  131. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
  132. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -37
  133. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
  134. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +26 -24
  135. package/lib/WAUSync/Protocols/index.js +39 -6
  136. package/lib/WAUSync/USyncQuery.js +132 -97
  137. package/lib/WAUSync/USyncUser.js +49 -30
  138. package/lib/WAUSync/index.js +31 -4
  139. package/lib/antiban.js +4637 -0
  140. package/lib/index.js +48 -15
  141. package/package.json +55 -92
  142. package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -263
  143. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -51
  144. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -29
@@ -1,262 +1,343 @@
1
- import { promisify } from 'util';
2
- import { inflate } from 'zlib';
3
- import * as constants from './constants.js';
4
- import { jidEncode, WAJIDDomains } from './jid-utils.js';
5
- const inflatePromise = promisify(inflate);
6
- export const decompressingIfRequired = async (buffer) => {
7
- if (2 & buffer.readUInt8()) {
8
- buffer = await inflatePromise(buffer.slice(1));
9
- }
10
- else {
11
- // nodes with no compression have a 0x00 prefix, we remove that
12
- buffer = buffer.slice(1);
13
- }
14
- return buffer;
15
- };
16
- export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
17
- const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;
18
- const checkEOS = (length) => {
19
- if (indexRef.index + length > buffer.length) {
20
- throw new Error('end of stream');
21
- }
22
- };
23
- const next = () => {
24
- const value = buffer[indexRef.index];
25
- indexRef.index += 1;
26
- return value;
27
- };
28
- const readByte = () => {
29
- checkEOS(1);
30
- return next();
31
- };
32
- const readBytes = (n) => {
33
- checkEOS(n);
34
- const value = buffer.slice(indexRef.index, indexRef.index + n);
35
- indexRef.index += n;
36
- return value;
37
- };
38
- const readStringFromChars = (length) => {
39
- return readBytes(length).toString('utf-8');
40
- };
41
- const readInt = (n, littleEndian = false) => {
42
- checkEOS(n);
43
- let val = 0;
44
- for (let i = 0; i < n; i++) {
45
- const shift = littleEndian ? i : n - 1 - i;
46
- val |= next() << (shift * 8);
47
- }
48
- return val;
49
- };
50
- const readInt20 = () => {
51
- checkEOS(3);
52
- return ((next() & 15) << 16) + (next() << 8) + next();
53
- };
54
- const unpackHex = (value) => {
55
- if (value >= 0 && value < 16) {
56
- return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10;
57
- }
58
- throw new Error('invalid hex: ' + value);
59
- };
60
- const unpackNibble = (value) => {
61
- if (value >= 0 && value <= 9) {
62
- return '0'.charCodeAt(0) + value;
63
- }
64
- switch (value) {
65
- case 10:
66
- return '-'.charCodeAt(0);
67
- case 11:
68
- return '.'.charCodeAt(0);
69
- case 15:
70
- return '\0'.charCodeAt(0);
71
- default:
72
- throw new Error('invalid nibble: ' + value);
73
- }
74
- };
75
- const unpackByte = (tag, value) => {
76
- if (tag === TAGS.NIBBLE_8) {
77
- return unpackNibble(value);
78
- }
79
- else if (tag === TAGS.HEX_8) {
80
- return unpackHex(value);
81
- }
82
- else {
83
- throw new Error('unknown tag: ' + tag);
84
- }
85
- };
86
- const readPacked8 = (tag) => {
87
- const startByte = readByte();
88
- let value = '';
89
- for (let i = 0; i < (startByte & 127); i++) {
90
- const curByte = readByte();
91
- value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));
92
- value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));
93
- }
94
- if (startByte >> 7 !== 0) {
95
- value = value.slice(0, -1);
96
- }
97
- return value;
98
- };
99
- const isListTag = (tag) => {
100
- return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;
101
- };
102
- const readListSize = (tag) => {
103
- switch (tag) {
104
- case TAGS.LIST_EMPTY:
105
- return 0;
106
- case TAGS.LIST_8:
107
- return readByte();
108
- case TAGS.LIST_16:
109
- return readInt(2);
110
- default:
111
- throw new Error('invalid tag for list size: ' + tag);
112
- }
113
- };
114
- const readJidPair = () => {
115
- const i = readString(readByte());
116
- const j = readString(readByte());
117
- if (j) {
118
- return (i || '') + '@' + j;
119
- }
120
- throw new Error('invalid jid pair: ' + i + ', ' + j);
121
- };
122
- const readAdJid = () => {
123
- const rawDomainType = readByte();
124
- const domainType = Number(rawDomainType);
125
- const device = readByte();
126
- const user = readString(readByte());
127
- let server = 's.whatsapp.net'; // default whatsapp server
128
- if (domainType === WAJIDDomains.LID) {
129
- server = 'lid';
130
- }
131
- else if (domainType === WAJIDDomains.HOSTED) {
132
- server = 'hosted';
133
- }
134
- else if (domainType === WAJIDDomains.HOSTED_LID) {
135
- server = 'hosted.lid';
136
- }
137
- return jidEncode(user, server, device);
138
- };
139
- const readFbJid = () => {
140
- const user = readString(readByte());
141
- const device = readInt(2);
142
- const server = readString(readByte());
143
- return `${user}:${device}@${server}`;
144
- };
145
- const readInteropJid = () => {
146
- const user = readString(readByte());
147
- const device = readInt(2);
148
- const integrator = readInt(2);
149
- let server = 'interop';
150
- const beforeServer = indexRef.index;
151
- try {
152
- server = readString(readByte());
153
- }
154
- catch (err) {
155
- indexRef.index = beforeServer;
156
- }
157
- return `${integrator}-${user}:${device}@${server}`;
158
- };
159
- const readString = (tag) => {
160
- if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
161
- return SINGLE_BYTE_TOKENS[tag] || '';
162
- }
163
- switch (tag) {
164
- case TAGS.DICTIONARY_0:
165
- case TAGS.DICTIONARY_1:
166
- case TAGS.DICTIONARY_2:
167
- case TAGS.DICTIONARY_3:
168
- return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
169
- case TAGS.LIST_EMPTY:
170
- return '';
171
- case TAGS.BINARY_8:
172
- return readStringFromChars(readByte());
173
- case TAGS.BINARY_20:
174
- return readStringFromChars(readInt20());
175
- case TAGS.BINARY_32:
176
- return readStringFromChars(readInt(4));
177
- case TAGS.JID_PAIR:
178
- return readJidPair();
179
- case TAGS.FB_JID:
180
- return readFbJid();
181
- case TAGS.INTEROP_JID:
182
- return readInteropJid();
183
- case TAGS.AD_JID:
184
- return readAdJid();
185
- case TAGS.HEX_8:
186
- case TAGS.NIBBLE_8:
187
- return readPacked8(tag);
188
- default:
189
- throw new Error('invalid string with tag: ' + tag);
190
- }
191
- };
192
- const readList = (tag) => {
193
- const items = [];
194
- const size = readListSize(tag);
195
- for (let i = 0; i < size; i++) {
196
- items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef));
197
- }
198
- return items;
199
- };
200
- const getTokenDouble = (index1, index2) => {
201
- const dict = DOUBLE_BYTE_TOKENS[index1];
202
- if (!dict) {
203
- throw new Error(`Invalid double token dict (${index1})`);
204
- }
205
- const value = dict[index2];
206
- if (typeof value === 'undefined') {
207
- throw new Error(`Invalid double token (${index2})`);
208
- }
209
- return value;
210
- };
211
- const listSize = readListSize(readByte());
212
- const header = readString(readByte());
213
- if (!listSize || !header.length) {
214
- throw new Error('invalid node');
215
- }
216
- const attrs = {};
217
- let data;
218
- if (listSize === 0 || !header) {
219
- throw new Error('invalid node');
220
- }
221
- // read the attributes in
222
- const attributesLength = (listSize - 1) >> 1;
223
- for (let i = 0; i < attributesLength; i++) {
224
- const key = readString(readByte());
225
- const value = readString(readByte());
226
- attrs[key] = value;
227
- }
228
- if (listSize % 2 === 0) {
229
- const tag = readByte();
230
- if (isListTag(tag)) {
231
- data = readList(tag);
232
- }
233
- else {
234
- let decoded;
235
- switch (tag) {
236
- case TAGS.BINARY_8:
237
- decoded = readBytes(readByte());
238
- break;
239
- case TAGS.BINARY_20:
240
- decoded = readBytes(readInt20());
241
- break;
242
- case TAGS.BINARY_32:
243
- decoded = readBytes(readInt(4));
244
- break;
245
- default:
246
- decoded = readString(tag);
247
- break;
248
- }
249
- data = decoded;
250
- }
251
- }
252
- return {
253
- tag: header,
254
- attrs,
255
- content: data
256
- };
257
- };
258
- export const decodeBinaryNode = async (buff) => {
259
- const decompBuff = await decompressingIfRequired(buff);
260
- return decodeDecompressedBinaryNode(decompBuff, constants);
261
- };
262
- //# sourceMappingURL=decode.js.map
1
+ 'use strict'
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k
7
+ var 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
+ var __setModuleDefault =
23
+ (this && this.__setModuleDefault) ||
24
+ (Object.create
25
+ ? function (o, v) {
26
+ Object.defineProperty(o, 'default', { enumerable: true, value: v })
27
+ }
28
+ : function (o, v) {
29
+ o['default'] = v
30
+ })
31
+ var __importStar =
32
+ (this && this.__importStar) ||
33
+ (function () {
34
+ var ownKeys = function (o) {
35
+ ownKeys =
36
+ Object.getOwnPropertyNames ||
37
+ function (o) {
38
+ var ar = []
39
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k
40
+ return ar
41
+ }
42
+ return ownKeys(o)
43
+ }
44
+ return function (mod) {
45
+ if (mod && mod.__esModule) return mod
46
+ var result = {}
47
+ if (mod != null)
48
+ for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== 'default') __createBinding(result, mod, k[i])
49
+ __setModuleDefault(result, mod)
50
+ return result
51
+ }
52
+ })()
53
+ Object.defineProperty(exports, '__esModule', { value: true })
54
+ exports.decodeBinaryNode = exports.decodeDecompressedBinaryNode = exports.decompressingIfRequired = void 0
55
+ const util_1 = require('util')
56
+ const zlib_1 = require('zlib')
57
+ const constants = __importStar(require('./constants'))
58
+ const jid_utils_1 = require('./jid-utils')
59
+ const inflatePromise = (0, util_1.promisify)(zlib_1.inflate)
60
+ const decompressingIfRequired = async buffer => {
61
+ if (2 & buffer.readUInt8()) {
62
+ buffer = await inflatePromise(buffer.slice(1))
63
+ } else {
64
+ // nodes with no compression have a 0x00 prefix, we remove that
65
+ buffer = buffer.slice(1)
66
+ }
67
+ return buffer
68
+ }
69
+ exports.decompressingIfRequired = decompressingIfRequired
70
+ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
71
+ const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts
72
+ const checkEOS = length => {
73
+ if (indexRef.index + length > buffer.length) {
74
+ throw new Error('end of stream')
75
+ }
76
+ }
77
+ const next = () => {
78
+ const value = buffer[indexRef.index]
79
+ indexRef.index += 1
80
+ return value
81
+ }
82
+ const readByte = () => {
83
+ checkEOS(1)
84
+ return next()
85
+ }
86
+ const readBytes = n => {
87
+ checkEOS(n)
88
+ const value = buffer.slice(indexRef.index, indexRef.index + n)
89
+ indexRef.index += n
90
+ return value
91
+ }
92
+ const readStringFromChars = length => {
93
+ return readBytes(length).toString('utf-8')
94
+ }
95
+ const readInt = (n, littleEndian = false) => {
96
+ checkEOS(n)
97
+ let val = 0
98
+ for (let i = 0; i < n; i++) {
99
+ const shift = littleEndian ? i : n - 1 - i
100
+ val |= next() << (shift * 8)
101
+ }
102
+ return val
103
+ }
104
+ const readInt20 = () => {
105
+ checkEOS(3)
106
+ return ((next() & 15) << 16) + (next() << 8) + next()
107
+ }
108
+ const unpackHex = value => {
109
+ if (value >= 0 && value < 16) {
110
+ return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10
111
+ }
112
+ throw new Error('invalid hex: ' + value)
113
+ }
114
+ const unpackNibble = value => {
115
+ if (value >= 0 && value <= 9) {
116
+ return '0'.charCodeAt(0) + value
117
+ }
118
+ switch (value) {
119
+ case 10:
120
+ return '-'.charCodeAt(0)
121
+ case 11:
122
+ return '.'.charCodeAt(0)
123
+ case 15:
124
+ return '\0'.charCodeAt(0)
125
+ default:
126
+ throw new Error('invalid nibble: ' + value)
127
+ }
128
+ }
129
+ const unpackByte = (tag, value) => {
130
+ if (tag === TAGS.NIBBLE_8) {
131
+ return unpackNibble(value)
132
+ } else if (tag === TAGS.HEX_8) {
133
+ return unpackHex(value)
134
+ } else {
135
+ throw new Error('unknown tag: ' + tag)
136
+ }
137
+ }
138
+ const readPacked8 = tag => {
139
+ const startByte = readByte()
140
+ let value = ''
141
+ for (let i = 0; i < (startByte & 127); i++) {
142
+ const curByte = readByte()
143
+ value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
144
+ value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
145
+ }
146
+ if (startByte >> 7 !== 0) {
147
+ value = value.slice(0, -1)
148
+ }
149
+ return value
150
+ }
151
+ const isListTag = tag => {
152
+ return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16
153
+ }
154
+ const readListSize = tag => {
155
+ switch (tag) {
156
+ case TAGS.LIST_EMPTY:
157
+ return 0
158
+ case TAGS.LIST_8:
159
+ return readByte()
160
+ case TAGS.LIST_16:
161
+ return readInt(2)
162
+ default:
163
+ throw new Error('invalid tag for list size: ' + tag)
164
+ }
165
+ }
166
+ const readJidPair = () => {
167
+ const i = readString(readByte())
168
+ const j = readString(readByte())
169
+ if (j) {
170
+ return (i || '') + '@' + j
171
+ }
172
+ throw new Error('invalid jid pair: ' + i + ', ' + j)
173
+ }
174
+ const readAdJid = () => {
175
+ const rawDomainType = readByte()
176
+ const domainType = Number(rawDomainType)
177
+ const device = readByte()
178
+ const user = readString(readByte())
179
+ let server = 's.whatsapp.net' // default whatsapp server
180
+ if (domainType === jid_utils_1.WAJIDDomains.LID) {
181
+ server = 'lid'
182
+ } else if (domainType === jid_utils_1.WAJIDDomains.HOSTED) {
183
+ server = 'hosted'
184
+ } else if (domainType === jid_utils_1.WAJIDDomains.HOSTED_LID) {
185
+ server = 'hosted.lid'
186
+ }
187
+ return (0, jid_utils_1.jidEncode)(user, server, device)
188
+ }
189
+ const readFbJid = () => {
190
+ const user = readString(readByte())
191
+ const device = readInt(2)
192
+ const server = readString(readByte())
193
+ return `${user}:${device}@${server}`
194
+ }
195
+ const readInteropJid = () => {
196
+ const user = readString(readByte())
197
+ const device = readInt(2)
198
+ const integrator = readInt(2)
199
+ let server = 'interop'
200
+ // Only attempt to read the optional server field if at least 1 byte remains
201
+ // and the next byte is a valid string tag (avoids corrupting the stream on
202
+ // unknown tags that readString would swallow silently after a catch).
203
+ if (indexRef.index < buffer.length) {
204
+ const serverTag = buffer[indexRef.index]
205
+ // BINARY_8 / BINARY_20 / BINARY_32 or a single-byte token (1–235) are
206
+ // valid string tags; LIST_EMPTY (0) means no server string follows.
207
+ const isValidStringTag =
208
+ (serverTag >= 1 && serverTag <= 235) ||
209
+ serverTag === TAGS.BINARY_8 ||
210
+ serverTag === TAGS.BINARY_20 ||
211
+ serverTag === TAGS.BINARY_32 ||
212
+ serverTag === TAGS.NIBBLE_8 ||
213
+ serverTag === TAGS.HEX_8
214
+ if (isValidStringTag) {
215
+ indexRef.index++ // consume the tag byte
216
+ server = readString(serverTag)
217
+ }
218
+ }
219
+ return `${integrator}-${user}:${device}@${server}`
220
+ }
221
+ const readInteropJidTuple = () => {
222
+ const user = readString(readByte())
223
+ const device = readInt(2)
224
+ const integrator = readInt(2)
225
+ const domain = readByte()
226
+ if (domain !== 0) {
227
+ throw new Error(`invalid domain for INTEROP_JID_TUPLE: ${domain}`)
228
+ }
229
+ const devicePart = device !== 0 ? `:${device}` : ''
230
+ return `${integrator}-${user}${devicePart}@interop`
231
+ }
232
+ const readString = tag => {
233
+ if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
234
+ return SINGLE_BYTE_TOKENS[tag] || ''
235
+ }
236
+ switch (tag) {
237
+ case TAGS.DICTIONARY_0:
238
+ case TAGS.DICTIONARY_1:
239
+ case TAGS.DICTIONARY_2:
240
+ case TAGS.DICTIONARY_3:
241
+ return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
242
+ case TAGS.LIST_EMPTY:
243
+ return ''
244
+ case TAGS.BINARY_8:
245
+ return readStringFromChars(readByte())
246
+ case TAGS.BINARY_20:
247
+ return readStringFromChars(readInt20())
248
+ case TAGS.BINARY_32:
249
+ return readStringFromChars(readInt(4))
250
+ case TAGS.JID_PAIR:
251
+ return readJidPair()
252
+ case TAGS.FB_JID:
253
+ return readFbJid()
254
+ case TAGS.INTEROP_JID_TUPLE:
255
+ return readInteropJidTuple()
256
+ case TAGS.INTEROP_JID:
257
+ return readInteropJid()
258
+ case TAGS.AD_JID:
259
+ return readAdJid()
260
+ case TAGS.HEX_8:
261
+ case TAGS.NIBBLE_8:
262
+ return readPacked8(tag)
263
+ default:
264
+ // Unknown tag — token table may be outdated (new WA protocol version)
265
+ process.stderr.write(`[WABinary] unknown string tag 0x${tag.toString(16)} — token table outdated?\n`)
266
+ return ''
267
+ }
268
+ }
269
+ const readList = tag => {
270
+ const items = []
271
+ const size = readListSize(tag)
272
+ for (let i = 0; i < size; i++) {
273
+ items.push((0, exports.decodeDecompressedBinaryNode)(buffer, opts, indexRef))
274
+ }
275
+ return items
276
+ }
277
+ const getTokenDouble = (index1, index2) => {
278
+ const dict = DOUBLE_BYTE_TOKENS[index1]
279
+ if (!dict) {
280
+ // Unknown dictionary — token table may be outdated (new WA protocol version)
281
+ process.stderr.write(`[WABinary] unknown double-byte token dict ${index1} — token table outdated?\n`)
282
+ return ''
283
+ }
284
+ const value = dict[index2]
285
+ if (typeof value === 'undefined') {
286
+ // Unknown token in known dictionary — token table may be outdated
287
+ process.stderr.write(`[WABinary] unknown double-byte token dict=${index1} idx=${index2} — token table outdated?\n`)
288
+ return ''
289
+ }
290
+ return value
291
+ }
292
+ const listSize = readListSize(readByte())
293
+ const header = readString(readByte())
294
+ if (!listSize || !header.length) {
295
+ throw new Error('invalid node')
296
+ }
297
+ const attrs = {}
298
+ let data
299
+ if (listSize === 0 || !header) {
300
+ throw new Error('invalid node')
301
+ }
302
+ // read the attributes in
303
+ const attributesLength = (listSize - 1) >> 1
304
+ for (let i = 0; i < attributesLength; i++) {
305
+ const key = readString(readByte())
306
+ const value = readString(readByte())
307
+ attrs[key] = value
308
+ }
309
+ if (listSize % 2 === 0) {
310
+ const tag = readByte()
311
+ if (isListTag(tag)) {
312
+ data = readList(tag)
313
+ } else {
314
+ let decoded
315
+ switch (tag) {
316
+ case TAGS.BINARY_8:
317
+ decoded = readBytes(readByte())
318
+ break
319
+ case TAGS.BINARY_20:
320
+ decoded = readBytes(readInt20())
321
+ break
322
+ case TAGS.BINARY_32:
323
+ decoded = readBytes(readInt(4))
324
+ break
325
+ default:
326
+ decoded = readString(tag)
327
+ break
328
+ }
329
+ data = decoded
330
+ }
331
+ }
332
+ return {
333
+ tag: header,
334
+ attrs,
335
+ content: data
336
+ }
337
+ }
338
+ exports.decodeDecompressedBinaryNode = decodeDecompressedBinaryNode
339
+ const decodeBinaryNode = buff => {
340
+ const buffer = Buffer.isBuffer(buff) ? buff : Buffer.from(buff)
341
+ return decodeDecompressedBinaryNode(buffer, constants)
342
+ }
343
+ exports.decodeBinaryNode = decodeBinaryNode
@@ -1,6 +1,10 @@
1
- import * as constants from './constants.js';
2
- import { jidDecode } from './jid-utils.js';
3
- export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.encodeBinaryNode = void 0
4
+ const constants = require('./constants')
5
+ const { jidDecode } = require('./jid-utils')
6
+
7
+ const encodeBinaryNodeJS = (node, opts = constants, buffer = [0]) => {
4
8
  const encoded = encodeBinaryNodeInner(node, opts, buffer);
5
9
  return Buffer.from(encoded);
6
10
  };
@@ -217,4 +221,8 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
217
221
  }
218
222
  return buffer;
219
223
  };
220
- //# sourceMappingURL=encode.js.map
224
+
225
+ const encodeBinaryNode = (node, opts, buffer) => {
226
+ return encodeBinaryNodeJS(node, opts || constants, buffer || [0])
227
+ }
228
+ exports.encodeBinaryNode = encodeBinaryNode