@jayfong/x-server 2.22.1 → 2.22.3

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.
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
3
  exports.__esModule = true;
5
4
  exports.SensitiveWordsService = void 0;
6
- var _mintFilter = _interopRequireDefault(require("mint-filter"));
7
5
  var _vtils = require("vtils");
8
6
  /**
9
7
  * 敏感词服务。
@@ -12,7 +10,8 @@ class SensitiveWordsService {
12
10
  constructor(options) {
13
11
  this.options = options;
14
12
  this.serviceName = 'sensitiveWords';
15
- this.mint = void 0;
13
+ this.sensitiveWords = [];
14
+ this.sensitiveWords = (0, _vtils.uniq)(options.words.map(word => word.toLowerCase()));
16
15
  }
17
16
 
18
17
  /**
@@ -33,36 +32,21 @@ class SensitiveWordsService {
33
32
  );
34
33
  }
35
34
 
36
- /**
37
- * 处理文本,敏感词将被替换为 * 号。
38
- */
39
- async process(text) {
40
- if (!this.mint) {
41
- this.mint = new _mintFilter.default(this.options.words);
42
- }
43
- const res = await this.mint.filter(this.transform(text), {
44
- every: true,
45
- replace: true,
46
- words: false
47
- });
48
- return res.text;
49
- }
50
-
51
35
  /**
52
36
  * 验证文本是否包含敏感词。
53
37
  */
54
38
  async validate(text, every) {
55
- if (!this.mint) {
56
- this.mint = new _mintFilter.default(this.options.words);
39
+ text = this.transform(text);
40
+ const words = [];
41
+ for (const word of this.sensitiveWords) {
42
+ if (text.includes(word)) {
43
+ words.push(word);
44
+ if (!every) break;
45
+ }
57
46
  }
58
- const res = await this.mint.filter(this.transform(text), {
59
- replace: false,
60
- every: !!every,
61
- words: true
62
- });
63
47
  return {
64
- pass: !!res.pass,
65
- words: res.words
48
+ pass: !words.length,
49
+ words: words
66
50
  };
67
51
  }
68
52
  }
@@ -9,16 +9,12 @@ export interface SensitiveWordsServiceOptions {
9
9
  export declare class SensitiveWordsService implements BaseService {
10
10
  private options;
11
11
  serviceName: string;
12
- private mint;
12
+ private sensitiveWords;
13
13
  constructor(options: SensitiveWordsServiceOptions);
14
14
  /**
15
15
  * 文本转换。
16
16
  */
17
17
  private transform;
18
- /**
19
- * 处理文本,敏感词将被替换为 * 号。
20
- */
21
- process(text: string): Promise<string>;
22
18
  /**
23
19
  * 验证文本是否包含敏感词。
24
20
  */
@@ -1,5 +1,4 @@
1
- import Mint from 'mint-filter';
2
- import { removeBlankChars, removeNonWordChars, toHalfWidthString } from 'vtils';
1
+ import { removeBlankChars, removeNonWordChars, toHalfWidthString, uniq } from 'vtils';
3
2
  /**
4
3
  * 敏感词服务。
5
4
  */
@@ -7,7 +6,8 @@ export class SensitiveWordsService {
7
6
  constructor(options) {
8
7
  this.options = options;
9
8
  this.serviceName = 'sensitiveWords';
10
- this.mint = void 0;
9
+ this.sensitiveWords = [];
10
+ this.sensitiveWords = uniq(options.words.map(word => word.toLowerCase()));
11
11
  }
12
12
 
13
13
  /**
@@ -28,36 +28,21 @@ export class SensitiveWordsService {
28
28
  );
29
29
  }
30
30
 
31
- /**
32
- * 处理文本,敏感词将被替换为 * 号。
33
- */
34
- async process(text) {
35
- if (!this.mint) {
36
- this.mint = new Mint(this.options.words);
37
- }
38
- const res = await this.mint.filter(this.transform(text), {
39
- every: true,
40
- replace: true,
41
- words: false
42
- });
43
- return res.text;
44
- }
45
-
46
31
  /**
47
32
  * 验证文本是否包含敏感词。
48
33
  */
49
34
  async validate(text, every) {
50
- if (!this.mint) {
51
- this.mint = new Mint(this.options.words);
35
+ text = this.transform(text);
36
+ const words = [];
37
+ for (const word of this.sensitiveWords) {
38
+ if (text.includes(word)) {
39
+ words.push(word);
40
+ if (!every) break;
41
+ }
52
42
  }
53
- const res = await this.mint.filter(this.transform(text), {
54
- replace: false,
55
- every: !!every,
56
- words: true
57
- });
58
43
  return {
59
- pass: !!res.pass,
60
- words: res.words
44
+ pass: !words.length,
45
+ words: words
61
46
  };
62
47
  }
63
48
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.22.1",
3
+ "version": "2.22.3",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",
@@ -54,7 +54,6 @@
54
54
  "lru-cache": "^10.0.0",
55
55
  "lz-string": "^1.4.4",
56
56
  "mini-svg-data-uri": "^1.4.4",
57
- "mint-filter": "^3.0.1",
58
57
  "node-ssh": "^12.0.4",
59
58
  "nodemailer": "^6.7.3",
60
59
  "pino-pretty": "^10.0.1",
@@ -62,7 +61,7 @@
62
61
  "select-run": "^1.1.2",
63
62
  "supports-color": "^8",
64
63
  "svg-captcha": "^1.4.0",
65
- "tencentcloud-sdk-nodejs-sms": "^4.0.649",
64
+ "tencentcloud-sdk-nodejs-sms": "^4.0.681",
66
65
  "ts-morph": "^12.2.0",
67
66
  "tsx": "^3.12.7",
68
67
  "utf-8-validate": "^5.0.9",