[Polyfills] Add request, rewrite request wrapper to be generic polyfill
This commit is contained in:
parent
6e42e15a0d
commit
ebf459d846
|
@ -4,6 +4,13 @@ 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', {});
|
||||
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
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
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
};
|
|
@ -9,8 +9,7 @@ var _electron = require("electron");
|
|||
|
||||
var _querystring = _interopRequireDefault(require("querystring"));
|
||||
|
||||
// var _request = _interopRequireDefault(require("request"));
|
||||
const https = require('https');
|
||||
var _request = _interopRequireDefault(require("request"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
|
@ -79,7 +78,7 @@ function handleHTTPResponse(resolve, reject, response, stream) {
|
|||
});
|
||||
}
|
||||
|
||||
const nodeReq = ({
|
||||
function nodeRequest({
|
||||
method,
|
||||
url,
|
||||
headers,
|
||||
|
@ -87,39 +86,20 @@ const nodeReq = ({
|
|||
timeout,
|
||||
body,
|
||||
stream
|
||||
}) => {
|
||||
}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fullUrl = `${url}${qs != null ? `?${_querystring.default.stringify(qs)}` : ''}`; // With query string
|
||||
const req = https.request(fullUrl, {
|
||||
const req = (0, _request.default)({
|
||||
method,
|
||||
url,
|
||||
qs,
|
||||
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);
|
||||
followAllRedirects: true,
|
||||
encoding: null,
|
||||
timeout: timeout != null ? timeout : DEFAULT_REQUEST_TIMEOUT,
|
||||
body
|
||||
});
|
||||
|
||||
if (body) req.write(body); // Write POST body if included
|
||||
|
||||
req.end();
|
||||
});
|
||||
};
|
||||
|
||||
function nodeRequest(opts) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const res = await nodeReq(opts);
|
||||
handleHTTPResponse(resolve, reject, res, opts.stream);
|
||||
req.on('response', response => handleHTTPResponse(resolve, reject, response, stream));
|
||||
req.on('error', err => reject(err));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -181,12 +161,12 @@ async function requestWithMethod(method, options) {
|
|||
};
|
||||
}
|
||||
|
||||
log('Request', options.url);
|
||||
|
||||
options = { ...options,
|
||||
method
|
||||
};
|
||||
|
||||
log('Request', method, options.url);
|
||||
|
||||
try {
|
||||
return await electronRequest(options);
|
||||
} catch (err) {
|
||||
|
|
Loading…
Reference in New Issue