From cfa51e5f1b9712518903a0e69b85eee6fecb6837 Mon Sep 17 00:00:00 2001 From: kanomahoro1224 <3497593286@qq.com> Date: Sat, 3 Oct 2026 22:33:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=20review=20=E6=84=8F=E8=A7=81?= =?UTF-8?q?=EF=BC=9A/media/=20=E7=9A=84=E8=B7=AF=E5=BE=84=E4=B8=8E?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E5=90=8D=E5=88=97=E8=A1=A8=E6=8F=90=E5=88=B0?= =?UTF-8?q?=20shared/=20=E5=85=B1=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - server/index.js 开头的路由说明补上 /media/ - AUDIO_EXTS 原先客户端(public/js/media.js)与服务端各一份,只改一边就会悄悄漂移: 现在两边都从 shared/media.js 取;服务端那份不再 export - isMediaUrl 只有测试在用,删掉(新增 shared/media.js,测试改从那里取前缀/扩展名) - 顺手做了 review 里「可选」的那点:有些静态托管对不存在的路径回 200 + index.html, 这种响应解码会静默失败,所以除了 404,声明了非 audio/* 类型的响应也回退到原地址 (完全没声明 content-type 的不算错——没有证据表明它不是音频) --- public/js/audio.js | 20 ++++++++++++++- public/js/media.js | 22 ++++------------- server/index.js | 10 +++++--- shared/media.js | 20 +++++++++++++++ test/media-url.test.js | 9 +++---- test/ui/audio.test.js | 56 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 28 deletions(-) create mode 100644 shared/media.js diff --git a/public/js/audio.js b/public/js/audio.js index a8948dd8..a23eb06e 100644 --- a/public/js/audio.js +++ b/public/js/audio.js @@ -216,6 +216,20 @@ export class SfxLimiter { // ---- manager ----------------------------------------------------------------------------------------------- +/** + * Could Web Audio decode this response? A host without the `/media/` route answers 404; some static hosts answer a + * missing path with 200 + the SPA's index.html instead, and fetching *that* would fail to decode as silently as a + * 404 would — so the fallback looks at the declared type too. + * + * A response that declares no type at all is not treated as wrong: absence of a header is not evidence of an HTML + * page, and fetch stubs / minimal hosts legitimately omit it. + * @param {{ ok?: boolean, headers?: { get?: (n: string) => string | null } }} res + */ +function isAudioResponse(res) { + if (!res || !res.ok) return false; + const type = res.headers?.get?.('content-type'); + return !type || /^\s*audio\//i.test(type); +} export class AudioManager { /** * @param {{ getManifest?: () => any, win?: any }} [opts] @@ -387,7 +401,11 @@ export class AudioManager { // Extension-less URL first so download managers leave the BGM alone; a host without /media/ still works. const media = mediaUrl(url); let res = await fetch(media); - if (!res.ok && media !== url) res = await fetch(url); + if (media !== url && !isAudioResponse(res)) { + // Drop the unusable response (404, or a 200 that is really index.html) before trying the original URL. + try { await res.body?.cancel?.(); } catch { /* the fallback request matters more than draining this one */ } + res = await fetch(url); + } if (!res.ok) throw new Error(`HTTP ${res.status}`); const ab = await res.arrayBuffer(); return await new Promise((resolve) => { diff --git a/public/js/media.js b/public/js/media.js index 6b7fade5..7d125407 100644 --- a/public/js/media.js +++ b/public/js/media.js @@ -1,4 +1,4 @@ -// Audio URLs for download managers (IDM / 迅雷 / FDM …) — and why they have no extension. +// audio URL rewriting for download managers (IDM / 迅雷 / FDM …) — see shared/media.js for the why. // // The game plays audio with fetch() + Web Audio (`audio.js`): it never uses