Repository: withastro/astro
File: packages/integrations/cloudflare/src/utils/handler.ts (TypeScript)

Code before:
	if (env[sessionKVBindingName]) {
		const sessionConfigOptions = app.manifest.sessionConfig?.options ?? {};
		Object.assign(sessionConfigOptions, {
			binding: env[sessionKVBindingName],
		});
	}

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

	const routeData = app.match(request as Request & Parameters<ExportedHandlerFetchHandler>[0]);
	if (!routeData) {
		// https://developers.cloudflare.com/pages/functions/api-reference/#envassetsfetch
		const asset = await env.ASSETS.fetch(
			request.url.replace(/index.html$/, '').replace(/\.html$/, ''),
		);
		if (asset.status !== 404) {
			return asset;
		}
	}

	const locals: Runtime = {
		cfContext: context,
		runtime: {
			get env(): never {
				throw new Error(
					`Astro.locals.runtime.env has been removed in Astro v6. Use 'import { env } from "cloudflare:workers"' instead.`,
				);
			},
			get cf(): never {
				throw new Error(
					`Astro.locals.runtime.cf has been removed in Astro v6. Use 'Astro.request.cf' instead.`,
				);
			},
			get caches(): never {
				throw new Error(
					`Astro.locals.runtime.caches has been removed in Astro v6. Use the global 'caches' object instead.`,
				);
			},
			get ctx(): never {
				throw new Error(
					`Astro.locals.runtime.ctx has been removed in Astro v6. Use 'Astro.locals.cfContext' instead.`,
				);
			},
		},
	};

	const response = await app.render(
		request as Request & Parameters<ExportedHandlerFetchHandler>[0],

Review comment (suggestion):
How about using `Object.defineProperty` with `enumerable: false` to define `runtime`, so it doesn't show up on the objects

Code after:

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

	const routeData = app.match(request as Request & Parameters<ExportedHandlerFetchHandler>[0]);
	if (!routeData) {
		// https://developers.cloudflare.com/pages/functions/api-reference/#envassetsfetch
		const asset = await env.ASSETS.fetch(
			request.url.replace(/index.html$/, '').replace(/\.html$/, ''),
		);
		if (asset.status !== 404) {
			return asset;
		}
	}

	const locals: Runtime = {
		cfContext: context,
	};
	Object.defineProperty(locals, 'runtime', {
		enumerable: false,
		value: {
			get env(): never {
				throw new Error(
					`Astro.locals.runtime.env has been removed in Astro v6. Use 'import { env } from "cloudflare:workers"' instead.`,
				);
			},
			get cf(): never {
				throw new Error(
					`Astro.locals.runtime.cf has been removed in Astro v6. Use 'Astro.request.cf' instead.`,
				);
			},
			get caches(): never {
				throw new Error(
					`Astro.locals.runtime.caches has been removed in Astro v6. Use the global 'caches' object instead.`,
				);
			},
			get ctx(): never {
				throw new Error(
					`Astro.locals.runtime.ctx has been removed in Astro v6. Use 'Astro.locals.cfContext' instead.`,
				);
			},
		},
	});

	const response = await app.render(
		request as Request & Parameters<ExportedHandlerFetchHandler>[0],
		{
			routeData,
			locals,