@rizzxdc/baileys 8.0.10 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/LICENSE +4 -2
  2. package/README.md +107 -245
  3. package/WAProto/index.d.ts +15795 -0
  4. package/WAProto/index.js +25587 -4867
  5. package/lib/Defaults/index.d.ts +147 -0
  6. package/lib/Defaults/index.js +134 -104
  7. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  8. package/lib/Signal/Group/ciphertext-message.js +9 -11
  9. package/lib/Signal/Group/group-session-builder.d.ts +8 -0
  10. package/lib/Signal/Group/group-session-builder.js +5 -8
  11. package/lib/Signal/Group/group_cipher.d.ts +11 -0
  12. package/lib/Signal/Group/group_cipher.js +72 -75
  13. package/lib/Signal/Group/index.d.ts +12 -0
  14. package/lib/Signal/Group/index.js +11 -13
  15. package/lib/Signal/Group/keyhelper.d.ts +7 -0
  16. package/lib/Signal/Group/keyhelper.js +11 -14
  17. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  18. package/lib/Signal/Group/sender-chain-key.js +23 -26
  19. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  20. package/lib/Signal/Group/sender-key-distribution-message.js +58 -61
  21. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  22. package/lib/Signal/Group/sender-key-message.js +61 -64
  23. package/lib/Signal/Group/sender-key-name.d.ts +12 -0
  24. package/lib/Signal/Group/sender-key-name.js +39 -44
  25. package/lib/Signal/Group/sender-key-record.d.ts +13 -0
  26. package/lib/Signal/Group/sender-key-record.js +35 -38
  27. package/lib/Signal/Group/sender-key-state.d.ts +17 -0
  28. package/lib/Signal/Group/sender-key-state.js +76 -79
  29. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  30. package/lib/Signal/Group/sender-message-key.js +23 -26
  31. package/lib/Signal/libsignal.d.ts +55 -0
  32. package/lib/Signal/libsignal.js +414 -308
  33. package/lib/Signal/lid-mapping.d.ts +21 -0
  34. package/lib/Signal/lid-mapping.js +270 -149
  35. package/lib/Socket/Client/index.d.ts +3 -0
  36. package/lib/Socket/Client/index.js +2 -4
  37. package/lib/Socket/Client/types.d.ts +6 -0
  38. package/lib/Socket/Client/types.js +8 -11
  39. package/lib/Socket/Client/websocket.d.ts +13 -0
  40. package/lib/Socket/Client/websocket.js +51 -50
  41. package/lib/Socket/business.d.ts +395 -0
  42. package/lib/Socket/business.js +357 -355
  43. package/lib/Socket/chats.d.ts +137 -0
  44. package/lib/Socket/chats.js +1195 -867
  45. package/lib/Socket/communities.d.ts +562 -0
  46. package/lib/Socket/communities.js +416 -398
  47. package/lib/Socket/dugong.js +8 -1
  48. package/lib/Socket/groups.d.ts +286 -0
  49. package/lib/Socket/groups.js +336 -301
  50. package/lib/Socket/index.d.ts +530 -0
  51. package/lib/Socket/index.js +8 -13
  52. package/lib/Socket/messages-recv.d.ts +334 -0
  53. package/lib/Socket/messages-recv.js +1729 -1121
  54. package/lib/Socket/messages-send.d.ts +330 -0
  55. package/lib/Socket/messages-send.js +684 -462
  56. package/lib/Socket/mex.d.ts +2 -0
  57. package/lib/Socket/mex.js +36 -40
  58. package/lib/Socket/newsletter.d.ts +284 -0
  59. package/lib/Socket/newsletter.js +247 -237
  60. package/lib/Socket/socket.d.ts +61 -0
  61. package/lib/Socket/socket.js +968 -790
  62. package/lib/Store/index.d.ts +4 -0
  63. package/lib/Store/index.js +4 -6
  64. package/lib/Store/make-cache-manager-store.js +2 -2
  65. package/lib/Store/make-in-memory-store.d.ts +63 -0
  66. package/lib/Store/make-in-memory-store.js +414 -284
  67. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  68. package/lib/Store/make-ordered-dictionary.js +75 -76
  69. package/lib/Store/object-repository.d.ts +11 -0
  70. package/lib/Store/object-repository.js +21 -23
  71. package/lib/Types/Auth.d.ts +2 -0
  72. package/lib/Types/Auth.js +0 -2
  73. package/lib/Types/Bussines.d.ts +2 -0
  74. package/lib/Types/Bussines.js +0 -2
  75. package/lib/Types/Call.d.ts +2 -0
  76. package/lib/Types/Call.js +0 -2
  77. package/lib/Types/Chat.d.ts +2 -0
  78. package/lib/Types/Chat.js +5 -7
  79. package/lib/Types/Contact.d.ts +2 -0
  80. package/lib/Types/Contact.js +0 -2
  81. package/lib/Types/Events.d.ts +2 -0
  82. package/lib/Types/Events.js +1 -3
  83. package/lib/Types/GroupMetadata.d.ts +2 -0
  84. package/lib/Types/GroupMetadata.js +0 -2
  85. package/lib/Types/Label.d.ts +3 -0
  86. package/lib/Types/Label.js +21 -22
  87. package/lib/Types/LabelAssociation.d.ts +3 -0
  88. package/lib/Types/LabelAssociation.js +3 -4
  89. package/lib/Types/Message.d.ts +12 -0
  90. package/lib/Types/Message.js +11 -6
  91. package/lib/Types/Mex.d.ts +3 -0
  92. package/lib/Types/Mex.js +38 -0
  93. package/lib/Types/Product.d.ts +2 -0
  94. package/lib/Types/Product.js +0 -2
  95. package/lib/Types/RichType.d.ts +3 -0
  96. package/lib/Types/RichType.js +22 -0
  97. package/lib/Types/Signal.d.ts +2 -0
  98. package/lib/Types/Signal.js +1 -3
  99. package/lib/Types/Socket.d.ts +2 -0
  100. package/lib/Types/Socket.js +1 -4
  101. package/lib/Types/State.d.ts +6 -0
  102. package/lib/Types/State.js +52 -8
  103. package/lib/Types/USync.d.ts +2 -0
  104. package/lib/Types/USync.js +1 -3
  105. package/lib/Types/index.d.ts +14 -0
  106. package/lib/Types/index.js +22 -25
  107. package/lib/Utils/auth-utils.d.ts +58 -0
  108. package/lib/Utils/auth-utils.js +285 -203
  109. package/lib/Utils/browser-utils.d.ts +9 -0
  110. package/lib/Utils/browser-utils.js +21 -19
  111. package/lib/Utils/business.d.ts +50 -0
  112. package/lib/Utils/business.js +206 -209
  113. package/lib/Utils/chat-utils.d.ts +409 -0
  114. package/lib/Utils/chat-utils.js +825 -706
  115. package/lib/Utils/companion-reg-client-utils.d.ts +5 -0
  116. package/lib/Utils/companion-reg-client-utils.js +39 -0
  117. package/lib/Utils/crypto.d.ts +40 -0
  118. package/lib/Utils/crypto.js +88 -101
  119. package/lib/Utils/decode-wa-message.d.ts +84 -0
  120. package/lib/Utils/decode-wa-message.js +287 -238
  121. package/lib/Utils/event-buffer.d.ts +13 -0
  122. package/lib/Utils/event-buffer.js +602 -509
  123. package/lib/Utils/generics.d.ts +64 -0
  124. package/lib/Utils/generics.js +334 -295
  125. package/lib/Utils/history.d.ts +76 -0
  126. package/lib/Utils/history.js +125 -79
  127. package/lib/Utils/identity-change-handler.d.ts +14 -0
  128. package/lib/Utils/identity-change-handler.js +49 -0
  129. package/lib/Utils/index.d.ts +25 -0
  130. package/lib/Utils/index.js +25 -21
  131. package/lib/Utils/link-preview.d.ts +13 -0
  132. package/lib/Utils/link-preview.js +73 -70
  133. package/lib/Utils/logger.d.ts +3 -0
  134. package/lib/Utils/logger.js +1 -4
  135. package/lib/Utils/lt-hash.d.ts +7 -0
  136. package/lib/Utils/lt-hash.js +7 -45
  137. package/lib/Utils/make-mutex.d.ts +7 -0
  138. package/lib/Utils/make-mutex.js +27 -31
  139. package/lib/Utils/message-retry-manager.d.ts +80 -0
  140. package/lib/Utils/message-retry-manager.js +165 -14
  141. package/lib/Utils/messages-media.d.ts +137 -0
  142. package/lib/Utils/messages-media.js +768 -572
  143. package/lib/Utils/messages.d.ts +46 -0
  144. package/lib/Utils/messages.js +1082 -130
  145. package/lib/Utils/noise-handler.d.ts +21 -0
  146. package/lib/Utils/noise-handler.js +191 -135
  147. package/lib/Utils/offline-node-processor.d.ts +10 -0
  148. package/lib/Utils/offline-node-processor.js +39 -0
  149. package/lib/Utils/pre-key-manager.d.ts +26 -0
  150. package/lib/Utils/pre-key-manager.js +96 -76
  151. package/lib/Utils/process-message.d.ts +84 -0
  152. package/lib/Utils/process-message.js +718 -311
  153. package/lib/Utils/reporting-utils.d.ts +13 -0
  154. package/lib/Utils/reporting-utils.js +257 -0
  155. package/lib/Utils/rich-message-utils.d.ts +49 -0
  156. package/lib/Utils/rich-message-utils.js +257 -0
  157. package/lib/Utils/signal.d.ts +83 -0
  158. package/lib/Utils/signal.js +174 -135
  159. package/lib/Utils/stanza-ack.d.ts +17 -0
  160. package/lib/Utils/stanza-ack.js +37 -0
  161. package/lib/Utils/sync-action-utils.d.ts +3 -0
  162. package/lib/Utils/sync-action-utils.js +48 -0
  163. package/lib/Utils/tc-token-utils.d.ts +30 -0
  164. package/lib/Utils/tc-token-utils.js +162 -0
  165. package/lib/Utils/use-multi-file-auth-state.d.ts +11 -0
  166. package/lib/Utils/use-multi-file-auth-state.js +104 -95
  167. package/lib/Utils/use-single-file-auth-state.d.ts +11 -0
  168. package/lib/Utils/use-single-file-auth-state.js +108 -0
  169. package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
  170. package/lib/Utils/use-sqlite-auth-state.js +108 -0
  171. package/lib/Utils/validate-connection.d.ts +44 -0
  172. package/lib/Utils/validate-connection.js +182 -180
  173. package/lib/WABinary/constants.d.ts +62 -0
  174. package/lib/WABinary/constants.js +1455 -1292
  175. package/lib/WABinary/decode.d.ts +10 -0
  176. package/lib/WABinary/decode.js +249 -228
  177. package/lib/WABinary/encode.d.ts +3 -0
  178. package/lib/WABinary/encode.js +207 -206
  179. package/lib/WABinary/generic-utils.d.ts +77 -0
  180. package/lib/WABinary/generic-utils.js +135 -99
  181. package/lib/WABinary/index.d.ts +6 -0
  182. package/lib/WABinary/index.js +5 -7
  183. package/lib/WABinary/jid-utils.d.ts +29 -0
  184. package/lib/WABinary/jid-utils.js +80 -78
  185. package/lib/WABinary/types.d.ts +2 -0
  186. package/lib/WABinary/types.js +1 -3
  187. package/lib/WAM/BinaryInfo.d.ts +8 -0
  188. package/lib/WAM/BinaryInfo.js +7 -9
  189. package/lib/WAM/constants.d.ts +34927 -0
  190. package/lib/WAM/constants.js +22847 -22848
  191. package/lib/WAM/encode.d.ts +2 -0
  192. package/lib/WAM/encode.js +129 -134
  193. package/lib/WAM/index.d.ts +4 -0
  194. package/lib/WAM/index.js +3 -5
  195. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +37 -0
  196. package/lib/WAUSync/Protocols/USyncContactProtocol.js +48 -27
  197. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  198. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -48
  199. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  200. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -26
  201. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  202. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -35
  203. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  204. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  205. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +39 -0
  206. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +46 -49
  207. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +15 -0
  208. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +23 -25
  209. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  210. package/lib/WAUSync/Protocols/index.js +5 -6
  211. package/lib/WAUSync/USyncQuery.d.ts +21 -0
  212. package/lib/WAUSync/USyncQuery.js +92 -85
  213. package/lib/WAUSync/USyncUser.d.ts +17 -0
  214. package/lib/WAUSync/USyncUser.js +28 -22
  215. package/lib/WAUSync/index.d.ts +4 -0
  216. package/lib/WAUSync/index.js +3 -5
  217. package/lib/index.d.ts +12 -0
  218. package/lib/index.js +31 -19
  219. package/package.json +12 -6
  220. package/lib/Types/Newsletter.js +0 -33
@@ -0,0 +1,10 @@
1
+ export function decompressingIfRequired(buffer: any): Promise<any>;
2
+ export function decodeDecompressedBinaryNode(buffer: any, opts: any, indexRef?: {
3
+ index: number;
4
+ }): {
5
+ tag: any;
6
+ attrs: {};
7
+ content: any;
8
+ };
9
+ export function decodeBinaryNode(buff: any): Promise<any>;
10
+ //# sourceMappingURL=decode.d.ts.map
@@ -1,240 +1,261 @@
1
- //=======================================================//
2
- import { jidEncode, WAJIDDomains } from "./jid-utils.js";
3
- import * as constants from "./constants.js";
4
- import { promisify } from "util";
5
- import { inflate } from "zlib";
6
- //=======================================================//
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';
7
5
  const inflatePromise = promisify(inflate);
8
6
  export const decompressingIfRequired = async (buffer) => {
9
- if (2 & buffer.readUInt8()) {
10
- buffer = await inflatePromise(buffer.slice(1));
11
- }
12
- else {
13
- buffer = buffer.slice(1);
14
- }
15
- return buffer;
16
- };
17
- //=======================================================//
18
- export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
19
- const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;
20
- const checkEOS = (length) => {
21
- if (indexRef.index + length > buffer.length) {
22
- throw new Error("end of stream");
23
- }
24
- };
25
- const next = () => {
26
- const value = buffer[indexRef.index];
27
- indexRef.index += 1;
28
- return value;
29
- };
30
- const readByte = () => {
31
- checkEOS(1);
32
- return next();
33
- };
34
- const readBytes = (n) => {
35
- checkEOS(n);
36
- const value = buffer.slice(indexRef.index, indexRef.index + n);
37
- indexRef.index += n;
38
- return value;
39
- };
40
- const readStringFromChars = (length) => {
41
- return readBytes(length).toString("utf-8");
42
- };
43
- const readInt = (n, littleEndian = false) => {
44
- checkEOS(n);
45
- let val = 0;
46
- for (let i = 0; i < n; i++) {
47
- const shift = littleEndian ? i : n - 1 - i;
48
- val |= next() << (shift * 8);
49
- }
50
- return val;
51
- };
52
- const readInt20 = () => {
53
- checkEOS(3);
54
- return ((next() & 15) << 16) + (next() << 8) + next();
55
- };
56
- const unpackHex = (value) => {
57
- if (value >= 0 && value < 16) {
58
- return value < 10 ? "0".charCodeAt(0) + value : "A".charCodeAt(0) + value - 10;
59
- }
60
- throw new Error("invalid hex: " + value);
61
- };
62
- const unpackNibble = (value) => {
63
- if (value >= 0 && value <= 9) {
64
- return "0".charCodeAt(0) + value;
65
- }
66
- switch (value) {
67
- case 10:
68
- return "-".charCodeAt(0);
69
- case 11:
70
- return ".".charCodeAt(0);
71
- case 15:
72
- return "\0".charCodeAt(0);
73
- default:
74
- throw new Error("invalid nibble: " + value);
75
- }
76
- };
77
- const unpackByte = (tag, value) => {
78
- if (tag === TAGS.NIBBLE_8) {
79
- return unpackNibble(value);
80
- }
81
- else if (tag === TAGS.HEX_8) {
82
- return unpackHex(value);
7
+ if (2 & buffer.readUInt8()) {
8
+ buffer = await inflatePromise(buffer.slice(1));
83
9
  }
84
10
  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);
11
+ // nodes with no compression have a 0x00 prefix, we remove that
12
+ buffer = buffer.slice(1);
114
13
  }
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
- }
133
- else if (domainType === WAJIDDomains.HOSTED) {
134
- server = "hosted";
135
- }
136
- else if (domainType === WAJIDDomains.HOSTED_LID) {
137
- server = "hosted.lid";
138
- }
139
- return jidEncode(user, server, device);
140
- };
141
- const readString = (tag) => {
142
- if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
143
- return SINGLE_BYTE_TOKENS[tag] || "";
144
- }
145
- switch (tag) {
146
- case TAGS.DICTIONARY_0:
147
- case TAGS.DICTIONARY_1:
148
- case TAGS.DICTIONARY_2:
149
- case TAGS.DICTIONARY_3:
150
- return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte());
151
- case TAGS.LIST_EMPTY:
152
- return "";
153
- case TAGS.BINARY_8:
154
- return readStringFromChars(readByte());
155
- case TAGS.BINARY_20:
156
- return readStringFromChars(readInt20());
157
- case TAGS.BINARY_32:
158
- return readStringFromChars(readInt(4));
159
- case TAGS.JID_PAIR:
160
- return readJidPair();
161
- case TAGS.AD_JID:
162
- return readAdJid();
163
- case TAGS.HEX_8:
164
- case TAGS.NIBBLE_8:
165
- return readPacked8(tag);
166
- default:
167
- throw new Error("invalid string with tag: " + tag);
168
- }
169
- };
170
- const readList = (tag) => {
171
- const items = [];
172
- const size = readListSize(tag);
173
- for (let i = 0; i < size; i++) {
174
- items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef));
175
- }
176
- return items;
177
- };
178
- const getTokenDouble = (index1, index2) => {
179
- const dict = DOUBLE_BYTE_TOKENS[index1];
180
- if (!dict) {
181
- throw new Error(`Invalid double token dict (${index1})`);
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');
182
215
  }
183
- const value = dict[index2];
184
- if (typeof value === "undefined") {
185
- throw new Error(`Invalid double token (${index2})`);
216
+ const attrs = {};
217
+ let data;
218
+ if (listSize === 0 || !header) {
219
+ throw new Error('invalid node');
186
220
  }
187
- return value;
188
- };
189
- const listSize = readListSize(readByte());
190
- const header = readString(readByte());
191
- if (!listSize || !header.length) {
192
- throw new Error("invalid node");
193
- }
194
- const attrs = {};
195
- let data;
196
- if (listSize === 0 || !header) {
197
- throw new Error("invalid node");
198
- }
199
- const attributesLength = (listSize - 1) >> 1;
200
- for (let i = 0; i < attributesLength; i++) {
201
- const key = readString(readByte());
202
- const value = readString(readByte());
203
- attrs[key] = value;
204
- }
205
- if (listSize % 2 === 0) {
206
- const tag = readByte();
207
- if (isListTag(tag)) {
208
- data = readList(tag);
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;
209
227
  }
210
- else {
211
- let decoded;
212
- switch (tag) {
213
- case TAGS.BINARY_8:
214
- decoded = readBytes(readByte());
215
- break;
216
- case TAGS.BINARY_20:
217
- decoded = readBytes(readInt20());
218
- break;
219
- case TAGS.BINARY_32:
220
- decoded = readBytes(readInt(4));
221
- break;
222
- default:
223
- decoded = readString(tag);
224
- break;
225
- }
226
- 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
+ }
227
251
  }
228
- }
229
- return {
230
- tag: header,
231
- attrs,
232
- content: data
233
- };
252
+ return {
253
+ tag: header,
254
+ attrs,
255
+ content: data
256
+ };
234
257
  };
235
- //=======================================================//
236
258
  export const decodeBinaryNode = async (buff) => {
237
- const decompBuff = await decompressingIfRequired(buff);
238
- return decodeDecompressedBinaryNode(decompBuff, constants);
259
+ const decompBuff = await decompressingIfRequired(buff);
260
+ return decodeDecompressedBinaryNode(decompBuff, constants);
239
261
  };
240
- //=======================================================//
@@ -0,0 +1,3 @@
1
+ export function encodeBinaryNode(node: any, opts?: typeof constants, buffer?: number[]): any;
2
+ import * as constants from './constants.js';
3
+ //# sourceMappingURL=encode.d.ts.map