Skip to content

Commit

Permalink
fix: correctly handle wildcard hosts in preview (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Jan 22, 2025
1 parent af69a12 commit ea4297b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-islands-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---

Fixes a bug that caused the preview server to ignore wildcard host options
14 changes: 6 additions & 8 deletions packages/node/src/log-listening-on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import os from 'node:os';
import type { AstroIntegrationLogger } from 'astro';
import type { Options } from './types.js';

const wildcardHosts = new Set(['0.0.0.0', '::', '0000:0000:0000:0000:0000:0000:0000:0000']);

export async function logListeningOn(
logger: AstroIntegrationLogger,
server: http.Server | https.Server,
options: Pick<Options, 'host'>
configuredHost: string | boolean | undefined
) {
await new Promise<void>((resolve) => server.once('listening', resolve));
const protocol = server instanceof https.Server ? 'https' : 'http';
// Allow to provide host value at runtime
const host = getResolvedHostForHttpServer(
process.env.HOST !== undefined && process.env.HOST !== '' ? process.env.HOST : options.host
);
const host = getResolvedHostForHttpServer(configuredHost);
const { port } = server.address() as AddressInfo;
const address = getNetworkAddress(protocol, host, port);

if (host === undefined) {
if (host === undefined || wildcardHosts.has(host)) {
logger.info(
`Server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n`
);
Expand All @@ -28,7 +28,7 @@ export async function logListeningOn(
}
}

function getResolvedHostForHttpServer(host: string | boolean) {
function getResolvedHostForHttpServer(host: string | boolean | undefined) {
if (host === false) {
// Use a secure default
return 'localhost';
Expand All @@ -47,8 +47,6 @@ interface NetworkAddressOpt {
network: string[];
}

const wildcardHosts = new Set(['0.0.0.0', '::', '0000:0000:0000:0000:0000:0000:0000:0000']);

// this code from vite https://github.com/vitejs/vite/blob/d09bbd093a4b893e78f0bbff5b17c7cf7821f403/packages/vite/src/node/utils.ts#L892-L914
export function getNetworkAddress(
// biome-ignore lint/style/useDefaultParameterLast: <explanation>
Expand Down
10 changes: 8 additions & 2 deletions packages/node/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ const createPreviewServer: CreatePreviewServer = async (preview) => {
throw err;
}
}
const host = preview.host ?? 'localhost';
// If the user didn't specify a host, it will already have been defaulted to
// "localhost" by getResolvedHostForHttpServer in astro core/preview/util.ts.
// The value `undefined` actually means that either the user set `options.server.host`
// to `true`, or they passed `--host` without an argument. In that case, we
// should listen on all IPs.
const host = process.env.HOST ?? preview.host ?? '0.0.0.0';

const port = preview.port ?? 4321;
const server = createServer(ssrHandler, host, port);

Expand All @@ -51,7 +57,7 @@ const createPreviewServer: CreatePreviewServer = async (preview) => {
});
}

logListeningOn(preview.logger, server.server, options);
logListeningOn(preview.logger, server.server, host);
await new Promise<void>((resolve, reject) => {
server.server.once('listening', resolve);
server.server.once('error', reject);
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function standalone(app: NodeApp, options: Options) {
const server = createServer(handler, host, port);
server.server.listen(port, host);
if (process.env.ASTRO_NODE_LOGGING !== 'disabled') {
logListeningOn(app.getAdapterLogger(), server.server, options);
logListeningOn(app.getAdapterLogger(), server.server, host);
}
return {
server,
Expand Down

0 comments on commit ea4297b

Please sign in to comment.