|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import { getServerSideConfig } from "@/app/config/server"; |
| 3 | +import { ModelProvider, STABILITY_BASE_URL } from "@/app/constant"; |
| 4 | +import { auth } from "@/app/api/auth"; |
| 5 | + |
| 6 | +async function handle( |
| 7 | + req: NextRequest, |
| 8 | + { params }: { params: { path: string[] } }, |
| 9 | +) { |
| 10 | + console.log("[Stability] params ", params); |
| 11 | + |
| 12 | + if (req.method === "OPTIONS") { |
| 13 | + return NextResponse.json({ body: "OK" }, { status: 200 }); |
| 14 | + } |
| 15 | + |
| 16 | + const controller = new AbortController(); |
| 17 | + |
| 18 | + const serverConfig = getServerSideConfig(); |
| 19 | + |
| 20 | + let baseUrl = serverConfig.stabilityUrl || STABILITY_BASE_URL; |
| 21 | + |
| 22 | + if (!baseUrl.startsWith("http")) { |
| 23 | + baseUrl = `https://${baseUrl}`; |
| 24 | + } |
| 25 | + |
| 26 | + if (baseUrl.endsWith("/")) { |
| 27 | + baseUrl = baseUrl.slice(0, -1); |
| 28 | + } |
| 29 | + |
| 30 | + let path = `${req.nextUrl.pathname}`.replaceAll("/api/stability/", ""); |
| 31 | + |
| 32 | + console.log("[Stability Proxy] ", path); |
| 33 | + console.log("[Stability Base Url]", baseUrl); |
| 34 | + |
| 35 | + const timeoutId = setTimeout( |
| 36 | + () => { |
| 37 | + controller.abort(); |
| 38 | + }, |
| 39 | + 10 * 60 * 1000, |
| 40 | + ); |
| 41 | + |
| 42 | + const authResult = auth(req, ModelProvider.Stability); |
| 43 | + |
| 44 | + if (authResult.error) { |
| 45 | + return NextResponse.json(authResult, { |
| 46 | + status: 401, |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + const bearToken = req.headers.get("Authorization") ?? ""; |
| 51 | + const token = bearToken.trim().replaceAll("Bearer ", "").trim(); |
| 52 | + |
| 53 | + const key = token ? token : serverConfig.stabilityApiKey; |
| 54 | + |
| 55 | + if (!key) { |
| 56 | + return NextResponse.json( |
| 57 | + { |
| 58 | + error: true, |
| 59 | + message: `missing STABILITY_API_KEY in server env vars`, |
| 60 | + }, |
| 61 | + { |
| 62 | + status: 401, |
| 63 | + }, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + const fetchUrl = `${baseUrl}/${path}`; |
| 68 | + console.log("[Stability Url] ", fetchUrl); |
| 69 | + const fetchOptions: RequestInit = { |
| 70 | + headers: { |
| 71 | + "Content-Type": req.headers.get("Content-Type") || "multipart/form-data", |
| 72 | + Accept: req.headers.get("Accept") || "application/json", |
| 73 | + Authorization: `Bearer ${key}`, |
| 74 | + }, |
| 75 | + method: req.method, |
| 76 | + body: req.body, |
| 77 | + // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body |
| 78 | + redirect: "manual", |
| 79 | + // @ts-ignore |
| 80 | + duplex: "half", |
| 81 | + signal: controller.signal, |
| 82 | + }; |
| 83 | + |
| 84 | + try { |
| 85 | + const res = await fetch(fetchUrl, fetchOptions); |
| 86 | + // to prevent browser prompt for credentials |
| 87 | + const newHeaders = new Headers(res.headers); |
| 88 | + newHeaders.delete("www-authenticate"); |
| 89 | + // to disable nginx buffering |
| 90 | + newHeaders.set("X-Accel-Buffering", "no"); |
| 91 | + return new Response(res.body, { |
| 92 | + status: res.status, |
| 93 | + statusText: res.statusText, |
| 94 | + headers: newHeaders, |
| 95 | + }); |
| 96 | + } finally { |
| 97 | + clearTimeout(timeoutId); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export const GET = handle; |
| 102 | +export const POST = handle; |
| 103 | + |
| 104 | +export const runtime = "edge"; |
0 commit comments