2022-06-06 11:47:28 -05:00
|
|
|
// Normal Imports
|
2022-03-30 07:42:19 -05:00
|
|
|
import type { SSRManifest } from 'astro';
|
|
|
|
import { App } from 'astro/app';
|
2023-02-16 10:16:07 -05:00
|
|
|
|
2022-03-30 07:42:19 -05:00
|
|
|
interface Options {
|
|
|
|
port?: number;
|
|
|
|
hostname?: string;
|
|
|
|
start?: boolean;
|
|
|
|
}
|
|
|
|
|
2023-02-16 10:16:07 -05:00
|
|
|
// @ts-ignore
|
2022-03-30 07:42:19 -05:00
|
|
|
let _server: Server | undefined = undefined;
|
|
|
|
let _startPromise: Promise<void> | undefined = undefined;
|
|
|
|
|
|
|
|
export function start(manifest: SSRManifest, options: Options) {
|
2022-03-30 07:43:13 -05:00
|
|
|
if (options.start === false) {
|
2022-03-30 07:42:19 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-15 16:01:33 -05:00
|
|
|
const clientRoot = new URL('../client/', import.meta.url);
|
2022-03-30 07:42:19 -05:00
|
|
|
const app = new App(manifest);
|
2022-07-19 15:10:15 -05:00
|
|
|
const handler = async (request: Request, connInfo: any) => {
|
2022-04-15 16:02:19 -05:00
|
|
|
if (app.match(request)) {
|
2022-07-19 15:10:15 -05:00
|
|
|
let ip = connInfo?.remoteAddr?.hostname;
|
|
|
|
Reflect.set(request, Symbol.for('astro.clientAddress'), ip);
|
2022-09-28 15:55:27 -05:00
|
|
|
const response = await app.render(request);
|
2022-09-28 15:57:35 -05:00
|
|
|
if (app.setCookieHeaders) {
|
|
|
|
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
2022-09-28 15:55:27 -05:00
|
|
|
response.headers.append('Set-Cookie', setCookieHeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
2022-04-15 16:01:33 -05:00
|
|
|
}
|
2022-08-31 15:14:25 -05:00
|
|
|
|
2022-08-31 15:11:46 -05:00
|
|
|
// If the request path wasn't found in astro,
|
|
|
|
// try to fetch a static file instead
|
2022-04-15 16:01:33 -05:00
|
|
|
const url = new URL(request.url);
|
2022-11-07 10:05:12 -05:00
|
|
|
const localPath = new URL('./' + app.removeBase(url.pathname), clientRoot);
|
2023-02-16 10:16:07 -05:00
|
|
|
const stringLocalPath = localPath.toString();
|
|
|
|
// @ts-ignore
|
|
|
|
const extendName = fileExtension(stringLocalPath);
|
|
|
|
const fileResp = await fetch(
|
|
|
|
!extendName
|
|
|
|
? `${
|
|
|
|
stringLocalPath.endsWith('/')
|
|
|
|
? `${stringLocalPath}index.html`
|
|
|
|
: `${stringLocalPath}/index.html`
|
|
|
|
}`
|
|
|
|
: stringLocalPath
|
|
|
|
);
|
2023-02-16 10:18:31 -05:00
|
|
|
|
2022-08-31 15:11:46 -05:00
|
|
|
// If the static file can't be found
|
|
|
|
if (fileResp.status == 404) {
|
2022-08-31 15:14:25 -05:00
|
|
|
// Render the astro custom 404 page
|
2022-09-28 15:55:27 -05:00
|
|
|
const response = await app.render(request);
|
|
|
|
|
2022-09-28 15:57:35 -05:00
|
|
|
if (app.setCookieHeaders) {
|
|
|
|
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
2022-09-28 15:55:27 -05:00
|
|
|
response.headers.append('Set-Cookie', setCookieHeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
2022-08-31 15:14:25 -05:00
|
|
|
|
|
|
|
// If the static file is found
|
2022-08-31 15:11:46 -05:00
|
|
|
} else {
|
2022-08-31 15:14:25 -05:00
|
|
|
return fileResp;
|
2022-08-31 15:11:46 -05:00
|
|
|
}
|
2022-03-30 07:42:19 -05:00
|
|
|
};
|
|
|
|
|
2022-06-06 11:02:13 -05:00
|
|
|
const port = options.port ?? 8085;
|
2023-02-16 10:16:07 -05:00
|
|
|
// @ts-ignore
|
2022-03-30 07:42:19 -05:00
|
|
|
_server = new Server({
|
2022-06-06 11:02:13 -05:00
|
|
|
port,
|
2022-03-30 07:43:13 -05:00
|
|
|
hostname: options.hostname ?? '0.0.0.0',
|
|
|
|
handler,
|
|
|
|
});
|
2022-03-30 07:42:19 -05:00
|
|
|
|
2022-06-06 11:02:13 -05:00
|
|
|
_startPromise = Promise.resolve(_server.listenAndServe());
|
2022-06-22 10:59:49 -05:00
|
|
|
// eslint-disable-next-line no-console
|
2022-06-06 11:02:13 -05:00
|
|
|
console.error(`Server running on port ${port}`);
|
2022-03-30 07:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-04-15 16:01:33 -05:00
|
|
|
export function createExports(manifest: SSRManifest, options: Options) {
|
2022-03-30 07:42:19 -05:00
|
|
|
const app = new App(manifest);
|
|
|
|
return {
|
|
|
|
async stop() {
|
2022-03-30 07:43:13 -05:00
|
|
|
if (_server) {
|
|
|
|
_server.close();
|
2022-04-15 16:01:33 -05:00
|
|
|
_server = undefined;
|
2022-03-30 07:42:19 -05:00
|
|
|
}
|
|
|
|
await Promise.resolve(_startPromise);
|
|
|
|
},
|
2022-04-15 16:01:33 -05:00
|
|
|
running() {
|
|
|
|
return _server !== undefined;
|
|
|
|
},
|
|
|
|
async start() {
|
|
|
|
return start(manifest, options);
|
|
|
|
},
|
2022-03-30 07:42:19 -05:00
|
|
|
async handle(request: Request) {
|
|
|
|
return app.render(request);
|
2022-03-30 07:43:13 -05:00
|
|
|
},
|
|
|
|
};
|
2022-03-30 07:42:19 -05:00
|
|
|
}
|