brsh 3.9.0 → 3.10.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 +22 -4
- package/bin/Readme.md +33 -3
- package/bin/exportFileSystem.js +127 -110
- package/bin/wbt.js +118 -0
- package/dist/shell.es.js +6852 -6616
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +23 -22
- package/dist/shell.min.js.map +1 -1
- package/dist/wbt.es.js +2534 -2342
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +15 -11
- package/dist/wbt.min.js.map +1 -1
- package/index.js +66 -71
- package/lib/BaseFileSystem.js +55 -2
- package/lib/Context.js +1 -1
- package/lib/FileSystem.js +4 -2
- package/lib/JsCommand.js +113 -0
- package/lib/NodeFileSystem.js +164 -30
- package/lib/Readme.md +1 -0
- package/lib/ScriptCommand.js +110 -7
- package/lib/WasmCommand.js +85 -25
- package/lib/buildFilesystemStructure.js +38 -24
- package/lib/isUtf8Text.js +47 -0
- package/lib/local/which.js +8 -3
- package/lib/wbt/index.js +344 -160
- package/lib/wbt/registry.js +21 -1
- package/package.json +3 -2
- package/test/exportFileSystem.test.js +37 -15
- package/test/js-command.test.js +125 -0
- package/test/node-filesystem.test.js +7 -0
- package/test/scripts.test.js +30 -0
- package/test/wbt.test.js +308 -10
- package/test/wbtCli.test.js +160 -0
package/lib/WasmCommand.js
CHANGED
|
@@ -36,15 +36,35 @@ const FDFLAGS_SYNC = 1 << 4;
|
|
|
36
36
|
|
|
37
37
|
class WasmCommand extends ScriptCommand {
|
|
38
38
|
// this.script = filepath (set by ScriptCommand parent constructor)
|
|
39
|
+
constructor(script, args, context) {
|
|
40
|
+
super(script, args, context);
|
|
41
|
+
this._ensurePkgMeta();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_ensurePkgMeta() {
|
|
45
|
+
if (this._pkgMeta === undefined && this.script) {
|
|
46
|
+
const fs = (this.context && this.context.fs) || this.fs;
|
|
47
|
+
if (fs) {
|
|
48
|
+
this._pkgMeta = readSiblingPkgMeta(fs, this.script);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return this._pkgMeta || null;
|
|
52
|
+
}
|
|
39
53
|
|
|
40
|
-
get name() {
|
|
54
|
+
get name() {
|
|
55
|
+
const meta = this._ensurePkgMeta();
|
|
56
|
+
if (meta && meta.bin) return String(meta.bin).toLowerCase();
|
|
57
|
+
if (this.script) return this.script.split('/').pop().replace(/\.(wasm|wat)$/i, '').toLowerCase();
|
|
58
|
+
return 'wasm';
|
|
59
|
+
}
|
|
41
60
|
get requiresFilesystem() { return true; }
|
|
42
61
|
|
|
43
62
|
get interactive() {
|
|
44
63
|
const isHelpOrVersion = (this.arguments || []).some(a => a === '--help' || a === '-h' || a === '--version' || a === '-V' || a === '-v');
|
|
45
64
|
if (isHelpOrVersion) return false;
|
|
46
65
|
if (this._isInteractive !== undefined) return Boolean(this._isInteractive);
|
|
47
|
-
|
|
66
|
+
const meta = this._ensurePkgMeta();
|
|
67
|
+
if (meta && (meta.interactive || meta.wasm?.interactive)) return true;
|
|
48
68
|
if (this._inspectedWasm && this._inspectedWasm.isInteractive) return true;
|
|
49
69
|
return false;
|
|
50
70
|
}
|
|
@@ -151,7 +171,15 @@ class WasmCommand extends ScriptCommand {
|
|
|
151
171
|
const nodeFs = (() => { try { return require('fs'); } catch { return null; } })();
|
|
152
172
|
|
|
153
173
|
return this._loadBytes(filepath, nodeFs)
|
|
154
|
-
.then(({ bytes, isWat }) =>
|
|
174
|
+
.then(({ bytes, isWat }) => {
|
|
175
|
+
if (this.interactive) {
|
|
176
|
+
this.emit('interactive');
|
|
177
|
+
if (this.context && typeof this.context.emit === 'function') {
|
|
178
|
+
this.context.emit('interactive');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return isWat ? this._runWat(bytes) : this._runWasm(bytes);
|
|
182
|
+
})
|
|
155
183
|
.catch(e => {
|
|
156
184
|
this.stdErr = e.message || String(e);
|
|
157
185
|
this.exitCode = 1;
|
|
@@ -258,10 +286,21 @@ class WasmCommand extends ScriptCommand {
|
|
|
258
286
|
this.interactive && isTtyEnvironment
|
|
259
287
|
);
|
|
260
288
|
|
|
289
|
+
if (isInteractive) {
|
|
290
|
+
this.emit('interactive');
|
|
291
|
+
if (this.context && typeof this.context.emit === 'function') {
|
|
292
|
+
this.context.emit('interactive');
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
261
296
|
const promoteToInteractive = () => {
|
|
262
297
|
if (isInteractive || !isTtyEnvironment) return;
|
|
263
298
|
isInteractive = true;
|
|
264
299
|
this._isInteractive = true;
|
|
300
|
+
this.emit('interactive');
|
|
301
|
+
if (this.context && typeof this.context.emit === 'function') {
|
|
302
|
+
this.context.emit('interactive');
|
|
303
|
+
}
|
|
265
304
|
|
|
266
305
|
this._enterAltScreen();
|
|
267
306
|
|
|
@@ -1385,12 +1424,13 @@ class WasmCommand extends ScriptCommand {
|
|
|
1385
1424
|
const args = this.arguments || [];
|
|
1386
1425
|
const hasOutput = this.result && this.result.getStdOutput && this.result.getStdOutput().length > 0;
|
|
1387
1426
|
if (!hasOutput && (this.exitCode === 0 || this.exitCode === undefined)) {
|
|
1388
|
-
const
|
|
1427
|
+
const pkgMeta = this._ensurePkgMeta();
|
|
1428
|
+
const binLabel = (pkgMeta && pkgMeta.bin) || (this.script || 'wasm').split('/').pop();
|
|
1389
1429
|
const meta = Object.assign({
|
|
1390
|
-
name: binLabel,
|
|
1391
|
-
version: 'wasi',
|
|
1430
|
+
name: (pkgMeta && pkgMeta.name) || binLabel,
|
|
1431
|
+
version: (pkgMeta && pkgMeta.version) || 'wasi',
|
|
1392
1432
|
description: `WebAssembly WASI binary (${this.script})`
|
|
1393
|
-
},
|
|
1433
|
+
}, pkgMeta || {});
|
|
1394
1434
|
if (args.includes('--help') || args.includes('-h')) {
|
|
1395
1435
|
const inspected = inspectWasmBytes(bytes);
|
|
1396
1436
|
for (const line of formatWasmHelp(binLabel, meta, inspected)) {
|
|
@@ -1458,24 +1498,13 @@ function readBytesFromFs(fs, filepath) {
|
|
|
1458
1498
|
*/
|
|
1459
1499
|
function writeBytesToFs(fs, filepath, bytes, mimeType = 'application/octet-stream') {
|
|
1460
1500
|
const uint8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
1501
|
+
if (fs && fs.isRealFilesystem) {
|
|
1502
|
+
fs.setFileByPath(filepath, uint8, true);
|
|
1503
|
+
return;
|
|
1504
|
+
}
|
|
1461
1505
|
const copy = new Uint8Array(uint8.byteLength);
|
|
1462
1506
|
copy.set(uint8);
|
|
1463
1507
|
const bf = new BinaryFile(copy.buffer, mimeType);
|
|
1464
|
-
if (fs.isRealFilesystem) {
|
|
1465
|
-
try {
|
|
1466
|
-
const nodeFs = require('fs');
|
|
1467
|
-
const nodePath = require('path');
|
|
1468
|
-
const abs = fs.absolutePath(filepath);
|
|
1469
|
-
nodeFs.mkdirSync(nodePath.dirname(abs), { recursive: true });
|
|
1470
|
-
nodeFs.writeFileSync(abs, Buffer.from(uint8.buffer, uint8.byteOffset, uint8.byteLength));
|
|
1471
|
-
return;
|
|
1472
|
-
} catch {
|
|
1473
|
-
if (typeof fs._setFileRaw === 'function') {
|
|
1474
|
-
fs._setFileRaw(filepath, bf, true);
|
|
1475
|
-
return;
|
|
1476
|
-
}
|
|
1477
|
-
}
|
|
1478
|
-
}
|
|
1479
1508
|
if (typeof fs._setFileRaw === 'function' && filepath.startsWith('/var/lib/wbt/')) {
|
|
1480
1509
|
fs._setFileRaw(filepath, bf, true);
|
|
1481
1510
|
return;
|
|
@@ -1809,7 +1838,7 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
1809
1838
|
await this._runRawWasmBytes(bytes, args);
|
|
1810
1839
|
} else if (typeof target === 'function') {
|
|
1811
1840
|
// 3. Factory / constructor / CLI function (`export default function(opts)` or class)
|
|
1812
|
-
await this._runFunctionOrEmscripten(target, args, isHelp);
|
|
1841
|
+
await this._runFunctionOrEmscripten(target, args, isHelp, isVersion);
|
|
1813
1842
|
} else if (target && typeof target === 'object') {
|
|
1814
1843
|
// 4. Standard CLI module object (`main`, `run`, `callMain`, `exec`)
|
|
1815
1844
|
if (target.ready) await target.ready;
|
|
@@ -1997,7 +2026,7 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
1997
2026
|
};
|
|
1998
2027
|
}
|
|
1999
2028
|
|
|
2000
|
-
async _runFunctionOrEmscripten(fn, args, isHelp = false) {
|
|
2029
|
+
async _runFunctionOrEmscripten(fn, args, isHelp = false, isVersion = false) {
|
|
2001
2030
|
const outLines = [];
|
|
2002
2031
|
const errLines = [];
|
|
2003
2032
|
const stdinBytes = new TextEncoder().encode(this.stdin || '');
|
|
@@ -2290,6 +2319,38 @@ function createGenericWasmCommand(pkgMeta, config = {}, binNameOverride = null)
|
|
|
2290
2319
|
return GenericWasmCommand;
|
|
2291
2320
|
}
|
|
2292
2321
|
|
|
2322
|
+
function isWasmFile(filepath, content) {
|
|
2323
|
+
if (filepath && (filepath.endsWith('.wasm') || filepath.endsWith('.wat'))) return true;
|
|
2324
|
+
if (content instanceof Uint8Array) {
|
|
2325
|
+
return content.length >= 4 && content[0] === 0x00 && content[1] === 0x61 && content[2] === 0x73 && content[3] === 0x6d;
|
|
2326
|
+
}
|
|
2327
|
+
if (typeof content !== 'string') return false;
|
|
2328
|
+
// Binary WASM: magic bytes \0asm
|
|
2329
|
+
if (content.charCodeAt(0) === 0 && content.slice(1, 4) === 'asm') return true;
|
|
2330
|
+
// WAT text format
|
|
2331
|
+
return content.trimStart().startsWith('(module');
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
function readSiblingPkgMeta(fs, filepath) {
|
|
2335
|
+
if (!fs || !filepath || typeof fs.exists !== 'function' || typeof fs.getFileByPath !== 'function') {
|
|
2336
|
+
return null;
|
|
2337
|
+
}
|
|
2338
|
+
try {
|
|
2339
|
+
const dirParts = filepath.split('/').filter(Boolean);
|
|
2340
|
+
dirParts.pop();
|
|
2341
|
+
const pkgJsonPath = (dirParts.length ? '/' + dirParts.join('/') : '') + '/package.json';
|
|
2342
|
+
if (fs.exists(pkgJsonPath)) {
|
|
2343
|
+
const rawJson = fs.getFileByPath(pkgJsonPath);
|
|
2344
|
+
if (typeof rawJson === 'string') {
|
|
2345
|
+
return JSON.parse(rawJson);
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
} catch (_) {}
|
|
2349
|
+
return null;
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
WasmCommand.isWasmFile = isWasmFile;
|
|
2353
|
+
WasmCommand.readSiblingPkgMeta = readSiblingPkgMeta;
|
|
2293
2354
|
WasmCommand.readBytesFromFs = readBytesFromFs;
|
|
2294
2355
|
WasmCommand.writeBytesToFs = writeBytesToFs;
|
|
2295
2356
|
WasmCommand.loadModule = loadModule;
|
|
@@ -2300,4 +2361,3 @@ WasmCommand.createGenericWasmCommand = createGenericWasmCommand;
|
|
|
2300
2361
|
WasmCommand.createCommand = createGenericWasmCommand;
|
|
2301
2362
|
|
|
2302
2363
|
module.exports = WasmCommand;
|
|
2303
|
-
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
|
+
const { SymlinkRef } = require('./BaseFileSystem');
|
|
6
|
+
const isUtf8Text = require('./isUtf8Text');
|
|
5
7
|
|
|
6
8
|
const IGNORE = ['.DS_Store'];
|
|
7
9
|
|
|
@@ -23,9 +25,13 @@ function _isWasmBuffer(buf) {
|
|
|
23
25
|
/**
|
|
24
26
|
* Recursively builds a filesystem structure object from a real directory.
|
|
25
27
|
* Returns { filesystem, permissions } where:
|
|
26
|
-
* - filesystem is the nested content tree (directories as objects, files as
|
|
28
|
+
* - filesystem is the nested content tree (directories as objects, files as strings or BinaryFiles/SymlinkRefs)
|
|
27
29
|
* - permissions maps virtual absolute paths to Unix mode bits (e.g. { '/bin/script.sh': 0o755 })
|
|
28
30
|
*
|
|
31
|
+
* Real filenames and extensions (.js, .wasm, etc.) are preserved verbatim.
|
|
32
|
+
* OS symlinks are serialized as SymlinkRef instances.
|
|
33
|
+
* Files are checked for UTF-8/ASCII text content; anything that is not valid text defaults to binary.
|
|
34
|
+
*
|
|
29
35
|
* Throws if `root` cannot be read.
|
|
30
36
|
*/
|
|
31
37
|
function buildStructure(root) {
|
|
@@ -42,42 +48,50 @@ function _buildTree(root, virtualPrefix, permissions) {
|
|
|
42
48
|
return structure;
|
|
43
49
|
}
|
|
44
50
|
try {
|
|
45
|
-
const
|
|
51
|
+
const filePath = path.join(root, handle);
|
|
52
|
+
const stat = fs.lstatSync(filePath);
|
|
53
|
+
|
|
54
|
+
if(stat.isSymbolicLink()) {
|
|
55
|
+
const target = fs.readlinkSync(filePath);
|
|
56
|
+
structure[handle] = new SymlinkRef(target);
|
|
57
|
+
permissions[virtualPrefix + '/' + handle] = stat.mode & 0o777;
|
|
58
|
+
return structure;
|
|
59
|
+
}
|
|
60
|
+
|
|
46
61
|
if(stat.isDirectory()) {
|
|
47
62
|
structure[handle] = _buildTree(
|
|
48
|
-
|
|
63
|
+
filePath,
|
|
49
64
|
virtualPrefix + '/' + handle,
|
|
50
65
|
permissions
|
|
51
66
|
);
|
|
52
67
|
return structure;
|
|
53
68
|
}
|
|
54
69
|
|
|
55
|
-
const filePath = path.join(root, handle);
|
|
56
70
|
const isJavaScript = handle.endsWith('.js');
|
|
57
71
|
const ext = path.extname(handle).toLowerCase();
|
|
58
|
-
const rawBuf =
|
|
59
|
-
const isWasmMagic =
|
|
60
|
-
const
|
|
61
|
-
const mime = BINARY_MIME[ext] || (isWasmMagic ? 'application/wasm' : 'application/octet-stream');
|
|
62
|
-
const fileContents = isJavaScript
|
|
63
|
-
? null
|
|
64
|
-
: isBinary
|
|
65
|
-
? `data:${mime};base64,${rawBuf.toString('base64')}`
|
|
66
|
-
: rawBuf.toString('utf8');
|
|
72
|
+
const rawBuf = fs.readFileSync(filePath);
|
|
73
|
+
const isWasmMagic = _isWasmBuffer(rawBuf);
|
|
74
|
+
const isKnownBinary = (ext in BINARY_MIME) || isWasmMagic;
|
|
67
75
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
let fileContents;
|
|
77
|
+
if (isJavaScript) {
|
|
78
|
+
// Preserved as UTF-8 text string (bundled with esbuild later during export)
|
|
79
|
+
fileContents = rawBuf.toString('utf8');
|
|
80
|
+
} else if (isKnownBinary) {
|
|
81
|
+
const mime = BINARY_MIME[ext] || (isWasmMagic ? 'application/wasm' : 'application/octet-stream');
|
|
82
|
+
fileContents = `data:${mime};base64,${rawBuf.toString('base64')}`;
|
|
83
|
+
} else if (isUtf8Text(rawBuf)) {
|
|
84
|
+
fileContents = rawBuf.toString('utf8');
|
|
85
|
+
} else {
|
|
86
|
+
// Content-based binary fallback for unknown binary formats (.wad, .gguf, ELF, images, etc.)
|
|
87
|
+
const mime = 'application/octet-stream';
|
|
88
|
+
fileContents = `data:${mime};base64,${rawBuf.toString('base64')}`;
|
|
89
|
+
}
|
|
74
90
|
|
|
75
|
-
structure[
|
|
76
|
-
? `require('${path.resolve(root, handle)}')`
|
|
77
|
-
: fileContents;
|
|
91
|
+
structure[handle] = fileContents;
|
|
78
92
|
|
|
79
93
|
// Record real Unix permission bits for this file
|
|
80
|
-
permissions[virtualPrefix + '/' +
|
|
94
|
+
permissions[virtualPrefix + '/' + handle] = stat.mode & 0o777;
|
|
81
95
|
} catch(fe) {
|
|
82
96
|
// Silently skip files/dirs that are inaccessible
|
|
83
97
|
}
|
|
@@ -85,4 +99,4 @@ function _buildTree(root, virtualPrefix, permissions) {
|
|
|
85
99
|
}, {});
|
|
86
100
|
}
|
|
87
101
|
|
|
88
|
-
module.exports = { buildStructure, IGNORE };
|
|
102
|
+
module.exports = { buildStructure, IGNORE, BINARY_MIME, _isWasmBuffer };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks whether a byte buffer contains valid UTF-8/ASCII text.
|
|
5
|
+
* Returns true if the buffer is text, false if it should be treated as binary.
|
|
6
|
+
*
|
|
7
|
+
* Rules:
|
|
8
|
+
* - Empty buffers are considered valid text.
|
|
9
|
+
* - Checks for NUL bytes (0x00) or non-printable ASCII control characters.
|
|
10
|
+
* Valid text allows tabs (\t, 0x09), line feeds (\n, 0x0A), carriage returns (\r, 0x0D),
|
|
11
|
+
* and ANSI escape sequences (ESC, 0x1B).
|
|
12
|
+
* Characters 0x00-0x08, 0x0B, 0x0C, 0x0E-0x1A, 0x1C-0x1F are treated as binary.
|
|
13
|
+
* - Uses TextDecoder('utf-8', { fatal: true }) to verify strict UTF-8 validity.
|
|
14
|
+
*/
|
|
15
|
+
function isUtf8Text(buf) {
|
|
16
|
+
if (!buf || buf.length === 0) return true;
|
|
17
|
+
|
|
18
|
+
const u8 = buf instanceof Uint8Array
|
|
19
|
+
? buf
|
|
20
|
+
: (typeof Buffer !== 'undefined' && Buffer.isBuffer(buf))
|
|
21
|
+
? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
|
|
22
|
+
: (buf instanceof ArrayBuffer || ArrayBuffer.isView(buf))
|
|
23
|
+
? new Uint8Array(buf.buffer || buf, buf.byteOffset || 0, buf.byteLength)
|
|
24
|
+
: null;
|
|
25
|
+
|
|
26
|
+
if (!u8) return false;
|
|
27
|
+
|
|
28
|
+
// Fast check first 8KB (or entire buffer if smaller) for binary control bytes
|
|
29
|
+
const checkLength = Math.min(u8.length, 8192);
|
|
30
|
+
for (let i = 0; i < checkLength; i++) {
|
|
31
|
+
const b = u8[i];
|
|
32
|
+
if (b === 0x00) return false;
|
|
33
|
+
if (b < 0x20 && b !== 0x09 && b !== 0x0A && b !== 0x0D && b !== 0x1B) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
new TextDecoder('utf-8', { fatal: true }).decode(u8);
|
|
40
|
+
return true;
|
|
41
|
+
} catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = isUtf8Text;
|
|
47
|
+
module.exports.isUtf8Text = isUtf8Text;
|
package/lib/local/which.js
CHANGED
|
@@ -10,13 +10,18 @@ class Which extends LocalCommand {
|
|
|
10
10
|
return Promise.reject('which requires a single argument');
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
const
|
|
13
|
+
const cmdPath = `${this.context.getVar('PATH')}/${this.arguments[0]}`;
|
|
14
|
+
const file = this.context.fs.getFileByPath(cmdPath);
|
|
14
15
|
|
|
15
|
-
if(!file || file.constructor === Object
|
|
16
|
+
if(!file || file.constructor === Object) {
|
|
16
17
|
return Promise.reject();
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
if(file.constructor === String && !file.startsWith('#!') && !this.context.fs.isExecutable(cmdPath)) {
|
|
21
|
+
return Promise.reject();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return Promise.resolve(cmdPath);
|
|
20
25
|
}
|
|
21
26
|
}
|
|
22
27
|
|