summaryrefslogtreecommitdiff
path: root/packages/integrations/cloudflare/src/server.advanced.ts
blob: 175756d6ab4a19314d471d1df505412d9a1c1779 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import type { Request as CFRequest, ExecutionContext } from '@cloudflare/workers-types';
import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import { getProcessEnvProxy, isNode } from './util.js';

if (!isNode) {
	process.env = getProcessEnvProxy();
}

type Env = {
	ASSETS: { fetch: (req: Request) => Promise<Response> };
	name: string;
};

interface WorkerRuntime {
	runtime: {
		waitUntil: (promise: Promise<any>) => void;
		env: Env;
		cf: CFRequest['cf'];
		caches: typeof caches;
	};
}

export function createExports(manifest: SSRManifest) {
	const app = new App(manifest);

	const fetch = async (request: Request & CFRequest, env: Env, context: ExecutionContext) => {
		// TODO: remove this any cast in the future
		// REF: the type cast to any is needed because the Cloudflare Env Type is not assignable to type 'ProcessEnv'
		process.env = env as any;

		const { pathname } = new URL(request.url);

		// static assets fallback, in case default _routes.json is not used
		if (manifest.assets.has(pathname)) {
			return env.ASSETS.fetch(request);
		}

		let routeData = app.match(request, { matchNotFound: true });
		if (routeData) {
			Reflect.set(
				request,
				Symbol.for('astro.clientAddress'),
				request.headers.get('cf-connecting-ip')
			);

			// `getRuntime()` is deprecated, currently available additionally to new Astro.locals.runtime
			// TODO: remove `getRuntime()` in Astro 3.0
			Reflect.set(request, Symbol.for('runtime'), {
				env,
				name: 'cloudflare',
				caches,
				cf: request.cf,
				...context,
				waitUntil: (promise: Promise<any>) => {
					context.waitUntil(promise);
				},
			});

			const locals: WorkerRuntime = {
				runtime: {
					waitUntil: (promise: Promise<any>) => {
						context.waitUntil(promise);
					},
					env: env,
					cf: request.cf,
					caches: caches,
				},
			};

			let response = await app.render(request, routeData, locals);

			if (app.setCookieHeaders) {
				for (const setCookieHeader of app.setCookieHeaders(response)) {
					response.headers.append('Set-Cookie', setCookieHeader);
				}
			}

			return response;
		}

		return new Response(null, {
			status: 404,
			statusText: 'Not found',
		});
	};

	return { default: { fetch } };
}