Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
fix: cors server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanleecode committed Apr 3, 2023
1 parent 90274d2 commit b4cf3f9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
28 changes: 17 additions & 11 deletions providers/frame/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,23 @@ export const createUsers ${
}

export async function handleCount(request: Request, cache: { count: number }): Promise<Response> {
const body = await request.json()
$.assert($.field("count", $.u32), body)
const { count } = body
const index = cache.count
const newCount = index + count
if (newCount < DEFAULT_TEST_USER_COUNT) cache.count = newCount
else throw new Error("Maximum test user count reached")
return new Response(JSON.stringify({ index }), {
status: 200,
headers: { "Content-Type": "application/json" },
})
switch (request.method) {
case "POST": {
const body = await request.json()
$.assert($.field("count", $.u32), body)
const { count } = body
const index = cache.count
const newCount = index + count
if (newCount < DEFAULT_TEST_USER_COUNT) cache.count = newCount
else throw new Error("Maximum test user count reached")
return new Response(JSON.stringify({ index }), {
status: 200,
headers: { "Content-Type": "application/json" },
})
}
default:
return new Response(null, { status: 200 })
}
}

export async function generateTar(_files: Map<string, string>, chainName: string, version: string) {
Expand Down
4 changes: 2 additions & 2 deletions server/capi.dev/delegator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { serve } from "../../deps/std/http.ts"
import { TimedMemo } from "../../util/memo.ts"
import { f, handleErrors } from "../mod.ts"
import { f, handleCors, handleErrors } from "../mod.ts"

serve(handleErrors(handler))
serve(handleCors(handleErrors(handler)))

const ttl = 60_000
const shaAbbrevLength = 8
Expand Down
33 changes: 28 additions & 5 deletions server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as f from "./factories.ts"
import { parsePathInfo } from "./PathInfo.ts"

export function handler(env: Env): Handler {
return handleErrors(async (request) => {
return handleCors(handleErrors(async (request) => {
const url = new URL(request.url)
const { pathname } = url
if (pathname === "/") return new Response("capi dev server active")
Expand Down Expand Up @@ -34,19 +34,42 @@ export function handler(env: Env): Handler {
} catch (_e) {}
}
return f.notFound()
})
}))
}

const staticDirs = ["../", "./static/"].map((p) => import.meta.resolve(p))

export function handleErrors(handler: (request: Request) => Promise<Response>) {
return async (request: Request) => {
export function handleErrors(handler: Handler): Handler {
return async (request, connInfo) => {
try {
return await handler(request)
return await handler(request, connInfo)
} catch (e) {
if (e instanceof Response) return e.clone()
console.error(e)
return f.serverError(Deno.inspect(e))
}
}
}

export function handleCors(handler: Handler): Handler {
return async (request, connInfo) => {
const res = await handler(request, connInfo)

// Deno.upgradeWebSocket response objects cannot be modified
if (res.headers.get("upgrade") !== "websocket") {
const newHeaders = new Headers(res.headers)
newHeaders.set("Access-Control-Allow-Origin", "*")
newHeaders.set("Access-Control-Allow-Headers", "*")
newHeaders.set("Access-Control-Allow-Methods", "*")
newHeaders.set("Access-Control-Allow-Credentials", "true")

return new Response(res.body, {
headers: newHeaders,
status: res.status,
statusText: res.statusText,
})
}

return res
}
}

0 comments on commit b4cf3f9

Please sign in to comment.