diff --git a/.changeset/soft-pianos-happen.md b/.changeset/soft-pianos-happen.md new file mode 100644 index 000000000..3a6400297 --- /dev/null +++ b/.changeset/soft-pianos-happen.md @@ -0,0 +1,6 @@ +--- +'astro': minor +'@astrojs/node': minor +--- + +supporting a network address access a website when an user set host = true in Node environment diff --git a/packages/astro/src/core/preview/get-network-address.ts b/packages/astro/src/core/preview/get-network-address.ts new file mode 100644 index 000000000..ff017b2e0 --- /dev/null +++ b/packages/astro/src/core/preview/get-network-address.ts @@ -0,0 +1,13 @@ +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'; +} diff --git a/packages/astro/src/core/preview/util.ts b/packages/astro/src/core/preview/util.ts index d02e4c3c8..7a12417f3 100644 --- a/packages/astro/src/core/preview/util.ts +++ b/packages/astro/src/core/preview/util.ts @@ -1,10 +1,12 @@ +import { getNetworkAddress } from './get-network-address.js' + export function getResolvedHostForHttpServer(host: string | boolean) { if (host === false) { // Use a secure default return 'localhost'; } else if (host === true) { // If passed --host in the CLI without arguments - return undefined; // undefined typically means 0.0.0.0 or :: (listen on all IPs) + return getNetworkAddress(); // undefined typically means 0.0.0.0 or :: (listen on all IPs) } else { return host; } @@ -17,3 +19,4 @@ export function stripBase(path: string, base: string): string { const baseWithSlash = base.endsWith('/') ? base : base + '/'; return path.replace(RegExp('^' + baseWithSlash), '/'); } + diff --git a/packages/integrations/node/src/get-network-address.ts b/packages/integrations/node/src/get-network-address.ts new file mode 100644 index 000000000..ff017b2e0 --- /dev/null +++ b/packages/integrations/node/src/get-network-address.ts @@ -0,0 +1,13 @@ +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'; +} diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts index 68b2cebcd..c573fcf5d 100644 --- a/packages/integrations/node/src/standalone.ts +++ b/packages/integrations/node/src/standalone.ts @@ -4,6 +4,7 @@ import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { createServer } from './http-server.js'; import middleware from './nodeMiddleware.js'; +import { getNetworkAddress } from './get-network-address.js' import type { Options } from './types'; function resolvePaths(options: Options) { @@ -29,7 +30,7 @@ export function getResolvedHostForHttpServer(host: string | boolean) { return '127.0.0.1'; } else if (host === true) { // If passed --host in the CLI without arguments - return undefined; // undefined typically means 0.0.0.0 or :: (listen on all IPs) + return getNetworkAddress(); // undefined typically means 0.0.0.0 or :: (listen on all IPs) } else { return host; } diff --git a/packages/integrations/node/test/url-protocol.test.js b/packages/integrations/node/test/url-protocol.test.js index a83cb2a41..0834b0882 100644 --- a/packages/integrations/node/test/url-protocol.test.js +++ b/packages/integrations/node/test/url-protocol.test.js @@ -9,6 +9,9 @@ describe('URL protocol', () => { before(async () => { fixture = await loadFixture({ + server: { + host: true + }, root: './fixtures/url-protocol/', output: 'server', adapter: nodejs({ mode: 'standalone' }), @@ -16,6 +19,19 @@ describe('URL protocol', () => { await fixture.build(); }); + describe('test preview when host is true', async () => { + let devPreview; + + before(async () => { + 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('return http when non-secure', async () => { const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs'); let { req, res, text } = createRequestAndResponse({