brsh 2.8.1 → 3.0.0
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/.github/workflows/release.yml +3 -1
- package/CHANGELOG.md +25 -0
- package/bin/brsh-shell.js +16 -131
- package/bin/exportFileSystem.js +4 -0
- package/bin/zsh-server.js +261 -0
- package/dist/brsh.css +1 -1
- package/dist/shell.es.js +783 -418
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +30 -15
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.cjs.js +77 -100
- package/dist/terminal.cjs.js.map +1 -1
- package/dist/terminal.es.js +1382 -603
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +77 -100
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +266 -116
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +7 -5
- package/dist/wbt.min.js.map +1 -1
- package/index.js +472 -0
- package/lib/Command.js +30 -0
- package/lib/InteractiveCommand.js +28 -0
- package/lib/ReplCommand.js +15 -0
- package/lib/Terminal.js +769 -510
- package/lib/TtyStream.js +181 -0
- package/lib/builtins/cat.js +2 -2
- package/lib/builtins/grep.js +2 -2
- package/lib/local/clear.js +3 -0
- package/lib/terminal/DomRenderer.js +325 -0
- package/lib/terminal/PtyBridge.js +169 -0
- package/lib/terminal/ScreenBuffer.js +594 -0
- package/lib/terminal/VtParser.js +728 -0
- package/package.json +4 -2
- package/styles/terminal.css +33 -4
- package/test/terminal.test.js +762 -40
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const PY_PTY_BRIDGE = `
|
|
4
|
+
import os, sys, pty, select, fcntl, termios, struct, signal, json
|
|
5
|
+
|
|
6
|
+
cmd = sys.argv[1]
|
|
7
|
+
args = json.loads(sys.argv[2])
|
|
8
|
+
cols = int(sys.argv[3])
|
|
9
|
+
rows = int(sys.argv[4])
|
|
10
|
+
cwd = sys.argv[5]
|
|
11
|
+
|
|
12
|
+
pid, fd = pty.fork()
|
|
13
|
+
if pid == 0:
|
|
14
|
+
if cwd:
|
|
15
|
+
try: os.chdir(cwd)
|
|
16
|
+
except Exception: pass
|
|
17
|
+
os.execvp(cmd, [cmd] + args)
|
|
18
|
+
|
|
19
|
+
def set_winsize(c, r):
|
|
20
|
+
try:
|
|
21
|
+
fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", r, c, 0, 0))
|
|
22
|
+
os.kill(pid, signal.SIGWINCH)
|
|
23
|
+
except Exception:
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
set_winsize(cols, rows)
|
|
27
|
+
stdin_fd = sys.stdin.fileno()
|
|
28
|
+
stdout_fd = sys.stdout.fileno()
|
|
29
|
+
|
|
30
|
+
try:
|
|
31
|
+
while True:
|
|
32
|
+
rlist, _, _ = select.select([fd, stdin_fd], [], [])
|
|
33
|
+
if fd in rlist:
|
|
34
|
+
try:
|
|
35
|
+
data = os.read(fd, 8192)
|
|
36
|
+
if not data:
|
|
37
|
+
break
|
|
38
|
+
os.write(stdout_fd, data)
|
|
39
|
+
except OSError:
|
|
40
|
+
break
|
|
41
|
+
if stdin_fd in rlist:
|
|
42
|
+
try:
|
|
43
|
+
chunk = os.read(stdin_fd, 8192)
|
|
44
|
+
if not chunk:
|
|
45
|
+
break
|
|
46
|
+
# Check for inline resize control packet: \\x1b]9999;cols;rows\\x07
|
|
47
|
+
while b"\\x1b]9999;" in chunk:
|
|
48
|
+
start = chunk.find(b"\\x1b]9999;")
|
|
49
|
+
end = chunk.find(b"\\x07", start)
|
|
50
|
+
if end == -1:
|
|
51
|
+
break
|
|
52
|
+
packet = chunk[start+7:end].decode("ascii", "ignore")
|
|
53
|
+
parts = packet.split(";")
|
|
54
|
+
if len(parts) == 2:
|
|
55
|
+
set_winsize(int(parts[0]), int(parts[1]))
|
|
56
|
+
chunk = chunk[:start] + chunk[end+1:]
|
|
57
|
+
if chunk:
|
|
58
|
+
os.write(fd, chunk)
|
|
59
|
+
except OSError:
|
|
60
|
+
break
|
|
61
|
+
finally:
|
|
62
|
+
try:
|
|
63
|
+
os.close(fd)
|
|
64
|
+
except Exception:
|
|
65
|
+
pass
|
|
66
|
+
try:
|
|
67
|
+
_, status = os.waitpid(pid, 0)
|
|
68
|
+
sys.exit(os.waitstatus_to_exitcode(status) if hasattr(os, "waitstatus_to_exitcode") else 0)
|
|
69
|
+
except Exception:
|
|
70
|
+
sys.exit(0)
|
|
71
|
+
`;
|
|
72
|
+
|
|
73
|
+
function getDefaultShell(req) {
|
|
74
|
+
if (typeof process !== 'undefined' && process.env && process.env.SHELL) {
|
|
75
|
+
return process.env.SHELL;
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
const fs = req ? req('fs') : null;
|
|
79
|
+
if (fs) {
|
|
80
|
+
for (const candidate of ['/bin/zsh', '/usr/bin/zsh', '/bin/bash', '/usr/bin/bash', '/bin/sh']) {
|
|
81
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} catch (_) {}
|
|
85
|
+
return '/bin/sh';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function spawnPty(command, args = [], options = {}) {
|
|
89
|
+
// Dynamic require so browser bundles don't fail trying to bundle child_process
|
|
90
|
+
const req = typeof require === 'function'
|
|
91
|
+
? require
|
|
92
|
+
: (typeof globalThis !== 'undefined' && globalThis.process && globalThis.process.getBuiltinModule
|
|
93
|
+
? (m) => globalThis.process.getBuiltinModule(m)
|
|
94
|
+
: null);
|
|
95
|
+
|
|
96
|
+
if (!req) {
|
|
97
|
+
throw new Error('spawnPty is only available in a Node.js environment');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const shellCmd = command || getDefaultShell(req);
|
|
101
|
+
const { spawn } = req('child_process');
|
|
102
|
+
const cols = options.cols || 80;
|
|
103
|
+
const rows = options.rows || 24;
|
|
104
|
+
const cwd = options.cwd || (typeof process !== 'undefined' ? process.cwd() : '');
|
|
105
|
+
const env = Object.assign(
|
|
106
|
+
{},
|
|
107
|
+
typeof process !== 'undefined' ? process.env : {},
|
|
108
|
+
{
|
|
109
|
+
TERM: 'xterm-256color',
|
|
110
|
+
COLORTERM: 'truecolor',
|
|
111
|
+
COLUMNS: String(cols),
|
|
112
|
+
LINES: String(rows)
|
|
113
|
+
},
|
|
114
|
+
options.env || {}
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const child = spawn(
|
|
118
|
+
'python3',
|
|
119
|
+
['-u', '-c', PY_PTY_BRIDGE, shellCmd, JSON.stringify(args), String(cols), String(rows), cwd],
|
|
120
|
+
{
|
|
121
|
+
env,
|
|
122
|
+
stdio: ['pipe', 'pipe', 'pipe']
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const listeners = new Map();
|
|
127
|
+
const emit = (ev, ...a) => {
|
|
128
|
+
const list = listeners.get(ev);
|
|
129
|
+
if (!list) return;
|
|
130
|
+
for (const fn of [...list]) fn(...a);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
child.stdout.on('data', (chunk) => emit('data', chunk));
|
|
134
|
+
child.on('exit', (code, signal) => emit('exit', code, signal));
|
|
135
|
+
child.on('close', (code) => emit('close', code));
|
|
136
|
+
child.on('error', (err) => emit('error', err));
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
pid: child.pid,
|
|
140
|
+
process: child,
|
|
141
|
+
write(data) {
|
|
142
|
+
if (child.stdin && !child.stdin.destroyed) {
|
|
143
|
+
child.stdin.write(data);
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
resize(c, r) {
|
|
147
|
+
if (child.stdin && !child.stdin.destroyed) {
|
|
148
|
+
child.stdin.write(`\x1b]9999;${Math.max(1, c)};${Math.max(1, r)}\x07`);
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
on(event, fn) {
|
|
152
|
+
if (!listeners.has(event)) listeners.set(event, []);
|
|
153
|
+
listeners.get(event).push(fn);
|
|
154
|
+
return this;
|
|
155
|
+
},
|
|
156
|
+
removeListener(event, fn) {
|
|
157
|
+
const list = listeners.get(event);
|
|
158
|
+
if (!list) return this;
|
|
159
|
+
const idx = list.indexOf(fn);
|
|
160
|
+
if (idx !== -1) list.splice(idx, 1);
|
|
161
|
+
return this;
|
|
162
|
+
},
|
|
163
|
+
kill(sig = 'SIGTERM') {
|
|
164
|
+
try { child.kill(sig); } catch (e) { /* ignore */ }
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export default spawnPty;
|