Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 64 additions & 14 deletions src/bun.js/http.exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const NODE_HTTP_WARNING =
"WARN: Agent is mostly unused in Bun's implementation of http. If you see strange behavior, this is probably the cause.";

var _globalAgent;
var _defaultHTTPSAgent;
var kInternalRequest = Symbol("kInternalRequest");

var FakeSocket = class Socket {
on() {
Expand Down Expand Up @@ -97,6 +99,8 @@ export class Agent extends EventEmitter {
this.#scheduling = options.scheduling || "lifo";
this.#maxTotalSockets = options.maxTotalSockets;
this.#totalSocketCount = 0;
this.#defaultPort = options.defaultPort || 80;
this.#protocol = options.protocol || "http:";
}

get defaultPort() {
Expand Down Expand Up @@ -226,7 +230,7 @@ export class Server extends EventEmitter {
}

address() {
return this.#server.hostname;
return this.#server?.hostname;
}

listen(port, host, onListen) {
Expand Down Expand Up @@ -322,14 +326,21 @@ function destroyBodyStreamNT(bodyStream) {
}

var defaultIncomingOpts = { type: "request" };

function getDefaultHTTPSAgent() {
return (_defaultHTTPSAgent ??= new Agent({ defaultPort: 443, protocol: "https:" }));
}

export class IncomingMessage extends Readable {
constructor(req, { type = "request" } = defaultIncomingOpts) {
constructor(req, defaultIncomingOpts) {
const method = req.method;

super();

const url = new URL(req.url);

var { type = "request", [kInternalRequest]: nodeReq } = defaultIncomingOpts || {};

this.#noBody =
type === "request" // TODO: Add logic for checking for body on response
? "GET" === method ||
Expand All @@ -349,6 +360,7 @@ export class IncomingMessage extends Readable {
this.#fakeSocket = undefined;

this.url = url.pathname + url.search;
this.#nodeReq = nodeReq;
assignHeaders(this, req);
}

Expand All @@ -363,6 +375,11 @@ export class IncomingMessage extends Readable {
#req;
url;
#type;
#nodeReq;

get req() {
return this.#nodeReq;
}

_construct(callback) {
// TODO: streaming
Expand All @@ -373,7 +390,6 @@ export class IncomingMessage extends Readable {

const contentLength = this.#req.headers.get("content-length");
const length = contentLength ? parseInt(contentLength, 10) : 0;

if (length === 0) {
this.#noBody = true;
callback();
Expand Down Expand Up @@ -898,6 +914,7 @@ export class ClientRequest extends OutgoingMessage {
#protocol;
#method;
#port;
#useDefaultPort;
#joinDuplicateHeaders;
#maxHeaderSize;
#agent = _globalAgent;
Expand Down Expand Up @@ -967,17 +984,21 @@ export class ClientRequest extends OutgoingMessage {
var method = this.#method,
body = this.#body;

this.#fetchRequest = fetch(`${this.#protocol}//${this.#host}:${this.#port}${this.#path}`, {
method,
headers: this.getHeaders(),
body: body && method !== "GET" && method !== "HEAD" && method !== "OPTIONS" ? body : undefined,
redirect: "manual",
verbose: Boolean(__DEBUG__),
signal: this[kAbortController].signal,
})
this.#fetchRequest = fetch(
`${this.#protocol}//${this.#host}${this.#useDefaultPort ? "" : ":" + this.#port}${this.#path}`,
{
method,
headers: this.getHeaders(),
body: body && method !== "GET" && method !== "HEAD" && method !== "OPTIONS" ? body : undefined,
redirect: "manual",
verbose: Boolean(__DEBUG__),
signal: this[kAbortController].signal,
},
)
.then(response => {
var res = (this.#res = new IncomingMessage(response, {
type: "response",
[kInternalRequest]: this,
}));
this.emit("response", res);
})
Expand Down Expand Up @@ -1008,7 +1029,12 @@ export class ClientRequest extends OutgoingMessage {

if (typeof input === "string") {
const urlStr = input;
input = urlToHttpOptions(new URL(urlStr));
try {
var urlObject = new URL(urlStr);
} catch (e) {
throw new TypeError(`Invalid URL: ${urlStr}`);
}
input = urlToHttpOptions(urlObject);
} else if (input && typeof input === "object" && input instanceof URL) {
// url.URL instance
input = urlToHttpOptions(input);
Expand All @@ -1025,8 +1051,29 @@ export class ClientRequest extends OutgoingMessage {
options = ObjectAssign(input || {}, options);
}

const defaultAgent = options._defaultAgent || Agent.globalAgent;
var defaultAgent = options._defaultAgent || Agent.globalAgent;

const protocol = (this.#protocol = options.protocol ||= defaultAgent.protocol);
switch (this.#agent?.protocol) {
case undefined: {
break;
}
case "http:": {
if (protocol === "https:") {
defaultAgent = this.#agent = getDefaultHTTPSAgent();
break;
}
}
case "https:": {
if (protocol === "https") {
defaultAgent = this.#agent = Agent.globalAgent;
break;
}
}
default: {
break;
}
}

if (options.path) {
const path = String(options.path);
Expand All @@ -1044,7 +1091,10 @@ export class ClientRequest extends OutgoingMessage {
// throw new ERR_INVALID_PROTOCOL(protocol, expectedProtocol);
}

this.#port = options.port || options.defaultPort || this.#agent?.defaultPort || (protocol === "https:" ? 443 : 80);
const defaultPort = protocol === "https:" ? 443 : 80;

this.#port = options.port || options.defaultPort || this.#agent?.defaultPort || defaultPort;
this.#useDefaultPort = this.#port === defaultPort;
const host =
(this.#host =
options.host =
Expand Down
53 changes: 21 additions & 32 deletions src/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -598,54 +598,42 @@ pub const Platform = enum {
break :brk array;
};

pub const default_conditions_strings = .{
.browser = @as(string, "browser"),
.import = @as(string, "import"),
.worker = @as(string, "worker"),
.require = @as(string, "require"),
.node = @as(string, "node"),
.default = @as(string, "default"),
.bun = @as(string, "bun"),
.bun_macro = @as(string, "bun_macro"),
.module = @as(string, "module"), // used in tslib
.development = @as(string, "development"),
.production = @as(string, "production"),
};

pub const DefaultConditions: std.EnumArray(Platform, []const string) = brk: {
var array = std.EnumArray(Platform, []const string).initUndefined();

array.set(Platform.node, &[_]string{
default_conditions_strings.node,
default_conditions_strings.module,
"node",
"module",
});

var listc = [_]string{
default_conditions_strings.browser,
default_conditions_strings.module,
"browser",
"module",
};
array.set(Platform.browser, &listc);
array.set(
Platform.bun,
&[_]string{
default_conditions_strings.bun,
default_conditions_strings.worker,
default_conditions_strings.module,
default_conditions_strings.node,
default_conditions_strings.browser,
"bun",
"worker",
"module",
"node",
"default",
"browser",
},
);
array.set(
Platform.bun_macro,
&[_]string{
default_conditions_strings.bun,
default_conditions_strings.worker,
default_conditions_strings.module,
default_conditions_strings.node,
default_conditions_strings.browser,
"bun",
"worker",
"module",
"node",
"default",
"browser",
},
);
// array.set(Platform.bun_macro, [_]string{ default_conditions_strings.bun_macro, default_conditions_strings.browser, default_conditions_strings.default, },);
// array.set(Platform.bun_macro, [_]string{ "bun_macro", "browser", "default", },);

// Original comment:
// The neutral platform is for people that don't want esbuild to try to
Expand Down Expand Up @@ -868,16 +856,17 @@ pub const ESMConditions = struct {
try import_condition_map.ensureTotalCapacity(defaults.len + 1);
try require_condition_map.ensureTotalCapacity(defaults.len + 1);

import_condition_map.putAssumeCapacityNoClobber(Platform.default_conditions_strings.import, {});
require_condition_map.putAssumeCapacityNoClobber(Platform.default_conditions_strings.require, {});
default_condition_amp.putAssumeCapacityNoClobber(Platform.default_conditions_strings.default, {});
import_condition_map.putAssumeCapacity("import", {});
require_condition_map.putAssumeCapacity("require", {});

for (defaults) |default| {
default_condition_amp.putAssumeCapacityNoClobber(default, {});
import_condition_map.putAssumeCapacityNoClobber(default, {});
require_condition_map.putAssumeCapacityNoClobber(default, {});
}

default_condition_amp.putAssumeCapacity("default", {});

return ESMConditions{
.default = default_condition_amp,
.import = import_condition_map,
Expand Down
Loading