[Polyfills] Add request, rewrite request wrapper to be generic polyfill
This commit is contained in:
parent
6e42e15a0d
commit
ebf459d846
3 changed files with 74 additions and 34 deletions
|
@ -4,6 +4,13 @@ global.oaVersion = '0.2';
|
||||||
|
|
||||||
log('Init', 'OpenAsar v' + oaVersion);
|
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');
|
const appSettings = require('./appSettings');
|
||||||
global.oaConfig = appSettings.getSettings().get('openasar', {});
|
global.oaConfig = appSettings.getSettings().get('openasar', {});
|
||||||
|
|
||||||
|
|
53
src/polyfills/request.js
Normal file
53
src/polyfills/request.js
Normal file
|
@ -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 _querystring = _interopRequireDefault(require("querystring"));
|
||||||
|
|
||||||
// var _request = _interopRequireDefault(require("request"));
|
var _request = _interopRequireDefault(require("request"));
|
||||||
const https = require('https');
|
|
||||||
|
|
||||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
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,
|
method,
|
||||||
url,
|
url,
|
||||||
headers,
|
headers,
|
||||||
|
@ -87,39 +86,20 @@ const nodeReq = ({
|
||||||
timeout,
|
timeout,
|
||||||
body,
|
body,
|
||||||
stream
|
stream
|
||||||
}) => {
|
}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const fullUrl = `${url}${qs != null ? `?${_querystring.default.stringify(qs)}` : ''}`; // With query string
|
const req = (0, _request.default)({
|
||||||
const req = https.request(fullUrl, {
|
|
||||||
method,
|
method,
|
||||||
|
url,
|
||||||
|
qs,
|
||||||
headers,
|
headers,
|
||||||
timeout: timeout != null ? timeout : DEFAULT_REQUEST_TIMEOUT
|
followAllRedirects: true,
|
||||||
}, async (res) => {
|
encoding: null,
|
||||||
if (res.statusCode === 301 || res.statusCode === 302) { // Redirect, recall function
|
timeout: timeout != null ? timeout : DEFAULT_REQUEST_TIMEOUT,
|
||||||
return resolve(await nodeReq({
|
body
|
||||||
url: res.headers.location,
|
|
||||||
qs: null,
|
|
||||||
method,
|
|
||||||
headers,
|
|
||||||
timeout,
|
|
||||||
body,
|
|
||||||
stream
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(res);
|
|
||||||
});
|
});
|
||||||
|
req.on('response', response => handleHTTPResponse(resolve, reject, response, stream));
|
||||||
if (body) req.write(body); // Write POST body if included
|
req.on('error', err => reject(err));
|
||||||
|
|
||||||
req.end();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function nodeRequest(opts) {
|
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
const res = await nodeReq(opts);
|
|
||||||
handleHTTPResponse(resolve, reject, res, opts.stream);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,12 +161,12 @@ async function requestWithMethod(method, options) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
log('Request', options.url);
|
|
||||||
|
|
||||||
options = { ...options,
|
options = { ...options,
|
||||||
method
|
method
|
||||||
};
|
};
|
||||||
|
|
||||||
|
log('Request', method, options.url);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await electronRequest(options);
|
return await electronRequest(options);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue