1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { NextRequest, NextResponse } from "next/server";
- import { getServerSideConfig } from "../../config/server";
- const serverConfig = getServerSideConfig();
- async function handle(
- req: NextRequest,
- { params }: { params: { path: string[] } },
- ) {
- const controller = new AbortController();
- const authValue = req.headers.get("Authorization") ?? "";
- const authHeaderName = serverConfig.isAzure ? "api-key" : "Authorization";
- const fetchUrl = serverConfig.OAUTH_USERINFO;
- console.log("[Proxy] ", fetchUrl);
- // this fix [Org ID] undefined in server side if not using custom point
- const timeoutId = setTimeout(
- () => {
- controller.abort();
- },
- 10 * 60 * 1000,
- );
- if (!fetchUrl) {
- const err = {
- code: 404,
- msg: "未配置",
- };
- return new Response(JSON.stringify(err), {
- status: 404,
- statusText: "not found",
- });
- }
- const fetchOptions: RequestInit = {
- headers: {
- "Content-Type": "application/json",
- "Cache-Control": "no-store",
- [authHeaderName]: authValue,
- },
- method: req.method,
- body: req.body,
- // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
- redirect: "manual",
- // @ts-ignore
- duplex: "half",
- signal: controller.signal,
- };
- try {
- const res = await fetch(fetchUrl, fetchOptions);
- const newHeaders = new Headers(res.headers);
- newHeaders.delete("www-authenticate");
- newHeaders.set("X-Accel-Buffering", "no");
- return new Response(res.body, {
- status: res.status,
- statusText: res.statusText,
- headers: newHeaders,
- });
- } finally {
- clearTimeout(timeoutId);
- }
- }
- export const GET = handle;
- export const POST = handle;
- export const runtime = "edge";
|