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

Code before:
				_config = config;
				finalBuildOutput = buildOutput;

				setAdapter({
					name: '@astrojs/cloudflare',
					serverEntrypoint: '@astrojs/cloudflare/entrypoints/server.js',
					exports: ['default'],
					adapterFeatures: {
						edgeMiddleware: false,
						buildOutput: 'server',
					},
					supportedAstroFeatures: {
						serverOutput: 'stable',
						hybridOutput: 'stable',
						staticOutput: 'unsupported',
						i18nDomains: 'experimental',
						// For explicitly set image services, this hack is used to suppress the warning about using `sharp`
						// by inferring the user is aware of its implications.
						// TODO: If an option to supress the warnings for `supportedAstroFeatures` is added, we should use it instead.
						sharpImageService: args?.imageService
							? 'stable'
							: {
									support: 'limited',
									message:
										'Cloudflare does not support sharp at runtime, but the `compile` image service can be used to optimize images with sharp on pre-rendered pages during build time.',
								},
						envGetSecret: 'stable',
					},
				});
			},
			'astro:server:setup': async ({ server }) => {
				if ((args?.platformProxy?.enabled ?? true) === true) {
					const platformProxy = await getPlatformProxy(args?.platformProxy);

					setProcessEnv(_config, platformProxy.env);

					const clientLocalsSymbol = Symbol.for('astro.locals');

					server.middlewares.use(async function middleware(req, _res, next) {
						Reflect.set(req, clientLocalsSymbol, {
							runtime: {
								env: platformProxy.env,
								cf: platformProxy.cf,
								caches: platformProxy.caches,
								ctx: {
									waitUntil: (promise: Promise<any>) => platformProxy.ctx.waitUntil(promise),
									// Currently not available: https://developers.cloudflare.com/pages/platform/known-issues/#pages-functions
									passThroughOnException: () => {
										throw new AstroError(
											'`passThroughOnException` is currently not available in Cloudflare Pages. See https://developers.cloudflare.com/pages/platform/known-issues/#pages-functions.',
										);

Review comment (suggestion):
Just to make it clear: this is a DUMB, HACKY, or, as the Brazilians would say, "GAMBIARRA" fix.

Changing the shape of this method would be a breaking change (well, unless we do it in a backwards compatible way - e.g. a optional field) for users and would need more of a back-and-forth to decide how that could be changed/decoupled to allow suppressing (or better controlling) warnings.

This doesn't need to be part of this PR so I went with the dumber solution, but my suggestion would be to:
- Change [`logFeatureSupport`](https://github.com/withastro/astro/blob/main/packages/astro/src/integrations/features-validation.ts#L142-L174 ) to *only* log the custom message if provided.
- Add an optional `suppress`  boolean to `supportedAstroFeatures` for when an unsupported feature warning is not applicable for specific reasons.


Code after:
				finalBuildOutput = buildOutput;

				setAdapter({
					name: '@astrojs/cloudflare',
					serverEntrypoint: '@astrojs/cloudflare/entrypoints/server.js',
					exports: ['default'],
					adapterFeatures: {
						edgeMiddleware: false,
						buildOutput: 'server',
					},
					supportedAstroFeatures: {
						serverOutput: 'stable',
						hybridOutput: 'stable',
						staticOutput: 'unsupported',
						i18nDomains: 'experimental',
						sharpImageService: {
							support: 'limited',
							message:
								'Cloudflare does not support sharp at runtime. However, you can configure `imageService: "compile"` to optimize images with sharp on prerendered pages during build time.',
							// For explicitly set image services, we suppress the warning about sharp not being supported at runtime,
							// inferring the user is aware of the limitations.
							suppress: args?.imageService ? 'all' : 'default',
						},
						envGetSecret: 'stable',
					},
				});
			},
			'astro:server:setup': async ({ server }) => {
				if ((args?.platformProxy?.enabled ?? true) === true) {
					const platformProxy = await getPlatformProxy(args?.platformProxy);

					setProcessEnv(_config, platformProxy.env);

					const clientLocalsSymbol = Symbol.for('astro.locals');

					server.middlewares.use(async function middleware(req, _res, next) {
						Reflect.set(req, clientLocalsSymbol, {
							runtime: {
								env: platformProxy.env,
								cf: platformProxy.cf,
								caches: platformProxy.caches,
								ctx: {
									waitUntil: (promise: Promise<any>) => platformProxy.ctx.waitUntil(promise),
									// Currently not available: https://developers.cloudflare.com/pages/platform/known-issues/#pages-functions
									passThroughOnException: () => {
										throw new AstroError(
											'`passThroughOnException` is currently not available in Cloudflare Pages. See https://developers.cloudflare.com/pages/platform/known-issues/#pages-functions.',
										);
									},
								},
							},