@domain.js/main 0.3.27 → 0.4.3

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.
@@ -20,8 +20,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
22
  exports.Main = void 0;
23
- const cryptojs = __importStar(require("crypto-js/core"));
24
23
  const AES = __importStar(require("crypto-js/aes"));
24
+ const cryptojs = __importStar(require("crypto-js/core"));
25
25
  function Main() {
26
26
  // aes-256-cbc encrypt
27
27
  const encrypt = (message, key) => AES.encrypt(message, key).toString();
@@ -1,2 +1,2 @@
1
- import { CnfDef, DepsDef, PubSubDef, Cache } from "./Define";
1
+ import { Cache, CnfDef, DepsDef, PubSubDef } from "./Define";
2
2
  export declare const After: (lru: Pick<Cache, "del">, cnf: CnfDef, deps: Pick<DepsDef, "logger">, pubsub?: PubSubDef | undefined) => void;
@@ -18,6 +18,7 @@ const After = (lru, cnf, deps, pubsub) => {
18
18
  });
19
19
  const del = lru.del.bind(lru);
20
20
  lru.del = (key) => {
21
+ del(key);
21
22
  pub.publish(delSignalChannel, key);
22
23
  };
23
24
  sub.on("message", async (channel, key) => {
@@ -21,6 +21,12 @@ interface Deps {
21
21
  submit: (name: string, times: number, callback: (arg: callbackArg) => void) => void;
22
22
  };
23
23
  }
24
+ /**
25
+ * 定时执行函数, 解决 setTimeout 第二个参数不能大于 2147483647 的问题
26
+ * @param fn 要执行的函数
27
+ * @param timeoutMS 执行间隔时间,单位毫秒
28
+ */
29
+ export declare const timeout: (fn: Function, timeoutMS: number) => void;
24
30
  export declare function Main(cnf: Cnf, deps: Deps): {
25
31
  regist: (name: string, intervalStr: string, startAt?: string | undefined) => void;
26
32
  start: () => void;
@@ -1,6 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Deps = exports.Main = void 0;
3
+ exports.Deps = exports.Main = exports.timeout = void 0;
4
+ /**
5
+ * 定时执行函数, 解决 setTimeout 第二个参数不能大于 2147483647 的问题
6
+ * @param fn 要执行的函数
7
+ * @param timeoutMS 执行间隔时间,单位毫秒
8
+ */
9
+ const timeout = (fn, timeoutMS) => {
10
+ if (timeoutMS < 2147483647) {
11
+ setTimeout(fn, timeoutMS);
12
+ }
13
+ else {
14
+ setTimeout(() => {
15
+ (0, exports.timeout)(fn, timeoutMS - 2147483647);
16
+ }, 2147483647);
17
+ }
18
+ };
19
+ exports.timeout = timeout;
4
20
  function Main(cnf, deps) {
5
21
  const { cron = {} } = cnf;
6
22
  const ciaTaskType = "cronJob";
@@ -37,7 +53,7 @@ function Main(cnf, deps) {
37
53
  throw Error("startAt 定义不合法");
38
54
  timeoutMS = startAt;
39
55
  }
40
- setTimeout(() => {
56
+ (0, exports.timeout)(() => {
41
57
  opt.times += 1;
42
58
  opt.triggeredAt = Date.now();
43
59
  myCia.submit(`Cron::${name}`, opt.times, ({ cronJob: [err, , totalMS] }) => {
@@ -8,7 +8,7 @@ interface Deps {
8
8
  redis: Pick<Redis, "hget" | "hset" | "hdel" | "hincrby">;
9
9
  }
10
10
  export declare function Main(cnf: Cnf, deps: Deps): {
11
- get: (key: string) => Promise<number>;
11
+ get: (key: string) => Promise<string | null>;
12
12
  set: (key: string, value: string) => Promise<number>;
13
13
  del: (key: string) => Promise<number>;
14
14
  incr: (key: string, step?: number) => Promise<number>;
@@ -5,8 +5,7 @@ function Main(cnf, deps) {
5
5
  const { hash: { key: REDIS_KEY }, } = cnf;
6
6
  const { redis } = deps;
7
7
  const get = async (key) => {
8
- const num = await redis.hget(REDIS_KEY, key);
9
- return Number(num) | 0;
8
+ return redis.hget(REDIS_KEY, key);
10
9
  };
11
10
  const set = (key, value) => redis.hset(REDIS_KEY, key, value);
12
11
  const del = (key) => redis.hdel(REDIS_KEY, key);
@@ -1,13 +1,22 @@
1
1
  import { Model, Options, Sequelize } from "sequelize";
2
2
  interface Cnf {
3
3
  sequelize: {
4
- [propName: string]: Options;
4
+ [propName: string]: Options & {
5
+ /** 是否开启账号密码aes加密,以增强安全性 */
6
+ security?: boolean;
7
+ };
8
+ };
9
+ aes: {
10
+ key: string;
5
11
  };
6
12
  }
7
13
  interface Deps {
8
14
  Sequelize: {
9
15
  Sequelize: typeof Sequelize;
10
16
  };
17
+ aes: {
18
+ decrypt: (str: string, key: string) => string;
19
+ };
11
20
  }
12
21
  export declare const Deps: string[];
13
22
  declare type NonConstructorKeys<T> = {
@@ -2,14 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ModelBase = exports.Main = exports.Deps = void 0;
4
4
  const sequelize_1 = require("sequelize");
5
- exports.Deps = ["Sequelize"];
5
+ exports.Deps = ["Sequelize", "aes"];
6
6
  function Main(cnf, deps) {
7
7
  // 这里之所以要注入 Sequelize 是为了保证项目自身可以灵活选择自己的 Sequelize 版本, 这样改公共模块就会更加稳定, 避免频繁升级
8
- const { sequelize: dbs } = cnf;
9
- const { Sequelize } = deps;
8
+ const { sequelize: dbs, aes: { key: AES_KEY }, } = cnf;
9
+ const { Sequelize, aes } = deps;
10
10
  const sequelizes = {};
11
11
  for (const k of Object.keys(dbs)) {
12
12
  const db = dbs[k];
13
+ if (db.security) {
14
+ db.username = aes.decrypt(db.username, AES_KEY);
15
+ db.password = aes.decrypt(db.password, AES_KEY);
16
+ }
13
17
  sequelizes[k] = new Sequelize.Sequelize(db);
14
18
  }
15
19
  return sequelizes;
@@ -9,6 +9,10 @@ export declare type Client = Socket<DefaultEventsMap, DefaultEventsMap, DefaultE
9
9
  profile?: ReturnType<typeof makeProfile>;
10
10
  inited?: boolean;
11
11
  roomId?: string;
12
+ methods?: Record<string, Function>;
13
+ operator?: any;
14
+ /** 退出房间回调函数 */
15
+ quit?: Function;
12
16
  };
13
17
  declare const makeProfile: (client: Client, type: string | undefined, auth: string | Signature, extra?: Profile["extra"]) => Profile;
14
18
  export declare function BridgeSocket(io: Server, domain: Domain): void;
@@ -105,10 +105,7 @@ function BridgeSocket(io, domain) {
105
105
  }
106
106
  catch (e) {
107
107
  client.inited = false;
108
- if (e instanceof MyError) {
109
- client.emit("internalError", e.message, e.code || "unknown");
110
- return;
111
- }
108
+ client.emit("internalError", e.code || "unknown", e.message, e.data);
112
109
  console.error(e);
113
110
  }
114
111
  });
@@ -116,44 +113,40 @@ function BridgeSocket(io, domain) {
116
113
  try {
117
114
  if (!client.profile || !client.inited)
118
115
  return;
119
- const res = await entrance({ ...client.profile, roomId }, client);
116
+ const ret = await entrance({ ...client.profile, roomId }, client);
120
117
  client.profile.roomId = roomId;
121
118
  client.roomId = roomId;
122
- client.emit("entranced", res);
119
+ Object.assign(client, ret);
120
+ client.emit("entranced", { ...ret, methods: Object.keys(ret.methods) });
123
121
  }
124
122
  catch (e) {
125
- if (e instanceof MyError) {
126
- client.emit("internalError", e.message, e.code || "unknown");
127
- return;
128
- }
123
+ client.emit("internalError", e.code || "unknown", e.message, e.data);
129
124
  console.error(e);
130
125
  }
131
126
  });
132
127
  client.use(async ([name, params, responseId], next) => {
133
128
  if (name === "init" || name === "entrance")
134
129
  return next();
135
- const { method } = domain[name];
130
+ if (!client.methods)
131
+ throw new MyError("没有允许执行的方法", "请先进入房间");
132
+ const method = client.methods[name];
136
133
  try {
137
134
  if (!method)
138
- throw new MyError("notFound", "不存在该领域方法");
135
+ throw new MyError("notFound", `不存在该领域方法: ${name}`);
139
136
  if (!client.profile)
140
137
  throw new MyError("noAuth", "请先执行 init");
141
- const res = await method({ ...client.profile, method: name }, params);
138
+ const res = await method(...(params || []));
142
139
  if (responseId) {
143
140
  client.emit("response", responseId, res);
144
141
  }
145
142
  }
146
143
  catch (e) {
147
- if (e instanceof Error) {
148
- if (responseId) {
149
- client.emit("responseError", responseId, e.code, e.message, e.data);
150
- }
151
- else {
152
- client.emit(`${name}Error`, e.code, e.message, e.data);
153
- }
144
+ if (responseId) {
145
+ console.error(e);
146
+ client.emit("responseError", responseId, e.code, e.message, e.data);
154
147
  }
155
148
  else {
156
- console.error(e);
149
+ client.emit("internalError", e.code, e.message, e.data);
157
150
  }
158
151
  }
159
152
  return next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.27",
3
+ "version": "0.4.3",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {