kokkoro-plugin-eval 0.0.0 → 1.0.2
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 +21 -0
- package/README.md +54 -1
- package/package.json +22 -6
- package/src/index.ts +20 -1
- package/src/service.ts +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Yuki
|
|
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/README.md
CHANGED
|
@@ -1 +1,54 @@
|
|
|
1
|
-
#
|
|
1
|
+
# kokkoro-plugin-eval
|
|
2
|
+
|
|
3
|
+
代码执行,运行 JavaScript 或 TypeScript 代码并返回执行结果。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
bun add kokkoro-plugin-eval
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 指令
|
|
12
|
+
|
|
13
|
+
```text
|
|
14
|
+
/执行 <code>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`code` 表示需要执行的代码。发送指令后,插件会返回执行结果。
|
|
18
|
+
|
|
19
|
+
```text
|
|
20
|
+
/执行 0.1 + 0.2
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 快捷方式
|
|
24
|
+
|
|
25
|
+
也可以使用「>」快捷执行:
|
|
26
|
+
|
|
27
|
+
```text
|
|
28
|
+
> await fetch('https://example.com').then(response => response.status)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
群聊需要开启「获取群内全部消息」权限。
|
|
32
|
+
|
|
33
|
+
## 环境变量
|
|
34
|
+
|
|
35
|
+
`EVAL_TIMEOUT` 和 `EVAL_MAX_BUFFER` 用于自定义插件的执行超时时间和输出上限,单位分别为毫秒和字节。默认值如下:
|
|
36
|
+
|
|
37
|
+
```ini
|
|
38
|
+
EVAL_TIMEOUT=60000
|
|
39
|
+
EVAL_MAX_BUFFER=65536
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
其他插件可以从 `service` 入口导入 `evaluate()`,在独立的 Bun 子进程中执行代码:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { evaluate } from 'kokkoro-plugin-eval/service';
|
|
48
|
+
|
|
49
|
+
const output = await evaluate('0.1 + 0.2');
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## 安全
|
|
53
|
+
|
|
54
|
+
建议只允许可信用户使用该插件。执行的代码拥有与 Kokkoro 进程相同的系统权限,可以读取文件和环境变量,也可以执行系统命令。
|
package/package.json
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kokkoro-plugin-eval",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "代码执行,运行 JavaScript 或 TypeScript 代码并返回执行结果。",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bot",
|
|
7
|
+
"kokkoro",
|
|
8
|
+
"eval",
|
|
9
|
+
"qq"
|
|
10
|
+
],
|
|
11
|
+
"bugs": "https://github.com/kokkorojs/kokkoro/issues",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/kokkorojs/kokkoro.git",
|
|
15
|
+
"directory": "plugins/eval"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Yuki <admin@yuki.sh>",
|
|
5
19
|
"type": "module",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./src/index.ts",
|
|
22
|
+
"./service": "./src/service.ts"
|
|
23
|
+
},
|
|
6
24
|
"files": [
|
|
7
25
|
"src"
|
|
8
26
|
],
|
|
9
|
-
"devDependencies": {
|
|
10
|
-
"@types/bun": "latest"
|
|
11
|
-
},
|
|
12
27
|
"peerDependencies": {
|
|
13
|
-
"
|
|
28
|
+
"@kokkoro/core": "^3.1.4",
|
|
29
|
+
"typescript": "^6.0.3"
|
|
14
30
|
}
|
|
15
31
|
}
|
package/src/index.ts
CHANGED
|
@@ -1 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
import { useCommand, useLogger } from '@kokkoro/core';
|
|
2
|
+
|
|
3
|
+
import { evaluate } from './service';
|
|
4
|
+
|
|
5
|
+
const logger = useLogger();
|
|
6
|
+
|
|
7
|
+
export default () => {
|
|
8
|
+
useCommand('/执行 <code>', async context => {
|
|
9
|
+
const source = context.trigger === 'shortcut' ? context.args.code : context.content.replace(/^.*?\/执行\s+/s, '');
|
|
10
|
+
|
|
11
|
+
logger.debug('开始执行代码', { source });
|
|
12
|
+
|
|
13
|
+
const output = await evaluate(source);
|
|
14
|
+
|
|
15
|
+
logger.debug('代码执行结果', { output });
|
|
16
|
+
logger.info('已执行代码');
|
|
17
|
+
|
|
18
|
+
return output;
|
|
19
|
+
}).shortcut(/^>\s*(?<code>.+)$/s);
|
|
20
|
+
};
|
package/src/service.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { spawn } from 'bun';
|
|
2
|
+
|
|
3
|
+
const { EVAL_TIMEOUT: TIMEOUT = 1000 * 60, EVAL_MAX_BUFFER: MAX_BUFFER = 1024 * 64 } = import.meta.env;
|
|
4
|
+
|
|
5
|
+
/** 在独立的 Bun 子进程中执行代码,并返回标准输出和错误输出。 */
|
|
6
|
+
export async function evaluate(source: string): Promise<string | undefined> {
|
|
7
|
+
const signal = AbortSignal.timeout(Number(TIMEOUT));
|
|
8
|
+
const subprocess = spawn(['bun', '--print', source], {
|
|
9
|
+
stderr: 'pipe',
|
|
10
|
+
signal,
|
|
11
|
+
killSignal: 'SIGKILL',
|
|
12
|
+
maxBuffer: Number(MAX_BUFFER),
|
|
13
|
+
});
|
|
14
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
15
|
+
subprocess.stdout.text(),
|
|
16
|
+
subprocess.stderr.text(),
|
|
17
|
+
subprocess.exited,
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
if (signal.aborted) {
|
|
21
|
+
throw new Error('代码执行超时');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (exitCode) {
|
|
25
|
+
throw new Error(stderr.trim() || `代码执行失败,退出码 ${exitCode}`);
|
|
26
|
+
}
|
|
27
|
+
return [stdout.trim(), stderr.trim()].filter(Boolean).join('\n') || undefined;
|
|
28
|
+
}
|