merge fix/desktop-shell-http-base into dev(桌面壳的 HTTP 与 WebSocket 需要不同基址:壳把页面放在 dsh-app://app/、注入的 streamBaseUrl 指向另一个 origin,而壳的 protocol.handle 会把非静态 dsh-app 路径同源转发给宿主——插件的 JSON 路由不带 CORS 头,发到注入 origin 就是跨源、被浏览器直接拒(满屏 Failed to fetch);于是 HTTP 改骑页面基址 hostHttpBase()(document.baseURI,反代前缀也带在里面),WebSocket 继续用注入 origin。含 base-path 三种场景的用例)
This commit is contained in:
commit
698721eb34
3 files changed
+80
-14
No files matched your search
@@ -30,3 +30,29 @@ export function hostTransportBase(): string {
|
||||
if (base !== undefined && base !== '') return base
|
||||
return typeof document !== 'undefined' && typeof document.baseURI === 'string' ? document.baseURI : ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Base for the plugin's own HTTP routes (`/sidebar/api/*`, `/sidebar/file`,
|
||||
* `/sidebar/bundle/*`, …): the PAGE's own base, not the injected transport
|
||||
* origin.
|
||||
*
|
||||
* The desktop shell makes those two different — page `dsh-app://app/`, injected
|
||||
* `streamBaseUrl` = the Host's `http://127.0.0.1:<port>`. HTTP must stay on the
|
||||
* page: the shell's `protocol.handle` forwards every non-static `dsh-app://app`
|
||||
* path to the Host same-origin (that is how `fetch('/sidebar/api/…')` reached it
|
||||
* before the prefix fix), while the Host's plugin routes answer no
|
||||
* `Access-Control-Allow-*` headers, so a cross-origin `fetch` to the injected
|
||||
* origin is blocked by the browser outright ("Failed to fetch"). WebSockets go
|
||||
* the other way round — see `hostTransportBase` and `host-route-url.ts`.
|
||||
*
|
||||
* `document.baseURI` carries a reverse-proxy directory prefix too, so one rule
|
||||
* covers both deployments.
|
||||
* @returns A URL string usable as the base argument of `new URL`; the transport
|
||||
* base only when no page base exists (specs, SSR).
|
||||
*/
|
||||
export function hostHttpBase(): string {
|
||||
const base = typeof document !== 'undefined' ? document.baseURI : undefined
|
||||
if (typeof base === 'string' && base !== '') return base
|
||||
return hostTransportBase()
|
||||
}
|
||||
|
||||
@@ -6,16 +6,29 @@
|
||||
* reverse-proxy directory (`https://host/dataops/proxy/3080/`) sends every
|
||||
* request OUTSIDE the prefix: the JSON API answers 404, lazy chunks never
|
||||
* load, media and HTML previews stay blank, and the WebSockets close with
|
||||
* 1006. Resolving a RELATIVE path against the injected transport base keeps
|
||||
* the request under the page's own directory — the same shape DSH's own client
|
||||
* uses (`new URL(REMOTE_STREAM_MUX_PATH.slice(1), __DSH_TRANSPORT__?.streamBaseUrl
|
||||
* ?? document.baseURI)` in `@deepseek-ai/dsh-api-gateway`).
|
||||
* 1006. Resolving a RELATIVE path against the page's own base keeps the
|
||||
* request under the page's own directory.
|
||||
*
|
||||
* The base is normalized to a directory first (search/hash dropped, trailing
|
||||
* slash forced) so a launch-token query cannot leak into the route and a
|
||||
* prefix written without its final slash cannot eat the last path segment.
|
||||
* The two transports need DIFFERENT bases, and the desktop shell is why:
|
||||
*
|
||||
* - **HTTP** rides the PAGE base (`document.baseURI`). In the Electron shell
|
||||
* the page is `dsh-app://app/`, and the shell's protocol handler forwards
|
||||
* every non-static path to the Host (`protocol.handle` → `forwardWebRequest`
|
||||
* in the shell's `main.js`), so `dsh-app://app/sidebar/api/…` reaches the
|
||||
* Host same-origin — the exact shape `fetch('/sidebar/api/…')` produced
|
||||
* before the prefix fix. The shell also publishes
|
||||
* `__DSH_TRANSPORT__.streamBaseUrl` = the Host's real origin
|
||||
* (`new URL(hostUrl).origin`), which is a DIFFERENT origin from the page:
|
||||
* sending the plugin's JSON POSTs there makes them cross-origin, and the
|
||||
* plugin's routes answer no `Access-Control-Allow-*` headers (the shell's own
|
||||
* UI avoids this by going through the same-origin forwarding), so the browser
|
||||
* rejects them — every tree/git/editor request fails as "Failed to fetch".
|
||||
* - **WebSocket** needs that injected base: a custom scheme cannot host `ws:`,
|
||||
* and `dsh-app://app`'s host is the literal string `app`, whose DNS lookup
|
||||
* never completes (see `desktop-env.ts`). The Host accepts the shell page's
|
||||
* origin on those sockets.
|
||||
*/
|
||||
import { hostTransportBase } from './desktop-env.ts'
|
||||
import { hostHttpBase, hostTransportBase } from './desktop-env.ts'
|
||||
|
||||
/** Placeholder origin for the non-DOM case (specs / SSR): resolution needs an
|
||||
* absolute base, but the caller never issues the resulting request. */
|
||||
@@ -30,7 +43,7 @@ const PLACEHOLDER_ORIGIN = 'http://dsh.internal/'
|
||||
* prefix so resolution stays pure.
|
||||
* @returns The absolute route URL (the base's origin + any prefix included).
|
||||
*/
|
||||
export function hostRouteUrl(path: string, baseUrl: string = hostTransportBase()): URL {
|
||||
export function hostRouteUrl(path: string, baseUrl: string = hostHttpBase()): URL {
|
||||
return new URL(path.replace(/^\/+/, ''), directoryBase(baseUrl))
|
||||
}
|
||||
|
||||
|
||||
+32
-5
@@ -84,16 +84,43 @@ describe('hostRouteUrl / hostWebSocketUrl', () => {
|
||||
.toBe('https://host.test/sidebar/api/fs.tree')
|
||||
})
|
||||
|
||||
it('prefers the shell transport base over the document base (desktop shell)', () => {
|
||||
it('splits the base per transport in the desktop shell: HTTP rides the page, WS the injected origin', async () => {
|
||||
// 2718725: the Electron shell serves the GUI from `dsh-app://app/`, whose
|
||||
// host is the literal string `app` — resolving against it never connects.
|
||||
// host is the literal string `app` — resolving a WebSocket against it never
|
||||
// connects, so sockets use the injected Host origin.
|
||||
vi.stubGlobal('__DSH_TRANSPORT__', { streamBaseUrl: 'http://127.0.0.1:4199/' })
|
||||
// The default parameter is exercised on purpose (undefined = "read it").
|
||||
expect(hostRouteUrl('sidebar/api/fs.tree', undefined).href)
|
||||
.toBe('http://127.0.0.1:4199/sidebar/api/fs.tree')
|
||||
await withPageBase('dsh-app://app/', () => {
|
||||
// HTTP stays on the page: the shell's protocol handler forwards every
|
||||
// non-static dsh-app path to the Host same-origin, and the plugin's own
|
||||
// routes carry no CORS headers — a cross-origin POST to the injected
|
||||
// origin is rejected by the browser ("Failed to fetch").
|
||||
expect(hostRouteUrl('sidebar/api/fs.tree', undefined).href)
|
||||
.toBe('dsh-app://app/sidebar/api/fs.tree')
|
||||
expect(hostRouteUrl('sidebar/bundle/editor.js', undefined).href)
|
||||
.toBe('dsh-app://app/sidebar/bundle/editor.js')
|
||||
})
|
||||
expect(hostWebSocketUrl('sidebar/ws/fs-watch', undefined).href)
|
||||
.toBe('ws://127.0.0.1:4199/sidebar/ws/fs-watch')
|
||||
})
|
||||
|
||||
it('falls back to the injected base for HTTP when the page has none (specs, SSR)', () => {
|
||||
vi.stubGlobal('__DSH_TRANSPORT__', { streamBaseUrl: 'http://127.0.0.1:4199/' })
|
||||
const document = globalThis.document as { baseURI?: string }
|
||||
const previous = document.baseURI
|
||||
Object.defineProperty(document, 'baseURI', { value: '', configurable: true, writable: true })
|
||||
try {
|
||||
expect(hostRouteUrl('sidebar/api/fs.tree', undefined).href)
|
||||
.toBe('http://127.0.0.1:4199/sidebar/api/fs.tree')
|
||||
} finally {
|
||||
Object.defineProperty(document, 'baseURI', { value: previous, configurable: true, writable: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('keeps the injected base for HTTP on an ordinary http(s) page (same origin)', () => {
|
||||
vi.stubGlobal('__DSH_TRANSPORT__', { streamBaseUrl: 'http://127.0.0.1:4199/' })
|
||||
expect(hostRouteUrl('sidebar/api/fs.tree', 'http://127.0.0.1:4199/').href)
|
||||
.toBe('http://127.0.0.1:4199/sidebar/api/fs.tree')
|
||||
})
|
||||
})
|
||||
|
||||
describe('the real call sites resolve through the page prefix', () => {
|
||||
|
||||
Reference in new issue
Block a user