按 review 意见:/media/ 的路径与扩展名列表提到 shared/ 共用

- 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 的不算错——没有证据表明它不是音频)
This commit is contained in:
kanomahoro1224 committed 2026-10-03 22:33:20 +08:00
1 parent a069be4538
commit cfa51e5f1b
6 files changed
+109 -28

No files matched your search

+56
View File
@@ -229,6 +229,62 @@ describe('AudioManager', () => {
assert.ok(!urls.includes(raw), '/media/ 成功就不该再请求 .mp3 地址');
} finally { globalThis.fetch = origFetch; }
}
// 第一发 200 但内容不是音频:有些静态托管对不存在的路径回 200 + index.html,解码会静默失败,也要回退。
{
const fw = fakeWindow();
const urls = [];
let cancelled = 0;
const origFetch = globalThis.fetch;
globalThis.fetch = async (u) => {
urls.push(u);
if (u !== media) return { ok: true, arrayBuffer: async () => new ArrayBuffer(8) };
return {
ok: true,
status: 200,
headers: { get: (n) => (n.toLowerCase() === 'content-type' ? 'text/html; charset=utf-8' : null) },
body: { cancel: async () => { cancelled += 1; } },
arrayBuffer: async () => new ArrayBuffer(8),
};
};
try {
const a = new AudioManager({ win: fw.win, getManifest: () => manifest });
a.install();
fw.fire('pointerdown');
a.playBgm('prep');
await new Promise((r) => setTimeout(r, 25));
assert.ok(urls.includes(raw), '内容不是音频时回退到原地址');
assert.equal(cancelled, 1, '丢掉那个用不上的响应,别把连接挂着');
} finally { globalThis.fetch = origFetch; }
}
// /media/ 直接给出 audio/*(服务端真实行为):不回退,也不去 cancel 一个能用的响应
{
const fw = fakeWindow();
const urls = [];
let cancelled = 0;
const origFetch = globalThis.fetch;
globalThis.fetch = async (u) => {
urls.push(u);
return {
ok: true,
status: 200,
headers: { get: (n) => (n.toLowerCase() === 'content-type' ? 'audio/mpeg' : null) },
body: { cancel: async () => { cancelled += 1; } },
arrayBuffer: async () => new ArrayBuffer(8),
};
};
try {
const a = new AudioManager({ win: fw.win, getManifest: () => manifest });
a.install();
fw.fire('pointerdown');
a.playBgm('prep');
await new Promise((r) => setTimeout(r, 25));
assert.ok(urls.includes(media));
assert.ok(!urls.includes(raw), 'audio/* 就是成功,不该再回退');
assert.equal(cancelled, 0);
} finally { globalThis.fetch = origFetch; }
}
});
});