@nimbus-sh/core 0.1.0 → 0.2.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.
Files changed (60) hide show
  1. package/dist/index.d.ts +2 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +1 -0
  4. package/dist/runtime/bash-runner.d.ts +63 -0
  5. package/dist/runtime/bash-runner.d.ts.map +1 -0
  6. package/dist/runtime/bash-runner.generated.d.ts +14 -0
  7. package/dist/runtime/bash-runner.generated.d.ts.map +1 -0
  8. package/dist/runtime/bash-runner.generated.js +13 -0
  9. package/dist/runtime/bash-runner.js +290 -0
  10. package/dist/runtime/cpython-runner.d.ts +86 -0
  11. package/dist/runtime/cpython-runner.d.ts.map +1 -0
  12. package/dist/runtime/cpython-runner.js +425 -0
  13. package/dist/runtime/facet-host.d.ts +159 -0
  14. package/dist/runtime/facet-host.d.ts.map +1 -0
  15. package/dist/runtime/facet-host.js +22 -0
  16. package/dist/runtime/installed-runtimes.d.ts +99 -0
  17. package/dist/runtime/installed-runtimes.d.ts.map +1 -0
  18. package/dist/runtime/installed-runtimes.js +162 -0
  19. package/dist/runtime/local-facet-host.d.ts +38 -0
  20. package/dist/runtime/local-facet-host.d.ts.map +1 -0
  21. package/dist/runtime/local-facet-host.js +171 -0
  22. package/dist/runtime/python-pip.d.ts +38 -0
  23. package/dist/runtime/python-pip.d.ts.map +1 -0
  24. package/dist/runtime/python-pip.js +1063 -0
  25. package/dist/runtime/runtime-manifest.d.ts +86 -0
  26. package/dist/runtime/runtime-manifest.d.ts.map +1 -0
  27. package/dist/runtime/runtime-manifest.js +72 -0
  28. package/dist/runtime/runtime-registry.d.ts +161 -0
  29. package/dist/runtime/runtime-registry.d.ts.map +1 -0
  30. package/dist/runtime/runtime-registry.js +363 -0
  31. package/dist/runtime/vfs-snapshot.d.ts.map +1 -1
  32. package/dist/runtime/vfs-snapshot.js +15 -1
  33. package/dist/runtime/vfs-supervisor.d.ts +22 -0
  34. package/dist/runtime/vfs-supervisor.d.ts.map +1 -0
  35. package/dist/runtime/vfs-supervisor.js +65 -0
  36. package/dist/runtime/virtual-socket-kernel.generated.d.ts +14 -0
  37. package/dist/runtime/virtual-socket-kernel.generated.d.ts.map +1 -0
  38. package/dist/runtime/virtual-socket-kernel.generated.js +13 -0
  39. package/dist/runtime/wasm-runner.d.ts +80 -0
  40. package/dist/runtime/wasm-runner.d.ts.map +1 -0
  41. package/dist/runtime/wasm-runner.js +686 -0
  42. package/dist/workspace/nimbus-workspace.d.ts +16 -0
  43. package/dist/workspace/nimbus-workspace.d.ts.map +1 -1
  44. package/dist/workspace/nimbus-workspace.js +57 -2
  45. package/package.json +4 -2
  46. package/src/index.ts +9 -0
  47. package/src/runtime/bash-runner.generated.ts +14 -0
  48. package/src/runtime/bash-runner.ts +347 -0
  49. package/src/runtime/cpython-runner.ts +504 -0
  50. package/src/runtime/facet-host.ts +170 -0
  51. package/src/runtime/installed-runtimes.ts +235 -0
  52. package/src/runtime/local-facet-host.ts +205 -0
  53. package/src/runtime/python-pip.ts +1211 -0
  54. package/src/runtime/runtime-manifest.ts +155 -0
  55. package/src/runtime/runtime-registry.ts +510 -0
  56. package/src/runtime/vfs-snapshot.ts +15 -1
  57. package/src/runtime/vfs-supervisor.ts +67 -0
  58. package/src/runtime/virtual-socket-kernel.generated.ts +14 -0
  59. package/src/runtime/wasm-runner.ts +835 -0
  60. package/src/workspace/nimbus-workspace.ts +102 -3
@@ -0,0 +1,155 @@
1
+ /**
2
+ * runtime-manifest.ts — what an installed language runtime IS.
3
+ *
4
+ * A manifest names the files a runtime is made of, the shell commands it
5
+ * provides, and the runner that answers them. `nimbus install <name>` writes
6
+ * one into `~/.nimbus/runtimes/<name>/<version>/manifest.json`; every runner
7
+ * reads its own out of the session filesystem from there.
8
+ *
9
+ * The data contract only. Where the bytes come FROM is the publisher's
10
+ * problem, and the Cloudflare deployment's answer to it — an R2 bucket, a
11
+ * per-colo cache, and a digest chain rooted at a build-time pin — lives in
12
+ * `@nimbus-sh/worker`'s `runtime/runtime-catalog.ts`. A runtime that has been
13
+ * installed is the same runtime whichever tier fetched it, so nothing below
14
+ * this point knows there were tiers.
15
+ */
16
+
17
+ import { z } from 'zod/v4';
18
+ import { PYODIDE_PACKAGE_ABI } from './os-contracts.js';
19
+
20
+ export interface ManifestFile {
21
+ /** VFS path relative to ~/.nimbus/runtimes/<name>/<version>/. */
22
+ path: string;
23
+ /** Publisher-side key for the content blob. */
24
+ content: string;
25
+ /** Hex sha256 of the content blob bytes. */
26
+ sha256: string;
27
+ /** Byte size. */
28
+ size: number;
29
+ /** Optional file mode hint ("exec" → registered as a shell bin). */
30
+ mode?: 'exec';
31
+ }
32
+
33
+ export interface ManifestEntrypoint {
34
+ /** Shell command name. */
35
+ binName: string;
36
+ /** Runner key (e.g. "clang-runner") — package manager dispatches to
37
+ * the right runner factory by this. */
38
+ runner: string;
39
+ /** Default args prepended to user args at invocation. */
40
+ args: string[];
41
+ /** Optional secondary classification (e.g. "linker" for wasm-ld). */
42
+ kind?: string;
43
+ }
44
+
45
+ export interface RuntimeArtifactMetadata {
46
+ path: string;
47
+ kind: string;
48
+ id: string;
49
+ source_sha256?: string;
50
+ sha256: string;
51
+ }
52
+
53
+ export type RuntimePythonPackageAbi = typeof PYODIDE_PACKAGE_ABI;
54
+
55
+ export interface RuntimePythonExtensionModuleMetadata {
56
+ /** Path inside Python site-packages, as stored in the wheel. */
57
+ path: string;
58
+ /** Path inside the installed Nimbus runtime root. */
59
+ runtimePath: string;
60
+ sha256: string;
61
+ }
62
+
63
+ export interface RuntimePythonPackageArtifactMetadata extends RuntimeArtifactMetadata {
64
+ kind: 'python-package';
65
+ language: 'python';
66
+ packageName: string;
67
+ version: string;
68
+ abi: RuntimePythonPackageAbi;
69
+ pyodideVersion: string;
70
+ pythonVersion: string;
71
+ wheelFileName: string;
72
+ wheelSha256: string;
73
+ loadMode: 'startup-module';
74
+ imports: string[];
75
+ dependencies: string[];
76
+ extensionModules: RuntimePythonExtensionModuleMetadata[];
77
+ }
78
+
79
+ export interface RuntimeManifest {
80
+ name: string;
81
+ version: string;
82
+ license: string;
83
+ /** Which WASI namespace the binaries import — `wasi_unstable` for
84
+ * binji clang. `null` for non-WASI runtimes (e.g. Pyodide). */
85
+ wasi_namespace: string | null;
86
+ files: ManifestFile[];
87
+ entrypoints: ManifestEntrypoint[];
88
+ runtime_artifacts?: RuntimeArtifactMetadata[];
89
+ }
90
+
91
+ export const HexSha256Schema = z.string().regex(/^[a-f0-9]{64}$/);
92
+
93
+ const ManifestFileSchema: z.ZodType<ManifestFile> = z.object({
94
+ path: z.string().min(1),
95
+ content: z.string().min(1),
96
+ sha256: HexSha256Schema,
97
+ size: z.number().int().nonnegative(),
98
+ mode: z.literal('exec').optional(),
99
+ });
100
+
101
+ const ManifestEntrypointSchema: z.ZodType<ManifestEntrypoint> = z.object({
102
+ binName: z.string().min(1),
103
+ runner: z.string().min(1),
104
+ args: z.array(z.string()),
105
+ kind: z.string().optional(),
106
+ });
107
+
108
+ const RuntimeArtifactMetadataSchema: z.ZodType<RuntimeArtifactMetadata> = z.object({
109
+ path: z.string().min(1),
110
+ kind: z.string().min(1),
111
+ id: z.string().min(1),
112
+ source_sha256: HexSha256Schema.optional(),
113
+ sha256: HexSha256Schema,
114
+ }).passthrough();
115
+
116
+ export const RuntimePythonPackageArtifactMetadataSchema: z.ZodType<RuntimePythonPackageArtifactMetadata> =
117
+ RuntimeArtifactMetadataSchema.and(z.object({
118
+ kind: z.literal('python-package'),
119
+ language: z.literal('python'),
120
+ packageName: z.string().min(1),
121
+ version: z.string().min(1),
122
+ abi: z.literal(PYODIDE_PACKAGE_ABI),
123
+ pyodideVersion: z.string().min(1),
124
+ pythonVersion: z.string().min(1),
125
+ wheelFileName: z.string().min(1),
126
+ wheelSha256: HexSha256Schema,
127
+ loadMode: z.literal('startup-module'),
128
+ imports: z.array(z.string()),
129
+ dependencies: z.array(z.string()),
130
+ extensionModules: z.array(z.object({
131
+ path: z.string().min(1),
132
+ runtimePath: z.string().min(1),
133
+ sha256: HexSha256Schema,
134
+ })),
135
+ }));
136
+
137
+ const RuntimeManifestSchema: z.ZodType<RuntimeManifest> = z.object({
138
+ name: z.string().min(1),
139
+ version: z.string().min(1),
140
+ license: z.string(),
141
+ wasi_namespace: z.string().nullable(),
142
+ files: z.array(ManifestFileSchema),
143
+ entrypoints: z.array(ManifestEntrypointSchema),
144
+ runtime_artifacts: z.array(RuntimeArtifactMetadataSchema).optional(),
145
+ });
146
+
147
+ export function parseRuntimeManifest(value: unknown): RuntimeManifest {
148
+ return RuntimeManifestSchema.parse(value);
149
+ }
150
+
151
+ export function isRuntimePythonPackageArtifactMetadata(
152
+ artifact: RuntimeArtifactMetadata,
153
+ ): artifact is RuntimePythonPackageArtifactMetadata {
154
+ return RuntimePythonPackageArtifactMetadataSchema.safeParse(artifact).success;
155
+ }
@@ -0,0 +1,510 @@
1
+ /**
2
+ * runtime-registry.ts — shared shell-command factory for runtime
3
+ * dispatchers (node, bun, and future native-WASM / Python / Ruby /
4
+ * AssemblyScript runtimes).
5
+ *
6
+ * Why this exists
7
+ * ───────────────
8
+ * `node` and `bun` shell-command handlers in src/session/init.ts
9
+ * shared ~85% of their code: argv parsing for --version / --help /
10
+ * -e / script-path, VFS lookup, shebang strip, esbuild transform for
11
+ * .ts/.tsx/.jsx, dispatch to the runner. The duplication had drifted
12
+ * — only `node` had the primitive #1 nodeFlagSpan fix
13
+ * (init.ts:233-243), only `node` had primitive-#2 binSpawn ctx
14
+ * propagation (init.ts:391-403), only `bun` had install / run
15
+ * subcommand routing.
16
+ *
17
+ * `buildRuntimeHandler` returns a single shell-handler function that
18
+ * encodes the shared contract. Per-runtime variation is supplied
19
+ * via the `RuntimeSpec` parameter:
20
+ *
21
+ * - name + version + helpText
22
+ * - run(): runner fn (runFresh / runBunScript / wasm-runner)
23
+ * - subcommands: optional map of `<verb> → handler` for
24
+ * bun-style `bun install`, `bun run` (node has none today)
25
+ * - transform(): optional code rewriter (bun prepends BUN_SHIM_PREAMBLE)
26
+ * - supportsBinSpawn: true for node (the .bin handler propagates
27
+ * a callerPid); other runtimes use a plain spawn flow.
28
+ *
29
+ * Anti-requirements observed
30
+ * ──────────────────────────
31
+ * - NO setTimeout / NO retry / NO defensive-catch added.
32
+ * - NO behavioral change vs the pre-refactor handlers — every
33
+ * runtime-specific quirk is preserved exactly.
34
+ * - Per-runtime test parity: existing runtime-primitives probes
35
+ * (#1 npx / #2 .bin) and runtime-pkg probes (G1-G4) MUST still
36
+ * pass against the refactored handlers — the contract is
37
+ * observable behaviour, not implementation shape.
38
+ */
39
+
40
+ import type { SqliteVFS } from '../vfs/sqlite-vfs.js';
41
+ import { normalizeVfsPath, resolveVfsPath, vfsPathExtension } from '../vfs/path.js';
42
+ import { CRED_KERNEL, type VfsCred } from './os-contracts.js';
43
+ import type { EsbuildService } from './esbuild-service.js';
44
+ import { parseFacetBundleProfile, type FacetBundleProfile } from './bundle-profile.js';
45
+ import { bindImportMetaResolve, importMetaDefines } from './import-meta-transform.js';
46
+
47
+ /**
48
+ * Result shape that runtime-registry expects from a runner. Mirrors
49
+ * the existing RunFreshResult / RunBunResult shapes — kept narrow so
50
+ * future runtimes don't have to plumb runtime-internal state.
51
+ */
52
+ export interface RuntimeRunResult {
53
+ exitCode: number;
54
+ stdout: string;
55
+ stderr: string;
56
+ }
57
+
58
+ /**
59
+ * Options the handler passes to the runner. Mirrors RunFreshOpts.
60
+ */
61
+ export interface RuntimeRunOpts {
62
+ argv: string[];
63
+ env: Record<string, string> | undefined;
64
+ cwd: string | undefined;
65
+ filename: string;
66
+ dirname: string;
67
+ command: string;
68
+ /** Primitive #1/G4 hooks. node-runner consumes these; other
69
+ * runtimes ignore them safely. */
70
+ skipSpawn?: boolean;
71
+ callerPid?: number;
72
+ /** Capture stdout/stderr in the result instead of streaming to the
73
+ * terminal supervisor. Used by child_process pipe semantics. */
74
+ captureOutput?: boolean;
75
+ forceLongRunning?: boolean;
76
+ attachedTty?: boolean;
77
+ bundleProfile?: FacetBundleProfile;
78
+ /** Invoking process credentials for credential-bound runtime snapshots. */
79
+ cred?: VfsCred;
80
+ }
81
+
82
+ /** Extensions probed when a target names no exact file, in Node's order. */
83
+ const SCRIPT_RESOLUTION_CANDIDATES = ['.js', '.ts', '.tsx', '.mjs', '.jsx', '/index.js', '/index.ts'];
84
+
85
+ /** The VFS surface script resolution needs. */
86
+ export interface ScriptResolutionFs {
87
+ isFile(path: string): boolean;
88
+ readFileString(path: string): string;
89
+ }
90
+
91
+ /**
92
+ * Resolve a runtime target — `./cli.ts`, `sub/x`, `.`, or a bare name — to a
93
+ * canonical VFS key, or null when nothing runnable sits there.
94
+ *
95
+ * A directory never resolves to itself: it falls through to the index
96
+ * candidates, so `bun ./tools` finds `tools/index.js` the way real bun does
97
+ * rather than trying to read the directory as source.
98
+ */
99
+ export function resolveRuntimeScriptPath(
100
+ fs: ScriptResolutionFs,
101
+ cwd: string,
102
+ target: string,
103
+ opts?: { preferModuleField?: boolean },
104
+ ): string | null {
105
+ const base = normalizeVfsPath(cwd || '/home/user');
106
+ let resolved: string;
107
+ if (target === '.' || target === './') {
108
+ // `node .` / `bun .` — the package's declared entry point.
109
+ let main = 'index.js';
110
+ try {
111
+ const pkg = JSON.parse(fs.readFileString(`${base}/package.json`));
112
+ main = (opts?.preferModuleField ? pkg.module : undefined) || pkg.main || 'index.js';
113
+ } catch { /* no readable package.json — index.js */ }
114
+ resolved = resolveVfsPath(main, base);
115
+ } else {
116
+ resolved = resolveVfsPath(target, base);
117
+ }
118
+ if (fs.isFile(resolved)) return resolved;
119
+ for (const candidate of SCRIPT_RESOLUTION_CANDIDATES) {
120
+ if (fs.isFile(resolved + candidate)) return resolved + candidate;
121
+ }
122
+ return null;
123
+ }
124
+
125
+ /**
126
+ * A subcommand handler. `runAsRuntime` re-enters the standard flow — flag
127
+ * span, script resolution, transform, exec — with a rewritten argv, as if
128
+ * the verb had never been typed. `bun run <file>` uses it to hand a path
129
+ * to the very same execution path `bun <file>` takes, rather than growing
130
+ * a second one.
131
+ */
132
+ export type RuntimeSubcommand = (
133
+ ctx: any,
134
+ registry: ShellRegistry,
135
+ runAsRuntime: (args: string[]) => Promise<number>,
136
+ ) => Promise<number>;
137
+
138
+ export interface RuntimeSpec {
139
+ /** Shell-command name: 'node' / 'bun' / 'wasm-runner' / 'python'. */
140
+ name: string;
141
+ /** Output of `<name> --version`. Includes the leading 'v' if the
142
+ * runtime convention does (Node: 'v20.0.0'; Bun: '1.1.42'). */
143
+ version: string;
144
+ /** Multi-line help text for `<name> --help`. */
145
+ helpText: string;
146
+ /**
147
+ * Runner function. Closes over whatever substrate the runtime executes on —
148
+ * a FacetManager for node and bun, a {@link ./facet-host.js FacetHost} for
149
+ * wasm-runner — because this factory never inspects it. It used to travel
150
+ * through here as a first parameter, which is the only thing that tied the
151
+ * shared handler to a Durable Object.
152
+ */
153
+ run(code: string, opts: RuntimeRunOpts): Promise<RuntimeRunResult>;
154
+ /**
155
+ * Subcommand router. When the first positional arg is a key in
156
+ * this map, the handler is invoked instead of the standard
157
+ * script-execution flow. Used by `bun install`, `bun run <script>`.
158
+ */
159
+ subcommands?: Record<string, RuntimeSubcommand>;
160
+ /**
161
+ * When true, the runtime treats the args list as a binary file
162
+ * path (NOT a JS script). Used by `wasm-runner` — the args[0] is a
163
+ * .wasm path, args[1+] are the function name + integer args.
164
+ * The handler skips the read-and-transform-script flow and calls
165
+ * `run()` with a synthetic empty `code` — runtimes that set this
166
+ * flag implement the actual bytes-load inside their runner.
167
+ */
168
+ bypassesScriptRead?: boolean;
169
+ /**
170
+ * Primitive #1 / G4 — when true, the script-execution branch
171
+ * propagates `ctx.__nimbusBinSpawn` into RuntimeRunOpts. Only
172
+ * `node` enables this; bun's runFresh chain doesn't share PID
173
+ * state with the .bin handler today. Future runtimes set this
174
+ * iff they share the runFresh contract.
175
+ */
176
+ supportsBinSpawn?: boolean;
177
+ }
178
+
179
+ /**
180
+ * Minimal registry shape we depend on. Avoids importing the full vendored
181
+ * shell registry type tree when the runtime path only needs resolve().
182
+ */
183
+ export interface ShellRegistry {
184
+ resolve(name: string): Promise<any> | any;
185
+ }
186
+
187
+ /**
188
+ * Build a shell-handler function for a runtime. The returned function
189
+ * is the value passed to `registry.register('<name>', handler)`.
190
+ *
191
+ * Captures `vfs`, `getEsbuild` (for lazy init) + the spec. The same factory is used for every runtime; the only
192
+ * runtime-specific code lives in `spec`.
193
+ */
194
+ export function buildRuntimeHandler(
195
+ spec: RuntimeSpec,
196
+ ctx0: {
197
+ vfs: SqliteVFS;
198
+ /** Lazy esbuild initialiser. Called once per first .ts/.tsx/.jsx
199
+ * invocation — the host owns the init lifecycle. */
200
+ getEsbuild(): EsbuildService;
201
+ registry: ShellRegistry;
202
+ },
203
+ ): (ctx: any) => Promise<number> {
204
+ const { vfs, getEsbuild, registry } = ctx0;
205
+ const fs = vfs.as(CRED_KERNEL);
206
+
207
+ /**
208
+ * The standard invocation: flag span, --version/--help/-e, then the
209
+ * script-path flow. Subcommand verbs are NOT considered here — the
210
+ * caller has already consumed them — so a verb handler can delegate
211
+ * back in with a rewritten argv without re-triggering itself.
212
+ */
213
+ async function runtimeInvocation(ctx: any, args: string[]): Promise<number> {
214
+ const name = spec.name;
215
+ const nimbusCtx = ctx as {
216
+ __nimbusCaptureOutput?: unknown;
217
+ __nimbusBundleProfile?: unknown;
218
+ __nimbusBinSpawn?: {
219
+ callerPid?: number;
220
+ command?: string;
221
+ forceLongRunning?: boolean;
222
+ attachedTty?: boolean;
223
+ };
224
+ };
225
+ // A facet-hosted runtime streams its output to the session terminal over
226
+ // the supervisor RPC and hands the shell an empty string. That is live and
227
+ // cheap, and it is only correct while the process's stdout IS the terminal:
228
+ // the stream bypasses the shell's stdout chain, so the moment fd 1 or fd 2
229
+ // is a file, a pipe, a command substitution, or the capture sink of a
230
+ // programmatic exec, the bytes have to come back in the result and be
231
+ // written through ctx.stdout instead. A context with no fd table of its own
232
+ // — the child_process broker synthesizes one — says so directly.
233
+ const captureOutput = !!nimbusCtx.__nimbusCaptureOutput
234
+ || ctx.isFdTerminal?.(1) === false
235
+ || ctx.isFdTerminal?.(2) === false;
236
+ const bundleProfile = parseFacetBundleProfile(nimbusCtx.__nimbusBundleProfile);
237
+
238
+ // ── Flag-span computation (primitive #1) ──
239
+ //
240
+ // Real-Node only treats args UP TO the first non-flag token as
241
+ // CLI flags. Pre-refactor, version/help/eval scanned the entire
242
+ // args array, breaking `node /path/to/tsc --version` (the user's
243
+ // --version was misinterpreted as a node flag).
244
+ let flagSpan = 0;
245
+ while (flagSpan < args.length && args[flagSpan].startsWith('-')) {
246
+ flagSpan++;
247
+ const prev = args[flagSpan - 1];
248
+ // -e / --eval consumes one value; advance past it.
249
+ if ((prev === '-e' || prev === '--eval') && flagSpan < args.length) {
250
+ flagSpan++;
251
+ }
252
+ }
253
+ const flagSlice = args.slice(0, flagSpan);
254
+
255
+ // ── --version ──
256
+ if (flagSlice.includes('-v') || flagSlice.includes('--version')) {
257
+ ctx.stdout.write(spec.version + '\n');
258
+ return 0;
259
+ }
260
+
261
+ // ── --help ──
262
+ if (flagSlice.includes('--help') || flagSlice.includes('-h')) {
263
+ ctx.stdout.write(spec.helpText);
264
+ if (!spec.helpText.endsWith('\n')) ctx.stdout.write('\n');
265
+ return 0;
266
+ }
267
+
268
+ // ── -e / --eval ──
269
+ const evalIdx = flagSlice.indexOf('-e') !== -1
270
+ ? flagSlice.indexOf('-e')
271
+ : flagSlice.indexOf('--eval');
272
+ if (evalIdx !== -1) {
273
+ const code = args[evalIdx + 1];
274
+ if (!code) {
275
+ ctx.stderr.write(`${name}: -e requires an argument\n`);
276
+ return 1;
277
+ }
278
+ const result = await spec.run(code, {
279
+ cred: ctx.cred,
280
+ argv: args.slice(evalIdx + 2),
281
+ env: ctx.env,
282
+ cwd: ctx.cwd,
283
+ filename: '<eval>',
284
+ dirname: ctx.cwd || '/home/user',
285
+ command: `${name} -e ...`,
286
+ ...(captureOutput ? { captureOutput: true } : {}),
287
+ ...(bundleProfile ? { bundleProfile } : {}),
288
+ });
289
+ if (result.stdout) ctx.stdout.write(result.stdout);
290
+ if (result.stderr) ctx.stderr.write(result.stderr);
291
+ return result.exitCode;
292
+ }
293
+
294
+ // ── script path (or .wasm path for bypassesScriptRead) ──
295
+ const scriptIdx = flagSpan;
296
+ const scriptPath = args[scriptIdx];
297
+ if (!scriptPath) {
298
+ ctx.stderr.write(
299
+ `${name}: REPL not supported. Use ${name} -e "code" or ${name} script.js\n`,
300
+ );
301
+ return 1;
302
+ }
303
+
304
+ // ── bypassesScriptRead branch (wasm-runner) ──
305
+ //
306
+ // The runner takes the path AS-IS (it's a .wasm, not JS source).
307
+ // We don't read or transform here; the runner reads the bytes
308
+ // and instantiates them. `.` resolution and extension probing are
309
+ // meaningless for a .wasm target and stay out of this branch.
310
+ if (spec.bypassesScriptRead) {
311
+ const filename = '/' + resolveVfsPath(scriptPath, ctx.cwd || '/home/user');
312
+ const dirname = filename.includes('/')
313
+ ? filename.substring(0, filename.lastIndexOf('/'))
314
+ : '/';
315
+ // `args.slice(scriptIdx + 1)` are the runner's user args (e.g.
316
+ // [exportName, intArg1, intArg2, ...] for wasm-runner).
317
+ const result = await spec.run('', {
318
+ cred: ctx.cred,
319
+ argv: args.slice(scriptIdx + 1),
320
+ env: ctx.env,
321
+ cwd: ctx.cwd,
322
+ filename,
323
+ dirname,
324
+ command: `${name} ${args.slice(0, scriptIdx + 1).join(' ')}`,
325
+ ...(captureOutput ? { captureOutput: true } : {}),
326
+ ...(bundleProfile ? { bundleProfile } : {}),
327
+ });
328
+ if (result.stdout) ctx.stdout.write(result.stdout);
329
+ if (result.stderr) ctx.stderr.write(result.stderr);
330
+ return result.exitCode;
331
+ }
332
+
333
+ // Resolve against cwd: `.` → the package entry, then extension probing.
334
+ const resolvedPath = resolveRuntimeScriptPath(fs, ctx.cwd || '/home/user', scriptPath, {
335
+ // bun prefers .module over .main when both exist; node uses .main.
336
+ preferModuleField: name === 'bun',
337
+ });
338
+
339
+ let code: string | null = null;
340
+ if (resolvedPath !== null) {
341
+ try {
342
+ code = fs.readFileString(resolvedPath);
343
+ } catch { /* unreadable — reported below */ }
344
+ }
345
+ if (resolvedPath === null || code === null) {
346
+ ctx.stderr.write(`${name}: cannot find module '${scriptPath}'\n`);
347
+ return 1;
348
+ }
349
+
350
+ // Shebang strip (primitive #1).
351
+ if (code.startsWith('#!')) {
352
+ const nl = code.indexOf('\n');
353
+ code = nl >= 0 ? code.substring(nl + 1) : '';
354
+ }
355
+
356
+ // ── ESM-source detection (primitive: type:module entry scripts) ──
357
+ //
358
+ // Nimbus's facet pre-compile loop wraps every entry script in
359
+ // `new Function(...)` which runs it as CJS. A real `node script.js`
360
+ // dispatch honours the nearest package.json's `"type"` field
361
+ // (and the file extension) to decide whether to parse as ESM:
362
+ //
363
+ // - .mjs → always ESM
364
+ // - .cjs → always CJS
365
+ // - .js → ESM iff nearest package.json has "type": "module"
366
+ // - no extension → same rule as .js. Node allows an extensionless
367
+ // main entry and resolves its format from the
368
+ // package type, and that is the shape of nearly
369
+ // every npm `bin` script (typescript's `bin/tsc`,
370
+ // and the `node_modules/.bin/<cli>` target the bin
371
+ // dispatcher hands us).
372
+ //
373
+ // Without this, every modern ESM-only npm initialiser
374
+ // (create-vite, create-astro, create-svelte, modern create-*)
375
+ // crashes immediately with "Cannot use import statement outside
376
+ // a module" because their bin entry is `index.js` and the
377
+ // package.json declares `type: module`.
378
+ //
379
+ // We transform to CJS (format: 'cjs') so the facet's `new
380
+ // Function()` runs it as a CJS module body — same path that the
381
+ // bundle's `transformEsmInBundle` (W3.5 Fix B) takes for
382
+ // sub-module ESM files. esbuild's CJS output emits __require /
383
+ // module.exports / exports.X so the facet's pre-compile loop
384
+ // sees ordinary CJS source.
385
+ function nearestPackageTypeIsModule(absPath: string): boolean {
386
+ // Walk up dirs looking for the nearest package.json. First one
387
+ // wins (Node spec); we do NOT consult ancestors past it.
388
+ let dir = absPath.replace(/^\/+/, '');
389
+ const slash = dir.lastIndexOf('/');
390
+ dir = slash > 0 ? dir.substring(0, slash) : '';
391
+ const visited = new Set<string>();
392
+ while (dir && !visited.has(dir)) {
393
+ visited.add(dir);
394
+ const pj = dir + '/package.json';
395
+ if (fs.exists(pj)) {
396
+ try {
397
+ const pkg = JSON.parse(fs.readFileString(pj));
398
+ return pkg && pkg.type === 'module';
399
+ } catch {
400
+ return false;
401
+ }
402
+ }
403
+ const last = dir.lastIndexOf('/');
404
+ if (last <= 0) break;
405
+ dir = dir.substring(0, last);
406
+ }
407
+ return false;
408
+ }
409
+
410
+ const scriptExt = vfsPathExtension(resolvedPath);
411
+ const needsEsmTransform =
412
+ scriptExt === '.mjs' ||
413
+ ((scriptExt === '.js' || scriptExt === '') && nearestPackageTypeIsModule(resolvedPath));
414
+
415
+ // esbuild transform for TypeScript / TSX / JSX (both node and bun)
416
+ // AND for ESM entry scripts (primitive ESM-detect).
417
+ if (
418
+ scriptExt === '.ts' ||
419
+ scriptExt === '.tsx' ||
420
+ scriptExt === '.jsx' ||
421
+ needsEsmTransform
422
+ ) {
423
+ try {
424
+ const eb = getEsbuild();
425
+ const loader =
426
+ scriptExt === '.tsx' ? 'tsx' :
427
+ scriptExt === '.jsx' ? 'jsx' :
428
+ scriptExt === '.ts' ? 'ts' :
429
+ 'js';
430
+ // Substitute `import.meta.url` at compile-time so esbuild's
431
+ // CJS output doesn't reduce it to `undefined` (its default
432
+ // for unknown import.meta references). The substitution
433
+ // value is a real `file://<absolute-path>` URL — exactly
434
+ // what real Node returns when running this script. Tools
435
+ // that compute `fileURLToPath(import.meta.url)` (create-vite,
436
+ // most modern CLIs) then resolve relative paths against
437
+ // the actual script location.
438
+ //
439
+ // Without this, `create-vite` does
440
+ // r(import.meta.url) → fileURLToPath(undefined) → throws
441
+ // → falls into a different code path
442
+ // → readdirSync(wrong-template-dir) returns []
443
+ // → "Scaffolding..." but writes no files.
444
+ const absUrl = 'file:///' + resolvedPath.replace(/^\/+/, '');
445
+ const transformed = await eb.transform(code, {
446
+ loader,
447
+ format: 'cjs',
448
+ define: importMetaDefines(absUrl),
449
+ });
450
+ code = bindImportMetaResolve(transformed.code, absUrl);
451
+ } catch (e: any) {
452
+ ctx.stderr.write(`${name}: transform error for ${scriptPath}: ${e?.message}\n`);
453
+ return 1;
454
+ }
455
+ }
456
+
457
+ const filename = '/' + resolvedPath;
458
+ const dirname = filename.includes('/')
459
+ ? filename.substring(0, filename.lastIndexOf('/'))
460
+ : '/';
461
+
462
+ // Primitive #1 / G4 — propagate bin-spawn ctx if the runtime
463
+ // supports it (currently node only).
464
+ const binSpawn = spec.supportsBinSpawn ? nimbusCtx.__nimbusBinSpawn : undefined;
465
+
466
+ const leadingFlags = args.slice(0, scriptIdx);
467
+ const result = await spec.run(code, {
468
+ cred: ctx.cred,
469
+ argv: [...leadingFlags, filename, ...args.slice(scriptIdx + 1)],
470
+ env: ctx.env,
471
+ cwd: ctx.cwd,
472
+ filename,
473
+ dirname,
474
+ command:
475
+ binSpawn?.command || `${name} ${args.slice(0, scriptIdx + 1).join(' ')}`,
476
+ ...(binSpawn ? {
477
+ skipSpawn: true,
478
+ callerPid: binSpawn.callerPid,
479
+ forceLongRunning: binSpawn.forceLongRunning === true,
480
+ attachedTty: binSpawn.attachedTty === true,
481
+ } : {}),
482
+ ...(captureOutput ? { captureOutput: true } : {}),
483
+ ...(bundleProfile ? { bundleProfile } : {}),
484
+ });
485
+ if (result.stdout) ctx.stdout.write(result.stdout);
486
+ if (result.stderr) ctx.stderr.write(result.stderr);
487
+ return result.exitCode;
488
+ }
489
+
490
+ return async function runtimeHandler(ctx: any): Promise<number> {
491
+ const args: string[] = ctx.args || [];
492
+
493
+ // ── Subcommand dispatch ──
494
+ //
495
+ // BEFORE flag-span computation: subcommands like `bun install`
496
+ // have their first positional arg as the verb, NOT a node-style
497
+ // flag. A verb owns the whole invocation, but it may hand a
498
+ // rewritten argv back to the standard flow — that is how
499
+ // `bun run <file>` reaches the same execution path as `bun <file>`.
500
+ if (spec.subcommands && args.length > 0 && spec.subcommands[args[0]]) {
501
+ return spec.subcommands[args[0]](
502
+ ctx,
503
+ registry,
504
+ (rewritten: string[]) => runtimeInvocation(ctx, rewritten),
505
+ );
506
+ }
507
+
508
+ return runtimeInvocation(ctx, args);
509
+ };
510
+ }
@@ -198,7 +198,21 @@ export function snapshotVfs(
198
198
  }
199
199
 
200
200
  return {
201
- snapshot: { root, roots, preopens: [], files, dirs: Array.from(dirsSet).sort(), modes },
201
+ snapshot: {
202
+ root,
203
+ roots,
204
+ preopens: [],
205
+ files,
206
+ dirs: Array.from(dirsSet).sort(),
207
+ modes,
208
+ // Only when the walk hid nothing. The walk either finishes or returns an
209
+ // error above, so a snapshot taken with no skip list HAS listed each root
210
+ // exhaustively — which is what lets a guest treat a path it does not find
211
+ // as absent instead of asking for it. With the default skip list it has
212
+ // not, and claiming otherwise would make node_modules invisible rather
213
+ // than merely unseeded.
214
+ ...(skipSubdirs.size === 0 ? { enumeratedRoots: roots } : {}),
215
+ },
202
216
  bytes: totalBytes,
203
217
  files: fileCount,
204
218
  };