brsh 2.7.0 → 2.8.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 +20 -0
- package/Readme.md +33 -16
- package/bin/brsh-shell.js +15 -1
- package/dist/shell.es.js +243 -109
- 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/terminal.cjs.js +101 -0
- package/dist/terminal.cjs.js.map +1 -0
- package/dist/terminal.es.js +290 -8107
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +4 -32
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +1619 -6133
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +6 -20
- package/dist/wbt.min.js.map +1 -1
- package/index.js +29 -0
- package/lib/BaseFileSystem.js +40 -0
- package/lib/BinaryContainer.js +1 -1
- package/lib/FileSystem.js +1 -38
- package/lib/NodeFileSystem.js +4 -2
- package/lib/Terminal.js +45 -10
- package/lib/WasmCommand.js +168 -18
- package/lib/local/curl.js +60 -2
- package/lib/wbt/index.js +297 -64
- package/lib/wbt/packages/wllama.js +1061 -0
- package/lib/wbt/registry.js +31 -19
- package/package.json +3 -2
- package/test/curl.test.js +166 -0
- package/test/terminal.test.js +113 -0
- package/test/wasm-cli.test.js +127 -0
- package/test/wbt.test.js +342 -232
- package/vite.config.terminal.js +16 -12
- package/vitest.config.js +6 -0
- package/lib/wbt/daemon.js +0 -264
- package/lib/wbt/packages/pglite.js +0 -590
package/lib/wbt/registry.js
CHANGED
|
@@ -15,19 +15,6 @@
|
|
|
15
15
|
* - type: 'builtin-adapter' (uses a built-in Command adapter) | 'custom-wasm' (raw WASI binary)
|
|
16
16
|
*/
|
|
17
17
|
const DEFAULT_REGISTRY = {
|
|
18
|
-
pglite: {
|
|
19
|
-
name: 'pglite',
|
|
20
|
-
bin: 'pglite',
|
|
21
|
-
aliases: ['psql', 'pg_ctl'],
|
|
22
|
-
version: '0.2.17',
|
|
23
|
-
description: 'Lightweight WASM Postgres database & daemon in the browser and Node',
|
|
24
|
-
details: 'PGlite is a WASM build of PostgreSQL packaged into a client-side library that runs a full Postgres database in memory, persisted to the brsh filesystem, or as a background server daemon (`pglite serve` / `pg_ctl start`).',
|
|
25
|
-
homepage: 'https://pglite.dev',
|
|
26
|
-
cdnUrl: 'https://esm.sh/@electric-sql/pglite@0.2.17',
|
|
27
|
-
type: 'builtin-adapter',
|
|
28
|
-
adapter: 'pglite',
|
|
29
|
-
size: '3.1 MB'
|
|
30
|
-
},
|
|
31
18
|
sqlite3: {
|
|
32
19
|
name: 'sqlite3',
|
|
33
20
|
bin: 'sqlite3',
|
|
@@ -51,6 +38,7 @@ const DEFAULT_REGISTRY = {
|
|
|
51
38
|
details: 'AssemblyScript compiles a strict variant of TypeScript to WebAssembly. Includes `asc` (ahead-of-time compiler to .wasm/.wat) and `asrun` / `asnode`.',
|
|
52
39
|
homepage: 'https://www.assemblyscript.org',
|
|
53
40
|
cdnUrl: 'https://esm.sh/assemblyscript@0.27.31/asc',
|
|
41
|
+
wasmUrl: 'https://cdn.jsdelivr.net/npm/binaryen@119.0.0/index.wasm',
|
|
54
42
|
type: 'generic-runtime',
|
|
55
43
|
repl: { prompt: 'as> ', evalFlag: '-e' },
|
|
56
44
|
size: '1.4 MB'
|
|
@@ -59,11 +47,12 @@ const DEFAULT_REGISTRY = {
|
|
|
59
47
|
name: 'jq',
|
|
60
48
|
bin: 'jq',
|
|
61
49
|
aliases: [],
|
|
62
|
-
version: '
|
|
50
|
+
version: '0.6.2',
|
|
63
51
|
description: 'Command-line JSON processor compiled to WebAssembly (jq-web)',
|
|
64
52
|
details: 'jq is like sed for JSON data — you can use it to slice, filter, map, and transform structured JSON data from files or piped standard input.',
|
|
65
53
|
homepage: 'https://jqlang.github.io/jq/',
|
|
66
|
-
cdnUrl: 'https://esm.sh/jq-web@0.
|
|
54
|
+
cdnUrl: 'https://esm.sh/jq-web@0.6.2',
|
|
55
|
+
wasmUrl: 'https://cdn.jsdelivr.net/npm/jq-web@0.6.2/jq.wasm',
|
|
67
56
|
type: 'generic-runtime',
|
|
68
57
|
size: '620 KB'
|
|
69
58
|
},
|
|
@@ -76,6 +65,7 @@ const DEFAULT_REGISTRY = {
|
|
|
76
65
|
details: 'Full Python 3.12 runtime in WebAssembly powered by Pyodide. Supports interactive Python REPL (`>>>`), running `.py` scripts from the brsh filesystem, `-c` inline code evaluation, and piped standard input.',
|
|
77
66
|
homepage: 'https://pyodide.org',
|
|
78
67
|
cdnUrl: 'https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.mjs',
|
|
68
|
+
wasmUrl: 'https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.asm.wasm',
|
|
79
69
|
indexURL: 'https://cdn.jsdelivr.net/pyodide/v0.26.4/full/',
|
|
80
70
|
type: 'generic-runtime',
|
|
81
71
|
repl: { prompt: '>>> ', evalFlag: '-c' },
|
|
@@ -90,6 +80,7 @@ const DEFAULT_REGISTRY = {
|
|
|
90
80
|
details: 'DuckDB-WASM executes analytical SQL queries directly in WebAssembly, including querying CSV and JSON files stored in the brsh filesystem, interactive SQL shell, and one-shot / piped queries.',
|
|
91
81
|
homepage: 'https://duckdb.org',
|
|
92
82
|
cdnUrl: 'https://esm.sh/@duckdb/duckdb-wasm@1.29.0',
|
|
83
|
+
wasmUrl: 'https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm@1.29.0/dist/duckdb-mvp.wasm',
|
|
93
84
|
type: 'generic-runtime',
|
|
94
85
|
repl: { prompt: 'D ', continuePrompt: ' > ', evalFlag: '-c' },
|
|
95
86
|
size: '4.2 MB'
|
|
@@ -103,6 +94,7 @@ const DEFAULT_REGISTRY = {
|
|
|
103
94
|
details: 'Powered by @hpcc-js/wasm-graphviz. Compiles DOT language graph descriptions (`.dot` / `.gv`) into SVG, DOT, canon, or JSON formats (`dot -Tsvg input.dot -o output.svg`).',
|
|
104
95
|
homepage: 'https://graphviz.org',
|
|
105
96
|
cdnUrl: 'https://esm.sh/@hpcc-js/wasm-graphviz@1.13.0',
|
|
97
|
+
wasmUrl: 'https://cdn.jsdelivr.net/npm/@hpcc-js/wasm-graphviz@1.13.0/dist/graphvizlib.wasm',
|
|
106
98
|
type: 'generic-runtime',
|
|
107
99
|
size: '1.1 MB'
|
|
108
100
|
},
|
|
@@ -116,6 +108,7 @@ const DEFAULT_REGISTRY = {
|
|
|
116
108
|
homepage: 'https://ffmpegwasm.netlify.app',
|
|
117
109
|
cdnUrl: 'https://esm.sh/@ffmpeg/ffmpeg@0.12.10',
|
|
118
110
|
coreUrl: 'https://unpkg.com/@ffmpeg/core@0.12.6/dist/esm/ffmpeg-core.js',
|
|
111
|
+
wasmUrl: 'https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.6/dist/esm/ffmpeg-core.wasm',
|
|
119
112
|
type: 'generic-runtime',
|
|
120
113
|
size: '8.5 MB'
|
|
121
114
|
},
|
|
@@ -128,6 +121,7 @@ const DEFAULT_REGISTRY = {
|
|
|
128
121
|
details: 'WABT provides tools for translating between WebAssembly Text Format (.wat) and the WebAssembly binary format (.wasm), as well as inspecting WASM modules.',
|
|
129
122
|
homepage: 'https://github.com/WebAssembly/wabt',
|
|
130
123
|
cdnUrl: 'https://esm.sh/wabt@1.0.36',
|
|
124
|
+
wasmUrl: 'https://cdn.jsdelivr.net/npm/wabt@1.0.36/libwabt.wasm',
|
|
131
125
|
type: 'generic-runtime',
|
|
132
126
|
size: '540 KB'
|
|
133
127
|
},
|
|
@@ -140,6 +134,7 @@ const DEFAULT_REGISTRY = {
|
|
|
140
134
|
details: 'QuickJS is a small and embeddable Javascript engine supporting ES2023. Runs JS code and files inside an isolated WebAssembly sandbox.',
|
|
141
135
|
homepage: 'https://bellard.org/quickjs/',
|
|
142
136
|
cdnUrl: 'https://esm.sh/quickjs-emscripten@0.31.0',
|
|
137
|
+
wasmUrl: 'https://cdn.jsdelivr.net/npm/@jitl/quickjs-wasmfile-release-sync@0.31.0/dist/emscripten-module.wasm',
|
|
143
138
|
type: 'generic-runtime',
|
|
144
139
|
repl: { prompt: 'qjs > ', evalFlag: '-e' },
|
|
145
140
|
size: '820 KB'
|
|
@@ -153,9 +148,24 @@ const DEFAULT_REGISTRY = {
|
|
|
153
148
|
details: 'Full Lua 5.4 virtual machine compiled to WebAssembly. Supports interactive REPL, script execution from the brsh filesystem, and `-e` one-liners.',
|
|
154
149
|
homepage: 'https://www.lua.org',
|
|
155
150
|
cdnUrl: 'https://esm.sh/wasmoon@1.16.0',
|
|
151
|
+
wasmUrl: 'https://cdn.jsdelivr.net/npm/wasmoon@1.16.0/dist/glue.wasm',
|
|
156
152
|
type: 'generic-runtime',
|
|
157
153
|
repl: { prompt: 'lua> ', evalFlag: '-e' },
|
|
158
154
|
size: '410 KB'
|
|
155
|
+
},
|
|
156
|
+
wllama: {
|
|
157
|
+
name: 'wllama',
|
|
158
|
+
bin: 'wllama',
|
|
159
|
+
aliases: ['llama', 'llama-cli'],
|
|
160
|
+
version: '3.8.1',
|
|
161
|
+
description: 'WebAssembly & WebGPU LLM inference engine powered by llama.cpp (@wllama/wllama)',
|
|
162
|
+
details: 'Wllama v3 runs GGUF large language models in WebAssembly with automatic WebGPU hardware acceleration. Supports one-shot chat/text completions, interactive chat REPL, model inspection, embeddings, benchmarks, and Hugging Face & local filesystem GGUF loading.',
|
|
163
|
+
homepage: 'https://github.com/ngxson/wllama',
|
|
164
|
+
cdnUrl: 'https://cdn.jsdelivr.net/npm/@wllama/wllama@3.8.1/esm/index.js',
|
|
165
|
+
wasmUrl: 'https://cdn.jsdelivr.net/npm/@wllama/wllama@3.8.1/src/wasm/wllama.wasm',
|
|
166
|
+
type: 'builtin-adapter',
|
|
167
|
+
adapter: 'wllama',
|
|
168
|
+
size: '8.8 MB'
|
|
159
169
|
}
|
|
160
170
|
};
|
|
161
171
|
|
|
@@ -163,8 +173,6 @@ const DEFAULT_REGISTRY = {
|
|
|
163
173
|
const PACKAGE_ALIASES = {
|
|
164
174
|
sqlite: 'sqlite3',
|
|
165
175
|
'sqlite-wasm': 'sqlite3',
|
|
166
|
-
psql: 'pglite',
|
|
167
|
-
postgres: 'pglite',
|
|
168
176
|
asc: 'assemblyscript',
|
|
169
177
|
asrun: 'assemblyscript',
|
|
170
178
|
asnode: 'assemblyscript',
|
|
@@ -174,12 +182,16 @@ const PACKAGE_ALIASES = {
|
|
|
174
182
|
dot: 'graphviz',
|
|
175
183
|
wat2wasm: 'wabt',
|
|
176
184
|
wasm2wat: 'wabt',
|
|
177
|
-
qjs: 'quickjs'
|
|
185
|
+
qjs: 'quickjs',
|
|
186
|
+
llama: 'wllama',
|
|
187
|
+
'llama-cli': 'wllama',
|
|
188
|
+
'llama.cpp': 'wllama',
|
|
189
|
+
'@wllama/wllama': 'wllama'
|
|
178
190
|
};
|
|
179
191
|
|
|
180
192
|
function resolvePackageName(name) {
|
|
181
193
|
if (!name) return null;
|
|
182
|
-
const lower = name.toLowerCase();
|
|
194
|
+
const lower = name.toLowerCase().replace(/^(npm|esm):/i, '').replace(/@[\d.^~><=*]+$/, '');
|
|
183
195
|
return PACKAGE_ALIASES[lower] || lower;
|
|
184
196
|
}
|
|
185
197
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brsh",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.1",
|
|
4
4
|
"description": "A unix like shell written in javascript",
|
|
5
5
|
"main": "./dist/shell.min.js",
|
|
6
6
|
"module": "./dist/shell.es.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"./terminal": {
|
|
14
14
|
"import": "./dist/terminal.es.js",
|
|
15
|
-
"require": "./dist/terminal.
|
|
15
|
+
"require": "./dist/terminal.cjs.js",
|
|
16
16
|
"default": "./dist/terminal.min.js"
|
|
17
17
|
},
|
|
18
18
|
"./wbt": {
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"build:wbt": "vite build --config vite.config.wbt.js",
|
|
36
36
|
"shell": "node bin/brsh-shell.js",
|
|
37
37
|
"demo:shell": "node bin/brsh-shell.js --demo",
|
|
38
|
+
"pretest": "npm run build",
|
|
38
39
|
"test": "vitest run",
|
|
39
40
|
"test:watch": "vitest",
|
|
40
41
|
"prepublishOnly": "npm run build"
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { createShell, run } from './helpers.js';
|
|
3
|
+
import { BinaryFile } from '../lib/FileSystem.js';
|
|
4
|
+
|
|
5
|
+
describe('curl command', () => {
|
|
6
|
+
let shell;
|
|
7
|
+
let originalFetch;
|
|
8
|
+
|
|
9
|
+
beforeEach(async () => {
|
|
10
|
+
shell = await createShell({
|
|
11
|
+
filesystem: {
|
|
12
|
+
home: {}
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
originalFetch = globalThis.fetch;
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
globalThis.fetch = originalFetch;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('fetches text and prints to stdout', async () => {
|
|
23
|
+
globalThis.fetch = vi.fn().mockResolvedValue({
|
|
24
|
+
ok: true,
|
|
25
|
+
status: 200,
|
|
26
|
+
statusText: 'OK',
|
|
27
|
+
headers: new Headers({ 'content-type': 'text/plain' }),
|
|
28
|
+
text: async () => 'Hello, world!\nLine 2',
|
|
29
|
+
arrayBuffer: async () => new TextEncoder().encode('Hello, world!\nLine 2').buffer
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const res = await run(shell, 'curl https://example.com/hello.txt');
|
|
33
|
+
expect(res.exitCode).toBe(0);
|
|
34
|
+
expect(res.output).toEqual(['Hello, world!', 'Line 2']);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('includes response headers with -i', async () => {
|
|
38
|
+
globalThis.fetch = vi.fn().mockResolvedValue({
|
|
39
|
+
ok: true,
|
|
40
|
+
status: 200,
|
|
41
|
+
statusText: 'OK',
|
|
42
|
+
headers: new Headers({
|
|
43
|
+
'content-type': 'text/plain',
|
|
44
|
+
'x-custom-header': 'test-val'
|
|
45
|
+
}),
|
|
46
|
+
text: async () => 'response body',
|
|
47
|
+
arrayBuffer: async () => new TextEncoder().encode('response body').buffer
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const res = await run(shell, 'curl -i https://example.com');
|
|
51
|
+
expect(res.exitCode).toBe(0);
|
|
52
|
+
expect(res.output).toContain('HTTP/1.1 200 OK');
|
|
53
|
+
expect(res.output).toContain('x-custom-header: test-val');
|
|
54
|
+
expect(res.output).toContain('response body');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('downloads binary data to a file without UTF-8 corruption', async () => {
|
|
58
|
+
// Raw bytes with non-UTF8 sequences (e.g. WASM or GGUF magic headers)
|
|
59
|
+
const binaryBytes = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0xff, 0xfe, 0x80]);
|
|
60
|
+
|
|
61
|
+
globalThis.fetch = vi.fn().mockResolvedValue({
|
|
62
|
+
ok: true,
|
|
63
|
+
status: 200,
|
|
64
|
+
statusText: 'OK',
|
|
65
|
+
headers: new Headers({
|
|
66
|
+
'content-type': 'application/octet-stream',
|
|
67
|
+
'content-length': String(binaryBytes.length)
|
|
68
|
+
}),
|
|
69
|
+
arrayBuffer: async () => binaryBytes.buffer
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const res = await run(shell, 'curl -s -o /home/model.wasm https://example.com/model.wasm');
|
|
73
|
+
expect(res.exitCode).toBe(0);
|
|
74
|
+
|
|
75
|
+
const savedFile = shell.context.fs.getFileByPath('/home/model.wasm');
|
|
76
|
+
expect(savedFile).toBeDefined();
|
|
77
|
+
expect(savedFile instanceof BinaryFile || savedFile instanceof Uint8Array).toBe(true);
|
|
78
|
+
expect(new Uint8Array(savedFile)).toEqual(binaryBytes);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('emits live carriage return progress bar during streaming download to a file', async () => {
|
|
82
|
+
const chunk1 = new Uint8Array([1, 2, 3, 4, 5]);
|
|
83
|
+
const chunk2 = new Uint8Array([6, 7, 8, 9, 10]);
|
|
84
|
+
let readCount = 0;
|
|
85
|
+
|
|
86
|
+
const mockReader = {
|
|
87
|
+
read: vi.fn().mockImplementation(async () => {
|
|
88
|
+
readCount++;
|
|
89
|
+
if (readCount === 1) return { done: false, value: chunk1 };
|
|
90
|
+
if (readCount === 2) return { done: false, value: chunk2 };
|
|
91
|
+
return { done: true, value: undefined };
|
|
92
|
+
})
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
globalThis.fetch = vi.fn().mockResolvedValue({
|
|
96
|
+
ok: true,
|
|
97
|
+
status: 200,
|
|
98
|
+
statusText: 'OK',
|
|
99
|
+
headers: new Headers({
|
|
100
|
+
'content-type': 'application/octet-stream',
|
|
101
|
+
'content-length': '10'
|
|
102
|
+
}),
|
|
103
|
+
body: {
|
|
104
|
+
getReader: () => mockReader
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const res = await run(shell, 'curl -o /home/stream.bin https://example.com/stream.bin');
|
|
109
|
+
expect(res.exitCode).toBe(0);
|
|
110
|
+
|
|
111
|
+
// Verify progress updates with \r were emitted
|
|
112
|
+
const progressLines = res.output.filter(l => l.startsWith('\r'));
|
|
113
|
+
expect(progressLines.length).toBeGreaterThan(0);
|
|
114
|
+
expect(progressLines.some(l => l.includes('50%'))).toBe(true);
|
|
115
|
+
expect(progressLines.some(l => l.includes('100%'))).toBe(true);
|
|
116
|
+
|
|
117
|
+
const savedFile = shell.context.fs.getFileByPath('/home/stream.bin');
|
|
118
|
+
expect(new Uint8Array(savedFile)).toEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('suppresses progress bar when -s / --silent is passed', async () => {
|
|
122
|
+
const chunk = new Uint8Array([1, 2, 3, 4, 5]);
|
|
123
|
+
let done = false;
|
|
124
|
+
|
|
125
|
+
const mockReader = {
|
|
126
|
+
read: vi.fn().mockImplementation(async () => {
|
|
127
|
+
if (!done) {
|
|
128
|
+
done = true;
|
|
129
|
+
return { done: false, value: chunk };
|
|
130
|
+
}
|
|
131
|
+
return { done: true, value: undefined };
|
|
132
|
+
})
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
globalThis.fetch = vi.fn().mockResolvedValue({
|
|
136
|
+
ok: true,
|
|
137
|
+
status: 200,
|
|
138
|
+
statusText: 'OK',
|
|
139
|
+
headers: new Headers({
|
|
140
|
+
'content-type': 'application/octet-stream',
|
|
141
|
+
'content-length': '5'
|
|
142
|
+
}),
|
|
143
|
+
body: {
|
|
144
|
+
getReader: () => mockReader
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const res = await run(shell, 'curl -s -o /home/quiet.bin https://example.com/quiet.bin');
|
|
149
|
+
expect(res.exitCode).toBe(0);
|
|
150
|
+
expect(res.output.some(l => l.startsWith('\r'))).toBe(false);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('automatically creates parent directories for output files', async () => {
|
|
154
|
+
globalThis.fetch = vi.fn().mockResolvedValue({
|
|
155
|
+
ok: true,
|
|
156
|
+
status: 200,
|
|
157
|
+
statusText: 'OK',
|
|
158
|
+
headers: new Headers({ 'content-type': 'text/plain' }),
|
|
159
|
+
arrayBuffer: async () => new TextEncoder().encode('nested data').buffer
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const res = await run(shell, 'curl -s -o /home/nested/sub/dir/app.txt https://example.com/app.txt');
|
|
163
|
+
expect(res.exitCode).toBe(0);
|
|
164
|
+
expect(shell.context.fs.getFileByPath('/home/nested/sub/dir/app.txt')).toBe('nested data');
|
|
165
|
+
});
|
|
166
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
2
|
+
import Shell from '../index.js';
|
|
3
|
+
import Terminal from '../lib/Terminal.js';
|
|
4
|
+
|
|
5
|
+
describe('Terminal decoupling and Shell resolution', () => {
|
|
6
|
+
let originalWindow;
|
|
7
|
+
let originalDocument;
|
|
8
|
+
let originalShell;
|
|
9
|
+
let originalGlobalShell;
|
|
10
|
+
|
|
11
|
+
beforeAll(() => {
|
|
12
|
+
originalWindow = globalThis.window;
|
|
13
|
+
originalDocument = globalThis.document;
|
|
14
|
+
originalShell = globalThis.Shell;
|
|
15
|
+
originalGlobalShell = globalThis.shell;
|
|
16
|
+
|
|
17
|
+
// Minimal DOM mock
|
|
18
|
+
globalThis.window = {
|
|
19
|
+
addEventListener: () => {},
|
|
20
|
+
removeEventListener: () => {}
|
|
21
|
+
};
|
|
22
|
+
globalThis.document = {
|
|
23
|
+
createElement: () => ({
|
|
24
|
+
style: {},
|
|
25
|
+
classList: { add: () => {} },
|
|
26
|
+
appendChild: () => {},
|
|
27
|
+
setAttribute: () => {},
|
|
28
|
+
addEventListener: () => {},
|
|
29
|
+
getBoundingClientRect: () => ({ width: 800, height: 600 }),
|
|
30
|
+
querySelectorAll: () => []
|
|
31
|
+
}),
|
|
32
|
+
head: { appendChild: () => {} }
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterAll(() => {
|
|
37
|
+
globalThis.window = originalWindow;
|
|
38
|
+
globalThis.document = originalDocument;
|
|
39
|
+
globalThis.Shell = originalShell;
|
|
40
|
+
globalThis.shell = originalGlobalShell;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('instantiates with options.cli directly', () => {
|
|
44
|
+
delete globalThis.Shell;
|
|
45
|
+
delete globalThis.shell;
|
|
46
|
+
|
|
47
|
+
const customShell = new Shell({ cwd: '/test' });
|
|
48
|
+
const term = new Terminal({
|
|
49
|
+
el: document.createElement('div'),
|
|
50
|
+
cli: customShell
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
expect(term.cli).toBe(customShell);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('instantiates with options.Shell constructor', () => {
|
|
57
|
+
delete globalThis.Shell;
|
|
58
|
+
delete globalThis.shell;
|
|
59
|
+
|
|
60
|
+
const term = new Terminal({
|
|
61
|
+
el: document.createElement('div'),
|
|
62
|
+
Shell: Shell,
|
|
63
|
+
cwd: '/test'
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(term.cli).toBeInstanceOf(Shell);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('instantiates with globalThis.Shell fallback', () => {
|
|
70
|
+
globalThis.Shell = Shell;
|
|
71
|
+
|
|
72
|
+
const term = new Terminal({
|
|
73
|
+
el: document.createElement('div'),
|
|
74
|
+
cwd: '/test'
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
expect(term.cli).toBeInstanceOf(Shell);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('throws a helpful error when no Shell or cli is available', () => {
|
|
81
|
+
delete globalThis.Shell;
|
|
82
|
+
delete globalThis.shell;
|
|
83
|
+
|
|
84
|
+
expect(() => {
|
|
85
|
+
new Terminal({
|
|
86
|
+
el: document.createElement('div'),
|
|
87
|
+
Shell: null
|
|
88
|
+
});
|
|
89
|
+
}).toThrow(/Shell is required by Terminal/);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('can be required as a CommonJS module via dist/terminal.cjs.js', () => {
|
|
93
|
+
const { createRequire } = require('module');
|
|
94
|
+
const fs = require('fs');
|
|
95
|
+
const path = require('path');
|
|
96
|
+
const cjsRequire = createRequire(import.meta.url);
|
|
97
|
+
const distFile = path.resolve(import.meta.dirname, '../dist/terminal.cjs.js');
|
|
98
|
+
if (!fs.existsSync(distFile)) {
|
|
99
|
+
const { execSync } = require('child_process');
|
|
100
|
+
execSync('npm run build:terminal', { stdio: 'ignore' });
|
|
101
|
+
}
|
|
102
|
+
const CjsTerminal = cjsRequire('../dist/terminal.cjs.js');
|
|
103
|
+
const TermClass = CjsTerminal.default || CjsTerminal;
|
|
104
|
+
expect(typeof TermClass).toBe('function');
|
|
105
|
+
|
|
106
|
+
const customShell = new Shell({ cwd: '/test' });
|
|
107
|
+
const term = new TermClass({
|
|
108
|
+
el: document.createElement('div'),
|
|
109
|
+
cli: customShell
|
|
110
|
+
});
|
|
111
|
+
expect(term.cli).toBe(customShell);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import Shell from '../index.js';
|
|
3
|
+
import { run } from './helpers.js';
|
|
4
|
+
|
|
5
|
+
// Minimal valid WASM binary that exports _start and exits with 0:
|
|
6
|
+
const MINIMAL_WASM_BYTES = new Uint8Array([
|
|
7
|
+
0x00, 0x61, 0x73, 0x6d,
|
|
8
|
+
0x01, 0x00, 0x00, 0x00,
|
|
9
|
+
0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
|
|
10
|
+
0x03, 0x02, 0x01, 0x00,
|
|
11
|
+
0x07, 0x0a, 0x01, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x00,
|
|
12
|
+
0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
describe('Running .wasm files directly as CLI tools in base shell', () => {
|
|
16
|
+
it('refuses to execute a .wasm file without execute permissions (permission denied)', async () => {
|
|
17
|
+
const shell = new Shell({
|
|
18
|
+
filesystem: {
|
|
19
|
+
home: {
|
|
20
|
+
'mytool.wasm': MINIMAL_WASM_BYTES
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
cwd: '/home'
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Default mode is 0o644 -> not executable
|
|
27
|
+
const res = await run(shell, './mytool.wasm');
|
|
28
|
+
expect(res.exitCode).toBe(1);
|
|
29
|
+
expect(res.errors.join('')).toContain('permission denied');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('executes a .wasm file via ./mytool.wasm after chmod +x', async () => {
|
|
33
|
+
const shell = new Shell({
|
|
34
|
+
filesystem: {
|
|
35
|
+
home: {
|
|
36
|
+
'mytool.wasm': MINIMAL_WASM_BYTES
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
cwd: '/home'
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await run(shell, 'chmod +x mytool.wasm');
|
|
43
|
+
const res = await run(shell, './mytool.wasm');
|
|
44
|
+
expect(res.exitCode).toBe(0);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('refuses to run a file in cwd by bare name without path (standard PATH behavior)', async () => {
|
|
48
|
+
const shell = new Shell({
|
|
49
|
+
filesystem: {
|
|
50
|
+
home: {
|
|
51
|
+
'mytool.wasm': MINIMAL_WASM_BYTES
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
permissions: {
|
|
55
|
+
'/home/mytool.wasm': 0o755
|
|
56
|
+
},
|
|
57
|
+
cwd: '/home'
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const res = await run(shell, 'mytool.wasm');
|
|
61
|
+
expect(res.exitCode).toBe(1);
|
|
62
|
+
expect(res.errors.join('')).toContain('Command not found');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('executes an executable .wasm file placed in /bin by bare name without .wasm extension', async () => {
|
|
66
|
+
const shell = new Shell({
|
|
67
|
+
filesystem: {
|
|
68
|
+
bin: {
|
|
69
|
+
'cowsay.wasm': MINIMAL_WASM_BYTES
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
permissions: {
|
|
73
|
+
'/bin/cowsay.wasm': 0o755
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const res = await run(shell, 'cowsay');
|
|
78
|
+
expect(res.exitCode).toBe(0);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('supports --help and --version automatically on raw .wasm binaries', async () => {
|
|
82
|
+
const shell = new Shell({
|
|
83
|
+
filesystem: {
|
|
84
|
+
home: {
|
|
85
|
+
'mytool.wasm': MINIMAL_WASM_BYTES
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
permissions: {
|
|
89
|
+
'/home/mytool.wasm': 0o755
|
|
90
|
+
},
|
|
91
|
+
cwd: '/home'
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const helpRes = await run(shell, './mytool.wasm --help');
|
|
95
|
+
expect(helpRes.exitCode).toBe(0);
|
|
96
|
+
const helpText = helpRes.output.join('\n');
|
|
97
|
+
expect(helpText).toContain('Usage: mytool.wasm');
|
|
98
|
+
expect(helpText).toContain('-h, --help');
|
|
99
|
+
expect(helpText).toContain('-V, --version');
|
|
100
|
+
expect(helpText).toContain('WASM Exports: _start');
|
|
101
|
+
|
|
102
|
+
const verRes = await run(shell, './mytool.wasm --version');
|
|
103
|
+
expect(verRes.exitCode).toBe(0);
|
|
104
|
+
expect(verRes.output.join('\n')).toContain('mytool.wasm');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('supports piping stdin into .wasm binaries and redirecting stdout to files', async () => {
|
|
108
|
+
const shell = new Shell({
|
|
109
|
+
filesystem: {
|
|
110
|
+
bin: {
|
|
111
|
+
'mytool.wasm': MINIMAL_WASM_BYTES
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
permissions: {
|
|
115
|
+
'/bin/mytool.wasm': 0o755
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const pipeRes = await run(shell, 'echo "input data" | mytool');
|
|
120
|
+
expect(pipeRes.exitCode).toBe(0);
|
|
121
|
+
|
|
122
|
+
const redirRes = await run(shell, 'mytool --help > /help.txt');
|
|
123
|
+
expect(redirRes.exitCode).toBe(0);
|
|
124
|
+
expect(shell.context.fs.exists('/help.txt')).toBe(true);
|
|
125
|
+
expect(shell.context.fs.getFileByPath('/help.txt')).toContain('Usage: mytool.wasm');
|
|
126
|
+
});
|
|
127
|
+
});
|