fix: 修复代码审查发现的 P0/P1 缺陷(含测试加固)
审查明细见 CODE-AUDIT.md。本提交覆盖其中的代码级修复:
P0-2 migu 解密流异常逃逸 → 进程崩溃
src/http/migu-stream.ts 把 transform/onChunk 就地 try/catch,转 reject 并 res.destroy()。
为什么:二者在 res 的 data 事件回调里同步执行,抛出的异常既不在 Promise 链上、
也不在路由层 try/catch 内,会逃逸成 uncaughtException(全项目无全局处理器)直接杀进程。
已用最小复现验证:修复前 exit 9,修复后正常 reject。
P0-1 /login/lr 缺失 → lr 取流 100% 不可用
src/routes-login.ts 补 POST/DELETE /login/lr。
为什么:sdk/lr/auth.ts 早已实现 login、该文件也已 import lrAuth,只差路由注册;
lr 取流强制要求登录态,缺此入口等于整个平台不可用,而 README 已将其文档化。
P0-3 + P1-7 凭据存储
sdk/auth/store.ts 写入改 0600 + 「临时文件 + rename」原子替换;解析失败改为改名备份而非静默清空;
新增 DATA_DIR 环境变量(测试隔离用)。
为什么:原先直接覆写明文 JSON(0644)且解析失败静默置空,写入中途被杀会留下截断 JSON,
下次启动解析失败后,任意一次 setCredentials 都会以空表覆盖,导致全部平台凭据丢失。
P1-9 / P1-10 / P1-11 / P1-12 平台模块缺陷
sdk/kg/krcDecode.ts:空歌词显式 resolve('')(原先裸 return 让 Promise 永久 pending,请求挂死)
sdk/kg/lyric.ts:重试改调 getLyricDownload(原先误调 getLyric,参数语义不同导致重试恒失败)
sdk/kw/songList.ts:重试改自递归(原先误调 getListDetail,返回类型错位且把 tryNum 当 page)
sdk/kw/songList.ts:删除 URL 模板中多余的右花括号
P1-6 /song-list 参数契约
src/routes.ts 移除不生效的 limit 声明。
为什么:各平台 getList 签名不一致(如 kw 为 (sortId, tagId, page, tryNum)),无法统一透传,
保留声明只会让文档与实现不符。
P1-4/5/13/15 冒烟测试加固
test/smoke-test.ts:子实例统一登记 + 进程退出回收(原先异常路径留下孤儿实例,曾累积 5 个);
启动超时 10s→30s 且失败时附子进程输出、提前退出立即报错;显式 SOURCE_ENABLED='' 与 DATA_DIR
隔离(原先继承 .env,取流用例走外部音源掩盖 SDK 回归,且 /login/refresh 会改写真实 data/auth.json);
AUTH 断言移入 test()(原先裸抛错会跳过实例回收);修正 642 行过期断言并补 kw parse 正向用例。
P1-8 src/auth/store.ts 删除引用已不存在命令 `pnpm build` 的过期注释。
src/server.ts 头注释的接口清单改为 path 风格,与实际路由一致。
注:src/routes.ts、src/routes-login.ts、src/server.ts、test/smoke-test.ts 同时承载了工作区
既有的在途改动(parse/playlist/album 路由接入与相应用例),一并随本提交入库。
This commit is contained in:
1 parent
ece9c5fc9a
commit
47ce651f46
10 files changed
+825
-73
No files matched your search
+3
-1
@@ -7,7 +7,9 @@ import { decodeName } from '../common/utils.ts'
|
||||
const enc_key = Buffer.from([0x40, 0x47, 0x61, 0x77, 0x5e, 0x32, 0x74, 0x47, 0x51, 0x36, 0x31, 0x2d, 0xce, 0xd2, 0x6e, 0x69])
|
||||
|
||||
const decodeLyric = str => new Promise((resolve, reject) => {
|
||||
if (!str.length) return
|
||||
// 空歌词必须显式 resolve:executor 里裸 return 会让 Promise 永久 pending,
|
||||
// 调用方的 await 将一直挂起(连接与内存无法释放,只能等客户端超时)
|
||||
if (!str.length) return resolve('')
|
||||
const buf_str = Buffer.from(str, 'base64').subarray(4)
|
||||
for (let i = 0, len = buf_str.length; i < len; i++) {
|
||||
buf_str[i] = buf_str[i] ^ enc_key[i % 16]
|
||||
|
||||
+2
-1
@@ -69,7 +69,8 @@ export default {
|
||||
requestObj.promise = (requestObj.promise as any).then(({ body, statusCode }) => {
|
||||
if (statusCode !== 200) {
|
||||
if (tryNum > 5) return Promise.reject(new Error('歌词获取失败'))
|
||||
let tryRequestObj = (this.getLyric as any)(id, accessKey, fmt, ++tryNum)
|
||||
// 重试必须回到本方法 getLyricDownload;原先误调 getLyric(参数语义不同)导致重试恒失败
|
||||
const tryRequestObj = this.getLyricDownload(id, accessKey, fmt, ++tryNum)
|
||||
requestObj.cancelHttp = tryRequestObj.cancelHttp.bind(tryRequestObj)
|
||||
return tryRequestObj.promise
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user