@domain.js/main 0.2.2 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cfg/index.js CHANGED
@@ -1,31 +1,12 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
22
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
4
  };
24
5
  Object.defineProperty(exports, "__esModule", { value: true });
25
6
  exports.Main = void 0;
26
- const _ = __importStar(require("lodash"));
27
7
  const ajv_1 = __importDefault(require("ajv"));
28
8
  const ajv_formats_1 = __importDefault(require("ajv-formats"));
9
+ const lodash_1 = __importDefault(require("lodash"));
29
10
  function Main(object, schema) {
30
11
  if (typeof schema !== "object")
31
12
  throw Error("object type isnt an object");
@@ -33,7 +14,7 @@ function Main(object, schema) {
33
14
  const ajv = new ajv_1.default({ allowUnionTypes: true, coerceTypes: true, useDefaults: true });
34
15
  (0, ajv_formats_1.default)(ajv);
35
16
  const validate = ajv.compile(schema);
36
- const obj = _.pick(object, [...FIELDS]);
17
+ const obj = lodash_1.default.pick(object, [...FIELDS]);
37
18
  if (!validate(obj)) {
38
19
  console.log("Config object data validate failed", obj);
39
20
  console.log(JSON.stringify(validate.errors, null, 2));
@@ -12,6 +12,8 @@ interface Deps {
12
12
  * @param cnf
13
13
  * @returns An instance of ioredis
14
14
  */
15
- export declare function Main(cnf: Cnf, deps: Deps): Redis.Redis;
15
+ export declare function Main(cnf: Cnf, deps: Deps): Redis.Redis & {
16
+ update: (key: string, data: string) => Promise<void>;
17
+ };
16
18
  export declare const Deps: string[];
17
19
  export {};
@@ -11,7 +11,19 @@ exports.Deps = exports.Main = void 0;
11
11
  function Main(cnf, deps) {
12
12
  const { redis } = cnf;
13
13
  const { IORedis } = deps;
14
- return new IORedis(redis);
14
+ const rds = new IORedis(redis);
15
+ /**
16
+ * 在不改变原数据的有效性、过期时间的前提下更新数据
17
+ * @param key redis 存储的 key
18
+ * @param data redis 要更新的数据
19
+ */
20
+ const update = async (key, data) => {
21
+ const ttl = await rds.ttl(key);
22
+ if (ttl < 1)
23
+ return;
24
+ await rds.setex(key, ttl, data);
25
+ };
26
+ return Object.assign(rds, { update });
15
27
  }
16
28
  exports.Main = Main;
17
29
  exports.Deps = ["IORedis"];
@@ -14,7 +14,7 @@ interface Deps {
14
14
  /** 对 params 的处理函数 */
15
15
  declare type Handler = (params: any) => void;
16
16
  /** 对执行结构的处理 */
17
- declare type ResHandler = (results: any, res: restify.Response) => void;
17
+ declare type ResHandler = (results: any, res: restify.Response, params?: any) => void;
18
18
  export declare function Router(deps: Deps): {
19
19
  get: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
20
20
  post: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
@@ -74,7 +74,7 @@ function Router(deps) {
74
74
  // eslint-disable-next-line max-params
75
75
  function register(verb, route, methodPath, code = 200, isList = false, handler, resHandler) {
76
76
  /**
77
- * 暂存起来,提供给apis接口来用
77
+ * 暂存起来,提供给apis接口来
78
78
  * apis接口用来返回当前 services 提供的可用的 api
79
79
  */
80
80
  apis.push(`[${verb.toUpperCase()}] ${route} Domain: ${methodPath}`);
@@ -97,7 +97,7 @@ function Router(deps) {
97
97
  if (results === null || results === undefined)
98
98
  results = "Ok";
99
99
  if (resHandler) {
100
- resHandler(results, res);
100
+ resHandler(results, res, params);
101
101
  }
102
102
  else if (isList) {
103
103
  const { _ignoreTotal, _format } = params;
@@ -165,13 +165,18 @@ function BridgeSocket(io, domain) {
165
165
  return next();
166
166
  });
167
167
  // 掉线
168
- client.on("disconnect", () => {
168
+ client.on("disconnect", async () => {
169
169
  if (!client.profile)
170
170
  return;
171
171
  if (!client.inited)
172
172
  return;
173
173
  // 这里要取消对领域消息的监听
174
- unsubscribe(client.profile, client);
174
+ try {
175
+ return await unsubscribe(client.profile, client);
176
+ }
177
+ catch (e) {
178
+ console.error(e);
179
+ }
175
180
  });
176
181
  });
177
182
  }
@@ -12,7 +12,7 @@ export declare const md5: (str: {
12
12
  * @param type "strong" or "normal" or other string that custom character range string
13
13
  */
14
14
  export declare function randStr(len: number, type: "strong"): string;
15
- export declare function randStr(len: number, type: "normal"): string;
15
+ export declare function randStr(len: number, type?: "normal"): string;
16
16
  export declare function randStr(len: number, type: string): string;
17
17
  /**
18
18
  * Replace line breaks and tabs in the string with ordinary spaces
@@ -81,4 +81,19 @@ export declare const modifiyURL: (address: string, adds?: Params | undefined, re
81
81
  * @param intervalMS 间隔多久判断一次, 单位毫秒 默认 100
82
82
  */
83
83
  export declare const waitFor: (test: () => boolean, intervalMS?: number) => Promise<void>;
84
+ /**
85
+ * 读取录下的所有文件,之后返回数组
86
+ * params
87
+ * dir 要加载的目录
88
+ * exts 要加载的模块文件后缀,多个可以是数组, 默认为 coffee
89
+ * excludes 要排除的文件, 默认排除 index
90
+ */
91
+ /**
92
+ * 读取录下的所有文件,之后返回数组
93
+ * @param dir 要读取的目录
94
+ * @param exts 要读取的文件后缀,不包含 (.) 点,例如 jpg 而非 .jpg
95
+ * @param excludes 要排除的文件列表
96
+ * @param files 读取到的文件路径存放地址
97
+ */
98
+ export declare const deepReaddir: (dir: string, exts: Set<string>, excludes?: Set<string>, files?: string[]) => string[];
84
99
  export {};
@@ -22,9 +22,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.waitFor = exports.modifiyURL = exports.inExpired = exports.tryCatchLog = exports.deepFreeze = exports.sleep = exports.lcfirst = exports.ucfirst = exports.nt2space = exports.randStr = exports.md5 = void 0;
25
+ exports.deepReaddir = exports.waitFor = exports.modifiyURL = exports.inExpired = exports.tryCatchLog = exports.deepFreeze = exports.sleep = exports.lcfirst = exports.ucfirst = exports.nt2space = exports.randStr = exports.md5 = void 0;
26
26
  const async_1 = __importDefault(require("async"));
27
27
  const crypto = __importStar(require("crypto"));
28
+ const fs_1 = __importDefault(require("fs"));
29
+ const path_1 = __importDefault(require("path"));
28
30
  /** 随机字符串字典 */
29
31
  const RAND_STR_DICT = {
30
32
  normal: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
@@ -44,9 +46,8 @@ function randStr(len, type = "normal") {
44
46
  const dict = type === "strong" || type === "normal" ? RAND_STR_DICT[type] : type;
45
47
  const { length } = dict;
46
48
  /** 随机字符串的长度不能等于 0 或者负数 */
47
- len |= 0;
48
- len = Math.max(len, 3);
49
- return Array(len)
49
+ const _len = Math.max(3, len | 0);
50
+ return Array(_len)
50
51
  .fill("")
51
52
  .map(() => dict[Math.floor(Math.random() * length)])
52
53
  .join("");
@@ -162,3 +163,40 @@ const waitFor = async (test, intervalMS = 100) => {
162
163
  }, async () => test());
163
164
  };
164
165
  exports.waitFor = waitFor;
166
+ /**
167
+ * 读取录下的所有文件,之后返回数组
168
+ * params
169
+ * dir 要加载的目录
170
+ * exts 要加载的模块文件后缀,多个可以是数组, 默认为 coffee
171
+ * excludes 要排除的文件, 默认排除 index
172
+ */
173
+ /**
174
+ * 读取录下的所有文件,之后返回数组
175
+ * @param dir 要读取的目录
176
+ * @param exts 要读取的文件后缀,不包含 (.) 点,例如 jpg 而非 .jpg
177
+ * @param excludes 要排除的文件列表
178
+ * @param files 读取到的文件路径存放地址
179
+ */
180
+ const deepReaddir = (dir, exts, excludes = new Set(), files = []) => {
181
+ for (const x of fs_1.default.readdirSync(dir)) {
182
+ const file = path_1.default.resolve(dir, x);
183
+ const stat = fs_1.default.lstatSync(file);
184
+ if (stat.isFile()) {
185
+ // 忽略隐藏文件
186
+ if (x[0] === ".")
187
+ continue;
188
+ const arr = x.split(".");
189
+ const ext = arr.pop();
190
+ const name = arr.join(".");
191
+ // 如果是不希望的后缀或者排除的名称,则直接忽略/跳过
192
+ if ((ext && !exts.has(ext)) || excludes.has(name))
193
+ continue;
194
+ files.push(file);
195
+ }
196
+ else if (stat.isDirectory()) {
197
+ (0, exports.deepReaddir)(file, exts, excludes, files);
198
+ }
199
+ }
200
+ return files;
201
+ };
202
+ exports.deepReaddir = deepReaddir;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.2.2",
3
+ "version": "0.2.5",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {