@domain.js/main 0.3.22 → 0.3.25

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.
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "typescript.tsdk": "node_modules/typescript/lib",
3
3
  "[markdown]": {
4
- "editor.quickSuggestions": true
4
+ "editor.quickSuggestions": {
5
+ "comments": "on",
6
+ "strings": "on",
7
+ "other": "on"
8
+ }
5
9
  },
6
10
  "editor.quickSuggestions": {
7
11
  "other": true,
@@ -1,7 +1,8 @@
1
- import * as _ from "lodash";
1
+ import _ from "lodash";
2
2
  import { v4 } from "uuid";
3
3
  interface Cnf {
4
4
  logger: {
5
+ level?: "info" | "error" | "none";
5
6
  clientId: string;
6
7
  errorLogPath: string;
7
8
  infoLogPath: string;
@@ -1,37 +1,25 @@
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;
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
20
4
  };
21
5
  Object.defineProperty(exports, "__esModule", { value: true });
22
6
  exports.Deps = exports.Main = void 0;
23
- const fs = __importStar(require("fs"));
24
- const path = __importStar(require("path"));
7
+ const fs_1 = require("fs");
8
+ const promises_1 = __importDefault(require("fs/promises"));
9
+ const path_1 = __importDefault(require("path"));
10
+ const util_1 = require("util");
25
11
  const date = (offset = 0) => new Date(Date.now() + (offset | 0)).toISOString();
26
12
  function Main(cnf, deps) {
27
- const { logger: { errorLogPath, infoLogPath, ignoreErrors, clientId }, } = cnf;
13
+ const { logger: { level, errorLogPath, infoLogPath, ignoreErrors, clientId }, } = cnf;
28
14
  const { _, uuid: { v4: uuid }, } = deps;
29
15
  const makeDir = _.memoize((dir) => {
30
- if (!fs.existsSync(dir))
31
- fs.mkdirSync(dir);
16
+ if (!(0, fs_1.existsSync)(dir))
17
+ (0, fs_1.mkdirSync)(dir);
32
18
  });
33
19
  const ignores = new Set(ignoreErrors);
34
20
  const error = (e, extra) => {
21
+ if (level === "none")
22
+ return;
35
23
  if (!e) {
36
24
  console.trace("Logger.error but error is null or undefined");
37
25
  return;
@@ -42,40 +30,53 @@ function Main(cnf, deps) {
42
30
  // 忽略某些错误
43
31
  if (ignores.has(code))
44
32
  return;
45
- const dir = path.resolve(errorLogPath, today);
33
+ const dir = path_1.default.resolve(errorLogPath, today);
46
34
  makeDir(dir);
47
- const file = path.resolve(dir, `${code}.err`);
35
+ const file = path_1.default.resolve(dir, `${code}.err`);
48
36
  const content = [time, clientId, e.message];
49
- if (e.data)
50
- content.push(JSON.stringify(e.data));
51
- if (extra != null)
52
- content.push(JSON.stringify(extra));
53
- if (e.stack)
54
- content.push(JSON.stringify(e.stack));
55
- try {
56
- fs.appendFileSync(file, `${content.join("\t")}\n`);
37
+ if (e.data) {
38
+ try {
39
+ content.push(JSON.stringify(e.data));
40
+ }
41
+ catch (err) {
42
+ content.push((0, util_1.format)(e.data));
43
+ }
57
44
  }
58
- catch (err) {
59
- console.error("Logger.error appendFileSync faid: %o", err);
45
+ if (extra !== null || extra !== undefined) {
46
+ try {
47
+ content.push(JSON.stringify(extra));
48
+ }
49
+ catch (e) {
50
+ content.push((0, util_1.format)(extra));
51
+ }
60
52
  }
53
+ if (e.stack)
54
+ content.push(JSON.stringify(e.stack));
55
+ promises_1.default.appendFile(file, `${content.join("\t")}\n`).catch(console.error);
61
56
  };
62
57
  const info = (message, extra) => {
58
+ if (level === "none" || level === "error")
59
+ return;
63
60
  const time = date();
64
61
  const today = time.split("T")[0];
65
- const dir = path.resolve(infoLogPath, today);
62
+ const dir = path_1.default.resolve(infoLogPath, today);
66
63
  makeDir(dir);
67
- const file = path.resolve(dir, "info.log");
64
+ const file = path_1.default.resolve(dir, "info.log");
68
65
  const content = [time, clientId, message];
69
- if (extra != null)
70
- content.push(JSON.stringify(extra));
71
- try {
72
- fs.appendFileSync(file, `${content.join("\t")}\n`);
73
- }
74
- catch (e) {
75
- console.error("Logger.info appendFileSync faid: %o", e);
66
+ // eslint-disable-next-line no-eq-null
67
+ if (extra !== null || extra !== undefined) {
68
+ try {
69
+ content.push(JSON.stringify(extra));
70
+ }
71
+ catch (e) {
72
+ content.push((0, util_1.format)(extra));
73
+ }
76
74
  }
75
+ promises_1.default.appendFile(file, `${content.join("\t")}\n`).catch(console.error);
77
76
  };
78
77
  const logger = (fn, name, isAsync = false, transform = (x) => x, errorHandler = (e) => (e instanceof Error ? e.message : ""), argsHandler = JSON.stringify) => {
78
+ if (level === "none")
79
+ return fn;
79
80
  if (isAsync) {
80
81
  const handler = {
81
82
  async apply(fn, me, args) {
@@ -6,7 +6,7 @@ interface Deps {
6
6
  _: typeof _;
7
7
  Sequelize: Pick<typeof Sequelize, "literal" | "and" | "fn">;
8
8
  }
9
- export declare function Stats(cnf: {}, deps: Deps, utils: ReturnType<typeof Utils>): <T extends ModelBase<any, any>>(Model: ModelStatic<T>, params: Record<string, any>, where?: any, conf?: {
9
+ export declare function Stats(_cnf: any, deps: Deps, utils: ReturnType<typeof Utils>): <T extends ModelBase<any, any>>(Model: ModelStatic<T>, params: Record<string, any>, where?: any, conf?: {
10
10
  dimensions?: Record<string, string> | undefined;
11
11
  metrics: Record<string, string>;
12
12
  pagination?: {
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Stats = void 0;
4
- function Stats(cnf, deps, utils) {
4
+ function Stats(_cnf, deps, utils) {
5
5
  const defaultPagination = {
6
6
  maxResults: 10,
7
7
  maxStartIndex: 10000,
@@ -116,15 +116,12 @@ function BridgeSocket(io, domain) {
116
116
  try {
117
117
  if (!client.profile || !client.inited)
118
118
  return;
119
- const res = await entrance({ roomId, ...client.profile }, client);
119
+ const res = await entrance({ ...client.profile, roomId }, client);
120
120
  client.profile.roomId = roomId;
121
121
  client.roomId = roomId;
122
122
  client.emit("entranced", res);
123
123
  }
124
124
  catch (e) {
125
- client.roomId = undefined;
126
- if (client.profile)
127
- client.profile.roomId = undefined;
128
125
  if (e instanceof MyError) {
129
126
  client.emit("internalError", e.message, e.code || "unknown");
130
127
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.22",
3
+ "version": "0.3.25",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {