From da2a317a1a2292cc6905d52d28cef1310c11847c Mon Sep 17 00:00:00 2001 From: AirBorne04 Date: Sun, 16 Oct 2022 15:14:48 +0200 Subject: [PATCH] enable access to cloudflare runtime --- .changeset/poor-frogs-clean.md | 5 +++++ packages/integrations/cloudflare/src/server.advanced.ts | 7 +++++++ packages/integrations/cloudflare/src/server.directory.ts | 9 ++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 .changeset/poor-frogs-clean.md diff --git a/.changeset/poor-frogs-clean.md b/.changeset/poor-frogs-clean.md new file mode 100644 index 0000000000000..ffe3b23dbb26c --- /dev/null +++ b/.changeset/poor-frogs-clean.md @@ -0,0 +1,5 @@ +--- +'@astrojs/cloudflare': patch +--- + +enable access to cloudflare runtime [KV, R2, Durable Objects] diff --git a/packages/integrations/cloudflare/src/server.advanced.ts b/packages/integrations/cloudflare/src/server.advanced.ts index c285ccaba044f..f75196ea07463 100644 --- a/packages/integrations/cloudflare/src/server.advanced.ts +++ b/packages/integrations/cloudflare/src/server.advanced.ts @@ -5,6 +5,7 @@ import { App } from 'astro/app'; type Env = { ASSETS: { fetch: (req: Request) => Promise }; + name: string; }; export function createExports(manifest: SSRManifest) { @@ -12,6 +13,7 @@ export function createExports(manifest: SSRManifest) { const fetch = async (request: Request, env: Env) => { const { origin, pathname } = new URL(request.url); + env.name = "cloudflare"; // static assets if (manifest.assets.has(pathname)) { @@ -26,6 +28,11 @@ export function createExports(manifest: SSRManifest) { Symbol.for('astro.clientAddress'), request.headers.get('cf-connecting-ip') ); + Reflect.set( + request, + Symbol.for('runtime'), + env + ); 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 e51d0ea57c7a5..eb52ff79a0ab6 100644 --- a/packages/integrations/cloudflare/src/server.directory.ts +++ b/packages/integrations/cloudflare/src/server.directory.ts @@ -9,11 +9,13 @@ export function createExports(manifest: SSRManifest) { const onRequest = async ({ request, next, + ...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)) { @@ -28,6 +30,11 @@ export function createExports(manifest: SSRManifest) { Symbol.for('astro.clientAddress'), request.headers.get('cf-connecting-ip') ); + Reflect.set( + request, + Symbol.for('runtime'), + runtimeEnv + ); let response = await app.render(request, routeData); if (app.setCookieHeaders) {