@jayfong/x-server 2.2.12 → 2.2.14
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.
package/lib/_cjs/services/log.js
CHANGED
|
@@ -39,17 +39,21 @@ class LogService {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
parseLogLineText(text) {
|
|
42
|
-
const [time, level, title, desc] = text.split(this.separator);
|
|
42
|
+
const [time, level, title, ...desc] = text.split(this.separator);
|
|
43
43
|
return {
|
|
44
44
|
time: time,
|
|
45
45
|
level: level,
|
|
46
46
|
title: title,
|
|
47
|
-
desc:
|
|
47
|
+
desc: desc.reduce((res, item) => {
|
|
48
|
+
const i = item.indexOf(':');
|
|
49
|
+
res[item.substring(0, i)] = item.substring(i + 1);
|
|
50
|
+
return res;
|
|
51
|
+
}, {})
|
|
48
52
|
};
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
log(payload) {
|
|
52
|
-
this.getLogWriter(this.logFile).write(`${(0, _date.formatDate)(new Date(), 'yyyy-mm-dd hh:ii:ss')
|
|
56
|
+
this.getLogWriter(this.logFile).write(`${[(0, _date.formatDate)(new Date(), 'yyyy-mm-dd hh:ii:ss'), payload.level, payload.title, ...Object.keys(payload.desc || {}).map(key => `${key}:${payload.desc[key]}`)].join(this.separator).replace(/[\r\n]+/g, ' ')}\n`);
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
info(title, desc) {
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.RateLimitService = void 0;
|
|
5
5
|
|
|
6
|
+
var _http_error = require("../core/http_error");
|
|
7
|
+
|
|
6
8
|
var _x = require("../x");
|
|
7
9
|
|
|
8
10
|
class RateLimitService {
|
|
@@ -27,6 +29,16 @@ class RateLimitService {
|
|
|
27
29
|
return remainingCount - 1;
|
|
28
30
|
}
|
|
29
31
|
|
|
32
|
+
async limitByCountOrFail(options) {
|
|
33
|
+
const count = await this.limitByCount(options);
|
|
34
|
+
|
|
35
|
+
if (count === 0) {
|
|
36
|
+
throw new _http_error.HttpError.Forbidden(options.message);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return count;
|
|
40
|
+
}
|
|
41
|
+
|
|
30
42
|
}
|
|
31
43
|
|
|
32
44
|
exports.RateLimitService = RateLimitService;
|
package/lib/services/log.js
CHANGED
|
@@ -26,17 +26,21 @@ export class LogService {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
parseLogLineText(text) {
|
|
29
|
-
const [time, level, title, desc] = text.split(this.separator);
|
|
29
|
+
const [time, level, title, ...desc] = text.split(this.separator);
|
|
30
30
|
return {
|
|
31
31
|
time: time,
|
|
32
32
|
level: level,
|
|
33
33
|
title: title,
|
|
34
|
-
desc:
|
|
34
|
+
desc: desc.reduce((res, item) => {
|
|
35
|
+
const i = item.indexOf(':');
|
|
36
|
+
res[item.substring(0, i)] = item.substring(i + 1);
|
|
37
|
+
return res;
|
|
38
|
+
}, {})
|
|
35
39
|
};
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
log(payload) {
|
|
39
|
-
this.getLogWriter(this.logFile).write(`${formatDate(new Date(), 'yyyy-mm-dd hh:ii:ss')
|
|
43
|
+
this.getLogWriter(this.logFile).write(`${[formatDate(new Date(), 'yyyy-mm-dd hh:ii:ss'), payload.level, payload.title, ...Object.keys(payload.desc || {}).map(key => `${key}:${payload.desc[key]}`)].join(this.separator).replace(/[\r\n]+/g, ' ')}\n`);
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
info(title, desc) {
|
|
@@ -5,9 +5,13 @@ export interface RateLimitServiceLimitByCountOptions {
|
|
|
5
5
|
count: number;
|
|
6
6
|
ttl: MsValue;
|
|
7
7
|
}
|
|
8
|
+
export interface RateLimitServiceLimitByCountOrFailOptions extends RateLimitServiceLimitByCountOptions {
|
|
9
|
+
message?: string;
|
|
10
|
+
}
|
|
8
11
|
export declare class RateLimitService implements BaseService {
|
|
9
12
|
serviceName: string;
|
|
10
13
|
limitByCount(options: RateLimitServiceLimitByCountOptions): Promise<number>;
|
|
14
|
+
limitByCountOrFail(options: RateLimitServiceLimitByCountOrFailOptions): Promise<number>;
|
|
11
15
|
}
|
|
12
16
|
declare module '../x' {
|
|
13
17
|
interface X {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HttpError } from "../core/http_error";
|
|
1
2
|
import { x } from "../x";
|
|
2
3
|
export class RateLimitService {
|
|
3
4
|
constructor() {
|
|
@@ -21,4 +22,14 @@ export class RateLimitService {
|
|
|
21
22
|
return remainingCount - 1;
|
|
22
23
|
}
|
|
23
24
|
|
|
25
|
+
async limitByCountOrFail(options) {
|
|
26
|
+
const count = await this.limitByCount(options);
|
|
27
|
+
|
|
28
|
+
if (count === 0) {
|
|
29
|
+
throw new HttpError.Forbidden(options.message);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return count;
|
|
33
|
+
}
|
|
34
|
+
|
|
24
35
|
}
|