Avoid request-time HttpApi layer provisioning (#25179)

This commit is contained in:
Kit Langton 2026-04-30 19:36:57 -04:00 committed by GitHub
commit 2dd1f2d453
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 58 additions and 48 deletions

View file

@ -74,11 +74,10 @@ function app(input?: { password?: string; username?: string }) {
function uiApp(input?: { password?: string; username?: string; client?: Layer.Layer<HttpClient.HttpClient> }) {
const handler = HttpRouter.toWebHandler(
Layer.effectDiscard(
HttpRouter.use((router) =>
Effect.gen(function* () {
const fs = yield* AppFileSystem.Service
const client = yield* HttpClient.HttpClient
const router = yield* HttpRouter.HttpRouter
yield* router.add("*", "/*", (request) => serveUIEffect(request, { fs, client }))
}),
).pipe(

View file

@ -3,6 +3,7 @@ import { Flag } from "@opencode-ai/core/flag/flag"
import { describe, expect } from "bun:test"
import { Context, Effect, Layer, Queue } from "effect"
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
HttpRouter,
@ -66,7 +67,7 @@ type TestHandler<E, R> = (
) => Effect.Effect<HttpServerResponse.HttpServerResponse, E, R>
const workspaceRoutingTestLayer = workspaceRouterMiddleware.layer.pipe(
Layer.provide(Socket.layerWebSocketConstructorGlobal),
Layer.provide([Socket.layerWebSocketConstructorGlobal, FetchHttpClient.layer]),
)
const serverUrl = HttpServer.HttpServer.use((server) => Effect.succeed(HttpServer.formatAddress(server.address)))

View file

@ -1,8 +1,8 @@
import { NodeHttpServer } from "@effect/platform-node"
import { NodeHttpServer, NodeServices } from "@effect/platform-node"
import Http from "node:http"
import { describe, expect } from "bun:test"
import { Context, Effect, Layer, Queue } from "effect"
import { HttpServer, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
import { FetchHttpClient, HttpClient, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
import * as Socket from "effect/unstable/socket/Socket"
import { HttpApiProxy } from "../../src/server/routes/instance/httpapi/middleware/proxy"
import { testEffect } from "../lib/effect"
@ -13,6 +13,8 @@ function serverUrl() {
const testServerLayer = Layer.mergeAll(
NodeHttpServer.layer(Http.createServer, { host: "127.0.0.1", port: 0 }),
NodeServices.layer,
FetchHttpClient.layer,
Socket.layerWebSocketConstructorGlobal,
)
const it = testEffect(testServerLayer)
@ -79,7 +81,8 @@ describe("HttpApi workspace proxy", () => {
const request = HttpServerRequest.fromWeb(
new Request("http://localhost/session/abc", { method: "POST", body: "request-body" }),
)
const response = yield* HttpApiProxy.http(`${url}/session/abc?keep=yes`, { "x-extra": "injected" }, request)
const httpClient = yield* HttpClient.HttpClient
const response = yield* HttpApiProxy.http(httpClient, `${url}/session/abc?keep=yes`, { "x-extra": "injected" }, request)
expect(response.status).toBe(201)
const client = HttpServerResponse.toClientResponse(response)
@ -97,7 +100,8 @@ describe("HttpApi workspace proxy", () => {
it.live("returns 500 when remote is unreachable", () =>
Effect.gen(function* () {
const request = HttpServerRequest.fromWeb(new Request("http://localhost/anything"))
const response = yield* HttpApiProxy.http("http://127.0.0.1:1/unreachable", undefined, request)
const httpClient = yield* HttpClient.HttpClient
const response = yield* HttpApiProxy.http(httpClient, "http://127.0.0.1:1/unreachable", undefined, request)
expect(response.status).toBe(500)
}),
@ -122,7 +126,8 @@ describe("HttpApi workspace proxy", () => {
},
}),
)
yield* HttpApiProxy.http(`${url}/test`, { "x-injected": "extra" }, request)
const httpClient = yield* HttpClient.HttpClient
yield* HttpApiProxy.http(httpClient, `${url}/test`, { "x-injected": "extra" }, request)
expect(forwarded["x-opencode-directory"]).toBeUndefined()
expect(forwarded["x-opencode-workspace"]).toBeUndefined()