@x-9lab/xlab 1.4.1 → 1.5.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.
@@ -24,28 +24,6 @@ export { base64Toascii };
24
24
  */
25
25
  declare function params(dat: Record<string, any>, url: string): string;
26
26
  export { params };
27
- /**
28
- * 格式化时间
29
- * @param format 日期格式
30
- * @param ts 时间戳
31
- * @return 格式化后的时间
32
- */
33
- declare function date(format: string, ts: number): string;
34
- /**
35
- * 格式化时间
36
- * @param format 日期格式
37
- * @param ts 日期字符串
38
- * @return 格式化后的时间
39
- */
40
- declare function date(format: string, ts: string): string;
41
- /**
42
- * 格式化时间
43
- * @param format 日期格式
44
- * @param ts 日期对象
45
- * @return 格式化后的时间
46
- */
47
- declare function date(format: string, ts: Date): string;
48
- export { date };
49
27
  /**
50
28
  * 生成操作结果数据对象
51
29
  * @param status 返回码
@@ -76,6 +54,9 @@ export { responseResult };
76
54
  */
77
55
  declare function getMD5(...args: any[]): string;
78
56
  export { getMD5 };
57
+ /**将一个输入字符串转为 md5 */
58
+ declare function md5(str: string): string;
59
+ export { md5 };
79
60
  /**
80
61
  * 对象值转化为数组
81
62
  * @param obj 待转的对象
@@ -83,22 +64,6 @@ export { getMD5 };
83
64
  */
84
65
  declare function object2Array(obj: XLab.JsonObject): any[];
85
66
  export { object2Array };
86
- /**
87
- * 默认 Promise 处理函数
88
- * @param resolve Promise 成功处理函数
89
- * @param reject Promise 异常处理函数
90
- */
91
- declare function defPromiseHandler<T = any>(resolve: Function, reject: Function): (err: Error, re: T) => void;
92
- export { defPromiseHandler };
93
- /**Promise 包装函数 */
94
- declare function promiseWapper(...args: any[]): Promise<unknown>;
95
- export { promiseWapper };
96
- /**
97
- * 封装一个 promise 形式的 request 方法
98
- * @deprecated 请直接使用 component 中的 request 模块
99
- */
100
- declare function requestWapper<T = any>(...args: any[]): Promise<unknown>;
101
- export { requestWapper };
102
67
  /**
103
68
  * 格式化 JSON 数据
104
69
  * @param data 原始数据字符串
@@ -2,32 +2,14 @@
2
2
  interface InternalComponents {
3
3
  /**资产 */
4
4
  assets: typeof import("./assets");
5
- /**缓存 */
6
- cache: typeof import("./cache");
7
5
  /**cluster */
8
6
  cluster: typeof import("./cluster");
9
7
  /**Http header 相关 */
10
8
  header: typeof import("./header");
11
- /**Html 内容处理器 */
12
- "html-processor": typeof import("./html-processor");
13
- /**JS 内容处理器 */
14
- "js-processor": typeof import("./js-processor");
15
- /**数据注入 */
16
- injection: typeof import("./injection");
17
- /**MD5 */
18
- md5: typeof import("./md5");
19
9
  /**MIME */
20
10
  mime: typeof import("./mime");
21
- /**客户端判断 */
22
- platform: typeof import("./platform");
23
- /**请求参数处理 */
24
- querystring: typeof import("./querystring");
25
- /**各种重定向方法 */
26
- redirect: typeof import("./redirect");
27
11
  /**简单的 uuid */
28
12
  uuid: typeof import("./uuid");
29
- /**版本比对 */
30
- version: typeof import("./version");
31
13
  /**定时任务 */
32
14
  cron: typeof import("./cron");
33
15
  /**日志 */
@@ -36,7 +18,5 @@ interface InternalComponents {
36
18
  "return-code": typeof import("./return-code");
37
19
  /**通用工具函数集 */
38
20
  common: typeof import("./common");
39
- /**内部请求模块 */
40
- request: typeof import("./request");
41
21
  }
42
22
  export type { InternalComponents };
@@ -1,5 +1,4 @@
1
1
  import type { InternalComponents } from "./components";
2
- export type { IPlat } from "./components/platform";
3
2
  import type Koa from "koa";
4
3
  declare global {
5
4
  /**全局日志对象 */
@@ -170,6 +169,8 @@ declare global {
170
169
  staticResourceCacheTime?: number;
171
170
  /**是否允许静态资源跨域访问 */
172
171
  staticCros?: boolean;
172
+ /**时区, 默认是 Asia/Shanghai */
173
+ timezone?: string;
173
174
  }
174
175
  /**业务错误数据对象 */
175
176
  interface ICodeItem {
@@ -19,6 +19,7 @@ const CONFIG = {
19
19
  "enableCron": false,
20
20
  "middleware": [],
21
21
  "strictSSL": false,
22
- "root": "public"
22
+ "root": "public",
23
+ "timezone": "Asia/Shanghai"
23
24
  };
24
25
  const _default = CONFIG;
@@ -16,13 +16,10 @@ _export(exports, {
16
16
  asciiTobase64: ()=>asciiTobase64,
17
17
  base64Toascii: ()=>base64Toascii,
18
18
  params: ()=>params,
19
- date: ()=>date,
20
19
  responseResult: ()=>responseResult,
21
20
  getMD5: ()=>getMD5,
21
+ md5: ()=>md5,
22
22
  object2Array: ()=>object2Array,
23
- defPromiseHandler: ()=>defPromiseHandler,
24
- promiseWapper: ()=>promiseWapper,
25
- requestWapper: ()=>requestWapper,
26
23
  formatJson: ()=>formatJson,
27
24
  toJsonp: ()=>toJsonp,
28
25
  walk: ()=>walk,
@@ -79,52 +76,6 @@ function checkLogger() {
79
76
  }
80
77
  return data;
81
78
  }
82
- const format_exp = /[YymndjNwaAghGHis]/g;
83
- /**
84
- * 格式化时间
85
- * @param format 日期格式
86
- * @param ts 日期数据(时间戳, 字符串)
87
- * @return 格式化后的时间
88
- */ function date(format, ts) {
89
- ts = new Date(ts);
90
- return format.replace(format_exp, function(tag) {
91
- switch(tag){
92
- case "Y":
93
- return ts.getFullYear();
94
- case "y":
95
- return ts.getFullYear() % 100;
96
- case "m":
97
- return (0, _utils.fix0)(ts.getMonth() + 1, 2);
98
- case "n":
99
- return ts.getMonth() + 1;
100
- case "d":
101
- return (0, _utils.fix0)(ts.getDate(), 2);
102
- case "j":
103
- return ts.getDate();
104
- case "N":
105
- return ts.getDay();
106
- case "w":
107
- return ts.getDay() % 7;
108
- case "a":
109
- return ts.getHours() < 12 ? "am" : "pm";
110
- case "A":
111
- return ts.getHours() < 12 ? "AM" : "PM";
112
- case "g":
113
- return ts.getHours() % 12 + 1;
114
- case "h":
115
- return (0, _utils.fix0)(ts.getHours() % 12 + 1, 2);
116
- case "G":
117
- return ts.getHours();
118
- case "H":
119
- return (0, _utils.fix0)(ts.getHours(), 2);
120
- case "i":
121
- return (0, _utils.fix0)(ts.getMinutes(), 2);
122
- case "s":
123
- return (0, _utils.fix0)(ts.getSeconds(), 2);
124
- }
125
- return tag;
126
- });
127
- }
128
79
  /**
129
80
  * 生成操作结果数据对象
130
81
  * @param status 布尔,操作结果
@@ -150,6 +101,11 @@ const format_exp = /[YymndjNwaAghGHis]/g;
150
101
  });
151
102
  return _crypto.default.createHash("md5").update(JSON.stringify(conditions), "utf8").digest("hex");
152
103
  }
104
+ /**将一个输入字符串转为 md5 */ function md5(str) {
105
+ var md5 = _crypto.default.createHash("md5");
106
+ md5.update(str);
107
+ return md5.digest("hex");
108
+ }
153
109
  /**
154
110
  * 对象值转化为数组
155
111
  * @param obj 待转的对象
@@ -161,36 +117,6 @@ const format_exp = /[YymndjNwaAghGHis]/g;
161
117
  });
162
118
  return arr;
163
119
  }
164
- /**
165
- * 默认 Promise 处理函数
166
- * @param resolve Promise 成功处理函数
167
- * @param reject Promise 异常处理函数
168
- */ function defPromiseHandler(resolve, reject) {
169
- return function(err, re) {
170
- if (err) {
171
- console.error(err);
172
- reject(err);
173
- } else if (re) {
174
- resolve(re);
175
- } else {
176
- reject("No result.");
177
- }
178
- };
179
- }
180
- /**Promise 包装函数 */ function promiseWapper(...args) {
181
- var handler = args.pop();
182
- handler = handler;
183
- var p = new Promise(handler);
184
- return p;
185
- }
186
- /**
187
- * 封装一个 promise 形式的 request 方法
188
- * @deprecated 请直接使用 component 中的 request 模块
189
- */ function requestWapper(...args) {
190
- return new Promise(function(_, reject) {
191
- reject(new Error("请直接使用 component 中的 request 模块"));
192
- });
193
- }
194
120
  /**
195
121
  * 格式化 JSON 数据
196
122
  * @param data 原始数据字符串
@@ -59,7 +59,7 @@ const config = getSysConfig("cron") || {
59
59
  * @return 无返回值
60
60
  */ function killSomeone(name) {
61
61
  if (CRON_TIMERS[name]) {
62
- console.log("Stop crontab job ", name);
62
+ logger.info("Stop crontab job ", name);
63
63
  clearTimeout(CRON_TIMERS[name]);
64
64
  CRON_TIMERS[name] = null;
65
65
  }
@@ -14,7 +14,7 @@ _export(exports, {
14
14
  });
15
15
  const _utils = require("@x-drive/utils");
16
16
  const _os = require("os");
17
- const _md5 = require("../md5");
17
+ const _common = require("../common");
18
18
  var ref;
19
19
  var netInterfaces = (0, _os.networkInterfaces)();
20
20
  const X_MAC = ((ref = Object.keys(netInterfaces).sort().map((key)=>netInterfaces[key]).flat(1).find((en)=>{
@@ -29,7 +29,7 @@ netInterfaces = null;
29
29
  var r = (0, _utils.random)(2333333, 23333).toString(16);
30
30
  r = r.length < 6 ? r += "0" : r;
31
31
  return [
32
- (0, _md5.md5)([
32
+ (0, _common.getMD5)([
33
33
  one,
34
34
  X_MAC,
35
35
  tow
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "default", {
6
6
  enumerable: true,
7
7
  get: ()=>_default
8
8
  });
9
+ const _utils = require("@x-drive/utils");
9
10
  const serverPackge = require("../../package.json");
10
11
  var info = {};
11
12
  // 业务名称
@@ -17,7 +18,7 @@ info.serverVer = serverPackge.version;
17
18
  info.serverName = serverPackge.name;
18
19
  // 业务启动时间
19
20
  // FIX ME : 服务器不是北京时间的时候这个数据跟实际启动的时间存在时差
20
- info.date = requireMod("common").date("Y-m-d H:i:s", new Date());
21
+ info.date = (0, _utils.date)(new Date(), "Y-m-d H:i:s");
21
22
  // 模块版本信息
22
23
  const CLIENT_MARK = `${info.mark}@${info.version}`;
23
24
  const SERVER_MARK = `${info.serverName}@${info.serverVer}`;
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "default", {
6
6
  enumerable: true,
7
7
  get: ()=>_default
8
8
  });
9
- const _common = require("./components/common");
10
9
  const _utils = require("@x-drive/utils");
10
+ const _common = require("./components/common");
11
11
  const _defaultXConfig = /*#__PURE__*/ _interopRequireDefault(require("./default-x-config"));
12
12
  function _interopRequireDefault(obj) {
13
13
  return obj && obj.__esModule ? obj : {
@@ -25,34 +25,11 @@ const cwd = process.cwd();
25
25
  "compress": true,
26
26
  "cors": true
27
27
  };
28
- /**兼容老的中间件声明 */ function compatibleWithOldVer(list) {
29
- const middlewares = {};
30
- list.forEach((item, index)=>{
31
- var subject;
32
- var name;
33
- subject = {
34
- index
35
- };
36
- if ((0, _utils.isString)(item)) {
37
- name = item;
38
- } else if ((0, _utils.isArray)(item)) {
39
- name = item[0];
40
- if (item[1]) {
41
- subject.config = item[1];
42
- }
43
- }
44
- middlewares[name] = subject;
45
- });
46
- return middlewares;
47
- }
48
28
  /**获取中间件加载执行列表 */ function getExeList() {
49
29
  const config = getSysConfig();
50
30
  var mConfig;
51
31
  if (config.middlewares) {
52
32
  mConfig = config.middlewares;
53
- } else if ((0, _utils.isArray)(config.middleware) && config.middleware.length) {
54
- masterLog("middlewares", "warn", "middleware 配置已弃用并将在 v1.3.0 之后删除,请使用 middlewares 配置中间件");
55
- mConfig = compatibleWithOldVer(config.middleware);
56
33
  } else {
57
34
  mConfig = null;
58
35
  }
package/dist/router.js CHANGED
@@ -155,7 +155,9 @@ function _interopRequireDefault(obj) {
155
155
  };
156
156
  // 业务基本目录
157
157
  var _p = _path.default.resolve(__dirname, API_BUSINESS_DIR);
158
- walk(_p, {}, processBusiness);
158
+ if ((0, _common.checkFileStat)(_p)) {
159
+ walk(_p, {}, processBusiness);
160
+ }
159
161
  // 支持自定义 api
160
162
  _p = _path.default.resolve(process.cwd(), _defaultXConfig.default.businessDir, API_BUSINESS_DIR);
161
163
  if (process.cwd() !== __dirname && (0, _common.checkFileStat)(_p)) {
package/dist/server.js CHANGED
@@ -72,6 +72,7 @@ function _interopRequireWildcard(obj, nodeInterop) {
72
72
  }
73
73
  return newObj;
74
74
  }
75
+ process.env.TZ = _config.default.timezone;
75
76
  const logger = _log.globalLog.getLogger("system");
76
77
  (0, _dotFile.processDotFile)(_config.default.env, logger);
77
78
  // 只在 master 上输出的日志
@@ -93,8 +94,6 @@ if (_config.default.custom) {
93
94
  }
94
95
  // 加载中间件
95
96
  (0, _middlewares.default)(app);
96
- // 数据体解析
97
- // app.use(koaBody());
98
97
  const koaRouter = new _koaRouter.default();
99
98
  // 路由中间件
100
99
  app.use(koaRouter.routes());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-9lab/xlab",
3
- "version": "1.4.1",
3
+ "version": "1.5.1",
4
4
  "description": "9lab 服务端模块",
5
5
  "versionDesc": "静态文件服务支持跨域",
6
6
  "scripts": {
@@ -24,33 +24,25 @@
24
24
  "cron": "1.8.2",
25
25
  "log4js": "^1.1.1",
26
26
  "liftoff": "4.0.0",
27
- "koa-ejs": "4.3.0",
28
27
  "minimist": "1.2.6",
29
- "koa-body": "5.0.0",
30
- "lru-cache": "^6.0.0",
31
- "node-fetch": "3.0.0",
32
28
  "koa-static": "5.0.0",
33
29
  "koa-router": "11.0.1",
34
- "http-proxy": "1.18.1",
35
30
  "koa-compress": "5.1.0",
36
- "@x-drive/utils": "1.1.23",
37
- "@x-drive/node-fetch-warper": "1.0.1"
31
+ "@x-drive/utils": "1.1.24"
38
32
  },
39
33
  "devDependencies": {
40
- "co": "4.6.0",
41
- "mocha": "10.0.0",
42
34
  "tslib": "2.3.0",
43
35
  "colors": "1.4.0",
44
36
  "should": "11.2.1",
45
37
  "nodemon": "2.0.12",
46
38
  "chokidar": "3.5.3",
39
+ "@swc/cli": "0.1.57",
47
40
  "typescript": "4.3.4",
48
- "@types/node": "16.11.40",
41
+ "@swc/core": "1.2.212",
49
42
  "@types/log4js": "2.3.5",
43
+ "@types/node": "16.11.40",
50
44
  "@types/koa-static": "4.0.2",
51
45
  "@types/koa-router": "7.4.4",
52
- "@swc/cli": "0.1.57",
53
- "@swc/core": "1.2.212",
54
46
  "@x-drive/changelog": "1.1.6"
55
47
  },
56
48
  "bin": {
@@ -1,35 +0,0 @@
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
- function onClean(ctx) {
10
- var cookies = ctx.query.c && ctx.query.c.split(",") || null;
11
- if (!cookies) {
12
- cookies = [];
13
- (ctx.headers.cookie || "").split("; ").forEach((item)=>{
14
- cookies.push(item.split("=")[0]);
15
- });
16
- }
17
- let len = 0;
18
- if (cookies && cookies.length) {
19
- let cookiePath = "/";
20
- let expires = new Date(-1);
21
- let httpOnly = false;
22
- cookies.forEach((cookie)=>{
23
- if (cookie) {
24
- ctx.cookies.set(cookie, "", {
25
- "path": cookiePath,
26
- expires,
27
- httpOnly
28
- });
29
- len += 1;
30
- }
31
- });
32
- }
33
- ctx.body = `Clean ${len}`;
34
- }
35
- const _default = onClean;
@@ -1,32 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8">
5
- <meta name="HandheldFriendly" content="true">
6
- <meta name="apple-mobile-web-app-capable" content="yes">
7
- <meta name="format-detection" content="telephone=no" />
8
- <script type="text/javascript">
9
- (function(){
10
- var el = document.createElement("p");
11
- var cookies = {cookies};
12
- var expires = (new Date(-1)).toUTCString();
13
- var len = 0;
14
- if (cookies && cookies.length) {
15
- cookies.forEach(item => {
16
- if (item) {
17
- document.cookie = item + "=''; path=/;expires=" + expires;
18
- len += 1;
19
- }
20
- });
21
- }
22
- el.innerHTML = "Clean " + len;
23
- setTimeout(function(){
24
- document.body.append(el);
25
- el = null;
26
- }, 100)
27
- })()
28
- </script>
29
- </head>
30
- <body>
31
- </body>
32
- </html>
@@ -1,30 +0,0 @@
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 _path = /*#__PURE__*/ _interopRequireDefault(require("path"));
10
- const _fs = /*#__PURE__*/ _interopRequireDefault(require("fs"));
11
- function _interopRequireDefault(obj) {
12
- return obj && obj.__esModule ? obj : {
13
- default: obj
14
- };
15
- }
16
- const JS_TPL = _fs.default.readFileSync(_path.default.resolve(__dirname, "./@tpls/js.tpl"), "utf8");
17
- function onJsClean(ctx) {
18
- var cookies = ctx.query.c;
19
- cookies = cookies && cookies.split(",") || null;
20
- if (!cookies) {
21
- cookies = [];
22
- (ctx.headers.cookie || "").split("; ").forEach((item)=>{
23
- if (item) {
24
- cookies.push(item.split("=")[0]);
25
- }
26
- });
27
- }
28
- ctx.body = JS_TPL.replace("{cookies}", JSON.stringify(cookies));
29
- }
30
- const _default = onJsClean;
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "local", {
6
- enumerable: true,
7
- get: ()=>_lru.create
8
- });
9
- const _lru = require("./lru");
@@ -1,93 +0,0 @@
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
- create: ()=>create,
13
- get: ()=>get,
14
- len: ()=>len
15
- });
16
- const _utils = require("@x-drive/utils");
17
- const _lruCache = /*#__PURE__*/ _interopRequireDefault(require("lru-cache"));
18
- function _interopRequireDefault(obj) {
19
- return obj && obj.__esModule ? obj : {
20
- default: obj
21
- };
22
- }
23
- const maxCache = 100;
24
- const maxAge = 60 * 60 * 1000;
25
- const MAX_NUM = 20;
26
- var NOW_NUM = 0;
27
- const CACHE_LIST = [];
28
- /**
29
- * LRU-CACHE实例缓存对象
30
- */ var LRU_CACHES = {};
31
- /**
32
- * 初始化缓存实例
33
- * @param cacheName 缓存名称
34
- * @param conf 缓存配置
35
- * @return 缓存实例
36
- */ function init(cacheName, conf) {
37
- if (NOW_NUM >= MAX_NUM) {
38
- var cache;
39
- var deadName;
40
- deadName = CACHE_LIST.shift();
41
- cache = LRU_CACHES[deadName];
42
- if (cache) {
43
- cache.reset();
44
- cache = null;
45
- LRU_CACHES[deadName] = null;
46
- NOW_NUM -= 1;
47
- }
48
- }
49
- conf = conf || {};
50
- conf.max = conf.max || maxCache;
51
- conf.maxAge = conf.maxAge || maxAge;
52
- LRU_CACHES[cacheName] = new _lruCache.default(conf);
53
- NOW_NUM += 1;
54
- CACHE_LIST.push(cacheName);
55
- return LRU_CACHES[cacheName];
56
- }
57
- /**
58
- * 生成或获取一个缓存实例
59
- * @param cacheName 实例名称
60
- * @param conf 缓存配置,仅在生成的时候有效
61
- * @return 缓存实例
62
- */ function create(cacheName, conf) {
63
- cacheName = cacheName || "DEFAULT";
64
- if (!LRU_CACHES[cacheName]) {
65
- if (LRU_CACHES[cacheName] === null) {
66
- console.log(`${cacheName} 为已被回收的缓存实例,重新生成新实例对象`);
67
- }
68
- init(cacheName, conf);
69
- }
70
- return LRU_CACHES[cacheName];
71
- }
72
- /**
73
- * 获取一个缓存实例
74
- * @param name 缓存名称
75
- * @param conf 缓存配置
76
- */ function get(name, conf) {
77
- if (!(0, _utils.isString)(name)) {
78
- throw new Error("必须指定缓存名称");
79
- }
80
- if (LRU_CACHES[name]) {
81
- return LRU_CACHES[name];
82
- }
83
- if ((0, _utils.isObject)(conf)) {
84
- return create(name, conf);
85
- }
86
- throw new Error(`找不到缓存 ${name}, 你可以先创建再调用本方法获取,或传入配置对象自动生成`);
87
- }
88
- /**
89
- * 获取当前缓存实例数量
90
- * @return 缓存数量
91
- */ function len() {
92
- return CACHE_LIST.length;
93
- }