diff options
author | 2023-10-18 04:18:14 +0800 | |
---|---|---|
committer | 2023-10-17 13:18:14 -0700 | |
commit | e91436e5248d947b50f90b4a7402690be8a41f39 (patch) | |
tree | 535ebf46dfaf55ab0cd3d55def9943e2a8ab8403 /src/bun.js/api/server.zig | |
parent | bbc2e96090d3cdc4ccfd5e4456a8d2a236c84c44 (diff) | |
download | bun-e91436e5248d947b50f90b4a7402690be8a41f39.tar.gz bun-e91436e5248d947b50f90b4a7402690be8a41f39.tar.zst bun-e91436e5248d947b50f90b4a7402690be8a41f39.zip |
fix(node:http): fix `server.address()` (#6442)
Closes #6413, #5850
Diffstat (limited to 'src/bun.js/api/server.zig')
-rw-r--r-- | src/bun.js/api/server.zig | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 1df908a0b..edf1d6d69 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -5297,6 +5297,37 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp return JSC.JSValue.jsNumber(@as(i32, @intCast(@as(u31, @truncate(this.activeSocketsCount()))))); } + pub fn getAddress(this: *ThisServer, globalThis: *JSGlobalObject) callconv(.C) JSC.JSValue { + switch (this.config.address) { + .unix => |unix| { + var value = bun.String.create(bun.sliceTo(@constCast(unix), 0)); + defer value.deref(); + return value.toJS(globalThis); + }, + .tcp => { + var port: u16 = this.config.address.tcp.port; + + if (this.listener) |listener| { + port = @intCast(listener.getLocalPort()); + + var buf: [64]u8 = [_]u8{0} ** 64; + var is_ipv6: bool = false; + + if (listener.socket().localAddressText(&buf, &is_ipv6)) |slice| { + var ip = bun.String.create(slice); + return JSSocketAddress__create( + this.globalThis, + ip.toJS(this.globalThis), + port, + is_ipv6, + ); + } + } + return JSValue.jsNull(); + }, + } + } + pub fn getHostname(this: *ThisServer, globalThis: *JSGlobalObject) callconv(.C) JSC.JSValue { if (this.cached_hostname.isEmpty()) { if (this.listener) |listener| { |