From 8fe2eadd4355e68fd2060b05851180056b5f47b4 Mon Sep 17 00:00:00 2001 From: Ward Werbrouck <34573943+kletse@users.noreply.github.com> Date: Wed, 1 Nov 2023 13:46:40 +0100 Subject: [PATCH 1/4] improved network url in (dev) cli --- packages/next/src/lib/get-network-host.ts | 36 ++++++++++++++++++++ packages/next/src/server/lib/app-info-log.ts | 4 ++- packages/next/src/server/lib/start-server.ts | 10 ++++-- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 packages/next/src/lib/get-network-host.ts diff --git a/packages/next/src/lib/get-network-host.ts b/packages/next/src/lib/get-network-host.ts new file mode 100644 index 0000000000000..dda582ae57f20 --- /dev/null +++ b/packages/next/src/lib/get-network-host.ts @@ -0,0 +1,36 @@ +import os from 'os' + +function getNetworkHosts(family: 'IPv4' | 'IPv6'): string[] { + const interfaces = os.networkInterfaces() + const hosts: string[] = [] + + Object.keys(interfaces).forEach((key) => { + interfaces[key] + ?.filter((networkInterface) => { + switch (networkInterface.family) { + case 'IPv6': + return ( + family === 'IPv6' && + networkInterface.scopeid === 0 && + networkInterface.address !== '::1' + ) + case 'IPv4': + return family === 'IPv4' && networkInterface.address !== '127.0.0.1' + default: + return false + } + }) + .forEach((networkInterface) => { + if (networkInterface.address) { + hosts.push(networkInterface.address) + } + }) + }) + + return hosts +} + +export function getNetworkHost(family: 'IPv4' | 'IPv6'): string | null { + const hosts = getNetworkHosts(family) + return hosts[0] ?? null +} diff --git a/packages/next/src/server/lib/app-info-log.ts b/packages/next/src/server/lib/app-info-log.ts index fb94929350644..e400f41978443 100644 --- a/packages/next/src/server/lib/app-info-log.ts +++ b/packages/next/src/server/lib/app-info-log.ts @@ -31,7 +31,9 @@ export function logStartInfo({ if (networkUrl) { Log.bootstrap(`- Network: ${networkUrl}`) } - if (envInfo?.length) Log.bootstrap(`- Environments: ${envInfo.join(', ')}`) + if (envInfo?.length) { + Log.bootstrap(` - Environments: ${envInfo.join(', ')}`) + } if (expFeatureInfo?.length) { Log.bootstrap(`- Experiments (use with caution):`) diff --git a/packages/next/src/server/lib/start-server.ts b/packages/next/src/server/lib/start-server.ts index 7f21d0a5e1477..7848bac13ac58 100644 --- a/packages/next/src/server/lib/start-server.ts +++ b/packages/next/src/server/lib/start-server.ts @@ -1,3 +1,5 @@ +import { getNetworkHost } from '../../lib/get-network-host' + if (performance.getEntriesByName('next-start').length === 0) { performance.mark('next-start') } @@ -29,6 +31,7 @@ import { getStartServerInfo, logStartInfo } from './app-info-log' import { validateTurboNextConfig } from '../../lib/turbopack-warning' import { type Span, trace, flushAllTraces } from '../../trace' import { isPostpone } from './router-utils/is-postpone' +import { isIPv6 } from './is-ipv6' const debug = setupDebug('next:start-server') let startServerSpan: Span | undefined @@ -234,9 +237,12 @@ export async function startServer( port = typeof addr === 'object' ? addr?.port || port : port - const networkUrl = hostname - ? `${selfSignedCertificate ? 'https' : 'http'}://${actualHostname}:${port}` + const networkHostname = + hostname ?? getNetworkHost(isIPv6(actualHostname) ? 'IPv6' : 'IPv4') + const networkUrl = networkHostname + ? `http://${formatHostname(networkHostname)}:${port}` : null + const appUrl = `${ selfSignedCertificate ? 'https' : 'http' }://${formattedHostname}:${port}` From ae105499c6002fd8050215520de7eaf7d6d6c3cf Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 12 Nov 2024 13:27:07 -0800 Subject: [PATCH 2/4] add test case --- packages/next/src/server/lib/app-info-log.ts | 4 +--- test/integration/cli/test/index.test.js | 8 ++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/next/src/server/lib/app-info-log.ts b/packages/next/src/server/lib/app-info-log.ts index e400f41978443..85fac2bdc19c9 100644 --- a/packages/next/src/server/lib/app-info-log.ts +++ b/packages/next/src/server/lib/app-info-log.ts @@ -31,9 +31,7 @@ export function logStartInfo({ if (networkUrl) { Log.bootstrap(`- Network: ${networkUrl}`) } - if (envInfo?.length) { - Log.bootstrap(` - Environments: ${envInfo.join(', ')}`) - } + if (envInfo?.length) Log.bootstrap(` - Environments: ${envInfo.join(', ')}`) if (expFeatureInfo?.length) { Log.bootstrap(`- Experiments (use with caution):`) diff --git a/test/integration/cli/test/index.test.js b/test/integration/cli/test/index.test.js index a18c3a3e778c3..4aafaee5a46ac 100644 --- a/test/integration/cli/test/index.test.js +++ b/test/integration/cli/test/index.test.js @@ -476,6 +476,10 @@ describe('CLI Usage', () => { ) try { await check(() => output, new RegExp(`http://localhost:${port}`)) + await check( + () => output, + /Network:\s*http:\/\/[\d]{1,}\.[\d]{1,}\.[\d]{1,}/ + ) } finally { await killApp(app) } @@ -495,6 +499,10 @@ describe('CLI Usage', () => { ) try { await check(() => output, new RegExp(`http://localhost:${port}`)) + await check( + () => output, + /Network:\s*http:\/\/[\d]{1,}\.[\d]{1,}\.[\d]{1,}/ + ) } finally { await killApp(app) } From 1a57e1b967f740f9d05c861b7e4f9806c4671870 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 12 Nov 2024 14:04:01 -0800 Subject: [PATCH 3/4] update protocol --- packages/next/src/server/lib/start-server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/src/server/lib/start-server.ts b/packages/next/src/server/lib/start-server.ts index 7848bac13ac58..8e7f1c6ffac2e 100644 --- a/packages/next/src/server/lib/start-server.ts +++ b/packages/next/src/server/lib/start-server.ts @@ -240,7 +240,7 @@ export async function startServer( const networkHostname = hostname ?? getNetworkHost(isIPv6(actualHostname) ? 'IPv6' : 'IPv4') const networkUrl = networkHostname - ? `http://${formatHostname(networkHostname)}:${port}` + ? `${selfSignedCertificate ? 'https' : 'http'}://${formatHostname(networkHostname)}:${port}` : null const appUrl = `${ From a9386a21e08ab679f4a027be9b022965e6ed5c62 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Tue, 12 Nov 2024 14:07:01 -0800 Subject: [PATCH 4/4] nit re-use protocol --- packages/next/src/server/lib/start-server.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/next/src/server/lib/start-server.ts b/packages/next/src/server/lib/start-server.ts index 8e7f1c6ffac2e..fcd7fd338eba7 100644 --- a/packages/next/src/server/lib/start-server.ts +++ b/packages/next/src/server/lib/start-server.ts @@ -239,13 +239,14 @@ export async function startServer( const networkHostname = hostname ?? getNetworkHost(isIPv6(actualHostname) ? 'IPv6' : 'IPv4') + + const protocol = selfSignedCertificate ? 'https' : 'http' + const networkUrl = networkHostname - ? `${selfSignedCertificate ? 'https' : 'http'}://${formatHostname(networkHostname)}:${port}` + ? `${protocol}://${formatHostname(networkHostname)}:${port}` : null - const appUrl = `${ - selfSignedCertificate ? 'https' : 'http' - }://${formattedHostname}:${port}` + const appUrl = `${protocol}://${formattedHostname}:${port}` if (nodeDebugType) { const formattedDebugAddress = getFormattedDebugAddress()