@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/src/shell.js
CHANGED
|
@@ -2336,38 +2336,221 @@
|
|
|
2336
2336
|
} catch (_) {}
|
|
2337
2337
|
}
|
|
2338
2338
|
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2339
|
+
// ---------------------------------------------------------------------------
|
|
2340
|
+
// Real npm package installation via the jsDelivr CDN
|
|
2341
|
+
// ---------------------------------------------------------------------------
|
|
2342
|
+
const CDN_NPM_BASE = 'https://cdn.jsdelivr.net/npm';
|
|
2343
|
+
|
|
2344
|
+
// Minimal offline registry, used only when the CDN is unreachable so common
|
|
2345
|
+
// demos still resolve without network access.
|
|
2346
|
+
const OFFLINE_PACKAGE_FALLBACKS = {
|
|
2347
|
+
'is-even': 'module.exports = function isEven(n) { return (Number(n) % 2) === 0; };\n',
|
|
2348
|
+
'tiny-invariant': 'module.exports = function invariant(condition, message) { if (!condition) throw new Error(message || "Invariant failed"); };\n',
|
|
2349
|
+
'lodash': 'module.exports = { isArray: Array.isArray, isObject: function (v) { return v !== null && typeof v === "object"; } };\n',
|
|
2350
|
+
'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'
|
|
2351
|
+
};
|
|
2352
|
+
|
|
2353
|
+
function packageCtxFetch(ctx) {
|
|
2354
|
+
if (ctx && ctx.shell && ctx.shell.options && typeof ctx.shell.options.fetchImpl === 'function') {
|
|
2355
|
+
return ctx.shell.options.fetchImpl;
|
|
2356
|
+
}
|
|
2357
|
+
if (typeof fetch === 'function') return fetch.bind(globalThis);
|
|
2358
|
+
return null;
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
function parsePackageSpec(spec) {
|
|
2362
|
+
const raw = String(spec || '').trim();
|
|
2363
|
+
if (!raw) return { name: '', range: null };
|
|
2364
|
+
if (raw.startsWith('@')) {
|
|
2365
|
+
const at = raw.indexOf('@', 1);
|
|
2366
|
+
if (at === -1) return { name: raw, range: null };
|
|
2367
|
+
return { name: raw.slice(0, at), range: raw.slice(at + 1) || null };
|
|
2368
|
+
}
|
|
2369
|
+
const at = raw.indexOf('@');
|
|
2370
|
+
if (at <= 0) return { name: raw, range: null };
|
|
2371
|
+
return { name: raw.slice(0, at), range: raw.slice(at + 1) || null };
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
function resolveEntryRelPath(pkgJson) {
|
|
2375
|
+
let entry = pkgJson.main || pkgJson.module ||
|
|
2376
|
+
(typeof pkgJson.browser === 'string' ? pkgJson.browser : '') || 'index.js';
|
|
2377
|
+
entry = String(entry).replace(/^[./]+/, '');
|
|
2378
|
+
return entry || 'index.js';
|
|
2379
|
+
}
|
|
2380
|
+
|
|
2381
|
+
function stripRelPath(rel) {
|
|
2382
|
+
return String(rel || '').replace(/^[./]+/, '');
|
|
2383
|
+
}
|
|
2384
|
+
|
|
2385
|
+
function extractRelativeSpecifiers(code) {
|
|
2386
|
+
const out = new Set();
|
|
2387
|
+
const re = /(?:\brequire\s*\(\s*|\bimport\s+[\s\S]{0,120}?\bfrom\s*|\bimport\s*\(\s*)['"](\.{1,2}\/[^'"]+)['"]/g;
|
|
2388
|
+
let m;
|
|
2389
|
+
while ((m = re.exec(code)) !== null) out.add(m[1]);
|
|
2390
|
+
return Array.from(out);
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
function normalizeRelPath(baseDir, rel) {
|
|
2394
|
+
const parts = String(baseDir || '').split('/').filter(Boolean);
|
|
2395
|
+
for (const seg of String(rel).split('/')) {
|
|
2396
|
+
if (!seg || seg === '.') continue;
|
|
2397
|
+
if (seg === '..') parts.pop();
|
|
2398
|
+
else parts.push(seg);
|
|
2399
|
+
}
|
|
2400
|
+
return parts.join('/');
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
async function fetchTextFromCdn(fetchImpl, url) {
|
|
2404
|
+
try {
|
|
2405
|
+
const res = await fetchImpl(url);
|
|
2406
|
+
if (!res) return null;
|
|
2407
|
+
const ok = (res.ok === true) || res.status === 200 || res.status === undefined;
|
|
2408
|
+
if (!ok) return null;
|
|
2409
|
+
if (typeof res.text === 'function') return await res.text();
|
|
2410
|
+
return String(res.body != null ? res.body : '');
|
|
2411
|
+
} catch (_) {
|
|
2412
|
+
return null;
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
async function resolvePackageMetadata(fetchImpl, name, range) {
|
|
2417
|
+
const urls = range
|
|
2418
|
+
? [`${CDN_NPM_BASE}/${name}@${range}/package.json`, `${CDN_NPM_BASE}/${name}/package.json`]
|
|
2419
|
+
: [`${CDN_NPM_BASE}/${name}/package.json`];
|
|
2420
|
+
for (const url of urls) {
|
|
2421
|
+
const text = await fetchTextFromCdn(fetchImpl, url);
|
|
2422
|
+
if (!text) continue;
|
|
2423
|
+
try {
|
|
2424
|
+
const parsed = JSON.parse(text);
|
|
2425
|
+
if (parsed && parsed.version) return parsed;
|
|
2426
|
+
} catch (_) {}
|
|
2427
|
+
}
|
|
2428
|
+
return null;
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
// Download a package's entry module, its relative submodules and record its
|
|
2432
|
+
// declared dependencies so the caller can resolve them transitively.
|
|
2433
|
+
async function downloadPackageFiles(fetchImpl, name, range, seen) {
|
|
2434
|
+
const meta = await resolvePackageMetadata(fetchImpl, name, range);
|
|
2435
|
+
if (!meta) {
|
|
2436
|
+
const fallback = OFFLINE_PACKAGE_FALLBACKS[name];
|
|
2437
|
+
if (fallback) {
|
|
2438
|
+
const v = range ? range.replace(/^[\^~>=<\s]+/, '') || '1.0.0' : '1.0.0';
|
|
2439
|
+
return { ok: true, offline: true, version: v, entry: 'index.js', files: [{ rel: 'index.js', content: fallback }], deps: [] };
|
|
2440
|
+
}
|
|
2441
|
+
return { ok: false, reason: `${name}: package metadata not found on the registry (offline?)` };
|
|
2442
|
+
}
|
|
2443
|
+
|
|
2444
|
+
const version = meta.version;
|
|
2445
|
+
const key = `${name}@${version}`;
|
|
2446
|
+
if (seen.has(key)) {
|
|
2447
|
+
return { ok: true, deduped: true, version, entry: resolveEntryRelPath(meta), files: [], deps: [] };
|
|
2448
|
+
}
|
|
2449
|
+
seen.add(key);
|
|
2450
|
+
|
|
2451
|
+
const entry = resolveEntryRelPath(meta);
|
|
2452
|
+
const files = [];
|
|
2453
|
+
const visited = new Set();
|
|
2454
|
+
const queue = [entry];
|
|
2455
|
+
while (queue.length > 0) {
|
|
2456
|
+
const rel = stripRelPath(queue.shift());
|
|
2457
|
+
if (!rel || visited.has(rel)) continue;
|
|
2458
|
+
visited.add(rel);
|
|
2459
|
+
const text = await fetchTextFromCdn(fetchImpl, `${CDN_NPM_BASE}/${name}@${version}/${rel}`);
|
|
2460
|
+
if (text === null) continue;
|
|
2461
|
+
files.push({ rel, content: text });
|
|
2462
|
+
const baseDir = rel.includes('/') ? rel.slice(0, rel.lastIndexOf('/')) : '';
|
|
2463
|
+
for (const spec of extractRelativeSpecifiers(text)) {
|
|
2464
|
+
const target = normalizeRelPath(baseDir, spec);
|
|
2465
|
+
for (const cand of [target, `${target}.js`, `${target}.json`, `${target}/index.js`]) {
|
|
2466
|
+
const c = stripRelPath(cand);
|
|
2467
|
+
if (c && !visited.has(c)) { queue.push(cand); break; }
|
|
2468
|
+
}
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
// The declared entry may be missing on the CDN; fall back to index.js.
|
|
2472
|
+
if (!files.some(f => f.rel === entry)) {
|
|
2473
|
+
const idxText = await fetchTextFromCdn(fetchImpl, `${CDN_NPM_BASE}/${name}@${version}/index.js`);
|
|
2474
|
+
if (idxText !== null) files.push({ rel: 'index.js', content: idxText });
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
return { ok: true, version, entry, files, deps: Object.keys(meta.dependencies || {}) };
|
|
2478
|
+
}
|
|
2479
|
+
|
|
2480
|
+
function installTargetDirs(ctx, name) {
|
|
2481
|
+
const dirs = ['/workspace/node_modules/' + name];
|
|
2482
|
+
const cwdTarget = PosixPath.join(ctx.cwd || '/workspace', 'node_modules', name);
|
|
2483
|
+
if (!dirs.includes(cwdTarget)) dirs.push(cwdTarget);
|
|
2484
|
+
return dirs;
|
|
2485
|
+
}
|
|
2486
|
+
|
|
2487
|
+
function writeInstalledPackageFiles(ctx, dirs, name, info) {
|
|
2488
|
+
for (const dir of dirs) {
|
|
2489
|
+
ensureDirSync(ctx.vfs, dir, ctx.emitMutation);
|
|
2490
|
+
for (const f of info.files) {
|
|
2491
|
+
const full = PosixPath.join(dir, f.rel);
|
|
2492
|
+
ensureDirSync(ctx.vfs, PosixPath.dirname(full), ctx.emitMutation);
|
|
2493
|
+
ctx.vfs.writeFileSync(full, f.content);
|
|
2494
|
+
if (typeof ctx.emitMutation === 'function') ctx.emitMutation('create', full);
|
|
2495
|
+
}
|
|
2496
|
+
const modPkgPath = PosixPath.join(dir, 'package.json');
|
|
2497
|
+
const modPkg = { name, version: info.version || '1.0.0', main: './' + (info.entry || 'index.js') };
|
|
2498
|
+
ctx.vfs.writeFileSync(modPkgPath, JSON.stringify(modPkg, null, 2) + '\n');
|
|
2499
|
+
if (typeof ctx.emitMutation === 'function') ctx.emitMutation('create', modPkgPath);
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
|
|
2503
|
+
async function installPackage(pkgSpec, ctx) {
|
|
2504
|
+
const { name: pkgName, range } = parsePackageSpec(pkgSpec);
|
|
2505
|
+
if (!pkgName) return { name: null, ok: false, installed: [], warning: null };
|
|
2343
2506
|
|
|
2344
2507
|
const { path: pkgPath, data: pkgData } = readPackageJson(ctx.vfs, ctx.cwd);
|
|
2345
2508
|
if (!pkgData.dependencies) pkgData.dependencies = {};
|
|
2346
|
-
pkgData.dependencies[pkgName] = pkgVersion;
|
|
2347
|
-
writePackageJson(ctx.vfs, pkgPath, pkgData, ctx.emitMutation);
|
|
2348
2509
|
|
|
2349
|
-
const
|
|
2350
|
-
const
|
|
2351
|
-
|
|
2510
|
+
const fetchImpl = packageCtxFetch(ctx);
|
|
2511
|
+
const seen = (ctx.shell && ctx.shell._cdnSeen)
|
|
2512
|
+
? ctx.shell._cdnSeen
|
|
2513
|
+
: (ctx.shell ? (ctx.shell._cdnSeen = new Set()) : new Set());
|
|
2514
|
+
|
|
2515
|
+
const queue = [{ name: pkgName, range, direct: true }];
|
|
2516
|
+
const requested = new Set([pkgName]);
|
|
2517
|
+
const installed = [];
|
|
2518
|
+
let warning = null;
|
|
2519
|
+
let directVersion = null;
|
|
2520
|
+
|
|
2521
|
+
while (queue.length > 0) {
|
|
2522
|
+
const item = queue.shift();
|
|
2523
|
+
const info = fetchImpl
|
|
2524
|
+
? await downloadPackageFiles(fetchImpl, item.name, item.range, seen)
|
|
2525
|
+
: { ok: false, reason: `${item.name}: no fetch implementation available` };
|
|
2526
|
+
|
|
2527
|
+
if (!info.ok) {
|
|
2528
|
+
// Final fallback: an empty stub so offline demos still resolve the module.
|
|
2529
|
+
writeInstalledPackageFiles(ctx, installTargetDirs(ctx, item.name), item.name, {
|
|
2530
|
+
version: '0.0.0', entry: 'index.js', files: [{ rel: 'index.js', content: 'module.exports = {};\n' }]
|
|
2531
|
+
});
|
|
2532
|
+
if (!warning) warning = info.reason;
|
|
2533
|
+
installed.push({ name: item.name, version: '0.0.0' });
|
|
2534
|
+
continue;
|
|
2535
|
+
}
|
|
2352
2536
|
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
const modIndexPath = PosixPath.join(modDir, 'index.js');
|
|
2357
|
-
ctx.vfs.writeFileSync(modPkgPath, JSON.stringify({ name: pkgName, version: pkgVersion.replace(/^[\^~]/, ''), main: './index.js' }, null, 2) + '\n');
|
|
2358
|
-
if (typeof ctx.emitMutation === 'function') ctx.emitMutation('create', modPkgPath);
|
|
2537
|
+
writeInstalledPackageFiles(ctx, installTargetDirs(ctx, item.name), item.name, info);
|
|
2538
|
+
installed.push({ name: item.name, version: info.version || '1.0.0' });
|
|
2539
|
+
if (item.direct) directVersion = info.version || null;
|
|
2359
2540
|
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
code = 'module.exports = { isArray: Array.isArray, isObject: function(v) { return v !== null && typeof v === "object"; } };\n';
|
|
2541
|
+
for (const dep of (info.deps || [])) {
|
|
2542
|
+
const parsed = parsePackageSpec(dep);
|
|
2543
|
+
if (parsed.name && !requested.has(parsed.name)) {
|
|
2544
|
+
requested.add(parsed.name);
|
|
2545
|
+
queue.push({ name: parsed.name, range: null, direct: false });
|
|
2546
|
+
}
|
|
2367
2547
|
}
|
|
2368
|
-
ctx.vfs.writeFileSync(modIndexPath, code);
|
|
2369
|
-
if (typeof ctx.emitMutation === 'function') ctx.emitMutation('create', modIndexPath);
|
|
2370
2548
|
}
|
|
2549
|
+
|
|
2550
|
+
pkgData.dependencies[pkgName] = range || (directVersion ? `^${directVersion}` : (pkgData.dependencies[pkgName] || 'latest'));
|
|
2551
|
+
writePackageJson(ctx.vfs, pkgPath, pkgData, ctx.emitMutation);
|
|
2552
|
+
|
|
2553
|
+
return { name: pkgName, ok: true, version: directVersion, installed, warning };
|
|
2371
2554
|
}
|
|
2372
2555
|
|
|
2373
2556
|
function uninstallPackage(pkgName, ctx) {
|
|
@@ -5044,6 +5227,18 @@
|
|
|
5044
5227
|
|
|
5045
5228
|
const sub = args[0];
|
|
5046
5229
|
|
|
5230
|
+
if (sub === '--version' || sub === '-v' || sub === '--revision') {
|
|
5231
|
+
return { code: 0, stdout: '1.1.20\n', stderr: '' };
|
|
5232
|
+
}
|
|
5233
|
+
|
|
5234
|
+
if (sub === '--help' || sub === '-h') {
|
|
5235
|
+
return {
|
|
5236
|
+
code: 0,
|
|
5237
|
+
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',
|
|
5238
|
+
stderr: ''
|
|
5239
|
+
};
|
|
5240
|
+
}
|
|
5241
|
+
|
|
5047
5242
|
if (sub === 'init') {
|
|
5048
5243
|
const pkgPath = PosixPath.join(ctx.cwd, 'package.json');
|
|
5049
5244
|
const dirName = PosixPath.basename(ctx.cwd) || 'app';
|
|
@@ -5093,12 +5288,28 @@
|
|
|
5093
5288
|
return { code: 0, stdout: 'Done! A package.json file was created in ' + ctx.cwd + '\n', stderr: '' };
|
|
5094
5289
|
}
|
|
5095
5290
|
|
|
5096
|
-
if (sub === '
|
|
5291
|
+
if (sub === 'install' || sub === 'i' || sub === 'ci') {
|
|
5097
5292
|
const pkgs = args.slice(1).filter(a => !a.startsWith('-'));
|
|
5098
|
-
|
|
5099
|
-
|
|
5293
|
+
if (pkgs.length === 0) {
|
|
5294
|
+
const { data: pkgData } = readPackageJson(ctx.vfs, ctx.cwd);
|
|
5295
|
+
const deps = Object.keys(pkgData.dependencies || {});
|
|
5296
|
+
const results = [];
|
|
5297
|
+
for (const p of deps) results.push(await installPackage(p, ctx));
|
|
5298
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5299
|
+
return { code: 0, stdout: deps.length === 0 ? 'up to date\n' : `installed ${deps.length} package(s)\n`, stderr: warning ? `bun warn ${warning}\n` : '' };
|
|
5100
5300
|
}
|
|
5101
|
-
|
|
5301
|
+
const results = [];
|
|
5302
|
+
for (const p of pkgs) results.push(await installPackage(p, ctx));
|
|
5303
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5304
|
+
return { code: 0, stdout: `saved ${results.filter(r => r && r.name).length} package(s)\n`, stderr: warning ? `bun warn ${warning}\n` : '' };
|
|
5305
|
+
}
|
|
5306
|
+
|
|
5307
|
+
if (sub === 'add') {
|
|
5308
|
+
const pkgs = args.slice(1).filter(a => !a.startsWith('-'));
|
|
5309
|
+
const results = [];
|
|
5310
|
+
for (const p of pkgs) results.push(await installPackage(p, ctx));
|
|
5311
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5312
|
+
return { code: 0, stdout: `saved ${results.filter(r => r && r.name).length} package(s)\n`, stderr: warning ? `bun warn ${warning}\n` : '' };
|
|
5102
5313
|
}
|
|
5103
5314
|
|
|
5104
5315
|
if (sub === 'remove' || sub === 'rm') {
|
|
@@ -5261,15 +5472,19 @@
|
|
|
5261
5472
|
const pkgs = args.slice(1).filter(a => !a.startsWith('-'));
|
|
5262
5473
|
if (pkgs.length === 0) {
|
|
5263
5474
|
const { data: pkgData } = readPackageJson(ctx.vfs, ctx.cwd);
|
|
5475
|
+
const results = [];
|
|
5264
5476
|
for (const p of Object.keys(pkgData.dependencies || {})) {
|
|
5265
|
-
installPackage(p, ctx);
|
|
5477
|
+
results.push(await installPackage(p, ctx));
|
|
5266
5478
|
}
|
|
5267
|
-
|
|
5479
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5480
|
+
return { code: 0, stdout: 'up to date\n', stderr: warning ? `npm warn ${warning}\n` : '' };
|
|
5268
5481
|
}
|
|
5482
|
+
const results = [];
|
|
5269
5483
|
for (const p of pkgs) {
|
|
5270
|
-
installPackage(p, ctx);
|
|
5484
|
+
results.push(await installPackage(p, ctx));
|
|
5271
5485
|
}
|
|
5272
|
-
|
|
5486
|
+
const warning = results.map(r => r && r.warning).filter(Boolean)[0];
|
|
5487
|
+
return { code: 0, stdout: `added ${results.filter(r => r && r.name).length} package(s) in 0.1s\n`, stderr: warning ? `npm warn ${warning}\n` : '' };
|
|
5273
5488
|
}
|
|
5274
5489
|
|
|
5275
5490
|
if (sub === 'uninstall' || sub === 'remove' || sub === 'rm') {
|
|
@@ -5494,6 +5709,16 @@
|
|
|
5494
5709
|
return PosixPath.resolve(this.cwd, p);
|
|
5495
5710
|
}
|
|
5496
5711
|
|
|
5712
|
+
// True when `name` resolves to a shell function or coreutil (used by the
|
|
5713
|
+
// workbench to route recognised commands straight to the shell instead of
|
|
5714
|
+
// treating flag-looking input such as `node --version` as a JS expression).
|
|
5715
|
+
hasCommand(name) {
|
|
5716
|
+
if (!name) return false;
|
|
5717
|
+
const n = String(name);
|
|
5718
|
+
if (this.functions && Object.prototype.hasOwnProperty.call(this.functions, n)) return true;
|
|
5719
|
+
return !!this.commands && Object.prototype.hasOwnProperty.call(this.commands, n);
|
|
5720
|
+
}
|
|
5721
|
+
|
|
5497
5722
|
_emitVfsMutation(type, absPath) {
|
|
5498
5723
|
if (!this.vfs) return;
|
|
5499
5724
|
const evt = { type, path: absPath, timestamp: Date.now() };
|