koishi-plugin-terminal 1.0.3 → 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/lib/config.d.ts +12 -0
- package/lib/helper.d.ts +4 -0
- package/lib/index.d.ts +2 -12
- package/lib/index.js +40 -28
- 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,7 @@
|
|
|
1
|
-
import { Context
|
|
1
|
+
import { Context } from 'koishi';
|
|
2
2
|
import * as pty from "node-pty";
|
|
3
|
+
import { Config } from "./config";
|
|
3
4
|
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
5
|
export interface ShellSession {
|
|
15
6
|
terminal: pty.IPty;
|
|
16
7
|
buffer: string;
|
|
@@ -20,5 +11,4 @@ export interface ShellSession {
|
|
|
20
11
|
dispose(): void;
|
|
21
12
|
}>;
|
|
22
13
|
}
|
|
23
|
-
export declare const Config: Schema<Config>;
|
|
24
14
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -30,27 +30,40 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
-
Config: () => Config,
|
|
34
33
|
apply: () => apply,
|
|
35
34
|
name: () => name
|
|
36
35
|
});
|
|
37
36
|
module.exports = __toCommonJS(src_exports);
|
|
38
37
|
var import_koishi = require("koishi");
|
|
39
38
|
var pty = __toESM(require("node-pty"));
|
|
40
|
-
var import_node_fs = require("node:fs");
|
|
41
|
-
var import_node_path = require("node:path");
|
|
42
39
|
var import_node_timers = require("node:timers");
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
40
|
+
|
|
41
|
+
// src/stripper.ts
|
|
42
|
+
function stripBackspaces(input) {
|
|
43
|
+
const chars = [];
|
|
44
|
+
for (const char of input) {
|
|
45
|
+
if (char === "\b") {
|
|
46
|
+
chars.pop();
|
|
47
|
+
} else {
|
|
48
|
+
chars.push(char);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return chars.join("");
|
|
52
|
+
}
|
|
53
|
+
__name(stripBackspaces, "stripBackspaces");
|
|
54
|
+
function stripAnsi(input) {
|
|
55
|
+
const text = stripBackspaces(input.replace(
|
|
56
|
+
// eslint-disable-next-line no-control-regex
|
|
57
|
+
/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g,
|
|
58
|
+
""
|
|
59
|
+
));
|
|
60
|
+
return text.replace(/\r\n/g, "\n").split("\n").map((line) => line.split("\r").at(-1)).join("\n");
|
|
61
|
+
}
|
|
62
|
+
__name(stripAnsi, "stripAnsi");
|
|
63
|
+
|
|
64
|
+
// src/helper.ts
|
|
65
|
+
var import_node_path = require("node:path");
|
|
66
|
+
var import_node_fs = require("node:fs");
|
|
54
67
|
function resolveShell(shell) {
|
|
55
68
|
if (shell) return shell;
|
|
56
69
|
switch (process.platform) {
|
|
@@ -64,15 +77,6 @@ function resolveShell(shell) {
|
|
|
64
77
|
return "bash";
|
|
65
78
|
}
|
|
66
79
|
__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
80
|
function getKey(session) {
|
|
77
81
|
return `${session.platform}:${session.userId}`;
|
|
78
82
|
}
|
|
@@ -93,7 +97,7 @@ function isInteractiveCommand(command) {
|
|
|
93
97
|
if (!trimmed) return false;
|
|
94
98
|
const [name2, ...args] = trimmed.split(/\s+/);
|
|
95
99
|
if (/^(vi|vim|nvim|nano|emacs)$/.test(name2)) return true;
|
|
96
|
-
if (/^(less|more
|
|
100
|
+
if (/^(less|more)$/.test(name2)) return true;
|
|
97
101
|
if (/^(top|htop|btop|watch)$/.test(name2)) return true;
|
|
98
102
|
if (/^(tmux|screen)$/.test(name2)) return true;
|
|
99
103
|
if (/^(sftp|ftp|telnet)$/.test(name2)) return true;
|
|
@@ -105,6 +109,9 @@ function isInteractiveCommand(command) {
|
|
|
105
109
|
return false;
|
|
106
110
|
}
|
|
107
111
|
__name(isInteractiveCommand, "isInteractiveCommand");
|
|
112
|
+
|
|
113
|
+
// src/index.ts
|
|
114
|
+
var name = "terminal";
|
|
108
115
|
var map = /* @__PURE__ */ new Map();
|
|
109
116
|
function apply(ctx, config) {
|
|
110
117
|
fixNodePtyHelper();
|
|
@@ -134,7 +141,13 @@ function apply(ctx, config) {
|
|
|
134
141
|
cols: config.cols,
|
|
135
142
|
rows: config.rows,
|
|
136
143
|
cwd: config.root,
|
|
137
|
-
env:
|
|
144
|
+
env: {
|
|
145
|
+
...process.env,
|
|
146
|
+
PAGER: "cat",
|
|
147
|
+
MANPAGER: "cat",
|
|
148
|
+
GIT_PAGER: "cat",
|
|
149
|
+
LESS: "-FRX"
|
|
150
|
+
}
|
|
138
151
|
});
|
|
139
152
|
const shellSession = {
|
|
140
153
|
terminal,
|
|
@@ -147,7 +160,7 @@ function apply(ctx, config) {
|
|
|
147
160
|
shellSession.buffer = "";
|
|
148
161
|
if (!output) return;
|
|
149
162
|
const text = output.length > config.maxOutputLength ? output.slice(0, config.maxOutputLength - 1) + "\n ...Truncated output" : output;
|
|
150
|
-
await session.send(text);
|
|
163
|
+
await session.send(import_koishi.h.text(text));
|
|
151
164
|
}, "flush");
|
|
152
165
|
const dataDisposable = terminal.onData((data) => {
|
|
153
166
|
shellSession.buffer += data;
|
|
@@ -179,7 +192,7 @@ function apply(ctx, config) {
|
|
|
179
192
|
}
|
|
180
193
|
}
|
|
181
194
|
__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,
|
|
195
|
+
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
196
|
if (!allowedUsers.includes(session.userId)) {
|
|
184
197
|
return "Unauthorized user.";
|
|
185
198
|
}
|
|
@@ -222,7 +235,6 @@ function apply(ctx, config) {
|
|
|
222
235
|
__name(apply, "apply");
|
|
223
236
|
// Annotate the CommonJS export names for ESM import in node:
|
|
224
237
|
0 && (module.exports = {
|
|
225
|
-
Config,
|
|
226
238
|
apply,
|
|
227
239
|
name
|
|
228
240
|
});
|
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.4",
|
|
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",
|