koishi-plugin-codec-tools 1.1.2 → 1.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Minecraft_1314
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/lib/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import { Context, Schema } from 'koishi';
2
2
  export declare const name = "codec-tools";
3
3
  export declare const using: readonly ["i18n"];
4
4
  export interface Config {
5
+ maxInputLength?: number;
5
6
  }
6
7
  export declare const Config: Schema<Config>;
7
8
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -1,32 +1,119 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.Config = exports.using = exports.name = void 0;
7
4
  exports.apply = apply;
8
5
  const koishi_1 = require("koishi");
9
- const fs_1 = __importDefault(require("fs"));
10
- const path_1 = __importDefault(require("path"));
11
- const yaml_1 = __importDefault(require("yaml"));
6
+ const fs_1 = require("fs");
7
+ const path_1 = require("path");
8
+ const yaml_1 = require("yaml");
12
9
  exports.name = 'codec-tools';
13
10
  exports.using = ['i18n'];
14
- exports.Config = koishi_1.Schema.object({}).i18n({ 'zh-CN': {} });
11
+ exports.Config = koishi_1.Schema.object({
12
+ maxInputLength: koishi_1.Schema.natural()
13
+ .default(10000)
14
+ .description('最大输入长度(字符数)'),
15
+ });
16
+ function validateInput(text, maxLen) {
17
+ if (text.length === 0)
18
+ return '输入不能为空';
19
+ if (text.length > maxLen)
20
+ return `输入过长,最大允许 ${maxLen} 个字符`;
21
+ return null;
22
+ }
23
+ function encodeUrl(text) {
24
+ return encodeURIComponent(text);
25
+ }
26
+ function decodeUrl(text) {
27
+ return decodeURIComponent(text);
28
+ }
29
+ function encodeBase64(text) {
30
+ return Buffer.from(text, 'utf-8').toString('base64');
31
+ }
32
+ function decodeBase64(text) {
33
+ return Buffer.from(text, 'base64').toString('utf-8');
34
+ }
35
+ function encodeUnicode(text) {
36
+ let result = '';
37
+ for (const char of text) {
38
+ const cp = char.codePointAt(0);
39
+ if (cp > 0xffff) {
40
+ result += '\\u{' + cp.toString(16) + '}';
41
+ }
42
+ else {
43
+ result += '\\u' + cp.toString(16).padStart(4, '0');
44
+ }
45
+ }
46
+ return result;
47
+ }
48
+ function decodeUnicode(text) {
49
+ return text.replace(/\\u\{([0-9a-fA-F]{1,6})\}|\\u([0-9a-fA-F]{4})/g, (_, brace, fixed) => String.fromCodePoint(parseInt(brace || fixed, 16)));
50
+ }
15
51
  function apply(ctx, config) {
16
- const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
17
- const zhCNContent = fs_1.default.readFileSync(zhCNPath, 'utf8');
18
- const zhCN = yaml_1.default.parse(zhCNContent);
52
+ const maxLen = config.maxInputLength ?? 10000;
53
+ const zhCNPath = (0, path_1.join)(__dirname, './locales/zh-CN.yml');
54
+ const zhCNContent = (0, fs_1.readFileSync)(zhCNPath, 'utf8');
55
+ const zhCN = (0, yaml_1.parse)(zhCNContent);
19
56
  ctx.i18n.define('zh-CN', zhCN);
20
- ctx.command('url-encode <text:text>')
21
- .action((_, text) => encodeURIComponent(text));
22
- ctx.command('url-decode <text:text>')
23
- .action((_, text) => decodeURIComponent(text));
24
- ctx.command('base64-encode <text:text>')
25
- .action((_, text) => Buffer.from(text).toString('base64'));
26
- ctx.command('base64-decode <text:text>')
27
- .action((_, text) => Buffer.from(text, 'base64').toString());
28
- ctx.command('unicode-encode <text:text>')
29
- .action((_, text) => text.split('').map(c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')).join(''));
30
- ctx.command('unicode-decode <text:text>')
31
- .action((_, text) => text.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))));
57
+ ctx.command('url-encode <text:text>').action((_, text) => {
58
+ const err = validateInput(text, maxLen);
59
+ if (err)
60
+ return err;
61
+ try {
62
+ return encodeUrl(text);
63
+ }
64
+ catch {
65
+ return 'URL 编码失败:输入包含无效字符';
66
+ }
67
+ });
68
+ ctx.command('url-decode <text:text>').action((_, text) => {
69
+ const err = validateInput(text, maxLen);
70
+ if (err)
71
+ return err;
72
+ try {
73
+ return decodeUrl(text);
74
+ }
75
+ catch {
76
+ return 'URL 解码失败:输入格式不正确';
77
+ }
78
+ });
79
+ ctx.command('base64-encode <text:text>').action((_, text) => {
80
+ const err = validateInput(text, maxLen);
81
+ if (err)
82
+ return err;
83
+ return encodeBase64(text);
84
+ });
85
+ ctx.command('base64-decode <text:text>').action((_, text) => {
86
+ const err = validateInput(text, maxLen);
87
+ if (err)
88
+ return err;
89
+ try {
90
+ const result = decodeBase64(text);
91
+ if (result.length === 0)
92
+ return 'Base64 解码结果为空';
93
+ return result;
94
+ }
95
+ catch {
96
+ return 'Base64 解码失败:输入格式不正确';
97
+ }
98
+ });
99
+ ctx.command('unicode-encode <text:text>').action((_, text) => {
100
+ const err = validateInput(text, maxLen);
101
+ if (err)
102
+ return err;
103
+ return encodeUnicode(text);
104
+ });
105
+ ctx.command('unicode-decode <text:text>').action((_, text) => {
106
+ const err = validateInput(text, maxLen);
107
+ if (err)
108
+ return err;
109
+ try {
110
+ const result = decodeUnicode(text);
111
+ if (result.length === 0)
112
+ return 'Unicode 解码结果为空';
113
+ return result;
114
+ }
115
+ catch {
116
+ return 'Unicode 解码失败:输入格式不正确';
117
+ }
118
+ });
32
119
  }
@@ -1,15 +1,15 @@
1
- commands:
2
- url-encode:
3
- description: URL 编码
4
- url-decode:
5
- description: URL 解码
6
- base64-encode:
7
- description: Base64 编码
8
- base64-decode:
9
- description: Base64 解码
10
- unicode-encode:
11
- description: Unicode 编码
12
- unicode-decode:
13
- description: Unicode 解码
14
-
1
+ commands:
2
+ url-encode:
3
+ description: URL 编码
4
+ url-decode:
5
+ description: URL 解码
6
+ base64-encode:
7
+ description: Base64 编码
8
+ base64-decode:
9
+ description: Base64 解码
10
+ unicode-encode:
11
+ description: Unicode 编码
12
+ unicode-decode:
13
+ description: Unicode 解码
14
+
15
15
  _config: {}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-codec-tools",
3
3
  "description": "编码&解码 URL/Base64/Unicode的 Koishi 插件",
4
- "version": "1.1.2",
4
+ "version": "1.1.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -1,15 +1,15 @@
1
- commands:
2
- url-encode:
3
- description: URL 编码
4
- url-decode:
5
- description: URL 解码
6
- base64-encode:
7
- description: Base64 编码
8
- base64-decode:
9
- description: Base64 解码
10
- unicode-encode:
11
- description: Unicode 编码
12
- unicode-decode:
13
- description: Unicode 解码
14
-
1
+ commands:
2
+ url-encode:
3
+ description: URL 编码
4
+ url-decode:
5
+ description: URL 解码
6
+ base64-encode:
7
+ description: Base64 编码
8
+ base64-decode:
9
+ description: Base64 解码
10
+ unicode-encode:
11
+ description: Unicode 编码
12
+ unicode-decode:
13
+ description: Unicode 解码
14
+
15
15
  _config: {}