按 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
+5
-17
@@ -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 <audio src>, <a download> or a
|
||||
// navigation. Download managers do not care — their browser integration hooks XHR/fetch whose URL ends in a
|
||||
@@ -11,13 +11,10 @@
|
||||
//
|
||||
// `server/index.js` resolves /media/… back to the real file under public/assets/audio and still answers with
|
||||
// `Content-Type: audio/mpeg` + range support; Web Audio sniffs the container, so the URL is all the same to it.
|
||||
// A host that does not implement /media/ keeps working: `audio.js` falls back to the original URL on 404.
|
||||
|
||||
/** Prefix of the extension-less audio route served by `server/index.js`. */
|
||||
export const MEDIA_PREFIX = '/media/';
|
||||
|
||||
/** Extensions `/media/…` may resolve to, in the order the server tries them. */
|
||||
export const AUDIO_EXTS = Object.freeze(['.mp3', '.m4a', '.aac', '.ogg', '.oga', '.opus', '.wav']);
|
||||
// A host that does not implement /media/ keeps working: `audio.js` falls back to the original URL whenever the
|
||||
// /media/ response is unusable (a 404, or a 200 that is not audio at all — some static hosts answer a missing
|
||||
// path with the SPA's index.html).
|
||||
import { MEDIA_PREFIX, AUDIO_EXTS } from '../../shared/media.js';
|
||||
|
||||
const AUDIO_PATH = /^\/assets\/audio\/(.+)$/i;
|
||||
|
||||
@@ -49,12 +46,3 @@ export function mediaUrl(url, origin = globalThis.location?.origin) {
|
||||
if (!rest || segments.some((s) => !s || s === '.' || s === '..' || s.startsWith('.'))) return url;
|
||||
return `${MEDIA_PREFIX}${rest}${u.search}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this URL already one of the extension-less media paths?
|
||||
* @param {string} url
|
||||
* @param {string} [origin]
|
||||
*/
|
||||
export function isMediaUrl(url, origin = globalThis.location?.origin) {
|
||||
return mediaUrl(url, origin) !== url;
|
||||
}
|
||||
Reference in new issue
Block a user