fix(app): terminal url

This commit is contained in:
Adam
2026-02-04 15:43:08 -06:00
parent 0d38e69038
commit 2896b8a863
2 changed files with 25 additions and 15 deletions

View File

@@ -2,26 +2,34 @@ import { describe, expect, test } from "bun:test"
import { ptySocketUrl } from "./terminal-url" import { ptySocketUrl } from "./terminal-url"
describe("ptySocketUrl", () => { describe("ptySocketUrl", () => {
test("uses browser host instead of sdk host", () => { test("uses sdk host for absolute server url", () => {
const url = ptySocketUrl("http://localhost:4096", "pty_1", "/repo", { const url = ptySocketUrl("http://localhost:4096", "pty_1", "/repo", {
host: "192.168.1.50:4096", host: "192.168.1.50:4096",
protocol: "http:", protocol: "http:",
}) })
expect(url.toString()).toBe("ws://192.168.1.50:4096/pty/pty_1/connect?directory=%2Frepo") expect(url.toString()).toBe("ws://localhost:4096/pty/pty_1/connect?directory=%2Frepo")
})
test("does not use browser port for local dev", () => {
const url = ptySocketUrl("http://localhost:4096", "pty_1", "/repo", {
host: "localhost:3000",
protocol: "http:",
})
expect(url.toString()).toBe("ws://localhost:4096/pty/pty_1/connect?directory=%2Frepo")
}) })
test("uses secure websocket on https", () => { test("uses secure websocket on https", () => {
const url = ptySocketUrl("http://localhost:4096", "pty_1", "/repo", { const url = ptySocketUrl("https://opencode.local", "pty_1", "/repo", {
host: "opencode.local", host: "localhost:3000",
protocol: "https:", protocol: "http:",
}) })
expect(url.toString()).toBe("wss://opencode.local/pty/pty_1/connect?directory=%2Frepo") expect(url.toString()).toBe("wss://opencode.local/pty/pty_1/connect?directory=%2Frepo")
}) })
test("preserves browser port", () => { test("preserves base url port", () => {
const url = ptySocketUrl("http://localhost:4096", "pty_1", "/repo", { const url = ptySocketUrl("https://opencode.local:8443", "pty_1", "/repo", {
host: "opencode.local:8443", host: "localhost:3000",
protocol: "https:", protocol: "http:",
}) })
expect(url.toString()).toBe("wss://opencode.local:8443/pty/pty_1/connect?directory=%2Frepo") expect(url.toString()).toBe("wss://opencode.local:8443/pty/pty_1/connect?directory=%2Frepo")
}) })

View File

@@ -1,10 +1,12 @@
export function ptySocketUrl(base: string, id: string, directory: string, origin: { host: string; protocol: string }) { export function ptySocketUrl(base: string, id: string, directory: string, origin: { host: string; protocol: string }) {
const root = `${origin.protocol}//${origin.host}` const root = `${origin.protocol}//${origin.host}`
const current = new URL(root) const absolute = /^https?:\/\//.test(base)
const prefix = /^https?:\/\//.test(base) ? base : new URL(base || "/", root).toString() const resolved = absolute ? new URL(base) : new URL(base || "/", root)
const url = new URL(prefix.replace(/\/+$/, "") + `/pty/${id}/connect?directory=${encodeURIComponent(directory)}`)
url.hostname = current.hostname const url = new URL(resolved)
url.port = current.port url.pathname = resolved.pathname.replace(/\/+$/, "") + `/pty/${id}/connect`
url.protocol = origin.protocol === "https:" ? "wss:" : "ws:" url.search = ""
url.searchParams.set("directory", directory)
url.protocol = resolved.protocol === "https:" ? "wss:" : "ws:"
return url return url
} }