brsh 2.6.0 → 2.8.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 +20 -0
- package/Readme.md +145 -1
- package/bin/Readme.md +6 -3
- package/bin/brsh-shell.js +68 -9
- package/bin/exportFileSystem.js +192 -80
- package/dist/shell.es.js +2497 -1300
- 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.cjs.js +101 -0
- package/dist/terminal.cjs.js.map +1 -0
- package/dist/terminal.es.js +290 -6965
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +4 -25
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +3974 -0
- package/dist/wbt.es.js.map +1 -0
- package/dist/wbt.min.js +10 -0
- package/dist/wbt.min.js.map +1 -0
- package/index.js +160 -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/Terminal.js +45 -10
- package/lib/WasmCommand.js +1123 -35
- package/lib/buildFilesystemStructure.js +15 -4
- package/lib/local/curl.js +60 -2
- package/lib/wbt/index.js +756 -0
- package/lib/wbt/packages/wllama.js +1051 -0
- package/lib/wbt/registry.js +193 -0
- package/lib/wbt/utils.js +118 -0
- package/package.json +11 -3
- package/test/binary-filesystem.test.js +248 -0
- package/test/curl.test.js +166 -0
- package/test/exportFileSystem.test.js +78 -0
- package/test/terminal.test.js +113 -0
- package/test/wasm-cli.test.js +127 -0
- package/test/wbt.test.js +1175 -0
- package/vite.config.demo.js +4 -0
- package/vite.config.js +4 -0
- package/vite.config.terminal.js +16 -12
- package/vite.config.wbt.js +21 -0
- package/vitest.config.js +6 -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,29 @@ 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
|
+
if (!this.context.fs.isExecutable(binLocation)) {
|
|
650
|
+
throw new Error(`${command}: permission denied`);
|
|
651
|
+
}
|
|
652
|
+
return WasmCommand.bind(null, binLocation);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
if (typeof cmd === 'string' && cmd.startsWith('#!')) {
|
|
656
|
+
return ScriptCommand.bind(this, cmd);
|
|
657
|
+
}
|
|
658
|
+
|
|
552
659
|
return cmd;
|
|
553
660
|
}
|
|
554
661
|
|
|
@@ -618,7 +725,7 @@ class Shell extends EventEmitter {
|
|
|
618
725
|
}
|
|
619
726
|
|
|
620
727
|
async _runAndCapture(cmd) {
|
|
621
|
-
const subshell = new
|
|
728
|
+
const subshell = new globalThis.shell({
|
|
622
729
|
cwd: this.context.fs.cwd,
|
|
623
730
|
filesystem: this.context.fs.getRaw(),
|
|
624
731
|
permissions: this.context.fs.getRawPermissions(),
|
|
@@ -668,7 +775,7 @@ class Shell extends EventEmitter {
|
|
|
668
775
|
}
|
|
669
776
|
|
|
670
777
|
async _captureForPipe(cmd, stdin) {
|
|
671
|
-
const subshell = new
|
|
778
|
+
const subshell = new globalThis.shell({
|
|
672
779
|
cwd: this.context.fs.cwd,
|
|
673
780
|
filesystem: this.context.fs.getRaw(),
|
|
674
781
|
permissions: this.context.fs.getRawPermissions(),
|
|
@@ -755,6 +862,9 @@ class Shell extends EventEmitter {
|
|
|
755
862
|
|
|
756
863
|
_isWasmFile(path, content) {
|
|
757
864
|
if (path.endsWith('.wasm') || path.endsWith('.wat')) return true;
|
|
865
|
+
if (content instanceof Uint8Array) {
|
|
866
|
+
return content.length >= 4 && content[0] === 0x00 && content[1] === 0x61 && content[2] === 0x73 && content[3] === 0x6d;
|
|
867
|
+
}
|
|
758
868
|
if (typeof content !== 'string') return false;
|
|
759
869
|
// Binary WASM: magic bytes \0asm
|
|
760
870
|
if (content.charCodeAt(0) === 0 && content.slice(1, 4) === 'asm') return true;
|
|
@@ -765,6 +875,30 @@ class Shell extends EventEmitter {
|
|
|
765
875
|
static _escapeRegExp(string) {
|
|
766
876
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
767
877
|
}
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* Asynchronously creates and boots a Shell instance.
|
|
881
|
+
* Pre-resolves any async filesystem sources (URLs, Promises, .brfs containers, or IndexedDB)
|
|
882
|
+
* before returning the fully initialized shell.
|
|
883
|
+
*/
|
|
884
|
+
static async create(options = {}) {
|
|
885
|
+
let { filesystem, permissions, ...rest } = options;
|
|
886
|
+
if (isAsyncFilesystemSource(filesystem) || (filesystem && typeof filesystem === 'object' && isAsyncFilesystemSource(filesystem.filesystem))) {
|
|
887
|
+
const resolved = await resolveFilesystemSource(filesystem, permissions);
|
|
888
|
+
filesystem = resolved.filesystem;
|
|
889
|
+
permissions = resolved.permissions;
|
|
890
|
+
}
|
|
891
|
+
const shell = new Shell(Object.assign({}, rest, { filesystem, permissions }));
|
|
892
|
+
await shell.ready;
|
|
893
|
+
return shell;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
/**
|
|
897
|
+
* Helper to create a Shell directly from a .brfs container (URL, Uint8Array, or Promise).
|
|
898
|
+
*/
|
|
899
|
+
static async fromContainer(source, options = {}) {
|
|
900
|
+
return Shell.create(Object.assign({}, options, { filesystem: source }));
|
|
901
|
+
}
|
|
768
902
|
}
|
|
769
903
|
|
|
770
904
|
Shell.STATUS_READY = 'READY';
|
|
@@ -773,7 +907,13 @@ Shell.STATUS_DESTROYED = 'DESTROYED';
|
|
|
773
907
|
|
|
774
908
|
Shell.Command = Command;
|
|
775
909
|
Shell.InteractiveCommand = InteractiveCommand;
|
|
910
|
+
Shell.WasmCommand = WasmCommand;
|
|
911
|
+
Shell.createWasmCommand = WasmCommand.createGenericWasmCommand || WasmCommand.createCommand;
|
|
912
|
+
Shell.ReplCommand = require('./lib/ReplCommand');
|
|
776
913
|
Shell.FS = BaseFileSystem;
|
|
914
|
+
Shell.BinaryContainer = require('./lib/BinaryContainer');
|
|
915
|
+
Shell.Storage = Shell.BinaryContainer.FilesystemStorage;
|
|
777
916
|
|
|
778
|
-
|
|
917
|
+
globalThis.shell = Shell;
|
|
918
|
+
globalThis.Shell = Shell;
|
|
779
919
|
module.exports = Shell;
|