brsh 2.5.6 → 2.7.0
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/Readme.md +130 -1
- package/bin/Readme.md +6 -3
- package/bin/brsh-shell.js +53 -8
- package/bin/exportFileSystem.js +192 -80
- package/dist/shell.es.js +2491 -1276
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +21 -13
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.es.js +2568 -1343
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +23 -15
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +9449 -0
- package/dist/wbt.es.js.map +1 -0
- package/dist/wbt.min.js +24 -0
- package/dist/wbt.min.js.map +1 -0
- package/index.js +273 -22
- package/lib/BinaryContainer.js +528 -0
- package/lib/Command.js +2 -1
- package/lib/FileSystem.js +31 -0
- package/lib/NodeFileSystem.js +19 -9
- package/lib/ReplCommand.js +520 -0
- package/lib/ScriptCommand.js +3 -2
- package/lib/Terminal.js +24 -4
- package/lib/WasmCommand.js +1123 -35
- package/lib/buildFilesystemStructure.js +15 -4
- package/lib/wbt/daemon.js +264 -0
- package/lib/wbt/index.js +779 -0
- package/lib/wbt/packages/pglite.js +590 -0
- package/lib/wbt/registry.js +190 -0
- package/lib/wbt/utils.js +118 -0
- package/package.json +9 -2
- package/test/binary-filesystem.test.js +248 -0
- package/test/exportFileSystem.test.js +78 -0
- package/test/shell.test.js +220 -0
- package/test/wbt.test.js +1084 -0
- package/vite.config.demo.js +4 -0
- package/vite.config.js +4 -0
- package/vite.config.wbt.js +21 -0
package/index.js
CHANGED
|
@@ -12,7 +12,10 @@ const localCommands = require('./lib/local');
|
|
|
12
12
|
const events = require('events');
|
|
13
13
|
const BaseFileSystem = require('./lib/BaseFileSystem');
|
|
14
14
|
|
|
15
|
-
const
|
|
15
|
+
const BinaryContainer = require('./lib/BinaryContainer');
|
|
16
|
+
const { isAsyncFilesystemSource, resolveFilesystemSource } = BinaryContainer;
|
|
17
|
+
|
|
18
|
+
const variableRegex = /\$(?:\{([\w\d?#-]+)\}|([\w\d]+|[?#-]))/g;
|
|
16
19
|
|
|
17
20
|
|
|
18
21
|
const EventEmitter = events.EventEmitter || events.default || events;
|
|
@@ -26,7 +29,20 @@ require('string.prototype.matchall').shim();
|
|
|
26
29
|
class Shell extends EventEmitter {
|
|
27
30
|
constructor(options) {
|
|
28
31
|
super();
|
|
29
|
-
const {
|
|
32
|
+
const {
|
|
33
|
+
path = '/bin',
|
|
34
|
+
profile,
|
|
35
|
+
motd,
|
|
36
|
+
command,
|
|
37
|
+
interactive = true,
|
|
38
|
+
subshell = false,
|
|
39
|
+
hostname = 'browser',
|
|
40
|
+
filesystem = {},
|
|
41
|
+
permissions = {},
|
|
42
|
+
cwd = '/',
|
|
43
|
+
onFsChange,
|
|
44
|
+
useRealFilesystem = false
|
|
45
|
+
} = options;
|
|
30
46
|
|
|
31
47
|
this.context = new Context(Object.assign({}, options, {
|
|
32
48
|
clearFn: this.clear.bind(this),
|
|
@@ -42,24 +58,119 @@ class Shell extends EventEmitter {
|
|
|
42
58
|
|
|
43
59
|
this.context.setVar('PATH', path);
|
|
44
60
|
this.context.setVar('HOST', hostname);
|
|
61
|
+
if (!subshell) {
|
|
62
|
+
const initialFlags = (interactive ? 'i' : '') + (command ? 'c' : '');
|
|
63
|
+
if (initialFlags) {
|
|
64
|
+
this.context.setVar('-', initialFlags);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this._lastCode = 0;
|
|
68
|
+
this._subshell = subshell;
|
|
69
|
+
this._useRealFilesystem = useRealFilesystem;
|
|
70
|
+
this._profile = profile;
|
|
71
|
+
this._motd = motd;
|
|
72
|
+
this._initialCommand = command || null;
|
|
73
|
+
this._suppressMotd = Boolean(command);
|
|
74
|
+
this._loadingFs = null;
|
|
75
|
+
this._queuedWhileLoading = false;
|
|
76
|
+
|
|
45
77
|
if (useRealFilesystem) {
|
|
46
78
|
this.context.setNodeFilesystem(cwd);
|
|
79
|
+
this.context.setCwd(cwd);
|
|
80
|
+
this._loadDefaultCommands();
|
|
81
|
+
this._loadBuiltinCommands(true);
|
|
82
|
+
this.ready = Promise.resolve(this);
|
|
83
|
+
if (!subshell) {
|
|
84
|
+
this._initializing = true;
|
|
85
|
+
setTimeout(() => this._init(), 0);
|
|
86
|
+
} else {
|
|
87
|
+
this._initializing = false;
|
|
88
|
+
}
|
|
89
|
+
} else if (isAsyncFilesystemSource(filesystem) || (filesystem && typeof filesystem === 'object' && isAsyncFilesystemSource(filesystem.filesystem))) {
|
|
90
|
+
// Initialize a temporary empty filesystem so synchronous calls like `shell.context.setCommand(Wbt)` work immediately
|
|
91
|
+
this.context.setFilesystem({}, permissions);
|
|
92
|
+
this._loadDefaultCommands();
|
|
93
|
+
this._loadBuiltinCommands(false);
|
|
94
|
+
this._initializing = !subshell;
|
|
95
|
+
this._loadingFs = this._initAsyncFilesystem(filesystem, permissions, cwd, profile);
|
|
96
|
+
this.ready = this._loadingFs;
|
|
47
97
|
} else {
|
|
48
98
|
this.context.setFilesystem(filesystem, permissions);
|
|
99
|
+
this.context.setCwd(cwd);
|
|
100
|
+
|
|
101
|
+
const pathDirExisted = this.context.fs.exists(this.context.getVar('PATH'));
|
|
102
|
+
this._loadDefaultCommands();
|
|
103
|
+
this._loadBuiltinCommands(pathDirExisted);
|
|
104
|
+
this.ready = Promise.resolve(this);
|
|
105
|
+
|
|
106
|
+
if (!subshell) {
|
|
107
|
+
this._initializing = true;
|
|
108
|
+
setTimeout(() => this._init(), 0);
|
|
109
|
+
} else {
|
|
110
|
+
this._initializing = false;
|
|
111
|
+
}
|
|
49
112
|
}
|
|
50
|
-
|
|
113
|
+
}
|
|
51
114
|
|
|
52
|
-
|
|
53
|
-
this.
|
|
54
|
-
|
|
115
|
+
async _initAsyncFilesystem(filesystemSource, permissions, cwd) {
|
|
116
|
+
const binPath = this.context.getVar('PATH') || '/bin';
|
|
117
|
+
const getBinSnapshot = () => {
|
|
118
|
+
try {
|
|
119
|
+
const raw = this.context.fs.getRaw();
|
|
120
|
+
const parts = binPath.replace(/^\/+|\/+$/g, '').split('/').filter(Boolean);
|
|
121
|
+
let dir = raw;
|
|
122
|
+
for (const p of parts) {
|
|
123
|
+
dir = dir && dir[p];
|
|
124
|
+
}
|
|
125
|
+
return dir && typeof dir === 'object' ? Object.assign({}, dir) : {};
|
|
126
|
+
} catch (_) {
|
|
127
|
+
return {};
|
|
128
|
+
}
|
|
129
|
+
};
|
|
55
130
|
|
|
56
|
-
|
|
131
|
+
const defaultBinSnapshot = getBinSnapshot();
|
|
132
|
+
try {
|
|
133
|
+
const resolved = await resolveFilesystemSource(filesystemSource, permissions);
|
|
134
|
+
const postBinSnapshot = getBinSnapshot();
|
|
135
|
+
|
|
136
|
+
// Preserve any custom commands registered via `shell.context.setCommand(...)` while the async load was in flight
|
|
137
|
+
const customCommands = [];
|
|
138
|
+
for (const [k, v] of Object.entries(postBinSnapshot)) {
|
|
139
|
+
if (defaultBinSnapshot[k] !== v && typeof v === 'function') {
|
|
140
|
+
customCommands.push(v);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
57
143
|
|
|
58
|
-
|
|
59
|
-
this.
|
|
144
|
+
// Temporarily suppress onFsChange during initial mount
|
|
145
|
+
const savedOnChange = this.context._onFsChange;
|
|
146
|
+
this.context._onFsChange = null;
|
|
147
|
+
this.context.setFilesystem(resolved.filesystem, resolved.permissions);
|
|
148
|
+
const pathDirExisted = this.context.fs.exists(binPath);
|
|
149
|
+
this._loadDefaultCommands();
|
|
150
|
+
this._loadBuiltinCommands(pathDirExisted);
|
|
151
|
+
for (const cmd of customCommands) {
|
|
152
|
+
this.context.setCommand(cmd);
|
|
153
|
+
}
|
|
154
|
+
this.context.setCwd(cwd);
|
|
155
|
+
this.context._onFsChange = savedOnChange;
|
|
156
|
+
if (savedOnChange && this.context.fs) {
|
|
157
|
+
this.context.fs._onChange = () => savedOnChange(this.context.fs.getRaw(), this.context.fs.getRawPermissions());
|
|
158
|
+
}
|
|
159
|
+
} catch (err) {
|
|
160
|
+
this.emit('stdErr', err.message || String(err));
|
|
161
|
+
} finally {
|
|
162
|
+
this._loadingFs = null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!this._subshell) {
|
|
166
|
+
await this._init();
|
|
60
167
|
} else {
|
|
61
|
-
|
|
168
|
+
this._initializing = false;
|
|
169
|
+
if (!this._queuedWhileLoading && !this.destroyed) {
|
|
170
|
+
this.emit('status', Shell.STATUS_READY);
|
|
171
|
+
}
|
|
62
172
|
}
|
|
173
|
+
return this;
|
|
63
174
|
}
|
|
64
175
|
|
|
65
176
|
get path() {
|
|
@@ -67,7 +178,7 @@ class Shell extends EventEmitter {
|
|
|
67
178
|
}
|
|
68
179
|
|
|
69
180
|
set lastCode(code) {
|
|
70
|
-
this.context.setVar('?', code);
|
|
181
|
+
this.context.setVar('?', String(code));
|
|
71
182
|
this._lastCode = code;
|
|
72
183
|
}
|
|
73
184
|
|
|
@@ -114,8 +225,117 @@ class Shell extends EventEmitter {
|
|
|
114
225
|
}
|
|
115
226
|
}
|
|
116
227
|
|
|
117
|
-
|
|
118
|
-
this.
|
|
228
|
+
suppressMotd() {
|
|
229
|
+
this._suppressMotd = true;
|
|
230
|
+
const flags = this.context.getVar('-');
|
|
231
|
+
if (!flags.includes('c')) {
|
|
232
|
+
this.context.setVar('-', flags + 'c');
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async _init() {
|
|
237
|
+
try {
|
|
238
|
+
if (!this._suppressMotd && !this._initialCommand) {
|
|
239
|
+
await this._runMotd();
|
|
240
|
+
}
|
|
241
|
+
await this._loadProfile(this._profile);
|
|
242
|
+
if (this._initialCommand) {
|
|
243
|
+
await this.onCommand(this._initialCommand, false);
|
|
244
|
+
const flags = this.context.getVar('-');
|
|
245
|
+
if (flags.includes('c')) {
|
|
246
|
+
this.context.setVar('-', flags.replace(/c/g, ''));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
} finally {
|
|
250
|
+
this._initializing = false;
|
|
251
|
+
if (!this._queuedWhileLoading && !this.destroyed) {
|
|
252
|
+
this.emit('status', Shell.STATUS_READY);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
_hasHushlogin() {
|
|
258
|
+
if (this.context.fs.exists('.hushlogin')) return true;
|
|
259
|
+
const home = this.context.getVar('HOME');
|
|
260
|
+
if (home && this.context.fs.exists(`${home.replace(/\/$/, '')}/.hushlogin`)) return true;
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async _runMotd() {
|
|
265
|
+
if (this._motd === false || this._hasHushlogin()) return;
|
|
266
|
+
if (typeof this._motd === 'string') {
|
|
267
|
+
await this._renderMotdPath(this._motd);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (!this._useRealFilesystem) {
|
|
271
|
+
if (this.context.fs.exists('/etc/update-motd.d')) {
|
|
272
|
+
await this._runMotdDir('/etc/update-motd.d');
|
|
273
|
+
}
|
|
274
|
+
if (this.context.fs.exists('/etc/motd')) {
|
|
275
|
+
await this._renderMotdPath('/etc/motd');
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async _runMotdDir(dirPath) {
|
|
281
|
+
const dir = this.context.fs.getFileByPath(dirPath);
|
|
282
|
+
if (!dir || dir.constructor !== Object) return;
|
|
283
|
+
const entries = Object.keys(dir).sort();
|
|
284
|
+
const cleanDir = dirPath.endsWith('/') ? dirPath.slice(0, -1) : dirPath;
|
|
285
|
+
for (const name of entries) {
|
|
286
|
+
const fullPath = `${cleanDir}/${name}`;
|
|
287
|
+
const stat = this.context.fs.stat(fullPath);
|
|
288
|
+
if (!stat || stat.isDirectory) continue;
|
|
289
|
+
if (this.context.fs.isExecutable(fullPath)) {
|
|
290
|
+
await this.onCommand(fullPath, false);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async _renderMotdPath(motdPath) {
|
|
296
|
+
if (!this.context.fs.exists(motdPath)) return;
|
|
297
|
+
const stat = this.context.fs.stat(motdPath);
|
|
298
|
+
if (!stat) return;
|
|
299
|
+
if (stat.isDirectory) {
|
|
300
|
+
await this._runMotdDir(motdPath);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
const content = this.context.fs.getFileByPath(motdPath);
|
|
304
|
+
if (content === null || content === undefined) return;
|
|
305
|
+
|
|
306
|
+
if (typeof content === 'function') {
|
|
307
|
+
if (this.context.fs.isExecutable(motdPath)) {
|
|
308
|
+
await this.onCommand(motdPath, false);
|
|
309
|
+
}
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (typeof content === 'string' && content.startsWith('#!')) {
|
|
314
|
+
if (this.context.fs.isExecutable(motdPath)) {
|
|
315
|
+
await this.onCommand(motdPath, false);
|
|
316
|
+
} else {
|
|
317
|
+
await this.onCommand(`source ${motdPath}`, false);
|
|
318
|
+
}
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
let text = content.toString();
|
|
323
|
+
if (text.endsWith('\n')) text = text.slice(0, -1);
|
|
324
|
+
text.split(/\r?\n/).forEach(line => {
|
|
325
|
+
this.emit('stdOut', line);
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async _loadProfile(profile) {
|
|
330
|
+
if (!this._useRealFilesystem && profile !== '/etc/profile' && this.context.fs.exists('/etc/profile')) {
|
|
331
|
+
const etcProfile = this.context.fs.getFileByPath('/etc/profile');
|
|
332
|
+
if (typeof etcProfile === 'string' && etcProfile.startsWith('#!')) {
|
|
333
|
+
await this.onCommand('source /etc/profile', false);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (profile) {
|
|
337
|
+
await this.onCommand(`source ${profile}`, false);
|
|
338
|
+
}
|
|
119
339
|
}
|
|
120
340
|
|
|
121
341
|
onCommand(raw, events = true) {
|
|
@@ -123,6 +343,11 @@ class Shell extends EventEmitter {
|
|
|
123
343
|
const groups = this._groupPipes(segments);
|
|
124
344
|
let shouldRun = true;
|
|
125
345
|
|
|
346
|
+
if (this._loadingFs && events) {
|
|
347
|
+
this._queuedWhileLoading = true;
|
|
348
|
+
}
|
|
349
|
+
const initialPromise = this._loadingFs ? Promise.resolve(this._loadingFs) : Promise.resolve();
|
|
350
|
+
|
|
126
351
|
const promise = groups.reduce((chain, { commands, op }) => {
|
|
127
352
|
return chain.then(() => {
|
|
128
353
|
if (!shouldRun) {
|
|
@@ -137,7 +362,7 @@ class Shell extends EventEmitter {
|
|
|
137
362
|
else if (op === ';') shouldRun = true;
|
|
138
363
|
});
|
|
139
364
|
});
|
|
140
|
-
},
|
|
365
|
+
}, initialPromise);
|
|
141
366
|
|
|
142
367
|
if(events) {
|
|
143
368
|
return promise.then(() => {
|
|
@@ -160,7 +385,9 @@ class Shell extends EventEmitter {
|
|
|
160
385
|
return;
|
|
161
386
|
}
|
|
162
387
|
|
|
163
|
-
this.
|
|
388
|
+
if (!this._initializing) {
|
|
389
|
+
this.emit('status', Shell.STATUS_WORKING);
|
|
390
|
+
}
|
|
164
391
|
|
|
165
392
|
let args;
|
|
166
393
|
try {
|
|
@@ -406,12 +633,26 @@ class Shell extends EventEmitter {
|
|
|
406
633
|
|
|
407
634
|
throw new Error(`${command}: permission denied`);
|
|
408
635
|
}
|
|
409
|
-
|
|
636
|
+
let cmd = this.context.getCommand(command);
|
|
637
|
+
let cmdName = command;
|
|
638
|
+
if (!cmd && !command.endsWith('.wasm')) {
|
|
639
|
+
cmd = this.context.getCommand(`${command}.wasm`);
|
|
640
|
+
if (cmd) cmdName = `${command}.wasm`;
|
|
641
|
+
}
|
|
410
642
|
|
|
411
643
|
if(!cmd) {
|
|
412
644
|
throw new Error(`${command}: Command not found`);
|
|
413
645
|
}
|
|
414
646
|
|
|
647
|
+
const binLocation = `${this.path}/${cmdName}`;
|
|
648
|
+
if (this._isWasmFile(binLocation, cmd)) {
|
|
649
|
+
return WasmCommand.bind(null, binLocation);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
if (typeof cmd === 'string' && cmd.startsWith('#!')) {
|
|
653
|
+
return ScriptCommand.bind(this, cmd);
|
|
654
|
+
}
|
|
655
|
+
|
|
415
656
|
return cmd;
|
|
416
657
|
}
|
|
417
658
|
|
|
@@ -456,7 +697,8 @@ class Shell extends EventEmitter {
|
|
|
456
697
|
const matches = part.matchAll(variableRegex);
|
|
457
698
|
let match = matches.next();
|
|
458
699
|
while (!match.done) {
|
|
459
|
-
const
|
|
700
|
+
const varName = match.value[1] || match.value[2];
|
|
701
|
+
const value = this.context.getVar(varName);
|
|
460
702
|
part = part.replace(new RegExp(Shell._escapeRegExp(match.value[0]), 'g'), String(value));
|
|
461
703
|
match = matches.next();
|
|
462
704
|
}
|
|
@@ -480,10 +722,11 @@ class Shell extends EventEmitter {
|
|
|
480
722
|
}
|
|
481
723
|
|
|
482
724
|
async _runAndCapture(cmd) {
|
|
483
|
-
const subshell = new
|
|
725
|
+
const subshell = new globalThis.shell({
|
|
484
726
|
cwd: this.context.fs.cwd,
|
|
485
727
|
filesystem: this.context.fs.getRaw(),
|
|
486
|
-
permissions: this.context.fs.getRawPermissions()
|
|
728
|
+
permissions: this.context.fs.getRawPermissions(),
|
|
729
|
+
subshell: true
|
|
487
730
|
});
|
|
488
731
|
subshell.context.source(this.context);
|
|
489
732
|
|
|
@@ -529,10 +772,11 @@ class Shell extends EventEmitter {
|
|
|
529
772
|
}
|
|
530
773
|
|
|
531
774
|
async _captureForPipe(cmd, stdin) {
|
|
532
|
-
const subshell = new
|
|
775
|
+
const subshell = new globalThis.shell({
|
|
533
776
|
cwd: this.context.fs.cwd,
|
|
534
777
|
filesystem: this.context.fs.getRaw(),
|
|
535
|
-
permissions: this.context.fs.getRawPermissions()
|
|
778
|
+
permissions: this.context.fs.getRawPermissions(),
|
|
779
|
+
subshell: true
|
|
536
780
|
});
|
|
537
781
|
subshell.context.source(this.context);
|
|
538
782
|
if (stdin !== undefined) subshell.stdin = stdin;
|
|
@@ -615,6 +859,9 @@ class Shell extends EventEmitter {
|
|
|
615
859
|
|
|
616
860
|
_isWasmFile(path, content) {
|
|
617
861
|
if (path.endsWith('.wasm') || path.endsWith('.wat')) return true;
|
|
862
|
+
if (content instanceof Uint8Array) {
|
|
863
|
+
return content.length >= 4 && content[0] === 0x00 && content[1] === 0x61 && content[2] === 0x73 && content[3] === 0x6d;
|
|
864
|
+
}
|
|
618
865
|
if (typeof content !== 'string') return false;
|
|
619
866
|
// Binary WASM: magic bytes \0asm
|
|
620
867
|
if (content.charCodeAt(0) === 0 && content.slice(1, 4) === 'asm') return true;
|
|
@@ -633,7 +880,11 @@ Shell.STATUS_DESTROYED = 'DESTROYED';
|
|
|
633
880
|
|
|
634
881
|
Shell.Command = Command;
|
|
635
882
|
Shell.InteractiveCommand = InteractiveCommand;
|
|
883
|
+
Shell.WasmCommand = WasmCommand;
|
|
884
|
+
Shell.ReplCommand = require('./lib/ReplCommand');
|
|
636
885
|
Shell.FS = BaseFileSystem;
|
|
886
|
+
Shell.BinaryContainer = require('./lib/BinaryContainer');
|
|
887
|
+
Shell.Storage = Shell.BinaryContainer.FilesystemStorage;
|
|
637
888
|
|
|
638
|
-
|
|
889
|
+
globalThis.shell = Shell;
|
|
639
890
|
module.exports = Shell;
|