gifted-baileys 1.5.4 → 1.5.6

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 (252) hide show
  1. package/LICENSE +1 -1
  2. package/WAProto/WAProto.proto +88 -969
  3. package/WAProto/index.d.ts +1256 -13195
  4. package/WAProto/index.js +74730 -125106
  5. package/package.json +9 -27
  6. package/src/Defaults/baileys-version.json +3 -0
  7. package/{lib → src}/Defaults/index.js +14 -7
  8. package/src/Defaults/index.ts +131 -0
  9. package/src/README.md +1 -0
  10. package/src/Signal/libsignal.js +180 -0
  11. package/src/Signal/libsignal.ts +141 -0
  12. package/src/Socket/Client/abstract-socket-client.ts +19 -0
  13. package/{lib → src}/Socket/Client/index.js +3 -2
  14. package/src/Socket/Client/index.ts +3 -0
  15. package/src/Socket/Client/mobile-socket-client.js +78 -0
  16. package/src/Socket/Client/mobile-socket-client.ts +66 -0
  17. package/src/Socket/Client/web-socket-client.js +75 -0
  18. package/src/Socket/Client/web-socket-client.ts +57 -0
  19. package/{lib → src}/Socket/business.js +33 -28
  20. package/src/Socket/business.ts +281 -0
  21. package/{lib → src}/Socket/chats.js +176 -174
  22. package/src/Socket/chats.ts +1030 -0
  23. package/{lib → src}/Socket/groups.js +68 -80
  24. package/src/Socket/groups.ts +356 -0
  25. package/{lib → src}/Socket/index.js +1 -4
  26. package/src/Socket/index.ts +13 -0
  27. package/{lib → src}/Socket/messages-recv.js +211 -378
  28. package/src/Socket/messages-recv.ts +985 -0
  29. package/{lib → src}/Socket/messages-send.js +177 -452
  30. package/src/Socket/messages-send.ts +871 -0
  31. package/{lib → src}/Socket/newsletter.js +98 -107
  32. package/src/Socket/newsletter.ts +282 -0
  33. package/{lib → src}/Socket/registration.js +48 -56
  34. package/src/Socket/registration.ts +250 -0
  35. package/{lib → src}/Socket/socket.js +77 -77
  36. package/src/Socket/socket.ts +777 -0
  37. package/src/Store/index.ts +3 -0
  38. package/{lib → src}/Store/make-cache-manager-store.js +34 -25
  39. package/src/Store/make-cache-manager-store.ts +100 -0
  40. package/{lib → src}/Store/make-in-memory-store.js +32 -36
  41. package/src/Store/make-in-memory-store.ts +475 -0
  42. package/src/Store/make-ordered-dictionary.ts +86 -0
  43. package/{lib → src}/Store/object-repository.js +1 -1
  44. package/src/Store/object-repository.ts +32 -0
  45. package/src/Tests/test.app-state-sync.js +204 -0
  46. package/src/Tests/test.app-state-sync.ts +207 -0
  47. package/src/Tests/test.event-buffer.js +270 -0
  48. package/src/Tests/test.event-buffer.ts +319 -0
  49. package/src/Tests/test.key-store.js +76 -0
  50. package/src/Tests/test.key-store.ts +92 -0
  51. package/src/Tests/test.libsignal.js +141 -0
  52. package/src/Tests/test.libsignal.ts +186 -0
  53. package/src/Tests/test.media-download.js +93 -0
  54. package/src/Tests/test.media-download.ts +76 -0
  55. package/src/Tests/test.messages.js +33 -0
  56. package/src/Tests/test.messages.ts +37 -0
  57. package/src/Tests/utils.js +34 -0
  58. package/src/Tests/utils.ts +36 -0
  59. package/src/Types/Auth.ts +113 -0
  60. package/src/Types/Call.ts +15 -0
  61. package/src/Types/Chat.ts +106 -0
  62. package/{lib/Types/Contact.d.ts → src/Types/Contact.ts} +9 -8
  63. package/src/Types/Events.ts +93 -0
  64. package/src/Types/GroupMetadata.ts +53 -0
  65. package/src/Types/Label.ts +36 -0
  66. package/{lib/Types/LabelAssociation.d.ts → src/Types/LabelAssociation.ts} +22 -16
  67. package/src/Types/Message.ts +288 -0
  68. package/src/Types/Newsletter.ts +98 -0
  69. package/src/Types/Product.ts +85 -0
  70. package/src/Types/Signal.ts +68 -0
  71. package/{lib/Types/Socket.d.ts → src/Types/Socket.ts} +68 -61
  72. package/src/Types/State.ts +29 -0
  73. package/src/Types/index.ts +59 -0
  74. package/{lib → src}/Utils/auth-utils.js +90 -73
  75. package/src/Utils/auth-utils.ts +222 -0
  76. package/src/Utils/baileys-event-stream.js +92 -0
  77. package/src/Utils/baileys-event-stream.ts +66 -0
  78. package/{lib → src}/Utils/business.js +43 -15
  79. package/src/Utils/business.ts +275 -0
  80. package/{lib → src}/Utils/chat-utils.js +94 -87
  81. package/src/Utils/chat-utils.ts +860 -0
  82. package/{lib → src}/Utils/crypto.js +2 -4
  83. package/src/Utils/crypto.ts +131 -0
  84. package/src/Utils/decode-wa-message.js +211 -0
  85. package/src/Utils/decode-wa-message.ts +228 -0
  86. package/{lib → src}/Utils/event-buffer.js +13 -4
  87. package/src/Utils/event-buffer.ts +613 -0
  88. package/{lib → src}/Utils/generics.js +86 -67
  89. package/src/Utils/generics.ts +434 -0
  90. package/{lib → src}/Utils/history.js +39 -13
  91. package/src/Utils/history.ts +112 -0
  92. package/src/Utils/index.ts +17 -0
  93. package/{lib → src}/Utils/link-preview.js +54 -17
  94. package/src/Utils/link-preview.ts +122 -0
  95. package/src/Utils/logger.ts +3 -0
  96. package/src/Utils/lt-hash.ts +61 -0
  97. package/{lib → src}/Utils/make-mutex.js +13 -4
  98. package/src/Utils/make-mutex.ts +44 -0
  99. package/{lib → src}/Utils/messages-media.js +255 -193
  100. package/src/Utils/messages-media.ts +847 -0
  101. package/{lib → src}/Utils/messages.js +118 -588
  102. package/src/Utils/messages.ts +956 -0
  103. package/src/Utils/noise-handler.ts +197 -0
  104. package/{lib → src}/Utils/process-message.js +30 -27
  105. package/src/Utils/process-message.ts +414 -0
  106. package/{lib → src}/Utils/signal.js +42 -25
  107. package/src/Utils/signal.ts +177 -0
  108. package/{lib → src}/Utils/use-multi-file-auth-state.js +28 -27
  109. package/src/Utils/use-multi-file-auth-state.ts +90 -0
  110. package/{lib → src}/Utils/validate-connection.js +9 -40
  111. package/src/Utils/validate-connection.ts +238 -0
  112. package/src/WABinary/constants.ts +42 -0
  113. package/src/WABinary/decode.ts +265 -0
  114. package/{lib → src}/WABinary/encode.js +10 -16
  115. package/src/WABinary/encode.ts +236 -0
  116. package/src/WABinary/generic-utils.ts +121 -0
  117. package/src/WABinary/index.ts +5 -0
  118. package/src/WABinary/jid-utils.ts +68 -0
  119. package/src/WABinary/types.ts +17 -0
  120. package/src/WAM/BinaryInfo.ts +12 -0
  121. package/src/WAM/constants.ts +15382 -0
  122. package/src/WAM/encode.ts +174 -0
  123. package/src/WAM/index.ts +3 -0
  124. package/{lib → src}/index.js +0 -1
  125. package/src/index.ts +13 -0
  126. package/README.md +0 -1645
  127. package/lib/Defaults/baileys-version.json +0 -3
  128. package/lib/Defaults/index.d.ts +0 -284
  129. package/lib/Signal/libsignal.d.ts +0 -3
  130. package/lib/Signal/libsignal.js +0 -161
  131. package/lib/Socket/Client/abstract-socket-client.d.ts +0 -15
  132. package/lib/Socket/Client/index.d.ts +0 -2
  133. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -12
  134. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  135. package/lib/Socket/Client/types.d.ts +0 -17
  136. package/lib/Socket/Client/types.js +0 -13
  137. package/lib/Socket/Client/websocket.d.ts +0 -12
  138. package/lib/Socket/Client/websocket.js +0 -62
  139. package/lib/Socket/business.d.ts +0 -170
  140. package/lib/Socket/chats.d.ts +0 -81
  141. package/lib/Socket/groups.d.ts +0 -115
  142. package/lib/Socket/index.d.ts +0 -172
  143. package/lib/Socket/messages-recv.d.ts +0 -158
  144. package/lib/Socket/messages-send.d.ts +0 -155
  145. package/lib/Socket/newsletter.d.ts +0 -132
  146. package/lib/Socket/registration.d.ts +0 -264
  147. package/lib/Socket/socket.d.ts +0 -44
  148. package/lib/Socket/usync.d.ts +0 -37
  149. package/lib/Socket/usync.js +0 -70
  150. package/lib/Store/index.d.ts +0 -3
  151. package/lib/Store/make-cache-manager-store.d.ts +0 -14
  152. package/lib/Store/make-in-memory-store.d.ts +0 -118
  153. package/lib/Store/make-ordered-dictionary.d.ts +0 -13
  154. package/lib/Store/object-repository.d.ts +0 -10
  155. package/lib/Types/Auth.d.ts +0 -109
  156. package/lib/Types/Call.d.ts +0 -13
  157. package/lib/Types/Chat.d.ts +0 -107
  158. package/lib/Types/Events.d.ts +0 -172
  159. package/lib/Types/GroupMetadata.d.ts +0 -56
  160. package/lib/Types/Label.d.ts +0 -46
  161. package/lib/Types/Message.d.ts +0 -433
  162. package/lib/Types/Newsletter.d.ts +0 -92
  163. package/lib/Types/Product.d.ts +0 -78
  164. package/lib/Types/Signal.d.ts +0 -57
  165. package/lib/Types/State.d.ts +0 -27
  166. package/lib/Types/USync.d.ts +0 -25
  167. package/lib/Types/USync.js +0 -2
  168. package/lib/Types/index.d.ts +0 -66
  169. package/lib/Utils/auth-utils.d.ts +0 -18
  170. package/lib/Utils/baileys-event-stream.d.ts +0 -16
  171. package/lib/Utils/baileys-event-stream.js +0 -63
  172. package/lib/Utils/business.d.ts +0 -22
  173. package/lib/Utils/chat-utils.d.ts +0 -70
  174. package/lib/Utils/crypto.d.ts +0 -40
  175. package/lib/Utils/decode-wa-message.d.ts +0 -36
  176. package/lib/Utils/decode-wa-message.js +0 -226
  177. package/lib/Utils/event-buffer.d.ts +0 -35
  178. package/lib/Utils/generics.d.ts +0 -88
  179. package/lib/Utils/history.d.ts +0 -19
  180. package/lib/Utils/index.d.ts +0 -17
  181. package/lib/Utils/link-preview.d.ts +0 -21
  182. package/lib/Utils/logger.d.ts +0 -2
  183. package/lib/Utils/lt-hash.d.ts +0 -12
  184. package/lib/Utils/make-mutex.d.ts +0 -7
  185. package/lib/Utils/messages-media.d.ts +0 -113
  186. package/lib/Utils/messages.d.ts +0 -77
  187. package/lib/Utils/noise-handler.d.ts +0 -20
  188. package/lib/Utils/process-message.d.ts +0 -41
  189. package/lib/Utils/signal.d.ts +0 -33
  190. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -12
  191. package/lib/Utils/validate-connection.d.ts +0 -11
  192. package/lib/WABinary/constants.d.ts +0 -27
  193. package/lib/WABinary/decode.d.ts +0 -6
  194. package/lib/WABinary/encode.d.ts +0 -2
  195. package/lib/WABinary/generic-utils.d.ts +0 -14
  196. package/lib/WABinary/index.d.ts +0 -5
  197. package/lib/WABinary/jid-utils.d.ts +0 -31
  198. package/lib/WABinary/types.d.ts +0 -18
  199. package/lib/WAM/BinaryInfo.d.ts +0 -8
  200. package/lib/WAM/constants.d.ts +0 -38
  201. package/lib/WAM/encode.d.ts +0 -2
  202. package/lib/WAM/index.d.ts +0 -3
  203. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -9
  204. package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -32
  205. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -22
  206. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -57
  207. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -12
  208. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -30
  209. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -12
  210. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -42
  211. package/lib/WAUSync/Protocols/index.d.ts +0 -4
  212. package/lib/WAUSync/Protocols/index.js +0 -20
  213. package/lib/WAUSync/USyncQuery.d.ts +0 -26
  214. package/lib/WAUSync/USyncQuery.js +0 -79
  215. package/lib/WAUSync/USyncUser.d.ts +0 -10
  216. package/lib/WAUSync/USyncUser.js +0 -22
  217. package/lib/WAUSync/index.d.ts +0 -3
  218. package/lib/WAUSync/index.js +0 -19
  219. /package/{lib → src}/Defaults/phonenumber-mcc.json +0 -0
  220. /package/{lib → src}/Socket/Client/abstract-socket-client.js +0 -0
  221. /package/{lib → src}/Store/index.js +0 -0
  222. /package/{lib → src}/Store/make-ordered-dictionary.js +0 -0
  223. /package/{lib → src}/Types/Auth.js +0 -0
  224. /package/{lib → src}/Types/Call.js +0 -0
  225. /package/{lib → src}/Types/Chat.js +0 -0
  226. /package/{lib → src}/Types/Contact.js +0 -0
  227. /package/{lib → src}/Types/Events.js +0 -0
  228. /package/{lib → src}/Types/GroupMetadata.js +0 -0
  229. /package/{lib → src}/Types/Label.js +0 -0
  230. /package/{lib → src}/Types/LabelAssociation.js +0 -0
  231. /package/{lib → src}/Types/Message.js +0 -0
  232. /package/{lib → src}/Types/Newsletter.js +0 -0
  233. /package/{lib → src}/Types/Product.js +0 -0
  234. /package/{lib → src}/Types/Signal.js +0 -0
  235. /package/{lib → src}/Types/Socket.js +0 -0
  236. /package/{lib → src}/Types/State.js +0 -0
  237. /package/{lib → src}/Types/index.js +0 -0
  238. /package/{lib → src}/Utils/index.js +0 -0
  239. /package/{lib → src}/Utils/logger.js +0 -0
  240. /package/{lib → src}/Utils/lt-hash.js +0 -0
  241. /package/{lib → src}/Utils/noise-handler.js +0 -0
  242. /package/{lib → src}/WABinary/constants.js +0 -0
  243. /package/{lib → src}/WABinary/decode.js +0 -0
  244. /package/{lib → src}/WABinary/generic-utils.js +0 -0
  245. /package/{lib → src}/WABinary/index.js +0 -0
  246. /package/{lib → src}/WABinary/jid-utils.js +0 -0
  247. /package/{lib → src}/WABinary/types.js +0 -0
  248. /package/{lib → src}/WAM/BinaryInfo.js +0 -0
  249. /package/{lib → src}/WAM/constants.js +0 -0
  250. /package/{lib → src}/WAM/encode.js +0 -0
  251. /package/{lib → src}/WAM/index.js +0 -0
  252. /package/{lib → src}/gifted +0 -0
@@ -36,12 +36,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.encodeBinaryNode = void 0;
37
37
  const constants = __importStar(require("./constants"));
38
38
  const jid_utils_1 = require("./jid-utils");
39
- const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
40
- const encoded = encodeBinaryNodeInner(node, opts, buffer);
41
- return Buffer.from(encoded);
42
- };
43
- exports.encodeBinaryNode = encodeBinaryNode;
44
- const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
39
+ const encodeBinaryNode = ({ tag, attrs, content }, opts = constants, buffer = [0]) => {
45
40
  const { TAGS, TOKEN_MAP } = opts;
46
41
  const pushByte = (value) => buffer.push(value & 0xff);
47
42
  const pushInt = (value, n, littleEndian = false) => {
@@ -50,11 +45,7 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
50
45
  buffer.push((value >> (curShift * 8)) & 0xff);
51
46
  }
52
47
  };
53
- const pushBytes = (bytes) => {
54
- for (const b of bytes) {
55
- buffer.push(b);
56
- }
57
- };
48
+ const pushBytes = (bytes) => (bytes.forEach(b => buffer.push(b)));
58
49
  const pushInt16 = (value) => {
59
50
  pushBytes([(value >> 8) & 0xff, value & 0xff]);
60
51
  };
@@ -156,7 +147,8 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
156
147
  if (str.length > TAGS.PACKED_MAX) {
157
148
  return false;
158
149
  }
159
- for (const char of str) {
150
+ for (let i = 0; i < str.length; i++) {
151
+ const char = str[i];
160
152
  const isInNibbleRange = char >= '0' && char <= '9';
161
153
  if (!isInNibbleRange && char !== '-' && char !== '.') {
162
154
  return false;
@@ -168,9 +160,10 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
168
160
  if (str.length > TAGS.PACKED_MAX) {
169
161
  return false;
170
162
  }
171
- for (const char of str) {
163
+ for (let i = 0; i < str.length; i++) {
164
+ const char = str[i];
172
165
  const isInNibbleRange = char >= '0' && char <= '9';
173
- if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
166
+ if (!isInNibbleRange && !(char >= 'A' && char <= 'F') && !(char >= 'a' && char <= 'f')) {
174
167
  return false;
175
168
  }
176
169
  }
@@ -231,7 +224,7 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
231
224
  else if (Array.isArray(content)) {
232
225
  writeListStart(content.length);
233
226
  for (const item of content) {
234
- encodeBinaryNodeInner(item, opts, buffer);
227
+ (0, exports.encodeBinaryNode)(item, opts, buffer);
235
228
  }
236
229
  }
237
230
  else if (typeof content === 'undefined') {
@@ -240,5 +233,6 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
240
233
  else {
241
234
  throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
242
235
  }
243
- return buffer;
236
+ return Buffer.from(buffer);
244
237
  };
238
+ exports.encodeBinaryNode = encodeBinaryNode;
@@ -0,0 +1,236 @@
1
+
2
+ import * as constants from './constants'
3
+ import { FullJid, jidDecode } from './jid-utils'
4
+ import type { BinaryNode, BinaryNodeCodingOptions } from './types'
5
+
6
+ export const encodeBinaryNode = (
7
+ { tag, attrs, content }: BinaryNode,
8
+ opts: Pick<BinaryNodeCodingOptions, 'TAGS' | 'TOKEN_MAP'> = constants,
9
+ buffer: number[] = [0]
10
+ ) => {
11
+ const { TAGS, TOKEN_MAP } = opts
12
+
13
+ const pushByte = (value: number) => buffer.push(value & 0xff)
14
+
15
+ const pushInt = (value: number, n: number, littleEndian = false) => {
16
+ for(let i = 0; i < n; i++) {
17
+ const curShift = littleEndian ? i : n - 1 - i
18
+ buffer.push((value >> (curShift * 8)) & 0xff)
19
+ }
20
+ }
21
+
22
+ const pushBytes = (bytes: Uint8Array | Buffer | number[]) => (
23
+ bytes.forEach (b => buffer.push(b))
24
+ )
25
+ const pushInt16 = (value: number) => {
26
+ pushBytes([(value >> 8) & 0xff, value & 0xff])
27
+ }
28
+
29
+ const pushInt20 = (value: number) => (
30
+ pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff])
31
+ )
32
+ const writeByteLength = (length: number) => {
33
+ if(length >= 4294967296) {
34
+ throw new Error('string too large to encode: ' + length)
35
+ }
36
+
37
+ if(length >= 1 << 20) {
38
+ pushByte(TAGS.BINARY_32)
39
+ pushInt(length, 4) // 32 bit integer
40
+ } else if(length >= 256) {
41
+ pushByte(TAGS.BINARY_20)
42
+ pushInt20(length)
43
+ } else {
44
+ pushByte(TAGS.BINARY_8)
45
+ pushByte(length)
46
+ }
47
+ }
48
+
49
+ const writeStringRaw = (str: string) => {
50
+ const bytes = Buffer.from (str, 'utf-8')
51
+ writeByteLength(bytes.length)
52
+ pushBytes(bytes)
53
+ }
54
+
55
+ const writeJid = ({ domainType, device, user, server }: FullJid) => {
56
+ if(typeof device !== 'undefined') {
57
+ pushByte(TAGS.AD_JID)
58
+ pushByte(domainType || 0)
59
+ pushByte(device || 0)
60
+ writeString(user)
61
+ } else {
62
+ pushByte(TAGS.JID_PAIR)
63
+ if(user.length) {
64
+ writeString(user)
65
+ } else {
66
+ pushByte(TAGS.LIST_EMPTY)
67
+ }
68
+
69
+ writeString(server)
70
+ }
71
+ }
72
+
73
+ const packNibble = (char: string) => {
74
+ switch (char) {
75
+ case '-':
76
+ return 10
77
+ case '.':
78
+ return 11
79
+ case '\0':
80
+ return 15
81
+ default:
82
+ if(char >= '0' && char <= '9') {
83
+ return char.charCodeAt(0) - '0'.charCodeAt(0)
84
+ }
85
+
86
+ throw new Error(`invalid byte for nibble "${char}"`)
87
+ }
88
+ }
89
+
90
+ const packHex = (char: string) => {
91
+ if(char >= '0' && char <= '9') {
92
+ return char.charCodeAt(0) - '0'.charCodeAt(0)
93
+ }
94
+
95
+ if(char >= 'A' && char <= 'F') {
96
+ return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0)
97
+ }
98
+
99
+ if(char >= 'a' && char <= 'f') {
100
+ return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0)
101
+ }
102
+
103
+ if(char === '\0') {
104
+ return 15
105
+ }
106
+
107
+ throw new Error(`Invalid hex char "${char}"`)
108
+ }
109
+
110
+ const writePackedBytes = (str: string, type: 'nibble' | 'hex') => {
111
+ if(str.length > TAGS.PACKED_MAX) {
112
+ throw new Error('Too many bytes to pack')
113
+ }
114
+
115
+ pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8)
116
+
117
+ let roundedLength = Math.ceil(str.length / 2.0)
118
+ if(str.length % 2 !== 0) {
119
+ roundedLength |= 128
120
+ }
121
+
122
+ pushByte(roundedLength)
123
+ const packFunction = type === 'nibble' ? packNibble : packHex
124
+
125
+ const packBytePair = (v1: string, v2: string) => {
126
+ const result = (packFunction(v1) << 4) | packFunction(v2)
127
+ return result
128
+ }
129
+
130
+ const strLengthHalf = Math.floor(str.length / 2)
131
+ for(let i = 0; i < strLengthHalf;i++) {
132
+ pushByte(packBytePair(str[2 * i], str[2 * i + 1]))
133
+ }
134
+
135
+ if(str.length % 2 !== 0) {
136
+ pushByte(packBytePair(str[str.length - 1], '\x00'))
137
+ }
138
+ }
139
+
140
+ const isNibble = (str: string) => {
141
+ if(str.length > TAGS.PACKED_MAX) {
142
+ return false
143
+ }
144
+
145
+ for(let i = 0;i < str.length;i++) {
146
+ const char = str[i]
147
+ const isInNibbleRange = char >= '0' && char <= '9'
148
+ if(!isInNibbleRange && char !== '-' && char !== '.') {
149
+ return false
150
+ }
151
+ }
152
+
153
+ return true
154
+ }
155
+
156
+ const isHex = (str: string) => {
157
+ if(str.length > TAGS.PACKED_MAX) {
158
+ return false
159
+ }
160
+
161
+ for(let i = 0;i < str.length;i++) {
162
+ const char = str[i]
163
+ const isInNibbleRange = char >= '0' && char <= '9'
164
+ if(!isInNibbleRange && !(char >= 'A' && char <= 'F') && !(char >= 'a' && char <= 'f')) {
165
+ return false
166
+ }
167
+ }
168
+
169
+ return true
170
+ }
171
+
172
+ const writeString = (str: string) => {
173
+ const tokenIndex = TOKEN_MAP[str]
174
+ if(tokenIndex) {
175
+ if(typeof tokenIndex.dict === 'number') {
176
+ pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict)
177
+ }
178
+
179
+ pushByte(tokenIndex.index)
180
+ } else if(isNibble(str)) {
181
+ writePackedBytes(str, 'nibble')
182
+ } else if(isHex(str)) {
183
+ writePackedBytes(str, 'hex')
184
+ } else if(str) {
185
+ const decodedJid = jidDecode(str)
186
+ if(decodedJid) {
187
+ writeJid(decodedJid)
188
+ } else {
189
+ writeStringRaw(str)
190
+ }
191
+ }
192
+ }
193
+
194
+ const writeListStart = (listSize: number) => {
195
+ if(listSize === 0) {
196
+ pushByte(TAGS.LIST_EMPTY)
197
+ } else if(listSize < 256) {
198
+ pushBytes([TAGS.LIST_8, listSize])
199
+ } else {
200
+ pushByte(TAGS.LIST_16)
201
+ pushInt16(listSize)
202
+ }
203
+ }
204
+
205
+ const validAttributes = Object.keys(attrs).filter(k => (
206
+ typeof attrs[k] !== 'undefined' && attrs[k] !== null
207
+ ))
208
+
209
+ writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0))
210
+ writeString(tag)
211
+
212
+ for(const key of validAttributes) {
213
+ if(typeof attrs[key] === 'string') {
214
+ writeString(key)
215
+ writeString(attrs[key])
216
+ }
217
+ }
218
+
219
+ if(typeof content === 'string') {
220
+ writeString(content)
221
+ } else if(Buffer.isBuffer(content) || content instanceof Uint8Array) {
222
+ writeByteLength(content.length)
223
+ pushBytes(content)
224
+ } else if(Array.isArray(content)) {
225
+ writeListStart(content.length)
226
+ for(const item of content) {
227
+ encodeBinaryNode(item, opts, buffer)
228
+ }
229
+ } else if(typeof content === 'undefined') {
230
+ // do nothing
231
+ } else {
232
+ throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`)
233
+ }
234
+
235
+ return Buffer.from(buffer)
236
+ }
@@ -0,0 +1,121 @@
1
+ import { Boom } from '@hapi/boom'
2
+ import { proto } from '../../WAProto'
3
+ import { BinaryNode } from './types'
4
+
5
+ // some extra useful utilities
6
+
7
+ export const getBinaryNodeChildren = (node: BinaryNode | undefined, childTag: string) => {
8
+ if(Array.isArray(node?.content)) {
9
+ return node!.content.filter(item => item.tag === childTag)
10
+ }
11
+
12
+ return []
13
+ }
14
+
15
+ export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
16
+ if(Array.isArray(content)) {
17
+ return content
18
+ }
19
+
20
+ return []
21
+ }
22
+
23
+ export const getBinaryNodeChild = (node: BinaryNode | undefined, childTag: string) => {
24
+ if(Array.isArray(node?.content)) {
25
+ return node?.content.find(item => item.tag === childTag)
26
+ }
27
+ }
28
+
29
+ export const getBinaryNodeChildBuffer = (node: BinaryNode | undefined, childTag: string) => {
30
+ const child = getBinaryNodeChild(node, childTag)?.content
31
+ if(Buffer.isBuffer(child) || child instanceof Uint8Array) {
32
+ return child
33
+ }
34
+ }
35
+
36
+ export const getBinaryNodeChildString = (node: BinaryNode | undefined, childTag: string) => {
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
+ }
43
+ }
44
+
45
+ export const getBinaryNodeChildUInt = (node: BinaryNode, childTag: string, length: number) => {
46
+ const buff = getBinaryNodeChildBuffer(node, childTag)
47
+ if(buff) {
48
+ return bufferToUInt(buff, length)
49
+ }
50
+ }
51
+
52
+ export const assertNodeErrorFree = (node: BinaryNode) => {
53
+ const errNode = getBinaryNodeChild(node, 'error')
54
+ if(errNode) {
55
+ throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code })
56
+ }
57
+ }
58
+
59
+ export const reduceBinaryNodeToDictionary = (node: BinaryNode, tag: string) => {
60
+ const nodes = getBinaryNodeChildren(node, tag)
61
+ const dict = nodes.reduce(
62
+ (dict, { attrs }) => {
63
+ dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value
64
+ return dict
65
+ }, { } as { [_: string]: string }
66
+ )
67
+ return dict
68
+ }
69
+
70
+ export const getBinaryNodeMessages = ({ content }: BinaryNode) => {
71
+ const msgs: proto.WebMessageInfo[] = []
72
+ if(Array.isArray(content)) {
73
+ for(const item of content) {
74
+ if(item.tag === 'message') {
75
+ msgs.push(proto.WebMessageInfo.decode(item.content as Buffer))
76
+ }
77
+ }
78
+ }
79
+
80
+ return msgs
81
+ }
82
+
83
+ function bufferToUInt(e: Uint8Array | Buffer, t: number) {
84
+ let a = 0
85
+ for(let i = 0; i < t; i++) {
86
+ a = 256 * a + e[i]
87
+ }
88
+
89
+ return a
90
+ }
91
+
92
+ const tabs = (n: number) => '\t'.repeat(n)
93
+
94
+ export function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i = 0) {
95
+ if(!node) {
96
+ return node
97
+ }
98
+
99
+ if(typeof node === 'string') {
100
+ return tabs(i) + node
101
+ }
102
+
103
+ if(node instanceof Uint8Array) {
104
+ return tabs(i) + Buffer.from(node).toString('hex')
105
+ }
106
+
107
+ if(Array.isArray(node)) {
108
+ return node.map((x) => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n')
109
+ }
110
+
111
+ const children = binaryNodeToString(node.content, i + 1)
112
+
113
+ const tag = `<${node.tag} ${Object.entries(node.attrs || {})
114
+ .filter(([, v]) => v !== undefined)
115
+ .map(([k, v]) => `${k}='${v}'`)
116
+ .join(' ')}`
117
+
118
+ const content: string = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>'
119
+
120
+ return tag + content
121
+ }
@@ -0,0 +1,5 @@
1
+ export * from './encode'
2
+ export * from './decode'
3
+ export * from './generic-utils'
4
+ export * from './jid-utils'
5
+ export * from './types'
@@ -0,0 +1,68 @@
1
+ export const S_WHATSAPP_NET = '@s.whatsapp.net'
2
+ export const OFFICIAL_BIZ_JID = '16505361212@c.us'
3
+ export const SERVER_JID = 'server@c.us'
4
+ export const PSA_WID = '0@c.us'
5
+ export const STORIES_JID = 'status@broadcast'
6
+
7
+ export type JidServer = 'c.us' | 'g.us' | 'broadcast' | 's.whatsapp.net' | 'call' | 'lid' | 'newsletter'
8
+
9
+ export type JidWithDevice = {
10
+ user: string
11
+ device?: number
12
+ }
13
+
14
+ export type FullJid = JidWithDevice & {
15
+ server: JidServer | string
16
+ domainType?: number
17
+ }
18
+
19
+ export const jidEncode = (user: string | number | null, server: JidServer, device?: number, agent?: number) => {
20
+ return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
21
+ }
22
+
23
+ export const jidDecode = (jid: string | undefined): FullJid | undefined => {
24
+ const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1
25
+ if(sepIdx < 0) {
26
+ return undefined
27
+ }
28
+
29
+ const server = jid!.slice(sepIdx + 1)
30
+ const userCombined = jid!.slice(0, sepIdx)
31
+
32
+ const [userAgent, device] = userCombined.split(':')
33
+ const user = userAgent.split('_')[0]
34
+
35
+ return {
36
+ server,
37
+ user,
38
+ domainType: server === 'lid' ? 1 : 0,
39
+ device: device ? +device : undefined
40
+ }
41
+ }
42
+
43
+ /** is the jid a user */
44
+ export const areJidsSameUser = (jid1: string | undefined, jid2: string | undefined) => (
45
+ jidDecode(jid1)?.user === jidDecode(jid2)?.user
46
+ )
47
+ /** is the jid a user */
48
+ export const isJidUser = (jid: string | undefined) => (jid?.endsWith('@s.whatsapp.net'))
49
+ /** is the jid a group */
50
+ export const isLidUser = (jid: string | undefined) => (jid?.endsWith('@lid'))
51
+ /** is the jid a broadcast */
52
+ export const isJidBroadcast = (jid: string | undefined) => (jid?.endsWith('@broadcast'))
53
+ /** is the jid a group */
54
+ export const isJidGroup = (jid: string | undefined) => (jid?.endsWith('@g.us'))
55
+ /** is the jid the status broadcast */
56
+ export const isJidStatusBroadcast = (jid: string) => jid === 'status@broadcast'
57
+ /** is the jid the newsletter */
58
+ export const isJidNewsLetter = (jid: string | undefined) => (jid?.endsWith('newsletter'))
59
+
60
+ export const jidNormalizedUser = (jid: string | undefined) => {
61
+ const result = jidDecode(jid)
62
+ if(!result) {
63
+ return ''
64
+ }
65
+
66
+ const { user, server } = result
67
+ return jidEncode(user, server === 'c.us' ? 's.whatsapp.net' : server as JidServer)
68
+ }
@@ -0,0 +1,17 @@
1
+ import * as constants from './constants'
2
+ /**
3
+ * the binary node WA uses internally for communication
4
+ *
5
+ * this is manipulated soley as an object and it does not have any functions.
6
+ * This is done for easy serialization, to prevent running into issues with prototypes &
7
+ * to maintain functional code structure
8
+ * */
9
+ export type BinaryNode = {
10
+ tag: string
11
+ attrs: { [key: string]: string }
12
+ content?: BinaryNode[] | string | Uint8Array
13
+ }
14
+ export type BinaryNodeAttributes = BinaryNode['attrs']
15
+ export type BinaryNodeData = BinaryNode['content']
16
+
17
+ export type BinaryNodeCodingOptions = typeof constants
@@ -0,0 +1,12 @@
1
+ import { EventInputType } from './constants'
2
+
3
+ export class BinaryInfo {
4
+ protocolVersion = 5
5
+ sequence = 0
6
+ events = [] as EventInputType[]
7
+ buffer: Buffer[] = []
8
+
9
+ constructor(options: Partial<BinaryInfo> = {}) {
10
+ Object.assign(this, options)
11
+ }
12
+ }