brsh 3.6.1 → 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
  }
@@ -309,19 +309,55 @@ class WasmCommand extends ScriptCommand {
309
309
  // fd 3: pre-opened root directory '/'
310
310
  // fd 4: pre-opened current working directory '.'
311
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;
312
318
  let nextFd = 5;
313
319
  const fdTable = new Map([
314
320
  [3, { type: 'preopen', path: '/', guestName: '/' }],
315
- [4, { type: 'preopen', path: (fs && fs.cwd) || '/', guestName: '.' }]
321
+ [4, { type: 'preopen', path: cwd, guestName: '.' }]
316
322
  ]);
317
323
 
324
+ const getParentPath = (p) => {
325
+ const parts = p.split('/').filter(Boolean);
326
+ parts.pop();
327
+ return '/' + parts.join('/');
328
+ };
329
+
318
330
  const resolveGuestPath = (dirFd, relPath) => {
319
331
  const dirEntry = fdTable.get(dirFd);
320
332
  if (!dirEntry || (dirEntry.type !== 'preopen' && dirEntry.type !== 'dir')) return null;
321
333
  if (relPath.startsWith('/')) return fs.absolutePath(relPath);
322
334
  if (!relPath || relPath === '.') return fs.absolutePath(dirEntry.path);
335
+
323
336
  const base = dirEntry.path.endsWith('/') ? dirEntry.path : dirEntry.path + '/';
324
- 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;
325
361
  };
326
362
 
327
363
  const flushOpenFile = (entry) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "3.6.1",
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",
@@ -591,6 +591,38 @@ describe('brsh Shell Integration, Line Editing, and Full-Screen Apps', () => {
591
591
 
592
592
  expect(term.screen.isAltScreen).toBe(false);
593
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
+ });
594
626
  });
595
627
 
596
628
  describe('TUI (blessed-style) Application over Virtual TTY & Real zsh PTY Compatibility', () => {