mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-04 08:28:36 +00:00
fix(webui): override wsUrl with local LAN IP when on dev server port 5173
This commit is contained in:
parent
27d869d3cc
commit
5573a9d78e
@ -72,20 +72,20 @@ export function deriveWsUrl(
|
|||||||
wsUrl?: string | null,
|
wsUrl?: string | null,
|
||||||
): string {
|
): string {
|
||||||
const query = `?token=${encodeURIComponent(token)}`;
|
const query = `?token=${encodeURIComponent(token)}`;
|
||||||
if (wsUrl && /^(wss?|nanobot-host):\/\//i.test(wsUrl)) {
|
|
||||||
const join = wsUrl.includes("?") ? "&" : "?";
|
|
||||||
return `${wsUrl}${join}token=${encodeURIComponent(token)}`;
|
|
||||||
}
|
|
||||||
const path = wsPath && wsPath.startsWith("/") ? wsPath : `/${wsPath || ""}`;
|
const path = wsPath && wsPath.startsWith("/") ? wsPath : `/${wsPath || ""}`;
|
||||||
if (typeof window === "undefined") {
|
if (typeof window !== "undefined" && window.location.port === "5173") {
|
||||||
return `ws://127.0.0.1:8765${path}${query}`;
|
|
||||||
}
|
|
||||||
if (window.location.port === "5173") {
|
|
||||||
const host = window.location.hostname.includes(":")
|
const host = window.location.hostname.includes(":")
|
||||||
? `[${window.location.hostname}]`
|
? `[${window.location.hostname}]`
|
||||||
: window.location.hostname;
|
: window.location.hostname;
|
||||||
return `ws://${host}:8765${path}${query}`;
|
return `ws://${host}:8765${path}${query}`;
|
||||||
}
|
}
|
||||||
|
if (wsUrl && /^(wss?|nanobot-host):\/\//i.test(wsUrl)) {
|
||||||
|
const join = wsUrl.includes("?") ? "&" : "?";
|
||||||
|
return `${wsUrl}${join}token=${encodeURIComponent(token)}`;
|
||||||
|
}
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return `ws://127.0.0.1:8765${path}${query}`;
|
||||||
|
}
|
||||||
const scheme = window.location.protocol === "https:" ? "wss" : "ws";
|
const scheme = window.location.protocol === "https:" ? "wss" : "ws";
|
||||||
const host = window.location.host;
|
const host = window.location.host;
|
||||||
return `${scheme}://${host}${path}${query}`;
|
return `${scheme}://${host}${path}${query}`;
|
||||||
|
|||||||
@ -14,6 +14,19 @@ describe("bootstrap helpers", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("overrides the server-provided websocket URL when on dev server port 5173", () => {
|
||||||
|
vi.stubGlobal("window", {
|
||||||
|
location: {
|
||||||
|
port: "5173",
|
||||||
|
hostname: "192.168.1.100",
|
||||||
|
protocol: "http:",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(deriveWsUrl("/", "tok", "ws://127.0.0.1:8765/")).toBe(
|
||||||
|
"ws://192.168.1.100:8765/?token=tok",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it("preserves the host socket bridge URL", () => {
|
it("preserves the host socket bridge URL", () => {
|
||||||
expect(deriveWsUrl("/", "tok en", "nanobot-host://engine/")).toBe(
|
expect(deriveWsUrl("/", "tok en", "nanobot-host://engine/")).toBe(
|
||||||
"nanobot-host://engine/?token=tok%20en",
|
"nanobot-host://engine/?token=tok%20en",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user