Unverified Commit a4b430e6 authored by Ngô Đức Anh's avatar Ngô Đức Anh Committed by GitHub

Better IPv6 support for `next-server` (#53131)

### What?
This PR makes it easier to use Next.js with IPv6 hostnames such as `::1` and `::`.

### How?
It does so by removing rewrites from `localhost` to `127.0.0.1` introduced in #52492. It also fixes the issue where Next.js tries to fetch something like `http://::1:3000` when `--hostname` is `::1` as it is not a valid URL (browsers' `URL` class throws an error when constructed with such hosts). It also fixes `NextURL` so that it doesn't accept `http://::1:3000` but refuse `http://[::1]:3000`. It also changes `next/src/server/lib/setup-server-worker.ts` so that it uses the server's `address` method to retrieve the host instead of our provided `opts.hostname`, ensuring that no matter what `opts.hostname` is we will always get the correct one.

### Note
I've verified that `next dev`, `next start` and `node .next/standalone/server.js` work with IPv6 hostnames (such as `::` and `::1`), IPv4 hostnames (such as `127.0.0.1`, `0.0.0.0`) and `localhost` - and with any of these hostnames fetching to `localhost` also works. Server Actions and middleware have no problems as well.

This also removes `.next/standalone/server.js`'s logging as we now use `start-server`'s logging to avoid duplicates. `start-server`'s logging has also been updated to report the actual hostname.
![image](https://github.com/vercel/next.js/assets/75556609/cefa5f23-ff09-4cef-a055-13eea7c11d89)
![image](https://github.com/vercel/next.js/assets/75556609/619e82ce-45d9-47b7-8644-f4ad083429db)
The above pictures also demonstrate using Server Actions with Next.js after this PR.
![image](https://github.com/vercel/next.js/assets/75556609/3d4166e9-f950-4390-bde9-af2547658148)

Fixes #53171
Fixes #49578
Closes NEXT-1510
Co-authored-by: 's avatarTim Neutkens <6324199+timneutkens@users.noreply.github.com>
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent df6ec96a
...@@ -6,11 +6,12 @@ import { ...@@ -6,11 +6,12 @@ import {
import { NodeNextRequest } from 'next/dist/server/base-http/node' import { NodeNextRequest } from 'next/dist/server/base-http/node'
import { BaseNextRequest } from 'next/dist/server/base-http' import { BaseNextRequest } from 'next/dist/server/base-http'
import { getCloneableBody } from 'next/dist/server/body-streams' import { getCloneableBody } from 'next/dist/server/body-streams'
import { formatHostname } from 'next/dist/server/lib/format-hostname'
export function attachRequestMeta( export function attachRequestMeta(
req: BaseNextRequest, req: BaseNextRequest,
parsedUrl: NextUrlWithParsedQuery, parsedUrl: NextUrlWithParsedQuery,
host: string hostname: string
) { ) {
const protocol = ( const protocol = (
(req as NodeNextRequest).originalRequest?.socket as TLSSocket (req as NodeNextRequest).originalRequest?.socket as TLSSocket
...@@ -18,7 +19,7 @@ export function attachRequestMeta( ...@@ -18,7 +19,7 @@ export function attachRequestMeta(
? 'https' ? 'https'
: 'http' : 'http'
const initUrl = `${protocol}://${host}${req.url}` const initUrl = `${protocol}://${formatHostname(hostname)}${req.url}`
addRequestMeta(req, '__NEXT_INIT_URL', initUrl) addRequestMeta(req, '__NEXT_INIT_URL', initUrl)
addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query }) addRequestMeta(req, '__NEXT_INIT_QUERY', { ...parsedUrl.query })
......
...@@ -1972,17 +1972,11 @@ startServer({ ...@@ -1972,17 +1972,11 @@ startServer({
dir, dir,
isDev: false, isDev: false,
config: nextConfig, config: nextConfig,
hostname: hostname === 'localhost' ? '0.0.0.0' : hostname, hostname,
port: currentPort, port: currentPort,
allowRetry: false, allowRetry: false,
keepAliveTimeout, keepAliveTimeout,
useWorkers: !!nextConfig.experimental?.appDir, useWorkers: !!nextConfig.experimental?.appDir,
}).then(() => {
console.log(
'Listening on port',
currentPort,
'url: http://' + hostname + ':' + currentPort
)
}).catch((err) => { }).catch((err) => {
console.error(err); console.error(err);
process.exit(1); process.exit(1);
......
...@@ -466,7 +466,7 @@ async function revalidate( ...@@ -466,7 +466,7 @@ async function revalidate(
const ipcKey = process.env.__NEXT_PRIVATE_ROUTER_IPC_KEY const ipcKey = process.env.__NEXT_PRIVATE_ROUTER_IPC_KEY
const res = await invokeRequest( const res = await invokeRequest(
`http://${ `http://${
context.hostname context.hostname || 'localhost'
}:${ipcPort}?key=${ipcKey}&method=revalidate&args=${encodeURIComponent( }:${ipcPort}?key=${ipcKey}&method=revalidate&args=${encodeURIComponent(
JSON.stringify([{ urlPath, revalidateHeaders, opts }]) JSON.stringify([{ urlPath, revalidateHeaders, opts }])
)}`, )}`,
......
...@@ -121,24 +121,6 @@ function getForwardedHeaders( ...@@ -121,24 +121,6 @@ function getForwardedHeaders(
return new Headers(mergedHeaders) return new Headers(mergedHeaders)
} }
function fetchIPv4v6(
url: URL,
init: RequestInit,
v6 = false
): Promise<Response> {
const hostname = url.hostname
if (!v6 && hostname === 'localhost') {
url.hostname = '127.0.0.1'
}
return fetch(url, init).catch((err) => {
if (err.code === 'ECONNREFUSED' && !v6) {
return fetchIPv4v6(url, init, true)
}
throw err
})
}
async function addRevalidationHeader( async function addRevalidationHeader(
res: ServerResponse, res: ServerResponse,
{ {
...@@ -212,7 +194,7 @@ async function createRedirectRenderResult( ...@@ -212,7 +194,7 @@ async function createRedirectRenderResult(
// } // }
try { try {
const headResponse = await fetchIPv4v6(fetchUrl, { const headResponse = await fetch(fetchUrl, {
method: 'HEAD', method: 'HEAD',
headers: forwardedHeaders, headers: forwardedHeaders,
next: { next: {
...@@ -224,7 +206,7 @@ async function createRedirectRenderResult( ...@@ -224,7 +206,7 @@ async function createRedirectRenderResult(
if ( if (
headResponse.headers.get('content-type') === RSC_CONTENT_TYPE_HEADER headResponse.headers.get('content-type') === RSC_CONTENT_TYPE_HEADER
) { ) {
const response = await fetchIPv4v6(fetchUrl, { const response = await fetch(fetchUrl, {
method: 'GET', method: 'GET',
headers: forwardedHeaders, headers: forwardedHeaders,
next: { next: {
......
...@@ -36,6 +36,7 @@ import type { ...@@ -36,6 +36,7 @@ import type {
} from './future/route-modules/app-route/module' } from './future/route-modules/app-route/module'
import { format as formatUrl, parse as parseUrl } from 'url' import { format as formatUrl, parse as parseUrl } from 'url'
import { formatHostname } from './lib/format-hostname'
import { getRedirectStatus } from '../lib/redirect-status' import { getRedirectStatus } from '../lib/redirect-status'
import { isEdgeRuntime } from '../lib/is-edge-runtime' import { isEdgeRuntime } from '../lib/is-edge-runtime'
import { import {
...@@ -268,6 +269,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -268,6 +269,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
protected clientReferenceManifest?: ClientReferenceManifest protected clientReferenceManifest?: ClientReferenceManifest
protected nextFontManifest?: NextFontManifest protected nextFontManifest?: NextFontManifest
public readonly hostname?: string public readonly hostname?: string
public readonly fetchHostname?: string
public readonly port?: number public readonly port?: number
protected abstract getPublicDir(): string protected abstract getPublicDir(): string
...@@ -367,6 +369,10 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -367,6 +369,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// values from causing issues as this can be user provided // values from causing issues as this can be user provided
this.nextConfig = conf as NextConfigComplete this.nextConfig = conf as NextConfigComplete
this.hostname = hostname this.hostname = hostname
if (this.hostname) {
// we format the hostname so that it can be fetched
this.fetchHostname = formatHostname(this.hostname)
}
this.port = port this.port = port
this.distDir = this.distDir =
process.env.NEXT_RUNTIME === 'edge' process.env.NEXT_RUNTIME === 'edge'
......
...@@ -476,7 +476,7 @@ export default class DevServer extends Server { ...@@ -476,7 +476,7 @@ export default class DevServer extends Server {
) { ) {
if (this.isRenderWorker) { if (this.isRenderWorker) {
await invokeIpcMethod({ await invokeIpcMethod({
hostname: this.hostname, fetchHostname: this.fetchHostname,
method: 'logErrorWithOriginalStack', method: 'logErrorWithOriginalStack',
args: [errorToJSON(err as Error), type], args: [errorToJSON(err as Error), type],
ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT, ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT,
...@@ -732,7 +732,7 @@ export default class DevServer extends Server { ...@@ -732,7 +732,7 @@ export default class DevServer extends Server {
}) { }) {
if (this.isRenderWorker) { if (this.isRenderWorker) {
await invokeIpcMethod({ await invokeIpcMethod({
hostname: this.hostname, fetchHostname: this.fetchHostname,
method: 'ensurePage', method: 'ensurePage',
args: [opts], args: [opts],
ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT, ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT,
...@@ -797,7 +797,7 @@ export default class DevServer extends Server { ...@@ -797,7 +797,7 @@ export default class DevServer extends Server {
protected async getFallbackErrorComponents(): Promise<LoadComponentsReturnType | null> { protected async getFallbackErrorComponents(): Promise<LoadComponentsReturnType | null> {
if (this.isRenderWorker) { if (this.isRenderWorker) {
await invokeIpcMethod({ await invokeIpcMethod({
hostname: this.hostname, fetchHostname: this.fetchHostname,
method: 'getFallbackErrorComponents', method: 'getFallbackErrorComponents',
args: [], args: [],
ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT, ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT,
...@@ -813,7 +813,7 @@ export default class DevServer extends Server { ...@@ -813,7 +813,7 @@ export default class DevServer extends Server {
async getCompilationError(page: string): Promise<any> { async getCompilationError(page: string): Promise<any> {
if (this.isRenderWorker) { if (this.isRenderWorker) {
const err = await invokeIpcMethod({ const err = await invokeIpcMethod({
hostname: this.hostname, fetchHostname: this.fetchHostname,
method: 'getCompilationError', method: 'getCompilationError',
args: [page], args: [page],
ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT, ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT,
......
import { isIPv6 } from './is-ipv6'
/**
* Formats a hostname so that it is a valid host that can be fetched by wrapping
* IPv6 hosts with brackets.
* @param hostname
* @returns
*/
export function formatHostname(hostname: string): string {
return isIPv6(hostname) ? `[${hostname}]` : hostname
}
// Regex from `node/lib/internal/net.js`: https://github.com/nodejs/node/blob/9fc57006c27564ed7f75eee090eca86786508f51/lib/internal/net.js#L19-L29
// License included below:
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
const v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
const v4Str = `(${v4Seg}[.]){3}${v4Seg}`
const v6Seg = '(?:[0-9a-fA-F]{1,4})'
const IPv6Reg = new RegExp(
'^(' +
`(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
`(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
`(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` +
`(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` +
`(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` +
`(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` +
`(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` +
`(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
')(%[0-9a-zA-Z-.:]{1,})?$'
)
export function isIPv6(s: string) {
return IPv6Reg.test(s)
}
...@@ -2,6 +2,7 @@ import type { RequestHandler } from '../next' ...@@ -2,6 +2,7 @@ import type { RequestHandler } from '../next'
// this must come first as it includes require hooks // this must come first as it includes require hooks
import { initializeServerWorker } from './setup-server-worker' import { initializeServerWorker } from './setup-server-worker'
import { formatHostname } from './format-hostname'
import next from '../next' import next from '../next'
import { PropagateToWorkersField } from './router-utils/types' import { PropagateToWorkersField } from './router-utils/types'
...@@ -98,7 +99,7 @@ export async function initialize(opts: { ...@@ -98,7 +99,7 @@ export async function initialize(opts: {
...opts, ...opts,
_routerWorker: opts.workerType === 'router', _routerWorker: opts.workerType === 'router',
_renderWorker: opts.workerType === 'render', _renderWorker: opts.workerType === 'render',
hostname: hostname === '0.0.0.0' ? 'localhost' : hostname, hostname,
customServer: false, customServer: false,
httpServer: server, httpServer: server,
port: opts.port, port: opts.port,
...@@ -111,7 +112,8 @@ export async function initialize(opts: { ...@@ -111,7 +112,8 @@ export async function initialize(opts: {
result = { result = {
port, port,
hostname: hostname === '0.0.0.0' ? '127.0.0.1' : hostname, hostname: formatHostname(hostname),
} }
return result return result
} }
...@@ -17,8 +17,10 @@ import { proxyRequest } from './router-utils/proxy-request' ...@@ -17,8 +17,10 @@ import { proxyRequest } from './router-utils/proxy-request'
import { getResolveRoutes } from './router-utils/resolve-routes' import { getResolveRoutes } from './router-utils/resolve-routes'
import { PERMANENT_REDIRECT_STATUS } from '../../shared/lib/constants' import { PERMANENT_REDIRECT_STATUS } from '../../shared/lib/constants'
import { splitCookiesString, toNodeOutgoingHttpHeaders } from '../web/utils' import { splitCookiesString, toNodeOutgoingHttpHeaders } from '../web/utils'
import { formatHostname } from './format-hostname'
import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-request' import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-request'
import { getMiddlewareRouteMatcher } from '../../shared/lib/router/utils/middleware-route-matcher' import { getMiddlewareRouteMatcher } from '../../shared/lib/router/utils/middleware-route-matcher'
import type { RenderWorker } from './router-server'
import { pipeReadable } from '../pipe-readable' import { pipeReadable } from '../pipe-readable'
type RouteResult = type RouteResult =
...@@ -56,7 +58,7 @@ export async function makeResolver( ...@@ -56,7 +58,7 @@ export async function makeResolver(
dir: string, dir: string,
nextConfig: NextConfigComplete, nextConfig: NextConfigComplete,
middleware: MiddlewareConfig, middleware: MiddlewareConfig,
serverAddr: Partial<ServerAddress> { hostname = 'localhost', port = 3000 }: Partial<ServerAddress>
) { ) {
const fsChecker = await setupFsCheck({ const fsChecker = await setupFsCheck({
dir, dir,
...@@ -68,6 +70,8 @@ export async function makeResolver( ...@@ -68,6 +70,8 @@ export async function makeResolver(
dir, dir,
!!nextConfig.experimental.appDir !!nextConfig.experimental.appDir
) )
// we format the hostname so that it can be fetched
const fetchHostname = formatHostname(hostname)
fsChecker.ensureCallback(async (item) => { fsChecker.ensureCallback(async (item) => {
let result: string | null = null let result: string | null = null
...@@ -108,7 +112,10 @@ export async function makeResolver( ...@@ -108,7 +112,10 @@ export async function makeResolver(
} }
: {} : {}
const middlewareServerPort = await new Promise((resolve) => { const middlewareServerAddr = await new Promise<{
hostname: string
port: number
}>((resolve) => {
const srv = http.createServer(async (req, res) => { const srv = http.createServer(async (req, res) => {
const cloneableBody = getCloneableBody(req) const cloneableBody = getCloneableBody(req)
try { try {
...@@ -128,9 +135,7 @@ export async function makeResolver( ...@@ -128,9 +135,7 @@ export async function makeResolver(
basePath: nextConfig.basePath, basePath: nextConfig.basePath,
trailingSlash: nextConfig.trailingSlash, trailingSlash: nextConfig.trailingSlash,
}, },
url: `http://${serverAddr.hostname || 'localhost'}:${ url: `http://${fetchHostname}:${port}${req.url}`,
serverAddr.port || 3000
}${req.url}`,
body: cloneableBody, body: cloneableBody,
signal: signalFromNodeResponse(res), signal: signalFromNodeResponse(res),
}, },
...@@ -172,7 +177,14 @@ export async function makeResolver( ...@@ -172,7 +177,14 @@ export async function makeResolver(
} }
}) })
srv.on('listening', () => { srv.on('listening', () => {
resolve((srv.address() as any).port) const srvAddr = srv.address()
if (!srvAddr || typeof srvAddr === 'string') {
throw new Error("Failed to determine middleware's host/port.")
}
resolve({
hostname: srvAddr.address,
port: srvAddr.port,
})
}) })
srv.listen(0) srv.listen(0)
}) })
...@@ -191,8 +203,8 @@ export async function makeResolver( ...@@ -191,8 +203,8 @@ export async function makeResolver(
nextConfig, nextConfig,
{ {
dir, dir,
port: serverAddr.port || 3000, port,
hostname: serverAddr.hostname, hostname,
isNodeDebugging: false, isNodeDebugging: false,
dev: true, dev: true,
workerType: 'render', workerType: 'render',
...@@ -201,15 +213,15 @@ export async function makeResolver( ...@@ -201,15 +213,15 @@ export async function makeResolver(
pages: { pages: {
async initialize() { async initialize() {
return { return {
port: middlewareServerPort, port: middlewareServerAddr.port,
hostname: '127.0.0.1', hostname: formatHostname(middlewareServerAddr.hostname),
} }
}, },
async deleteCache() {}, async deleteCache() {},
async clearModuleContext() {}, async clearModuleContext() {},
async deleteAppClientCache() {}, async deleteAppClientCache() {},
async propagateServerField() {}, async propagateServerField() {},
} as any, } as Partial<RenderWorker> as any,
}, },
{} as any {} as any
) )
......
...@@ -11,6 +11,7 @@ import { getCloneableBody } from '../../body-streams' ...@@ -11,6 +11,7 @@ import { getCloneableBody } from '../../body-streams'
import { filterReqHeaders, ipcForbiddenHeaders } from '../server-ipc/utils' import { filterReqHeaders, ipcForbiddenHeaders } from '../server-ipc/utils'
import { Header } from '../../../lib/load-custom-routes' import { Header } from '../../../lib/load-custom-routes'
import { stringifyQuery } from '../../server-route-utils' import { stringifyQuery } from '../../server-route-utils'
import { formatHostname } from '../format-hostname'
import { toNodeOutgoingHttpHeaders } from '../../web/utils' import { toNodeOutgoingHttpHeaders } from '../../web/utils'
import { invokeRequest } from '../server-ipc/invoke-request' import { invokeRequest } from '../server-ipc/invoke-request'
import { isAbortError } from '../../pipe-readable' import { isAbortError } from '../../pipe-readable'
...@@ -137,7 +138,9 @@ export function getResolveRoutes( ...@@ -137,7 +138,9 @@ export function getResolveRoutes(
const initUrl = (config.experimental as any).trustHostHeader const initUrl = (config.experimental as any).trustHostHeader
? `https://${req.headers.host || 'localhost'}${req.url}` ? `https://${req.headers.host || 'localhost'}${req.url}`
: opts.port : opts.port
? `${protocol}://${opts.hostname || 'localhost'}:${opts.port}${req.url}` ? `${protocol}://${formatHostname(opts.hostname || 'localhost')}:${
opts.port
}${req.url}`
: req.url || '' : req.url || ''
addRequestMeta(req, '__NEXT_INIT_URL', initUrl) addRequestMeta(req, '__NEXT_INIT_URL', initUrl)
......
...@@ -66,7 +66,7 @@ export async function createIpcServer( ...@@ -66,7 +66,7 @@ export async function createIpcServer(
) )
const ipcPort = await new Promise<number>((resolveIpc) => { const ipcPort = await new Promise<number>((resolveIpc) => {
ipcServer.listen(0, '0.0.0.0', () => { ipcServer.listen(0, server.hostname, () => {
const addr = ipcServer.address() const addr = ipcServer.address()
if (addr && typeof addr === 'object') { if (addr && typeof addr === 'object') {
......
...@@ -11,11 +11,6 @@ export const invokeRequest = async ( ...@@ -11,11 +11,6 @@ export const invokeRequest = async (
}, },
readableBody?: Readable | ReadableStream readableBody?: Readable | ReadableStream
) => { ) => {
// force to 127.0.0.1 as IPC always runs on this hostname
// to avoid localhost issues
const parsedTargetUrl = new URL(targetUrl)
parsedTargetUrl.hostname = '127.0.0.1'
const invokeHeaders = filterReqHeaders( const invokeHeaders = filterReqHeaders(
{ {
'cache-control': '', 'cache-control': '',
...@@ -24,7 +19,7 @@ export const invokeRequest = async ( ...@@ -24,7 +19,7 @@ export const invokeRequest = async (
ipcForbiddenHeaders ipcForbiddenHeaders
) as IncomingMessage['headers'] ) as IncomingMessage['headers']
return await fetch(parsedTargetUrl.toString(), { return await fetch(targetUrl, {
headers: invokeHeaders as any as Headers, headers: invokeHeaders as any as Headers,
method: requestInit.method, method: requestInit.method,
redirect: 'manual', redirect: 'manual',
......
...@@ -29,13 +29,13 @@ export const deserializeErr = (serializedErr: any) => { ...@@ -29,13 +29,13 @@ export const deserializeErr = (serializedErr: any) => {
} }
export async function invokeIpcMethod({ export async function invokeIpcMethod({
hostname = '127.0.0.1', fetchHostname = 'localhost',
method, method,
args, args,
ipcPort, ipcPort,
ipcKey, ipcKey,
}: { }: {
hostname?: string fetchHostname?: string
method: string method: string
args: any[] args: any[]
ipcPort?: string ipcPort?: string
...@@ -43,7 +43,7 @@ export async function invokeIpcMethod({ ...@@ -43,7 +43,7 @@ export async function invokeIpcMethod({
}): Promise<any> { }): Promise<any> {
if (ipcPort) { if (ipcPort) {
const res = await invokeRequest( const res = await invokeRequest(
`http://${hostname}:${ipcPort}?key=${ipcKey}&method=${ `http://${fetchHostname}:${ipcPort}?key=${ipcKey}&method=${
method as string method as string
}&args=${encodeURIComponent(JSON.stringify(args))}`, }&args=${encodeURIComponent(JSON.stringify(args))}`,
{ {
......
...@@ -87,25 +87,30 @@ export async function initializeServerWorker( ...@@ -87,25 +87,30 @@ export async function initializeServerWorker(
upgradeHandler(req, socket, upgrade) upgradeHandler(req, socket, upgrade)
}) })
} }
const hostname = let hostname = opts.hostname || 'localhost'
!opts.hostname || opts.hostname === 'localhost'
? '0.0.0.0'
: opts.hostname
server.on('listening', async () => { server.on('listening', async () => {
try { try {
const addr = server.address() const addr = server.address()
const host = addr
? typeof addr === 'object'
? addr.address
: addr
: undefined
const port = addr && typeof addr === 'object' ? addr.port : 0 const port = addr && typeof addr === 'object' ? addr.port : 0
if (!port) { if (!port || !host) {
console.error(`Invariant failed to detect render worker port`, addr) console.error(
`Invariant failed to detect render worker host/port`,
addr
)
process.exit(1) process.exit(1)
} }
resolve({ resolve({
server, server,
port, port,
hostname, hostname: host,
}) })
} catch (err) { } catch (err) {
return reject(err) return reject(err)
......
...@@ -3,10 +3,10 @@ import '../node-polyfill-fetch' ...@@ -3,10 +3,10 @@ import '../node-polyfill-fetch'
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import http from 'http' import http from 'http'
import { isIPv6 } from 'net'
import * as Log from '../../build/output/log' import * as Log from '../../build/output/log'
import setupDebug from 'next/dist/compiled/debug' import setupDebug from 'next/dist/compiled/debug'
import { getDebugPort } from './utils' import { getDebugPort } from './utils'
import { formatHostname } from './format-hostname'
import { initialize } from './router-server' import { initialize } from './router-server'
import { import {
WorkerRequestHandler, WorkerRequestHandler,
...@@ -147,25 +147,26 @@ export async function startServer({ ...@@ -147,25 +147,26 @@ export async function startServer({
} }
}) })
let targetHost = hostname
const isNodeDebugging = checkIsNodeDebugging() const isNodeDebugging = checkIsNodeDebugging()
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
server.on('listening', async () => { server.on('listening', async () => {
const addr = server.address() const addr = server.address()
port = typeof addr === 'object' ? addr?.port || port : port const actualHostname = formatHostname(
typeof addr === 'object'
let host = !hostname || hostname === '0.0.0.0' ? 'localhost' : hostname ? addr?.address || hostname || 'localhost'
: addr
)
const formattedHostname =
!hostname || hostname === '0.0.0.0'
? 'localhost'
: actualHostname === '[::]'
? '[::1]'
: actualHostname
let normalizedHostname = hostname || '0.0.0.0' port = typeof addr === 'object' ? addr?.port || port : port
const appUrl = `http://${formattedHostname}:${port}`
if (isIPv6(hostname)) {
host = host === '::' ? '[::1]' : `[${host}]`
normalizedHostname = `[${hostname}]`
}
targetHost = host
const appUrl = `http://${host}:${port}`
if (isNodeDebugging) { if (isNodeDebugging) {
const debugPort = getDebugPort() const debugPort = getDebugPort()
...@@ -177,11 +178,7 @@ export async function startServer({ ...@@ -177,11 +178,7 @@ export async function startServer({
} }
if (logReady) { if (logReady) {
Log.ready( Log.ready(`started server on ${actualHostname}:${port}, url: ${appUrl}`)
`started server on ${normalizedHostname}${
(port + '').startsWith(':') ? '' : ':'
}${port}, url: ${appUrl}`
)
// expose the main port to render workers // expose the main port to render workers
process.env.PORT = port + '' process.env.PORT = port + ''
} }
...@@ -202,7 +199,7 @@ export async function startServer({ ...@@ -202,7 +199,7 @@ export async function startServer({
dir, dir,
port, port,
isDev, isDev,
hostname: targetHost, hostname,
minimalMode, minimalMode,
isNodeDebugging: Boolean(isNodeDebugging), isNodeDebugging: Boolean(isNodeDebugging),
keepAliveTimeout, keepAliveTimeout,
...@@ -219,6 +216,6 @@ export async function startServer({ ...@@ -219,6 +216,6 @@ export async function startServer({
resolve() resolve()
}) })
server.listen(port, hostname === 'localhost' ? '0.0.0.0' : hostname) server.listen(port, hostname)
}) })
} }
...@@ -425,7 +425,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -425,7 +425,7 @@ export default class NextNodeServer extends BaseServer {
trustHostHeader: this.nextConfig.experimental.trustHostHeader, trustHostHeader: this.nextConfig.experimental.trustHostHeader,
allowedRevalidateHeaderKeys: allowedRevalidateHeaderKeys:
this.nextConfig.experimental.allowedRevalidateHeaderKeys, this.nextConfig.experimental.allowedRevalidateHeaderKeys,
hostname: this.hostname, hostname: this.fetchHostname,
minimalMode: this.minimalMode, minimalMode: this.minimalMode,
dev: this.renderOpts.dev === true, dev: this.renderOpts.dev === true,
query, query,
...@@ -515,7 +515,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -515,7 +515,7 @@ export default class NextNodeServer extends BaseServer {
if (this.isRenderWorker) { if (this.isRenderWorker) {
const invokeRes = await invokeRequest( const invokeRes = await invokeRequest(
`http://${this.hostname || '127.0.0.1'}:${this.port}${ `http://${this.fetchHostname || 'localhost'}:${this.port}${
newReq.url || '' newReq.url || ''
}`, }`,
{ {
...@@ -1496,7 +1496,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1496,7 +1496,7 @@ export default class NextNodeServer extends BaseServer {
const locale = params.parsed.query.__nextLocale const locale = params.parsed.query.__nextLocale
url = `${getRequestMeta(params.request, '_protocol')}://${ url = `${getRequestMeta(params.request, '_protocol')}://${
this.hostname this.fetchHostname
}:${this.port}${locale ? `/${locale}` : ''}${params.parsed.pathname}${ }:${this.port}${locale ? `/${locale}` : ''}${params.parsed.pathname}${
query ? `?${query}` : '' query ? `?${query}` : ''
}` }`
...@@ -1747,9 +1747,9 @@ export default class NextNodeServer extends BaseServer { ...@@ -1747,9 +1747,9 @@ export default class NextNodeServer extends BaseServer {
// When there are hostname and port we build an absolute URL // When there are hostname and port we build an absolute URL
const initUrl = const initUrl =
this.hostname && this.port this.fetchHostname && this.port
? `${protocol}://${this.hostname}:${this.port}${req.url}` ? `${protocol}://${this.fetchHostname}:${this.port}${req.url}`
: (this.nextConfig.experimental as any).trustHostHeader : this.nextConfig.experimental.trustHostHeader
? `https://${req.headers.host || 'localhost'}${req.url}` ? `https://${req.headers.host || 'localhost'}${req.url}`
: req.url : req.url
......
...@@ -20,7 +20,7 @@ interface Options { ...@@ -20,7 +20,7 @@ interface Options {
} }
const REGEX_LOCALHOST_HOSTNAME = const REGEX_LOCALHOST_HOSTNAME =
/(?!^https?:\/\/)(127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|::1|localhost)/ /(?!^https?:\/\/)(127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|\[::1\]|localhost)/
function parseURL(url: string | URL, base?: string | URL) { function parseURL(url: string | URL, base?: string | URL) {
return new URL( return new URL(
......
...@@ -67,7 +67,7 @@ if (!(globalThis as any).isNextStart) { ...@@ -67,7 +67,7 @@ if (!(globalThis as any).isNextStart) {
const appPort = await findPort() const appPort = await findPort()
server = await initNextServerScript( server = await initNextServerScript(
testServer, testServer,
/Listening on/, /ready started server on/,
{ {
...process.env, ...process.env,
PORT: appPort.toString(), PORT: appPort.toString(),
......
...@@ -363,7 +363,7 @@ describe('CLI Usage', () => { ...@@ -363,7 +363,7 @@ describe('CLI Usage', () => {
} }
) )
try { try {
await check(() => output, new RegExp(`on 0.0.0.0:${port}`)) await check(() => output, new RegExp(`on \\[::\\]:${port}`))
await check(() => output, new RegExp(`http://localhost:${port}`)) await check(() => output, new RegExp(`http://localhost:${port}`))
} finally { } finally {
await killApp(app) await killApp(app)
...@@ -383,12 +383,12 @@ describe('CLI Usage', () => { ...@@ -383,12 +383,12 @@ describe('CLI Usage', () => {
} }
) )
try { try {
await check(() => output, new RegExp(`on 0.0.0.0:${port}`)) await check(() => output, new RegExp(`on \\[::\\]:${port}`))
await check(() => output, new RegExp(`http://localhost:${port}`)) await check(() => output, new RegExp(`http://localhost:${port}`))
} finally { } finally {
await killApp(app) await killApp(app)
} }
const matches = /on 0.0.0.0:(\d+)/.exec(output) const matches = /on \[::\]:(\d+)/.exec(output)
expect(matches).not.toBe(null) expect(matches).not.toBe(null)
const _port = parseInt(matches[1]) const _port = parseInt(matches[1])
...@@ -408,8 +408,8 @@ describe('CLI Usage', () => { ...@@ -408,8 +408,8 @@ describe('CLI Usage', () => {
}, },
}) })
try { try {
await check(() => output, /on 0.0.0.0:(\d+)/) await check(() => output, /on \[::\]:(\d+)/)
const matches = /on 0.0.0.0:(\d+)/.exec(output) const matches = /on \[::\]:(\d+)/.exec(output)
const _port = parseInt(matches[1]) const _port = parseInt(matches[1])
expect(matches).not.toBe(null) expect(matches).not.toBe(null)
// Regression test: port 0 was interpreted as if no port had been // Regression test: port 0 was interpreted as if no port had been
...@@ -434,7 +434,7 @@ describe('CLI Usage', () => { ...@@ -434,7 +434,7 @@ describe('CLI Usage', () => {
} }
) )
try { try {
await check(() => output, new RegExp(`on 0.0.0.0:${port}`)) await check(() => output, new RegExp(`on \\[::\\]:${port}`))
await check(() => output, new RegExp(`http://localhost:${port}`)) await check(() => output, new RegExp(`http://localhost:${port}`))
} finally { } finally {
await killApp(app) await killApp(app)
...@@ -451,7 +451,7 @@ describe('CLI Usage', () => { ...@@ -451,7 +451,7 @@ describe('CLI Usage', () => {
env: { NODE_OPTIONS: '--inspect' }, env: { NODE_OPTIONS: '--inspect' },
}) })
try { try {
await check(() => output, new RegExp(`on 0.0.0.0:${port}`)) await check(() => output, new RegExp(`on \\[::\\]:${port}`))
await check(() => output, new RegExp(`http://localhost:${port}`)) await check(() => output, new RegExp(`http://localhost:${port}`))
} finally { } finally {
await killApp(app) await killApp(app)
......
...@@ -8,7 +8,6 @@ import { ...@@ -8,7 +8,6 @@ import {
} from 'fs' } from 'fs'
import { promisify } from 'util' import { promisify } from 'util'
import http from 'http' import http from 'http'
import https from 'https'
import path from 'path' import path from 'path'
import spawn from 'cross-spawn' import spawn from 'cross-spawn'
...@@ -162,19 +161,7 @@ export function fetchViaHTTP( ...@@ -162,19 +161,7 @@ export function fetchViaHTTP(
opts?: RequestInit opts?: RequestInit
): Promise<Response> { ): Promise<Response> {
const url = query ? withQuery(pathname, query) : pathname const url = query ? withQuery(pathname, query) : pathname
return fetch(getFullUrl(appPort, url), { return fetch(getFullUrl(appPort, url), opts)
// in node.js v17 fetch favors IPv6 but Next.js is
// listening on IPv4 by default so force IPv4 DNS resolving
agent: (parsedUrl) => {
if (parsedUrl.protocol === 'https:') {
return new https.Agent({ family: 4 })
}
if (parsedUrl.protocol === 'http:') {
return new http.Agent({ family: 4 })
}
},
...opts,
})
} }
export function renderViaHTTP( export function renderViaHTTP(
......
...@@ -103,7 +103,7 @@ describe('pnpm support', () => { ...@@ -103,7 +103,7 @@ describe('pnpm support', () => {
) )
server = await initNextServerScript( server = await initNextServerScript(
path.join(standaloneDir, 'server.js'), path.join(standaloneDir, 'server.js'),
/Listening/, /ready started server on/,
{ {
...process.env, ...process.env,
PORT: appPort, PORT: appPort,
......
...@@ -76,7 +76,7 @@ describe('should set-up next', () => { ...@@ -76,7 +76,7 @@ describe('should set-up next', () => {
appPort = await findPort() appPort = await findPort()
server = await initNextServerScript( server = await initNextServerScript(
testServer, testServer,
/Listening on/, /ready started server on/,
{ {
...process.env, ...process.env,
PORT: appPort, PORT: appPort,
......
...@@ -112,7 +112,7 @@ describe('should set-up next', () => { ...@@ -112,7 +112,7 @@ describe('should set-up next', () => {
appPort = await findPort() appPort = await findPort()
server = await initNextServerScript( server = await initNextServerScript(
testServer, testServer,
/Listening on/, /ready started server on/,
{ {
...process.env, ...process.env,
PORT: appPort, PORT: appPort,
......
...@@ -119,7 +119,7 @@ describe('should set-up next', () => { ...@@ -119,7 +119,7 @@ describe('should set-up next', () => {
appPort = await findPort() appPort = await findPort()
server = await initNextServerScript( server = await initNextServerScript(
testServer, testServer,
/Listening on/, /ready started server on/,
{ {
...process.env, ...process.env,
PORT: appPort, PORT: appPort,
...@@ -1289,7 +1289,7 @@ describe('should set-up next', () => { ...@@ -1289,7 +1289,7 @@ describe('should set-up next', () => {
appPort = await findPort() appPort = await findPort()
server = await initNextServerScript( server = await initNextServerScript(
testServer, testServer,
/Listening on/, /ready started server on/,
{ {
...process.env, ...process.env,
PORT: appPort, PORT: appPort,
......
...@@ -63,7 +63,7 @@ describe('minimal-mode-response-cache', () => { ...@@ -63,7 +63,7 @@ describe('minimal-mode-response-cache', () => {
appPort = await findPort() appPort = await findPort()
server = await initNextServerScript( server = await initNextServerScript(
testServer, testServer,
/Listening on/, /ready started server on/,
{ {
...process.env, ...process.env,
HOSTNAME: '', HOSTNAME: '',
...@@ -141,9 +141,12 @@ describe('minimal-mode-response-cache', () => { ...@@ -141,9 +141,12 @@ describe('minimal-mode-response-cache', () => {
expect(res2.headers.get('content-type')).toContain('text/html') expect(res2.headers.get('content-type')).toContain('text/html')
}) })
it('should have correct "Listening on" log', async () => { it('should have correct "Started server on" log', async () => {
expect(output).toContain(`Listening on port`) expect(output).toContain(`started server on`)
expect(output).toContain(`url: http://localhost:${appPort}`) let pattern = new RegExp(
`url: http://localhost:${appPort}|url: http://127.0.0.1:${appPort}|url: http://\\[::1\\]:${appPort}`
)
expect(output).toMatch(pattern)
}) })
it('should have correct responses', async () => { it('should have correct responses', async () => {
......
...@@ -43,7 +43,7 @@ describe('type-module', () => { ...@@ -43,7 +43,7 @@ describe('type-module', () => {
const appPort = await findPort() const appPort = await findPort()
const server = await initNextServerScript( const server = await initNextServerScript(
serverFile, serverFile,
/Listening on/, /ready started server on/,
{ ...process.env, PORT: appPort.toString() }, { ...process.env, PORT: appPort.toString() },
undefined, undefined,
{ cwd: next.testDir } { cwd: next.testDir }
......
...@@ -197,12 +197,12 @@ it('consider 127.0.0.1 and variations as localhost', () => { ...@@ -197,12 +197,12 @@ it('consider 127.0.0.1 and variations as localhost', () => {
const httpUrl = new NextURL('http://localhost:3000/hello') const httpUrl = new NextURL('http://localhost:3000/hello')
expect(new NextURL('http://127.0.0.1:3000/hello')).toStrictEqual(httpUrl) expect(new NextURL('http://127.0.0.1:3000/hello')).toStrictEqual(httpUrl)
expect(new NextURL('http://127.0.1.0:3000/hello')).toStrictEqual(httpUrl) expect(new NextURL('http://127.0.1.0:3000/hello')).toStrictEqual(httpUrl)
expect(new NextURL('http://::1:3000/hello')).toStrictEqual(httpUrl) expect(new NextURL('http://[::1]:3000/hello')).toStrictEqual(httpUrl)
const httpsUrl = new NextURL('https://localhost:3000/hello') const httpsUrl = new NextURL('https://localhost:3000/hello')
expect(new NextURL('https://127.0.0.1:3000/hello')).toStrictEqual(httpsUrl) expect(new NextURL('https://127.0.0.1:3000/hello')).toStrictEqual(httpsUrl)
expect(new NextURL('https://127.0.1.0:3000/hello')).toStrictEqual(httpsUrl) expect(new NextURL('https://127.0.1.0:3000/hello')).toStrictEqual(httpsUrl)
expect(new NextURL('https://::1:3000/hello')).toStrictEqual(httpsUrl) expect(new NextURL('https://[::1]:3000/hello')).toStrictEqual(httpsUrl)
}) })
it('allows to change the port', () => { it('allows to change the port', () => {
......
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