brsh 3.5.0 → 3.6.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 +14 -0
- package/dist/shell.es.js +303 -151
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +18 -18
- package/dist/shell.min.js.map +1 -1
- package/dist/wbt.es.js +346 -155
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +9 -9
- package/dist/wbt.min.js.map +1 -1
- package/lib/WasmCommand.js +305 -10
- package/lib/wbt/index.js +88 -7
- package/lib/wbt/registry.js +3 -2
- package/package.json +1 -1
- package/test/fixtures/nano.wasm +0 -0
- package/test/terminal.test.js +30 -0
- package/test/wbt.test.js +46 -2
package/lib/WasmCommand.js
CHANGED
|
@@ -34,6 +34,93 @@ class WasmCommand extends ScriptCommand {
|
|
|
34
34
|
get name() { return 'wasm'; }
|
|
35
35
|
get requiresFilesystem() { return true; }
|
|
36
36
|
|
|
37
|
+
get interactive() {
|
|
38
|
+
const isHelpOrVersion = (this.arguments || []).some(a => a === '--help' || a === '-h' || a === '--version' || a === '-V' || a === '-v');
|
|
39
|
+
if (isHelpOrVersion) return false;
|
|
40
|
+
if (this._isInteractive !== undefined) return Boolean(this._isInteractive);
|
|
41
|
+
if (this._pkgMeta && this._pkgMeta.interactive !== undefined) return Boolean(this._pkgMeta.interactive);
|
|
42
|
+
const name = (this.script || this.name || '').toLowerCase();
|
|
43
|
+
return name === 'nano' || name.endsWith('/nano') || name.endsWith('nano.wasm');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get captureInput() {
|
|
47
|
+
return this.interactive;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_enterAltScreen() {
|
|
51
|
+
if (this._inAltScreen) return;
|
|
52
|
+
this._inAltScreen = true;
|
|
53
|
+
if (this.stdinStream && typeof this.stdinStream.setRawMode === 'function') {
|
|
54
|
+
this.stdinStream.setRawMode(true);
|
|
55
|
+
}
|
|
56
|
+
if (this.stdout && typeof this.stdout.write === 'function') {
|
|
57
|
+
this.stdout.write('\x1b[?1049h\x1b[2J\x1b[H');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_exitAltScreen() {
|
|
62
|
+
if (!this._inAltScreen) return;
|
|
63
|
+
this._inAltScreen = false;
|
|
64
|
+
if (this.stdinStream && typeof this.stdinStream.setRawMode === 'function') {
|
|
65
|
+
this.stdinStream.setRawMode(false);
|
|
66
|
+
}
|
|
67
|
+
if (this.stdout && typeof this.stdout.write === 'function') {
|
|
68
|
+
this.stdout.write('\x1b[?1049l\x1b[?25h');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
_keyToBytes(key) {
|
|
73
|
+
if (!key) return null;
|
|
74
|
+
if (typeof key !== 'string') {
|
|
75
|
+
if (key instanceof Uint8Array) return key;
|
|
76
|
+
return new Uint8Array([Number(key) & 0xff]);
|
|
77
|
+
}
|
|
78
|
+
const ctrlMatch = key.match(/^\^([A-Za-z@\[\\\]^_?])$/);
|
|
79
|
+
if (ctrlMatch) {
|
|
80
|
+
const ch = ctrlMatch[1].toUpperCase();
|
|
81
|
+
if (ch === '?') return new Uint8Array([0x7f]);
|
|
82
|
+
return new Uint8Array([ch.charCodeAt(0) - 64]);
|
|
83
|
+
}
|
|
84
|
+
const map = {
|
|
85
|
+
'Enter': '\r',
|
|
86
|
+
'Return': '\r',
|
|
87
|
+
'Backspace': '\x7f',
|
|
88
|
+
'Tab': '\t',
|
|
89
|
+
'Escape': '\x1b',
|
|
90
|
+
'ArrowUp': '\x1b[A',
|
|
91
|
+
'ArrowDown': '\x1b[B',
|
|
92
|
+
'ArrowRight': '\x1b[C',
|
|
93
|
+
'ArrowLeft': '\x1b[D',
|
|
94
|
+
'Home': '\x1b[H',
|
|
95
|
+
'End': '\x1b[F',
|
|
96
|
+
'PageUp': '\x1b[5~',
|
|
97
|
+
'PageDown': '\x1b[6~',
|
|
98
|
+
'Delete': '\x1b[3~',
|
|
99
|
+
'Insert': '\x1b[2~',
|
|
100
|
+
};
|
|
101
|
+
if (map[key]) return new TextEncoder().encode(map[key]);
|
|
102
|
+
return new TextEncoder().encode(key);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
onInput(key) {
|
|
106
|
+
if (this._handlingStreamData) return;
|
|
107
|
+
if (typeof this._externalKeyHandler === 'function') {
|
|
108
|
+
this._externalKeyHandler(key);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (this.stdinStream && this.stdinStream.isRaw) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
runCommand() {
|
|
117
|
+
return super.runCommand().finally(() => {
|
|
118
|
+
if (this._inAltScreen) {
|
|
119
|
+
this._exitAltScreen();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
37
124
|
run() {
|
|
38
125
|
const filepath = this.script;
|
|
39
126
|
const nodeFs = (() => { try { return require('fs'); } catch { return null; } })();
|
|
@@ -121,8 +208,30 @@ class WasmCommand extends ScriptCommand {
|
|
|
121
208
|
const decoder = new TextDecoder();
|
|
122
209
|
const encoder = new TextEncoder();
|
|
123
210
|
let memRef = null;
|
|
211
|
+
let instance = null;
|
|
212
|
+
let hasAsyncify = false;
|
|
213
|
+
let asyncifyDataAddr = 0;
|
|
214
|
+
let asyncifyPromise = null;
|
|
215
|
+
let pendingReadResolve = null;
|
|
216
|
+
let onResize = null;
|
|
217
|
+
let onStreamData = null;
|
|
218
|
+
|
|
219
|
+
const isInteractive = Boolean(
|
|
220
|
+
this.interactive &&
|
|
221
|
+
(this.stdin === undefined || this.stdin === null || this.stdin === this.stdinStream || (this.stdin && this.stdin.isTTY))
|
|
222
|
+
);
|
|
124
223
|
|
|
125
224
|
const writeFd = (fd, text) => {
|
|
225
|
+
if (isInteractive && (fd === 1 || fd === 2)) {
|
|
226
|
+
if (fd === 1 && this.stdout) {
|
|
227
|
+
this.stdout.write(text);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (fd === 2 && this.stderr) {
|
|
231
|
+
this.stderr.write(text);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
126
235
|
outBuf[fd] = (outBuf[fd] || '') + text;
|
|
127
236
|
const lines = outBuf[fd].split('\n');
|
|
128
237
|
outBuf[fd] = lines.pop();
|
|
@@ -133,15 +242,66 @@ class WasmCommand extends ScriptCommand {
|
|
|
133
242
|
};
|
|
134
243
|
|
|
135
244
|
const flushFd = () => {
|
|
136
|
-
if (outBuf[1]) {
|
|
137
|
-
|
|
245
|
+
if (outBuf[1]) {
|
|
246
|
+
if (isInteractive && this.stdout) this.stdout.write(outBuf[1]);
|
|
247
|
+
else this.stdOut = outBuf[1];
|
|
248
|
+
outBuf[1] = '';
|
|
249
|
+
}
|
|
250
|
+
if (outBuf[2]) {
|
|
251
|
+
if (isInteractive && this.stderr) this.stderr.write(outBuf[2]);
|
|
252
|
+
else this.stdErr = outBuf[2];
|
|
253
|
+
outBuf[2] = '';
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const inputQueue = [];
|
|
258
|
+
|
|
259
|
+
const enqueueBytes = (b) => {
|
|
260
|
+
if (!b || b.length === 0) return;
|
|
261
|
+
for (let i = 0; i < b.length; i++) {
|
|
262
|
+
inputQueue.push(b[i]);
|
|
263
|
+
}
|
|
264
|
+
if (pendingReadResolve) {
|
|
265
|
+
const r = pendingReadResolve;
|
|
266
|
+
pendingReadResolve = null;
|
|
267
|
+
r();
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
onStreamData = (chunk) => {
|
|
272
|
+
this._handlingStreamData = true;
|
|
273
|
+
try {
|
|
274
|
+
const b = typeof chunk === 'string'
|
|
275
|
+
? encoder.encode(chunk)
|
|
276
|
+
: (chunk instanceof Uint8Array
|
|
277
|
+
? chunk
|
|
278
|
+
: (typeof Buffer !== 'undefined' && Buffer.isBuffer(chunk)
|
|
279
|
+
? new Uint8Array(chunk)
|
|
280
|
+
: encoder.encode(String(chunk ?? ''))));
|
|
281
|
+
enqueueBytes(b);
|
|
282
|
+
} finally {
|
|
283
|
+
this._handlingStreamData = false;
|
|
284
|
+
}
|
|
138
285
|
};
|
|
139
286
|
|
|
140
|
-
|
|
287
|
+
this._externalKeyHandler = (key) => {
|
|
288
|
+
if (this._handlingStreamData) return;
|
|
289
|
+
const b = this._keyToBytes(key);
|
|
290
|
+
if (b) enqueueBytes(b);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
if (isInteractive && this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
294
|
+
this.stdinStream.on('data', onStreamData);
|
|
295
|
+
}
|
|
296
|
+
if (isInteractive && this.stdin && this.stdin !== this.stdinStream && typeof this.stdin.on === 'function') {
|
|
297
|
+
this.stdin.on('data', onStreamData);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const stdinBytes = (!isInteractive && typeof this.stdin === 'string')
|
|
141
301
|
? encoder.encode(this.stdin)
|
|
142
|
-
: (this.stdin instanceof Uint8Array
|
|
302
|
+
: (!isInteractive && this.stdin instanceof Uint8Array
|
|
143
303
|
? this.stdin
|
|
144
|
-
: (typeof Buffer !== 'undefined' && Buffer.isBuffer(this.stdin) ? new Uint8Array(this.stdin) : new Uint8Array(0)));
|
|
304
|
+
: (!isInteractive && typeof Buffer !== 'undefined' && Buffer.isBuffer(this.stdin) ? new Uint8Array(this.stdin) : new Uint8Array(0)));
|
|
145
305
|
let stdinPos = 0;
|
|
146
306
|
|
|
147
307
|
// ── Virtual WASI file descriptor table backed by this.context.fs ──
|
|
@@ -159,6 +319,7 @@ class WasmCommand extends ScriptCommand {
|
|
|
159
319
|
const dirEntry = fdTable.get(dirFd);
|
|
160
320
|
if (!dirEntry || (dirEntry.type !== 'preopen' && dirEntry.type !== 'dir')) return null;
|
|
161
321
|
if (relPath.startsWith('/')) return fs.absolutePath(relPath);
|
|
322
|
+
if (!relPath || relPath === '.') return fs.absolutePath(dirEntry.path);
|
|
162
323
|
const base = dirEntry.path.endsWith('/') ? dirEntry.path : dirEntry.path + '/';
|
|
163
324
|
return fs.absolutePath(base + relPath);
|
|
164
325
|
};
|
|
@@ -185,9 +346,15 @@ class WasmCommand extends ScriptCommand {
|
|
|
185
346
|
}
|
|
186
347
|
};
|
|
187
348
|
|
|
188
|
-
// Environment variables from context.vars
|
|
189
|
-
const
|
|
190
|
-
|
|
349
|
+
// Environment variables from context.vars with terminal defaults
|
|
350
|
+
const termVars = {
|
|
351
|
+
TERM: 'xterm-256color',
|
|
352
|
+
COLORTERM: 'truecolor',
|
|
353
|
+
COLUMNS: String((this.stdout && this.stdout.columns) || (this.context && typeof this.context.getVar === 'function' && this.context.getVar('COLUMNS')) || 80),
|
|
354
|
+
LINES: String((this.stdout && this.stdout.rows) || (this.context && typeof this.context.getVar === 'function' && this.context.getVar('LINES')) || 24),
|
|
355
|
+
...((this.context && this.context.vars) || {})
|
|
356
|
+
};
|
|
357
|
+
const envPairs = Object.entries(termVars).map(([k, v]) => `${k}=${v}`);
|
|
191
358
|
|
|
192
359
|
const wasi = {
|
|
193
360
|
fd_write: (fd, iovs, iovs_len, nwritten_ptr) => {
|
|
@@ -236,6 +403,48 @@ class WasmCommand extends ScriptCommand {
|
|
|
236
403
|
const view = new DataView(memRef.buffer);
|
|
237
404
|
|
|
238
405
|
if (fd === 0) {
|
|
406
|
+
if (isInteractive && hasAsyncify) {
|
|
407
|
+
const state = (typeof instance?.exports?.asyncify_get_state === 'function')
|
|
408
|
+
? instance.exports.asyncify_get_state()
|
|
409
|
+
: 0;
|
|
410
|
+
|
|
411
|
+
if (state === 2) {
|
|
412
|
+
instance.exports.asyncify_stop_rewind();
|
|
413
|
+
let total = 0;
|
|
414
|
+
for (let i = 0; i < iovs_len && inputQueue.length > 0; i++) {
|
|
415
|
+
const ptr = view.getUint32(iovs + i * 8, true);
|
|
416
|
+
const len = view.getUint32(iovs + i * 8 + 4, true);
|
|
417
|
+
const chunk = Math.min(len, inputQueue.length);
|
|
418
|
+
const slice = inputQueue.splice(0, chunk);
|
|
419
|
+
new Uint8Array(memRef.buffer).set(slice, ptr);
|
|
420
|
+
total += chunk;
|
|
421
|
+
}
|
|
422
|
+
view.setUint32(nread_ptr, total, true);
|
|
423
|
+
return ERRNO_SUCCESS;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (inputQueue.length > 0) {
|
|
427
|
+
let total = 0;
|
|
428
|
+
for (let i = 0; i < iovs_len && inputQueue.length > 0; i++) {
|
|
429
|
+
const ptr = view.getUint32(iovs + i * 8, true);
|
|
430
|
+
const len = view.getUint32(iovs + i * 8 + 4, true);
|
|
431
|
+
const chunk = Math.min(len, inputQueue.length);
|
|
432
|
+
const slice = inputQueue.splice(0, chunk);
|
|
433
|
+
new Uint8Array(memRef.buffer).set(slice, ptr);
|
|
434
|
+
total += chunk;
|
|
435
|
+
}
|
|
436
|
+
view.setUint32(nread_ptr, total, true);
|
|
437
|
+
return ERRNO_SUCCESS;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
instance.exports.asyncify_start_unwind(asyncifyDataAddr);
|
|
441
|
+
asyncifyPromise = new Promise(resolve => {
|
|
442
|
+
pendingReadResolve = resolve;
|
|
443
|
+
});
|
|
444
|
+
view.setUint32(nread_ptr, 0, true);
|
|
445
|
+
return ERRNO_SUCCESS;
|
|
446
|
+
}
|
|
447
|
+
|
|
239
448
|
let total = 0;
|
|
240
449
|
for (let i = 0; i < iovs_len && stdinPos < stdinBytes.length; i++) {
|
|
241
450
|
const ptr = view.getUint32(iovs + i * 8, true);
|
|
@@ -481,6 +690,31 @@ class WasmCommand extends ScriptCommand {
|
|
|
481
690
|
return ERRNO_INVAL;
|
|
482
691
|
}
|
|
483
692
|
},
|
|
693
|
+
path_rename: (old_dirfd, old_path_ptr, old_path_len, new_dirfd, new_path_ptr, new_path_len) => {
|
|
694
|
+
if (!memRef || !fs) return ERRNO_BADF;
|
|
695
|
+
const oldRel = decoder.decode(new Uint8Array(memRef.buffer, old_path_ptr, old_path_len));
|
|
696
|
+
const newRel = decoder.decode(new Uint8Array(memRef.buffer, new_path_ptr, new_path_len));
|
|
697
|
+
const oldAbs = resolveGuestPath(old_dirfd, oldRel);
|
|
698
|
+
const newAbs = resolveGuestPath(new_dirfd, newRel);
|
|
699
|
+
if (!oldAbs || !newAbs) return ERRNO_BADF;
|
|
700
|
+
if (!fs.exists(oldAbs)) return ERRNO_NOENT;
|
|
701
|
+
|
|
702
|
+
for (const entry of fdTable.values()) {
|
|
703
|
+
if (entry.path === oldAbs) {
|
|
704
|
+
flushOpenFile(entry);
|
|
705
|
+
entry.path = newAbs;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
try {
|
|
710
|
+
const content = fs.getFileByPath(oldAbs);
|
|
711
|
+
fs.setFileByPath(newAbs, content, true);
|
|
712
|
+
fs.deleteFileByPath(oldAbs);
|
|
713
|
+
return ERRNO_SUCCESS;
|
|
714
|
+
} catch {
|
|
715
|
+
return ERRNO_INVAL;
|
|
716
|
+
}
|
|
717
|
+
},
|
|
484
718
|
proc_exit: (code) => {
|
|
485
719
|
if (code !== 0) this.exitCode = code;
|
|
486
720
|
throw { _wasmExit: true, code };
|
|
@@ -597,11 +831,47 @@ class WasmCommand extends ScriptCommand {
|
|
|
597
831
|
}
|
|
598
832
|
}
|
|
599
833
|
|
|
600
|
-
|
|
834
|
+
instance = await WebAssembly.instantiate(module, imports);
|
|
601
835
|
|
|
602
836
|
// Prefer the module's own exported memory
|
|
603
837
|
if (instance.exports.memory) memRef = instance.exports.memory;
|
|
604
838
|
|
|
839
|
+
hasAsyncify = Boolean(instance.exports.asyncify_start_unwind && instance.exports.asyncify_stop_unwind);
|
|
840
|
+
if (hasAsyncify && memRef) {
|
|
841
|
+
try {
|
|
842
|
+
const prevPages = memRef.grow(1);
|
|
843
|
+
asyncifyDataAddr = prevPages * 65536;
|
|
844
|
+
} catch {
|
|
845
|
+
asyncifyDataAddr = 16;
|
|
846
|
+
}
|
|
847
|
+
const dataStart = asyncifyDataAddr + 8;
|
|
848
|
+
const dataEnd = asyncifyDataAddr + 65536 - 8;
|
|
849
|
+
new Int32Array(memRef.buffer, asyncifyDataAddr, 2).set([dataStart, dataEnd]);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
if (isInteractive) {
|
|
853
|
+
this._enterAltScreen();
|
|
854
|
+
const cols = (this.stdout && this.stdout.columns) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('COLUMNS'), 10)) || 80;
|
|
855
|
+
const rows = (this.stdout && this.stdout.rows) || (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('LINES'), 10)) || 24;
|
|
856
|
+
if (typeof instance.exports.set_terminal_size === 'function') {
|
|
857
|
+
try { instance.exports.set_terminal_size(cols, rows); } catch {}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
onResize = () => {
|
|
861
|
+
const c = (this.stdout && this.stdout.columns) || 80;
|
|
862
|
+
const r = (this.stdout && this.stdout.rows) || 24;
|
|
863
|
+
if (typeof instance?.exports?.set_terminal_size === 'function') {
|
|
864
|
+
try { instance.exports.set_terminal_size(c, r); } catch {}
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
if (this.stdout && typeof this.stdout.on === 'function') {
|
|
868
|
+
this.stdout.on('resize', onResize);
|
|
869
|
+
}
|
|
870
|
+
if (this.stdinStream && typeof this.stdinStream.on === 'function') {
|
|
871
|
+
this.stdinStream.on('resize', onResize);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
605
875
|
const start = instance.exports._start
|
|
606
876
|
|| instance.exports.__wasi_command
|
|
607
877
|
|| instance.exports.main
|
|
@@ -618,7 +888,19 @@ class WasmCommand extends ScriptCommand {
|
|
|
618
888
|
this.exitCode = 1;
|
|
619
889
|
} else {
|
|
620
890
|
try {
|
|
621
|
-
|
|
891
|
+
let res = start();
|
|
892
|
+
if (res && typeof res.then === 'function') res = await res;
|
|
893
|
+
|
|
894
|
+
while (hasAsyncify && typeof instance.exports.asyncify_get_state === 'function' && instance.exports.asyncify_get_state() === 1) {
|
|
895
|
+
instance.exports.asyncify_stop_unwind();
|
|
896
|
+
if (asyncifyPromise) {
|
|
897
|
+
await asyncifyPromise;
|
|
898
|
+
asyncifyPromise = null;
|
|
899
|
+
}
|
|
900
|
+
instance.exports.asyncify_start_rewind(asyncifyDataAddr);
|
|
901
|
+
res = start();
|
|
902
|
+
if (res && typeof res.then === 'function') res = await res;
|
|
903
|
+
}
|
|
622
904
|
} catch(e) {
|
|
623
905
|
if (!e || !e._wasmExit) {
|
|
624
906
|
this.stdErr = `${this.script}: runtime error: ${e?.message || String(e)}`;
|
|
@@ -632,6 +914,19 @@ class WasmCommand extends ScriptCommand {
|
|
|
632
914
|
this.exitCode = 1;
|
|
633
915
|
}
|
|
634
916
|
} finally {
|
|
917
|
+
if (isInteractive) {
|
|
918
|
+
this._exitAltScreen();
|
|
919
|
+
}
|
|
920
|
+
if (isInteractive && onStreamData) {
|
|
921
|
+
if (this.stdinStream && typeof this.stdinStream.off === 'function') this.stdinStream.off('data', onStreamData);
|
|
922
|
+
if (this.stdin && typeof this.stdin.off === 'function') this.stdin.off('data', onStreamData);
|
|
923
|
+
}
|
|
924
|
+
if (onResize) {
|
|
925
|
+
if (this.stdout && typeof this.stdout.off === 'function') this.stdout.off('resize', onResize);
|
|
926
|
+
if (this.stdinStream && typeof this.stdinStream.off === 'function') this.stdinStream.off('resize', onResize);
|
|
927
|
+
}
|
|
928
|
+
this._externalKeyHandler = null;
|
|
929
|
+
|
|
635
930
|
// Flush any open dirty WASI file descriptors back to the brsh filesystem
|
|
636
931
|
for (const entry of fdTable.values()) {
|
|
637
932
|
flushOpenFile(entry);
|
package/lib/wbt/index.js
CHANGED
|
@@ -19,6 +19,17 @@ function createCustomWasmBinCommand(binName, wasmPath, pkgMeta = {}) {
|
|
|
19
19
|
get name() { return binName; }
|
|
20
20
|
get requiresFilesystem() { return true; }
|
|
21
21
|
|
|
22
|
+
get interactive() {
|
|
23
|
+
const isHelpOrVersion = (this.arguments || []).some(a => a === '--help' || a === '-h' || a === '--version' || a === '-V' || a === '-v');
|
|
24
|
+
if (isHelpOrVersion) return false;
|
|
25
|
+
if (this._isInteractive !== undefined) return Boolean(this._isInteractive);
|
|
26
|
+
return Boolean(pkgMeta.interactive || binName === 'nano');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get captureInput() {
|
|
30
|
+
return this.interactive;
|
|
31
|
+
}
|
|
32
|
+
|
|
22
33
|
constructor(args, context) {
|
|
23
34
|
super(wasmPath, args, context);
|
|
24
35
|
this.script = wasmPath;
|
|
@@ -49,6 +60,34 @@ class Wbt extends LocalCommand {
|
|
|
49
60
|
get name() { return 'wbt'; }
|
|
50
61
|
get requiresFilesystem() { return true; }
|
|
51
62
|
|
|
63
|
+
get interactive() {
|
|
64
|
+
if (this._activeSubCommand && this._activeSubCommand.interactive !== undefined) {
|
|
65
|
+
return Boolean(this._activeSubCommand.interactive);
|
|
66
|
+
}
|
|
67
|
+
if (this.arguments && this.arguments[0] === 'run' && this.arguments[1]) {
|
|
68
|
+
const rawName = this.arguments[1].toLowerCase();
|
|
69
|
+
const pkgMeta = this._getRegistry()[rawName];
|
|
70
|
+
if (pkgMeta && pkgMeta.interactive) {
|
|
71
|
+
const isHelpOrVersion = this.arguments.slice(2).some(a => a === '--help' || a === '-h' || a === '--version' || a === '-V' || a === '-v');
|
|
72
|
+
return !isHelpOrVersion;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get captureInput() {
|
|
79
|
+
if (this._activeSubCommand && this._activeSubCommand.captureInput !== undefined) {
|
|
80
|
+
return Boolean(this._activeSubCommand.captureInput);
|
|
81
|
+
}
|
|
82
|
+
return this.interactive;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
onInput(key) {
|
|
86
|
+
if (this._activeSubCommand && typeof this._activeSubCommand.onInput === 'function') {
|
|
87
|
+
this._activeSubCommand.onInput(key);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
52
91
|
static get _config() {
|
|
53
92
|
return this.__wbtConfig || {};
|
|
54
93
|
}
|
|
@@ -959,6 +998,19 @@ class Wbt extends LocalCommand {
|
|
|
959
998
|
return;
|
|
960
999
|
}
|
|
961
1000
|
|
|
1001
|
+
if ((pkgMeta.type === 'custom-wasm' || pkgMeta.type === 'wasm') && !pkgMeta.wasmPath) {
|
|
1002
|
+
try {
|
|
1003
|
+
const wasmPath = await this._pullPackageWasm(pkgMeta, true);
|
|
1004
|
+
if (wasmPath) {
|
|
1005
|
+
pkgMeta = Object.assign({}, pkgMeta, { wasmPath });
|
|
1006
|
+
}
|
|
1007
|
+
} catch (err) {
|
|
1008
|
+
this.stdErr = `E: Failed to fetch ${pkgMeta.name}: ${err.message || err}`;
|
|
1009
|
+
this.exitCode = 1;
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
|
|
962
1014
|
const entries = this._buildCommandsForPackage(pkgMeta);
|
|
963
1015
|
// Find matching entry if user specified e.g. `wbt run asrun` or `wbt run wasm2wat`
|
|
964
1016
|
const matched = entries.find(e => e.bin === rawName.toLowerCase()) || entries[0];
|
|
@@ -968,15 +1020,44 @@ class Wbt extends LocalCommand {
|
|
|
968
1020
|
if (instance.requiresFilesystem) {
|
|
969
1021
|
instance.fs = this.context.fs.sandbox();
|
|
970
1022
|
}
|
|
971
|
-
|
|
972
|
-
|
|
1023
|
+
|
|
1024
|
+
// Pipe I/O and forward interactivity
|
|
1025
|
+
this._activeSubCommand = instance;
|
|
1026
|
+
instance.stdinStream = this.stdinStream;
|
|
1027
|
+
instance.stdin = (this.stdin && !this.stdin.isTTY) ? this.stdin : this.stdinStream;
|
|
1028
|
+
|
|
1029
|
+
const onStdout = (chunk) => {
|
|
1030
|
+
if (this.stdout && typeof this.stdout.write === 'function') this.stdout.write(chunk);
|
|
1031
|
+
};
|
|
1032
|
+
const onStderr = (chunk) => {
|
|
1033
|
+
if (this.stderr && typeof this.stderr.write === 'function') this.stderr.write(chunk);
|
|
1034
|
+
};
|
|
1035
|
+
if (instance.stdout && typeof instance.stdout.on === 'function') {
|
|
1036
|
+
instance.stdout.on('data', onStdout);
|
|
1037
|
+
}
|
|
1038
|
+
if (instance.stderr && typeof instance.stderr.on === 'function') {
|
|
1039
|
+
instance.stderr.on('data', onStderr);
|
|
973
1040
|
}
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
1041
|
+
|
|
1042
|
+
let res;
|
|
1043
|
+
try {
|
|
1044
|
+
res = await instance.runCommand();
|
|
1045
|
+
} finally {
|
|
1046
|
+
this._activeSubCommand = null;
|
|
1047
|
+
if (instance.stdout && typeof instance.stdout.off === 'function') {
|
|
1048
|
+
instance.stdout.off('data', onStdout);
|
|
1049
|
+
}
|
|
1050
|
+
if (instance.stderr && typeof instance.stderr.off === 'function') {
|
|
1051
|
+
instance.stderr.off('data', onStderr);
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
if (!instance.interactive && res) {
|
|
1055
|
+
for (const line of res.getStdOutput()) {
|
|
1056
|
+
if (line.type === 'out') this.stdOut = line.string;
|
|
1057
|
+
else if (line.type === 'err') this.stdErr = line.string;
|
|
1058
|
+
}
|
|
978
1059
|
}
|
|
979
|
-
this.exitCode = res.getExitCode();
|
|
1060
|
+
this.exitCode = res ? res.getExitCode() : 0;
|
|
980
1061
|
}
|
|
981
1062
|
|
|
982
1063
|
_printHelp() {
|
package/lib/wbt/registry.js
CHANGED
|
@@ -171,14 +171,15 @@ const DEFAULT_REGISTRY = {
|
|
|
171
171
|
name: 'nano',
|
|
172
172
|
bin: 'nano',
|
|
173
173
|
aliases: ['nano-wasm'],
|
|
174
|
-
version: '8.0.
|
|
174
|
+
version: '8.0.1',
|
|
175
175
|
description: 'GNU nano 8.0 text editor compiled to WebAssembly (wasm32-wasip1) with ncurses',
|
|
176
176
|
details: 'GNU nano is a small, user-friendly command-line text editor compiled to WebAssembly with wide-character ncurses 6.5, built-in terminfo fallbacks (xterm-256color, vt100, etc.), dynamic terminal dimensions (TIOCGWINSZ / LINES & COLUMNS), and zero custom imports (standard wasi_snapshot_preview1).',
|
|
177
177
|
homepage: 'https://github.com/eliotstocker/nano-wasm',
|
|
178
178
|
cdnUrl: 'https://cdn.jsdelivr.net/gh/eliotstocker/nano-wasm@main/dist/nano.wasm',
|
|
179
179
|
wasmUrl: 'https://cdn.jsdelivr.net/gh/eliotstocker/nano-wasm@main/dist/nano.wasm',
|
|
180
180
|
type: 'custom-wasm',
|
|
181
|
-
|
|
181
|
+
interactive: true,
|
|
182
|
+
size: '680 KB'
|
|
182
183
|
}
|
|
183
184
|
};
|
|
184
185
|
|
package/package.json
CHANGED
|
Binary file
|
package/test/terminal.test.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import fs from 'fs';
|
|
2
4
|
import Shell from '../index.js';
|
|
3
5
|
import Terminal from '../lib/Terminal.js';
|
|
4
6
|
import Pico from '../demo/pico.js';
|
|
@@ -561,6 +563,34 @@ describe('brsh Shell Integration, Line Editing, and Full-Screen Apps', () => {
|
|
|
561
563
|
expect(term.screen.isAltScreen).toBe(false);
|
|
562
564
|
expect(term.getLineText(0)).toContain('brsh:root$ demorepl');
|
|
563
565
|
});
|
|
566
|
+
|
|
567
|
+
it('runs nano text editor in Terminal alternate screen and restores screen on exit', async () => {
|
|
568
|
+
const FIXTURE_NANO_WASM = path.resolve(import.meta.dirname, 'fixtures/nano.wasm');
|
|
569
|
+
if (!fs.existsSync(FIXTURE_NANO_WASM)) return;
|
|
570
|
+
const bytes = fs.readFileSync(FIXTURE_NANO_WASM);
|
|
571
|
+
const Wbt = (await import('../lib/wbt/index.js')).default.configure({
|
|
572
|
+
fetch: async () => ({
|
|
573
|
+
ok: true,
|
|
574
|
+
status: 200,
|
|
575
|
+
arrayBuffer: async () => bytes.buffer
|
|
576
|
+
})
|
|
577
|
+
});
|
|
578
|
+
shell.context.setCommand(Wbt);
|
|
579
|
+
await shell.onCommand('wbt install nano');
|
|
580
|
+
|
|
581
|
+
const nanoDone = waitForReady(shell);
|
|
582
|
+
shell.onCommand('nano');
|
|
583
|
+
await new Promise(r => setTimeout(r, 80));
|
|
584
|
+
|
|
585
|
+
expect(term.screen.isAltScreen).toBe(true);
|
|
586
|
+
expect(term.getScreenText()).toContain('GNU nano 8.0');
|
|
587
|
+
|
|
588
|
+
// Exit nano with Ctrl+X
|
|
589
|
+
term.sendKey('x', { ctrl: true });
|
|
590
|
+
await nanoDone;
|
|
591
|
+
|
|
592
|
+
expect(term.screen.isAltScreen).toBe(false);
|
|
593
|
+
});
|
|
564
594
|
});
|
|
565
595
|
|
|
566
596
|
describe('TUI (blessed-style) Application over Virtual TTY & Real zsh PTY Compatibility', () => {
|
package/test/wbt.test.js
CHANGED
|
@@ -8,9 +8,13 @@ const fs = require('fs');
|
|
|
8
8
|
const Wbt = require('../lib/wbt/index.js');
|
|
9
9
|
const { wllamaInstances, DEFAULT_MODEL_URL, MODEL_ALIASES, resolveModelSpec } = require('../lib/wbt/packages/wllama.js');
|
|
10
10
|
|
|
11
|
+
const FIXTURE_NANO_WASM = path.resolve(import.meta.dirname, 'fixtures/nano.wasm');
|
|
11
12
|
const NANO_WASM_PATH = path.resolve(import.meta.dirname, '../../nano-wasm/dist/nano.wasm');
|
|
12
13
|
const NANO_WASM_BYTES = (() => {
|
|
13
14
|
try {
|
|
15
|
+
if (fs.existsSync(FIXTURE_NANO_WASM)) {
|
|
16
|
+
return new Uint8Array(fs.readFileSync(FIXTURE_NANO_WASM));
|
|
17
|
+
}
|
|
14
18
|
if (fs.existsSync(NANO_WASM_PATH)) {
|
|
15
19
|
return new Uint8Array(fs.readFileSync(NANO_WASM_PATH));
|
|
16
20
|
}
|
|
@@ -1342,7 +1346,7 @@ describe('WBT GNU nano WASM package (nano / nano-wasm)', () => {
|
|
|
1342
1346
|
it('registers nano and nano-wasm in registry, wbt search, and wbt show', async () => {
|
|
1343
1347
|
const listRes = await run(shell, 'wbt list');
|
|
1344
1348
|
expect(listRes.exitCode).toBe(0);
|
|
1345
|
-
expect(listRes.output.join('\n')).toContain('nano/wasm 8.0.
|
|
1349
|
+
expect(listRes.output.join('\n')).toContain('nano/wasm 8.0.1');
|
|
1346
1350
|
|
|
1347
1351
|
const searchRes = await run(shell, 'wbt search editor');
|
|
1348
1352
|
expect(searchRes.exitCode).toBe(0);
|
|
@@ -1351,7 +1355,7 @@ describe('WBT GNU nano WASM package (nano / nano-wasm)', () => {
|
|
|
1351
1355
|
const showRes = await run(shell, 'wbt show nano');
|
|
1352
1356
|
expect(showRes.exitCode).toBe(0);
|
|
1353
1357
|
expect(showRes.output.join('\n')).toContain('Package: nano');
|
|
1354
|
-
expect(showRes.output.join('\n')).toContain('Version: 8.0.
|
|
1358
|
+
expect(showRes.output.join('\n')).toContain('Version: 8.0.1');
|
|
1355
1359
|
expect(showRes.output.join('\n')).toContain('Aliases: nano-wasm');
|
|
1356
1360
|
|
|
1357
1361
|
const showAliasRes = await run(shell, 'wbt show nano-wasm');
|
|
@@ -1415,6 +1419,46 @@ describe('WBT GNU nano WASM package (nano / nano-wasm)', () => {
|
|
|
1415
1419
|
expect(res.getExitCode()).toBe(0);
|
|
1416
1420
|
expect(res.getStdOutput().map(l => l.string).join('\n')).toMatch(/GNU nano, version 8\.0|nano.*8\.0\.0.*wasm/i);
|
|
1417
1421
|
});
|
|
1422
|
+
|
|
1423
|
+
it('runs nano interactively, accepts text input, writes out file, and exits cleanly', async () => {
|
|
1424
|
+
if (!NANO_WASM_BYTES) return;
|
|
1425
|
+
await run(shell, 'wbt install nano');
|
|
1426
|
+
|
|
1427
|
+
const stdoutChunks = [];
|
|
1428
|
+
const onData = (chunk) => stdoutChunks.push(chunk);
|
|
1429
|
+
shell.stdout.on('data', onData);
|
|
1430
|
+
|
|
1431
|
+
const cmdPromise = shell.onCommand('nano /test.txt');
|
|
1432
|
+
|
|
1433
|
+
// Allow nano to initialize, query terminal size, enter alt-screen, and render initial UI
|
|
1434
|
+
await new Promise(r => setTimeout(r, 60));
|
|
1435
|
+
|
|
1436
|
+
// Type text into nano
|
|
1437
|
+
for (const ch of 'Hello from nano!') {
|
|
1438
|
+
shell.onInput(ch);
|
|
1439
|
+
}
|
|
1440
|
+
await new Promise(r => setTimeout(r, 30));
|
|
1441
|
+
|
|
1442
|
+
// Save file: Ctrl+O, then Enter to confirm filename
|
|
1443
|
+
shell.onInput('^O');
|
|
1444
|
+
await new Promise(r => setTimeout(r, 30));
|
|
1445
|
+
shell.onInput('Enter');
|
|
1446
|
+
await new Promise(r => setTimeout(r, 30));
|
|
1447
|
+
|
|
1448
|
+
// Exit nano: Ctrl+X
|
|
1449
|
+
shell.onInput('^X');
|
|
1450
|
+
|
|
1451
|
+
await cmdPromise;
|
|
1452
|
+
shell.stdout.off('data', onData);
|
|
1453
|
+
|
|
1454
|
+
const saved = shell.context.fs.getFileByPath('/test.txt');
|
|
1455
|
+
const joinedOutput = stdoutChunks.join('');
|
|
1456
|
+
|
|
1457
|
+
expect(shell.lastCode).toBe(0);
|
|
1458
|
+
expect(saved).toContain('Hello from nano!');
|
|
1459
|
+
expect(joinedOutput).toContain('\x1b[?1049h'); // entered alt screen
|
|
1460
|
+
expect(joinedOutput).toContain('\x1b[?1049l'); // exited alt screen
|
|
1461
|
+
});
|
|
1418
1462
|
});
|
|
1419
1463
|
|
|
1420
1464
|
|