OpenAsar/polyfills/request.js

120 lines
3.2 KiB
JavaScript
Raw Normal View History

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 {
req = https.request(fullUrl, {
method,
headers,
timeout
}, async (res) => {
if (res.statusCode === 301 || res.statusCode === 302) { // Redirect, recall function
return resolve(await nodeReq({
url: res.headers.location,
qs: null,
method,
headers,
timeout,
body,
stream
}));
}
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();
});
const request = (...args) => { // Main function
// We have to use ...args because fun fact! request supports both:
// request(url, callback)
// request(options, callback)
// ^ These are fine as they have the same number of arguments, however it also supports:
// request(url, options, callback)
// ...I know, right
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
};
}
2021-12-11 00:45:56 +00:00
// log('Polyfill > Request', options.method, options.url);
console.log('[OpenAsar Request Polyfill]', options.url);
2021-12-11 00:14:21 +00:00
const listeners = {};
nodeReq(options).then(async (res) => { // No error handling because yes
const isError = !res.statusCode;
if (isError) {
2022-01-13 13:40:06 +00:00
console.log('[OpenAsar Request Polyfill] Error:', res);
listeners['error']?.(res);
callback?.(res, null, null); // Return null for others?
return;
}
listeners['response']?.(res);
let body = '';
res.on('data', (chunk) => {
body += chunk;
listeners['data']?.(chunk);
});
await new Promise((resolve) => res.on('end', resolve)); // Wait to read full body
callback?.(undefined, res, body);
});
const ret = {
on: (type, handler) => {
listeners[type] = handler;
return ret; // Return self
}
};
return ret;
};
// Method functions
request.get = (url, callback) => request({ url: url, method: 'GET' }, callback);
request.post = (url, callback) => request({ url: url, method: 'POST' }, callback);
request.put = (url, callback) => request({ url: url, method: 'PUT' }, callback);
request.patch = (url, callback) => request({ url: url, method: 'PATCH' }, callback);
request.delete = (url, callback) => request({ url: url, method: 'DELETE' }, callback);
request.del = request.delete; // Random shortened func because request
request.head = (url, callback) => request({ url: url, method: 'HEAD' }, callback);
request.options = (url, callback) => request({ url: url, method: 'OPTIONS' }, callback);
2022-01-13 13:40:06 +00:00
module.exports = request;