From db2ecda0ecde47c038e2841e93ee90cf76614a13 Mon Sep 17 00:00:00 2001 From: Oj Date: Fri, 10 Dec 2021 20:01:41 +0000 Subject: [PATCH] [Updater > Request] Rewrite to not use request npm dependency, custom https built-in wrapper --- src/package.json | 1 - src/updater/request.js | 48 ++++++++++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/package.json b/src/package.json index d0e0585..e70289a 100644 --- a/src/package.json +++ b/src/package.json @@ -3,7 +3,6 @@ "description": "Open-source alternative of Discord desktop's app.asar", "main": "index.js", "dependencies": { - "request": "2.88.0", "yauzl": "^2.10.0" } } \ No newline at end of file diff --git a/src/updater/request.js b/src/updater/request.js index 265723e..31d3e96 100644 --- a/src/updater/request.js +++ b/src/updater/request.js @@ -9,7 +9,8 @@ var _electron = require("electron"); 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 }; } @@ -78,7 +79,7 @@ function handleHTTPResponse(resolve, reject, response, stream) { }); } -function nodeRequest({ +const nodeReq = ({ method, url, headers, @@ -86,20 +87,39 @@ function nodeRequest({ timeout, body, stream -}) { +}) => { return new Promise((resolve, reject) => { - const req = (0, _request.default)({ + const fullUrl = `${url}${qs != null ? `?${_querystring.default.stringify(qs)}` : ''}`; // With query string + const req = https.request(fullUrl, { method, - url, - qs, headers, - followAllRedirects: true, - encoding: null, - timeout: timeout != null ? timeout : DEFAULT_REQUEST_TIMEOUT, - body + 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); }); - req.on('response', response => handleHTTPResponse(resolve, reject, response, stream)); - req.on('error', err => reject(err)); + + 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); }); } @@ -155,14 +175,14 @@ async function electronRequest({ } async function requestWithMethod(method, options) { - log('Request', method, options); - if (typeof options === 'string') { options = { url: options }; } + log('Request', options.url); + options = { ...options, method };