From 8447313a7658c5adf0a076f7a6cb4a15315adbb7 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 11 Oct 2024 15:15:27 +0100 Subject: [PATCH] fix: revert regression of port computation --- .changeset/tidy-dancers-act.md | 5 +++++ packages/astro/src/core/app/node.ts | 11 +++++------ 2 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 .changeset/tidy-dancers-act.md diff --git a/.changeset/tidy-dancers-act.md b/.changeset/tidy-dancers-act.md new file mode 100644 index 000000000000..abe7895da123 --- /dev/null +++ b/.changeset/tidy-dancers-act.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix a regression where a port was incorrectly added to the `Astro.url` diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index 87ef2a0defad..aa3159d407a2 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -85,11 +85,11 @@ export class NodeApp extends App { // @example "443,8080,80" => "443" const forwardedPort = getFirstForwardedValue(req.headers['x-forwarded-port']); const port = - forwardedPort ?? req.socket?.remotePort?.toString() ?? (isEncrypted ? '443' : '80'); + forwardedPort ?? (isEncrypted ? '443' : '80'); const portInHostname = typeof hostname === 'string' && /:\d+$/.test(hostname); - const hostnamePort = portInHostname ? hostname : `${hostname}:${port}`; - + const hostnamePort = portInHostname ? hostname : `${hostname}${port ? `:${port}` : ''}`; + const url = `${protocol}://${hostnamePort}${req.url}`; const options: RequestInit = { method: req.method || 'GET', @@ -105,9 +105,8 @@ export class NodeApp extends App { // Get the IP of end client behind the proxy. // @example "1.1.1.1,8.8.8.8" => "1.1.1.1" const forwardedClientIp = getFirstForwardedValue(req.headers['x-forwarded-for']); - const clientIp = forwardedClientIp || req.socket?.remoteAddress; - if (clientIp) { - Reflect.set(request, clientAddressSymbol, clientIp); + if (forwardedClientIp) { + Reflect.set(request, clientAddressSymbol, forwardedClientIp); } return request;