[Polyfills] Rewrite
This commit is contained in:
parent
9ce5e8c443
commit
6cfa808f7f
6 changed files with 26 additions and 22 deletions
|
@ -4,13 +4,6 @@ global.oaVersion = '0.2';
|
|||
|
||||
log('Init', 'OpenAsar v' + oaVersion);
|
||||
|
||||
const NodeModule = require('module');
|
||||
const { join } = require('path');
|
||||
|
||||
NodeModule.globalPaths.push(join(__dirname, 'polyfills'));
|
||||
|
||||
log('Polyfills', 'Set up polyfills usage');
|
||||
|
||||
const appSettings = require('./appSettings');
|
||||
global.oaConfig = appSettings.getSettings().get('openasar', {});
|
||||
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
// Stub
|
||||
exports.lookup = (file) => 'text/plain';
|
|
@ -1,55 +0,0 @@
|
|||
const https = require('https');
|
||||
const querystring = require("querystring");
|
||||
|
||||
// Generic polyfill for "request" npm package, wrapper for https
|
||||
const nodeReq = ({ method, url, headers, qs, timeout, body, stream }) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fullUrl = `${url}${qs != null ? `?${querystring.stringify(qs)}` : ''}`; // With query string
|
||||
const req = https.request(fullUrl, {
|
||||
method,
|
||||
headers,
|
||||
timeout: timeout != null ? timeout : DEFAULT_REQUEST_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);
|
||||
});
|
||||
|
||||
if (body) req.write(body); // Write POST body if included
|
||||
|
||||
req.end();
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = (options, callback) => {
|
||||
if (typeof options === 'string') {
|
||||
options = {
|
||||
url: options
|
||||
};
|
||||
}
|
||||
|
||||
log('Polyfill > Request', options.method, options.url);
|
||||
|
||||
const listener = {};
|
||||
|
||||
nodeReq(options).then((res) => { // No error handling because yes
|
||||
if (callback) callback(undefined, res, res.body);
|
||||
listener['response'](res);
|
||||
});
|
||||
|
||||
return {
|
||||
on: (type, handler) => {
|
||||
listener[type] = handler;
|
||||
}
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue