refactor(sdk): musicSdk 全量迁移至 TypeScript

This commit is contained in:
snowy-yang committed 2026-08-29 00:23:50 +08:00
1 parent f289df4cd7
commit 1dfb7a59ca
83 files changed
+924 -732

No files matched your search

@@ -7,31 +7,31 @@ const base62 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
const publicKey = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6sXqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9SdB1Ua44oncaTWz7OBGLbCiK45wIDAQAB\n-----END PUBLIC KEY-----'
const eapiKey = 'e82ckenh8dichen8'
const aesEncrypt = (buffer, mode, key, iv) => {
const aesEncrypt = (buffer: Buffer, mode: string, key: Buffer | string, iv: Buffer | string): Buffer => {
const cipher = createCipheriv(mode, key, iv)
return Buffer.concat([cipher.update(buffer), cipher.final()])
}
const aesDecrypt = function(cipherBuffer, mode, key, iv) {
const aesDecrypt = function(cipherBuffer: Buffer, mode: string, key: Buffer | string, iv: Buffer | string): Buffer {
let decipher = createDecipheriv(mode, key, iv)
return Buffer.concat([decipher.update(cipherBuffer), decipher.final()])
}
const rsaEncrypt = (buffer, key) => {
const rsaEncrypt = (buffer: Buffer, key: string): Buffer => {
buffer = Buffer.concat([Buffer.alloc(128 - buffer.length), buffer])
return publicEncrypt({ key, padding: constants.RSA_NO_PADDING }, buffer)
}
export const weapi = object => {
export const weapi = (object: object): { params: string, encSecKey: string } => {
const text = JSON.stringify(object)
const secretKey = randomBytes(16).map(n => (base62.charAt(n % 62).charCodeAt()))
const secretKey = randomBytes(16).map(n => ((base62.charAt(n % 62) as any).charCodeAt())) as Buffer
return {
params: aesEncrypt(Buffer.from(aesEncrypt(Buffer.from(text), 'aes-128-cbc', presetKey, iv).toString('base64')), 'aes-128-cbc', secretKey, iv).toString('base64'),
encSecKey: rsaEncrypt(secretKey.reverse(), publicKey).toString('hex'),
}
}
export const linuxapi = object => {
export const linuxapi = (object: object): { eparams: string } => {
const text = JSON.stringify(object)
return {
eparams: aesEncrypt(Buffer.from(text), 'aes-128-ecb', linuxapiKey, '').toString('hex').toUpperCase(),
@@ -39,7 +39,7 @@ export const linuxapi = object => {
}
export const eapi = (url, object) => {
export const eapi = (url: string, object: object | string): { params: string } => {
const text = typeof object === 'object' ? JSON.stringify(object) : object
const message = `nobody${url}use${text}md5forencrypt`
const digest = createHash('md5').update(message).digest('hex')
@@ -49,6 +49,6 @@ export const eapi = (url, object) => {
}
}
export const eapiDecrypt = cipherBuffer => {
export const eapiDecrypt = (cipherBuffer: Buffer): string => {
return aesDecrypt(cipherBuffer, 'aes-128-ecb', eapiKey, '').toString()
}
@@ -1,7 +1,8 @@
import { httpFetch } from '../../request.js'
import { eapi } from './crypto.js'
import { httpFetch } from '../../request.ts'
import { eapi } from './crypto.ts'
import type { HttpRequestObject } from '../../types.ts'
export const eapiRequest = (url, data) => {
export const eapiRequest = (url: string, data: object): HttpRequestObject => {
return httpFetch('http://interface.music.163.com/eapi/batch', {
method: 'post',
headers: {