mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
node:https: provide proper Agent definition (#11826)
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user