1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

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.
This commit is contained in:
Marvin Hagemeister 2024-07-04 18:28:48 +02:00 committed by GitHub
parent f632b4a92e
commit 96b527b8df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View file

@ -1657,7 +1657,7 @@ export class ServerImpl extends EventEmitter {
#httpConnections: Set<Deno.HttpConn> = new Set();
#listener?: Deno.Listener;
#addr: Deno.NetAddr;
#addr: Deno.NetAddr | null = null;
#hasClosed = false;
#server: Deno.HttpServer;
#unref = false;
@ -1843,6 +1843,7 @@ export class ServerImpl extends EventEmitter {
}
address() {
if (this.#addr === null) return null;
return {
port: this.#addr.port,
address: this.#addr.hostname,

View file

@ -1251,3 +1251,8 @@ Deno.test("[node/http] http.request() post streaming body works", async () => {
clearTimeout(timeout);
assertEquals(server.listening, false);
});
Deno.test("[node/http] Server.address() can be null", () => {
const server = http.createServer((_req, res) => res.end("it works"));
assertEquals(server.address(), null);
});