mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 23:24:00 +03:00
fix(client): preserve base URL path prefix in promise client (#50428)
Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
067a528b1d
commit
10aa949f43
@@ -292,7 +292,10 @@ export function make(options: ClientOptions) {
|
||||
const fetch = options.fetch ?? globalThis.fetch
|
||||
|
||||
const prepare = (descriptor: RequestDescriptor, requestOptions?: RequestOptions) => {
|
||||
const url = new URL(descriptor.path, options.baseUrl)
|
||||
// A leading slash would replace any path prefix on baseUrl, so join relative to it.
|
||||
const baseUrl = new URL(options.baseUrl)
|
||||
if (!baseUrl.pathname.endsWith("/")) baseUrl.pathname += "/"
|
||||
const url = new URL(descriptor.path.slice(1), baseUrl)
|
||||
for (const [key, value] of Object.entries(descriptor.query ?? {})) appendQuery(url.searchParams, key, value)
|
||||
const headers = new Headers(options.headers)
|
||||
for (const [key, value] of Object.entries(descriptor.headers ?? {})) {
|
||||
|
||||
@@ -26,7 +26,9 @@ export function createPtyClient(api: OpenCodeClient, options: PtyClientOptions)
|
||||
location: input.location,
|
||||
"x-opencode-ticket": "1",
|
||||
})
|
||||
const url = new URL(`/api/pty/${encodeURIComponent(input.ptyID)}/connect`, options.url)
|
||||
const baseUrl = new URL(options.url)
|
||||
if (!baseUrl.pathname.endsWith("/")) baseUrl.pathname += "/"
|
||||
const url = new URL(`api/pty/${encodeURIComponent(input.ptyID)}/connect`, baseUrl)
|
||||
if (input.location?.directory) url.searchParams.set("location[directory]", input.location.directory)
|
||||
if (input.cursor !== undefined) url.searchParams.set("cursor", String(input.cursor))
|
||||
url.searchParams.set("ticket", result.data.ticket)
|
||||
@@ -46,7 +48,9 @@ export function createPersistentPtyClient(api: OpenCodeClient, options: PtyClien
|
||||
ptyID: input.ptyID,
|
||||
"x-opencode-ticket": "1",
|
||||
})
|
||||
const url = new URL(`/api/experimental/persistent-pty/${encodeURIComponent(input.ptyID)}/connect`, options.url)
|
||||
const baseUrl = new URL(options.url)
|
||||
if (!baseUrl.pathname.endsWith("/")) baseUrl.pathname += "/"
|
||||
const url = new URL(`api/experimental/persistent-pty/${encodeURIComponent(input.ptyID)}/connect`, baseUrl)
|
||||
url.searchParams.set("ticket", token.ticket)
|
||||
url.searchParams.set("cursor", String(input.cursor))
|
||||
url.searchParams.set("attachment_id", input.attachmentID)
|
||||
|
||||
@@ -83,6 +83,34 @@ test("config.get returns ordered config entries for a location", async () => {
|
||||
expect(request?.url).toBe("http://localhost:3000/api/config?location%5Bdirectory%5D=%2Ftmp%2Fproject")
|
||||
})
|
||||
|
||||
test("requests keep a path prefix on baseUrl", async () => {
|
||||
let request: Request | undefined
|
||||
const client = OpenCode.make({
|
||||
baseUrl: "http://localhost:8888/ws/abc",
|
||||
fetch: async (input) => {
|
||||
request = input instanceof Request ? input : new Request(input)
|
||||
return Response.json({ version: "2.0.0", pid: 1, urls: [], paths: { tmp: "/tmp" } })
|
||||
},
|
||||
})
|
||||
|
||||
await client.server.info()
|
||||
expect(request?.url).toBe("http://localhost:8888/ws/abc/api/info")
|
||||
})
|
||||
|
||||
test("requests join against the base URL pathname", async () => {
|
||||
let request: Request | undefined
|
||||
const client = OpenCode.make({
|
||||
baseUrl: "http://localhost:8888/ws/abc?tenant=one#fragment",
|
||||
fetch: async (input) => {
|
||||
request = input instanceof Request ? input : new Request(input)
|
||||
return Response.json({ version: "2.0.0", pid: 1, urls: [], paths: { tmp: "/tmp" } })
|
||||
},
|
||||
})
|
||||
|
||||
await client.server.info()
|
||||
expect(request?.url).toBe("http://localhost:8888/ws/abc/api/info")
|
||||
})
|
||||
|
||||
test("vcs.base and committed diffs preserve location and explicit base on the wire", async () => {
|
||||
const requests: Request[] = []
|
||||
const location = { directory: "/repo", project: { id: "global", directory: "/repo", canonical: "/repo" } }
|
||||
|
||||
@@ -8,7 +8,7 @@ describe("createPtyClient", () => {
|
||||
let socketURL: URL | undefined
|
||||
const socket = { binaryType: "blob" } as unknown as WebSocket
|
||||
const api = OpenCode.make({
|
||||
baseUrl: "https://server.example/base",
|
||||
baseUrl: "https://server.example",
|
||||
headers: { Authorization: "Basic credential" },
|
||||
fetch: async (input, init) => {
|
||||
request = input instanceof Request ? input : new Request(input, init)
|
||||
@@ -22,7 +22,7 @@ describe("createPtyClient", () => {
|
||||
},
|
||||
})
|
||||
const pty = createPtyClient(api, {
|
||||
url: "https://server.example/base",
|
||||
url: "https://server.example",
|
||||
openSocket(url) {
|
||||
socketURL = url
|
||||
return socket
|
||||
@@ -65,6 +65,31 @@ describe("createPtyClient", () => {
|
||||
await expect(pty.connect({ ptyID: "pty_1", location: { directory: "/repo" } })).rejects.toThrow()
|
||||
expect(opened).toBe(false)
|
||||
})
|
||||
|
||||
test("keeps the server path prefix when opening the terminal socket", async () => {
|
||||
let socketURL: URL | undefined
|
||||
const api = OpenCode.make({
|
||||
baseUrl: "https://server.example/base",
|
||||
fetch: async () =>
|
||||
Response.json({
|
||||
location: {
|
||||
directory: "/repo",
|
||||
project: { id: "project_1", directory: "/repo", canonical: "/repo" },
|
||||
},
|
||||
data: { ticket: "ticket-1", expires_in: 60 },
|
||||
}),
|
||||
})
|
||||
const pty = createPtyClient(api, {
|
||||
url: "https://server.example/base",
|
||||
openSocket(url) {
|
||||
socketURL = url
|
||||
return { binaryType: "blob" } as unknown as WebSocket
|
||||
},
|
||||
})
|
||||
|
||||
await pty.connect({ ptyID: "pty_1" })
|
||||
expect(socketURL?.toString()).toBe("wss://server.example/base/api/pty/pty_1/connect?ticket=ticket-1")
|
||||
})
|
||||
})
|
||||
|
||||
describe("createPersistentPtyClient", () => {
|
||||
@@ -73,7 +98,7 @@ describe("createPersistentPtyClient", () => {
|
||||
let socketURL: URL | undefined
|
||||
const socket = { binaryType: "blob" } as unknown as WebSocket
|
||||
const api = OpenCode.make({
|
||||
baseUrl: "https://server.example/base",
|
||||
baseUrl: "https://server.example",
|
||||
headers: { Authorization: "Basic credential" },
|
||||
fetch: async (input, init) => {
|
||||
request = input instanceof Request ? input : new Request(input, init)
|
||||
@@ -81,7 +106,7 @@ describe("createPersistentPtyClient", () => {
|
||||
},
|
||||
})
|
||||
const pty = createPersistentPtyClient(api, {
|
||||
url: "https://server.example/base",
|
||||
url: "https://server.example",
|
||||
openSocket(url) {
|
||||
socketURL = url
|
||||
return socket
|
||||
@@ -98,4 +123,24 @@ describe("createPersistentPtyClient", () => {
|
||||
)
|
||||
expect(socket.binaryType).toBe("arraybuffer")
|
||||
})
|
||||
|
||||
test("keeps the server path prefix when opening the persistent terminal socket", async () => {
|
||||
let socketURL: URL | undefined
|
||||
const api = OpenCode.make({
|
||||
baseUrl: "https://server.example/base",
|
||||
fetch: async () => Response.json({ data: { ticket: "ticket-1", expires_in: 60 } }),
|
||||
})
|
||||
const pty = createPersistentPtyClient(api, {
|
||||
url: "https://server.example/base",
|
||||
openSocket(url) {
|
||||
socketURL = url
|
||||
return { binaryType: "blob" } as unknown as WebSocket
|
||||
},
|
||||
})
|
||||
|
||||
await pty.connect({ ptyID: "pty_1", cursor: 0, attachmentID: "attachment_1" })
|
||||
expect(socketURL?.toString()).toBe(
|
||||
"wss://server.example/base/api/experimental/persistent-pty/pty_1/connect?ticket=ticket-1&cursor=0&attachment_id=attachment_1&takeover=false&input_protocol=1",
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -885,7 +885,7 @@ describe("HttpApiCodegen.generate", () => {
|
||||
await using emitted = await emittedModule(output)
|
||||
let request: Request | undefined
|
||||
const client = emitted.module.OpenCode.make({
|
||||
baseUrl: "https://example.com",
|
||||
baseUrl: "https://example.com/base?tenant=one#fragment",
|
||||
fetch: async (input: RequestInfo | URL) => {
|
||||
request = input instanceof Request ? input : new Request(input)
|
||||
return Response.json({ data: "hello" })
|
||||
@@ -894,7 +894,7 @@ describe("HttpApiCodegen.generate", () => {
|
||||
|
||||
expect(await client.session.get({ sessionID: "a/b" })).toBe("hello")
|
||||
expect(request?.method).toBe("GET")
|
||||
expect(request?.url).toBe("https://example.com/session/a%2Fb")
|
||||
expect(request?.url).toBe("https://example.com/base/session/a%2Fb")
|
||||
})
|
||||
|
||||
test("maps an emitted no-content response to undefined", async () => {
|
||||
|
||||
Reference in New Issue
Block a user