0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/vercel/src/server-entrypoint.ts
2022-04-07 17:23:16 +00:00

34 lines
874 B
TypeScript

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 handler = 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: handler };
};