aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/node/src/standalone.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/node/src/standalone.ts')
-rw-r--r--packages/integrations/node/src/standalone.ts53
1 files changed, 53 insertions, 0 deletions
diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts
new file mode 100644
index 000000000..8fef96ed5
--- /dev/null
+++ b/packages/integrations/node/src/standalone.ts
@@ -0,0 +1,53 @@
+import type { NodeApp } from 'astro/app/node';
+import type { Options } from './types';
+import path from 'path';
+import { fileURLToPath } from 'url';
+import middleware from './middleware.js';
+import { createServer } from './http-server.js';
+
+function resolvePaths(options: Options) {
+ const clientURLRaw = new URL(options.client);
+ const serverURLRaw = new URL(options.server);
+ const rel = path.relative(fileURLToPath(serverURLRaw), fileURLToPath(clientURLRaw));
+
+ const serverEntryURL = new URL(import.meta.url);
+ const clientURL = new URL(appendForwardSlash(rel), serverEntryURL);
+
+ return {
+ client: clientURL,
+ };
+}
+
+function appendForwardSlash(pth: string) {
+ return pth.endsWith('/') ? pth : pth + '/';
+}
+
+export function getResolvedHostForHttpServer(host: string | boolean) {
+ if (host === false) {
+ // Use a secure default
+ 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)
+ } else {
+ return host;
+ }
+}
+
+export default function startServer(app: NodeApp, options: Options) {
+ const port = process.env.PORT ? Number(process.env.port) : (options.port ?? 8080);
+ const { client } = resolvePaths(options);
+ const handler = middleware(app);
+
+ const host = getResolvedHostForHttpServer(options.host);
+ const server = createServer({
+ client,
+ port,
+ host,
+ }, handler);
+
+ // eslint-disable-next-line no-console
+ console.log(`Server listening on http://${host}:${port}`);
+
+ return server.closed();
+}