Use Deno API to set Astro.clientAddress (#6651)

* Use Deno API to set Astro.clientAddress

* Add types
This commit is contained in:
Matthew Phillips 2023-04-06 15:40:50 -04:00 committed by GitHub
parent 9e88e0f23c
commit 416ceb9730
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/netlify': patch
---
Use Deno API to set Astro.clientAddress in Netlify Edge

View file

@ -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:*",

View file

@ -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<Response | void> => {
const handler = async (request: Request, context: Context): Promise<Response | void> => {
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) {

View file

@ -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: