@anjianshi/utils 2.4.14 → 2.4.16

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.
@@ -49,9 +49,9 @@ export declare class FileHandler extends LogHandler {
49
49
  protected stringifyDataItem(item: unknown): string;
50
50
  private buffer;
51
51
  private bufferSize;
52
+ private flushTimeoutId;
52
53
  protected pushBuffer(...strings: string[]): void;
53
54
  protected flush(sync?: boolean): void;
54
- protected initFlush(): void;
55
55
  get filepath(): string;
56
56
  protected initLogDir(): void;
57
57
  protected write(content: string, sync?: boolean): void;
@@ -70,7 +70,9 @@ export class FileHandler extends LogHandler {
70
70
  ...(options ?? {}),
71
71
  };
72
72
  this.initLogDir();
73
- this.initFlush();
73
+ // 进程退出前把尚未写入文件的日志强制写入
74
+ // 这里必须用同步的方式来写,不然会写入不进去(可能是因为异步的话是放到下一个事件循环,但进程在这个事件循环内就退出了)
75
+ process.on('exit', () => this.flush(true));
74
76
  }
75
77
  // Format log content
76
78
  log(info) {
@@ -103,16 +105,25 @@ export class FileHandler extends LogHandler {
103
105
  // 利用 util.format() 获得和 console.log() 相同的输出(因为 console.log() 底层也是用的 util.format())
104
106
  return util.format(item);
105
107
  }
106
- // Handle buffer
108
+ // Handle buffer & flush
107
109
  buffer = [];
108
110
  bufferSize = 0;
111
+ flushTimeoutId = null;
109
112
  pushBuffer(...strings) {
110
113
  this.buffer.push(...strings);
111
114
  this.bufferSize = strings.reduce((sum, v) => sum + v.length, this.bufferSize);
112
- if (this.options.flushInterval === 0 || this.bufferSize >= this.options.flushLength)
115
+ if (this.options.flushInterval === 0 || this.bufferSize >= this.options.flushLength) {
113
116
  this.flush();
117
+ }
118
+ else if (!this.flushTimeoutId) {
119
+ this.flushTimeoutId = setTimeout(() => this.flush(), this.options.flushInterval);
120
+ }
114
121
  }
115
122
  flush(sync) {
123
+ if (this.flushTimeoutId) {
124
+ clearTimeout(this.flushTimeoutId);
125
+ this.flushTimeoutId = null;
126
+ }
116
127
  if (this.buffer.length) {
117
128
  const content = this.buffer.join('');
118
129
  this.buffer = [];
@@ -120,14 +131,6 @@ export class FileHandler extends LogHandler {
120
131
  this.write(content, sync);
121
132
  }
122
133
  }
123
- initFlush() {
124
- if (this.options.flushInterval !== 0) {
125
- setInterval(() => this.flush(), this.options.flushInterval);
126
- }
127
- // 进程退出前把尚未写入文件的日志强制写入
128
- // 这里必须用同步的方式来写,不然会写入不进去(可能是因为异步的话是放到下一个事件循环,但进程在这个事件循环内就退出了)
129
- process.on('exit', () => this.flush(true));
130
- }
131
134
  // 文件系统交互 File system interaction
132
135
  get filepath() {
133
136
  const { dir, filePrefix } = this.options;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "2.4.14",
3
+ "version": "2.4.16",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-packages/utils",
6
6
  "bugs": {
@@ -27,10 +27,10 @@
27
27
  "redis": "^4.7.0",
28
28
  "typescript": "^5.6.3",
29
29
  "vconsole": "^3.15.1",
30
+ "@anjianshi/presets-eslint-node": "4.0.15",
31
+ "@anjianshi/presets-eslint-react": "4.0.14",
32
+ "@anjianshi/presets-eslint-typescript": "5.0.12",
30
33
  "@anjianshi/presets-prettier": "3.0.1",
31
- "@anjianshi/presets-eslint-react": "4.0.13",
32
- "@anjianshi/presets-eslint-node": "4.0.14",
33
- "@anjianshi/presets-eslint-typescript": "5.0.11",
34
34
  "@anjianshi/presets-typescript": "3.2.3"
35
35
  },
36
36
  "peerDependencies": {
@@ -2,11 +2,14 @@ import { failed } from '../lang/index.js';
2
2
  import { getValidatorGenerator, } from './base.js';
3
3
  export function getOneOfValidator(options = {}) {
4
4
  return getValidatorGenerator(function validate(field, value) {
5
+ const errors = [];
5
6
  for (const validator of options.validators) {
6
7
  const result = validator(field, value);
7
8
  if (result.success)
8
9
  return result;
10
+ else
11
+ errors.push(result.message);
9
12
  }
10
- return failed(`${field} do not match any valid format`);
13
+ return failed(`${field} do not match any valid format:\n- ` + errors.join('\n- '));
11
14
  })(options);
12
15
  }