审查明细见 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 路由接入与相应用例),一并随本提交入库。
110 lines
4.2 KiB
TypeScript
110 lines
4.2 KiB
TypeScript
import { httpFetch } from '../request.ts'
|
|
import { decodeKrc } from './krcDecode.ts'
|
|
|
|
export default {
|
|
getIntv(interval) {
|
|
if (!interval) return 0
|
|
let intvArr = interval.split(':')
|
|
let intv = 0
|
|
let unit = 1
|
|
while (intvArr.length) {
|
|
intv += (intvArr.pop()) * unit
|
|
unit *= 60
|
|
}
|
|
return parseInt(intv as unknown as string)
|
|
},
|
|
// getLyric(songInfo, tryNum = 0) {
|
|
// let requestObj = httpFetch(`http://m.kugou.com/app/i/krc.php?cmd=100&keyword=${encodeURIComponent(songInfo.name)}&hash=${songInfo.hash}&timelength=${songInfo._interval || this.getIntv(songInfo.interval)}&d=0.38664927426725626`, {
|
|
// headers: {
|
|
// 'KG-RC': 1,
|
|
// 'KG-THash': 'expand_search_manager.cpp:852736169:451',
|
|
// 'User-Agent': 'KuGou2012-9020-ExpandSearchManager',
|
|
// },
|
|
// })
|
|
// requestObj.promise = requestObj.promise.then(({ body, statusCode }) => {
|
|
// if (statusCode !== 200) {
|
|
// if (tryNum > 5) return Promise.reject(new Error('歌词获取失败'))
|
|
// let tryRequestObj = this.getLyric(songInfo, ++tryNum)
|
|
// requestObj.cancelHttp = tryRequestObj.cancelHttp.bind(tryRequestObj)
|
|
// return tryRequestObj.promise
|
|
// }
|
|
// return {
|
|
// lyric: body,
|
|
// tlyric: '',
|
|
// }
|
|
// })
|
|
// return requestObj
|
|
// },
|
|
searchLyric(name, hash, time, tryNum = 0) {
|
|
let requestObj = httpFetch(`http://lyrics.kugou.com/search?ver=1&man=yes&client=pc&keyword=${encodeURIComponent(name)}&hash=${hash}&timelength=${time}&lrctxt=1`, {
|
|
headers: {
|
|
'KG-RC': '1',
|
|
'KG-THash': 'expand_search_manager.cpp:852736169:451',
|
|
'User-Agent': 'KuGou2012-9020-ExpandSearchManager',
|
|
},
|
|
})
|
|
requestObj.promise = (requestObj.promise as any).then(({ body, statusCode }) => {
|
|
if (statusCode !== 200) {
|
|
if (tryNum > 5) return Promise.reject(new Error('歌词获取失败'))
|
|
let tryRequestObj = this.searchLyric(name, hash, time, ++tryNum)
|
|
requestObj.cancelHttp = tryRequestObj.cancelHttp.bind(tryRequestObj)
|
|
return tryRequestObj.promise
|
|
}
|
|
if (body.candidates.length) {
|
|
let info = body.candidates[0]
|
|
return { id: info.id, accessKey: info.accesskey, fmt: (info.krctype == 1 && info.contenttype != 1) ? 'krc' : 'lrc' }
|
|
}
|
|
return null
|
|
})
|
|
return requestObj
|
|
},
|
|
getLyricDownload(id, accessKey, fmt, tryNum = 0) {
|
|
let requestObj = httpFetch(`http://lyrics.kugou.com/download?ver=1&client=pc&id=${id}&accesskey=${accessKey}&fmt=${fmt}&charset=utf8`, {
|
|
headers: {
|
|
'KG-RC': '1',
|
|
'KG-THash': 'expand_search_manager.cpp:852736169:451',
|
|
'User-Agent': 'KuGou2012-9020-ExpandSearchManager',
|
|
},
|
|
})
|
|
requestObj.promise = (requestObj.promise as any).then(({ body, statusCode }) => {
|
|
if (statusCode !== 200) {
|
|
if (tryNum > 5) return Promise.reject(new Error('歌词获取失败'))
|
|
// 重试必须回到本方法 getLyricDownload;原先误调 getLyric(参数语义不同)导致重试恒失败
|
|
const tryRequestObj = this.getLyricDownload(id, accessKey, fmt, ++tryNum)
|
|
requestObj.cancelHttp = tryRequestObj.cancelHttp.bind(tryRequestObj)
|
|
return tryRequestObj.promise
|
|
}
|
|
|
|
switch (body.fmt) {
|
|
case 'krc':
|
|
return decodeKrc(body.content)
|
|
case 'lrc':
|
|
return {
|
|
lyric: Buffer.from(body.content, 'base64').toString('utf-8'),
|
|
tlyric: '',
|
|
rlyric: '',
|
|
yrc: '',
|
|
}
|
|
default:
|
|
return Promise.reject(new Error(`未知歌词格式: ${body.fmt}`))
|
|
}
|
|
})
|
|
|
|
return requestObj
|
|
},
|
|
getLyric(songInfo, tryNum = 0) {
|
|
let requestObj = this.searchLyric(songInfo.name, songInfo.hash, songInfo._interval || this.getIntv(songInfo.interval))
|
|
|
|
requestObj.promise = requestObj.promise.then(result => {
|
|
if (!result) return Promise.reject(new Error('Get lyric failed'))
|
|
|
|
let requestObj2 = this.getLyricDownload((result as any).id, (result as any).accessKey, (result as any).fmt)
|
|
|
|
requestObj.cancelHttp = requestObj2.cancelHttp.bind(requestObj2)
|
|
|
|
return requestObj2.promise
|
|
}) as any
|
|
return requestObj
|
|
},
|
|
}
|