Skip to content
Closed
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
5 changes: 5 additions & 0 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,11 @@ declare module "bun" {
* When bun is not started with the `--hot` flag, this ID is currently unused.
*/
readonly id: string;

/**
* A URL object representing the address the server is listening on.
*/
readonly url: URL;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/bun.js/api/server.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ function generate(name) {
development: {
getter: "getDevelopment",
},
url: {
getter: "getURL",
},
},
klass: {},
finalize: true,
Expand Down
10 changes: 10 additions & 0 deletions src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5897,6 +5897,16 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp
},
}
}

pub fn getURL(
this: *ThisServer,
globalThis: *JSC.JSGlobalObject,
) callconv(.C) JSC.JSValue {
var href_ = bun.String.init(this.config.base_url.href);
var href = href_.toJS(globalThis).toBunString(globalThis);
const url = JSC.URL.fromString(href) orelse return JSValue.jsUndefined();
return JSC.URL.toJS(url, globalThis);
}
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/bun.js/bindings/BunString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ extern "C" WTF::URL* URL__fromJS(EncodedJSValue encodedValue, JSC::JSGlobalObjec
return new WTF::URL(WTFMove(url));
}

extern "C" EncodedJSValue URL__toJS(WTF::URL* wtfUrl, JSC::JSGlobalObject* globalObject)
{
auto throwScope = DECLARE_THROW_SCOPE(globalObject->vm());
return JSC::JSValue::encode(jsString(globalObject->vm(), wtfUrl->string()));
}

extern "C" BunString URL__getHrefFromJS(EncodedJSValue encodedValue, JSC::JSGlobalObject* globalObject)
{
auto throwScope = DECLARE_THROW_SCOPE(globalObject->vm());
Expand Down
72 changes: 68 additions & 4 deletions src/bun.js/bindings/ZigGeneratedClasses.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5481,6 +5481,7 @@ pub fn untrackFunction(

pub const URL = opaque {
extern fn URL__fromJS(JSValue, *JSC.JSGlobalObject) ?*URL;
extern fn URL__toJS(globalObject: *JSC.JSGlobalObject, in: *URL) JSC.JSValue;
extern fn URL__fromString(*bun.String) ?*URL;
extern fn URL__protocol(*URL) String;
extern fn URL__href(*URL) String;
Expand Down Expand Up @@ -5535,6 +5536,11 @@ pub const URL = opaque {
return URL__fromJS(value, globalObject);
}

pub fn toJS(url: *URL, globalObject: *JSC.JSGlobalObject) JSValue {
JSC.markBinding(@src());
return URL__toJS(globalObject, url);
}

pub fn fromUTF8(input: []const u8) ?*URL {
return fromString(String.fromUTF8(input));
}
Expand Down
16 changes: 16 additions & 0 deletions src/bun.js/bindings/generated_classes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,9 @@ pub const JSDebugHTTPSServer = struct {
@compileLog("Expected DebugHTTPSServer.doStop to be a callback but received " ++ @typeName(@TypeOf(DebugHTTPSServer.doStop)));
if (@TypeOf(DebugHTTPSServer.doUpgrade) != CallbackType)
@compileLog("Expected DebugHTTPSServer.doUpgrade to be a callback but received " ++ @typeName(@TypeOf(DebugHTTPSServer.doUpgrade)));
if (@TypeOf(DebugHTTPSServer.getURL) != GetterType)
@compileLog("Expected DebugHTTPSServer.getURL to be a getter");

if (!JSC.is_bindgen) {
@export(DebugHTTPSServer.doFetch, .{ .name = "DebugHTTPSServerPrototype__doFetch" });
@export(DebugHTTPSServer.doPublish, .{ .name = "DebugHTTPSServerPrototype__doPublish" });
Expand All @@ -1348,6 +1351,7 @@ pub const JSDebugHTTPSServer = struct {
@export(DebugHTTPSServer.getPendingWebSockets, .{ .name = "DebugHTTPSServerPrototype__getPendingWebSockets" });
@export(DebugHTTPSServer.getPort, .{ .name = "DebugHTTPSServerPrototype__getPort" });
@export(DebugHTTPSServer.getProtocol, .{ .name = "DebugHTTPSServerPrototype__getProtocol" });
@export(DebugHTTPSServer.getURL, .{ .name = "DebugHTTPSServerPrototype__getURL" });
}
}
};
Expand Down Expand Up @@ -1479,6 +1483,9 @@ pub const JSDebugHTTPServer = struct {
@compileLog("Expected DebugHTTPServer.doStop to be a callback but received " ++ @typeName(@TypeOf(DebugHTTPServer.doStop)));
if (@TypeOf(DebugHTTPServer.doUpgrade) != CallbackType)
@compileLog("Expected DebugHTTPServer.doUpgrade to be a callback but received " ++ @typeName(@TypeOf(DebugHTTPServer.doUpgrade)));
if (@TypeOf(DebugHTTPServer.getURL) != GetterType)
@compileLog("Expected DebugHTTPServer.getURL to be a getter");

if (!JSC.is_bindgen) {
@export(DebugHTTPServer.doFetch, .{ .name = "DebugHTTPServerPrototype__doFetch" });
@export(DebugHTTPServer.doPublish, .{ .name = "DebugHTTPServerPrototype__doPublish" });
Expand All @@ -1494,6 +1501,7 @@ pub const JSDebugHTTPServer = struct {
@export(DebugHTTPServer.getPendingWebSockets, .{ .name = "DebugHTTPServerPrototype__getPendingWebSockets" });
@export(DebugHTTPServer.getPort, .{ .name = "DebugHTTPServerPrototype__getPort" });
@export(DebugHTTPServer.getProtocol, .{ .name = "DebugHTTPServerPrototype__getProtocol" });
@export(DebugHTTPServer.getURL, .{ .name = "DebugHTTPServerPrototype__getURL" });
}
}
};
Expand Down Expand Up @@ -3289,6 +3297,9 @@ pub const JSHTTPSServer = struct {
@compileLog("Expected HTTPSServer.doStop to be a callback but received " ++ @typeName(@TypeOf(HTTPSServer.doStop)));
if (@TypeOf(HTTPSServer.doUpgrade) != CallbackType)
@compileLog("Expected HTTPSServer.doUpgrade to be a callback but received " ++ @typeName(@TypeOf(HTTPSServer.doUpgrade)));
if (@TypeOf(HTTPSServer.getURL) != GetterType)
@compileLog("Expected HTTPSServer.getURL to be a getter");

if (!JSC.is_bindgen) {
@export(HTTPSServer.doFetch, .{ .name = "HTTPSServerPrototype__doFetch" });
@export(HTTPSServer.doPublish, .{ .name = "HTTPSServerPrototype__doPublish" });
Expand All @@ -3304,6 +3315,7 @@ pub const JSHTTPSServer = struct {
@export(HTTPSServer.getPendingWebSockets, .{ .name = "HTTPSServerPrototype__getPendingWebSockets" });
@export(HTTPSServer.getPort, .{ .name = "HTTPSServerPrototype__getPort" });
@export(HTTPSServer.getProtocol, .{ .name = "HTTPSServerPrototype__getProtocol" });
@export(HTTPSServer.getURL, .{ .name = "HTTPSServerPrototype__getURL" });
}
}
};
Expand Down Expand Up @@ -3435,6 +3447,9 @@ pub const JSHTTPServer = struct {
@compileLog("Expected HTTPServer.doStop to be a callback but received " ++ @typeName(@TypeOf(HTTPServer.doStop)));
if (@TypeOf(HTTPServer.doUpgrade) != CallbackType)
@compileLog("Expected HTTPServer.doUpgrade to be a callback but received " ++ @typeName(@TypeOf(HTTPServer.doUpgrade)));
if (@TypeOf(HTTPServer.getURL) != GetterType)
@compileLog("Expected HTTPServer.getURL to be a getter");

if (!JSC.is_bindgen) {
@export(HTTPServer.doFetch, .{ .name = "HTTPServerPrototype__doFetch" });
@export(HTTPServer.doPublish, .{ .name = "HTTPServerPrototype__doPublish" });
Expand All @@ -3450,6 +3465,7 @@ pub const JSHTTPServer = struct {
@export(HTTPServer.getPendingWebSockets, .{ .name = "HTTPServerPrototype__getPendingWebSockets" });
@export(HTTPServer.getPort, .{ .name = "HTTPServerPrototype__getPort" });
@export(HTTPServer.getProtocol, .{ .name = "HTTPServerPrototype__getProtocol" });
@export(HTTPServer.getURL, .{ .name = "HTTPServerPrototype__getURL" });
}
}
};
Expand Down
Loading