brsh 2.1.3 → 2.1.4

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/index.js CHANGED
@@ -42,8 +42,9 @@ class Shell extends EventEmitter {
42
42
  }
43
43
  this.context.setCwd(cwd);
44
44
 
45
+ const pathDirExisted = useRealFilesystem || this.context.fs.exists(this.context.getVar('PATH'));
45
46
  this._loadDefaultCommands();
46
- this._loadBuiltinCommands();
47
+ this._loadBuiltinCommands(pathDirExisted);
47
48
 
48
49
  this._lastCode = 0;
49
50
 
@@ -84,10 +85,15 @@ class Shell extends EventEmitter {
84
85
  });
85
86
  }
86
87
 
87
- _loadBuiltinCommands() {
88
+ _loadBuiltinCommands(pathDirExisted = false) {
88
89
  builtins.forEach(command => {
89
90
  this.context.setCommand(command);
90
91
  });
92
+ // Lock the bin directory only if we implicitly created it.
93
+ // If the caller supplied their own bin dir, respect their permissions.
94
+ if (!pathDirExisted) {
95
+ this.context.fs.setMode(this.context.getVar('PATH'), 0o555);
96
+ }
91
97
  }
92
98
 
93
99
  _loadProfile(profile) {
@@ -346,6 +352,23 @@ class Shell extends EventEmitter {
346
352
  return WasmCommand.bind(null, command);
347
353
  }
348
354
 
355
+ // Executable .js files on the real filesystem: load via require() as a Command class.
356
+ if (this.context.fs.isRealFilesystem && typeof handle === 'string' && command.endsWith('.js')) {
357
+ if (!this.context.fs.isExecutable(command)) {
358
+ throw new Error(`${command}: permission denied`);
359
+ }
360
+ const abs = this.context.fs.absolutePath(command);
361
+ try {
362
+ const mod = require(abs);
363
+ const Cls = mod.default || mod;
364
+ if (typeof Cls === 'function' && typeof Cls.prototype.run === 'function' && typeof Cls.prototype.runCommand === 'function') {
365
+ return Cls;
366
+ }
367
+ } catch(e) {
368
+ // Not a loadable command module — fall through
369
+ }
370
+ }
371
+
349
372
  throw new Error(`${command}: permission denied`);
350
373
  }
351
374
  const cmd = this.context.getCommand(command);
package/lib/FileSystem.js CHANGED
@@ -131,6 +131,14 @@ class FileSystem extends BaseFileSystem {
131
131
 
132
132
  deleteFileByPath(path) {
133
133
  const absolute = this._pathToAbsolute(path);
134
+ // Deleting requires write permission on the parent directory.
135
+ const parentParts = absolute.split('/');
136
+ parentParts.pop();
137
+ const parentAbs = parentParts.join('/') || '/';
138
+ const parentMode = this.getMode(parentAbs);
139
+ if (parentMode !== null && !(parentMode & 0o200)) {
140
+ throw new Error(`${path}: Permission denied`);
141
+ }
134
142
  this._deleteByAbsolutePath(absolute);
135
143
  // Remove any explicit permissions for this path and children
136
144
  const prefix = absolute + '/';
@@ -168,7 +176,7 @@ class FileSystem extends BaseFileSystem {
168
176
  const file = this._getFileByAbsolutePath(absolute);
169
177
  if (file === null || file === undefined) return null;
170
178
  if (file.constructor === Object) return 0o755;
171
- if (typeof file === 'function') return 0o755;
179
+ if (typeof file === 'function') return 0o555;
172
180
  return 0o644;
173
181
  }
174
182
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
@@ -130,4 +130,22 @@ describe('useRealFilesystem', () => {
130
130
  const { output } = await run(shell, scriptPath);
131
131
  expect(output).toContain('Hello World');
132
132
  });
133
+
134
+ it('executable .js files are loaded as command classes', async () => {
135
+ const commandPath = path.resolve(__dirname, '../lib/Command.js');
136
+ const jsContent = `'use strict';
137
+ const Command = require(${JSON.stringify(commandPath)});
138
+ class Hello extends Command {
139
+ static get name() { return 'hello'; }
140
+ run() { return Promise.resolve('hello from js bin'); }
141
+ }
142
+ module.exports = Hello;
143
+ `;
144
+ const binPath = path.join(tmpDir, 'hello.js');
145
+ fs.writeFileSync(binPath, jsContent);
146
+ fs.chmodSync(binPath, 0o755);
147
+ const { output, exitCode } = await run(shell, binPath);
148
+ expect(exitCode).toBe(0);
149
+ expect(output).toContain('hello from js bin');
150
+ });
133
151
  });