brsh 2.6.0 → 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 +7 -0
- package/Readme.md +127 -0
- package/bin/Readme.md +6 -3
- package/bin/brsh-shell.js +53 -8
- package/bin/exportFileSystem.js +192 -80
- package/dist/shell.es.js +2419 -1277
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +21 -14
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.es.js +2475 -1333
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +23 -16
- 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 +131 -20
- 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 +1 -1
- 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/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,6 +12,9 @@ const localCommands = require('./lib/local');
|
|
|
12
12
|
const events = require('events');
|
|
13
13
|
const BaseFileSystem = require('./lib/BaseFileSystem');
|
|
14
14
|
|
|
15
|
+
const BinaryContainer = require('./lib/BinaryContainer');
|
|
16
|
+
const { isAsyncFilesystemSource, resolveFilesystemSource } = BinaryContainer;
|
|
17
|
+
|
|
15
18
|
const variableRegex = /\$(?:\{([\w\d?#-]+)\}|([\w\d]+|[?#-]))/g;
|
|
16
19
|
|
|
17
20
|
|
|
@@ -61,17 +64,6 @@ class Shell extends EventEmitter {
|
|
|
61
64
|
this.context.setVar('-', initialFlags);
|
|
62
65
|
}
|
|
63
66
|
}
|
|
64
|
-
if (useRealFilesystem) {
|
|
65
|
-
this.context.setNodeFilesystem(cwd);
|
|
66
|
-
} else {
|
|
67
|
-
this.context.setFilesystem(filesystem, permissions);
|
|
68
|
-
}
|
|
69
|
-
this.context.setCwd(cwd);
|
|
70
|
-
|
|
71
|
-
const pathDirExisted = useRealFilesystem || this.context.fs.exists(this.context.getVar('PATH'));
|
|
72
|
-
this._loadDefaultCommands();
|
|
73
|
-
this._loadBuiltinCommands(pathDirExisted);
|
|
74
|
-
|
|
75
67
|
this._lastCode = 0;
|
|
76
68
|
this._subshell = subshell;
|
|
77
69
|
this._useRealFilesystem = useRealFilesystem;
|
|
@@ -79,13 +71,106 @@ class Shell extends EventEmitter {
|
|
|
79
71
|
this._motd = motd;
|
|
80
72
|
this._initialCommand = command || null;
|
|
81
73
|
this._suppressMotd = Boolean(command);
|
|
74
|
+
this._loadingFs = null;
|
|
75
|
+
this._queuedWhileLoading = false;
|
|
82
76
|
|
|
83
|
-
if (
|
|
84
|
-
this.
|
|
85
|
-
|
|
77
|
+
if (useRealFilesystem) {
|
|
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;
|
|
97
|
+
} else {
|
|
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
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
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
|
+
};
|
|
130
|
+
|
|
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
|
+
}
|
|
143
|
+
|
|
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();
|
|
86
167
|
} else {
|
|
87
168
|
this._initializing = false;
|
|
169
|
+
if (!this._queuedWhileLoading && !this.destroyed) {
|
|
170
|
+
this.emit('status', Shell.STATUS_READY);
|
|
171
|
+
}
|
|
88
172
|
}
|
|
173
|
+
return this;
|
|
89
174
|
}
|
|
90
175
|
|
|
91
176
|
get path() {
|
|
@@ -163,7 +248,7 @@ class Shell extends EventEmitter {
|
|
|
163
248
|
}
|
|
164
249
|
} finally {
|
|
165
250
|
this._initializing = false;
|
|
166
|
-
if (!this.destroyed) {
|
|
251
|
+
if (!this._queuedWhileLoading && !this.destroyed) {
|
|
167
252
|
this.emit('status', Shell.STATUS_READY);
|
|
168
253
|
}
|
|
169
254
|
}
|
|
@@ -258,6 +343,11 @@ class Shell extends EventEmitter {
|
|
|
258
343
|
const groups = this._groupPipes(segments);
|
|
259
344
|
let shouldRun = true;
|
|
260
345
|
|
|
346
|
+
if (this._loadingFs && events) {
|
|
347
|
+
this._queuedWhileLoading = true;
|
|
348
|
+
}
|
|
349
|
+
const initialPromise = this._loadingFs ? Promise.resolve(this._loadingFs) : Promise.resolve();
|
|
350
|
+
|
|
261
351
|
const promise = groups.reduce((chain, { commands, op }) => {
|
|
262
352
|
return chain.then(() => {
|
|
263
353
|
if (!shouldRun) {
|
|
@@ -272,7 +362,7 @@ class Shell extends EventEmitter {
|
|
|
272
362
|
else if (op === ';') shouldRun = true;
|
|
273
363
|
});
|
|
274
364
|
});
|
|
275
|
-
},
|
|
365
|
+
}, initialPromise);
|
|
276
366
|
|
|
277
367
|
if(events) {
|
|
278
368
|
return promise.then(() => {
|
|
@@ -543,12 +633,26 @@ class Shell extends EventEmitter {
|
|
|
543
633
|
|
|
544
634
|
throw new Error(`${command}: permission denied`);
|
|
545
635
|
}
|
|
546
|
-
|
|
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
|
+
}
|
|
547
642
|
|
|
548
643
|
if(!cmd) {
|
|
549
644
|
throw new Error(`${command}: Command not found`);
|
|
550
645
|
}
|
|
551
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
|
+
|
|
552
656
|
return cmd;
|
|
553
657
|
}
|
|
554
658
|
|
|
@@ -618,7 +722,7 @@ class Shell extends EventEmitter {
|
|
|
618
722
|
}
|
|
619
723
|
|
|
620
724
|
async _runAndCapture(cmd) {
|
|
621
|
-
const subshell = new
|
|
725
|
+
const subshell = new globalThis.shell({
|
|
622
726
|
cwd: this.context.fs.cwd,
|
|
623
727
|
filesystem: this.context.fs.getRaw(),
|
|
624
728
|
permissions: this.context.fs.getRawPermissions(),
|
|
@@ -668,7 +772,7 @@ class Shell extends EventEmitter {
|
|
|
668
772
|
}
|
|
669
773
|
|
|
670
774
|
async _captureForPipe(cmd, stdin) {
|
|
671
|
-
const subshell = new
|
|
775
|
+
const subshell = new globalThis.shell({
|
|
672
776
|
cwd: this.context.fs.cwd,
|
|
673
777
|
filesystem: this.context.fs.getRaw(),
|
|
674
778
|
permissions: this.context.fs.getRawPermissions(),
|
|
@@ -755,6 +859,9 @@ class Shell extends EventEmitter {
|
|
|
755
859
|
|
|
756
860
|
_isWasmFile(path, content) {
|
|
757
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
|
+
}
|
|
758
865
|
if (typeof content !== 'string') return false;
|
|
759
866
|
// Binary WASM: magic bytes \0asm
|
|
760
867
|
if (content.charCodeAt(0) === 0 && content.slice(1, 4) === 'asm') return true;
|
|
@@ -773,7 +880,11 @@ Shell.STATUS_DESTROYED = 'DESTROYED';
|
|
|
773
880
|
|
|
774
881
|
Shell.Command = Command;
|
|
775
882
|
Shell.InteractiveCommand = InteractiveCommand;
|
|
883
|
+
Shell.WasmCommand = WasmCommand;
|
|
884
|
+
Shell.ReplCommand = require('./lib/ReplCommand');
|
|
776
885
|
Shell.FS = BaseFileSystem;
|
|
886
|
+
Shell.BinaryContainer = require('./lib/BinaryContainer');
|
|
887
|
+
Shell.Storage = Shell.BinaryContainer.FilesystemStorage;
|
|
777
888
|
|
|
778
|
-
|
|
889
|
+
globalThis.shell = Shell;
|
|
779
890
|
module.exports = Shell;
|