@domain.js/main 0.7.15 → 1.0.0-alpha.10

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.
Files changed (76) hide show
  1. package/.husky/pre-commit +1 -7
  2. package/.prettierignore +34 -0
  3. package/.prettierrc +11 -0
  4. package/.vscode/extensions.json +7 -0
  5. package/.vscode/settings.json +27 -1
  6. package/dist/Errors/index.d.ts +1 -1
  7. package/dist/Errors/index.js +1 -2
  8. package/dist/basic-errors.d.ts +1 -1
  9. package/dist/basic-errors.js +1 -0
  10. package/dist/cfg/index.js +1 -2
  11. package/dist/cli/index.js +22 -8
  12. package/dist/defaults.d.ts +0 -30
  13. package/dist/defaults.js +22 -18
  14. package/dist/deps/aes/index.js +63 -14
  15. package/dist/deps/cache/index.d.ts +29 -4
  16. package/dist/deps/cache/index.js +34 -13
  17. package/dist/deps/counter/index.js +2 -2
  18. package/dist/deps/cron/index.d.ts +3 -3
  19. package/dist/deps/cron/index.js +4 -4
  20. package/dist/deps/defines.d.ts +0 -2
  21. package/dist/deps/defines.js +38 -26
  22. package/dist/deps/frequency/index.js +2 -2
  23. package/dist/deps/graceful/index.d.ts +1 -1
  24. package/dist/deps/graceful/index.js +2 -2
  25. package/dist/deps/hash/index.js +2 -2
  26. package/dist/deps/logger/index.d.ts +1 -9
  27. package/dist/deps/logger/index.js +8 -8
  28. package/dist/deps/myCia/errors.d.ts +1 -1
  29. package/dist/deps/myCia/errors.js +22 -8
  30. package/dist/deps/myCia/index.d.ts +2 -10
  31. package/dist/deps/myCia/index.js +15 -12
  32. package/dist/deps/parallel/index.d.ts +1 -8
  33. package/dist/deps/parallel/index.js +31 -16
  34. package/dist/deps/redis/index.js +2 -2
  35. package/dist/deps/request/index.d.ts +2 -2
  36. package/dist/deps/request/index.js +2 -2
  37. package/dist/deps/rest/Before.d.ts +2 -2
  38. package/dist/deps/rest/Before.js +1 -2
  39. package/dist/deps/rest/index.d.ts +12 -19
  40. package/dist/deps/rest/index.js +4 -5
  41. package/dist/deps/rest/stats.d.ts +1 -9
  42. package/dist/deps/rest/stats.js +1 -2
  43. package/dist/deps/rest/utils.d.ts +3 -17
  44. package/dist/deps/rest/utils.js +77 -59
  45. package/dist/deps/schema/index.d.ts +2 -2
  46. package/dist/deps/schema/index.js +24 -10
  47. package/dist/deps/sequelize/index.d.ts +3 -3
  48. package/dist/deps/sequelize/index.js +2 -2
  49. package/dist/deps/signer/index.js +24 -10
  50. package/dist/dm/index.d.ts +1 -1
  51. package/dist/dm/index.js +24 -11
  52. package/dist/http/defines.d.ts +13 -0
  53. package/dist/http/index.d.ts +11 -3
  54. package/dist/http/index.js +53 -50
  55. package/dist/http/router.d.ts +20 -20
  56. package/dist/http/router.js +42 -97
  57. package/dist/http/socket.d.ts +2 -2
  58. package/dist/http/socket.js +1 -2
  59. package/dist/http/utils.d.ts +13 -7
  60. package/dist/http/utils.js +104 -24
  61. package/dist/index.d.ts +20 -61
  62. package/dist/index.js +30 -12
  63. package/dist/types/index.d.ts +2 -2
  64. package/dist/utils/index.d.ts +3 -3
  65. package/dist/utils/index.js +24 -10
  66. package/eslint.config.js +117 -0
  67. package/package.json +59 -55
  68. package/.eslintrc.js +0 -33
  69. package/dist/deps/cache/After.d.ts +0 -2
  70. package/dist/deps/cache/After.js +0 -29
  71. package/dist/deps/cache/Before.d.ts +0 -4
  72. package/dist/deps/cache/Before.js +0 -16
  73. package/dist/deps/cache/Define.d.ts +0 -30
  74. package/dist/deps/cache/Define.js +0 -2
  75. package/dist/deps/checker/index.d.ts +0 -15
  76. package/dist/deps/checker/index.js +0 -24
package/.husky/pre-commit CHANGED
@@ -1,8 +1,2 @@
1
- #!/bin/sh
2
- if [ ! -f "$(dirname "$0")/_/husky.sh" ]; then
3
- npx husky install
4
- fi
5
- . "$(dirname "$0")/_/husky.sh"
6
-
7
1
  ts-node src/cli/index.ts loadDeps ./src/deps ts && npm run lint-staged && npm run build
8
- npm tst
2
+ # npm tst
@@ -0,0 +1,34 @@
1
+ # Dependencies
2
+ node_modules/
3
+ dist/
4
+ coverage/
5
+
6
+ # Build outputs
7
+ *.min.js
8
+ *.min.css
9
+
10
+ # Lock files
11
+ package-lock.json
12
+ yarn.lock
13
+ pnpm-lock.yaml
14
+
15
+ # Generated files
16
+ *.d.ts
17
+
18
+ # Logs
19
+ *.log
20
+
21
+ # OS generated files
22
+ .DS_Store
23
+ Thumbs.db
24
+
25
+ # IDE files
26
+ .vscode/
27
+ .idea/
28
+
29
+ # Test coverage
30
+ coverage/
31
+
32
+ # Temporary files
33
+ *.tmp
34
+ *.temp
package/.prettierrc ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "printWidth": 100,
3
+ "tabWidth": 2,
4
+ "useTabs": false,
5
+ "semi": true,
6
+ "singleQuote": true,
7
+ "trailingComma": "all",
8
+ "bracketSpacing": true,
9
+ "arrowParens": "avoid",
10
+ "endOfLine": "lf"
11
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "recommendations": [
3
+ "esbenp.prettier-vscode",
4
+ "dbaeumer.vscode-eslint",
5
+ "ms-vscode.vscode-typescript-next"
6
+ ]
7
+ }
@@ -11,5 +11,31 @@
11
11
  "other": true,
12
12
  "comments": true,
13
13
  "strings": true
14
- }
14
+ },
15
+ "editor.formatOnSave": true,
16
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
17
+ "[typescript]": {
18
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
19
+ "editor.formatOnSave": true
20
+ },
21
+ "[javascript]": {
22
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
23
+ "editor.formatOnSave": true
24
+ },
25
+ "[json]": {
26
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
27
+ "editor.formatOnSave": true
28
+ },
29
+ "[jsonc]": {
30
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
31
+ "editor.formatOnSave": true
32
+ },
33
+ "editor.codeActionsOnSave": {
34
+ "source.fixAll.eslint": "explicit",
35
+ "source.organizeImports": "explicit"
36
+ },
37
+ "eslint.validate": [
38
+ "javascript",
39
+ "typescript"
40
+ ]
15
41
  }
@@ -6,7 +6,7 @@ export interface MyError extends Error {
6
6
  export interface ErrorFn {
7
7
  (...args: any): MyError;
8
8
  }
9
- declare type RemoveReadonlyArray<T extends readonly any[]> = T extends readonly (infer A)[] ? A extends readonly [infer key, any] ? key : never : never;
9
+ type RemoveReadonlyArray<T extends readonly any[]> = T extends readonly (infer A)[] ? A extends readonly [infer key, any] ? key : never : never;
10
10
  /**
11
11
  * Convert error configuration to errors function
12
12
  * @param defines error configuration
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Errors = void 0;
3
+ exports.Errors = Errors;
4
4
  /**
5
5
  * Convert error configuration to errors function
6
6
  * @param defines error configuration
@@ -24,4 +24,3 @@ function Errors(defines) {
24
24
  }
25
25
  return errors;
26
26
  }
27
- exports.Errors = Errors;
@@ -1 +1 @@
1
- export declare const errors: Readonly<Record<"notFound" | "notAllowed" | "noAuth", import("./Errors").ErrorFn>>;
1
+ export declare const errors: Readonly<Record<"notFound" | "notAllowed" | "noAuth" | "resourceDuplicateAdd", import("./Errors").ErrorFn>>;
@@ -6,5 +6,6 @@ const defines = [
6
6
  ["notFound", "Resource not found"],
7
7
  ["notAllowed", "No access"],
8
8
  ["noAuth", "Not authentication"],
9
+ ["resourceDuplicateAdd", "Resource duplicate add"],
9
10
  ];
10
11
  exports.errors = Object.freeze((0, Errors_1.Errors)(defines));
package/dist/cfg/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Main = void 0;
6
+ exports.Main = Main;
7
7
  const ajv_1 = __importDefault(require("ajv"));
8
8
  const ajv_formats_1 = __importDefault(require("ajv-formats"));
9
9
  const ajv_keywords_1 = __importDefault(require("ajv-keywords"));
@@ -28,4 +28,3 @@ function Main(object, schema) {
28
28
  return obj[key];
29
29
  };
30
30
  }
31
- exports.Main = Main;
package/dist/cli/index.js CHANGED
@@ -2,7 +2,11 @@
2
2
  "use strict";
3
3
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
4
  if (k2 === undefined) k2 = k;
5
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
6
10
  }) : (function(o, m, k, k2) {
7
11
  if (k2 === undefined) k2 = k;
8
12
  o[k2] = m[k];
@@ -12,13 +16,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
12
16
  }) : function(o, v) {
13
17
  o["default"] = v;
14
18
  });
15
- var __importStar = (this && this.__importStar) || function (mod) {
16
- if (mod && mod.__esModule) return mod;
17
- var result = {};
18
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
19
- __setModuleDefault(result, mod);
20
- return result;
21
- };
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
22
36
  Object.defineProperty(exports, "__esModule", { value: true });
23
37
  const child_process_1 = require("child_process");
24
38
  const fs = __importStar(require("fs"));
@@ -1,31 +1,16 @@
1
1
  import * as ajv from "ajv";
2
2
  import * as ajvFormats from "ajv-formats";
3
3
  import ajvKeywords from "ajv-keywords";
4
- import * as async from "async";
5
- import * as axios from "axios";
6
4
  import * as cronParser from "cron-parser";
7
5
  import humanInterval from "human-interval";
8
6
  import IORedis from "ioredis";
9
- import _ from "lodash";
10
7
  import LRU from "lru-cache";
11
- import moment from "moment";
12
8
  import * as mysql from "mysql2";
13
9
  import * as Sequelize from "sequelize";
14
- import * as uuid from "uuid";
15
10
  import { errors } from "./basic-errors";
16
11
  import * as utils from "./utils";
17
12
  /** npm packages injection */
18
13
  export interface Defaults {
19
- /**
20
- * The Lodash library exported as Node.js modules.
21
- * @link https://www.npmjs.com/package/lodash
22
- */
23
- _: typeof _;
24
- /**
25
- * For the creation of RFC4122 UUIDs
26
- * @link https://www.npmjs.com/package/uuid
27
- */
28
- uuid: typeof uuid;
29
14
  /**
30
15
  * Ajv JSON schema validator
31
16
  * @link https://www.npmjs.com/package/ajv
@@ -37,17 +22,6 @@ export interface Defaults {
37
22
  */
38
23
  ajvFormats: typeof ajvFormats;
39
24
  ajvKeywords: typeof ajvKeywords;
40
- /**
41
- * Async is a utility module which provides straight-forward
42
- * powerful functions for working with asynchronous JavaScript.
43
- * @link https://www.npmjs.com/package/async
44
- */
45
- async: typeof async;
46
- /**
47
- * Promise based HTTP client for the browser and node.js
48
- * @Link https://www.npmjs.com/package/axios
49
- */
50
- axios: typeof axios;
51
25
  /**
52
26
  * Node.js library for parsing and manipulating crontab instructions.
53
27
  * It includes support for timezones and DST transitions.
@@ -84,10 +58,6 @@ export interface Defaults {
84
58
  * @link https://www.npmjs.com/package/sequelize
85
59
  */
86
60
  Sequelize: typeof Sequelize;
87
- /**
88
- * A JavaScript date library for parsing, validating, manipulating, and formatting dates.
89
- */
90
- moment: typeof moment;
91
61
  /**
92
62
  * utils tools function
93
63
  */
package/dist/defaults.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -11,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
11
15
  }) : function(o, v) {
12
16
  o["default"] = v;
13
17
  });
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
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
21
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
22
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
37
  };
@@ -26,34 +40,24 @@ exports.defaults = void 0;
26
40
  const ajv = __importStar(require("ajv"));
27
41
  const ajvFormats = __importStar(require("ajv-formats"));
28
42
  const ajv_keywords_1 = __importDefault(require("ajv-keywords"));
29
- const async = __importStar(require("async"));
30
- const axios = __importStar(require("axios"));
31
43
  const cronParser = __importStar(require("cron-parser"));
32
44
  const human_interval_1 = __importDefault(require("human-interval"));
33
45
  const ioredis_1 = __importDefault(require("ioredis"));
34
- const lodash_1 = __importDefault(require("lodash"));
35
46
  const lru_cache_1 = __importDefault(require("lru-cache"));
36
- const moment_1 = __importDefault(require("moment"));
37
47
  const mysql = __importStar(require("mysql2"));
38
48
  const Sequelize = __importStar(require("sequelize"));
39
- const uuid = __importStar(require("uuid"));
40
49
  const basic_errors_1 = require("./basic-errors");
41
50
  const utils = __importStar(require("./utils"));
42
51
  exports.defaults = {
43
- _: lodash_1.default,
44
- uuid,
45
52
  ajv,
46
53
  ajvFormats,
47
54
  ajvKeywords: ajv_keywords_1.default,
48
- async,
49
- axios,
50
55
  cronParser,
51
56
  humanInterval: human_interval_1.default,
52
57
  IORedis: ioredis_1.default,
53
58
  LRU: lru_cache_1.default,
54
59
  mysql,
55
60
  Sequelize,
56
- moment: moment_1.default,
57
61
  utils,
58
62
  U: utils,
59
63
  errors: basic_errors_1.errors,
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -11,25 +15,70 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
11
15
  }) : function(o, v) {
12
16
  o["default"] = v;
13
17
  });
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
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
21
35
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.Main = void 0;
23
- const AES = __importStar(require("crypto-js/aes"));
24
- const cryptojs = __importStar(require("crypto-js/core"));
36
+ exports.Main = Main;
37
+ const crypto = __importStar(require("crypto"));
25
38
  function Main() {
26
39
  // aes-256-cbc encrypt
27
- const encrypt = (message, key) => AES.encrypt(message, key).toString();
40
+ const encrypt = (message, key) => {
41
+ // 生成随机 IV
42
+ const iv = crypto.randomBytes(16);
43
+ // 使用 PBKDF2 从密钥生成固定长度的密钥
44
+ const derivedKey = crypto.pbkdf2Sync(key, "salt", 1000, 32, "sha256");
45
+ // 创建加密器
46
+ const cipher = crypto.createCipheriv("aes-256-cbc", derivedKey, iv);
47
+ cipher.setAutoPadding(true);
48
+ // 加密
49
+ let encrypted = cipher.update(message, "utf8", "hex");
50
+ encrypted += cipher.final("hex");
51
+ // 返回 IV + 加密数据的组合
52
+ const result = iv.toString("hex") + ":" + encrypted;
53
+ return result;
54
+ };
28
55
  // aes-256-cbc decrypt
29
- const decrypt = (message, key) => AES.decrypt(message, key).toString(cryptojs.enc.Utf8);
56
+ const decrypt = (message, key) => {
57
+ try {
58
+ // 分离 IV 和加密数据
59
+ const parts = message.split(":");
60
+ if (parts.length !== 2) {
61
+ throw new Error("Invalid encrypted message format");
62
+ }
63
+ const iv = Buffer.from(parts[0], "hex");
64
+ const encrypted = parts[1];
65
+ // 使用相同的 PBKDF2 生成密钥
66
+ const derivedKey = crypto.pbkdf2Sync(key, "salt", 1000, 32, "sha256");
67
+ // 创建解密器
68
+ const decipher = crypto.createDecipheriv("aes-256-cbc", derivedKey, iv);
69
+ decipher.setAutoPadding(true);
70
+ // 解密
71
+ let decrypted = decipher.update(encrypted, "hex", "utf8");
72
+ decrypted += decipher.final("utf8");
73
+ return decrypted;
74
+ }
75
+ catch (error) {
76
+ const errorMessage = error instanceof Error ? error.message : "Unknown error";
77
+ throw new Error("Decryption failed: " + errorMessage);
78
+ }
79
+ };
30
80
  return Object.freeze({
31
81
  encrypt,
32
82
  decrypt,
33
83
  });
34
84
  }
35
- exports.Main = Main;
@@ -1,5 +1,30 @@
1
- import { Cache, CnfDef, DepsDef, PubSubDef } from "./Define";
2
- export { After } from "./After";
3
- export { Before } from "./Before";
4
- export declare function Main(cnf: CnfDef, deps: DepsDef, pubsub?: PubSubDef): Cache;
1
+ import { Redis, RedisOptions } from "ioredis";
2
+ import { LRUCache } from "lru-cache";
3
+ export interface CnfDef {
4
+ cache: {
5
+ isMulti?: boolean;
6
+ delSignalChannel?: string;
7
+ } & LRUCache.Options<{}, {}, unknown>;
8
+ redis?: RedisOptions;
9
+ }
10
+ export interface DepsDef {
11
+ IORedis: typeof Redis;
12
+ logger: {
13
+ info: (message: string, extra?: any) => void;
14
+ error: (error: Error, extra?: any) => void;
15
+ };
16
+ }
17
+ export interface Cache extends LRUCache<{}, {}, unknown> {
18
+ caching: <T extends (...args: any[]) => Promise<any>>(func: T, life: number, getKey: (...args: Parameters<T>) => string, hit?: (hited: boolean) => void) => T;
19
+ /**
20
+ * @deprecated use delete instead of del
21
+ */
22
+ del: (key: string) => boolean;
23
+ hitCount: () => {
24
+ hits: number;
25
+ misseds: number;
26
+ };
27
+ needToBroad: boolean;
28
+ }
5
29
  export declare const Deps: string[];
30
+ export declare function Main(cnf: CnfDef, deps: DepsDef): Cache;
@@ -1,15 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Deps = exports.Main = exports.Before = exports.After = void 0;
4
- var After_1 = require("./After");
5
- Object.defineProperty(exports, "After", { enumerable: true, get: function () { return After_1.After; } });
6
- var Before_1 = require("./Before");
7
- Object.defineProperty(exports, "Before", { enumerable: true, get: function () { return Before_1.Before; } });
8
- function Main(cnf, deps, pubsub) {
3
+ exports.Deps = void 0;
4
+ exports.Main = Main;
5
+ const lru_cache_1 = require("lru-cache");
6
+ exports.Deps = ["logger", "LRU", "IORedis"];
7
+ function Main(cnf, deps) {
9
8
  let hits = 0; // 击中次数
10
9
  let misseds = 0; // 未击中次数
11
- const { LRU } = deps;
12
- const lru = new LRU(cnf.cache);
10
+ const { logger, IORedis } = deps;
11
+ const lru = new lru_cache_1.LRUCache(cnf.cache);
13
12
  const isFunction = (arg) => typeof arg === "function";
14
13
  function caching(func, life, getKey, hit) {
15
14
  if (!isFunction(func))
@@ -32,19 +31,41 @@ function Main(cnf, deps, pubsub) {
32
31
  hit(false);
33
32
  misseds += 1;
34
33
  const res = await func(...args);
35
- lru.set(key, res, life);
34
+ lru.set(key, res, { ttl: life });
36
35
  return res;
37
36
  };
38
37
  return wrapped;
39
38
  }
40
39
  const hitCount = () => ({ hits, misseds });
41
- const needToBroad = Boolean(pubsub);
40
+ const needToBroad = Boolean(cnf.cache.isMulti);
41
+ // 如果不是多节点分部署部署,则不需要处理
42
+ // 开启多节点分布式部署后,要通过redis广播cache的del事件,依次来保持cache的有效性
43
+ if (needToBroad && cnf.redis) {
44
+ const pub = new IORedis(cnf.redis);
45
+ const sub = new IORedis(cnf.redis);
46
+ const { delSignalChannel = "LRU_DEL_SIGNAL_CHANNEL" } = cnf.cache;
47
+ sub.subscribe(delSignalChannel, (err, count) => {
48
+ logger.info("cache.redis.subscribe", { chanels: delSignalChannel, count });
49
+ if (err)
50
+ return logger.error(err);
51
+ return logger.info(`cache.redis.subscribe succeed, channel count: ${count}`);
52
+ });
53
+ const del = lru.delete.bind(lru);
54
+ lru.delete = (key) => {
55
+ del(key);
56
+ pub.publish(delSignalChannel, key);
57
+ return true;
58
+ };
59
+ sub.on("message", async (channel, key) => {
60
+ if (channel === delSignalChannel)
61
+ del(key);
62
+ });
63
+ }
42
64
  const cache = Object.assign(lru, {
65
+ del: lru.delete.bind(lru),
43
66
  caching,
44
67
  hitCount,
45
- needToBroad,
68
+ needToBroad: Boolean(cnf.cache.isMulti),
46
69
  });
47
70
  return cache;
48
71
  }
49
- exports.Main = Main;
50
- exports.Deps = ["logger", "LRU", "IORedis"];
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Deps = exports.Main = void 0;
3
+ exports.Deps = void 0;
4
+ exports.Main = Main;
4
5
  function Main(cnf, deps) {
5
6
  const { counter: { key: REDIS_KEY }, } = cnf;
6
7
  const { redis } = deps;
@@ -38,5 +39,4 @@ function Main(cnf, deps) {
38
39
  };
39
40
  return { mget, get, set, incr, decr };
40
41
  }
41
- exports.Main = Main;
42
42
  exports.Deps = ["redis"];
@@ -1,4 +1,4 @@
1
- import * as cronParser from "cron-parser";
1
+ import { CronExpressionParser } from "cron-parser";
2
2
  import human = require("human-interval");
3
3
  interface Cnf {
4
4
  cron?: {
@@ -14,7 +14,7 @@ interface callbackArg {
14
14
  cronJob: [Error | null, any, number];
15
15
  }
16
16
  interface Deps {
17
- cronParser: typeof cronParser;
17
+ cronParser: typeof CronExpressionParser;
18
18
  humanInterval: typeof human;
19
19
  myCia: {
20
20
  regist: (name: string, validator: any, waiters: waiter[]) => void;
@@ -28,7 +28,7 @@ interface Deps {
28
28
  */
29
29
  export declare const timeout: (fn: Function, timeoutMS: number) => void;
30
30
  export declare function Main(cnf: Cnf, deps: Deps): {
31
- regist: (name: string, intervalStr: string, startAt?: string | undefined) => void;
31
+ regist: (name: string, intervalStr: string, startAt?: string) => void;
32
32
  start: () => void;
33
33
  getStats: () => {
34
34
  name: string;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Deps = exports.Main = exports.timeout = void 0;
3
+ exports.Deps = exports.timeout = void 0;
4
+ exports.Main = Main;
4
5
  /**
5
6
  * 定时执行函数, 解决 setTimeout 第二个参数不能大于 2147483647 的问题
6
7
  * @param fn 要执行的函数
@@ -20,7 +21,7 @@ exports.timeout = timeout;
20
21
  function Main(cnf, deps) {
21
22
  const { cron = {} } = cnf;
22
23
  const ciaTaskType = "cronJob";
23
- const { myCia, humanInterval: human, cronParser: parser } = deps;
24
+ const { myCia, humanInterval: human, cronParser: CronParser } = deps;
24
25
  const { tz = "Asia/Shanghai" } = cron;
25
26
  // 注册信息
26
27
  const registed = {};
@@ -28,7 +29,7 @@ function Main(cnf, deps) {
28
29
  let startedAt;
29
30
  // 计算具体下次执行还有多少毫秒
30
31
  const calcNextMS = (intervalStr) => {
31
- const interval = human(intervalStr) || parser.parseExpression(intervalStr, { tz });
32
+ const interval = human(intervalStr) || CronParser.parse(intervalStr, { tz });
32
33
  if (typeof interval === "number")
33
34
  return interval;
34
35
  // * * * * * *
@@ -108,5 +109,4 @@ function Main(cnf, deps) {
108
109
  };
109
110
  return { regist, start, getStats };
110
111
  }
111
- exports.Main = Main;
112
112
  exports.Deps = ["myCia", "humanInterval", "cronParser"];
@@ -1,6 +1,5 @@
1
1
  import * as aes from "./aes";
2
2
  import * as cache from "./cache";
3
- import * as checker from "./checker";
4
3
  import * as counter from "./counter";
5
4
  import * as cron from "./cron";
6
5
  import * as frequency from "./frequency";
@@ -18,7 +17,6 @@ import * as signer from "./signer";
18
17
  declare const _default: {
19
18
  aes: typeof aes;
20
19
  cache: typeof cache;
21
- checker: typeof checker;
22
20
  counter: typeof counter;
23
21
  cron: typeof cron;
24
22
  frequency: typeof frequency;