@queenanya/baileys 7.5.3-0 → 8.0.0

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