Files
web2app/worker/src/index.ts
shumengya cf8c32061b fix(tauri): bypass WebView CORS for packaged R2/S3 apps
Preserve aws4fetch Request headers in the shell fetch interceptor, and
proxy cross-origin HTTP through a native reqwest command so Windows/Android
builds work even when remote CORS only allows web origins.
2026-07-19 10:11:26 +08:00

53 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Env } from "./env";
import { handleBuildsRequest } from "./routes/builds";
import {
corsPreflightResponse,
jsonResponseWithCors,
} from "./lib/response";
export default {
async fetch(request: Request, env: Env): Promise<Response> {
try {
const url = new URL(request.url);
if (request.method === "OPTIONS" && url.pathname.startsWith("/api/")) {
return corsPreflightResponse(request);
}
if (url.pathname === "/api/health") {
return jsonResponseWithCors(request, { ok: true });
}
if (url.pathname.startsWith("/api/builds")) {
return await handleBuildsRequest(request, env, url);
}
// 静态资源 / SPA 回退(需 wrangler.toml [assets] binding = "ASSETS"
if (!env.ASSETS) {
return jsonResponseWithCors(
request,
{
error:
"ASSETS binding is not configured. Set [assets] binding = \"ASSETS\" in wrangler.toml and redeploy.",
},
500,
);
}
return env.ASSETS.fetch(request);
} catch (error) {
console.error("Unhandled worker error:", error);
return jsonResponseWithCors(
request,
{
error:
error instanceof Error
? error.message
: "Internal server error",
},
500,
);
}
},
};