gifted-baileys 1.5.0 → 1.5.5
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.
- package/LICENSE +1 -1
- package/README.md +1429 -684
- package/package.json +11 -26
- package/src/Defaults/baileys-version.json +3 -0
- package/{lib → src}/Defaults/index.js +16 -8
- package/src/Defaults/index.ts +131 -0
- package/src/Defaults/phonenumber-mcc.json +223 -0
- package/src/Signal/libsignal.js +180 -0
- package/src/Signal/libsignal.ts +141 -0
- package/src/Socket/Client/abstract-socket-client.ts +19 -0
- package/src/Socket/Client/index.ts +3 -0
- package/src/Socket/Client/mobile-socket-client.js +78 -0
- package/src/Socket/Client/mobile-socket-client.ts +66 -0
- package/src/Socket/Client/web-socket-client.js +75 -0
- package/src/Socket/Client/web-socket-client.ts +57 -0
- package/{lib → src}/Socket/business.js +33 -27
- package/src/Socket/business.ts +281 -0
- package/{lib → src}/Socket/chats.js +197 -178
- package/src/Socket/chats.ts +1030 -0
- package/{lib → src}/Socket/groups.js +69 -79
- package/src/Socket/groups.ts +356 -0
- package/{lib → src}/Socket/index.js +1 -4
- package/src/Socket/index.ts +13 -0
- package/{lib → src}/Socket/messages-recv.js +160 -108
- package/src/Socket/messages-recv.ts +985 -0
- package/{lib → src}/Socket/messages-send.js +183 -100
- package/src/Socket/messages-send.ts +871 -0
- package/src/Socket/newsletter.js +227 -0
- package/src/Socket/newsletter.ts +282 -0
- package/{lib → src}/Socket/registration.js +55 -63
- package/src/Socket/registration.ts +250 -0
- package/{lib → src}/Socket/socket.js +107 -66
- package/src/Socket/socket.ts +777 -0
- package/src/Store/index.ts +3 -0
- package/{lib → src}/Store/make-cache-manager-store.js +34 -25
- package/src/Store/make-cache-manager-store.ts +100 -0
- package/{lib → src}/Store/make-in-memory-store.js +51 -61
- package/src/Store/make-in-memory-store.ts +475 -0
- package/src/Store/make-ordered-dictionary.ts +86 -0
- package/{lib → src}/Store/object-repository.js +1 -1
- package/src/Store/object-repository.ts +32 -0
- package/src/Tests/test.app-state-sync.js +204 -0
- package/src/Tests/test.app-state-sync.ts +207 -0
- package/src/Tests/test.event-buffer.js +270 -0
- package/src/Tests/test.event-buffer.ts +319 -0
- package/src/Tests/test.key-store.js +76 -0
- package/src/Tests/test.key-store.ts +92 -0
- package/src/Tests/test.libsignal.js +141 -0
- package/src/Tests/test.libsignal.ts +186 -0
- package/src/Tests/test.media-download.js +93 -0
- package/src/Tests/test.media-download.ts +76 -0
- package/src/Tests/test.messages.js +33 -0
- package/src/Tests/test.messages.ts +37 -0
- package/src/Tests/utils.js +34 -0
- package/src/Tests/utils.ts +36 -0
- package/src/Types/Auth.ts +113 -0
- package/src/Types/Call.ts +15 -0
- package/src/Types/Chat.ts +106 -0
- package/{lib/Types/Contact.d.ts → src/Types/Contact.ts} +9 -8
- package/src/Types/Events.ts +93 -0
- package/src/Types/GroupMetadata.ts +53 -0
- package/{lib → src}/Types/Label.js +1 -1
- package/src/Types/Label.ts +36 -0
- package/{lib → src}/Types/LabelAssociation.js +1 -1
- package/{lib/Types/LabelAssociation.d.ts → src/Types/LabelAssociation.ts} +22 -16
- package/src/Types/Message.ts +288 -0
- package/src/Types/Newsletter.js +32 -0
- package/src/Types/Newsletter.ts +98 -0
- package/src/Types/Product.ts +85 -0
- package/src/Types/Signal.ts +68 -0
- package/{lib/Types/Socket.d.ts → src/Types/Socket.ts} +68 -56
- package/src/Types/State.ts +29 -0
- package/{lib → src}/Types/index.js +2 -1
- package/src/Types/index.ts +59 -0
- package/{lib → src}/Utils/auth-utils.js +95 -76
- package/src/Utils/auth-utils.ts +222 -0
- package/src/Utils/baileys-event-stream.js +92 -0
- package/src/Utils/baileys-event-stream.ts +66 -0
- package/{lib → src}/Utils/business.js +45 -17
- package/src/Utils/business.ts +275 -0
- package/{lib → src}/Utils/chat-utils.js +74 -46
- package/src/Utils/chat-utils.ts +860 -0
- package/{lib → src}/Utils/crypto.js +31 -21
- package/src/Utils/crypto.ts +131 -0
- package/src/Utils/decode-wa-message.js +211 -0
- package/src/Utils/decode-wa-message.ts +228 -0
- package/{lib → src}/Utils/event-buffer.js +13 -4
- package/src/Utils/event-buffer.ts +613 -0
- package/{lib → src}/Utils/generics.js +98 -45
- package/src/Utils/generics.ts +434 -0
- package/{lib → src}/Utils/history.js +39 -10
- package/src/Utils/history.ts +112 -0
- package/src/Utils/index.ts +17 -0
- package/{lib → src}/Utils/link-preview.js +54 -17
- package/src/Utils/link-preview.ts +122 -0
- package/src/Utils/logger.ts +3 -0
- package/src/Utils/lt-hash.ts +61 -0
- package/{lib → src}/Utils/make-mutex.js +13 -4
- package/src/Utils/make-mutex.ts +44 -0
- package/{lib → src}/Utils/messages-media.js +296 -192
- package/src/Utils/messages-media.ts +847 -0
- package/{lib → src}/Utils/messages.js +124 -113
- package/src/Utils/messages.ts +956 -0
- package/{lib → src}/Utils/noise-handler.js +16 -3
- package/src/Utils/noise-handler.ts +197 -0
- package/{lib → src}/Utils/process-message.js +33 -29
- package/src/Utils/process-message.ts +414 -0
- package/{lib → src}/Utils/signal.js +23 -14
- package/src/Utils/signal.ts +177 -0
- package/{lib → src}/Utils/use-multi-file-auth-state.js +28 -19
- package/src/Utils/use-multi-file-auth-state.ts +90 -0
- package/{lib → src}/Utils/validate-connection.js +25 -42
- package/src/Utils/validate-connection.ts +238 -0
- package/src/WABinary/constants.ts +42 -0
- package/{lib → src}/WABinary/decode.js +17 -7
- package/src/WABinary/decode.ts +265 -0
- package/{lib → src}/WABinary/encode.js +17 -7
- package/src/WABinary/encode.ts +236 -0
- package/{lib → src}/WABinary/generic-utils.js +2 -2
- package/src/WABinary/generic-utils.ts +121 -0
- package/src/WABinary/index.ts +5 -0
- package/{lib → src}/WABinary/jid-utils.js +4 -1
- package/src/WABinary/jid-utils.ts +68 -0
- package/src/WABinary/types.ts +17 -0
- package/src/WAM/BinaryInfo.js +13 -0
- package/src/WAM/BinaryInfo.ts +12 -0
- package/src/WAM/constants.js +15350 -0
- package/src/WAM/constants.ts +15382 -0
- package/src/WAM/encode.js +155 -0
- package/src/WAM/encode.ts +174 -0
- package/src/WAM/index.js +19 -0
- package/src/WAM/index.ts +3 -0
- package/src/gifted +1 -0
- package/{lib → src}/index.js +1 -0
- package/src/index.ts +13 -0
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/index.d.ts +0 -284
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Signal/libsignal.d.ts +0 -3
- package/lib/Signal/libsignal.js +0 -152
- package/lib/Socket/Client/abstract-socket-client.d.ts +0 -17
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.d.ts +0 -12
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/business.d.ts +0 -135
- package/lib/Socket/chats.d.ts +0 -79
- package/lib/Socket/groups.d.ts +0 -113
- package/lib/Socket/index.d.ts +0 -137
- package/lib/Socket/messages-recv.d.ts +0 -124
- package/lib/Socket/messages-send.d.ts +0 -119
- package/lib/Socket/registration.d.ts +0 -232
- package/lib/Socket/socket.d.ts +0 -42
- package/lib/Store/index.d.ts +0 -3
- package/lib/Store/make-cache-manager-store.d.ts +0 -13
- package/lib/Store/make-in-memory-store.d.ts +0 -117
- package/lib/Store/make-ordered-dictionary.d.ts +0 -13
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -108
- package/lib/Types/Call.d.ts +0 -13
- package/lib/Types/Chat.d.ts +0 -102
- package/lib/Types/Events.d.ts +0 -157
- package/lib/Types/GroupMetadata.d.ts +0 -52
- package/lib/Types/Label.d.ts +0 -35
- package/lib/Types/Message.d.ts +0 -261
- package/lib/Types/Product.d.ts +0 -78
- package/lib/Types/Signal.d.ts +0 -57
- package/lib/Types/State.d.ts +0 -27
- package/lib/Types/index.d.ts +0 -56
- package/lib/Utils/auth-utils.d.ts +0 -18
- package/lib/Utils/baileys-event-stream.d.ts +0 -16
- package/lib/Utils/baileys-event-stream.js +0 -63
- package/lib/Utils/business.d.ts +0 -22
- package/lib/Utils/chat-utils.d.ts +0 -71
- package/lib/Utils/crypto.d.ts +0 -41
- package/lib/Utils/decode-wa-message.d.ts +0 -19
- package/lib/Utils/decode-wa-message.js +0 -174
- package/lib/Utils/event-buffer.d.ts +0 -35
- package/lib/Utils/generics.d.ts +0 -94
- package/lib/Utils/history.d.ts +0 -15
- package/lib/Utils/index.d.ts +0 -17
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -4
- package/lib/Utils/lt-hash.d.ts +0 -12
- package/lib/Utils/make-mutex.d.ts +0 -7
- package/lib/Utils/messages-media.d.ts +0 -107
- package/lib/Utils/messages.d.ts +0 -76
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/process-message.d.ts +0 -41
- package/lib/Utils/signal.d.ts +0 -32
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -12
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -27
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -15
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -29
- package/lib/WABinary/types.d.ts +0 -18
- package/lib/index.d.ts +0 -10
- /package/{lib → src}/Socket/Client/abstract-socket-client.js +0 -0
- /package/{lib → src}/Socket/Client/index.js +0 -0
- /package/{lib → src}/Store/index.js +0 -0
- /package/{lib → src}/Store/make-ordered-dictionary.js +0 -0
- /package/{lib → src}/Types/Auth.js +0 -0
- /package/{lib → src}/Types/Call.js +0 -0
- /package/{lib → src}/Types/Chat.js +0 -0
- /package/{lib → src}/Types/Contact.js +0 -0
- /package/{lib → src}/Types/Events.js +0 -0
- /package/{lib → src}/Types/GroupMetadata.js +0 -0
- /package/{lib → src}/Types/Message.js +0 -0
- /package/{lib → src}/Types/Product.js +0 -0
- /package/{lib → src}/Types/Signal.js +0 -0
- /package/{lib → src}/Types/Socket.js +0 -0
- /package/{lib → src}/Types/State.js +0 -0
- /package/{lib → src}/Utils/index.js +0 -0
- /package/{lib → src}/Utils/logger.js +0 -0
- /package/{lib → src}/Utils/lt-hash.js +0 -0
- /package/{lib → src}/WABinary/constants.js +0 -0
- /package/{lib → src}/WABinary/index.js +0 -0
- /package/{lib → src}/WABinary/types.js +0 -0
|
@@ -15,15 +15,46 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
45
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
46
|
+
var m = o[Symbol.asyncIterator], i;
|
|
47
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
48
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
49
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
24
50
|
};
|
|
25
51
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.getStatusCodeForMediaRetry = exports.decryptMediaRetryData = exports.decodeMediaRetryNode = exports.encryptMediaRetryRequest = exports.getWAUploadToServer = exports.
|
|
52
|
+
exports.getStatusCodeForMediaRetry = exports.decryptMediaRetryData = exports.decodeMediaRetryNode = exports.encryptMediaRetryRequest = exports.getWAUploadToServer = exports.downloadEncryptedContent = exports.downloadContentFromMessage = exports.getUrlFromDirectPath = exports.encryptedStream = exports.prepareStream = exports.getHttpStream = exports.getStream = exports.toBuffer = exports.toReadable = exports.mediaMessageSHA256B64 = exports.generateProfilePicture = exports.encodeBase64EncodedStringForUpload = exports.extractImageThumb = exports.hkdfInfoKey = void 0;
|
|
53
|
+
exports.getMediaKeys = getMediaKeys;
|
|
54
|
+
exports.getAudioDuration = getAudioDuration;
|
|
55
|
+
exports.getAudioWaveform = getAudioWaveform;
|
|
56
|
+
exports.generateThumbnail = generateThumbnail;
|
|
57
|
+
exports.extensionForMediaMessage = extensionForMediaMessage;
|
|
27
58
|
const boom_1 = require("@hapi/boom");
|
|
28
59
|
const child_process_1 = require("child_process");
|
|
29
60
|
const Crypto = __importStar(require("crypto"));
|
|
@@ -38,18 +69,16 @@ const WABinary_1 = require("../WABinary");
|
|
|
38
69
|
const crypto_1 = require("./crypto");
|
|
39
70
|
const generics_1 = require("./generics");
|
|
40
71
|
const getTmpFilesDirectory = () => (0, os_1.tmpdir)();
|
|
41
|
-
const getImageProcessingLibrary =
|
|
42
|
-
const [_jimp, sharp] =
|
|
43
|
-
(
|
|
44
|
-
const jimp =
|
|
45
|
-
.catch(() => { }));
|
|
72
|
+
const getImageProcessingLibrary = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
73
|
+
const [_jimp, sharp] = yield Promise.all([
|
|
74
|
+
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
+
const jimp = yield (Promise.resolve().then(() => __importStar(require('jimp'))).catch(() => { }));
|
|
46
76
|
return jimp;
|
|
47
|
-
})(),
|
|
48
|
-
(
|
|
49
|
-
const sharp =
|
|
50
|
-
.catch(() => { }));
|
|
77
|
+
}))(),
|
|
78
|
+
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
79
|
+
const sharp = yield (Promise.resolve().then(() => __importStar(require('sharp'))).catch(() => { }));
|
|
51
80
|
return sharp;
|
|
52
|
-
})()
|
|
81
|
+
}))()
|
|
53
82
|
]);
|
|
54
83
|
if (sharp) {
|
|
55
84
|
return { sharp };
|
|
@@ -59,7 +88,7 @@ const getImageProcessingLibrary = async () => {
|
|
|
59
88
|
return { jimp };
|
|
60
89
|
}
|
|
61
90
|
throw new boom_1.Boom('No image processing library available');
|
|
62
|
-
};
|
|
91
|
+
});
|
|
63
92
|
const hkdfInfoKey = (type) => {
|
|
64
93
|
const hkdfInfo = Defaults_1.MEDIA_HKDF_KEY_MAPPING[type];
|
|
65
94
|
return `WhatsApp ${hkdfInfo} Keys`;
|
|
@@ -81,29 +110,30 @@ function getMediaKeys(buffer, mediaType) {
|
|
|
81
110
|
macKey: expandedMediaKey.slice(48, 80),
|
|
82
111
|
};
|
|
83
112
|
}
|
|
84
|
-
exports.getMediaKeys = getMediaKeys;
|
|
85
113
|
/** Extracts video thumb using FFMPEG */
|
|
86
|
-
const extractVideoThumb =
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
114
|
+
const extractVideoThumb = (path, destPath, time, size) => __awaiter(void 0, void 0, void 0, function* () {
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
const cmd = `ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`;
|
|
117
|
+
(0, child_process_1.exec)(cmd, (err) => {
|
|
118
|
+
if (err) {
|
|
119
|
+
reject(err);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
resolve();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
95
125
|
});
|
|
96
126
|
});
|
|
97
|
-
const extractImageThumb =
|
|
127
|
+
const extractImageThumb = (bufferOrFilePath_1, ...args_1) => __awaiter(void 0, [bufferOrFilePath_1, ...args_1], void 0, function* (bufferOrFilePath, width = 32) {
|
|
98
128
|
var _a, _b;
|
|
99
129
|
if (bufferOrFilePath instanceof stream_1.Readable) {
|
|
100
|
-
bufferOrFilePath =
|
|
130
|
+
bufferOrFilePath = yield (0, exports.toBuffer)(bufferOrFilePath);
|
|
101
131
|
}
|
|
102
|
-
const lib =
|
|
132
|
+
const lib = yield getImageProcessingLibrary();
|
|
103
133
|
if ('sharp' in lib && typeof ((_a = lib.sharp) === null || _a === void 0 ? void 0 : _a.default) === 'function') {
|
|
104
134
|
const img = lib.sharp.default(bufferOrFilePath);
|
|
105
|
-
const dimensions =
|
|
106
|
-
const buffer =
|
|
135
|
+
const dimensions = yield img.metadata();
|
|
136
|
+
const buffer = yield img
|
|
107
137
|
.resize(width)
|
|
108
138
|
.jpeg({ quality: 50 })
|
|
109
139
|
.toBuffer();
|
|
@@ -117,12 +147,12 @@ const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
|
117
147
|
}
|
|
118
148
|
else if ('jimp' in lib && typeof ((_b = lib.jimp) === null || _b === void 0 ? void 0 : _b.read) === 'function') {
|
|
119
149
|
const { read, MIME_JPEG, RESIZE_BILINEAR, AUTO } = lib.jimp;
|
|
120
|
-
const jimp =
|
|
150
|
+
const jimp = yield read(bufferOrFilePath);
|
|
121
151
|
const dimensions = {
|
|
122
152
|
width: jimp.getWidth(),
|
|
123
153
|
height: jimp.getHeight()
|
|
124
154
|
};
|
|
125
|
-
const buffer =
|
|
155
|
+
const buffer = yield jimp
|
|
126
156
|
.quality(50)
|
|
127
157
|
.resize(width, AUTO, RESIZE_BILINEAR)
|
|
128
158
|
.getBufferAsync(MIME_JPEG);
|
|
@@ -134,14 +164,14 @@ const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
|
134
164
|
else {
|
|
135
165
|
throw new boom_1.Boom('No image processing library available');
|
|
136
166
|
}
|
|
137
|
-
};
|
|
167
|
+
});
|
|
138
168
|
exports.extractImageThumb = extractImageThumb;
|
|
139
169
|
const encodeBase64EncodedStringForUpload = (b64) => (encodeURIComponent(b64
|
|
140
170
|
.replace(/\+/g, '-')
|
|
141
171
|
.replace(/\//g, '_')
|
|
142
172
|
.replace(/\=+$/, '')));
|
|
143
173
|
exports.encodeBase64EncodedStringForUpload = encodeBase64EncodedStringForUpload;
|
|
144
|
-
const generateProfilePicture =
|
|
174
|
+
const generateProfilePicture = (mediaUpload) => __awaiter(void 0, void 0, void 0, function* () {
|
|
145
175
|
var _a, _b;
|
|
146
176
|
let bufferOrFilePath;
|
|
147
177
|
if (Buffer.isBuffer(mediaUpload)) {
|
|
@@ -151,9 +181,9 @@ const generateProfilePicture = async (mediaUpload) => {
|
|
|
151
181
|
bufferOrFilePath = mediaUpload.url.toString();
|
|
152
182
|
}
|
|
153
183
|
else {
|
|
154
|
-
bufferOrFilePath =
|
|
184
|
+
bufferOrFilePath = yield (0, exports.toBuffer)(mediaUpload.stream);
|
|
155
185
|
}
|
|
156
|
-
const lib =
|
|
186
|
+
const lib = yield getImageProcessingLibrary();
|
|
157
187
|
let img;
|
|
158
188
|
if ('sharp' in lib && typeof ((_a = lib.sharp) === null || _a === void 0 ? void 0 : _a.default) === 'function') {
|
|
159
189
|
img = lib.sharp.default(bufferOrFilePath)
|
|
@@ -165,7 +195,7 @@ const generateProfilePicture = async (mediaUpload) => {
|
|
|
165
195
|
}
|
|
166
196
|
else if ('jimp' in lib && typeof ((_b = lib.jimp) === null || _b === void 0 ? void 0 : _b.read) === 'function') {
|
|
167
197
|
const { read, MIME_JPEG, RESIZE_BILINEAR } = lib.jimp;
|
|
168
|
-
const jimp =
|
|
198
|
+
const jimp = yield read(bufferOrFilePath);
|
|
169
199
|
const min = Math.min(jimp.getWidth(), jimp.getHeight());
|
|
170
200
|
const cropped = jimp.crop(0, 0, min, min);
|
|
171
201
|
img = cropped
|
|
@@ -177,9 +207,9 @@ const generateProfilePicture = async (mediaUpload) => {
|
|
|
177
207
|
throw new boom_1.Boom('No image processing library available');
|
|
178
208
|
}
|
|
179
209
|
return {
|
|
180
|
-
img:
|
|
210
|
+
img: yield img,
|
|
181
211
|
};
|
|
182
|
-
};
|
|
212
|
+
});
|
|
183
213
|
exports.generateProfilePicture = generateProfilePicture;
|
|
184
214
|
/** gets the SHA256 of the given media message */
|
|
185
215
|
const mediaMessageSHA256B64 = (message) => {
|
|
@@ -187,69 +217,71 @@ const mediaMessageSHA256B64 = (message) => {
|
|
|
187
217
|
return (media === null || media === void 0 ? void 0 : media.fileSha256) && Buffer.from(media.fileSha256).toString('base64');
|
|
188
218
|
};
|
|
189
219
|
exports.mediaMessageSHA256B64 = mediaMessageSHA256B64;
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
metadata = await musicMetadata.parseBuffer(buffer, undefined, { duration: true });
|
|
195
|
-
}
|
|
196
|
-
else if (typeof buffer === 'string') {
|
|
197
|
-
const rStream = (0, fs_1.createReadStream)(buffer);
|
|
198
|
-
try {
|
|
199
|
-
metadata = await musicMetadata.parseStream(rStream, undefined, { duration: true });
|
|
200
|
-
}
|
|
201
|
-
finally {
|
|
202
|
-
rStream.destroy();
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
metadata = await musicMetadata.parseStream(buffer, undefined, { duration: true });
|
|
207
|
-
}
|
|
208
|
-
return metadata.format.duration;
|
|
209
|
-
}
|
|
210
|
-
exports.getAudioDuration = getAudioDuration;
|
|
211
|
-
/**
|
|
212
|
-
referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
|
|
213
|
-
*/
|
|
214
|
-
async function getAudioWaveform(buffer, logger) {
|
|
215
|
-
try {
|
|
216
|
-
const audioDecode = (buffer) => import('audio-decode').then(({ default: audioDecode }) => audioDecode(buffer));
|
|
217
|
-
let audioData;
|
|
220
|
+
function getAudioDuration(buffer) {
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
const musicMetadata = yield Promise.resolve().then(() => __importStar(require('music-metadata')));
|
|
223
|
+
let metadata;
|
|
218
224
|
if (Buffer.isBuffer(buffer)) {
|
|
219
|
-
|
|
225
|
+
metadata = yield musicMetadata.parseBuffer(buffer, undefined, { duration: true });
|
|
220
226
|
}
|
|
221
227
|
else if (typeof buffer === 'string') {
|
|
222
228
|
const rStream = (0, fs_1.createReadStream)(buffer);
|
|
223
|
-
|
|
229
|
+
try {
|
|
230
|
+
metadata = yield musicMetadata.parseStream(rStream, undefined, { duration: true });
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
rStream.destroy();
|
|
234
|
+
}
|
|
224
235
|
}
|
|
225
236
|
else {
|
|
226
|
-
|
|
237
|
+
metadata = yield musicMetadata.parseStream(buffer, undefined, { duration: true });
|
|
227
238
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
239
|
+
return metadata.format.duration;
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
|
|
244
|
+
*/
|
|
245
|
+
function getAudioWaveform(buffer, logger) {
|
|
246
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
247
|
+
try {
|
|
248
|
+
const audioDecode = (buffer) => Promise.resolve().then(() => __importStar(require('audio-decode'))).then(({ default: audioDecode }) => audioDecode(buffer));
|
|
249
|
+
let audioData;
|
|
250
|
+
if (Buffer.isBuffer(buffer)) {
|
|
251
|
+
audioData = buffer;
|
|
252
|
+
}
|
|
253
|
+
else if (typeof buffer === 'string') {
|
|
254
|
+
const rStream = (0, fs_1.createReadStream)(buffer);
|
|
255
|
+
audioData = yield (0, exports.toBuffer)(rStream);
|
|
238
256
|
}
|
|
239
|
-
|
|
257
|
+
else {
|
|
258
|
+
audioData = yield (0, exports.toBuffer)(buffer);
|
|
259
|
+
}
|
|
260
|
+
const audioBuffer = yield audioDecode(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;
|
|
240
279
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
const waveform = new Uint8Array(normalizedData.map((n) => Math.floor(100 * n)));
|
|
246
|
-
return waveform;
|
|
247
|
-
}
|
|
248
|
-
catch (e) {
|
|
249
|
-
logger === null || logger === void 0 ? void 0 : logger.debug('Failed to generate waveform: ' + e);
|
|
250
|
-
}
|
|
280
|
+
catch (e) {
|
|
281
|
+
logger === null || logger === void 0 ? void 0 : logger.debug('Failed to generate waveform: ' + e);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
251
284
|
}
|
|
252
|
-
exports.getAudioWaveform = getAudioWaveform;
|
|
253
285
|
const toReadable = (buffer) => {
|
|
254
286
|
const readable = new stream_1.Readable({ read: () => { } });
|
|
255
287
|
readable.push(buffer);
|
|
@@ -257,16 +289,30 @@ const toReadable = (buffer) => {
|
|
|
257
289
|
return readable;
|
|
258
290
|
};
|
|
259
291
|
exports.toReadable = toReadable;
|
|
260
|
-
const toBuffer =
|
|
292
|
+
const toBuffer = (stream) => __awaiter(void 0, void 0, void 0, function* () {
|
|
293
|
+
var _a, stream_2, stream_2_1;
|
|
294
|
+
var _b, e_1, _c, _d;
|
|
261
295
|
const chunks = [];
|
|
262
|
-
|
|
263
|
-
|
|
296
|
+
try {
|
|
297
|
+
for (_a = true, stream_2 = __asyncValues(stream); stream_2_1 = yield stream_2.next(), _b = stream_2_1.done, !_b; _a = true) {
|
|
298
|
+
_d = stream_2_1.value;
|
|
299
|
+
_a = false;
|
|
300
|
+
const chunk = _d;
|
|
301
|
+
chunks.push(chunk);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
305
|
+
finally {
|
|
306
|
+
try {
|
|
307
|
+
if (!_a && !_b && (_c = stream_2.return)) yield _c.call(stream_2);
|
|
308
|
+
}
|
|
309
|
+
finally { if (e_1) throw e_1.error; }
|
|
264
310
|
}
|
|
265
311
|
stream.destroy();
|
|
266
312
|
return Buffer.concat(chunks);
|
|
267
|
-
};
|
|
313
|
+
});
|
|
268
314
|
exports.toBuffer = toBuffer;
|
|
269
|
-
const getStream =
|
|
315
|
+
const getStream = (item, opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
270
316
|
if (Buffer.isBuffer(item)) {
|
|
271
317
|
return { stream: (0, exports.toReadable)(item), type: 'buffer' };
|
|
272
318
|
}
|
|
@@ -274,52 +320,98 @@ const getStream = async (item, opts) => {
|
|
|
274
320
|
return { stream: item.stream, type: 'readable' };
|
|
275
321
|
}
|
|
276
322
|
if (item.url.toString().startsWith('http://') || item.url.toString().startsWith('https://')) {
|
|
277
|
-
return { stream:
|
|
323
|
+
return { stream: yield (0, exports.getHttpStream)(item.url, opts), type: 'remote' };
|
|
278
324
|
}
|
|
279
325
|
return { stream: (0, fs_1.createReadStream)(item.url), type: 'file' };
|
|
280
|
-
};
|
|
326
|
+
});
|
|
281
327
|
exports.getStream = getStream;
|
|
282
328
|
/** generates a thumbnail for a given media, if required */
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
else if (mediaType === 'video') {
|
|
298
|
-
const imgFilename = (0, path_1.join)(getTmpFilesDirectory(), (0, generics_1.generateMessageID)() + '.jpg');
|
|
299
|
-
try {
|
|
300
|
-
await extractVideoThumb(file, imgFilename, '00:00:00', { width: 32, height: 32 });
|
|
301
|
-
const buff = await fs_1.promises.readFile(imgFilename);
|
|
302
|
-
thumbnail = buff.toString('base64');
|
|
303
|
-
await fs_1.promises.unlink(imgFilename);
|
|
329
|
+
function generateThumbnail(file, mediaType, options) {
|
|
330
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
331
|
+
var _a;
|
|
332
|
+
let thumbnail;
|
|
333
|
+
let originalImageDimensions;
|
|
334
|
+
if (mediaType === 'image') {
|
|
335
|
+
const { buffer, original } = yield (0, exports.extractImageThumb)(file);
|
|
336
|
+
thumbnail = buffer.toString('base64');
|
|
337
|
+
if (original.width && original.height) {
|
|
338
|
+
originalImageDimensions = {
|
|
339
|
+
width: original.width,
|
|
340
|
+
height: original.height,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
304
343
|
}
|
|
305
|
-
|
|
306
|
-
|
|
344
|
+
else if (mediaType === 'video') {
|
|
345
|
+
const imgFilename = (0, path_1.join)(getTmpFilesDirectory(), (0, generics_1.generateMessageID)() + '.jpg');
|
|
346
|
+
try {
|
|
347
|
+
yield extractVideoThumb(file, imgFilename, '00:00:00', { width: 32, height: 32 });
|
|
348
|
+
const buff = yield fs_1.promises.readFile(imgFilename);
|
|
349
|
+
thumbnail = buff.toString('base64');
|
|
350
|
+
yield fs_1.promises.unlink(imgFilename);
|
|
351
|
+
}
|
|
352
|
+
catch (err) {
|
|
353
|
+
(_a = options.logger) === null || _a === void 0 ? void 0 : _a.debug('could not generate video thumb: ' + err);
|
|
354
|
+
}
|
|
307
355
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
};
|
|
356
|
+
return {
|
|
357
|
+
thumbnail,
|
|
358
|
+
originalImageDimensions
|
|
359
|
+
};
|
|
360
|
+
});
|
|
313
361
|
}
|
|
314
|
-
|
|
315
|
-
const
|
|
316
|
-
const
|
|
317
|
-
const fetched = await axios.get(url.toString(), { ...options, responseType: 'stream' });
|
|
362
|
+
const getHttpStream = (url_1, ...args_1) => __awaiter(void 0, [url_1, ...args_1], void 0, function* (url, options = {}) {
|
|
363
|
+
const { default: axios } = yield Promise.resolve().then(() => __importStar(require('axios')));
|
|
364
|
+
const fetched = yield axios.get(url.toString(), Object.assign(Object.assign({}, options), { responseType: 'stream' }));
|
|
318
365
|
return fetched.data;
|
|
319
|
-
};
|
|
366
|
+
});
|
|
320
367
|
exports.getHttpStream = getHttpStream;
|
|
321
|
-
const
|
|
322
|
-
const { stream, type } =
|
|
368
|
+
const prepareStream = (media_1, mediaType_1, ...args_1) => __awaiter(void 0, [media_1, mediaType_1, ...args_1], void 0, function* (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) {
|
|
369
|
+
const { stream, type } = yield (0, exports.getStream)(media, opts);
|
|
370
|
+
logger === null || logger === void 0 ? void 0 : logger.debug('fetched media stream');
|
|
371
|
+
let bodyPath;
|
|
372
|
+
let didSaveToTmpPath = false;
|
|
373
|
+
try {
|
|
374
|
+
const buffer = yield (0, exports.toBuffer)(stream);
|
|
375
|
+
if (type === 'file') {
|
|
376
|
+
bodyPath = media.url;
|
|
377
|
+
}
|
|
378
|
+
else if (saveOriginalFileIfRequired) {
|
|
379
|
+
bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.generateMessageID)());
|
|
380
|
+
(0, fs_1.writeFileSync)(bodyPath, buffer);
|
|
381
|
+
didSaveToTmpPath = true;
|
|
382
|
+
}
|
|
383
|
+
const fileLength = buffer.length;
|
|
384
|
+
const fileSha256 = Crypto.createHash('sha256').update(buffer).digest();
|
|
385
|
+
stream === null || stream === void 0 ? void 0 : stream.destroy();
|
|
386
|
+
logger === null || logger === void 0 ? void 0 : logger.debug('prepare stream data successfully');
|
|
387
|
+
return {
|
|
388
|
+
mediaKey: undefined,
|
|
389
|
+
encWriteStream: buffer,
|
|
390
|
+
fileLength,
|
|
391
|
+
fileSha256,
|
|
392
|
+
fileEncSha256: undefined,
|
|
393
|
+
bodyPath,
|
|
394
|
+
didSaveToTmpPath
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
catch (error) {
|
|
398
|
+
// destroy all streams with error
|
|
399
|
+
stream.destroy();
|
|
400
|
+
if (didSaveToTmpPath) {
|
|
401
|
+
try {
|
|
402
|
+
yield fs_1.promises.unlink(bodyPath);
|
|
403
|
+
}
|
|
404
|
+
catch (err) {
|
|
405
|
+
logger === null || logger === void 0 ? void 0 : logger.error({ err }, 'failed to save to tmp path');
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
throw error;
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
exports.prepareStream = prepareStream;
|
|
412
|
+
const encryptedStream = (media_1, mediaType_1, ...args_1) => __awaiter(void 0, [media_1, mediaType_1, ...args_1], void 0, function* (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) {
|
|
413
|
+
var _a, e_2, _b, _c;
|
|
414
|
+
const { stream, type } = yield (0, exports.getStream)(media, opts);
|
|
323
415
|
logger === null || logger === void 0 ? void 0 : logger.debug('fetched media stream');
|
|
324
416
|
const mediaKey = Crypto.randomBytes(32);
|
|
325
417
|
const { cipherKey, iv, macKey } = getMediaKeys(mediaKey, mediaType);
|
|
@@ -341,22 +433,34 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
341
433
|
let sha256Plain = Crypto.createHash('sha256');
|
|
342
434
|
let sha256Enc = Crypto.createHash('sha256');
|
|
343
435
|
try {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
436
|
+
try {
|
|
437
|
+
for (var _d = true, stream_3 = __asyncValues(stream), stream_3_1; stream_3_1 = yield stream_3.next(), _a = stream_3_1.done, !_a; _d = true) {
|
|
438
|
+
_c = stream_3_1.value;
|
|
439
|
+
_d = false;
|
|
440
|
+
const data = _c;
|
|
441
|
+
fileLength += data.length;
|
|
442
|
+
if (type === 'remote'
|
|
443
|
+
&& (opts === null || opts === void 0 ? void 0 : opts.maxContentLength)
|
|
444
|
+
&& fileLength + data.length > opts.maxContentLength) {
|
|
445
|
+
throw new boom_1.Boom(`content length exceeded when encrypting "${type}"`, {
|
|
446
|
+
data: { media, type }
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
sha256Plain = sha256Plain.update(data);
|
|
450
|
+
if (writeStream) {
|
|
451
|
+
if (!writeStream.write(data)) {
|
|
452
|
+
yield (0, events_1.once)(writeStream, 'drain');
|
|
453
|
+
}
|
|
357
454
|
}
|
|
455
|
+
onChunk(aes.update(data));
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
459
|
+
finally {
|
|
460
|
+
try {
|
|
461
|
+
if (!_d && !_a && (_b = stream_3.return)) yield _b.call(stream_3);
|
|
358
462
|
}
|
|
359
|
-
|
|
463
|
+
finally { if (e_2) throw e_2.error; }
|
|
360
464
|
}
|
|
361
465
|
onChunk(aes.final());
|
|
362
466
|
const mac = hmac.digest().slice(0, 10);
|
|
@@ -390,7 +494,7 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
390
494
|
stream.destroy();
|
|
391
495
|
if (didSaveToTmpPath) {
|
|
392
496
|
try {
|
|
393
|
-
|
|
497
|
+
yield fs_1.promises.unlink(bodyPath);
|
|
394
498
|
}
|
|
395
499
|
catch (err) {
|
|
396
500
|
logger === null || logger === void 0 ? void 0 : logger.error({ err }, 'failed to save to tmp path');
|
|
@@ -403,7 +507,7 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
403
507
|
hmac = hmac.update(buff);
|
|
404
508
|
encWriteStream.push(buff);
|
|
405
509
|
}
|
|
406
|
-
};
|
|
510
|
+
});
|
|
407
511
|
exports.encryptedStream = encryptedStream;
|
|
408
512
|
const DEF_HOST = 'mmg.whatsapp.net';
|
|
409
513
|
const AES_CHUNK_SIZE = 16;
|
|
@@ -422,7 +526,7 @@ exports.downloadContentFromMessage = downloadContentFromMessage;
|
|
|
422
526
|
* Decrypts and downloads an AES256-CBC encrypted file given the keys.
|
|
423
527
|
* Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
|
|
424
528
|
* */
|
|
425
|
-
const downloadEncryptedContent =
|
|
529
|
+
const downloadEncryptedContent = (downloadUrl_1, _a, ...args_1) => __awaiter(void 0, [downloadUrl_1, _a, ...args_1], void 0, function* (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) {
|
|
426
530
|
let bytesFetched = 0;
|
|
427
531
|
let startChunk = 0;
|
|
428
532
|
let firstBlockIsIV = false;
|
|
@@ -436,10 +540,7 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
436
540
|
}
|
|
437
541
|
}
|
|
438
542
|
const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
|
|
439
|
-
const headers = {
|
|
440
|
-
...(options === null || options === void 0 ? void 0 : options.headers) || {},
|
|
441
|
-
Origin: Defaults_1.DEFAULT_ORIGIN,
|
|
442
|
-
};
|
|
543
|
+
const headers = Object.assign(Object.assign({}, (options === null || options === void 0 ? void 0 : options.headers) || {}), { Origin: Defaults_1.DEFAULT_ORIGIN });
|
|
443
544
|
if (startChunk || endChunk) {
|
|
444
545
|
headers.Range = `bytes=${startChunk}-`;
|
|
445
546
|
if (endChunk) {
|
|
@@ -447,12 +548,7 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
447
548
|
}
|
|
448
549
|
}
|
|
449
550
|
// download the message
|
|
450
|
-
const fetched =
|
|
451
|
-
...options || {},
|
|
452
|
-
headers,
|
|
453
|
-
maxBodyLength: Infinity,
|
|
454
|
-
maxContentLength: Infinity,
|
|
455
|
-
});
|
|
551
|
+
const fetched = yield (0, exports.getHttpStream)(downloadUrl, Object.assign(Object.assign({}, options || {}), { headers, maxBodyLength: Infinity, maxContentLength: Infinity }));
|
|
456
552
|
let remainingBytes = Buffer.from([]);
|
|
457
553
|
let aes;
|
|
458
554
|
const pushBytes = (bytes, push) => {
|
|
@@ -504,7 +600,7 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
504
600
|
},
|
|
505
601
|
});
|
|
506
602
|
return fetched.pipe(output, { end: true });
|
|
507
|
-
};
|
|
603
|
+
});
|
|
508
604
|
exports.downloadEncryptedContent = downloadEncryptedContent;
|
|
509
605
|
function extensionForMediaMessage(message) {
|
|
510
606
|
const getExtension = (mimetype) => mimetype.split(';')[0].split('/')[1];
|
|
@@ -521,61 +617,69 @@ function extensionForMediaMessage(message) {
|
|
|
521
617
|
}
|
|
522
618
|
return extension;
|
|
523
619
|
}
|
|
524
|
-
exports.extensionForMediaMessage = extensionForMediaMessage;
|
|
525
620
|
const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
|
|
526
|
-
return
|
|
527
|
-
var
|
|
528
|
-
|
|
621
|
+
return (stream_4, _a) => __awaiter(void 0, [stream_4, _a], void 0, function* (stream, { mediaType, fileEncSha256B64, newsletter, timeoutMs }) {
|
|
622
|
+
var _b, stream_5, stream_5_1;
|
|
623
|
+
var _c, e_3, _d, _e;
|
|
624
|
+
var _f, _g;
|
|
625
|
+
const { default: axios } = yield Promise.resolve().then(() => __importStar(require('axios')));
|
|
529
626
|
// send a query JSON to obtain the url & auth token to upload our media
|
|
530
|
-
let uploadInfo =
|
|
627
|
+
let uploadInfo = yield refreshMediaConn(false);
|
|
531
628
|
let urls;
|
|
532
629
|
const hosts = [...customUploadHosts, ...uploadInfo.hosts];
|
|
533
630
|
const chunks = [];
|
|
534
|
-
|
|
535
|
-
|
|
631
|
+
if (!Buffer.isBuffer(stream)) {
|
|
632
|
+
try {
|
|
633
|
+
for (_b = true, stream_5 = __asyncValues(stream); stream_5_1 = yield stream_5.next(), _c = stream_5_1.done, !_c; _b = true) {
|
|
634
|
+
_e = stream_5_1.value;
|
|
635
|
+
_b = false;
|
|
636
|
+
const chunk = _e;
|
|
637
|
+
chunks.push(chunk);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
641
|
+
finally {
|
|
642
|
+
try {
|
|
643
|
+
if (!_b && !_c && (_d = stream_5.return)) yield _d.call(stream_5);
|
|
644
|
+
}
|
|
645
|
+
finally { if (e_3) throw e_3.error; }
|
|
646
|
+
}
|
|
536
647
|
}
|
|
537
|
-
const reqBody = Buffer.concat(chunks);
|
|
648
|
+
const reqBody = Buffer.isBuffer(stream) ? stream : Buffer.concat(chunks);
|
|
538
649
|
fileEncSha256B64 = (0, exports.encodeBase64EncodedStringForUpload)(fileEncSha256B64);
|
|
650
|
+
let media = Defaults_1.MEDIA_PATH_MAP[mediaType];
|
|
651
|
+
if (newsletter) {
|
|
652
|
+
media = media === null || media === void 0 ? void 0 : media.replace('/mms/', '/newsletter/newsletter-');
|
|
653
|
+
}
|
|
539
654
|
for (const { hostname, maxContentLengthBytes } of hosts) {
|
|
540
655
|
logger.debug(`uploading to "${hostname}"`);
|
|
541
656
|
const auth = encodeURIComponent(uploadInfo.auth); // the auth token
|
|
542
|
-
const url = `https://${hostname}${
|
|
657
|
+
const url = `https://${hostname}${media}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
|
|
543
658
|
let result;
|
|
544
659
|
try {
|
|
545
660
|
if (maxContentLengthBytes && reqBody.length > maxContentLengthBytes) {
|
|
546
661
|
throw new boom_1.Boom(`Body too large for "${hostname}"`, { statusCode: 413 });
|
|
547
662
|
}
|
|
548
|
-
const body =
|
|
549
|
-
...options,
|
|
550
|
-
headers: {
|
|
551
|
-
...options.headers || {},
|
|
552
|
-
'Content-Type': 'application/octet-stream',
|
|
553
|
-
'Origin': Defaults_1.DEFAULT_ORIGIN
|
|
554
|
-
},
|
|
555
|
-
httpsAgent: fetchAgent,
|
|
556
|
-
timeout: timeoutMs,
|
|
557
|
-
responseType: 'json',
|
|
558
|
-
maxBodyLength: Infinity,
|
|
559
|
-
maxContentLength: Infinity,
|
|
560
|
-
});
|
|
663
|
+
const body = yield axios.post(url, reqBody, Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, options.headers || {}), { 'Content-Type': 'application/octet-stream', 'Origin': Defaults_1.DEFAULT_ORIGIN }), httpsAgent: fetchAgent, timeout: timeoutMs, responseType: 'json', maxBodyLength: Infinity, maxContentLength: Infinity }));
|
|
561
664
|
result = body.data;
|
|
562
665
|
if ((result === null || result === void 0 ? void 0 : result.url) || (result === null || result === void 0 ? void 0 : result.directPath)) {
|
|
563
666
|
urls = {
|
|
564
667
|
mediaUrl: result.url,
|
|
565
|
-
directPath: result.direct_path
|
|
668
|
+
directPath: result.direct_path,
|
|
669
|
+
handle: result.handle
|
|
566
670
|
};
|
|
567
671
|
break;
|
|
568
672
|
}
|
|
569
673
|
else {
|
|
570
|
-
uploadInfo =
|
|
674
|
+
uploadInfo = yield refreshMediaConn(true);
|
|
571
675
|
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
|
|
572
676
|
}
|
|
573
677
|
}
|
|
574
678
|
catch (error) {
|
|
575
679
|
if (axios.isAxiosError(error)) {
|
|
576
|
-
result = (
|
|
680
|
+
result = (_f = error.response) === null || _f === void 0 ? void 0 : _f.data;
|
|
577
681
|
}
|
|
578
|
-
const isLast = hostname === ((
|
|
682
|
+
const isLast = hostname === ((_g = hosts[uploadInfo.hosts.length - 1]) === null || _g === void 0 ? void 0 : _g.hostname);
|
|
579
683
|
logger.warn({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`);
|
|
580
684
|
}
|
|
581
685
|
}
|
|
@@ -583,7 +687,7 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
|
|
|
583
687
|
throw new boom_1.Boom('Media upload failed on all hosts', { statusCode: 500 });
|
|
584
688
|
}
|
|
585
689
|
return urls;
|
|
586
|
-
};
|
|
690
|
+
});
|
|
587
691
|
};
|
|
588
692
|
exports.getWAUploadToServer = getWAUploadToServer;
|
|
589
693
|
const getMediaRetryKey = (mediaKey) => {
|