@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,117 @@
|
|
|
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
|
+
on: ()=>on,
|
|
13
|
+
kill: ()=>kill
|
|
14
|
+
});
|
|
15
|
+
const _utils = require("@x-drive/utils");
|
|
16
|
+
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
17
|
+
const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
|
|
18
|
+
function _interopRequireDefault(obj) {
|
|
19
|
+
return obj && obj.__esModule ? obj : {
|
|
20
|
+
default: obj
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const logger = log.getLogger("cron");
|
|
24
|
+
const config = getSysConfig("cron") || {
|
|
25
|
+
"def": 60
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* 默认间隔时间
|
|
29
|
+
*/ const DEF_TIME = config.def * 1000;
|
|
30
|
+
/**
|
|
31
|
+
* 计时器对象
|
|
32
|
+
*/ var CRON_TIMERS = {};
|
|
33
|
+
/**
|
|
34
|
+
* 定时刷新函数
|
|
35
|
+
* @param mod 模块对象
|
|
36
|
+
* @param name 计时器名称
|
|
37
|
+
* @param dont 不执行
|
|
38
|
+
* @return 计时器对象
|
|
39
|
+
*/ function fetch(mod, name, dont) {
|
|
40
|
+
var time = (0, _utils.isFunction)(mod.getDelay) ? mod.getDelay() : DEF_TIME;
|
|
41
|
+
if (isNaN(time)) {
|
|
42
|
+
logger.warn("[ %s ] Time Invalid.", name);
|
|
43
|
+
time = DEF_TIME;
|
|
44
|
+
}
|
|
45
|
+
var enable = (0, _utils.isFunction)(mod.enable) ? mod.enable() === true : true;
|
|
46
|
+
if (!enable) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
if (!dont) {
|
|
50
|
+
mod();
|
|
51
|
+
}
|
|
52
|
+
return setTimeout(function() {
|
|
53
|
+
CRON_TIMERS[name] = fetch(mod, name, dont);
|
|
54
|
+
}, time);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 停止一个计时器
|
|
58
|
+
* @param name 计时器名称
|
|
59
|
+
* @return 无返回值
|
|
60
|
+
*/ function killSomeone(name) {
|
|
61
|
+
if (CRON_TIMERS[name]) {
|
|
62
|
+
console.log("Stop crontab job ", name);
|
|
63
|
+
clearTimeout(CRON_TIMERS[name]);
|
|
64
|
+
CRON_TIMERS[name] = null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 启动指定目录下的所有计时任务
|
|
69
|
+
* @param dirPath 任务目录
|
|
70
|
+
* @return 无返回值
|
|
71
|
+
*/ function on(dirPath) {
|
|
72
|
+
var cronPath = _path.default.resolve(dirPath || "./cron");
|
|
73
|
+
var dirStat;
|
|
74
|
+
try {
|
|
75
|
+
dirStat = _fs.default.accessSync(cronPath, _fs.default.constants.F_OK);
|
|
76
|
+
} catch (e) {
|
|
77
|
+
dirStat = true;
|
|
78
|
+
logger.warn(e);
|
|
79
|
+
}
|
|
80
|
+
// accessSync 在文件不存在的时候才会返回内容
|
|
81
|
+
if (dirStat) {
|
|
82
|
+
logger.info("No cron job.");
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
var crons = _fs.default.readdirSync(cronPath);
|
|
86
|
+
logger.info("Cron online.");
|
|
87
|
+
try {
|
|
88
|
+
crons.forEach(function(cron) {
|
|
89
|
+
if (cron.charAt(0) !== ".") {
|
|
90
|
+
var tmpPath = cronPath + "/" + cron;
|
|
91
|
+
var stats = _fs.default.statSync(tmpPath);
|
|
92
|
+
if (stats.isFile()) {
|
|
93
|
+
var name = cron.replace(".js", "");
|
|
94
|
+
CRON_TIMERS[name] = fetch(require(_path.default.resolve(tmpPath)), name);
|
|
95
|
+
logger.info("\t>>> [ %s ] %s.", name, CRON_TIMERS[name] ? "running" : "disabled");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
} catch (err) {
|
|
100
|
+
logger.error(err);
|
|
101
|
+
}
|
|
102
|
+
console.log("");
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* 停止一个或所有的计时器
|
|
106
|
+
* @param name 计时器名称
|
|
107
|
+
*/ function kill(name) {
|
|
108
|
+
if (name && CRON_TIMERS[name]) {
|
|
109
|
+
killSomeone(name);
|
|
110
|
+
} else {
|
|
111
|
+
Object.keys(CRON_TIMERS).forEach(function(key) {
|
|
112
|
+
if (CRON_TIMERS[key]) {
|
|
113
|
+
killSomeone(key);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
cacheControl: ()=>cacheControl,
|
|
13
|
+
expire: ()=>expire,
|
|
14
|
+
contentType: ()=>contentType,
|
|
15
|
+
lastModified: ()=>lastModified
|
|
16
|
+
});
|
|
17
|
+
const _utils = require("@x-drive/utils");
|
|
18
|
+
const _time = require("./time");
|
|
19
|
+
const _mime = require("../mime");
|
|
20
|
+
/**
|
|
21
|
+
* 设置 Cache-Control
|
|
22
|
+
* @param options max-age 缓存时间
|
|
23
|
+
* @param res 请求上下文
|
|
24
|
+
*/ function cacheControl(options, res) {
|
|
25
|
+
var age;
|
|
26
|
+
if ((0, _utils.isString)(options)) {
|
|
27
|
+
if (options.indexOf("max-age") === -1) {
|
|
28
|
+
age = options;
|
|
29
|
+
} else {
|
|
30
|
+
age = "max-age=" + (0, _time.string2seconds)(options);
|
|
31
|
+
}
|
|
32
|
+
} else if ((0, _utils.isNumber)(options)) {
|
|
33
|
+
age = "max-age=" + options;
|
|
34
|
+
}
|
|
35
|
+
if (age) {
|
|
36
|
+
res.set("Cache-Control", age);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 设置 Expires
|
|
41
|
+
* @param options Expires 缓存时间
|
|
42
|
+
* @param res 请求上下文
|
|
43
|
+
*/ function expire(options, res) {
|
|
44
|
+
var exp;
|
|
45
|
+
if ((0, _utils.isString)(options)) {
|
|
46
|
+
exp = new Date(Date.now() + (0, _time.string2seconds)(options) * 1000);
|
|
47
|
+
} else if ((0, _utils.isNumber)(options)) {
|
|
48
|
+
exp = new Date(Date.now() + options * 1000);
|
|
49
|
+
}
|
|
50
|
+
if (exp) {
|
|
51
|
+
res.set("Expires", exp.toUTCString());
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 设置 Content-Type
|
|
56
|
+
* @param options 类型
|
|
57
|
+
* @param res 请求上下文
|
|
58
|
+
*/ function contentType(options, res) {
|
|
59
|
+
var type;
|
|
60
|
+
if ((0, _utils.isString)(options)) {
|
|
61
|
+
type = _mime.MIME[options];
|
|
62
|
+
}
|
|
63
|
+
if (type) {
|
|
64
|
+
res.set("Content-Type", `${type}; charset=utf-8`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 设置 Last-Modified
|
|
69
|
+
* @param options 最后更新时间,传入非 String 类型时会使用当前时间
|
|
70
|
+
* @param res 请求上下文
|
|
71
|
+
*/ function lastModified(options, res) {
|
|
72
|
+
var time = (0, _utils.isString)(options) ? options : new Date().toUTCString();
|
|
73
|
+
res.set('Last-Modified', time);
|
|
74
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "string2seconds", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>string2seconds
|
|
8
|
+
});
|
|
9
|
+
/**一年的秒数 */ const YEAR_SEC = 365 * 24 * 60 * 60;
|
|
10
|
+
/**一月的秒数 */ const MONTH_SEC = 30 * 24 * 60 * 60;
|
|
11
|
+
/**一天的秒数 */ const DAY_SEC = 24 * 60 * 60;
|
|
12
|
+
/**一小时的秒数 */ const HOUR_SEC = 60 * 60;
|
|
13
|
+
/**一分钟的秒数 */ const MINUTE_SEC = 60;
|
|
14
|
+
/**字符串转为秒数 */ function string2seconds(string) {
|
|
15
|
+
const rBlock = /(\d{1,4})([YMdhms])/;
|
|
16
|
+
var r;
|
|
17
|
+
var sec = 0;
|
|
18
|
+
while((r = rBlock.exec(string)) !== null){
|
|
19
|
+
let value = Number(r[1]);
|
|
20
|
+
let unit = r[2];
|
|
21
|
+
if (unit === "Y") {
|
|
22
|
+
sec += YEAR_SEC * value;
|
|
23
|
+
} else if (unit === "M") {
|
|
24
|
+
sec += MONTH_SEC * value;
|
|
25
|
+
} else if (unit === "d") {
|
|
26
|
+
sec += DAY_SEC * value;
|
|
27
|
+
} else if (unit === "h") {
|
|
28
|
+
sec += HOUR_SEC * value;
|
|
29
|
+
} else if (unit === "m") {
|
|
30
|
+
sec += MINUTE_SEC * value;
|
|
31
|
+
} else {
|
|
32
|
+
sec += value;
|
|
33
|
+
}
|
|
34
|
+
string = string.replace(r[0], "");
|
|
35
|
+
}
|
|
36
|
+
return sec;
|
|
37
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
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
|
+
init: ()=>init,
|
|
13
|
+
htmlProcessor: ()=>htmlProcessor
|
|
14
|
+
});
|
|
15
|
+
const _utils = require("@x-drive/utils");
|
|
16
|
+
const _cache = require("../cache");
|
|
17
|
+
const _injection = require("../injection");
|
|
18
|
+
const _md5 = require("../md5");
|
|
19
|
+
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
20
|
+
const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
|
|
21
|
+
function _interopRequireDefault(obj) {
|
|
22
|
+
return obj && obj.__esModule ? obj : {
|
|
23
|
+
default: obj
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
var conf;
|
|
27
|
+
var logger;
|
|
28
|
+
const html_cache = (0, _cache.local)("HTML_CACHE", {
|
|
29
|
+
"maxAge": 1000 * 60 * 60,
|
|
30
|
+
"max": 10
|
|
31
|
+
});
|
|
32
|
+
function processorVar(html, req) {
|
|
33
|
+
return (0, _injection.inject)(html, req);
|
|
34
|
+
}
|
|
35
|
+
// 处理 HTML
|
|
36
|
+
// 读取 HTML 文件,如果没有缓存则缓存
|
|
37
|
+
// 填充相关字段
|
|
38
|
+
// 根据配置可以设置为清除本地缓存
|
|
39
|
+
// 设置缓存头
|
|
40
|
+
// 根据配置设置相应头部
|
|
41
|
+
function htmlProcessor(pathname, options, context) {
|
|
42
|
+
var filename = _path.default.resolve(conf.root + pathname);
|
|
43
|
+
// ua = req.get('user-agent'),
|
|
44
|
+
var cacheKey;
|
|
45
|
+
var html;
|
|
46
|
+
var route;
|
|
47
|
+
// 发送访问统计
|
|
48
|
+
// serverTrack(req, 'normal');
|
|
49
|
+
// 取出参数
|
|
50
|
+
if (options) {
|
|
51
|
+
route = options.route;
|
|
52
|
+
}
|
|
53
|
+
// 生成缓存 key
|
|
54
|
+
cacheKey = (0, _md5.md5)(pathname);
|
|
55
|
+
try {
|
|
56
|
+
html = html_cache.get(cacheKey);
|
|
57
|
+
if (!html) {
|
|
58
|
+
html = _fs.default.readFileSync(filename, 'utf8');
|
|
59
|
+
html_cache.set(cacheKey, html);
|
|
60
|
+
}
|
|
61
|
+
} catch (e) {
|
|
62
|
+
logger.error('HTML Process Error', e);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
// 无法正确读取 HTML
|
|
66
|
+
// 交给后续的 404 处理
|
|
67
|
+
if (!html) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
// 进行参数处理
|
|
71
|
+
html = processorVar(html, context);
|
|
72
|
+
// 按照配置清空 localStorage
|
|
73
|
+
if (conf.clearLocalStorage || context && context.query.no_cache) {
|
|
74
|
+
html = html.replace('<head>', '<head><script>if(window.localStorage) {window.localStorage.clear()}<\/script>');
|
|
75
|
+
}
|
|
76
|
+
// 读取 route 的配置并设置对应的 headers 信息
|
|
77
|
+
var headers = route && route.headers;
|
|
78
|
+
if (headers && context) {
|
|
79
|
+
Object.keys(headers).forEach((key)=>{
|
|
80
|
+
let val;
|
|
81
|
+
if ((0, _utils.isFunction)(headers[key])) {
|
|
82
|
+
val = headers[key].call();
|
|
83
|
+
} else {
|
|
84
|
+
val = headers[key];
|
|
85
|
+
}
|
|
86
|
+
context.set(key, val);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
// 返回网页
|
|
90
|
+
return html;
|
|
91
|
+
}
|
|
92
|
+
function init() {
|
|
93
|
+
conf = getSysConfig();
|
|
94
|
+
logger = log.getLogger("html-processor");
|
|
95
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
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
|
+
inject: ()=>inject,
|
|
13
|
+
add: ()=>add,
|
|
14
|
+
modify: ()=>modify
|
|
15
|
+
});
|
|
16
|
+
const _utils = require("@x-drive/utils");
|
|
17
|
+
const conf = getSysConfig();
|
|
18
|
+
// 注入字段
|
|
19
|
+
const injectVars = conf.injection;
|
|
20
|
+
// 错误代码管理模块
|
|
21
|
+
const returnCode = requireMod("return-code").getCodeDetail();
|
|
22
|
+
/**
|
|
23
|
+
* 注入数据存储对象
|
|
24
|
+
*/ var INJECTIONS = {
|
|
25
|
+
"errCode": {},
|
|
26
|
+
"apis": conf.apis,
|
|
27
|
+
"biServer": conf.biServer ? conf.protocol + "://" + conf.biServer : "",
|
|
28
|
+
"navMap": {}
|
|
29
|
+
};
|
|
30
|
+
Object.keys(returnCode).forEach(function(key) {
|
|
31
|
+
let code = returnCode[key];
|
|
32
|
+
INJECTIONS.errCode[code.errorcode] = code.msg;
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* 数据注入方法
|
|
36
|
+
* @param html 待处理的字符串
|
|
37
|
+
* @param req 请求对象
|
|
38
|
+
* @return 处理完的字符串
|
|
39
|
+
*/ function inject(html, req) {
|
|
40
|
+
if ((0, _utils.isArray)(injectVars) && injectVars.length) {
|
|
41
|
+
let processorData = {};
|
|
42
|
+
injectVars.forEach((key)=>{
|
|
43
|
+
// 优先在模块內的缓存对象中找
|
|
44
|
+
let dat = INJECTIONS[key];
|
|
45
|
+
// 找不到再去配置中找
|
|
46
|
+
if ((0, _utils.isUndefined)(dat) || (0, _utils.isNull)(dat)) {
|
|
47
|
+
dat = conf[key];
|
|
48
|
+
}
|
|
49
|
+
// 如果找出来的是个函数,则尝试执行函数得到返回结果
|
|
50
|
+
if ((0, _utils.isFunction)(dat)) {
|
|
51
|
+
dat = dat(key, req);
|
|
52
|
+
}
|
|
53
|
+
if ((0, _utils.isObject)(dat) || Array.isArray(dat)) {
|
|
54
|
+
dat = JSON.stringify(dat);
|
|
55
|
+
}
|
|
56
|
+
processorData[key] = dat;
|
|
57
|
+
});
|
|
58
|
+
return (0, _utils.labelReplace)(html, processorData);
|
|
59
|
+
}
|
|
60
|
+
return html;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 添加一个字段到缓存对象
|
|
64
|
+
* @param key 字段名
|
|
65
|
+
* @param val 数据
|
|
66
|
+
* @return 存储的数据
|
|
67
|
+
*/ function add(key, val) {
|
|
68
|
+
INJECTIONS[key] = val;
|
|
69
|
+
injectVars.push(key);
|
|
70
|
+
return INJECTIONS[key];
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 修改一个字段
|
|
74
|
+
* @param key 字段名
|
|
75
|
+
* @param val 数据
|
|
76
|
+
* @return 存储的数据
|
|
77
|
+
*/ function modify(key, val) {
|
|
78
|
+
var re = INJECTIONS.hasOwnProperty(key);
|
|
79
|
+
if (re) {
|
|
80
|
+
INJECTIONS[key] = val;
|
|
81
|
+
}
|
|
82
|
+
return re;
|
|
83
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
init: ()=>init,
|
|
13
|
+
jsProcessor: ()=>jsProcessor
|
|
14
|
+
});
|
|
15
|
+
const _cache = require("../cache");
|
|
16
|
+
const _injection = require("../injection");
|
|
17
|
+
const _md5 = require("../md5");
|
|
18
|
+
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
19
|
+
const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
|
|
20
|
+
function _interopRequireDefault(obj) {
|
|
21
|
+
return obj && obj.__esModule ? obj : {
|
|
22
|
+
default: obj
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
var conf;
|
|
26
|
+
var logger;
|
|
27
|
+
var js_cache = (0, _cache.local)("JS_CACHE", {
|
|
28
|
+
"maxAge": 1000 * 60 * 60 * 24,
|
|
29
|
+
"max": 20
|
|
30
|
+
});
|
|
31
|
+
function processorVar(html, req) {
|
|
32
|
+
return (0, _injection.inject)(html, req);
|
|
33
|
+
}
|
|
34
|
+
// 处理 JS 文件
|
|
35
|
+
function jsProcessor(pathname, context) {
|
|
36
|
+
const filename = _path.default.resolve(conf.root + pathname);
|
|
37
|
+
var cacheKey;
|
|
38
|
+
var js;
|
|
39
|
+
// 生成缓存 key
|
|
40
|
+
cacheKey = (0, _md5.md5)(pathname);
|
|
41
|
+
// 检查是否有缓存
|
|
42
|
+
// 如果有缓存直接返回
|
|
43
|
+
try {
|
|
44
|
+
js = js_cache.get(cacheKey);
|
|
45
|
+
if (!js) {
|
|
46
|
+
js = _fs.default.readFileSync(filename, "utf8");
|
|
47
|
+
js_cache.set(cacheKey, js);
|
|
48
|
+
}
|
|
49
|
+
} catch (e) {
|
|
50
|
+
logger.error("JS Process Error", e);
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
// 无法正确读取 JS
|
|
54
|
+
// 交给后续的 404 处理
|
|
55
|
+
if (!js) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
// 进行参数处理
|
|
59
|
+
js = processorVar(js, context);
|
|
60
|
+
return js;
|
|
61
|
+
}
|
|
62
|
+
function init() {
|
|
63
|
+
conf = getSysConfig();
|
|
64
|
+
logger = log.getLogger("js-processor");
|
|
65
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
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: ()=>Log,
|
|
13
|
+
globalLog: ()=>globalLog
|
|
14
|
+
});
|
|
15
|
+
const _log4Js = /*#__PURE__*/ _interopRequireDefault(require("log4js"));
|
|
16
|
+
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
17
|
+
const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
|
|
18
|
+
function _interopRequireDefault(obj) {
|
|
19
|
+
return obj && obj.__esModule ? obj : {
|
|
20
|
+
default: obj
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 按照文件夹目录地址创建文件夹
|
|
25
|
+
* @param pathStr 文件夹目录地址
|
|
26
|
+
*/ function mkdir(pathStr) {
|
|
27
|
+
var level = pathStr.split(_path.default.sep);
|
|
28
|
+
if (level) {
|
|
29
|
+
let dirIndex = 1;
|
|
30
|
+
let last = level.length;
|
|
31
|
+
while(dirIndex <= last){
|
|
32
|
+
let dirPath = level.slice(0, dirIndex++).join(_path.default.sep);
|
|
33
|
+
let status;
|
|
34
|
+
try {
|
|
35
|
+
status = _fs.default.statSync(dirPath);
|
|
36
|
+
} catch (e) {
|
|
37
|
+
if (e.errno === -2) {
|
|
38
|
+
status = null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (status) {
|
|
42
|
+
if (!status.isDirectory()) {
|
|
43
|
+
console.error(`Target [${pathStr}] exists and is not a directory.`);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
} else if (status === null) {
|
|
47
|
+
_fs.default.mkdirSync(dirPath);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
level = null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// 日志类型对象
|
|
54
|
+
const LOGERS = {};
|
|
55
|
+
function Log(config) {
|
|
56
|
+
this.config = {
|
|
57
|
+
"name": "app",
|
|
58
|
+
"level": "warn",
|
|
59
|
+
"dir": "private/log",
|
|
60
|
+
"fileLog": false
|
|
61
|
+
};
|
|
62
|
+
if (Object.prototype.toString.call(config) === "[object Object]") {
|
|
63
|
+
this.config = Object.assign(this.config, config);
|
|
64
|
+
}
|
|
65
|
+
if (this.config.fileLog) {
|
|
66
|
+
let tmp = _path.default.join(this.config.dir);
|
|
67
|
+
mkdir(tmp);
|
|
68
|
+
tmp = null;
|
|
69
|
+
}
|
|
70
|
+
_log4Js.default.clearAppenders();
|
|
71
|
+
}
|
|
72
|
+
var LP = Log.prototype;
|
|
73
|
+
/**
|
|
74
|
+
* 获取统计实例
|
|
75
|
+
* @param {String} cat 实例分类名称
|
|
76
|
+
* @return {Object} 统计实例对象
|
|
77
|
+
*/ LP.getLogger = function(cat) {
|
|
78
|
+
var conf = this.config;
|
|
79
|
+
cat = "[" + cat + "]";
|
|
80
|
+
var logger = LOGERS[cat];
|
|
81
|
+
if (logger) {
|
|
82
|
+
return logger;
|
|
83
|
+
}
|
|
84
|
+
var dateFileConf = {
|
|
85
|
+
"filename": conf.name,
|
|
86
|
+
"pattern": ".yyyyMMddhh.log",
|
|
87
|
+
"alwaysIncludePattern": true
|
|
88
|
+
};
|
|
89
|
+
var stdoutConf = {};
|
|
90
|
+
if (conf.layout) {
|
|
91
|
+
dateFileConf.layout = conf.layout;
|
|
92
|
+
stdoutConf.layout = conf.layout;
|
|
93
|
+
}
|
|
94
|
+
if (this.config.fileLog) {
|
|
95
|
+
_log4Js.default.loadAppender("dateFile");
|
|
96
|
+
_log4Js.default.addAppender(_log4Js.default.appenderMakers.dateFile(dateFileConf, {
|
|
97
|
+
"cwd": conf.dir
|
|
98
|
+
}), cat);
|
|
99
|
+
}
|
|
100
|
+
_log4Js.default.addAppender(_log4Js.default.appenderMakers.stdout(stdoutConf), cat);
|
|
101
|
+
logger = _log4Js.default.getLogger(cat);
|
|
102
|
+
logger.setLevel(conf.level);
|
|
103
|
+
LOGERS[cat] = logger;
|
|
104
|
+
return logger;
|
|
105
|
+
};
|
|
106
|
+
var meta = require(_path.default.resolve(process.cwd(), "package.json"));
|
|
107
|
+
const globalLog = new Log({
|
|
108
|
+
"name": meta.name,
|
|
109
|
+
"level": "all"
|
|
110
|
+
});
|
|
111
|
+
// 日志
|
|
112
|
+
global.log = globalLog;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "md5", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>md5
|
|
8
|
+
});
|
|
9
|
+
const _crypto = /*#__PURE__*/ _interopRequireDefault(require("crypto"));
|
|
10
|
+
function _interopRequireDefault(obj) {
|
|
11
|
+
return obj && obj.__esModule ? obj : {
|
|
12
|
+
default: obj
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function md5(str) {
|
|
16
|
+
var md5 = _crypto.default.createHash("md5");
|
|
17
|
+
md5.update(str);
|
|
18
|
+
return md5.digest("hex");
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "MIME", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>MIME
|
|
8
|
+
});
|
|
9
|
+
const MIME = {
|
|
10
|
+
"html": "text/html",
|
|
11
|
+
"css": "text/css",
|
|
12
|
+
"js": "application/javascript",
|
|
13
|
+
"png": "image/png",
|
|
14
|
+
"jpg": "image/jpeg",
|
|
15
|
+
"gif": "image/gif",
|
|
16
|
+
"xml": "application/xml",
|
|
17
|
+
"json": "application/json"
|
|
18
|
+
};
|