node:https: provide proper Agent definition (#11826)

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
Meghan Denny
2024-09-06 16:11:19 -07:00
committed by GitHub
parent 9adf42b373
commit ed7741a662
12 changed files with 191 additions and 121 deletions

View File

@@ -1,14 +1,30 @@
// Hardcoded module "node:https"
const http = require("node:http");
const { urlToHttpOptions } = require("internal/url");
function request(input, options, cb) {
if (input && typeof input === "object" && !(input instanceof URL)) {
input.protocol ??= "https:";
} else if (typeof options === "object") {
options.protocol ??= "https:";
const ObjectSetPrototypeOf = Object.setPrototypeOf;
const ArrayPrototypeShift = Array.prototype.shift;
const ObjectAssign = Object.assign;
const ArrayPrototypeUnshift = Array.prototype.unshift;
function request(...args) {
let options = {};
if (typeof args[0] === "string") {
const urlStr = ArrayPrototypeShift.$call(args);
options = urlToHttpOptions(new URL(urlStr));
} else if (args[0] instanceof URL) {
options = urlToHttpOptions(ArrayPrototypeShift.$call(args));
}
return http.request(input, options, cb);
if (args[0] && typeof args[0] !== "function") {
ObjectAssign.$call(null, options, ArrayPrototypeShift.$call(args));
}
options._defaultAgent = https.globalAgent;
ArrayPrototypeUnshift.$call(args, options);
return new http.ClientRequest(...args);
}
function get(input, options, cb) {
@@ -17,8 +33,24 @@ function get(input, options, cb) {
return req;
}
export default {
...http,
function Agent(options) {
if (!(this instanceof Agent)) return new Agent(options);
http.Agent.$apply(this, [options]);
this.defaultPort = 443;
this.protocol = "https:";
this.maxCachedSessions = this.options.maxCachedSessions;
if (this.maxCachedSessions === undefined) this.maxCachedSessions = 100;
}
Agent.prototype = Object.create(http.Agent.prototype);
Agent.prototype.createConnection = http.createConnection;
var https = {
Agent,
globalAgent: new Agent({ keepAlive: true, scheduling: "lifo", timeout: 5000 }),
Server: http.Server,
createServer: http.createServer,
get,
request,
};
export default https;