@leyyo/common 1.3.19 → 1.3.21

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 (127) hide show
  1. package/dist/base/index.types.d.ts +67 -0
  2. package/dist/base/index.types.js +32 -0
  3. package/dist/base/leyyo.d.ts +3 -0
  4. package/dist/base/leyyo.js +18 -7
  5. package/dist/class/index.types.d.ts +20 -0
  6. package/dist/class/list.d.ts +6 -0
  7. package/dist/class/list.js +6 -0
  8. package/dist/class/logger.instance.d.ts +5 -0
  9. package/dist/class/logger.instance.js +13 -0
  10. package/dist/common/deploy.common.d.ts +16 -0
  11. package/dist/common/deploy.common.js +17 -0
  12. package/dist/common/enum.pool.d.ts +10 -0
  13. package/dist/common/enum.pool.js +14 -0
  14. package/dist/common/error.common.d.ts +30 -0
  15. package/dist/common/error.common.js +40 -0
  16. package/dist/common/error.pool.d.ts +9 -0
  17. package/dist/common/error.pool.js +10 -0
  18. package/dist/common/event.common.d.ts +8 -0
  19. package/dist/common/event.common.js +16 -3
  20. package/dist/common/index.types.d.ts +432 -0
  21. package/dist/common/index.types.js +1 -0
  22. package/dist/common/inert.d.ts +48 -0
  23. package/dist/common/inert.js +29 -0
  24. package/dist/common/lifecycle.common.d.ts +14 -0
  25. package/dist/common/lifecycle.common.js +16 -0
  26. package/dist/common/literal.pool.d.ts +11 -0
  27. package/dist/common/literal.pool.js +15 -0
  28. package/dist/common/log.common.d.ts +54 -0
  29. package/dist/common/log.common.js +96 -6
  30. package/dist/common/repo.common.d.ts +188 -0
  31. package/dist/common/repo.common.js +211 -0
  32. package/dist/const/index.d.ts +6 -0
  33. package/dist/const/index.js +7 -0
  34. package/dist/enum/log-level.d.ts +3 -0
  35. package/dist/error/caused.error.d.ts +10 -0
  36. package/dist/error/caused.error.js +10 -0
  37. package/dist/error/developer.error.d.ts +8 -0
  38. package/dist/error/developer.error.js +10 -0
  39. package/dist/error/http.error.d.ts +13 -0
  40. package/dist/error/http.error.js +7 -0
  41. package/dist/error/index.types.d.ts +149 -0
  42. package/dist/error/invalid-value.error.d.ts +3 -0
  43. package/dist/error/invalid-value.error.js +3 -0
  44. package/dist/error/leyyo.error.d.ts +33 -0
  45. package/dist/error/leyyo.error.js +36 -0
  46. package/dist/error/multiple.error.d.ts +6 -0
  47. package/dist/error/multiple.error.js +7 -0
  48. package/dist/function/define-loader.d.ts +6 -0
  49. package/dist/function/define-loader.js +6 -0
  50. package/dist/function/delay.d.ts +8 -0
  51. package/dist/function/delay.js +9 -0
  52. package/dist/function/delete-prop.d.ts +21 -0
  53. package/dist/function/delete-prop.js +22 -0
  54. package/dist/function/empty-fn.d.ts +6 -0
  55. package/dist/function/empty-fn.js +7 -0
  56. package/dist/function/extended-type.d.ts +6 -0
  57. package/dist/function/extended-type.js +7 -0
  58. package/dist/function/get-fqn.d.ts +6 -0
  59. package/dist/function/get-fqn.js +7 -0
  60. package/dist/function/get-prop.d.ts +21 -0
  61. package/dist/function/get-prop.js +21 -0
  62. package/dist/function/get-stat.d.ts +20 -0
  63. package/dist/function/has-fqn.d.ts +6 -0
  64. package/dist/function/has-fqn.js +7 -0
  65. package/dist/function/index.d.ts +1 -0
  66. package/dist/function/index.js +1 -0
  67. package/dist/function/is-class.d.ts +6 -0
  68. package/dist/function/is-class.js +18 -0
  69. package/dist/function/is-empty.d.ts +11 -0
  70. package/dist/function/is-empty.js +11 -0
  71. package/dist/function/is-filled-arr.d.ts +9 -0
  72. package/dist/function/is-filled-arr.js +9 -0
  73. package/dist/function/is-filled-obj.d.ts +9 -0
  74. package/dist/function/is-filled-obj.js +9 -0
  75. package/dist/function/is-obj.d.ts +8 -0
  76. package/dist/function/is-obj.js +8 -0
  77. package/dist/function/is-test.d.ts +8 -0
  78. package/dist/function/is-test.js +14 -0
  79. package/dist/function/is-text.d.ts +10 -0
  80. package/dist/function/is-text.js +10 -0
  81. package/dist/function/jitter-interval.d.ts +9 -0
  82. package/dist/function/jitter-interval.js +16 -0
  83. package/dist/function/load-config.d.ts +19 -0
  84. package/dist/function/load-config.js +52 -1
  85. package/dist/function/on-fqn-set.js +1 -0
  86. package/dist/function/one-or-more.d.ts +6 -0
  87. package/dist/function/one-or-more.js +7 -0
  88. package/dist/function/opt-add.d.ts +8 -0
  89. package/dist/function/opt-add.js +8 -0
  90. package/dist/function/opt-append.d.ts +7 -0
  91. package/dist/function/opt-append.js +7 -0
  92. package/dist/function/opt-check.d.ts +6 -0
  93. package/dist/function/opt-check.js +7 -0
  94. package/dist/function/opt-clone.d.ts +6 -0
  95. package/dist/function/opt-clone.js +7 -0
  96. package/dist/function/opt-field.d.ts +8 -0
  97. package/dist/function/opt-field.js +8 -0
  98. package/dist/function/opt-fn.d.ts +6 -0
  99. package/dist/function/opt-fn.js +7 -0
  100. package/dist/function/random-test-no.d.ts +5 -0
  101. package/dist/function/random-test-no.js +5 -0
  102. package/dist/function/remove-fqn.d.ts +8 -0
  103. package/dist/function/remove-fqn.js +11 -1
  104. package/dist/function/run-exporter.d.ts +5 -0
  105. package/dist/function/run-exporter.js +14 -0
  106. package/dist/function/secure-json.d.ts +35 -0
  107. package/dist/function/secure-json.js +44 -0
  108. package/dist/function/set-anonymous-fqn.js +2 -0
  109. package/dist/function/set-fqn.d.ts +7 -0
  110. package/dist/function/set-fqn.js +8 -0
  111. package/dist/function/set-prop.d.ts +24 -0
  112. package/dist/function/set-prop.js +24 -0
  113. package/dist/function/stamp-loader.d.ts +6 -0
  114. package/dist/function/stamp-loader.js +6 -0
  115. package/dist/function/test-case.d.ts +7 -0
  116. package/dist/function/test-case.js +7 -0
  117. package/dist/function/test-name.d.ts +7 -0
  118. package/dist/function/test-name.js +7 -0
  119. package/dist/function/times.d.ts +6 -0
  120. package/dist/function/times.js +12 -0
  121. package/dist/function/trigger-fqn.js +1 -0
  122. package/dist/index.foretell.js +3 -0
  123. package/dist/index.js +1 -0
  124. package/dist/index.loader.js +8 -1
  125. package/dist/sys/leyyo-storage.d.ts +7 -0
  126. package/dist/sys/leyyo-storage.js +23 -0
  127. package/package.json +1 -1
@@ -1,12 +1,20 @@
1
1
  import { FQN } from "../internal.js";
2
2
  import { getFqn, isFilledArr, isFilledObj, isObj, isText, onFqnSet, setAnonymousName, setFqn, testCase, } from "../function/index.js";
3
3
  const where = `${FQN}.Inert`;
4
+ // noinspection JSUnusedGlobalSymbols
4
5
  export class Inert {
5
6
  leyyo;
6
7
  cluster;
7
8
  options;
8
9
  _repo;
9
10
  static _pool;
11
+ /**
12
+ * Constructor
13
+ *
14
+ * @param {LeyyoLike} leyyo
15
+ * @param {string} cluster
16
+ * @param {InertBuildOpt} options
17
+ * */
10
18
  constructor(leyyo, cluster, options) {
11
19
  this.leyyo = leyyo;
12
20
  this.cluster = cluster;
@@ -37,6 +45,7 @@ export class Inert {
37
45
  };
38
46
  Inert._pool.set(this.cluster, this._repo);
39
47
  }
48
+ // region private
40
49
  _inFqnStage(item) {
41
50
  item.full = getFqn(item.target);
42
51
  if (!item.full || !item.full.includes(".")) {
@@ -113,15 +122,19 @@ export class Inert {
113
122
  });
114
123
  }
115
124
  }
125
+ // endregion private
126
+ /** @inheritDoc */
116
127
  register(options) {
117
128
  const { uniqueLoaded } = this._repo;
118
129
  if (!isFilledObj(options)) {
119
130
  throw new this.leyyo.developerError("Invalid inert options", testCase(FQN, "XXX"), where);
120
131
  }
132
+ // target
121
133
  if (this._validate(options.target)) {
122
134
  if (options.lazyTarget) {
123
135
  delete options.lazyTarget;
124
136
  }
137
+ // already defined
125
138
  if (uniqueLoaded.has(options.target)) {
126
139
  return;
127
140
  }
@@ -146,10 +159,12 @@ export class Inert {
146
159
  this._onFqnCompleted(item);
147
160
  this._afterTargetFound(item);
148
161
  }
162
+ // lazy target
149
163
  else if (options.lazyTarget instanceof Promise) {
150
164
  if (!isText(options.name)) {
151
165
  throw new this.leyyo.developerError("Invalid inert name", testCase(FQN, "XXX"), where);
152
166
  }
167
+ // it's already pending to be loaded
153
168
  const { pendingLazy } = this._repo;
154
169
  if (pendingLazy.has(options.name)) {
155
170
  return;
@@ -161,27 +176,35 @@ export class Inert {
161
176
  throw new this.leyyo.developerError(`Invalid target or lazy target [${options.name}]`, testCase(FQN, 224), where);
162
177
  }
163
178
  }
179
+ /** @inheritDoc */
164
180
  lazy(fqn, name, lazyTarget, opt) {
165
181
  this.register({ ...(opt ?? {}), fqn, name, lazyTarget });
166
182
  }
183
+ /** @inheritDoc */
167
184
  isLazy(name) {
168
185
  return this.get(name)?.mode === "lazy";
169
186
  }
187
+ /** @inheritDoc */
170
188
  isInvalid(name) {
171
189
  return ["failed", "conflicted"].includes(this.get(name)?.mode);
172
190
  }
191
+ /** @inheritDoc */
173
192
  isFailed(name) {
174
193
  return this.get(name)?.mode === "failed";
175
194
  }
195
+ /** @inheritDoc */
176
196
  isConflicted(name) {
177
197
  return this.get(name)?.mode === "conflicted";
178
198
  }
199
+ /** @inheritDoc */
179
200
  isEager(name) {
180
201
  return this.get(name)?.mode === "eager";
181
202
  }
203
+ /** @inheritDoc */
182
204
  has(name) {
183
205
  return !!this.get(name);
184
206
  }
207
+ /** @inheritDoc */
185
208
  get(name) {
186
209
  if (!isText(name)) {
187
210
  return undefined;
@@ -193,6 +216,7 @@ export class Inert {
193
216
  }
194
217
  return this.get(name.split(".").pop());
195
218
  }
219
+ // no dot
196
220
  if (pendingFqn.has(name)) {
197
221
  return pendingFqn.get(name);
198
222
  }
@@ -204,6 +228,7 @@ export class Inert {
204
228
  }
205
229
  return basicNames.get(name);
206
230
  }
231
+ /** @inheritDoc */
207
232
  async load(name) {
208
233
  if (!isText(name)) {
209
234
  throw new this.leyyo.developerError(`Invalid lazy name`, testCase(FQN, "ZZZ"), where);
@@ -213,6 +238,7 @@ export class Inert {
213
238
  if (!item) {
214
239
  throw new this.leyyo.developerError(`Lazy was not defined [${name}]`, testCase(FQN, "ZZZ"), where);
215
240
  }
241
+ // It was already loaded
216
242
  if (item.mode === "eager") {
217
243
  return item;
218
244
  }
@@ -221,10 +247,12 @@ export class Inert {
221
247
  if (this._validate(item.target)) {
222
248
  item.mode = "eager";
223
249
  delete item.lazyTarget;
250
+ // remove from pending
224
251
  if (pendingLazy.has(name)) {
225
252
  pendingLazy.delete(name);
226
253
  }
227
254
  await this._nextLoad(item);
255
+ // already loaded
228
256
  if (uniqueLoaded.has(item.target)) {
229
257
  return item;
230
258
  }
@@ -237,6 +265,7 @@ export class Inert {
237
265
  item.mode = "failed";
238
266
  new this.leyyo.developerError(`Callback inert during loading lazy class [${name}]`, testCase(FQN, 227), where).log(e);
239
267
  }
268
+ // file could not be loaded
240
269
  if (!item.target) {
241
270
  return undefined;
242
271
  }
@@ -1,12 +1,26 @@
1
1
  import { LifecycleCommonLike, LifecycleSortLambda, LifecycleStage } from "./index.types.js";
2
2
  import { Fnc, LeyyoLike } from "../base/index.js";
3
+ /**
4
+ * Lifecycle common interface
5
+ * */
3
6
  export declare class LifecycleCommon implements LifecycleCommonLike {
4
7
  private leyyo;
5
8
  private _stages;
6
9
  private _sortLambda;
10
+ /**
11
+ * Constructor
12
+ * */
7
13
  constructor(leyyo: LeyyoLike);
14
+ /**
15
+ * Init lifecycle by stage
16
+ *
17
+ * @param {LifecycleStage} stage - stage
18
+ * */
8
19
  private _init;
20
+ /** @inheritDoc */
9
21
  addStage(stage: LifecycleStage, name: string, callback: Fnc): void;
22
+ /** @inheritDoc */
10
23
  runStage(stage: LifecycleStage, ...params: Array<unknown>): Promise<number>;
24
+ /** @inheritDoc */
11
25
  setOrderLambda(stage: LifecycleStage, lambda: LifecycleSortLambda): void;
12
26
  }
@@ -1,13 +1,21 @@
1
1
  import { FQN } from "../internal.js";
2
2
  import { isText, testCase } from "../function/index.js";
3
3
  const where = `${FQN}.LifecycleFn`;
4
+ // noinspection JSUnusedGlobalSymbols
5
+ /**
6
+ * Lifecycle common interface
7
+ * */
4
8
  export class LifecycleCommon {
5
9
  leyyo;
6
10
  _stages;
7
11
  _sortLambda;
12
+ /**
13
+ * Constructor
14
+ * */
8
15
  constructor(leyyo) {
9
16
  this.leyyo = leyyo;
10
17
  this._stages = this.leyyo.repoCommon.newMap(`${where}.stages`);
18
+ // initialize stages
11
19
  this._init("initialize");
12
20
  this._init("print");
13
21
  this._init("validate");
@@ -17,10 +25,16 @@ export class LifecycleCommon {
17
25
  this._init("ota-after");
18
26
  this._init("kill");
19
27
  }
28
+ /**
29
+ * Init lifecycle by stage
30
+ *
31
+ * @param {LifecycleStage} stage - stage
32
+ * */
20
33
  _init(stage) {
21
34
  const newMap = this.leyyo.repoCommon.newMap(`${where}.${stage}`);
22
35
  this._stages.set(stage, newMap);
23
36
  }
37
+ /** @inheritDoc */
24
38
  addStage(stage, name, callback) {
25
39
  if (!isText(stage)) {
26
40
  throw new this.leyyo.developerError("Invalid lifecycle stage", testCase(FQN, 100), where);
@@ -40,6 +54,7 @@ export class LifecycleCommon {
40
54
  }
41
55
  item.get(name).push(callback);
42
56
  }
57
+ /** @inheritDoc */
43
58
  async runStage(stage, ...params) {
44
59
  if (!isText(stage)) {
45
60
  throw new this.leyyo.developerError("Invalid lifecycle stage", testCase(FQN, 104), where);
@@ -80,6 +95,7 @@ export class LifecycleCommon {
80
95
  }
81
96
  return count;
82
97
  }
98
+ /** @inheritDoc */
83
99
  setOrderLambda(stage, lambda) {
84
100
  if (!isText(stage)) {
85
101
  throw new this.leyyo.developerError("Invalid lifecycle stage", testCase(FQN, 108), where);
@@ -1,15 +1,26 @@
1
1
  import { Inert } from "./inert.js";
2
2
  import { Literal, LiteralItemConfig, LiteralPoolItem, LiteralPoolLike, LiteralPoolOpt } from "./index.types.js";
3
3
  import { LeyyoLike } from "../base/index.js";
4
+ /**
5
+ * Literal pool for call with name and lazy loading
6
+ * */
4
7
  export declare class LiteralPool extends Inert<LiteralPoolItem, Literal, LiteralPoolOpt> implements LiteralPoolLike {
5
8
  protected leyyo: LeyyoLike;
6
9
  constructor(leyyo: LeyyoLike);
10
+ /** @inheritDoc */
7
11
  protected _getName(lit: Literal): string;
12
+ /** @inheritDoc */
8
13
  protected _nextLoad(item: LiteralPoolItem): Promise<void>;
14
+ /** @inheritDoc */
9
15
  protected _setName(lit: Literal, name: string): string;
16
+ /** @inheritDoc */
10
17
  protected _afterTargetFound(item: LiteralPoolItem): void;
18
+ /** @inheritDoc */
11
19
  protected _validate(lit: Literal): boolean;
20
+ /** @inheritDoc */
12
21
  setConfigItem(lit: Literal, conf: LiteralItemConfig): void;
22
+ /** @inheritDoc */
13
23
  getConfigItem(lit: Literal): LiteralItemConfig;
24
+ /** @inheritDoc */
14
25
  define(fqn: string, name: string, target: Literal, opt?: Omit<LiteralPoolOpt, "name" | "target" | "lazyTarget" | "fqn">): void;
15
26
  }
@@ -3,15 +3,22 @@ import { getSymbol, isEmpty, isFilledArr, isFilledObj, isObj, isText, setSymbol,
3
3
  import { FQN } from "../internal.js";
4
4
  import { KEY_LITERAL_ALT, KEY_LITERAL_I18N, KEY_LITERAL_NAME } from "../const/index.js";
5
5
  const where = `${FQN}.LiteralPool`;
6
+ // noinspection JSUnusedGlobalSymbols
7
+ /**
8
+ * Literal pool for call with name and lazy loading
9
+ * */
6
10
  export class LiteralPool extends Inert {
7
11
  leyyo;
8
12
  constructor(leyyo) {
9
13
  super(leyyo, "literal", {});
10
14
  this.leyyo = leyyo;
11
15
  }
16
+ // region protected
17
+ /** @inheritDoc */
12
18
  _getName(lit) {
13
19
  return this.getConfigItem(lit)?.name;
14
20
  }
21
+ /** @inheritDoc */
15
22
  async _nextLoad(item) {
16
23
  if (item.target && item.lazyAlt) {
17
24
  try {
@@ -25,6 +32,7 @@ export class LiteralPool extends Inert {
25
32
  }
26
33
  delete item.lazyAlt;
27
34
  }
35
+ /** @inheritDoc */
28
36
  _setName(lit, name) {
29
37
  if (isText(name)) {
30
38
  setSymbol(lit, KEY_LITERAL_NAME, name);
@@ -32,6 +40,7 @@ export class LiteralPool extends Inert {
32
40
  }
33
41
  return undefined;
34
42
  }
43
+ /** @inheritDoc */
35
44
  _afterTargetFound(item) {
36
45
  if (item.lazyTarget) {
37
46
  delete item.lazyTarget;
@@ -41,9 +50,13 @@ export class LiteralPool extends Inert {
41
50
  }
42
51
  this.setConfigItem(item.target, item);
43
52
  }
53
+ /** @inheritDoc */
44
54
  _validate(lit) {
45
55
  return isFilledArr(lit);
46
56
  }
57
+ // endregion protected
58
+ // region public
59
+ /** @inheritDoc */
47
60
  setConfigItem(lit, conf) {
48
61
  if (!isObj(conf)) {
49
62
  return;
@@ -58,6 +71,7 @@ export class LiteralPool extends Inert {
58
71
  setSymbol(lit, KEY_LITERAL_ALT, conf.alt);
59
72
  }
60
73
  }
74
+ /** @inheritDoc */
61
75
  getConfigItem(lit) {
62
76
  if (!this._validate(lit)) {
63
77
  return undefined;
@@ -68,6 +82,7 @@ export class LiteralPool extends Inert {
68
82
  alt: getSymbol(lit, KEY_LITERAL_ALT),
69
83
  };
70
84
  }
85
+ /** @inheritDoc */
71
86
  define(fqn, name, target, opt) {
72
87
  this.register({ ...(opt ?? {}), fqn, name, target });
73
88
  }
@@ -4,7 +4,15 @@ import { Opt } from "../function/index.js";
4
4
  import { ContextFinderLambda, LogCommonLike, LogFormatterLambda, Logger, LogStylerLambda } from "./index.types.js";
5
5
  export declare class LogCommon implements LogCommonLike {
6
6
  private leyyo;
7
+ /**
8
+ * Loggers
9
+ * */
7
10
  private _loggers;
11
+ /**
12
+ * Local console colors
13
+ *
14
+ * @type {LocalColorLike}
15
+ * */
8
16
  private _localColor;
9
17
  private _logFormatter;
10
18
  private _logDeploymentStyler;
@@ -12,16 +20,62 @@ export declare class LogCommon implements LogCommonLike {
12
20
  private _logStyler;
13
21
  private _contextFinder;
14
22
  constructor(leyyo: LeyyoLike);
23
+ /**
24
+ * Set formatter
25
+ *
26
+ * @param {function} fn - lambda for formatter
27
+ * */
15
28
  setLogFormatter(fn: LogFormatterLambda): void;
29
+ /**
30
+ * Set deployment styler
31
+ *
32
+ * @param {function} fn - lambda for styler
33
+ * */
16
34
  setLogDeploymentStyler(fn: LogStylerLambda): void;
35
+ /**
36
+ * Set local style
37
+ *
38
+ * @param {function} fn - lambda for styler
39
+ * */
17
40
  setLogLocalStyler(fn: LogStylerLambda): void;
41
+ /**
42
+ * Set local style
43
+ *
44
+ * @param {function} fn - lambda for styler
45
+ * */
18
46
  setContextFinder(fn: ContextFinderLambda): void;
19
47
  private _defaults;
48
+ /**
49
+ * Build short style of logger name
50
+ *
51
+ * @param {string} where - original logger name
52
+ * @param {string} - short style
53
+ * */
20
54
  private _shortenWhere;
55
+ /**
56
+ * Log consumer
57
+ *
58
+ * @param {LogItem} item
59
+ * */
21
60
  private _consumeLog;
61
+ /**
62
+ * Generate random logger name
63
+ *
64
+ * @return {string}
65
+ * */
22
66
  private _randomLoggerName;
67
+ /**
68
+ * Check logger name's uniqueness, and loop till unique name
69
+ *
70
+ * @param {string} name - base name
71
+ * @param {number} index - try count
72
+ * @return {string}
73
+ * */
23
74
  private _checkLoggerName;
75
+ /** @inheritDoc */
24
76
  of(value: ClassLike | Fnc | Obj | string): Logger;
77
+ /** @inheritDoc */
25
78
  initConsume(): void;
79
+ /** @inheritDoc */
26
80
  emitLog(level: LogLevel, where: string, message: any, params?: any | Opt): void;
27
81
  }
@@ -3,21 +3,31 @@ import { getFqn, hasFqn, isEmpty, isFilledObj, isObj, onFqnSet, secureJson, test
3
3
  import { KEY_LOG_ALREADY } from "../const/index.js";
4
4
  const where = `${FQN}.LogCommon`;
5
5
  const emptyWhere = "".padStart(20);
6
+ // noinspection JSUnusedGlobalSymbols
6
7
  export class LogCommon {
7
8
  leyyo;
9
+ // region properties
10
+ /**
11
+ * Loggers
12
+ * */
8
13
  _loggers;
14
+ /**
15
+ * Local console colors
16
+ *
17
+ * @type {LocalColorLike}
18
+ * */
9
19
  _localColor = {
10
20
  bold: "\x1b[1m",
11
21
  normal: "\x1b[21m",
12
22
  end: "\x1b[0m",
13
23
  param: "\x1b[90m",
14
24
  levels: {
15
- fatal: [true, "\x1b[31m", "\x1b[91m"],
16
- error: [false, "\x1b[31m", "\x1b[91m"],
17
- warn: [false, "\x1b[35m", "\x1b[95m"],
18
- info: [false, "\x1b[32m", "\x1b[92m"],
19
- debug: [false, "\x1b[33m", "\x1b[93m"],
20
- trace: [false, "\x1b[36m", "\x1b[96m"],
25
+ fatal: [true, "\x1b[31m", "\x1b[91m"], // bold red
26
+ error: [false, "\x1b[31m", "\x1b[91m"], // red
27
+ warn: [false, "\x1b[35m", "\x1b[95m"], // magenta
28
+ info: [false, "\x1b[32m", "\x1b[92m"], // green
29
+ debug: [false, "\x1b[33m", "\x1b[93m"], // yellow
30
+ trace: [false, "\x1b[36m", "\x1b[96m"], // yellow
21
31
  },
22
32
  };
23
33
  _logFormatter;
@@ -25,17 +35,29 @@ export class LogCommon {
25
35
  _logLocalStyler;
26
36
  _logStyler;
27
37
  _contextFinder;
38
+ // endregion properties
28
39
  constructor(leyyo) {
29
40
  this.leyyo = leyyo;
30
41
  this._loggers = this.leyyo.repoCommon.newMap(`${where}.loggers`);
31
42
  this._defaults();
32
43
  }
44
+ // region setters
45
+ /**
46
+ * Set formatter
47
+ *
48
+ * @param {function} fn - lambda for formatter
49
+ * */
33
50
  setLogFormatter(fn) {
34
51
  if (typeof fn !== "function") {
35
52
  throw new this.leyyo.developerError("Invalid log formatter", testCase(FQN, 200), where);
36
53
  }
37
54
  this._logFormatter = fn;
38
55
  }
56
+ /**
57
+ * Set deployment styler
58
+ *
59
+ * @param {function} fn - lambda for styler
60
+ * */
39
61
  setLogDeploymentStyler(fn) {
40
62
  if (typeof fn !== "function") {
41
63
  throw new this.leyyo.developerError("Invalid log styler", testCase(FQN, 201), where);
@@ -45,6 +67,11 @@ export class LogCommon {
45
67
  this._logStyler = this._logDeploymentStyler;
46
68
  }
47
69
  }
70
+ /**
71
+ * Set local style
72
+ *
73
+ * @param {function} fn - lambda for styler
74
+ * */
48
75
  setLogLocalStyler(fn) {
49
76
  if (typeof fn !== "function") {
50
77
  throw new this.leyyo.developerError("Invalid log local styler", testCase(FQN, 202), where);
@@ -54,13 +81,25 @@ export class LogCommon {
54
81
  this._logStyler = this._logLocalStyler;
55
82
  }
56
83
  }
84
+ /**
85
+ * Set local style
86
+ *
87
+ * @param {function} fn - lambda for styler
88
+ * */
57
89
  setContextFinder(fn) {
58
90
  if (typeof fn !== "function") {
59
91
  throw new this.leyyo.developerError("Invalid log local styler", testCase(FQN, 202), where);
60
92
  }
61
93
  this._contextFinder = fn;
62
94
  }
95
+ // endregion setters
96
+ // region private
63
97
  _defaults() {
98
+ /**
99
+ * Default log formatter
100
+ *
101
+ * @param {LogItem} item
102
+ * */
64
103
  this._logFormatter = (item) => {
65
104
  if (item?.ctx) {
66
105
  const ctx = item.ctx;
@@ -75,6 +114,12 @@ export class LogCommon {
75
114
  delete item.paramStr;
76
115
  }
77
116
  };
117
+ /**
118
+ * Default log deployment styler (on server)
119
+ *
120
+ * @param {LogItem} item
121
+ * @return {string}
122
+ * */
78
123
  this._logDeploymentStyler = (item) => {
79
124
  let message = item.now;
80
125
  if (item?.ctx) {
@@ -106,6 +151,12 @@ export class LogCommon {
106
151
  message += " " + item.message;
107
152
  return message + (item.paramStr ? " ~^~ " + item.paramStr : "");
108
153
  };
154
+ /**
155
+ * Default log locale styler (on local computer)
156
+ *
157
+ * @param {LogItem} item
158
+ * @return {string}
159
+ * */
109
160
  this._logLocalStyler = (item) => {
110
161
  const { bold, end, param } = this._localColor;
111
162
  const [isBold, regular, light] = this._localColor.levels[item.level] ?? this._localColor.levels.debug;
@@ -133,6 +184,9 @@ export class LogCommon {
133
184
  message += ` ${isBold ? bold : ""}${light}${item.message}${end}`;
134
185
  return message + (item.paramStr ? ` ~^~ ${param}${item.paramStr}${end}` : "");
135
186
  };
187
+ /**
188
+ * Set current styler by environment
189
+ * */
136
190
  if (process.env["NODE_ENV"] === "local") {
137
191
  this._logStyler = this._logLocalStyler;
138
192
  }
@@ -140,6 +194,12 @@ export class LogCommon {
140
194
  this._logStyler = this._logDeploymentStyler;
141
195
  }
142
196
  }
197
+ /**
198
+ * Build short style of logger name
199
+ *
200
+ * @param {string} where - original logger name
201
+ * @param {string} - short style
202
+ * */
143
203
  _shortenWhere(where) {
144
204
  if (!where) {
145
205
  return undefined;
@@ -154,6 +214,11 @@ export class LogCommon {
154
214
  }
155
215
  return where;
156
216
  }
217
+ /**
218
+ * Log consumer
219
+ *
220
+ * @param {LogItem} item
221
+ * */
157
222
  _consumeLog(item) {
158
223
  if (!isObj(item)) {
159
224
  return;
@@ -163,12 +228,14 @@ export class LogCommon {
163
228
  this._logFormatter(item);
164
229
  }
165
230
  catch (_e) {
231
+ // nothing
166
232
  }
167
233
  let message;
168
234
  try {
169
235
  message = this._logStyler(item);
170
236
  }
171
237
  catch (_e) {
238
+ // nothing
172
239
  }
173
240
  if (typeof message !== "string") {
174
241
  message = `${item?.now} - ${typeof item.message === "string" ? item.message : secureJson(item.message)}`;
@@ -178,15 +245,30 @@ export class LogCommon {
178
245
  }
179
246
  console[item.level](message);
180
247
  }
248
+ /**
249
+ * Generate random logger name
250
+ *
251
+ * @return {string}
252
+ * */
181
253
  _randomLoggerName() {
182
254
  return this._checkLoggerName("Logger", Math.floor(Math.random() * 1000));
183
255
  }
256
+ /**
257
+ * Check logger name's uniqueness, and loop till unique name
258
+ *
259
+ * @param {string} name - base name
260
+ * @param {number} index - try count
261
+ * @return {string}
262
+ * */
184
263
  _checkLoggerName(name, index) {
185
264
  if (this._loggers.has(name)) {
186
265
  return this._checkLoggerName(name, index + 1);
187
266
  }
188
267
  return name + (index === 0) ? "" : `(#${index})`;
189
268
  }
269
+ // endregion private
270
+ // region public
271
+ /** @inheritDoc */
190
272
  of(value) {
191
273
  let name;
192
274
  let fqn;
@@ -214,15 +296,19 @@ export class LogCommon {
214
296
  this._loggers.set(name, ins);
215
297
  return ins;
216
298
  }
299
+ /** @inheritDoc */
217
300
  initConsume() {
301
+ // bind to context finder
218
302
  this.leyyo.eventCommon.listen("context:set-finder", (v) => {
219
303
  if (typeof v === "function") {
220
304
  this._contextFinder = v;
221
305
  }
222
306
  });
307
+ // bind to event emitter
223
308
  this.leyyo.eventCommon.listen("log", (v) => this._consumeLog(v));
224
309
  this.initConsume = () => { };
225
310
  }
311
+ /** @inheritDoc */
226
312
  emitLog(level, where, message, params) {
227
313
  const item = {
228
314
  level,
@@ -240,6 +326,7 @@ export class LogCommon {
240
326
  err[KEY_LOG_ALREADY] = true;
241
327
  }
242
328
  catch (_e) {
329
+ // nothing
243
330
  }
244
331
  item.message = this.leyyo.errorCommon.text(message);
245
332
  item.params = this.leyyo.errorCommon.toJsonBasic(message, params);
@@ -258,6 +345,7 @@ export class LogCommon {
258
345
  message[KEY_LOG_ALREADY] = true;
259
346
  }
260
347
  catch (_e) {
348
+ // nothing
261
349
  }
262
350
  }
263
351
  message = secureJson(message);
@@ -279,6 +367,7 @@ export class LogCommon {
279
367
  }
280
368
  }
281
369
  catch (_e) {
370
+ // nothing
282
371
  }
283
372
  }
284
373
  if (this._contextFinder) {
@@ -286,6 +375,7 @@ export class LogCommon {
286
375
  item.ctx = this._contextFinder(item.params?.req);
287
376
  }
288
377
  catch (_e) {
378
+ // nothing
289
379
  }
290
380
  }
291
381
  this.leyyo.eventCommon.emit("log", item);