DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
feat(dsh-tauri): 桌面载体标记只随壳层 iframe 注入
hairyfa0273c55dc17
diff --git a/packages/dsh-tauri/src/host/apply.test.ts b/packages/dsh-tauri/src/host/apply.test.tsnew file mode 100644index 00000000..f8a6dd96--- /dev/null+++ b/packages/dsh-tauri/src/host/apply.test.ts@@ -0,0 +1,67 @@+import type { HostContext, IndexInjectRow } from './types'+import { createContext, runInContext } from 'node:vm'+import { afterEach, describe, expect, it, vi } from 'vitest'+import { apply } from './apply'+import { clearHostRuntime } from './config/runtime'++afterEach(() => {+ vi.unstubAllEnvs()+ clearHostRuntime()+})++function activate() {+ let inject: ((table: IndexInjectRow[]) => void) | undefined+ const on = vi.fn((event: string, listener: (table: IndexInjectRow[]) => void) => {+ expect(event).toBe('webserver/index-inject')+ inject = listener+ return () => {+ inject = undefined+ }+ })+ const ctx = {+ connection: { authorizeIndex: () => false },+ effect: (callback: () => unknown) => { callback() },+ on,+ } as unknown as HostContext+ apply(ctx)+ return { on, collect: (): IndexInjectRow[] => {+ const table: IndexInjectRow[] = []+ inject?.(table)+ return table+ } }+}++/** 在最小页面语境里执行注入脚本,取回 `globalThis.dshDesktop` 的落地值。 */+function runMarker(script: string, search: string): unknown {+ const sandbox: Record<string, unknown> = { location: { search }, URLSearchParams }+ runInContext(script, createContext(sandbox))+ return sandbox.dshDesktop+}++describe('desktop account marker', () => {+ /** 官方账号 UI 的准入就是 `'dshDesktop' in globalThis`,因此这里验的是真实脚本行为。 */+ it('marks the embedded index only when the shell claimed it', () => {+ vi.stubEnv('DSH_TAURI_EMBEDDED', '1')+ const { collect, on } = activate()++ expect(on).toHaveBeenCalledOnce()+ const rows = collect()+ expect(rows).toHaveLength(1)+ const row = rows[0]+ expect(row?.kind).toBe('script')+ expect(row?.kind === 'script' ? row.placement : undefined).toBe('head')++ const script = row?.kind === 'script' ? row.text : ''+ // 与官方 preload 的非 app 来源分支同值:只声明协议版本,产品 API 一律缺席。+ expect(runMarker(script, '?t=1&dshDesktop=1')).toEqual({ protocolVersion: 1 })+ expect(runMarker(script, '?t=1')).toBeUndefined()+ })++ it('does not advertise a desktop carrier to standalone web', () => {+ vi.stubEnv('DSH_TAURI_EMBEDDED', '0')+ const { collect, on } = activate()++ expect(on).not.toHaveBeenCalled()+ expect(collect()).toEqual([])+ })+})diff --git a/packages/dsh-tauri/src/host/apply.ts b/packages/dsh-tauri/src/host/apply.tsindex e5706141..21945f1e 100644--- a/packages/dsh-tauri/src/host/apply.ts+++ b/packages/dsh-tauri/src/host/apply.ts@@ -1,15 +1,35 @@import type { ConnectionHost, HostContext } from './types'+import process from 'node:process'import { PLUGIN_ID } from '../shared/constants'import { clearHostRuntime, setCurrentHostInstance } from './config/runtime'import { gate } from './service/gate'const GATE_EFFECT = `${PLUGIN_ID}: gate`-+const ACCOUNT_EFFECT = `${PLUGIN_ID}: account desktop marker`const HOST_RUNTIME_EFFECT = `${PLUGIN_ID}: host runtime`+/**+ * 官方账号 UI 只认 `'dshDesktop' in globalThis`(`dsh-client-ui-settings-account` 的+ * `apply()` 早退判据),而唯一的“我是桌面载体”凭据是壳层给 iframe 加的 `dshDesktop`+ * 查询参数(见 `src/store/modules/harness/utils.ts`)。写成解析期即执行的经典脚本,+ * 保证在任何模块插件 `apply()` 之前落位;用户用浏览器直开同一端口时不带该参数,+ * 因而不会误开账号入口。+ *+ * 取值与官方 preload 的非 app 来源分支逐字一致(`{ protocolVersion: 1 }`,见+ * `apps/desktop/src/preload-app.ts`):只声明载体,**不伪造** Electron 的产品 API+ * (`updates` / `browser`),消费方都是 `carrier?.protocolVersion === 1 ? carrier.updates : undefined`+ * 这样的可选链。+ */+const ACCOUNT_MARKER_SCRIPT = 'if (new URLSearchParams(location.search).has(\'dshDesktop\')) globalThis.dshDesktop = { protocolVersion: 1 };'+export function apply(ctx: HostContext): void {setCurrentHostInstance(ctx as unknown as ConnectionHost)ctx.effect(() => gate.attach(), GATE_EFFECT)+ if (process.env.DSH_TAURI_EMBEDDED === '1') {+ ctx.effect(() => ctx.on('webserver/index-inject', (table) => {+ table.push({ kind: 'script', placement: 'head', text: ACCOUNT_MARKER_SCRIPT })+ }), ACCOUNT_EFFECT)+ }ctx.effect(() => () => clearHostRuntime(), HOST_RUNTIME_EFFECT)}diff --git a/packages/dsh-tauri/src/host/types/harness.ts b/packages/dsh-tauri/src/host/types/harness.tsindex ef384c35..79640181 100644--- a/packages/dsh-tauri/src/host/types/harness.ts+++ b/packages/dsh-tauri/src/host/types/harness.ts@@ -55,8 +55,14 @@ export interface HostPluginLoader {* `sessions` 单独钉住:上游把 `Context` 同时按宿主/客户端两侧增广,本仓库的单个 TS* 程序里两侧都在场,因此不依赖增广合并顺序,显式取宿主权威面 `SessionStore`。*/+/** 宿主 webserver 的结构化 index 注入行(与 `dsh-host-webserver` 的 renderRow 契约一致)。 */+export type IndexInjectRow+ = | { kind: 'global', name: string, value: unknown }+ | { kind: 'script', placement: 'head' | 'body', text: string }+export type HostContext = Omit<Context, 'sessions'> & {sessions: SessionStorewebServer: WebServerServiceloader: HostPluginLoader+ on: Context['on'] & ((event: 'webserver/index-inject', listener: (table: IndexInjectRow[]) => void) => () => void)}diff --git a/src/store/modules/harness/utils.ts b/src/store/modules/harness/utils.tsindex 73a95601..3de5fd68 100644--- a/src/store/modules/harness/utils.ts+++ b/src/store/modules/harness/utils.ts@@ -34,11 +34,15 @@ import { pollReadiness } from './readiness'* 构建带时间戳的 iframe URL,避免 WebView2 缓存旧页面。* alpha 鉴权由启动前的桌面端 patch 处理,iframe 永远不携带启动 token;旧核心* 同样继续使用原有的缓存查询参数。+ *+ * `dshDesktop` 是桌面载体的唯一凭据:宿主只给带该参数的 index 注入官方 Electron+ * 载体标记(`globalThis.dshDesktop`),因此用户用浏览器直开同一端口时不会被误判成+ * 桌面端而冒出账号登录入口。*/export function generateTimestampedUrl(baseUrl: string): string {const timestamp = Date.now()const separator = baseUrl.includes('?') ? '&' : '?'- return `${baseUrl}${separator}t=${timestamp}`+ return `${baseUrl}${separator}t=${timestamp}&dshDesktop=1`}/**