brsh 3.0.0 → 3.1.1
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/CHANGELOG.md +14 -0
- package/POSIX-CHECKLIST.md +3 -3
- package/Readme.md +138 -223
- package/bin/Readme.md +70 -24
- package/dist/shell.es.js +353 -334
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +7 -7
- package/dist/shell.min.js.map +1 -1
- package/dist/wbt.es.js +198 -176
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +7 -7
- package/dist/wbt.min.js.map +1 -1
- package/index.js +20 -0
- package/lib/Readme.md +111 -12
- package/lib/WasmCommand.js +26 -5
- package/lib/wbt/index.js +3 -2
- package/lib/wbt/registry.js +15 -1
- package/package.json +1 -1
- package/test/shell.test.js +49 -0
- package/test/wbt.test.js +92 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [3.1.1](https://github.com/eliotstocker/Brsh-JS/compare/v3.1.0...v3.1.1) (2026-11-08)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **shell:** handle backslash quote escaping in _splitCommand ([692cc9c](https://github.com/eliotstocker/Brsh-JS/commit/692cc9c2852b5ed2b2ae94e9e7a1b3cacce9ada9))
|
|
7
|
+
|
|
8
|
+
# [3.1.0](https://github.com/eliotstocker/Brsh-JS/compare/v3.0.0...v3.1.0) (2026-11-08)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* **wbt:** add nano-wasm to curated package registry ([bb78039](https://github.com/eliotstocker/Brsh-JS/commit/bb78039c067c6b3fd6f902ebe74cb5cd2ccdba99))
|
|
14
|
+
|
|
1
15
|
# [3.0.0](https://github.com/eliotstocker/Brsh-JS/compare/v2.8.1...v3.0.0) (2026-11-08)
|
|
2
16
|
|
|
3
17
|
|
package/POSIX-CHECKLIST.md
CHANGED
|
@@ -66,7 +66,7 @@ Track progress toward a POSIX-compliant `sh` implementation in Brsh-JS.
|
|
|
66
66
|
- [x] `export VAR=val`
|
|
67
67
|
- [x] `return [n]`
|
|
68
68
|
- [x] `set` (flags + positional params)
|
|
69
|
-
- [x] `source` / `. script`
|
|
69
|
+
- [x] `source` / `. script`
|
|
70
70
|
- [x] `.` — dot command (alias for `source`)
|
|
71
71
|
- [ ] `:` — no-op, always exits 0
|
|
72
72
|
- [x] `break [n]`
|
|
@@ -118,7 +118,7 @@ Track progress toward a POSIX-compliant `sh` implementation in Brsh-JS.
|
|
|
118
118
|
- [x] `cd` / `pwd`
|
|
119
119
|
- [x] `touch`
|
|
120
120
|
- [ ] `find`
|
|
121
|
-
- [x] `ln` (symlinks
|
|
121
|
+
- [x] `ln` (`-s` symlinks on virtual & real FS, hard links on real FS, `-f` force)
|
|
122
122
|
- [x] `stat` (real FS: full lstat output; virtual FS: size, type, mode)
|
|
123
123
|
|
|
124
124
|
### Path Utilities
|
|
@@ -132,7 +132,7 @@ Track progress toward a POSIX-compliant `sh` implementation in Brsh-JS.
|
|
|
132
132
|
- [ ] `env` / `printenv`
|
|
133
133
|
- [ ] `kill`
|
|
134
134
|
- [ ] `xargs`
|
|
135
|
-
- [
|
|
135
|
+
- [x] `test` / `[` (standalone `test` and `[` builtins)
|
|
136
136
|
- [x] `which`
|
|
137
137
|
- [x] `true`
|
|
138
138
|
- [x] `false`
|
package/Readme.md
CHANGED
|
@@ -1,32 +1,33 @@
|
|
|
1
1
|
# BRowserSHell (brsh)
|
|
2
2
|
[](https://badge.fury.io/js/brsh)
|
|
3
3
|
|
|
4
|
-
An extensible POSIX
|
|
4
|
+
An extensible POSIX-like shell written in JavaScript with an accompanying VT100/ANSI terminal emulator.
|
|
5
5
|
|
|
6
6
|
[Demo](http://eliotstocker.github.io/Brsh-JS/)
|
|
7
7
|
|
|
8
8
|
## What is it?
|
|
9
|
-
A POSIX
|
|
9
|
+
A POSIX-like shell backend and VT100 terminal emulator that lets you embed a working terminal in a web app, connect to a real OS PTY / WebSocket stream, or run an interactive shell against the real filesystem in Node.js.
|
|
10
10
|
|
|
11
11
|
## Why?
|
|
12
12
|
No idea, you tell me
|
|
13
13
|
|
|
14
14
|
## Known Issues
|
|
15
|
-
* Input on
|
|
15
|
+
* Input on Android is not working
|
|
16
16
|
|
|
17
17
|
# How?
|
|
18
|
-
To create a new terminal
|
|
18
|
+
To create a new terminal in the browser, include the stylesheet, `Shell`, and `Terminal` on your page:
|
|
19
19
|
```html
|
|
20
|
-
<
|
|
21
|
-
<script src="//unpkg.com/brsh@
|
|
20
|
+
<link rel="stylesheet" href="//unpkg.com/brsh@3/dist/brsh.css">
|
|
21
|
+
<script src="//unpkg.com/brsh@3/dist/shell.min.js"></script>
|
|
22
|
+
<script src="//unpkg.com/brsh@3/dist/terminal.min.js"></script>
|
|
22
23
|
```
|
|
23
24
|
|
|
24
|
-
You may also use it as a
|
|
25
|
+
You may also use it as a Node module. Install via npm:
|
|
25
26
|
```shell
|
|
26
27
|
npm i brsh
|
|
27
28
|
```
|
|
28
29
|
|
|
29
|
-
Import both Shell and Terminal in your code:
|
|
30
|
+
Import both `Shell` and `Terminal` in your code:
|
|
30
31
|
```js
|
|
31
32
|
// ESM
|
|
32
33
|
import Shell from 'brsh';
|
|
@@ -37,7 +38,7 @@ const Shell = require('brsh');
|
|
|
37
38
|
const Terminal = require('brsh/terminal');
|
|
38
39
|
```
|
|
39
40
|
|
|
40
|
-
You may then
|
|
41
|
+
You may then instantiate the terminal like so:
|
|
41
42
|
```js
|
|
42
43
|
const term = new Terminal({
|
|
43
44
|
el: document.body,
|
|
@@ -50,48 +51,79 @@ const term = new Terminal({
|
|
|
50
51
|
});
|
|
51
52
|
```
|
|
52
53
|
|
|
53
|
-
|
|
54
|
+
### Terminal-Specific Options
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
All [Shell options](#shell-options) are also accepted and forwarded directly to the underlying `Shell` instance (unless `stream` is provided or `Shell: false` is set).
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
| option | required | default | description |
|
|
58
|
+
| Option | Required | Default | Description |
|
|
60
59
|
| ------ | -------- | ------- | ----------- |
|
|
61
|
-
| el | Yes | |
|
|
62
|
-
| cli
|
|
63
|
-
| Shell | No |
|
|
64
|
-
|
|
|
65
|
-
|
|
|
66
|
-
|
|
|
67
|
-
|
|
|
68
|
-
|
|
|
69
|
-
|
|
|
60
|
+
| el | Yes | | An `HTMLElement` to mount the terminal emulator into |
|
|
61
|
+
| cli / shell | No | | An existing `Shell` instance to attach the terminal emulator to |
|
|
62
|
+
| Shell | No | `Shell` | Custom `Shell` constructor to use, or `false`/`null` to run the terminal without a `Shell` instance |
|
|
63
|
+
| stream | No | `null` | A Duplex stream or WebSocket to attach directly to the terminal (bypasses creating a `Shell`) |
|
|
64
|
+
| cols | No | `80` | Initial terminal width in columns (automatically re-measured when mounted in the DOM) |
|
|
65
|
+
| rows | No | `24` | Initial terminal height in rows (automatically re-measured when mounted in the DOM) |
|
|
66
|
+
| maxScrollback | No | `1000` | Maximum number of scrollback lines retained in the primary screen buffer |
|
|
67
|
+
| font | No | `Roboto Mono` | Name of a Google Webfont to load for the console font; a monospace font is recommended |
|
|
68
|
+
| cursor | No | `none` | One of: `none`, `block`, `blink`. If `block` or `blink`, a cursor block character is shown at the cursor position |
|
|
69
|
+
| outputAnimation | No | `none` | One of: `none`, `type`. If `type`, characters are appended per frame for a retro typing effect |
|
|
70
|
+
| animateSpeed | No | `1` | Characters per tick (`1`–`20`) for the `type` animation. Higher is faster |
|
|
71
|
+
| onExit | No | `null` | Callback invoked with `(code)` when the attached shell or PTY session exits |
|
|
72
|
+
| mobileInput | No | `click` | One of `none` or `click`. Set to `click` to enable tap-to-focus (shows on-screen keyboard on mobile) |
|
|
70
73
|
| lineWrap | No | `false` | When `true`, long lines wrap to the next line instead of overflowing |
|
|
71
74
|
|
|
72
|
-
###
|
|
73
|
-
|
|
75
|
+
### Terminal Public Methods
|
|
76
|
+
|
|
77
|
+
`Terminal` includes a full VT100/ANSI state machine (supporting 16/256/TrueColor SGR, alternate screen buffer `?1049h`, DEC line drawing, OSC 8 hyperlinks, bracketed paste `?2004h`, and X10/UTF-8/SGR mouse tracking):
|
|
78
|
+
|
|
79
|
+
| Method | Arguments | Description |
|
|
80
|
+
| ------ | --------- | ----------- |
|
|
81
|
+
| write | `String \| Uint8Array` data | Write raw terminal output (including ANSI/VT100 escape sequences) to the screen |
|
|
82
|
+
| writeln | `String` data | Write data followed by `\r\n` |
|
|
83
|
+
| clear | | Clear the screen and scrollback buffer |
|
|
84
|
+
| reset | | Reset the VT100 parser and screen buffer to initial state |
|
|
85
|
+
| resize | `Number` cols, `Number` rows | Resize the terminal grid and notify attached shell/streams |
|
|
86
|
+
| onData | `Function` callback | Subscribe to raw terminal input sequences (keystrokes, paste, mouse reports). Returns an unsubscribe function |
|
|
87
|
+
| onResize | `Function` callback | Subscribe to `{ cols, rows }` resize events. Returns an unsubscribe function |
|
|
88
|
+
| onTitleChange | `Function` callback | Subscribe to window title changes set via OSC `0` / `2`. Returns an unsubscribe function |
|
|
89
|
+
| attach | `Stream \| WebSocket` stream | Attach an external Duplex stream, `Shell`, or WebSocket. Returns a `detach()` function |
|
|
90
|
+
| spawn | `String` cmd, `Array` args, `Object` opts | *(Node.js only)* Spawn a real OS PTY process (e.g. `'/bin/zsh'`) and attach it to the terminal. Returns `{ pty, detach }` |
|
|
91
|
+
| createTty | `Object` options | Create a Node-compatible `{ stdin, stdout, dispose }` TTY stream pair (compatible with `blessed` and `readline`) |
|
|
92
|
+
| onCommand | `String` command | Execute a command string in the attached shell (or send `command + '\r'` to an attached stream) |
|
|
93
|
+
| sendKey | `String` key, `Object` modifiers | Programmatically send a key press (e.g. `term.sendKey('ArrowLeft', { alt: true })`) |
|
|
94
|
+
| sendMouse | `String` type, `Object` event | Programmatically dispatch a mouse event (`mousedown`, `mouseup`, `mousemove`, `wheel`) at 1-based `{ col, row, button }` |
|
|
95
|
+
| getScreenText | `Object` options | Return the current visible screen text (`{ trimRight: true }`) |
|
|
96
|
+
| getLineText | `Number` row, `Boolean` trimRight | Return the text of a single 0-based screen row |
|
|
97
|
+
|
|
98
|
+
### Shell Options
|
|
99
|
+
|
|
100
|
+
| Option | Required | Default | Description |
|
|
74
101
|
| ------ | -------- | ------- | ----------- |
|
|
75
102
|
| path | No | `'/bin'` | Path in which to look for (and register) built-in binaries |
|
|
76
103
|
| profile | No | | A script to run before user interactivity (see [scripts](#writing-scripts)). If `/etc/profile` exists on the virtual filesystem, it is sourced first |
|
|
77
104
|
| motd | No | | Optional path to a Message of the Day file/script/directory, or `false` to disable. By default, executable scripts in `/etc/update-motd.d/` and `/etc/motd` are displayed before `profile` on interactive login unless `.hushlogin` exists or an initial `command` is run |
|
|
78
|
-
| command | No | `null` | An initial command string to run after `profile` loads (equivalent to `sh -c <cmd>`
|
|
105
|
+
| command | No | `null` | An initial command string to run after `profile` loads (equivalent to `sh -c <cmd>`). Suppresses MOTD and sets `c` in `$-` during initialization |
|
|
106
|
+
| interactive | No | `true` | Whether the shell is interactive (sets `i` in `$-`) |
|
|
79
107
|
| hostname | No | `'browser'` | Hostname shown in the prompt |
|
|
80
|
-
| filesystem | No | `{}` |
|
|
81
|
-
| permissions | No | `{}` | Map of virtual absolute paths to Unix mode bits (e.g. `{ '/bin/myscript.sh': 0o755 }`). Files without an entry default to `0o644`; function/class entries default to `
|
|
108
|
+
| filesystem | No | `{}` | Virtual filesystem source: a JS object, `.brfs` URL string, `Uint8Array` / `ArrayBuffer`, or `Promise`. Classes extending `Command` are treated as executables. Use [exportFileSystem](bin/Readme.md) to generate this from a real directory |
|
|
109
|
+
| permissions | No | `{}` | Map of virtual absolute paths to Unix mode bits (e.g. `{ '/bin/myscript.sh': 0o755 }`). Files without an entry default to `0o644`; function/class entries default to `0o555` |
|
|
82
110
|
| cwd | No | `'/'` | Starting working directory |
|
|
83
|
-
| onFsChange | No | `null` | Callback fired whenever the virtual filesystem changes (e.g. after a write or delete)
|
|
111
|
+
| onFsChange | No | `null` | Callback `(filesystem, permissions) => {}` fired whenever the virtual filesystem changes (e.g. after a write, chmod, or delete) |
|
|
84
112
|
| useRealFilesystem | No | `false` | When `true`, uses the real Node.js filesystem instead of the virtual one. Intended for CLI use |
|
|
85
113
|
|
|
86
|
-
## Running an
|
|
114
|
+
## Running an Interactive Shell in Node
|
|
87
115
|
|
|
88
|
-
`npm run shell` starts an interactive brsh session against the real filesystem, with tab completion:
|
|
116
|
+
`npm run shell` (or `npx brsh`) starts an interactive `brsh` session against the real filesystem, with tab completion and full-screen interactive app support:
|
|
89
117
|
|
|
90
118
|
```shell
|
|
91
119
|
npm run shell
|
|
120
|
+
# Or run the bundled virtual demo filesystem in Node:
|
|
121
|
+
npm run demo:shell
|
|
122
|
+
# Or start a browser-based real zsh PTY server on http://127.0.0.1:3080:
|
|
123
|
+
npm run demo:zsh
|
|
92
124
|
```
|
|
93
125
|
|
|
94
|
-
To use
|
|
126
|
+
To use `Shell` programmatically without `Terminal`:
|
|
95
127
|
|
|
96
128
|
```js
|
|
97
129
|
const Shell = require('brsh');
|
|
@@ -101,34 +133,50 @@ const shell = new Shell({
|
|
|
101
133
|
cwd: process.cwd(),
|
|
102
134
|
hostname: 'myapp'
|
|
103
135
|
});
|
|
136
|
+
|
|
137
|
+
// If loading an async filesystem (.brfs URL, Promise, or IndexedDB), await `shell.ready` or use `Shell.create()`:
|
|
138
|
+
await shell.ready;
|
|
104
139
|
```
|
|
105
140
|
|
|
106
|
-
## Shell Public Methods
|
|
107
|
-
|
|
108
|
-
|
|
|
109
|
-
|
|
|
110
|
-
|
|
|
111
|
-
|
|
|
112
|
-
|
|
|
113
|
-
|
|
|
141
|
+
## Shell Public Methods & Static Helpers
|
|
142
|
+
|
|
143
|
+
| Method / Property | Arguments | Description |
|
|
144
|
+
| ------ | --------- | ----------- |
|
|
145
|
+
| `onCommand` | `String` command, `Boolean` events | Run a command string in the shell, e.g. `ls -al ./root`. Returns a Promise |
|
|
146
|
+
| `sendCommand` | `String` command | Queue or run a command (automatically suppresses MOTD if called before boot completes) |
|
|
147
|
+
| `write` | `String \| Uint8Array` data | Feed raw terminal input bytes/keystrokes into the shell's built-in line editor or active interactive command |
|
|
148
|
+
| `resize` / `setTerminalSize` | `Number` cols, `Number` rows | Update `COLUMNS` and `LINES` and resize the shell's TTY streams |
|
|
149
|
+
| `tabCompletion` | `String` partial | Returns `{ path, options }` with completion candidates, or `null` if there are none |
|
|
150
|
+
| `getPrompt` | | Returns the current hostname and cwd formatted as a prompt string |
|
|
151
|
+
| `suppressMotd` | | Suppresses the startup MOTD banner and sets `c` in `$-` |
|
|
152
|
+
| `destroy` | `Number` code (optional) | Destroys the shell instance and emits `status` (`Shell.STATUS_DESTROYED`) and `exit` |
|
|
153
|
+
| `clear` | | Clears the terminal output and emits the `clear` event |
|
|
154
|
+
| `shell.ready` | | Promise that resolves to the `Shell` instance once any async filesystem source has mounted |
|
|
155
|
+
| `Shell.create` | `Object` options | Static async factory that pre-resolves async filesystems and returns an initialized `Shell` |
|
|
156
|
+
| `Shell.fromContainer` | `String \| Uint8Array` source, `Object` options | Static async helper to create a `Shell` directly from a `.brfs` container URL or byte array |
|
|
114
157
|
|
|
115
158
|
## Shell Events
|
|
116
|
-
|
|
117
|
-
|
|
|
118
|
-
|
|
|
119
|
-
|
|
|
120
|
-
|
|
|
121
|
-
|
|
|
122
|
-
|
|
|
123
|
-
|
|
|
159
|
+
|
|
160
|
+
| Event | Arguments | Description |
|
|
161
|
+
| ------ | --------- | ----------- |
|
|
162
|
+
| `stdOut` | `String` line | A line of standard output to display to the user |
|
|
163
|
+
| `stdErr` | `String` line | A line of standard error to display to the user |
|
|
164
|
+
| `stdOutRaw` | `String` chunk | Raw unbuffered chunk written directly to `stdout` |
|
|
165
|
+
| `data` | `String` chunk | Combined TTY output stream (prompts, line editing, ANSI sequences, and command output) |
|
|
166
|
+
| `exitCode` | `Number` code | Exit code of the most recently completed command |
|
|
167
|
+
| `status` | `String` status | Shell status: `Shell.STATUS_READY`, `Shell.STATUS_WORKING`, or `Shell.STATUS_DESTROYED` |
|
|
168
|
+
| `interactive`| | Fired when a full-screen `InteractiveCommand` or `ReplCommand` takes over the terminal |
|
|
169
|
+
| `resize` | `{ cols, rows }` | Fired when the terminal dimensions change |
|
|
170
|
+
| `clear` | | Fired when the terminal should clear the screen |
|
|
171
|
+
| `exit` | `Number` code | Fired when the shell is destroyed. `code` is the final exit code |
|
|
124
172
|
|
|
125
173
|
An example CLI implementation can be found in [bin/brsh-shell.js](bin/brsh-shell.js).
|
|
126
174
|
|
|
127
|
-
# Built-in and Local
|
|
175
|
+
# Built-in and Local Commands
|
|
128
176
|
|
|
129
177
|
These are simplified versions of standard Unix commands.
|
|
130
178
|
|
|
131
|
-
**Built-ins** (
|
|
179
|
+
**Built-ins** (installed into `PATH`, default `/bin`):
|
|
132
180
|
* `cat`
|
|
133
181
|
* `echo`
|
|
134
182
|
* `false`
|
|
@@ -138,14 +186,14 @@ These are simplified versions of standard Unix commands.
|
|
|
138
186
|
* `test` / `[`
|
|
139
187
|
* `true`
|
|
140
188
|
|
|
141
|
-
**Local commands
|
|
189
|
+
**Local commands** (always available in shell context):
|
|
142
190
|
* `alias`
|
|
143
191
|
* `cd`
|
|
144
192
|
* `chmod`
|
|
145
193
|
* `clear`
|
|
146
194
|
* `cp`
|
|
147
195
|
* `curl`
|
|
148
|
-
*
|
|
196
|
+
* `.` (dot) / `source`
|
|
149
197
|
* `exit`
|
|
150
198
|
* `export`
|
|
151
199
|
* `jsfunction`
|
|
@@ -160,11 +208,12 @@ These are simplified versions of standard Unix commands.
|
|
|
160
208
|
* `touch`
|
|
161
209
|
* `which`
|
|
162
210
|
|
|
163
|
-
## Adding
|
|
211
|
+
## Adding New Commands
|
|
164
212
|
|
|
165
|
-
Create a class that extends `Command
|
|
213
|
+
Create a class that extends `Shell.Command` (or `Shell.InteractiveCommand` / `Shell.ReplCommand` for full-screen TUI and REPL apps):
|
|
166
214
|
```js
|
|
167
215
|
class MyCommand extends Shell.Command {
|
|
216
|
+
static get name() { return 'mycommand'; }
|
|
168
217
|
get name() { return 'mycommand'; }
|
|
169
218
|
|
|
170
219
|
run() {
|
|
@@ -172,24 +221,19 @@ class MyCommand extends Shell.Command {
|
|
|
172
221
|
return Promise.resolve();
|
|
173
222
|
}
|
|
174
223
|
}
|
|
175
|
-
```
|
|
176
224
|
|
|
177
|
-
|
|
178
|
-
```js
|
|
179
|
-
// ESM
|
|
180
|
-
import { createRequire } from 'module';
|
|
181
|
-
const require = createRequire(import.meta.url);
|
|
182
|
-
const Command = require('brsh/lib/Command');
|
|
225
|
+
shell.context.setCommand(MyCommand);
|
|
183
226
|
```
|
|
184
227
|
|
|
185
|
-
For
|
|
228
|
+
For full documentation on `Command`, `InteractiveCommand`, `ReplCommand`, and `WasmCommand`, see the [lib docs](lib/Readme.md).
|
|
186
229
|
|
|
187
|
-
### Streaming
|
|
230
|
+
### Streaming Output
|
|
188
231
|
|
|
189
|
-
Commands can emit output in real time before they finish by calling `this.flush()
|
|
232
|
+
Commands can emit output in real time before they finish by calling `this.flush()` (or writing raw ANSI chunks to `this.stdout.write()`):
|
|
190
233
|
|
|
191
234
|
```js
|
|
192
235
|
class ProgressCommand extends Shell.Command {
|
|
236
|
+
static get name() { return 'progress'; }
|
|
193
237
|
get name() { return 'progress'; }
|
|
194
238
|
|
|
195
239
|
run() {
|
|
@@ -202,20 +246,22 @@ class ProgressCommand extends Shell.Command {
|
|
|
202
246
|
|
|
203
247
|
## Writing Scripts
|
|
204
248
|
|
|
205
|
-
Scripts must start with
|
|
249
|
+
Scripts must start with a supported shebang (`#!/bin/sh`, `#!/bin/brsh`, or legacy `#!/sh.js`):
|
|
206
250
|
|
|
207
251
|
```sh
|
|
208
|
-
#!/sh
|
|
252
|
+
#!/bin/sh
|
|
209
253
|
echo "Hello from a script"
|
|
210
254
|
```
|
|
211
255
|
|
|
212
|
-
Scripts
|
|
256
|
+
Scripts support `if`/`elif`/`else`/`fi`, `for ... in ...; do ... done`, `while ...; do ... done`, `case ... in ... esac`, functions, pipes (`|`), logical operators (`&&`, `||`, `;`), output redirection (`>`, `>>`), command substitution `$(...)`, arithmetic `$((...))`, and `set -e` / `set -x`.
|
|
257
|
+
|
|
258
|
+
Scripts run in a sub-shell and do not share context with the parent. To persist context changes use `source` (or `.`).
|
|
213
259
|
|
|
214
|
-
**Note:** when using [exportFileSystem](bin/Readme.md), do not give scripts a `.js` extension — that causes them to be
|
|
260
|
+
**Note:** when using [exportFileSystem](bin/Readme.md), do not give shell scripts a `.js` extension — that causes them to be bundled as JavaScript modules rather than script strings.
|
|
215
261
|
|
|
216
262
|
## Web Binary Tool (`wbt`) — Optional WASM Binary Manager
|
|
217
263
|
|
|
218
|
-
`wbt` is an optional, APT-style binary manager for `brsh` that lets users install and run WebAssembly binaries (such as `wllama`, `sqlite3`, `assemblyscript` / `asc`, `jq`, `python3`, `duckdb`, `graphviz`, `ffmpeg`, `wabt`, `quickjs`, `lua`, or custom `.wasm`/`.wat
|
|
264
|
+
`wbt` is an optional, APT-style binary manager for `brsh` that lets users install and run WebAssembly binaries (such as `nano`, `wllama`, `sqlite3`, `assemblyscript` / `asc`, `jq`, `python3`, `duckdb`, `graphviz`, `ffmpeg`, `wabt`, `quickjs`, `lua`, or custom `.wasm`/`.wat`/ESM URLs). It is **not** included in the default shell bundle unless you explicitly register it.
|
|
219
265
|
|
|
220
266
|
### Enabling `wbt`
|
|
221
267
|
|
|
@@ -235,6 +281,10 @@ const Wbt = require('brsh/wbt');
|
|
|
235
281
|
|
|
236
282
|
const shell = new Shell({ filesystem: {} });
|
|
237
283
|
shell.context.setCommand(Wbt);
|
|
284
|
+
|
|
285
|
+
// Optional: customize registry/sources or restore previously installed packages from a persisted FS
|
|
286
|
+
const CustomWbt = Wbt.configure({ registry: { /* custom packages */ } });
|
|
287
|
+
Wbt.restoreInstalled(shell);
|
|
238
288
|
```
|
|
239
289
|
|
|
240
290
|
### Loading WASM CLI Tools without `wbt` (Base Shell)
|
|
@@ -244,7 +294,7 @@ You can also register WASM CLI binaries directly into any `Shell` instance witho
|
|
|
244
294
|
```js
|
|
245
295
|
import Shell from 'brsh';
|
|
246
296
|
|
|
247
|
-
// 1. From a WASM URL or bytes
|
|
297
|
+
// 1. From a WASM/ESM URL or bytes
|
|
248
298
|
const MyTool = Shell.createWasmCommand({
|
|
249
299
|
name: 'mytool',
|
|
250
300
|
wasmUrl: 'https://example.com/mytool.wasm'
|
|
@@ -255,26 +305,33 @@ shell.context.setCommand(MyTool);
|
|
|
255
305
|
shell.onCommand('tool.wasm --help');
|
|
256
306
|
```
|
|
257
307
|
|
|
258
|
-
### Usage in the
|
|
308
|
+
### Usage in the Shell
|
|
259
309
|
|
|
260
310
|
```sh
|
|
261
311
|
# List available or installed WASM packages
|
|
262
312
|
wbt list
|
|
263
313
|
wbt list --installed
|
|
264
314
|
|
|
265
|
-
# Search and
|
|
315
|
+
# Search, inspect, and update package lists
|
|
266
316
|
wbt search sql
|
|
267
317
|
wbt show sqlite3
|
|
318
|
+
wbt update
|
|
268
319
|
|
|
269
320
|
# Install curated WASM packages into /bin
|
|
270
|
-
wbt install wllama sqlite3 assemblyscript jq python3 duckdb graphviz ffmpeg
|
|
321
|
+
wbt install nano wllama sqlite3 assemblyscript jq python3 duckdb graphviz ffmpeg wabt quickjs lua
|
|
322
|
+
|
|
323
|
+
# Run a package directly without permanently installing it
|
|
324
|
+
wbt run jq -r ".name" package.json
|
|
325
|
+
|
|
326
|
+
# Run GNU nano text editor (compiled to WebAssembly with wide-char ncurses and built-in terminfo)
|
|
327
|
+
nano /home/hello.txt
|
|
271
328
|
|
|
272
329
|
# Run Wllama v3 (llama.cpp WASM + WebGPU) for one-shot completions or interactive chat REPL!
|
|
273
330
|
wllama -m /home/model.gguf -p "Explain WebGPU in one sentence."
|
|
274
331
|
wllama pull https://huggingface.co/ggml-org/models/resolve/main/tinyllamas/stories15M.gguf -o /home/model.gguf
|
|
275
332
|
wllama -m /home/model.gguf # launches interactive multi-turn chat REPL
|
|
276
333
|
|
|
277
|
-
# Run SQLite3 (persists database files directly in the brsh filesystem)
|
|
334
|
+
# Run SQLite3 (persists database files directly in the brsh filesystem, or run `sqlite3 /home/app.db` for interactive SQL REPL)
|
|
278
335
|
sqlite3 /home/app.db "CREATE TABLE notes (id INT, body TEXT); INSERT INTO notes VALUES (1, 'Hello WASM');"
|
|
279
336
|
sqlite3 -table /home/app.db "SELECT * FROM notes;"
|
|
280
337
|
echo "SELECT * FROM notes;" | sqlite3 -csv /home/app.db
|
|
@@ -284,14 +341,16 @@ asrun -e "console.log('Hello from AssemblyScript!')"
|
|
|
284
341
|
asrun /home/script.ts
|
|
285
342
|
asc /home/hello.ts -o /home/hello.wasm && /home/hello.wasm
|
|
286
343
|
|
|
287
|
-
# Other built-in WASM tools
|
|
344
|
+
# Other built-in WASM tools (python3, duckdb, qjs, and lua also launch interactive REPLs when invoked with no arguments)
|
|
288
345
|
cat data.json | jq -r ".user.name"
|
|
289
346
|
python3 -c "print([x * x for x in range(5)])"
|
|
290
347
|
duckdb -c "SELECT 42 AS answer"
|
|
291
348
|
dot -Tsvg graph.dot -o graph.svg
|
|
292
349
|
ffmpeg -i input.mp4 output.webm
|
|
350
|
+
qjs -e "console.log(2 + 2)"
|
|
351
|
+
lua -e "print('Hello from Lua 5.4')"
|
|
293
352
|
|
|
294
|
-
# Install any generic Emscripten / WASI package or custom .wasm URL at runtime
|
|
353
|
+
# Install any generic Emscripten / WASI package or custom .wasm URL at runtime
|
|
295
354
|
wbt install npm:cowsay-wasm
|
|
296
355
|
wbt install mytool https://example.com/mytool.wasm
|
|
297
356
|
mytool
|
|
@@ -302,12 +361,12 @@ wbt remove sqlite3
|
|
|
302
361
|
|
|
303
362
|
## Binary Filesystem Container (`.brfs`) & Client-Side Persistence
|
|
304
363
|
|
|
305
|
-
While `brsh` continues to support standard JS object filesystems (and
|
|
364
|
+
While `brsh` continues to support standard JS object filesystems (and compressed `.js` + `.brfs` loader bundles), it also supports an **Indexed Binary Container (`.brfs`)** format (`BRFS` magic header + JSON metadata/permissions index + contiguous raw binary payload with zero base64 overhead) as well as **IndexedDB client-side persistence**.
|
|
306
365
|
|
|
307
366
|
### Exporting a `.brfs` Binary Container
|
|
308
367
|
|
|
309
368
|
```shell
|
|
310
|
-
# Standalone .brfs binary container (load via fetch('/fs.brfs').then(r => r.arrayBuffer()))
|
|
369
|
+
# Standalone .brfs binary container (load via URL or fetch('/fs.brfs').then(r => r.arrayBuffer()))
|
|
311
370
|
exportFileSystem ./filesystem -o ./fs.brfs
|
|
312
371
|
|
|
313
372
|
# Or generate a JS loader that fetches and mounts ./fs.brfs automatically
|
|
@@ -353,152 +412,8 @@ const savedFs = await Shell.Storage.load('my-app');
|
|
|
353
412
|
```
|
|
354
413
|
|
|
355
414
|
## More Examples
|
|
356
|
-
On my website I have extended the functionality adding some simple extra utilities such as an ANSI image viewer and a markdown parser based on open-source
|
|
415
|
+
On my website I have extended the functionality adding some simple extra utilities such as an ANSI image viewer and a markdown parser based on open-source Node CLI utilities.
|
|
357
416
|
|
|
358
417
|
[See the site in action here](https://piratemedia.tv/)
|
|
359
418
|
|
|
360
419
|
[GitHub source code here](https://github.com/eliotstocker/piratemedia.tv)
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
# How?
|
|
364
|
-
To create a new terminal first add both Terminal and Shell to your page like so:
|
|
365
|
-
```html
|
|
366
|
-
<script src="//unpkg.com/brsh@1/dist/shell.min.js"></script>
|
|
367
|
-
<script src="//unpkg.com/brsh@1/dist/terminal.min.js"></script>
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
You man also use terminal as a node module if you are bundling your code with something like browserify etc
|
|
371
|
-
Install the module via NPM:
|
|
372
|
-
```shell script
|
|
373
|
-
npm i brsh
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
require the module in your code:
|
|
377
|
-
```js
|
|
378
|
-
const Terminal = require('brsh/lib/Terminal');
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
you may then implement the terminal like so:
|
|
382
|
-
```js
|
|
383
|
-
const term = new Terminal({
|
|
384
|
-
el: document.body,
|
|
385
|
-
profile: '/test/.profile',
|
|
386
|
-
cwd: '/test',
|
|
387
|
-
filesystem: {"test": {".profile": "echo \"hi there!\""}},
|
|
388
|
-
cursor: 'blink',
|
|
389
|
-
outputAnimation: 'type',
|
|
390
|
-
animateSpeed: 2
|
|
391
|
-
});
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
The Terminal constructor Options extend those of the Shell:
|
|
395
|
-
|
|
396
|
-
### Terminal Specific options
|
|
397
|
-
| option | required | default | description |
|
|
398
|
-
| ------ | -------- | ------- | ----------- |
|
|
399
|
-
| el | Yes | | a HTMLElement to fill with the terminal emulator |
|
|
400
|
-
| font | No | `Roboto Mono` | the name of a google webfont to load for the emulators console font, a monospace font is recommended |
|
|
401
|
-
| cursor | No | `none` | One of: `none`, `block`, `bink` if block or blink a cursor block character will show at the end of the current line, if bink this will animate with a simple blink |
|
|
402
|
-
| outputAnimation | No | `none` | Out of: `none`, `type` if type is set each character will be appended in order as to animate a more retro style of console output |
|
|
403
|
-
| animateSpeed | No | `1` | number of frames to append per browser frame, the higher this number the faster the animation, i would recommend setting this higher than the default value of 1 |
|
|
404
|
-
| onExit | No | `null` | function to run should the Shell session be destroyed, whist no required, if this is not set and the user types exit, the terminal will then hang in an ended state |
|
|
405
|
-
| mobileInput | No | `click` | One of `none` or `click`, set to click to enable focus on the terminal (and show the onscreen keyboard on mobile |
|
|
406
|
-
|
|
407
|
-
### Shell options
|
|
408
|
-
| option | required | default | description |
|
|
409
|
-
| ------ | -------- | ------- | ----------- |
|
|
410
|
-
| path | No | '/bin' | path in which to look for (and add default entries) binaries |
|
|
411
|
-
| profile | No | | a script to run before user interactivity (see [scripts](#writing-scripts)) |
|
|
412
|
-
| hostname | No | 'browser' | the hostname of the instance, by default the included terminal emulator shows this as the prompt text|
|
|
413
|
-
| filesystem | No | {} | an object to represent the file system, any classes that extend Command will be treated as binaries it is probably best to use the included [exportFileSystem](bin/Readme.md) cli application to convert a directory to a filesystem object |
|
|
414
|
-
| cwd | No | '/' | the starting CWD for the shell, this affects relative paths to files, binaries and directories just like in any other shell |
|
|
415
|
-
|
|
416
|
-
you may also wish to implement your own terminal emulator, are use the Shell directly within node CLI
|
|
417
|
-
in which case you can use just Shell by requiring:
|
|
418
|
-
```js
|
|
419
|
-
const Shell = require('brsh');
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
the Shell class can be implemented like so:
|
|
423
|
-
```js
|
|
424
|
-
const shell = new Shell({
|
|
425
|
-
profile: '/root/.profile',
|
|
426
|
-
cwd: '/root',
|
|
427
|
-
filesystem: require('./filesystem')
|
|
428
|
-
});
|
|
429
|
-
```
|
|
430
|
-
|
|
431
|
-
the following methods and Events are available to interact with the shell instance
|
|
432
|
-
|
|
433
|
-
## Shell Public Methods
|
|
434
|
-
| Method | variables | description |
|
|
435
|
-
| ------ | --------- | ----------- |
|
|
436
|
-
| onCommand | `String` command | a command to run in the shell e.g. `ls -al ./root` |
|
|
437
|
-
| tabCompletion | `String` completion candidate | a candidate for completion, for instance `./test/compl`, an array will be returned of possible completion strings |
|
|
438
|
-
| getPrompt | | returns the current hostname and cwd |
|
|
439
|
-
| destroy | | destroys the instance of the Shell |
|
|
440
|
-
| clear | | cases the clear event to fire |
|
|
441
|
-
|
|
442
|
-
## Shell Events
|
|
443
|
-
| Event | variables | description |
|
|
444
|
-
| ------ | --------- | ----------- |
|
|
445
|
-
| stdOut | `String` line | a single line of standard output from the shell to display to the user |
|
|
446
|
-
| stdErr | `String` line | a single line of standard error from the shell to display to the user |
|
|
447
|
-
| exitCode | `Number` code | the exit code of any command run in the shell |
|
|
448
|
-
| status | `String` Status | Shell status, this should be listened to to check if commands can be accepted, state can be one of: Shell.STATUS_READY or Shell.STATUS_WORKING = 'WORKING', listen for Shell.STATUS_READY to show a prompt to use the user
|
|
449
|
-
| clear | | this event is fired to tell the Terminal emulator to clear the screen |
|
|
450
|
-
| exit | `Number` code | this event if fired when the terminal is destroyed, the code contains the final status code from the shell instance |
|
|
451
|
-
|
|
452
|
-
an example implementation of this shell running as a CLI can be found [here](demo/cli.js)
|
|
453
|
-
|
|
454
|
-
# Builtin and Local commands
|
|
455
|
-
|
|
456
|
-
these are in general simplified versions of what you will find in a standard unix shell
|
|
457
|
-
|
|
458
|
-
* cat
|
|
459
|
-
* echo
|
|
460
|
-
* ls
|
|
461
|
-
|
|
462
|
-
* alias
|
|
463
|
-
* cd
|
|
464
|
-
* clear
|
|
465
|
-
* export
|
|
466
|
-
* source
|
|
467
|
-
* which
|
|
468
|
-
* exit
|
|
469
|
-
|
|
470
|
-
## adding new commands:
|
|
471
|
-
new commands can be added by creating a class that extends Command, you can either access command as a Static property of Shell:
|
|
472
|
-
```js
|
|
473
|
-
class Test extends Shell.Command {
|
|
474
|
-
...
|
|
475
|
-
}
|
|
476
|
-
```
|
|
477
|
-
|
|
478
|
-
or Require Command directly:
|
|
479
|
-
```js
|
|
480
|
-
const Command = require('shell.js/lib/Command');
|
|
481
|
-
|
|
482
|
-
class Test extends Command {
|
|
483
|
-
...
|
|
484
|
-
}
|
|
485
|
-
```
|
|
486
|
-
|
|
487
|
-
for more on writing commands [see the docs here](lib/Readme.md#extending-command)
|
|
488
|
-
|
|
489
|
-
## Writing Scripts
|
|
490
|
-
|
|
491
|
-
to create a script you must first set the Shebang interpreter to `#!/sh.js` as the first line of the file
|
|
492
|
-
|
|
493
|
-
you may then run any commands in the system which will run as part of the script.
|
|
494
|
-
|
|
495
|
-
it is important to note that scripts run in a sub-shell and do not share context, if you wish from context changes from your script to persist you will need to use the source command like in Unix.
|
|
496
|
-
|
|
497
|
-
a final note, if you are using [exportFileSystem](bin/Readme.md) you must not set the file extension of a script to `.js` as this will cause to to be interpreted as javascript rather than a script string
|
|
498
|
-
|
|
499
|
-
## More Examples
|
|
500
|
-
On my website I have extended the functionality adding some simple extra utilities such as an ANSI image viewer and a markdown parser based on opensource node cli utilites.
|
|
501
|
-
|
|
502
|
-
[See the Site in action here](https://piratemedia.tv/)
|
|
503
|
-
|
|
504
|
-
[Github Source Code here](https://github.com/eliotstocker/piratemedia.tv)
|