@x-9lab/xlab 1.0.0-alpha.1
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/LICENSE +21 -0
- package/README.md +182 -0
- package/dist/@config/config.dev.js +13 -0
- package/dist/@config/config.js +25 -0
- package/dist/@config/config.sand.js +10 -0
- package/dist/bin/watch.js +145 -0
- package/dist/bin/xlab +49 -0
- package/dist/business/utils/cookie/1.1.0/clean/index.js +35 -0
- package/dist/business/utils/cookie/1.1.0/clean/js/@tpls/js.tpl +32 -0
- package/dist/business/utils/cookie/1.1.0/clean/js/index.js +30 -0
- package/dist/cluster.js +120 -0
- package/dist/components/assets/index.js +31 -0
- package/dist/components/cache/index.js +9 -0
- package/dist/components/cache/lru.js +75 -0
- package/dist/components/cluster/index.js +40 -0
- package/dist/components/combo/index.js +170 -0
- package/dist/components/common.js +324 -0
- package/dist/components/cron.js +117 -0
- package/dist/components/header/index.js +74 -0
- package/dist/components/header/time.js +37 -0
- package/dist/components/html-processor/index.js +95 -0
- package/dist/components/injection/index.js +83 -0
- package/dist/components/js-processor/index.js +65 -0
- package/dist/components/log.js +112 -0
- package/dist/components/md5/index.js +19 -0
- package/dist/components/mime/index.js +18 -0
- package/dist/components/platform/index.js +103 -0
- package/dist/components/proxy/index.js +62 -0
- package/dist/components/querystring/index.js +60 -0
- package/dist/components/redirect/index.js +79 -0
- package/dist/components/return-code.js +117 -0
- package/dist/components/uuid/index.js +33 -0
- package/dist/components/version/index.js +96 -0
- package/dist/config/getEnv.js +61 -0
- package/dist/config/index.js +137 -0
- package/dist/config/process-custom-config-files.js +67 -0
- package/dist/config/process-def-config-file.js +29 -0
- package/dist/custom.js +44 -0
- package/dist/global.js +67 -0
- package/dist/middleware/@bin/html-filter.js +94 -0
- package/dist/middleware/bad-request.js +36 -0
- package/dist/middleware/combo.js +13 -0
- package/dist/middleware/compress.js +36 -0
- package/dist/middleware/cors.js +66 -0
- package/dist/middleware/fresh-filter.js +63 -0
- package/dist/middleware/handle-pre-dir.js +25 -0
- package/dist/middleware/request-filter.js +144 -0
- package/dist/middleware/service-mark.js +30 -0
- package/dist/middlewares.js +40 -0
- package/dist/package.json +61 -0
- package/dist/router.js +101 -0
- package/dist/server.js +132 -0
- package/package.json +59 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
log: ()=>commonLog,
|
|
13
|
+
default: ()=>_default,
|
|
14
|
+
get: ()=>get,
|
|
15
|
+
update: ()=>update
|
|
16
|
+
});
|
|
17
|
+
const _processCustomConfigFiles = /*#__PURE__*/ _interopRequireDefault(require("./process-custom-config-files"));
|
|
18
|
+
const _processDefConfigFile = /*#__PURE__*/ _interopRequireDefault(require("./process-def-config-file"));
|
|
19
|
+
const _utils = require("@x-drive/utils");
|
|
20
|
+
const _log = require("../components/log");
|
|
21
|
+
const _cluster = /*#__PURE__*/ _interopRequireDefault(require("cluster"));
|
|
22
|
+
const _getEnv = /*#__PURE__*/ _interopRequireDefault(require("./getEnv"));
|
|
23
|
+
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
24
|
+
function _interopRequireDefault(obj) {
|
|
25
|
+
return obj && obj.__esModule ? obj : {
|
|
26
|
+
default: obj
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const argv = {};
|
|
30
|
+
process.argv.forEach(function(item) {
|
|
31
|
+
if (item.indexOf("=") !== -1) {
|
|
32
|
+
const argPart = item.split("=");
|
|
33
|
+
argv[argPart[0]] = argPart[1] || undefined;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
const meta = require(_path.default.resolve(process.cwd(), "package.json"));
|
|
37
|
+
// 是否正处于 master 进程
|
|
38
|
+
const IS_MASTER = parseInt(process.versions.node) >= 16 ? _cluster.default.isPrimary : _cluster.default.isMaster;
|
|
39
|
+
const logger = _log.globalLog.getLogger("config");
|
|
40
|
+
/**
|
|
41
|
+
* 日志类型存储对象
|
|
42
|
+
*/ const LOGERS = {
|
|
43
|
+
"config": logger
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* 通用日志方法
|
|
47
|
+
* @param cat 业务类型
|
|
48
|
+
* @param type 日志等级
|
|
49
|
+
*/ function commonLog(cat, type) {
|
|
50
|
+
var logArr;
|
|
51
|
+
var setp = 0;
|
|
52
|
+
switch(arguments.length){
|
|
53
|
+
case 0:
|
|
54
|
+
cat = "config";
|
|
55
|
+
type = "info";
|
|
56
|
+
setp = 1;
|
|
57
|
+
break;
|
|
58
|
+
case 1:
|
|
59
|
+
cat = "config";
|
|
60
|
+
type = "info";
|
|
61
|
+
setp = 0;
|
|
62
|
+
break;
|
|
63
|
+
case 2:
|
|
64
|
+
type = "info";
|
|
65
|
+
setp = 1;
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
setp = 2;
|
|
69
|
+
}
|
|
70
|
+
logArr = [].slice.call(arguments, setp);
|
|
71
|
+
if (IS_MASTER) {
|
|
72
|
+
if (!LOGERS[cat]) {
|
|
73
|
+
LOGERS[cat] = log.getLogger(cat);
|
|
74
|
+
}
|
|
75
|
+
LOGERS[cat][type].apply(LOGERS[cat], logArr);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 启动时用于输出启动信息的方法
|
|
80
|
+
*/ function theLog(...rest) {
|
|
81
|
+
if (IS_MASTER) {
|
|
82
|
+
logger.info.apply(logger, rest);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const { env , envAddConfPath , isProd } = (0, _getEnv.default)(argv);
|
|
86
|
+
const conf = (0, _processDefConfigFile.default)(argv);
|
|
87
|
+
theLog(`A [ ${env} ] server starting.`);
|
|
88
|
+
(0, _processCustomConfigFiles.default)(conf, env, envAddConfPath);
|
|
89
|
+
/**
|
|
90
|
+
* 默认配置
|
|
91
|
+
*/ const CONFIG = {
|
|
92
|
+
"name": meta.name,
|
|
93
|
+
"version": meta.version,
|
|
94
|
+
"env": env,
|
|
95
|
+
"debug": !isProd,
|
|
96
|
+
"port": 5000,
|
|
97
|
+
"ip": argv.IP || null,
|
|
98
|
+
"isProd": isProd,
|
|
99
|
+
"workers": 3,
|
|
100
|
+
"allowCache": true,
|
|
101
|
+
"root": _path.default.resolve(process.cwd(), "public"),
|
|
102
|
+
"isMaster": IS_MASTER
|
|
103
|
+
};
|
|
104
|
+
try {
|
|
105
|
+
(0, _utils.extend)(CONFIG, conf);
|
|
106
|
+
if (argv.PORT) {
|
|
107
|
+
const port = Number(argv.PORT);
|
|
108
|
+
if (!isNaN(port)) {
|
|
109
|
+
CONFIG.port = port;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} catch (e) {
|
|
113
|
+
logger.error("ASSIGN CONFIG ERROR.\n", e);
|
|
114
|
+
}
|
|
115
|
+
const NOT_SHOW = [
|
|
116
|
+
"root",
|
|
117
|
+
"sessionStore",
|
|
118
|
+
"mongodb"
|
|
119
|
+
];
|
|
120
|
+
Object.keys(CONFIG).forEach(function(key) {
|
|
121
|
+
var val = JSON.stringify(CONFIG[key]);
|
|
122
|
+
if (NOT_SHOW.indexOf(key) !== -1 && isProd) {
|
|
123
|
+
val = "*".repeat(20);
|
|
124
|
+
}
|
|
125
|
+
theLog(`${key} : ${val}`);
|
|
126
|
+
});
|
|
127
|
+
console.log("");
|
|
128
|
+
const _default = CONFIG;
|
|
129
|
+
function get() {
|
|
130
|
+
return CONFIG;
|
|
131
|
+
}
|
|
132
|
+
/**更新配置 */ function update(conf) {
|
|
133
|
+
if ((0, _utils.isObject)(conf)) {
|
|
134
|
+
(0, _utils.extend)(CONFIG, conf);
|
|
135
|
+
}
|
|
136
|
+
return CONFIG;
|
|
137
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>_default
|
|
8
|
+
});
|
|
9
|
+
const _common = require("../components/common");
|
|
10
|
+
const _utils = require("@x-drive/utils");
|
|
11
|
+
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
12
|
+
const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
|
|
13
|
+
function _interopRequireDefault(obj) {
|
|
14
|
+
return obj && obj.__esModule ? obj : {
|
|
15
|
+
default: obj
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function customConfigProcessor(conf, env, envAddConfPath) {
|
|
19
|
+
// 业务自定义服务端业务文件夹
|
|
20
|
+
const customDir = _path.default.resolve(process.cwd(), "@server");
|
|
21
|
+
// 是否存在业务自定义服务端业务文件夹
|
|
22
|
+
const hasCustom = (0, _common.checkFileStat)(customDir);
|
|
23
|
+
if (hasCustom) {
|
|
24
|
+
/**业务生产环境配置文件地址 */ let customConfPath = _path.default.resolve(customDir, "@config", "config.js");
|
|
25
|
+
/**是否有业务自定义配置 */ let hasCustomConf = (0, _common.checkFileStat)(customConfPath);
|
|
26
|
+
if (hasCustomConf) {
|
|
27
|
+
(0, _utils.merge)(conf, (0, _common.getRealDefaultMod)(require(customConfPath)));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (envAddConfPath) {
|
|
31
|
+
const devConf = (0, _common.getRealDefaultMod)(require(_path.default.join("..", envAddConfPath)));
|
|
32
|
+
let extConf = {};
|
|
33
|
+
let customDevConf = {};
|
|
34
|
+
if (hasCustom) {
|
|
35
|
+
/**对应环境配置文件地址 */ let customConfPath1 = _path.default.resolve(customDir, `${envAddConfPath}.js`);
|
|
36
|
+
if ((0, _common.checkFileStat)(customConfPath1)) {
|
|
37
|
+
customDevConf = (0, _common.getRealDefaultMod)(require(customConfPath1));
|
|
38
|
+
}
|
|
39
|
+
if (env === "DEVELOPMENT") {
|
|
40
|
+
let localConfPath = _path.default.resolve(customDir, "@config", "config.lo.js");
|
|
41
|
+
if ((0, _common.checkFileStat)(localConfPath)) {
|
|
42
|
+
extConf = (0, _common.getRealDefaultMod)(require(localConfPath));
|
|
43
|
+
extConf.hasLo = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// 合并
|
|
48
|
+
(0, _utils.merge)(conf, devConf, customDevConf, extConf);
|
|
49
|
+
}
|
|
50
|
+
// apis文件夹配置处理
|
|
51
|
+
if (hasCustom) {
|
|
52
|
+
const APIS_PATH = _path.default.resolve(process.cwd(), "@server", "@config", "@apis");
|
|
53
|
+
if ((0, _common.checkFileStat)(APIS_PATH) && conf.passExtApis !== true) {
|
|
54
|
+
let apis = [];
|
|
55
|
+
_fs.default.readdirSync(APIS_PATH).forEach((file)=>{
|
|
56
|
+
if (_path.default.extname(file) === ".js") {
|
|
57
|
+
apis.push((0, _common.getRealDefaultMod)(require(`${APIS_PATH}/${file}`)));
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
if (apis.length) {
|
|
61
|
+
conf.apis = Object.assign(conf.apis || {}, ...apis);
|
|
62
|
+
}
|
|
63
|
+
apis = null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const _default = customConfigProcessor;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>_default
|
|
8
|
+
});
|
|
9
|
+
const _common = require("../components/common");
|
|
10
|
+
const _config = /*#__PURE__*/ _interopRequireDefault(require("../@config/config"));
|
|
11
|
+
function _interopRequireDefault(obj) {
|
|
12
|
+
return obj && obj.__esModule ? obj : {
|
|
13
|
+
default: obj
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function defConfigProcessor(argv) {
|
|
17
|
+
if (argv.IP) {
|
|
18
|
+
// 绑定 IP
|
|
19
|
+
(_config.default).ip = argv.IP;
|
|
20
|
+
}
|
|
21
|
+
// api 配置文件处理
|
|
22
|
+
var hasCustomApiConf = (0, _common.checkFileStat)("../@config/@apis/index.js", true);
|
|
23
|
+
if (hasCustomApiConf) {
|
|
24
|
+
_config.default.apis = Object.assign(_config.default.apis || {}, (0, _common.getRealDefaultMod)(require("../conf/@conf/index.js")));
|
|
25
|
+
}
|
|
26
|
+
hasCustomApiConf = null;
|
|
27
|
+
return _config.default;
|
|
28
|
+
}
|
|
29
|
+
const _default = defConfigProcessor;
|
package/dist/custom.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>_default
|
|
8
|
+
});
|
|
9
|
+
const _utils = require("@x-drive/utils");
|
|
10
|
+
const _common = require("./components/common");
|
|
11
|
+
const _config = require("./config");
|
|
12
|
+
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
13
|
+
function _interopRequireDefault(obj) {
|
|
14
|
+
return obj && obj.__esModule ? obj : {
|
|
15
|
+
default: obj
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**处理自定义业务 */ function processCustom() {
|
|
19
|
+
const config = (0, _config.get)();
|
|
20
|
+
const CustomPath = _path.default.resolve(process.cwd(), "@server", "custom");
|
|
21
|
+
const hasCustom = (0, _common.checkFileStat)(CustomPath);
|
|
22
|
+
if (!hasCustom) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!(0, _utils.isArray)(config.custom) || !config.custom.length) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (hasCustom) {
|
|
29
|
+
config.custom.forEach(async (item)=>{
|
|
30
|
+
const isStrItem = (0, _utils.isString)(item);
|
|
31
|
+
const name = isStrItem ? item : item[0];
|
|
32
|
+
const conf = isStrItem ? null : item[1];
|
|
33
|
+
const mod = (0, _common.getRealDefaultMod)(require(_path.default.resolve(CustomPath, name)));
|
|
34
|
+
if ((0, _utils.isFunction)(mod)) {
|
|
35
|
+
mod(conf);
|
|
36
|
+
} else if ((0, _utils.isAsyncFunction)(mod)) {
|
|
37
|
+
await mod(conf);
|
|
38
|
+
}
|
|
39
|
+
masterLog("custom", `[${name}] loaded`);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
console.log("");
|
|
43
|
+
}
|
|
44
|
+
const _default = processCustom;
|
package/dist/global.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
const _config = require("./config");
|
|
6
|
+
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
7
|
+
function _interopRequireDefault(obj) {
|
|
8
|
+
return obj && obj.__esModule ? obj : {
|
|
9
|
+
default: obj
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 获取 components 模块路径
|
|
14
|
+
* @param name 模块名称
|
|
15
|
+
* @param root 模块存储目录所属路径
|
|
16
|
+
* @return 完整的模块目录地址
|
|
17
|
+
*/ function getModulePath(name, root) {
|
|
18
|
+
return _path.default.resolve(root || __dirname, "components", name);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 获取 components 模块
|
|
22
|
+
* @param name 模块名
|
|
23
|
+
* @return 模块对象
|
|
24
|
+
*/ function requireMod(name) {
|
|
25
|
+
return require(getModulePath(name));
|
|
26
|
+
}
|
|
27
|
+
global.requireMod = requireMod;
|
|
28
|
+
/**
|
|
29
|
+
* model 存放路径
|
|
30
|
+
*/ const MODEL_PATH = _path.default.resolve(process.cwd(), "@server", "business", "@models");
|
|
31
|
+
/**
|
|
32
|
+
* 全局获取 model 的方法
|
|
33
|
+
* @param name model 名称
|
|
34
|
+
* @return model 模块
|
|
35
|
+
*/ function requireModel(name) {
|
|
36
|
+
return require(_path.default.resolve(MODEL_PATH, name));
|
|
37
|
+
}
|
|
38
|
+
global.requireModel = requireModel;
|
|
39
|
+
/**
|
|
40
|
+
* services 存放目录
|
|
41
|
+
*/ const SERVICES_PATH = _path.default.resolve(process.cwd(), "@server", "business", "@services");
|
|
42
|
+
/**
|
|
43
|
+
* 全局获取 service 的方法
|
|
44
|
+
* @param name service 名称
|
|
45
|
+
* @return service 模块
|
|
46
|
+
*/ function requireService(name) {
|
|
47
|
+
return require(_path.default.resolve(SERVICES_PATH, name));
|
|
48
|
+
}
|
|
49
|
+
global.requireService = requireService;
|
|
50
|
+
/**
|
|
51
|
+
* 获取系统配置
|
|
52
|
+
* @param key 配置项
|
|
53
|
+
*/ function getSysConfig(key) {
|
|
54
|
+
const conf = (0, _config.get)();
|
|
55
|
+
if (key) {
|
|
56
|
+
return conf[key];
|
|
57
|
+
}
|
|
58
|
+
return conf;
|
|
59
|
+
}
|
|
60
|
+
global.getSysConfig = getSysConfig;
|
|
61
|
+
/**
|
|
62
|
+
* 更新系统配置
|
|
63
|
+
* @param conf 配置项
|
|
64
|
+
*/ function setSysConfig(conf) {
|
|
65
|
+
(0, _config.update)(conf);
|
|
66
|
+
}
|
|
67
|
+
global.setSysConfig = setSysConfig;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
filter: ()=>filter,
|
|
13
|
+
default: ()=>_default
|
|
14
|
+
});
|
|
15
|
+
const _utils = require("@x-drive/utils");
|
|
16
|
+
var conf;
|
|
17
|
+
const VER_RE = /\d+\.\d+\.\d+/;
|
|
18
|
+
var routeMobile;
|
|
19
|
+
// const MAX_COLLECT = global.Valkyr && Valkyr.getMaxCollect() || 0;
|
|
20
|
+
const MAX_COLLECT = 0;
|
|
21
|
+
const PRE_MIN_NUM = Math.ceil(MAX_COLLECT / 24);
|
|
22
|
+
const PRE_MAX_NUM = Math.ceil(MAX_COLLECT / 6);
|
|
23
|
+
const COLLECT_STATUS = {};
|
|
24
|
+
var nowDay = new Date().getDate();
|
|
25
|
+
/**
|
|
26
|
+
* 数组随机分布
|
|
27
|
+
* @param arr 原始数组
|
|
28
|
+
* @return 打乱后的数组
|
|
29
|
+
*/ function shuffle(arr) {
|
|
30
|
+
for(var i = arr.length - 1; i >= 0; i--){
|
|
31
|
+
let rIndex = Math.floor(Math.random() * (i + 1));
|
|
32
|
+
let item = arr[rIndex];
|
|
33
|
+
arr[rIndex] = arr[i];
|
|
34
|
+
arr[i] = item;
|
|
35
|
+
}
|
|
36
|
+
return arr;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 获取每个钟头里面随机要处理的分钟
|
|
40
|
+
* @param status 状态
|
|
41
|
+
*/ function getM4H(status) {
|
|
42
|
+
var m4h = [];
|
|
43
|
+
for(let i = 1; i <= 60; i++){
|
|
44
|
+
m4h.push(i);
|
|
45
|
+
}
|
|
46
|
+
shuffle(m4h);
|
|
47
|
+
var logLen = 0;
|
|
48
|
+
for(let j = 1; j <= 24; j++){
|
|
49
|
+
let sIndex = (0, _utils.random)(59, 0);
|
|
50
|
+
let sLen = Math.min((0, _utils.random)(PRE_MAX_NUM, PRE_MIN_NUM), 60 - sIndex);
|
|
51
|
+
if (sLen + logLen > MAX_COLLECT) {
|
|
52
|
+
sLen = MAX_COLLECT - logLen;
|
|
53
|
+
}
|
|
54
|
+
let part = m4h.concat([]).splice(sIndex, sLen);
|
|
55
|
+
part.forEach(function(m) {
|
|
56
|
+
COLLECT_STATUS[j][m] = status;
|
|
57
|
+
});
|
|
58
|
+
part = null;
|
|
59
|
+
logLen += sLen;
|
|
60
|
+
if (logLen >= MAX_COLLECT) {
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
m4h = null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 设置处理状态
|
|
68
|
+
* @param len 处理的数量
|
|
69
|
+
* @param status 处理状态
|
|
70
|
+
*/ function setCollectStatus(len, status) {
|
|
71
|
+
if (!MAX_COLLECT) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
while(len > 0){
|
|
75
|
+
COLLECT_STATUS[len--] = {};
|
|
76
|
+
}
|
|
77
|
+
getM4H(status);
|
|
78
|
+
}
|
|
79
|
+
setCollectStatus(24, false);
|
|
80
|
+
// HTML 过滤器
|
|
81
|
+
function filter(url) {
|
|
82
|
+
var ver = VER_RE.exec(url) || VER_RE.exec(routeMobile);
|
|
83
|
+
var data = {};
|
|
84
|
+
// 从路径提取版本号
|
|
85
|
+
if (ver && ver.length > 0) {
|
|
86
|
+
data.appVer = ver[0] || "";
|
|
87
|
+
}
|
|
88
|
+
// 放回请求数据中
|
|
89
|
+
return data;
|
|
90
|
+
}
|
|
91
|
+
function _default() {
|
|
92
|
+
conf = getSysConfig();
|
|
93
|
+
routeMobile = conf.routeMobile;
|
|
94
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>_default
|
|
8
|
+
});
|
|
9
|
+
const _common = require("../components/common");
|
|
10
|
+
const _assets = require("../components/assets");
|
|
11
|
+
const s5xx = (0, _assets.get)("5xx");
|
|
12
|
+
const code_map = {
|
|
13
|
+
"404": (0, _assets.get)("404"),
|
|
14
|
+
"503": s5xx,
|
|
15
|
+
"504": s5xx
|
|
16
|
+
};
|
|
17
|
+
var page_tpl = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><style>body{margin:0;padding:0;color:#444;background:#f0f0f0;font:14px/1.4em Arial "Hiragino Sans GB", "Microsoft YaHei",tahoma,arial,sans-serif normal;}</style></head><body>{dat}</body></html>';
|
|
18
|
+
function handler(ctx) {
|
|
19
|
+
var re = code_map[ctx.status] || "";
|
|
20
|
+
var dat;
|
|
21
|
+
if (ctx.url.indexOf("api/") === -1) {
|
|
22
|
+
switch(ctx.status){
|
|
23
|
+
case 404:
|
|
24
|
+
case 503:
|
|
25
|
+
case 504:
|
|
26
|
+
dat = re;
|
|
27
|
+
break;
|
|
28
|
+
default:
|
|
29
|
+
dat = page_tpl.replace("{dat}", re);
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
dat = (0, _common.responseResult)(false, re);
|
|
33
|
+
}
|
|
34
|
+
ctx.body = dat;
|
|
35
|
+
}
|
|
36
|
+
const _default = handler;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>_default
|
|
8
|
+
});
|
|
9
|
+
const _combo = require("../components/combo");
|
|
10
|
+
function handler() {
|
|
11
|
+
return (0, _combo.init)();
|
|
12
|
+
}
|
|
13
|
+
const _default = handler;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>_default
|
|
8
|
+
});
|
|
9
|
+
const _koaCompress = /*#__PURE__*/ _interopRequireDefault(require("koa-compress"));
|
|
10
|
+
const _zlib = /*#__PURE__*/ _interopRequireDefault(require("zlib"));
|
|
11
|
+
function _interopRequireDefault(obj) {
|
|
12
|
+
return obj && obj.__esModule ? obj : {
|
|
13
|
+
default: obj
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function handler(config) {
|
|
17
|
+
const gzipOpts = {
|
|
18
|
+
"flush": _zlib.default.Z_SYNC_FLUSH
|
|
19
|
+
};
|
|
20
|
+
if (config) {
|
|
21
|
+
if (config.threshold) {
|
|
22
|
+
gzipOpts.threshold = config.threshold;
|
|
23
|
+
}
|
|
24
|
+
if (config.filter) {
|
|
25
|
+
let filter = new RegExp(config.filter, "i");
|
|
26
|
+
gzipOpts.filter = function(type) {
|
|
27
|
+
return filter.test(type);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
if (config.flush && _zlib.default[config.gzip.flush]) {
|
|
31
|
+
gzipOpts.flush = _zlib.default[config.gzip.flush];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return (0, _koaCompress.default)(gzipOpts);
|
|
35
|
+
}
|
|
36
|
+
const _default = handler;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, /**跨域请求处理中间件初始化函数 */ "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>_default
|
|
8
|
+
});
|
|
9
|
+
const _utils = require("@x-drive/utils");
|
|
10
|
+
/**
|
|
11
|
+
一些本地的域名或 ip
|
|
12
|
+
|
|
13
|
+
`这里只是为开发阶段提供便捷,不应做为常规的控制手段`
|
|
14
|
+
*/ const IS_LOCAL = {
|
|
15
|
+
"localhost": true,
|
|
16
|
+
"127.0.0.1": true
|
|
17
|
+
};
|
|
18
|
+
/**根域获取正则 */ const ROOT_NAME_REGEXP = /[\w-]*\.(?:com|cc|cn|asia|biz|coop|edu|gov|int|info|jobs|mobi|name|net|org|pro|tel|trave)(\.[a-z]{2})?/im;
|
|
19
|
+
/**获取域名根域 */ function getRootName(host) {
|
|
20
|
+
const match = host.match(ROOT_NAME_REGEXP);
|
|
21
|
+
if (match && (0, _utils.isString)(match[0])) {
|
|
22
|
+
return match[0];
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
/**系统绑定的域名对应的根域 */ const ServerHostRootName = getRootName(getSysConfig("host"));
|
|
27
|
+
/**判断是否允许跨域 */ function isOneOfUs(origin, url, config = {}) {
|
|
28
|
+
if (config && config.enable === false) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
if ((0, _utils.isFunction)(config.check)) {
|
|
32
|
+
return config.check(origin, url);
|
|
33
|
+
} else if ((0, _utils.isString)(config.type)) {
|
|
34
|
+
if (config.type === "all") {
|
|
35
|
+
return true;
|
|
36
|
+
} else if (config.type === "sameroot") {
|
|
37
|
+
if (ServerHostRootName && getRootName(origin) === ServerHostRootName) {
|
|
38
|
+
return true;
|
|
39
|
+
} else {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
function _default(config) {
|
|
47
|
+
/**跨域请求处理中间件 */ async function cors(ctx, next) {
|
|
48
|
+
const { origin } = ctx.headers;
|
|
49
|
+
if (!origin) {
|
|
50
|
+
await next();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const originHostURL = new URL(origin);
|
|
54
|
+
const isLocal = IS_LOCAL[originHostURL.hostname];
|
|
55
|
+
if (isLocal || isOneOfUs(origin, ctx.url, config)) {
|
|
56
|
+
ctx.set("Access-Control-Allow-Origin", origin);
|
|
57
|
+
ctx.set("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
|
|
58
|
+
ctx.set("Access-Control-Allow-Credentials", "true");
|
|
59
|
+
ctx.set("X-Cors", isLocal ? "local" : config.type || "custom");
|
|
60
|
+
} else {
|
|
61
|
+
ctx.set("X-Cors", "disabled");
|
|
62
|
+
}
|
|
63
|
+
await next();
|
|
64
|
+
}
|
|
65
|
+
return cors;
|
|
66
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>_default
|
|
8
|
+
});
|
|
9
|
+
const _header = require("../components/header");
|
|
10
|
+
const conf = getSysConfig();
|
|
11
|
+
async function handle(ctx, next) {
|
|
12
|
+
// 头部字段
|
|
13
|
+
var pageUrl = ctx.state.parsedUrl && ctx.state.parsedUrl.pathname || ctx.url;
|
|
14
|
+
var queryMap = ctx.query;
|
|
15
|
+
var modifiedSince = ctx.headers["if-modified-since"];
|
|
16
|
+
var modifiedSinceTime;
|
|
17
|
+
// var noneMatch = ctx.headers["if-none-match"];
|
|
18
|
+
// var lastModified = ctx.headers["last-modified"];
|
|
19
|
+
// var etag = ctx.headers["etag"];
|
|
20
|
+
// var cc = ctx.headers["cache-control"];
|
|
21
|
+
var nowTime = new Date().getTime();
|
|
22
|
+
var nowDateString = new Date().toUTCString();
|
|
23
|
+
var isIndexPage = /\/$/.test(pageUrl);
|
|
24
|
+
isIndexPage = isIndexPage || /[\/]?index\.html/.test(pageUrl);
|
|
25
|
+
// 没有最后修改时间,附加上当前时间并继续
|
|
26
|
+
// 或者有nocache参数的话,也直接响应 200 操作
|
|
27
|
+
if (!conf["304"] || !!queryMap.no_cache || !modifiedSince) {
|
|
28
|
+
(0, _header.lastModified)(nowDateString, ctx);
|
|
29
|
+
await next();
|
|
30
|
+
// 发现有修改时间,直接返回304头
|
|
31
|
+
} else {
|
|
32
|
+
modifiedSinceTime = new Date(modifiedSince).getTime();
|
|
33
|
+
// 如果是首页,则进行半小时的缓存时间(时间可配置)
|
|
34
|
+
if (isIndexPage) {
|
|
35
|
+
// 如果没有超过半小时的缓存时间,则直接返回304,否则返回200,并重新设置last-modify
|
|
36
|
+
if (nowTime - modifiedSinceTime < conf.indexPageCacheTime) {
|
|
37
|
+
ctx.set("X-304-Filter", "HIT");
|
|
38
|
+
ctx.status = 304;
|
|
39
|
+
ctx.body = "";
|
|
40
|
+
} else {
|
|
41
|
+
(0, _header.lastModified)(nowDateString, ctx);
|
|
42
|
+
next();
|
|
43
|
+
}
|
|
44
|
+
// 其他的js/css/image静态资源设置两周的缓存时间(可配置)
|
|
45
|
+
} else if (ctx.state.srcType.js || ctx.state.srcType.css || ctx.state.srcType.img || ctx.state.srcType.tpl || ctx.state.srcType.font) {
|
|
46
|
+
if (nowTime - modifiedSinceTime < conf.staticResourceCacheTime) {
|
|
47
|
+
ctx.set("X-304-Filter", "HIT");
|
|
48
|
+
ctx.status = 304;
|
|
49
|
+
ctx.body = "";
|
|
50
|
+
} else {
|
|
51
|
+
(0, _header.lastModified)(nowDateString, ctx);
|
|
52
|
+
await next();
|
|
53
|
+
}
|
|
54
|
+
//其他类型的请求,比如Server API等等
|
|
55
|
+
} else {
|
|
56
|
+
(0, _header.lastModified)(nowDateString, ctx);
|
|
57
|
+
await next();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function _default() {
|
|
62
|
+
return handle;
|
|
63
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>_default
|
|
8
|
+
});
|
|
9
|
+
var conf = getSysConfig();
|
|
10
|
+
/**
|
|
11
|
+
* 路径处理匹配正则
|
|
12
|
+
*/ const replace_reg = new RegExp(conf.pathReplaceRegExp);
|
|
13
|
+
/**
|
|
14
|
+
* 处理代理过来多余的地址层级
|
|
15
|
+
*/ async function handlPreDir(ctx, next) {
|
|
16
|
+
if (replace_reg.test(ctx.url)) {
|
|
17
|
+
ctx.set("X-Pre-Dir", "Hit");
|
|
18
|
+
ctx.url = ctx.url.replace(replace_reg, "");
|
|
19
|
+
if (ctx.url.charAt(0) !== "/") {
|
|
20
|
+
ctx.url = "/" + ctx.url;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
await next();
|
|
24
|
+
}
|
|
25
|
+
const _default = handlPreDir;
|