@mrlegendbot/baileys 1.2.3 → 1.2.4

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 +1503 -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
@@ -1,220 +1,215 @@
1
1
  import * as constants from './constants.js';
2
2
  import { jidDecode } from './jid-utils.js';
3
3
  export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
4
- const encoded = encodeBinaryNodeInner(node, opts, buffer);
5
- return Buffer.from(encoded);
4
+ const encoded = encodeBinaryNodeInner(node, opts, buffer);
5
+ return Buffer.from(encoded);
6
6
  };
7
- const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
8
- const { TAGS, TOKEN_MAP } = opts;
9
- const pushByte = (value) => buffer.push(value & 0xff);
10
- const pushInt = (value, n, littleEndian = false) => {
11
- for (let i = 0; i < n; i++) {
12
- const curShift = littleEndian ? i : n - 1 - i;
13
- buffer.push((value >> (curShift * 8)) & 0xff);
14
- }
15
- };
16
- const pushBytes = (bytes) => {
17
- for (const b of bytes) {
18
- buffer.push(b);
19
- }
20
- };
21
- const pushInt16 = (value) => {
22
- pushBytes([(value >> 8) & 0xff, value & 0xff]);
23
- };
24
- const pushInt20 = (value) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]);
25
- const writeByteLength = (length) => {
26
- if (length >= 4294967296) {
27
- throw new Error('string too large to encode: ' + length);
28
- }
29
- if (length >= 1 << 20) {
30
- pushByte(TAGS.BINARY_32);
31
- pushInt(length, 4); // 32 bit integer
32
- }
33
- else if (length >= 256) {
34
- pushByte(TAGS.BINARY_20);
35
- pushInt20(length);
36
- }
37
- else {
38
- pushByte(TAGS.BINARY_8);
39
- pushByte(length);
40
- }
41
- };
42
- const writeStringRaw = (str) => {
43
- const bytes = Buffer.from(str, 'utf-8');
44
- writeByteLength(bytes.length);
45
- pushBytes(bytes);
46
- };
47
- const writeJid = ({ domainType, device, user, server }) => {
48
- if (typeof device !== 'undefined') {
49
- pushByte(TAGS.AD_JID);
50
- pushByte(domainType || 0);
51
- pushByte(device || 0);
52
- writeString(user);
53
- }
54
- else {
55
- pushByte(TAGS.JID_PAIR);
56
- if (user.length) {
57
- writeString(user);
58
- }
59
- else {
60
- pushByte(TAGS.LIST_EMPTY);
61
- }
62
- writeString(server);
63
- }
64
- };
65
- const packNibble = (char) => {
66
- switch (char) {
67
- case '-':
68
- return 10;
69
- case '.':
70
- return 11;
71
- case '\0':
72
- return 15;
73
- default:
74
- if (char >= '0' && char <= '9') {
75
- return char.charCodeAt(0) - '0'.charCodeAt(0);
76
- }
77
- throw new Error(`invalid byte for nibble "${char}"`);
78
- }
79
- };
80
- const packHex = (char) => {
7
+ const encodeBinaryNodeInner = ({
8
+ tag,
9
+ attrs,
10
+ content
11
+ }, opts, buffer) => {
12
+ const {
13
+ TAGS,
14
+ TOKEN_MAP
15
+ } = opts;
16
+ const pushByte = value => buffer.push(value & 0xff);
17
+ const pushInt = (value, n, littleEndian = false) => {
18
+ for (let i = 0; i < n; i++) {
19
+ const curShift = littleEndian ? i : n - 1 - i;
20
+ buffer.push(value >> curShift * 8 & 0xff);
21
+ }
22
+ };
23
+ const pushBytes = bytes => {
24
+ for (const b of bytes) {
25
+ buffer.push(b);
26
+ }
27
+ };
28
+ const pushInt16 = value => {
29
+ pushBytes([value >> 8 & 0xff, value & 0xff]);
30
+ };
31
+ const pushInt20 = value => pushBytes([value >> 16 & 0x0f, value >> 8 & 0xff, value & 0xff]);
32
+ const writeByteLength = length => {
33
+ if (length >= 4294967296) {
34
+ throw new Error('string too large to encode: ' + length);
35
+ }
36
+ if (length >= 1 << 20) {
37
+ pushByte(TAGS.BINARY_32);
38
+ pushInt(length, 4);
39
+ } else if (length >= 256) {
40
+ pushByte(TAGS.BINARY_20);
41
+ pushInt20(length);
42
+ } else {
43
+ pushByte(TAGS.BINARY_8);
44
+ pushByte(length);
45
+ }
46
+ };
47
+ const writeStringRaw = str => {
48
+ const bytes = Buffer.from(str, 'utf-8');
49
+ writeByteLength(bytes.length);
50
+ pushBytes(bytes);
51
+ };
52
+ const writeJid = ({
53
+ domainType,
54
+ device,
55
+ user,
56
+ server
57
+ }) => {
58
+ if (typeof device !== 'undefined') {
59
+ pushByte(TAGS.AD_JID);
60
+ pushByte(domainType || 0);
61
+ pushByte(device || 0);
62
+ writeString(user);
63
+ } else {
64
+ pushByte(TAGS.JID_PAIR);
65
+ if (user.length) {
66
+ writeString(user);
67
+ } else {
68
+ pushByte(TAGS.LIST_EMPTY);
69
+ }
70
+ writeString(server);
71
+ }
72
+ };
73
+ const packNibble = char => {
74
+ switch (char) {
75
+ case '-':
76
+ return 10;
77
+ case '.':
78
+ return 11;
79
+ case '\0':
80
+ return 15;
81
+ default:
81
82
  if (char >= '0' && char <= '9') {
82
- return char.charCodeAt(0) - '0'.charCodeAt(0);
83
- }
84
- if (char >= 'A' && char <= 'F') {
85
- return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
86
- }
87
- if (char >= 'a' && char <= 'f') {
88
- return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
89
- }
90
- if (char === '\0') {
91
- return 15;
92
- }
93
- throw new Error(`Invalid hex char "${char}"`);
94
- };
95
- const writePackedBytes = (str, type) => {
96
- if (str.length > TAGS.PACKED_MAX) {
97
- throw new Error('Too many bytes to pack');
98
- }
99
- pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
100
- let roundedLength = Math.ceil(str.length / 2.0);
101
- if (str.length % 2 !== 0) {
102
- roundedLength |= 128;
103
- }
104
- pushByte(roundedLength);
105
- const packFunction = type === 'nibble' ? packNibble : packHex;
106
- const packBytePair = (v1, v2) => {
107
- const result = (packFunction(v1) << 4) | packFunction(v2);
108
- return result;
109
- };
110
- const strLengthHalf = Math.floor(str.length / 2);
111
- for (let i = 0; i < strLengthHalf; i++) {
112
- pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
113
- }
114
- if (str.length % 2 !== 0) {
115
- pushByte(packBytePair(str[str.length - 1], '\x00'));
116
- }
117
- };
118
- const isNibble = (str) => {
119
- if (!str || str.length > TAGS.PACKED_MAX) {
120
- return false;
121
- }
122
- for (const char of str) {
123
- const isInNibbleRange = char >= '0' && char <= '9';
124
- if (!isInNibbleRange && char !== '-' && char !== '.') {
125
- return false;
126
- }
127
- }
128
- return true;
129
- };
130
- const isHex = (str) => {
131
- if (!str || str.length > TAGS.PACKED_MAX) {
132
- return false;
133
- }
134
- for (const char of str) {
135
- const isInNibbleRange = char >= '0' && char <= '9';
136
- if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
137
- return false;
138
- }
139
- }
140
- return true;
141
- };
142
- const writeString = (str) => {
143
- if (str === undefined || str === null) {
144
- pushByte(TAGS.LIST_EMPTY);
145
- return;
146
- }
147
- if (str === '') {
148
- writeStringRaw(str);
149
- return;
150
- }
151
- const tokenIndex = TOKEN_MAP[str];
152
- if (tokenIndex) {
153
- if (typeof tokenIndex.dict === 'number') {
154
- pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
155
- }
156
- pushByte(tokenIndex.index);
157
- }
158
- else if (isNibble(str)) {
159
- writePackedBytes(str, 'nibble');
160
- }
161
- else if (isHex(str)) {
162
- writePackedBytes(str, 'hex');
163
- }
164
- else {
165
- const decodedJid = jidDecode(str);
166
- if (decodedJid) {
167
- writeJid(decodedJid);
168
- }
169
- else {
170
- writeStringRaw(str);
171
- }
172
- }
173
- };
174
- const writeListStart = (listSize) => {
175
- if (listSize === 0) {
176
- pushByte(TAGS.LIST_EMPTY);
177
- }
178
- else if (listSize < 256) {
179
- pushBytes([TAGS.LIST_8, listSize]);
180
- }
181
- else {
182
- pushByte(TAGS.LIST_16);
183
- pushInt16(listSize);
83
+ return char.charCodeAt(0) - '0'.charCodeAt(0);
184
84
  }
85
+ throw new Error(`invalid byte for nibble "${char}"`);
86
+ }
87
+ };
88
+ const packHex = char => {
89
+ if (char >= '0' && char <= '9') {
90
+ return char.charCodeAt(0) - '0'.charCodeAt(0);
91
+ }
92
+ if (char >= 'A' && char <= 'F') {
93
+ return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
94
+ }
95
+ if (char >= 'a' && char <= 'f') {
96
+ return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
97
+ }
98
+ if (char === '\0') {
99
+ return 15;
100
+ }
101
+ throw new Error(`Invalid hex char "${char}"`);
102
+ };
103
+ const writePackedBytes = (str, type) => {
104
+ if (str.length > TAGS.PACKED_MAX) {
105
+ throw new Error('Too many bytes to pack');
106
+ }
107
+ pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
108
+ let roundedLength = Math.ceil(str.length / 2.0);
109
+ if (str.length % 2 !== 0) {
110
+ roundedLength |= 128;
111
+ }
112
+ pushByte(roundedLength);
113
+ const packFunction = type === 'nibble' ? packNibble : packHex;
114
+ const packBytePair = (v1, v2) => {
115
+ const result = packFunction(v1) << 4 | packFunction(v2);
116
+ return result;
185
117
  };
186
- if (!tag) {
187
- throw new Error('Invalid node: tag cannot be undefined');
188
- }
189
- const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null);
190
- writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
191
- writeString(tag);
192
- for (const key of validAttributes) {
193
- if (typeof attrs[key] === 'string') {
194
- writeString(key);
195
- writeString(attrs[key]);
196
- }
118
+ const strLengthHalf = Math.floor(str.length / 2);
119
+ for (let i = 0; i < strLengthHalf; i++) {
120
+ pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
197
121
  }
198
- if (typeof content === 'string') {
199
- writeString(content);
122
+ if (str.length % 2 !== 0) {
123
+ pushByte(packBytePair(str[str.length - 1], '\x00'));
200
124
  }
201
- else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
202
- writeByteLength(content.length);
203
- pushBytes(content);
125
+ };
126
+ const isNibble = str => {
127
+ if (!str || str.length > TAGS.PACKED_MAX) {
128
+ return false;
204
129
  }
205
- else if (Array.isArray(content)) {
206
- const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'));
207
- writeListStart(validContent.length);
208
- for (const item of validContent) {
209
- encodeBinaryNodeInner(item, opts, buffer);
210
- }
130
+ for (const char of str) {
131
+ const isInNibbleRange = char >= '0' && char <= '9';
132
+ if (!isInNibbleRange && char !== '-' && char !== '.') {
133
+ return false;
134
+ }
135
+ }
136
+ return true;
137
+ };
138
+ const isHex = str => {
139
+ if (!str || str.length > TAGS.PACKED_MAX) {
140
+ return false;
141
+ }
142
+ for (const char of str) {
143
+ const isInNibbleRange = char >= '0' && char <= '9';
144
+ if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
145
+ return false;
146
+ }
147
+ }
148
+ return true;
149
+ };
150
+ const writeString = str => {
151
+ if (str === undefined || str === null) {
152
+ pushByte(TAGS.LIST_EMPTY);
153
+ return;
154
+ }
155
+ if (str === '') {
156
+ writeStringRaw(str);
157
+ return;
158
+ }
159
+ const tokenIndex = TOKEN_MAP[str];
160
+ if (tokenIndex) {
161
+ if (typeof tokenIndex.dict === 'number') {
162
+ pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
163
+ }
164
+ pushByte(tokenIndex.index);
165
+ } else if (isNibble(str)) {
166
+ writePackedBytes(str, 'nibble');
167
+ } else if (isHex(str)) {
168
+ writePackedBytes(str, 'hex');
169
+ } else {
170
+ const decodedJid = jidDecode(str);
171
+ if (decodedJid) {
172
+ writeJid(decodedJid);
173
+ } else {
174
+ writeStringRaw(str);
175
+ }
176
+ }
177
+ };
178
+ const writeListStart = listSize => {
179
+ if (listSize === 0) {
180
+ pushByte(TAGS.LIST_EMPTY);
181
+ } else if (listSize < 256) {
182
+ pushBytes([TAGS.LIST_8, listSize]);
183
+ } else {
184
+ pushByte(TAGS.LIST_16);
185
+ pushInt16(listSize);
211
186
  }
212
- else if (typeof content === 'undefined') {
213
- // do nothing
187
+ };
188
+ if (!tag) {
189
+ throw new Error('Invalid node: tag cannot be undefined');
190
+ }
191
+ const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null);
192
+ writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
193
+ writeString(tag);
194
+ for (const key of validAttributes) {
195
+ if (typeof attrs[key] === 'string') {
196
+ writeString(key);
197
+ writeString(attrs[key]);
214
198
  }
215
- else {
216
- throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
199
+ }
200
+ if (typeof content === 'string') {
201
+ writeString(content);
202
+ } else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
203
+ writeByteLength(content.length);
204
+ pushBytes(content);
205
+ } else if (Array.isArray(content)) {
206
+ const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'));
207
+ writeListStart(validContent.length);
208
+ for (const item of validContent) {
209
+ encodeBinaryNodeInner(item, opts, buffer);
217
210
  }
218
- return buffer;
211
+ } else if (typeof content === 'undefined') {} else {
212
+ throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
213
+ }
214
+ return buffer;
219
215
  };
220
- //# sourceMappingURL=encode.js.map
@@ -1,113 +1,110 @@
1
1
  import { Boom } from '@hapi/boom';
2
- import { proto } from '../../WAProto/index.js';
3
- import {} from './types.js';
4
- // some extra useful utilities
2
+ import { proto } from '../../WAProto/compiler.js';
3
+ import './types.js';
5
4
  const indexCache = new WeakMap();
6
5
  export const getBinaryNodeChildren = (node, childTag) => {
7
- if (!node || !Array.isArray(node.content))
8
- return [];
9
- let index = indexCache.get(node);
10
- // Build the index once per node
11
- if (!index) {
12
- index = new Map();
13
- for (const child of node.content) {
14
- let arr = index.get(child.tag);
15
- if (!arr)
16
- index.set(child.tag, (arr = []));
17
- arr.push(child);
18
- }
19
- indexCache.set(node, index);
6
+ if (!node || !Array.isArray(node.content)) return [];
7
+ let index = indexCache.get(node);
8
+ if (!index) {
9
+ index = new Map();
10
+ for (const child of node.content) {
11
+ let arr = index.get(child.tag);
12
+ if (!arr) index.set(child.tag, arr = []);
13
+ arr.push(child);
20
14
  }
21
- // Return first matching child
22
- return index.get(childTag) || [];
15
+ indexCache.set(node, index);
16
+ }
17
+ return index.get(childTag) || [];
23
18
  };
24
19
  export const getBinaryNodeChild = (node, childTag) => {
25
- return getBinaryNodeChildren(node, childTag)[0];
20
+ return getBinaryNodeChildren(node, childTag)[0];
26
21
  };
27
- export const getAllBinaryNodeChildren = ({ content }) => {
28
- if (Array.isArray(content)) {
29
- return content;
30
- }
31
- return [];
22
+ export const getAllBinaryNodeChildren = ({
23
+ content
24
+ }) => {
25
+ if (Array.isArray(content)) {
26
+ return content;
27
+ }
28
+ return [];
32
29
  };
33
30
  export const getBinaryNodeChildBuffer = (node, childTag) => {
34
- const child = getBinaryNodeChild(node, childTag)?.content;
35
- if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
36
- return child;
37
- }
31
+ const child = getBinaryNodeChild(node, childTag)?.content;
32
+ if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
33
+ return child;
34
+ }
38
35
  };
39
36
  export const getBinaryNodeChildString = (node, childTag) => {
40
- const child = getBinaryNodeChild(node, childTag)?.content;
41
- if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
42
- return Buffer.from(child).toString('utf-8');
43
- }
44
- else if (typeof child === 'string') {
45
- return child;
46
- }
37
+ const child = getBinaryNodeChild(node, childTag)?.content;
38
+ if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
39
+ return Buffer.from(child).toString('utf-8');
40
+ } else if (typeof child === 'string') {
41
+ return child;
42
+ }
47
43
  };
48
44
  export const getBinaryNodeChildUInt = (node, childTag, length) => {
49
- const buff = getBinaryNodeChildBuffer(node, childTag);
50
- if (buff) {
51
- return bufferToUInt(buff, length);
52
- }
45
+ const buff = getBinaryNodeChildBuffer(node, childTag);
46
+ if (buff) {
47
+ return bufferToUInt(buff, length);
48
+ }
53
49
  };
54
- export const assertNodeErrorFree = (node) => {
55
- const errNode = getBinaryNodeChild(node, 'error');
56
- if (errNode) {
57
- throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });
58
- }
50
+ export const assertNodeErrorFree = node => {
51
+ const errNode = getBinaryNodeChild(node, 'error');
52
+ if (errNode) {
53
+ throw new Boom(errNode.attrs.text || 'Unknown error', {
54
+ data: +errNode.attrs.code
55
+ });
56
+ }
59
57
  };
60
58
  export const reduceBinaryNodeToDictionary = (node, tag) => {
61
- const nodes = getBinaryNodeChildren(node, tag);
62
- const dict = nodes.reduce((dict, { attrs }) => {
63
- if (typeof attrs.name === 'string') {
64
- dict[attrs.name] = attrs.value || attrs.config_value;
65
- }
66
- else {
67
- dict[attrs.config_code] = attrs.value || attrs.config_value;
68
- }
69
- return dict;
70
- }, {});
59
+ const nodes = getBinaryNodeChildren(node, tag);
60
+ const dict = nodes.reduce((dict, {
61
+ attrs
62
+ }) => {
63
+ if (typeof attrs.name === 'string') {
64
+ dict[attrs.name] = attrs.value || attrs.config_value;
65
+ } else {
66
+ dict[attrs.config_code] = attrs.value || attrs.config_value;
67
+ }
71
68
  return dict;
69
+ }, {});
70
+ return dict;
72
71
  };
73
- export const getBinaryNodeMessages = ({ content }) => {
74
- const msgs = [];
75
- if (Array.isArray(content)) {
76
- for (const item of content) {
77
- if (item.tag === 'message') {
78
- msgs.push(proto.WebMessageInfo.decode(item.content).toJSON());
79
- }
80
- }
72
+ export const getBinaryNodeMessages = ({
73
+ content
74
+ }) => {
75
+ const msgs = [];
76
+ if (Array.isArray(content)) {
77
+ for (const item of content) {
78
+ if (item.tag === 'message') {
79
+ msgs.push(proto.WebMessageInfo.decode(item.content).toJSON());
80
+ }
81
81
  }
82
- return msgs;
82
+ }
83
+ return msgs;
83
84
  };
84
85
  function bufferToUInt(e, t) {
85
- let a = 0;
86
- for (let i = 0; i < t; i++) {
87
- a = 256 * a + e[i];
88
- }
89
- return a;
86
+ let a = 0;
87
+ for (let i = 0; i < t; i++) {
88
+ a = 256 * a + e[i];
89
+ }
90
+ return a;
90
91
  }
91
- const tabs = (n) => '\t'.repeat(n);
92
+ const tabs = n => '\t'.repeat(n);
92
93
  export function binaryNodeToString(node, i = 0) {
93
- if (!node) {
94
- return node;
95
- }
96
- if (typeof node === 'string') {
97
- return tabs(i) + node;
98
- }
99
- if (node instanceof Uint8Array) {
100
- return tabs(i) + Buffer.from(node).toString('hex');
101
- }
102
- if (Array.isArray(node)) {
103
- return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n');
104
- }
105
- const children = binaryNodeToString(node.content, i + 1);
106
- const tag = `<${node.tag} ${Object.entries(node.attrs || {})
107
- .filter(([, v]) => v !== undefined)
108
- .map(([k, v]) => `${k}='${v}'`)
109
- .join(' ')}`;
110
- const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>';
111
- return tag + content;
94
+ if (!node) {
95
+ return node;
96
+ }
97
+ if (typeof node === 'string') {
98
+ return tabs(i) + node;
99
+ }
100
+ if (node instanceof Uint8Array) {
101
+ return tabs(i) + Buffer.from(node).toString('hex');
102
+ }
103
+ if (Array.isArray(node)) {
104
+ return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n');
105
+ }
106
+ const children = binaryNodeToString(node.content, i + 1);
107
+ const tag = `<${node.tag} ${Object.entries(node.attrs || {}).filter(([, v]) => v !== undefined).map(([k, v]) => `${k}='${v}'`).join(' ')}`;
108
+ const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>';
109
+ return tag + content;
112
110
  }
113
- //# sourceMappingURL=generic-utils.js.map
@@ -3,4 +3,3 @@ export * from './decode.js';
3
3
  export * from './generic-utils.js';
4
4
  export * from './jid-utils.js';
5
5
  export * from './types.js';
6
- //# sourceMappingURL=index.js.map