sanka-baileyss 2.3.2 → 2.3.3

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 (158) hide show
  1. package/WAProto/BotMetadata/BotMetadata.d.ts +594 -1
  2. package/WAProto/BotMetadata/BotMetadata.js +1910 -148
  3. package/WAProto/BotMetadata/BotMetadata.proto +36 -0
  4. package/WAProto/E2E/E2E.d.ts +570 -6
  5. package/WAProto/E2E/E2E.js +1591 -26
  6. package/WAProto/E2E/E2E.proto +10 -1
  7. package/WAProto/HistorySync/HistorySync.d.ts +806 -6
  8. package/WAProto/HistorySync/HistorySync.js +2193 -29
  9. package/WAProto/HistorySync/HistorySync.proto +2 -1
  10. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.d.ts +570 -6
  11. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.js +1591 -26
  12. package/WAProto/StatusAttributions/StatusAttributions.d.ts +2 -1
  13. package/WAProto/StatusAttributions/StatusAttributions.js +7 -0
  14. package/WAProto/StatusAttributions/StatusAttributions.proto +1 -0
  15. package/WAProto/SyncAction/SyncAction.d.ts +230 -0
  16. package/WAProto/SyncAction/SyncAction.js +578 -2
  17. package/WAProto/SyncAction/SyncAction.proto +13 -1
  18. package/WAProto/Web/Web.d.ts +570 -6
  19. package/WAProto/Web/Web.js +1591 -26
  20. package/lib/Defaults/baileys-version.json +2 -2
  21. package/lib/Defaults/index.d.ts +42 -25
  22. package/lib/Defaults/index.js +6 -2
  23. package/lib/Signal/libsignal.d.ts +1 -0
  24. package/lib/Socket/Client/index.js +13 -9
  25. package/lib/Socket/Client/types.d.ts +11 -10
  26. package/lib/Socket/Client/types.js +11 -7
  27. package/lib/Socket/Client/websocket.d.ts +9 -8
  28. package/lib/Socket/Client/websocket.js +52 -47
  29. package/lib/Socket/business.d.ts +185 -182
  30. package/lib/Socket/business.js +258 -249
  31. package/lib/Socket/chats.d.ts +90 -87
  32. package/lib/Socket/chats.js +1095 -888
  33. package/lib/Socket/community.d.ts +131 -0
  34. package/lib/Socket/community.js +369 -0
  35. package/lib/Socket/groups.d.ts +50 -41
  36. package/lib/Socket/groups.js +10 -3
  37. package/lib/Socket/index.d.ts +5 -1
  38. package/lib/Socket/index.js +19 -5
  39. package/lib/Socket/messages-recv.d.ts +6 -2
  40. package/lib/Socket/messages-recv.js +31 -8
  41. package/lib/Socket/messages-send.d.ts +5 -1
  42. package/lib/Socket/messages-send.js +1293 -1351
  43. package/lib/Socket/newsletter.d.ts +142 -135
  44. package/lib/Socket/newsletter.js +274 -230
  45. package/lib/Socket/socket.d.ts +37 -35
  46. package/lib/Socket/usync.d.ts +31 -30
  47. package/lib/Socket/usync.js +74 -62
  48. package/lib/Types/Auth.d.ts +89 -72
  49. package/lib/Types/Auth.js +1 -0
  50. package/lib/Types/Call.d.ts +11 -10
  51. package/lib/Types/Call.js +1 -0
  52. package/lib/Types/Chat.d.ts +85 -62
  53. package/lib/Types/Chat.js +4 -1
  54. package/lib/Types/Contact.js +1 -0
  55. package/lib/Types/Events.d.ts +209 -191
  56. package/lib/Types/Events.js +1 -0
  57. package/lib/Types/GroupMetadata.d.ts +12 -27
  58. package/lib/Types/GroupMetadata.js +1 -0
  59. package/lib/Types/Label.d.ts +41 -39
  60. package/lib/Types/Label.js +24 -21
  61. package/lib/Types/LabelAssociation.d.ts +18 -12
  62. package/lib/Types/LabelAssociation.js +6 -3
  63. package/lib/Types/Message.d.ts +3 -2
  64. package/lib/Types/Message.js +7 -3
  65. package/lib/Types/MexUpdates.d.ts +9 -0
  66. package/lib/Types/MexUpdates.js +18 -0
  67. package/lib/Types/Newsletter.d.ts +84 -67
  68. package/lib/Types/Newsletter.js +30 -23
  69. package/lib/Types/Product.d.ts +70 -56
  70. package/lib/Types/Product.js +1 -0
  71. package/lib/Types/Signal.d.ts +46 -35
  72. package/lib/Types/Signal.js +1 -0
  73. package/lib/Types/Socket.d.ts +105 -101
  74. package/lib/Types/Socket.js +1 -0
  75. package/lib/Types/State.d.ts +37 -23
  76. package/lib/Types/State.js +13 -1
  77. package/lib/Types/USync.d.ts +21 -20
  78. package/lib/Types/USync.js +1 -0
  79. package/lib/Types/index.d.ts +51 -42
  80. package/lib/Types/index.js +25 -19
  81. package/lib/Utils/auth-utils.d.ts +14 -11
  82. package/lib/Utils/auth-utils.js +189 -182
  83. package/lib/Utils/baileys-event-stream.d.ts +12 -10
  84. package/lib/Utils/baileys-event-stream.js +52 -46
  85. package/lib/Utils/business.d.ts +15 -8
  86. package/lib/Utils/business.js +229 -217
  87. package/lib/Utils/chat-utils.d.ts +22 -12
  88. package/lib/Utils/chat-utils.js +57 -27
  89. package/lib/Utils/crypto.d.ts +31 -15
  90. package/lib/Utils/crypto.js +136 -107
  91. package/lib/Utils/decode-wa-message.d.ts +34 -245
  92. package/lib/Utils/decode-wa-message.js +205 -217
  93. package/lib/Utils/event-buffer.d.ts +30 -26
  94. package/lib/Utils/event-buffer.js +519 -515
  95. package/lib/Utils/history.d.ts +2 -8
  96. package/lib/Utils/index.js +13 -9
  97. package/lib/Utils/link-preview.d.ts +16 -14
  98. package/lib/Utils/link-preview.js +100 -92
  99. package/lib/Utils/logger.d.ts +9 -7
  100. package/lib/Utils/logger.js +1 -1
  101. package/lib/Utils/lt-hash.d.ts +10 -8
  102. package/lib/Utils/lt-hash.js +49 -44
  103. package/lib/Utils/make-mutex.d.ts +4 -2
  104. package/lib/Utils/make-mutex.js +43 -39
  105. package/lib/Utils/messages-media.d.ts +75 -50
  106. package/lib/Utils/messages.d.ts +60 -35
  107. package/lib/Utils/messages.js +87 -8
  108. package/lib/Utils/noise-handler.d.ts +13 -12
  109. package/lib/Utils/noise-handler.js +143 -138
  110. package/lib/Utils/process-message.d.ts +32 -24
  111. package/lib/Utils/process-message.js +387 -367
  112. package/lib/Utils/signal.d.ts +24 -15
  113. package/lib/Utils/signal.js +143 -131
  114. package/lib/Utils/use-mongo-file-auth-state.d.ts +3 -2
  115. package/lib/Utils/use-mongo-file-auth-state.js +72 -63
  116. package/lib/Utils/use-multi-file-auth-state.d.ts +9 -8
  117. package/lib/Utils/use-multi-file-auth-state.js +110 -102
  118. package/lib/Utils/use-single-file-auth-state.d.ts +8 -7
  119. package/lib/Utils/use-single-file-auth-state.js +66 -61
  120. package/lib/Utils/validate-connection.d.ts +5 -2
  121. package/lib/Utils/validate-connection.js +7 -6
  122. package/lib/WABinary/constants.d.ts +24 -21
  123. package/lib/WABinary/constants.js +1302 -31
  124. package/lib/WABinary/decode.d.ts +4 -1
  125. package/lib/WABinary/decode.js +267 -234
  126. package/lib/WABinary/encode.d.ts +1 -0
  127. package/lib/WABinary/encode.js +248 -226
  128. package/lib/WABinary/generic-utils.d.ts +13 -1
  129. package/lib/WABinary/generic-utils.js +113 -96
  130. package/lib/WABinary/index.js +13 -9
  131. package/lib/WABinary/jid-utils.d.ts +23 -4
  132. package/lib/WABinary/jid-utils.js +58 -39
  133. package/lib/WABinary/types.d.ts +15 -11
  134. package/lib/WABinary/types.js +1 -0
  135. package/lib/WAM/BinaryInfo.d.ts +14 -14
  136. package/lib/WAM/BinaryInfo.js +11 -8
  137. package/lib/WAM/constants.d.ts +34 -26
  138. package/lib/WAM/constants.js +15358 -15349
  139. package/lib/WAM/encode.d.ts +1 -0
  140. package/lib/WAM/encode.js +153 -144
  141. package/lib/WAM/index.js +13 -9
  142. package/lib/index.js +11 -11
  143. package/package.json +1 -2
  144. package/WASignalGroup/GroupProtocol.js +0 -1771
  145. package/WASignalGroup/ciphertext_message.js +0 -10
  146. package/WASignalGroup/group_cipher.js +0 -103
  147. package/WASignalGroup/group_session_builder.js +0 -39
  148. package/WASignalGroup/index.js +0 -5
  149. package/WASignalGroup/keyhelper.js +0 -17
  150. package/WASignalGroup/protobufs.js +0 -2
  151. package/WASignalGroup/queue_job.js +0 -64
  152. package/WASignalGroup/sender_chain_key.js +0 -38
  153. package/WASignalGroup/sender_key_distribution_message.js +0 -68
  154. package/WASignalGroup/sender_key_message.js +0 -79
  155. package/WASignalGroup/sender_key_name.js +0 -59
  156. package/WASignalGroup/sender_key_record.js +0 -47
  157. package/WASignalGroup/sender_key_state.js +0 -110
  158. package/WASignalGroup/sender_message_key.js +0 -30
@@ -1,6 +1,9 @@
1
1
  import type { BinaryNode, BinaryNodeCodingOptions } from './types'
2
+
2
3
  export declare const decompressingIfRequired: (buffer: Buffer) => Promise<Buffer>
4
+
3
5
  export declare const decodeDecompressedBinaryNode: (buffer: Buffer, opts: Pick<BinaryNodeCodingOptions, 'DOUBLE_BYTE_TOKENS' | 'SINGLE_BYTE_TOKENS' | 'TAGS'>, indexRef?: {
4
- index: number
6
+ index: number
5
7
  }) => BinaryNode
8
+
6
9
  export declare const decodeBinaryNode: (buff: Buffer) => Promise<BinaryNode>
@@ -1,255 +1,288 @@
1
1
  "use strict"
2
+
2
3
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k
4
- var desc = Object.getOwnPropertyDescriptor(m, k)
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k] } }
7
- }
8
- Object.defineProperty(o, k2, desc)
4
+ if (k2 === undefined) k2 = k
5
+ var desc = Object.getOwnPropertyDescriptor(m, k)
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k] } }
8
+ }
9
+ Object.defineProperty(o, k2, desc)
9
10
  }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k
11
- o[k2] = m[k]
11
+ if (k2 === undefined) k2 = k
12
+ o[k2] = m[k]
12
13
  }))
14
+
13
15
  var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v })
16
+ Object.defineProperty(o, "default", { enumerable: true, value: v })
15
17
  }) : function(o, v) {
16
- o["default"] = v
18
+ o["default"] = v
17
19
  })
20
+
18
21
  var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod
20
- var result = {}
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k)
22
- __setModuleDefault(result, mod)
23
- return result
22
+ if (mod && mod.__esModule) return mod
23
+ var result = {}
24
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k)
25
+ __setModuleDefault(result, mod)
26
+ return result
24
27
  }
28
+
25
29
  Object.defineProperty(exports, "__esModule", { value: true })
30
+
26
31
  const util_1 = require("util")
27
32
  const zlib_1 = require("zlib")
28
33
  const constants = __importStar(require("./constants"))
29
34
  const jid_utils_1 = require("./jid-utils")
30
35
  const inflatePromise = util_1.promisify(zlib_1.inflate)
36
+
31
37
  const decompressingIfRequired = async (buffer) => {
32
- if (2 & buffer.readUInt8()) {
33
- buffer = await inflatePromise(buffer.slice(1))
34
- }
35
- else { // nodes with no compression have a 0x00 prefix, we remove that
36
- buffer = buffer.slice(1)
37
- }
38
- return buffer
39
- }
38
+ if (2 & buffer.readUInt8()) {
39
+ buffer = await inflatePromise(buffer.slice(1))
40
+ }
41
+ else { // nodes with no compression have a 0x00 prefix, we remove that
42
+ buffer = buffer.slice(1)
43
+ }
44
+ return buffer
45
+ }
46
+
40
47
  const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
41
- const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts
42
- const checkEOS = (length) => {
43
- if (indexRef.index + length > buffer.length) {
44
- throw new Error('end of stream')
45
- }
46
- }
47
- const next = () => {
48
- const value = buffer[indexRef.index]
49
- indexRef.index += 1
50
- return value
51
- }
52
- const readByte = () => {
53
- checkEOS(1)
54
- return next()
55
- }
56
- const readBytes = (n) => {
57
- checkEOS(n)
58
- const value = buffer.slice(indexRef.index, indexRef.index + n)
59
- indexRef.index += n
60
- return value
61
- }
62
- const readStringFromChars = (length) => {
63
- return readBytes(length).toString('utf-8')
64
- }
65
- const readInt = (n, littleEndian = false) => {
66
- checkEOS(n)
67
- let val = 0
68
- for (let i = 0; i < n; i++) {
69
- const shift = littleEndian ? i : n - 1 - i
70
- val |= next() << (shift * 8)
71
- }
72
- return val
73
- }
74
- const readInt20 = () => {
75
- checkEOS(3)
76
- return ((next() & 15) << 16) + (next() << 8) + next()
77
- }
78
- const unpackHex = (value) => {
79
- if (value >= 0 && value < 16) {
80
- return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10
81
- }
82
- throw new Error('invalid hex: ' + value)
83
- }
84
- const unpackNibble = (value) => {
85
- if (value >= 0 && value <= 9) {
86
- return '0'.charCodeAt(0) + value
87
- }
88
- switch (value) {
89
- case 10:
90
- return '-'.charCodeAt(0)
91
- case 11:
92
- return '.'.charCodeAt(0)
93
- case 15:
94
- return '\0'.charCodeAt(0)
95
- default:
96
- throw new Error('invalid nibble: ' + value)
97
- }
98
- }
99
- const unpackByte = (tag, value) => {
100
- if (tag === TAGS.NIBBLE_8) {
101
- return unpackNibble(value)
102
- }
103
- else if (tag === TAGS.HEX_8) {
104
- return unpackHex(value)
105
- }
106
- else {
107
- throw new Error('unknown tag: ' + tag)
108
- }
109
- }
110
- const readPacked8 = (tag) => {
111
- const startByte = readByte()
112
- let value = ''
113
- for (let i = 0; i < (startByte & 127); i++) {
114
- const curByte = readByte()
115
- value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
116
- value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
117
- }
118
- if (startByte >> 7 !== 0) {
119
- value = value.slice(0, -1)
120
- }
121
- return value
122
- }
123
- const isListTag = (tag) => {
124
- return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16
125
- }
126
- const readListSize = (tag) => {
127
- switch (tag) {
128
- case TAGS.LIST_EMPTY:
129
- return 0
130
- case TAGS.LIST_8:
131
- return readByte()
132
- case TAGS.LIST_16:
133
- return readInt(2)
134
- default:
135
- throw new Error('invalid tag for list size: ' + tag)
136
- }
137
- }
138
- const readJidPair = () => {
139
- const i = readString(readByte())
140
- const j = readString(readByte())
141
- if (j) {
142
- return (i || '') + '@' + j
143
- }
144
- throw new Error('invalid jid pair: ' + i + ', ' + j)
145
- }
146
- const readAdJid = () => {
147
- const agent = readByte()
148
- const device = readByte()
149
- const user = readString(readByte())
150
- return jid_utils_1.jidEncode(user, agent === 0 ? 's.whatsapp.net' : 'lid', device)
151
- }
152
- const readString = (tag) => {
153
- if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
154
- return SINGLE_BYTE_TOKENS[tag] || ''
155
- }
156
- switch (tag) {
157
- case TAGS.DICTIONARY_0:
158
- case TAGS.DICTIONARY_1:
159
- case TAGS.DICTIONARY_2:
160
- case TAGS.DICTIONARY_3:
161
- return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
162
- case TAGS.LIST_EMPTY:
163
- return ''
164
- case TAGS.BINARY_8:
165
- return readStringFromChars(readByte())
166
- case TAGS.BINARY_20:
167
- return readStringFromChars(readInt20())
168
- case TAGS.BINARY_32:
169
- return readStringFromChars(readInt(4))
170
- case TAGS.JID_PAIR:
171
- return readJidPair()
172
- case TAGS.AD_JID:
173
- return readAdJid()
174
- case TAGS.HEX_8:
175
- case TAGS.NIBBLE_8:
176
- return readPacked8(tag)
177
- default:
178
- throw new Error('invalid string with tag: ' + tag)
179
- }
180
- }
181
- const readList = (tag) => {
182
- const items = []
183
- const size = readListSize(tag)
184
- for (let i = 0; i < size; i++) {
185
- items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
186
- }
187
- return items
188
- }
189
- const getTokenDouble = (index1, index2) => {
190
- const dict = DOUBLE_BYTE_TOKENS[index1]
191
- if (!dict) {
192
- throw new Error(`Invalid double token dict (${index1})`)
193
- }
194
- const value = dict[index2]
195
- if (typeof value === 'undefined') {
196
- throw new Error(`Invalid double token (${index2})`)
197
- }
198
- return value
199
- }
200
- const listSize = readListSize(readByte())
201
- const header = readString(readByte())
202
- if (!listSize || !header.length) {
203
- throw new Error('invalid node')
204
- }
205
- const attrs = {}
206
- let data
207
- if (listSize === 0 || !header) {
208
- throw new Error('invalid node')
209
- }
210
- // read the attributes in
211
- const attributesLength = (listSize - 1) >> 1
212
- for (let i = 0; i < attributesLength; i++) {
213
- const key = readString(readByte())
214
- const value = readString(readByte())
215
- attrs[key] = value
216
- }
217
- if (listSize % 2 === 0) {
218
- const tag = readByte()
219
- if (isListTag(tag)) {
220
- data = readList(tag)
221
- }
222
- else {
223
- let decoded
224
- switch (tag) {
225
- case TAGS.BINARY_8:
226
- decoded = readBytes(readByte())
227
- break
228
- case TAGS.BINARY_20:
229
- decoded = readBytes(readInt20())
230
- break
231
- case TAGS.BINARY_32:
232
- decoded = readBytes(readInt(4))
233
- break
234
- default:
235
- decoded = readString(tag)
236
- break
237
- }
238
- data = decoded
239
- }
240
- }
241
- return {
242
- tag: header,
243
- attrs,
244
- content: data
245
- }
246
- }
48
+ const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts
49
+ const checkEOS = (length) => {
50
+ if (indexRef.index + length > buffer.length) {
51
+ throw new Error('end of stream')
52
+ }
53
+ }
54
+
55
+ const next = () => {
56
+ const value = buffer[indexRef.index]
57
+ indexRef.index += 1
58
+ return value
59
+ }
60
+
61
+ const readByte = () => {
62
+ checkEOS(1)
63
+ return next()
64
+ }
65
+
66
+ const readBytes = (n) => {
67
+ checkEOS(n)
68
+ const value = buffer.slice(indexRef.index, indexRef.index + n)
69
+ indexRef.index += n
70
+ return value
71
+ }
72
+
73
+ const readStringFromChars = (length) => {
74
+ return readBytes(length).toString('utf-8')
75
+ }
76
+
77
+ const readInt = (n, littleEndian = false) => {
78
+ checkEOS(n)
79
+ let val = 0
80
+ for (let i = 0; i < n; i++) {
81
+ const shift = littleEndian ? i : n - 1 - i
82
+ val |= next() << (shift * 8)
83
+ }
84
+ return val
85
+ }
86
+
87
+ const readInt20 = () => {
88
+ checkEOS(3)
89
+ return ((next() & 15) << 16) + (next() << 8) + next()
90
+ }
91
+
92
+ const unpackHex = (value) => {
93
+ if (value >= 0 && value < 16) {
94
+ return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10
95
+ }
96
+ throw new Error('invalid hex: ' + value)
97
+ }
98
+
99
+ const unpackNibble = (value) => {
100
+ if (value >= 0 && value <= 9) {
101
+ return '0'.charCodeAt(0) + value
102
+ }
103
+ switch (value) {
104
+ case 10:
105
+ return '-'.charCodeAt(0)
106
+ case 11:
107
+ return '.'.charCodeAt(0)
108
+ case 15:
109
+ return '\0'.charCodeAt(0)
110
+ default:
111
+ throw new Error('invalid nibble: ' + value)
112
+ }
113
+ }
114
+
115
+ const unpackByte = (tag, value) => {
116
+ if (tag === TAGS.NIBBLE_8) {
117
+ return unpackNibble(value)
118
+ }
119
+ else if (tag === TAGS.HEX_8) {
120
+ return unpackHex(value)
121
+ }
122
+ else {
123
+ throw new Error('unknown tag: ' + tag)
124
+ }
125
+ }
126
+
127
+ const readPacked8 = (tag) => {
128
+ const startByte = readByte()
129
+ let value = ''
130
+ for (let i = 0; i < (startByte & 127); i++) {
131
+ const curByte = readByte()
132
+ value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
133
+ value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
134
+ }
135
+ if (startByte >> 7 !== 0) {
136
+ value = value.slice(0, -1)
137
+ }
138
+ return value
139
+ }
140
+
141
+ const isListTag = (tag) => {
142
+ return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16
143
+ }
144
+
145
+ const readListSize = (tag) => {
146
+ switch (tag) {
147
+ case TAGS.LIST_EMPTY:
148
+ return 0
149
+ case TAGS.LIST_8:
150
+ return readByte()
151
+ case TAGS.LIST_16:
152
+ return readInt(2)
153
+ default:
154
+ throw new Error('invalid tag for list size: ' + tag)
155
+ }
156
+ }
157
+
158
+ const readJidPair = () => {
159
+ const i = readString(readByte())
160
+ const j = readString(readByte())
161
+ if (j) {
162
+ return (i || '') + '@' + j
163
+ }
164
+ throw new Error('invalid jid pair: ' + i + ', ' + j)
165
+ }
166
+
167
+ const readAdJid = () => {
168
+ const agent = readByte()
169
+ const device = readByte()
170
+ const user = readString(readByte())
171
+ return jid_utils_1.jidEncode(user, agent === 0 ? 's.whatsapp.net' : 'lid', device)
172
+ }
173
+
174
+ const readString = (tag) => {
175
+ if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
176
+ return SINGLE_BYTE_TOKENS[tag] || ''
177
+ }
178
+ switch (tag) {
179
+ case TAGS.DICTIONARY_0:
180
+ case TAGS.DICTIONARY_1:
181
+ case TAGS.DICTIONARY_2:
182
+ case TAGS.DICTIONARY_3:
183
+ return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
184
+ case TAGS.LIST_EMPTY:
185
+ return ''
186
+ case TAGS.BINARY_8:
187
+ return readStringFromChars(readByte())
188
+ case TAGS.BINARY_20:
189
+ return readStringFromChars(readInt20())
190
+ case TAGS.BINARY_32:
191
+ return readStringFromChars(readInt(4))
192
+ case TAGS.JID_PAIR:
193
+ return readJidPair()
194
+ case TAGS.AD_JID:
195
+ return readAdJid()
196
+ case TAGS.HEX_8:
197
+ case TAGS.NIBBLE_8:
198
+ return readPacked8(tag)
199
+ default:
200
+ throw new Error('invalid string with tag: ' + tag)
201
+ }
202
+ }
203
+
204
+ const readList = (tag) => {
205
+ const items = []
206
+ const size = readListSize(tag)
207
+ for (let i = 0; i < size; i++) {
208
+ items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
209
+ }
210
+ return items
211
+ }
212
+
213
+ const getTokenDouble = (index1, index2) => {
214
+ const dict = DOUBLE_BYTE_TOKENS[index1]
215
+ if (!dict) {
216
+ throw new Error(`Invalid double token dict (${index1})`)
217
+ }
218
+ const value = dict[index2]
219
+ if (typeof value === 'undefined') {
220
+ throw new Error(`Invalid double token (${index2})`)
221
+ }
222
+ return value
223
+ }
224
+
225
+ const listSize = readListSize(readByte())
226
+ const header = readString(readByte())
227
+
228
+ if (!listSize || !header.length) {
229
+ throw new Error('invalid node')
230
+ }
231
+
232
+ const attrs = {}
233
+
234
+ let data
235
+
236
+ if (listSize === 0 || !header) {
237
+ throw new Error('invalid node')
238
+ }
239
+
240
+ // read the attributes in
241
+ const attributesLength = (listSize - 1) >> 1
242
+ for (let i = 0; i < attributesLength; i++) {
243
+ const key = readString(readByte())
244
+ const value = readString(readByte())
245
+ attrs[key] = value
246
+ }
247
+
248
+ if (listSize % 2 === 0) {
249
+ const tag = readByte()
250
+ if (isListTag(tag)) {
251
+ data = readList(tag)
252
+ }
253
+ else {
254
+ let decoded
255
+ switch (tag) {
256
+ case TAGS.BINARY_8:
257
+ decoded = readBytes(readByte())
258
+ break
259
+ case TAGS.BINARY_20:
260
+ decoded = readBytes(readInt20())
261
+ break
262
+ case TAGS.BINARY_32:
263
+ decoded = readBytes(readInt(4))
264
+ break
265
+ default:
266
+ decoded = readString(tag)
267
+ break
268
+ }
269
+ data = decoded
270
+ }
271
+ }
272
+ return {
273
+ tag: header,
274
+ attrs,
275
+ content: data
276
+ }
277
+ }
278
+
247
279
  const decodeBinaryNode = async (buff) => {
248
- const decompBuff = await decompressingIfRequired(buff)
249
- return decodeDecompressedBinaryNode(decompBuff, constants)
280
+ const decompBuff = await decompressingIfRequired(buff)
281
+ return decodeDecompressedBinaryNode(decompBuff, constants)
250
282
  }
283
+
251
284
  module.exports = {
252
- decompressingIfRequired,
253
- decodeDecompressedBinaryNode,
254
- decodeBinaryNode
285
+ decompressingIfRequired,
286
+ decodeDecompressedBinaryNode,
287
+ decodeBinaryNode
255
288
  }
@@ -1,2 +1,3 @@
1
1
  import type { BinaryNode, BinaryNodeCodingOptions } from './types'
2
+
2
3
  export declare const encodeBinaryNode: (node: BinaryNode, opts?: Pick<BinaryNodeCodingOptions, 'TAGS' | 'TOKEN_MAP'>, buffer?: number[]) => Buffer