2022-03-30 02:01:37 +00:00
|
|
|
import type { SSRManifest } from 'astro';
|
|
|
|
import { App } from 'astro/app';
|
|
|
|
import { polyfill } from '@astrojs/webapi';
|
2022-05-11 21:10:38 +00:00
|
|
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
2022-03-30 02:01:37 +00:00
|
|
|
|
|
|
|
import { getRequest, setResponse } from './request-transform.js';
|
|
|
|
|
|
|
|
polyfill(globalThis, {
|
|
|
|
exclude: 'window document',
|
|
|
|
});
|
|
|
|
|
|
|
|
export const createExports = (manifest: SSRManifest) => {
|
|
|
|
const app = new App(manifest);
|
|
|
|
|
2022-04-07 17:21:56 +00:00
|
|
|
const handler = async (req: IncomingMessage, res: ServerResponse) => {
|
2022-03-30 02:01:37 +00:00
|
|
|
let request: Request;
|
|
|
|
|
|
|
|
try {
|
|
|
|
request = await getRequest(`https://${req.headers.host}`, req);
|
|
|
|
} catch (err: any) {
|
|
|
|
res.statusCode = err.status || 400;
|
|
|
|
return res.end(err.reason || 'Invalid request body');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!app.match(request)) {
|
|
|
|
res.statusCode = 404;
|
|
|
|
return res.end('Not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
await setResponse(res, await app.render(request));
|
|
|
|
};
|
|
|
|
|
2022-04-07 17:23:16 +00:00
|
|
|
return { default: handler };
|
2022-03-30 02:01:37 +00:00
|
|
|
};
|