2022-10-12 16:25:51 -05:00
|
|
|
import type { NodeApp } from 'astro/app/node';
|
2023-01-26 12:39:57 -05:00
|
|
|
import https from 'https';
|
2022-10-12 16:25:51 -05:00
|
|
|
import path from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { createServer } from './http-server.js';
|
2023-05-03 11:40:47 -05:00
|
|
|
import middleware from './nodeMiddleware.js';
|
2022-10-12 16:27:56 -05:00
|
|
|
import type { Options } from './types';
|
2022-10-12 16:25:51 -05:00
|
|
|
|
|
|
|
function resolvePaths(options: Options) {
|
|
|
|
const clientURLRaw = new URL(options.client);
|
|
|
|
const serverURLRaw = new URL(options.server);
|
|
|
|
const rel = path.relative(fileURLToPath(serverURLRaw), fileURLToPath(clientURLRaw));
|
2022-10-12 16:27:56 -05:00
|
|
|
|
2022-10-12 16:25:51 -05:00
|
|
|
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) {
|
2022-10-18 02:31:21 -05:00
|
|
|
const port = process.env.PORT ? Number(process.env.PORT) : options.port ?? 8080;
|
2022-10-12 16:25:51 -05:00
|
|
|
const { client } = resolvePaths(options);
|
2023-01-10 11:59:20 -05:00
|
|
|
const handler = middleware(app, options.mode);
|
2022-10-12 16:25:51 -05:00
|
|
|
|
2022-11-17 10:48:20 -05:00
|
|
|
// Allow to provide host value at runtime
|
2022-11-17 10:51:50 -05:00
|
|
|
const host = getResolvedHostForHttpServer(
|
|
|
|
process.env.HOST !== undefined && process.env.HOST !== '' ? process.env.HOST : options.host
|
|
|
|
);
|
2022-10-12 16:27:56 -05:00
|
|
|
const server = createServer(
|
|
|
|
{
|
|
|
|
client,
|
|
|
|
port,
|
|
|
|
host,
|
2022-11-07 10:05:12 -05:00
|
|
|
removeBase: app.removeBase.bind(app),
|
2022-10-12 16:27:56 -05:00
|
|
|
},
|
|
|
|
handler
|
|
|
|
);
|
2022-10-12 16:25:51 -05:00
|
|
|
|
2023-01-26 12:39:57 -05:00
|
|
|
const protocol = server.server instanceof https.Server ? 'https' : 'http';
|
2023-02-02 19:10:16 -05:00
|
|
|
|
2022-10-12 16:25:51 -05:00
|
|
|
// eslint-disable-next-line no-console
|
2023-01-26 12:39:57 -05:00
|
|
|
console.log(`Server listening on ${protocol}://${host}:${port}`);
|
2022-10-12 16:25:51 -05:00
|
|
|
|
2023-02-02 19:10:16 -05:00
|
|
|
return {
|
|
|
|
server,
|
2023-02-02 19:12:22 -05:00
|
|
|
done: server.closed(),
|
2023-02-02 19:10:16 -05:00
|
|
|
};
|
2022-10-12 16:25:51 -05:00
|
|
|
}
|