diff --git a/preload/src/api/https.js b/preload/src/api/https.js index de306a26..f86904f4 100644 --- a/preload/src/api/https.js +++ b/preload/src/api/https.js @@ -1,91 +1,59 @@ -import Logger from "common/logger"; +import * as https from "https"; -let request; +const methods = ["get", "put", "post", "delete"]; +const headersToClone = ["statusCode", "statusMessage", "url", "headers", "method", "aborted", "complete", "rawHeaders", "end"]; -const req = function (url, options, callback) { - if (!request) request = __non_webpack_require__("request"); +const request = function (url, options, callback) { + let responseObject = undefined; + let pipe = undefined; - return request(url, options, (error, res, body) => { - try { - Reflect.apply(callback, null, [error, res, body]); - } - catch (err) { - Logger.stacktrace("https", "Failed request", err); + const req = https.request(url, Object.assign({method: "GET"}, options), res => { + const chunks = []; + let error = null; + + responseObject = res; + + if (pipe) { + res.pipe(pipe); } + + res.addListener("error", err => {error = err;}); + + res.addListener("data", chunk => { + chunks.push(chunk); + }); + + res.addListener("end", () => { + const headers = Object.fromEntries(headersToClone.map(h => [h, res[h]])); + + callback(error, headers, Buffer.concat(chunks)); + req.end(); + }); }); + + req.end(); + + return { + end() {req.end();}, + pipe(fsStream) { + if (!responseObject) { + pipe = fsStream; + } else { + responseObject.pipe(fsStream); + } + } + }; }; -export const get = function (url, options, callback) { - if (!request) request = __non_webpack_require__("request"); - - return request.get(url, options, (error, res, body) => { - try { - Reflect.apply(callback, null, [error, res, body]); - } - catch (err) { - Logger.stacktrace("https", "Failed get request", err); - } - }); -}; +export default Object.assign({request}, + Object.fromEntries(methods.map(method => [ + method, + function () { + arguments[1] ??= {}; -export const put = function (url, options, callback) { - if (!request) request = __non_webpack_require__("request"); + arguments[1].method ??= method.toUpperCase(); - return request.put(url, options, (error, res, body) => { - try { - Reflect.apply(callback, null, [error, res, body]); + return Reflect.apply(request, this, arguments); } - catch (err) { - Logger.stacktrace("https", "Failed put request", err); - } - }); -}; - -export const post = function (url, options, callback) { - if (!request) request = __non_webpack_require__("request"); - - return request.post(url, options, (error, res, body) => { - try { - Reflect.apply(callback, null, [error, res, body]); - } - catch (err) { - Logger.stacktrace("https", "Failed post request", err); - } - }); -}; - -const del = function (url, options, callback) { - if (!request) request = __non_webpack_require__("request"); - - return request.delete(url, options, (error, res, body) => { - try { - Reflect.apply(callback, null, [error, res, body]); - } - catch (err) { - Logger.stacktrace("https", "Failed delete request", err); - } - }); -}; - -const head = function (url, options, callback) { - if (!request) request = __non_webpack_require__("request"); - - return request.head(url, options, (error, res, body) => { - try { - Reflect.apply(callback, null, [error, res, body]); - } - catch (err) { - Logger.stacktrace("https", "Failed head request", err); - } - }); -}; - -export default req; - -Object.assign(req, { - get, - put, - post, - head, - delete: del // eslint-disable-line quote-props -}); \ No newline at end of file + ])) +); diff --git a/preload/src/api/index.js b/preload/src/api/index.js index a397ec7b..5089d87e 100644 --- a/preload/src/api/index.js +++ b/preload/src/api/index.js @@ -33,7 +33,7 @@ Module._load = (load => (req, parent, isMain) => { // console.log(require("request")); export * as filesystem from "./filesystem"; -export * as https from "./https"; +export {default as https} from "./https"; export * as electron from "./electron"; export * as crypto from "./crypto"; diff --git a/renderer/src/polyfill/request.js b/renderer/src/polyfill/request.js index 48091a56..a44433d8 100644 --- a/renderer/src/polyfill/request.js +++ b/renderer/src/polyfill/request.js @@ -32,16 +32,28 @@ function validOptions(url, callback) { return typeof url === "string" && typeof callback === "function"; } +function fixBuffer(options, callback) { + return (error, res, body) => { + if ("Content-Type" in Object(options.headers) && props.headers["Content-Type"] !== "text/plain") { + body = Buffer.from(body); + } else { + body = Buffer.from(body).toString(); + } + + callback(error, res, body); + }; +} + export default function request() { const {url, options = {}, callback} = parseArguments.apply(this, arguments); if (!validOptions(url, callback)) return null; if ("method" in options && methods.indexOf(options.method.toLowerCase()) >= 0) { - return Remote.https[options.method](url, options, callback); + return Remote.https[options.method](url, options, fixBuffer(options, callback)); } - return Remote.https.default(url, options, callback); + return Remote.https.request(url, options, fixBuffer(options, callback)); } Object.assign(request, Object.fromEntries( @@ -50,6 +62,6 @@ Object.assign(request, Object.fromEntries( if (!validOptions(url, callback)) return null; - return Remote.https[aliases[method] || method](url, options, callback); + return Remote.https[aliases[method] || method](url, options, fixBuffer(options, callback)); }]) -)); \ No newline at end of file +));