stats-proxy.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import type { APIEvent } from "@solidjs/start/server"
  2. import { Resource } from "@opencode-ai/console-resource"
  3. export async function statsProxy(evt: APIEvent) {
  4. const req = evt.request.clone()
  5. const targetUrl = new URL(req.url)
  6. targetUrl.protocol = "https:"
  7. targetUrl.hostname = Resource.App.stage === "production" ? "stats.opencode.ai" : "stats.dev.opencode.ai"
  8. targetUrl.port = ""
  9. if (targetUrl.pathname.startsWith("/stats/_build/")) {
  10. targetUrl.pathname = targetUrl.pathname.slice("/stats".length)
  11. }
  12. const response = await fetch(targetUrl, {
  13. method: req.method,
  14. headers: req.headers,
  15. body: req.body,
  16. })
  17. if (!response.headers.get("content-type")?.includes("text/html")) return response
  18. const headers = new Headers(response.headers)
  19. headers.delete("content-encoding")
  20. headers.delete("content-length")
  21. headers.delete("etag")
  22. return new Response(rewriteStatsHtml(await response.text()), {
  23. status: response.status,
  24. statusText: response.statusText,
  25. headers,
  26. })
  27. }
  28. function rewriteStatsHtml(html: string) {
  29. return html
  30. .replaceAll("\"/_build/", "\"/stats/_build/")
  31. .replaceAll("'/_build/", "'/stats/_build/")
  32. }