brsh 2.8.1 → 3.1.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.
Files changed (44) hide show
  1. package/.github/workflows/release.yml +3 -1
  2. package/CHANGELOG.md +32 -0
  3. package/POSIX-CHECKLIST.md +3 -3
  4. package/Readme.md +138 -223
  5. package/bin/Readme.md +70 -24
  6. package/bin/brsh-shell.js +16 -131
  7. package/bin/exportFileSystem.js +4 -0
  8. package/bin/zsh-server.js +261 -0
  9. package/dist/brsh.css +1 -1
  10. package/dist/shell.es.js +1020 -648
  11. package/dist/shell.es.js.map +1 -1
  12. package/dist/shell.min.js +34 -19
  13. package/dist/shell.min.js.map +1 -1
  14. package/dist/terminal.cjs.js +77 -100
  15. package/dist/terminal.cjs.js.map +1 -1
  16. package/dist/terminal.es.js +1382 -603
  17. package/dist/terminal.es.js.map +1 -1
  18. package/dist/terminal.min.js +77 -100
  19. package/dist/terminal.min.js.map +1 -1
  20. package/dist/wbt.es.js +410 -238
  21. package/dist/wbt.es.js.map +1 -1
  22. package/dist/wbt.min.js +10 -8
  23. package/dist/wbt.min.js.map +1 -1
  24. package/index.js +472 -0
  25. package/lib/Command.js +30 -0
  26. package/lib/InteractiveCommand.js +28 -0
  27. package/lib/Readme.md +111 -12
  28. package/lib/ReplCommand.js +15 -0
  29. package/lib/Terminal.js +769 -510
  30. package/lib/TtyStream.js +181 -0
  31. package/lib/WasmCommand.js +26 -5
  32. package/lib/builtins/cat.js +2 -2
  33. package/lib/builtins/grep.js +2 -2
  34. package/lib/local/clear.js +3 -0
  35. package/lib/terminal/DomRenderer.js +325 -0
  36. package/lib/terminal/PtyBridge.js +169 -0
  37. package/lib/terminal/ScreenBuffer.js +594 -0
  38. package/lib/terminal/VtParser.js +728 -0
  39. package/lib/wbt/index.js +3 -2
  40. package/lib/wbt/registry.js +15 -1
  41. package/package.json +4 -2
  42. package/styles/terminal.css +33 -4
  43. package/test/terminal.test.js +762 -40
  44. package/test/wbt.test.js +92 -0
@@ -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;