@volter/tabnode 0.5.27 → 0.5.28

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.
@@ -29572,7 +29572,7 @@ Parser.acorn = {
29572
29572
  function parse3(input, options) {
29573
29573
  return Parser.parse(input, options);
29574
29574
  }
29575
- class Script {
29575
+ let Script$1 = class Script {
29576
29576
  constructor(code2, _options22) {
29577
29577
  __publicField(this, "code");
29578
29578
  this.code = code2;
@@ -29635,7 +29635,7 @@ class Script {
29635
29635
  createCachedData() {
29636
29636
  return Buffer.from("");
29637
29637
  }
29638
- }
29638
+ };
29639
29639
  function createContext(contextObject, _options22) {
29640
29640
  return contextObject || {};
29641
29641
  }
@@ -29646,7 +29646,7 @@ function runInThisContext(code, _options) {
29646
29646
  return eval(code);
29647
29647
  }
29648
29648
  function runInNewContext(code2, contextObject, _options22) {
29649
- const script = new Script(code2);
29649
+ const script = new Script$1(code2);
29650
29650
  return script.runInNewContext(contextObject);
29651
29651
  }
29652
29652
  function runInContext(code2, context2, _options22) {
@@ -29684,7 +29684,7 @@ class SyntheticModule extends Module {
29684
29684
  }
29685
29685
  }
29686
29686
  var vm$1 = {
29687
- Script,
29687
+ Script: Script$1,
29688
29688
  createContext,
29689
29689
  isContext,
29690
29690
  runInThisContext,
@@ -29698,7 +29698,7 @@ var vm$1 = {
29698
29698
  var vmShim = /* @__PURE__ */ Object.freeze({
29699
29699
  __proto__: null,
29700
29700
  Module,
29701
- Script,
29701
+ Script: Script$1,
29702
29702
  SourceTextModule,
29703
29703
  SyntheticModule,
29704
29704
  compileFunction,
@@ -74706,7 +74706,24 @@ function readPackageJson(cwd) {
74706
74706
  }
74707
74707
  }
74708
74708
  async function handleNpmRun(args, ctx) {
74709
- const scriptName = args[0];
74709
+ let scriptName;
74710
+ let silent = false;
74711
+ let ifPresent = false;
74712
+ const passed = [];
74713
+ for (let index2 = 0; index2 < args.length; index2++) {
74714
+ const arg = args[index2];
74715
+ if (arg === "--") {
74716
+ if (scriptName !== void 0) passed.push(...args.slice(index2 + 1));
74717
+ break;
74718
+ }
74719
+ if (arg.startsWith("-") && arg !== "-") {
74720
+ if (arg === "--silent" || arg === "-s" || arg === "--quiet" || arg === "-q" || arg === "--loglevel=silent") silent = true;
74721
+ else if (arg === "--if-present") ifPresent = true;
74722
+ continue;
74723
+ }
74724
+ if (scriptName === void 0) scriptName = arg;
74725
+ else passed.push(arg);
74726
+ }
74710
74727
  if (!scriptName) {
74711
74728
  return listScripts(ctx);
74712
74729
  }
@@ -74716,6 +74733,7 @@ async function handleNpmRun(args, ctx) {
74716
74733
  const scripts = pkgJson.scripts || {};
74717
74734
  const scriptCommand = scripts[scriptName];
74718
74735
  if (!scriptCommand) {
74736
+ if (ifPresent) return { stdout: "", stderr: "", exitCode: 0 };
74719
74737
  const available = Object.keys(scripts);
74720
74738
  let msg2 = `npm ERR! Missing script: "${scriptName}"
74721
74739
  `;
@@ -74748,7 +74766,7 @@ async function handleNpmRun(args, ctx) {
74748
74766
  const label = `${pkgJson.name || ""}@${pkgJson.version || ""}`;
74749
74767
  const preScript = scripts[`pre${scriptName}`];
74750
74768
  if (preScript) {
74751
- allStderr += `
74769
+ if (!silent) allStderr += `
74752
74770
  > ${label} pre${scriptName}
74753
74771
  > ${preScript}
74754
74772
 
@@ -74760,12 +74778,13 @@ async function handleNpmRun(args, ctx) {
74760
74778
  return { stdout: allStdout, stderr: allStderr, exitCode: preResult.exitCode };
74761
74779
  }
74762
74780
  }
74763
- allStderr += `
74781
+ const command = passed.length > 0 ? `${scriptCommand} ${passed.map((arg) => /^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replace(/'/g, `'\\''`)}'`).join(" ")}` : scriptCommand;
74782
+ if (!silent) allStderr += `
74764
74783
  > ${label} ${scriptName}
74765
- > ${scriptCommand}
74784
+ > ${command}
74766
74785
 
74767
74786
  `;
74768
- const mainResult = await ctx.exec(scriptCommand, { cwd: ctx.cwd, env: npmEnv });
74787
+ const mainResult = await ctx.exec(command, { cwd: ctx.cwd, env: npmEnv, ...typeof ctx.stdin === "string" ? { stdin: ctx.stdin } : {} });
74769
74788
  allStdout += mainResult.stdout;
74770
74789
  allStderr += mainResult.stderr;
74771
74790
  if (mainResult.exitCode !== 0) {
@@ -74773,7 +74792,7 @@ async function handleNpmRun(args, ctx) {
74773
74792
  }
74774
74793
  const postScript = scripts[`post${scriptName}`];
74775
74794
  if (postScript) {
74776
- allStderr += `
74795
+ if (!silent) allStderr += `
74777
74796
  > ${label} post${scriptName}
74778
74797
  > ${postScript}
74779
74798
 
@@ -78279,6 +78298,22 @@ function __substrateGuestConstructor(Constructor, process2) {
78279
78298
  return __substrateFunctionScope(__substrateGuestGlobal(process2), String(compiled));
78280
78299
  } });
78281
78300
  }
78301
+ const __substrateScriptScope = Function("__substrateScriptGlobals", "__substrateScriptSource", "with (__substrateScriptGlobals) { return eval(__substrateScriptSource); }");
78302
+ const __substrateGuestVms = /* @__PURE__ */ new WeakMap();
78303
+ function __substrateGuestVm(process2) {
78304
+ let vm2 = __substrateGuestVms.get(process2);
78305
+ if (vm2) return vm2;
78306
+ const run = (code2) => __substrateScriptScope(__substrateGuestGlobal(process2), String(code2));
78307
+ class Script2 extends Script$1 {
78308
+ runInThisContext(_options3) {
78309
+ return run(this.code);
78310
+ }
78311
+ }
78312
+ vm2 = { ...vmShim, Script: Script2, runInThisContext: (code2, _options3) => run(code2), default: void 0 };
78313
+ vm2.default = vm2;
78314
+ __substrateGuestVms.set(process2, vm2);
78315
+ return vm2;
78316
+ }
78282
78317
  function __substrateModuleTypes(code2, resolvedPath, format2, process2) {
78283
78318
  const typescript = format2 === void 0 ? /[.](?:ts|mts|cts)$/u.test(resolvedPath) && !resolvedPath.endsWith(".d.ts") : format2 === "typescript" || format2 === "commonjs-typescript" || format2 === "module-typescript";
78284
78319
  if (!typescript) return code2;
@@ -78512,7 +78547,7 @@ function __substratePrepareBody(rawCode, resolvedPath, format2, process2) {
78512
78547
  const lowered = transformEsmToCjs(code2, resolvedPath);
78513
78548
  code2 = lowered === code2 ? code2 : `${__substrateModuleMarker}"use strict";${lowered}`;
78514
78549
  } else {
78515
- code2 = transformDynamicImportsRegex(code2);
78550
+ code2 = __substrateRewriteDynamicImportsInScript(code2);
78516
78551
  }
78517
78552
  return code2;
78518
78553
  }
@@ -78659,6 +78694,17 @@ async function __substrateImportChain(hooks, id2, parentURL, moduleRequire) {
78659
78694
  }));
78660
78695
  return doors.__loadFromURL(resolved.url, loaded2.format ?? resolved.format, loaded2.source ?? void 0);
78661
78696
  }
78697
+ function createImportMeta(moduleRequire, url2, dirname2, filename) {
78698
+ const resolve2 = (specifier, parent) => {
78699
+ const id2 = typeof specifier === "string" ? specifier : String(specifier);
78700
+ const base = parent === void 0 ? url2 : String(parent);
78701
+ if (id2.startsWith("./") || id2.startsWith("../") || id2.startsWith("/")) return new URL(id2, base).href;
78702
+ if (/^[a-zA-Z][a-zA-Z\d+.-]*:/.test(id2)) return id2.startsWith("node:") ? id2 : new URL(id2).href;
78703
+ if (Object.prototype.hasOwnProperty.call(builtinModules, id2)) return `node:${id2}`;
78704
+ return "file://" + moduleRequire.resolve(id2);
78705
+ };
78706
+ return { url: url2, dirname: dirname2, filename, resolve: resolve2 };
78707
+ }
78662
78708
  function createDynamicImport(moduleRequire, process2, parentURL) {
78663
78709
  return async (specifier) => {
78664
78710
  var _a3;
@@ -79627,7 +79673,7 @@ function createRequire(vfs2, fsShim, process2, currentDir, moduleCache, options,
79627
79673
  dirname2,
79628
79674
  process2,
79629
79675
  consoleWrapper,
79630
- { url: importMetaUrl, dirname: dirname2, filename: resolvedPath },
79676
+ createImportMeta(moduleRequire, importMetaUrl, dirname2, resolvedPath),
79631
79677
  dynamicImport,
79632
79678
  __substrateGuestGlobal,
79633
79679
  __substrateGuestConstructor
@@ -79730,6 +79776,9 @@ function createRequire(vfs2, fsShim, process2, currentDir, moduleCache, options,
79730
79776
  if (id2 === "module") {
79731
79777
  return __substrateModule();
79732
79778
  }
79779
+ if (id2 === "vm") {
79780
+ return __substrateGuestVm(process2);
79781
+ }
79733
79782
  if (id2 === "wasi") {
79734
79783
  return __substrateGuestWasi(fsShim, process2);
79735
79784
  }
@@ -79952,7 +80001,7 @@ class Runtime {
79952
80001
  const lowered = transformEsmToCjs(code2, filename);
79953
80002
  code2 = lowered === code2 ? code2 : `${__substrateModuleMarker}"use strict";${lowered}`;
79954
80003
  } else {
79955
- code2 = transformDynamicImportsRegex(code2);
80004
+ code2 = __substrateRewriteDynamicImportsInScript(code2);
79956
80005
  }
79957
80006
  try {
79958
80007
  const importMetaUrl = "file://" + filename;
@@ -79983,7 +80032,7 @@ class Runtime {
79983
80032
  dirname$1,
79984
80033
  this.process,
79985
80034
  consoleWrapper,
79986
- { url: importMetaUrl, dirname: dirname$1, filename },
80035
+ createImportMeta(require2, importMetaUrl, dirname$1, filename),
79987
80036
  dynamicImport,
79988
80037
  __substrateGuestGlobal,
79989
80038
  __substrateGuestConstructor
package/dist/index.mjs CHANGED
@@ -29720,7 +29720,7 @@ Parser.acorn = {
29720
29720
  function parse3(input, options) {
29721
29721
  return Parser.parse(input, options);
29722
29722
  }
29723
- class Script {
29723
+ let Script$1 = class Script {
29724
29724
  constructor(code2, _options2) {
29725
29725
  __publicField(this, "code");
29726
29726
  this.code = code2;
@@ -29783,7 +29783,7 @@ class Script {
29783
29783
  createCachedData() {
29784
29784
  return Buffer.from("");
29785
29785
  }
29786
- }
29786
+ };
29787
29787
  function createContext(contextObject, _options2) {
29788
29788
  return contextObject || {};
29789
29789
  }
@@ -29794,7 +29794,7 @@ function runInThisContext(code, _options) {
29794
29794
  return eval(code);
29795
29795
  }
29796
29796
  function runInNewContext(code2, contextObject, _options2) {
29797
- const script = new Script(code2);
29797
+ const script = new Script$1(code2);
29798
29798
  return script.runInNewContext(contextObject);
29799
29799
  }
29800
29800
  function runInContext(code2, context2, _options2) {
@@ -29832,7 +29832,7 @@ class SyntheticModule extends Module {
29832
29832
  }
29833
29833
  }
29834
29834
  const vm = {
29835
- Script,
29835
+ Script: Script$1,
29836
29836
  createContext,
29837
29837
  isContext,
29838
29838
  runInThisContext,
@@ -29846,7 +29846,7 @@ const vm = {
29846
29846
  const vmShim = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
29847
29847
  __proto__: null,
29848
29848
  Module,
29849
- Script,
29849
+ Script: Script$1,
29850
29850
  SourceTextModule,
29851
29851
  SyntheticModule,
29852
29852
  compileFunction,
@@ -34343,7 +34343,24 @@ function readPackageJson(cwd) {
34343
34343
  }
34344
34344
  }
34345
34345
  async function handleNpmRun(args, ctx) {
34346
- const scriptName = args[0];
34346
+ let scriptName;
34347
+ let silent = false;
34348
+ let ifPresent = false;
34349
+ const passed = [];
34350
+ for (let index2 = 0; index2 < args.length; index2++) {
34351
+ const arg = args[index2];
34352
+ if (arg === "--") {
34353
+ if (scriptName !== void 0) passed.push(...args.slice(index2 + 1));
34354
+ break;
34355
+ }
34356
+ if (arg.startsWith("-") && arg !== "-") {
34357
+ if (arg === "--silent" || arg === "-s" || arg === "--quiet" || arg === "-q" || arg === "--loglevel=silent") silent = true;
34358
+ else if (arg === "--if-present") ifPresent = true;
34359
+ continue;
34360
+ }
34361
+ if (scriptName === void 0) scriptName = arg;
34362
+ else passed.push(arg);
34363
+ }
34347
34364
  if (!scriptName) {
34348
34365
  return listScripts(ctx);
34349
34366
  }
@@ -34353,6 +34370,7 @@ async function handleNpmRun(args, ctx) {
34353
34370
  const scripts = pkgJson.scripts || {};
34354
34371
  const scriptCommand = scripts[scriptName];
34355
34372
  if (!scriptCommand) {
34373
+ if (ifPresent) return { stdout: "", stderr: "", exitCode: 0 };
34356
34374
  const available = Object.keys(scripts);
34357
34375
  let msg2 = `npm ERR! Missing script: "${scriptName}"
34358
34376
  `;
@@ -34385,7 +34403,7 @@ async function handleNpmRun(args, ctx) {
34385
34403
  const label = `${pkgJson.name || ""}@${pkgJson.version || ""}`;
34386
34404
  const preScript = scripts[`pre${scriptName}`];
34387
34405
  if (preScript) {
34388
- allStderr += `
34406
+ if (!silent) allStderr += `
34389
34407
  > ${label} pre${scriptName}
34390
34408
  > ${preScript}
34391
34409
 
@@ -34397,12 +34415,13 @@ async function handleNpmRun(args, ctx) {
34397
34415
  return { stdout: allStdout, stderr: allStderr, exitCode: preResult.exitCode };
34398
34416
  }
34399
34417
  }
34400
- allStderr += `
34418
+ const command = passed.length > 0 ? `${scriptCommand} ${passed.map((arg) => /^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replace(/'/g, `'\\''`)}'`).join(" ")}` : scriptCommand;
34419
+ if (!silent) allStderr += `
34401
34420
  > ${label} ${scriptName}
34402
- > ${scriptCommand}
34421
+ > ${command}
34403
34422
 
34404
34423
  `;
34405
- const mainResult = await ctx.exec(scriptCommand, { cwd: ctx.cwd, env: npmEnv });
34424
+ const mainResult = await ctx.exec(command, { cwd: ctx.cwd, env: npmEnv, ...typeof ctx.stdin === "string" ? { stdin: ctx.stdin } : {} });
34406
34425
  allStdout += mainResult.stdout;
34407
34426
  allStderr += mainResult.stderr;
34408
34427
  if (mainResult.exitCode !== 0) {
@@ -34410,7 +34429,7 @@ async function handleNpmRun(args, ctx) {
34410
34429
  }
34411
34430
  const postScript = scripts[`post${scriptName}`];
34412
34431
  if (postScript) {
34413
- allStderr += `
34432
+ if (!silent) allStderr += `
34414
34433
  > ${label} post${scriptName}
34415
34434
  > ${postScript}
34416
34435
 
@@ -37937,6 +37956,22 @@ function __substrateGuestConstructor(Constructor, process2) {
37937
37956
  return __substrateFunctionScope(__substrateGuestGlobal(process2), String(compiled));
37938
37957
  } });
37939
37958
  }
37959
+ const __substrateScriptScope = Function("__substrateScriptGlobals", "__substrateScriptSource", "with (__substrateScriptGlobals) { return eval(__substrateScriptSource); }");
37960
+ const __substrateGuestVms = /* @__PURE__ */ new WeakMap();
37961
+ function __substrateGuestVm(process2) {
37962
+ let vm2 = __substrateGuestVms.get(process2);
37963
+ if (vm2) return vm2;
37964
+ const run = (code2) => __substrateScriptScope(__substrateGuestGlobal(process2), String(code2));
37965
+ class Script2 extends Script$1 {
37966
+ runInThisContext(_options2) {
37967
+ return run(this.code);
37968
+ }
37969
+ }
37970
+ vm2 = { ...vmShim, Script: Script2, runInThisContext: (code2, _options2) => run(code2), default: void 0 };
37971
+ vm2.default = vm2;
37972
+ __substrateGuestVms.set(process2, vm2);
37973
+ return vm2;
37974
+ }
37940
37975
  function __substrateModuleTypes(code2, resolvedPath, format2, process2) {
37941
37976
  const typescript = format2 === void 0 ? /[.](?:ts|mts|cts)$/u.test(resolvedPath) && !resolvedPath.endsWith(".d.ts") : format2 === "typescript" || format2 === "commonjs-typescript" || format2 === "module-typescript";
37942
37977
  if (!typescript) return code2;
@@ -38170,7 +38205,7 @@ function __substratePrepareBody(rawCode, resolvedPath, format2, process2) {
38170
38205
  const lowered = transformEsmToCjs(code2, resolvedPath);
38171
38206
  code2 = lowered === code2 ? code2 : `${__substrateModuleMarker}"use strict";${lowered}`;
38172
38207
  } else {
38173
- code2 = transformDynamicImportsRegex(code2);
38208
+ code2 = __substrateRewriteDynamicImportsInScript(code2);
38174
38209
  }
38175
38210
  return code2;
38176
38211
  }
@@ -38320,6 +38355,17 @@ async function __substrateImportChain(hooks, id, parentURL, moduleRequire) {
38320
38355
  }));
38321
38356
  return doors.__loadFromURL(resolved.url, loaded2.format ?? resolved.format, loaded2.source ?? void 0);
38322
38357
  }
38358
+ function createImportMeta(moduleRequire, url2, dirname2, filename) {
38359
+ const resolve2 = (specifier, parent) => {
38360
+ const id = typeof specifier === "string" ? specifier : String(specifier);
38361
+ const base = parent === void 0 ? url2 : String(parent);
38362
+ if (id.startsWith("./") || id.startsWith("../") || id.startsWith("/")) return new URL(id, base).href;
38363
+ if (/^[a-zA-Z][a-zA-Z\d+.-]*:/.test(id)) return id.startsWith("node:") ? id : new URL(id).href;
38364
+ if (Object.prototype.hasOwnProperty.call(builtinModules, id)) return `node:${id}`;
38365
+ return "file://" + moduleRequire.resolve(id);
38366
+ };
38367
+ return { url: url2, dirname: dirname2, filename, resolve: resolve2 };
38368
+ }
38323
38369
  function createDynamicImport(moduleRequire, process2, parentURL) {
38324
38370
  return async (specifier) => {
38325
38371
  var _a2;
@@ -39288,7 +39334,7 @@ function createRequire(vfs2, fsShim, process2, currentDir, moduleCache, options,
39288
39334
  dirname2,
39289
39335
  process2,
39290
39336
  consoleWrapper,
39291
- { url: importMetaUrl, dirname: dirname2, filename: resolvedPath },
39337
+ createImportMeta(moduleRequire, importMetaUrl, dirname2, resolvedPath),
39292
39338
  dynamicImport,
39293
39339
  __substrateGuestGlobal,
39294
39340
  __substrateGuestConstructor
@@ -39391,6 +39437,9 @@ function createRequire(vfs2, fsShim, process2, currentDir, moduleCache, options,
39391
39437
  if (id === "module") {
39392
39438
  return __substrateModule();
39393
39439
  }
39440
+ if (id === "vm") {
39441
+ return __substrateGuestVm(process2);
39442
+ }
39394
39443
  if (id === "wasi") {
39395
39444
  return __substrateGuestWasi(fsShim, process2);
39396
39445
  }
@@ -39613,7 +39662,7 @@ class Runtime {
39613
39662
  const lowered = transformEsmToCjs(code2, filename);
39614
39663
  code2 = lowered === code2 ? code2 : `${__substrateModuleMarker}"use strict";${lowered}`;
39615
39664
  } else {
39616
- code2 = transformDynamicImportsRegex(code2);
39665
+ code2 = __substrateRewriteDynamicImportsInScript(code2);
39617
39666
  }
39618
39667
  try {
39619
39668
  const importMetaUrl = "file://" + filename;
@@ -39644,7 +39693,7 @@ class Runtime {
39644
39693
  dirname$1,
39645
39694
  this.process,
39646
39695
  consoleWrapper,
39647
- { url: importMetaUrl, dirname: dirname$1, filename },
39696
+ createImportMeta(require2, importMetaUrl, dirname$1, filename),
39648
39697
  dynamicImport,
39649
39698
  __substrateGuestGlobal,
39650
39699
  __substrateGuestConstructor
@@ -40120,7 +40169,7 @@ class WorkerRuntime {
40120
40169
  this.worker = new Worker(
40121
40170
  new URL(
40122
40171
  /* @vite-ignore */
40123
- "/assets/runtime-worker-DPvtXRd8.js",
40172
+ "/assets/runtime-worker-C6ulimZ4.js",
40124
40173
  import.meta.url
40125
40174
  ),
40126
40175
  { type: "module" }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/tabnode",
3
- "version": "0.5.27",
3
+ "version": "0.5.28",
4
4
  "description": "Node's own library, in a browser tab.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/runtime.ts CHANGED
@@ -117,6 +117,29 @@ function __substrateGuestConstructor(Constructor: unknown, process: Process): un
117
117
  return __substrateFunctionScope(__substrateGuestGlobal(process), String(compiled));
118
118
  } });
119
119
  }
120
+ /**
121
+ * `vm.runInThisContext` runs its script in the calling process's global scope,
122
+ * as Node's does. The shim evaluated it in the host's, so a script saw the
123
+ * worker's own timers: jiti evaluates every module it loads this way, and
124
+ * pi-mcp-adapter's `setInterval(...).unref()` threw "unref is not a function"
125
+ * because the browser's `setInterval` returns a number. The script runs inside
126
+ * the process's guest global, as a guest `Function` does
127
+ * (`__substrateGuestConstructor`).
128
+ */
129
+ const __substrateScriptScope = Function("__substrateScriptGlobals", "__substrateScriptSource", "with (__substrateScriptGlobals) { return eval(__substrateScriptSource); }");
130
+ const __substrateGuestVms = new WeakMap<Process, unknown>();
131
+ function __substrateGuestVm(process: Process): unknown {
132
+ let vm = __substrateGuestVms.get(process);
133
+ if (vm) return vm;
134
+ const run = (code: string): unknown => __substrateScriptScope(__substrateGuestGlobal(process), String(code));
135
+ class Script extends vmShim.Script {
136
+ override runInThisContext(_options?: object): unknown { return run((this as unknown as { code: string }).code); }
137
+ }
138
+ vm = { ...vmShim, Script, runInThisContext: (code: string, _options?: object) => run(code), default: undefined };
139
+ (vm as { default: unknown }).default = vm;
140
+ __substrateGuestVms.set(process, vm);
141
+ return vm;
142
+ }
120
143
  /**
121
144
  * A `.ts`, `.mts` or `.cts` file runs with its types stripped, as Node runs
122
145
  * one since 22.18, before the module's imports are read -- the entry a `node`
@@ -456,8 +479,12 @@ function __substratePrepareBody(rawCode: string, resolvedPath: string, format: s
456
479
  // A `.cjs` module skips the ESM transform, and with it the rewrite of
457
480
  // `import(...)` to the engine's dynamic import, so its
458
481
  // `import("fs/promises")` reached the browser's own import and failed on
459
- // the bare specifier. The dynamic-import rewrite applies to `.cjs` too.
460
- code = transformDynamicImportsRegex(code);
482
+ // the bare specifier. The dynamic-import rewrite applies to `.cjs` too,
483
+ // from the syntax tree as for every other CommonJS module: the text
484
+ // rewrite also renamed a method called `import`, and jiti's
485
+ // `async import(e,t){...}` became `__dynamicImport`, so `jiti.import` was
486
+ // undefined and every Pi extension failed to load.
487
+ code = __substrateRewriteDynamicImportsInScript(code);
461
488
  }
462
489
 
463
490
  return code;
@@ -726,6 +753,27 @@ async function __substrateImportChain(
726
753
  return doors.__loadFromURL(resolved.url, loaded.format ?? resolved.format, loaded.source ?? void 0);
727
754
  }
728
755
 
756
+ /**
757
+ * A module's `import.meta`: `url`, `dirname`, `filename` and Node's synchronous
758
+ * `resolve` (Node 20.6+). Pi's extension loader resolves its own packages with
759
+ * `import.meta.resolve(specifier)`, and without it every Pi extension failed to
760
+ * load with "import_meta.resolve is not a function". A relative or URL specifier
761
+ * resolves against the module's URL, a builtin to `node:<name>`, and a bare one
762
+ * through the module's own resolver, the one `import()` uses here. Unlike Node,
763
+ * a relative specifier naming a missing file resolves instead of throwing.
764
+ */
765
+ function createImportMeta(moduleRequire: RequireFunction, url: string, dirname: string, filename: string): Record<string, unknown> {
766
+ const resolve = (specifier: unknown, parent?: unknown): string => {
767
+ const id = typeof specifier === 'string' ? specifier : String(specifier);
768
+ const base = parent === undefined ? url : String(parent);
769
+ if (id.startsWith('./') || id.startsWith('../') || id.startsWith('/')) return new URL(id, base).href;
770
+ if (/^[a-zA-Z][a-zA-Z\d+.-]*:/.test(id)) return id.startsWith('node:') ? id : new URL(id).href;
771
+ if (Object.prototype.hasOwnProperty.call(builtinModules, id)) return `node:${id}`;
772
+ return 'file://' + moduleRequire.resolve(id);
773
+ };
774
+ return { url, dirname, filename, resolve };
775
+ }
776
+
729
777
  function createDynamicImport(moduleRequire: RequireFunction, process: Process, parentURL?: string): (specifier: unknown) => Promise<unknown> {
730
778
  return async (specifier: unknown): Promise<unknown> => {
731
779
  try {
@@ -1962,7 +2010,7 @@ function createRequire(
1962
2010
  dirname,
1963
2011
  process,
1964
2012
  consoleWrapper,
1965
- { url: importMetaUrl, dirname, filename: resolvedPath },
2013
+ createImportMeta(moduleRequire, importMetaUrl, dirname, resolvedPath),
1966
2014
  dynamicImport,
1967
2015
  __substrateGuestGlobal,
1968
2016
  __substrateGuestConstructor
@@ -2130,6 +2178,11 @@ function createRequire(
2130
2178
  if (id === 'module') {
2131
2179
  return __substrateModule();
2132
2180
  }
2181
+ // Ahead of the per-process builtin table: node-lib's own modules require
2182
+ // `vm` first and that table keeps the first answer for the process.
2183
+ if (id === 'vm') {
2184
+ return __substrateGuestVm(process);
2185
+ }
2133
2186
  if (id === 'wasi') {
2134
2187
  return __substrateGuestWasi(fsShim, process);
2135
2188
  }
@@ -2445,8 +2498,12 @@ export class Runtime {
2445
2498
  // A `.cjs` module skips the ESM transform, and with it the rewrite of
2446
2499
  // `import(...)` to the engine's dynamic import, so its
2447
2500
  // `import("fs/promises")` reached the browser's own import and failed on
2448
- // the bare specifier. The dynamic-import rewrite applies to `.cjs` too.
2449
- code = transformDynamicImportsRegex(code);
2501
+ // the bare specifier. The dynamic-import rewrite applies to `.cjs` too,
2502
+ // from the syntax tree as for every other CommonJS module: the text
2503
+ // rewrite also renamed a method called `import`, and jiti's
2504
+ // `async import(e,t){...}` became `__dynamicImport`, so `jiti.import` was
2505
+ // undefined and every Pi extension failed to load.
2506
+ code = __substrateRewriteDynamicImportsInScript(code);
2450
2507
  }
2451
2508
 
2452
2509
  // Execute code
@@ -2492,7 +2549,7 @@ export class Runtime {
2492
2549
  dirname,
2493
2550
  this.process,
2494
2551
  consoleWrapper,
2495
- { url: importMetaUrl, dirname, filename },
2552
+ createImportMeta(require, importMetaUrl, dirname, filename),
2496
2553
  dynamicImport,
2497
2554
  __substrateGuestGlobal,
2498
2555
  __substrateGuestConstructor
@@ -1143,7 +1143,26 @@ function readPackageJson(cwd: string): { pkgJson: PackageJson; error?: undefined
1143
1143
  * Handle `npm run [script]` — execute a script from package.json
1144
1144
  */
1145
1145
  async function handleNpmRun(args: string[], ctx: CommandContext): Promise<JustBashExecResult> {
1146
- const scriptName = args[0];
1146
+ // npm's own options come before the script's name and are npm's
1147
+ // (`npm run --silent vgai -- mcp .`, as a project's `.mcp.json` launches its
1148
+ // server); what follows the name goes to the script, everything after `--`
1149
+ // untouched. The name used to be `args[0]`, so `--silent` was looked up as a
1150
+ // script, and the script's own arguments were dropped.
1151
+ let scriptName: string | undefined;
1152
+ let silent = false;
1153
+ let ifPresent = false;
1154
+ const passed: string[] = [];
1155
+ for (let index = 0; index < args.length; index++) {
1156
+ const arg = args[index]!;
1157
+ if (arg === '--') { if (scriptName !== undefined) passed.push(...args.slice(index + 1)); break; }
1158
+ if (arg.startsWith('-') && arg !== '-') {
1159
+ if (arg === '--silent' || arg === '-s' || arg === '--quiet' || arg === '-q' || arg === '--loglevel=silent') silent = true;
1160
+ else if (arg === '--if-present') ifPresent = true;
1161
+ continue;
1162
+ }
1163
+ if (scriptName === undefined) scriptName = arg;
1164
+ else passed.push(arg);
1165
+ }
1147
1166
 
1148
1167
  // "npm run" with no script name: list available scripts
1149
1168
  if (!scriptName) {
@@ -1158,6 +1177,7 @@ async function handleNpmRun(args: string[], ctx: CommandContext): Promise<JustBa
1158
1177
  const scriptCommand = scripts[scriptName];
1159
1178
 
1160
1179
  if (!scriptCommand) {
1180
+ if (ifPresent) return { stdout: '', stderr: '', exitCode: 0 };
1161
1181
  const available = Object.keys(scripts);
1162
1182
  let msg = `npm ERR! Missing script: "${scriptName}"\n`;
1163
1183
  if (available.length > 0) {
@@ -1193,7 +1213,7 @@ async function handleNpmRun(args: string[], ctx: CommandContext): Promise<JustBa
1193
1213
  // Run pre<script> if it exists
1194
1214
  const preScript = scripts[`pre${scriptName}`];
1195
1215
  if (preScript) {
1196
- allStderr += `\n> ${label} pre${scriptName}\n> ${preScript}\n\n`;
1216
+ if (!silent) allStderr += `\n> ${label} pre${scriptName}\n> ${preScript}\n\n`;
1197
1217
  const preResult = await ctx.exec(preScript, { cwd: ctx.cwd, env: npmEnv });
1198
1218
  allStdout += preResult.stdout;
1199
1219
  allStderr += preResult.stderr;
@@ -1202,9 +1222,14 @@ async function handleNpmRun(args: string[], ctx: CommandContext): Promise<JustBa
1202
1222
  }
1203
1223
  }
1204
1224
 
1205
- // Run the main script
1206
- allStderr += `\n> ${label} ${scriptName}\n> ${scriptCommand}\n\n`;
1207
- const mainResult = await ctx.exec(scriptCommand, { cwd: ctx.cwd, env: npmEnv });
1225
+ // Run the main script, with the arguments npm passes it, each quoted.
1226
+ const command = passed.length > 0
1227
+ ? `${scriptCommand} ${passed.map((arg) => /^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replace(/'/g, `'\\''`)}'`).join(' ')}`
1228
+ : scriptCommand;
1229
+ if (!silent) allStderr += `\n> ${label} ${scriptName}\n> ${command}\n\n`;
1230
+ // The script reads npm's standard input: a `.mcp.json` server launched as
1231
+ // `npm run … -- serve` speaks JSON-RPC on it.
1232
+ const mainResult = await ctx.exec(command, { cwd: ctx.cwd, env: npmEnv, ...(typeof ctx.stdin === 'string' ? { stdin: ctx.stdin } : {}) });
1208
1233
  allStdout += mainResult.stdout;
1209
1234
  allStderr += mainResult.stderr;
1210
1235
 
@@ -1215,7 +1240,7 @@ async function handleNpmRun(args: string[], ctx: CommandContext): Promise<JustBa
1215
1240
  // Run post<script> if it exists
1216
1241
  const postScript = scripts[`post${scriptName}`];
1217
1242
  if (postScript) {
1218
- allStderr += `\n> ${label} post${scriptName}\n> ${postScript}\n\n`;
1243
+ if (!silent) allStderr += `\n> ${label} post${scriptName}\n> ${postScript}\n\n`;
1219
1244
  const postResult = await ctx.exec(postScript, { cwd: ctx.cwd, env: npmEnv });
1220
1245
  allStdout += postResult.stdout;
1221
1246
  allStderr += postResult.stderr;