This commit is contained in:
Soph :3 2025-12-13 13:30:08 +02:00
parent 9229838f6a
commit ef0e135e61

View file

@ -168,11 +168,30 @@ async function runComparison() {
} }
} }
export class ClientResponse extends Response {
constructor(body?: BodyInit, init?: ResponseInit) {
super(body, init);
this.headers.set("Access-Control-Allow-Origin", process.env.ORIGIN!);
this.headers.set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE");
}
static override json(body: unknown, init?: ResponseInit): Response {
return new Response(JSON.stringify(body), {
...init,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": process.env.ORIGIN!,
"Access-Control-Allow-Methods": "OPTIONS, GET, POST, PUT, DELETE",
...init?.headers,
},
});
}
}
Bun.serve({ Bun.serve({
routes: { routes: {
"/": () => new Response("Sheets v2"), "/": () => new ClientResponse("Sheets v2"),
"/artists.ndjson": async () => new Response(await fs.readFile("artists.ndjson")), "/artists.ndjson": async () => new ClientResponse(await fs.readFile("artists.ndjson")),
"/th_artists.ndjson": async () => new Response(await fs.readFile("th_artists.ndjson")), "/th_artists.ndjson": async () => new ClientResponse(await fs.readFile("th_artists.ndjson")),
"/ignore-th/:id": async (req) => { "/ignore-th/:id": async (req) => {
const authFail = requireBasicAuth(req); const authFail = requireBasicAuth(req);
if (authFail) return authFail; if (authFail) return authFail;
@ -181,7 +200,7 @@ Bun.serve({
const changes = changelist_ids[id]; const changes = changelist_ids[id];
if (!changes) { if (!changes) {
return new Response("Id is invalid.", { status: 404 }); return new ClientResponse("Id is invalid.", { status: 404 });
} }
const embed = changes.webhook.embeds[0]; const embed = changes.webhook.embeds[0];
@ -199,7 +218,7 @@ Bun.serve({
delete changelist_ids[id]; delete changelist_ids[id];
await updateChangelistIds(); await updateChangelistIds();
return new Response("Ignored successfully."); return new ClientResponse("Ignored successfully.");
}, },
"/merge-th/:id": async (req) => { "/merge-th/:id": async (req) => {
const authFail = requireBasicAuth(req); const authFail = requireBasicAuth(req);
@ -209,7 +228,7 @@ Bun.serve({
const changes = changelist_ids[id]; const changes = changelist_ids[id];
if (!changes) { if (!changes) {
return new Response("Id is invalid.", { status: 404 }); return new ClientResponse("Id is invalid.", { status: 404 });
} }
const artistsRaw = await fs.readFile("./artists.ndjson", "utf8"); const artistsRaw = await fs.readFile("./artists.ndjson", "utf8");
@ -262,11 +281,11 @@ Bun.serve({
delete changelist_ids[id]; delete changelist_ids[id];
await updateChangelistIds(); await updateChangelistIds();
return new Response("Merged successfully."); return new ClientResponse("Merged successfully.");
} }
}, },
fetch() { fetch() {
return new Response("Unmatched route"); return new ClientResponse("Unmatched route");
}, },
hostname: process.env.HOST || "127.0.0.1", hostname: process.env.HOST || "127.0.0.1",