kokkoro-plugin-eval 1.0.2 → 1.0.4

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/README.md CHANGED
@@ -28,16 +28,7 @@ bun add kokkoro-plugin-eval
28
28
  > await fetch('https://example.com').then(response => response.status)
29
29
  ```
30
30
 
31
- 群聊需要开启「获取群内全部消息」权限。
32
-
33
- ## 环境变量
34
-
35
- `EVAL_TIMEOUT` 和 `EVAL_MAX_BUFFER` 用于自定义插件的执行超时时间和输出上限,单位分别为毫秒和字节。默认值如下:
36
-
37
- ```ini
38
- EVAL_TIMEOUT=60000
39
- EVAL_MAX_BUFFER=65536
40
- ```
31
+ 要让普通群消息触发快捷方式,需要开启「获取群内全部消息」权限。
41
32
 
42
33
  ## API
43
34
 
@@ -51,4 +42,13 @@ const output = await evaluate('0.1 + 0.2');
51
42
 
52
43
  ## 安全
53
44
 
54
- 建议只允许可信用户使用该插件。执行的代码拥有与 Kokkoro 进程相同的系统权限,可以读取文件和环境变量,也可以执行系统命令。
45
+ 建议只允许可信用户使用该插件。代码在子进程中执行,不继承 Kokkoro 主进程的环境变量,但拥有相同的系统权限,可以读取文件和执行系统命令。
46
+
47
+ ## 环境变量
48
+
49
+ `EVAL_TIMEOUT` 和 `EVAL_MAX_BUFFER` 用于自定义插件的执行超时时间和输出上限,单位分别为毫秒和字节。默认值如下:
50
+
51
+ ```ini
52
+ EVAL_TIMEOUT=10000
53
+ EVAL_MAX_BUFFER=32768
54
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kokkoro-plugin-eval",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "代码执行,运行 JavaScript 或 TypeScript 代码并返回执行结果。",
5
5
  "keywords": [
6
6
  "bot",
package/src/index.ts CHANGED
@@ -7,12 +7,8 @@ const logger = useLogger();
7
7
  export default () => {
8
8
  useCommand('/执行 <code>', async context => {
9
9
  const source = context.trigger === 'shortcut' ? context.args.code : context.content.replace(/^.*?\/执行\s+/s, '');
10
+ const output = await evaluate(source, logger);
10
11
 
11
- logger.debug('开始执行代码', { source });
12
-
13
- const output = await evaluate(source);
14
-
15
- logger.debug('代码执行结果', { output });
16
12
  logger.info('已执行代码');
17
13
 
18
14
  return output;
package/src/service.ts CHANGED
@@ -1,11 +1,21 @@
1
1
  import { spawn } from 'bun';
2
2
 
3
- const { EVAL_TIMEOUT: TIMEOUT = 1000 * 60, EVAL_MAX_BUFFER: MAX_BUFFER = 1024 * 64 } = import.meta.env;
3
+ import { type Logger } from '@kokkoro/core';
4
4
 
5
- /** 在独立的 Bun 子进程中执行代码,并返回标准输出和错误输出。 */
6
- export async function evaluate(source: string): Promise<string | undefined> {
5
+ const { EVAL_TIMEOUT: TIMEOUT = '10000', EVAL_MAX_BUFFER: MAX_BUFFER = '32768' } = import.meta.env;
6
+
7
+ /**
8
+ * 在独立的 Bun 子进程中执行代码,并返回标准输出和错误输出。
9
+ *
10
+ * @param logger - 在 debug 日志中记录代码、原始标准输出、原始错误输出和退出码。
11
+ */
12
+ export async function evaluate(source: string, logger?: Logger): Promise<string | undefined> {
7
13
  const signal = AbortSignal.timeout(Number(TIMEOUT));
8
- const subprocess = spawn(['bun', '--print', source], {
14
+
15
+ logger?.debug('开始执行代码', { source });
16
+
17
+ const subprocess = spawn([process.execPath, '--no-env-file', '--print', source], {
18
+ env: {},
9
19
  stderr: 'pipe',
10
20
  signal,
11
21
  killSignal: 'SIGKILL',
@@ -17,6 +27,8 @@ export async function evaluate(source: string): Promise<string | undefined> {
17
27
  subprocess.exited,
18
28
  ]);
19
29
 
30
+ logger?.debug('代码执行结果', { stdout, stderr, exitCode });
31
+
20
32
  if (signal.aborted) {
21
33
  throw new Error('代码执行超时');
22
34
  }