@anjianshi/utils 2.4.9 → 2.4.11
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.
|
@@ -50,9 +50,9 @@ export declare class FileHandler extends LogHandler {
|
|
|
50
50
|
private buffer;
|
|
51
51
|
private bufferSize;
|
|
52
52
|
protected pushBuffer(...strings: string[]): void;
|
|
53
|
-
protected flush(): void;
|
|
53
|
+
protected flush(sync?: boolean): void;
|
|
54
54
|
protected initFlush(): void;
|
|
55
55
|
get filepath(): string;
|
|
56
56
|
protected initLogDir(): void;
|
|
57
|
-
protected write(content: string): void;
|
|
57
|
+
protected write(content: string, sync?: boolean): void;
|
|
58
58
|
}
|
|
@@ -112,19 +112,21 @@ export class FileHandler extends LogHandler {
|
|
|
112
112
|
if (this.options.flushInterval === 0 || this.bufferSize >= this.options.flushLength)
|
|
113
113
|
this.flush();
|
|
114
114
|
}
|
|
115
|
-
flush() {
|
|
115
|
+
flush(sync) {
|
|
116
116
|
if (this.buffer.length) {
|
|
117
117
|
const content = this.buffer.join('');
|
|
118
118
|
this.buffer = [];
|
|
119
119
|
this.bufferSize = 0;
|
|
120
|
-
this.write(content);
|
|
120
|
+
this.write(content, sync);
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
initFlush() {
|
|
124
124
|
if (this.options.flushInterval !== 0) {
|
|
125
125
|
setInterval(() => this.flush(), this.options.flushInterval);
|
|
126
126
|
}
|
|
127
|
-
|
|
127
|
+
// 进程退出前把尚未写入文件的日志强制写入
|
|
128
|
+
// 这里必须用同步的方式来写,不然会写入不进去(可能是因为异步的话是放到下一个事件循环,但进程在这个事件循环内就退出了)
|
|
129
|
+
process.on('exit', () => this.flush(true));
|
|
128
130
|
}
|
|
129
131
|
// 文件系统交互 File system interaction
|
|
130
132
|
get filepath() {
|
|
@@ -135,10 +137,15 @@ export class FileHandler extends LogHandler {
|
|
|
135
137
|
if (!fs.existsSync(this.options.dir))
|
|
136
138
|
fs.mkdirSync(this.options.dir);
|
|
137
139
|
}
|
|
138
|
-
write(content) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
140
|
+
write(content, sync = false) {
|
|
141
|
+
if (sync) {
|
|
142
|
+
fs.appendFileSync(this.filepath, content);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
fs.appendFile(this.filepath, content, error => {
|
|
146
|
+
if (error)
|
|
147
|
+
console.error('[logger] write failed: ' + String(error));
|
|
148
|
+
});
|
|
149
|
+
}
|
|
143
150
|
}
|
|
144
151
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anjianshi/utils",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.11",
|
|
4
4
|
"description": "Common JavaScript Utils",
|
|
5
5
|
"homepage": "https://github.com/anjianshi/js-packages/utils",
|
|
6
6
|
"bugs": {
|
|
@@ -27,11 +27,11 @@
|
|
|
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.
|
|
31
|
-
"@anjianshi/presets-eslint-
|
|
30
|
+
"@anjianshi/presets-eslint-node": "4.0.14",
|
|
31
|
+
"@anjianshi/presets-eslint-react": "4.0.13",
|
|
32
32
|
"@anjianshi/presets-prettier": "3.0.1",
|
|
33
33
|
"@anjianshi/presets-typescript": "3.2.3",
|
|
34
|
-
"@anjianshi/presets-eslint-
|
|
34
|
+
"@anjianshi/presets-eslint-typescript": "5.0.11"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"@emotion/react": "^11.13.3",
|
package/validators/base.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type PrimitiveType = string | boolean | number | PrimitiveType[] | [...Pr
|
|
|
12
12
|
/**
|
|
13
13
|
* validator 通用参数
|
|
14
14
|
*/
|
|
15
|
-
export interface CommonOptions<
|
|
15
|
+
export interface CommonOptions<Value = unknown> {
|
|
16
16
|
/** 是否允许 null 值 @default false */
|
|
17
17
|
null?: boolean;
|
|
18
18
|
/** 字段是否必须有值(不能是 undefined) @default true */
|
|
@@ -21,7 +21,8 @@ export interface CommonOptions<Defaults = unknown> {
|
|
|
21
21
|
* 默认值,字段无值(或值为 undefined)时生效,值为 null 不会生效。
|
|
22
22
|
* 指定后 required 选项将失去作用。
|
|
23
23
|
*/
|
|
24
|
-
defaults?:
|
|
24
|
+
defaults?: Value;
|
|
25
|
+
custom?: <T extends Value>(input: T) => MaySuccess<T>;
|
|
25
26
|
[key: string]: unknown;
|
|
26
27
|
}
|
|
27
28
|
/**
|
|
@@ -33,6 +34,9 @@ type FullfiledOptions<Options extends Partial<CommonOptions>> = Omit<Options, ke
|
|
|
33
34
|
defaults: Options extends {
|
|
34
35
|
defaults: infer T;
|
|
35
36
|
} ? T : undefined;
|
|
37
|
+
custom: Options extends {
|
|
38
|
+
custom: infer T;
|
|
39
|
+
} ? T : undefined;
|
|
36
40
|
};
|
|
37
41
|
/**
|
|
38
42
|
* 验证完成后能得到的值类型
|
package/validators/base.js
CHANGED
|
@@ -7,7 +7,7 @@ import { success, failed } from '../lang/index.js';
|
|
|
7
7
|
export function getValidatorGenerator(validate) {
|
|
8
8
|
return function validatorGenerator(inputOptions) {
|
|
9
9
|
function validator(field, input) {
|
|
10
|
-
const { null: allowNull = false, required = true, defaults } = inputOptions;
|
|
10
|
+
const { null: allowNull = false, required = true, defaults, custom } = inputOptions;
|
|
11
11
|
if (typeof field !== 'string') {
|
|
12
12
|
input = field;
|
|
13
13
|
field = 'value';
|
|
@@ -25,7 +25,8 @@ export function getValidatorGenerator(validate) {
|
|
|
25
25
|
return failed(`${field} cannot be null`);
|
|
26
26
|
if (value === null || value === undefined)
|
|
27
27
|
return success(value);
|
|
28
|
-
|
|
28
|
+
const validated = validate(field, value, inputOptions);
|
|
29
|
+
return validated.success && custom ? custom(validated.data) : validated;
|
|
29
30
|
}
|
|
30
31
|
return validator;
|
|
31
32
|
};
|
package/validators/factory.js
CHANGED
|
@@ -22,23 +22,23 @@ export function getValidator(definition) {
|
|
|
22
22
|
return getArrayValidator({
|
|
23
23
|
// @ts-ignore 允许递归类型推断
|
|
24
24
|
...definition,
|
|
25
|
-
item: getValidator(definition
|
|
25
|
+
item: getValidator(definition.item),
|
|
26
26
|
});
|
|
27
27
|
case 'tuple':
|
|
28
28
|
return getTupleValidator({
|
|
29
29
|
...definition,
|
|
30
|
-
tuple: definition
|
|
30
|
+
tuple: definition.tuple.map(def => getValidator(def)),
|
|
31
31
|
});
|
|
32
32
|
case 'struct': {
|
|
33
33
|
const struct = {};
|
|
34
|
-
for (const [key, def] of Object.entries(definition
|
|
34
|
+
for (const [key, def] of Object.entries(definition.struct))
|
|
35
35
|
struct[key] = getValidator(def);
|
|
36
36
|
return getStructValidator({ ...definition, struct });
|
|
37
37
|
}
|
|
38
38
|
case 'record':
|
|
39
39
|
return getRecordValidator({
|
|
40
40
|
...definition,
|
|
41
|
-
record: getValidator(definition
|
|
41
|
+
record: getValidator(definition.record),
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
44
|
}
|