Skip to content

Commit 119e345

Browse files
committed
fix: listening on IPv6 address not possible
Wrap IPv6 addresses in square brackets when making URL in ensureAddress, fixing regression (#1582)
1 parent ed7bd2e commit 119e345

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/node/app.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ export const ensureAddress = (server: http.Server, protocol: string): URL | stri
9494
}
9595

9696
if (typeof addr !== "string") {
97-
return new URL(`${protocol}://${addr.address}:${addr.port}`)
97+
const host = addr.family === "IPv6" ? `[${addr.address}]` : addr.address
98+
return new URL(`${protocol}://${host}:${addr.port}`)
9899
}
99100

100101
// If this is a string then it is a pipe or Unix socket.

test/unit/node/app.test.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,20 @@ describe("ensureAddress", () => {
152152
it("should throw and error if no address", () => {
153153
expect(() => ensureAddress(mockServer, "http")).toThrow("Server has no address")
154154
})
155-
it("should return the address if it exists", async () => {
156-
mockServer.address = () => "http://localhost:8080/"
155+
it("should return the address if it's a string", async () => {
156+
mockServer.address = () => "/path/to/unix.sock"
157157
const address = ensureAddress(mockServer, "http")
158-
expect(address.toString()).toBe(`http://localhost:8080/`)
158+
expect(address.toString()).toBe(`/path/to/unix.sock`)
159+
})
160+
it("should construct URL with an IPv4 address", async () => {
161+
mockServer.address = () => ({ address: "1.2.3.4", port: 5678, family: "IPv4" })
162+
const address = ensureAddress(mockServer, "http")
163+
expect(address.toString()).toBe(`http://1.2.3.4:5678/`)
164+
})
165+
it("should construct URL with an IPv6 address", async () => {
166+
mockServer.address = () => ({ address: "a:b:c:d::1234", port: 5678, family: "IPv6" })
167+
const address = ensureAddress(mockServer, "http")
168+
expect(address.toString()).toBe(`http://[a:b:c:d::1234]:5678/`)
159169
})
160170
})
161171

0 commit comments

Comments
 (0)