fix(app): terminal URL issues
This commit is contained in:
@@ -1,44 +0,0 @@
|
|||||||
import { describe, expect, test } from "bun:test"
|
|
||||||
import { ptySocketUrl } from "./terminal-url"
|
|
||||||
|
|
||||||
describe("ptySocketUrl", () => {
|
|
||||||
test("uses sdk host for absolute server url", () => {
|
|
||||||
const url = ptySocketUrl("http://localhost:4096", "pty_1", "/repo", {
|
|
||||||
host: "192.168.1.50:4096",
|
|
||||||
protocol: "http:",
|
|
||||||
})
|
|
||||||
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", () => {
|
|
||||||
const url = ptySocketUrl("https://opencode.local", "pty_1", "/repo", {
|
|
||||||
host: "localhost:3000",
|
|
||||||
protocol: "http:",
|
|
||||||
})
|
|
||||||
expect(url.toString()).toBe("wss://opencode.local/pty/pty_1/connect?directory=%2Frepo")
|
|
||||||
})
|
|
||||||
|
|
||||||
test("preserves base url port", () => {
|
|
||||||
const url = ptySocketUrl("https://opencode.local:8443", "pty_1", "/repo", {
|
|
||||||
host: "localhost:3000",
|
|
||||||
protocol: "http:",
|
|
||||||
})
|
|
||||||
expect(url.toString()).toBe("wss://opencode.local:8443/pty/pty_1/connect?directory=%2Frepo")
|
|
||||||
})
|
|
||||||
|
|
||||||
test("handles slash base url", () => {
|
|
||||||
const url = ptySocketUrl("/", "pty_1", "/repo", {
|
|
||||||
host: "192.168.1.50:4096",
|
|
||||||
protocol: "http:",
|
|
||||||
})
|
|
||||||
expect(url.toString()).toBe("ws://192.168.1.50:4096/pty/pty_1/connect?directory=%2Frepo")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
export function ptySocketUrl(base: string, id: string, directory: string, origin: { host: string; protocol: string }) {
|
|
||||||
const root = `${origin.protocol}//${origin.host}`
|
|
||||||
const absolute = /^https?:\/\//.test(base)
|
|
||||||
const resolved = absolute ? new URL(base) : new URL(base || "/", root)
|
|
||||||
|
|
||||||
const url = new URL(resolved)
|
|
||||||
url.pathname = resolved.pathname.replace(/\/+$/, "") + `/pty/${id}/connect`
|
|
||||||
url.search = ""
|
|
||||||
url.searchParams.set("directory", directory)
|
|
||||||
url.protocol = resolved.protocol === "https:" ? "wss:" : "ws:"
|
|
||||||
return url
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,6 @@ import { LocalPTY } from "@/context/terminal"
|
|||||||
import { resolveThemeVariant, useTheme, withAlpha, type HexColor } from "@opencode-ai/ui/theme"
|
import { resolveThemeVariant, useTheme, withAlpha, type HexColor } from "@opencode-ai/ui/theme"
|
||||||
import { useLanguage } from "@/context/language"
|
import { useLanguage } from "@/context/language"
|
||||||
import { showToast } from "@opencode-ai/ui/toast"
|
import { showToast } from "@opencode-ai/ui/toast"
|
||||||
import { ptySocketUrl } from "./terminal-url"
|
|
||||||
|
|
||||||
export interface TerminalProps extends ComponentProps<"div"> {
|
export interface TerminalProps extends ComponentProps<"div"> {
|
||||||
pty: LocalPTY
|
pty: LocalPTY
|
||||||
@@ -164,7 +163,8 @@ export const Terminal = (props: TerminalProps) => {
|
|||||||
|
|
||||||
const once = { value: false }
|
const once = { value: false }
|
||||||
|
|
||||||
const url = ptySocketUrl(sdk.url, local.pty.id, sdk.directory, window.location)
|
const url = new URL(sdk.url + `/pty/${local.pty.id}/connect?directory=${encodeURIComponent(sdk.directory)}`)
|
||||||
|
url.protocol = url.protocol === "https:" ? "wss:" : "ws:"
|
||||||
if (window.__OPENCODE__?.serverPassword) {
|
if (window.__OPENCODE__?.serverPassword) {
|
||||||
url.username = "opencode"
|
url.username = "opencode"
|
||||||
url.password = window.__OPENCODE__?.serverPassword
|
url.password = window.__OPENCODE__?.serverPassword
|
||||||
|
|||||||
Reference in New Issue
Block a user