Skip to content

Commit

Permalink
Support --host for Node adapter preview (#6928)
Browse files Browse the repository at this point in the history
* supporting a network address access a website when an user set host = true in Node environment

* fix bug

* sumbit test code

* optimism

* delect white space

* test

* fix test

* fix test error

* test

* test

* test

* fix test error

* Optimizing code based on the comments

* optimize test

* fix: rebase issues

* chore: format

* chore: add changeset

* chore: format

* chore: format

* chore: lint

---------

Co-authored-by: wuls <[email protected]>
Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
3 people authored Aug 10, 2023
1 parent f7a901e commit b16cb78
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-spoons-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---

Support the `--host` flag when running the standalone server (also works for `astro preview --host`)
44 changes: 44 additions & 0 deletions packages/integrations/node/src/get-network-address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os from 'os'
interface NetworkAddressOpt {
local: string[]
network: string[]
}

const wildcardHosts = new Set([
'0.0.0.0',
'::',
'0000:0000:0000:0000:0000:0000:0000:0000',
])
type Protocol = 'http' | 'https'

// this code from vite https://github.com/vitejs/vite/blob/d09bbd093a4b893e78f0bbff5b17c7cf7821f403/packages/vite/src/node/utils.ts#L892-L914
export function getNetworkAddress(protocol: Protocol = 'http', hostname: string | undefined, port: number, base?: string) {
const NetworkAddress: NetworkAddressOpt = {
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')) {
NetworkAddress.local.push(url)
} else {
NetworkAddress.network.push(url)
}
})
return NetworkAddress
}
12 changes: 10 additions & 2 deletions packages/integrations/node/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { CreatePreviewServer } from 'astro';
import type http from 'node:http';
import { fileURLToPath } from 'node:url';
import { createServer } from './http-server.js';
import { getNetworkAddress } from './get-network-address.js'
import type { createExports } from './server';

const preview: CreatePreviewServer = async function ({
Expand Down Expand Up @@ -67,9 +68,16 @@ 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 \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n`);
} else {
// eslint-disable-next-line no-console
console.log(`Preview server listening on ${address.local[0]}`);
}

// eslint-disable-next-line no-console
console.log(`Preview server listening on http://${host}:${port}`);

return server;
};
Expand Down
12 changes: 10 additions & 2 deletions packages/integrations/node/src/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -55,9 +56,16 @@ export default function startServer(app: NodeApp, options: Options) {
);

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 \n local: ${address.local[0]} \t\n network: ${address.network[0]}\n`);
} else {
// eslint-disable-next-line no-console
console.log(`Preview server listening on ${address.local[0]}`);
}

return {
server,
Expand Down

0 comments on commit b16cb78

Please sign in to comment.