@neelegirl/baileys 1.4.3

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 (184) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +77 -0
  3. package/WAProto/GenerateStatics.sh +4 -0
  4. package/WAProto/WAProto.proto +4451 -0
  5. package/WAProto/index.d.ts +51459 -0
  6. package/WAProto/index.js +158781 -0
  7. package/WASignalGroup/GroupProtocol.js +1697 -0
  8. package/WASignalGroup/ciphertext_message.js +16 -0
  9. package/WASignalGroup/group_cipher.js +120 -0
  10. package/WASignalGroup/group_session_builder.js +46 -0
  11. package/WASignalGroup/index.js +5 -0
  12. package/WASignalGroup/keyhelper.js +21 -0
  13. package/WASignalGroup/protobufs.js +3 -0
  14. package/WASignalGroup/queue_job.js +69 -0
  15. package/WASignalGroup/sender_chain_key.js +50 -0
  16. package/WASignalGroup/sender_key_distribution_message.js +78 -0
  17. package/WASignalGroup/sender_key_message.js +92 -0
  18. package/WASignalGroup/sender_key_name.js +70 -0
  19. package/WASignalGroup/sender_key_record.js +56 -0
  20. package/WASignalGroup/sender_key_state.js +129 -0
  21. package/WASignalGroup/sender_message_key.js +39 -0
  22. package/package.json +88 -0
  23. package/src/Defaults/baileys-version.json +3 -0
  24. package/src/Defaults/index.d.ts +68 -0
  25. package/src/Defaults/index.js +131 -0
  26. package/src/Defaults/phonenumber-mcc.json +223 -0
  27. package/src/Signal/libsignal.d.ts +4 -0
  28. package/src/Signal/libsignal.js +162 -0
  29. package/src/Socket/Client/index.d.ts +2 -0
  30. package/src/Socket/Client/index.js +22 -0
  31. package/src/Socket/Client/types.d.ts +16 -0
  32. package/src/Socket/Client/types.js +18 -0
  33. package/src/Socket/Client/websocket.d.ts +13 -0
  34. package/src/Socket/Client/websocket.js +62 -0
  35. package/src/Socket/business.d.ts +182 -0
  36. package/src/Socket/business.js +268 -0
  37. package/src/Socket/chats.d.ts +96 -0
  38. package/src/Socket/chats.js +1278 -0
  39. package/src/Socket/groups.d.ts +130 -0
  40. package/src/Socket/groups.js +337 -0
  41. package/src/Socket/index.d.ts +185 -0
  42. package/src/Socket/index.js +14 -0
  43. package/src/Socket/messages-recv.d.ts +171 -0
  44. package/src/Socket/messages-recv.js +1182 -0
  45. package/src/Socket/messages-send.d.ts +162 -0
  46. package/src/Socket/messages-send.js +1086 -0
  47. package/src/Socket/newsletter.d.ts +142 -0
  48. package/src/Socket/newsletter.js +256 -0
  49. package/src/Socket/socket.d.ts +45 -0
  50. package/src/Socket/socket.js +749 -0
  51. package/src/Socket/usync.d.ts +37 -0
  52. package/src/Socket/usync.js +83 -0
  53. package/src/Store/index.d.ts +4 -0
  54. package/src/Store/index.js +24 -0
  55. package/src/Store/make-cache-manager-store.d.ts +14 -0
  56. package/src/Store/make-cache-manager-store.js +90 -0
  57. package/src/Store/make-in-memory-store.d.ts +123 -0
  58. package/src/Store/make-in-memory-store.js +429 -0
  59. package/src/Store/make-ordered-dictionary.d.ts +12 -0
  60. package/src/Store/make-ordered-dictionary.js +86 -0
  61. package/src/Store/object-repository.d.ts +10 -0
  62. package/src/Store/object-repository.js +31 -0
  63. package/src/Types/Auth.d.ts +120 -0
  64. package/src/Types/Auth.js +3 -0
  65. package/src/Types/Call.d.ts +14 -0
  66. package/src/Types/Call.js +3 -0
  67. package/src/Types/Chat.d.ts +134 -0
  68. package/src/Types/Chat.js +9 -0
  69. package/src/Types/Contact.d.ts +25 -0
  70. package/src/Types/Contact.js +3 -0
  71. package/src/Types/Events.d.ts +212 -0
  72. package/src/Types/Events.js +3 -0
  73. package/src/Types/GroupMetadata.d.ts +64 -0
  74. package/src/Types/GroupMetadata.js +3 -0
  75. package/src/Types/Label.d.ts +48 -0
  76. package/src/Types/Label.js +31 -0
  77. package/src/Types/LabelAssociation.d.ts +35 -0
  78. package/src/Types/LabelAssociation.js +13 -0
  79. package/src/Types/Message.d.ts +459 -0
  80. package/src/Types/Message.js +13 -0
  81. package/src/Types/Newsletter.d.ts +107 -0
  82. package/src/Types/Newsletter.js +38 -0
  83. package/src/Types/Product.d.ts +92 -0
  84. package/src/Types/Product.js +3 -0
  85. package/src/Types/Signal.d.ts +68 -0
  86. package/src/Types/Signal.js +3 -0
  87. package/src/Types/Socket.d.ts +120 -0
  88. package/src/Types/Socket.js +3 -0
  89. package/src/Types/State.d.ts +29 -0
  90. package/src/Types/State.js +3 -0
  91. package/src/Types/USync.d.ts +26 -0
  92. package/src/Types/USync.js +3 -0
  93. package/src/Types/index.d.ts +78 -0
  94. package/src/Types/index.js +47 -0
  95. package/src/Utils/auth-utils.d.ts +21 -0
  96. package/src/Utils/auth-utils.js +205 -0
  97. package/src/Utils/baileys-event-stream.d.ts +18 -0
  98. package/src/Utils/baileys-event-stream.js +70 -0
  99. package/src/Utils/business.d.ts +29 -0
  100. package/src/Utils/business.js +247 -0
  101. package/src/Utils/chat-utils.d.ts +82 -0
  102. package/src/Utils/chat-utils.js +774 -0
  103. package/src/Utils/crypto.d.ts +56 -0
  104. package/src/Utils/crypto.js +179 -0
  105. package/src/Utils/decode-wa-message.d.ts +41 -0
  106. package/src/Utils/decode-wa-message.js +236 -0
  107. package/src/Utils/event-buffer.d.ts +39 -0
  108. package/src/Utils/event-buffer.js +548 -0
  109. package/src/Utils/generics.d.ts +129 -0
  110. package/src/Utils/generics.js +584 -0
  111. package/src/Utils/history.d.ts +23 -0
  112. package/src/Utils/history.js +100 -0
  113. package/src/Utils/index.d.ts +19 -0
  114. package/src/Utils/index.js +39 -0
  115. package/src/Utils/link-preview.d.ts +23 -0
  116. package/src/Utils/link-preview.js +120 -0
  117. package/src/Utils/logger.d.ts +13 -0
  118. package/src/Utils/logger.js +7 -0
  119. package/src/Utils/lt-hash.d.ts +14 -0
  120. package/src/Utils/lt-hash.js +58 -0
  121. package/src/Utils/make-mutex.d.ts +9 -0
  122. package/src/Utils/make-mutex.js +49 -0
  123. package/src/Utils/messages-media.d.ts +129 -0
  124. package/src/Utils/messages-media.js +782 -0
  125. package/src/Utils/messages.d.ts +103 -0
  126. package/src/Utils/messages.js +1536 -0
  127. package/src/Utils/noise-handler.d.ts +20 -0
  128. package/src/Utils/noise-handler.js +155 -0
  129. package/src/Utils/process-message.d.ts +49 -0
  130. package/src/Utils/process-message.js +387 -0
  131. package/src/Utils/signal.d.ts +42 -0
  132. package/src/Utils/signal.js +166 -0
  133. package/src/Utils/use-mongo-file-auth-state.d.ts +6 -0
  134. package/src/Utils/use-mongo-file-auth-state.js +84 -0
  135. package/src/Utils/use-multi-file-auth-state.d.ts +13 -0
  136. package/src/Utils/use-multi-file-auth-state.js +131 -0
  137. package/src/Utils/use-single-file-auth-state.d.ts +13 -0
  138. package/src/Utils/use-single-file-auth-state.js +80 -0
  139. package/src/Utils/validate-connection.d.ts +13 -0
  140. package/src/Utils/validate-connection.js +186 -0
  141. package/src/WABinary/constants.d.ts +30 -0
  142. package/src/WABinary/constants.js +53 -0
  143. package/src/WABinary/decode.d.ts +9 -0
  144. package/src/WABinary/decode.js +288 -0
  145. package/src/WABinary/encode.d.ts +3 -0
  146. package/src/WABinary/encode.js +256 -0
  147. package/src/WABinary/generic-utils.d.ts +28 -0
  148. package/src/WABinary/generic-utils.js +149 -0
  149. package/src/WABinary/index.d.ts +5 -0
  150. package/src/WABinary/index.js +25 -0
  151. package/src/WABinary/jid-utils.d.ts +53 -0
  152. package/src/WABinary/jid-utils.js +92 -0
  153. package/src/WABinary/types.d.ts +22 -0
  154. package/src/WABinary/types.js +3 -0
  155. package/src/WAM/BinaryInfo.d.ts +16 -0
  156. package/src/WAM/BinaryInfo.js +17 -0
  157. package/src/WAM/constants.d.ts +47 -0
  158. package/src/WAM/constants.js +15371 -0
  159. package/src/WAM/encode.d.ts +3 -0
  160. package/src/WAM/encode.js +165 -0
  161. package/src/WAM/index.d.ts +3 -0
  162. package/src/WAM/index.js +23 -0
  163. package/src/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +28 -0
  164. package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +69 -0
  165. package/src/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  166. package/src/WAUSync/Protocols/USyncContactProtocol.js +36 -0
  167. package/src/WAUSync/Protocols/USyncDeviceProtocol.d.ts +26 -0
  168. package/src/WAUSync/Protocols/USyncDeviceProtocol.js +62 -0
  169. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +14 -0
  170. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +35 -0
  171. package/src/WAUSync/Protocols/USyncLIDProtocol.d.ts +9 -0
  172. package/src/WAUSync/Protocols/USyncLIDProtocol.js +30 -0
  173. package/src/WAUSync/Protocols/USyncStatusProtocol.d.ts +14 -0
  174. package/src/WAUSync/Protocols/USyncStatusProtocol.js +46 -0
  175. package/src/WAUSync/Protocols/index.d.ts +6 -0
  176. package/src/WAUSync/Protocols/index.js +26 -0
  177. package/src/WAUSync/USyncQuery.d.ts +31 -0
  178. package/src/WAUSync/USyncQuery.js +92 -0
  179. package/src/WAUSync/USyncUser.d.ts +12 -0
  180. package/src/WAUSync/USyncUser.js +30 -0
  181. package/src/WAUSync/index.d.ts +3 -0
  182. package/src/WAUSync/index.js +23 -0
  183. package/src/index.d.ts +13 -0
  184. package/src/index.js +33 -0
@@ -0,0 +1,782 @@
1
+ "use strict"
2
+
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k
5
+ var desc = Object.getOwnPropertyDescriptor(m, k)
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k] } }
8
+ }
9
+ Object.defineProperty(o, k2, desc)
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k
12
+ o[k2] = m[k]
13
+ }))
14
+
15
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
+ Object.defineProperty(o, "default", { enumerable: true, value: v })
17
+ }) : function(o, v) {
18
+ o["default"] = v
19
+ })
20
+
21
+ var __importStar = (this && this.__importStar) || function (mod) {
22
+ if (mod && mod.__esModule) return mod
23
+ var result = {}
24
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k)
25
+ __setModuleDefault(result, mod)
26
+ return result
27
+ }
28
+
29
+ var __importDefault = (this && this.__importDefault) || function (mod) {
30
+ return (mod && mod.__esModule) ? mod : { "default": mod }
31
+ }
32
+
33
+ Object.defineProperty(exports, "__esModule", { value: true })
34
+
35
+ const boom_1 = require("@hapi/boom")
36
+ const axios_1 = __importDefault(require("axios"))
37
+ const child_process_1 = require("child_process")
38
+ const Crypto = __importStar(require("crypto"))
39
+ const events_1 = require("events")
40
+ const fs_1 = require("fs")
41
+ const os_1 = require("os")
42
+ const path_1 = require("path")
43
+ const stream_1 = require("stream")
44
+ const WAProto_1 = require("../../WAProto")
45
+ const Defaults_1 = require("../Defaults")
46
+ const WABinary_1 = require("../WABinary")
47
+ const crypto_1 = require("./crypto")
48
+ const generics_1 = require("./generics")
49
+
50
+ const getImageProcessingLibrary = async () => {
51
+ const [_jimp, sharp] = await Promise.all([
52
+ (async () => {
53
+ const jimp = await (Promise.resolve().then(() => __importStar(require('jimp'))).catch(() => { }))
54
+ return jimp
55
+ })(),
56
+ (async () => {
57
+ const sharp = await (Promise.resolve().then(() => __importStar(require('sharp'))).catch(() => { }))
58
+ return sharp
59
+ })()
60
+ ])
61
+ if (sharp) {
62
+ return { sharp }
63
+ }
64
+ const jimp = (_jimp === null || _jimp === void 0 ? void 0 : _jimp.default) || _jimp
65
+ if (jimp) {
66
+ return { jimp }
67
+ }
68
+ throw new boom_1.Boom('No image processing library available')
69
+ }
70
+
71
+ const hkdfInfoKey = (type) => {
72
+ const hkdfInfo = Defaults_1.MEDIA_HKDF_KEY_MAPPING[type]
73
+ return `WhatsApp ${hkdfInfo} Keys`
74
+ }
75
+
76
+ /** generates all the keys required to encrypt/decrypt & sign a media message */
77
+ async function getMediaKeys(buffer, mediaType) {
78
+ if (!buffer) {
79
+ throw new boom_1.Boom('Cannot derive from empty media key')
80
+ }
81
+ if (typeof buffer === 'string') {
82
+ buffer = Buffer.from(buffer.replace('data:base64,', ''), 'base64')
83
+ }
84
+ // expand using HKDF to 112 bytes, also pass in the relevant app info
85
+ const expandedMediaKey = await crypto_1.hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) })
86
+ return {
87
+ iv: expandedMediaKey.slice(0, 16),
88
+ cipherKey: expandedMediaKey.slice(16, 48),
89
+ macKey: expandedMediaKey.slice(48, 80),
90
+ }
91
+ }
92
+
93
+ /** Extracts video thumb using FFMPEG */
94
+ const extractVideoThumb = (videoPath, time = '00:00:00', size = { width: 320 }) => {
95
+ return new Promise((resolve, reject) => {
96
+ const args = [
97
+ '-ss', time,
98
+ '-i', videoPath,
99
+ '-y',
100
+ '-vf', `scale=${size.width}:-1`,
101
+ '-vframes', '1',
102
+ '-f', 'image2',
103
+ '-vcodec', 'mjpeg',
104
+ 'pipe:1'
105
+ ]
106
+
107
+ const ffmpeg = child_process_1.spawn('ffmpeg', args)
108
+ const chunks = []
109
+ let errorOutput = ''
110
+
111
+ ffmpeg.stdout.on('data', chunk => chunks.push(chunk))
112
+ ffmpeg.stderr.on('data', data => {
113
+ errorOutput += data.toString()
114
+ })
115
+ ffmpeg.on('error', reject)
116
+ ffmpeg.on('close', code => {
117
+ if (code === 0) return resolve(Buffer.concat(chunks))
118
+ reject(new Error(`ffmpeg exited with code ${code}\n${errorOutput}`))
119
+ })
120
+ })
121
+ }
122
+
123
+ const extractImageThumb = async (bufferOrFilePath, width = 32, quality = 50) => {
124
+ if (typeof bufferOrFilePath === "string" && bufferOrFilePath.startsWith("http")) {
125
+ const response = await axios_1.default.get(bufferOrFilePath, { responseType: "arraybuffer" })
126
+ bufferOrFilePath = Buffer.from(response.data)
127
+ }
128
+ if (bufferOrFilePath instanceof stream_1.Readable) {
129
+ bufferOrFilePath = await toBuffer(bufferOrFilePath)
130
+ }
131
+ const lib = await getImageProcessingLibrary()
132
+ if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
133
+ const img = lib.sharp.default(bufferOrFilePath)
134
+ const dimensions = await img.metadata()
135
+ const buffer = await img
136
+ .resize({
137
+ width,
138
+ height: width,
139
+ fit: 'contain',
140
+ background: { r: 255, g: 255, b: 255, alpha: 0 }
141
+ })
142
+ .jpeg({ quality })
143
+ .toBuffer()
144
+ return {
145
+ buffer,
146
+ original: {
147
+ width: dimensions.width,
148
+ height: dimensions.height,
149
+ },
150
+ }
151
+ }
152
+ else if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
153
+ const { read, MIME_JPEG, RESIZE_BEZIER, AUTO } = lib.jimp
154
+ const jimp = await read(bufferOrFilePath)
155
+ const dimensions = {
156
+ width: jimp.getWidth(),
157
+ height: jimp.getHeight()
158
+ }
159
+ const buffer = await jimp
160
+ .quality(quality)
161
+ .resize(width, AUTO, RESIZE_BEZIER)
162
+ .getBufferAsync(MIME_JPEG)
163
+ return {
164
+ buffer,
165
+ original: dimensions
166
+ }
167
+ }
168
+ else {
169
+ throw new boom_1.Boom('No image processing library available')
170
+ }
171
+ }
172
+
173
+ const encodeBase64EncodedStringForUpload = (b64) => (encodeURIComponent(b64
174
+ .replace(/\+/g, '-')
175
+ .replace(/\//g, '_')
176
+ .replace(/\=+$/, '')))
177
+
178
+ const generateProfilePicture = async (mediaUpload) => {
179
+ let bufferOrFilePath
180
+ if (Buffer.isBuffer(mediaUpload)) {
181
+ bufferOrFilePath = mediaUpload
182
+ }
183
+ else if ('url' in mediaUpload) {
184
+ bufferOrFilePath = mediaUpload.url.toString()
185
+ }
186
+ else {
187
+ bufferOrFilePath = await toBuffer(mediaUpload.stream)
188
+ }
189
+ const lib = await getImageProcessingLibrary()
190
+ let img
191
+ if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
192
+ img = await lib.sharp.default(bufferOrFilePath)
193
+ .resize(720, 720, {
194
+ fit: 'inside',
195
+ })
196
+ .jpeg({ quality: 50 })
197
+ .toBuffer()
198
+ }
199
+ else if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
200
+ const { read, MIME_JPEG } = lib.jimp
201
+ const image = await read(bufferOrFilePath)
202
+ const min = image.getWidth()
203
+ const max = image.getHeight()
204
+ const cropped = image.crop(0, 0, min, max)
205
+ img = await cropped.scaleToFit(720, 720).getBufferAsync(MIME_JPEG)
206
+ }
207
+ else {
208
+ throw new boom_1.Boom('No image processing library available')
209
+ }
210
+ return {
211
+ img: await img,
212
+ }
213
+ }
214
+
215
+
216
+ /** gets the SHA256 of the given media message */
217
+ const mediaMessageSHA256B64 = (message) => {
218
+ const media = Object.values(message)[0]
219
+ return (media === null || media === void 0 ? void 0 : media.fileSha256) && Buffer.from(media.fileSha256).toString('base64')
220
+ }
221
+
222
+ async function getAudioDuration(buffer) {
223
+ const musicMetadata = await Promise.resolve().then(() => __importStar(require('music-metadata')))
224
+ let metadata
225
+ if (Buffer.isBuffer(buffer)) {
226
+ metadata = await musicMetadata.parseBuffer(buffer, undefined, { duration: true })
227
+ }
228
+ else if (typeof buffer === 'string') {
229
+ const rStream = fs_1.createReadStream(buffer)
230
+ try {
231
+ metadata = await musicMetadata.parseStream(rStream, undefined, { duration: true })
232
+ }
233
+ finally {
234
+ rStream.destroy()
235
+ }
236
+ }
237
+ else {
238
+ metadata = await musicMetadata.parseStream(buffer, undefined, { duration: true })
239
+ }
240
+ return metadata.format.duration
241
+ }
242
+
243
+ /**
244
+ referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
245
+ */
246
+ async function getAudioWaveform(buffer, logger) {
247
+ try {
248
+ const { default: decoder } = await eval('import(\'audio-decode\')')
249
+ let audioData
250
+ if (Buffer.isBuffer(buffer)) {
251
+ audioData = buffer
252
+ }
253
+ else if (typeof buffer === 'string') {
254
+ const rStream = fs_1.createReadStream(buffer)
255
+ audioData = await toBuffer(rStream)
256
+ }
257
+ else {
258
+ audioData = await toBuffer(buffer)
259
+ }
260
+ const audioBuffer = await decoder(audioData)
261
+ const rawData = audioBuffer.getChannelData(0) // We only need to work with one channel of data
262
+ const samples = 64 // Number of samples we want to have in our final data set
263
+ const blockSize = Math.floor(rawData.length / samples) // the number of samples in each subdivision
264
+ const filteredData = []
265
+ for (let i = 0; i < samples; i++) {
266
+ const blockStart = blockSize * i // the location of the first sample in the block
267
+ let sum = 0
268
+ for (let j = 0; j < blockSize; j++) {
269
+ sum = sum + Math.abs(rawData[blockStart + j]) // find the sum of all the samples in the block
270
+ }
271
+ filteredData.push(sum / blockSize) // divide the sum by the block size to get the average
272
+ }
273
+ // This guarantees that the largest data point will be set to 1, and the rest of the data will scale proportionally.
274
+ const multiplier = Math.pow(Math.max(...filteredData), -1)
275
+ const normalizedData = filteredData.map((n) => n * multiplier)
276
+ // Generate waveform like WhatsApp
277
+ const waveform = new Uint8Array(normalizedData.map((n) => Math.floor(100 * n)))
278
+ return waveform
279
+ }
280
+ catch (e) {
281
+ logger?.debug('Failed to generate waveform: ' + e)
282
+ }
283
+ }
284
+
285
+ const toReadable = (buffer) => {
286
+ const readable = new stream_1.Readable({ read: () => { } })
287
+ readable.push(buffer)
288
+ readable.push(null)
289
+ return readable
290
+ }
291
+
292
+ const toBuffer = async (stream) => {
293
+ const chunks = []
294
+ for await (const chunk of stream) {
295
+ chunks.push(chunk)
296
+ }
297
+ stream.destroy()
298
+ return Buffer.concat(chunks)
299
+ }
300
+
301
+ const getStream = async (item, opts) => {
302
+ if (Buffer.isBuffer(item)) {
303
+ return { stream: toReadable(item), type: 'buffer' }
304
+ }
305
+ if ('stream' in item) {
306
+ return { stream: item.stream, type: 'readable' }
307
+ }
308
+ if (item.url.toString().startsWith('http://') || item.url.toString().startsWith('https://')) {
309
+ return { stream: await getHttpStream(item.url, opts), type: 'remote' }
310
+ }
311
+ return { stream: fs_1.createReadStream(item.url), type: 'file' }
312
+ }
313
+
314
+ /** generates a thumbnail for a given media, if required */
315
+ async function generateThumbnail(file, mediaType, options) {
316
+ let thumbnail
317
+ let originalImageDimensions
318
+ if (mediaType === 'image') {
319
+ const { buffer, original } = await extractImageThumb(file, 256, 95)
320
+ thumbnail = buffer.toString('base64')
321
+ if (original.width && original.height) {
322
+ originalImageDimensions = {
323
+ width: original.width,
324
+ height: original.height,
325
+ }
326
+ }
327
+ }
328
+ else if (mediaType === 'video') {
329
+ try {
330
+ const buff = await extractVideoThumb(file, '00:00:00', { width: 32, height: 32 })
331
+ thumbnail = buff.toString('base64')
332
+ }
333
+ catch (err) {
334
+ options.logger?.debug('could not generate video thumb: ' + err)
335
+ }
336
+ }
337
+ return {
338
+ thumbnail,
339
+ originalImageDimensions
340
+ }
341
+ }
342
+
343
+ const getHttpStream = async (url, options = {}) => {
344
+ const fetched = await axios_1.default.get(url.toString(), { ...options, responseType: 'stream' })
345
+ return fetched.data
346
+ }
347
+
348
+ const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
349
+ const { stream, type } = await getStream(media, opts)
350
+ logger?.debug('fetched media stream')
351
+ let bodyPath
352
+ let didSaveToTmpPath = false
353
+ try {
354
+ const buffer = await toBuffer(stream)
355
+ if (type === 'file') {
356
+ bodyPath = media.url
357
+ }
358
+ else if (saveOriginalFileIfRequired) {
359
+ bodyPath = path_1.join(os_1.tmpdir(), mediaType + generics_1.generateMessageID())
360
+ fs_1.writeFileSync(bodyPath, buffer)
361
+ didSaveToTmpPath = true
362
+ }
363
+ const fileLength = buffer.length
364
+ const fileSha256 = Crypto.createHash('sha256').update(buffer).digest()
365
+ stream?.destroy()
366
+ logger?.debug('prepare stream data successfully')
367
+ return {
368
+ mediaKey: undefined,
369
+ encWriteStream: buffer,
370
+ fileLength,
371
+ fileSha256,
372
+ fileEncSha256: undefined,
373
+ bodyPath,
374
+ didSaveToTmpPath
375
+ }
376
+ }
377
+ catch (error) {
378
+ // destroy all streams with error
379
+ stream.destroy()
380
+ if (didSaveToTmpPath) {
381
+ try {
382
+ await fs_1.promises.unlink(bodyPath)
383
+ }
384
+ catch (err) {
385
+ logger?.error({ err }, 'failed to save to tmp path')
386
+ }
387
+ }
388
+ throw error
389
+ }
390
+ }
391
+
392
+ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
393
+ const { stream, type } = await getStream(media, opts)
394
+ logger?.debug('fetched media stream')
395
+ const mediaKey = Crypto.randomBytes(32)
396
+ const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType)
397
+ const encWriteStream = new stream_1.Readable({ read: () => { } })
398
+ let bodyPath
399
+ let writeStream
400
+ let didSaveToTmpPath = false
401
+ if (type === 'file') {
402
+ bodyPath = media.url.toString()
403
+ }
404
+ else if (saveOriginalFileIfRequired) {
405
+ bodyPath = path_1.join(os_1.tmpdir(), mediaType + generics_1.generateMessageID())
406
+ writeStream = fs_1.createWriteStream(bodyPath)
407
+ didSaveToTmpPath = true
408
+ }
409
+ let fileLength = 0
410
+ const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv)
411
+ let hmac = Crypto.createHmac('sha256', macKey).update(iv)
412
+ let sha256Plain = Crypto.createHash('sha256')
413
+ let sha256Enc = Crypto.createHash('sha256')
414
+ try {
415
+ for await (const data of stream) {
416
+ fileLength += data.length
417
+ if (type === 'remote'
418
+ && (opts === null || opts === void 0 ? void 0 : opts.maxContentLength)
419
+ && fileLength + data.length > opts.maxContentLength) {
420
+ throw new boom_1.Boom(`content length exceeded when encrypting "${type}"`, {
421
+ data: { media, type }
422
+ })
423
+ }
424
+ sha256Plain = sha256Plain.update(data)
425
+ if (writeStream && !writeStream.write(data)) {
426
+ await events_1.once(writeStream, 'drain')
427
+ }
428
+ onChunk(aes.update(data))
429
+ }
430
+ onChunk(aes.final())
431
+ const mac = hmac.digest().slice(0, 10)
432
+ sha256Enc = sha256Enc.update(mac)
433
+ const fileSha256 = sha256Plain.digest()
434
+ const fileEncSha256 = sha256Enc.digest()
435
+ encWriteStream.push(mac)
436
+ encWriteStream.push(null)
437
+ writeStream?.end()
438
+ stream.destroy()
439
+ logger?.debug('encrypted data successfully')
440
+ return {
441
+ mediaKey,
442
+ encWriteStream,
443
+ bodyPath,
444
+ mac,
445
+ fileEncSha256,
446
+ fileSha256,
447
+ fileLength,
448
+ didSaveToTmpPath
449
+ }
450
+ }
451
+ catch (error) {
452
+ // destroy all streams with error
453
+ encWriteStream.destroy()
454
+ writeStream?.destroy()
455
+ aes.destroy()
456
+ hmac.destroy()
457
+ sha256Plain.destroy()
458
+ sha256Enc.destroy()
459
+ stream.destroy()
460
+ if (didSaveToTmpPath) {
461
+ try {
462
+ await fs_1.promises.unlink(bodyPath)
463
+ }
464
+ catch (err) {
465
+ logger?.error({ err }, 'failed to save to tmp path')
466
+ }
467
+ }
468
+ throw error
469
+ }
470
+ function onChunk(buff) {
471
+ sha256Enc = sha256Enc.update(buff)
472
+ hmac = hmac.update(buff)
473
+ encWriteStream.push(buff)
474
+ }
475
+ }
476
+
477
+ const DEF_HOST = 'mmg.whatsapp.net'
478
+
479
+ const AES_CHUNK_SIZE = 16
480
+
481
+ const toSmallestChunkSize = (num) => {
482
+ return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE
483
+ }
484
+ const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`
485
+
486
+ const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
487
+ const downloadUrl = url || getUrlFromDirectPath(directPath)
488
+ const keys = await getMediaKeys(mediaKey, type)
489
+ return downloadEncryptedContent(downloadUrl, keys, opts)
490
+ }
491
+
492
+ /**
493
+ * Decrypts and downloads an AES256-CBC encrypted file given the keys.
494
+ * Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
495
+ * */
496
+ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
497
+ let bytesFetched = 0
498
+ let startChunk = 0
499
+ let firstBlockIsIV = false
500
+ // if a start byte is specified -- then we need to fetch the previous chunk as that will form the IV
501
+ if (startByte) {
502
+ const chunk = toSmallestChunkSize(startByte || 0)
503
+ if (chunk) {
504
+ startChunk = chunk - AES_CHUNK_SIZE
505
+ bytesFetched = chunk
506
+ firstBlockIsIV = true
507
+ }
508
+ }
509
+ const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined
510
+ const headers = {
511
+ ...(options?.headers) || {},
512
+ Origin: Defaults_1.DEFAULT_ORIGIN,
513
+ }
514
+ if (startChunk || endChunk) {
515
+ headers.Range = `bytes=${startChunk}-`
516
+ if (endChunk) {
517
+ headers.Range += endChunk
518
+ }
519
+ }
520
+ // download the message
521
+ const fetched = await getHttpStream(downloadUrl, {
522
+ ...options || {},
523
+ headers,
524
+ maxBodyLength: Infinity,
525
+ maxContentLength: Infinity,
526
+ })
527
+ let remainingBytes = Buffer.from([])
528
+ let aes
529
+ const pushBytes = (bytes, push) => {
530
+ if (startByte || endByte) {
531
+ const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0)
532
+ const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0)
533
+ push(bytes.slice(start, end))
534
+ bytesFetched += bytes.length
535
+ }
536
+ else {
537
+ push(bytes)
538
+ }
539
+ }
540
+ const output = new stream_1.Transform({
541
+ transform(chunk, _, callback) {
542
+ let data = Buffer.concat([remainingBytes, chunk])
543
+ const decryptLength = toSmallestChunkSize(data.length)
544
+ remainingBytes = data.slice(decryptLength)
545
+ data = data.slice(0, decryptLength)
546
+ if (!aes) {
547
+ let ivValue = iv
548
+ if (firstBlockIsIV) {
549
+ ivValue = data.slice(0, AES_CHUNK_SIZE)
550
+ data = data.slice(AES_CHUNK_SIZE)
551
+ }
552
+ aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue)
553
+ // if an end byte that is not EOF is specified
554
+ // stop auto padding (PKCS7) -- otherwise throws an error for decryption
555
+ if (endByte) {
556
+ aes.setAutoPadding(false)
557
+ }
558
+ }
559
+ try {
560
+ pushBytes(aes.update(data), b => this.push(b))
561
+ callback()
562
+ }
563
+ catch (error) {
564
+ callback(error)
565
+ }
566
+ },
567
+ final(callback) {
568
+ try {
569
+ pushBytes(aes.final(), b => this.push(b))
570
+ callback()
571
+ }
572
+ catch (error) {
573
+ callback(error)
574
+ }
575
+ },
576
+ })
577
+ return fetched.pipe(output, { end: true })
578
+ }
579
+
580
+ function extensionForMediaMessage(message) {
581
+ const getExtension = (mimetype) => mimetype.split('')[0].split('/')[1]
582
+ const type = Object.keys(message)[0]
583
+ let extension
584
+ if (type === 'locationMessage' ||
585
+ type === 'liveLocationMessage' ||
586
+ type === 'productMessage') {
587
+ extension = '.jpeg'
588
+ }
589
+ else {
590
+ const messageContent = message[type]
591
+ extension = getExtension(messageContent.mimetype)
592
+ }
593
+ return extension
594
+ }
595
+
596
+ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
597
+ return async (stream, { mediaType, fileEncSha256B64, newsletter, timeoutMs }) => {
598
+ // send a query JSON to obtain the url & auth token to upload our media
599
+ let uploadInfo = await refreshMediaConn(false)
600
+ let urls
601
+ const hosts = [...customUploadHosts, ...uploadInfo.hosts]
602
+ const chunks = []
603
+ if (!Buffer.isBuffer(stream)) {
604
+ for await (const chunk of stream) {
605
+ chunks.push(chunk)
606
+ }
607
+ }
608
+ const reqBody = Buffer.isBuffer(stream) ? stream : Buffer.concat(chunks)
609
+ fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64)
610
+ let media = Defaults_1.MEDIA_PATH_MAP[mediaType]
611
+ if (newsletter) {
612
+ media = media?.replace('/mms/', '/newsletter/newsletter-')
613
+ }
614
+ for (const { hostname, maxContentLengthBytes } of hosts) {
615
+ logger.debug(`uploading to "${hostname}"`)
616
+ const auth = encodeURIComponent(uploadInfo.auth) // the auth token
617
+ const url = `https://${hostname}${media}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
618
+ let result
619
+ try {
620
+ if (maxContentLengthBytes && reqBody.length > maxContentLengthBytes) {
621
+ throw new boom_1.Boom(`Body too large for "${hostname}"`, { statusCode: 413 })
622
+ }
623
+ const body = await axios_1.default.post(url, reqBody, {
624
+ ...options,
625
+ headers: {
626
+ ...options.headers || {},
627
+ 'Content-Type': 'application/octet-stream',
628
+ 'Origin': Defaults_1.DEFAULT_ORIGIN
629
+ },
630
+ httpsAgent: fetchAgent,
631
+ timeout: timeoutMs,
632
+ responseType: 'json',
633
+ maxBodyLength: Infinity,
634
+ maxContentLength: Infinity
635
+ })
636
+ result = body.data
637
+ if ((result?.url) || (result?.directPath)) {
638
+ urls = {
639
+ mediaUrl: result.url,
640
+ directPath: result.direct_path,
641
+ handle: result.handle
642
+ }
643
+ break
644
+ }
645
+ else {
646
+ uploadInfo = await refreshMediaConn(true)
647
+ throw new Error(`upload failed, reason: ${JSON.stringify(result)}`)
648
+ }
649
+ }
650
+ catch (error) {
651
+ if (axios.isAxiosError(error)) {
652
+ result = error.response?.data
653
+ }
654
+ const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname
655
+ logger.warn({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`)
656
+ }
657
+ }
658
+ if (!urls) {
659
+ throw new boom_1.Boom('Media upload failed on all hosts', { statusCode: 500 })
660
+ }
661
+ return urls
662
+ }
663
+ }
664
+
665
+ const getMediaRetryKey = (mediaKey) => {
666
+ return crypto_1.hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' })
667
+ }
668
+ /**
669
+ * Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
670
+ */
671
+ const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
672
+ const recp = { stanzaId: key.id }
673
+ const recpBuffer = WAProto_1.proto.ServerErrorReceipt.encode(recp).finish()
674
+ const iv = Crypto.randomBytes(12)
675
+ const retryKey = await getMediaRetryKey(mediaKey)
676
+ const ciphertext = crypto_1.aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id))
677
+ const req = {
678
+ tag: 'receipt',
679
+ attrs: {
680
+ id: key.id,
681
+ to: WABinary_1.jidNormalizedUser(meId),
682
+ type: 'server-error'
683
+ },
684
+ content: [
685
+ // this encrypt node is actually pretty useless
686
+ // the media is returned even without this node
687
+ // keeping it here to maintain parity with WA Web
688
+ {
689
+ tag: 'encrypt',
690
+ attrs: {},
691
+ content: [
692
+ { tag: 'enc_p', attrs: {}, content: ciphertext },
693
+ { tag: 'enc_iv', attrs: {}, content: iv }
694
+ ]
695
+ },
696
+ {
697
+ tag: 'rmr',
698
+ attrs: {
699
+ jid: key.remoteJid,
700
+ 'from_me': (!!key.fromMe).toString(),
701
+ // @ts-ignore
702
+ participant: key.participant || undefined
703
+ }
704
+ }
705
+ ]
706
+ }
707
+ return req
708
+ }
709
+
710
+ const decodeMediaRetryNode = (node) => {
711
+ const rmrNode = WABinary_1.getBinaryNodeChild(node, 'rmr')
712
+ const event = {
713
+ key: {
714
+ id: node.attrs.id,
715
+ remoteJid: rmrNode.attrs.jid,
716
+ fromMe: rmrNode.attrs.from_me === 'true',
717
+ participant: rmrNode.attrs.participant
718
+ }
719
+ }
720
+ const errorNode = WABinary_1.getBinaryNodeChild(node, 'error')
721
+ if (errorNode) {
722
+ const errorCode = +errorNode.attrs.code
723
+ event.error = new boom_1.Boom(`Failed to re-upload media (${errorCode})`, { data: errorNode.attrs, statusCode: getStatusCodeForMediaRetry(errorCode) })
724
+ }
725
+ else {
726
+ const encryptedInfoNode = WABinary_1.getBinaryNodeChild(node, 'encrypt')
727
+ const ciphertext = WABinary_1.getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p')
728
+ const iv = WABinary_1.getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv')
729
+ if (ciphertext && iv) {
730
+ event.media = { ciphertext, iv }
731
+ }
732
+ else {
733
+ event.error = new boom_1.Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 })
734
+ }
735
+ }
736
+ return event
737
+ }
738
+
739
+ const decryptMediaRetryData = async ({ ciphertext, iv }, mediaKey, msgId) => {
740
+ const retryKey = await getMediaRetryKey(mediaKey)
741
+ const plaintext = crypto_1.aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId))
742
+ return WAProto_1.proto.MediaRetryNotification.decode(plaintext)
743
+ }
744
+
745
+ const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code]
746
+
747
+ const MEDIA_RETRY_STATUS_MAP = {
748
+ [WAProto_1.proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
749
+ [WAProto_1.proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
750
+ [WAProto_1.proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
751
+ [WAProto_1.proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418,
752
+ }
753
+
754
+ module.exports = {
755
+ hkdfInfoKey,
756
+ getMediaKeys,
757
+ extractVideoThumb,
758
+ extractImageThumb,
759
+ encodeBase64EncodedStringForUpload,
760
+ generateProfilePicture,
761
+ mediaMessageSHA256B64,
762
+ getAudioDuration,
763
+ getAudioWaveform,
764
+ toReadable,
765
+ toBuffer,
766
+ getStream,
767
+ generateThumbnail,
768
+ getHttpStream,
769
+ prepareStream,
770
+ encryptedStream,
771
+ getUrlFromDirectPath,
772
+ downloadContentFromMessage,
773
+ downloadEncryptedContent,
774
+ extensionForMediaMessage,
775
+ getWAUploadToServer,
776
+ getMediaRetryKey,
777
+ encryptMediaRetryRequest,
778
+ decodeMediaRetryNode,
779
+ decryptMediaRetryData,
780
+ getStatusCodeForMediaRetry,
781
+ MEDIA_RETRY_STATUS_MAP
782
+ }