chanjs 2.0.4 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/common/api.js +20 -0
- package/common/code.js +20 -0
- package/extend/api.js +21 -0
- package/extend/file.js +92 -0
- package/extend/upload.js +80 -0
- package/extend/utils.js +342 -0
- package/global/env.js +5 -0
- package/global/global.js +27 -0
- package/global/import.js +46 -0
- package/global/index.js +3 -0
- package/helper/api.js +14 -0
- package/helper/bind.js +21 -0
- package/helper/dataparse.js +24 -0
- package/helper/db.js +75 -0
- package/helper/file.js +202 -0
- package/helper/html.js +19 -0
- package/helper/index.js +25 -0
- package/helper/ip.js +36 -0
- package/helper/jwt.js +41 -0
- package/helper/loader.js +65 -0
- package/helper/response.js +20 -0
- package/helper/safe.js +263 -0
- package/helper/time.js +28 -0
- package/helper/tree.js +32 -0
- package/index.js +9 -3
- package/middleware/safe.js +144 -0
- package/middleware/waf.js +197 -0
- package/package.json +1 -1
- package/utils/dataparse.js +24 -0
- package/utils/loader.js +23 -24
package/package.json
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
/**
|
2
|
+
* 将数组转换为键值对对象
|
3
|
+
* @param {Array} arr - 包含键值对的数组
|
4
|
+
* @param {string} keyField - 作为键的字段名
|
5
|
+
* @param {string} valueField - 作为值的字段名
|
6
|
+
* @returns {Object} 转换后的对象
|
7
|
+
*/
|
8
|
+
export const arrToObj = (arr, keyField = 'config_key', valueField = 'config_value') => {
|
9
|
+
// 输入验证
|
10
|
+
if (!Array.isArray(arr)) {
|
11
|
+
throw new Error('arrToObj 期望接收数组作为第一个参数');
|
12
|
+
}
|
13
|
+
|
14
|
+
return arr.reduce((result, item) => {
|
15
|
+
if (item && typeof item === 'object') {
|
16
|
+
const key = item[keyField];
|
17
|
+
const value = item[valueField];
|
18
|
+
if (key !== undefined && value !== undefined) {
|
19
|
+
result[key] = value;
|
20
|
+
}
|
21
|
+
}
|
22
|
+
return result;
|
23
|
+
}, {});
|
24
|
+
};
|
package/utils/loader.js
CHANGED
@@ -1,32 +1,32 @@
|
|
1
|
-
import fs from
|
2
|
-
import path from
|
3
|
-
import { bindClass } from
|
1
|
+
import fs from "fs";
|
2
|
+
import path from "path";
|
3
|
+
import { bindClass } from "./bind.js";
|
4
4
|
|
5
5
|
/**
|
6
|
-
*
|
6
|
+
*
|
7
7
|
* @param {*} module 模块目录
|
8
|
-
* @returns Array
|
8
|
+
* @returns Array
|
9
9
|
* @description 将web模块放到最后加载
|
10
10
|
*/
|
11
|
-
export const loaderSort = (modules=[])=>{
|
12
|
-
const index = modules.indexOf(
|
11
|
+
export const loaderSort = (modules = []) => {
|
12
|
+
const index = modules.indexOf("web");
|
13
13
|
if (index !== -1) {
|
14
|
-
|
15
|
-
|
14
|
+
const web = modules.splice(index, 1);
|
15
|
+
modules.push(web[0]);
|
16
16
|
}
|
17
17
|
return modules;
|
18
|
-
}
|
18
|
+
};
|
19
19
|
|
20
|
-
export const getPackage = async function(){
|
21
|
-
let pkg = await importFile(
|
20
|
+
export const getPackage = async function () {
|
21
|
+
let pkg = await importFile("package.json");
|
22
22
|
return pkg;
|
23
|
-
}
|
23
|
+
};
|
24
24
|
|
25
|
-
export const loadConfig = async function(){
|
26
|
-
let config = await importFile(
|
25
|
+
export const loadConfig = async function () {
|
26
|
+
let config = await importFile("config/index.js");
|
27
|
+
console.log("config", config);
|
27
28
|
return config;
|
28
|
-
}
|
29
|
-
|
29
|
+
};
|
30
30
|
|
31
31
|
/**
|
32
32
|
* 加载指定模块名下的所有控制器文件
|
@@ -36,22 +36,24 @@ export const loadConfig = async function(){
|
|
36
36
|
export const loadController = async function (moduleName) {
|
37
37
|
const controller = {};
|
38
38
|
|
39
|
-
const dir = path.join(MODULES_PATH, moduleName,
|
39
|
+
const dir = path.join(MODULES_PATH, moduleName, "controller");
|
40
40
|
|
41
41
|
if (!fs.existsSync(dir)) {
|
42
42
|
console.warn(`模块路径不存在,跳过加载控制器: ${dir}`);
|
43
43
|
return controller;
|
44
44
|
}
|
45
45
|
|
46
|
-
const files = fs.readdirSync(dir).filter(file => file.endsWith(
|
46
|
+
const files = fs.readdirSync(dir).filter((file) => file.endsWith(".js"));
|
47
47
|
|
48
48
|
for (const file of files) {
|
49
49
|
const filePath = path.join(dir, file);
|
50
|
-
const name = file.replace(/\.js$/i,
|
50
|
+
const name = file.replace(/\.js$/i, ""); // 安全处理 .js 后缀
|
51
51
|
|
52
52
|
try {
|
53
53
|
const module = await importFile(filePath);
|
54
|
-
|
54
|
+
let obj = module.default || module;
|
55
|
+
|
56
|
+
controller[name] = obj;
|
55
57
|
} catch (e) {
|
56
58
|
console.error(`加载控制器失败: ${filePath}`, e);
|
57
59
|
// 可选:抛出错误或继续加载其他文件
|
@@ -61,6 +63,3 @@ export const loadController = async function (moduleName) {
|
|
61
63
|
|
62
64
|
return controller;
|
63
65
|
};
|
64
|
-
|
65
|
-
|
66
|
-
|