- 删除旧 sdk/request.ts,全部 import 收敛到 sdk/common/request.ts(85 文件) - 注释死代码 630+ 行、裸 console.log 吞错 6 处、未使用符号 26 处清零 - bili 状态校验去重;utils DNS 预解析失败日志改 warn 带上下文 - CODE-AUDIT.md:第四节 P2 清理记录 + 第八节线索逐条复核结论(kw/kg/tx/wy parse 骨架相似实质各异,不抽公共层)
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import crypto from 'crypto'
|
|
import dns from 'dns'
|
|
import { decodeName } from './common/utils.ts'
|
|
|
|
export const toMD5 = (str: string): string => crypto.createHash('md5').update(str).digest('hex')
|
|
|
|
interface CachedIp {
|
|
address: string
|
|
family: number
|
|
}
|
|
|
|
const ipMap = new Map<string, CachedIp | true>()
|
|
|
|
export const getHostIp = (hostname: string): CachedIp | undefined => {
|
|
const result = ipMap.get(hostname)
|
|
if (typeof result === 'object') return result
|
|
if (result === true) return
|
|
ipMap.set(hostname, true)
|
|
dns.lookup(hostname, {
|
|
// family: 4,
|
|
all: false,
|
|
}, (err, address, family) => {
|
|
if (err) return console.warn(`[utils] DNS 预解析 ${hostname} 失败: ${err.message}`)
|
|
ipMap.set(hostname, { address, family })
|
|
})
|
|
}
|
|
|
|
export const dnsLookup = (
|
|
hostname: string,
|
|
options: dns.LookupOptions,
|
|
callback: (err: NodeJS.ErrnoException | null, address: string, family: number) => void,
|
|
): void => {
|
|
const result = getHostIp(hostname)
|
|
if (result) return callback(null, result.address, result.family)
|
|
|
|
dns.lookup(hostname, options, callback as any)
|
|
}
|
|
|
|
/**
|
|
* 格式化歌手
|
|
* @param singers 歌手数组或字符串
|
|
* @param nameKey 歌手名键值
|
|
* @param join 歌手分割字符
|
|
*/
|
|
export const formatSingerName = (
|
|
singers: Array<Record<string, unknown>> | string | null | undefined,
|
|
nameKey = 'name',
|
|
join = '、',
|
|
): string => {
|
|
if (Array.isArray(singers)) {
|
|
const singer: string[] = []
|
|
singers.forEach(item => {
|
|
const name = item[nameKey]
|
|
if (!name) return
|
|
singer.push(String(name))
|
|
})
|
|
return decodeName(singer.join(join))
|
|
}
|
|
return decodeName(String(singers ?? ''))
|
|
}
|