brsh 2.1.0 → 2.1.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.
- package/.github/workflows/pages.yml +5 -2
- package/.nvmrc +1 -0
- package/CHANGELOG.md +14 -0
- package/bin/brsh-shell.js +65 -0
- package/dist/brsh.css +1 -1
- package/dist/shell.es.js +585 -495
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +6 -6
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.es.js +598 -499
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +8 -8
- package/dist/terminal.min.js.map +1 -1
- package/index.js +9 -4
- package/lib/Command.js +3 -1
- package/lib/Context.js +1 -0
- package/lib/NodeFileSystem.js +5 -1
- package/lib/Terminal.js +29 -2
- package/lib/WasmCommand.js +14 -7
- package/lib/buildFilesystemStructure.js +9 -4
- package/lib/local/curl.js +133 -0
- package/lib/local/index.js +2 -1
- package/lib/local/source.js +1 -3
- package/package.json +6 -3
- package/styles/terminal.css +8 -1
- package/test/exportFileSystem.test.js +28 -8
- package/test/scripts.test.js +57 -0
- package/vite.config.js +34 -0
package/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const EventEmitter = require('events');
|
|
4
|
-
|
|
5
3
|
const Context = require('./lib/Context');
|
|
6
4
|
const LocalCommand = require('./lib/local/LocalCommand');
|
|
7
5
|
const ScriptCommand = require('./lib/ScriptCommand');
|
|
@@ -10,9 +8,12 @@ const Command = require('./lib/Command');
|
|
|
10
8
|
|
|
11
9
|
const builtins = require('./lib/builtins');
|
|
12
10
|
const localCommands = require('./lib/local');
|
|
11
|
+
const events = require('events');
|
|
13
12
|
|
|
14
13
|
const variableRegex = /\${?([\w\d]+)}?/g;
|
|
15
14
|
|
|
15
|
+
|
|
16
|
+
const EventEmitter = events.EventEmitter || events.default || events;
|
|
16
17
|
//shims etc
|
|
17
18
|
String.prototype.regexIndexOf = function(regex, startpos) {
|
|
18
19
|
const indexOf = this.substring(startpos || 0).search(regex);
|
|
@@ -166,10 +167,11 @@ class Shell extends EventEmitter {
|
|
|
166
167
|
return;
|
|
167
168
|
}
|
|
168
169
|
|
|
170
|
+
const isCommandClass = (C) => typeof C === 'function' && typeof C.prototype.run === 'function' && typeof C.prototype.runCommand === 'function';
|
|
169
171
|
let instance;
|
|
170
172
|
if(Cmd.prototype instanceof LocalCommand || new Cmd() instanceof ScriptCommand) {
|
|
171
173
|
instance = new Cmd(args, this.context);
|
|
172
|
-
} else if(Cmd
|
|
174
|
+
} else if(isCommandClass(Cmd)) {
|
|
173
175
|
instance = new Cmd(args);
|
|
174
176
|
} else {
|
|
175
177
|
this.emit('stdErr', `${bin}: Command not found`);
|
|
@@ -274,7 +276,10 @@ class Shell extends EventEmitter {
|
|
|
274
276
|
throw new Error(`${command}: is a directory`);
|
|
275
277
|
}
|
|
276
278
|
|
|
277
|
-
if(handle.prototype && handle.prototype
|
|
279
|
+
if(handle.prototype && typeof handle.prototype.run === 'function' && typeof handle.prototype.runCommand === 'function') {
|
|
280
|
+
if (!this.context.fs.isExecutable(command)) {
|
|
281
|
+
throw new Error(`${command}: permission denied`);
|
|
282
|
+
}
|
|
278
283
|
return handle;
|
|
279
284
|
}
|
|
280
285
|
|
package/lib/Command.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const events = require('events');
|
|
4
4
|
const Result = require('./Result');
|
|
5
5
|
|
|
6
|
+
const EventEmitter = events.EventEmitter || events.default || events;
|
|
7
|
+
|
|
6
8
|
class Command extends EventEmitter {
|
|
7
9
|
constructor(args) {
|
|
8
10
|
super();
|
package/lib/Context.js
CHANGED
package/lib/NodeFileSystem.js
CHANGED
|
@@ -126,7 +126,11 @@ class NodeFileSystem {
|
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
setMode(path, mode) {
|
|
129
|
-
|
|
129
|
+
const abs = this.absolutePath(path);
|
|
130
|
+
if (this._virtualFiles[abs] !== undefined) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
nodeFs.chmodSync(abs, mode);
|
|
130
134
|
this._triggerChange();
|
|
131
135
|
}
|
|
132
136
|
|
package/lib/Terminal.js
CHANGED
|
@@ -14,7 +14,8 @@ class Terminal {
|
|
|
14
14
|
outputAnimation = 'none',
|
|
15
15
|
animateSpeed = 1,
|
|
16
16
|
onExit = null,
|
|
17
|
-
mobileInput = 'click'
|
|
17
|
+
mobileInput = 'click',
|
|
18
|
+
lineWrap = false
|
|
18
19
|
} = options;
|
|
19
20
|
|
|
20
21
|
if(!el) {
|
|
@@ -37,6 +38,7 @@ class Terminal {
|
|
|
37
38
|
this._cursor = cursor;
|
|
38
39
|
this._outputAnimation = outputAnimation;
|
|
39
40
|
this._animateSpeed = animateSpeed;
|
|
41
|
+
this._lineWrap = lineWrap;
|
|
40
42
|
this._working = false;
|
|
41
43
|
this.cli = new Shell(options);
|
|
42
44
|
this._buffer = [];
|
|
@@ -46,7 +48,7 @@ class Terminal {
|
|
|
46
48
|
this._savedInput = null;
|
|
47
49
|
this._historyLoaded = false;
|
|
48
50
|
this._el = el;
|
|
49
|
-
this._ansi = new AnsiUp
|
|
51
|
+
this._ansi = new AnsiUp();
|
|
50
52
|
|
|
51
53
|
this._ansi.use_classes = true;
|
|
52
54
|
this._ansi.url_whitelist = {
|
|
@@ -134,6 +136,9 @@ class Terminal {
|
|
|
134
136
|
this._cmdList.style.fontFamily = `'${font}', monospace`;
|
|
135
137
|
this._cmdList.style.listStyle = 'none';
|
|
136
138
|
this._cmdList.style.padding = '0';
|
|
139
|
+
if (this._lineWrap) {
|
|
140
|
+
this._cmdList.classList.add('terminal--wrap');
|
|
141
|
+
}
|
|
137
142
|
parent.appendChild(this._cmdList);
|
|
138
143
|
}
|
|
139
144
|
|
|
@@ -373,6 +378,28 @@ class Terminal {
|
|
|
373
378
|
});
|
|
374
379
|
|
|
375
380
|
el.addEventListener('keyup', (e) => this._keyEvent(onUpdate, e.key));
|
|
381
|
+
|
|
382
|
+
window.addEventListener('paste', (e) => {
|
|
383
|
+
e.preventDefault();
|
|
384
|
+
const text = (e.clipboardData || window.clipboardData).getData('text');
|
|
385
|
+
if (!text) return;
|
|
386
|
+
|
|
387
|
+
// Split on newlines; each newline acts as Enter (submits a command)
|
|
388
|
+
const segments = text.split(/\r?\n/);
|
|
389
|
+
segments.forEach((segment, idx) => {
|
|
390
|
+
// Bulk-insert characters for this segment
|
|
391
|
+
const chars = segment.split('');
|
|
392
|
+
this._buffer.splice(this._cursorPos, 0, ...chars);
|
|
393
|
+
this._cursorPos += chars.length;
|
|
394
|
+
|
|
395
|
+
// Newline between segments → submit as Enter
|
|
396
|
+
if (idx < segments.length - 1) {
|
|
397
|
+
this._keyEvent(onUpdate, 'Enter');
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
onUpdate(this._buffer);
|
|
402
|
+
});
|
|
376
403
|
}
|
|
377
404
|
|
|
378
405
|
_wordLeft() {
|
package/lib/WasmCommand.js
CHANGED
|
@@ -56,22 +56,29 @@ class WasmCommand extends ScriptCommand {
|
|
|
56
56
|
async _runWat(bytes) {
|
|
57
57
|
const text = new TextDecoder().decode(bytes);
|
|
58
58
|
let wasmBytes;
|
|
59
|
+
|
|
60
|
+
let wabtModule;
|
|
61
|
+
try {
|
|
62
|
+
const wabtPkg = 'wabt'; // variable prevents Vite static import analysis
|
|
63
|
+
wabtModule = await import(/* @vite-ignore */wabtPkg);
|
|
64
|
+
} catch(e) {
|
|
65
|
+
this.stdErr = `${this.script}: WAT execution requires the 'wabt' package\nInstall it with: npm install wabt`;
|
|
66
|
+
this.exitCode = 1;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
59
70
|
try {
|
|
60
|
-
const wabtInit =
|
|
71
|
+
const wabtInit = wabtModule.default ?? wabtModule;
|
|
61
72
|
const wabt = typeof wabtInit === 'function' ? await wabtInit() : wabtInit;
|
|
62
73
|
const parsed = wabt.parseWat(this.script, text, { mutable_globals: true });
|
|
63
74
|
const { buffer } = parsed.toBinary({});
|
|
64
75
|
wasmBytes = new Uint8Array(buffer);
|
|
65
76
|
} catch(e) {
|
|
66
|
-
|
|
67
|
-
this.stdErr = `${this.script}: WAT execution requires the 'wabt' package`;
|
|
68
|
-
this.stdErr = `Install it with: npm install wabt`;
|
|
69
|
-
} else {
|
|
70
|
-
this.stdErr = `${this.script}: WAT compilation error: ${e.message}`;
|
|
71
|
-
}
|
|
77
|
+
this.stdErr = `${this.script}: WAT compilation error: ${e.message}`;
|
|
72
78
|
this.exitCode = 1;
|
|
73
79
|
return;
|
|
74
80
|
}
|
|
81
|
+
|
|
75
82
|
return this._runWasm(wasmBytes);
|
|
76
83
|
}
|
|
77
84
|
|
|
@@ -37,14 +37,19 @@ function _buildTree(root, virtualPrefix, permissions) {
|
|
|
37
37
|
return structure;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
const filePath = path.join(root, handle);
|
|
41
|
+
const isJavaScript = handle.endsWith('.js');
|
|
42
|
+
const fileContents = isJavaScript
|
|
43
|
+
? null
|
|
44
|
+
: fs.readFileSync(filePath, 'utf8');
|
|
42
45
|
|
|
43
|
-
const key =
|
|
46
|
+
const key = isJavaScript
|
|
44
47
|
? handle.substring(0, handle.length - 3)
|
|
45
48
|
: handle;
|
|
46
49
|
|
|
47
|
-
structure[key] =
|
|
50
|
+
structure[key] = isJavaScript
|
|
51
|
+
? `require('${path.resolve(root, handle)}')`
|
|
52
|
+
: fileContents;
|
|
48
53
|
|
|
49
54
|
// Record real Unix permission bits for this file
|
|
50
55
|
permissions[virtualPrefix + '/' + key] = stat.mode & 0o777;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LocalCommand = require('./LocalCommand');
|
|
4
|
+
|
|
5
|
+
class Curl extends LocalCommand {
|
|
6
|
+
get name() { return 'curl'; }
|
|
7
|
+
get requiresFilesystem() { return true; }
|
|
8
|
+
|
|
9
|
+
run() {
|
|
10
|
+
const args = [...this.arguments];
|
|
11
|
+
let method = null;
|
|
12
|
+
const headers = {};
|
|
13
|
+
let body = null;
|
|
14
|
+
let includeHeaders = false;
|
|
15
|
+
let silent = false;
|
|
16
|
+
let outputFile = null;
|
|
17
|
+
let url = null;
|
|
18
|
+
|
|
19
|
+
let i = 0;
|
|
20
|
+
while (i < args.length) {
|
|
21
|
+
const arg = args[i];
|
|
22
|
+
switch (arg) {
|
|
23
|
+
case '-X': case '--request':
|
|
24
|
+
method = args[++i];
|
|
25
|
+
break;
|
|
26
|
+
case '-H': case '--header': {
|
|
27
|
+
const raw = args[++i] || '';
|
|
28
|
+
const colon = raw.indexOf(':');
|
|
29
|
+
if (colon > 0) {
|
|
30
|
+
headers[raw.slice(0, colon).trim()] = raw.slice(colon + 1).trim();
|
|
31
|
+
}
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
case '-d': case '--data': case '--data-raw':
|
|
35
|
+
body = args[++i];
|
|
36
|
+
if (!method) method = 'POST';
|
|
37
|
+
break;
|
|
38
|
+
case '-u': case '--user': {
|
|
39
|
+
const creds = args[++i] || '';
|
|
40
|
+
headers['Authorization'] = 'Basic ' + btoa(creds);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
case '-o': case '--output':
|
|
44
|
+
outputFile = args[++i];
|
|
45
|
+
break;
|
|
46
|
+
case '-i': case '--include':
|
|
47
|
+
includeHeaders = true;
|
|
48
|
+
break;
|
|
49
|
+
case '-s': case '--silent':
|
|
50
|
+
silent = true;
|
|
51
|
+
break;
|
|
52
|
+
case '-L': case '--location':
|
|
53
|
+
// fetch follows redirects by default — accepted and ignored
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
if (!arg.startsWith('-')) {
|
|
57
|
+
url = arg;
|
|
58
|
+
} else if (!silent) {
|
|
59
|
+
this.stdErr = `curl: unknown option '${arg}'`;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
i++;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!url) {
|
|
66
|
+
this.stdErr = 'curl: no URL specified';
|
|
67
|
+
this.exitCode = 1;
|
|
68
|
+
return Promise.resolve();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Validate URL scheme to prevent file:// or data: abuse
|
|
72
|
+
if (!/^https?:\/\//i.test(url)) {
|
|
73
|
+
this.stdErr = `curl: unsupported protocol in URL: ${url}`;
|
|
74
|
+
this.exitCode = 1;
|
|
75
|
+
return Promise.resolve();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const fetchOptions = {
|
|
79
|
+
method: method || 'GET',
|
|
80
|
+
headers,
|
|
81
|
+
};
|
|
82
|
+
if (body !== null) fetchOptions.body = body;
|
|
83
|
+
|
|
84
|
+
return fetch(url, fetchOptions)
|
|
85
|
+
.then(async (res) => {
|
|
86
|
+
if (includeHeaders) {
|
|
87
|
+
this.stdOut = `HTTP/1.1 ${res.status} ${res.statusText}`;
|
|
88
|
+
res.headers.forEach((value, name) => {
|
|
89
|
+
this.stdOut = `${name}: ${value}`;
|
|
90
|
+
});
|
|
91
|
+
this.stdOut = '';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const text = await res.text();
|
|
95
|
+
|
|
96
|
+
if (outputFile) {
|
|
97
|
+
this.fs.setFileByPath(outputFile, text, false);
|
|
98
|
+
} else {
|
|
99
|
+
text.split(/\r?\n/).forEach(line => { this.stdOut = line; });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!res.ok) {
|
|
103
|
+
this.exitCode = 22;
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
.catch((err) => {
|
|
107
|
+
if (!silent) {
|
|
108
|
+
const msg = (err && err.message) ? err.message : String(err);
|
|
109
|
+
// Browsers surface CORS and mixed-content failures as opaque TypeErrors
|
|
110
|
+
// with no status — the actual reason is intentionally hidden by the browser.
|
|
111
|
+
const likelyNetworkBlock =
|
|
112
|
+
err instanceof TypeError &&
|
|
113
|
+
/failed to fetch|networkerror|load failed|fetch is aborted/i.test(msg);
|
|
114
|
+
|
|
115
|
+
if (likelyNetworkBlock) {
|
|
116
|
+
const isHttps = /^https:\/\//i.test(url);
|
|
117
|
+
const pageSsl = typeof location !== 'undefined' && location.protocol === 'https:';
|
|
118
|
+
if (!isHttps && pageSsl) {
|
|
119
|
+
this.stdErr = `curl: (35) Mixed content blocked — the page is served over HTTPS but the target URL is HTTP. Browsers block insecure requests from secure pages.`;
|
|
120
|
+
} else {
|
|
121
|
+
this.stdErr = `curl: (6) Network request failed — this is likely a CORS error. The server at ${url} did not include the required 'Access-Control-Allow-Origin' response header. You can only fetch cross-origin resources that explicitly allow it.`;
|
|
122
|
+
this.stdErr = ` Hint: try a public CORS proxy, or ensure the server sends 'Access-Control-Allow-Origin: *'.`;
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
this.stdErr = `curl: (6) Could not connect: ${msg}`;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
this.exitCode = 6;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
module.exports = Curl;
|
package/lib/local/index.js
CHANGED
package/lib/local/source.js
CHANGED
|
@@ -3,8 +3,6 @@
|
|
|
3
3
|
const ScriptCommand = require('../ScriptCommand');
|
|
4
4
|
const LocalCommand = require('./LocalCommand');
|
|
5
5
|
|
|
6
|
-
const Shell = require('../../index');
|
|
7
|
-
|
|
8
6
|
class Source extends LocalCommand {
|
|
9
7
|
constructor(args, context) {
|
|
10
8
|
super(args, context);
|
|
@@ -37,7 +35,7 @@ class Source extends LocalCommand {
|
|
|
37
35
|
return Promise.reject(`${script}: is a directory`);
|
|
38
36
|
}
|
|
39
37
|
|
|
40
|
-
if (handle.prototype && handle.prototype
|
|
38
|
+
if (handle.prototype && typeof handle.prototype.run === 'function' && typeof handle.prototype.runCommand === 'function') {
|
|
41
39
|
return Promise.reject(`${script}: command not found`);
|
|
42
40
|
}
|
|
43
41
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brsh",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.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",
|
|
@@ -20,10 +20,12 @@
|
|
|
20
20
|
"exportFileSystem": "bin/exportFileSystem.js"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
|
-
"start": "vite demo/",
|
|
23
|
+
"start": "vite --config vite.config.js demo/",
|
|
24
|
+
"build:demofs": "node bin/exportFileSystem.js ./demo/filesystem -o ./demo/fs.js -v fs",
|
|
24
25
|
"build": "npm run build:shell && npm run build:terminal",
|
|
25
26
|
"build:shell": "vite build --config vite.config.shell.js",
|
|
26
27
|
"build:terminal": "vite build --config vite.config.terminal.js",
|
|
28
|
+
"shell": "node bin/brsh-shell.js",
|
|
27
29
|
"test": "vitest run",
|
|
28
30
|
"test:watch": "vitest",
|
|
29
31
|
"prepublishOnly": "npm run build"
|
|
@@ -35,10 +37,10 @@
|
|
|
35
37
|
"author": "Eliot Stocker",
|
|
36
38
|
"license": "GPL-3.0",
|
|
37
39
|
"devDependencies": {
|
|
40
|
+
"@originjs/vite-plugin-commonjs": "^1.0.3",
|
|
38
41
|
"@semantic-release/changelog": "^6.0.3",
|
|
39
42
|
"@semantic-release/git": "^10.0.1",
|
|
40
43
|
"@vitest/coverage-v8": "^4.1.5",
|
|
41
|
-
"esbuild": "^0.25.9",
|
|
42
44
|
"semantic-release": "^25.0.3",
|
|
43
45
|
"vite": "^8.0.10",
|
|
44
46
|
"vite-plugin-node-polyfills": "^0.26.0",
|
|
@@ -46,6 +48,7 @@
|
|
|
46
48
|
},
|
|
47
49
|
"dependencies": {
|
|
48
50
|
"ansi_up": "git+https://github.com/eliotstocker/ansi_up.git",
|
|
51
|
+
"ansi-colors": "^4.1.3",
|
|
49
52
|
"command-line-args": "^6.0.1",
|
|
50
53
|
"esbuild-plugin-polyfill-node": "^0.3.0",
|
|
51
54
|
"string.prototype.matchall": "^4.0.12",
|
package/styles/terminal.css
CHANGED
|
@@ -7,11 +7,18 @@
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
.terminal__line {
|
|
10
|
-
white-space: pre
|
|
10
|
+
white-space: pre;
|
|
11
|
+
overflow: hidden;
|
|
11
12
|
min-height: 1.15em;
|
|
12
13
|
line-height: 1.15em;
|
|
13
14
|
}
|
|
14
15
|
|
|
16
|
+
.terminal--wrap .terminal__line,
|
|
17
|
+
.terminal--wrap li {
|
|
18
|
+
white-space: pre-wrap;
|
|
19
|
+
overflow: visible;
|
|
20
|
+
}
|
|
21
|
+
|
|
15
22
|
@keyframes blink {
|
|
16
23
|
from, to {
|
|
17
24
|
opacity: 0;
|
|
@@ -54,8 +54,7 @@ describe('buildStructure', () => {
|
|
|
54
54
|
writeFileSync(join(root, 'readme.txt'), 'hello');
|
|
55
55
|
const { filesystem } = buildStructure(root);
|
|
56
56
|
expect(filesystem).toHaveProperty('readme.txt');
|
|
57
|
-
expect(filesystem['readme.txt']).
|
|
58
|
-
expect(filesystem['readme.txt']).toContain('readme.txt');
|
|
57
|
+
expect(filesystem['readme.txt']).toBe('hello');
|
|
59
58
|
});
|
|
60
59
|
|
|
61
60
|
it('strips the .js extension from JavaScript file keys', () => {
|
|
@@ -66,11 +65,11 @@ describe('buildStructure', () => {
|
|
|
66
65
|
expect(filesystem['app']).toMatch(/^require\('/);
|
|
67
66
|
});
|
|
68
67
|
|
|
69
|
-
it('uses an absolute path in the require() string', () => {
|
|
70
|
-
writeFileSync(join(root, 'data.
|
|
68
|
+
it('uses an absolute path in the require() string for JavaScript files', () => {
|
|
69
|
+
writeFileSync(join(root, 'data.js'), 'module.exports = "content"');
|
|
71
70
|
const { filesystem } = buildStructure(root);
|
|
72
|
-
const reqPath = filesystem
|
|
73
|
-
expect(reqPath).toBe(resolve(root, 'data.
|
|
71
|
+
const reqPath = filesystem.data.match(/require\('(.+?)'\)/)[1];
|
|
72
|
+
expect(reqPath).toBe(resolve(root, 'data.js'));
|
|
74
73
|
});
|
|
75
74
|
|
|
76
75
|
it('recursively includes nested directories', () => {
|
|
@@ -87,9 +86,9 @@ describe('buildStructure', () => {
|
|
|
87
86
|
const b = join(a, 'b');
|
|
88
87
|
mkdirSync(a);
|
|
89
88
|
mkdirSync(b);
|
|
90
|
-
writeFileSync(join(b, 'deep.
|
|
89
|
+
writeFileSync(join(b, 'deep.js'), 'module.exports = "deep"');
|
|
91
90
|
const { filesystem } = buildStructure(root);
|
|
92
|
-
expect(filesystem.a.b['deep
|
|
91
|
+
expect(filesystem.a.b['deep']).toMatch(/^require\('/);
|
|
93
92
|
});
|
|
94
93
|
|
|
95
94
|
it('ignores .DS_Store files', () => {
|
|
@@ -204,6 +203,27 @@ describe('exportFileSystem CLI', () => {
|
|
|
204
203
|
expect(content).toContain('Hello World');
|
|
205
204
|
});
|
|
206
205
|
|
|
206
|
+
it('builds a directory tree without requiring a root index.js wrapper', () => {
|
|
207
|
+
rmSync(join(root, 'app.js'));
|
|
208
|
+
mkdirSync(join(root, 'bin'));
|
|
209
|
+
writeFileSync(join(root, 'bin', 'tool.js'), 'module.exports = function tool() {}');
|
|
210
|
+
|
|
211
|
+
const outFile = join(outDir, 'fs.js');
|
|
212
|
+
const result = runCLI([root, '-v', 'FS', '-o', outFile]);
|
|
213
|
+
const content = readFileSync(outFile, 'utf8');
|
|
214
|
+
|
|
215
|
+
expect(result.status).toBe(0);
|
|
216
|
+
expect(content).toContain('Hello World');
|
|
217
|
+
expect(content).toContain('tool');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('bundle does not contain readFileSync references', () => {
|
|
221
|
+
const outFile = join(outDir, 'fs.js');
|
|
222
|
+
runCLI([root, '-v', 'FS', '-o', outFile]);
|
|
223
|
+
const content = readFileSync(outFile, 'utf8');
|
|
224
|
+
expect(content).not.toContain('readFileSync');
|
|
225
|
+
});
|
|
226
|
+
|
|
207
227
|
it('handles a nested directory structure', () => {
|
|
208
228
|
const sub = join(root, 'sub');
|
|
209
229
|
mkdirSync(sub);
|
package/test/scripts.test.js
CHANGED
|
@@ -132,6 +132,63 @@ describe('shell script execution', () => {
|
|
|
132
132
|
});
|
|
133
133
|
});
|
|
134
134
|
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
// Permission checks
|
|
137
|
+
// ---------------------------------------------------------------------------
|
|
138
|
+
|
|
139
|
+
describe('execute permission enforcement', () => {
|
|
140
|
+
const SCRIPT = '#!/sh.js\necho ran';
|
|
141
|
+
|
|
142
|
+
it('refuses to run a script without execute permission', async () => {
|
|
143
|
+
const shell = await createShell({
|
|
144
|
+
filesystem: { scripts: { 'nox.sh': SCRIPT } },
|
|
145
|
+
permissions: { '/scripts/nox.sh': 0o644 }
|
|
146
|
+
});
|
|
147
|
+
const { errors, exitCode } = await run(shell, '/scripts/nox.sh');
|
|
148
|
+
expect(errors.length).toBeGreaterThan(0);
|
|
149
|
+
expect(exitCode).toBeGreaterThan(0);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('runs a script that has execute permission', async () => {
|
|
153
|
+
const shell = await createShell({
|
|
154
|
+
filesystem: { scripts: { 'withx.sh': SCRIPT } },
|
|
155
|
+
permissions: { '/scripts/withx.sh': 0o755 }
|
|
156
|
+
});
|
|
157
|
+
const { output, exitCode } = await run(shell, '/scripts/withx.sh');
|
|
158
|
+
expect(output).toContain('ran');
|
|
159
|
+
expect(exitCode).toBe(0);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('refuses to run a script when only owner-execute bit is cleared (0o644)', async () => {
|
|
163
|
+
const shell = await createShell({
|
|
164
|
+
filesystem: { scripts: { 'nox2.sh': SCRIPT } },
|
|
165
|
+
permissions: { '/scripts/nox2.sh': 0o644 }
|
|
166
|
+
});
|
|
167
|
+
const { errors, exitCode } = await run(shell, '/scripts/nox2.sh');
|
|
168
|
+
expect(errors.length).toBeGreaterThan(0);
|
|
169
|
+
expect(exitCode).toBeGreaterThan(0);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('runs a Command class that has no explicit permission entry (defaults to executable)', async () => {
|
|
173
|
+
const shell = await createShell({
|
|
174
|
+
filesystem: { apps: { hello: HelloApp } }
|
|
175
|
+
});
|
|
176
|
+
const { output, exitCode } = await run(shell, '/apps/hello');
|
|
177
|
+
expect(output).toContain('Hello from JS app');
|
|
178
|
+
expect(exitCode).toBe(0);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('refuses to run a Command class with explicit non-executable permission', async () => {
|
|
182
|
+
const shell = await createShell({
|
|
183
|
+
filesystem: { apps: { hello: HelloApp } },
|
|
184
|
+
permissions: { '/apps/hello': 0o644 }
|
|
185
|
+
});
|
|
186
|
+
const { errors, exitCode } = await run(shell, '/apps/hello');
|
|
187
|
+
expect(errors.length).toBeGreaterThan(0);
|
|
188
|
+
expect(exitCode).toBeGreaterThan(0);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
135
192
|
// ---------------------------------------------------------------------------
|
|
136
193
|
// Conditional execution via &&
|
|
137
194
|
// ---------------------------------------------------------------------------
|
package/vite.config.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
import { nodePolyfills } from 'vite-plugin-node-polyfills';
|
|
3
|
+
import { viteCommonjs } from '@originjs/vite-plugin-commonjs';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
const projectRoot = import.meta.dirname;
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
plugins: [
|
|
10
|
+
nodePolyfills(),
|
|
11
|
+
viteCommonjs()
|
|
12
|
+
],
|
|
13
|
+
resolve: {
|
|
14
|
+
alias: [
|
|
15
|
+
{
|
|
16
|
+
find: /^brsh\/terminal$/,
|
|
17
|
+
replacement: path.resolve(projectRoot, 'lib/Terminal.js')
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
find: /^brsh$/,
|
|
21
|
+
replacement: path.resolve(projectRoot, 'index.js')
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
find: /^Shell$/,
|
|
25
|
+
replacement: path.resolve(projectRoot, 'index.js')
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
server: {
|
|
30
|
+
fs: {
|
|
31
|
+
allow: [projectRoot]
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
});
|