Skip to content

Commit b02d4d5

Browse files
committed
refactor: allow using server statically
將原本 "NeteaseCloudMusicApi/server" 掃描 modules 目錄的部分抽出 server.js, 改移到 main.js。這樣使用者就有辦法只載入 靜態的伺服器部分(不動態載入模組)。 這個 patch 的 breaking changes 不易發生。 可作為 patch version 發佈。
1 parent 91a3ba7 commit b02d4d5

File tree

4 files changed

+55
-24
lines changed

4 files changed

+55
-24
lines changed

main.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const { cookieToJson } = require('./util')
4+
const request = require('./util/request')
5+
6+
/** @type {Record<string, any>} */
7+
let obj = {}
8+
fs.readdirSync(path.join(__dirname, 'module'))
9+
.reverse()
10+
.forEach((file) => {
11+
if (!file.endsWith('.js')) return
12+
let fileModule = require(path.join(__dirname, 'module', file))
13+
let fn = file.split('.').shift() || ''
14+
obj[fn] = function (data) {
15+
if (typeof data.cookie === 'string') {
16+
data.cookie = cookieToJson(data.cookie)
17+
}
18+
return fileModule(
19+
{
20+
...data,
21+
cookie: data.cookie ? data.cookie : {},
22+
},
23+
request,
24+
)
25+
}
26+
})
27+
28+
/**
29+
* @type {Record<string, any> & import("./server")}
30+
*/
31+
module.exports = {
32+
...require('./server'),
33+
...obj,
34+
}

main.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const assert = require('assert')
2+
const main = require('./main')
3+
4+
describe('methods in server.js', () => {
5+
it('has serveNcmApi', () => {
6+
assert.strictEqual(typeof main.serveNcmApi, 'function')
7+
})
8+
9+
it('has getModulesDefinitions', () => {
10+
assert.strictEqual(typeof main.getModulesDefinitions, 'function')
11+
})
12+
})
13+
14+
describe('methods in module', () => {
15+
it('has activate_init_profile', () => {
16+
assert.strictEqual(typeof main.activate_init_profile, 'function')
17+
})
18+
})

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "网易云音乐 NodeJS 版 API",
55
"scripts": {
66
"start": "node app.js",
7-
"test": "mocha -r intelli-espower-loader -t 20000 server.test.js --exit",
7+
"test": "mocha -r intelli-espower-loader -t 20000 server.test.js main.test.js --exit",
88
"lint": "eslint \"**/*.{js,ts}\"",
99
"lint-fix": "eslint --fix \"**/*.{js,ts}\"",
1010
"prepare": "husky install",
@@ -26,7 +26,7 @@
2626
"音乐",
2727
"网易云音乐nodejs"
2828
],
29-
"main": "server.js",
29+
"main": "main.js",
3030
"types": "./interface.d.ts",
3131
"engines": {
3232
"node": ">=12"
@@ -78,4 +78,4 @@
7878
"prettier": "2.5.1",
7979
"typescript": "4.5.2"
8080
}
81-
}
81+
}

server.js

-21
Original file line numberDiff line numberDiff line change
@@ -294,28 +294,7 @@ async function serveNcmApi(options) {
294294
return appExt
295295
}
296296

297-
let obj = {}
298-
fs.readdirSync(path.join(__dirname, 'module'))
299-
.reverse()
300-
.forEach((file) => {
301-
if (!file.endsWith('.js')) return
302-
let fileModule = require(path.join(__dirname, 'module', file))
303-
obj[file.split('.').shift()] = function (data) {
304-
if (typeof data.cookie === 'string') {
305-
data.cookie = cookieToJson(data.cookie)
306-
}
307-
return fileModule(
308-
{
309-
...data,
310-
cookie: data.cookie ? data.cookie : {},
311-
},
312-
request,
313-
)
314-
}
315-
})
316-
317297
module.exports = {
318298
serveNcmApi,
319299
getModulesDefinitions,
320-
...obj,
321300
}

0 commit comments

Comments
 (0)