2022-03-30 07:42:19 -05:00
|
|
|
import './shim.js';
|
|
|
|
import type { SSRManifest } from 'astro';
|
|
|
|
import { App } from 'astro/app';
|
|
|
|
// @ts-ignore
|
2022-03-30 07:43:13 -05:00
|
|
|
import { Server } from 'https://deno.land/std@0.132.0/http/server.ts';
|
2022-04-15 16:01:33 -05:00
|
|
|
// @ts-ignore
|
|
|
|
import { fetch } from 'https://deno.land/x/file_fetch/mod.ts';
|
2022-03-30 07:42:19 -05:00
|
|
|
|
|
|
|
interface Options {
|
|
|
|
port?: number;
|
|
|
|
hostname?: string;
|
|
|
|
start?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
const handler = async (request: Request) => {
|
2022-04-15 16:02:19 -05:00
|
|
|
if (app.match(request)) {
|
2022-04-15 16:01:33 -05:00
|
|
|
return await app.render(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = new URL(request.url);
|
|
|
|
const localPath = new URL('.' + url.pathname, clientRoot);
|
|
|
|
return fetch(localPath.toString());
|
2022-03-30 07:42:19 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
_server = new Server({
|
2022-03-30 07:43:13 -05:00
|
|
|
port: options.port ?? 8085,
|
|
|
|
hostname: options.hostname ?? '0.0.0.0',
|
|
|
|
handler,
|
|
|
|
});
|
2022-03-30 07:42:19 -05:00
|
|
|
|
2022-03-30 07:43:13 -05:00
|
|
|
_startPromise = _server.listenAndServe();
|
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
|
|
|
}
|