brsh 1.3.0 → 2.0.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/.github/workflows/busybox-compat.yml +166 -0
- package/.github/workflows/release.yml +40 -0
- package/.releaserc.json +18 -0
- package/CHANGELOG.md +355 -0
- package/bin/brsh-cmd.js +78 -0
- package/bin/exportFileSystem.js +81 -156
- package/busybox-testsuite/cat.tests +49 -0
- package/busybox-testsuite/cp.tests +206 -0
- package/busybox-testsuite/grep.tests +237 -0
- package/busybox-testsuite/testing.sh +180 -0
- package/dist/brsh.css +2 -0
- package/dist/shell.es.js +5667 -0
- package/dist/shell.es.js.map +1 -0
- package/dist/shell.min.js +14 -219
- package/dist/shell.min.js.map +1 -147
- package/dist/terminal.es.js +6228 -0
- package/dist/terminal.es.js.map +1 -0
- package/dist/terminal.min.js +113 -18
- package/dist/terminal.min.js.map +1 -21
- package/index.js +231 -56
- package/lib/Command.js +12 -1
- package/lib/Context.js +12 -4
- package/lib/FileSystem.js +99 -6
- package/lib/NodeFileSystem.js +238 -0
- package/lib/Result.js +6 -0
- package/lib/ScriptCommand.js +193 -15
- package/lib/Terminal.js +192 -11
- package/lib/WasmCommand.js +252 -0
- package/lib/buildFilesystemStructure.js +58 -0
- package/lib/builtins/cat.js +67 -9
- package/lib/builtins/echo.js +36 -5
- package/lib/builtins/false.js +13 -0
- package/lib/builtins/grep.js +267 -0
- package/lib/builtins/index.js +9 -2
- package/lib/builtins/ls.js +86 -18
- package/lib/builtins/test.js +105 -0
- package/lib/builtins/true.js +10 -0
- package/lib/local/chmod.js +72 -0
- package/lib/local/cp.js +156 -0
- package/lib/local/index.js +9 -1
- package/lib/local/jsfunction.js +58 -0
- package/lib/local/mkdir.js +44 -0
- package/lib/local/mv.js +96 -0
- package/lib/local/pwd.js +13 -0
- package/lib/local/read.js +107 -0
- package/lib/local/rm.js +71 -0
- package/lib/local/which.js +2 -2
- package/package.json +31 -34
- package/test/builtins.test.js +505 -0
- package/test/exportFileSystem.test.js +226 -0
- package/test/helpers.js +35 -0
- package/test/local-commands.test.js +593 -0
- package/test/node-filesystem.test.js +133 -0
- package/test/scripts.test.js +277 -0
- package/test/shell.test.js +245 -0
- package/test/streaming.test.js +113 -0
- package/vite.config.shell.js +21 -0
- package/vite.config.terminal.js +27 -0
- package/vitest.config.js +15 -0
package/lib/Terminal.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
const Shell = require('Shell');
|
|
4
4
|
const AnsiUp = require('ansi_up');
|
|
5
5
|
|
|
6
|
+
const HISTORY_FILE = '/.brsh_history';
|
|
7
|
+
|
|
6
8
|
class Terminal {
|
|
7
9
|
constructor(options) {
|
|
8
10
|
const {
|
|
@@ -38,6 +40,11 @@ class Terminal {
|
|
|
38
40
|
this._working = false;
|
|
39
41
|
this.cli = new Shell(options);
|
|
40
42
|
this._buffer = [];
|
|
43
|
+
this._cursorPos = 0;
|
|
44
|
+
this._history = [];
|
|
45
|
+
this._historyPos = null;
|
|
46
|
+
this._savedInput = null;
|
|
47
|
+
this._historyLoaded = false;
|
|
41
48
|
this._el = el;
|
|
42
49
|
this._ansi = new AnsiUp.default();
|
|
43
50
|
|
|
@@ -57,12 +64,25 @@ class Terminal {
|
|
|
57
64
|
}
|
|
58
65
|
}
|
|
59
66
|
|
|
67
|
+
sendKey(key, modifiers = {}) {
|
|
68
|
+
const { alt = false, ctrl = false } = modifiers;
|
|
69
|
+
const onUpdate = this._setCurrentLine.bind(this);
|
|
70
|
+
|
|
71
|
+
const navKeys = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Home', 'End', 'Delete'];
|
|
72
|
+
if (navKeys.includes(key) || (ctrl && ['a', 'e', 'u', 'k'].includes(key.toLowerCase()))) {
|
|
73
|
+
this._navKeyEvent(onUpdate, { key, altKey: alt, ctrlKey: ctrl });
|
|
74
|
+
} else {
|
|
75
|
+
this._keyEvent(onUpdate, key);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
60
79
|
onCommand(command) {
|
|
61
80
|
if(!this._currentLine) {
|
|
62
81
|
return setTimeout(this.onCommand.bind(this, command), 10);
|
|
63
82
|
}
|
|
64
|
-
|
|
83
|
+
|
|
65
84
|
const onUpdate = this._setCurrentLine.bind(this);
|
|
85
|
+
this._cursorPos = command.length;
|
|
66
86
|
onUpdate(Array.from(command));
|
|
67
87
|
this.cli.onCommand(command);
|
|
68
88
|
}
|
|
@@ -205,14 +225,32 @@ class Terminal {
|
|
|
205
225
|
}
|
|
206
226
|
|
|
207
227
|
_setCurrentLine(buffer) {
|
|
208
|
-
|
|
228
|
+
const pos = Math.min(this._cursorPos, buffer.length);
|
|
229
|
+
|
|
230
|
+
while (this._currentLine.firstChild) {
|
|
231
|
+
this._currentLine.removeChild(this._currentLine.firstChild);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const prompt = `${this.cli.getPrompt()} `;
|
|
235
|
+
|
|
236
|
+
if (this._cursor !== 'none') {
|
|
237
|
+
const before = buffer.slice(0, pos).join('');
|
|
238
|
+
const atChar = pos < buffer.length ? buffer[pos] : ' ';
|
|
239
|
+
const after = buffer.slice(pos + 1).join('');
|
|
240
|
+
|
|
241
|
+
this._currentLine.appendChild(document.createTextNode(prompt + before));
|
|
209
242
|
|
|
210
|
-
if(this._cursor !== 'none') {
|
|
211
243
|
const cursor = document.createElement('span');
|
|
212
|
-
cursor.
|
|
244
|
+
cursor.textContent = atChar;
|
|
213
245
|
cursor.classList.add('terminal__cursor');
|
|
214
246
|
cursor.classList.add(this._cursor);
|
|
215
247
|
this._currentLine.appendChild(cursor);
|
|
248
|
+
|
|
249
|
+
if (after) {
|
|
250
|
+
this._currentLine.appendChild(document.createTextNode(after));
|
|
251
|
+
}
|
|
252
|
+
} else {
|
|
253
|
+
this._currentLine.innerText = `${prompt}${buffer.join('')}`;
|
|
216
254
|
}
|
|
217
255
|
}
|
|
218
256
|
|
|
@@ -228,6 +266,10 @@ class Terminal {
|
|
|
228
266
|
_statusCheck(status) {
|
|
229
267
|
if(status === Shell.STATUS_READY) {
|
|
230
268
|
this._working = false;
|
|
269
|
+
if (!this._historyLoaded) {
|
|
270
|
+
this._loadHistory();
|
|
271
|
+
this._historyLoaded = true;
|
|
272
|
+
}
|
|
231
273
|
if(this._animating) {
|
|
232
274
|
return requestAnimationFrame(this._statusCheck.bind(this, status));
|
|
233
275
|
}
|
|
@@ -238,6 +280,54 @@ class Terminal {
|
|
|
238
280
|
}
|
|
239
281
|
}
|
|
240
282
|
|
|
283
|
+
_loadHistory() {
|
|
284
|
+
try {
|
|
285
|
+
const file = this.cli.context.fs.getFileByPath(HISTORY_FILE);
|
|
286
|
+
if (file && typeof file === 'string' && file.length > 0) {
|
|
287
|
+
this._history = file.split('\n').filter(l => l.length > 0);
|
|
288
|
+
}
|
|
289
|
+
} catch(e) {
|
|
290
|
+
// No history file yet
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
_saveHistory() {
|
|
295
|
+
try {
|
|
296
|
+
this.cli.context.fs.setFileByPath(HISTORY_FILE, this._history.join('\n'), true);
|
|
297
|
+
} catch(e) {
|
|
298
|
+
// Ignore persistence errors
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
_navigateHistory(direction, onUpdate) {
|
|
303
|
+
if (this._history.length === 0) return;
|
|
304
|
+
|
|
305
|
+
if (direction === -1) {
|
|
306
|
+
if (this._historyPos === null) {
|
|
307
|
+
this._savedInput = [...this._buffer];
|
|
308
|
+
this._historyPos = this._history.length - 1;
|
|
309
|
+
} else if (this._historyPos > 0) {
|
|
310
|
+
this._historyPos--;
|
|
311
|
+
} else {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
this._buffer = Array.from(this._history[this._historyPos]);
|
|
315
|
+
} else {
|
|
316
|
+
if (this._historyPos === null) return;
|
|
317
|
+
if (this._historyPos < this._history.length - 1) {
|
|
318
|
+
this._historyPos++;
|
|
319
|
+
this._buffer = Array.from(this._history[this._historyPos]);
|
|
320
|
+
} else {
|
|
321
|
+
this._historyPos = null;
|
|
322
|
+
this._buffer = this._savedInput ? [...this._savedInput] : [];
|
|
323
|
+
this._savedInput = null;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
this._cursorPos = this._buffer.length;
|
|
328
|
+
onUpdate(this._buffer);
|
|
329
|
+
}
|
|
330
|
+
|
|
241
331
|
_attachShellListeners() {
|
|
242
332
|
this.cli.on('status', this._statusCheck.bind(this));
|
|
243
333
|
|
|
@@ -265,18 +355,94 @@ class Terminal {
|
|
|
265
355
|
|
|
266
356
|
_listenForKeyEvents(onUpdate, el) {
|
|
267
357
|
window.addEventListener('keydown', e => {
|
|
268
|
-
if (e.keyCode
|
|
269
|
-
e.preventDefault();
|
|
358
|
+
if (e.keyCode === 9) {
|
|
359
|
+
e.preventDefault();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const navKeys = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Home', 'End', 'Delete'];
|
|
363
|
+
if (navKeys.includes(e.key)) {
|
|
364
|
+
e.preventDefault();
|
|
365
|
+
this._navKeyEvent(onUpdate, e);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (e.ctrlKey && ['a', 'e', 'u', 'k'].includes(e.key.toLowerCase())) {
|
|
370
|
+
e.preventDefault();
|
|
371
|
+
this._navKeyEvent(onUpdate, e);
|
|
270
372
|
}
|
|
271
373
|
});
|
|
272
374
|
|
|
273
375
|
el.addEventListener('keyup', (e) => this._keyEvent(onUpdate, e.key));
|
|
274
376
|
}
|
|
275
377
|
|
|
378
|
+
_wordLeft() {
|
|
379
|
+
let pos = this._cursorPos;
|
|
380
|
+
while (pos > 0 && this._buffer[pos - 1] === ' ') pos--;
|
|
381
|
+
while (pos > 0 && this._buffer[pos - 1] !== ' ') pos--;
|
|
382
|
+
return pos;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
_wordRight() {
|
|
386
|
+
let pos = this._cursorPos;
|
|
387
|
+
const len = this._buffer.length;
|
|
388
|
+
while (pos < len && this._buffer[pos] === ' ') pos++;
|
|
389
|
+
while (pos < len && this._buffer[pos] !== ' ') pos++;
|
|
390
|
+
return pos;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
_navKeyEvent(onUpdate, e) {
|
|
394
|
+
if (this._working || this._animating) return;
|
|
395
|
+
|
|
396
|
+
const key = e.key;
|
|
397
|
+
|
|
398
|
+
if (key === 'ArrowLeft') {
|
|
399
|
+
if (e.altKey) {
|
|
400
|
+
this._cursorPos = this._wordLeft();
|
|
401
|
+
} else if (this._cursorPos > 0) {
|
|
402
|
+
this._cursorPos--;
|
|
403
|
+
}
|
|
404
|
+
onUpdate(this._buffer);
|
|
405
|
+
} else if (key === 'ArrowRight') {
|
|
406
|
+
if (e.altKey) {
|
|
407
|
+
this._cursorPos = this._wordRight();
|
|
408
|
+
} else if (this._cursorPos < this._buffer.length) {
|
|
409
|
+
this._cursorPos++;
|
|
410
|
+
}
|
|
411
|
+
onUpdate(this._buffer);
|
|
412
|
+
} else if (key === 'Home' || (e.ctrlKey && key.toLowerCase() === 'a')) {
|
|
413
|
+
this._cursorPos = 0;
|
|
414
|
+
onUpdate(this._buffer);
|
|
415
|
+
} else if (key === 'End' || (e.ctrlKey && key.toLowerCase() === 'e')) {
|
|
416
|
+
this._cursorPos = this._buffer.length;
|
|
417
|
+
onUpdate(this._buffer);
|
|
418
|
+
} else if (key === 'Delete') {
|
|
419
|
+
if (this._cursorPos < this._buffer.length) {
|
|
420
|
+
this._buffer.splice(this._cursorPos, 1);
|
|
421
|
+
onUpdate(this._buffer);
|
|
422
|
+
}
|
|
423
|
+
} else if (e.ctrlKey && key.toLowerCase() === 'u') {
|
|
424
|
+
this._buffer.splice(0, this._cursorPos);
|
|
425
|
+
this._cursorPos = 0;
|
|
426
|
+
onUpdate(this._buffer);
|
|
427
|
+
} else if (e.ctrlKey && key.toLowerCase() === 'k') {
|
|
428
|
+
this._buffer.splice(this._cursorPos);
|
|
429
|
+
onUpdate(this._buffer);
|
|
430
|
+
} else if (key === 'ArrowUp') {
|
|
431
|
+
this._navigateHistory(-1, onUpdate);
|
|
432
|
+
} else if (key === 'ArrowDown') {
|
|
433
|
+
this._navigateHistory(1, onUpdate);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
276
437
|
_keyEvent(onUpdate, key) {
|
|
277
438
|
this._clearLine('autoComplete');
|
|
439
|
+
if (this.cli.onInput(key)) {
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
|
|
278
443
|
if (key.length === 1) {
|
|
279
|
-
this._buffer.
|
|
444
|
+
this._buffer.splice(this._cursorPos, 0, key);
|
|
445
|
+
this._cursorPos++;
|
|
280
446
|
|
|
281
447
|
if (!this._working && !this._animating) {
|
|
282
448
|
onUpdate(this._buffer);
|
|
@@ -284,12 +450,26 @@ class Terminal {
|
|
|
284
450
|
} else if (key === 'Enter' && !this._working) {
|
|
285
451
|
const command = this._buffer.join('');
|
|
286
452
|
this._buffer = [];
|
|
453
|
+
this._cursorPos = 0;
|
|
454
|
+
this._historyPos = null;
|
|
455
|
+
this._savedInput = null;
|
|
456
|
+
|
|
457
|
+
if (command.trim()) {
|
|
458
|
+
if (this._history[this._history.length - 1] !== command) {
|
|
459
|
+
this._history.push(command);
|
|
460
|
+
}
|
|
461
|
+
this._saveHistory();
|
|
462
|
+
}
|
|
463
|
+
|
|
287
464
|
this.cli.onCommand(command);
|
|
288
465
|
} else if (key === 'Backspace') {
|
|
289
|
-
this.
|
|
466
|
+
if (this._cursorPos > 0) {
|
|
467
|
+
this._buffer.splice(this._cursorPos - 1, 1);
|
|
468
|
+
this._cursorPos--;
|
|
290
469
|
|
|
291
|
-
|
|
292
|
-
|
|
470
|
+
if (!this._working && !this._animating) {
|
|
471
|
+
onUpdate(this._buffer);
|
|
472
|
+
}
|
|
293
473
|
}
|
|
294
474
|
} else if (key === 'Tab' && !this._working && !this._animating) {
|
|
295
475
|
const commandParts = this._buffer.join('').split(' ');
|
|
@@ -306,6 +486,7 @@ class Terminal {
|
|
|
306
486
|
this._print(options.join(' '), 'autoComplete');
|
|
307
487
|
}
|
|
308
488
|
|
|
489
|
+
this._cursorPos = this._buffer.length;
|
|
309
490
|
onUpdate(this._buffer);
|
|
310
491
|
}
|
|
311
492
|
}
|
|
@@ -315,4 +496,4 @@ class Terminal {
|
|
|
315
496
|
module.exports = Terminal;
|
|
316
497
|
|
|
317
498
|
//import css
|
|
318
|
-
require('../styles/terminal.css');
|
|
499
|
+
require('../styles/terminal.css');
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ScriptCommand = require('./ScriptCommand');
|
|
4
|
+
|
|
5
|
+
class WasmCommand extends ScriptCommand {
|
|
6
|
+
// this.script = filepath (set by ScriptCommand parent constructor)
|
|
7
|
+
|
|
8
|
+
get name() { return 'wasm'; }
|
|
9
|
+
get requiresFilesystem() { return true; }
|
|
10
|
+
|
|
11
|
+
run() {
|
|
12
|
+
const filepath = this.script;
|
|
13
|
+
const nodeFs = (() => { try { return require('fs'); } catch { return null; } })();
|
|
14
|
+
|
|
15
|
+
return this._loadBytes(filepath, nodeFs)
|
|
16
|
+
.then(({ bytes, isWat }) => isWat ? this._runWat(bytes) : this._runWasm(bytes))
|
|
17
|
+
.catch(e => {
|
|
18
|
+
this.stdErr = e.message || String(e);
|
|
19
|
+
this.exitCode = 1;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
_loadBytes(filepath, nodeFs) {
|
|
24
|
+
if (this.context.fs.isRealFilesystem && nodeFs) {
|
|
25
|
+
try {
|
|
26
|
+
const buf = nodeFs.readFileSync(this.context.fs.absolutePath(filepath));
|
|
27
|
+
const bytes = new Uint8Array(buf);
|
|
28
|
+
return Promise.resolve({ bytes, isWat: this._isWat(filepath, bytes) });
|
|
29
|
+
} catch(e) {
|
|
30
|
+
return Promise.reject(new Error(`${filepath}: ${e.message}`));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const content = this.context.fs.getFileByPath(filepath);
|
|
34
|
+
if (content == null) return Promise.reject(new Error(`${filepath}: No such file or directory`));
|
|
35
|
+
const bytes = this._contentToBytes(content);
|
|
36
|
+
return Promise.resolve({ bytes, isWat: this._isWat(filepath, bytes) });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_isWat(filepath, bytes) {
|
|
40
|
+
if (filepath.endsWith('.wat')) return true;
|
|
41
|
+
// WAT text starts with '(' (0x28); WASM binary starts with \0asm (0x00)
|
|
42
|
+
return bytes.length > 0 && bytes[0] === 0x28;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_contentToBytes(content) {
|
|
46
|
+
if (content instanceof Uint8Array) return content;
|
|
47
|
+
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(content))
|
|
48
|
+
return new Uint8Array(content.buffer, content.byteOffset, content.byteLength);
|
|
49
|
+
if (content instanceof ArrayBuffer) return new Uint8Array(content);
|
|
50
|
+
// Binary string stored in virtual filesystem
|
|
51
|
+
const bytes = new Uint8Array(content.length);
|
|
52
|
+
for (let i = 0; i < content.length; i++) bytes[i] = content.charCodeAt(i) & 0xff;
|
|
53
|
+
return bytes;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async _runWat(bytes) {
|
|
57
|
+
const text = new TextDecoder().decode(bytes);
|
|
58
|
+
let wasmBytes;
|
|
59
|
+
try {
|
|
60
|
+
const wabtInit = require('wabt');
|
|
61
|
+
const wabt = typeof wabtInit === 'function' ? await wabtInit() : wabtInit;
|
|
62
|
+
const parsed = wabt.parseWat(this.script, text, { mutable_globals: true });
|
|
63
|
+
const { buffer } = parsed.toBinary({});
|
|
64
|
+
wasmBytes = new Uint8Array(buffer);
|
|
65
|
+
} catch(e) {
|
|
66
|
+
if (e.code === 'MODULE_NOT_FOUND') {
|
|
67
|
+
this.stdErr = `${this.script}: WAT execution requires the 'wabt' package`;
|
|
68
|
+
this.stdErr = `Install it with: npm install wabt`;
|
|
69
|
+
} else {
|
|
70
|
+
this.stdErr = `${this.script}: WAT compilation error: ${e.message}`;
|
|
71
|
+
}
|
|
72
|
+
this.exitCode = 1;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
return this._runWasm(wasmBytes);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async _runWasm(bytes) {
|
|
79
|
+
if (typeof WebAssembly === 'undefined') {
|
|
80
|
+
this.stdErr = `${this.script}: WebAssembly is not available in this environment`;
|
|
81
|
+
this.exitCode = 1;
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const outBuf = { 1: '', 2: '' };
|
|
86
|
+
const decoder = new TextDecoder();
|
|
87
|
+
let memRef = null;
|
|
88
|
+
|
|
89
|
+
const writeFd = (fd, text) => {
|
|
90
|
+
outBuf[fd] = (outBuf[fd] || '') + text;
|
|
91
|
+
const lines = outBuf[fd].split('\n');
|
|
92
|
+
outBuf[fd] = lines.pop();
|
|
93
|
+
for (const line of lines) {
|
|
94
|
+
if (fd === 1) this.stdOut = line;
|
|
95
|
+
else this.stdErr = line;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const flushFd = () => {
|
|
100
|
+
if (outBuf[1]) { this.stdOut = outBuf[1]; outBuf[1] = ''; }
|
|
101
|
+
if (outBuf[2]) { this.stdErr = outBuf[2]; outBuf[2] = ''; }
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const stdinBytes = new TextEncoder().encode(this.stdin || '');
|
|
105
|
+
let stdinPos = 0;
|
|
106
|
+
|
|
107
|
+
const wasi = {
|
|
108
|
+
fd_write: (fd, iovs, iovs_len, nwritten_ptr) => {
|
|
109
|
+
if (!memRef) return 8;
|
|
110
|
+
const view = new DataView(memRef.buffer);
|
|
111
|
+
let total = 0;
|
|
112
|
+
for (let i = 0; i < iovs_len; i++) {
|
|
113
|
+
const ptr = view.getUint32(iovs + i * 8, true);
|
|
114
|
+
const len = view.getUint32(iovs + i * 8 + 4, true);
|
|
115
|
+
if (len > 0) writeFd(fd, decoder.decode(new Uint8Array(memRef.buffer, ptr, len)));
|
|
116
|
+
total += len;
|
|
117
|
+
}
|
|
118
|
+
view.setUint32(nwritten_ptr, total, true);
|
|
119
|
+
return 0;
|
|
120
|
+
},
|
|
121
|
+
fd_read: (fd, iovs, iovs_len, nread_ptr) => {
|
|
122
|
+
if (fd !== 0 || !memRef) return 8;
|
|
123
|
+
const view = new DataView(memRef.buffer);
|
|
124
|
+
let total = 0;
|
|
125
|
+
for (let i = 0; i < iovs_len && stdinPos < stdinBytes.length; i++) {
|
|
126
|
+
const ptr = view.getUint32(iovs + i * 8, true);
|
|
127
|
+
const len = view.getUint32(iovs + i * 8 + 4, true);
|
|
128
|
+
const chunk = Math.min(len, stdinBytes.length - stdinPos);
|
|
129
|
+
new Uint8Array(memRef.buffer).set(stdinBytes.slice(stdinPos, stdinPos + chunk), ptr);
|
|
130
|
+
stdinPos += chunk;
|
|
131
|
+
total += chunk;
|
|
132
|
+
}
|
|
133
|
+
view.setUint32(nread_ptr, total, true);
|
|
134
|
+
return 0;
|
|
135
|
+
},
|
|
136
|
+
proc_exit: (code) => {
|
|
137
|
+
if (code !== 0) this.exitCode = code;
|
|
138
|
+
throw { _wasmExit: true, code };
|
|
139
|
+
},
|
|
140
|
+
args_sizes_get: (argc_ptr, buf_size_ptr) => {
|
|
141
|
+
if (!memRef) return 28;
|
|
142
|
+
const args = [this.script, ...(this.arguments || [])];
|
|
143
|
+
const view = new DataView(memRef.buffer);
|
|
144
|
+
view.setUint32(argc_ptr, args.length, true);
|
|
145
|
+
view.setUint32(buf_size_ptr, args.reduce((s, a) => s + a.length + 1, 0), true);
|
|
146
|
+
return 0;
|
|
147
|
+
},
|
|
148
|
+
args_get: (argv_ptr, buf_ptr) => {
|
|
149
|
+
if (!memRef) return 28;
|
|
150
|
+
const args = [this.script, ...(this.arguments || [])];
|
|
151
|
+
const view = new DataView(memRef.buffer);
|
|
152
|
+
const enc = new TextEncoder();
|
|
153
|
+
let offset = buf_ptr;
|
|
154
|
+
for (let i = 0; i < args.length; i++) {
|
|
155
|
+
view.setUint32(argv_ptr + i * 4, offset, true);
|
|
156
|
+
const encoded = enc.encode(args[i] + '\0');
|
|
157
|
+
new Uint8Array(memRef.buffer).set(encoded, offset);
|
|
158
|
+
offset += encoded.length;
|
|
159
|
+
}
|
|
160
|
+
return 0;
|
|
161
|
+
},
|
|
162
|
+
environ_sizes_get: (cnt, sz) => {
|
|
163
|
+
if (memRef) {
|
|
164
|
+
const view = new DataView(memRef.buffer);
|
|
165
|
+
view.setUint32(cnt, 0, true);
|
|
166
|
+
view.setUint32(sz, 0, true);
|
|
167
|
+
}
|
|
168
|
+
return 0;
|
|
169
|
+
},
|
|
170
|
+
environ_get: () => 0,
|
|
171
|
+
clock_time_get: (id, prec, time_ptr) => {
|
|
172
|
+
if (memRef) {
|
|
173
|
+
const ns = BigInt(Date.now()) * 1_000_000n;
|
|
174
|
+
new DataView(memRef.buffer).setBigUint64(time_ptr, ns, true);
|
|
175
|
+
}
|
|
176
|
+
return 0;
|
|
177
|
+
},
|
|
178
|
+
fd_close: () => 0,
|
|
179
|
+
fd_seek: () => 70, // ESPIPE—no seekable fds
|
|
180
|
+
fd_prestat_get: () => 8, // EBADF—no pre-opened dirs
|
|
181
|
+
fd_prestat_dir_name: () => 8,
|
|
182
|
+
path_open: () => 76, // ENOTDIR
|
|
183
|
+
random_get: (buf_ptr, buf_len) => {
|
|
184
|
+
if (memRef) {
|
|
185
|
+
const arr = new Uint8Array(memRef.buffer, buf_ptr, buf_len);
|
|
186
|
+
for (let i = 0; i < buf_len; i++) arr[i] = Math.random() * 256 | 0;
|
|
187
|
+
}
|
|
188
|
+
return 0;
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const wasiProxy = new Proxy(wasi, { get(t, k) { return k in t ? t[k] : () => 0; } });
|
|
193
|
+
|
|
194
|
+
const imports = {
|
|
195
|
+
wasi_snapshot_preview1: wasiProxy,
|
|
196
|
+
wasi_unstable: wasiProxy,
|
|
197
|
+
env: new Proxy({
|
|
198
|
+
// Simple non-WASI imports for hand-written modules
|
|
199
|
+
print: (ptr, len) => { if (memRef) writeFd(1, decoder.decode(new Uint8Array(memRef.buffer, ptr, len))); },
|
|
200
|
+
eprint: (ptr, len) => { if (memRef) writeFd(2, decoder.decode(new Uint8Array(memRef.buffer, ptr, len))); },
|
|
201
|
+
abort: () => { this.exitCode = 134; throw { _wasmExit: true, code: 134 }; },
|
|
202
|
+
}, { get(t, k) { return k in t ? t[k] : () => 0; } }),
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
try {
|
|
206
|
+
const module = await WebAssembly.compile(bytes);
|
|
207
|
+
|
|
208
|
+
// Satisfy any imported memory (some modules import rather than define it)
|
|
209
|
+
for (const imp of WebAssembly.Module.imports(module)) {
|
|
210
|
+
if (imp.kind === 'memory') {
|
|
211
|
+
const mem = new WebAssembly.Memory({ initial: 1, maximum: 256 });
|
|
212
|
+
memRef = mem;
|
|
213
|
+
if (!imports[imp.module]) imports[imp.module] = {};
|
|
214
|
+
imports[imp.module][imp.name] = mem;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
219
|
+
|
|
220
|
+
// Prefer the module's own exported memory
|
|
221
|
+
if (instance.exports.memory) memRef = instance.exports.memory;
|
|
222
|
+
|
|
223
|
+
const start = instance.exports._start
|
|
224
|
+
|| instance.exports.__wasi_command
|
|
225
|
+
|| instance.exports.main
|
|
226
|
+
|| instance.exports.run;
|
|
227
|
+
|
|
228
|
+
if (!start) {
|
|
229
|
+
this.stdErr = `${this.script}: no entry point found (export _start, main, or run)`;
|
|
230
|
+
this.exitCode = 1;
|
|
231
|
+
} else {
|
|
232
|
+
try {
|
|
233
|
+
await start();
|
|
234
|
+
} catch(e) {
|
|
235
|
+
if (!e || !e._wasmExit) {
|
|
236
|
+
this.stdErr = `${this.script}: runtime error: ${e?.message || String(e)}`;
|
|
237
|
+
this.exitCode = 1;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
} catch(e) {
|
|
242
|
+
if (!e || !e._wasmExit) {
|
|
243
|
+
this.stdErr = `${this.script}: ${e?.message || String(e)}`;
|
|
244
|
+
this.exitCode = 1;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
flushFd();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
module.exports = WasmCommand;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
const IGNORE = ['.DS_Store'];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Recursively builds a filesystem structure object from a real directory.
|
|
10
|
+
* Returns { filesystem, permissions } where:
|
|
11
|
+
* - filesystem is the nested content tree (directories as objects, files as require() strings)
|
|
12
|
+
* - permissions maps virtual absolute paths to Unix mode bits (e.g. { '/bin/script.sh': 0o755 })
|
|
13
|
+
*
|
|
14
|
+
* Throws if `root` cannot be read.
|
|
15
|
+
*/
|
|
16
|
+
function buildStructure(root) {
|
|
17
|
+
const permissions = {};
|
|
18
|
+
const filesystem = _buildTree(root, '', permissions);
|
|
19
|
+
return { filesystem, permissions };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function _buildTree(root, virtualPrefix, permissions) {
|
|
23
|
+
const sub = fs.readdirSync(root);
|
|
24
|
+
|
|
25
|
+
return sub.reduce((structure, handle) => {
|
|
26
|
+
if(IGNORE.includes(handle)) {
|
|
27
|
+
return structure;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const stat = fs.lstatSync(path.join(root, handle));
|
|
31
|
+
if(stat.isDirectory()) {
|
|
32
|
+
structure[handle] = _buildTree(
|
|
33
|
+
path.join(root, handle),
|
|
34
|
+
virtualPrefix + '/' + handle,
|
|
35
|
+
permissions
|
|
36
|
+
);
|
|
37
|
+
return structure;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Confirm file is readable before including it
|
|
41
|
+
fs.readFileSync(path.join(root, handle), 'utf8');
|
|
42
|
+
|
|
43
|
+
const key = handle.endsWith('.js')
|
|
44
|
+
? handle.substring(0, handle.length - 3)
|
|
45
|
+
: handle;
|
|
46
|
+
|
|
47
|
+
structure[key] = `require('${path.resolve(root, handle)}')`;
|
|
48
|
+
|
|
49
|
+
// Record real Unix permission bits for this file
|
|
50
|
+
permissions[virtualPrefix + '/' + key] = stat.mode & 0o777;
|
|
51
|
+
} catch(fe) {
|
|
52
|
+
// Silently skip files/dirs that are inaccessible
|
|
53
|
+
}
|
|
54
|
+
return structure;
|
|
55
|
+
}, {});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { buildStructure, IGNORE };
|
package/lib/builtins/cat.js
CHANGED
|
@@ -8,22 +8,80 @@ class Cat extends Command {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
run() {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
let numberLines = false;
|
|
12
|
+
let numberNonBlank = false;
|
|
13
|
+
let squeezeBlank = false;
|
|
14
|
+
const paths = [];
|
|
15
|
+
|
|
16
|
+
for (const arg of this.arguments) {
|
|
17
|
+
if (arg.startsWith('-') && arg.length > 1) {
|
|
18
|
+
for (const ch of arg.slice(1)) {
|
|
19
|
+
if (ch === 'n') numberLines = true;
|
|
20
|
+
else if (ch === 'b') { numberNonBlank = true; numberLines = false; }
|
|
21
|
+
else if (ch === 's') squeezeBlank = true;
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
paths.push(arg);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const parts = [];
|
|
29
|
+
|
|
30
|
+
if (paths.length === 0) {
|
|
31
|
+
const stdinContent = this.stdin;
|
|
32
|
+
if (stdinContent == null) {
|
|
33
|
+
this.stdErr = 'cat: no file specified';
|
|
34
|
+
this.exitCode = 1;
|
|
35
|
+
return Promise.resolve();
|
|
36
|
+
}
|
|
37
|
+
parts.push(stdinContent);
|
|
38
|
+
} else {
|
|
39
|
+
for (const path of paths) {
|
|
40
|
+
if (path === '-') {
|
|
41
|
+
parts.push(this.stdin || '');
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const file = this.fs.getFileByPath(path);
|
|
45
|
+
if (file === null || file === undefined) {
|
|
46
|
+
this.stdErr = `${path}: No such file or directory`;
|
|
47
|
+
this.exitCode = 1;
|
|
48
|
+
return Promise.resolve();
|
|
49
|
+
}
|
|
50
|
+
if (file.constructor === Object) {
|
|
51
|
+
this.stdErr = `${path}: Is a directory`;
|
|
52
|
+
this.exitCode = 1;
|
|
53
|
+
return Promise.resolve();
|
|
54
|
+
}
|
|
55
|
+
parts.push(file.toString());
|
|
56
|
+
}
|
|
13
57
|
}
|
|
14
58
|
|
|
15
|
-
|
|
59
|
+
let content = parts.join('');
|
|
60
|
+
// POSIX text files end with \n; strip it before splitting so the trailing
|
|
61
|
+
// empty element doesn't become a spurious blank output line.
|
|
62
|
+
if (content.endsWith('\n')) content = content.slice(0, -1);
|
|
63
|
+
let lines = content.split('\n');
|
|
16
64
|
|
|
17
|
-
if(
|
|
18
|
-
|
|
65
|
+
if (squeezeBlank) {
|
|
66
|
+
lines = lines.reduce((acc, line) => {
|
|
67
|
+
if (line === '' && acc.length > 0 && acc[acc.length - 1] === '') return acc;
|
|
68
|
+
acc.push(line);
|
|
69
|
+
return acc;
|
|
70
|
+
}, []);
|
|
19
71
|
}
|
|
20
72
|
|
|
21
|
-
if(
|
|
22
|
-
|
|
73
|
+
if (numberNonBlank || numberLines) {
|
|
74
|
+
let n = 0;
|
|
75
|
+
lines = lines.map(line => {
|
|
76
|
+
if (numberNonBlank && line === '') return line;
|
|
77
|
+
n++;
|
|
78
|
+
return `${String(n).padStart(6)}\t${line}`;
|
|
79
|
+
});
|
|
23
80
|
}
|
|
24
81
|
|
|
25
|
-
|
|
82
|
+
lines.forEach(line => this.stdOut = line);
|
|
83
|
+
return Promise.resolve();
|
|
26
84
|
}
|
|
27
85
|
}
|
|
28
86
|
|
|
29
|
-
module.exports = Cat;
|
|
87
|
+
module.exports = Cat;
|