Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/node/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export const ensureAddress = (server: http.Server, protocol: string): URL | stri
}

if (typeof addr !== "string") {
return new URL(`${protocol}://${addr.address}:${addr.port}`)
const host = addr.family === "IPv6" ? `[${addr.address}]` : addr.address
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love how simple this is 👏🏼

return new URL(`${protocol}://${host}:${addr.port}`)
}

// If this is a string then it is a pipe or Unix socket.
Expand Down
16 changes: 13 additions & 3 deletions test/unit/node/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,20 @@ describe("ensureAddress", () => {
it("should throw and error if no address", () => {
expect(() => ensureAddress(mockServer, "http")).toThrow("Server has no address")
})
it("should return the address if it exists", async () => {
mockServer.address = () => "http://localhost:8080/"
it("should return the address if it's a string", async () => {
mockServer.address = () => "/path/to/unix.sock"
const address = ensureAddress(mockServer, "http")
expect(address.toString()).toBe(`http://localhost:8080/`)
expect(address.toString()).toBe(`/path/to/unix.sock`)
})
it("should construct URL with an IPv4 address", async () => {
mockServer.address = () => ({ address: "1.2.3.4", port: 5678, family: "IPv4" })
const address = ensureAddress(mockServer, "http")
expect(address.toString()).toBe(`http://1.2.3.4:5678/`)
})
it("should construct URL with an IPv6 address", async () => {
mockServer.address = () => ({ address: "a:b:c:d::1234", port: 5678, family: "IPv6" })
const address = ensureAddress(mockServer, "http")
expect(address.toString()).toBe(`http://[a:b:c:d::1234]:5678/`)
Comment on lines +160 to +168
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appreciate you adding more tests as well 👏🏼

})
})

Expand Down