@badzz88/baileys 8.5.7 → 8.5.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 (142) hide show
  1. package/README.md +337 -1055
  2. package/WAProto/WAProto.proto +284 -264
  3. package/WAProto/index.js +5 -5
  4. package/engine-requirements.js +7 -15
  5. package/lib/Defaults/index.js +130 -187
  6. package/lib/Signal/Group/ciphertext-message.js +11 -14
  7. package/lib/Signal/Group/group-session-builder.js +29 -91
  8. package/lib/Signal/Group/group_cipher.js +81 -88
  9. package/lib/Signal/Group/index.js +12 -136
  10. package/lib/Signal/Group/keyhelper.js +15 -70
  11. package/lib/Signal/Group/sender-chain-key.js +25 -31
  12. package/lib/Signal/Group/sender-key-distribution-message.js +62 -65
  13. package/lib/Signal/Group/sender-key-message.js +65 -68
  14. package/lib/Signal/Group/sender-key-name.js +41 -43
  15. package/lib/Signal/Group/sender-key-record.js +40 -43
  16. package/lib/Signal/Group/sender-key-state.js +83 -96
  17. package/lib/Signal/Group/sender-message-key.js +25 -29
  18. package/lib/Signal/libsignal.js +425 -453
  19. package/lib/Signal/lid-mapping.js +276 -259
  20. package/lib/Socket/Client/index.js +3 -30
  21. package/lib/Socket/Client/types.js +10 -12
  22. package/lib/Socket/Client/websocket.js +53 -61
  23. package/lib/Socket/business.js +379 -422
  24. package/lib/Socket/chats.js +1193 -1816
  25. package/lib/Socket/communities.js +431 -549
  26. package/lib/Socket/graphql.js +672 -612
  27. package/lib/Socket/groups.js +374 -764
  28. package/lib/Socket/index.js +17 -38
  29. package/lib/Socket/interop.js +417 -503
  30. package/lib/Socket/messages-recv.js +1915 -2441
  31. package/lib/Socket/messages-send.js +1213 -1630
  32. package/lib/Socket/mex.js +9 -15
  33. package/lib/Socket/newsletter.js +747 -808
  34. package/lib/Socket/privacy.js +310 -441
  35. package/lib/Socket/socket.js +1025 -1022
  36. package/lib/Socket/username.js +8 -15
  37. package/lib/Store/index.js +10 -36
  38. package/lib/Store/make-cache-manager-store.js +80 -85
  39. package/lib/Store/make-in-memory-store.js +231 -591
  40. package/lib/Store/make-ordered-dictionary.js +72 -78
  41. package/lib/Store/object-repository.js +28 -25
  42. package/lib/Types/Auth.js +2 -38
  43. package/lib/Types/Bussines.js +2 -2
  44. package/lib/Types/Call.js +2 -2
  45. package/lib/Types/Chat.js +8 -4
  46. package/lib/Types/Contact.js +2 -2
  47. package/lib/Types/Events.js +2 -2
  48. package/lib/Types/GroupMetadata.js +2 -2
  49. package/lib/Types/Label.js +24 -26
  50. package/lib/Types/LabelAssociation.js +6 -8
  51. package/lib/Types/Message.js +11 -95
  52. package/lib/Types/Mex.js +111 -112
  53. package/lib/Types/Product.js +2 -2
  54. package/lib/Types/Signal.js +2 -2
  55. package/lib/Types/Socket.js +3 -2
  56. package/lib/Types/State.js +56 -70
  57. package/lib/Types/USync.js +2 -2
  58. package/lib/Types/index.js +26 -55
  59. package/lib/Utils/auth-utils.js +288 -292
  60. package/lib/Utils/browser-utils.js +47 -113
  61. package/lib/Utils/business.js +224 -240
  62. package/lib/Utils/chat-utils.js +869 -1205
  63. package/lib/Utils/companion-reg-client-utils.js +34 -41
  64. package/lib/Utils/crypto.js +107 -144
  65. package/lib/Utils/decode-wa-message.js +308 -518
  66. package/lib/Utils/event-buffer.js +611 -596
  67. package/lib/Utils/generics.js +355 -515
  68. package/lib/Utils/history.js +134 -244
  69. package/lib/Utils/identity-change-handler.js +49 -51
  70. package/lib/Utils/index.js +23 -57
  71. package/lib/Utils/link-preview.js +77 -135
  72. package/lib/Utils/logger.js +3 -9
  73. package/lib/Utils/lt-hash.js +6 -37
  74. package/lib/Utils/make-mutex.js +33 -36
  75. package/lib/Utils/message-composer.js +233 -439
  76. package/lib/Utils/message-retry-manager.js +260 -265
  77. package/lib/Utils/messages-media.js +829 -855
  78. package/lib/Utils/messages.js +961 -2528
  79. package/lib/Utils/noise-handler.js +200 -195
  80. package/lib/Utils/offline-node-processor.js +33 -35
  81. package/lib/Utils/pre-key-manager.js +102 -103
  82. package/lib/Utils/process-message.js +560 -978
  83. package/lib/Utils/reporting-utils.js +253 -257
  84. package/lib/Utils/signal.js +195 -218
  85. package/lib/Utils/stanza-ack.js +29 -31
  86. package/lib/Utils/sticker.js +109 -144
  87. package/lib/Utils/sync-action-utils.js +45 -50
  88. package/lib/Utils/tc-token-utils.js +139 -137
  89. package/lib/Utils/use-multi-file-auth-state.js +109 -109
  90. package/lib/Utils/validate-connection.js +202 -218
  91. package/lib/WABinary/constants.js +1299 -1302
  92. package/lib/WABinary/decode.js +262 -343
  93. package/lib/WABinary/encode.js +4 -12
  94. package/lib/WABinary/generic-utils.js +187 -223
  95. package/lib/WABinary/index.js +6 -33
  96. package/lib/WABinary/jid-utils.js +88 -351
  97. package/lib/WABinary/types.js +2 -2
  98. package/lib/WAM/BinaryInfo.js +9 -12
  99. package/lib/WAM/constants.js +22853 -39486
  100. package/lib/WAM/encode.js +140 -132
  101. package/lib/WAM/index.js +4 -31
  102. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +21 -23
  103. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +9 -9
  104. package/lib/WAUSync/Protocols/USyncContactProtocol.js +7 -7
  105. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +10 -10
  106. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +7 -7
  107. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +11 -12
  108. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +4 -5
  109. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +7 -7
  110. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +7 -8
  111. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +7 -7
  112. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +8 -8
  113. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +7 -7
  114. package/lib/WAUSync/Protocols/index.js +12 -39
  115. package/lib/WAUSync/USyncQuery.js +67 -49
  116. package/lib/WAUSync/USyncUser.js +11 -5
  117. package/lib/WAUSync/index.js +3 -31
  118. package/lib/index.js +15 -48
  119. package/package.json +129 -99
  120. package/LICENSE +0 -21
  121. package/lib/Defaults/phonenumber-mcc.json +0 -223
  122. package/lib/Socket/aigroups.js +0 -240
  123. package/lib/Socket/interactive-handler.js +0 -522
  124. package/lib/Socket/managed-account.js +0 -183
  125. package/lib/Socket/registration.js +0 -427
  126. package/lib/Socket/text-router.js +0 -83
  127. package/lib/Types/Newsletter.js +0 -121
  128. package/lib/Utils/command-loader.d.ts +0 -31
  129. package/lib/Utils/command-loader.js +0 -100
  130. package/lib/Utils/consumer-application.js +0 -107
  131. package/lib/Utils/curve25519-js.js +0 -275
  132. package/lib/Utils/group-history.js +0 -60
  133. package/lib/Utils/jid-display-normalization.js +0 -218
  134. package/lib/Utils/meta-ai-msmsg.js +0 -262
  135. package/lib/Utils/native-bridge.js +0 -82
  136. package/lib/Utils/session-pool.d.ts +0 -16
  137. package/lib/Utils/session-pool.js +0 -94
  138. package/lib/Utils/sticker.d.ts +0 -17
  139. package/lib/Utils/view-once-cache.d.ts +0 -9
  140. package/lib/Utils/view-once-cache.js +0 -79
  141. package/lib/Utils/voip-rekey.js +0 -22
  142. package/lib/antiban.js +0 -4637
@@ -1,343 +1,262 @@
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
+ 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,10 +1,6 @@
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]) => {
1
+ import * as constants from './constants.js';
2
+ import { jidDecode } from './jid-utils.js';
3
+ export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
8
4
  const encoded = encodeBinaryNodeInner(node, opts, buffer);
9
5
  return Buffer.from(encoded);
10
6
  };
@@ -221,8 +217,4 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
221
217
  }
222
218
  return buffer;
223
219
  };
224
-
225
- const encodeBinaryNode = (node, opts, buffer) => {
226
- return encodeBinaryNodeJS(node, opts || constants, buffer || [0])
227
- }
228
- exports.encodeBinaryNode = encodeBinaryNode
220
+ //# sourceMappingURL=encode.js.map