Files
apes-sidebar/src/client/desktop-env.ts
T
finley08 fc61cddc16 fix(routes): HTTP routes ride the page base again — the desktop shell is cross-origin to the injected transport origin
The prefix fix routed every route through `__DSH_TRANSPORT__.streamBaseUrl`.
For WebSockets that base is required (a custom scheme cannot host `ws:`, and
the shell page's host is the literal `app`, whose DNS lookup never completes),
but for HTTP it is a DIFFERENT origin than the page: the Electron shell serves
the GUI from `dsh-app://app/` while `streamBaseUrl` is the Host's
`http://127.0.0.1:<port>` (`streamBaseUrl: new URL(hostUrl).origin` in the
shell's `main.js`, via `DESKTOP_IPC.boot`).

Measured on DSH 0.2.0-rc.2 + Electron shell:
- `OPTIONS /sidebar/api/fs.trees` (Origin: dsh-app://app, preflight for the
  plugin's JSON POSTs) → `405 method not allowed`, no `Access-Control-Allow-*`
  header; the actual POST answers `400` with no CORS header either.
- The shell's `protocol.handle('dsh-app', …)` forwards every NON-STATIC
  `dsh-app://app` path to the Host (`forwardWebRequest(request, hostUrl,
  hostCookie)`), so `dsh-app://app/sidebar/api/…` reaches the Host same-origin —
  the exact shape `fetch('/sidebar/api/…')` produced before the prefix fix.

Consequence in the desktop window: every plugin request is blocked by the
browser and surfaces as "Failed to fetch" — file tree (the whole sidebar body),
git, editor, uploads, archive, chunk loading. The web profile in a browser is
unaffected (there the page origin IS the transport origin), which is why the
mount lane cannot see it.

HTTP now resolves against `document.baseURI` — which carries a reverse-proxy
directory prefix too, so one rule covers both deployments — and falls back to
the transport base when no page base exists (specs / SSR). WebSockets keep the
transport base. `tests/base-path.spec.ts` pins the split per transport,
including the shell case and the no-page-base fallback.
2026-10-08 11:39:11 +08:00

59 lines
3.1 KiB
TypeScript

/**
* Absolute base for the plugin's own host transports — the HTTP routes
* (`/sidebar/api/*`, `/sidebar/file`, `/sidebar/bundle/*`, …) and the
* WebSockets (`/sidebar/ws/*`) resolve through the same source
* (see `host-route-url.ts`).
*
* A desktop shell may serve the GUI from a custom scheme: the official Electron
* shell uses `dsh-app://app/`, whose `location.host` is the literal string
* `app`. Resolving a route against that origin produces `ws://app/...`, which
* can never complete a DNS lookup — every socket the sidebar opens then fails
* with a connection error. The shell publishes the Host's real base through
* `__DSH_TRANSPORT__.streamBaseUrl`, the same source DSH's own downlink mux
* resolves through (`stream-client.ts` in `@deepseek-ai/dsh-api-gateway`), so
* prefer it and fall back to `document.baseURI` for ordinary http(s) pages —
* that base is also what carries a reverse-proxy directory prefix.
*
* This is the ONE shell fact the client still reads: the shell's own desktop
* stamps (`dsh-desktop-mode` / `-platform` / `-titlebar-inset`) and the
* `ctx.desktopWindow` geometry contract used to feed a title-bar "strip" the
* sidebar yielded at the top; that whole mechanism was removed (the plugin
* draws no top chrome, so nothing ever consumed the value — see
* docs/plans/2026-10-05-remove-titlebar-compat-strip.md).
* @returns A URL string usable as the base argument of `new URL`, or '' when
* neither source exists (non-DOM specs); route resolution then substitutes a
* placeholder origin it never actually requests.
*/
export function hostTransportBase(): string {
const transport = (globalThis as { __DSH_TRANSPORT__?: { streamBaseUrl?: string } }).__DSH_TRANSPORT__
const base = transport?.streamBaseUrl
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()
}