brsh 3.6.0 → 3.6.2

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.
@@ -3,6 +3,7 @@
3
3
  // The set of methods exposed to command instances via sandbox().
4
4
  // Anything not in this list is inaccessible to user-supplied commands.
5
5
  const SANDBOX_ALLOWED = new Set([
6
+ 'cwd',
6
7
  'getFileByPath', 'setFileByPath', 'deleteFileByPath',
7
8
  'exists', 'stat', 'absolutePath',
8
9
  'getMode', 'setMode',
package/lib/FileSystem.js CHANGED
@@ -55,7 +55,7 @@ class FileSystem extends BaseFileSystem {
55
55
  decodeBinaryFiles(filesystem);
56
56
  this.fileSystem = filesystem;
57
57
  this.permissions = Object.assign({}, permissions);
58
- this.cwd = cwd;
58
+ this.cwd = cwd.endsWith('/') ? cwd : (cwd + '/');
59
59
  this._onChange = onChange;
60
60
  this._changeScheduled = false;
61
61
  }
@@ -130,7 +130,8 @@ class FileSystem extends BaseFileSystem {
130
130
  path = path.substring(2);
131
131
  }
132
132
  if(!path.startsWith('/')) {
133
- path = this.cwd + path;
133
+ const base = (this.cwd && this.cwd.endsWith('/')) ? this.cwd : (this.cwd ? this.cwd + '/' : '/');
134
+ path = base + path;
134
135
  }
135
136
  return this._traverseComplexAbsolutePath(path);
136
137
  }
@@ -218,7 +218,7 @@ class WasmCommand extends ScriptCommand {
218
218
 
219
219
  const isInteractive = Boolean(
220
220
  this.interactive &&
221
- (this.stdin === undefined || this.stdin === null || this.stdin === this.stdinStream)
221
+ (this.stdin === undefined || this.stdin === null || this.stdin === this.stdinStream || (this.stdin && this.stdin.isTTY))
222
222
  );
223
223
 
224
224
  const writeFd = (fd, text) => {
@@ -293,6 +293,9 @@ class WasmCommand extends ScriptCommand {
293
293
  if (isInteractive && this.stdinStream && typeof this.stdinStream.on === 'function') {
294
294
  this.stdinStream.on('data', onStreamData);
295
295
  }
296
+ if (isInteractive && this.stdin && this.stdin !== this.stdinStream && typeof this.stdin.on === 'function') {
297
+ this.stdin.on('data', onStreamData);
298
+ }
296
299
 
297
300
  const stdinBytes = (!isInteractive && typeof this.stdin === 'string')
298
301
  ? encoder.encode(this.stdin)
@@ -306,19 +309,55 @@ class WasmCommand extends ScriptCommand {
306
309
  // fd 3: pre-opened root directory '/'
307
310
  // fd 4: pre-opened current working directory '.'
308
311
  const fs = this.context && this.context.fs ? this.context.fs : this.fs;
312
+ const rawCwd = (this.context && this.context.fs && this.context.fs.cwd)
313
+ || (this.fs && this.fs.cwd)
314
+ || (this.context && typeof this.context.getVar === 'function' && this.context.getVar('PWD'))
315
+ || (fs && fs.cwd)
316
+ || '/';
317
+ const cwd = rawCwd.length > 1 && rawCwd.endsWith('/') ? rawCwd.slice(0, -1) : rawCwd;
309
318
  let nextFd = 5;
310
319
  const fdTable = new Map([
311
320
  [3, { type: 'preopen', path: '/', guestName: '/' }],
312
- [4, { type: 'preopen', path: (fs && fs.cwd) || '/', guestName: '.' }]
321
+ [4, { type: 'preopen', path: cwd, guestName: '.' }]
313
322
  ]);
314
323
 
324
+ const getParentPath = (p) => {
325
+ const parts = p.split('/').filter(Boolean);
326
+ parts.pop();
327
+ return '/' + parts.join('/');
328
+ };
329
+
315
330
  const resolveGuestPath = (dirFd, relPath) => {
316
331
  const dirEntry = fdTable.get(dirFd);
317
332
  if (!dirEntry || (dirEntry.type !== 'preopen' && dirEntry.type !== 'dir')) return null;
318
333
  if (relPath.startsWith('/')) return fs.absolutePath(relPath);
319
334
  if (!relPath || relPath === '.') return fs.absolutePath(dirEntry.path);
335
+
320
336
  const base = dirEntry.path.endsWith('/') ? dirEntry.path : dirEntry.path + '/';
321
- return fs.absolutePath(base + relPath);
337
+ const directPath = fs.absolutePath(base + relPath);
338
+ const rootPath = fs.absolutePath('/' + relPath);
339
+
340
+ // If dirEntry is not '/', wasi-libc may have stripped the leading '/'
341
+ // from an absolute path (e.g. 'root/ansitest.js' or 'etc/hosts')
342
+ if (dirEntry.path !== '/') {
343
+ if (fs.exists(directPath)) return directPath;
344
+ if (fs.exists(rootPath)) return rootPath;
345
+
346
+ // When file doesn't exist yet (e.g. creating new file), check parent directories
347
+ const directParent = fs.exists(getParentPath(directPath));
348
+ const rootParent = fs.exists(getParentPath(rootPath));
349
+ if (rootParent && !directParent) return rootPath;
350
+ if (directParent && !rootParent) return directPath;
351
+
352
+ return directPath;
353
+ }
354
+
355
+ // dirEntry.path is '/'
356
+ if (fs.exists(directPath)) return directPath;
357
+ const cwdCandidate = fs.absolutePath(relPath);
358
+ if (fs.exists(cwdCandidate)) return cwdCandidate;
359
+
360
+ return directPath;
322
361
  };
323
362
 
324
363
  const flushOpenFile = (entry) => {
@@ -914,8 +953,9 @@ class WasmCommand extends ScriptCommand {
914
953
  if (isInteractive) {
915
954
  this._exitAltScreen();
916
955
  }
917
- if (isInteractive && onStreamData && this.stdinStream && typeof this.stdinStream.off === 'function') {
918
- this.stdinStream.off('data', onStreamData);
956
+ if (isInteractive && onStreamData) {
957
+ if (this.stdinStream && typeof this.stdinStream.off === 'function') this.stdinStream.off('data', onStreamData);
958
+ if (this.stdin && typeof this.stdin.off === 'function') this.stdin.off('data', onStreamData);
919
959
  }
920
960
  if (onResize) {
921
961
  if (this.stdout && typeof this.stdout.off === 'function') this.stdout.off('resize', onResize);
package/lib/wbt/index.js CHANGED
@@ -60,6 +60,34 @@ class Wbt extends LocalCommand {
60
60
  get name() { return 'wbt'; }
61
61
  get requiresFilesystem() { return true; }
62
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
+
63
91
  static get _config() {
64
92
  return this.__wbtConfig || {};
65
93
  }
@@ -970,6 +998,19 @@ class Wbt extends LocalCommand {
970
998
  return;
971
999
  }
972
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
+
973
1014
  const entries = this._buildCommandsForPackage(pkgMeta);
974
1015
  // Find matching entry if user specified e.g. `wbt run asrun` or `wbt run wasm2wat`
975
1016
  const matched = entries.find(e => e.bin === rawName.toLowerCase()) || entries[0];
@@ -979,15 +1020,44 @@ class Wbt extends LocalCommand {
979
1020
  if (instance.requiresFilesystem) {
980
1021
  instance.fs = this.context.fs.sandbox();
981
1022
  }
982
- if (this.stdin !== undefined) {
983
- instance.stdin = this.stdin;
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);
984
1040
  }
985
- const res = await instance.runCommand();
986
- for (const line of res.getStdOutput()) {
987
- if (line.type === 'out') this.stdOut = line.string;
988
- else if (line.type === 'err') this.stdErr = line.string;
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
+ }
989
1059
  }
990
- this.exitCode = res.getExitCode();
1060
+ this.exitCode = res ? res.getExitCode() : 0;
991
1061
  }
992
1062
 
993
1063
  _printHelp() {
@@ -171,7 +171,7 @@ const DEFAULT_REGISTRY = {
171
171
  name: 'nano',
172
172
  bin: 'nano',
173
173
  aliases: ['nano-wasm'],
174
- version: '8.0.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',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "3.6.0",
3
+ "version": "3.6.2",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
@@ -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,66 @@ 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
+ });
594
+
595
+ it('runs nano text editor opening a file and displaying its content', async () => {
596
+ const FIXTURE_NANO_WASM = path.resolve(import.meta.dirname, 'fixtures/nano.wasm');
597
+ if (!fs.existsSync(FIXTURE_NANO_WASM)) return;
598
+ const bytes = fs.readFileSync(FIXTURE_NANO_WASM);
599
+ const Wbt = (await import('../lib/wbt/index.js')).default.configure({
600
+ fetch: async () => ({
601
+ ok: true,
602
+ status: 200,
603
+ arrayBuffer: async () => bytes.buffer
604
+ })
605
+ });
606
+ shell.context.setCommand(Wbt);
607
+ await shell.onCommand('wbt install nano');
608
+
609
+ shell.context.fs.setFileByPath('greeting.txt', 'Hello from inside nano buffer!', true);
610
+
611
+ const nanoDone = waitForReady(shell);
612
+ shell.onCommand('nano greeting.txt');
613
+ await new Promise(r => setTimeout(r, 80));
614
+
615
+ expect(term.screen.isAltScreen).toBe(true);
616
+ expect(term.getScreenText()).toContain('greeting.txt');
617
+ expect(term.getScreenText()).toContain('Hello from inside nano buffer!');
618
+ expect(term.getScreenText()).not.toContain('does not exist');
619
+
620
+ // Exit nano with Ctrl+X
621
+ term.sendKey('x', { ctrl: true });
622
+ await nanoDone;
623
+
624
+ expect(term.screen.isAltScreen).toBe(false);
625
+ });
564
626
  });
565
627
 
566
628
  describe('TUI (blessed-style) Application over Virtual TTY & Real zsh PTY Compatibility', () => {
package/test/wbt.test.js CHANGED
@@ -1346,7 +1346,7 @@ describe('WBT GNU nano WASM package (nano / nano-wasm)', () => {
1346
1346
  it('registers nano and nano-wasm in registry, wbt search, and wbt show', async () => {
1347
1347
  const listRes = await run(shell, 'wbt list');
1348
1348
  expect(listRes.exitCode).toBe(0);
1349
- expect(listRes.output.join('\n')).toContain('nano/wasm 8.0.0');
1349
+ expect(listRes.output.join('\n')).toContain('nano/wasm 8.0.1');
1350
1350
 
1351
1351
  const searchRes = await run(shell, 'wbt search editor');
1352
1352
  expect(searchRes.exitCode).toBe(0);
@@ -1355,7 +1355,7 @@ describe('WBT GNU nano WASM package (nano / nano-wasm)', () => {
1355
1355
  const showRes = await run(shell, 'wbt show nano');
1356
1356
  expect(showRes.exitCode).toBe(0);
1357
1357
  expect(showRes.output.join('\n')).toContain('Package: nano');
1358
- expect(showRes.output.join('\n')).toContain('Version: 8.0.0');
1358
+ expect(showRes.output.join('\n')).toContain('Version: 8.0.1');
1359
1359
  expect(showRes.output.join('\n')).toContain('Aliases: nano-wasm');
1360
1360
 
1361
1361
  const showAliasRes = await run(shell, 'wbt show nano-wasm');