@mrlegendbot/baileys 1.2.5 → 1.2.7

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/WAProto/GenerateStatics.sh +3 -0
  2. package/WAProto/fix-imports.js +85 -0
  3. package/WAProto/index.d.ts +14017 -0
  4. package/WAProto/index.js +97691 -2
  5. package/lib/Defaults/index.d.ts +75 -0
  6. package/lib/Defaults/index.js +92 -89
  7. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  8. package/lib/Signal/Group/ciphertext-message.js +10 -9
  9. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  10. package/lib/Signal/Group/group-session-builder.js +22 -21
  11. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  12. package/lib/Signal/Group/group_cipher.js +69 -75
  13. package/lib/Signal/Group/index.d.ts +12 -0
  14. package/lib/Signal/Group/index.js +1 -0
  15. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  16. package/lib/Signal/Group/keyhelper.js +10 -9
  17. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  18. package/lib/Signal/Group/sender-chain-key.js +22 -21
  19. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  20. package/lib/Signal/Group/sender-key-distribution-message.js +60 -51
  21. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  22. package/lib/Signal/Group/sender-key-message.js +62 -56
  23. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  24. package/lib/Signal/Group/sender-key-name.js +40 -38
  25. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  26. package/lib/Signal/Group/sender-key-record.js +34 -33
  27. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  28. package/lib/Signal/Group/sender-key-state.js +76 -72
  29. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  30. package/lib/Signal/Group/sender-message-key.js +23 -22
  31. package/lib/Signal/libsignal.d.ts +5 -0
  32. package/lib/Signal/libsignal.js +405 -506
  33. package/lib/Signal/lid-mapping.d.ts +23 -0
  34. package/lib/Signal/lid-mapping.js +258 -276
  35. package/lib/Socket/Client/index.d.ts +3 -0
  36. package/lib/Socket/Client/index.js +1 -0
  37. package/lib/Socket/Client/types.d.ts +16 -0
  38. package/lib/Socket/Client/types.js +7 -6
  39. package/lib/Socket/Client/websocket.d.ts +13 -0
  40. package/lib/Socket/Client/websocket.js +49 -48
  41. package/lib/Socket/business.d.ts +217 -0
  42. package/lib/Socket/business.js +369 -349
  43. package/lib/Socket/chats.d.ts +125 -0
  44. package/lib/Socket/chats.js +1148 -1172
  45. package/lib/Socket/communities.d.ts +273 -0
  46. package/lib/Socket/communities.js +413 -473
  47. package/lib/Socket/groups.d.ts +161 -0
  48. package/lib/Socket/groups.js +331 -387
  49. package/lib/Socket/index.d.ts +260 -0
  50. package/lib/Socket/index.js +8 -6
  51. package/lib/Socket/messages-recv.d.ts +203 -0
  52. package/lib/Socket/messages-recv.js +1719 -1782
  53. package/lib/Socket/messages-send.d.ts +200 -0
  54. package/lib/Socket/messages-send.js +2508 -1384
  55. package/lib/Socket/mex.d.ts +3 -0
  56. package/lib/Socket/mex.js +35 -40
  57. package/lib/Socket/newsletter.d.ts +170 -0
  58. package/lib/Socket/newsletter.js +191 -239
  59. package/lib/Socket/socket.d.ts +59 -0
  60. package/lib/Socket/socket.js +923 -992
  61. package/lib/Types/Auth.d.ts +117 -0
  62. package/lib/Types/Auth.js +1 -0
  63. package/lib/Types/Bussines.d.ts +25 -0
  64. package/lib/Types/Bussines.js +1 -0
  65. package/lib/Types/Call.d.ts +15 -0
  66. package/lib/Types/Call.js +1 -0
  67. package/lib/Types/Chat.d.ts +124 -0
  68. package/lib/Types/Chat.js +8 -1
  69. package/lib/Types/Contact.d.ts +26 -0
  70. package/lib/Types/Contact.js +1 -0
  71. package/lib/Types/Events.d.ts +256 -0
  72. package/lib/Types/Events.js +2 -1
  73. package/lib/Types/GroupMetadata.d.ts +71 -0
  74. package/lib/Types/GroupMetadata.js +1 -0
  75. package/lib/Types/Label.d.ts +47 -0
  76. package/lib/Types/Label.js +22 -20
  77. package/lib/Types/LabelAssociation.d.ts +30 -0
  78. package/lib/Types/LabelAssociation.js +4 -2
  79. package/lib/Types/Message.d.ts +320 -0
  80. package/lib/Types/Message.js +5 -3
  81. package/lib/Types/Mex.d.ts +141 -0
  82. package/lib/Types/Mex.js +31 -26
  83. package/lib/Types/Product.d.ts +79 -0
  84. package/lib/Types/Product.js +1 -0
  85. package/lib/Types/Signal.d.ts +87 -0
  86. package/lib/Types/Signal.js +2 -1
  87. package/lib/Types/Socket.d.ts +136 -0
  88. package/lib/Types/Socket.js +3 -2
  89. package/lib/Types/State.d.ts +97 -0
  90. package/lib/Types/State.js +52 -4
  91. package/lib/Types/USync.d.ts +26 -0
  92. package/lib/Types/USync.js +1 -0
  93. package/lib/Types/index.d.ts +66 -0
  94. package/lib/Types/index.js +11 -10
  95. package/lib/Utils/auth-utils.d.ts +24 -0
  96. package/lib/Utils/auth-utils.js +277 -240
  97. package/lib/Utils/browser-utils.d.ts +4 -0
  98. package/lib/Utils/browser-utils.js +23 -20
  99. package/lib/Utils/business.d.ts +23 -0
  100. package/lib/Utils/business.js +204 -200
  101. package/lib/Utils/chat-utils.d.ts +100 -0
  102. package/lib/Utils/chat-utils.js +810 -836
  103. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  104. package/lib/Utils/companion-reg-client-utils.js +25 -23
  105. package/lib/Utils/crypto.d.ts +37 -0
  106. package/lib/Utils/crypto.js +86 -70
  107. package/lib/Utils/decode-wa-message.d.ts +66 -0
  108. package/lib/Utils/decode-wa-message.js +284 -277
  109. package/lib/Utils/event-buffer.d.ts +36 -0
  110. package/lib/Utils/event-buffer.js +595 -592
  111. package/lib/Utils/generics.d.ts +92 -0
  112. package/lib/Utils/generics.js +335 -312
  113. package/lib/Utils/history.d.ts +24 -0
  114. package/lib/Utils/history.js +115 -123
  115. package/lib/Utils/identity-change-handler.d.ts +44 -0
  116. package/lib/Utils/identity-change-handler.js +50 -0
  117. package/lib/Utils/index.d.ts +22 -0
  118. package/lib/Utils/index.js +2 -1
  119. package/lib/Utils/link-preview.d.ts +21 -0
  120. package/lib/Utils/link-preview.js +73 -74
  121. package/lib/Utils/logger.d.ts +12 -0
  122. package/lib/Utils/logger.js +2 -3
  123. package/lib/Utils/lt-hash.d.ts +8 -0
  124. package/lib/Utils/lt-hash.js +6 -0
  125. package/lib/Utils/make-mutex.d.ts +9 -0
  126. package/lib/Utils/make-mutex.js +27 -27
  127. package/lib/Utils/message-retry-manager.d.ts +115 -0
  128. package/lib/Utils/message-retry-manager.js +254 -213
  129. package/lib/Utils/messages-media.d.ts +133 -0
  130. package/lib/Utils/messages-media.js +715 -741
  131. package/lib/Utils/messages.d.ts +91 -0
  132. package/lib/Utils/messages.js +827 -791
  133. package/lib/Utils/noise-handler.d.ts +20 -0
  134. package/lib/Utils/noise-handler.js +187 -215
  135. package/lib/Utils/offline-node-processor.d.ts +17 -0
  136. package/lib/Utils/offline-node-processor.js +40 -0
  137. package/lib/Utils/pre-key-manager.d.ts +28 -0
  138. package/lib/Utils/pre-key-manager.js +98 -69
  139. package/lib/Utils/process-message.d.ts +60 -0
  140. package/lib/Utils/process-message.js +586 -617
  141. package/lib/Utils/reporting-utils.d.ts +11 -0
  142. package/lib/Utils/reporting-utils.js +234 -553
  143. package/lib/Utils/signal.d.ts +47 -0
  144. package/lib/Utils/signal.js +170 -217
  145. package/lib/Utils/stanza-ack.d.ts +11 -0
  146. package/lib/Utils/stanza-ack.js +35 -27
  147. package/lib/Utils/sync-action-utils.d.ts +19 -0
  148. package/lib/Utils/sync-action-utils.js +41 -37
  149. package/lib/Utils/tc-token-utils.d.ts +37 -0
  150. package/lib/Utils/tc-token-utils.js +141 -120
  151. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  152. package/lib/Utils/use-multi-file-auth-state.js +105 -87
  153. package/lib/Utils/validate-connection.d.ts +11 -0
  154. package/lib/Utils/validate-connection.js +188 -190
  155. package/lib/WABinary/constants.d.ts +28 -0
  156. package/lib/WABinary/constants.js +1294 -28
  157. package/lib/WABinary/decode.d.ts +7 -0
  158. package/lib/WABinary/decode.js +249 -245
  159. package/lib/WABinary/encode.d.ts +3 -0
  160. package/lib/WABinary/encode.js +206 -201
  161. package/lib/WABinary/generic-utils.d.ts +15 -0
  162. package/lib/WABinary/generic-utils.js +89 -86
  163. package/lib/WABinary/index.d.ts +6 -0
  164. package/lib/WABinary/index.js +1 -0
  165. package/lib/WABinary/jid-utils.d.ts +48 -0
  166. package/lib/WABinary/jid-utils.js +77 -68
  167. package/lib/WABinary/types.d.ts +19 -0
  168. package/lib/WABinary/types.js +1 -0
  169. package/lib/WAM/BinaryInfo.d.ts +9 -0
  170. package/lib/WAM/BinaryInfo.js +8 -7
  171. package/lib/WAM/constants.d.ts +40 -0
  172. package/lib/WAM/constants.js +22853 -19892
  173. package/lib/WAM/encode.d.ts +3 -0
  174. package/lib/WAM/encode.js +132 -121
  175. package/lib/WAM/index.d.ts +4 -0
  176. package/lib/WAM/index.js +1 -0
  177. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  178. package/lib/WAUSync/Protocols/USyncContactProtocol.js +43 -46
  179. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  180. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -47
  181. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  182. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -22
  183. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  184. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -30
  185. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  186. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +20 -19
  187. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  188. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +44 -53
  189. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  190. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
  191. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  192. package/lib/WAUSync/Protocols/index.js +1 -0
  193. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  194. package/lib/WAUSync/USyncQuery.js +88 -79
  195. package/lib/WAUSync/USyncUser.d.ts +17 -0
  196. package/lib/WAUSync/USyncUser.js +29 -28
  197. package/lib/WAUSync/index.d.ts +4 -0
  198. package/lib/WAUSync/index.js +1 -0
  199. package/lib/index.d.ts +12 -0
  200. package/lib/index.js +2 -2
  201. package/package.json +86 -22
  202. package/README.upstream.md +0 -1312
  203. package/WAProto/WAProto.json +0 -1
  204. package/WAProto/WAProto.proto +0 -6275
  205. package/WAProto/compiler.js +0 -550
  206. package/lib/Store/index.js +0 -8
  207. package/lib/Store/keyed-db.js +0 -108
  208. package/lib/Store/make-cache-manager-store.js +0 -70
  209. package/lib/Store/make-in-memory-store.js +0 -417
  210. package/lib/Store/make-ordered-dictionary.js +0 -69
  211. package/lib/Store/object-repository.js +0 -27
  212. package/lib/Utils/use-sqlite-auth-state.js +0 -162
@@ -0,0 +1,7 @@
1
+ import type { BinaryNode, BinaryNodeCodingOptions } from './types.js';
2
+ export declare const decompressingIfRequired: (buffer: Buffer) => Promise<Buffer<ArrayBufferLike>>;
3
+ export declare const decodeDecompressedBinaryNode: (buffer: Buffer, opts: Pick<BinaryNodeCodingOptions, "DOUBLE_BYTE_TOKENS" | "SINGLE_BYTE_TOKENS" | "TAGS">, indexRef?: {
4
+ index: number;
5
+ }) => BinaryNode;
6
+ export declare const decodeBinaryNode: (buff: Buffer) => Promise<BinaryNode>;
7
+ //# sourceMappingURL=decode.d.ts.map
@@ -3,256 +3,260 @@ 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));
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');
25
- }
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;
51
- }
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));
6
+ export const decompressingIfRequired = async (buffer) => {
7
+ if (2 & buffer.readUInt8()) {
8
+ buffer = await inflatePromise(buffer.slice(1));
95
9
  }
96
- if (startByte >> 7 !== 0) {
97
- value = value.slice(0, -1);
10
+ else {
11
+ // nodes with no compression have a 0x00 prefix, we remove that
12
+ buffer = buffer.slice(1);
98
13
  }
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));
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');
196
215
  }
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})`);
216
+ const attrs = {};
217
+ let data;
218
+ if (listSize === 0 || !header) {
219
+ throw new Error('invalid node');
203
220
  }
204
- const value = dict[index2];
205
- if (typeof value === 'undefined') {
206
- throw new Error(`Invalid double token (${index2})`);
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;
207
227
  }
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;
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
+ }
247
251
  }
248
- }
249
- return {
250
- tag: header,
251
- attrs,
252
- content: data
253
- };
252
+ return {
253
+ tag: header,
254
+ attrs,
255
+ content: data
256
+ };
254
257
  };
255
- export const decodeBinaryNode = async buff => {
256
- const decompBuff = await decompressingIfRequired(buff);
257
- return decodeDecompressedBinaryNode(decompBuff, constants);
258
+ export const decodeBinaryNode = async (buff) => {
259
+ const decompBuff = await decompressingIfRequired(buff);
260
+ return decodeDecompressedBinaryNode(decompBuff, constants);
258
261
  };
262
+ //# sourceMappingURL=decode.js.map
@@ -0,0 +1,3 @@
1
+ import type { BinaryNode, BinaryNodeCodingOptions } from './types.js';
2
+ export declare const encodeBinaryNode: (node: BinaryNode, opts?: Pick<BinaryNodeCodingOptions, "TAGS" | "TOKEN_MAP">, buffer?: number[]) => Buffer;
3
+ //# sourceMappingURL=encode.d.ts.map