@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,144 @@
|
|
|
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 _htmlFilter = /*#__PURE__*/ _interopRequireWildcard(require("./@bin/html-filter"));
|
|
10
|
+
const _url = /*#__PURE__*/ _interopRequireDefault(require("url"));
|
|
11
|
+
function _interopRequireDefault(obj) {
|
|
12
|
+
return obj && obj.__esModule ? obj : {
|
|
13
|
+
default: obj
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
17
|
+
if (typeof WeakMap !== "function") return null;
|
|
18
|
+
var cacheBabelInterop = new WeakMap();
|
|
19
|
+
var cacheNodeInterop = new WeakMap();
|
|
20
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
21
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
22
|
+
})(nodeInterop);
|
|
23
|
+
}
|
|
24
|
+
function _interopRequireWildcard(obj, nodeInterop) {
|
|
25
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
26
|
+
return obj;
|
|
27
|
+
}
|
|
28
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
29
|
+
return {
|
|
30
|
+
default: obj
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
34
|
+
if (cache && cache.has(obj)) {
|
|
35
|
+
return cache.get(obj);
|
|
36
|
+
}
|
|
37
|
+
var newObj = {};
|
|
38
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
39
|
+
for(var key in obj){
|
|
40
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
41
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
42
|
+
if (desc && (desc.get || desc.set)) {
|
|
43
|
+
Object.defineProperty(newObj, key, desc);
|
|
44
|
+
} else {
|
|
45
|
+
newObj[key] = obj[key];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
newObj.default = obj;
|
|
50
|
+
if (cache) {
|
|
51
|
+
cache.set(obj, newObj);
|
|
52
|
+
}
|
|
53
|
+
return newObj;
|
|
54
|
+
}
|
|
55
|
+
const IS_STATIC_RE = /^\/.*\.(ttf|woff|eot|html|css|js|json|png|jpeg|jpg|gif|bmp|handlebars|mp3|mp4|m3u8|avi|mpg|mpeg|xml|json|tpl)$/i;
|
|
56
|
+
const IS_IMG_RE = /\.(jpg|jpeg|png|gif)$/i;
|
|
57
|
+
const IS_CSS_RE = /\.css$/i;
|
|
58
|
+
const IS_JS_RE = /\.js$/i;
|
|
59
|
+
const IS_HTML_RE = /\.html$/i;
|
|
60
|
+
const IS_DIR_RE = /(\/|[\.\d\/]+)$/i;
|
|
61
|
+
const IS_TPL_RE = /\.tpl$/i;
|
|
62
|
+
const IS_FONT_RE = /\.(ttf|woff|eot|woff2)$/i;
|
|
63
|
+
// 判断是否是 JS 文件路径
|
|
64
|
+
function isJS(url) {
|
|
65
|
+
return url && IS_JS_RE.test(url);
|
|
66
|
+
}
|
|
67
|
+
// 判断是否是 HTML 文件路径
|
|
68
|
+
function isHtml(url) {
|
|
69
|
+
return url && IS_HTML_RE.test(url);
|
|
70
|
+
}
|
|
71
|
+
// 判断是否是图片路径
|
|
72
|
+
function isImg(url) {
|
|
73
|
+
return url && IS_IMG_RE.test(url);
|
|
74
|
+
}
|
|
75
|
+
// 判断是否目录路径
|
|
76
|
+
function isDir(url) {
|
|
77
|
+
return url && IS_DIR_RE.test(url) && !IS_FONT_RE.test(url);
|
|
78
|
+
}
|
|
79
|
+
// 判断是否是 css 文件路径
|
|
80
|
+
function isCss(url) {
|
|
81
|
+
return url && IS_CSS_RE.test(url);
|
|
82
|
+
}
|
|
83
|
+
// 判断是否文件
|
|
84
|
+
function isStatic(url) {
|
|
85
|
+
return url && IS_STATIC_RE.test(url);
|
|
86
|
+
}
|
|
87
|
+
// 判断是否模板
|
|
88
|
+
function isTpl(url) {
|
|
89
|
+
return url && IS_TPL_RE.test(url);
|
|
90
|
+
}
|
|
91
|
+
// 判断是否字体
|
|
92
|
+
function isFont(url) {
|
|
93
|
+
return url && IS_FONT_RE.test(url);
|
|
94
|
+
}
|
|
95
|
+
// 请求过滤器
|
|
96
|
+
// 用于补充识别一些必要信息
|
|
97
|
+
async function filter(ctx, next) {
|
|
98
|
+
var parsedUrl = _url.default.parse(ctx.url);
|
|
99
|
+
var pathname = parsedUrl.pathname;
|
|
100
|
+
ctx.state.parsedUrl = parsedUrl;
|
|
101
|
+
var srcType = {
|
|
102
|
+
"js": false,
|
|
103
|
+
"html": false,
|
|
104
|
+
"img": false,
|
|
105
|
+
"css": false,
|
|
106
|
+
"dir": false,
|
|
107
|
+
"static": false,
|
|
108
|
+
"tpl": false,
|
|
109
|
+
"font": false
|
|
110
|
+
};
|
|
111
|
+
// 辨别请求资源类型
|
|
112
|
+
if (isDir(pathname)) {
|
|
113
|
+
srcType.dir = true;
|
|
114
|
+
// 提取页面必要的信息
|
|
115
|
+
// 由于 dir 也可能直接返回 HTML 页面
|
|
116
|
+
// 所以这里也需要经过 HTML 的过滤器处理
|
|
117
|
+
ctx.state.originHost = ctx.headers["x-forwarded-host"] || ctx.headers.host;
|
|
118
|
+
ctx.state.ver = (0, _htmlFilter.filter)(ctx.url);
|
|
119
|
+
} else if (isStatic(pathname)) {
|
|
120
|
+
srcType.static = true;
|
|
121
|
+
if (isHtml(pathname)) {
|
|
122
|
+
srcType.html = true;
|
|
123
|
+
// 提取页面必要的信息
|
|
124
|
+
ctx.state.originHost = ctx.headers["x-forwarded-host"] || ctx.headers.host;
|
|
125
|
+
ctx.state.ver = (0, _htmlFilter.filter)(ctx.url);
|
|
126
|
+
} else if (isJS(pathname)) {
|
|
127
|
+
srcType.js = true;
|
|
128
|
+
} else if (isImg(pathname)) {
|
|
129
|
+
srcType.img = true;
|
|
130
|
+
} else if (isCss(pathname)) {
|
|
131
|
+
srcType.css = true;
|
|
132
|
+
} else if (isTpl(pathname)) {
|
|
133
|
+
srcType.tpl = true;
|
|
134
|
+
} else if (isFont(pathname)) {
|
|
135
|
+
srcType.font = true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
ctx.state.srcType = srcType;
|
|
139
|
+
await next();
|
|
140
|
+
}
|
|
141
|
+
function _default() {
|
|
142
|
+
(0, _htmlFilter.default)();
|
|
143
|
+
return filter;
|
|
144
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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 info = {};
|
|
10
|
+
// 业务名称
|
|
11
|
+
info.mark = getSysConfig("mark") || getSysConfig("name");
|
|
12
|
+
// 业务版本
|
|
13
|
+
info.version = getSysConfig("version");
|
|
14
|
+
// 服务端版本
|
|
15
|
+
info.serverVer = require("../../package.json").version;
|
|
16
|
+
// 业务启动时间
|
|
17
|
+
// FIX ME : 服务器不是北京时间的时候这个数据跟实际启动的时间存在时差
|
|
18
|
+
info.date = requireMod("common").date("Y-m-d h:i:s", Date.now());
|
|
19
|
+
// 模块版本信息
|
|
20
|
+
const MARK = `${info.mark}@${info.version}&${info.serverVer} (${info.date})`;
|
|
21
|
+
info = null;
|
|
22
|
+
/**
|
|
23
|
+
* 处理函数
|
|
24
|
+
*/ async function handler(ctx, next) {
|
|
25
|
+
ctx.set("X-Mark", MARK);
|
|
26
|
+
await next();
|
|
27
|
+
}
|
|
28
|
+
function _default() {
|
|
29
|
+
return handler;
|
|
30
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
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 cwd = process.cwd();
|
|
12
|
+
/**内部中间件 */ const INTERNAL_MIDDLEWARE = {
|
|
13
|
+
"combo": true,
|
|
14
|
+
"bad-request": true,
|
|
15
|
+
"fresh-filter": true,
|
|
16
|
+
"handle-pre-dir": true,
|
|
17
|
+
"request-filter": true,
|
|
18
|
+
"service-mark": true,
|
|
19
|
+
"compress": true,
|
|
20
|
+
"cors": true
|
|
21
|
+
};
|
|
22
|
+
// [最后3个中间件固定是 系统路由,静态文件服务,错误请求处理模块]
|
|
23
|
+
/**加载中间件 */ function loadMiddleware(app) {
|
|
24
|
+
const config = getSysConfig();
|
|
25
|
+
if ((0, _utils.isArray)(config.middleware) && config.middleware.length) {
|
|
26
|
+
config.middleware.forEach((item)=>{
|
|
27
|
+
const isSrtItem = (0, _utils.isString)(item);
|
|
28
|
+
let name = isSrtItem ? item : item[0];
|
|
29
|
+
const conf = isSrtItem ? null : item[1];
|
|
30
|
+
if (INTERNAL_MIDDLEWARE[name]) {
|
|
31
|
+
app.use((0, _common.getRealDefaultMod)(require(`./middleware/${name}`))(conf));
|
|
32
|
+
} else {
|
|
33
|
+
app.use((0, _common.getRealDefaultMod)(require(`${cwd}/@server/middleware/${name}`))(conf));
|
|
34
|
+
}
|
|
35
|
+
masterLog(`MIDDLEWARE >>> [${name}] launch`);
|
|
36
|
+
});
|
|
37
|
+
console.log("");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const _default = loadMiddleware;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@x-9lab/xlab",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "9lab 服务端模块",
|
|
6
|
+
"versionDesc": "",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"dev": "swc src -D ./src/bin --config-file .swcrc -d dist -w",
|
|
9
|
+
"compile": "swc src -D ./src/bin --config-file .swcrc -d dist",
|
|
10
|
+
"build": "yarn compile && yarn gen-declaration",
|
|
11
|
+
"gen-declaration": "tsc --emitDeclarationOnly",
|
|
12
|
+
"changelog": "changelog"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">10.0.0"
|
|
16
|
+
},
|
|
17
|
+
"main": "./dist/server.js",
|
|
18
|
+
"types": "./@types/global.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"@types"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"koa": "2.13.1",
|
|
25
|
+
"cron": "1.8.2",
|
|
26
|
+
"redis": "4.0.3",
|
|
27
|
+
"log4js": "1.1.1",
|
|
28
|
+
"liftoff": "3.1.0",
|
|
29
|
+
"koa-ejs": "4.3.0",
|
|
30
|
+
"minimist": "1.2.5",
|
|
31
|
+
"request": "2.86.0",
|
|
32
|
+
"koa-body": "4.2.0",
|
|
33
|
+
"lru-cache": "6.0.0",
|
|
34
|
+
"koa-static": "5.0.0",
|
|
35
|
+
"koa-router": "10.0.0",
|
|
36
|
+
"http-proxy": "1.18.1",
|
|
37
|
+
"koa-compress": "5.0.1",
|
|
38
|
+
"@x-drive/utils": "1.1.19"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"co": "4.6.0",
|
|
42
|
+
"mocha": "10.0.0",
|
|
43
|
+
"tslib": "2.3.0",
|
|
44
|
+
"colors": "1.4.0",
|
|
45
|
+
"should": "11.2.1",
|
|
46
|
+
"nodemon": "2.0.12",
|
|
47
|
+
"socket.io": "2.0.3",
|
|
48
|
+
"chokidar": "3.5.3",
|
|
49
|
+
"typescript": "4.3.4",
|
|
50
|
+
"@types/node": "16.11.40",
|
|
51
|
+
"@types/log4js": "2.3.5",
|
|
52
|
+
"@types/koa-static": "4.0.2",
|
|
53
|
+
"@types/koa-router": "7.4.4",
|
|
54
|
+
"@swc/cli": "0.1.57",
|
|
55
|
+
"@swc/core": "1.2.212",
|
|
56
|
+
"@x-drive/changelog": "1.1.6"
|
|
57
|
+
},
|
|
58
|
+
"bin": {
|
|
59
|
+
"xlab": "dist/bin/xlab"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
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 _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
12
|
+
function _interopRequireDefault(obj) {
|
|
13
|
+
return obj && obj.__esModule ? obj : {
|
|
14
|
+
default: obj
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**生成 api */ function buildApi(api) {
|
|
18
|
+
if (!api.startsWith("/")) {
|
|
19
|
+
api = `/${api}`;
|
|
20
|
+
}
|
|
21
|
+
return `/api${api}`;
|
|
22
|
+
}
|
|
23
|
+
/**加载业务服务接口 */ function appRouter(router) {
|
|
24
|
+
const IS_API_REGEXP = /^\/api\//;
|
|
25
|
+
var processBusiness = function(modPath, item) {
|
|
26
|
+
var mod = require(_path.default.resolve(modPath));
|
|
27
|
+
if (mod) {
|
|
28
|
+
mod = (0, _common.getRealDefaultMod)(mod);
|
|
29
|
+
let api_path = modPath.split("business");
|
|
30
|
+
let api;
|
|
31
|
+
let modName;
|
|
32
|
+
// 根据路径生成 url 规则
|
|
33
|
+
api = api_path[1].replace(".js", "").split("/");
|
|
34
|
+
// 文件名是 index
|
|
35
|
+
const lastIsIndex = api[api.length - 1] === "index";
|
|
36
|
+
// 文件名是 index 的则会被从 api 上先强行去掉
|
|
37
|
+
// 如果需要命名成 index 的需要自己用 object 形式,用 api 字段定义
|
|
38
|
+
if (lastIsIndex) {
|
|
39
|
+
api.pop();
|
|
40
|
+
}
|
|
41
|
+
// 如果模块返回的是数组,则使用文件名做为方法名,并在 api 上去掉文件名
|
|
42
|
+
if ((0, _utils.isArray)(mod)) {
|
|
43
|
+
if (!lastIsIndex) {
|
|
44
|
+
api.pop();
|
|
45
|
+
}
|
|
46
|
+
api = buildApi(api.join("/"));
|
|
47
|
+
mod.unshift(api);
|
|
48
|
+
} else {
|
|
49
|
+
// 不是数组则把文件当作正常的 api 地址
|
|
50
|
+
api = buildApi(api.join("/"));
|
|
51
|
+
}
|
|
52
|
+
// 是对象则取对应的字段
|
|
53
|
+
if ((0, _utils.isObject)(mod) && ((0, _utils.isFunction)(mod.handler) || (0, _utils.isAsyncFunction)(mod.handler))) {
|
|
54
|
+
modName = mod.method || "get";
|
|
55
|
+
api = mod.api || api;
|
|
56
|
+
// 保证一定是以 api 开头的
|
|
57
|
+
if (!IS_API_REGEXP.test(api)) {
|
|
58
|
+
api = buildApi(api);
|
|
59
|
+
}
|
|
60
|
+
if ((0, _utils.isArray)(mod.middleware)) {
|
|
61
|
+
mod = [
|
|
62
|
+
api,
|
|
63
|
+
...mod.middleware,
|
|
64
|
+
mod.handler
|
|
65
|
+
];
|
|
66
|
+
} else {
|
|
67
|
+
mod = [
|
|
68
|
+
api,
|
|
69
|
+
mod.handler
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// 直接返回函数则直接绑定
|
|
74
|
+
if ((0, _utils.isFunction)(mod) || (0, _utils.isAsyncFunction)(mod)) {
|
|
75
|
+
mod = [
|
|
76
|
+
api,
|
|
77
|
+
mod
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
if (!modName) {
|
|
81
|
+
modName = item.replace(".js", "");
|
|
82
|
+
if (modName === "index") {
|
|
83
|
+
modName = "get";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
router[modName].apply(router, mod);
|
|
87
|
+
masterLog("router", `Handling API >>> [${modName}] ${api}`);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
// 业务基本目录
|
|
91
|
+
var _p = _path.default.resolve(__dirname, "business");
|
|
92
|
+
(0, _common.walk)(_p, 0, processBusiness);
|
|
93
|
+
// 支持自定义 api
|
|
94
|
+
_p = _path.default.resolve(process.cwd(), "@server", "business");
|
|
95
|
+
if (process.cwd() !== __dirname && (0, _common.checkFileStat)(_p)) {
|
|
96
|
+
(0, _common.walk)(_p, 0, processBusiness);
|
|
97
|
+
}
|
|
98
|
+
processBusiness = null;
|
|
99
|
+
console.log("");
|
|
100
|
+
}
|
|
101
|
+
const _default = appRouter;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
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
|
+
boot: ()=>boot,
|
|
13
|
+
exit: ()=>exit,
|
|
14
|
+
default: ()=>_default
|
|
15
|
+
});
|
|
16
|
+
require("./global");
|
|
17
|
+
const _log = require("./components/log");
|
|
18
|
+
const _cron = require("./components/cron");
|
|
19
|
+
const _koaStatic = /*#__PURE__*/ _interopRequireDefault(require("koa-static"));
|
|
20
|
+
const _koaRouter = /*#__PURE__*/ _interopRequireDefault(require("koa-router"));
|
|
21
|
+
const _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
|
|
22
|
+
const _koa = /*#__PURE__*/ _interopRequireDefault(require("koa"));
|
|
23
|
+
const _config = /*#__PURE__*/ _interopRequireWildcard(require("./config"));
|
|
24
|
+
const _custom = /*#__PURE__*/ _interopRequireDefault(require("./custom"));
|
|
25
|
+
const _router = /*#__PURE__*/ _interopRequireDefault(require("./router"));
|
|
26
|
+
const _cluster = /*#__PURE__*/ _interopRequireDefault(require("./cluster"));
|
|
27
|
+
const _middlewares = /*#__PURE__*/ _interopRequireDefault(require("./middlewares"));
|
|
28
|
+
const _badRequest = /*#__PURE__*/ _interopRequireDefault(require("./middleware/bad-request"));
|
|
29
|
+
function _interopRequireDefault(obj) {
|
|
30
|
+
return obj && obj.__esModule ? obj : {
|
|
31
|
+
default: obj
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
35
|
+
if (typeof WeakMap !== "function") return null;
|
|
36
|
+
var cacheBabelInterop = new WeakMap();
|
|
37
|
+
var cacheNodeInterop = new WeakMap();
|
|
38
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
39
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
40
|
+
})(nodeInterop);
|
|
41
|
+
}
|
|
42
|
+
function _interopRequireWildcard(obj, nodeInterop) {
|
|
43
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
44
|
+
return obj;
|
|
45
|
+
}
|
|
46
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
47
|
+
return {
|
|
48
|
+
default: obj
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
52
|
+
if (cache && cache.has(obj)) {
|
|
53
|
+
return cache.get(obj);
|
|
54
|
+
}
|
|
55
|
+
var newObj = {};
|
|
56
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
57
|
+
for(var key in obj){
|
|
58
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
59
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
60
|
+
if (desc && (desc.get || desc.set)) {
|
|
61
|
+
Object.defineProperty(newObj, key, desc);
|
|
62
|
+
} else {
|
|
63
|
+
newObj[key] = obj[key];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
newObj.default = obj;
|
|
68
|
+
if (cache) {
|
|
69
|
+
cache.set(obj, newObj);
|
|
70
|
+
}
|
|
71
|
+
return newObj;
|
|
72
|
+
}
|
|
73
|
+
const logger = _log.globalLog.getLogger("system");
|
|
74
|
+
// 只在 master 上输出的日志
|
|
75
|
+
global.masterLog = _config.log;
|
|
76
|
+
// 服务实例
|
|
77
|
+
const app = new _koa.default();
|
|
78
|
+
/**
|
|
79
|
+
* 获取应用实例对象
|
|
80
|
+
* @return 实例对象
|
|
81
|
+
*/ global.getApp = function() {
|
|
82
|
+
return app;
|
|
83
|
+
};
|
|
84
|
+
// 不输出X-Powered-By
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
app.poweredBy = false;
|
|
87
|
+
if (_config.default.custom) {
|
|
88
|
+
// 自定义初始化业务
|
|
89
|
+
(0, _custom.default)();
|
|
90
|
+
}
|
|
91
|
+
// 加载中间件
|
|
92
|
+
(0, _middlewares.default)(app);
|
|
93
|
+
// 数据体解析
|
|
94
|
+
// app.use(koaBody());
|
|
95
|
+
const koaRouter = new _koaRouter.default();
|
|
96
|
+
// 路由中间件
|
|
97
|
+
app.use(koaRouter.routes());
|
|
98
|
+
// 具体路由
|
|
99
|
+
(0, _router.default)(koaRouter);
|
|
100
|
+
// 静态文件服务
|
|
101
|
+
var staticServeOpts = {};
|
|
102
|
+
if (_config.default.isProd) {
|
|
103
|
+
staticServeOpts.maxage = _config.default.staticMaxage;
|
|
104
|
+
const staticHtmlFileMaxage = _config.default.staticHtmlFileMaxage;
|
|
105
|
+
staticServeOpts.setHeaders = function(res, path) {
|
|
106
|
+
if (staticHtmlFileMaxage && path.endsWith(".html")) {
|
|
107
|
+
res.setHeader("Cache-Control", `max-age=${staticHtmlFileMaxage}`);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
app.use((0, _koaStatic.default)(_path.default.resolve(process.cwd(), _config.default.root), staticServeOpts));
|
|
112
|
+
// 异常状态处理中间件
|
|
113
|
+
app.use(_badRequest.default);
|
|
114
|
+
if (_config.default.enableCron) {
|
|
115
|
+
// 定时任务
|
|
116
|
+
(0, _cron.on)(_path.default.resolve(process.cwd(), "@server", "cron"));
|
|
117
|
+
}
|
|
118
|
+
/**启动服务 */ function boot() {
|
|
119
|
+
(0, _cluster.default)();
|
|
120
|
+
process.on("uncaughtException", (err)=>{
|
|
121
|
+
logger.error("uncaughtException:\n", err.stack);
|
|
122
|
+
}).on("unhandledRejection", (err)=>{
|
|
123
|
+
logger.error("unhandledRejection:\n", err.stack);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**退出服务 */ function exit() {
|
|
127
|
+
process.exit();
|
|
128
|
+
}
|
|
129
|
+
const _default = app;
|
|
130
|
+
if (require.main === module) {
|
|
131
|
+
boot();
|
|
132
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@x-9lab/xlab",
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
|
+
"description": "9lab 服务端模块",
|
|
5
|
+
"versionDesc": "",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "swc src -D ./src/bin --config-file .swcrc -d dist -w",
|
|
8
|
+
"compile": "swc src -D ./src/bin --config-file .swcrc -d dist",
|
|
9
|
+
"build": "yarn compile && yarn gen-declaration",
|
|
10
|
+
"gen-declaration": "tsc --emitDeclarationOnly",
|
|
11
|
+
"changelog": "changelog"
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">10.0.0"
|
|
15
|
+
},
|
|
16
|
+
"main": "./dist/server.js",
|
|
17
|
+
"types": "./@types/global.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"koa": "2.13.1",
|
|
23
|
+
"cron": "1.8.2",
|
|
24
|
+
"redis": "4.0.3",
|
|
25
|
+
"log4js": "1.1.1",
|
|
26
|
+
"liftoff": "3.1.0",
|
|
27
|
+
"koa-ejs": "4.3.0",
|
|
28
|
+
"minimist": "1.2.5",
|
|
29
|
+
"request": "2.86.0",
|
|
30
|
+
"koa-body": "4.2.0",
|
|
31
|
+
"lru-cache": "6.0.0",
|
|
32
|
+
"koa-static": "5.0.0",
|
|
33
|
+
"koa-router": "10.0.0",
|
|
34
|
+
"http-proxy": "1.18.1",
|
|
35
|
+
"koa-compress": "5.0.1",
|
|
36
|
+
"@x-drive/utils": "1.1.19"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"co": "4.6.0",
|
|
40
|
+
"mocha": "10.0.0",
|
|
41
|
+
"tslib": "2.3.0",
|
|
42
|
+
"colors": "1.4.0",
|
|
43
|
+
"should": "11.2.1",
|
|
44
|
+
"nodemon": "2.0.12",
|
|
45
|
+
"socket.io": "2.0.3",
|
|
46
|
+
"chokidar": "3.5.3",
|
|
47
|
+
"typescript": "4.3.4",
|
|
48
|
+
"@types/node": "16.11.40",
|
|
49
|
+
"@types/log4js": "2.3.5",
|
|
50
|
+
"@types/koa-static": "4.0.2",
|
|
51
|
+
"@types/koa-router": "7.4.4",
|
|
52
|
+
"@swc/cli": "0.1.57",
|
|
53
|
+
"@swc/core": "1.2.212",
|
|
54
|
+
"@x-drive/changelog": "1.1.6"
|
|
55
|
+
},
|
|
56
|
+
"bin": {
|
|
57
|
+
"xlab": "dist/bin/xlab"
|
|
58
|
+
}
|
|
59
|
+
}
|