Skip to content

Commit

Permalink
feat: add cors headers to sendJson
Browse files Browse the repository at this point in the history
  • Loading branch information
rxri committed Jun 4, 2024
1 parent 37c56b8 commit 31b7d18
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
6 changes: 3 additions & 3 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.7.2/schema.json",
"$schema": "https://biomejs.dev/schemas/1.8.0/schema.json",
"organizeImports": {
"enabled": true
},
Expand All @@ -17,11 +17,11 @@
"formatWithErrors": true,
"indentStyle": "tab",
"indentWidth": 2,
"lineWidth": 150
"lineWidth": 120
},
"javascript": {
"formatter": {
"trailingComma": "none",
"trailingCommas": "all",
"arrowParentheses": "asNeeded"
}
},
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "simple-proxy",
"private": true,
"version": "2.1.4",
"version": "2.2.0",
"scripts": {
"prepare": "nitropack prepare",
"dev": "nitropack dev",
Expand All @@ -12,6 +12,9 @@
"preinstall": "npx only-allow pnpm"
},
"dependencies": {
"@elysiajs/cors": "^1.0.2",
"elysia": "^1.0.16",
"eventemitter3": "^5.0.1",
"fetch-cookie": "^3.0.1",
"h3": "^1.10.0",
"nitropack": "^2.8.1"
Expand Down
87 changes: 87 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/routes/[...dest].ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ export default defineEventHandler(async event => {
});
}

if (checkBlacklistedTarget(destination) || !isValidURL(destination)) {
if (
checkBlacklistedTarget(destination) ||
!isValidURL(destination) ||
isCorsOriginAllowed(event.headers.get("origin") ?? undefined, {
origin: (origin: string) => {
return origin !== "https://xpui.app.spotify.com";
}
})
) {
return await sendJson({
event,
status: 400,
Expand Down
5 changes: 5 additions & 0 deletions src/utils/sending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ export async function sendJson(ops: {
status?: number;
}) {
setResponseStatus(ops.event, ops.status ?? 200);
appendCorsHeaders(ops.event, {
origin: (origin: string) => {
return origin === "https://xpui.app.spotify.com";
},
});
await send(ops.event, JSON.stringify(ops.data, null, 2), "application/json");
}

0 comments on commit 31b7d18

Please sign in to comment.