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-08-27 16:21:28 -05:00
|
|
|
import type { Options } from './types';
|
2023-02-16 10:16:07 -05:00
|
|
|
|
2023-03-07 08:41:24 -05:00
|
|
|
// @ts-expect-error
|
2023-02-17 13:21:11 -05:00
|
|
|
import { fromFileUrl, serveFile, Server } from '@astrojs/deno/__deno_imports.js';
|
2023-02-17 10:58:53 -05:00
|
|
|
|
2022-03-30 07:42:19 -05:00
|
|
|
let _server: Server | undefined = undefined;
|
|
|
|
let _startPromise: Promise<void> | undefined = undefined;
|
|
|
|
|
2023-02-17 13:19:19 -05:00
|
|
|
async function* getPrerenderedFiles(clientRoot: URL): AsyncGenerator<URL> {
|
2023-03-07 08:41:24 -05:00
|
|
|
// @ts-expect-error
|
2023-02-17 13:19:19 -05:00
|
|
|
for await (const ent of Deno.readDir(clientRoot)) {
|
|
|
|
if (ent.isDirectory) {
|
2023-02-17 13:21:11 -05:00
|
|
|
yield* getPrerenderedFiles(new URL(`./${ent.name}/`, clientRoot));
|
2023-02-17 13:19:19 -05:00
|
|
|
} else if (ent.name.endsWith('.html')) {
|
2023-02-17 13:21:11 -05:00
|
|
|
yield new URL(`./${ent.name}`, clientRoot);
|
2023-02-17 13:19:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:42:19 -05:00
|
|
|
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-17 13:19:19 -05:00
|
|
|
|
|
|
|
let fileResp = await serveFile(request, fromFileUrl(localPath));
|
|
|
|
|
|
|
|
// Attempt to serve `index.html` if 404
|
|
|
|
if (fileResp.status == 404) {
|
|
|
|
let fallback;
|
|
|
|
for await (const file of getPrerenderedFiles(clientRoot)) {
|
|
|
|
const pathname = file.pathname.replace(/\/(index)?\.html$/, '');
|
|
|
|
if (localPath.pathname.endsWith(pathname)) {
|
|
|
|
fallback = file;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fallback) {
|
|
|
|
fileResp = await serveFile(request, fromFileUrl(fallback));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
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());
|
|
|
|
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
|
|
|
}
|