From b5678a64d2dcf972a74bd65fc81d689e775e9a4f Mon Sep 17 00:00:00 2001 From: Philipp Zerelles Date: Tue, 20 Feb 2024 08:19:07 +0100 Subject: [PATCH 1/2] fix: preserve body in request copy --- src/handler.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/handler.js b/src/handler.js index 92c0e21..f0f1d15 100644 --- a/src/handler.js +++ b/src/handler.js @@ -16,11 +16,11 @@ const server = new Server(manifest); await server.init({ env: (Bun || process).env }); const xff_depth = parseInt(env("XFF_DEPTH", build_options.xff_depth ?? 1)); -const origin = env('ORIGIN', undefined); +const origin = env("ORIGIN", undefined); const address_header = env("ADDRESS_HEADER", "").toLowerCase(); const protocol_header = env("PROTOCOL_HEADER", "").toLowerCase(); -const host_header = env("HOST_HEADER", "").toLowerCase(); +const host_header = env("HOST_HEADER", "host").toLowerCase(); /** @param {boolean} assets */ export default function (assets) { @@ -92,13 +92,26 @@ function serve(path, client = false) { ); } -/**@param {Request} req */ -function ssr(req) { - let request = req; - let url = req.url; - let path = url.slice(url.split("/", 3).join("/").length); - let base = origin || get_origin(req.headers); - request = new Request(base + path, req); +/**@param {Request} request */ +function ssr(request) { + if (origin) { + const requestOrigin = get_origin(request.headers); + if (origin !== requestOrigin) { + const url = request.url.slice(request.url.split("/", 3).join("/").length); + request = new Request(origin + url, { + method: request.method, + headers: request.headers, + body: request.body, + referrer: request.referrer, + referrerPolicy: request.referrerPolicy, + mode: request.mode, + credentials: request.credentials, + cache: request.cache, + redirect: request.redirect, + integrity: request.integrity, + }); + } + } if (address_header && !request.headers.has(address_header)) { throw new Error( From fb17cafa1aad7d585e0c9f73e4868a5dd0440fe7 Mon Sep 17 00:00:00 2001 From: Philipp Zerelles Date: Tue, 20 Feb 2024 08:31:26 +0100 Subject: [PATCH 2/2] add prepare script --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 06f910f..9fbef0d 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ ], "scripts": { "build": "rm -fr files && bun run build.js", + "prepare": "bun run build", "lint": "prettier --check .", "format": "prettier --write ." },