brsh 3.8.1 → 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 +14 -0
- package/Readme.md +26 -10
- package/bin/Readme.md +33 -3
- package/bin/exportFileSystem.js +127 -110
- package/bin/wbt.js +118 -0
- package/dist/shell.es.js +3924 -3468
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +25 -23
- package/dist/shell.min.js.map +1 -1
- package/dist/wbt.es.js +2700 -2300
- 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 +93 -71
- package/lib/BaseFileSystem.js +74 -1
- package/lib/BinaryContainer.js +10 -1
- package/lib/Command.js +63 -21
- package/lib/Context.js +1 -1
- package/lib/FileSystem.js +159 -6
- package/lib/InteractiveCommand.js +5 -0
- package/lib/JsCommand.js +113 -0
- package/lib/NodeFileSystem.js +164 -30
- package/lib/Readme.md +2 -0
- package/lib/ScriptCommand.js +120 -7
- package/lib/WasmCommand.js +213 -44
- 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/devices-signals.test.js +173 -0
- package/test/doom.test.js +36 -0
- package/test/exportFileSystem.test.js +37 -15
- package/test/fixtures/nano.wasm +0 -0
- 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/",
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
3
|
+
import { createShell, run } from './helpers.js';
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const Shell = require('../index.js');
|
|
7
|
+
const FileSystem = require('../lib/FileSystem.js');
|
|
8
|
+
const Command = require('../lib/Command.js');
|
|
9
|
+
const WasmCommand = require('../lib/WasmCommand.js');
|
|
10
|
+
|
|
11
|
+
describe('Virtual /dev endpoints in filesystem', () => {
|
|
12
|
+
let shell;
|
|
13
|
+
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
shell = await createShell();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('has /dev directory and standard device nodes mounted', () => {
|
|
19
|
+
expect(shell.context.fs.exists('/dev')).toBe(true);
|
|
20
|
+
expect(shell.context.fs.exists('/dev/null')).toBe(true);
|
|
21
|
+
expect(shell.context.fs.exists('/dev/zero')).toBe(true);
|
|
22
|
+
expect(shell.context.fs.exists('/dev/urandom')).toBe(true);
|
|
23
|
+
expect(shell.context.fs.exists('/dev/random')).toBe(true);
|
|
24
|
+
expect(shell.context.fs.exists('/dev/tty')).toBe(true);
|
|
25
|
+
expect(shell.context.fs.exists('/dev/stdin')).toBe(true);
|
|
26
|
+
expect(shell.context.fs.exists('/dev/stdout')).toBe(true);
|
|
27
|
+
expect(shell.context.fs.exists('/dev/stderr')).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('identifies /dev character devices with stat', () => {
|
|
31
|
+
const nullStat = shell.context.fs.stat('/dev/null');
|
|
32
|
+
expect(nullStat.isCharacterDevice).toBe(true);
|
|
33
|
+
expect(nullStat.isDirectory).toBe(false);
|
|
34
|
+
|
|
35
|
+
const urandomStat = shell.context.fs.stat('/dev/urandom');
|
|
36
|
+
expect(urandomStat.isCharacterDevice).toBe(true);
|
|
37
|
+
|
|
38
|
+
const devStat = shell.context.fs.stat('/dev');
|
|
39
|
+
expect(devStat.isDirectory).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('lists /dev contents using ls', async () => {
|
|
43
|
+
const res = await run(shell, 'ls /dev');
|
|
44
|
+
expect(res.output.join(' ')).toContain('null');
|
|
45
|
+
expect(res.output.join(' ')).toContain('zero');
|
|
46
|
+
expect(res.output.join(' ')).toContain('urandom');
|
|
47
|
+
expect(res.output.join(' ')).toContain('tty');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('reads empty from /dev/null and discards writes to /dev/null', async () => {
|
|
51
|
+
const catRes = await run(shell, 'cat /dev/null');
|
|
52
|
+
expect(catRes.exitCode).toBe(0);
|
|
53
|
+
expect(catRes.output.join('')).toBe('');
|
|
54
|
+
|
|
55
|
+
const writeRes = await run(shell, 'echo "discarded output" > /dev/null');
|
|
56
|
+
expect(writeRes.exitCode).toBe(0);
|
|
57
|
+
|
|
58
|
+
const catRes2 = await run(shell, 'cat /dev/null');
|
|
59
|
+
expect(catRes2.exitCode).toBe(0);
|
|
60
|
+
expect(catRes2.output.join('')).toBe('');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('reads non-empty bytes from /dev/zero and /dev/urandom', () => {
|
|
64
|
+
const zeroBytes = shell.context.fs.getFileByPath('/dev/zero');
|
|
65
|
+
expect(zeroBytes instanceof Uint8Array).toBe(true);
|
|
66
|
+
expect(zeroBytes.length).toBeGreaterThan(0);
|
|
67
|
+
expect(zeroBytes.every(b => b === 0)).toBe(true);
|
|
68
|
+
|
|
69
|
+
const randBytes = shell.context.fs.getFileByPath('/dev/urandom');
|
|
70
|
+
expect(randBytes instanceof Uint8Array).toBe(true);
|
|
71
|
+
expect(randBytes.length).toBeGreaterThan(0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('writes to /dev/tty directly to terminal stdout', async () => {
|
|
75
|
+
const stdoutChunks = [];
|
|
76
|
+
shell.stdout.on('data', chunk => stdoutChunks.push(String(chunk)));
|
|
77
|
+
await shell.onCommand('echo "direct tty message" > /dev/tty');
|
|
78
|
+
expect(stdoutChunks.join('')).toContain('direct tty message');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('is an in-memory overlay that is never persisted to raw fs or binary containers', async () => {
|
|
82
|
+
// getRaw() must not contain dev
|
|
83
|
+
expect(shell.context.fs.getRaw().dev).toBeUndefined();
|
|
84
|
+
|
|
85
|
+
// writing to /dev/null must not write anything to getRaw()
|
|
86
|
+
await run(shell, 'echo "test" > /dev/null');
|
|
87
|
+
expect(shell.context.fs.getRaw().dev).toBeUndefined();
|
|
88
|
+
|
|
89
|
+
// toBinaryContainer must not serialize /dev or device nodes
|
|
90
|
+
const container = shell.context.fs.toBinaryContainer();
|
|
91
|
+
const deserialized = FileSystem.fromBinaryContainer(container);
|
|
92
|
+
expect(deserialized.filesystem.dev).toBeUndefined();
|
|
93
|
+
|
|
94
|
+
// Booting a fresh filesystem from this container still has /dev mounted dynamically
|
|
95
|
+
const freshFs = new FileSystem('/', container);
|
|
96
|
+
expect(freshFs.exists('/dev/null')).toBe(true);
|
|
97
|
+
expect(freshFs.getRaw().dev).toBeUndefined();
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('Signal emulation and Ctrl+C (SIGINT)', () => {
|
|
102
|
+
let shell;
|
|
103
|
+
|
|
104
|
+
beforeEach(async () => {
|
|
105
|
+
shell = await createShell();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('supports kill(signal) on Command with standard exit codes', () => {
|
|
109
|
+
const cmd = new Command([], shell.context);
|
|
110
|
+
let signalReceived = null;
|
|
111
|
+
cmd.on('signal', sig => { signalReceived = sig; });
|
|
112
|
+
|
|
113
|
+
cmd.kill('SIGINT');
|
|
114
|
+
expect(cmd.result.getExitCode()).toBe(130);
|
|
115
|
+
expect(signalReceived).toBe('SIGINT');
|
|
116
|
+
|
|
117
|
+
cmd.kill('SIGTERM');
|
|
118
|
+
expect(cmd.result.getExitCode()).toBe(143);
|
|
119
|
+
expect(signalReceived).toBe('SIGTERM');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('aborts running command on Ctrl+C (\\x03) with exit code 130', async () => {
|
|
123
|
+
class SlowCommand extends Command {
|
|
124
|
+
static get name() { return 'slowcmd'; }
|
|
125
|
+
run() {
|
|
126
|
+
return new Promise(resolve => {
|
|
127
|
+
this._timer = setTimeout(() => resolve('done'), 5000);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
kill(sig) {
|
|
131
|
+
if (this._timer) clearTimeout(this._timer);
|
|
132
|
+
super.kill(sig);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
shell.context.setCommand(SlowCommand);
|
|
137
|
+
|
|
138
|
+
const runPromise = shell.onCommand('slowcmd');
|
|
139
|
+
// Let command start running
|
|
140
|
+
await new Promise(r => setTimeout(r, 50));
|
|
141
|
+
expect(shell.runningCommand).toBeDefined();
|
|
142
|
+
|
|
143
|
+
// Send Ctrl+C
|
|
144
|
+
shell.write('\x03');
|
|
145
|
+
await runPromise;
|
|
146
|
+
|
|
147
|
+
expect(shell.lastCode).toBe(130);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe('WASM requirements auto-discovery', () => {
|
|
152
|
+
it('detects asyncify from wasm exports without hardcoded names', () => {
|
|
153
|
+
const fakeWasmWithAsyncify = {
|
|
154
|
+
name: 'custom-app',
|
|
155
|
+
exports: ['_start', 'asyncify_start_unwind', 'asyncify_stop_unwind']
|
|
156
|
+
};
|
|
157
|
+
const hasAsyncify = fakeWasmWithAsyncify.exports.includes('asyncify_start_unwind') &&
|
|
158
|
+
fakeWasmWithAsyncify.exports.includes('asyncify_stop_unwind');
|
|
159
|
+
expect(hasAsyncify).toBe(true);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('computes interactivity from pkgMeta or inspected wasm', () => {
|
|
163
|
+
const wasmCmd = new WasmCommand('some-random-game.wasm', [], {});
|
|
164
|
+
expect(wasmCmd.interactive).toBe(false);
|
|
165
|
+
|
|
166
|
+
wasmCmd._pkgMeta = { interactive: true };
|
|
167
|
+
expect(wasmCmd.interactive).toBe(true);
|
|
168
|
+
|
|
169
|
+
const wasmCmd2 = new WasmCommand('another-tool.wasm', [], {});
|
|
170
|
+
wasmCmd2._inspectedWasm = { isInteractive: true };
|
|
171
|
+
expect(wasmCmd2.interactive).toBe(true);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { createShell } from './helpers.js';
|
|
5
|
+
|
|
6
|
+
const DOOM_WASM_PATH = fs.existsSync('/home/antigravity/doom-ascii/dist/doom.wasm')
|
|
7
|
+
? '/home/antigravity/doom-ascii/dist/doom.wasm'
|
|
8
|
+
: '/tmp/doom-asyncify.wasm';
|
|
9
|
+
const DOOM_WAD_PATH = '/tmp/doom1.wad';
|
|
10
|
+
const hasDoom = fs.existsSync(DOOM_WASM_PATH) && fs.existsSync(DOOM_WAD_PATH);
|
|
11
|
+
|
|
12
|
+
describe('Doom in brsh', () => {
|
|
13
|
+
it.skipIf(!hasDoom)('executes doom timedemo benchmark cleanly to completion', async () => {
|
|
14
|
+
const doomWasm = fs.readFileSync(DOOM_WASM_PATH);
|
|
15
|
+
const doomWad = fs.readFileSync(DOOM_WAD_PATH);
|
|
16
|
+
const shell = await createShell({
|
|
17
|
+
filesystem: {
|
|
18
|
+
bin: { 'doom': doomWasm },
|
|
19
|
+
game: { 'doom1.wad': doomWad }
|
|
20
|
+
},
|
|
21
|
+
permissions: {
|
|
22
|
+
'/bin/doom': 0o755
|
|
23
|
+
},
|
|
24
|
+
cwd: '/game'
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const lines = [];
|
|
28
|
+
shell.stdout.on('data', chunk => lines.push(chunk.toString()));
|
|
29
|
+
|
|
30
|
+
await shell.onCommand('doom -timedemo demo1 -nosound -nodraw');
|
|
31
|
+
expect(shell.lastCode).toBe(0);
|
|
32
|
+
const allOut = lines.join('');
|
|
33
|
+
expect(allOut).toContain('DOOM ASCII');
|
|
34
|
+
expect(allOut).toContain('timed 5026 gametics');
|
|
35
|
+
}, 30000);
|
|
36
|
+
});
|
|
@@ -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', () => {
|
package/test/fixtures/nano.wasm
CHANGED
|
Binary file
|
|
@@ -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
|
});
|