koishi-plugin-terminal 1.0.3 → 1.0.5
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/config.d.ts +12 -0
- package/lib/helper.d.ts +4 -0
- package/lib/index.d.ts +3 -12
- package/lib/index.js +54 -27
- package/lib/stripper.d.ts +2 -0
- package/package.json +5 -1
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Schema } from "koishi";
|
|
2
|
+
export interface Config {
|
|
3
|
+
admin?: Array<string>;
|
|
4
|
+
auth?: number;
|
|
5
|
+
root?: string;
|
|
6
|
+
shell?: string;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
cols?: number;
|
|
9
|
+
rows?: number;
|
|
10
|
+
maxOutputLength: number;
|
|
11
|
+
}
|
|
12
|
+
export declare const Config: Schema<Config>;
|
package/lib/helper.d.ts
ADDED
package/lib/index.d.ts
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
|
-
import { Context
|
|
1
|
+
import { Context } from 'koishi';
|
|
2
2
|
import * as pty from "node-pty";
|
|
3
|
+
import { Config } from "./config";
|
|
4
|
+
export * from './config';
|
|
3
5
|
export declare const name = "terminal";
|
|
4
|
-
export interface Config {
|
|
5
|
-
admin?: Array<string>;
|
|
6
|
-
auth?: number;
|
|
7
|
-
root?: string;
|
|
8
|
-
shell?: string;
|
|
9
|
-
timeout?: number;
|
|
10
|
-
cols?: number;
|
|
11
|
-
rows?: number;
|
|
12
|
-
maxOutputLength: number;
|
|
13
|
-
}
|
|
14
6
|
export interface ShellSession {
|
|
15
7
|
terminal: pty.IPty;
|
|
16
8
|
buffer: string;
|
|
@@ -20,5 +12,4 @@ export interface ShellSession {
|
|
|
20
12
|
dispose(): void;
|
|
21
13
|
}>;
|
|
22
14
|
}
|
|
23
|
-
export declare const Config: Schema<Config>;
|
|
24
15
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -35,22 +35,36 @@ __export(src_exports, {
|
|
|
35
35
|
name: () => name
|
|
36
36
|
});
|
|
37
37
|
module.exports = __toCommonJS(src_exports);
|
|
38
|
-
var
|
|
38
|
+
var import_koishi2 = require("koishi");
|
|
39
39
|
var pty = __toESM(require("node-pty"));
|
|
40
|
-
var import_node_fs = require("node:fs");
|
|
41
|
-
var import_node_path = require("node:path");
|
|
42
40
|
var import_node_timers = require("node:timers");
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
41
|
+
|
|
42
|
+
// src/stripper.ts
|
|
43
|
+
function stripBackspaces(input) {
|
|
44
|
+
const chars = [];
|
|
45
|
+
for (const char of input) {
|
|
46
|
+
if (char === "\b") {
|
|
47
|
+
chars.pop();
|
|
48
|
+
} else {
|
|
49
|
+
chars.push(char);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return chars.join("");
|
|
53
|
+
}
|
|
54
|
+
__name(stripBackspaces, "stripBackspaces");
|
|
55
|
+
function stripAnsi(input) {
|
|
56
|
+
const text = stripBackspaces(input.replace(
|
|
57
|
+
// eslint-disable-next-line no-control-regex
|
|
58
|
+
/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g,
|
|
59
|
+
""
|
|
60
|
+
));
|
|
61
|
+
return text.replace(/\r\n/g, "\n").split("\n").map((line) => line.split("\r").at(-1)).join("\n");
|
|
62
|
+
}
|
|
63
|
+
__name(stripAnsi, "stripAnsi");
|
|
64
|
+
|
|
65
|
+
// src/helper.ts
|
|
66
|
+
var import_node_path = require("node:path");
|
|
67
|
+
var import_node_fs = require("node:fs");
|
|
54
68
|
function resolveShell(shell) {
|
|
55
69
|
if (shell) return shell;
|
|
56
70
|
switch (process.platform) {
|
|
@@ -64,15 +78,6 @@ function resolveShell(shell) {
|
|
|
64
78
|
return "bash";
|
|
65
79
|
}
|
|
66
80
|
__name(resolveShell, "resolveShell");
|
|
67
|
-
function stripAnsi(input) {
|
|
68
|
-
const text = input.replace(
|
|
69
|
-
// eslint-disable-next-line no-control-regex
|
|
70
|
-
/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g,
|
|
71
|
-
""
|
|
72
|
-
);
|
|
73
|
-
return text.replace(/\r\n/g, "\n").split("\n").map((line) => line.split("\r").at(-1)).join("\n");
|
|
74
|
-
}
|
|
75
|
-
__name(stripAnsi, "stripAnsi");
|
|
76
81
|
function getKey(session) {
|
|
77
82
|
return `${session.platform}:${session.userId}`;
|
|
78
83
|
}
|
|
@@ -93,7 +98,7 @@ function isInteractiveCommand(command) {
|
|
|
93
98
|
if (!trimmed) return false;
|
|
94
99
|
const [name2, ...args] = trimmed.split(/\s+/);
|
|
95
100
|
if (/^(vi|vim|nvim|nano|emacs)$/.test(name2)) return true;
|
|
96
|
-
if (/^(less|more
|
|
101
|
+
if (/^(less|more)$/.test(name2)) return true;
|
|
97
102
|
if (/^(top|htop|btop|watch)$/.test(name2)) return true;
|
|
98
103
|
if (/^(tmux|screen)$/.test(name2)) return true;
|
|
99
104
|
if (/^(sftp|ftp|telnet)$/.test(name2)) return true;
|
|
@@ -105,6 +110,22 @@ function isInteractiveCommand(command) {
|
|
|
105
110
|
return false;
|
|
106
111
|
}
|
|
107
112
|
__name(isInteractiveCommand, "isInteractiveCommand");
|
|
113
|
+
|
|
114
|
+
// src/config.ts
|
|
115
|
+
var import_koishi = require("koishi");
|
|
116
|
+
var Config = import_koishi.Schema.object({
|
|
117
|
+
admin: import_koishi.Schema.array(String).description("超级管理员用户名单").default([]),
|
|
118
|
+
auth: import_koishi.Schema.number().description("使用本插件所需的最低权限,此外,用户也需要在超级管理员名单中。").min(1).max(4).step(1).default(4),
|
|
119
|
+
root: import_koishi.Schema.string().description("初始工作路径").default(process.env.HOME),
|
|
120
|
+
shell: import_koishi.Schema.string().description("Shell路径,留空则自动检测系统默认Shell"),
|
|
121
|
+
timeout: import_koishi.Schema.number().description("超时时长").default(import_koishi.Time.minute),
|
|
122
|
+
cols: import_koishi.Schema.number().description("终端列数").default(80),
|
|
123
|
+
rows: import_koishi.Schema.number().description("终端行数").default(24),
|
|
124
|
+
maxOutputLength: import_koishi.Schema.number().description("单次发送最大输出长度").default(16384)
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// src/index.ts
|
|
128
|
+
var name = "terminal";
|
|
108
129
|
var map = /* @__PURE__ */ new Map();
|
|
109
130
|
function apply(ctx, config) {
|
|
110
131
|
fixNodePtyHelper();
|
|
@@ -134,7 +155,13 @@ function apply(ctx, config) {
|
|
|
134
155
|
cols: config.cols,
|
|
135
156
|
rows: config.rows,
|
|
136
157
|
cwd: config.root,
|
|
137
|
-
env:
|
|
158
|
+
env: {
|
|
159
|
+
...process.env,
|
|
160
|
+
PAGER: "cat",
|
|
161
|
+
MANPAGER: "cat",
|
|
162
|
+
GIT_PAGER: "cat",
|
|
163
|
+
LESS: "-FRX"
|
|
164
|
+
}
|
|
138
165
|
});
|
|
139
166
|
const shellSession = {
|
|
140
167
|
terminal,
|
|
@@ -147,7 +174,7 @@ function apply(ctx, config) {
|
|
|
147
174
|
shellSession.buffer = "";
|
|
148
175
|
if (!output) return;
|
|
149
176
|
const text = output.length > config.maxOutputLength ? output.slice(0, config.maxOutputLength - 1) + "\n ...Truncated output" : output;
|
|
150
|
-
await session.send(text);
|
|
177
|
+
await session.send(import_koishi2.h.text(text));
|
|
151
178
|
}, "flush");
|
|
152
179
|
const dataDisposable = terminal.onData((data) => {
|
|
153
180
|
shellSession.buffer += data;
|
|
@@ -179,7 +206,7 @@ function apply(ctx, config) {
|
|
|
179
206
|
}
|
|
180
207
|
}
|
|
181
208
|
__name(cleanupSession, "cleanupSession");
|
|
182
|
-
ctx.command("shell [command:text]", "Start a persistent shell session", { authority: config.auth }).option("terminate", "-t Terminate current shell session").usage("After start up,
|
|
209
|
+
ctx.command("shell [command:text]", "Start a persistent shell session.", { authority: config.auth }).option("terminate", "-t Terminate current shell session").usage("After start up, user messages will be sent to shell process.").example("shell echo Operating System: Three Easy Pieces > qljj.txt").action(async ({ session, options }, command) => {
|
|
183
210
|
if (!allowedUsers.includes(session.userId)) {
|
|
184
211
|
return "Unauthorized user.";
|
|
185
212
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-terminal",
|
|
3
3
|
"description": "通过 QQ 运行持久的 Shell 终端",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.5",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"author": {
|
|
7
|
+
"email": "ygong68@wisc.edu",
|
|
8
|
+
"name": "Jingmozhiyu"
|
|
9
|
+
},
|
|
6
10
|
"typings": "lib/index.d.ts",
|
|
7
11
|
"files": [
|
|
8
12
|
"lib",
|