brsh 2.1.3 → 2.1.5
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/bin/exportFileSystem.js +5 -1
- package/dist/shell.es.js +461 -445
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +3 -3
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.es.js +465 -449
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +3 -3
- package/dist/terminal.min.js.map +1 -1
- package/index.js +25 -2
- package/lib/FileSystem.js +9 -1
- package/lib/buildFilesystemStructure.js +15 -1
- package/package.json +1 -1
- package/test/node-filesystem.test.js +18 -0
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
|
|
179
|
+
if (typeof file === 'function') return 0o555;
|
|
172
180
|
return 0o644;
|
|
173
181
|
}
|
|
174
182
|
|
|
@@ -5,6 +5,16 @@ const path = require('path');
|
|
|
5
5
|
|
|
6
6
|
const IGNORE = ['.DS_Store'];
|
|
7
7
|
|
|
8
|
+
const BINARY_MIME = {
|
|
9
|
+
'.gif': 'image/gif',
|
|
10
|
+
'.png': 'image/png',
|
|
11
|
+
'.jpg': 'image/jpeg',
|
|
12
|
+
'.jpeg': 'image/jpeg',
|
|
13
|
+
'.bmp': 'image/bmp',
|
|
14
|
+
'.webp': 'image/webp',
|
|
15
|
+
'.ico': 'image/x-icon',
|
|
16
|
+
};
|
|
17
|
+
|
|
8
18
|
/**
|
|
9
19
|
* Recursively builds a filesystem structure object from a real directory.
|
|
10
20
|
* Returns { filesystem, permissions } where:
|
|
@@ -39,9 +49,13 @@ function _buildTree(root, virtualPrefix, permissions) {
|
|
|
39
49
|
|
|
40
50
|
const filePath = path.join(root, handle);
|
|
41
51
|
const isJavaScript = handle.endsWith('.js');
|
|
52
|
+
const ext = path.extname(handle).toLowerCase();
|
|
53
|
+
const isBinary = ext in BINARY_MIME;
|
|
42
54
|
const fileContents = isJavaScript
|
|
43
55
|
? null
|
|
44
|
-
:
|
|
56
|
+
: isBinary
|
|
57
|
+
? `data:${BINARY_MIME[ext]};base64,${fs.readFileSync(filePath).toString('base64')}`
|
|
58
|
+
: fs.readFileSync(filePath, 'utf8');
|
|
45
59
|
|
|
46
60
|
const key = isJavaScript
|
|
47
61
|
? handle.substring(0, handle.length - 3)
|
package/package.json
CHANGED
|
@@ -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
|
});
|