astro/packages/integrations/vercel/src/server-entrypoint.ts

35 lines
867 B
TypeScript
Raw Normal View History

2022-03-30 02:01:37 +00:00
import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import { polyfill } from '@astrojs/webapi';
import type { IncomingMessage, ServerResponse } from 'http';
import { getRequest, setResponse } from './request-transform.js';
polyfill(globalThis, {
exclude: 'window document',
});
export const createExports = (manifest: SSRManifest) => {
const app = new App(manifest);
const _default = async (req: IncomingMessage, res: ServerResponse) => {
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));
};
return { _default };
};