2021-12-11 00:06:47 +00:00
|
|
|
const https = require('https');
|
|
|
|
const querystring = require("querystring");
|
|
|
|
|
|
|
|
// Generic polyfill for "request" npm package, wrapper for https
|
2022-01-13 13:40:06 +00:00
|
|
|
const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Promise((resolve, reject) => {
|
|
|
|
const fullUrl = `${url}${qs != null ? `?${querystring.stringify(qs)}` : ''}`; // With query string
|
|
|
|
|
|
|
|
let req;
|
|
|
|
try {
|
2022-01-15 11:53:28 +00:00
|
|
|
req = https.request(fullUrl, { method, headers, timeout }, async (res) => {
|
2022-01-13 13:40:06 +00:00
|
|
|
if (res.statusCode === 301 || res.statusCode === 302) { // Redirect, recall function
|
2022-01-15 11:53:28 +00:00
|
|
|
return resolve(await nodeReq({ url: res.headers.location, method, headers, timeout, body, stream }));
|
2022-01-13 13:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resolve(res);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return resolve(e);
|
|
|
|
}
|
2021-12-23 17:17:55 +00:00
|
|
|
|
2022-01-13 13:40:06 +00:00
|
|
|
req.on('error', resolve);
|
2021-12-11 00:06:47 +00:00
|
|
|
|
2022-01-13 13:40:06 +00:00
|
|
|
if (body) req.write(body); // Write POST body if included
|
|
|
|
|
|
|
|
req.end();
|
|
|
|
});
|
2021-12-11 00:06:47 +00:00
|
|
|
|
2022-01-15 11:53:28 +00:00
|
|
|
const request = (...args) => {
|
|
|
|
// We have to use ...args because we have to support all of these possible args:
|
2021-12-27 19:50:50 +00:00
|
|
|
// request(url, callback)
|
|
|
|
// request(options, callback)
|
|
|
|
// request(url, options, callback)
|
|
|
|
|
|
|
|
let options, callback;
|
|
|
|
switch (args.length) {
|
|
|
|
case 3: // request(url, options, callback)
|
|
|
|
options = {
|
|
|
|
url: args[0],
|
|
|
|
...args[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
callback = args[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // request(url, callback) / request(options, callback)
|
|
|
|
options = args[0];
|
|
|
|
callback = args[1];
|
|
|
|
}
|
|
|
|
|
2021-12-11 00:06:47 +00:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = {
|
|
|
|
url: options
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:31:38 +00:00
|
|
|
console.log('[OpenAsar Request Polyfill]', options.url);
|
2021-12-11 00:14:21 +00:00
|
|
|
|
2021-12-23 17:17:55 +00:00
|
|
|
const listeners = {};
|
2021-12-11 00:06:47 +00:00
|
|
|
|
2022-01-15 11:53:28 +00:00
|
|
|
nodeReq(options).then(async (res) => {
|
2021-12-24 21:40:40 +00:00
|
|
|
const isError = !res.statusCode;
|
2021-12-23 17:17:55 +00:00
|
|
|
|
|
|
|
if (isError) {
|
2022-01-13 13:40:06 +00:00
|
|
|
console.log('[OpenAsar Request Polyfill] Error:', res);
|
2021-12-24 13:02:28 +00:00
|
|
|
listeners['error']?.(res);
|
2022-01-15 11:53:28 +00:00
|
|
|
callback?.(res, null, null);
|
2021-12-23 17:17:55 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-24 13:02:28 +00:00
|
|
|
listeners['response']?.(res);
|
2021-12-18 22:48:34 +00:00
|
|
|
|
2021-12-18 22:21:19 +00:00
|
|
|
let body = '';
|
|
|
|
|
2021-12-24 13:02:28 +00:00
|
|
|
res.on('data', (chunk) => {
|
|
|
|
body += chunk;
|
|
|
|
listeners['data']?.(chunk);
|
|
|
|
});
|
2021-12-18 22:21:19 +00:00
|
|
|
|
|
|
|
await new Promise((resolve) => res.on('end', resolve)); // Wait to read full body
|
|
|
|
|
2021-12-24 13:02:28 +00:00
|
|
|
callback?.(undefined, res, body);
|
2021-12-11 00:06:47 +00:00
|
|
|
});
|
|
|
|
|
2021-12-24 13:02:28 +00:00
|
|
|
const ret = {
|
2021-12-11 00:06:47 +00:00
|
|
|
on: (type, handler) => {
|
2021-12-23 17:17:55 +00:00
|
|
|
listeners[type] = handler;
|
2021-12-24 13:02:28 +00:00
|
|
|
return ret; // Return self
|
2021-12-11 00:06:47 +00:00
|
|
|
}
|
2021-12-24 13:02:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2021-12-18 22:21:19 +00:00
|
|
|
};
|
|
|
|
|
2022-01-15 11:53:28 +00:00
|
|
|
for (const m of [ 'get', 'post', 'put', 'patch', 'delete', 'head', 'options' ]) {
|
|
|
|
request[m] = (url, callback) => request({ url, method: m.toUpperCase() }, callback);
|
|
|
|
}
|
|
|
|
request.del = request.delete; // Special case
|
2021-12-18 22:21:19 +00:00
|
|
|
|
2022-01-13 13:40:06 +00:00
|
|
|
module.exports = request;
|