按 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:
1 parent
a069be4538
commit
cfa51e5f1b
6 files changed
+109
-28
No files matched your search
@@ -2,7 +2,9 @@
|
||||
// stop hijacking BGM playback with a "下载文件信息" dialog. See public/js/media.js for the full rationale.
|
||||
import { test, describe } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mediaUrl, isMediaUrl, MEDIA_PREFIX, AUDIO_EXTS } from '../public/js/media.js';
|
||||
import { mediaUrl } from '../public/js/media.js';
|
||||
// 前缀与扩展名列表只有一份(客户端和服务端都从这里取),所以断言也直接盯住这一份。
|
||||
import { MEDIA_PREFIX, AUDIO_EXTS } from '../shared/media.js';
|
||||
|
||||
const ORIGIN = 'http://127.0.0.1:3000';
|
||||
|
||||
@@ -44,11 +46,6 @@ describe('mediaUrl: 音频地址去掉扩展名(躲开下载器嗅探)', ()
|
||||
assert.equal(mediaUrl('/assets/audio/bgm/../x.mp3', ORIGIN), '/media/x');
|
||||
});
|
||||
|
||||
test('isMediaUrl 只对真正会改写的地址为真', () => {
|
||||
assert.equal(isMediaUrl('/assets/audio/bgm/a.mp3', ORIGIN), true);
|
||||
assert.equal(isMediaUrl('/assets/img/a.png', ORIGIN), false);
|
||||
});
|
||||
|
||||
test('没有 location 时(测试环境)用传入的 origin 判断同源', () => {
|
||||
assert.equal(mediaUrl('/assets/audio/bgm/a.mp3'), '/media/bgm/a', '相对地址本来就同源');
|
||||
assert.equal(mediaUrl('https://other.example/assets/audio/a.mp3'), 'https://other.example/assets/audio/a.mp3');
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user