brsh 3.8.1 → 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 +14 -0
- package/Readme.md +26 -10
- package/bin/Readme.md +33 -3
- package/bin/exportFileSystem.js +127 -110
- package/bin/wbt.js +118 -0
- package/dist/shell.es.js +3924 -3468
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +25 -23
- package/dist/shell.min.js.map +1 -1
- package/dist/wbt.es.js +2700 -2300
- 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 +93 -71
- package/lib/BaseFileSystem.js +74 -1
- package/lib/BinaryContainer.js +10 -1
- package/lib/Command.js +63 -21
- package/lib/Context.js +1 -1
- package/lib/FileSystem.js +159 -6
- package/lib/InteractiveCommand.js +5 -0
- package/lib/JsCommand.js +113 -0
- package/lib/NodeFileSystem.js +164 -30
- package/lib/Readme.md +2 -0
- package/lib/ScriptCommand.js +120 -7
- package/lib/WasmCommand.js +213 -44
- 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/devices-signals.test.js +173 -0
- package/test/doom.test.js +36 -0
- package/test/exportFileSystem.test.js +37 -15
- package/test/fixtures/nano.wasm +0 -0
- 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/wbt/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const path = require('path');
|
|
3
4
|
const LocalCommand = require('../local/LocalCommand');
|
|
4
5
|
const WasmCommand = require('../WasmCommand');
|
|
5
6
|
const ReplCommand = require('../ReplCommand');
|
|
@@ -8,7 +9,6 @@ const wllamaAdapter = require('./packages/wllama');
|
|
|
8
9
|
const { writeBytesToFs, createGenericWasmCommand } = WasmCommand;
|
|
9
10
|
const { createReplWasmCommand } = ReplCommand;
|
|
10
11
|
|
|
11
|
-
const STATE_FILE = '/var/lib/wbt/installed.json';
|
|
12
12
|
const CACHE_DIR = '/var/cache/wbt';
|
|
13
13
|
/**
|
|
14
14
|
* Creates a Command class that executes a custom installed WASM or WAT file at `wasmPath`.
|
|
@@ -65,8 +65,8 @@ class Wbt extends LocalCommand {
|
|
|
65
65
|
return Boolean(this._activeSubCommand.interactive);
|
|
66
66
|
}
|
|
67
67
|
if (this.arguments && this.arguments[0] === 'run' && this.arguments[1]) {
|
|
68
|
-
const
|
|
69
|
-
const pkgMeta = this._getRegistry()[
|
|
68
|
+
const resolvedName = resolvePackageName(this.arguments[1]);
|
|
69
|
+
const pkgMeta = this._getRegistry()[resolvedName];
|
|
70
70
|
if (pkgMeta && pkgMeta.interactive) {
|
|
71
71
|
const isHelpOrVersion = this.arguments.slice(2).some(a => a === '--help' || a === '-h' || a === '--version' || a === '-V' || a === '-v');
|
|
72
72
|
return !isHelpOrVersion;
|
|
@@ -117,15 +117,11 @@ class Wbt extends LocalCommand {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
/**
|
|
120
|
-
* Restores any packages
|
|
121
|
-
*
|
|
120
|
+
* Restores any packages into the given Shell or Context.
|
|
121
|
+
* Retained as a safe no-op for backward compatibility: packages are self-contained on the filesystem.
|
|
122
122
|
*/
|
|
123
|
-
static restoreInstalled(
|
|
124
|
-
|
|
125
|
-
if (!ctx || !ctx.fs) return;
|
|
126
|
-
const instance = new (this.configure(config))([], ctx);
|
|
127
|
-
instance.fs = ctx.fs.sandbox();
|
|
128
|
-
instance._restoreCommands();
|
|
123
|
+
static restoreInstalled() {
|
|
124
|
+
// No-op: virtual filesystem is self-contained via /usr/lib/ and /bin symlinks
|
|
129
125
|
}
|
|
130
126
|
|
|
131
127
|
_getRegistry() {
|
|
@@ -143,23 +139,77 @@ class Wbt extends LocalCommand {
|
|
|
143
139
|
return base;
|
|
144
140
|
}
|
|
145
141
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
142
|
+
_getInstalledPackages() {
|
|
143
|
+
const pkgs = {};
|
|
144
|
+
const usrLib = this.context.fs.getFileByPath('/usr/lib');
|
|
145
|
+
if (!usrLib || typeof usrLib !== 'object') {
|
|
146
|
+
return pkgs;
|
|
147
|
+
}
|
|
148
|
+
const registry = this._getRegistry();
|
|
149
|
+
|
|
150
|
+
for (const dirName of Object.keys(usrLib)) {
|
|
151
|
+
if (dirName.startsWith('.')) continue;
|
|
152
|
+
const pkgName = dirName.toLowerCase();
|
|
153
|
+
const reg = registry[pkgName] || Object.values(registry).find(p => p.name === pkgName || p.bin === pkgName);
|
|
154
|
+
const dirFiles = this.context.fs.getFileByPath(`/usr/lib/${dirName}`) || {};
|
|
155
|
+
const files = (typeof dirFiles === 'object') ? Object.keys(dirFiles) : [];
|
|
156
|
+
const wasmFile = files.find(f => f.endsWith('.wasm') || f.endsWith('.wat'));
|
|
157
|
+
const wasmPath = wasmFile ? `/usr/lib/${dirName}/${wasmFile}` : (this.context.fs.exists(`/usr/lib/${dirName}/${pkgName}.wasm`) ? `/usr/lib/${dirName}/${pkgName}.wasm` : null);
|
|
158
|
+
|
|
159
|
+
const primaryBin = (reg && reg.bin) || pkgName;
|
|
160
|
+
const bins = [primaryBin];
|
|
161
|
+
if (reg && reg.aliases) {
|
|
162
|
+
bins.push(...reg.aliases);
|
|
163
|
+
}
|
|
164
|
+
if (reg && reg.scripts) {
|
|
165
|
+
bins.push(...Object.keys(reg.scripts));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
pkgs[pkgName] = Object.assign({
|
|
169
|
+
name: pkgName,
|
|
170
|
+
bin: primaryBin,
|
|
171
|
+
bins,
|
|
172
|
+
version: (reg && reg.version) || 'custom',
|
|
173
|
+
description: (reg && reg.description) || `Installed package ${pkgName}`,
|
|
174
|
+
type: (reg && reg.type) || (wasmPath ? 'custom-wasm' : 'generic-runtime'),
|
|
175
|
+
wasmPath
|
|
176
|
+
}, reg || {});
|
|
177
|
+
if (wasmPath) {
|
|
178
|
+
pkgs[pkgName].wasmPath = wasmPath;
|
|
154
179
|
}
|
|
155
|
-
} catch {
|
|
156
|
-
// ignore
|
|
157
180
|
}
|
|
158
|
-
return
|
|
181
|
+
return pkgs;
|
|
159
182
|
}
|
|
160
183
|
|
|
161
|
-
|
|
162
|
-
|
|
184
|
+
_generateJsCommandSource(pkgMeta, binName) {
|
|
185
|
+
if (pkgMeta.adapter === 'wllama' || pkgMeta.name === 'wllama') {
|
|
186
|
+
const wllamaPath = path.resolve(__dirname, 'packages/wllama.js');
|
|
187
|
+
return `'use strict';\n` +
|
|
188
|
+
`let wllamaModule;\n` +
|
|
189
|
+
`try {\n` +
|
|
190
|
+
` wllamaModule = require(${JSON.stringify(wllamaPath)});\n` +
|
|
191
|
+
`} catch (_) {\n` +
|
|
192
|
+
` try {\n` +
|
|
193
|
+
` wllamaModule = require('brsh/wbt/packages/wllama');\n` +
|
|
194
|
+
` } catch (e) {\n` +
|
|
195
|
+
` wllamaModule = require('./wllama');\n` +
|
|
196
|
+
` }\n` +
|
|
197
|
+
`}\n` +
|
|
198
|
+
`module.exports = wllamaModule.createWllamaCommand(${JSON.stringify(pkgMeta)}, {}, ${JSON.stringify(binName)});\n`;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const serializedMeta = JSON.stringify(pkgMeta, null, 2);
|
|
202
|
+
if (pkgMeta.repl) {
|
|
203
|
+
return `'use strict';\n` +
|
|
204
|
+
`const pkgMeta = ${serializedMeta};\n` +
|
|
205
|
+
`const ReplCommand = typeof require !== 'undefined' ? require('brsh/ReplCommand') : globalThis.ReplCommand;\n` +
|
|
206
|
+
`module.exports = ReplCommand.createReplWasmCommand(pkgMeta, {}, ${JSON.stringify(binName)});\n`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return `'use strict';\n` +
|
|
210
|
+
`const pkgMeta = ${serializedMeta};\n` +
|
|
211
|
+
`const WasmCommand = typeof require !== 'undefined' ? require('brsh/WasmCommand') : globalThis.WasmCommand;\n` +
|
|
212
|
+
`module.exports = WasmCommand.createGenericWasmCommand(pkgMeta, {}, ${JSON.stringify(binName)});\n`;
|
|
163
213
|
}
|
|
164
214
|
|
|
165
215
|
_buildCommandsForPackage(pkgMeta) {
|
|
@@ -203,36 +253,88 @@ class Wbt extends LocalCommand {
|
|
|
203
253
|
|
|
204
254
|
_registerPackageIntoContext(pkgMeta) {
|
|
205
255
|
const pathDir = this.context.getVar('PATH') || '/bin';
|
|
256
|
+
const pkgName = (pkgMeta.name || '').split('/').pop().toLowerCase();
|
|
257
|
+
const primaryBin = (pkgMeta.bin || pkgName).toLowerCase();
|
|
258
|
+
const targetDir = `/usr/lib/${pkgName}`;
|
|
206
259
|
const entries = this._buildCommandsForPackage(pkgMeta);
|
|
207
260
|
const installedBins = [];
|
|
208
261
|
|
|
262
|
+
// Check if pure wasm exists at targetDir
|
|
263
|
+
const wasmPath = pkgMeta.wasmPath || (this.context.fs.exists(`${targetDir}/${primaryBin}.wasm`) ? `${targetDir}/${primaryBin}.wasm` : null);
|
|
264
|
+
|
|
265
|
+
const metaPath = `${targetDir}/package.json`;
|
|
266
|
+
const pkgInfo = {
|
|
267
|
+
name: pkgMeta.name,
|
|
268
|
+
version: pkgMeta.version || '1.0.0',
|
|
269
|
+
bin: primaryBin,
|
|
270
|
+
aliases: pkgMeta.aliases || [],
|
|
271
|
+
description: pkgMeta.description || ''
|
|
272
|
+
};
|
|
273
|
+
this.context.fs.setFileByPath(metaPath, JSON.stringify(pkgInfo, null, 2), true);
|
|
274
|
+
|
|
209
275
|
for (const { bin, Cmd } of entries) {
|
|
210
276
|
const binName = bin.toLowerCase();
|
|
211
|
-
|
|
277
|
+
let targetFile;
|
|
278
|
+
|
|
279
|
+
if (wasmPath && (!pkgMeta.type || pkgMeta.type === 'custom-wasm' || pkgMeta.type === 'wasm')) {
|
|
280
|
+
targetFile = wasmPath;
|
|
281
|
+
} else {
|
|
282
|
+
targetFile = `${targetDir}/${binName}.js`;
|
|
283
|
+
const jsSource = this._generateJsCommandSource(pkgMeta, binName);
|
|
284
|
+
this.context.fs.setFileByPath(targetFile, jsSource, true);
|
|
285
|
+
if (!this.context.fs.isRealFilesystem && typeof Cmd === 'function') {
|
|
286
|
+
this.context.fs.setFileByPath(targetFile, Cmd, true);
|
|
287
|
+
}
|
|
288
|
+
try { this.context.fs.setMode(targetFile, 0o755); } catch { /* ignore */ }
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
this._createBinSymlink(pathDir, binName, targetFile);
|
|
212
292
|
installedBins.push(binName);
|
|
213
293
|
}
|
|
214
294
|
|
|
215
|
-
// Also register any aliases not already created by the factory
|
|
216
295
|
for (const alias of (pkgMeta.aliases || [])) {
|
|
217
296
|
const aliasLower = alias.toLowerCase();
|
|
218
297
|
if (!installedBins.includes(aliasLower) && entries.length > 0) {
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
this._installBin(pathDir, aliasLower, AliasCommand);
|
|
298
|
+
const primaryTarget = wasmPath && (!pkgMeta.type || pkgMeta.type === 'custom-wasm' || pkgMeta.type === 'wasm')
|
|
299
|
+
? wasmPath
|
|
300
|
+
: `${targetDir}/${primaryBin}.js`;
|
|
301
|
+
this._createBinSymlink(pathDir, aliasLower, primaryTarget);
|
|
225
302
|
installedBins.push(aliasLower);
|
|
226
303
|
}
|
|
227
304
|
}
|
|
228
305
|
|
|
306
|
+
const scriptMap = pkgMeta.scriptPaths || {};
|
|
307
|
+
for (const [scriptBin, scriptTarget] of Object.entries(scriptMap)) {
|
|
308
|
+
const binLower = scriptBin.toLowerCase();
|
|
309
|
+
if (!installedBins.includes(binLower) && this.context.fs.exists(scriptTarget)) {
|
|
310
|
+
this._createBinSymlink(pathDir, binLower, scriptTarget);
|
|
311
|
+
installedBins.push(binLower);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (pkgMeta.scripts && typeof pkgMeta.scripts === 'object') {
|
|
315
|
+
for (const scriptBin of Object.keys(pkgMeta.scripts)) {
|
|
316
|
+
const binLower = scriptBin.toLowerCase();
|
|
317
|
+
const fallbackTarget = `${targetDir}/${binLower}`;
|
|
318
|
+
if (!installedBins.includes(binLower) && this.context.fs.exists(fallbackTarget)) {
|
|
319
|
+
this._createBinSymlink(pathDir, binLower, fallbackTarget);
|
|
320
|
+
installedBins.push(binLower);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
229
325
|
return installedBins;
|
|
230
326
|
}
|
|
231
327
|
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
this.
|
|
235
|
-
|
|
328
|
+
_createBinSymlink(pathDir, binName, targetFile) {
|
|
329
|
+
const symlinkPath = `${pathDir}/${binName.toLowerCase()}`;
|
|
330
|
+
this._removeBin(pathDir, binName);
|
|
331
|
+
try {
|
|
332
|
+
this.context.fs.createSymlink(targetFile, symlinkPath);
|
|
333
|
+
this.context.fs.setMode(symlinkPath, 0o755);
|
|
334
|
+
} catch {
|
|
335
|
+
this.context.fs.addFiles({ [symlinkPath]: this.context.fs.getFileByPath(targetFile) });
|
|
336
|
+
try { this.context.fs.setMode(symlinkPath, 0o755); } catch { /* ignore */ }
|
|
337
|
+
}
|
|
236
338
|
}
|
|
237
339
|
|
|
238
340
|
_removeBin(pathDir, binName) {
|
|
@@ -245,21 +347,6 @@ class Wbt extends LocalCommand {
|
|
|
245
347
|
}
|
|
246
348
|
}
|
|
247
349
|
|
|
248
|
-
_restoreCommands() {
|
|
249
|
-
const state = this._readState();
|
|
250
|
-
const registry = this._getRegistry();
|
|
251
|
-
for (const [pkgName, record] of Object.entries(state.packages)) {
|
|
252
|
-
const meta = registry[pkgName] || record;
|
|
253
|
-
if (meta) {
|
|
254
|
-
try {
|
|
255
|
-
this._registerPackageIntoContext(meta);
|
|
256
|
-
} catch {
|
|
257
|
-
// ignore
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
350
|
async run() {
|
|
264
351
|
const args = [...(this.arguments || [])];
|
|
265
352
|
const isRunSubcmd = args.length >= 2 && args[0].toLowerCase() === 'run';
|
|
@@ -341,7 +428,7 @@ class Wbt extends LocalCommand {
|
|
|
341
428
|
);
|
|
342
429
|
|
|
343
430
|
const registry = this._getRegistry();
|
|
344
|
-
const
|
|
431
|
+
const installed = this._getInstalledPackages();
|
|
345
432
|
let printedHeader = false;
|
|
346
433
|
|
|
347
434
|
for (let i = 0; i < positional.length; i++) {
|
|
@@ -351,7 +438,7 @@ class Wbt extends LocalCommand {
|
|
|
351
438
|
if (!isUrl(rawName) && i + 1 < positional.length && isUrl(positional[i + 1])) {
|
|
352
439
|
const url = positional[++i];
|
|
353
440
|
const pkgName = (asName || rawName).toLowerCase();
|
|
354
|
-
await this._installCustomUrl(pkgName, url, quiet
|
|
441
|
+
await this._installCustomUrl(pkgName, url, quiet);
|
|
355
442
|
if (this.exitCode) return;
|
|
356
443
|
continue;
|
|
357
444
|
}
|
|
@@ -360,7 +447,7 @@ class Wbt extends LocalCommand {
|
|
|
360
447
|
if (isUrl(rawName)) {
|
|
361
448
|
const url = rawName;
|
|
362
449
|
const pkgName = (asName || deriveUrlBinName(url)).toLowerCase();
|
|
363
|
-
await this._installCustomUrl(pkgName, url, quiet
|
|
450
|
+
await this._installCustomUrl(pkgName, url, quiet);
|
|
364
451
|
if (this.exitCode) return;
|
|
365
452
|
continue;
|
|
366
453
|
}
|
|
@@ -396,7 +483,7 @@ class Wbt extends LocalCommand {
|
|
|
396
483
|
return;
|
|
397
484
|
}
|
|
398
485
|
|
|
399
|
-
if (
|
|
486
|
+
if (installed[pkgMeta.name] && !flags.includes('--reinstall') && !flags.includes('-f')) {
|
|
400
487
|
if (!quiet) {
|
|
401
488
|
this.stdOut = `${pkgMeta.name} is already the newest version (${pkgMeta.version}).`;
|
|
402
489
|
}
|
|
@@ -409,8 +496,10 @@ class Wbt extends LocalCommand {
|
|
|
409
496
|
}
|
|
410
497
|
|
|
411
498
|
let wasmPath = null;
|
|
499
|
+
let scriptPaths = null;
|
|
412
500
|
try {
|
|
413
501
|
wasmPath = await this._pullPackageWasm(pkgMeta, quiet, flags.includes('--preload'));
|
|
502
|
+
scriptPaths = await this._pullPackageScripts(pkgMeta, quiet);
|
|
414
503
|
} catch (err) {
|
|
415
504
|
this.stdErr = `E: Failed to fetch ${pkgMeta.name}: ${err.message || err}`;
|
|
416
505
|
this.exitCode = 1;
|
|
@@ -422,30 +511,54 @@ class Wbt extends LocalCommand {
|
|
|
422
511
|
this.flush();
|
|
423
512
|
}
|
|
424
513
|
|
|
425
|
-
const resolvedMeta = Object.assign(
|
|
514
|
+
const resolvedMeta = Object.assign(
|
|
515
|
+
{},
|
|
516
|
+
pkgMeta,
|
|
517
|
+
wasmPath ? { wasmPath } : {},
|
|
518
|
+
scriptPaths ? { scriptPaths } : {}
|
|
519
|
+
);
|
|
426
520
|
const bins = this._registerPackageIntoContext(resolvedMeta);
|
|
427
|
-
state.packages[pkgMeta.name] = {
|
|
428
|
-
name: pkgMeta.name,
|
|
429
|
-
bin: pkgMeta.bin,
|
|
430
|
-
bins,
|
|
431
|
-
aliases: pkgMeta.aliases || [],
|
|
432
|
-
version: pkgMeta.version,
|
|
433
|
-
description: pkgMeta.description,
|
|
434
|
-
cdnUrl: pkgMeta.cdnUrl,
|
|
435
|
-
wasmUrl: pkgMeta.wasmUrl,
|
|
436
|
-
wasmPath: wasmPath || pkgMeta.wasmPath,
|
|
437
|
-
type: pkgMeta.type,
|
|
438
|
-
adapter: pkgMeta.adapter,
|
|
439
|
-
repl: pkgMeta.repl,
|
|
440
|
-
installedAt: new Date().toISOString()
|
|
441
|
-
};
|
|
442
521
|
|
|
443
522
|
if (!quiet) {
|
|
444
523
|
this.stdOut = `Setting up ${pkgMeta.name} (${pkgMeta.version}) -> /bin/${bins.join(', /bin/')} ...`;
|
|
445
524
|
}
|
|
446
525
|
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
async _pullPackageScripts(pkgMeta, quiet = false, targetDir = null) {
|
|
529
|
+
if (!pkgMeta || !pkgMeta.scripts || typeof pkgMeta.scripts !== 'object') {
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
const cfg = this.constructor._config || {};
|
|
533
|
+
const fetchFn = cfg.fetch || (typeof fetch === 'function' ? fetch : null);
|
|
534
|
+
if (!fetchFn) return null;
|
|
535
|
+
|
|
536
|
+
const pkgName = (pkgMeta.name || '').split('/').pop().toLowerCase();
|
|
537
|
+
const destDir = targetDir || `/usr/lib/${pkgName}`;
|
|
538
|
+
const installed = {};
|
|
539
|
+
|
|
540
|
+
for (const [scriptName, scriptUrl] of Object.entries(pkgMeta.scripts)) {
|
|
541
|
+
if (!scriptUrl) continue;
|
|
542
|
+
const binLower = scriptName.toLowerCase();
|
|
543
|
+
const res = await fetchFn(scriptUrl);
|
|
544
|
+
if (!res || !res.ok) {
|
|
545
|
+
throw new Error(`HTTP ${res ? res.status : 'error'} fetching script ${scriptUrl}`);
|
|
546
|
+
}
|
|
547
|
+
let text;
|
|
548
|
+
if (typeof res.text === 'function') {
|
|
549
|
+
text = await res.text();
|
|
550
|
+
} else if (typeof res.arrayBuffer === 'function') {
|
|
551
|
+
text = new TextDecoder().decode(new Uint8Array(await res.arrayBuffer()));
|
|
552
|
+
} else {
|
|
553
|
+
text = '';
|
|
554
|
+
}
|
|
555
|
+
const targetPath = `${destDir}/${binLower}`;
|
|
556
|
+
this.context.fs._setFileRaw(targetPath, text, true);
|
|
557
|
+
try { this.context.fs.setMode(targetPath, 0o755); } catch { /* ignore */ }
|
|
558
|
+
installed[binLower] = targetPath;
|
|
559
|
+
}
|
|
447
560
|
|
|
448
|
-
|
|
561
|
+
return Object.keys(installed).length > 0 ? installed : null;
|
|
449
562
|
}
|
|
450
563
|
|
|
451
564
|
async _readResponseBytes(res, label, quiet = false) {
|
|
@@ -580,7 +693,7 @@ class Wbt extends LocalCommand {
|
|
|
580
693
|
return null;
|
|
581
694
|
}
|
|
582
695
|
|
|
583
|
-
async _pullPackageWasm(pkgMeta, quiet = false, requireModuleLoad = false) {
|
|
696
|
+
async _pullPackageWasm(pkgMeta, quiet = false, requireModuleLoad = false, targetDir = null) {
|
|
584
697
|
const cfg = this.constructor._config || {};
|
|
585
698
|
const fetchFn = cfg.fetch || (typeof fetch === 'function' ? fetch : null);
|
|
586
699
|
const { loadModule } = require('./utils');
|
|
@@ -681,16 +794,19 @@ class Wbt extends LocalCommand {
|
|
|
681
794
|
}
|
|
682
795
|
|
|
683
796
|
if (wasmBytes) {
|
|
684
|
-
const
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
797
|
+
const pkgName = (pkgMeta.name || '').split('/').pop().toLowerCase();
|
|
798
|
+
const binName = (pkgMeta.bin || pkgName).toLowerCase();
|
|
799
|
+
const destDir = targetDir || `/usr/lib/${pkgName}`;
|
|
800
|
+
const targetPath = `${destDir}/${binName}.wasm`;
|
|
801
|
+
writeBytesToFs(this.context.fs, targetPath, wasmBytes, 'application/wasm');
|
|
802
|
+
try { this.context.fs.setMode(targetPath, 0o755); } catch { /* ignore */ }
|
|
803
|
+
return targetPath;
|
|
688
804
|
}
|
|
689
805
|
|
|
690
806
|
return null;
|
|
691
807
|
}
|
|
692
808
|
|
|
693
|
-
async _installCustomUrl(pkgName, url, quiet = false
|
|
809
|
+
async _installCustomUrl(pkgName, url, quiet = false) {
|
|
694
810
|
// If URL is an ESM JS module (.mjs / .js or esm.sh URL without .wasm/.wat), wrap with GenericWasmCommand
|
|
695
811
|
const isBinaryWasmOrWat = /\.(wasm|wat)(\?.*)?$/i.test(url);
|
|
696
812
|
if (!isBinaryWasmOrWat && (/^https?:\/\/esm\.sh\//i.test(url) || /\.(mjs|js)(\?.*)?$/i.test(url))) {
|
|
@@ -707,9 +823,6 @@ class Wbt extends LocalCommand {
|
|
|
707
823
|
const wasmPath = await this._pullPackageWasm(pkgMeta, quiet, false);
|
|
708
824
|
if (wasmPath) pkgMeta.wasmPath = wasmPath;
|
|
709
825
|
this._registerPackageIntoContext(pkgMeta);
|
|
710
|
-
const state = existingState || this._readState();
|
|
711
|
-
state.packages[pkgName] = pkgMeta;
|
|
712
|
-
this._writeState(state);
|
|
713
826
|
if (!quiet) {
|
|
714
827
|
this.stdOut = `Setting up ${pkgName} (generic runtime) -> /bin/${pkgName} ...`;
|
|
715
828
|
}
|
|
@@ -739,8 +852,6 @@ class Wbt extends LocalCommand {
|
|
|
739
852
|
try {
|
|
740
853
|
res = await fetchFn(normalizedUrl);
|
|
741
854
|
} catch (err) {
|
|
742
|
-
// GitHub `/releases/download/...` redirects to objects.githubusercontent.com without CORS headers in browsers;
|
|
743
|
-
// fall back to a CORS proxy if direct browser fetch throws a network/CORS TypeError
|
|
744
855
|
if (/^https?:\/\/github\.com\/[^/]+\/[^/]+\/releases\/download\//i.test(normalizedUrl)) {
|
|
745
856
|
try {
|
|
746
857
|
res = await fetchFn(`https://corsproxy.io/?${encodeURIComponent(normalizedUrl)}`);
|
|
@@ -764,7 +875,8 @@ class Wbt extends LocalCommand {
|
|
|
764
875
|
|
|
765
876
|
const isWat = url.endsWith('.wat');
|
|
766
877
|
const ext = isWat ? '.wat' : '.wasm';
|
|
767
|
-
const
|
|
878
|
+
const targetDir = `/usr/lib/${pkgName}`;
|
|
879
|
+
const wasmPath = `${targetDir}/${pkgName}${ext}`;
|
|
768
880
|
|
|
769
881
|
if (isWat) {
|
|
770
882
|
const text = await res.text();
|
|
@@ -788,9 +900,6 @@ class Wbt extends LocalCommand {
|
|
|
788
900
|
};
|
|
789
901
|
|
|
790
902
|
this._registerPackageIntoContext(pkgMeta);
|
|
791
|
-
const state = existingState || this._readState();
|
|
792
|
-
state.packages[pkgName] = pkgMeta;
|
|
793
|
-
this._writeState(state);
|
|
794
903
|
|
|
795
904
|
if (!quiet) {
|
|
796
905
|
this.stdOut = `Setting up ${pkgName} (custom) -> /bin/${pkgName} ...`;
|
|
@@ -805,12 +914,13 @@ class Wbt extends LocalCommand {
|
|
|
805
914
|
return;
|
|
806
915
|
}
|
|
807
916
|
|
|
808
|
-
const
|
|
917
|
+
const installed = this._getInstalledPackages();
|
|
918
|
+
const registry = this._getRegistry();
|
|
809
919
|
const pathDir = this.context.getVar('PATH') || '/bin';
|
|
810
920
|
|
|
811
921
|
for (const rawName of positional) {
|
|
812
922
|
const pkgName = resolvePackageName(rawName);
|
|
813
|
-
const record =
|
|
923
|
+
const record = installed[pkgName] || installed[rawName.toLowerCase()];
|
|
814
924
|
|
|
815
925
|
if (!record) {
|
|
816
926
|
this.stdErr = `E: Package '${rawName}' is not installed, so not removed`;
|
|
@@ -818,36 +928,46 @@ class Wbt extends LocalCommand {
|
|
|
818
928
|
return;
|
|
819
929
|
}
|
|
820
930
|
|
|
821
|
-
const
|
|
931
|
+
const reg = registry[pkgName] || {};
|
|
932
|
+
const binsToRemove = new Set([
|
|
933
|
+
record.bin || record.name,
|
|
934
|
+
...(record.aliases || []),
|
|
935
|
+
...(reg.aliases || []),
|
|
936
|
+
rawName.toLowerCase(),
|
|
937
|
+
pkgName
|
|
938
|
+
]);
|
|
939
|
+
if (record.scripts) {
|
|
940
|
+
for (const scriptKey of Object.keys(record.scripts)) {
|
|
941
|
+
binsToRemove.add(scriptKey);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
822
945
|
for (const b of binsToRemove) {
|
|
823
946
|
this._removeBin(pathDir, b);
|
|
824
947
|
}
|
|
825
948
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
}
|
|
949
|
+
// Remove /usr/lib/<pkgName>
|
|
950
|
+
const pkgDir = `/usr/lib/${pkgName}`;
|
|
951
|
+
try { this.context.fs.deleteFileByPath(pkgDir); } catch { /* ignore */ }
|
|
829
952
|
|
|
830
|
-
delete state.packages[record.name];
|
|
831
953
|
this.stdOut = `Removing ${record.name} (${record.version}) ...`;
|
|
832
954
|
}
|
|
833
|
-
|
|
834
|
-
this._writeState(state);
|
|
835
955
|
}
|
|
836
956
|
|
|
837
957
|
_handleList(args) {
|
|
838
958
|
const installedOnly = args.includes('--installed') || args.includes('-i');
|
|
839
959
|
const registry = this._getRegistry();
|
|
840
|
-
const
|
|
960
|
+
const installed = this._getInstalledPackages();
|
|
841
961
|
|
|
842
962
|
this.stdOut = 'Listing...';
|
|
843
963
|
|
|
844
964
|
const names = installedOnly
|
|
845
|
-
? Object.keys(
|
|
846
|
-
: Array.from(new Set([...Object.keys(registry), ...Object.keys(
|
|
965
|
+
? Object.keys(installed).sort()
|
|
966
|
+
: Array.from(new Set([...Object.keys(registry), ...Object.keys(installed)])).sort();
|
|
847
967
|
|
|
848
968
|
for (const name of names) {
|
|
849
969
|
const reg = registry[name];
|
|
850
|
-
const inst =
|
|
970
|
+
const inst = installed[name];
|
|
851
971
|
const meta = inst || reg;
|
|
852
972
|
if (!meta) continue;
|
|
853
973
|
|
|
@@ -860,7 +980,7 @@ class Wbt extends LocalCommand {
|
|
|
860
980
|
_handleSearch(args) {
|
|
861
981
|
const query = args.filter(a => !a.startsWith('-')).join(' ').toLowerCase();
|
|
862
982
|
const registry = this._getRegistry();
|
|
863
|
-
const
|
|
983
|
+
const installed = this._getInstalledPackages();
|
|
864
984
|
|
|
865
985
|
this.stdOut = 'Sorting... Done';
|
|
866
986
|
this.stdOut = 'Full Text Search... Done';
|
|
@@ -871,13 +991,14 @@ class Wbt extends LocalCommand {
|
|
|
871
991
|
pkg.name.toLowerCase().includes(query) ||
|
|
872
992
|
(pkg.bin && pkg.bin.toLowerCase().includes(query)) ||
|
|
873
993
|
(pkg.aliases && pkg.aliases.some(a => a.toLowerCase().includes(query))) ||
|
|
994
|
+
(pkg.scripts && Object.keys(pkg.scripts).some(s => s.toLowerCase().includes(query))) ||
|
|
874
995
|
(pkg.description && pkg.description.toLowerCase().includes(query)) ||
|
|
875
996
|
(pkg.details && pkg.details.toLowerCase().includes(query))
|
|
876
997
|
);
|
|
877
998
|
});
|
|
878
999
|
|
|
879
1000
|
for (const pkg of entries) {
|
|
880
|
-
const inst =
|
|
1001
|
+
const inst = installed[pkg.name] ? ' [installed]' : '';
|
|
881
1002
|
this.stdOut = `${pkg.name}/wasm ${pkg.version} wasm${inst}`;
|
|
882
1003
|
this.stdOut = ` ${pkg.description}`;
|
|
883
1004
|
}
|
|
@@ -893,8 +1014,8 @@ class Wbt extends LocalCommand {
|
|
|
893
1014
|
|
|
894
1015
|
const pkgName = resolvePackageName(rawName);
|
|
895
1016
|
const registry = this._getRegistry();
|
|
896
|
-
const
|
|
897
|
-
const meta =
|
|
1017
|
+
const installed = this._getInstalledPackages();
|
|
1018
|
+
const meta = installed[pkgName] || registry[pkgName];
|
|
898
1019
|
|
|
899
1020
|
if (!meta) {
|
|
900
1021
|
this.stdErr = `E: No packages found matching ${rawName}`;
|
|
@@ -903,7 +1024,7 @@ class Wbt extends LocalCommand {
|
|
|
903
1024
|
}
|
|
904
1025
|
|
|
905
1026
|
const full = Object.assign({}, registry[pkgName] || {}, meta);
|
|
906
|
-
const isInstalled = Boolean(
|
|
1027
|
+
const isInstalled = Boolean(installed[full.name]);
|
|
907
1028
|
|
|
908
1029
|
this.stdOut = `Package: ${full.name}`;
|
|
909
1030
|
this.stdOut = `Version: ${full.version}`;
|
|
@@ -912,6 +1033,9 @@ class Wbt extends LocalCommand {
|
|
|
912
1033
|
if (full.aliases && full.aliases.length > 0) {
|
|
913
1034
|
this.stdOut = `Aliases: ${full.aliases.join(', ')}`;
|
|
914
1035
|
}
|
|
1036
|
+
if (full.scripts && Object.keys(full.scripts).length > 0) {
|
|
1037
|
+
this.stdOut = `Scripts: ${Object.keys(full.scripts).join(', ')}`;
|
|
1038
|
+
}
|
|
915
1039
|
if (full.size) {
|
|
916
1040
|
this.stdOut = `Download-Size: ${full.size}`;
|
|
917
1041
|
}
|
|
@@ -961,9 +1085,6 @@ class Wbt extends LocalCommand {
|
|
|
961
1085
|
this.context.fs._setFileRaw(`${CACHE_DIR}/remote-registry.json`, JSON.stringify(mergedRemote, null, 2), true);
|
|
962
1086
|
}
|
|
963
1087
|
|
|
964
|
-
// Also ensure any installed packages in state are registered in PATH
|
|
965
|
-
this._restoreCommands();
|
|
966
|
-
|
|
967
1088
|
const registry = this._getRegistry();
|
|
968
1089
|
this.stdOut = `Reading package lists... Done (${Object.keys(registry).length} packages available)`;
|
|
969
1090
|
}
|
|
@@ -977,8 +1098,9 @@ class Wbt extends LocalCommand {
|
|
|
977
1098
|
const rawName = args.shift();
|
|
978
1099
|
const pkgName = resolvePackageName(rawName);
|
|
979
1100
|
const registry = this._getRegistry();
|
|
980
|
-
const
|
|
981
|
-
|
|
1101
|
+
const installed = this._getInstalledPackages();
|
|
1102
|
+
const isInstalled = Boolean(installed[pkgName]);
|
|
1103
|
+
let pkgMeta = installed[pkgName] || registry[pkgName];
|
|
982
1104
|
|
|
983
1105
|
if (!pkgMeta && (rawName.startsWith('npm:') || rawName.startsWith('esm:'))) {
|
|
984
1106
|
const cleanSpec = rawName.replace(/^(npm|esm):/i, '');
|
|
@@ -998,66 +1120,127 @@ class Wbt extends LocalCommand {
|
|
|
998
1120
|
return;
|
|
999
1121
|
}
|
|
1000
1122
|
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1123
|
+
let tempDir = null;
|
|
1124
|
+
const origPath = this.context.getVar('PATH') || '/bin';
|
|
1125
|
+
let modifiedPath = false;
|
|
1126
|
+
try {
|
|
1127
|
+
if (!isInstalled) {
|
|
1128
|
+
tempDir = `/tmp/.wbt-run-${pkgName}-${Date.now()}`;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
if ((pkgMeta.type === 'custom-wasm' || pkgMeta.type === 'wasm') && !pkgMeta.wasmPath) {
|
|
1132
|
+
try {
|
|
1133
|
+
const wasmPath = await this._pullPackageWasm(pkgMeta, true, false, tempDir);
|
|
1134
|
+
if (wasmPath) {
|
|
1135
|
+
pkgMeta = Object.assign({}, pkgMeta, { wasmPath });
|
|
1136
|
+
}
|
|
1137
|
+
} catch (err) {
|
|
1138
|
+
this.stdErr = `E: Failed to fetch ${pkgMeta.name}: ${err.message || err}`;
|
|
1139
|
+
this.exitCode = 1;
|
|
1140
|
+
return;
|
|
1006
1141
|
}
|
|
1007
|
-
} catch (err) {
|
|
1008
|
-
this.stdErr = `E: Failed to fetch ${pkgMeta.name}: ${err.message || err}`;
|
|
1009
|
-
this.exitCode = 1;
|
|
1010
|
-
return;
|
|
1011
1142
|
}
|
|
1012
|
-
}
|
|
1013
1143
|
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1144
|
+
const rawLower = rawName.toLowerCase();
|
|
1145
|
+
let instance;
|
|
1146
|
+
if (pkgMeta.scripts && pkgMeta.scripts[rawLower]) {
|
|
1147
|
+
let scriptPath = (pkgMeta.scriptPaths && pkgMeta.scriptPaths[rawLower]) || `/usr/lib/${pkgName}/${rawLower}`;
|
|
1148
|
+
if (!this.context.fs.exists(scriptPath)) {
|
|
1149
|
+
try {
|
|
1150
|
+
const pulledScripts = await this._pullPackageScripts(pkgMeta, true, tempDir);
|
|
1151
|
+
if (pulledScripts && pulledScripts[rawLower]) {
|
|
1152
|
+
scriptPath = pulledScripts[rawLower];
|
|
1153
|
+
}
|
|
1154
|
+
} catch (err) {
|
|
1155
|
+
this.stdErr = `E: Failed to fetch script ${rawLower}: ${err.message || err}`;
|
|
1156
|
+
this.exitCode = 1;
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
if (tempDir) {
|
|
1161
|
+
this.context.setVar('PATH', `${tempDir}:${origPath}`);
|
|
1162
|
+
modifiedPath = true;
|
|
1163
|
+
}
|
|
1164
|
+
const ScriptCommand = require('../ScriptCommand');
|
|
1165
|
+
const scriptContent = this.context.fs.getFileByPath(scriptPath);
|
|
1166
|
+
instance = new ScriptCommand(scriptContent, args, this.context);
|
|
1167
|
+
} else {
|
|
1168
|
+
const entries = this._buildCommandsForPackage(pkgMeta);
|
|
1169
|
+
// Find matching entry if user specified e.g. `wbt run asrun` or `wbt run wasm2wat`
|
|
1170
|
+
const matched = entries.find(e => e.bin === rawLower) || entries[0];
|
|
1171
|
+
const Cmd = matched.Cmd;
|
|
1172
|
+
instance = new Cmd(args, this.context);
|
|
1173
|
+
}
|
|
1018
1174
|
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
}
|
|
1175
|
+
if (instance.requiresFilesystem) {
|
|
1176
|
+
instance.fs = this.context.fs.sandbox();
|
|
1177
|
+
}
|
|
1023
1178
|
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1179
|
+
// Pipe I/O and forward interactivity
|
|
1180
|
+
this._activeSubCommand = instance;
|
|
1181
|
+
instance.stdinStream = this.stdinStream;
|
|
1182
|
+
instance.stdin = (this.stdin && !this.stdin.isTTY) ? this.stdin : this.stdinStream;
|
|
1028
1183
|
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1184
|
+
const onStdout = (chunk) => {
|
|
1185
|
+
if (this.stdout && typeof this.stdout.write === 'function') this.stdout.write(chunk);
|
|
1186
|
+
};
|
|
1187
|
+
const onStderr = (chunk) => {
|
|
1188
|
+
if (this.stderr && typeof this.stderr.write === 'function') this.stderr.write(chunk);
|
|
1189
|
+
};
|
|
1190
|
+
const onFlush = (flushed) => {
|
|
1191
|
+
const lines = Array.isArray(flushed)
|
|
1192
|
+
? flushed
|
|
1193
|
+
: (flushed && typeof flushed.getStdOutput === 'function' ? flushed.getStdOutput() : []);
|
|
1194
|
+
for (const line of lines) {
|
|
1195
|
+
if (line.type === 'out') this.stdOut = line.string;
|
|
1196
|
+
else if (line.type === 'err') this.stdErr = line.string;
|
|
1197
|
+
}
|
|
1198
|
+
this.flush();
|
|
1199
|
+
};
|
|
1200
|
+
if (instance.stdout && typeof instance.stdout.on === 'function') {
|
|
1201
|
+
instance.stdout.on('data', onStdout);
|
|
1202
|
+
}
|
|
1203
|
+
if (instance.stderr && typeof instance.stderr.on === 'function') {
|
|
1204
|
+
instance.stderr.on('data', onStderr);
|
|
1205
|
+
}
|
|
1206
|
+
if (typeof instance.on === 'function') {
|
|
1207
|
+
instance.on('flush', onFlush);
|
|
1208
|
+
}
|
|
1041
1209
|
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1210
|
+
let res;
|
|
1211
|
+
try {
|
|
1212
|
+
res = await instance.runCommand();
|
|
1213
|
+
} finally {
|
|
1214
|
+
this._activeSubCommand = null;
|
|
1215
|
+
if (instance.stdout && typeof instance.stdout.off === 'function') {
|
|
1216
|
+
instance.stdout.off('data', onStdout);
|
|
1217
|
+
}
|
|
1218
|
+
if (instance.stderr && typeof instance.stderr.off === 'function') {
|
|
1219
|
+
instance.stderr.off('data', onStderr);
|
|
1220
|
+
}
|
|
1221
|
+
if (typeof instance.removeListener === 'function') {
|
|
1222
|
+
instance.removeListener('flush', onFlush);
|
|
1223
|
+
}
|
|
1049
1224
|
}
|
|
1050
|
-
if (instance.
|
|
1051
|
-
|
|
1225
|
+
if (!instance.interactive && res) {
|
|
1226
|
+
for (const line of res.getStdOutput()) {
|
|
1227
|
+
if (line.type === 'out') this.stdOut = line.string;
|
|
1228
|
+
else if (line.type === 'err') this.stdErr = line.string;
|
|
1229
|
+
}
|
|
1052
1230
|
}
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1231
|
+
this.exitCode = res ? res.getExitCode() : 0;
|
|
1232
|
+
} finally {
|
|
1233
|
+
if (modifiedPath) {
|
|
1234
|
+
this.context.setVar('PATH', origPath);
|
|
1235
|
+
}
|
|
1236
|
+
if (tempDir) {
|
|
1237
|
+
try {
|
|
1238
|
+
this.context.fs.deleteFileByPath(tempDir);
|
|
1239
|
+
} catch {
|
|
1240
|
+
// ignore
|
|
1241
|
+
}
|
|
1058
1242
|
}
|
|
1059
1243
|
}
|
|
1060
|
-
this.exitCode = res ? res.getExitCode() : 0;
|
|
1061
1244
|
}
|
|
1062
1245
|
|
|
1063
1246
|
_printHelp() {
|
|
@@ -1086,7 +1269,8 @@ class Wbt extends LocalCommand {
|
|
|
1086
1269
|
' quickjs Sandboxed QuickJS JavaScript engine (qjs)',
|
|
1087
1270
|
' lua Lua 5.4 WebAssembly interpreter (lua)',
|
|
1088
1271
|
' wllama WASM & WebGPU LLM inference engine (wllama, llama, llama-cli)',
|
|
1089
|
-
' nano GNU nano 8.0 text editor with ncurses (nano, nano-wasm)'
|
|
1272
|
+
' nano GNU nano 8.0 text editor with ncurses (nano, nano-wasm)',
|
|
1273
|
+
' doom Classic Doom (1993) with 24-bit ANSI rendering (doom, freedoom)'
|
|
1090
1274
|
].forEach(l => { this.stdOut = l; });
|
|
1091
1275
|
}
|
|
1092
1276
|
}
|