-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpsGet.js
41 lines (40 loc) · 961 Bytes
/
httpsGet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const https = require('https');
const fs = require("fs");
/**
* @param {Object} options
* @param {string} options.imgSource
* @param {string} options.pathOpts
* @param {boolean} options.withoutPrefix
* @param {boolean} options.skipFs
* @returns {Promise<string>} base64 data
*/
module.exports = ({
imgSource,
pathOpts,
withoutPrefix,
skipFs }) => new Promise((resolve, reject) => {
const req = https.request({
method: "GET",
hostname: imgSource.hostname,
path: imgSource.path,
rejectUnauthorized: false,
headers: {
Connection: "keep-alive",
Accept: "*/*",
}
}, (res) => {
res.setEncoding("base64")
let response = '';
res.on("error", (e) => reject(e));
res.on("data", (d) => response += d)
if (!skipFs) {
res.pipe(
fs.createWriteStream(pathOpts, "base64")
);
}
res.on("end", () =>
resolve(`${withoutPrefix ? "" : "data:image/png;base64,"}${response}`)
)
})
req.end();
})