@jayfong/x-server 2.118.1 → 3.0.0
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/_virtual/_rolldown/runtime.mjs +4 -0
- package/lib/cli/api_generator.d.mts +74 -0
- package/lib/cli/api_generator.mjs +310 -0
- package/lib/cli/build_util.d.mts +17 -0
- package/lib/cli/build_util.mjs +156 -0
- package/lib/cli/cli.d.mts +1 -0
- package/lib/cli/cli.mjs +355 -0
- package/lib/cli/css_register.d.mts +1 -0
- package/lib/cli/css_register.mjs +7 -0
- package/lib/cli/deploy_util.d.mts +23 -0
- package/lib/cli/deploy_util.mjs +70 -0
- package/lib/cli/dev_util.d.mts +7 -0
- package/lib/cli/dev_util.mjs +25 -0
- package/lib/cli/env_util.d.mts +45 -0
- package/lib/cli/env_util.mjs +207 -0
- package/lib/cli/esbuild_register.d.mts +1 -0
- package/lib/cli/esbuild_register.mjs +7 -0
- package/lib/cli/template_util.d.mts +18 -0
- package/lib/cli/template_util.mjs +85 -0
- package/lib/cli/templates/crons.ts +1 -1
- package/lib/cli/templates/handlers.ts +1 -1
- package/lib/cli/templates/hooks.ts +1 -1
- package/lib/cli/templates/models.ts +7 -7
- package/lib/cli/templates/package.json +11 -1
- package/lib/cli/templates/routes.ts +2 -1
- package/lib/core/ctx_storage.d.mts +3 -0
- package/lib/core/ctx_storage.mjs +4 -0
- package/lib/core/define_bus.d.mts +37 -0
- package/lib/core/define_bus.mjs +12 -0
- package/lib/core/{define_cron.d.ts → define_cron.d.mts} +2 -2
- package/lib/core/define_cron.mjs +11 -0
- package/lib/core/{define_handler.d.ts → define_handler.d.mts} +19 -19
- package/lib/core/define_handler.mjs +44 -0
- package/lib/core/define_hook.d.mts +2 -0
- package/lib/core/define_hook.mjs +6 -0
- package/lib/core/define_server.d.mts +3 -0
- package/lib/core/define_server.mjs +8 -0
- package/lib/core/{define_task.d.ts → define_task.d.mts} +2 -2
- package/lib/core/define_task.mjs +102 -0
- package/lib/core/get_handler_url.d.mts +2 -0
- package/lib/core/get_handler_url.mjs +7 -0
- package/lib/core/handler.d.mts +16 -0
- package/lib/core/handler.mjs +132 -0
- package/lib/core/http_error.d.mts +2 -0
- package/lib/core/http_error.mjs +1 -0
- package/lib/core/http_header.d.mts +856 -0
- package/lib/core/http_header.mjs +860 -0
- package/lib/core/http_method.d.mts +3 -0
- package/lib/core/http_method.mjs +10 -0
- package/lib/core/http_redirect.d.mts +5 -0
- package/lib/core/http_redirect.mjs +10 -0
- package/lib/core/server.d.mts +20 -0
- package/lib/core/server.mjs +186 -0
- package/lib/core/types.d.mts +256 -0
- package/lib/exports.d.mts +46 -0
- package/lib/exports.mjs +46 -0
- package/lib/index.d.mts +48 -0
- package/lib/index.mjs +45 -0
- package/lib/plugins/base.d.mts +4 -0
- package/lib/plugins/compress.d.mts +12 -0
- package/lib/plugins/compress.mjs +15 -0
- package/lib/plugins/cookie.d.mts +12 -0
- package/lib/plugins/cookie.mjs +15 -0
- package/lib/plugins/cors.d.mts +22 -0
- package/lib/plugins/cors.mjs +34 -0
- package/lib/plugins/file_parser.d.mts +15 -0
- package/lib/plugins/file_parser.mjs +24 -0
- package/lib/plugins/form_body_parser.d.mts +12 -0
- package/lib/plugins/form_body_parser.mjs +15 -0
- package/lib/plugins/ws_parser.d.mts +12 -0
- package/lib/plugins/ws_parser.mjs +15 -0
- package/lib/plugins/xml_parser.d.mts +19 -0
- package/lib/plugins/xml_parser.mjs +40 -0
- package/lib/services/ai.d.mts +31 -0
- package/lib/services/ai.mjs +44 -0
- package/lib/services/base.d.mts +4 -0
- package/lib/services/base.mjs +1 -0
- package/lib/services/cache.d.mts +165 -0
- package/lib/services/cache.mjs +323 -0
- package/lib/services/captcha.d.mts +65 -0
- package/lib/services/captcha.mjs +61 -0
- package/lib/services/{crypto.js → crypto.d.mts} +19 -78
- package/lib/services/crypto.mjs +141 -0
- package/lib/services/db_value.d.mts +60 -0
- package/lib/services/db_value.mjs +64 -0
- package/lib/services/dingtalk.d.mts +39 -0
- package/lib/services/dingtalk.mjs +44 -0
- package/lib/services/dispose.d.mts +17 -0
- package/lib/services/dispose.mjs +18 -0
- package/lib/services/emoji.d.mts +10 -0
- package/lib/services/emoji.mjs +2025 -0
- package/lib/services/file_store.d.mts +49 -0
- package/lib/services/file_store.mjs +58 -0
- package/lib/services/html.d.mts +11 -0
- package/lib/services/html.mjs +12 -0
- package/lib/services/jwt.d.mts +33 -0
- package/lib/services/jwt.mjs +42 -0
- package/lib/services/log.d.mts +37 -0
- package/lib/services/log.mjs +99 -0
- package/lib/services/mail.d.mts +22 -0
- package/lib/services/mail.mjs +28 -0
- package/lib/services/pay.d.mts +123 -0
- package/lib/services/pay.mjs +157 -0
- package/lib/services/random.d.mts +27 -0
- package/lib/services/random.mjs +42 -0
- package/lib/services/rate_limit.d.mts +37 -0
- package/lib/services/rate_limit.mjs +40 -0
- package/lib/services/redis.d.mts +11 -0
- package/lib/services/redis.mjs +6 -0
- package/lib/services/sensitive_words.d.mts +29 -0
- package/lib/services/sensitive_words.mjs +37 -0
- package/lib/services/sms.d.mts +40 -0
- package/lib/services/sms.mjs +30238 -0
- package/lib/types/dto.d.mts +30 -0
- package/lib/types/dto.mjs +1 -0
- package/lib/x.d.mts +11 -0
- package/lib/x.mjs +21 -0
- package/lib/x_init.d.mts +1 -0
- package/lib/x_init.mjs +10 -0
- package/package.json +99 -43
- package/lib/_cjs/cli/api_generator.js +0 -438
- package/lib/_cjs/cli/build_util.js +0 -243
- package/lib/_cjs/cli/cli.js +0 -378
- package/lib/_cjs/cli/css_register.js +0 -7
- package/lib/_cjs/cli/deploy_util.js +0 -76
- package/lib/_cjs/cli/dev_util.js +0 -25
- package/lib/_cjs/cli/env_util.js +0 -252
- package/lib/_cjs/cli/esbuild_register.js +0 -7
- package/lib/_cjs/cli/template_util.js +0 -101
- package/lib/_cjs/cli/templates/crons.ts +0 -2
- package/lib/_cjs/cli/templates/handlers.ts +0 -3
- package/lib/_cjs/cli/templates/hooks.ts +0 -2
- package/lib/_cjs/cli/templates/models.ts +0 -123
- package/lib/_cjs/cli/templates/package.json +0 -3
- package/lib/_cjs/cli/templates/routes.ts +0 -68
- package/lib/_cjs/core/ctx_storage.js +0 -6
- package/lib/_cjs/core/define_bus.js +0 -31
- package/lib/_cjs/core/define_cron.js +0 -13
- package/lib/_cjs/core/define_handler.js +0 -56
- package/lib/_cjs/core/define_hook.js +0 -8
- package/lib/_cjs/core/define_server.js +0 -10
- package/lib/_cjs/core/define_task.js +0 -113
- package/lib/_cjs/core/get_handler_url.js +0 -11
- package/lib/_cjs/core/handler.js +0 -216
- package/lib/_cjs/core/http_error.js +0 -6
- package/lib/_cjs/core/http_header.js +0 -142
- package/lib/_cjs/core/http_method.js +0 -12
- package/lib/_cjs/core/http_redirect.js +0 -11
- package/lib/_cjs/core/server.js +0 -221
- package/lib/_cjs/core/types.js +0 -8
- package/lib/_cjs/exports.js +0 -273
- package/lib/_cjs/index.js +0 -15
- package/lib/_cjs/plugins/base.js +0 -3
- package/lib/_cjs/plugins/compress.js +0 -18
- package/lib/_cjs/plugins/cookie.js +0 -18
- package/lib/_cjs/plugins/cors.js +0 -38
- package/lib/_cjs/plugins/file_parser.js +0 -28
- package/lib/_cjs/plugins/form_body_parser.js +0 -18
- package/lib/_cjs/plugins/ws_parser.js +0 -18
- package/lib/_cjs/plugins/xml_parser.js +0 -48
- package/lib/_cjs/services/ai.js +0 -48
- package/lib/_cjs/services/base.js +0 -3
- package/lib/_cjs/services/cache.js +0 -400
- package/lib/_cjs/services/captcha.js +0 -78
- package/lib/_cjs/services/crypto.js +0 -175
- package/lib/_cjs/services/db_value.js +0 -77
- package/lib/_cjs/services/dingtalk.js +0 -51
- package/lib/_cjs/services/dispose.js +0 -23
- package/lib/_cjs/services/emoji.js +0 -2034
- package/lib/_cjs/services/file_store.js +0 -67
- package/lib/_cjs/services/html.js +0 -17
- package/lib/_cjs/services/jwt.js +0 -65
- package/lib/_cjs/services/log.js +0 -108
- package/lib/_cjs/services/mail.js +0 -31
- package/lib/_cjs/services/pay.js +0 -191
- package/lib/_cjs/services/random.js +0 -51
- package/lib/_cjs/services/rate_limit.js +0 -54
- package/lib/_cjs/services/redis.js +0 -12
- package/lib/_cjs/services/sensitive_words.js +0 -62
- package/lib/_cjs/services/sms.js +0 -84
- package/lib/_cjs/types/dto.js +0 -3
- package/lib/_cjs/x.js +0 -35
- package/lib/cli/api_generator.d.ts +0 -74
- package/lib/cli/api_generator.js +0 -432
- package/lib/cli/build_util.d.ts +0 -17
- package/lib/cli/build_util.js +0 -236
- package/lib/cli/cli.d.ts +0 -2
- package/lib/cli/cli.js +0 -374
- package/lib/cli/css_register.js +0 -5
- package/lib/cli/deploy_util.d.ts +0 -23
- package/lib/cli/deploy_util.js +0 -70
- package/lib/cli/dev_util.d.ts +0 -8
- package/lib/cli/dev_util.js +0 -19
- package/lib/cli/env_util.d.ts +0 -45
- package/lib/cli/env_util.js +0 -246
- package/lib/cli/esbuild_register.js +0 -5
- package/lib/cli/template_util.d.ts +0 -18
- package/lib/cli/template_util.js +0 -95
- package/lib/core/ctx_storage.d.ts +0 -3
- package/lib/core/ctx_storage.js +0 -2
- package/lib/core/define_bus.d.ts +0 -37
- package/lib/core/define_bus.js +0 -27
- package/lib/core/define_cron.js +0 -9
- package/lib/core/define_handler.js +0 -48
- package/lib/core/define_hook.d.ts +0 -2
- package/lib/core/define_hook.js +0 -4
- package/lib/core/define_server.d.ts +0 -3
- package/lib/core/define_server.js +0 -6
- package/lib/core/define_task.js +0 -107
- package/lib/core/get_handler_url.d.ts +0 -2
- package/lib/core/get_handler_url.js +0 -7
- package/lib/core/handler.d.ts +0 -16
- package/lib/core/handler.js +0 -209
- package/lib/core/http_error.d.ts +0 -2
- package/lib/core/http_error.js +0 -2
- package/lib/core/http_header.d.ts +0 -856
- package/lib/core/http_header.js +0 -139
- package/lib/core/http_method.d.ts +0 -3
- package/lib/core/http_method.js +0 -8
- package/lib/core/http_redirect.d.ts +0 -5
- package/lib/core/http_redirect.js +0 -6
- package/lib/core/server.d.ts +0 -20
- package/lib/core/server.js +0 -215
- package/lib/core/types.d.ts +0 -265
- package/lib/core/types.js +0 -4
- package/lib/exports.d.ts +0 -45
- package/lib/exports.js +0 -47
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -3
- package/lib/plugins/base.d.ts +0 -4
- package/lib/plugins/base.js +0 -1
- package/lib/plugins/compress.d.ts +0 -13
- package/lib/plugins/compress.js +0 -12
- package/lib/plugins/cookie.d.ts +0 -13
- package/lib/plugins/cookie.js +0 -12
- package/lib/plugins/cors.d.ts +0 -22
- package/lib/plugins/cors.js +0 -33
- package/lib/plugins/file_parser.d.ts +0 -16
- package/lib/plugins/file_parser.js +0 -22
- package/lib/plugins/form_body_parser.d.ts +0 -13
- package/lib/plugins/form_body_parser.js +0 -12
- package/lib/plugins/ws_parser.d.ts +0 -13
- package/lib/plugins/ws_parser.js +0 -12
- package/lib/plugins/xml_parser.d.ts +0 -19
- package/lib/plugins/xml_parser.js +0 -43
- package/lib/services/ai.d.ts +0 -31
- package/lib/services/ai.js +0 -43
- package/lib/services/base.d.ts +0 -4
- package/lib/services/base.js +0 -1
- package/lib/services/cache.d.ts +0 -173
- package/lib/services/cache.js +0 -394
- package/lib/services/captcha.d.ts +0 -65
- package/lib/services/captcha.js +0 -72
- package/lib/services/crypto.d.ts +0 -110
- package/lib/services/db_value.d.ts +0 -60
- package/lib/services/db_value.js +0 -72
- package/lib/services/dingtalk.d.ts +0 -39
- package/lib/services/dingtalk.js +0 -45
- package/lib/services/dispose.d.ts +0 -17
- package/lib/services/dispose.js +0 -17
- package/lib/services/emoji.d.ts +0 -10
- package/lib/services/emoji.js +0 -2029
- package/lib/services/file_store.d.ts +0 -49
- package/lib/services/file_store.js +0 -61
- package/lib/services/html.d.ts +0 -11
- package/lib/services/html.js +0 -12
- package/lib/services/jwt.d.ts +0 -34
- package/lib/services/jwt.js +0 -59
- package/lib/services/log.d.ts +0 -37
- package/lib/services/log.js +0 -102
- package/lib/services/mail.d.ts +0 -23
- package/lib/services/mail.js +0 -25
- package/lib/services/pay.d.ts +0 -123
- package/lib/services/pay.js +0 -185
- package/lib/services/random.d.ts +0 -27
- package/lib/services/random.js +0 -45
- package/lib/services/rate_limit.d.ts +0 -37
- package/lib/services/rate_limit.js +0 -49
- package/lib/services/redis.d.ts +0 -12
- package/lib/services/redis.js +0 -7
- package/lib/services/sensitive_words.d.ts +0 -30
- package/lib/services/sensitive_words.js +0 -57
- package/lib/services/sms.d.ts +0 -40
- package/lib/services/sms.js +0 -78
- package/lib/types/dto.d.ts +0 -86
- package/lib/types/dto.js +0 -1
- package/lib/x.d.ts +0 -17
- package/lib/x.js +0 -30
- /package/lib/{cli/css_register.d.ts → core/types.mjs} +0 -0
- /package/lib/{cli/esbuild_register.d.ts → plugins/base.mjs} +0 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { BaseService } from "./base.mjs";
|
|
2
|
+
export declare class RandomService implements BaseService {
|
|
3
|
+
serviceName: string;
|
|
4
|
+
/**
|
|
5
|
+
* 24位。
|
|
6
|
+
*
|
|
7
|
+
* @see https://github.com/paralleldrive/cuid2
|
|
8
|
+
*/
|
|
9
|
+
cuid2(): string;
|
|
10
|
+
/**
|
|
11
|
+
* [0-9 a-z A-Z]
|
|
12
|
+
*/
|
|
13
|
+
generateAlphanumeric(length: number): string;
|
|
14
|
+
/**
|
|
15
|
+
* [a-z A-Z]
|
|
16
|
+
*/
|
|
17
|
+
generateAlphabetic(length: number): string;
|
|
18
|
+
/**
|
|
19
|
+
* [0-9]
|
|
20
|
+
*/
|
|
21
|
+
generateNumeric(length: number): string;
|
|
22
|
+
}
|
|
23
|
+
declare module '../x.mjs' {
|
|
24
|
+
interface X {
|
|
25
|
+
random: RandomService;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { createId } from "@paralleldrive/cuid2";
|
|
2
|
+
import randomstring from "randomstring";
|
|
3
|
+
// src/services/random.ts
|
|
4
|
+
var RandomService = class {
|
|
5
|
+
serviceName = "random";
|
|
6
|
+
/**
|
|
7
|
+
* 24位。
|
|
8
|
+
*
|
|
9
|
+
* @see https://github.com/paralleldrive/cuid2
|
|
10
|
+
*/
|
|
11
|
+
cuid2() {
|
|
12
|
+
return createId();
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* [0-9 a-z A-Z]
|
|
16
|
+
*/
|
|
17
|
+
generateAlphanumeric(length) {
|
|
18
|
+
return randomstring.generate({
|
|
19
|
+
charset: "alphanumeric",
|
|
20
|
+
length
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* [a-z A-Z]
|
|
25
|
+
*/
|
|
26
|
+
generateAlphabetic(length) {
|
|
27
|
+
return randomstring.generate({
|
|
28
|
+
charset: "alphabetic",
|
|
29
|
+
length
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* [0-9]
|
|
34
|
+
*/
|
|
35
|
+
generateNumeric(length) {
|
|
36
|
+
return randomstring.generate({
|
|
37
|
+
charset: "numeric",
|
|
38
|
+
length
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
export { RandomService };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BaseService } from "./base.mjs";
|
|
2
|
+
import { MsValue } from "vtils";
|
|
3
|
+
export interface RateLimitServiceLimitByCountOptions {
|
|
4
|
+
key: string;
|
|
5
|
+
ttl: MsValue;
|
|
6
|
+
count: number;
|
|
7
|
+
}
|
|
8
|
+
export interface RateLimitServiceLimitByCountOrFailOptions extends RateLimitServiceLimitByCountOptions {
|
|
9
|
+
message?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface RateLimitServiceLimitByManyCountOptions {
|
|
12
|
+
key: string;
|
|
13
|
+
limits: Array<{
|
|
14
|
+
ttl: MsValue;
|
|
15
|
+
count: number;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
export interface RateLimitServiceLimitByManyCountOrFailOptions extends RateLimitServiceLimitByManyCountOptions {
|
|
19
|
+
message?: string;
|
|
20
|
+
limits: Array<{
|
|
21
|
+
ttl: MsValue;
|
|
22
|
+
count: number;
|
|
23
|
+
message?: string;
|
|
24
|
+
}>;
|
|
25
|
+
}
|
|
26
|
+
export declare class RateLimitService implements BaseService {
|
|
27
|
+
serviceName: string;
|
|
28
|
+
limitByCount(options: RateLimitServiceLimitByCountOptions): Promise<number>;
|
|
29
|
+
limitByCountOrFail(options: RateLimitServiceLimitByCountOrFailOptions): Promise<number>;
|
|
30
|
+
limitByManyCount(options: RateLimitServiceLimitByManyCountOptions): Promise<true | number>;
|
|
31
|
+
limitByManyCountOrFail(options: RateLimitServiceLimitByManyCountOrFailOptions): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
declare module '../x.mjs' {
|
|
34
|
+
interface X {
|
|
35
|
+
rateLimit: RateLimitService;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { HttpError } from "../core/http_error.mjs";
|
|
2
|
+
import { x } from "../x.mjs";
|
|
3
|
+
import { ms } from "vtils";
|
|
4
|
+
// src/services/rate_limit.ts
|
|
5
|
+
var RateLimitService = class {
|
|
6
|
+
serviceName = "rateLimit";
|
|
7
|
+
async limitByCount(options) {
|
|
8
|
+
const cacheKey = `rateLimit_${options.key}`;
|
|
9
|
+
const remainingCount = await x.cache.get(cacheKey);
|
|
10
|
+
if (remainingCount == null) {
|
|
11
|
+
await x.cache.set(cacheKey, options.count, options.ttl);
|
|
12
|
+
return options.count;
|
|
13
|
+
}
|
|
14
|
+
if (remainingCount === 0) return 0;
|
|
15
|
+
await x.cache.decrease(cacheKey);
|
|
16
|
+
return remainingCount - 1;
|
|
17
|
+
}
|
|
18
|
+
async limitByCountOrFail(options) {
|
|
19
|
+
const count = await this.limitByCount(options);
|
|
20
|
+
if (count === 0) throw new HttpError.Forbidden(options.message);
|
|
21
|
+
return count;
|
|
22
|
+
}
|
|
23
|
+
async limitByManyCount(options) {
|
|
24
|
+
for (let i = 0; i < options.limits.length; i++) {
|
|
25
|
+
const item = options.limits[i];
|
|
26
|
+
const ttl = ms(item.ttl);
|
|
27
|
+
if (await this.limitByCount({
|
|
28
|
+
key: `${options.key}_${ttl}`,
|
|
29
|
+
ttl,
|
|
30
|
+
count: item.count
|
|
31
|
+
}) === 0) return i;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
async limitByManyCountOrFail(options) {
|
|
36
|
+
const pass = await this.limitByManyCount(options);
|
|
37
|
+
if (pass !== true) throw new HttpError.Forbidden(options.limits[pass].message ?? options.message);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
export { RateLimitService };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BaseService } from "./base.mjs";
|
|
2
|
+
import { Redis, RedisOptions } from "ioredis";
|
|
3
|
+
export interface RedisServiceOptions extends RedisOptions {}
|
|
4
|
+
export declare class RedisService extends Redis implements BaseService {
|
|
5
|
+
serviceName: string;
|
|
6
|
+
}
|
|
7
|
+
declare module '../x.mjs' {
|
|
8
|
+
interface X {
|
|
9
|
+
redis: RedisService;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BaseService } from "./base.mjs";
|
|
2
|
+
export interface SensitiveWordsServiceOptions {
|
|
3
|
+
/** 敏感词列表 */
|
|
4
|
+
words: string[];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 敏感词服务。
|
|
8
|
+
*/
|
|
9
|
+
export declare class SensitiveWordsService implements BaseService {
|
|
10
|
+
serviceName: string;
|
|
11
|
+
private sensitiveWords;
|
|
12
|
+
constructor(options: SensitiveWordsServiceOptions);
|
|
13
|
+
/**
|
|
14
|
+
* 文本转换。
|
|
15
|
+
*/
|
|
16
|
+
private transform;
|
|
17
|
+
/**
|
|
18
|
+
* 验证文本是否包含敏感词。
|
|
19
|
+
*/
|
|
20
|
+
validate(text: string, every?: boolean): Promise<{
|
|
21
|
+
pass: boolean;
|
|
22
|
+
words: string[];
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
declare module '../x.mjs' {
|
|
26
|
+
interface X {
|
|
27
|
+
sensitiveWords: SensitiveWordsService;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { memoize, removeBlankChars, removeNonWordChars, toHalfWidthString, uniq } from "vtils";
|
|
2
|
+
import { phoneNumberRegExpBuilder } from "vtils/regexp";
|
|
3
|
+
// src/services/sensitive_words.ts
|
|
4
|
+
const phoneNumberRegExp = phoneNumberRegExpBuilder.build();
|
|
5
|
+
const toRegExp = memoize((re) => new RegExp(re.substring(1, re.length - 1)), (re) => re);
|
|
6
|
+
/**
|
|
7
|
+
* 敏感词服务。
|
|
8
|
+
*/
|
|
9
|
+
var SensitiveWordsService = class {
|
|
10
|
+
serviceName = "sensitiveWords";
|
|
11
|
+
sensitiveWords = [];
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.sensitiveWords = uniq(options.words.map((word) => word.toLowerCase()));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 文本转换。
|
|
17
|
+
*/
|
|
18
|
+
transform(text) {
|
|
19
|
+
return removeBlankChars(removeNonWordChars(toHalfWidthString(text))).toLowerCase().replace(/([^a-zA-Z0-9])\1+/g, "$1");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 验证文本是否包含敏感词。
|
|
23
|
+
*/
|
|
24
|
+
async validate(text, every) {
|
|
25
|
+
text = this.transform(text);
|
|
26
|
+
const words = [];
|
|
27
|
+
for (const word of this.sensitiveWords) if (word === "<手机号>" ? phoneNumberRegExp.test(text) : word[0] === "/" && word[word.length - 1] === "/" ? toRegExp(word).test(text) : text.includes(word)) {
|
|
28
|
+
words.push(word);
|
|
29
|
+
if (!every) break;
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
pass: !words.length,
|
|
33
|
+
words
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
export { SensitiveWordsService };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { BaseService } from "./base.mjs";
|
|
2
|
+
export type SmsServiceProvider = 'tencentCloud';
|
|
3
|
+
export type SmsServiceOptions = {
|
|
4
|
+
provider: 'tencentCloud';
|
|
5
|
+
providerOptions: {
|
|
6
|
+
appId: string;
|
|
7
|
+
secretId: string;
|
|
8
|
+
secretKey: string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export type SmsServiceSendPayload = {
|
|
12
|
+
provider: SmsServiceProvider;
|
|
13
|
+
signature: string;
|
|
14
|
+
templateId: string;
|
|
15
|
+
templateParamValues: string[];
|
|
16
|
+
phoneNumber: string;
|
|
17
|
+
};
|
|
18
|
+
export type SmsServiceSendResult = {
|
|
19
|
+
success: boolean;
|
|
20
|
+
message: string;
|
|
21
|
+
smsId: string;
|
|
22
|
+
};
|
|
23
|
+
export type SmsServiceErrorType = 'TEXT_INTERCEPTION' | 'PROVIDER_EXCEPTION' | 'RATE_LIMITATION' | 'NUMBER_EXCEPTION' | 'USER_REJECTION' | 'OTHERS';
|
|
24
|
+
export declare class SmsService implements BaseService {
|
|
25
|
+
private options;
|
|
26
|
+
serviceName: string;
|
|
27
|
+
constructor(options: SmsServiceOptions);
|
|
28
|
+
send(payload: SmsServiceSendPayload): Promise<SmsServiceSendResult>;
|
|
29
|
+
/**
|
|
30
|
+
* 推断错误类型
|
|
31
|
+
*
|
|
32
|
+
* @param errMsg 错误信息
|
|
33
|
+
*/
|
|
34
|
+
static inferErrorType(errMsg: string): SmsServiceErrorType;
|
|
35
|
+
}
|
|
36
|
+
declare module '../x.mjs' {
|
|
37
|
+
interface X {
|
|
38
|
+
sms: SmsService;
|
|
39
|
+
}
|
|
40
|
+
}
|