- 移除 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
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.log(err)
|
|
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 ?? ''))
|
|
}
|