Commit f815f0d9 authored by ChanHaeng Lee's avatar ChanHaeng Lee
Browse files

Replace host using "x-forwarded-fetch"

parent c8cec07c
Loading
Loading
Loading
Loading
+5 −12
Original line number Diff line number Diff line
import { getXForwardedRequest } from "x-forwarded-fetch";

/**
 * Replaces the host of the request with the value of the
 * x-forwarded-host header, if present.
@@ -5,15 +7,6 @@
 * @param handler The request handler to wrap.
 * @return A new request handler with the host replaced.
 */
export function replaceHost(handler: (request: Request) => unknown) {
  return (request: Request) => {
    const forwardedHost = request.headers.get("x-forwarded-host");
    if (!forwardedHost) return handler(request);
    const headers = new Headers(request.headers);
    headers.append("host", forwardedHost);
    const url = new URL(request.url);
    url.host = forwardedHost;
    url.port = "";
    return handler(new Request(url, { ...request, headers }));
  };
}
export const replaceHost =
  (handler: (request: Request) => unknown) => (request: Request) =>
    getXForwardedRequest(request).then(handler);
+6 −6
Original line number Diff line number Diff line
@@ -7,24 +7,24 @@
    "build": "next build --turbopack",
    "start": "next start",
    "lint": "eslint"
    "test": "true"
  },
  "dependencies": {
    "@fedify/fedify": "workspace:",
    "@fedify/next": "workspace:",
    "next": "15.5.0",
    "react": "19.1.0",
    "react-dom": "19.1.0",
    "next": "15.5.0"
    "x-forwarded-fetch": "^0.2.0"
  },
  "devDependencies": {
    "typescript": "^5",
    "@eslint/eslintrc": "^3",
    "@tailwindcss/postcss": "^4",
    "@types/node": "^20",
    "@types/react": "^19",
    "@types/react-dom": "^19",
    "@tailwindcss/postcss": "^4",
    "tailwindcss": "^4",
    "eslint": "^9",
    "eslint-config-next": "15.5.0",
    "@eslint/eslintrc": "^3"
    "tailwindcss": "^4",
    "typescript": "^5"
  }
}
+3 −0
Original line number Diff line number Diff line
@@ -297,6 +297,9 @@ importers:
      react-dom:
        specifier: 19.1.0
        version: 19.1.0(react@19.1.0)
      x-forwarded-fetch:
        specifier: ^0.2.0
        version: 0.2.0
    devDependencies:
      '@eslint/eslintrc':
        specifier: ^3