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/lib/wbt/registry.js
CHANGED
|
@@ -180,6 +180,23 @@ const DEFAULT_REGISTRY = {
|
|
|
180
180
|
type: 'custom-wasm',
|
|
181
181
|
interactive: true,
|
|
182
182
|
size: '680 KB'
|
|
183
|
+
},
|
|
184
|
+
doom: {
|
|
185
|
+
name: 'doom',
|
|
186
|
+
bin: 'doom',
|
|
187
|
+
aliases: ['doom-wasm', 'doom-ascii'],
|
|
188
|
+
scripts: {
|
|
189
|
+
freedoom: 'https://cdn.jsdelivr.net/gh/eliotstocker/doom-wasm@main/bin/freedoom'
|
|
190
|
+
},
|
|
191
|
+
version: '1.0.0',
|
|
192
|
+
description: 'Classic Doom (1993) compiled to WebAssembly with 24-bit ANSI rendering',
|
|
193
|
+
details: 'Classic Doom engine compiled to WebAssembly (wasm32-wasip1) with 24-bit TrueColor ANSI terminal graphics, wasi-termios-brsh terminal shim, and Asyncify real-time frame loop. Includes the `freedoom` launcher script which automatically downloads and caches Freedoom Phase 1/2 WADs into /usr/share/games/doom/.',
|
|
194
|
+
homepage: 'https://github.com/eliotstocker/doom-wasm',
|
|
195
|
+
cdnUrl: 'https://cdn.jsdelivr.net/gh/eliotstocker/doom-wasm@main/dist/doom.wasm',
|
|
196
|
+
wasmUrl: 'https://cdn.jsdelivr.net/gh/eliotstocker/doom-wasm@main/dist/doom.wasm',
|
|
197
|
+
type: 'custom-wasm',
|
|
198
|
+
interactive: true,
|
|
199
|
+
size: '1.0 MB'
|
|
183
200
|
}
|
|
184
201
|
};
|
|
185
202
|
|
|
@@ -201,7 +218,10 @@ const PACKAGE_ALIASES = {
|
|
|
201
218
|
'llama-cli': 'wllama',
|
|
202
219
|
'llama.cpp': 'wllama',
|
|
203
220
|
'@wllama/wllama': 'wllama',
|
|
204
|
-
'nano-wasm': 'nano'
|
|
221
|
+
'nano-wasm': 'nano',
|
|
222
|
+
'doom-wasm': 'doom',
|
|
223
|
+
'doom-ascii': 'doom',
|
|
224
|
+
freedoom: 'doom'
|
|
205
225
|
};
|
|
206
226
|
|
|
207
227
|
function resolvePackageName(name) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brsh",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.10.0",
|
|
4
4
|
"description": "A unix like shell written in javascript",
|
|
5
5
|
"main": "./dist/shell.min.js",
|
|
6
6
|
"module": "./dist/shell.es.js",
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
},
|
|
24
24
|
"bin": {
|
|
25
25
|
"exportFileSystem": "bin/exportFileSystem.js",
|
|
26
|
-
"brsh": "bin/brsh-shell.js"
|
|
26
|
+
"brsh": "bin/brsh-shell.js",
|
|
27
|
+
"wbt": "bin/wbt.js"
|
|
27
28
|
},
|
|
28
29
|
"scripts": {
|
|
29
30
|
"start": "node bin/exportFileSystem.js ./demo/filesystem -o ./demo/fs.js -v fs --binary -c && vite --config vite.config.js demo/",
|
|
@@ -58,19 +58,18 @@ describe('buildStructure', () => {
|
|
|
58
58
|
expect(filesystem['readme.txt']).toBe('hello');
|
|
59
59
|
});
|
|
60
60
|
|
|
61
|
-
it('
|
|
61
|
+
it('preserves the .js extension from JavaScript file keys and stores content directly', () => {
|
|
62
62
|
writeFileSync(join(root, 'app.js'), 'module.exports = {}');
|
|
63
63
|
const { filesystem } = buildStructure(root);
|
|
64
|
-
expect(filesystem).toHaveProperty('app');
|
|
65
|
-
expect(filesystem).not.toHaveProperty('app
|
|
66
|
-
expect(filesystem['app']).
|
|
64
|
+
expect(filesystem).toHaveProperty('app.js');
|
|
65
|
+
expect(filesystem).not.toHaveProperty('app');
|
|
66
|
+
expect(filesystem['app.js']).toBe('module.exports = {}');
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
-
it('
|
|
69
|
+
it('stores plain text JavaScript content for JavaScript files', () => {
|
|
70
70
|
writeFileSync(join(root, 'data.js'), 'module.exports = "content"');
|
|
71
71
|
const { filesystem } = buildStructure(root);
|
|
72
|
-
|
|
73
|
-
expect(reqPath).toBe(resolve(root, 'data.js'));
|
|
72
|
+
expect(filesystem['data.js']).toBe('module.exports = "content"');
|
|
74
73
|
});
|
|
75
74
|
|
|
76
75
|
it('recursively includes nested directories', () => {
|
|
@@ -89,7 +88,7 @@ describe('buildStructure', () => {
|
|
|
89
88
|
mkdirSync(b);
|
|
90
89
|
writeFileSync(join(b, 'deep.js'), 'module.exports = "deep"');
|
|
91
90
|
const { filesystem } = buildStructure(root);
|
|
92
|
-
expect(filesystem.a.b['deep']).
|
|
91
|
+
expect(filesystem.a.b['deep.js']).toBe('module.exports = "deep"');
|
|
93
92
|
});
|
|
94
93
|
|
|
95
94
|
it('ignores .DS_Store files', () => {
|
|
@@ -108,7 +107,30 @@ describe('buildStructure', () => {
|
|
|
108
107
|
expect(Object.keys(filesystem)).toHaveLength(3);
|
|
109
108
|
expect(filesystem).toHaveProperty('a.txt');
|
|
110
109
|
expect(filesystem).toHaveProperty('b.txt');
|
|
111
|
-
expect(filesystem).toHaveProperty('c');
|
|
110
|
+
expect(filesystem).toHaveProperty('c.js');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('serializes filesystem symlinks into SymlinkRef references', () => {
|
|
114
|
+
const { symlinkSync } = require('fs');
|
|
115
|
+
const { SymlinkRef } = require('../lib/BaseFileSystem.js');
|
|
116
|
+
mkdirSync(join(root, 'bin'));
|
|
117
|
+
mkdirSync(join(root, 'usr', 'lib', 'nano'), { recursive: true });
|
|
118
|
+
writeFileSync(join(root, 'usr', 'lib', 'nano', 'nano.wasm'), Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]));
|
|
119
|
+
symlinkSync('/usr/lib/nano/nano.wasm', join(root, 'bin', 'nano'));
|
|
120
|
+
|
|
121
|
+
const { filesystem } = buildStructure(root);
|
|
122
|
+
expect(filesystem.bin.nano instanceof SymlinkRef).toBe(true);
|
|
123
|
+
expect(filesystem.bin.nano.target).toBe('/usr/lib/nano/nano.wasm');
|
|
124
|
+
expect(JSON.stringify(filesystem.bin.nano)).toBe(JSON.stringify({ $l: '/usr/lib/nano/nano.wasm' }));
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('detects UTF-8 text vs binary bytes by content and defaults unknown binary bytes to binary URI', () => {
|
|
128
|
+
writeFileSync(join(root, 'unknown_text.custom'), 'Hello UTF-8 Custom Text');
|
|
129
|
+
writeFileSync(join(root, 'unknown_binary.custom'), Buffer.from([0x00, 0x01, 0x02, 0xff, 0xfe]));
|
|
130
|
+
|
|
131
|
+
const { filesystem } = buildStructure(root);
|
|
132
|
+
expect(filesystem['unknown_text.custom']).toBe('Hello UTF-8 Custom Text');
|
|
133
|
+
expect(filesystem['unknown_binary.custom']).toMatch(/^data:application\/octet-stream;base64,/);
|
|
112
134
|
});
|
|
113
135
|
|
|
114
136
|
it('throws when the root directory does not exist', () => {
|
|
@@ -138,16 +160,16 @@ describe('buildStructure', () => {
|
|
|
138
160
|
expect(permissions['/bin/tool.sh']).toBe(0o755);
|
|
139
161
|
});
|
|
140
162
|
|
|
141
|
-
it('encodes .wasm files as data:application/wasm;base64 and
|
|
163
|
+
it('encodes .wasm files as data:application/wasm;base64 and preserves .wasm extension', () => {
|
|
142
164
|
const sub = join(root, 'bin');
|
|
143
165
|
mkdirSync(sub);
|
|
144
166
|
const wasmBytes = Buffer.from([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
|
|
145
167
|
writeFileSync(join(sub, 'jq.wasm'), wasmBytes);
|
|
146
168
|
chmodSync(join(sub, 'jq.wasm'), 0o755);
|
|
147
169
|
const { filesystem, permissions } = buildStructure(root);
|
|
148
|
-
expect(filesystem.bin).toHaveProperty('jq');
|
|
149
|
-
expect(filesystem.bin.
|
|
150
|
-
expect(permissions['/bin/jq']).toBe(0o755);
|
|
170
|
+
expect(filesystem.bin).toHaveProperty('jq.wasm');
|
|
171
|
+
expect(filesystem.bin['jq.wasm']).toBe(`data:application/wasm;base64,${wasmBytes.toString('base64')}`);
|
|
172
|
+
expect(permissions['/bin/jq.wasm']).toBe(0o755);
|
|
151
173
|
});
|
|
152
174
|
});
|
|
153
175
|
|
|
@@ -393,9 +415,9 @@ describe('exportFileSystem CLI --binary (.brfs indexed binary container)', () =>
|
|
|
393
415
|
const { filesystem, permissions } = deserializeBinaryFilesystem(raw);
|
|
394
416
|
expect(filesystem['hello.txt']).toBe('Hello Binary Container');
|
|
395
417
|
expect(filesystem.bin['run.sh']).toBe('#!/sh.js\necho hi');
|
|
396
|
-
expect(filesystem.bin.
|
|
418
|
+
expect(filesystem.bin['tool.wasm'] instanceof Uint8Array).toBe(true);
|
|
397
419
|
expect(permissions['/bin/run.sh']).toBe(0o755);
|
|
398
|
-
expect(permissions['/bin/tool']).toBe(0o755);
|
|
420
|
+
expect(permissions['/bin/tool.wasm']).toBe(0o755);
|
|
399
421
|
});
|
|
400
422
|
|
|
401
423
|
it('writes a JS loader and .brfs binary container when --binary (-b) is passed with -o fs.js', () => {
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from 'vitest';
|
|
4
|
+
import JsCommand from '../lib/JsCommand.js';
|
|
5
|
+
import FileSystem from '../lib/FileSystem.js';
|
|
6
|
+
import LocalCommand from '../lib/local/LocalCommand.js';
|
|
7
|
+
|
|
8
|
+
describe('JsCommand (lib/JsCommand.js)', () => {
|
|
9
|
+
it('returns an exported Command subclass directly', () => {
|
|
10
|
+
class CustomCmd extends LocalCommand {
|
|
11
|
+
static get name() { return 'custom'; }
|
|
12
|
+
get name() { return 'custom'; }
|
|
13
|
+
run() { this.stdOut = 'custom ran'; }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const fs = new FileSystem('/', {
|
|
17
|
+
bin: {
|
|
18
|
+
'custom.js': 'module.exports = class CustomCmd extends LocalCommand {}'
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// Mock loadModule on fs
|
|
23
|
+
fs.loadModule = () => CustomCmd;
|
|
24
|
+
|
|
25
|
+
const Loaded = JsCommand.load(fs, '/bin/custom.js');
|
|
26
|
+
expect(Loaded).toBe(CustomCmd);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('wraps an exported function or object as a generic command', () => {
|
|
30
|
+
const fs = new FileSystem('/', {
|
|
31
|
+
bin: {
|
|
32
|
+
'tool.js': 'module.exports = function(args) { return "ok"; }'
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
fs.loadModule = () => (args) => 'ok';
|
|
36
|
+
|
|
37
|
+
const Loaded = JsCommand.load(fs, '/bin/tool.js');
|
|
38
|
+
expect(typeof Loaded).toBe('function');
|
|
39
|
+
expect(Loaded.prototype && typeof Loaded.prototype.run === 'function').toBe(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('auto-wires sibling .wasm binary when wrapping a module', () => {
|
|
43
|
+
const dummyWasm = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
|
|
44
|
+
const fs = new FileSystem('/', {
|
|
45
|
+
usr: {
|
|
46
|
+
lib: {
|
|
47
|
+
mytool: {
|
|
48
|
+
'mytool.js': 'module.exports = {}',
|
|
49
|
+
'mytool.wasm': dummyWasm
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
fs.loadModule = () => ({});
|
|
55
|
+
|
|
56
|
+
const Loaded = JsCommand.load(fs, '/usr/lib/mytool/mytool.js');
|
|
57
|
+
expect(typeof Loaded).toBe('function');
|
|
58
|
+
const instance = new Loaded([], { fs });
|
|
59
|
+
expect(instance.requiresFilesystem).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('uses the cache when content matches and invalidates when content changes', () => {
|
|
63
|
+
const cache = new Map();
|
|
64
|
+
const fs = new FileSystem('/', {
|
|
65
|
+
bin: {
|
|
66
|
+
'cached.js': 'module.exports = class V1 extends LocalCommand {}'
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
class V1 extends LocalCommand { static get name() { return 'v1'; } }
|
|
71
|
+
class V2 extends LocalCommand { static get name() { return 'v2'; } }
|
|
72
|
+
|
|
73
|
+
fs.loadModule = (p) => V1;
|
|
74
|
+
const First = JsCommand.load(fs, '/bin/cached.js', 'content1', cache);
|
|
75
|
+
expect(First).toBe(V1);
|
|
76
|
+
expect(cache.has('/bin/cached.js')).toBe(true);
|
|
77
|
+
|
|
78
|
+
// Second call with same content returns cached V1 without re-evaluating
|
|
79
|
+
fs.loadModule = (p) => V2;
|
|
80
|
+
const Second = JsCommand.load(fs, '/bin/cached.js', 'content1', cache);
|
|
81
|
+
expect(Second).toBe(V1);
|
|
82
|
+
|
|
83
|
+
// Third call with changed content re-evaluates to V2
|
|
84
|
+
const Third = JsCommand.load(fs, '/bin/cached.js', 'content2', cache);
|
|
85
|
+
expect(Third).toBe(V2);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('throws a descriptive error when script evaluation fails', () => {
|
|
89
|
+
const fs = new FileSystem('/', {
|
|
90
|
+
bin: {
|
|
91
|
+
'broken.js': 'syntax error !!!'
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
fs.loadModule = () => { throw new Error('Unexpected token'); };
|
|
95
|
+
|
|
96
|
+
expect(() => {
|
|
97
|
+
JsCommand.load(fs, '/bin/broken.js');
|
|
98
|
+
}).toThrow(/Failed to evaluate script/);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('detects JS files and Command classes via static helpers', () => {
|
|
102
|
+
class ValidCmd extends LocalCommand {
|
|
103
|
+
run() {}
|
|
104
|
+
}
|
|
105
|
+
function plainFn() {}
|
|
106
|
+
|
|
107
|
+
expect(JsCommand.isCommandClass(ValidCmd)).toBe(true);
|
|
108
|
+
expect(JsCommand.isCommandClass(plainFn)).toBe(false);
|
|
109
|
+
expect(JsCommand.isJsFile('/bin/foo.js', 'console.log(1)')).toBe(true);
|
|
110
|
+
expect(JsCommand.isJsFile('/bin/foo', 'module.exports = {}')).toBe(true);
|
|
111
|
+
expect(JsCommand.isJsFile('/bin/foo', plainFn)).toBe(true);
|
|
112
|
+
expect(JsCommand.isJsFile('/bin/foo', '#!/bin/sh\necho hi')).toBe(false);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('resolves multi-hop relative and absolute symlinks via fs.realpath', () => {
|
|
116
|
+
const fs = new FileSystem('/', {
|
|
117
|
+
usr: { lib: { pkg: { 'tool.js': 'module.exports = {}' } } },
|
|
118
|
+
bin: {}
|
|
119
|
+
});
|
|
120
|
+
fs.createSymlink('/usr/lib/pkg/tool.js', '/bin/tool');
|
|
121
|
+
fs.createSymlink('tool', '/bin/tool-alias');
|
|
122
|
+
|
|
123
|
+
expect(fs.realpath('/bin/tool-alias')).toBe('/usr/lib/pkg/tool.js');
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -148,6 +148,13 @@ module.exports = Hello;
|
|
|
148
148
|
expect(exitCode).toBe(0);
|
|
149
149
|
expect(output).toContain('hello from js bin');
|
|
150
150
|
});
|
|
151
|
+
|
|
152
|
+
it('fs.loadModule loads CommonJS modules from disk on NodeFileSystem', async () => {
|
|
153
|
+
const modPath = path.join(tmpDir, 'helper.js');
|
|
154
|
+
fs.writeFileSync(modPath, `module.exports = { answer: 42 };\n`);
|
|
155
|
+
const loaded = shell.context.fs.loadModule(modPath);
|
|
156
|
+
expect(loaded).toEqual({ answer: 42 });
|
|
157
|
+
});
|
|
151
158
|
});
|
|
152
159
|
|
|
153
160
|
// ---------------------------------------------------------------------------
|
package/test/scripts.test.js
CHANGED
|
@@ -531,4 +531,34 @@ describe('multiline quotes and line continuations in scripts', () => {
|
|
|
531
531
|
const { output } = await run(shell, '/scripts/if-not-true.sh');
|
|
532
532
|
expect(output).toEqual(['expected']);
|
|
533
533
|
});
|
|
534
|
+
|
|
535
|
+
it('expands $@ in for loops and matches pipe-separated alternatives in case clauses', async () => {
|
|
536
|
+
const shell = await createShell({
|
|
537
|
+
filesystem: {
|
|
538
|
+
scripts: {
|
|
539
|
+
'args-case.sh': [
|
|
540
|
+
'#!/bin/sh',
|
|
541
|
+
'for ARG in $@',
|
|
542
|
+
'do',
|
|
543
|
+
' case "$ARG" in',
|
|
544
|
+
' --help|-h)',
|
|
545
|
+
' echo "matched-help"',
|
|
546
|
+
' ;;',
|
|
547
|
+
' --phase2|-2)',
|
|
548
|
+
' echo "matched-2"',
|
|
549
|
+
' ;;',
|
|
550
|
+
' *)',
|
|
551
|
+
' echo "other:$ARG"',
|
|
552
|
+
' ;;',
|
|
553
|
+
' esac',
|
|
554
|
+
'done'
|
|
555
|
+
].join('\n')
|
|
556
|
+
}
|
|
557
|
+
},
|
|
558
|
+
permissions: execPerms('/scripts/args-case.sh')
|
|
559
|
+
});
|
|
560
|
+
const res = await run(shell, '/scripts/args-case.sh -2 --help custom');
|
|
561
|
+
expect(res.exitCode).toBe(0);
|
|
562
|
+
expect(res.output).toEqual(['matched-2', 'matched-help', 'other:custom']);
|
|
563
|
+
});
|
|
534
564
|
});
|