diff --git a/packages/integrations/cloudflare/package.json b/packages/integrations/cloudflare/package.json index df1348a18868a..56443bb3452d2 100644 --- a/packages/integrations/cloudflare/package.json +++ b/packages/integrations/cloudflare/package.json @@ -19,7 +19,10 @@ "homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/", "exports": { ".": "./dist/index.js", - "./runtime": "./dist/runtime.js", + "./runtime": { + "import": "./dist/runtime.js", + "types": "./dist/runtime.d.ts" + }, "./server.advanced.js": "./dist/server.advanced.js", "./server.directory.js": "./dist/server.directory.js", "./package.json": "./package.json" diff --git a/packages/integrations/cloudflare/src/runtime.ts b/packages/integrations/cloudflare/src/runtime.ts index a56360a88367e..bb7f13fa6686a 100644 --- a/packages/integrations/cloudflare/src/runtime.ts +++ b/packages/integrations/cloudflare/src/runtime.ts @@ -1,7 +1,20 @@ +export type WorkerRuntime = { + name: 'cloudflare'; + env: T; + waitUntil(promise: Promise): void; + passThroughOnException(): void; +}; -export function getRuntime(request: Request): any { - return Reflect.get( - request, - Symbol.for('runtime') - ); -} \ No newline at end of file +export type PagesRuntime = { + name: 'cloudflare'; + env: T; + functionPath: string; + params: Record; + data: U; + waitUntil(promise: Promise): void; + next(request: Request): void; +}; + +export function getRuntime(request: Request): WorkerRuntime | PagesRuntime { + return Reflect.get(request, Symbol.for('runtime')); +} diff --git a/packages/integrations/cloudflare/src/server.advanced.ts b/packages/integrations/cloudflare/src/server.advanced.ts index a63e173d93f73..502700e0be976 100644 --- a/packages/integrations/cloudflare/src/server.advanced.ts +++ b/packages/integrations/cloudflare/src/server.advanced.ts @@ -5,16 +5,14 @@ import { App } from 'astro/app'; type Env = { ASSETS: { fetch: (req: Request) => Promise }; - name: string; + name: string; }; export function createExports(manifest: SSRManifest) { const app = new App(manifest, false); const fetch = async (request: Request, env: Env, context: any) => { - const { origin, pathname } = new URL(request.url); - env.name = "cloudflare"; // static assets if (manifest.assets.has(pathname)) { @@ -29,11 +27,7 @@ export function createExports(manifest: SSRManifest) { Symbol.for('astro.clientAddress'), request.headers.get('cf-connecting-ip') ); - Reflect.set( - request, - Symbol.for('runtime'), - { env, ...context } - ); + Reflect.set(request, Symbol.for('runtime'), { env, name: 'cloudflare', ...context }); let response = await app.render(request, routeData); if (app.setCookieHeaders) { diff --git a/packages/integrations/cloudflare/src/server.directory.ts b/packages/integrations/cloudflare/src/server.directory.ts index eb52ff79a0ab6..d31e2189fde65 100644 --- a/packages/integrations/cloudflare/src/server.directory.ts +++ b/packages/integrations/cloudflare/src/server.directory.ts @@ -9,14 +9,12 @@ export function createExports(manifest: SSRManifest) { const onRequest = async ({ request, next, - ...runtimeEnv + ...runtimeEnv }: { request: Request; next: (request: Request) => void; } & Record) => { const { origin, pathname } = new URL(request.url); - runtimeEnv.name = "cloudflare"; - // static assets if (manifest.assets.has(pathname)) { const assetRequest = new Request(`${origin}/static${pathname}`, request); @@ -30,11 +28,11 @@ export function createExports(manifest: SSRManifest) { Symbol.for('astro.clientAddress'), request.headers.get('cf-connecting-ip') ); - Reflect.set( - request, - Symbol.for('runtime'), - runtimeEnv - ); + Reflect.set(request, Symbol.for('runtime'), { + ...runtimeEnv, + name: 'cloudflare', + next, + }); let response = await app.render(request, routeData); if (app.setCookieHeaders) {