summaryrefslogtreecommitdiff
path: root/packages/integrations/node/src
diff options
context:
space:
mode:
authorGravatar Emanuele Stoppa <my.burning@gmail.com> 2023-08-15 09:24:06 +0100
committerGravatar Emanuele Stoppa <my.burning@gmail.com> 2023-08-15 09:24:06 +0100
commitf1c1c99ad4b40c700d81c295efad02875f0ff782 (patch)
tree9384a6981f48d056d0fcb5ed51afa8ae16e6350f /packages/integrations/node/src
parent578357a5ae62a56aa372820175a3de3dee1c9cdc (diff)
parent81269c66859b51ab4e4ec064e39bcb46b5783704 (diff)
downloadastro-f1c1c99ad4b40c700d81c295efad02875f0ff782.tar.gz
astro-f1c1c99ad4b40c700d81c295efad02875f0ff782.tar.zst
astro-f1c1c99ad4b40c700d81c295efad02875f0ff782.zip
Merge remote-tracking branch 'origin/main' into next
Diffstat (limited to 'packages/integrations/node/src')
-rw-r--r--packages/integrations/node/src/get-network-address.ts48
-rw-r--r--packages/integrations/node/src/preview.ts13
-rw-r--r--packages/integrations/node/src/standalone.ts13
3 files changed, 70 insertions, 4 deletions
diff --git a/packages/integrations/node/src/get-network-address.ts b/packages/integrations/node/src/get-network-address.ts
new file mode 100644
index 000000000..3834c7617
--- /dev/null
+++ b/packages/integrations/node/src/get-network-address.ts
@@ -0,0 +1,48 @@
+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;
+}
diff --git a/packages/integrations/node/src/preview.ts b/packages/integrations/node/src/preview.ts
index 92f9b86ba..4a4db4632 100644
--- a/packages/integrations/node/src/preview.ts
+++ b/packages/integrations/node/src/preview.ts
@@ -1,6 +1,7 @@
import type { CreatePreviewServer } from 'astro';
import type http from 'node:http';
import { fileURLToPath } from 'node:url';
+import { getNetworkAddress } from './get-network-address.js';
import { createServer } from './http-server.js';
import type { createExports } from './server';
@@ -67,9 +68,17 @@ const preview: CreatePreviewServer = async function ({
},
handler
);
+ const address = getNetworkAddress('http', host, port);
- // eslint-disable-next-line no-console
- console.log(`Preview server listening on 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]}`);
+ }
return server;
};
diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts
index 68b2cebcd..94dc26758 100644
--- a/packages/integrations/node/src/standalone.ts
+++ b/packages/integrations/node/src/standalone.ts
@@ -2,6 +2,7 @@ import type { NodeApp } from 'astro/app/node';
import https from 'https';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
+import { getNetworkAddress } from './get-network-address.js';
import { createServer } from './http-server.js';
import middleware from './nodeMiddleware.js';
import type { Options } from './types';
@@ -55,9 +56,17 @@ 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,