-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
--host
for Node adapter preview (#6928)
* 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
1 parent
f7a901e
commit b16cb78
Showing
4 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters