supporting a network address access a website when an user set host = true in Node environment
This commit is contained in:
parent
f7a901e7c4
commit
9b33014958
6 changed files with 54 additions and 2 deletions
6
.changeset/soft-pianos-happen.md
Normal file
6
.changeset/soft-pianos-happen.md
Normal file
|
@ -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
|
13
packages/astro/src/core/preview/get-network-address.ts
Normal file
13
packages/astro/src/core/preview/get-network-address.ts
Normal file
|
@ -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';
|
||||
}
|
|
@ -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), '/');
|
||||
}
|
||||
|
||||
|
|
13
packages/integrations/node/src/get-network-address.ts
Normal file
13
packages/integrations/node/src/get-network-address.ts
Normal file
|
@ -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';
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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({
|
||||
|
|
Loading…
Reference in a new issue