@tlockney/cdp-kernel 0.2.0 → 0.4.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 (43) hide show
  1. package/README.md +259 -33
  2. package/dist/bun-target.d.ts +60 -0
  3. package/dist/bun-target.js +236 -0
  4. package/dist/celld-target.d.ts +39 -0
  5. package/dist/celld-target.js +274 -0
  6. package/dist/debugger.d.ts +42 -0
  7. package/dist/debugger.js +534 -0
  8. package/dist/history.d.ts +18 -0
  9. package/dist/history.js +45 -0
  10. package/dist/kernel.js +91 -15
  11. package/dist/main.js +8 -6
  12. package/dist/murmur2.d.ts +2 -0
  13. package/dist/murmur2.js +41 -0
  14. package/dist/package.d.ts +16 -2
  15. package/dist/package.js +28 -2
  16. package/dist/sourcemap.d.ts +22 -0
  17. package/dist/sourcemap.js +86 -0
  18. package/dist/target.d.ts +65 -7
  19. package/dist/target.js +57 -6
  20. package/dist/text.d.ts +6 -0
  21. package/dist/text.js +17 -0
  22. package/dist/transform.d.ts +16 -0
  23. package/dist/transform.js +173 -11
  24. package/dist/workerd-target.d.ts +125 -0
  25. package/dist/workerd-target.js +455 -0
  26. package/dist/workers-project.d.ts +27 -0
  27. package/dist/workers-project.js +195 -0
  28. package/dist/ws-client.d.ts +32 -0
  29. package/dist/ws-client.js +166 -0
  30. package/package.json +18 -4
  31. package/runtime/bootstrap.mjs +55 -5
  32. package/runtime/bun-channel.d.mts +3 -0
  33. package/runtime/bun-channel.mjs +58 -0
  34. package/runtime/deno-compat.mjs +3 -1
  35. package/runtime/display.d.mts +2 -0
  36. package/runtime/display.mjs +9 -1
  37. package/runtime/evaluator.d.mts +22 -0
  38. package/runtime/evaluator.mjs +91 -0
  39. package/runtime/introspect.d.mts +13 -0
  40. package/runtime/introspect.mjs +120 -0
  41. package/runtime/node-hooks.d.mts +29 -0
  42. package/runtime/node-hooks.mjs +160 -20
  43. package/runtime/workerd-worker.mjs +194 -0
package/README.md CHANGED
@@ -3,11 +3,12 @@
3
3
  A Jupyter kernel extracted from Deno's (`deno jupyter`, v2.9.6) that drives
4
4
  any V8 runtime over the Chrome DevTools Protocol. The kernel host runs on
5
5
  Node or Deno; the code you type runs in a separate **target** runtime:
6
- Node or Deno today, and anything else whose inspector speaks CDP in
7
- principle.
6
+ Node or Deno over CDP. Bun, workerd and celld evaluate cells in-process
7
+ instead (see [Bun](#bun), [workerd](#workerd) and [celld](#celld)). In
8
+ principle, anything else whose inspector speaks CDP works too.
8
9
 
9
10
  This is early software. It passes a conformance suite derived from Deno's
10
- own Jupyter integration tests on both targets, and it's packaged for npm and
11
+ own Jupyter integration tests on every target, and it's packaged for npm and
11
12
  JSR as `@tlockney/cdp-kernel`, but it hasn't seen much real use yet. The
12
13
  feasibility study behind it covers the design,
13
14
  the other options considered (workerd, Bun, rusty_v8, FFI), and the
@@ -40,17 +41,21 @@ So the extraction is:
40
41
 
41
42
  ## Install
42
43
 
43
- Each install writes a Jupyter kernelspec: `cdp-node`, `cdp-deno`, or
44
- `cdp-node-deno` (Node with the Deno compatibility namespace). Then pick the
45
- kernel in JupyterLab or VS Code.
44
+ Each install writes a Jupyter kernelspec: `cdp-node`, `cdp-deno`,
45
+ `cdp-node-deno` (Node with the Deno compatibility namespace), `cdp-bun`,
46
+ `cdp-workerd`, or `cdp-celld`. Then pick the kernel in JupyterLab or VS Code.
46
47
 
47
- **From npm** (host runs on Node 24+; Node 26 is what's been tested):
48
+ **From npm** (host runs on Node 24+). CI tests Node 24 and 26 with Deno 2.9
49
+ and the latest 2.x, on Linux and macOS. Windows isn't supported yet.
48
50
 
49
51
  ```sh
50
52
  npm install -g @tlockney/cdp-kernel
51
53
  cdp-kernel install --runtime node
52
54
  cdp-kernel install --runtime deno
53
55
  cdp-kernel install --runtime node --deno-compat
56
+ cdp-kernel install --runtime bun
57
+ cdp-kernel install --runtime workerd
58
+ cdp-kernel install --runtime celld
54
59
  ```
55
60
 
56
61
  The kernelspec records the absolute paths of `node` and the installed
@@ -65,15 +70,28 @@ deno run -A jsr:@tlockney/cdp-kernel/cli install --runtime deno
65
70
  The kernelspec pins the release it was installed from
66
71
  (`jsr:@tlockney/cdp-kernel@<version>/cli`). On first launch the target's
67
72
  runtime modules are copied into `~/.cache/cdp-kernel/runtime/`. A Node
68
- target needs `node` on `PATH`.
73
+ target needs `node` on `PATH`, and a Bun target needs `bun` (1.4 is what's
74
+ been tested). A workerd target uses the `workerd` in the notebook
75
+ directory's `node_modules` (a Workers project has one through wrangler),
76
+ or the one on `PATH` (`npm install -g workerd`; 2026-10-15 is what's been
77
+ tested). A celld target uses `~/.local/bin/celld`, where celld's installer
78
+ puts it, or the one on `PATH` (0.6.2 is what's been tested; celld has no
79
+ Windows build).
69
80
 
70
81
  Deno 2.9+ refuses package versions less than 24 hours old by default
71
82
  ([minimum dependency age](https://docs.deno.com/go/minimum-dependency-age)).
72
83
  In a release's first day, the install command fails with "blocked by the
73
- minimum dependency age policy". Wait out the day, or install an earlier
74
- release. Passing `--min-dep-age=0` gets the install through, but the
75
- kernelspec doesn't carry the flag, so the kernel still won't launch until
76
- the release is a day old.
84
+ minimum dependency age policy". Pass `--min-dep-age=0` to the install
85
+ command to get it through:
86
+
87
+ ```sh
88
+ deno run -A --min-dep-age=0 jsr:@tlockney/cdp-kernel/cli install --runtime deno
89
+ ```
90
+
91
+ The kernelspec it writes works on release day too. It relaunches the kernel
92
+ with a dependency cutoff of two days after the release
93
+ (`--min-dep-age=<date>`), so it never resolves anything published after
94
+ that.
77
95
 
78
96
  **From a checkout**, for working on the kernel itself:
79
97
 
@@ -82,6 +100,9 @@ npm ci
82
100
  npm run install:node # kernelspec cdp-node
83
101
  npm run install:deno # kernelspec cdp-deno
84
102
  npm run install:node-deno # kernelspec cdp-node-deno
103
+ npm run install:bun # kernelspec cdp-bun
104
+ npm run install:workerd # kernelspec cdp-workerd
105
+ npm run install:celld # kernelspec cdp-celld
85
106
  ```
86
107
 
87
108
  These kernelspecs point at the checkout, so don't move it after installing.
@@ -103,9 +124,29 @@ In a cell:
103
124
  from the notebook's `node_modules`, `node:` builtins, and `npm:pkg@range`
104
125
  or `jsr:@scope/pkg@range`.
105
126
  - On Node, `npm:` and `jsr:` install on first use into
106
- `~/.cache/cdp-kernel/npm`.
127
+ `~/.cache/cdp-kernel/npm`. An exact version (`npm:react@19.0.0`) installs
128
+ exactly that version. A range (`npm:react@19`) is resolved the first
129
+ time it's imported and stays at that version, even as other packages
130
+ are installed later. Caches written by cdp-kernel 0.3.0 or earlier
131
+ recorded ranges, which later installs could re-resolve. Delete
132
+ `~/.cache/cdp-kernel/npm` to start fresh. `jsr:` works with npm 12,
133
+ which otherwise refuses JSR's registry tarballs.
134
+ - **Lockfile** (cdp-kernel 0.4.0 or later). To pin versions, create a `cdp-kernel.lock.json` in the
135
+ notebook's directory (or above it) containing `{}`. From then on, each
136
+ `npm:`/`jsr:` range is recorded with the exact version it resolved to,
137
+ and installed at that version, so the notebook gets the same packages
138
+ on another machine. Commit it next to the notebook. Deno targets use
139
+ Deno's own resolution and lock instead. Installs into the shared cache
140
+ are serialized, so two kernels installing at once can't corrupt it.
107
141
  - On Deno they use Deno's global cache, unless the notebook is in a Deno
108
142
  project (below).
143
+ - On Bun they install with `bun add` into `~/.cache/cdp-kernel/bun`. Other
144
+ bare imports resolve from the notebook's directory, so without a
145
+ `node_modules` there, Bun auto-installs them.
146
+ - On workerd, the host bundles every import other than a builtin
147
+ (`node:`, `cloudflare:`) with esbuild, using Workers' resolution
148
+ conditions, and sends it with the cell. `npm:` and `jsr:` install into
149
+ the npm cache first, as on Node.
109
150
  - **Deno projects.** If a `deno.json` or `deno.jsonc` sits in the notebook's
110
151
  directory or above it, a Deno target uses it, as `deno jupyter` does:
111
152
  - its import map (`"@/": "./lib/"` aliases, mapped packages);
@@ -120,8 +161,149 @@ In a cell:
120
161
  reported in the first cell's output, and the kernel runs as in a plain
121
162
  directory. Outside a project, the target runs with
122
163
  `--node-modules-dir=none --no-lock`.
123
- - `prompt()` and `confirm()` work over the stdin channel. Interrupt keeps
124
- state.
164
+ - `prompt()` and `confirm()` work over the stdin channel, except on
165
+ workerd. Interrupt keeps state, except for a busy cell on Bun or workerd
166
+ (below).
167
+ - **Shift-Tab** shows a tooltip for the name at the cursor: its type, a
168
+ function's signature (and its source at the second press), a preview, and
169
+ its properties. Nothing is run to get it. On Node and Deno the value is
170
+ evaluated with V8's side-effect check (`throwOnSideEffect`), which also
171
+ guards completion. Bun, workerd and celld look the name up by property,
172
+ without calling getters. Executed cells are kept for `history_request`
173
+ (tail, range and search).
174
+
175
+ ## Bun
176
+
177
+ Bun's `--inspect` speaks the WebKit Inspector Protocol, which lacks what the
178
+ CDP backend relies on: `replMode`, `Runtime.addBinding`,
179
+ `Runtime.terminateExecution`, and top-level `await` in `Runtime.evaluate`.
180
+ So the Bun target has its own backend (`src/bun-target.ts`,
181
+ `runtime/bun-channel.mjs`):
182
+
183
+ - **No inspector.** The host and Bun exchange JSON lines over a pipe on
184
+ fd 3, which only the host holds, so there is no port to discover.
185
+ - **Cells run in-process.** Each cell becomes global code wrapped in an
186
+ async function (`transformCellForEval` in `src/transform.ts`). Its
187
+ top-level declarations become properties of `globalThis`, so later
188
+ cells, and closures from this one, see them. This is the
189
+ tslab/IJavascript technique.
190
+ - **Same output ordering.** stdout and stderr stay pipes, ordered against
191
+ display messages by the same barrier markers as the CDP targets.
192
+ - **Interrupt.** A cell that's waiting on a promise is interrupted in place,
193
+ keeping state. A busy one (`while (true) {}`) can't be reached, so Bun is
194
+ stopped and restarts on the next cell without the earlier state. The cell
195
+ output says so.
196
+
197
+ What differs from the CDP targets is listed under [Known gaps](#known-gaps).
198
+
199
+ ## workerd
200
+
201
+ workerd's inspector can evaluate code, but outside a request there is no
202
+ I/O context: timers, `fetch` and `crypto.randomUUID()` throw "Disallowed
203
+ operation called within global scope". So the workerd target runs its
204
+ cells inside a driver Worker (`src/workerd-target.ts`,
205
+ `runtime/workerd-worker.mjs`):
206
+
207
+ - **One session.** The host opens a single WebSocket to the driver, and
208
+ every cell runs inside that session's request. Cells get Workers'
209
+ `fetch`, `Request`, `caches`, timers and randomness, and anything one
210
+ cell makes carries over to the next: globals, a Durable Object stub, a
211
+ stream, a timer still pending.
212
+ - **A private socket.** Outside a Workers project, the host writes a
213
+ workerd config into a private temporary directory and serves the driver
214
+ on a Unix socket there, opened with a per-launch token. There is no TCP
215
+ port and no inspector.
216
+ - **Cells.** Cells are transformed as for Bun and run by workerd's
217
+ `UnsafeEval` binding (hence `--experimental`).
218
+ - **Output.** `console` output and display messages come back over the
219
+ session as they happen, in order.
220
+ - **Interrupt.** A cell that's waiting on something is abandoned in place,
221
+ keeping state, as on Bun. A busy one gets Bun's treatment too: workerd is
222
+ stopped and restarts on the next cell.
223
+
224
+ What workerd's own process prints, such as its warnings, goes to the Jupyter
225
+ server log, not the notebook.
226
+
227
+ ### Workers projects
228
+
229
+ If a `wrangler.jsonc`, `wrangler.json` or `wrangler.toml` sits in the
230
+ notebook's directory or above it, the driver runs on the project's own
231
+ Miniflare, the simulator `wrangler dev` uses, with the project's bindings
232
+ (`src/workers-project.ts`):
233
+
234
+ - `env` holds the bindings: whatever wrangler translates for Miniflare.
235
+ Vars, KV, D1 and Durable Objects are tested; R2, Queues and the others
236
+ go through the same path but aren't.
237
+ - The project's `main` is bundled into the driver, so its Durable Object
238
+ classes run, and `worker` is its default export:
239
+ `await worker.fetch(new Request("https://x/"), env)`.
240
+ - State lives in the project's `.wrangler/state/cdp-kernel`, beside
241
+ `wrangler dev`'s own, so the two never open the same Durable Object.
242
+ - The first cell's output names the bindings. If the project can't load,
243
+ for example because wrangler isn't installed in it, the first cell says
244
+ why and the kernel runs on plain workerd.
245
+
246
+ wrangler and Miniflare come from the project's `node_modules`, so they match
247
+ its versions. Miniflare 4 and 5 both work. Miniflare listens on a loopback
248
+ TCP port, so there the per-launch token is what keeps other local processes
249
+ out.
250
+
251
+ ## celld
252
+
253
+ [celld](https://github.com/denoland/celld) runs Workers and Durable Objects
254
+ on your own storage. Like workerd it has no I/O outside a request, so the
255
+ celld target runs the same driver Worker over the same WebSocket session
256
+ (`src/celld-target.ts`). Cells, output, interrupts and host-bundled imports
257
+ all work as on workerd. The differences:
258
+
259
+ - **No `UnsafeEval` needed.** celld allows a plain global `eval`, so it runs
260
+ without workerd's `--experimental`.
261
+ - **One module.** The host bundles the driver, and a project's `main`, into
262
+ a single module, and gives celld the result with `no_bundle`. celld
263
+ doesn't need its own esbuild.
264
+ - **Loopback TCP.** celld listens only on TCP, so the session is a loopback
265
+ WebSocket opened with a per-launch token. The kernel picks a free port
266
+ and runs `celld dev --no-watch` on a generated config.
267
+ - **Projects.** In a celld project (a `wrangler.jsonc` or `wrangler.json`
268
+ at or above the notebook), the generated config copies the project's
269
+ compatibility settings and bindings: vars, KV, D1, R2, Durable Objects,
270
+ Queues, Workflows, services, Worker Loaders. `env` and `worker` then work
271
+ as on workerd. The generated config is written to the project's
272
+ `.celld/cdp-kernel` and removed when the kernel stops. The state stays
273
+ there across kernels, beside `celld dev`'s own `.celld/dev`. celld rejects `wrangler.toml`; a project
274
+ that can't load is reported in the first cell, and the driver runs alone.
275
+ - **Stopping.** `celld dev` runs its node in a separate process group and
276
+ waits for it to stop gracefully, which a busy cell never allows. So an
277
+ interrupt of a busy cell kills the node, then `celld dev`.
278
+
279
+ ## Debugger
280
+
281
+ On Node and Deno targets, JupyterLab's visual debugger works: the bug
282
+ button in the notebook toolbar, breakpoints in the cell gutter, the call
283
+ stack, variables and scopes, stepping, pause on exceptions, the debug
284
+ console, and the rich variable viewer. The kernel translates the Jupyter
285
+ debug protocol (DAP over `debug_request`) to V8's `Debugger` domain on the
286
+ session it already holds (`src/debugger.ts`):
287
+
288
+ - **Cell paths.** Each cell is named as JupyterLab names it: a temp-dir
289
+ prefix plus the Murmur2 hash of its code (`src/murmur2.ts`, matched
290
+ against ipykernel's). It's evaluated with that name as its
291
+ `//# sourceURL`, so breakpoints bind to the cell and frames point back at
292
+ it.
293
+ - **Lines.** Breakpoint and frame lines go through the cell's source map
294
+ (`src/sourcemap.ts`), since esbuild rewrites the TypeScript. A breakpoint
295
+ on a blank or type-only line moves to the next statement.
296
+ - **Nothing runs on its own.** The variable explorer between cells reads
297
+ the cells' declarations with V8's side-effect check, so a getter never
298
+ runs. Frames, scopes and the rich viewer evaluate on the paused frame.
299
+ - **Kernelspec.** `metadata.debugger` is set for Node and Deno kernelspecs,
300
+ which is what JupyterLab checks. Reinstall older kernelspecs to get it.
301
+
302
+ `CDPK_DEBUG_TRACE=1` logs every debug request, reply and event to the
303
+ Jupyter server's log.
304
+
305
+ Bun, workerd and celld don't run over an inspector, so they have no
306
+ debugger.
125
307
 
126
308
  ## Editor types
127
309
 
@@ -216,8 +398,8 @@ long cell waits for the cell to finish.
216
398
  ```sh
217
399
  npm run check # tsc --strict over src/ and test/
218
400
  npm test # unit tests (node:test)
219
- npm run e2e # conformance suite via jupyter_client/libzmq, on node, deno, node-deno
220
- npm run test:lab # an anywidget counter in real JupyterLab, headless Chromium (node, deno)
401
+ npm run e2e # conformance suite via jupyter_client/libzmq, on every target variant
402
+ npm run test:lab # an anywidget counter in real JupyterLab, headless Chromium (node, deno, bun, workerd, celld)
221
403
  uv run --with nbclient --with nbformat python examples/run_notebook.py # Deno-style notebook on deno + node-deno
222
404
  ```
223
405
 
@@ -232,12 +414,15 @@ CLI: an installed npm bin, or `deno run -A src/main.ts` to test a Deno host.
232
414
  One version number lives in three places: `package.json`, `deno.jsonc` and
233
415
  `src/package.ts`. `test/package.test.ts` fails if they disagree. To release:
234
416
 
235
- 1. Bump all three, commit, and merge to `main`.
417
+ 1. Bump all three, and set `RELEASE_DATE` in `src/package.ts` to today
418
+ (UTC). Commit, and merge to `main`.
236
419
  2. Tag and push: `git tag v0.1.1 && git push origin v0.1.1`.
237
420
 
238
421
  The `publish` workflow then:
239
422
 
240
- 1. Checks that the tag matches the version.
423
+ 1. Checks that the tag matches the version, and that `RELEASE_DATE` is
424
+ today or yesterday. A JSR kernelspec's dependency cutoff comes from that
425
+ date, so a stale one would block the release it ships in.
241
426
  2. Runs the type check, the unit tests, and the e2e suite against the packed
242
427
  npm tarball and against a Deno host.
243
428
  3. Publishes to JSR (`deno publish`).
@@ -256,22 +441,63 @@ that failed.
256
441
 
257
442
  - Widgets are anywidget-style (`jupyter.widget`). The classic ipywidgets
258
443
  controls (sliders, dropdowns) are Python classes, so they're not available.
259
- - No `inspect_request` content and no history. Deno has neither.
260
- - No debugger.
261
- - `npm:` on Node:
262
- - one npm alias per `name@range`;
263
- - no lockfile;
264
- - peer dependencies resolve by real name from the shared cache.
444
+ - Inspection tooltips show what's found at runtime (type, signature,
445
+ preview, properties), not TypeScript types or JSDoc. On Bun, workerd and
446
+ celld, a getter on the path is reported but not called. History is kept
447
+ for the current kernel session only.
448
+ - The debugger is for Node and Deno targets only. Breakpoints are per line,
449
+ conditional breakpoints and logpoints aren't supported, variables can't be
450
+ set from the debugger, and "copy to globals" isn't offered.
451
+ - `npm:` on Node, Bun, workerd and celld:
452
+ - one npm alias per `name@range` (or exact version, when locked);
453
+ - the cache is shared by every notebook, so peer dependencies resolve by
454
+ real name from it;
455
+ - a project's own `package.json` doesn't decide an `npm:` import's version
456
+ (bare imports do use its `node_modules`).
265
457
  - No type checking. Deno doesn't type-check REPL cells either.
266
- - Results are formatted with `util.inspect` on Node, so they look slightly
267
- different from Deno's `Deno.inspect`.
458
+ - Results are formatted by each runtime's own inspector: `util.inspect` on
459
+ Node, Bun, workerd and celld, and `Deno.inspect` on Deno. On a fixture of
460
+ 30 typical values, 23 format identically on Node and Deno. The rest
461
+ differ in two ways:
462
+ - strings are quoted `'…'` on Node and `"…"` on Deno, wherever they
463
+ appear;
464
+ - a displayed `Error`'s stack also lists Node's own module-loader frames.
465
+
466
+ That's deliberate: each runtime's users see the format they expect, and
467
+ Node's inspector has no quote-style option, short of porting Deno's
468
+ formatter. `test/formatting.test.ts` runs both runtimes on the fixture
469
+ and fails if a new kind of difference appears (#17).
268
470
  - WebGPU texture and buffer display is dropped.
269
- - Targets:
270
- - **Bun** speaks the WebKit inspector protocol: no `replMode`, bindings,
271
- `terminateExecution`, or top-level await in `Runtime.evaluate`.
272
- - **workerd** forbids timers and I/O outside a request.
273
-
274
- Both need a different backend; see the writeup.
471
+ - On Bun:
472
+ - Interrupting a busy cell restarts Bun and loses state. An awaiting cell
473
+ keeps it.
474
+ - Top-level declarations are properties of `globalThis`. A `const` can be
475
+ reassigned, and declaring a global's name (`const fetch = ...`) replaces
476
+ that global rather than shadowing it.
477
+ - Only a trailing expression statement shows a result: `x` does, but
478
+ `if (c) { x }` doesn't.
479
+ - Bun 1.4 can't import a module whose path contains `?`.
480
+ - No `--deno-compat` namespace.
481
+ - On workerd:
482
+ - All cells share one request, so it never ends. Unlike production
483
+ Workers, there is no per-request CPU or duration limit, and
484
+ `ctx.waitUntil` isn't available in a cell.
485
+ - No `prompt()` or `confirm()` answers: they return `null`, as without a
486
+ stdin channel. `jupyter.image(path)` can't read files; pass the bytes.
487
+ - Each imported module is bundled with its dependencies. Two imports that
488
+ share a dependency each get their own copy.
489
+ - Interrupting a busy cell restarts workerd and loses state.
490
+ - Top-level declarations and results behave as on Bun: declarations are
491
+ `globalThis` properties, and only a trailing expression shows a result.
492
+ - In a Workers project, `main` is bundled with esbuild directly, not with
493
+ wrangler's build. Custom `build` commands, `alias` and module `rules`
494
+ aren't applied.
495
+ - On celld, as on workerd, except:
496
+ - Only the binding and compatibility keys of a project's config are
497
+ copied. `assets`, `triggers` and `containers` aren't, and neither is
498
+ `.dev.vars`. Relative paths inside the copied keys resolve from
499
+ `.celld/cdp-kernel`, not from the project.
500
+ - Interrupting a busy cell restarts celld and loses state.
275
501
 
276
502
  ## Provenance
277
503
 
@@ -0,0 +1,60 @@
1
+ import type { CdpParams, EvaluateResult, RemoteObject } from "./cdp.ts";
2
+ import { transformCellForEval } from "./transform.ts";
3
+ import { type KernelTarget, type TargetEvents } from "./target.ts";
4
+ /**
5
+ * How long an interrupted target has to answer a ping. A cell waiting on a
6
+ * promise leaves the event loop free, so the answer is near-instant; a busy
7
+ * one can't answer at all.
8
+ */
9
+ export declare const INTERRUPT_GRACE_MS = 500;
10
+ export declare const RESTART_NOTE: string;
11
+ export declare function bunLaunchCommand(bootstrap: string, executable?: string): {
12
+ cmd: string;
13
+ args: string[];
14
+ };
15
+ export declare class BunTarget implements KernelTarget {
16
+ #private;
17
+ readonly runtime = "bun";
18
+ readonly versions: {
19
+ runtime: string;
20
+ engine: string;
21
+ };
22
+ readonly transform: typeof transformCellForEval;
23
+ private constructor();
24
+ static launch(events: TargetEvents, options?: {
25
+ cwd?: string;
26
+ executable?: string;
27
+ }): Promise<BunTarget>;
28
+ evaluate(script: string): Promise<EvaluateResult>;
29
+ /**
30
+ * There's no terminateExecution, but a cell that's waiting on a promise
31
+ * leaves the event loop free: the target answers a ping, and the cell is
32
+ * abandoned with the session intact, like terminateExecution on an idle
33
+ * isolate (its pending work still runs). A busy cell can't be reached, so
34
+ * the process is stopped and restarts on the next cell, losing its state.
35
+ */
36
+ interrupt(): Promise<void>;
37
+ flush(): Promise<void>;
38
+ broadcastResult(executionCount: number, result: RemoteObject): Promise<void>;
39
+ dispatchComm(msgType: string, content: CdpParams, metadata: CdpParams, buffers: Uint8Array[]): Promise<{
40
+ handled: boolean;
41
+ } | {
42
+ error: {
43
+ name: string;
44
+ message: string;
45
+ stack: string;
46
+ };
47
+ }>;
48
+ describeError(exception: RemoteObject): Promise<{
49
+ name: string;
50
+ message: string;
51
+ stack: string;
52
+ }>;
53
+ propertyNames(expression: string): Promise<string[]>;
54
+ /** Resolved by property descriptor in the target, so nothing runs (#10). */
55
+ inspect(path: string, detail: number): Promise<string | null>;
56
+ /** None: top-level declarations here are properties of globalThis. */
57
+ lexicalNames(): Promise<string[]>;
58
+ replyInput(value: string | null): void;
59
+ kill(): void;
60
+ }
@@ -0,0 +1,236 @@
1
+ // The Bun target (#7). Bun's --inspect speaks the WebKit Inspector Protocol,
2
+ // which has no replMode, Runtime.addBinding or Runtime.terminateExecution, so
3
+ // Bun isn't driven over an inspector at all. The host sends requests over a
4
+ // private pipe (fd 3) to runtime/bun-channel.mjs, which evaluates cells
5
+ // in-process (transformCellForEval). Output ordering is the CDP targets':
6
+ // stdout and stderr are pipes, ordered against channel messages by the
7
+ // bootstrap's barrier markers.
8
+ import { Buffer } from "node:buffer";
9
+ import process from "node:process";
10
+ import { spawn } from "node:child_process";
11
+ import { join } from "node:path";
12
+ import { transformCellForEval } from "./transform.js";
13
+ import { runtimeDir } from "./package.js";
14
+ import { Gates, Interrupted, isTargetMessage, PipeDemux } from "./target.js";
15
+ /**
16
+ * How long an interrupted target has to answer a ping. A cell waiting on a
17
+ * promise leaves the event loop free, so the answer is near-instant; a busy
18
+ * one can't answer at all.
19
+ */
20
+ export const INTERRUPT_GRACE_MS = 500;
21
+ export const RESTART_NOTE = "[cdp-kernel] bun was busy and can't be interrupted in place, so it was stopped; " +
22
+ "it restarts on the next cell, without the earlier cells' state";
23
+ /** Rejects a request whose reply the host no longer waits for. */
24
+ class Abandoned extends Error {
25
+ }
26
+ function isDuplex(stream) {
27
+ return typeof stream?.write === "function" && typeof stream?.on === "function";
28
+ }
29
+ function isRecord(value) {
30
+ return typeof value === "object" && value !== null;
31
+ }
32
+ export function bunLaunchCommand(bootstrap, executable) {
33
+ // No flags: Bun reads the notebook directory's bunfig.toml and package.json
34
+ // on its own, and auto-installs bare imports when there's no node_modules.
35
+ return { cmd: executable ?? "bun", args: [bootstrap] };
36
+ }
37
+ export class BunTarget {
38
+ runtime = "bun";
39
+ versions = { runtime: "", engine: "" };
40
+ transform = transformCellForEval;
41
+ #child;
42
+ #channel;
43
+ #stdout;
44
+ #stderr;
45
+ #gates = new Gates();
46
+ #pending = new Map();
47
+ #nextId = 0;
48
+ #messages = Promise.resolve();
49
+ #interrupted = false;
50
+ #stopNote;
51
+ constructor(child, channel, events) {
52
+ this.#child = child;
53
+ this.#channel = channel;
54
+ this.#stdout = new PipeDemux((text) => events.stream("stdout", text), (n) => this.#gates.wait(n));
55
+ this.#stderr = new PipeDemux((text) => events.stream("stderr", text), (n) => this.#gates.wait(n));
56
+ }
57
+ static async launch(events, options = {}) {
58
+ const bootstrap = join(await runtimeDir(new URL("../runtime/", import.meta.url).href), "bootstrap.mjs");
59
+ const { cmd, args } = bunLaunchCommand(bootstrap, options.executable);
60
+ const env = { ...process.env, FORCE_COLOR: "1" };
61
+ delete env.NO_COLOR;
62
+ const child = spawn(cmd, args, { cwd: options.cwd, stdio: ["pipe", "pipe", "pipe", "pipe"], env });
63
+ const channel = child.stdio[3];
64
+ if (!child.stdout || !child.stderr || !isDuplex(channel)) {
65
+ child.kill();
66
+ throw new Error("could not open the pipes to bun");
67
+ }
68
+ const target = new BunTarget(child, channel, events);
69
+ child.stdout.setEncoding("utf8");
70
+ child.stderr.setEncoding("utf8");
71
+ child.stdout.on("data", (chunk) => target.#stdout.push(chunk));
72
+ const ready = new Promise((resolve, reject) => {
73
+ let early = "";
74
+ const onEarly = (chunk) => (early += chunk);
75
+ child.stderr?.on("data", onEarly);
76
+ target.#onReady = (msg) => {
77
+ child.stderr?.off("data", onEarly);
78
+ if (early)
79
+ target.#stderr.push(early);
80
+ child.stderr?.on("data", (chunk) => target.#stderr.push(chunk));
81
+ resolve(msg);
82
+ };
83
+ child.once("error", reject);
84
+ child.once("exit", (code) => reject(new Error(`${cmd} exited (${code}) before it was ready:\n${early}`)));
85
+ });
86
+ target.#readChannel(events);
87
+ child.once("exit", (code) => target.#exited(code, events));
88
+ const { versions } = await ready;
89
+ if (isRecord(versions))
90
+ Object.assign(target.versions, versions);
91
+ return target;
92
+ }
93
+ #onReady = () => { };
94
+ #readChannel(events) {
95
+ let buffer = "";
96
+ this.#channel.setEncoding("utf8");
97
+ this.#channel.on("data", (chunk) => {
98
+ buffer += chunk;
99
+ for (let nl = buffer.indexOf("\n"); nl !== -1; nl = buffer.indexOf("\n")) {
100
+ const msg = JSON.parse(buffer.slice(0, nl));
101
+ buffer = buffer.slice(nl + 1);
102
+ if (isTargetMessage(msg))
103
+ this.#deliver(msg, events);
104
+ else if (isRecord(msg) && msg.kind === "reply")
105
+ this.#settle(msg);
106
+ else if (isRecord(msg) && msg.kind === "ready")
107
+ this.#onReady(msg);
108
+ }
109
+ });
110
+ // A closed pipe is reported through the child's exit.
111
+ this.#channel.on("error", () => { });
112
+ }
113
+ #settle(reply) {
114
+ const id = Number(reply.id);
115
+ const pending = this.#pending.get(id);
116
+ if (!pending)
117
+ return; // abandoned
118
+ this.#pending.delete(id);
119
+ if (typeof reply.failure === "string")
120
+ pending.reject(new Error(reply.failure));
121
+ else
122
+ pending.resolve(reply);
123
+ }
124
+ #exited(code, events) {
125
+ this.#gates.openAll();
126
+ this.#stdout.close();
127
+ this.#stderr.close();
128
+ events.exit(code, this.#stopNote);
129
+ this.#abandonPending(`bun exited (${code})`);
130
+ }
131
+ #abandonPending(reason) {
132
+ for (const pending of this.#pending.values())
133
+ pending.reject(new Abandoned(reason));
134
+ this.#pending.clear();
135
+ }
136
+ // Binding messages wait until both pipes have caught up to their marker,
137
+ // and are delivered strictly in order (as in Target).
138
+ #deliver(msg, events) {
139
+ this.#messages = this.#messages.then(async () => {
140
+ await Promise.all([this.#stdout.waitFor(msg.barrier), this.#stderr.waitFor(msg.barrier)]);
141
+ events.message(msg);
142
+ this.#gates.open(msg.barrier);
143
+ });
144
+ }
145
+ #request(op, params = {}) {
146
+ const id = ++this.#nextId;
147
+ return new Promise((resolve, reject) => {
148
+ this.#pending.set(id, { resolve, reject });
149
+ this.#channel.write(JSON.stringify({ id, op, ...params }) + "\n");
150
+ });
151
+ }
152
+ /** A request that interrupt() may cut short. */
153
+ async #interruptible(op, params, what) {
154
+ this.#interrupted = false;
155
+ try {
156
+ return await this.#request(op, params);
157
+ }
158
+ catch (err) {
159
+ if (err instanceof Abandoned || this.#interrupted)
160
+ throw new Interrupted(`${what} interrupted`);
161
+ throw err;
162
+ }
163
+ }
164
+ async evaluate(script) {
165
+ return (await this.#interruptible("eval", { code: script }, "Execution"));
166
+ }
167
+ /**
168
+ * There's no terminateExecution, but a cell that's waiting on a promise
169
+ * leaves the event loop free: the target answers a ping, and the cell is
170
+ * abandoned with the session intact, like terminateExecution on an idle
171
+ * isolate (its pending work still runs). A busy cell can't be reached, so
172
+ * the process is stopped and restarts on the next cell, losing its state.
173
+ */
174
+ async interrupt() {
175
+ this.#interrupted = true;
176
+ const answered = await Promise.race([
177
+ this.#request("ping").then(() => true, () => false),
178
+ new Promise((resolve) => setTimeout(() => resolve(false), INTERRUPT_GRACE_MS)),
179
+ ]);
180
+ if (answered) {
181
+ this.#abandonPending("interrupted");
182
+ return;
183
+ }
184
+ this.#stopNote = RESTART_NOTE;
185
+ this.#child.kill("SIGKILL");
186
+ }
187
+ async flush() {
188
+ const { value } = await this.#request("barrier");
189
+ const barrier = Number(value);
190
+ await Promise.all([this.#stdout.waitFor(barrier), this.#stderr.waitFor(barrier)]);
191
+ await this.#messages;
192
+ }
193
+ async broadcastResult(executionCount, result) {
194
+ if (result.objectId === undefined)
195
+ return;
196
+ await this.#request("broadcast", { count: executionCount, handle: result.objectId });
197
+ }
198
+ async dispatchComm(msgType, content, metadata, buffers) {
199
+ const reply = await this.#interruptible("comm", {
200
+ msgType,
201
+ content,
202
+ metadata,
203
+ buffers: buffers.map((b) => Buffer.from(b).toString("base64")),
204
+ }, "Comm handler");
205
+ if (isRecord(reply.error)) {
206
+ const { name, message, stack } = reply.error;
207
+ return { error: { name: String(name), message: String(message), stack: String(stack) } };
208
+ }
209
+ return { handled: reply.handled === true };
210
+ }
211
+ async describeError(exception) {
212
+ if (exception.objectId === undefined)
213
+ return { name: "Error", message: exception.description ?? "", stack: "" };
214
+ const { value } = await this.#request("describe", { handle: exception.objectId });
215
+ return JSON.parse(typeof value === "string" ? value : '{"name":"Error","message":"","stack":""}');
216
+ }
217
+ async propertyNames(expression) {
218
+ const reply = await this.#request("props", { expr: expression }).catch(() => null);
219
+ return Array.isArray(reply?.names) ? reply.names.filter((n) => typeof n === "string") : [];
220
+ }
221
+ /** Resolved by property descriptor in the target, so nothing runs (#10). */
222
+ async inspect(path, detail) {
223
+ const reply = await this.#request("inspect", { path, detail }).catch(() => null);
224
+ return typeof reply?.text === "string" ? reply.text : null;
225
+ }
226
+ /** None: top-level declarations here are properties of globalThis. */
227
+ lexicalNames() {
228
+ return Promise.resolve([]);
229
+ }
230
+ replyInput(value) {
231
+ this.#child.stdin?.write(JSON.stringify({ value }) + "\n");
232
+ }
233
+ kill() {
234
+ this.#child.kill();
235
+ }
236
+ }