diff options
Diffstat (limited to 'packages/integrations/cloudflare/src')
4 files changed, 74 insertions, 20 deletions
diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index c378a195f..6b64a0a1f 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -230,7 +230,7 @@ export default function createIntegration(args?: Options): AstroIntegration { } } - // // // throw the server folder in the bin + // throw the server folder in the bin const serverUrl = new URL(_buildConfig.server); await fs.promises.rm(serverUrl, { recursive: true, force: true }); diff --git a/packages/integrations/cloudflare/src/runtime.ts b/packages/integrations/cloudflare/src/runtime.ts index cd3dfff47..03c15d4a3 100644 --- a/packages/integrations/cloudflare/src/runtime.ts +++ b/packages/integrations/cloudflare/src/runtime.ts @@ -1,3 +1,4 @@ +// TODO: remove `getRuntime()` in Astro 3.0 import type { Cache, CacheStorage, IncomingRequestCfProperties } from '@cloudflare/workers-types'; export type WorkerRuntime<T = unknown> = { @@ -21,6 +22,16 @@ export type PagesRuntime<T = unknown, U = unknown> = { cf?: IncomingRequestCfProperties; }; +/** + * @deprecated since version 6.8.0 + * The `getRuntime` utility has been deprecated and should be updated to the new [`Astro.locals`](https://docs.astro.build/en/guides/middleware/#locals) API. + * ```diff + * - import { getRuntime } from '@astrojs/cloudflare/runtime'; + * - getRuntime(Astro.request); + * + * + const runtime = Astro.locals.runtime; + * ``` + */ export function getRuntime<T = unknown, U = unknown>( request: Request ): WorkerRuntime<T> | PagesRuntime<T, U> { diff --git a/packages/integrations/cloudflare/src/server.advanced.ts b/packages/integrations/cloudflare/src/server.advanced.ts index 9758b8b19..175756d6a 100644 --- a/packages/integrations/cloudflare/src/server.advanced.ts +++ b/packages/integrations/cloudflare/src/server.advanced.ts @@ -12,10 +12,21 @@ type Env = { 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); @@ -32,6 +43,9 @@ export function createExports(manifest: SSRManifest) { 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', @@ -42,7 +56,19 @@ export function createExports(manifest: SSRManifest) { context.waitUntil(promise); }, }); - let response = await app.render(request, routeData); + + 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)) { diff --git a/packages/integrations/cloudflare/src/server.directory.ts b/packages/integrations/cloudflare/src/server.directory.ts index 3e9531a56..d4e4094de 100644 --- a/packages/integrations/cloudflare/src/server.directory.ts +++ b/packages/integrations/cloudflare/src/server.directory.ts @@ -7,28 +7,30 @@ if (!isNode) { process.env = getProcessEnvProxy(); } +interface FunctionRuntime { + runtime: { + waitUntil: (promise: Promise<any>) => void; + env: EventContext<unknown, string, unknown>['env']; + cf: CFRequest['cf']; + caches: typeof caches; + }; +} + export function createExports(manifest: SSRManifest) { const app = new App(manifest); - const onRequest = async ({ - request, - next, - ...runtimeEnv - }: { - request: Request & CFRequest; - next: (request: Request) => void; - waitUntil: EventContext<unknown, any, unknown>['waitUntil']; - } & Record<string, unknown>) => { - process.env = runtimeEnv.env as any; + const onRequest = async (context: EventContext<unknown, string, unknown>) => { + const request = context.request as CFRequest & Request; + const { next, env } = context; + + // 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)) { - // we need this so the page does not error - // https://developers.cloudflare.com/pages/platform/functions/advanced-mode/#set-up-a-function - return (runtimeEnv.env as EventContext<unknown, string, unknown>['env']).ASSETS.fetch( - request - ); + return env.ASSETS.fetch(request); } let routeData = app.match(request, { matchNotFound: true }); @@ -38,17 +40,32 @@ export function createExports(manifest: SSRManifest) { 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'), { - ...runtimeEnv, + ...context, waitUntil: (promise: Promise<any>) => { - runtimeEnv.waitUntil(promise); + context.waitUntil(promise); }, name: 'cloudflare', next, caches, cf: request.cf, }); - let response = await app.render(request, routeData); + + const locals: FunctionRuntime = { + runtime: { + waitUntil: (promise: Promise<any>) => { + context.waitUntil(promise); + }, + env: context.env, + cf: request.cf, + caches: caches, + }, + }; + + let response = await app.render(request, routeData, locals); if (app.setCookieHeaders) { for (const setCookieHeader of app.setCookieHeaders(response)) { |