- 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 的不算错——没有证据表明它不是音频)
21 lines
1.3 KiB
JavaScript
21 lines
1.3 KiB
JavaScript
// Extension-less audio route (/media/…) — the one place that defines it.
|
|
//
|
|
// The game plays audio with fetch() + Web Audio, never <audio src> / <a download>. Download managers (IDM / 迅雷 /
|
|
// FDM …) do not care: their browser integration hooks XHR/fetch whose URL ends in a media extension and pops up
|
|
// "下载文件信息" for every BGM track. So the browser asks for audio through a same-origin, extension-less path —
|
|
//
|
|
// /assets/audio/bgm/act1.mp3 → /media/bgm/act1
|
|
//
|
|
// — and `server/index.js` resolves it back to the real file under public/assets/audio, still answering with
|
|
// `Content-Type: audio/mpeg`, Range support and the ETag of the file it resolved.
|
|
//
|
|
// Both sides import this module: the browser to decide which URLs to rewrite (`public/js/media.js`), the server to
|
|
// decide which extensions a `/media/…` request may resolve to. A second copy of the list on one side would let them
|
|
// drift apart silently — the client would rewrite to a path the server no longer accepts.
|
|
|
|
/** Prefix of the extension-less audio route served by `server/index.js`. */
|
|
export const MEDIA_PREFIX = '/media/';
|
|
|
|
/** Extensions a `/media/…` request may resolve to, in the order the server tries them. */
|
|
export const AUDIO_EXTS = Object.freeze(['.mp3', '.m4a', '.aac', '.ogg', '.oga', '.opus', '.wav']);
|