Skip to content

Commit 96b527b

Browse files
fix(node/http): don't throw on .address() before .listen() (#24432)
It's perfectly valid to access `server.address()` before calling `.listen()`. Until a server actively listens on a socket Node will return `null` here, but we threw a "Cannot access property 'port' of undefined" instead. This was discovered when inspecting failures in Koa's test suite with Deno.
1 parent f632b4a commit 96b527b

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

ext/node/polyfills/http.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ export class ServerImpl extends EventEmitter {
16571657
#httpConnections: Set<Deno.HttpConn> = new Set();
16581658
#listener?: Deno.Listener;
16591659

1660-
#addr: Deno.NetAddr;
1660+
#addr: Deno.NetAddr | null = null;
16611661
#hasClosed = false;
16621662
#server: Deno.HttpServer;
16631663
#unref = false;
@@ -1843,6 +1843,7 @@ export class ServerImpl extends EventEmitter {
18431843
}
18441844

18451845
address() {
1846+
if (this.#addr === null) return null;
18461847
return {
18471848
port: this.#addr.port,
18481849
address: this.#addr.hostname,

tests/unit_node/http_test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1251,3 +1251,8 @@ Deno.test("[node/http] http.request() post streaming body works", async () => {
12511251
clearTimeout(timeout);
12521252
assertEquals(server.listening, false);
12531253
});
1254+
1255+
Deno.test("[node/http] Server.address() can be null", () => {
1256+
const server = http.createServer((_req, res) => res.end("it works"));
1257+
assertEquals(server.address(), null);
1258+
});

0 commit comments

Comments
 (0)