feat: move away from `request` dependency
This commit is contained in:
parent
6d37fb63fc
commit
b5a69db0b3
|
@ -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
|
||||
});
|
||||
]))
|
||||
);
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
|
|
@ -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));
|
||||
}])
|
||||
));
|
||||
));
|
||||
|
|
Loading…
Reference in New Issue