Download packages/server/src/cors.ts from SaylorTwift/opencode: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/SaylorTwift/opencode/resolve/main/packages/server/src/cors.ts
- Command line
-
hf download hf://SaylorTwift/opencode/packages/server/src/cors.ts
-
curl -L -o cors.ts https://huggingface.co/SaylorTwift/opencode/resolve/main/packages/server/src/cors.ts
1.18 kB
| import { Context } from "effect" | |
| const opencodeOrigin = /^https:\/\/([a-z0-9-]+\.)*opencode\.ai$/ | |
| export type CorsOptions = { readonly cors?: ReadonlyArray<string> } | |
| export const CorsConfig = Context.Reference<CorsOptions | undefined>("@opencode/ServerCorsConfig", { | |
| defaultValue: () => undefined, | |
| }) | |
| export function isAllowedCorsOrigin(input: string | undefined, opts?: CorsOptions) { | |
| if (!input) return true | |
| if (input.startsWith("http://localhost:")) return true | |
| if (input.startsWith("http://127.0.0.1:")) return true | |
| if (input.startsWith("oc://renderer")) return true | |
| if (input === "tauri://localhost" || input === "http://tauri.localhost" || input === "https://tauri.localhost") | |
| return true | |
| if (opencodeOrigin.test(input)) return true | |
| return opts?.cors?.includes(input) ?? false | |
| } | |
| export function isAllowedRequestOrigin(input: string | undefined, host: string | undefined, opts?: CorsOptions) { | |
| if (!input) return true | |
| if (host && sameHost(input, host)) return true | |
| return isAllowedCorsOrigin(input, opts) | |
| } | |
| function sameHost(origin: string, host: string) { | |
| try { | |
| return new URL(origin).host === host | |
| } catch { | |
| return false | |
| } | |
| } | |