Skip to content

Commit 2357641

Browse files
authored
fix(serve): support ipv6 by default (#206)
* fix(serve): support ipv6 by default For node's server.listen, if no hostname param is specified it'll adaptively pick the unspecified ipv6 address (`::`) when available or the unspecified ipv4 address (`0.0.0.0`) otherwise. This fix gets rid of the behavior where we manually default to `0.0.0.0` and rely on node's underlying handling of this. See here for the docs on node: https://nodejs.org/api/net.html#serverlistenport-host-backlog-callback:~:text=If%20host%20is%20omitted%2C%20the%20server%20will%20accept%20connections%20on%20the%20unspecified%20IPv6%20address%20(%3A%3A)%20when%20IPv6%20is%20available%2C%20or%20the%20unspecified%20IPv4%20address%20(0.0.0.0)%20otherwise. * format
1 parent 74e86a2 commit 2357641

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const serve = (
2121
listeningListener?: (info: AddressInfo) => void
2222
): ServerType => {
2323
const server = createAdaptorServer(options)
24-
server.listen(options?.port ?? 3000, options.hostname ?? '0.0.0.0', () => {
24+
server.listen(options?.port ?? 3000, options.hostname, () => {
2525
const serverInfo = server.address() as AddressInfo
2626
listeningListener && listeningListener(serverInfo)
2727
})

test/server.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { createServer as createHttp2Server } from 'node:http2'
1111
import { createServer as createHTTPSServer } from 'node:https'
1212
import { GlobalRequest, Request as LightweightRequest, getAbortController } from '../src/request'
1313
import { GlobalResponse, Response as LightweightResponse } from '../src/response'
14-
import { createAdaptorServer } from '../src/server'
14+
import { createAdaptorServer, serve } from '../src/server'
1515
import type { HttpBindings } from '../src/types'
1616

1717
describe('Basic', () => {
@@ -908,3 +908,19 @@ describe('Memory leak test', () => {
908908
expect(counter).toBe(0)
909909
})
910910
})
911+
912+
describe('serve', () => {
913+
const app = new Hono()
914+
app.get('/', (c) => c.newResponse(null, 200))
915+
serve(app)
916+
917+
it('should serve on ipv4', async () => {
918+
const response = await fetch('http://localhost:3000')
919+
expect(response.status).toBe(200)
920+
})
921+
922+
it('should serve on ipv6', async () => {
923+
const response = await fetch('http://[::1]:3000')
924+
expect(response.status).toBe(200)
925+
})
926+
})

0 commit comments

Comments
 (0)