brsh 3.9.0 → 3.10.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/CHANGELOG.md +7 -0
- package/Readme.md +22 -4
- package/bin/Readme.md +33 -3
- package/bin/exportFileSystem.js +127 -110
- package/bin/wbt.js +118 -0
- package/dist/shell.es.js +6852 -6616
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +23 -22
- package/dist/shell.min.js.map +1 -1
- package/dist/wbt.es.js +2534 -2342
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +15 -11
- package/dist/wbt.min.js.map +1 -1
- package/index.js +66 -71
- package/lib/BaseFileSystem.js +55 -2
- package/lib/Context.js +1 -1
- package/lib/FileSystem.js +4 -2
- package/lib/JsCommand.js +113 -0
- package/lib/NodeFileSystem.js +164 -30
- package/lib/Readme.md +1 -0
- package/lib/ScriptCommand.js +110 -7
- package/lib/WasmCommand.js +85 -25
- package/lib/buildFilesystemStructure.js +38 -24
- package/lib/isUtf8Text.js +47 -0
- package/lib/local/which.js +8 -3
- package/lib/wbt/index.js +344 -160
- package/lib/wbt/registry.js +21 -1
- package/package.json +3 -2
- package/test/exportFileSystem.test.js +37 -15
- package/test/js-command.test.js +125 -0
- package/test/node-filesystem.test.js +7 -0
- package/test/scripts.test.js +30 -0
- package/test/wbt.test.js +308 -10
- package/test/wbtCli.test.js +160 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [3.10.0](https://github.com/eliotstocker/Brsh-JS/compare/v3.9.0...v3.10.0) (2026-11-10)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **wbt:** filesystem installer architecture and native exec without wbt runtime ([#35](https://github.com/eliotstocker/Brsh-JS/issues/35)) ([fd407d2](https://github.com/eliotstocker/Brsh-JS/commit/fd407d246092617f00cd929a068b0c1b9e31b746))
|
|
7
|
+
|
|
1
8
|
# [3.9.0](https://github.com/eliotstocker/Brsh-JS/compare/v3.8.1...v3.9.0) (2026-11-10)
|
|
2
9
|
|
|
3
10
|
|
package/Readme.md
CHANGED
|
@@ -261,7 +261,7 @@ Scripts run in a sub-shell and do not share context with the parent. To persist
|
|
|
261
261
|
|
|
262
262
|
## Web Binary Tool (`wbt`) — Optional WASM Binary Manager
|
|
263
263
|
|
|
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.
|
|
264
|
+
`wbt` is an optional, APT-style binary manager for `brsh` that lets users install and run WebAssembly binaries (such as `nano`, `doom` / `freedoom`, `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.
|
|
265
265
|
|
|
266
266
|
### Enabling `wbt`
|
|
267
267
|
|
|
@@ -282,11 +282,24 @@ const Wbt = require('brsh/wbt');
|
|
|
282
282
|
const shell = new Shell({ filesystem: {} });
|
|
283
283
|
shell.context.setCommand(Wbt);
|
|
284
284
|
|
|
285
|
-
// Optional: customize registry
|
|
285
|
+
// Optional: customize registry or sources
|
|
286
286
|
const CustomWbt = Wbt.configure({ registry: { /* custom packages */ } });
|
|
287
|
-
Wbt.restoreInstalled(shell);
|
|
288
287
|
```
|
|
289
288
|
|
|
289
|
+
### Pre-Installing Packages Prior to Container Packaging (Developer Workflow)
|
|
290
|
+
|
|
291
|
+
To ship pre-installed packages (such as `nano`, `doom`, `sqlite3`, or `wllama`) in your `.brfs` binary container without needing users to run `wbt` in the browser or bundling `wbt` at runtime:
|
|
292
|
+
|
|
293
|
+
```shell
|
|
294
|
+
# 1. Install packages into your local filesystem root
|
|
295
|
+
npx wbt --root ./my-filesystem install nano doom wllama sqlite3
|
|
296
|
+
|
|
297
|
+
# 2. Package into a standalone .brfs container
|
|
298
|
+
exportFileSystem ./my-filesystem -o ./dist/fs.brfs
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
When booted in the browser (`new Shell({ filesystem: '/dist/fs.brfs' })`), `nano`, `doom`, `sqlite3`, and `wllama` run immediately as native commands from `/bin` without `wbt` or extra network requests.
|
|
302
|
+
|
|
290
303
|
### Loading WASM CLI Tools without `wbt` (Base Shell)
|
|
291
304
|
|
|
292
305
|
You can also register WASM CLI binaries directly into any `Shell` instance without `wbt`:
|
|
@@ -318,7 +331,7 @@ wbt show sqlite3
|
|
|
318
331
|
wbt update
|
|
319
332
|
|
|
320
333
|
# Install curated WASM packages into /bin
|
|
321
|
-
wbt install nano wllama sqlite3 assemblyscript jq python3 duckdb graphviz ffmpeg wabt quickjs lua
|
|
334
|
+
wbt install nano doom wllama sqlite3 assemblyscript jq python3 duckdb graphviz ffmpeg wabt quickjs lua
|
|
322
335
|
|
|
323
336
|
# Run a package directly without permanently installing it
|
|
324
337
|
wbt run jq -r ".name" package.json
|
|
@@ -326,6 +339,11 @@ wbt run jq -r ".name" package.json
|
|
|
326
339
|
# Run GNU nano text editor (compiled to WebAssembly with wide-char ncurses and built-in terminfo)
|
|
327
340
|
nano /home/hello.txt
|
|
328
341
|
|
|
342
|
+
# Play Doom in 24-bit ANSI TrueColor (`freedoom` downloads & caches Freedoom Phase 1/2 WAD into /usr/share/games/doom/)
|
|
343
|
+
freedoom
|
|
344
|
+
freedoom --phase2
|
|
345
|
+
doom -iwad /home/doom1.wad -nosound
|
|
346
|
+
|
|
329
347
|
# Run Wllama v3 (llama.cpp WASM + WebGPU) for one-shot completions or interactive chat REPL!
|
|
330
348
|
wllama -p "Explain WebGPU in one sentence." # uses default SmolLM2 model (or pass -m qwen)
|
|
331
349
|
wllama pull smol -o /home/model.gguf
|
package/bin/Readme.md
CHANGED
|
@@ -48,7 +48,37 @@ exportFileSystem ./demo/filesystem -o ./demo/fs.js -v fs --binary --compress
|
|
|
48
48
|
|
|
49
49
|
---
|
|
50
50
|
|
|
51
|
-
## 2. `
|
|
51
|
+
## 2. `wbt` (`bin/wbt.js`)
|
|
52
|
+
|
|
53
|
+
Standalone developer CLI to install WASM packages directly into a filesystem directory before packaging into a `.brfs` binary container with `exportFileSystem`.
|
|
54
|
+
|
|
55
|
+
### Pre-Installing Packages Prior to Container Export
|
|
56
|
+
|
|
57
|
+
```shell
|
|
58
|
+
# Pre-install packages into a local filesystem root
|
|
59
|
+
npx wbt --root ./demo/filesystem install nano wllama doom sqlite3
|
|
60
|
+
|
|
61
|
+
# Inspect installed packages in the root
|
|
62
|
+
npx wbt --root ./demo/filesystem list --installed
|
|
63
|
+
|
|
64
|
+
# Export the pre-installed filesystem into a standalone .brfs container
|
|
65
|
+
exportFileSystem ./demo/filesystem -o ./demo/fs.brfs
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Packages are placed in `/usr/lib/<pkg>/` with symbolic links in `/bin/`. Because `brsh` resolves and executes commands, `.wasm` binaries, and `.js` modules directly from `/bin`, pre-packaged containers run out of the box in the browser without needing `wbt` or browser network calls.
|
|
69
|
+
|
|
70
|
+
### CLI Options
|
|
71
|
+
|
|
72
|
+
| Option | Alias | Description |
|
|
73
|
+
| ------ | ----- | ----------- |
|
|
74
|
+
| `--root <dir>` | `-r` | Target filesystem directory (required for `install`, `remove`, `list`, `update`) |
|
|
75
|
+
| `--allow-system-root` | | Explicit safety override to allow operating on host `/` |
|
|
76
|
+
| `--help` | `-h` | Show CLI help and available curated packages |
|
|
77
|
+
| `--version` | `-v` | Show `wbt` CLI version |
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## 3. `brsh` (`bin/brsh-shell.js`)
|
|
52
82
|
|
|
53
83
|
Interactive Node.js REPL and non-interactive script/command runner backed by `brsh` (with `wbt` and `pico` pre-registered).
|
|
54
84
|
|
|
@@ -68,12 +98,12 @@ brsh -e -x ./myscript.sh arg1 arg2
|
|
|
68
98
|
|
|
69
99
|
---
|
|
70
100
|
|
|
71
|
-
##
|
|
101
|
+
## 4. `bin/brsh-cmd.js`
|
|
72
102
|
|
|
73
103
|
Multi-call CLI wrapper used by the BusyBox compatibility test suite. Can be invoked directly (`node bin/brsh-cmd.js grep foo file.txt`) or symlinked under a command's name (`cat`, `cp`, `grep`, `ls`, `mv`, `rm`, `busybox`, etc.) to run any `brsh` command against the real Node.js filesystem.
|
|
74
104
|
|
|
75
105
|
---
|
|
76
106
|
|
|
77
|
-
##
|
|
107
|
+
## 5. `bin/zsh-server.js`
|
|
78
108
|
|
|
79
109
|
Zero-dependency HTTP + WebSocket server (`npm run demo:zsh`) that spawns a real OS shell (`$SHELL` / `/bin/zsh`) via [`lib/terminal/PtyBridge.js`](../lib/terminal/PtyBridge.js) and attaches it to `brsh/terminal` in the browser at `http://127.0.0.1:3080`.
|
package/bin/exportFileSystem.js
CHANGED
|
@@ -10,6 +10,7 @@ const tmp = require('tmp');
|
|
|
10
10
|
const { polyfillNode } = require('esbuild-plugin-polyfill-node');
|
|
11
11
|
const { buildStructure } = require('../lib/buildFilesystemStructure');
|
|
12
12
|
const { serializeBinaryFilesystem } = require('../lib/BinaryContainer');
|
|
13
|
+
const { SymlinkRef } = require('../lib/BaseFileSystem');
|
|
13
14
|
|
|
14
15
|
let options;
|
|
15
16
|
try {
|
|
@@ -123,62 +124,92 @@ const textLoaderPlugin = {
|
|
|
123
124
|
}
|
|
124
125
|
};
|
|
125
126
|
|
|
127
|
+
const brshGlobalsPlugin = {
|
|
128
|
+
name: 'brshGlobals',
|
|
129
|
+
setup(build) {
|
|
130
|
+
const BRSH_FILTER = /(^brsh(\/.*)?$|(\/|^)(Command|LocalCommand|WasmCommand|InteractiveCommand|ReplCommand)(\.js)?$)/;
|
|
131
|
+
build.onResolve({ filter: BRSH_FILTER }, args => {
|
|
132
|
+
return { path: args.path, namespace: 'brsh-globals' };
|
|
133
|
+
});
|
|
134
|
+
build.onLoad({ filter: /.*/, namespace: 'brsh-globals' }, args => {
|
|
135
|
+
const base = path.basename(args.path, '.js');
|
|
136
|
+
let exportStmt;
|
|
137
|
+
if (base === 'Command') {
|
|
138
|
+
exportStmt = 'module.exports = typeof Command !== "undefined" ? Command : globalThis.Command;';
|
|
139
|
+
} else if (base === 'LocalCommand') {
|
|
140
|
+
exportStmt = 'module.exports = typeof LocalCommand !== "undefined" ? LocalCommand : globalThis.LocalCommand;';
|
|
141
|
+
} else if (base === 'WasmCommand') {
|
|
142
|
+
exportStmt = 'module.exports = typeof WasmCommand !== "undefined" ? WasmCommand : globalThis.WasmCommand;';
|
|
143
|
+
} else if (base === 'InteractiveCommand') {
|
|
144
|
+
exportStmt = 'module.exports = typeof InteractiveCommand !== "undefined" ? InteractiveCommand : globalThis.InteractiveCommand;';
|
|
145
|
+
} else if (base === 'ReplCommand') {
|
|
146
|
+
exportStmt = 'module.exports = typeof ReplCommand !== "undefined" ? ReplCommand : globalThis.ReplCommand;';
|
|
147
|
+
} else {
|
|
148
|
+
exportStmt = `
|
|
149
|
+
const _Cmd = typeof Command !== "undefined" ? Command : globalThis.Command;
|
|
150
|
+
const _Local = typeof LocalCommand !== "undefined" ? LocalCommand : globalThis.LocalCommand;
|
|
151
|
+
const _Wasm = typeof WasmCommand !== "undefined" ? WasmCommand : globalThis.WasmCommand;
|
|
152
|
+
const _Interactive = typeof InteractiveCommand !== "undefined" ? InteractiveCommand : globalThis.InteractiveCommand;
|
|
153
|
+
const _Repl = typeof ReplCommand !== "undefined" ? ReplCommand : globalThis.ReplCommand;
|
|
154
|
+
module.exports = {
|
|
155
|
+
Command: _Cmd,
|
|
156
|
+
LocalCommand: _Local,
|
|
157
|
+
WasmCommand: _Wasm,
|
|
158
|
+
InteractiveCommand: _Interactive,
|
|
159
|
+
ReplCommand: _Repl,
|
|
160
|
+
default: _Cmd
|
|
161
|
+
};
|
|
162
|
+
`;
|
|
163
|
+
}
|
|
164
|
+
return { contents: exportStmt, loader: 'js' };
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
126
169
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
170
|
+
* Bundles any `.js` files found in the filesystem structure into self-contained
|
|
171
|
+
* plain UTF-8 text strings using esbuild to inline external npm dependencies,
|
|
172
|
+
* mapping brsh base classes to runtime parameters.
|
|
130
173
|
*/
|
|
131
|
-
function
|
|
132
|
-
for (const [
|
|
133
|
-
const vPath = `${
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
} else if (
|
|
138
|
-
|
|
174
|
+
async function bundleJsFiles(node, virtualPrefix, hostPrefix) {
|
|
175
|
+
for (const [key, value] of Object.entries(node)) {
|
|
176
|
+
const vPath = `${virtualPrefix}/${key}`;
|
|
177
|
+
const hostPath = path.join(hostPrefix, key);
|
|
178
|
+
if (value && typeof value === 'object' && !(value instanceof Uint8Array) && !(value instanceof SymlinkRef)) {
|
|
179
|
+
await bundleJsFiles(value, vPath, hostPath);
|
|
180
|
+
} else if (key.endsWith('.js') && typeof value === 'string') {
|
|
181
|
+
try {
|
|
182
|
+
const result = await esbuild.build({
|
|
183
|
+
entryPoints: [hostPath],
|
|
184
|
+
bundle: true,
|
|
185
|
+
format: 'cjs',
|
|
186
|
+
platform: 'browser',
|
|
187
|
+
write: false,
|
|
188
|
+
minify: !options.pretty,
|
|
189
|
+
sourcemap: false,
|
|
190
|
+
plugins: [brshGlobalsPlugin, textLoaderPlugin, polyfillNode()],
|
|
191
|
+
define: {
|
|
192
|
+
'process.env.NODE_ENV': JSON.stringify('production'),
|
|
193
|
+
'process.env.COLORTERM': JSON.stringify('truecolor'),
|
|
194
|
+
'process.env.TERM': JSON.stringify('color'),
|
|
195
|
+
'process.env.FORCE_HYPERLINK': options.hyperlinks ? JSON.stringify('true') : JSON.stringify('false'),
|
|
196
|
+
},
|
|
197
|
+
alias,
|
|
198
|
+
});
|
|
199
|
+
if (result.outputFiles && result.outputFiles[0]) {
|
|
200
|
+
node[key] = result.outputFiles[0].text;
|
|
201
|
+
}
|
|
202
|
+
} catch (err) {
|
|
203
|
+
// If esbuild fails to bundle (e.g. dynamic require), preserve existing file string
|
|
204
|
+
}
|
|
139
205
|
}
|
|
140
206
|
}
|
|
141
|
-
return map;
|
|
142
207
|
}
|
|
143
208
|
|
|
144
209
|
async function buildBinaryContainerMode() {
|
|
145
|
-
|
|
146
|
-
let commandsBundle = null;
|
|
147
|
-
|
|
148
|
-
if (Object.keys(cmdMap).length > 0) {
|
|
149
|
-
const entriesCode = Object.entries(cmdMap)
|
|
150
|
-
.map(([vPath, reqExpr]) => `${JSON.stringify(vPath)}: ${reqExpr}`)
|
|
151
|
-
.join(',\n');
|
|
152
|
-
const cmdEntryJs = `module.exports = {\n${entriesCode}\n};`;
|
|
153
|
-
const tfCmd = tmp.fileSync({ postfix: '.cmd.tmp.js' });
|
|
154
|
-
const tfOut = tmp.fileSync({ postfix: '.cmd.out.js' });
|
|
155
|
-
fs.writeFileSync(tfCmd.name, cmdEntryJs);
|
|
156
|
-
|
|
157
|
-
await esbuild.build({
|
|
158
|
-
entryPoints: [tfCmd.name],
|
|
159
|
-
bundle: true,
|
|
160
|
-
format: 'iife',
|
|
161
|
-
platform: 'browser',
|
|
162
|
-
treeShaking: true,
|
|
163
|
-
outfile: tfOut.name,
|
|
164
|
-
allowOverwrite: true,
|
|
165
|
-
globalName: '__brfs_cmds',
|
|
166
|
-
minify: !options.pretty,
|
|
167
|
-
sourcemap: false,
|
|
168
|
-
plugins: [textLoaderPlugin, polyfillNode()],
|
|
169
|
-
define: {
|
|
170
|
-
'process.env.NODE_ENV': JSON.stringify('production'),
|
|
171
|
-
'process.env.COLORTERM': JSON.stringify('truecolor'),
|
|
172
|
-
'process.env.TERM': JSON.stringify('color'),
|
|
173
|
-
'process.env.FORCE_HYPERLINK': options.hyperlinks ? JSON.stringify('true') : JSON.stringify('false'),
|
|
174
|
-
},
|
|
175
|
-
alias,
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
commandsBundle = fs.readFileSync(tfOut.name, 'utf8') + '\nreturn __brfs_cmds;';
|
|
179
|
-
}
|
|
210
|
+
await bundleJsFiles(filesystem, '', options.root);
|
|
180
211
|
|
|
181
|
-
const rawContainer = serializeBinaryFilesystem(filesystem, permissions
|
|
212
|
+
const rawContainer = serializeBinaryFilesystem(filesystem, permissions);
|
|
182
213
|
const finalBytes = options.compress ? zlib.gzipSync(Buffer.from(rawContainer)) : Buffer.from(rawContainer);
|
|
183
214
|
|
|
184
215
|
if (isStandaloneBrfsOutput) {
|
|
@@ -217,22 +248,18 @@ async function buildBinaryContainerMode() {
|
|
|
217
248
|
console.log('Build succeeded');
|
|
218
249
|
}
|
|
219
250
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
process.exit(1);
|
|
224
|
-
});
|
|
225
|
-
} else {
|
|
251
|
+
async function buildLegacyJsMode() {
|
|
252
|
+
await bundleJsFiles(filesystem, '', options.root);
|
|
253
|
+
|
|
226
254
|
const fsJson = JSON.stringify(filesystem, null, options.pretty ? 2 : null);
|
|
227
255
|
const permJson = JSON.stringify(permissions, null, options.pretty ? 2 : null);
|
|
228
|
-
const fsJS = fsJson.replace(/"(require\('.*?'\))"/gm, '$1');
|
|
229
256
|
|
|
230
|
-
const JS = `module.exports = { filesystem: ${
|
|
257
|
+
const JS = `module.exports = { filesystem: ${fsJson}, permissions: ${permJson} };`;
|
|
231
258
|
|
|
232
259
|
const tf = tmp.fileSync({ postfix: '.tmp.js' });
|
|
233
260
|
fs.writeFileSync(tf.name, JS);
|
|
234
261
|
|
|
235
|
-
esbuild.build({
|
|
262
|
+
await esbuild.build({
|
|
236
263
|
entryPoints: [tf.name],
|
|
237
264
|
bundle: true,
|
|
238
265
|
format: 'iife',
|
|
@@ -241,18 +268,8 @@ if (useBinaryContainer) {
|
|
|
241
268
|
outfile: options.output,
|
|
242
269
|
allowOverwrite: true,
|
|
243
270
|
globalName: options.variable,
|
|
244
|
-
minify:
|
|
271
|
+
minify: !options.pretty,
|
|
245
272
|
sourcemap: false,
|
|
246
|
-
loader: {
|
|
247
|
-
'.gif': 'binary',
|
|
248
|
-
'.png': 'binary',
|
|
249
|
-
'.jpg': 'binary',
|
|
250
|
-
'.jpeg': 'binary',
|
|
251
|
-
'.bmp': 'binary',
|
|
252
|
-
'.webp': 'binary',
|
|
253
|
-
'.ico': 'binary',
|
|
254
|
-
'.wasm': 'binary'
|
|
255
|
-
},
|
|
256
273
|
plugins: [
|
|
257
274
|
textLoaderPlugin,
|
|
258
275
|
polyfillNode()
|
|
@@ -264,47 +281,47 @@ if (useBinaryContainer) {
|
|
|
264
281
|
'process.env.FORCE_HYPERLINK': options.hyperlinks ? JSON.stringify('true') : JSON.stringify('false'),
|
|
265
282
|
},
|
|
266
283
|
alias,
|
|
267
|
-
})
|
|
268
|
-
.then(() => {
|
|
269
|
-
if (options.compress) {
|
|
270
|
-
// Post-process: gzip the entire esbuild output.
|
|
271
|
-
// The compressed bytes are written to <output>.brfs as raw binary.
|
|
272
|
-
// The JS file is replaced with a tiny loader that fetches the .brfs,
|
|
273
|
-
// decompresses it, and executes it — no base64 overhead.
|
|
274
|
-
const bundle = fs.readFileSync(options.output, 'utf8');
|
|
275
|
-
// Append a return statement so new Function() can hand back the value
|
|
276
|
-
const codeToCompress = bundle + `\nreturn ${options.variable};`;
|
|
277
|
-
const compressed = zlib.gzipSync(Buffer.from(codeToCompress, 'utf8'));
|
|
278
|
-
const gzPath = options.output.replace(/\.js$/, '') + '.brfs';
|
|
279
|
-
fs.writeFileSync(gzPath, compressed);
|
|
284
|
+
});
|
|
280
285
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
`
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
.
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
286
|
+
if (options.compress) {
|
|
287
|
+
const bundle = fs.readFileSync(options.output, 'utf8');
|
|
288
|
+
const codeToCompress = bundle + `\nreturn ${options.variable};`;
|
|
289
|
+
const compressed = zlib.gzipSync(Buffer.from(codeToCompress, 'utf8'));
|
|
290
|
+
const gzPath = options.output.replace(/\.js$/, '') + '.brfs';
|
|
291
|
+
fs.writeFileSync(gzPath, compressed);
|
|
292
|
+
|
|
293
|
+
const varName = options.variable;
|
|
294
|
+
const loader =
|
|
295
|
+
`globalThis.${varName}=(function(){` +
|
|
296
|
+
`var s=typeof document!=="undefined"&&document.currentScript?document.currentScript.src:` +
|
|
297
|
+
`typeof location!=="undefined"?location.href:null;` +
|
|
298
|
+
`if(!s)throw new Error("${varName}: cannot resolve .brfs URL");` +
|
|
299
|
+
`var u=s.replace(/\\.js(\\?[^#]*)?$/,".brfs");` +
|
|
300
|
+
`return fetch(u)` +
|
|
301
|
+
`.then(function(r){return r.arrayBuffer();})` +
|
|
302
|
+
`.then(function(b){` +
|
|
303
|
+
`var ds=new DecompressionStream("gzip");` +
|
|
304
|
+
`var w=ds.writable.getWriter();` +
|
|
305
|
+
`w.write(new Uint8Array(b));` +
|
|
306
|
+
`w.close();` +
|
|
307
|
+
`return new Response(ds.readable).text();` +
|
|
308
|
+
`})` +
|
|
309
|
+
`.then(function(code){return new Function(code)();});` +
|
|
310
|
+
`})();`;
|
|
311
|
+
fs.writeFileSync(options.output, loader);
|
|
312
|
+
console.log(`Compressed bundle written to ${gzPath}`);
|
|
313
|
+
}
|
|
314
|
+
console.log('Build succeeded');
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (useBinaryContainer) {
|
|
318
|
+
buildBinaryContainerMode().catch(err => {
|
|
319
|
+
console.error(err);
|
|
320
|
+
process.exit(1);
|
|
321
|
+
});
|
|
322
|
+
} else {
|
|
323
|
+
buildLegacyJsMode().catch(err => {
|
|
324
|
+
console.error(err);
|
|
325
|
+
process.exit(1);
|
|
326
|
+
});
|
|
310
327
|
}
|
package/bin/wbt.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const Context = require('../lib/Context');
|
|
7
|
+
const NodeFileSystem = require('../lib/NodeFileSystem');
|
|
8
|
+
const FileSystem = require('../lib/FileSystem');
|
|
9
|
+
const Wbt = require('../lib/wbt/index.js');
|
|
10
|
+
|
|
11
|
+
async function main() {
|
|
12
|
+
const rawArgs = process.argv.slice(2);
|
|
13
|
+
|
|
14
|
+
let rootDir = null;
|
|
15
|
+
let allowSystemRoot = false;
|
|
16
|
+
const wbtArgs = [];
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < rawArgs.length; i++) {
|
|
19
|
+
const a = rawArgs[i];
|
|
20
|
+
if (a === '--root' || a === '-r') {
|
|
21
|
+
if (i + 1 < rawArgs.length) {
|
|
22
|
+
rootDir = rawArgs[++i];
|
|
23
|
+
}
|
|
24
|
+
} else if (a.startsWith('--root=')) {
|
|
25
|
+
rootDir = a.slice(7);
|
|
26
|
+
} else if (a === '--allow-system-root') {
|
|
27
|
+
allowSystemRoot = true;
|
|
28
|
+
} else {
|
|
29
|
+
wbtArgs.push(a);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const firstArg = wbtArgs[0] ? wbtArgs[0].toLowerCase() : '';
|
|
34
|
+
const isHelp = wbtArgs.length === 0 || wbtArgs.includes('--help') || wbtArgs.includes('-h') || firstArg === 'help';
|
|
35
|
+
const isVersion = wbtArgs.includes('--version') || wbtArgs.includes('-v');
|
|
36
|
+
const isStaticInspection = !rootDir && (firstArg === 'search' || firstArg === 'show' || firstArg === 'info');
|
|
37
|
+
|
|
38
|
+
if (isHelp) {
|
|
39
|
+
console.log('wbt [options] <command> [args...]');
|
|
40
|
+
console.log('');
|
|
41
|
+
console.log('Options:');
|
|
42
|
+
console.log(' --root, -r <dir> Target filesystem directory (required for install/remove/list/update)');
|
|
43
|
+
console.log(' --allow-system-root Allow operating on / as root directory');
|
|
44
|
+
console.log('');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Operations that require an explicit root directory
|
|
48
|
+
if (!rootDir && !isHelp && !isVersion && !isStaticInspection) {
|
|
49
|
+
const op = firstArg || 'this';
|
|
50
|
+
console.error(`E: --root <dir> (-r <dir>) is required for '${op}' operation`);
|
|
51
|
+
console.error(`Usage: wbt --root <dir> ${firstArg || '<command>'} [options]`);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let resolvedRootDir = null;
|
|
56
|
+
let targetFs = null;
|
|
57
|
+
|
|
58
|
+
if (rootDir) {
|
|
59
|
+
resolvedRootDir = path.resolve(rootDir);
|
|
60
|
+
if ((resolvedRootDir === '/' || resolvedRootDir === path.resolve('/')) && !allowSystemRoot) {
|
|
61
|
+
console.error("E: Refusing to operate on system root '/' without --allow-system-root");
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
if (!fs.existsSync(resolvedRootDir)) {
|
|
65
|
+
console.error(`E: Root directory '${resolvedRootDir}' does not exist`);
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
targetFs = new NodeFileSystem('/', null, { rootDir: resolvedRootDir });
|
|
69
|
+
} else {
|
|
70
|
+
// Read-only static inspection (help, version, search, show) with minimal virtual FS
|
|
71
|
+
targetFs = new FileSystem('/', { bin: {}, usr: { lib: {} } });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const context = new Context({
|
|
75
|
+
env: { PATH: '/bin' },
|
|
76
|
+
fs: targetFs
|
|
77
|
+
});
|
|
78
|
+
context.fs = targetFs;
|
|
79
|
+
|
|
80
|
+
const instance = new Wbt(wbtArgs, context);
|
|
81
|
+
if (instance.requiresFilesystem) {
|
|
82
|
+
instance.fs = targetFs;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Forward stdout & stderr to the host terminal
|
|
86
|
+
if (instance.stdout && typeof instance.stdout.on === 'function') {
|
|
87
|
+
instance.stdout.on('data', chunk => {
|
|
88
|
+
process.stdout.write(typeof chunk === 'string' ? chunk : new TextDecoder().decode(chunk));
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (instance.stderr && typeof instance.stderr.on === 'function') {
|
|
92
|
+
instance.stderr.on('data', chunk => {
|
|
93
|
+
process.stderr.write(typeof chunk === 'string' ? chunk : new TextDecoder().decode(chunk));
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
await instance.runCommand();
|
|
99
|
+
} catch (err) {
|
|
100
|
+
console.error(`E: ${err.message || err}`);
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Flush any buffered lines on the command instance
|
|
105
|
+
if (instance.result) {
|
|
106
|
+
for (const line of instance.result.getStdOutput()) {
|
|
107
|
+
if (line.type === 'out') console.log(line.string);
|
|
108
|
+
else if (line.type === 'err') console.error(line.string);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
process.exit(instance.exitCode !== undefined ? instance.exitCode : 0);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
main().catch(err => {
|
|
116
|
+
console.error(`E: ${err.message || err}`);
|
|
117
|
+
process.exit(1);
|
|
118
|
+
});
|