@tlockney/cdp-kernel 0.4.0 → 0.6.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 (66) hide show
  1. package/README.md +78 -478
  2. package/THIRD-PARTY-NOTICES +62 -0
  3. package/dist/bun-target.d.ts +2 -2
  4. package/dist/bun-target.js +30 -8
  5. package/dist/cdp.d.ts +8 -1
  6. package/dist/cdp.js +31 -11
  7. package/dist/celld-target.d.ts +1 -1
  8. package/dist/celld-target.js +54 -19
  9. package/dist/children.d.ts +29 -0
  10. package/dist/children.js +114 -0
  11. package/dist/debugger.d.ts +1 -1
  12. package/dist/debugger.js +21 -10
  13. package/dist/deno-types.d.ts +30 -0
  14. package/dist/deno-types.js +93 -0
  15. package/dist/globals.d.ts +3 -3
  16. package/dist/globals.js +3 -3
  17. package/dist/kernel.d.ts +7 -1
  18. package/dist/kernel.js +348 -68
  19. package/dist/language-service-worker.d.ts +1 -0
  20. package/dist/language-service-worker.js +88 -0
  21. package/dist/language-service.d.ts +64 -0
  22. package/dist/language-service.js +128 -0
  23. package/dist/main.js +102 -8
  24. package/dist/mod.d.ts +6 -3
  25. package/dist/mod.js +6 -3
  26. package/dist/notebook-types.d.ts +62 -0
  27. package/dist/notebook-types.js +310 -0
  28. package/dist/package.d.ts +2 -2
  29. package/dist/package.js +3 -3
  30. package/dist/parent.d.ts +12 -0
  31. package/dist/parent.js +39 -0
  32. package/dist/port-holder.d.ts +13 -0
  33. package/dist/port-holder.js +105 -0
  34. package/dist/sourcemap.d.ts +7 -2
  35. package/dist/sourcemap.js +9 -3
  36. package/dist/strip-esbuild.d.ts +2 -0
  37. package/dist/strip-esbuild.js +44 -0
  38. package/dist/strip-sucrase.d.ts +2 -0
  39. package/dist/strip-sucrase.js +78 -0
  40. package/dist/target.d.ts +58 -6
  41. package/dist/target.js +412 -54
  42. package/dist/text.d.ts +5 -1
  43. package/dist/text.js +55 -2
  44. package/dist/transform.d.ts +30 -1
  45. package/dist/transform.js +68 -44
  46. package/dist/wire.d.ts +8 -1
  47. package/dist/wire.js +3 -1
  48. package/dist/workerd-target.d.ts +7 -1
  49. package/dist/workerd-target.js +102 -8
  50. package/dist/workers-project.js +13 -2
  51. package/dist/ws-client.d.ts +5 -1
  52. package/dist/ws-client.js +46 -13
  53. package/dist/zmtp.d.ts +9 -1
  54. package/dist/zmtp.js +42 -1
  55. package/package.json +17 -6
  56. package/runtime/bootstrap.mjs +233 -42
  57. package/runtime/bun-channel.d.mts +3 -0
  58. package/runtime/bun-channel.mjs +2 -2
  59. package/runtime/comm.mjs +11 -1
  60. package/runtime/deno-compat.d.mts +2 -0
  61. package/runtime/deno-compat.mjs +14 -1
  62. package/runtime/display.mjs +8 -1
  63. package/runtime/introspect.mjs +1 -1
  64. package/runtime/node-hooks.d.mts +48 -0
  65. package/runtime/node-hooks.mjs +257 -41
  66. package/runtime/workerd-worker.mjs +77 -6
@@ -0,0 +1,93 @@
1
+ // Types for a Deno target's imports (#275). Deno resolves npm:, jsr:, URLs
2
+ // and its project's import map itself and keeps what it fetched in its own
3
+ // cache, so the language service asks it where a specifier went:
4
+ // `deno info --json` over a one-line module importing it. npm packages map
5
+ // to their directory in Deno's npm cache; remote modules (jsr:, https:)
6
+ // are stored under hashed names without an extension, so the language
7
+ // service sees each under a virtual path built from its URL, which keeps
8
+ // relative imports between them working.
9
+ import { spawnSync } from "node:child_process";
10
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
11
+ import { tmpdir } from "node:os";
12
+ import { join } from "node:path";
13
+ function runDeno(deno) {
14
+ return (args) => {
15
+ const result = spawnSync(deno, args, { encoding: "utf8", timeout: 30_000, maxBuffer: 64 * 1024 * 1024 });
16
+ return { status: result.status, stdout: result.stdout ?? "" };
17
+ };
18
+ }
19
+ export class DenoResolver {
20
+ #run;
21
+ #config;
22
+ /** Where remote modules appear to the language service. */
23
+ root;
24
+ #lookups = new Map();
25
+ /** Virtual path -> the cached file holding that URL's module. */
26
+ #remote = new Map();
27
+ constructor({ deno = "deno", config = null, root }, run = runDeno(deno)) {
28
+ this.#run = run;
29
+ this.#config = config;
30
+ this.root = root;
31
+ }
32
+ /** The virtual path for a remote module's URL, e.g. <root>/https/jsr.io/@std/path/1.1.6/mod.ts. */
33
+ pathFor(url) {
34
+ const m = /^(https?):\/\/(.+)$/.exec(url);
35
+ return m ? join(this.root, m[1], m[2]) : null;
36
+ }
37
+ /** The cached file behind a virtual path, if a lookup has found it. */
38
+ localFile(path) {
39
+ return this.#remote.get(path);
40
+ }
41
+ /** Where `specifier` resolves for Deno, from what's already in its cache. */
42
+ lookup(specifier) {
43
+ if (this.#lookups.has(specifier))
44
+ return this.#lookups.get(specifier) ?? null;
45
+ const resolution = this.#lookup(specifier);
46
+ this.#lookups.set(specifier, resolution);
47
+ return resolution;
48
+ }
49
+ #lookup(specifier) {
50
+ const dir = mkdtempSync(join(tmpdir(), "cdpk-deno-info-"));
51
+ try {
52
+ const entry = join(dir, "entry.ts");
53
+ writeFileSync(entry, `import ${JSON.stringify(specifier)};\n`);
54
+ const flags = this.#config === null ? ["--node-modules-dir=none"] : ["--config", this.#config];
55
+ const result = this.#run(["info", "--json", "--no-lock", ...flags, entry]);
56
+ if (result.status !== 0)
57
+ return null;
58
+ return this.#read(JSON.parse(result.stdout));
59
+ }
60
+ catch {
61
+ return null;
62
+ }
63
+ finally {
64
+ rmSync(dir, { recursive: true, force: true });
65
+ }
66
+ }
67
+ #read(info) {
68
+ for (const mod of info.modules) {
69
+ const path = mod.local === undefined ? null : this.pathFor(mod.specifier);
70
+ if (path !== null && mod.local !== undefined)
71
+ this.#remote.set(path, mod.local);
72
+ }
73
+ const root = info.modules.find((m) => m.specifier === info.roots[0]);
74
+ const dep = root?.dependencies?.[0];
75
+ let target = dep?.code?.specifier ?? dep?.specifier;
76
+ if (target === undefined)
77
+ return null;
78
+ for (let i = 0; i < 10 && info.redirects[target] !== undefined; i++)
79
+ target = info.redirects[target];
80
+ const npm = /^npm:\/?((?:@[^/]+\/)?[^/@]+@[^/]+)(\/.*)?$/.exec(target);
81
+ if (npm) {
82
+ const dir = info.npmPackages[npm[1]]?.localPath;
83
+ return dir === undefined ? null : { kind: "npm", dir, subpath: npm[2] ?? "" };
84
+ }
85
+ const path = this.pathFor(target);
86
+ return path !== null && this.#remote.has(path) ? { kind: "remote", path } : null;
87
+ }
88
+ }
89
+ /** `deno types`: declarations for the Deno namespace and its web globals. */
90
+ export function denoTypes(run = runDeno("deno")) {
91
+ const result = run(["types"]);
92
+ return result.status === 0 ? result.stdout : null;
93
+ }
package/dist/globals.d.ts CHANGED
@@ -2,9 +2,9 @@
2
2
  * Ambient types for notebook cells run by cdp-kernel: the `jupyter` display
3
3
  * namespace and the stdin-backed `prompt`/`confirm`.
4
4
  *
5
- * Load it once per project, for example
6
- * `/// <reference types="@tlockney/cdp-kernel/globals" />` (npm) or
7
- * `import type {} from "jsr:@tlockney/cdp-kernel/globals";` (Deno).
5
+ * Load it once per project, for example with
6
+ * `/// <reference types="@tlockney/cdp-kernel/globals" />`. It ships in the
7
+ * npm package only: Deno notebooks use `Deno.jupyter`, which Deno types.
8
8
  *
9
9
  * A custom display is a class with a `[jupyter.$display]()` method returning
10
10
  * a media bundle. At runtime that key is `Symbol.for("Jupyter.display")`, but
package/dist/globals.js CHANGED
@@ -2,9 +2,9 @@
2
2
  * Ambient types for notebook cells run by cdp-kernel: the `jupyter` display
3
3
  * namespace and the stdin-backed `prompt`/`confirm`.
4
4
  *
5
- * Load it once per project, for example
6
- * `/// <reference types="@tlockney/cdp-kernel/globals" />` (npm) or
7
- * `import type {} from "jsr:@tlockney/cdp-kernel/globals";` (Deno).
5
+ * Load it once per project, for example with
6
+ * `/// <reference types="@tlockney/cdp-kernel/globals" />`. It ships in the
7
+ * npm package only: Deno notebooks use `Deno.jupyter`, which Deno types.
8
8
  *
9
9
  * A custom display is a class with a `[jupyter.$display]()` method returning
10
10
  * a media bundle. At runtime that key is `Symbol.for("Jupyter.display")`, but
package/dist/kernel.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { type RuntimeName } from "./target.ts";
2
+ import { type TypeStripper, type TypeStripperName } from "./transform.ts";
2
3
  export interface ConnectionInfo {
3
4
  ip: string;
4
5
  key: string;
@@ -14,8 +15,13 @@ export declare function parseConnectionInfo(json: string): ConnectionInfo;
14
15
  export declare class Kernel {
15
16
  #private;
16
17
  constructor(conn: ConnectionInfo, runtime: RuntimeName, options?: {
17
- denoCompat?: boolean; /** Defaults to the process cwd, which Jupyter sets. */
18
+ denoCompat?: boolean;
19
+ /** Defaults to the process cwd, which Jupyter sets. */
18
20
  notebookDir?: string;
21
+ /** What strips cells' types; esbuild by default (#238). */
22
+ typeStrip?: TypeStripperName | TypeStripper;
23
+ /** Typed completion from TypeScript's language service; on by default (#273). */
24
+ languageService?: boolean;
19
25
  });
20
26
  run(): Promise<void>;
21
27
  }