Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(pet): 修复桌宠素材不落 IndexedDB 缓存与中止时的 resource id 报错

hairyf4693394132d4
diff --git a/patches/@[email protected] b/patches/@[email protected]
new file mode 100644
index 00000000..1c6d654f
--- /dev/null
+++ b/patches/@[email protected]
@@ -0,0 +1,116 @@
+diff --git a/dist-js/index.cjs b/dist-js/index.cjs
+index f1bba4502af349efbde8b94ac1b1773a490f4836..305702b4b5420d3b0660476cf544778edab67010 100644
+--- a/dist-js/index.cjs
++++ b/dist-js/index.cjs
+@@ -29,6 +29,13 @@ var core = require('@tauri-apps/api/core');
+ * @module
+ */
+ const ERROR_REQUEST_CANCELLED = 'Request cancelled';
++// Header names/values must be ISO-8859-1: GitHub adds a `content-disposition`
++// carrying the raw file name, so any asset with a non-Latin name (all dsh-pet
++// animations) makes `new Headers(...)` throw and fails the whole fetch.
++const ISO_8859_1_ONLY = /^[\u0000-\u00ff]*$/;
++function toResponseHeaders(rawHeaders) {
++ return new Headers(rawHeaders.filter(([name, value]) => ISO_8859_1_ONLY.test(name) && ISO_8859_1_ONLY.test(value)));
++}
+ /**
+ * Fetch a resource from the network. It returns a `Promise` that resolves to the
+ * `Response` to that `Request`, whether it is successful or not.
+@@ -103,7 +110,11 @@ async function fetch(input, init) {
+ danger
+ }
+ });
+- const abort = () => core.invoke('plugin:http|fetch_cancel', { rid });
++ // Cancellation cleanup is best-effort: `fetch_read_body` closes the response
++ // resource at EOF, so these fire-and-forget commands legitimately fail with
++ // "The resource id ... is invalid" — swallow it, otherwise every aborted
++ // fetch leaks an unhandled rejection (hosts often forward those to logs).
++ const abort = () => core.invoke('plugin:http|fetch_cancel', { rid }).catch(() => {});
+ // Optimistically check for abort signal
+ // and avoid doing any work after doing intial work on the Rust side
+ if (signal?.aborted) {
+@@ -117,7 +128,7 @@ async function fetch(input, init) {
+ rid
+ });
+ const dropBody = () => {
+- return core.invoke('plugin:http|fetch_cancel_body', { rid: responseRid });
++ return core.invoke('plugin:http|fetch_cancel_body', { rid: responseRid }).catch(() => {});
+ };
+ const readChunk = async (controller) => {
+ let data;
+@@ -172,7 +183,7 @@ async function fetch(input, init) {
+ // browsers, where `set-cookie` is treated as a forbidden response
+ // header and is generally not readable from JavaScript.
+ Object.defineProperty(res, 'headers', {
+- value: new Headers(responseHeaders),
++ value: toResponseHeaders(responseHeaders),
+ writable: false
+ });
+ // Patch clone() per-instance so cloning preserves the overridden properties
+@@ -182,7 +193,7 @@ async function fetch(input, init) {
+ const cloned = originalClone();
+ Object.defineProperty(cloned, 'url', { value: url, writable: false });
+ Object.defineProperty(cloned, 'headers', {
+- value: new Headers(responseHeaders),
++ value: toResponseHeaders(responseHeaders),
+ writable: false
+ });
+ return cloned;
+diff --git a/dist-js/index.js b/dist-js/index.js
+index 01f0ea49e0fe45425adcb8596f57e97a92c8c958..a35a916bf78f219f9012a7609e06b04c3ac9c149 100644
+--- a/dist-js/index.js
++++ b/dist-js/index.js
+@@ -27,6 +27,13 @@ import { invoke } from '@tauri-apps/api/core';
+ * @module
+ */
+ const ERROR_REQUEST_CANCELLED = 'Request cancelled';
++// Header names/values must be ISO-8859-1: GitHub adds a `content-disposition`
++// carrying the raw file name, so any asset with a non-Latin name (all dsh-pet
++// animations) makes `new Headers(...)` throw and fails the whole fetch.
++const ISO_8859_1_ONLY = /^[\u0000-\u00ff]*$/;
++function toResponseHeaders(rawHeaders) {
++ return new Headers(rawHeaders.filter(([name, value]) => ISO_8859_1_ONLY.test(name) && ISO_8859_1_ONLY.test(value)));
++}
+ /**
+ * Fetch a resource from the network. It returns a `Promise` that resolves to the
+ * `Response` to that `Request`, whether it is successful or not.
+@@ -101,7 +108,11 @@ async function fetch(input, init) {
+ danger
+ }
+ });
+- const abort = () => invoke('plugin:http|fetch_cancel', { rid });
++ // Cancellation cleanup is best-effort: `fetch_read_body` closes the response
++ // resource at EOF, so these fire-and-forget commands legitimately fail with
++ // "The resource id ... is invalid" — swallow it, otherwise every aborted
++ // fetch leaks an unhandled rejection (hosts often forward those to logs).
++ const abort = () => invoke('plugin:http|fetch_cancel', { rid }).catch(() => {});
+ // Optimistically check for abort signal
+ // and avoid doing any work after doing intial work on the Rust side
+ if (signal?.aborted) {
+@@ -115,7 +126,7 @@ async function fetch(input, init) {
+ rid
+ });
+ const dropBody = () => {
+- return invoke('plugin:http|fetch_cancel_body', { rid: responseRid });
++ return invoke('plugin:http|fetch_cancel_body', { rid: responseRid }).catch(() => {});
+ };
+ const readChunk = async (controller) => {
+ let data;
+@@ -170,7 +181,7 @@ async function fetch(input, init) {
+ // browsers, where `set-cookie` is treated as a forbidden response
+ // header and is generally not readable from JavaScript.
+ Object.defineProperty(res, 'headers', {
+- value: new Headers(responseHeaders),
++ value: toResponseHeaders(responseHeaders),
+ writable: false
+ });
+ // Patch clone() per-instance so cloning preserves the overridden properties
+@@ -180,7 +191,7 @@ async function fetch(input, init) {
+ const cloned = originalClone();
+ Object.defineProperty(cloned, 'url', { value: url, writable: false });
+ Object.defineProperty(cloned, 'headers', {
+- value: new Headers(responseHeaders),
++ value: toResponseHeaders(responseHeaders),
+ writable: false
+ });
+ return cloned;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index dc4fe43f..9cc46955 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -298,6 +298,7 @@ catalogs:
version: 1.17.5
patchedDependencies:
+ '@tauri-apps/[email protected]': 6bbec7f8a8e969a867d222e8b2b542d2dbd639ffbaf2184a4df47f330b6ad431
bumpp: 362685edd02f1561e78b740e2b4c1ec2bd8e266eb2e26a64055ae305cac1c3ac
importers:
@@ -333,7 +334,7 @@ importers:
version: 2.11.1
'@tauri-apps/plugin-http':
specifier: catalog:tauri
- version: 2.6.0
+ version: 2.6.0(patch_hash=6bbec7f8a8e969a867d222e8b2b542d2dbd639ffbaf2184a4df47f330b6ad431)
'@tauri-apps/plugin-os':
specifier: 'catalog:'
version: 2.3.2
@@ -7294,7 +7295,7 @@ snapshots:
'@tauri-apps/cli-win32-ia32-msvc': 2.11.4
'@tauri-apps/cli-win32-x64-msvc': 2.11.4
- '@tauri-apps/[email protected]':
+ '@tauri-apps/[email protected](patch_hash=6bbec7f8a8e969a867d222e8b2b542d2dbd639ffbaf2184a4df47f330b6ad431)':
dependencies:
'@tauri-apps/api': 2.11.1
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index 14bbfd94..8c6df95d 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -17,6 +17,7 @@ packages:
- packages/*
patchedDependencies:
+ '@tauri-apps/[email protected]': patches/@[email protected]
bumpp: patches/bumpp.patch
catalog:
diff --git a/src/pet/app.tsx b/src/pet/app.tsx
index f09f00ea..0c61decb 100644
--- a/src/pet/app.tsx
+++ b/src/pet/app.tsx
@@ -12,6 +12,7 @@ import { useBubble } from './hooks/use-bubble'
import { usePetSource } from './hooks/use-pet-source'
import { normalizeSizePercent, usePetStatus } from './hooks/use-pet-status'
import { usePetWindowSize } from './hooks/use-pet-window'
+import { reportPetIssue } from './utils/log'
/**
* 桌宠窗口的唯一组合入口。
@@ -73,6 +74,7 @@ export function App() {
cache={true}
hidden={!visible}
hitboxRef={hitboxRef}
+ onError={reportPetAssetError}
/>
)}
{/* 选中了宠物但资源解析不出来(导入的宠物被删除、清单里没有该 id):必须给出
@@ -82,3 +84,12 @@ export function App() {
</ToastProvider>
)
}
+
+/**
+ * 资源加载/播放失败的出口:组件内部抓取失败会静默降级为「直接播远端地址」
+ * (`useCachedMediaUrl` 的 catch 只调 `onError`),而桌宠窗口的 console 要手动
+ * F12 才看得见 —— 不接这个回调,「IndexedDB 一次都没写进去」就无从察觉。
+ */
+function reportPetAssetError(error: unknown): void {
+ reportPetIssue('asset', error)
+}
diff --git a/test/pet-asset-headers.test.ts b/test/pet-asset-headers.test.ts
new file mode 100644
index 00000000..8d490435
--- /dev/null
+++ b/test/pet-asset-headers.test.ts
@@ -0,0 +1,107 @@
+// @vitest-environment node
+import { readFileSync } from 'node:fs'
+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
+
+/**
+ * 桌宠素材响应头 / 取消清理契约。
+ *
+ * 1. **响应头**:`raw.githubusercontent.com` 对非 ASCII 文件名会附上
+ * `content-disposition: attachment; filename=.../待机呼吸休闲.webm`,而
+ * `@tauri-apps/plugin-http` 用 `new Headers(responseHeaders)` 复刻响应头,这个
+ * 构造器只接受 ISO-8859-1 ⇒ 整条 fetch reject ⇒ `dsh-pet-component` 静默降级为
+ * 「直连远端播放」⇒ IndexedDB 永远只有空库(dsh-pet 动画名全是中文,即 100% 命中)。
+ * 2. **取消清理**:`fetch_read_body` 读到 EOF 就关掉 Rust 侧的响应资源,于是
+ * fire-and-forget 的 `fetch_cancel` / `fetch_cancel_body` 必然收到
+ * `The resource id ... is invalid`;不吞掉就会让每次中止(切动画 / 关窗口)都冒一个
+ * unhandled rejection,被桌宠窗口的全局处理器记成 ERROR 日志刷屏。
+ *
+ * 两处都由 pnpm patch 修在 `dist-js/index.js` + `index.cjs`:这里既锁「补丁在册且已装
+ * 进产物」,也打桩 `window.__TAURI_INTERNALS__.invoke` 跑真实 `fetch`(含中止路径)。
+ */
+const ANIMATION_URL = 'https://raw.githubusercontent.com/PC2005-cloud/dsh-pet/e1ff8c1e4001878cbb80441262d530e16541f138/dsh-pet/assets/webm/%E5%BE%85%E6%9C%BA%E5%91%BC%E5%90%B8%E4%BC%91%E9%97%B2.webm'
+const HAZARDOUS_HEADERS: [string, string][] = [
+ ['content-type', 'audio/webm'],
+ ['content-disposition', 'attachment; filename=dsh-pet/assets/webm/待机呼吸休闲.webm'],
+]
+
+let bodyReads = 0
+let hanging = false
+const invoke = vi.fn(async (command: string) => {
+ if (command === 'plugin:http|fetch')
+ return 1
+ if (command === 'plugin:http|fetch_send')
+ return { status: 200, statusText: 'OK', url: ANIMATION_URL, headers: HAZARDOUS_HEADERS, rid: 2 }
+ if (command === 'plugin:http|fetch_read_body')
+ return hanging ? new Promise<number[]>(() => {}) : (bodyReads++ === 0 ? [104, 105, 0] : [1])
+ if (command === 'plugin:http|fetch_cancel' || command === 'plugin:http|fetch_cancel_body')
+ return hanging ? Promise.reject(new Error('The resource id 1234 is invalid')) : undefined
+ return undefined
+})
+
+describe('pet asset headers contract', () => {
+ beforeAll(() => {
+ vi.stubGlobal('window', { __TAURI_INTERNALS__: { invoke } })
+ })
+
+ afterAll(() => {
+ vi.unstubAllGlobals()
+ })
+
+ it('fetches an asset whose response carries a non ISO-8859-1 header', async () => {
+ const { fetch: pluginFetch } = await import('@tauri-apps/plugin-http')
+ const response = await pluginFetch(ANIMATION_URL, { cache: 'force-cache' })
+
+ expect(response.status).toBe(200)
+ expect(response.headers.get('content-type')).toBe('audio/webm')
+ // 中文文件名头被剔除(组件从不读它),而不是让整条 fetch 炸掉。
+ expect(response.headers.get('content-disposition')).toBeNull()
+ expect(await response.text()).toBe('hi')
+ })
+
+ it('does not leak unhandled rejections when cancellation cleanup fails', async () => {
+ const { fetch: pluginFetch } = await import('@tauri-apps/plugin-http')
+ const rejections: unknown[] = []
+ const collect = (reason: unknown) => void rejections.push(reason)
+ const controller = new AbortController()
+
+ hanging = true
+ try {
+ const response = await pluginFetch(ANIMATION_URL, { signal: controller.signal })
+ process.on('unhandledRejection', collect)
+ controller.abort()
+ await expect(response.text()).rejects.toThrow('Request cancelled')
+ await new Promise(resolve => setTimeout(resolve, 50))
+ process.off('unhandledRejection', collect)
+
+ expect(invoke).toHaveBeenCalledWith('plugin:http|fetch_cancel', { rid: 1 }, undefined)
+ expect(invoke).toHaveBeenCalledWith('plugin:http|fetch_cancel_body', { rid: 2 }, undefined)
+ expect(rejections).toEqual([])
+ }
+ finally {
+ process.off('unhandledRejection', collect)
+ hanging = false
+ }
+ })
+
+ it('keeps the filter patch registered in the workspace and applied on disk', () => {
+ const workspace = readFileSync(new URL('../pnpm-workspace.yaml', import.meta.url), 'utf8')
+ expect(workspace).toContain('@tauri-apps/[email protected]')
+ expect(workspace).toContain('patches/@[email protected]')
+
+ const patch = readFileSync(new URL('../patches/@[email protected]', import.meta.url), 'utf8')
+ expect(patch).toContain('ISO_8859_1_ONLY')
+
+ for (const file of ['index.js', 'index.cjs']) {
+ const bundle = readFileSync(new URL(`../node_modules/@tauri-apps/plugin-http/dist-js/${file}`, import.meta.url), 'utf8')
+ expect(bundle).toContain('toResponseHeaders(responseHeaders)')
+ expect(bundle).not.toContain('new Headers(responseHeaders)')
+ expect(bundle.match(/\.catch\(\(\) => \{\}\)/g)?.length).toBeGreaterThanOrEqual(2)
+ }
+ })
+
+ it('reports pet asset failures instead of swallowing them', () => {
+ const source = readFileSync(new URL('../src/pet/app.tsx', import.meta.url), 'utf8')
+ expect(source).toContain('onError={reportPetAssetError}')
+ expect(source).toMatch(/reportPetIssue\('asset'/)
+ })
+})
diff --git a/vitest.config.ts b/vitest.config.ts
index 7631982e..6e784ada 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -32,6 +32,9 @@ export default defineConfig({
'test/patch-layer.test.ts',
// issue #539:旧版 WebKit 缺全局 Iterator 导致插件加载失败,壳层兼容垫片回归。
'test/compat-iterator.test.ts',
+ // 桌宠素材响应头:GitHub 对中文动画名回 `content-disposition`,插件版 fetch 会因
+ // 非 ISO-8859-1 抛错并静默不落 IndexedDB(pnpm patch 过滤该头,这里锁住契约)。
+ 'test/pet-asset-headers.test.ts',
],
maxWorkers: 4,
testTimeout: 30_000,