@innovatorved/code-lab 0.1.3 → 0.1.4
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/README.md +1 -1
- package/dist/code-lab.d.ts +2 -1
- package/dist/code-lab.esm.js +325 -41
- package/dist/code-lab.js +325 -41
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.esm.js +48 -3
- package/dist/runtime.js +48 -3
- package/dist/shell.esm.js +257 -32
- package/dist/shell.js +257 -32
- package/package.json +1 -1
- package/src/runtime.js +48 -3
- package/src/runtime.ts +53 -4
- package/src/shell.js +257 -32
- package/src/shell.ts +257 -32
- package/src/workbench.js +20 -6
- package/src/workbench.ts +20 -6
package/dist/shell.js
CHANGED
|
@@ -2355,38 +2355,221 @@
|
|
|
2355
2355
|
} catch (_) {}
|
|
2356
2356
|
}
|
|
2357
2357
|
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2358
|
+
// ---------------------------------------------------------------------------
|
|
2359
|
+
// Real npm package installation via the jsDelivr CDN
|
|
2360
|
+
// ---------------------------------------------------------------------------
|
|
2361
|
+
const CDN_NPM_BASE = 'https://cdn.jsdelivr.net/npm';
|
|
2362
|
+
|
|
2363
|
+
// Minimal offline registry, used only when the CDN is unreachable so common
|
|
2364
|
+
// demos still resolve without network access.
|
|
2365
|
+
const OFFLINE_PACKAGE_FALLBACKS = {
|
|
2366
|
+
'is-even': 'module.exports = function isEven(n) { return (Number(n) % 2) === 0; };\n',
|
|
2367
|
+
'tiny-invariant': 'module.exports = function invariant(condition, message) { if (!condition) throw new Error(message || "Invariant failed"); };\n',
|
|
2368
|
+
'lodash': 'module.exports = { isArray: Array.isArray, isObject: function (v) { return v !== null && typeof v === "object"; } };\n',
|
|
2369
|
+
'left-pad': 'module.exports = function leftPad(str, len, ch) { str = String(str); ch = (ch === undefined ? " " : String(ch)); while (str.length < len) str = ch + str; return str.slice(-len); };\n'
|
|
2370
|
+
};
|
|
2371
|
+
|
|
2372
|
+
function packageCtxFetch(ctx) {
|
|
2373
|
+
if (ctx && ctx.shell && ctx.shell.options && typeof ctx.shell.options.fetchImpl === 'function') {
|
|
2374
|
+
return ctx.shell.options.fetchImpl;
|
|
2375
|
+
}
|
|
2376
|
+
if (typeof fetch === 'function') return fetch.bind(globalThis);
|
|
2377
|
+
return null;
|
|
2378
|
+
}
|
|
2379
|
+
|
|
2380
|
+
function parsePackageSpec(spec) {
|
|
2381
|
+
const raw = String(spec || '').trim();
|
|
2382
|
+
if (!raw) return { name: '', range: null };
|
|
2383
|
+
if (raw.startsWith('@')) {
|
|
2384
|
+
const at = raw.indexOf('@', 1);
|
|
2385
|
+
if (at === -1) return { name: raw, range: null };
|
|
2386
|
+
return { name: raw.slice(0, at), range: raw.slice(at + 1) || null };
|
|
2387
|
+
}
|
|
2388
|
+
const at = raw.indexOf('@');
|
|
2389
|
+
if (at <= 0) return { name: raw, range: null };
|
|
2390
|
+
return { name: raw.slice(0, at), range: raw.slice(at + 1) || null };
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
function resolveEntryRelPath(pkgJson) {
|
|
2394
|
+
let entry = pkgJson.main || pkgJson.module ||
|
|
2395
|
+
(typeof pkgJson.browser === 'string' ? pkgJson.browser : '') || 'index.js';
|
|
2396
|
+
entry = String(entry).replace(/^[./]+/, '');
|
|
2397
|
+
return entry || 'index.js';
|
|
2398
|
+
}
|
|
2399
|
+
|
|
2400
|
+
function stripRelPath(rel) {
|
|
2401
|
+
return String(rel || '').replace(/^[./]+/, '');
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
function extractRelativeSpecifiers(code) {
|
|
2405
|
+
const out = new Set();
|
|
2406
|
+
const re = /(?:\brequire\s*\(\s*|\bimport\s+[\s\S]{0,120}?\bfrom\s*|\bimport\s*\(\s*)['"](\.{1,2}\/[^'"]+)['"]/g;
|
|
2407
|
+
let m;
|
|
2408
|
+
while ((m = re.exec(code)) !== null) out.add(m[1]);
|
|
2409
|
+
return Array.from(out);
|
|
2410
|
+
}
|
|
2411
|
+
|
|
2412
|
+
function normalizeRelPath(baseDir, rel) {
|
|
2413
|
+
const parts = String(baseDir || '').split('/').filter(Boolean);
|
|
2414
|
+
for (const seg of String(rel).split('/')) {
|
|
2415
|
+
if (!seg || seg === '.') continue;
|
|
2416
|
+
if (seg === '..') parts.pop();
|
|
2417
|
+
else parts.push(seg);
|
|
2418
|
+
}
|
|
2419
|
+
return parts.join('/');
|
|
2420
|
+
}
|
|
2421
|
+
|
|
2422
|
+
async function fetchTextFromCdn(fetchImpl, url) {
|
|
2423
|
+
try {
|
|
2424
|
+
const res = await fetchImpl(url);
|
|
2425
|
+
if (!res) return null;
|
|
2426
|
+
const ok = (res.ok === true) || res.status === 200 || res.status === undefined;
|
|
2427
|
+
if (!ok) return null;
|
|
2428
|
+
if (typeof res.text === 'function') return await res.text();
|
|
2429
|
+
return String(res.body != null ? res.body : '');
|
|
2430
|
+
} catch (_) {
|
|
2431
|
+
return null;
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
async function resolvePackageMetadata(fetchImpl, name, range) {
|
|
2436
|
+
const urls = range
|
|
2437
|
+
? [`${CDN_NPM_BASE}/${name}@${range}/package.json`, `${CDN_NPM_BASE}/${name}/package.json`]
|
|
2438
|
+
: [`${CDN_NPM_BASE}/${name}/package.json`];
|
|
2439
|
+
for (const url of urls) {
|
|
2440
|
+
const text = await fetchTextFromCdn(fetchImpl, url);
|
|
2441
|
+
if (!text) continue;
|
|
2442
|
+
try {
|
|
2443
|
+
const parsed = JSON.parse(text);
|
|
2444
|
+
if (parsed && parsed.version) return parsed;
|
|
2445
|
+
} catch (_) {}
|
|
2446
|
+
}
|
|
2447
|
+
return null;
|
|
2448
|
+
}
|
|
2449
|
+
|
|
2450
|
+
// Download a package's entry module, its relative submodules and record its
|
|
2451
|
+
// declared dependencies so the caller can resolve them transitively.
|
|
2452
|
+
async function downloadPackageFiles(fetchImpl, name, range, seen) {
|
|
2453
|
+
const meta = await resolvePackageMetadata(fetchImpl, name, range);
|
|
2454
|
+
if (!meta) {
|
|
2455
|
+
const fallback = OFFLINE_PACKAGE_FALLBACKS[name];
|
|
2456
|
+
if (fallback) {
|
|
2457
|
+
const v = range ? range.replace(/^[\^~>=<\s]+/, '') || '1.0.0' : '1.0.0';
|
|
2458
|
+
return { ok: true, offline: true, version: v, entry: 'index.js', files: [{ rel: 'index.js', content: fallback }], deps: [] };
|
|
2459
|
+
}
|
|
2460
|
+
return { ok: false, reason: `${name}: package metadata not found on the registry (offline?)` };
|
|
2461
|
+
}
|
|
2462
|
+
|
|
2463
|
+
const version = meta.version;
|
|
2464
|
+
const key = `${name}@${version}`;
|
|
2465
|
+
if (seen.has(key)) {
|
|
2466
|
+
return { ok: true, deduped: true, version, entry: resolveEntryRelPath(meta), files: [], deps: [] };
|
|
2467
|
+
}
|
|
2468
|
+
seen.add(key);
|
|
2469
|
+
|
|
2470
|
+
const entry = resolveEntryRelPath(meta);
|
|
2471
|
+
const files = [];
|
|
2472
|
+
const visited = new Set();
|
|
2473
|
+
const queue = [entry];
|
|
2474
|
+
while (queue.length > 0) {
|
|
2475
|
+
const rel = stripRelPath(queue.shift());
|
|
2476
|
+
if (!rel || visited.has(rel)) continue;
|
|
2477
|
+
visited.add(rel);
|
|
2478
|
+
const text = await fetchTextFromCdn(fetchImpl, `${CDN_NPM_BASE}/${name}@${version}/${rel}`);
|
|
2479
|
+
if (text === null) continue;
|
|
2480
|
+
files.push({ rel, content: text });
|
|
2481
|
+
const baseDir = rel.includes('/') ? rel.slice(0, rel.lastIndexOf('/')) : '';
|
|
2482
|
+
for (const spec of extractRelativeSpecifiers(text)) {
|
|
2483
|
+
const target = normalizeRelPath(baseDir, spec);
|
|
2484
|
+
for (const cand of [target, `${target}.js`, `${target}.json`, `${target}/index.js`]) {
|
|
2485
|
+
const c = stripRelPath(cand);
|
|
2486
|
+
if (c && !visited.has(c)) { queue.push(cand); break; }
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
}
|
|
2490
|
+
// The declared entry may be missing on the CDN; fall back to index.js.
|
|
2491
|
+
if (!files.some(f => f.rel === entry)) {
|
|
2492
|
+
const idxText = await fetchTextFromCdn(fetchImpl, `${CDN_NPM_BASE}/${name}@${version}/index.js`);
|
|
2493
|
+
if (idxText !== null) files.push({ rel: 'index.js', content: idxText });
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2496
|
+
return { ok: true, version, entry, files, deps: Object.keys(meta.dependencies || {}) };
|
|
2497
|
+
}
|
|
2498
|
+
|
|
2499
|
+
function installTargetDirs(ctx, name) {
|
|
2500
|
+
const dirs = ['/workspace/node_modules/' + name];
|
|
2501
|
+
const cwdTarget = PosixPath.join(ctx.cwd || '/workspace', 'node_modules', name);
|
|
2502
|
+
if (!dirs.includes(cwdTarget)) dirs.push(cwdTarget);
|
|
2503
|
+
return dirs;
|
|
2504
|
+
}
|
|
2505
|
+
|
|
2506
|
+
function writeInstalledPackageFiles(ctx, dirs, name, info) {
|
|
2507
|
+
for (const dir of dirs) {
|
|
2508
|
+
ensureDirSync(ctx.vfs, dir, ctx.emitMutation);
|
|
2509
|
+
for (const f of info.files) {
|
|
2510
|
+
const full = PosixPath.join(dir, f.rel);
|
|
2511
|
+
ensureDirSync(ctx.vfs, PosixPath.dirname(full), ctx.emitMutation);
|
|
2512
|
+
ctx.vfs.writeFileSync(full, f.content);
|
|
2513
|
+
if (typeof ctx.emitMutation === 'function') ctx.emitMutation('create', full);
|
|
2514
|
+
}
|
|
2515
|
+
const modPkgPath = PosixPath.join(dir, 'package.json');
|
|
2516
|
+
const modPkg = { name, version: info.version || '1.0.0', main: './' + (info.entry || 'index.js') };
|
|
2517
|
+
ctx.vfs.writeFileSync(modPkgPath, JSON.stringify(modPkg, null, 2) + '\n');
|
|
2518
|
+
if (typeof ctx.emitMutation === 'function') ctx.emitMutation('create', modPkgPath);
|
|
2519
|
+
}
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
async function installPackage(pkgSpec, ctx) {
|
|
2523
|
+
const { name: pkgName, range } = parsePackageSpec(pkgSpec);
|
|
2524
|
+
if (!pkgName) return { name: null, ok: false, installed: [], warning: null };
|
|
2362
2525
|
|
|
2363
2526
|
const { path: pkgPath, data: pkgData } = readPackageJson(ctx.vfs, ctx.cwd);
|
|
2364
2527
|
if (!pkgData.dependencies) pkgData.dependencies = {};
|
|
2365
|
-
pkgData.dependencies[pkgName] = pkgVersion;
|
|
2366
|
-
writePackageJson(ctx.vfs, pkgPath, pkgData, ctx.emitMutation);
|
|
2367
2528
|
|
|
2368
|
-
const
|
|
2369
|
-
const
|
|
2370
|
-
|
|
2529
|
+
const fetchImpl = packageCtxFetch(ctx);
|
|
2530
|
+
const seen = (ctx.shell && ctx.shell._cdnSeen)
|
|
2531
|
+
? ctx.shell._cdnSeen
|
|
2532
|
+
: (ctx.shell ? (ctx.shell._cdnSeen = new Set()) : new Set());
|
|
2533
|
+
|
|
2534
|
+
const queue = [{ name: pkgName, range, direct: true }];
|
|
2535
|
+
const requested = new Set([pkgName]);
|
|
2536
|
+
const installed = [];
|
|
2537
|
+
let warning = null;
|
|
2538
|
+
let directVersion = null;
|
|
2539
|
+
|
|
2540
|
+
while (queue.length > 0) {
|
|
2541
|
+
const item = queue.shift();
|
|
2542
|
+
const info = fetchImpl
|
|
2543
|
+
? await downloadPackageFiles(fetchImpl, item.name, item.range, seen)
|
|
2544
|
+
: { ok: false, reason: `${item.name}: no fetch implementation available` };
|
|
2545
|
+
|
|
2546
|
+
if (!info.ok) {
|
|
2547
|
+
// Final fallback: an empty stub so offline demos still resolve the module.
|
|
2548
|
+
writeInstalledPackageFiles(ctx, installTargetDirs(ctx, item.name), item.name, {
|
|
2549
|
+
version: '0.0.0', entry: 'index.js', files: [{ rel: 'index.js', content: 'module.exports = {};\n' }]
|
|
2550
|
+
});
|
|
2551
|
+
if (!warning) warning = info.reason;
|
|
2552
|
+
installed.push({ name: item.name, version: '0.0.0' });
|
|
2553
|
+
continue;
|
|
2554
|
+
}
|
|
2371
2555
|
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
const modIndexPath = PosixPath.join(modDir, 'index.js');
|
|
2376
|
-
ctx.vfs.writeFileSync(modPkgPath, JSON.stringify({ name: pkgName, version: pkgVersion.replace(/^[\^~]/, ''), main: './index.js' }, null, 2) + '\n');
|
|
2377
|
-
if (typeof ctx.emitMutation === 'function') ctx.emitMutation('create', modPkgPath);
|
|
2556
|
+
writeInstalledPackageFiles(ctx, installTargetDirs(ctx, item.name), item.name, info);
|
|
2557
|
+
installed.push({ name: item.name, version: info.version || '1.0.0' });
|
|
2558
|
+
if (item.direct) directVersion = info.version || null;
|
|
2378
2559
|
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
code = 'module.exports = { isArray: Array.isArray, isObject: function(v) { return v !== null && typeof v === "object"; } };\n';
|
|
2560
|
+
for (const dep of (info.deps || [])) {
|
|
2561
|
+
const parsed = parsePackageSpec(dep);
|
|
2562
|
+
if (parsed.name && !requested.has(parsed.name)) {
|
|
2563
|
+
requested.add(parsed.name);
|
|
2564
|
+
queue.push({ name: parsed.name, range: null, direct: false });
|
|
2565
|
+
}
|
|
2386
2566
|
}
|
|
2387
|
-
ctx.vfs.writeFileSync(modIndexPath, code);
|
|
2388
|
-
if (typeof ctx.emitMutation === 'function') ctx.emitMutation('create', modIndexPath);
|
|
2389
2567
|
}
|
|
2568
|
+
|
|
2569
|
+
pkgData.dependencies[pkgName] = range || (directVersion ? `^${directVersion}` : (pkgData.dependencies[pkgName] || 'latest'));
|
|
2570
|
+
writePackageJson(ctx.vfs, pkgPath, pkgData, ctx.emitMutation);
|
|
2571
|
+
|
|
2572
|
+
return { name: pkgName, ok: true, version: directVersion, installed, warning };
|
|
2390
2573
|
}
|
|
2391
2574
|
|
|
2392
2575
|
function uninstallPackage(pkgName, ctx) {
|
|
@@ -5063,6 +5246,18 @@
|
|
|
5063
5246
|
|
|
5064
5247
|
const sub = args[0];
|
|
5065
5248
|
|
|
5249
|
+
if (sub === '--version' || sub === '-v' || sub === '--revision') {
|
|
5250
|
+
return { code: 0, stdout: '1.1.20\n', stderr: '' };
|
|
5251
|
+
}
|
|
5252
|
+
|
|
5253
|
+
if (sub === '--help' || sub === '-h') {
|
|
5254
|
+
return {
|
|
5255
|
+
code: 0,
|
|
5256
|
+
stdout: 'Bun v1.1.20\nUsage: bun <command> [flags]\n\nCommands:\n run <file|script> Run a file or package script\n install, i, add Install dependencies\n remove, rm Remove dependencies\n test Run tests\n init Scaffold a new project\n -e <code> Evaluate JavaScript\n',
|
|
5257
|
+
stderr: ''
|
|
5258
|
+
};
|
|
5259
|
+
}
|
|
5260
|
+
|
|
5066
5261
|
if (sub === 'init') {
|
|
5067
5262
|
const pkgPath = PosixPath.join(ctx.cwd, 'package.json');
|
|
5068
5263
|
const dirName = PosixPath.basename(ctx.cwd) || 'app';
|
|
@@ -5112,12 +5307,28 @@
|
|
|
5112
5307
|
return { code: 0, stdout: 'Done! A package.json file was created in ' + ctx.cwd + '\n', stderr: '' };
|
|
5113
5308
|
}
|
|
5114
5309
|
|
|
5115
|
-
if (sub === '
|
|
5310
|
+
if (sub === 'install' || sub === 'i' || sub === 'ci') {
|
|
5116
5311
|
const pkgs = args.slice(1).filter(a => !a.startsWith('-'));
|
|
5117
|
-
|
|
5118
|
-
|
|
5312
|
+
if (pkgs.length === 0) {
|
|
5313
|
+
const { data: pkgData } = readPackageJson(ctx.vfs, ctx.cwd);
|
|
5314
|
+
const deps = Object.keys(pkgData.dependencies || {});
|
|
5315
|
+
const results = [];
|
|
5316
|
+
for (const p of deps) results.push(await installPackage(p, ctx));
|
|
5317
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5318
|
+
return { code: 0, stdout: deps.length === 0 ? 'up to date\n' : `installed ${deps.length} package(s)\n`, stderr: warning ? `bun warn ${warning}\n` : '' };
|
|
5119
5319
|
}
|
|
5120
|
-
|
|
5320
|
+
const results = [];
|
|
5321
|
+
for (const p of pkgs) results.push(await installPackage(p, ctx));
|
|
5322
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5323
|
+
return { code: 0, stdout: `saved ${results.filter(r => r && r.name).length} package(s)\n`, stderr: warning ? `bun warn ${warning}\n` : '' };
|
|
5324
|
+
}
|
|
5325
|
+
|
|
5326
|
+
if (sub === 'add') {
|
|
5327
|
+
const pkgs = args.slice(1).filter(a => !a.startsWith('-'));
|
|
5328
|
+
const results = [];
|
|
5329
|
+
for (const p of pkgs) results.push(await installPackage(p, ctx));
|
|
5330
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5331
|
+
return { code: 0, stdout: `saved ${results.filter(r => r && r.name).length} package(s)\n`, stderr: warning ? `bun warn ${warning}\n` : '' };
|
|
5121
5332
|
}
|
|
5122
5333
|
|
|
5123
5334
|
if (sub === 'remove' || sub === 'rm') {
|
|
@@ -5280,15 +5491,19 @@
|
|
|
5280
5491
|
const pkgs = args.slice(1).filter(a => !a.startsWith('-'));
|
|
5281
5492
|
if (pkgs.length === 0) {
|
|
5282
5493
|
const { data: pkgData } = readPackageJson(ctx.vfs, ctx.cwd);
|
|
5494
|
+
const results = [];
|
|
5283
5495
|
for (const p of Object.keys(pkgData.dependencies || {})) {
|
|
5284
|
-
installPackage(p, ctx);
|
|
5496
|
+
results.push(await installPackage(p, ctx));
|
|
5285
5497
|
}
|
|
5286
|
-
|
|
5498
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5499
|
+
return { code: 0, stdout: 'up to date\n', stderr: warning ? `npm warn ${warning}\n` : '' };
|
|
5287
5500
|
}
|
|
5501
|
+
const results = [];
|
|
5288
5502
|
for (const p of pkgs) {
|
|
5289
|
-
installPackage(p, ctx);
|
|
5503
|
+
results.push(await installPackage(p, ctx));
|
|
5290
5504
|
}
|
|
5291
|
-
|
|
5505
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5506
|
+
return { code: 0, stdout: `added ${results.filter(r => r && r.name).length} package(s) in 0.1s\n`, stderr: warning ? `npm warn ${warning}\n` : '' };
|
|
5292
5507
|
}
|
|
5293
5508
|
|
|
5294
5509
|
if (sub === 'uninstall' || sub === 'remove' || sub === 'rm') {
|
|
@@ -5513,6 +5728,16 @@
|
|
|
5513
5728
|
return PosixPath.resolve(this.cwd, p);
|
|
5514
5729
|
}
|
|
5515
5730
|
|
|
5731
|
+
// True when `name` resolves to a shell function or coreutil (used by the
|
|
5732
|
+
// workbench to route recognised commands straight to the shell instead of
|
|
5733
|
+
// treating flag-looking input such as `node --version` as a JS expression).
|
|
5734
|
+
hasCommand(name) {
|
|
5735
|
+
if (!name) return false;
|
|
5736
|
+
const n = String(name);
|
|
5737
|
+
if (this.functions && Object.prototype.hasOwnProperty.call(this.functions, n)) return true;
|
|
5738
|
+
return !!this.commands && Object.prototype.hasOwnProperty.call(this.commands, n);
|
|
5739
|
+
}
|
|
5740
|
+
|
|
5516
5741
|
_emitVfsMutation(type, absPath) {
|
|
5517
5742
|
if (!this.vfs) return;
|
|
5518
5743
|
const evt = { type, path: absPath, timestamp: Date.now() };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@innovatorved/code-lab",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Standalone browser-native IDE Workbench, POSIX Shell & Coreutils, PTY Terminal, and Web Worker Node.js/Bun Virtual Runtime.",
|
|
5
5
|
"main": "./dist/code-lab.js",
|
|
6
6
|
"module": "./dist/code-lab.esm.js",
|
package/src/runtime.js
CHANGED
|
@@ -4661,8 +4661,22 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4661
4661
|
}
|
|
4662
4662
|
|
|
4663
4663
|
const _npmPackageCache = new Map();
|
|
4664
|
+
const _npmMetadataCache = new Map();
|
|
4665
|
+
|
|
4666
|
+
async function preloadNpmPackages(files = {}, secondArg = null, thirdArg = null) {
|
|
4667
|
+
// The 2nd/3rd arguments are overloaded for backwards compatibility:
|
|
4668
|
+
// preloadNpmPackages(files)
|
|
4669
|
+
// preloadNpmPackages(files, onProgress) // progress callback (used by the Workbench)
|
|
4670
|
+
// preloadNpmPackages(files, existingCache) // package-text cache (Map)
|
|
4671
|
+
// preloadNpmPackages(files, existingCache, onProgress)
|
|
4672
|
+
const isCacheLike = (v) => !!v && typeof v !== 'function' && typeof v.has === 'function' && typeof v.get === 'function';
|
|
4673
|
+
let onProgress = null;
|
|
4674
|
+
let existingCache = _npmPackageCache;
|
|
4675
|
+
if (typeof secondArg === 'function') onProgress = secondArg;
|
|
4676
|
+
else if (isCacheLike(secondArg)) existingCache = secondArg;
|
|
4677
|
+
if (typeof thirdArg === 'function') onProgress = onProgress || thirdArg;
|
|
4678
|
+
else if (isCacheLike(thirdArg)) existingCache = thirdArg;
|
|
4664
4679
|
|
|
4665
|
-
async function preloadNpmPackages(files = {}, existingCache = _npmPackageCache) {
|
|
4666
4680
|
const specs = new Set();
|
|
4667
4681
|
const builtinSet = new Set([
|
|
4668
4682
|
'fs', 'fs/promises', 'path', 'os', 'crypto', 'zlib', 'http', 'https', 'net',
|
|
@@ -4698,20 +4712,51 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4698
4712
|
while ((m = impRe.exec(content)) !== null) addSpec(m[1]);
|
|
4699
4713
|
}
|
|
4700
4714
|
|
|
4715
|
+
// Follow declared dependencies transitively (best effort, bounded). Metadata
|
|
4716
|
+
// is memoised across calls so repeated preloads stay cheap.
|
|
4717
|
+
if (typeof fetch === 'function') {
|
|
4718
|
+
let pass = 0;
|
|
4719
|
+
while (pass < 6) {
|
|
4720
|
+
const pending = [];
|
|
4721
|
+
for (const pkg of specs) {
|
|
4722
|
+
if (!_npmMetadataCache.has(pkg)) pending.push(pkg);
|
|
4723
|
+
}
|
|
4724
|
+
if (pending.length === 0) break;
|
|
4725
|
+
pass++;
|
|
4726
|
+
await Promise.all(pending.map(async (pkg) => {
|
|
4727
|
+
let deps = [];
|
|
4728
|
+
try {
|
|
4729
|
+
const res = await fetch(`https://cdn.jsdelivr.net/npm/${pkg}/package.json`);
|
|
4730
|
+
if (res && res.ok) {
|
|
4731
|
+
const meta = await res.json();
|
|
4732
|
+
deps = Object.keys((meta && meta.dependencies) || {});
|
|
4733
|
+
}
|
|
4734
|
+
} catch (_) {}
|
|
4735
|
+
_npmMetadataCache.set(pkg, deps);
|
|
4736
|
+
}));
|
|
4737
|
+
for (const pkg of pending) {
|
|
4738
|
+
for (const dep of (_npmMetadataCache.get(pkg) || [])) addSpec(dep);
|
|
4739
|
+
}
|
|
4740
|
+
}
|
|
4741
|
+
}
|
|
4742
|
+
|
|
4701
4743
|
const loaded = {};
|
|
4702
4744
|
for (const pkg of specs) {
|
|
4703
4745
|
const nmKey = `node_modules/${pkg}/index.js`;
|
|
4704
4746
|
if (files[nmKey] || files[`/workspace/${nmKey}`]) continue;
|
|
4705
|
-
if (existingCache
|
|
4747
|
+
if (existingCache.has(pkg)) {
|
|
4706
4748
|
loaded[pkg] = existingCache.get(pkg);
|
|
4707
4749
|
continue;
|
|
4708
4750
|
}
|
|
4751
|
+
if (typeof onProgress === 'function') {
|
|
4752
|
+
try { onProgress(pkg); } catch (_) {}
|
|
4753
|
+
}
|
|
4709
4754
|
if (typeof fetch === 'function') {
|
|
4710
4755
|
try {
|
|
4711
4756
|
const res = await fetch(`https://cdn.jsdelivr.net/npm/${pkg}`);
|
|
4712
4757
|
if (res && res.ok) {
|
|
4713
4758
|
const text = await res.text();
|
|
4714
|
-
if (existingCache) existingCache.set(pkg, text);
|
|
4759
|
+
if (typeof existingCache.set === 'function') existingCache.set(pkg, text);
|
|
4715
4760
|
loaded[pkg] = text;
|
|
4716
4761
|
}
|
|
4717
4762
|
} catch (_) {}
|
package/src/runtime.ts
CHANGED
|
@@ -4662,8 +4662,22 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4662
4662
|
}
|
|
4663
4663
|
|
|
4664
4664
|
const _npmPackageCache = new Map();
|
|
4665
|
+
const _npmMetadataCache = new Map();
|
|
4666
|
+
|
|
4667
|
+
async function preloadNpmPackages(files = {}, secondArg = null, thirdArg = null) {
|
|
4668
|
+
// The 2nd/3rd arguments are overloaded for backwards compatibility:
|
|
4669
|
+
// preloadNpmPackages(files)
|
|
4670
|
+
// preloadNpmPackages(files, onProgress) // progress callback (used by the Workbench)
|
|
4671
|
+
// preloadNpmPackages(files, existingCache) // package-text cache (Map)
|
|
4672
|
+
// preloadNpmPackages(files, existingCache, onProgress)
|
|
4673
|
+
const isCacheLike = (v: any) => !!v && typeof v !== 'function' && typeof v.has === 'function' && typeof v.get === 'function';
|
|
4674
|
+
let onProgress: any = null;
|
|
4675
|
+
let existingCache: any = _npmPackageCache;
|
|
4676
|
+
if (typeof secondArg === 'function') onProgress = secondArg;
|
|
4677
|
+
else if (isCacheLike(secondArg)) existingCache = secondArg;
|
|
4678
|
+
if (typeof thirdArg === 'function') onProgress = onProgress || thirdArg;
|
|
4679
|
+
else if (isCacheLike(thirdArg)) existingCache = thirdArg;
|
|
4665
4680
|
|
|
4666
|
-
async function preloadNpmPackages(files = {}, existingCache = _npmPackageCache) {
|
|
4667
4681
|
const specs = new Set();
|
|
4668
4682
|
const builtinSet = new Set([
|
|
4669
4683
|
'fs', 'fs/promises', 'path', 'os', 'crypto', 'zlib', 'http', 'https', 'net',
|
|
@@ -4699,20 +4713,51 @@ function __initVirtualRuntime(rootScope) {
|
|
|
4699
4713
|
while ((m = impRe.exec(content)) !== null) addSpec(m[1]);
|
|
4700
4714
|
}
|
|
4701
4715
|
|
|
4716
|
+
// Follow declared dependencies transitively (best effort, bounded). Metadata
|
|
4717
|
+
// is memoised across calls so repeated preloads stay cheap.
|
|
4718
|
+
if (typeof fetch === 'function') {
|
|
4719
|
+
let pass = 0;
|
|
4720
|
+
while (pass < 6) {
|
|
4721
|
+
const pending: string[] = [];
|
|
4722
|
+
for (const pkg of specs) {
|
|
4723
|
+
if (!_npmMetadataCache.has(pkg)) pending.push(pkg as string);
|
|
4724
|
+
}
|
|
4725
|
+
if (pending.length === 0) break;
|
|
4726
|
+
pass++;
|
|
4727
|
+
await Promise.all(pending.map(async (pkg) => {
|
|
4728
|
+
let deps: string[] = [];
|
|
4729
|
+
try {
|
|
4730
|
+
const res = await fetch(`https://cdn.jsdelivr.net/npm/${pkg}/package.json`);
|
|
4731
|
+
if (res && res.ok) {
|
|
4732
|
+
const meta = await res.json();
|
|
4733
|
+
deps = Object.keys((meta && meta.dependencies) || {});
|
|
4734
|
+
}
|
|
4735
|
+
} catch (_) {}
|
|
4736
|
+
_npmMetadataCache.set(pkg, deps);
|
|
4737
|
+
}));
|
|
4738
|
+
for (const pkg of pending) {
|
|
4739
|
+
for (const dep of (_npmMetadataCache.get(pkg) || [])) addSpec(dep);
|
|
4740
|
+
}
|
|
4741
|
+
}
|
|
4742
|
+
}
|
|
4743
|
+
|
|
4702
4744
|
const loaded = {};
|
|
4703
4745
|
for (const pkg of specs) {
|
|
4704
4746
|
const nmKey = `node_modules/${pkg}/index.js`;
|
|
4705
4747
|
if (files[nmKey] || files[`/workspace/${nmKey}`]) continue;
|
|
4706
|
-
if (existingCache
|
|
4748
|
+
if (existingCache.has(pkg)) {
|
|
4707
4749
|
loaded[pkg] = existingCache.get(pkg);
|
|
4708
4750
|
continue;
|
|
4709
4751
|
}
|
|
4752
|
+
if (typeof onProgress === 'function') {
|
|
4753
|
+
try { onProgress(pkg); } catch (_) {}
|
|
4754
|
+
}
|
|
4710
4755
|
if (typeof fetch === 'function') {
|
|
4711
4756
|
try {
|
|
4712
4757
|
const res = await fetch(`https://cdn.jsdelivr.net/npm/${pkg}`);
|
|
4713
4758
|
if (res && res.ok) {
|
|
4714
4759
|
const text = await res.text();
|
|
4715
|
-
if (existingCache) existingCache.set(pkg, text);
|
|
4760
|
+
if (typeof existingCache.set === 'function') existingCache.set(pkg, text);
|
|
4716
4761
|
loaded[pkg] = text;
|
|
4717
4762
|
}
|
|
4718
4763
|
} catch (_) {}
|
|
@@ -7502,7 +7547,11 @@ export const createVirtualFs: (options?: any) => any = _hostRuntime.createVirtua
|
|
|
7502
7547
|
export const createVirtualHttp: (portTable?: any, vfs?: any) => any = _hostRuntime.createVirtualHttp;
|
|
7503
7548
|
export const createVirtualNet: (portTable?: any) => any = _hostRuntime.createVirtualNet;
|
|
7504
7549
|
export const transpileTS: (code: string, filename?: string) => string = _hostRuntime.transpileTS;
|
|
7505
|
-
export const preloadNpmPackages: (
|
|
7550
|
+
export const preloadNpmPackages: (
|
|
7551
|
+
files?: Record<string, any>,
|
|
7552
|
+
onProgressOrCache?: ((pkg: string) => void) | Map<string, string>,
|
|
7553
|
+
onProgress?: (pkg: string) => void
|
|
7554
|
+
) => Promise<Record<string, string>> = _hostRuntime.preloadNpmPackages;
|
|
7506
7555
|
export const createVirtualPortTable: () => any = _hostRuntime.createVirtualPortTable;
|
|
7507
7556
|
export const executeInVirtualRuntime: (options?: any) => Promise<RuntimeExecutionResult> = _hostRuntime.executeInVirtualRuntime;
|
|
7508
7557
|
export const createWorkerRuntimeSource: () => string = _hostRuntime.createWorkerRuntimeSource;
|