Optimizing code based on the comments

This commit is contained in:
wuls 2023-05-26 15:03:09 +08:00 committed by Nate Moore
parent 876d19b6d6
commit 12c0b43e42
5 changed files with 11 additions and 43 deletions

View file

@ -1,6 +0,0 @@
---
'astro': minor
'@astrojs/node': minor
---
supporting a network address access a website when an user set host = true in Node environment

View file

@ -1,5 +1,5 @@
import os from 'os'
interface ReturnValOpt {
interface NetworkAddressOpt {
local: string[]
network: string[]
}
@ -11,9 +11,10 @@ const wildcardHosts = new Set([
])
type Protocol = 'http' | 'https'
export function getNetworkAddress(protocol: Protocol = 'http', hostname: string, port:number, base?:string) {
// 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 returnVal:ReturnValOpt = {
const NetworkAddress:NetworkAddressOpt = {
local: [],
network: []
}
@ -36,12 +37,12 @@ export function getNetworkAddress(protocol: Protocol = 'http', hostname: string,
}
const url = `${protocol}://${host}:${port}${base ? base : ''}`
if (detail.address.includes('127.0.0.1')) {
returnVal.local.push(url)
NetworkAddress.local.push(url)
} else {
returnVal.network.push(url)
NetworkAddress.network.push(url)
}
})
return returnVal
return NetworkAddress
}

View file

@ -74,16 +74,11 @@ const preview: CreatePreviewServer = async function ({
if(host === undefined ){
// eslint-disable-next-line no-console
console.log(
`Preview server listening on
\t
local: ${address.local[0]}
\t
network: ${address.network[0]}
`Preview server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}
` );
}else{
// eslint-disable-next-line no-console
console.log(`Preview server listening on \t
${address.local[0]}`);
console.log(`Preview server listening on ${address.local[0]}`);
}

View file

@ -61,16 +61,11 @@ export default function startServer(app: NodeApp, options: Options) {
if(host === undefined ){
// eslint-disable-next-line no-console
console.log(
`Preview server listening on
\t
local: ${address.local[0]}
\t
network: ${address.network[0]}
`Preview server listening on \n local: ${address.local[0]} \t\n network: ${address.network[0]}
` );
}else{
// eslint-disable-next-line no-console
console.log(`Preview server listening on \t
${address.local[0]}`);
console.log(`Preview server listening on ${address.local[0]}`);
}
return {

View file

@ -1,5 +1,4 @@
import { expect } from 'chai';
import { getNetworkAddress } from '../dist/get-network-address.js'
import { fetch } from 'undici';
describe('URL protocol', () => {
@ -18,22 +17,6 @@ describe('URL protocol', () => {
await fixture.build();
});
describe('test preview when host is true', async () => {
let devPreview;
before(async () => {
devPreview = await fixture.preview();
});
after(async () => {
await devPreview.stop();
});
it('test host is true ', async () => {
const address = getNetworkAddress('http', undefined, 3000)
const res = await fetch(address.network[0])
expect(res.status).to.equal(200);
});
})
it('return http when non-secure', async () => {
const { handler } = await import('./fixtures/url-protocol/dist/server/entry.mjs');
let { req, res, text } = createRequestAndResponse({