Skip to content

Commit

Permalink
Support IPv6 ::1 in listeningOrigin (#4902)
Browse files Browse the repository at this point in the history
* Support IPv6 ::1 in listeningOrigin

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* Update fastify.js

Co-authored-by: KaKa <23028015+climba03003@users.noreply.github.com>

---------

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Co-authored-by: KaKa <23028015+climba03003@users.noreply.github.com>
  • Loading branch information
mcollina and climba03003 committed Jul 15, 2023
1 parent 9f575d0 commit 56ca418
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ function fastify (options) {
if (typeof address === 'string') {
return address
}
return `${this[kOptions].https ? 'https' : 'http'}://${address.address}:${address.port}`
const host = address.family === 'IPv6' ? `[${address.address}]` : address.address
return `${this[kOptions].https ? 'https' : 'http'}://${host}:${address.port}`
}
},
pluginName: {
Expand Down
19 changes: 10 additions & 9 deletions test/fastify-instance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,20 @@ test('fastify instance should contains listeningOrigin property (with port and h
await fastify.close()
})

test('fastify instance should contains listeningOrigin property (no options)', async t => {
t.plan(1)
const fastify = Fastify()
await fastify.listen()
const address = fastify.server.address()
t.same(fastify.listeningOrigin, `http://${address.address}:${address.port}`)
await fastify.close()
})

test('fastify instance should contains listeningOrigin property (unix socket)', { skip: os.platform() === 'win32' }, async t => {
const fastify = Fastify()
const path = `fastify.${Date.now()}.sock`
await fastify.listen({ path })
t.same(fastify.listeningOrigin, path)
await fastify.close()
})

test('fastify instance should contains listeningOrigin property (IPv6)', async t => {
t.plan(1)
const port = 3000
const host = '::1'
const fastify = Fastify()
await fastify.listen({ port, host })
t.same(fastify.listeningOrigin, `http://[::1]:${port}`)
await fastify.close()
})

0 comments on commit 56ca418

Please sign in to comment.