Compare commits

...

18 commits

Author SHA1 Message Date
Nate Moore
1244e6fee1 chore: format 2023-08-10 13:26:46 -04:00
Nate Moore
6b0e3b22f5 chore: add changeset 2023-08-10 13:25:33 -04:00
Nate Moore
70fd552e88 chore: format 2023-08-10 13:23:34 -04:00
Nate Moore
0093d426c6 fix: rebase issues 2023-08-10 13:20:58 -04:00
wuls
36c5ab3140 optimize test 2023-08-10 13:15:33 -04:00
wuls
12c0b43e42 Optimizing code based on the comments 2023-08-10 13:15:17 -04:00
wulinsheng123
876d19b6d6 fix test error 2023-08-10 13:14:20 -04:00
wuls
c7277100ec test 2023-08-10 13:14:20 -04:00
wuls
41cd7bb939 test 2023-08-10 13:14:20 -04:00
wuls
ae8c22cfb2 test 2023-08-10 13:14:20 -04:00
wuls
f346ae2827 fix test error 2023-08-10 13:14:20 -04:00
wuls
234b04252a fix test 2023-08-10 13:14:20 -04:00
wuls
5030094099 test 2023-08-10 13:14:20 -04:00
wuls
0e1e20b817 delect white space 2023-08-10 13:14:20 -04:00
wuls
f257895e43 optimism 2023-08-10 13:14:19 -04:00
wuls
31f822e27d sumbit test code 2023-08-10 13:13:51 -04:00
wuls
bb0b0169c7 fix bug 2023-08-10 13:13:51 -04:00
wuls
9b33014958 supporting a network address access a website when an user set host = true in Node environment 2023-08-10 13:13:34 -04:00
4 changed files with 72 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---
Support the `--host` flag when running the standalone server (also works for `astro preview --host`)

View file

@ -0,0 +1,47 @@
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
}

View file

@ -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 ({
@ -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;
};

View file

@ -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) {
@ -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,