Unverified Commit 9dedc945 authored by Akshit Sinha's avatar Akshit Sinha Committed by GitHub

Fix IPv6 hostname formatting in CLI (#43491)

Co-authored-by: 's avatarJan Kaifer <jan@kaifer.cz>
Fixes https://github.com/vercel/next.js/issues/21979
parent c7162c42
...@@ -13,6 +13,7 @@ import path from 'path' ...@@ -13,6 +13,7 @@ import path from 'path'
import type { NextConfig } from '../../types' import type { NextConfig } from '../../types'
import type { NextConfigComplete } from '../server/config-shared' import type { NextConfigComplete } from '../server/config-shared'
import { traceGlobals } from '../trace/shared' import { traceGlobals } from '../trace/shared'
import { isIPv6 } from 'net'
import cluster from 'cluster' import cluster from 'cluster'
import { Telemetry } from '../telemetry/storage' import { Telemetry } from '../telemetry/storage'
import loadConfig from '../server/config' import loadConfig from '../server/config'
...@@ -403,7 +404,11 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit ...@@ -403,7 +404,11 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit
startServer(devServerOptions) startServer(devServerOptions)
.then(async (app) => { .then(async (app) => {
const appUrl = `http://${app.hostname}:${app.port}` const appUrl = `http://${app.hostname}:${app.port}`
startedDevelopmentServer(appUrl, `${host || '0.0.0.0'}:${app.port}`) const hostname = host || '0.0.0.0'
startedDevelopmentServer(
appUrl,
`${isIPv6(hostname) ? `[${hostname}]` : hostname}:${app.port}`
)
// Start preflight after server is listening and ignore errors: // Start preflight after server is listening and ignore errors:
preflight().catch(() => {}) preflight().catch(() => {})
// Finalize server bootup: // Finalize server bootup:
......
...@@ -7,6 +7,7 @@ import * as Log from '../build/output/log' ...@@ -7,6 +7,7 @@ import * as Log from '../build/output/log'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { CliCommand } from '../lib/commands' import { CliCommand } from '../lib/commands'
import { isIPv6 } from 'net'
const nextStart: CliCommand = (argv) => { const nextStart: CliCommand = (argv) => {
const validArgs: arg.Spec = { const validArgs: arg.Spec = {
...@@ -80,7 +81,8 @@ const nextStart: CliCommand = (argv) => { ...@@ -80,7 +81,8 @@ const nextStart: CliCommand = (argv) => {
}) })
.then(async (app) => { .then(async (app) => {
const appUrl = `http://${app.hostname}:${app.port}` const appUrl = `http://${app.hostname}:${app.port}`
Log.ready(`started server on ${host}:${app.port}, url: ${appUrl}`) const hostname = isIPv6(host) ? `[${host}]` : host
Log.ready(`started server on ${hostname}:${app.port}, url: ${appUrl}`)
await app.prepare() await app.prepare()
}) })
.catch((err) => { .catch((err) => {
......
...@@ -2,8 +2,10 @@ import type { NextServerOptions, NextServer, RequestHandler } from '../next' ...@@ -2,8 +2,10 @@ import type { NextServerOptions, NextServer, RequestHandler } from '../next'
import { warn } from '../../build/output/log' import { warn } from '../../build/output/log'
import http from 'http' import http from 'http'
import next from '../next' import next from '../next'
import { isIPv6 } from 'net'
import cluster from 'cluster' import cluster from 'cluster'
import v8 from 'v8' import v8 from 'v8'
interface StartServerOptions extends NextServerOptions { interface StartServerOptions extends NextServerOptions {
allowRetry?: boolean allowRetry?: boolean
keepAliveTimeout?: number keepAliveTimeout?: number
...@@ -60,10 +62,13 @@ export function startServer(opts: StartServerOptions) { ...@@ -60,10 +62,13 @@ export function startServer(opts: StartServerOptions) {
server.on('listening', () => { server.on('listening', () => {
const addr = server.address() const addr = server.address()
const hostname = let hostname =
!opts.hostname || opts.hostname === '0.0.0.0' !opts.hostname || opts.hostname === '0.0.0.0'
? 'localhost' ? 'localhost'
: opts.hostname : opts.hostname
if (isIPv6(hostname)) {
hostname = hostname === '::' ? '[::1]' : `[${hostname}]`
}
const app = next({ const app = next({
...opts, ...opts,
......
...@@ -277,6 +277,16 @@ describe('CLI Usage', () => { ...@@ -277,6 +277,16 @@ describe('CLI Usage', () => {
expect(output).toMatch(new RegExp(`http://localhost:${port}`)) expect(output).toMatch(new RegExp(`http://localhost:${port}`))
}) })
test('should format IPv6 addresses correctly', async () => {
const port = await findPort()
const output = await runNextCommandDev(
[dir, '--hostname', '::', '--port', port],
true
)
expect(output).toMatch(new RegExp(`on \\[::\\]:${port}`))
expect(output).toMatch(new RegExp(`http://\\[::1\\]:${port}`))
})
test('should warn when unknown argument provided', async () => { test('should warn when unknown argument provided', async () => {
const { stderr } = await runNextCommand(['dev', '--random'], { const { stderr } = await runNextCommand(['dev', '--random'], {
stderr: true, stderr: true,
...@@ -346,6 +356,18 @@ describe('CLI Usage', () => { ...@@ -346,6 +356,18 @@ describe('CLI Usage', () => {
expect(help.stdout).toMatch(/Starts the application in production mode/) expect(help.stdout).toMatch(/Starts the application in production mode/)
}) })
test('should format IPv6 addresses correctly', async () => {
const port = await findPort()
const output = await runNextCommand(
['start', '--hostname', '::', '--port', port],
{
stdout: true,
}
)
expect(output.stdout).toMatch(new RegExp(`on \\[::\\]:${port}`))
expect(output.stdout).toMatch(new RegExp(`http://\\[::1\\]:${port}`))
})
test('should warn when unknown argument provided', async () => { test('should warn when unknown argument provided', async () => {
const { stderr } = await runNextCommand(['start', '--random'], { const { stderr } = await runNextCommand(['start', '--random'], {
stderr: true, stderr: true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment