@mrlegendbot/baileys 1.2.3 → 1.2.5

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 (212) hide show
  1. package/README.upstream.md +1312 -0
  2. package/WAProto/WAProto.json +1 -0
  3. package/WAProto/WAProto.proto +6275 -0
  4. package/WAProto/compiler.js +550 -0
  5. package/WAProto/index.js +2 -97691
  6. package/lib/Defaults/index.js +89 -92
  7. package/lib/Signal/Group/ciphertext-message.js +9 -10
  8. package/lib/Signal/Group/group-session-builder.js +21 -22
  9. package/lib/Signal/Group/group_cipher.js +75 -69
  10. package/lib/Signal/Group/index.js +0 -1
  11. package/lib/Signal/Group/keyhelper.js +9 -10
  12. package/lib/Signal/Group/sender-chain-key.js +21 -22
  13. package/lib/Signal/Group/sender-key-distribution-message.js +51 -60
  14. package/lib/Signal/Group/sender-key-message.js +56 -62
  15. package/lib/Signal/Group/sender-key-name.js +38 -40
  16. package/lib/Signal/Group/sender-key-record.js +33 -34
  17. package/lib/Signal/Group/sender-key-state.js +72 -76
  18. package/lib/Signal/Group/sender-message-key.js +22 -23
  19. package/lib/Signal/libsignal.js +506 -405
  20. package/lib/Signal/lid-mapping.js +276 -258
  21. package/lib/Socket/Client/index.js +0 -1
  22. package/lib/Socket/Client/types.js +6 -7
  23. package/lib/Socket/Client/websocket.js +48 -49
  24. package/lib/Socket/business.js +349 -369
  25. package/lib/Socket/chats.js +1172 -1148
  26. package/lib/Socket/communities.js +473 -413
  27. package/lib/Socket/groups.js +387 -331
  28. package/lib/Socket/index.js +6 -8
  29. package/lib/Socket/messages-recv.js +1782 -1719
  30. package/lib/Socket/messages-send.js +1554 -2450
  31. package/lib/Socket/mex.js +40 -35
  32. package/lib/Socket/newsletter.js +239 -191
  33. package/lib/Socket/socket.js +992 -923
  34. package/lib/Store/index.js +8 -0
  35. package/lib/Store/keyed-db.js +108 -0
  36. package/lib/Store/make-cache-manager-store.js +70 -0
  37. package/lib/Store/make-in-memory-store.js +417 -0
  38. package/lib/Store/make-ordered-dictionary.js +69 -0
  39. package/lib/Store/object-repository.js +27 -0
  40. package/lib/Types/Auth.js +0 -1
  41. package/lib/Types/Bussines.js +0 -1
  42. package/lib/Types/Call.js +0 -1
  43. package/lib/Types/Chat.js +1 -8
  44. package/lib/Types/Contact.js +0 -1
  45. package/lib/Types/Events.js +1 -2
  46. package/lib/Types/GroupMetadata.js +0 -1
  47. package/lib/Types/Label.js +20 -22
  48. package/lib/Types/LabelAssociation.js +2 -4
  49. package/lib/Types/Message.js +3 -5
  50. package/lib/Types/Mex.js +26 -31
  51. package/lib/Types/Product.js +0 -1
  52. package/lib/Types/Signal.js +1 -2
  53. package/lib/Types/Socket.js +2 -3
  54. package/lib/Types/State.js +4 -52
  55. package/lib/Types/USync.js +0 -1
  56. package/lib/Types/index.js +10 -11
  57. package/lib/Utils/auth-utils.js +240 -277
  58. package/lib/Utils/browser-utils.js +20 -23
  59. package/lib/Utils/business.js +200 -204
  60. package/lib/Utils/chat-utils.js +836 -810
  61. package/lib/Utils/companion-reg-client-utils.js +23 -25
  62. package/lib/Utils/crypto.js +70 -86
  63. package/lib/Utils/decode-wa-message.js +277 -284
  64. package/lib/Utils/event-buffer.js +592 -595
  65. package/lib/Utils/generics.js +312 -335
  66. package/lib/Utils/history.js +123 -115
  67. package/lib/Utils/index.js +1 -2
  68. package/lib/Utils/link-preview.js +74 -73
  69. package/lib/Utils/logger.js +3 -2
  70. package/lib/Utils/lt-hash.js +0 -6
  71. package/lib/Utils/make-mutex.js +27 -27
  72. package/lib/Utils/message-retry-manager.js +213 -254
  73. package/lib/Utils/messages-media.js +741 -715
  74. package/lib/Utils/messages.js +791 -827
  75. package/lib/Utils/noise-handler.js +215 -187
  76. package/lib/Utils/pre-key-manager.js +69 -98
  77. package/lib/Utils/process-message.js +617 -586
  78. package/lib/Utils/reporting-utils.js +553 -234
  79. package/lib/Utils/signal.js +217 -170
  80. package/lib/Utils/stanza-ack.js +27 -35
  81. package/lib/Utils/sync-action-utils.js +37 -41
  82. package/lib/Utils/tc-token-utils.js +120 -141
  83. package/lib/Utils/use-multi-file-auth-state.js +87 -105
  84. package/lib/Utils/use-sqlite-auth-state.js +162 -0
  85. package/lib/Utils/validate-connection.js +190 -188
  86. package/lib/WABinary/constants.js +28 -1294
  87. package/lib/WABinary/decode.js +245 -249
  88. package/lib/WABinary/encode.js +201 -206
  89. package/lib/WABinary/generic-utils.js +86 -89
  90. package/lib/WABinary/index.js +0 -1
  91. package/lib/WABinary/jid-utils.js +68 -77
  92. package/lib/WABinary/types.js +0 -1
  93. package/lib/WAM/BinaryInfo.js +7 -8
  94. package/lib/WAM/constants.js +19892 -22853
  95. package/lib/WAM/encode.js +121 -132
  96. package/lib/WAM/index.js +0 -1
  97. package/lib/WAUSync/Protocols/USyncContactProtocol.js +46 -43
  98. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +47 -48
  99. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -23
  100. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -33
  101. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -20
  102. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -44
  103. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
  104. package/lib/WAUSync/Protocols/index.js +0 -1
  105. package/lib/WAUSync/USyncQuery.js +79 -88
  106. package/lib/WAUSync/USyncUser.js +28 -29
  107. package/lib/WAUSync/index.js +0 -1
  108. package/lib/index.js +2 -2
  109. package/package.json +22 -86
  110. package/WAProto/GenerateStatics.sh +0 -3
  111. package/WAProto/fix-imports.js +0 -85
  112. package/WAProto/index.d.ts +0 -14017
  113. package/lib/Defaults/index.d.ts +0 -75
  114. package/lib/Signal/Group/ciphertext-message.d.ts +0 -10
  115. package/lib/Signal/Group/group-session-builder.d.ts +0 -15
  116. package/lib/Signal/Group/group_cipher.d.ts +0 -17
  117. package/lib/Signal/Group/index.d.ts +0 -12
  118. package/lib/Signal/Group/keyhelper.d.ts +0 -11
  119. package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
  120. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
  121. package/lib/Signal/Group/sender-key-message.d.ts +0 -19
  122. package/lib/Signal/Group/sender-key-name.d.ts +0 -18
  123. package/lib/Signal/Group/sender-key-record.d.ts +0 -31
  124. package/lib/Signal/Group/sender-key-state.d.ts +0 -39
  125. package/lib/Signal/Group/sender-message-key.d.ts +0 -12
  126. package/lib/Signal/libsignal.d.ts +0 -5
  127. package/lib/Signal/lid-mapping.d.ts +0 -23
  128. package/lib/Socket/Client/index.d.ts +0 -3
  129. package/lib/Socket/Client/types.d.ts +0 -16
  130. package/lib/Socket/Client/websocket.d.ts +0 -13
  131. package/lib/Socket/business.d.ts +0 -217
  132. package/lib/Socket/chats.d.ts +0 -125
  133. package/lib/Socket/communities.d.ts +0 -273
  134. package/lib/Socket/groups.d.ts +0 -161
  135. package/lib/Socket/index.d.ts +0 -260
  136. package/lib/Socket/messages-recv.d.ts +0 -203
  137. package/lib/Socket/messages-send.d.ts +0 -199
  138. package/lib/Socket/mex.d.ts +0 -3
  139. package/lib/Socket/newsletter.d.ts +0 -170
  140. package/lib/Socket/socket.d.ts +0 -59
  141. package/lib/Types/Auth.d.ts +0 -117
  142. package/lib/Types/Bussines.d.ts +0 -25
  143. package/lib/Types/Call.d.ts +0 -15
  144. package/lib/Types/Chat.d.ts +0 -124
  145. package/lib/Types/Contact.d.ts +0 -26
  146. package/lib/Types/Events.d.ts +0 -256
  147. package/lib/Types/GroupMetadata.d.ts +0 -71
  148. package/lib/Types/Label.d.ts +0 -47
  149. package/lib/Types/LabelAssociation.d.ts +0 -30
  150. package/lib/Types/Message.d.ts +0 -320
  151. package/lib/Types/Mex.d.ts +0 -141
  152. package/lib/Types/Product.d.ts +0 -79
  153. package/lib/Types/Signal.d.ts +0 -87
  154. package/lib/Types/Socket.d.ts +0 -136
  155. package/lib/Types/State.d.ts +0 -97
  156. package/lib/Types/USync.d.ts +0 -26
  157. package/lib/Types/index.d.ts +0 -66
  158. package/lib/Utils/auth-utils.d.ts +0 -24
  159. package/lib/Utils/browser-utils.d.ts +0 -4
  160. package/lib/Utils/business.d.ts +0 -23
  161. package/lib/Utils/chat-utils.d.ts +0 -100
  162. package/lib/Utils/companion-reg-client-utils.d.ts +0 -17
  163. package/lib/Utils/crypto.d.ts +0 -37
  164. package/lib/Utils/decode-wa-message.d.ts +0 -66
  165. package/lib/Utils/event-buffer.d.ts +0 -36
  166. package/lib/Utils/generics.d.ts +0 -92
  167. package/lib/Utils/history.d.ts +0 -24
  168. package/lib/Utils/identity-change-handler.d.ts +0 -44
  169. package/lib/Utils/identity-change-handler.js +0 -50
  170. package/lib/Utils/index.d.ts +0 -22
  171. package/lib/Utils/link-preview.d.ts +0 -21
  172. package/lib/Utils/logger.d.ts +0 -12
  173. package/lib/Utils/lt-hash.d.ts +0 -8
  174. package/lib/Utils/make-mutex.d.ts +0 -9
  175. package/lib/Utils/message-retry-manager.d.ts +0 -115
  176. package/lib/Utils/messages-media.d.ts +0 -133
  177. package/lib/Utils/messages.d.ts +0 -91
  178. package/lib/Utils/noise-handler.d.ts +0 -20
  179. package/lib/Utils/offline-node-processor.d.ts +0 -17
  180. package/lib/Utils/offline-node-processor.js +0 -40
  181. package/lib/Utils/pre-key-manager.d.ts +0 -28
  182. package/lib/Utils/process-message.d.ts +0 -60
  183. package/lib/Utils/reporting-utils.d.ts +0 -11
  184. package/lib/Utils/signal.d.ts +0 -47
  185. package/lib/Utils/stanza-ack.d.ts +0 -11
  186. package/lib/Utils/sync-action-utils.d.ts +0 -19
  187. package/lib/Utils/tc-token-utils.d.ts +0 -37
  188. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
  189. package/lib/Utils/validate-connection.d.ts +0 -11
  190. package/lib/WABinary/constants.d.ts +0 -28
  191. package/lib/WABinary/decode.d.ts +0 -7
  192. package/lib/WABinary/encode.d.ts +0 -3
  193. package/lib/WABinary/generic-utils.d.ts +0 -15
  194. package/lib/WABinary/index.d.ts +0 -6
  195. package/lib/WABinary/jid-utils.d.ts +0 -48
  196. package/lib/WABinary/types.d.ts +0 -19
  197. package/lib/WAM/BinaryInfo.d.ts +0 -9
  198. package/lib/WAM/constants.d.ts +0 -40
  199. package/lib/WAM/encode.d.ts +0 -3
  200. package/lib/WAM/index.d.ts +0 -4
  201. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
  202. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -23
  203. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -13
  204. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -13
  205. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -10
  206. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -26
  207. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -10
  208. package/lib/WAUSync/Protocols/index.d.ts +0 -6
  209. package/lib/WAUSync/USyncQuery.d.ts +0 -30
  210. package/lib/WAUSync/USyncUser.d.ts +0 -17
  211. package/lib/WAUSync/index.d.ts +0 -4
  212. package/lib/index.d.ts +0 -12
@@ -3,260 +3,256 @@ import { inflate } from 'zlib';
3
3
  import * as constants from './constants.js';
4
4
  import { jidEncode, WAJIDDomains } from './jid-utils.js';
5
5
  const inflatePromise = promisify(inflate);
6
- export const decompressingIfRequired = async (buffer) => {
7
- if (2 & buffer.readUInt8()) {
8
- buffer = await inflatePromise(buffer.slice(1));
6
+ export const decompressingIfRequired = async buffer => {
7
+ if (2 & buffer.readUInt8()) {
8
+ buffer = await inflatePromise(buffer.slice(1));
9
+ } else {
10
+ buffer = buffer.slice(1);
11
+ }
12
+ return buffer;
13
+ };
14
+ export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = {
15
+ index: 0
16
+ }) => {
17
+ const {
18
+ DOUBLE_BYTE_TOKENS,
19
+ SINGLE_BYTE_TOKENS,
20
+ TAGS
21
+ } = opts;
22
+ const checkEOS = length => {
23
+ if (indexRef.index + length > buffer.length) {
24
+ throw new Error('end of stream');
9
25
  }
10
- else {
11
- // nodes with no compression have a 0x00 prefix, we remove that
12
- buffer = buffer.slice(1);
26
+ };
27
+ const next = () => {
28
+ const value = buffer[indexRef.index];
29
+ indexRef.index += 1;
30
+ return value;
31
+ };
32
+ const readByte = () => {
33
+ checkEOS(1);
34
+ return next();
35
+ };
36
+ const readBytes = n => {
37
+ checkEOS(n);
38
+ const value = buffer.slice(indexRef.index, indexRef.index + n);
39
+ indexRef.index += n;
40
+ return value;
41
+ };
42
+ const readStringFromChars = length => {
43
+ return readBytes(length).toString('utf-8');
44
+ };
45
+ const readInt = (n, littleEndian = false) => {
46
+ checkEOS(n);
47
+ let val = 0;
48
+ for (let i = 0; i < n; i++) {
49
+ const shift = littleEndian ? i : n - 1 - i;
50
+ val |= next() << shift * 8;
13
51
  }
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');
52
+ return val;
53
+ };
54
+ const readInt20 = () => {
55
+ checkEOS(3);
56
+ return ((next() & 15) << 16) + (next() << 8) + next();
57
+ };
58
+ const unpackHex = value => {
59
+ if (value >= 0 && value < 16) {
60
+ return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10;
61
+ }
62
+ throw new Error('invalid hex: ' + value);
63
+ };
64
+ const unpackNibble = value => {
65
+ if (value >= 0 && value <= 9) {
66
+ return '0'.charCodeAt(0) + value;
67
+ }
68
+ switch (value) {
69
+ case 10:
70
+ return '-'.charCodeAt(0);
71
+ case 11:
72
+ return '.'.charCodeAt(0);
73
+ case 15:
74
+ return '\0'.charCodeAt(0);
75
+ default:
76
+ throw new Error('invalid nibble: ' + value);
77
+ }
78
+ };
79
+ const unpackByte = (tag, value) => {
80
+ if (tag === TAGS.NIBBLE_8) {
81
+ return unpackNibble(value);
82
+ } else if (tag === TAGS.HEX_8) {
83
+ return unpackHex(value);
84
+ } else {
85
+ throw new Error('unknown tag: ' + tag);
86
+ }
87
+ };
88
+ const readPacked8 = tag => {
89
+ const startByte = readByte();
90
+ let value = '';
91
+ for (let i = 0; i < (startByte & 127); i++) {
92
+ const curByte = readByte();
93
+ value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4));
94
+ value += String.fromCharCode(unpackByte(tag, curByte & 0x0f));
95
+ }
96
+ if (startByte >> 7 !== 0) {
97
+ value = value.slice(0, -1);
98
+ }
99
+ return value;
100
+ };
101
+ const isListTag = tag => {
102
+ return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16;
103
+ };
104
+ const readListSize = tag => {
105
+ switch (tag) {
106
+ case TAGS.LIST_EMPTY:
107
+ return 0;
108
+ case TAGS.LIST_8:
109
+ return readByte();
110
+ case TAGS.LIST_16:
111
+ return readInt(2);
112
+ default:
113
+ throw new Error('invalid tag for list size: ' + tag);
114
+ }
115
+ };
116
+ const readJidPair = () => {
117
+ const i = readString(readByte());
118
+ const j = readString(readByte());
119
+ if (j) {
120
+ return (i || '') + '@' + j;
121
+ }
122
+ throw new Error('invalid jid pair: ' + i + ', ' + j);
123
+ };
124
+ const readAdJid = () => {
125
+ const rawDomainType = readByte();
126
+ const domainType = Number(rawDomainType);
127
+ const device = readByte();
128
+ const user = readString(readByte());
129
+ let server = 's.whatsapp.net';
130
+ if (domainType === WAJIDDomains.LID) {
131
+ server = 'lid';
132
+ } else if (domainType === WAJIDDomains.HOSTED) {
133
+ server = 'hosted';
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
+ } catch (err) {
154
+ indexRef.index = beforeServer;
155
+ }
156
+ return `${integrator}-${user}:${device}@${server}`;
157
+ };
158
+ const readString = tag => {
159
+ if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
160
+ return SINGLE_BYTE_TOKENS[tag] || '';
161
+ }
162
+ switch (tag) {
163
+ case TAGS.DICTIONARY_0:
164
+ case TAGS.DICTIONARY_1:
165
+ case TAGS.DICTIONARY_2:
166
+ case TAGS.DICTIONARY_3:
167
+ return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
168
+ case TAGS.LIST_EMPTY:
169
+ return '';
170
+ case TAGS.BINARY_8:
171
+ return readStringFromChars(readByte());
172
+ case TAGS.BINARY_20:
173
+ return readStringFromChars(readInt20());
174
+ case TAGS.BINARY_32:
175
+ return readStringFromChars(readInt(4));
176
+ case TAGS.JID_PAIR:
177
+ return readJidPair();
178
+ case TAGS.FB_JID:
179
+ return readFbJid();
180
+ case TAGS.INTEROP_JID:
181
+ return readInteropJid();
182
+ case TAGS.AD_JID:
183
+ return readAdJid();
184
+ case TAGS.HEX_8:
185
+ case TAGS.NIBBLE_8:
186
+ return readPacked8(tag);
187
+ default:
188
+ throw new Error('invalid string with tag: ' + tag);
189
+ }
190
+ };
191
+ const readList = tag => {
192
+ const items = [];
193
+ const size = readListSize(tag);
194
+ for (let i = 0; i < size; i++) {
195
+ items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef));
215
196
  }
216
- const attrs = {};
217
- let data;
218
- if (listSize === 0 || !header) {
219
- throw new Error('invalid node');
197
+ return items;
198
+ };
199
+ const getTokenDouble = (index1, index2) => {
200
+ const dict = DOUBLE_BYTE_TOKENS[index1];
201
+ if (!dict) {
202
+ throw new Error(`Invalid double token dict (${index1})`);
220
203
  }
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;
204
+ const value = dict[index2];
205
+ if (typeof value === 'undefined') {
206
+ throw new Error(`Invalid double token (${index2})`);
227
207
  }
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
- }
208
+ return value;
209
+ };
210
+ const listSize = readListSize(readByte());
211
+ const header = readString(readByte());
212
+ if (!listSize || !header.length) {
213
+ throw new Error('invalid node');
214
+ }
215
+ const attrs = {};
216
+ let data;
217
+ if (listSize === 0 || !header) {
218
+ throw new Error('invalid node');
219
+ }
220
+ const attributesLength = listSize - 1 >> 1;
221
+ for (let i = 0; i < attributesLength; i++) {
222
+ const key = readString(readByte());
223
+ const value = readString(readByte());
224
+ attrs[key] = value;
225
+ }
226
+ if (listSize % 2 === 0) {
227
+ const tag = readByte();
228
+ if (isListTag(tag)) {
229
+ data = readList(tag);
230
+ } else {
231
+ let decoded;
232
+ switch (tag) {
233
+ case TAGS.BINARY_8:
234
+ decoded = readBytes(readByte());
235
+ break;
236
+ case TAGS.BINARY_20:
237
+ decoded = readBytes(readInt20());
238
+ break;
239
+ case TAGS.BINARY_32:
240
+ decoded = readBytes(readInt(4));
241
+ break;
242
+ default:
243
+ decoded = readString(tag);
244
+ break;
245
+ }
246
+ data = decoded;
251
247
  }
252
- return {
253
- tag: header,
254
- attrs,
255
- content: data
256
- };
248
+ }
249
+ return {
250
+ tag: header,
251
+ attrs,
252
+ content: data
253
+ };
257
254
  };
258
- export const decodeBinaryNode = async (buff) => {
259
- const decompBuff = await decompressingIfRequired(buff);
260
- return decodeDecompressedBinaryNode(decompBuff, constants);
255
+ export const decodeBinaryNode = async buff => {
256
+ const decompBuff = await decompressingIfRequired(buff);
257
+ return decodeDecompressedBinaryNode(decompBuff, constants);
261
258
  };
262
- //# sourceMappingURL=decode.js.map