diff --git a/packages/integrations/node/package.json b/packages/integrations/node/package.json index d0c660a35..3306b2e57 100644 --- a/packages/integrations/node/package.json +++ b/packages/integrations/node/package.json @@ -30,7 +30,6 @@ "build": "astro-scripts build \"src/**/*.ts\" && tsc", "build:ci": "astro-scripts build \"src/**/*.ts\"", "dev": "astro-scripts dev \"src/**/*.ts\"", - "t": "mocha --exit --timeout 20000 test/url-protocol.test.js", "test": "mocha --exit --timeout 20000 test/" }, "dependencies": { diff --git a/packages/integrations/node/src/get-network-address.ts b/packages/integrations/node/src/get-network-address.ts index ff017b2e0..3c1a8e691 100644 --- a/packages/integrations/node/src/get-network-address.ts +++ b/packages/integrations/node/src/get-network-address.ts @@ -1,13 +1,48 @@ import os from 'os' -export function getNetworkAddress(){ - const interfaces = os.networkInterfaces(); - const ips: string[] = []; - Object.keys(interfaces).forEach((nic) => { - interfaces[nic]!.forEach((details) => { - if (details.family === 'IPv4' && !details.internal) { - ips.push(details.address); - } - }); - }); - return ips?.[0] ? ips?.[0] : '127.0.0.1'; +interface ReturnValOpt { + local: string[] + network: string[] } + +const wildcardHosts = new Set([ + '0.0.0.0', + '::', + '0000:0000:0000:0000:0000:0000:0000:0000', +]) +type Protocol = 'http' | 'https' + +export function getNetworkAddress(protocol: Protocol = 'http', hostname: string, port:number, base?:string) { + + const returnVal:ReturnValOpt = { + local: [], + network: [] + } + + Object.values(os.networkInterfaces()) + .flatMap((nInterface) => nInterface ?? []) + .filter( + (detail) => + detail && + detail.address && + (detail.family === 'IPv4' || + // @ts-expect-error Node 18.0 - 18.3 returns number + detail.family === 4), + ) + .forEach((detail) => { + let host = detail.address.replace('127.0.0.1', hostname === undefined || wildcardHosts.has(hostname) ? 'localhost' : hostname) + // ipv6 host + if (host.includes(':')) { + host = `[${host}]` + } + const url = `${protocol}://${host}:${port}${base ? base : ''}` + if (detail.address.includes('127.0.0.1')) { + returnVal.local.push(url) + } else { + returnVal.network.push(url) + } + }) + return returnVal +} + + + diff --git a/packages/integrations/node/src/preview.ts b/packages/integrations/node/src/preview.ts index 7ef584c81..aade70012 100644 --- a/packages/integrations/node/src/preview.ts +++ b/packages/integrations/node/src/preview.ts @@ -59,9 +59,6 @@ const preview: CreatePreviewServer = async function ({ return pathname; } - if (!host){ - host = getNetworkAddress() - } const server = createServer( { @@ -72,9 +69,23 @@ const preview: CreatePreviewServer = async function ({ }, handler ); + const address = getNetworkAddress('http', host!, port) + + if(host === undefined ){ + // eslint-disable-next-line no-console + console.log( + `Preview server listening on + \t + local: ${address.local[0]} + \t + network: ${address.network[0]} + ` ); + }else{ + // eslint-disable-next-line no-console + console.log(`Preview server listening on \t + ${address.local[0]}`); + } - // eslint-disable-next-line no-console - console.log(`Preview server listening on http://${host}:${port}`); return server; }; diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts index 815640fc3..d6fe0a195 100644 --- a/packages/integrations/node/src/standalone.ts +++ b/packages/integrations/node/src/standalone.ts @@ -40,11 +40,14 @@ export default function startServer(app: NodeApp, options: Options) { const port = process.env.PORT ? Number(process.env.PORT) : options.port ?? 8080; const { client } = resolvePaths(options); const handler = middleware(app, options.mode); - // Allow to provide host value at runtime const host = getResolvedHostForHttpServer( process.env.HOST !== undefined && process.env.HOST !== '' ? process.env.HOST : options.host ); + + + + const server = createServer( { client, @@ -54,11 +57,23 @@ export default function startServer(app: NodeApp, options: Options) { }, handler ); - const protocol = server.server instanceof https.Server ? 'https' : 'http'; + const address = getNetworkAddress(protocol, host!, port) - // eslint-disable-next-line no-console - console.log(`Server listening on ${protocol}://${host}:${port}`); + if(host === undefined ){ + // eslint-disable-next-line no-console + console.log( + `Preview server listening on + \t + local: ${address.local[0]} + \t + network: ${address.network[0]} + ` ); + }else{ + // eslint-disable-next-line no-console + console.log(`Preview server listening on \t + ${address.local[0]}`); + } return { server, diff --git a/packages/integrations/node/test/fixtures/url-protocol/package.json b/packages/integrations/node/test/fixtures/url-protocol/package.json index d894d0718..4b0775716 100644 --- a/packages/integrations/node/test/fixtures/url-protocol/package.json +++ b/packages/integrations/node/test/fixtures/url-protocol/package.json @@ -5,9 +5,5 @@ "dependencies": { "astro": "workspace:*", "@astrojs/node": "workspace:*" - }, - "scripts": { - "build": "astro build", - "preview": "astro preview" } } diff --git a/packages/integrations/node/test/url-protocol.test.js b/packages/integrations/node/test/url-protocol.test.js index 0834b0882..3ee091221 100644 --- a/packages/integrations/node/test/url-protocol.test.js +++ b/packages/integrations/node/test/url-protocol.test.js @@ -1,7 +1,6 @@ import { expect } from 'chai'; -import { TLSSocket } from 'node:tls'; -import nodejs from '../dist/index.js'; -import { createRequestAndResponse, loadFixture } from './test-utils.js'; +import { getNetworkAddress } from '../dist/get-network-address.js' +import { fetch } from 'undici'; describe('URL protocol', () => { /** @type {import('./test-utils').Fixture} */ @@ -26,10 +25,10 @@ describe('URL protocol', () => { devPreview = await fixture.preview(); }); - it('test host is true ', () => { - const host = devPreview.host - const ishost = () => host.startsWith('127') || host.startsWith('loc') - expect(!ishost()).eq(true) + it('test host is true ', async () => { + const address = getNetworkAddress('http', undefined, 3000) + const res = await fetch(address.network[0]) + expect(res.status).to.equal(200); }); }) it('return http when non-secure', async () => {