- 移除 bd/mg 两平台及磁盘残留产物;清理 api-test 死脚本、musicSdk/message+options 死模块、infSign vendor - musicSdk/ 改名 sdk/;src/common、src/auth、src/apis 整体移入 sdk/,依赖方向定为 src → sdk 严格单向 - src/ 手写 JS 全量转 .ts,删除 JS 类型垫片 modules.d.ts;源码 import 统一 .ts 后缀 - Node 原生类型剥离直接运行 .ts(engines >=22.18),取消 tsc 发射产物,tsc 仅类型检查(pnpm typecheck) - 删除 kwdes 备用实现与 src/index.ts 转发垫片;测试移至 test/smoke-test.ts - common/request.ts 补显式导出类型;修复 set-cookie 单值形态、ApiError 类字段等隐患 验证:tsc 零错误,smoke-test 41/41
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
// 酷狗 krc 歌词解码
|
|
|
|
import zlib from 'node:zlib'
|
|
import { decodeName } from '../utils.ts'
|
|
|
|
// 基于社区通用的 krc 解析思路
|
|
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
|
|
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]
|
|
}
|
|
zlib.inflate(buf_str, (err, result) => {
|
|
if (err) return reject(err)
|
|
resolve(result.toString())
|
|
})
|
|
})
|
|
|
|
const headExp = /^.*\[id:\$\w+\]\n/
|
|
|
|
const parseLyric = str => {
|
|
str = str.replace(/\r/g, '')
|
|
if (headExp.test(str)) str = str.replace(headExp, '')
|
|
let trans = str.match(/\[language:([\w=\\/+]+)\]/)
|
|
let lyric
|
|
let rlyric
|
|
let tlyric
|
|
if (trans) {
|
|
str = str.replace(/\[language:[\w=\\/+]+\]\n/, '')
|
|
let json = JSON.parse(Buffer.from(trans[1], 'base64').toString())
|
|
for (const item of json.content) {
|
|
switch (item.type) {
|
|
case 0:
|
|
rlyric = item.lyricContent
|
|
break
|
|
case 1:
|
|
tlyric = item.lyricContent
|
|
break
|
|
}
|
|
}
|
|
}
|
|
let i = 0
|
|
let yrc = str.replace(/\[((\d+),\d+)\].*/g, str => {
|
|
let result = str.match(/\[((\d+),\d+)\].*/)
|
|
let time: any = parseInt(result[2])
|
|
let ms = time % 1000
|
|
time /= 1000
|
|
let m = Math.trunc(time / 60).toString().padStart(2, '0')
|
|
time %= 60
|
|
let s = Math.trunc(time).toString().padStart(2, '0')
|
|
time = `${m}:${s}.${ms}`
|
|
if (rlyric) rlyric[i] = `[${time}]${rlyric[i]?.join('') ?? ''}`
|
|
if (tlyric) tlyric[i] = `[${time}]${tlyric[i]?.join('') ?? ''}`
|
|
i++
|
|
return str.replace(result[1], time)
|
|
})
|
|
rlyric = rlyric ? rlyric.join('\n') : ''
|
|
tlyric = tlyric ? tlyric.join('\n') : ''
|
|
yrc = yrc.replace(/<(\d+,\d+),\d+>/g, '<$1>')
|
|
yrc = decodeName(yrc)
|
|
lyric = yrc.replace(/<\d+,\d+>/g, '')
|
|
rlyric = decodeName(rlyric)
|
|
tlyric = decodeName(tlyric)
|
|
return {
|
|
lyric,
|
|
tlyric,
|
|
rlyric,
|
|
yrc,
|
|
}
|
|
}
|
|
|
|
export const decodeKrc = async data => {
|
|
return decodeLyric(data).then(parseLyric)
|
|
}
|
|
|
|
export default { decodeKrc }
|