OpenAsar/poly/request.js

87 lines
2.1 KiB
JavaScript
Raw Normal View History

const https = require('https');
// Generic polyfill for "request" npm package, wrapper for https
const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => new Promise((resolve) => {
const fullUrl = `${url}${qs != null ? `?${(new URLSearchParams(qs)).toString()}` : ''}`; // With query string
2022-01-13 13:40:06 +00:00
let req;
try {
2022-01-15 11:53:28 +00:00
req = https.request(fullUrl, { method, headers, timeout }, async (res) => {
if (res.statusCode === 301 || res.statusCode === 302) 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);
}
2022-01-13 13:40:06 +00:00
req.on('error', resolve);
2022-01-13 13:40:06 +00:00
if (body) req.write(body); // Write POST body if included
req.end();
});
2022-01-15 11:53:28 +00:00
const request = (...args) => {
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];
}
if (typeof options === 'string') {
options = {
url: options
};
}
const listeners = {};
2022-01-15 11:53:28 +00:00
nodeReq(options).then(async (res) => {
2022-01-29 10:04:45 +00:00
if (!res.statusCode) {
listeners['error']?.(res);
2022-01-15 11:53:28 +00:00
callback?.(res, null, null);
return;
}
listeners['response']?.(res);
let data = [];
res.on('data', (chunk) => {
data.push(chunk);
listeners['data']?.(chunk);
});
await new Promise((resolve) => res.on('end', resolve)); // Wait to read full body
const buf = Buffer.concat(data);
callback?.(undefined, res, options.encoding !== null ? buf.toString() : buf);
});
const ret = {
on: (type, handler) => {
listeners[type] = handler;
return ret; // Return self
}
};
return ret;
};
2022-01-15 11:53:28 +00:00
for (const m of [ 'get', 'post', 'put', 'patch', 'delete', 'head', 'options' ]) {
2022-01-29 10:04:45 +00:00
request[m] = (url, callback) => request({ url, method: m }, callback);
2022-01-15 11:53:28 +00:00
}
request.del = request.delete; // Special case
2022-01-13 13:40:06 +00:00
module.exports = request;