From b02d4d5e046179aac8608246d3569f3e0f42b90b Mon Sep 17 00:00:00 2001 From: pan93412 Date: Fri, 28 Jan 2022 12:52:21 +0800 Subject: [PATCH] refactor: allow using server statically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 將原本 "NeteaseCloudMusicApi/server" 掃描 modules 目錄的部分抽出 server.js, 改移到 main.js。這樣使用者就有辦法只載入 靜態的伺服器部分(不動態載入模組)。 這個 patch 的 breaking changes 不易發生。 可作為 patch version 發佈。 --- main.js | 34 ++++++++++++++++++++++++++++++++++ main.test.js | 18 ++++++++++++++++++ package.json | 6 +++--- server.js | 21 --------------------- 4 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 main.js create mode 100644 main.test.js diff --git a/main.js b/main.js new file mode 100644 index 00000000000..3cca747b498 --- /dev/null +++ b/main.js @@ -0,0 +1,34 @@ +const fs = require('fs') +const path = require('path') +const { cookieToJson } = require('./util') +const request = require('./util/request') + +/** @type {Record} */ +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 & import("./server")} + */ +module.exports = { + ...require('./server'), + ...obj, +} diff --git a/main.test.js b/main.test.js new file mode 100644 index 00000000000..2a7a6cad942 --- /dev/null +++ b/main.test.js @@ -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') + }) +}) diff --git a/package.json b/package.json index fdc53bf4675..1d3c76046c2 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "网易云音乐 NodeJS 版 API", "scripts": { "start": "node app.js", - "test": "mocha -r intelli-espower-loader -t 20000 server.test.js --exit", + "test": "mocha -r intelli-espower-loader -t 20000 server.test.js main.test.js --exit", "lint": "eslint \"**/*.{js,ts}\"", "lint-fix": "eslint --fix \"**/*.{js,ts}\"", "prepare": "husky install", @@ -26,7 +26,7 @@ "音乐", "网易云音乐nodejs" ], - "main": "server.js", + "main": "main.js", "types": "./interface.d.ts", "engines": { "node": ">=12" @@ -78,4 +78,4 @@ "prettier": "2.5.1", "typescript": "4.5.2" } -} +} \ No newline at end of file diff --git a/server.js b/server.js index f89ef46af00..4d8e4b086bd 100644 --- a/server.js +++ b/server.js @@ -294,28 +294,7 @@ async function serveNcmApi(options) { return appExt } -let obj = {} -fs.readdirSync(path.join(__dirname, 'module')) - .reverse() - .forEach((file) => { - if (!file.endsWith('.js')) return - let fileModule = require(path.join(__dirname, 'module', file)) - obj[file.split('.').shift()] = function (data) { - if (typeof data.cookie === 'string') { - data.cookie = cookieToJson(data.cookie) - } - return fileModule( - { - ...data, - cookie: data.cookie ? data.cookie : {}, - }, - request, - ) - } - }) - module.exports = { serveNcmApi, getModulesDefinitions, - ...obj, }