mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-06 08:21:50 +00:00
chore: generate
This commit is contained in:
parent
e0305e47f3
commit
247284b9af
3 changed files with 16 additions and 27 deletions
|
|
@ -123,9 +123,7 @@ const instanceRoutes = Layer.mergeAll(rawInstanceRoutes, instanceApiRoutes).pipe
|
|||
|
||||
const uiRoute = HttpRouter.add("*", "/*", (request) =>
|
||||
serveUIEffect(request).pipe(Effect.provide(AppFileSystem.defaultLayer), Effect.provide(FetchHttpClient.layer)),
|
||||
).pipe(
|
||||
Layer.provide(authorizationRouterMiddleware.layer.pipe(Layer.provide(ServerAuthConfig.defaultLayer))),
|
||||
)
|
||||
).pipe(Layer.provide(authorizationRouterMiddleware.layer.pipe(Layer.provide(ServerAuthConfig.defaultLayer))))
|
||||
|
||||
export const routes = Layer.mergeAll(rootApiRoutes, instanceRoutes, uiRoute).pipe(
|
||||
Layer.provide([
|
||||
|
|
|
|||
|
|
@ -22,19 +22,13 @@ const csp = (hash = "") =>
|
|||
`default-src 'self'; script-src 'self' 'wasm-unsafe-eval'${hash ? ` 'sha256-${hash}'` : ""}; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; media-src 'self' data:; connect-src 'self' data:`
|
||||
|
||||
function themePreloadHash(body: string) {
|
||||
return body.match(
|
||||
/<script\b(?![^>]*\bsrc\s*=)[^>]*\bid=(['"])oc-theme-preload-script\1[^>]*>([\s\S]*?)<\/script>/i,
|
||||
)
|
||||
return body.match(/<script\b(?![^>]*\bsrc\s*=)[^>]*\bid=(['"])oc-theme-preload-script\1[^>]*>([\s\S]*?)<\/script>/i)
|
||||
}
|
||||
|
||||
function requestBody(request: HttpServerRequest.HttpServerRequest) {
|
||||
if (request.method === "GET" || request.method === "HEAD") return HttpBody.empty
|
||||
const len = request.headers["content-length"]
|
||||
return HttpBody.stream(
|
||||
request.stream,
|
||||
request.headers["content-type"],
|
||||
len === undefined ? undefined : Number(len),
|
||||
)
|
||||
return HttpBody.stream(request.stream, request.headers["content-type"], len === undefined ? undefined : Number(len))
|
||||
}
|
||||
|
||||
function proxyResponseHeaders(headers: Record<string, string>) {
|
||||
|
|
@ -116,10 +110,7 @@ export function serveUIEffect(request: HttpServerRequest.HttpServerRequest) {
|
|||
if (response.headers["content-type"]?.includes("text/html")) {
|
||||
const body = yield* response.text
|
||||
const match = themePreloadHash(body)
|
||||
headers.set(
|
||||
"Content-Security-Policy",
|
||||
csp(match ? createHash("sha256").update(match[2]).digest("base64") : ""),
|
||||
)
|
||||
headers.set("Content-Security-Policy", csp(match ? createHash("sha256").update(match[2]).digest("base64") : ""))
|
||||
return HttpServerResponse.text(body, { status: response.status, headers })
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,10 @@ import {
|
|||
HttpServerResponse,
|
||||
} from "effect/unstable/http"
|
||||
import { AppFileSystem } from "@opencode-ai/core/filesystem"
|
||||
import { ServerAuthConfig, authorizationRouterMiddleware } from "../../src/server/routes/instance/httpapi/middleware/authorization"
|
||||
import {
|
||||
ServerAuthConfig,
|
||||
authorizationRouterMiddleware,
|
||||
} from "../../src/server/routes/instance/httpapi/middleware/authorization"
|
||||
import { ExperimentalHttpApiServer } from "../../src/server/routes/instance/httpapi/server"
|
||||
import { serveUIEffect } from "../../src/server/routes/ui"
|
||||
import { Server } from "../../src/server/server"
|
||||
|
|
@ -69,11 +72,7 @@ function app(input?: { password?: string; username?: string }) {
|
|||
}
|
||||
}
|
||||
|
||||
function uiApp(input?: {
|
||||
password?: string
|
||||
username?: string
|
||||
client?: Layer.Layer<HttpClient.HttpClient>
|
||||
}) {
|
||||
function uiApp(input?: { password?: string; username?: string; client?: Layer.Layer<HttpClient.HttpClient> }) {
|
||||
const handler = HttpRouter.toWebHandler(
|
||||
HttpRouter.add("*", "/*", (request) =>
|
||||
serveUIEffect(request).pipe(
|
||||
|
|
@ -121,9 +120,12 @@ describe("HttpApi UI fallback", () => {
|
|||
let proxiedUrl: string | undefined
|
||||
|
||||
const response = await uiApp({
|
||||
client: httpClient(new Response("<html>opencode</html>", { headers: { "content-type": "text/html" } }), (request) => {
|
||||
proxiedUrl = request.url
|
||||
}),
|
||||
client: httpClient(
|
||||
new Response("<html>opencode</html>", { headers: { "content-type": "text/html" } }),
|
||||
(request) => {
|
||||
proxiedUrl = request.url
|
||||
},
|
||||
),
|
||||
}).request("/")
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
|
|
@ -197,9 +199,7 @@ describe("HttpApi UI fallback", () => {
|
|||
password: "secret",
|
||||
username: "opencode",
|
||||
client: httpClient(new Response("<html>opencode</html>", { headers: { "content-type": "text/html" } })),
|
||||
}).request(
|
||||
`/?auth_token=${btoa("opencode:secret")}`,
|
||||
)
|
||||
}).request(`/?auth_token=${btoa("opencode:secret")}`)
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(await response.text()).toBe("<html>opencode</html>")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue