parent
d90c8ded64
commit
f71c7c36fc
2 changed files with 52 additions and 53 deletions
|
@ -67,9 +67,6 @@
|
|||
],
|
||||
"jsdoc/require-param-description": [
|
||||
"off"
|
||||
],
|
||||
"no-async-promise-executor": [
|
||||
"off"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -63,60 +63,62 @@ module.exports = {
|
|||
// TODO: Support following redirects.
|
||||
options["follow-redirects"] = options["follow-redirects"]??true;
|
||||
var headers = {"accept-encoding": "br, gzip", ...options["headers"]};
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const parsedUrl = new URL(requestUrl);
|
||||
var method = "GET";
|
||||
if ( options["method"] ) {
|
||||
method = options["method"];
|
||||
}
|
||||
var body = "";
|
||||
if ( method === "POST" ) {
|
||||
if ( typeof options["body"] === "string" ) {
|
||||
body = options["body"];
|
||||
} else if ( Buffer.isBuffer(options["body"]) ) {
|
||||
body = options["body"];
|
||||
} else if ( options["headers"]["content-type"].toLowerCase() === "application/ld+json" ||
|
||||
return new Promise((resolve, reject) => {
|
||||
(async () => {
|
||||
const parsedUrl = new URL(requestUrl);
|
||||
var method = "GET";
|
||||
if ( options["method"] ) {
|
||||
method = options["method"];
|
||||
}
|
||||
var body = "";
|
||||
if ( method === "POST" ) {
|
||||
if ( typeof options["body"] === "string" ) {
|
||||
body = options["body"];
|
||||
} else if ( Buffer.isBuffer(options["body"]) ) {
|
||||
body = options["body"];
|
||||
} else if ( options["headers"]["content-type"].toLowerCase() === "application/ld+json" ||
|
||||
options["headers"]["content-type"].toLowerCase() === "application/activity+json" ||
|
||||
options["headers"]["content-type"].toLowerCase() === "application/json") {
|
||||
body = JSON.stringify(options["body"]);
|
||||
body = JSON.stringify(options["body"]);
|
||||
} else {
|
||||
return reject(new Error("Unrecognized body content-type, passed as object."));
|
||||
}
|
||||
}
|
||||
if ( body !== "" ) {
|
||||
options["headers"]["content-length"] = Buffer.byteLength(body);
|
||||
}
|
||||
if ( options["actor"] ) {
|
||||
var keyPair = getKeyPair("actor");
|
||||
var rsaSha256Sign = crypto.createSign("RSA-SHA256");
|
||||
headers["date"] = headers["date"]??(new Date()).toUTCString();
|
||||
var toSign = `(request-target): ${method.toLowerCase()} ${parsedUrl.pathname}?${parsedUrl.search}\nhost: ${parsedUrl.host}\ndate: ${headers["date"]}`;
|
||||
if ( method === "POST" ) {
|
||||
var digest = crypto.createHash("sha256").update(body).end().digest("hex");
|
||||
headers["digest"] = digest;
|
||||
toSign = `${toSign}\ndigest: ${headers["digest"]}`;
|
||||
}
|
||||
headers["signature"] = `keyId=${options["actor"]}#main-key,headers="(request-target) host date${method === "post" ? " digest":""}",signature=${rsaSha256Sign.update(toSign).end().sign((await keyPair).privateKey, "base64")}`;
|
||||
}
|
||||
if ( parsedUrl.protocol.toLowerCase() === "https:" ) {
|
||||
const httpsRequest = https.request(parsedUrl, {headers}, (res) => {
|
||||
handleCallback(requestUrl, options, res, resolve, reject);
|
||||
}).on('error', (error) => reject(error));
|
||||
if ( body !== "" ) {
|
||||
httpsRequest.write(body);
|
||||
}
|
||||
httpsRequest.end();
|
||||
} else if ( parsedUrl.protocol.toLowerCase() === "http:" ) {
|
||||
const httpRequest = http.request(parsedUrl, {headers}, (res) => {
|
||||
handleCallback(requestUrl, options, res, resolve, reject);
|
||||
}).on('error', (error) => reject(error));
|
||||
if ( body !== "" ) {
|
||||
httpRequest.write(body);
|
||||
}
|
||||
httpRequest.end();
|
||||
} else {
|
||||
return reject(new Error("Unrecognized body content-type, passed as object."));
|
||||
reject("Unrecognized protocol.");
|
||||
}
|
||||
}
|
||||
if ( body !== "" ) {
|
||||
options["headers"]["content-length"] = Buffer.byteLength(body);
|
||||
}
|
||||
if ( options["actor"] ) {
|
||||
var keyPair = getKeyPair("actor");
|
||||
var rsaSha256Sign = crypto.createSign("RSA-SHA256");
|
||||
headers["date"] = headers["date"]??(new Date()).toUTCString();
|
||||
var toSign = `(request-target): ${method.toLowerCase()} ${parsedUrl.pathname}?${parsedUrl.search}\nhost: ${parsedUrl.host}\ndate: ${headers["date"]}`;
|
||||
if ( method === "POST" ) {
|
||||
var digest = crypto.createHash("sha256").update(body).end().digest("hex");
|
||||
headers["digest"] = digest;
|
||||
toSign = `${toSign}\ndigest: ${headers["digest"]}`;
|
||||
}
|
||||
headers["signature"] = `keyId=${options["actor"]}#main-key,headers="(request-target) host date${method === "post" ? " digest":""}",signature=${rsaSha256Sign.update(toSign).end().sign((await keyPair).privateKey, "base64")}`;
|
||||
}
|
||||
if ( parsedUrl.protocol.toLowerCase() === "https:" ) {
|
||||
const httpsRequest = https.request(parsedUrl, {headers}, (res) => {
|
||||
handleCallback(requestUrl, options, res, resolve, reject);
|
||||
}).on('error', (error) => reject(error));
|
||||
if ( body !== "" ) {
|
||||
httpsRequest.write(body);
|
||||
}
|
||||
httpsRequest.end();
|
||||
} else if ( parsedUrl.protocol.toLowerCase() === "http:" ) {
|
||||
const httpRequest = http.request(parsedUrl, {headers}, (res) => {
|
||||
handleCallback(requestUrl, options, res, resolve, reject);
|
||||
}).on('error', (error) => reject(error));
|
||||
if ( body !== "" ) {
|
||||
httpRequest.write(body);
|
||||
}
|
||||
httpRequest.end();
|
||||
} else {
|
||||
reject("Unrecognized protocol.");
|
||||
}
|
||||
})().then();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue