2022-06-16 14:12:25 +00:00
|
|
|
import type { SSRManifest } from 'astro';
|
|
|
|
import { App } from 'astro/app';
|
2022-11-08 13:54:49 +00:00
|
|
|
import { getProcessEnvProxy } from './util.js';
|
|
|
|
|
|
|
|
process.env = getProcessEnvProxy();
|
2022-06-16 14:12:25 +00:00
|
|
|
|
|
|
|
type Env = {
|
|
|
|
ASSETS: { fetch: (req: Request) => Promise<Response> };
|
2022-10-26 13:46:25 +00:00
|
|
|
name: string;
|
2022-06-16 14:12:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function createExports(manifest: SSRManifest) {
|
2022-07-01 02:29:59 +00:00
|
|
|
const app = new App(manifest, false);
|
2022-06-16 14:12:25 +00:00
|
|
|
|
2022-10-26 13:46:25 +00:00
|
|
|
const fetch = async (request: Request, env: Env, context: any) => {
|
2022-11-08 13:54:49 +00:00
|
|
|
process.env = env as any;
|
|
|
|
|
2022-06-16 14:12:25 +00:00
|
|
|
const { origin, pathname } = new URL(request.url);
|
|
|
|
|
|
|
|
// static assets
|
|
|
|
if (manifest.assets.has(pathname)) {
|
2022-11-07 15:05:12 +00:00
|
|
|
const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
|
2022-06-16 14:12:25 +00:00
|
|
|
return env.ASSETS.fetch(assetRequest);
|
|
|
|
}
|
|
|
|
|
2022-07-22 20:30:17 +00:00
|
|
|
let routeData = app.match(request, { matchNotFound: true });
|
|
|
|
if (routeData) {
|
2022-07-19 20:11:53 +00:00
|
|
|
Reflect.set(
|
|
|
|
request,
|
|
|
|
Symbol.for('astro.clientAddress'),
|
|
|
|
request.headers.get('cf-connecting-ip')
|
|
|
|
);
|
2022-10-26 13:46:25 +00:00
|
|
|
Reflect.set(request, Symbol.for('runtime'), { env, name: 'cloudflare', ...context });
|
2022-09-28 20:55:27 +00:00
|
|
|
let response = await app.render(request, routeData);
|
|
|
|
|
2022-09-28 20:57:35 +00:00
|
|
|
if (app.setCookieHeaders) {
|
|
|
|
for (const setCookieHeader of app.setCookieHeaders(response)) {
|
2022-09-28 20:55:27 +00:00
|
|
|
response.headers.append('Set-Cookie', setCookieHeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
2022-06-24 19:30:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 14:12:25 +00:00
|
|
|
return new Response(null, {
|
|
|
|
status: 404,
|
|
|
|
statusText: 'Not found',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return { default: { fetch } };
|
|
|
|
}
|