From fefdf727914dde530e82835b45fe741a64055a1d Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 24 Mar 2023 16:12:38 -0400 Subject: [PATCH 1/2] Use Deno API to set Astro.clientAddress --- .changeset/silver-avocados-drum.md | 5 +++++ packages/integrations/netlify/src/netlify-edge-functions.ts | 4 ++-- 2 files changed, 7 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 000000000000..05f7881a2d5e --- /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/src/netlify-edge-functions.ts b/packages/integrations/netlify/src/netlify-edge-functions.ts index f4766b27652a..5a1ec7174e76 100644 --- a/packages/integrations/netlify/src/netlify-edge-functions.ts +++ b/packages/integrations/netlify/src/netlify-edge-functions.ts @@ -6,7 +6,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, connInfo?: any): Promise => { const url = new URL(request.url); // If this matches a static asset, just return and Netlify will forward it @@ -15,7 +15,7 @@ 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') || connInfo?.remoteAddr?.hostname; Reflect.set(request, clientAddressSymbol, ip); const response = await app.render(request); if (app.setCookieHeaders) { From 45a90d83474d9ed62c37e85fbb75caca543a303f Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 6 Apr 2023 14:38:15 -0400 Subject: [PATCH 2/2] Add types --- packages/integrations/netlify/package.json | 1 + .../netlify/src/netlify-edge-functions.ts | 7 +++++-- pnpm-lock.yaml | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/integrations/netlify/package.json b/packages/integrations/netlify/package.json index 865249e54368..61cfde77eb10 100644 --- a/packages/integrations/netlify/package.json +++ b/packages/integrations/netlify/package.json @@ -42,6 +42,7 @@ "astro": "workspace:^2.1.6" }, "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 5a1ec7174e76..8011e2b1be43 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, connInfo?: any): 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') || connInfo?.remoteAddr?.hostname; + 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 63f82fce0890..7ced1014a8f6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3299,6 +3299,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 @@ -3314,6 +3315,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 @@ -6118,6 +6120,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 @@ -7207,6 +7220,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: