From 416ceb9730ce604cd3f73c22200907d9b9978073 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 6 Apr 2023 15:40:50 -0400 Subject: [PATCH] Use Deno API to set Astro.clientAddress (#6651) * Use Deno API to set Astro.clientAddress * Add types --- .changeset/silver-avocados-drum.md | 5 +++++ packages/integrations/netlify/package.json | 1 + .../netlify/src/netlify-edge-functions.ts | 7 +++++-- pnpm-lock.yaml | 20 +++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/silver-avocados-drum.md diff --git a/.changeset/silver-avocados-drum.md b/.changeset/silver-avocados-drum.md new file mode 100644 index 000000000..05f7881a2 --- /dev/null +++ b/.changeset/silver-avocados-drum.md @@ -0,0 +1,5 @@ +--- +'@astrojs/netlify': patch +--- + +Use Deno API to set Astro.clientAddress in Netlify Edge diff --git a/packages/integrations/netlify/package.json b/packages/integrations/netlify/package.json index 33fe2e73e..dfe8385a1 100644 --- a/packages/integrations/netlify/package.json +++ b/packages/integrations/netlify/package.json @@ -42,6 +42,7 @@ "astro": "workspace:^2.2.0" }, "devDependencies": { + "@netlify/edge-functions": "^2.0.0", "@netlify/edge-handler-types": "^0.34.1", "@types/node": "^14.18.20", "astro": "workspace:*", diff --git a/packages/integrations/netlify/src/netlify-edge-functions.ts b/packages/integrations/netlify/src/netlify-edge-functions.ts index f4766b276..8011e2b1b 100644 --- a/packages/integrations/netlify/src/netlify-edge-functions.ts +++ b/packages/integrations/netlify/src/netlify-edge-functions.ts @@ -1,4 +1,5 @@ import type { SSRManifest } from 'astro'; +import type { Context } from '@netlify/edge-functions'; import { App } from 'astro/app'; const clientAddressSymbol = Symbol.for('astro.clientAddress'); @@ -6,7 +7,7 @@ const clientAddressSymbol = Symbol.for('astro.clientAddress'); export function createExports(manifest: SSRManifest) { const app = new App(manifest); - const handler = async (request: Request): Promise => { + const handler = async (request: Request, context: Context): Promise => { const url = new URL(request.url); // If this matches a static asset, just return and Netlify will forward it @@ -15,7 +16,9 @@ export function createExports(manifest: SSRManifest) { return; } if (app.match(request)) { - const ip = request.headers.get('x-nf-client-connection-ip'); + const ip = request.headers.get('x-nf-client-connection-ip') + || context?.ip + || (context as any)?.remoteAddr?.hostname; Reflect.set(request, clientAddressSymbol, ip); const response = await app.render(request); if (app.setCookieHeaders) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index faf46d5a3..74c7a775a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3375,6 +3375,7 @@ importers: packages/integrations/netlify: specifiers: '@astrojs/webapi': ^2.1.0 + '@netlify/edge-functions': ^2.0.0 '@netlify/edge-handler-types': ^0.34.1 '@netlify/functions': ^1.0.0 '@types/node': ^14.18.20 @@ -3390,6 +3391,7 @@ importers: '@netlify/functions': 1.4.0 esbuild: 0.15.18 devDependencies: + '@netlify/edge-functions': 2.0.0 '@netlify/edge-handler-types': 0.34.1 '@types/node': 14.18.36 astro: link:../../astro @@ -6102,6 +6104,17 @@ packages: postcss-selector-parser: 6.0.11 dev: true + /@deno/shim-deno-test/0.3.3: + resolution: {integrity: sha512-Ge0Tnl7zZY0VvEfgsyLhjid8DzI1d0La0dgm+3m0/A8gZXgp5xwlyIyue5e4SCUuVB/3AH/0lun9LcJhhTwmbg==} + dev: true + + /@deno/shim-deno/0.10.0: + resolution: {integrity: sha512-E7rQ0Hk33V45xQXKEnCxizdSP5C+hhqw1H3xWXsct3kYFWgG93B5gN3LKlyvcxbckt8d67jVa6s+y5duRYawvg==} + dependencies: + '@deno/shim-deno-test': 0.3.3 + which: 2.0.2 + dev: true + /@docsearch/css/3.3.3: resolution: {integrity: sha512-6SCwI7P8ao+se1TUsdZ7B4XzL+gqeQZnBc+2EONZlcVa0dVrk0NjETxozFKgMv0eEGH8QzP1fkN+A1rH61l4eg==} dev: false @@ -7372,6 +7385,13 @@ packages: preact: 10.12.0 dev: false + /@netlify/edge-functions/2.0.0: + resolution: {integrity: sha512-mRVGnPNA4YayDLPwnO1ZrcWwBODPj5BQPbx3/FUlQtZ5ow2D+PjMPQr8IcFm0HfMJQgtHZS39p9VS6PRSi1ePw==} + engines: {node: ^14.16.0 || >=16.0.0} + dependencies: + '@deno/shim-deno': 0.10.0 + dev: true + /@netlify/edge-handler-types/0.34.1: resolution: {integrity: sha512-YTwn8cw89M4lRTmoUhl9s8ljSGMDt7FOIsxsrx7YrRz/RZlbh4Yuh4RU13DDafDRBEVuRbjGo93cnN621ZfBjA==} dependencies: