Skip to content

Commit 58cda16

Browse files
author
akira
committed
fix: Add CORS headers to OPTIONS requests and 204 responses
1 parent ea8ef85 commit 58cda16

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/worker.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ export default {
8686
const { pathname, searchParams } = url;
8787

8888
if (pathname.startsWith("/canary/")) {
89+
if (request.method === "OPTIONS") {
90+
return new Response(null, { status: 204, headers: corsHeaders() });
91+
}
92+
8993
const token = pathname.slice("/canary/".length).split("/")[0];
9094
if (!token) {
91-
return new Response("Not found", { status: 404 });
95+
return new Response("Not found", { status: 404, headers: corsHeaders() });
9296
}
9397

9498
const src = textOrEmpty(searchParams.get("src") || "");
@@ -133,7 +137,7 @@ export default {
133137
const rateKey = `rl:${token}:${ipHash}:${windowId}`;
134138
const rateCount = Number(await env.KANARI_KV.get(rateKey)) || 0;
135139
if (rateCount >= rateLimitMax) {
136-
return new Response(null, { status: 204 });
140+
return new Response(null, { status: 204, headers: corsHeaders() });
137141
}
138142
await env.KANARI_KV.put(rateKey, String(rateCount + 1), {
139143
expirationTtl: rateLimitWindow,
@@ -207,7 +211,7 @@ export default {
207211
console.error("kanariya_error", err);
208212
}
209213

210-
return new Response(null, { status: 204 });
214+
return new Response(null, { status: 204, headers: corsHeaders() });
211215
}
212216

213217
if (pathname === "/admin/export") {

0 commit comments

Comments
 (0)