forked from Binaryify/NeteaseCloudMusicApi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: allow using server statically
將原本 "NeteaseCloudMusicApi/server" 掃描 modules 目錄的部分抽出 server.js, 改移到 main.js。這樣使用者就有辦法只載入 靜態的伺服器部分(不動態載入模組)。 這個 patch 的 breaking changes 不易發生。 可作為 patch version 發佈。
- Loading branch information
Showing
4 changed files
with
55 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const { cookieToJson } = require('./util') | ||
const request = require('./util/request') | ||
|
||
/** @type {Record<string, any>} */ | ||
let obj = {} | ||
fs.readdirSync(path.join(__dirname, 'module')) | ||
.reverse() | ||
.forEach((file) => { | ||
if (!file.endsWith('.js')) return | ||
let fileModule = require(path.join(__dirname, 'module', file)) | ||
let fn = file.split('.').shift() || '' | ||
obj[fn] = function (data) { | ||
if (typeof data.cookie === 'string') { | ||
data.cookie = cookieToJson(data.cookie) | ||
} | ||
return fileModule( | ||
{ | ||
...data, | ||
cookie: data.cookie ? data.cookie : {}, | ||
}, | ||
request, | ||
) | ||
} | ||
}) | ||
|
||
/** | ||
* @type {Record<string, any> & import("./server")} | ||
*/ | ||
module.exports = { | ||
...require('./server'), | ||
...obj, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const assert = require('assert') | ||
const main = require('./main') | ||
|
||
describe('methods in server.js', () => { | ||
it('has serveNcmApi', () => { | ||
assert.strictEqual(typeof main.serveNcmApi, 'function') | ||
}) | ||
|
||
it('has getModulesDefinitions', () => { | ||
assert.strictEqual(typeof main.getModulesDefinitions, 'function') | ||
}) | ||
}) | ||
|
||
describe('methods in module', () => { | ||
it('has activate_init_profile', () => { | ||
assert.strictEqual(typeof main.activate_init_profile, 'function') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters