From ebf459d84633f7dca5f6c62ac4ff876f76954e1e Mon Sep 17 00:00:00 2001 From: Oj Date: Sat, 11 Dec 2021 00:06:47 +0000 Subject: [PATCH] [Polyfills] Add request, rewrite request wrapper to be generic polyfill --- src/index.js | 7 ++++++ src/polyfills/request.js | 53 ++++++++++++++++++++++++++++++++++++++++ src/updater/request.js | 48 +++++++++++------------------------- 3 files changed, 74 insertions(+), 34 deletions(-) create mode 100644 src/polyfills/request.js diff --git a/src/index.js b/src/index.js index 8e1c7fe..80a5baf 100644 --- a/src/index.js +++ b/src/index.js @@ -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', {}); diff --git a/src/polyfills/request.js b/src/polyfills/request.js new file mode 100644 index 0000000..a22d319 --- /dev/null +++ b/src/polyfills/request.js @@ -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; + } + } +}; \ No newline at end of file diff --git a/src/updater/request.js b/src/updater/request.js index 31d3e96..eef9fd9 100644 --- a/src/updater/request.js +++ b/src/updater/request.js @@ -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) {