brsh 2.5.0 → 2.5.2

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 CHANGED
@@ -1,3 +1,17 @@
1
+ ## [2.5.2](https://github.com/eliotstocker/Brsh-JS/compare/v2.5.1...v2.5.2) (2026-05-25)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * symlinking etc sorted ([7baf30d](https://github.com/eliotstocker/Brsh-JS/commit/7baf30df99f3d4fd6d46b3da2a337c3ff6efe755))
7
+
8
+ ## [2.5.1](https://github.com/eliotstocker/Brsh-JS/compare/v2.5.0...v2.5.1) (2026-05-25)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * store binary data at runtime as uint8array rather than base64 string ([f2edfec](https://github.com/eliotstocker/Brsh-JS/commit/f2edfec18cb890846e152f71f1e033ee6f9c80f1))
14
+
1
15
  # [2.5.0](https://github.com/eliotstocker/Brsh-JS/compare/v2.4.1...v2.5.0) (2026-05-25)
2
16
 
3
17
 
@@ -116,10 +116,10 @@ Track progress toward a POSIX-compliant `sh` implementation in Brsh-JS.
116
116
  - [x] `mkdir`
117
117
  - [x] `chmod`
118
118
  - [x] `cd` / `pwd`
119
- - [ ] `touch`
119
+ - [x] `touch`
120
120
  - [ ] `find`
121
- - [ ] `ln` (symlinks)
122
- - [ ] `stat`
121
+ - [x] `ln` (symlinks — real FS only; `-s` symlinks, hard links, `-f` force)
122
+ - [x] `stat` (real FS: full lstat output; virtual FS: size, type, mode)
123
123
 
124
124
  ### Path Utilities
125
125
  - [ ] `basename`
@@ -144,15 +144,14 @@ Track progress toward a POSIX-compliant `sh` implementation in Brsh-JS.
144
144
 
145
145
  ## Priority Order
146
146
 
147
- 1. **Pipes (`|`)** — unlocks most shell idioms; highest value
148
- 2. **`break` / `continue`** — small change, very common in loops
149
- 3. **`shift` / `unset` / `.` / `:`** — small special builtins, high frequency in scripts
150
- 4. **`$@` / `$*` / `$0`** — needed for argument-forwarding patterns
151
- 5. **`${var:-default}` parameter expansion forms** — very common in real scripts
152
- 6. **`wc`, `head`, `tail`, `sort`, `uniq`, `tr`, `cut`** — most-used text utilities
153
- 7. **`basename` / `dirname`** — common path helpers
154
- 8. **`local`** — function-scoped variables
155
- 9. **`date`, `sleep`, `touch`** — misc utilities
156
- 10. **`set -u` enforcement** — nounset mode
157
- 11. **Glob expansion** — needed for `ls *.txt` style usage
158
- 12. **`sed` / `awk`** — complex; consider wasm or partial implementation
147
+ 1. **Input redirect (`<`)** — needed for many real scripts
148
+ 2. **`shift` / `unset` / `:`** — small special builtins, high frequency in scripts
149
+ 3. **`$@` / `$*` / `$0`** — needed for argument-forwarding patterns
150
+ 4. **`${var:-default}` parameter expansion forms** — very common in real scripts
151
+ 5. **`wc`, `head`, `tail`, `sort`, `uniq`, `tr`, `cut`** — most-used text utilities
152
+ 6. **`basename` / `dirname`** — common path helpers
153
+ 7. **`local`** — function-scoped variables
154
+ 8. **`date`, `sleep`** — misc utilities
155
+ 9. **`set -u` enforcement** — nounset mode
156
+ 10. **Glob expansion** — needed for `ls *.txt` style usage
157
+ 11. **`sed` / `awk`** — complex; consider wasm or partial implementation
package/Readme.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # BRowserSHell (brsh)
2
2
  [![npm version](https://badge.fury.io/js/brsh.svg)](https://badge.fury.io/js/brsh)
3
3
 
4
- An extensible shell written in Javascript with an accompanying terminal emulator
4
+ An extensible POSIX Like shell written in Javascript with an accompanying terminal emulator
5
5
 
6
6
  [Demo](http://eliotstocker.github.io/Brsh-JS/)
7
7
 
8
8
  ## What is it?
9
- A Unix-like Shell backend and Terminal Emulator that lets you embed a working terminal in a webapp, or run an interactive shell against the real filesystem in Node.
9
+ A POSIX Like Shell backend and Terminal Emulator that lets you embed a working terminal in a webapp, or run an interactive shell against the real filesystem in Node.
10
10
 
11
11
  ## Why?
12
12
  No idea, you tell me
@@ -50,6 +50,9 @@ const term = new Terminal({
50
50
  The Terminal constructor options extend those of the Shell:
51
51
 
52
52
  ### Terminal Specific options
53
+
54
+ All [Shell options](#shell-options) are also accepted and forwarded directly to the Shell instance.
55
+
53
56
  | option | required | default | description |
54
57
  | ------ | -------- | ------- | ----------- |
55
58
  | el | Yes | | A HTMLElement to fill with the terminal emulator |
@@ -59,6 +62,7 @@ The Terminal constructor options extend those of the Shell:
59
62
  | animateSpeed | No | `1` | Characters per browser frame for the `type` animation. Higher is faster |
60
63
  | onExit | No | `null` | Function called when the shell session is destroyed. Without this, typing `exit` will leave the terminal in an ended state |
61
64
  | mobileInput | No | `click` | One of `none` or `click`. Set to `click` to enable tap-to-focus (and show the on-screen keyboard on mobile) |
65
+ | lineWrap | No | `false` | When `true`, long lines wrap to the next line instead of overflowing |
62
66
 
63
67
  ### Shell options
64
68
  | option | required | default | description |
@@ -69,6 +73,7 @@ The Terminal constructor options extend those of the Shell:
69
73
  | filesystem | No | `{}` | Object representing the virtual file system. Classes extending `Command` are treated as executables. Use [exportFileSystem](bin/Readme.md) to generate this from a real directory |
70
74
  | 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 `0o755` |
71
75
  | cwd | No | `'/'` | Starting working directory |
76
+ | onFsChange | No | `null` | Callback fired whenever the virtual filesystem changes (e.g. after a write or delete). Useful for persisting filesystem state |
72
77
  | useRealFilesystem | No | `false` | When `true`, uses the real Node.js filesystem instead of the virtual one. Intended for CLI use |
73
78
 
74
79
  ## Running an interactive shell in Node
@@ -122,6 +127,7 @@ These are simplified versions of standard Unix commands.
122
127
  * `false`
123
128
  * `grep`
124
129
  * `ls`
130
+ * `printf`
125
131
  * `test` / `[`
126
132
  * `true`
127
133
 
@@ -132,15 +138,19 @@ These are simplified versions of standard Unix commands.
132
138
  * `clear`
133
139
  * `cp`
134
140
  * `curl`
141
+ * `. (dot)` / `source`
135
142
  * `exit`
136
143
  * `export`
137
144
  * `jsfunction`
145
+ * `ln`
138
146
  * `mkdir`
139
147
  * `mv`
140
148
  * `pwd`
141
149
  * `read`
142
150
  * `rm`
143
- * `source`
151
+ * `set`
152
+ * `stat`
153
+ * `touch`
144
154
  * `which`
145
155
 
146
156
  ## Adding new commands
package/bin/brsh-shell.js CHANGED
File without changes
@@ -77,56 +77,16 @@ try {
77
77
  }
78
78
  const { filesystem, permissions } = struct;
79
79
 
80
- /**
81
- * Recursively splits a filesystem tree into two trees:
82
- * code – entries whose value is a require() placeholder (bundled by esbuild)
83
- * data – entries whose value is a plain string/null (text / binary content)
84
- */
85
- function splitFilesystem(node) {
86
- const code = {};
87
- const data = {};
88
- for (const [key, value] of Object.entries(node)) {
89
- if (value !== null && typeof value === 'object') {
90
- const [subCode, subData] = splitFilesystem(value);
91
- if (Object.keys(subCode).length > 0) code[key] = subCode;
92
- if (Object.keys(subData).length > 0) data[key] = subData;
93
- } else if (typeof value === 'string' && value.startsWith("require('")) {
94
- code[key] = value;
95
- } else {
96
- data[key] = value;
97
- }
98
- }
99
- return [code, data];
80
+ if (options.compress && !options.output) {
81
+ console.log('--compress requires --output (-o) to be set');
82
+ process.exit(1);
100
83
  }
101
84
 
102
- let JS;
103
-
104
- if (options.compress) {
105
- const [codeFilesystem, dataFilesystem] = splitFilesystem(filesystem);
85
+ const fsJson = JSON.stringify(filesystem, null, options.pretty ? 2 : null);
86
+ const permJson = JSON.stringify(permissions, null, options.pretty ? 2 : null);
87
+ const fsJS = fsJson.replace(/"(require\('.*?'\))"/gm, '$1');
106
88
 
107
- // Compress the data portion (text/binary file contents + permissions)
108
- const dataPayload = JSON.stringify({ filesystem: dataFilesystem, permissions });
109
- const compressed = zlib.gzipSync(Buffer.from(dataPayload, 'utf8'));
110
- const b64 = compressed.toString('base64');
111
-
112
- // Serialise only the code portion (require() entries) with substitution
113
- const codeFsJSON = JSON.stringify(codeFilesystem);
114
- const codeFsJS = codeFsJSON.replace(/"(require\('.*?'\))"/gm, '$1');
115
-
116
- JS = `
117
- const __c="${b64}";
118
- function __bd(s){const b=atob(s);const a=new Uint8Array(b.length);for(let i=0;i<b.length;i++)a[i]=b.charCodeAt(i);return a;}
119
- async function __decompress(b64){const ds=new DecompressionStream("gzip");const w=ds.writable.getWriter();w.write(__bd(b64));w.close();const buf=await new Response(ds.readable).arrayBuffer();return JSON.parse(new TextDecoder().decode(buf));}
120
- function __merge(a,b){const r={};const ks=new Set([...Object.keys(a||{}),...Object.keys(b||{})]);for(const k of ks){const av=a&&a[k],bv=b&&b[k];r[k]=(av!==null&&typeof av==="object"&&bv!==null&&typeof bv==="object")?__merge(av,bv):(av!==undefined?av:bv);}return r;}
121
- const __codeFs=${codeFsJS};
122
- module.exports=__decompress(__c).then(function(__d){return{filesystem:__merge(__codeFs,__d.filesystem),permissions:__d.permissions};});
123
- `;
124
- } else {
125
- const fsJson = JSON.stringify(filesystem, null, options.pretty ? 2 : null);
126
- const permJson = JSON.stringify(permissions, null, options.pretty ? 2 : null);
127
- const fsJS = fsJson.replace(/"(require\('.*?'\))"/gm, '$1');
128
- JS = `module.exports = { filesystem: ${fsJS}, permissions: ${permJson} }`;
129
- }
89
+ const JS = `module.exports = { filesystem: ${fsJS}, permissions: ${permJson} }`;
130
90
 
131
91
  const tf = tmp.fileSync({ postfix: '.tmp.js' });
132
92
  fs.writeFileSync(tf.name, JS);
@@ -195,5 +155,40 @@ esbuild.build({
195
155
  alias,
196
156
  })
197
157
  .then(() => {
158
+ if (options.compress) {
159
+ // Post-process: gzip the entire esbuild output.
160
+ // The compressed bytes are written to <output>.gz as raw binary.
161
+ // The JS file is replaced with a tiny loader that fetches the .gz,
162
+ // decompresses it, and executes it — no base64 overhead.
163
+ const bundle = fs.readFileSync(options.output, 'utf8');
164
+ // Append a return statement so new Function() can hand back the value
165
+ const codeToCompress = bundle + `\nreturn ${options.variable};`;
166
+ const compressed = zlib.gzipSync(Buffer.from(codeToCompress, 'utf8'));
167
+ const gzPath = options.output.replace(/\.js$/, '') + '.brfs';
168
+ fs.writeFileSync(gzPath, compressed);
169
+
170
+ const varName = options.variable;
171
+ // The loader captures document.currentScript.src synchronously
172
+ // (unavailable once the async IIFE runs) and derives the .brfs URL.
173
+ const loader =
174
+ `globalThis.${varName}=(function(){` +
175
+ `var s=typeof document!=="undefined"&&document.currentScript?document.currentScript.src:` +
176
+ `typeof location!=="undefined"?location.href:null;` +
177
+ `if(!s)throw new Error("${varName}: cannot resolve .brfs URL");` +
178
+ `var u=s.replace(/\\.js(\\?[^#]*)?$/,".brfs");` +
179
+ `return fetch(u)` +
180
+ `.then(function(r){return r.arrayBuffer();})` +
181
+ `.then(function(b){` +
182
+ `var ds=new DecompressionStream("gzip");` +
183
+ `var w=ds.writable.getWriter();` +
184
+ `w.write(new Uint8Array(b));` +
185
+ `w.close();` +
186
+ `return new Response(ds.readable).text();` +
187
+ `})` +
188
+ `.then(function(code){return new Function(code)();});` +
189
+ `})();`;
190
+ fs.writeFileSync(options.output, loader);
191
+ console.log(`Compressed bundle written to ${gzPath}`);
192
+ }
198
193
  console.log('Build succeeded');
199
194
  });