this gotta work

This commit is contained in:
Soph :3 2026-01-05 02:22:48 +02:00
parent 6b6e4638d8
commit 255d185964

View file

@ -195,13 +195,14 @@ async function runComparison() {
} }
export class ClientResponse extends Response { export class ClientResponse extends Response {
constructor(url: URL, body?: BodyInit, init?: ResponseInit) { constructor(req: Request, body?: BodyInit, init?: ResponseInit) {
super(body, init); super(body, init);
const origin = req.headers.get("Origin");
const origins = process.env.ORIGIN!.split(","); const origins = process.env.ORIGIN!.split(",");
if (origins.includes(url.origin)) { if (origin && origins.includes(origin)) {
this.headers.set("Access-Control-Allow-Origin", url.origin); this.headers.set("Access-Control-Allow-Origin", origin);
this.headers.set( this.headers.set(
"Access-Control-Allow-Methods", "Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS", "GET, POST, PUT, DELETE, OPTIONS",
@ -209,7 +210,7 @@ export class ClientResponse extends Response {
} }
} }
static json_c(body: unknown, url: URL, init?: ResponseInit): Response { static json_c(body: unknown, req: Request, init?: ResponseInit): Response {
const res = new Response(JSON.stringify(body), { const res = new Response(JSON.stringify(body), {
...init, ...init,
headers: { headers: {
@ -218,31 +219,30 @@ export class ClientResponse extends Response {
}, },
}); });
const origin = req.headers.get("Origin");
const origins = process.env.ORIGIN!.split(","); const origins = process.env.ORIGIN!.split(",");
if (origins.includes(url.origin)) { if (origin && origins.includes(origin)) {
res.headers.set("Access-Control-Allow-Origin", url.origin); res.headers.set("Access-Control-Allow-Origin", origin);
res.headers.set( res.headers.set(
"Access-Control-Allow-Methods", "Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS", "GET, POST, PUT, DELETE, OPTIONS",
); );
return res;
} else {
return res;
} }
return res;
} }
} }
Bun.serve({ Bun.serve({
routes: { routes: {
"/": (req) => new ClientResponse(new URL(req.url), "Sheets v2"), "/": (req) => new ClientResponse(req, "Sheets v2"),
"/artists.json": async (req) => "/artists.json": async (req) =>
ClientResponse.json_c( ClientResponse.json_c(
ndjsonToJson((await fs.readFile("artists.ndjson")).toString("utf8")), ndjsonToJson((await fs.readFile("artists.ndjson")).toString("utf8")),
new URL(req.url), req,
), ),
"/artists.csv": async (req) => "/artists.csv": async (req) =>
new ClientResponse( new ClientResponse(
new URL(req.url), req,
jsonToCsv( jsonToCsv(
ndjsonToJson((await fs.readFile("artists.ndjson")).toString("utf8")), ndjsonToJson((await fs.readFile("artists.ndjson")).toString("utf8")),
), ),
@ -253,12 +253,9 @@ Bun.serve({
}, },
), ),
"/artists.ndjson": async (req) => "/artists.ndjson": async (req) =>
new ClientResponse(new URL(req.url), await fs.readFile("artists.ndjson")), new ClientResponse(req, await fs.readFile("artists.ndjson")),
"/th_artists.ndjson": async (req) => "/th_artists.ndjson": async (req) =>
new ClientResponse( new ClientResponse(req, await fs.readFile("th_artists.ndjson")),
new URL(req.url),
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;
@ -267,7 +264,7 @@ Bun.serve({
const changes = changelist_ids[id]; const changes = changelist_ids[id];
if (!changes) { if (!changes) {
return new ClientResponse(new URL(req.url), "Id is invalid.", { return new ClientResponse(req, "Id is invalid.", {
status: 404, status: 404,
}); });
} }
@ -293,7 +290,7 @@ Bun.serve({
delete changelist_ids[id]; delete changelist_ids[id];
await updateChangelistIds(); await updateChangelistIds();
return new ClientResponse(new URL(req.url), "Ignored successfully."); return new ClientResponse(req, "Ignored successfully.");
}, },
"/merge-th/:id": async (req) => { "/merge-th/:id": async (req) => {
const authFail = requireBasicAuth(req); const authFail = requireBasicAuth(req);
@ -303,7 +300,7 @@ Bun.serve({
const changes = changelist_ids[id]; const changes = changelist_ids[id];
if (!changes) { if (!changes) {
return new ClientResponse(new URL(req.url), "Id is invalid.", { return new ClientResponse(req, "Id is invalid.", {
status: 404, status: 404,
}); });
} }
@ -380,7 +377,7 @@ Bun.serve({
} catch (err) { } catch (err) {
console.error("Error:", err); console.error("Error:", err);
return new ClientResponse( return new ClientResponse(
new URL(req.url), req,
"Failed to automerge to git. Error: " + err, "Failed to automerge to git. Error: " + err,
{ {
status: 400, status: 400,
@ -388,11 +385,11 @@ Bun.serve({
); );
} }
return new ClientResponse(new URL(req.url), "Merged successfully."); return new ClientResponse(req, "Merged successfully.");
}, },
}, },
fetch(req) { fetch(req) {
return new ClientResponse(new URL(req.url), "Unmatched route"); return new ClientResponse(req, "Unmatched route");
}, },
hostname: process.env.HOST || "127.0.0.1", hostname: process.env.HOST || "127.0.0.1",