gifted-baileys 1.5.4 → 1.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/WAProto/WAProto.proto +88 -969
- package/WAProto/index.d.ts +1256 -13195
- package/WAProto/index.js +74730 -125106
- package/package.json +9 -27
- package/src/Defaults/baileys-version.json +3 -0
- package/{lib → src}/Defaults/index.js +14 -7
- package/src/Defaults/index.ts +131 -0
- package/src/README.md +1 -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/{lib → src}/Socket/Client/index.js +3 -2
- 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 -28
- package/src/Socket/business.ts +281 -0
- package/{lib → src}/Socket/chats.js +176 -174
- package/src/Socket/chats.ts +1030 -0
- package/{lib → src}/Socket/groups.js +68 -80
- 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 +211 -378
- package/src/Socket/messages-recv.ts +985 -0
- package/{lib → src}/Socket/messages-send.js +177 -452
- package/src/Socket/messages-send.ts +871 -0
- package/{lib → src}/Socket/newsletter.js +98 -107
- package/src/Socket/newsletter.ts +282 -0
- package/{lib → src}/Socket/registration.js +48 -56
- package/src/Socket/registration.ts +250 -0
- package/{lib → src}/Socket/socket.js +77 -77
- 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 +32 -36
- 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/src/Types/Label.ts +36 -0
- package/{lib/Types/LabelAssociation.d.ts → src/Types/LabelAssociation.ts} +22 -16
- package/src/Types/Message.ts +288 -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 -61
- package/src/Types/State.ts +29 -0
- package/src/Types/index.ts +59 -0
- package/{lib → src}/Utils/auth-utils.js +90 -73
- 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 +43 -15
- package/src/Utils/business.ts +275 -0
- package/{lib → src}/Utils/chat-utils.js +94 -87
- package/src/Utils/chat-utils.ts +860 -0
- package/{lib → src}/Utils/crypto.js +2 -4
- 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 +86 -67
- package/src/Utils/generics.ts +434 -0
- package/{lib → src}/Utils/history.js +39 -13
- 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 +255 -193
- package/src/Utils/messages-media.ts +847 -0
- package/{lib → src}/Utils/messages.js +118 -588
- package/src/Utils/messages.ts +956 -0
- package/src/Utils/noise-handler.ts +197 -0
- package/{lib → src}/Utils/process-message.js +30 -27
- package/src/Utils/process-message.ts +414 -0
- package/{lib → src}/Utils/signal.js +42 -25
- package/src/Utils/signal.ts +177 -0
- package/{lib → src}/Utils/use-multi-file-auth-state.js +28 -27
- package/src/Utils/use-multi-file-auth-state.ts +90 -0
- package/{lib → src}/Utils/validate-connection.js +9 -40
- package/src/Utils/validate-connection.ts +238 -0
- package/src/WABinary/constants.ts +42 -0
- package/src/WABinary/decode.ts +265 -0
- package/{lib → src}/WABinary/encode.js +10 -16
- package/src/WABinary/encode.ts +236 -0
- package/src/WABinary/generic-utils.ts +121 -0
- package/src/WABinary/index.ts +5 -0
- package/src/WABinary/jid-utils.ts +68 -0
- package/src/WABinary/types.ts +17 -0
- package/src/WAM/BinaryInfo.ts +12 -0
- package/src/WAM/constants.ts +15382 -0
- package/src/WAM/encode.ts +174 -0
- package/src/WAM/index.ts +3 -0
- package/{lib → src}/index.js +0 -1
- package/src/index.ts +13 -0
- package/README.md +0 -1645
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/index.d.ts +0 -284
- package/lib/Signal/libsignal.d.ts +0 -3
- package/lib/Signal/libsignal.js +0 -161
- package/lib/Socket/Client/abstract-socket-client.d.ts +0 -15
- package/lib/Socket/Client/index.d.ts +0 -2
- package/lib/Socket/Client/mobile-socket-client.d.ts +0 -12
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/types.d.ts +0 -17
- package/lib/Socket/Client/types.js +0 -13
- package/lib/Socket/Client/websocket.d.ts +0 -12
- package/lib/Socket/Client/websocket.js +0 -62
- package/lib/Socket/business.d.ts +0 -170
- package/lib/Socket/chats.d.ts +0 -81
- package/lib/Socket/groups.d.ts +0 -115
- package/lib/Socket/index.d.ts +0 -172
- package/lib/Socket/messages-recv.d.ts +0 -158
- package/lib/Socket/messages-send.d.ts +0 -155
- package/lib/Socket/newsletter.d.ts +0 -132
- package/lib/Socket/registration.d.ts +0 -264
- package/lib/Socket/socket.d.ts +0 -44
- package/lib/Socket/usync.d.ts +0 -37
- package/lib/Socket/usync.js +0 -70
- package/lib/Store/index.d.ts +0 -3
- package/lib/Store/make-cache-manager-store.d.ts +0 -14
- package/lib/Store/make-in-memory-store.d.ts +0 -118
- 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 -109
- package/lib/Types/Call.d.ts +0 -13
- package/lib/Types/Chat.d.ts +0 -107
- package/lib/Types/Events.d.ts +0 -172
- package/lib/Types/GroupMetadata.d.ts +0 -56
- package/lib/Types/Label.d.ts +0 -46
- package/lib/Types/Message.d.ts +0 -433
- package/lib/Types/Newsletter.d.ts +0 -92
- 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/USync.d.ts +0 -25
- package/lib/Types/USync.js +0 -2
- package/lib/Types/index.d.ts +0 -66
- 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 -70
- package/lib/Utils/crypto.d.ts +0 -40
- package/lib/Utils/decode-wa-message.d.ts +0 -36
- package/lib/Utils/decode-wa-message.js +0 -226
- package/lib/Utils/event-buffer.d.ts +0 -35
- package/lib/Utils/generics.d.ts +0 -88
- package/lib/Utils/history.d.ts +0 -19
- package/lib/Utils/index.d.ts +0 -17
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -2
- 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 -113
- package/lib/Utils/messages.d.ts +0 -77
- 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 -33
- 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 -6
- package/lib/WABinary/encode.d.ts +0 -2
- package/lib/WABinary/generic-utils.d.ts +0 -14
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -31
- package/lib/WABinary/types.d.ts +0 -18
- package/lib/WAM/BinaryInfo.d.ts +0 -8
- package/lib/WAM/constants.d.ts +0 -38
- package/lib/WAM/encode.d.ts +0 -2
- package/lib/WAM/index.d.ts +0 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -9
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -32
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -22
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -57
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -30
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -42
- package/lib/WAUSync/Protocols/index.d.ts +0 -4
- package/lib/WAUSync/Protocols/index.js +0 -20
- package/lib/WAUSync/USyncQuery.d.ts +0 -26
- package/lib/WAUSync/USyncQuery.js +0 -79
- package/lib/WAUSync/USyncUser.d.ts +0 -10
- package/lib/WAUSync/USyncUser.js +0 -22
- package/lib/WAUSync/index.d.ts +0 -3
- package/lib/WAUSync/index.js +0 -19
- /package/{lib → src}/Defaults/phonenumber-mcc.json +0 -0
- /package/{lib → src}/Socket/Client/abstract-socket-client.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/Label.js +0 -0
- /package/{lib → src}/Types/LabelAssociation.js +0 -0
- /package/{lib → src}/Types/Message.js +0 -0
- /package/{lib → src}/Types/Newsletter.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}/Types/index.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}/Utils/noise-handler.js +0 -0
- /package/{lib → src}/WABinary/constants.js +0 -0
- /package/{lib → src}/WABinary/decode.js +0 -0
- /package/{lib → src}/WABinary/generic-utils.js +0 -0
- /package/{lib → src}/WABinary/index.js +0 -0
- /package/{lib → src}/WABinary/jid-utils.js +0 -0
- /package/{lib → src}/WABinary/types.js +0 -0
- /package/{lib → src}/WAM/BinaryInfo.js +0 -0
- /package/{lib → src}/WAM/constants.js +0 -0
- /package/{lib → src}/WAM/encode.js +0 -0
- /package/{lib → src}/WAM/index.js +0 -0
- /package/{lib → src}/gifted +0 -0
|
@@ -32,6 +32,22 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
32
32
|
return result;
|
|
33
33
|
};
|
|
34
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); }
|
|
50
|
+
};
|
|
35
51
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
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;
|
|
37
53
|
exports.getMediaKeys = getMediaKeys;
|
|
@@ -53,18 +69,16 @@ const WABinary_1 = require("../WABinary");
|
|
|
53
69
|
const crypto_1 = require("./crypto");
|
|
54
70
|
const generics_1 = require("./generics");
|
|
55
71
|
const getTmpFilesDirectory = () => (0, os_1.tmpdir)();
|
|
56
|
-
const getImageProcessingLibrary =
|
|
57
|
-
const [_jimp, sharp] =
|
|
58
|
-
(
|
|
59
|
-
const jimp =
|
|
60
|
-
.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(() => { }));
|
|
61
76
|
return jimp;
|
|
62
|
-
})(),
|
|
63
|
-
(
|
|
64
|
-
const sharp =
|
|
65
|
-
.catch(() => { }));
|
|
77
|
+
}))(),
|
|
78
|
+
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
79
|
+
const sharp = yield (Promise.resolve().then(() => __importStar(require('sharp'))).catch(() => { }));
|
|
66
80
|
return sharp;
|
|
67
|
-
})()
|
|
81
|
+
}))()
|
|
68
82
|
]);
|
|
69
83
|
if (sharp) {
|
|
70
84
|
return { sharp };
|
|
@@ -74,7 +88,7 @@ const getImageProcessingLibrary = async () => {
|
|
|
74
88
|
return { jimp };
|
|
75
89
|
}
|
|
76
90
|
throw new boom_1.Boom('No image processing library available');
|
|
77
|
-
};
|
|
91
|
+
});
|
|
78
92
|
const hkdfInfoKey = (type) => {
|
|
79
93
|
const hkdfInfo = Defaults_1.MEDIA_HKDF_KEY_MAPPING[type];
|
|
80
94
|
return `WhatsApp ${hkdfInfo} Keys`;
|
|
@@ -97,27 +111,29 @@ function getMediaKeys(buffer, mediaType) {
|
|
|
97
111
|
};
|
|
98
112
|
}
|
|
99
113
|
/** Extracts video thumb using FFMPEG */
|
|
100
|
-
const extractVideoThumb =
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
+
});
|
|
109
125
|
});
|
|
110
126
|
});
|
|
111
|
-
const extractImageThumb =
|
|
127
|
+
const extractImageThumb = (bufferOrFilePath_1, ...args_1) => __awaiter(void 0, [bufferOrFilePath_1, ...args_1], void 0, function* (bufferOrFilePath, width = 32) {
|
|
112
128
|
var _a, _b;
|
|
113
129
|
if (bufferOrFilePath instanceof stream_1.Readable) {
|
|
114
|
-
bufferOrFilePath =
|
|
130
|
+
bufferOrFilePath = yield (0, exports.toBuffer)(bufferOrFilePath);
|
|
115
131
|
}
|
|
116
|
-
const lib =
|
|
132
|
+
const lib = yield getImageProcessingLibrary();
|
|
117
133
|
if ('sharp' in lib && typeof ((_a = lib.sharp) === null || _a === void 0 ? void 0 : _a.default) === 'function') {
|
|
118
134
|
const img = lib.sharp.default(bufferOrFilePath);
|
|
119
|
-
const dimensions =
|
|
120
|
-
const buffer =
|
|
135
|
+
const dimensions = yield img.metadata();
|
|
136
|
+
const buffer = yield img
|
|
121
137
|
.resize(width)
|
|
122
138
|
.jpeg({ quality: 50 })
|
|
123
139
|
.toBuffer();
|
|
@@ -131,12 +147,12 @@ const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
|
131
147
|
}
|
|
132
148
|
else if ('jimp' in lib && typeof ((_b = lib.jimp) === null || _b === void 0 ? void 0 : _b.read) === 'function') {
|
|
133
149
|
const { read, MIME_JPEG, RESIZE_BILINEAR, AUTO } = lib.jimp;
|
|
134
|
-
const jimp =
|
|
150
|
+
const jimp = yield read(bufferOrFilePath);
|
|
135
151
|
const dimensions = {
|
|
136
152
|
width: jimp.getWidth(),
|
|
137
153
|
height: jimp.getHeight()
|
|
138
154
|
};
|
|
139
|
-
const buffer =
|
|
155
|
+
const buffer = yield jimp
|
|
140
156
|
.quality(50)
|
|
141
157
|
.resize(width, AUTO, RESIZE_BILINEAR)
|
|
142
158
|
.getBufferAsync(MIME_JPEG);
|
|
@@ -148,33 +164,52 @@ const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
|
148
164
|
else {
|
|
149
165
|
throw new boom_1.Boom('No image processing library available');
|
|
150
166
|
}
|
|
151
|
-
};
|
|
167
|
+
});
|
|
152
168
|
exports.extractImageThumb = extractImageThumb;
|
|
153
169
|
const encodeBase64EncodedStringForUpload = (b64) => (encodeURIComponent(b64
|
|
154
170
|
.replace(/\+/g, '-')
|
|
155
171
|
.replace(/\//g, '_')
|
|
156
172
|
.replace(/\=+$/, '')));
|
|
157
173
|
exports.encodeBase64EncodedStringForUpload = encodeBase64EncodedStringForUpload;
|
|
158
|
-
const generateProfilePicture =
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
174
|
+
const generateProfilePicture = (mediaUpload) => __awaiter(void 0, void 0, void 0, function* () {
|
|
175
|
+
var _a, _b;
|
|
176
|
+
let bufferOrFilePath;
|
|
177
|
+
if (Buffer.isBuffer(mediaUpload)) {
|
|
178
|
+
bufferOrFilePath = mediaUpload;
|
|
179
|
+
}
|
|
180
|
+
else if ('url' in mediaUpload) {
|
|
181
|
+
bufferOrFilePath = mediaUpload.url.toString();
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
bufferOrFilePath = yield (0, exports.toBuffer)(mediaUpload.stream);
|
|
185
|
+
}
|
|
186
|
+
const lib = yield getImageProcessingLibrary();
|
|
164
187
|
let img;
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
188
|
+
if ('sharp' in lib && typeof ((_a = lib.sharp) === null || _a === void 0 ? void 0 : _a.default) === 'function') {
|
|
189
|
+
img = lib.sharp.default(bufferOrFilePath)
|
|
190
|
+
.resize(640, 640)
|
|
191
|
+
.jpeg({
|
|
192
|
+
quality: 50,
|
|
193
|
+
})
|
|
194
|
+
.toBuffer();
|
|
195
|
+
}
|
|
196
|
+
else if ('jimp' in lib && typeof ((_b = lib.jimp) === null || _b === void 0 ? void 0 : _b.read) === 'function') {
|
|
197
|
+
const { read, MIME_JPEG, RESIZE_BILINEAR } = lib.jimp;
|
|
198
|
+
const jimp = yield read(bufferOrFilePath);
|
|
199
|
+
const min = Math.min(jimp.getWidth(), jimp.getHeight());
|
|
200
|
+
const cropped = jimp.crop(0, 0, min, min);
|
|
201
|
+
img = cropped
|
|
202
|
+
.quality(50)
|
|
203
|
+
.resize(640, 640, RESIZE_BILINEAR)
|
|
204
|
+
.getBufferAsync(MIME_JPEG);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
throw new boom_1.Boom('No image processing library available');
|
|
208
|
+
}
|
|
174
209
|
return {
|
|
175
|
-
img:
|
|
210
|
+
img: yield img,
|
|
176
211
|
};
|
|
177
|
-
};
|
|
212
|
+
});
|
|
178
213
|
exports.generateProfilePicture = generateProfilePicture;
|
|
179
214
|
/** gets the SHA256 of the given media message */
|
|
180
215
|
const mediaMessageSHA256B64 = (message) => {
|
|
@@ -182,66 +217,70 @@ const mediaMessageSHA256B64 = (message) => {
|
|
|
182
217
|
return (media === null || media === void 0 ? void 0 : media.fileSha256) && Buffer.from(media.fileSha256).toString('base64');
|
|
183
218
|
};
|
|
184
219
|
exports.mediaMessageSHA256B64 = mediaMessageSHA256B64;
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
metadata = await musicMetadata.parseBuffer(buffer, undefined, { duration: true });
|
|
190
|
-
}
|
|
191
|
-
else if (typeof buffer === 'string') {
|
|
192
|
-
const rStream = (0, fs_1.createReadStream)(buffer);
|
|
193
|
-
try {
|
|
194
|
-
metadata = await musicMetadata.parseStream(rStream, undefined, { duration: true });
|
|
195
|
-
}
|
|
196
|
-
finally {
|
|
197
|
-
rStream.destroy();
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
metadata = await musicMetadata.parseStream(buffer, undefined, { duration: true });
|
|
202
|
-
}
|
|
203
|
-
return metadata.format.duration;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
|
|
207
|
-
*/
|
|
208
|
-
async function getAudioWaveform(buffer, logger) {
|
|
209
|
-
try {
|
|
210
|
-
const audioDecode = (buffer) => import('audio-decode').then(({ default: audioDecode }) => audioDecode(buffer));
|
|
211
|
-
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;
|
|
212
224
|
if (Buffer.isBuffer(buffer)) {
|
|
213
|
-
|
|
225
|
+
metadata = yield musicMetadata.parseBuffer(buffer, undefined, { duration: true });
|
|
214
226
|
}
|
|
215
227
|
else if (typeof buffer === 'string') {
|
|
216
228
|
const rStream = (0, fs_1.createReadStream)(buffer);
|
|
217
|
-
|
|
229
|
+
try {
|
|
230
|
+
metadata = yield musicMetadata.parseStream(rStream, undefined, { duration: true });
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
rStream.destroy();
|
|
234
|
+
}
|
|
218
235
|
}
|
|
219
236
|
else {
|
|
220
|
-
|
|
237
|
+
metadata = yield musicMetadata.parseStream(buffer, undefined, { duration: true });
|
|
221
238
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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);
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
audioData = yield (0, exports.toBuffer)(buffer);
|
|
232
259
|
}
|
|
233
|
-
|
|
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;
|
|
234
279
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const waveform = new Uint8Array(normalizedData.map((n) => Math.floor(100 * n)));
|
|
240
|
-
return waveform;
|
|
241
|
-
}
|
|
242
|
-
catch (e) {
|
|
243
|
-
logger === null || logger === void 0 ? void 0 : logger.debug('Failed to generate waveform: ' + e);
|
|
244
|
-
}
|
|
280
|
+
catch (e) {
|
|
281
|
+
logger === null || logger === void 0 ? void 0 : logger.debug('Failed to generate waveform: ' + e);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
245
284
|
}
|
|
246
285
|
const toReadable = (buffer) => {
|
|
247
286
|
const readable = new stream_1.Readable({ read: () => { } });
|
|
@@ -250,16 +289,30 @@ const toReadable = (buffer) => {
|
|
|
250
289
|
return readable;
|
|
251
290
|
};
|
|
252
291
|
exports.toReadable = toReadable;
|
|
253
|
-
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;
|
|
254
295
|
const chunks = [];
|
|
255
|
-
|
|
256
|
-
|
|
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; }
|
|
257
310
|
}
|
|
258
311
|
stream.destroy();
|
|
259
312
|
return Buffer.concat(chunks);
|
|
260
|
-
};
|
|
313
|
+
});
|
|
261
314
|
exports.toBuffer = toBuffer;
|
|
262
|
-
const getStream =
|
|
315
|
+
const getStream = (item, opts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
263
316
|
if (Buffer.isBuffer(item)) {
|
|
264
317
|
return { stream: (0, exports.toReadable)(item), type: 'buffer' };
|
|
265
318
|
}
|
|
@@ -267,56 +320,58 @@ const getStream = async (item, opts) => {
|
|
|
267
320
|
return { stream: item.stream, type: 'readable' };
|
|
268
321
|
}
|
|
269
322
|
if (item.url.toString().startsWith('http://') || item.url.toString().startsWith('https://')) {
|
|
270
|
-
return { stream:
|
|
323
|
+
return { stream: yield (0, exports.getHttpStream)(item.url, opts), type: 'remote' };
|
|
271
324
|
}
|
|
272
325
|
return { stream: (0, fs_1.createReadStream)(item.url), type: 'file' };
|
|
273
|
-
};
|
|
326
|
+
});
|
|
274
327
|
exports.getStream = getStream;
|
|
275
328
|
/** generates a thumbnail for a given media, if required */
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
else if (mediaType === 'video') {
|
|
291
|
-
const imgFilename = (0, path_1.join)(getTmpFilesDirectory(), (0, generics_1.generateMessageID)() + '.jpg');
|
|
292
|
-
try {
|
|
293
|
-
await extractVideoThumb(file, imgFilename, '00:00:00', { width: 32, height: 32 });
|
|
294
|
-
const buff = await fs_1.promises.readFile(imgFilename);
|
|
295
|
-
thumbnail = buff.toString('base64');
|
|
296
|
-
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
|
+
}
|
|
297
343
|
}
|
|
298
|
-
|
|
299
|
-
|
|
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
|
+
}
|
|
300
355
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
};
|
|
356
|
+
return {
|
|
357
|
+
thumbnail,
|
|
358
|
+
originalImageDimensions
|
|
359
|
+
};
|
|
360
|
+
});
|
|
306
361
|
}
|
|
307
|
-
const getHttpStream =
|
|
308
|
-
const { default: axios } =
|
|
309
|
-
const fetched =
|
|
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' }));
|
|
310
365
|
return fetched.data;
|
|
311
|
-
};
|
|
366
|
+
});
|
|
312
367
|
exports.getHttpStream = getHttpStream;
|
|
313
|
-
const prepareStream =
|
|
314
|
-
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);
|
|
315
370
|
logger === null || logger === void 0 ? void 0 : logger.debug('fetched media stream');
|
|
316
371
|
let bodyPath;
|
|
317
372
|
let didSaveToTmpPath = false;
|
|
318
373
|
try {
|
|
319
|
-
const buffer =
|
|
374
|
+
const buffer = yield (0, exports.toBuffer)(stream);
|
|
320
375
|
if (type === 'file') {
|
|
321
376
|
bodyPath = media.url;
|
|
322
377
|
}
|
|
@@ -344,7 +399,7 @@ const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequi
|
|
|
344
399
|
stream.destroy();
|
|
345
400
|
if (didSaveToTmpPath) {
|
|
346
401
|
try {
|
|
347
|
-
|
|
402
|
+
yield fs_1.promises.unlink(bodyPath);
|
|
348
403
|
}
|
|
349
404
|
catch (err) {
|
|
350
405
|
logger === null || logger === void 0 ? void 0 : logger.error({ err }, 'failed to save to tmp path');
|
|
@@ -352,10 +407,11 @@ const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequi
|
|
|
352
407
|
}
|
|
353
408
|
throw error;
|
|
354
409
|
}
|
|
355
|
-
};
|
|
410
|
+
});
|
|
356
411
|
exports.prepareStream = prepareStream;
|
|
357
|
-
const encryptedStream =
|
|
358
|
-
|
|
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);
|
|
359
415
|
logger === null || logger === void 0 ? void 0 : logger.debug('fetched media stream');
|
|
360
416
|
const mediaKey = Crypto.randomBytes(32);
|
|
361
417
|
const { cipherKey, iv, macKey } = getMediaKeys(mediaKey, mediaType);
|
|
@@ -377,22 +433,34 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
377
433
|
let sha256Plain = Crypto.createHash('sha256');
|
|
378
434
|
let sha256Enc = Crypto.createHash('sha256');
|
|
379
435
|
try {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
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
|
+
}
|
|
393
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);
|
|
394
462
|
}
|
|
395
|
-
|
|
463
|
+
finally { if (e_2) throw e_2.error; }
|
|
396
464
|
}
|
|
397
465
|
onChunk(aes.final());
|
|
398
466
|
const mac = hmac.digest().slice(0, 10);
|
|
@@ -426,7 +494,7 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
426
494
|
stream.destroy();
|
|
427
495
|
if (didSaveToTmpPath) {
|
|
428
496
|
try {
|
|
429
|
-
|
|
497
|
+
yield fs_1.promises.unlink(bodyPath);
|
|
430
498
|
}
|
|
431
499
|
catch (err) {
|
|
432
500
|
logger === null || logger === void 0 ? void 0 : logger.error({ err }, 'failed to save to tmp path');
|
|
@@ -439,7 +507,7 @@ const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfReq
|
|
|
439
507
|
hmac = hmac.update(buff);
|
|
440
508
|
encWriteStream.push(buff);
|
|
441
509
|
}
|
|
442
|
-
};
|
|
510
|
+
});
|
|
443
511
|
exports.encryptedStream = encryptedStream;
|
|
444
512
|
const DEF_HOST = 'mmg.whatsapp.net';
|
|
445
513
|
const AES_CHUNK_SIZE = 16;
|
|
@@ -458,7 +526,7 @@ exports.downloadContentFromMessage = downloadContentFromMessage;
|
|
|
458
526
|
* Decrypts and downloads an AES256-CBC encrypted file given the keys.
|
|
459
527
|
* Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
|
|
460
528
|
* */
|
|
461
|
-
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 } = {}) {
|
|
462
530
|
let bytesFetched = 0;
|
|
463
531
|
let startChunk = 0;
|
|
464
532
|
let firstBlockIsIV = false;
|
|
@@ -472,10 +540,7 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
472
540
|
}
|
|
473
541
|
}
|
|
474
542
|
const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
|
|
475
|
-
const headers = {
|
|
476
|
-
...(options === null || options === void 0 ? void 0 : options.headers) || {},
|
|
477
|
-
Origin: Defaults_1.DEFAULT_ORIGIN,
|
|
478
|
-
};
|
|
543
|
+
const headers = Object.assign(Object.assign({}, (options === null || options === void 0 ? void 0 : options.headers) || {}), { Origin: Defaults_1.DEFAULT_ORIGIN });
|
|
479
544
|
if (startChunk || endChunk) {
|
|
480
545
|
headers.Range = `bytes=${startChunk}-`;
|
|
481
546
|
if (endChunk) {
|
|
@@ -483,12 +548,7 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
483
548
|
}
|
|
484
549
|
}
|
|
485
550
|
// download the message
|
|
486
|
-
const fetched =
|
|
487
|
-
...options || {},
|
|
488
|
-
headers,
|
|
489
|
-
maxBodyLength: Infinity,
|
|
490
|
-
maxContentLength: Infinity,
|
|
491
|
-
});
|
|
551
|
+
const fetched = yield (0, exports.getHttpStream)(downloadUrl, Object.assign(Object.assign({}, options || {}), { headers, maxBodyLength: Infinity, maxContentLength: Infinity }));
|
|
492
552
|
let remainingBytes = Buffer.from([]);
|
|
493
553
|
let aes;
|
|
494
554
|
const pushBytes = (bytes, push) => {
|
|
@@ -540,7 +600,7 @@ const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startB
|
|
|
540
600
|
},
|
|
541
601
|
});
|
|
542
602
|
return fetched.pipe(output, { end: true });
|
|
543
|
-
};
|
|
603
|
+
});
|
|
544
604
|
exports.downloadEncryptedContent = downloadEncryptedContent;
|
|
545
605
|
function extensionForMediaMessage(message) {
|
|
546
606
|
const getExtension = (mimetype) => mimetype.split(';')[0].split('/')[1];
|
|
@@ -558,17 +618,31 @@ function extensionForMediaMessage(message) {
|
|
|
558
618
|
return extension;
|
|
559
619
|
}
|
|
560
620
|
const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
|
|
561
|
-
return
|
|
562
|
-
var
|
|
563
|
-
|
|
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')));
|
|
564
626
|
// send a query JSON to obtain the url & auth token to upload our media
|
|
565
|
-
let uploadInfo =
|
|
627
|
+
let uploadInfo = yield refreshMediaConn(false);
|
|
566
628
|
let urls;
|
|
567
629
|
const hosts = [...customUploadHosts, ...uploadInfo.hosts];
|
|
568
630
|
const chunks = [];
|
|
569
631
|
if (!Buffer.isBuffer(stream)) {
|
|
570
|
-
|
|
571
|
-
|
|
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; }
|
|
572
646
|
}
|
|
573
647
|
}
|
|
574
648
|
const reqBody = Buffer.isBuffer(stream) ? stream : Buffer.concat(chunks);
|
|
@@ -586,19 +660,7 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
|
|
|
586
660
|
if (maxContentLengthBytes && reqBody.length > maxContentLengthBytes) {
|
|
587
661
|
throw new boom_1.Boom(`Body too large for "${hostname}"`, { statusCode: 413 });
|
|
588
662
|
}
|
|
589
|
-
const body =
|
|
590
|
-
...options,
|
|
591
|
-
headers: {
|
|
592
|
-
...options.headers || {},
|
|
593
|
-
'Content-Type': 'application/octet-stream',
|
|
594
|
-
'Origin': Defaults_1.DEFAULT_ORIGIN
|
|
595
|
-
},
|
|
596
|
-
httpsAgent: fetchAgent,
|
|
597
|
-
timeout: timeoutMs,
|
|
598
|
-
responseType: 'json',
|
|
599
|
-
maxBodyLength: Infinity,
|
|
600
|
-
maxContentLength: Infinity,
|
|
601
|
-
});
|
|
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 }));
|
|
602
664
|
result = body.data;
|
|
603
665
|
if ((result === null || result === void 0 ? void 0 : result.url) || (result === null || result === void 0 ? void 0 : result.directPath)) {
|
|
604
666
|
urls = {
|
|
@@ -609,15 +671,15 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
|
|
|
609
671
|
break;
|
|
610
672
|
}
|
|
611
673
|
else {
|
|
612
|
-
uploadInfo =
|
|
674
|
+
uploadInfo = yield refreshMediaConn(true);
|
|
613
675
|
throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
|
|
614
676
|
}
|
|
615
677
|
}
|
|
616
678
|
catch (error) {
|
|
617
679
|
if (axios.isAxiosError(error)) {
|
|
618
|
-
result = (
|
|
680
|
+
result = (_f = error.response) === null || _f === void 0 ? void 0 : _f.data;
|
|
619
681
|
}
|
|
620
|
-
const isLast = hostname === ((
|
|
682
|
+
const isLast = hostname === ((_g = hosts[uploadInfo.hosts.length - 1]) === null || _g === void 0 ? void 0 : _g.hostname);
|
|
621
683
|
logger.warn({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`);
|
|
622
684
|
}
|
|
623
685
|
}
|
|
@@ -625,7 +687,7 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
|
|
|
625
687
|
throw new boom_1.Boom('Media upload failed on all hosts', { statusCode: 500 });
|
|
626
688
|
}
|
|
627
689
|
return urls;
|
|
628
|
-
};
|
|
690
|
+
});
|
|
629
691
|
};
|
|
630
692
|
exports.getWAUploadToServer = getWAUploadToServer;
|
|
631
693
|
const getMediaRetryKey = (mediaKey) => {
|