@gadgetinc/substrate 0.1.0-rc.1

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 (181) hide show
  1. package/dist/assets/kernel.wasm +0 -0
  2. package/dist/assets/manifest.json +32 -0
  3. package/dist/assets/network-driver.js +2 -0
  4. package/dist/assets/network-driver.js.map +1 -0
  5. package/dist/assets/runtime.js +264 -0
  6. package/dist/assets/runtime.js.map +1 -0
  7. package/dist/binaries.d.ts +58 -0
  8. package/dist/binaries.d.ts.map +1 -0
  9. package/dist/boot/index.d.ts +318 -0
  10. package/dist/boot/index.d.ts.map +1 -0
  11. package/dist/index.d.ts +8 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +4589 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/kernel/bridge.d.ts +64 -0
  16. package/dist/kernel/bridge.d.ts.map +1 -0
  17. package/dist/kernel/types.d.ts +518 -0
  18. package/dist/kernel/types.d.ts.map +1 -0
  19. package/dist/kernel/wasm.d.ts +20 -0
  20. package/dist/kernel/wasm.d.ts.map +1 -0
  21. package/dist/node/assert.d.ts +99 -0
  22. package/dist/node/assert.d.ts.map +1 -0
  23. package/dist/node/async_hooks.d.ts +88 -0
  24. package/dist/node/async_hooks.d.ts.map +1 -0
  25. package/dist/node/buffer.d.ts +62 -0
  26. package/dist/node/buffer.d.ts.map +1 -0
  27. package/dist/node/child_process.d.ts +300 -0
  28. package/dist/node/child_process.d.ts.map +1 -0
  29. package/dist/node/cluster.d.ts +37 -0
  30. package/dist/node/cluster.d.ts.map +1 -0
  31. package/dist/node/constants.d.ts +306 -0
  32. package/dist/node/constants.d.ts.map +1 -0
  33. package/dist/node/crypto.d.ts +176 -0
  34. package/dist/node/crypto.d.ts.map +1 -0
  35. package/dist/node/dgram.d.ts +44 -0
  36. package/dist/node/dgram.d.ts.map +1 -0
  37. package/dist/node/diagnostics_channel.d.ts +57 -0
  38. package/dist/node/diagnostics_channel.d.ts.map +1 -0
  39. package/dist/node/dns.d.ts +278 -0
  40. package/dist/node/dns.d.ts.map +1 -0
  41. package/dist/node/domain.d.ts +21 -0
  42. package/dist/node/domain.d.ts.map +1 -0
  43. package/dist/node/events.d.ts +54 -0
  44. package/dist/node/events.d.ts.map +1 -0
  45. package/dist/node/fs/promises.d.ts +116 -0
  46. package/dist/node/fs/promises.d.ts.map +1 -0
  47. package/dist/node/fs.d.ts +536 -0
  48. package/dist/node/fs.d.ts.map +1 -0
  49. package/dist/node/http.d.ts +471 -0
  50. package/dist/node/http.d.ts.map +1 -0
  51. package/dist/node/http2.d.ts +508 -0
  52. package/dist/node/http2.d.ts.map +1 -0
  53. package/dist/node/https.d.ts +42 -0
  54. package/dist/node/https.d.ts.map +1 -0
  55. package/dist/node/inspector.d.ts +25 -0
  56. package/dist/node/inspector.d.ts.map +1 -0
  57. package/dist/node/net.d.ts +231 -0
  58. package/dist/node/net.d.ts.map +1 -0
  59. package/dist/node/os.d.ts +318 -0
  60. package/dist/node/os.d.ts.map +1 -0
  61. package/dist/node/path.d.ts +144 -0
  62. package/dist/node/path.d.ts.map +1 -0
  63. package/dist/node/process.d.ts +105 -0
  64. package/dist/node/process.d.ts.map +1 -0
  65. package/dist/node/punycode.d.ts +34 -0
  66. package/dist/node/punycode.d.ts.map +1 -0
  67. package/dist/node/querystring.d.ts +49 -0
  68. package/dist/node/querystring.d.ts.map +1 -0
  69. package/dist/node/readline.d.ts +99 -0
  70. package/dist/node/readline.d.ts.map +1 -0
  71. package/dist/node/stream.d.ts +409 -0
  72. package/dist/node/stream.d.ts.map +1 -0
  73. package/dist/node/string_decoder.d.ts +46 -0
  74. package/dist/node/string_decoder.d.ts.map +1 -0
  75. package/dist/node/timers.d.ts +79 -0
  76. package/dist/node/timers.d.ts.map +1 -0
  77. package/dist/node/tls.d.ts +159 -0
  78. package/dist/node/tls.d.ts.map +1 -0
  79. package/dist/node/trace_events.d.ts +18 -0
  80. package/dist/node/trace_events.d.ts.map +1 -0
  81. package/dist/node/tty.d.ts +67 -0
  82. package/dist/node/tty.d.ts.map +1 -0
  83. package/dist/node/url.d.ts +85 -0
  84. package/dist/node/url.d.ts.map +1 -0
  85. package/dist/node/util.d.ts +252 -0
  86. package/dist/node/util.d.ts.map +1 -0
  87. package/dist/node/v8.d.ts +134 -0
  88. package/dist/node/v8.d.ts.map +1 -0
  89. package/dist/node/vm.d.ts +89 -0
  90. package/dist/node/vm.d.ts.map +1 -0
  91. package/dist/node/wasi.d.ts +25 -0
  92. package/dist/node/wasi.d.ts.map +1 -0
  93. package/dist/node/worker_threads.d.ts +206 -0
  94. package/dist/node/worker_threads.d.ts.map +1 -0
  95. package/dist/node/ws.d.ts +110 -0
  96. package/dist/node/ws.d.ts.map +1 -0
  97. package/dist/node/zlib.d.ts +328 -0
  98. package/dist/node/zlib.d.ts.map +1 -0
  99. package/dist/persistence/opfs-store.d.ts +93 -0
  100. package/dist/persistence/opfs-store.d.ts.map +1 -0
  101. package/dist/runtime/async-function-shim.d.ts +27 -0
  102. package/dist/runtime/async-function-shim.d.ts.map +1 -0
  103. package/dist/runtime/fs-interface.d.ts +60 -0
  104. package/dist/runtime/fs-interface.d.ts.map +1 -0
  105. package/dist/runtime/index.d.ts +113 -0
  106. package/dist/runtime/index.d.ts.map +1 -0
  107. package/dist/runtime/loader.d.ts +50 -0
  108. package/dist/runtime/loader.d.ts.map +1 -0
  109. package/dist/runtime/loaders/async-transform.d.ts +40 -0
  110. package/dist/runtime/loaders/async-transform.d.ts.map +1 -0
  111. package/dist/runtime/loaders/cjs.d.ts +37 -0
  112. package/dist/runtime/loaders/cjs.d.ts.map +1 -0
  113. package/dist/runtime/loaders/detect-module-type.d.ts +21 -0
  114. package/dist/runtime/loaders/detect-module-type.d.ts.map +1 -0
  115. package/dist/runtime/loaders/esm.d.ts +140 -0
  116. package/dist/runtime/loaders/esm.d.ts.map +1 -0
  117. package/dist/runtime/loaders/source-map-registry.d.ts +43 -0
  118. package/dist/runtime/loaders/source-map-registry.d.ts.map +1 -0
  119. package/dist/runtime/loaders/source-map.d.ts +50 -0
  120. package/dist/runtime/loaders/source-map.d.ts.map +1 -0
  121. package/dist/runtime/loaders/utils.d.ts +6 -0
  122. package/dist/runtime/loaders/utils.d.ts.map +1 -0
  123. package/dist/runtime/native-globals.d.ts +24 -0
  124. package/dist/runtime/native-globals.d.ts.map +1 -0
  125. package/dist/runtime/network-driver.d.ts +78 -0
  126. package/dist/runtime/network-driver.d.ts.map +1 -0
  127. package/dist/runtime/process-context.d.ts +96 -0
  128. package/dist/runtime/process-context.d.ts.map +1 -0
  129. package/dist/runtime/process-event-loop.d.ts +356 -0
  130. package/dist/runtime/process-event-loop.d.ts.map +1 -0
  131. package/dist/runtime/process-handler.d.ts +71 -0
  132. package/dist/runtime/process-handler.d.ts.map +1 -0
  133. package/dist/runtime/process-handlers/node.d.ts +22 -0
  134. package/dist/runtime/process-handlers/node.d.ts.map +1 -0
  135. package/dist/runtime/process-handlers/npm.d.ts +20 -0
  136. package/dist/runtime/process-handlers/npm.d.ts.map +1 -0
  137. package/dist/runtime/process-handlers/npx.d.ts +11 -0
  138. package/dist/runtime/process-handlers/npx.d.ts.map +1 -0
  139. package/dist/runtime/process-handlers/pnpm.d.ts +12 -0
  140. package/dist/runtime/process-handlers/pnpm.d.ts.map +1 -0
  141. package/dist/runtime/process-handlers/shell.d.ts +24 -0
  142. package/dist/runtime/process-handlers/shell.d.ts.map +1 -0
  143. package/dist/runtime/process-handlers/yarn.d.ts +12 -0
  144. package/dist/runtime/process-handlers/yarn.d.ts.map +1 -0
  145. package/dist/runtime/process-helpers.d.ts +17 -0
  146. package/dist/runtime/process-helpers.d.ts.map +1 -0
  147. package/dist/runtime/process-manager.d.ts +87 -0
  148. package/dist/runtime/process-manager.d.ts.map +1 -0
  149. package/dist/runtime/process-scheduler.d.ts +123 -0
  150. package/dist/runtime/process-scheduler.d.ts.map +1 -0
  151. package/dist/runtime/process-waker.d.ts +24 -0
  152. package/dist/runtime/process-waker.d.ts.map +1 -0
  153. package/dist/runtime/promise.d.ts +44 -0
  154. package/dist/runtime/promise.d.ts.map +1 -0
  155. package/dist/runtime/stack-trace.d.ts +52 -0
  156. package/dist/runtime/stack-trace.d.ts.map +1 -0
  157. package/dist/runtime/wasm-package-interceptor.d.ts +141 -0
  158. package/dist/runtime/wasm-package-interceptor.d.ts.map +1 -0
  159. package/dist/runtime/web-streams.d.ts +57 -0
  160. package/dist/runtime/web-streams.d.ts.map +1 -0
  161. package/dist/runtime/websocket.d.ts +102 -0
  162. package/dist/runtime/websocket.d.ts.map +1 -0
  163. package/dist/shim/websocket-shim-inline.d.ts +9 -0
  164. package/dist/shim/websocket-shim-inline.d.ts.map +1 -0
  165. package/dist/shim/websocket-shim.d.ts +15 -0
  166. package/dist/shim/websocket-shim.d.ts.map +1 -0
  167. package/dist/sw/index.d.ts +53 -0
  168. package/dist/sw/index.d.ts.map +1 -0
  169. package/dist/utils/debug.d.ts +75 -0
  170. package/dist/utils/debug.d.ts.map +1 -0
  171. package/dist/utils/path.d.ts +20 -0
  172. package/dist/utils/path.d.ts.map +1 -0
  173. package/dist/utils/tarball.d.ts +37 -0
  174. package/dist/utils/tarball.d.ts.map +1 -0
  175. package/dist/utils/websocket-protocol.d.ts +102 -0
  176. package/dist/utils/websocket-protocol.d.ts.map +1 -0
  177. package/dist/worker/host.d.ts +14 -0
  178. package/dist/worker/host.d.ts.map +1 -0
  179. package/dist/worker/types.d.ts +209 -0
  180. package/dist/worker/types.d.ts.map +1 -0
  181. package/package.json +53 -0
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Process Manager
3
+ *
4
+ * Manages all process contexts in the runtime. Each process gets its own
5
+ * isolated context with its own event loop, timers, and state.
6
+ *
7
+ * The ProcessManager is the single point of truth for:
8
+ * - Creating new processes (root process, child processes via spawn/fork)
9
+ * - Tracking active processes by PID
10
+ * - Cleaning up exited processes
11
+ * - Routing to the correct ProcessContext
12
+ */
13
+ import type { KernelBridge } from "../kernel/types.js";
14
+ import { ProcessContext, type ProcessCreateOptions } from "./process-context.js";
15
+ /**
16
+ * Process Manager
17
+ *
18
+ * Owns all ProcessContext instances and provides methods for creating,
19
+ * looking up, and destroying processes.
20
+ */
21
+ export declare class ProcessManager {
22
+ /** All active processes, keyed by PID */
23
+ private processes;
24
+ /** Kernel bridge (shared across all processes) */
25
+ private kernel;
26
+ /** Root process (PID 1) */
27
+ private rootProcess;
28
+ constructor(kernel: KernelBridge);
29
+ /**
30
+ * Create the root process (PID 1)
31
+ *
32
+ * This is called once during runtime initialization.
33
+ * The root process is the parent of all other processes.
34
+ *
35
+ * The kernel already creates PID 1 during initialization with cwd="/".
36
+ * We claim PID 1 as our root process and update its cwd if needed.
37
+ */
38
+ createRootProcess(options: {
39
+ cwd: string;
40
+ env: Record<string, string | undefined>;
41
+ argv: string[];
42
+ }): ProcessContext;
43
+ /**
44
+ * Get the root process
45
+ */
46
+ getRootProcess(): ProcessContext;
47
+ /**
48
+ * Create a ProcessContext for an already-spawned child process.
49
+ *
50
+ * The kernel proc_spawn was already called by child_process.spawn(), which
51
+ * assigned the PID. This method creates the JS-side ProcessContext with
52
+ * its own event loop, module cache, etc.
53
+ *
54
+ * @param pid - The kernel-assigned PID (from ChildProcess.pid)
55
+ * @param options - Process options (parent, cwd, env, argv)
56
+ */
57
+ createChildContext(pid: number, options: ProcessCreateOptions): ProcessContext;
58
+ /**
59
+ * Get a process by PID
60
+ */
61
+ getProcess(pid: number): ProcessContext | undefined;
62
+ /**
63
+ * Check if a process exists
64
+ */
65
+ hasProcess(pid: number): boolean;
66
+ /**
67
+ * Get all active processes
68
+ */
69
+ getAllProcesses(): ProcessContext[];
70
+ /**
71
+ * Get the kernel bridge backing this process manager.
72
+ */
73
+ getKernelBridge(): KernelBridge;
74
+ }
75
+ /**
76
+ * Set the current process manager (used by node compat).
77
+ */
78
+ export declare function setCurrentProcessManager(manager: ProcessManager): void;
79
+ /**
80
+ * Get the current process manager (used by node compat).
81
+ */
82
+ export declare function getCurrentProcessManager(): ProcessManager;
83
+ /**
84
+ * Reset the process manager (tests).
85
+ */
86
+ export declare function resetProcessManager(): void;
87
+ //# sourceMappingURL=process-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-manager.d.ts","sourceRoot":"","sources":["../../src/runtime/process-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,KAAK,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAYjF;;;;;GAKG;AACH,qBAAa,cAAc;IACzB,yCAAyC;IACzC,OAAO,CAAC,SAAS,CAAqC;IAEtD,kDAAkD;IAClD,OAAO,CAAC,MAAM,CAAe;IAE7B,2BAA2B;IAC3B,OAAO,CAAC,WAAW,CAA+B;IAElD,YAAY,MAAM,EAAE,YAAY,EAE/B;IAMD;;;;;;;;OAQG;IACH,iBAAiB,CAAC,OAAO,EAAE;QACzB,GAAG,EAAE,MAAM,CAAC;QACZ,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;QACxC,IAAI,EAAE,MAAM,EAAE,CAAC;KAChB,GAAG,cAAc,CA2BjB;IAED;;OAEG;IACH,cAAc,IAAI,cAAc,CAK/B;IAMD;;;;;;;;;OASG;IACH,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,oBAAoB,GAAG,cAAc,CAkB7E;IAMD;;OAEG;IACH,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS,CAElD;IAED;;OAEG;IACH,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE/B;IAED;;OAEG;IACH,eAAe,IAAI,cAAc,EAAE,CAElC;IAED;;OAEG;IACH,eAAe,IAAI,YAAY,CAE9B;CACF;AAID;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI,CAEtE;AAED;;GAEG;AACH,wBAAgB,wBAAwB,IAAI,cAAc,CAKzD;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,IAAI,CAI1C"}
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Global Process Scheduler
3
+ *
4
+ * Manages all process lifecycles independently of their parent's execution context.
5
+ * This ensures child processes continue running even if their parent's Promise
6
+ * chain is disrupted.
7
+ *
8
+ * ## Why This Exists
9
+ *
10
+ * Previously, when a parent process spawned a child, the child's event loop was
11
+ * managed through a Promise chain: `execute().then(handleExit)`. This created a
12
+ * structural dependency where the child's completion handling was tied to the
13
+ * parent's context.
14
+ *
15
+ * The scheduler decouples this by:
16
+ * 1. Owning all process registrations globally
17
+ * 2. Starting event loops in a fire-and-forget manner
18
+ * 3. Handling exit callbacks through its own context, not the parent's
19
+ *
20
+ * ## Usage
21
+ *
22
+ * ```typescript
23
+ * const scheduler = getProcessScheduler();
24
+ *
25
+ * // Register a process with its event loop and exit handler
26
+ * scheduler.register(pid, eventLoop, (exitCode, signal) => {
27
+ * // Called when the process exits - owned by scheduler, not parent
28
+ * child._handleExit(exitCode, signal);
29
+ * });
30
+ *
31
+ * // Start the process - fire and forget
32
+ * scheduler.startProcess(pid, async () => {
33
+ * // Run the script
34
+ * Module.runMain(scriptPath);
35
+ * });
36
+ * ```
37
+ */
38
+ import type { ProcessEventLoop } from "./process-event-loop.js";
39
+ /**
40
+ * Callback invoked when a process exits
41
+ */
42
+ export type ProcessExitCallback = (exitCode: number, signal: string | null) => void;
43
+ /**
44
+ * Global process scheduler that owns all process lifecycles.
45
+ *
46
+ * The scheduler ensures that:
47
+ * 1. Process event loops run independently of their parent's context
48
+ * 2. Exit handling is managed globally, not through parent Promise chains
49
+ * 3. All processes are properly cleaned up when they exit
50
+ */
51
+ declare class ProcessScheduler {
52
+ /** All registered processes */
53
+ private processes;
54
+ /** Whether the scheduler's pump loop is running */
55
+ private pumping;
56
+ /**
57
+ * Register a process with the scheduler.
58
+ *
59
+ * The process's event loop will be managed by the scheduler, and the
60
+ * onExit callback will be called when the event loop completes.
61
+ *
62
+ * @param pid - Process ID
63
+ * @param eventLoop - The process's event loop instance
64
+ * @param onExit - Callback to invoke when the process exits
65
+ */
66
+ register(pid: number, eventLoop: ProcessEventLoop, onExit: ProcessExitCallback): void;
67
+ /**
68
+ * Start a registered process's event loop.
69
+ *
70
+ * This method:
71
+ * 1. Runs the provided script setup function
72
+ * 2. Starts the event loop (fire-and-forget)
73
+ * 3. When the event loop exits, calls the registered onExit callback
74
+ *
75
+ * @param pid - Process ID to start
76
+ * @param runScript - Function that runs the process's script (e.g., Module.runMain)
77
+ */
78
+ startProcess(pid: number, runScript: () => void): void;
79
+ /**
80
+ * Unregister a process from the scheduler.
81
+ *
82
+ * Called when a process is cleaned up externally (e.g., killed, test cleanup).
83
+ */
84
+ unregister(pid: number): void;
85
+ /**
86
+ * Check if a process is registered with the scheduler.
87
+ */
88
+ isRegistered(pid: number): boolean;
89
+ /**
90
+ * Get the number of active processes.
91
+ */
92
+ get activeCount(): number;
93
+ /**
94
+ * Handle a process exit.
95
+ *
96
+ * This is called when a process's event loop completes (either normally or with error).
97
+ * The exit callback is invoked in the scheduler's context, not the parent's.
98
+ */
99
+ private handleProcessExit;
100
+ /**
101
+ * Ensure the pump loop is running.
102
+ *
103
+ * The pump loop keeps the scheduler alive as long as there are processes.
104
+ * It doesn't actively pump event loops (they run themselves via start()),
105
+ * but it prevents the scheduler from being garbage collected.
106
+ */
107
+ private ensurePumping;
108
+ private pumpLoop;
109
+ /**
110
+ * Reset the scheduler (for tests).
111
+ */
112
+ reset(): void;
113
+ }
114
+ /**
115
+ * Get the global process scheduler instance.
116
+ */
117
+ export declare function getProcessScheduler(): ProcessScheduler;
118
+ /**
119
+ * Reset the process scheduler (for tests).
120
+ */
121
+ export declare function resetProcessScheduler(): void;
122
+ export {};
123
+ //# sourceMappingURL=process-scheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-scheduler.d.ts","sourceRoot":"","sources":["../../src/runtime/process-scheduler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAUhE;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,CAAC;AAgBpF;;;;;;;GAOG;AACH,cAAM,gBAAgB;IACpB,+BAA+B;IAC/B,OAAO,CAAC,SAAS,CAAwC;IAEzD,mDAAmD;IACnD,OAAO,CAAC,OAAO,CAAS;IAExB;;;;;;;;;OASG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,EAAE,mBAAmB,GAAG,IAAI,CAiBpF;IAED;;;;;;;;;;OAUG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,IAAI,GAAG,IAAI,CAyDrD;IAED;;;;OAIG;IACH,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAM5B;IAED;;OAEG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEjC;IAED;;OAEG;IACH,IAAI,WAAW,IAAI,MAAM,CAExB;IAED;;;;;OAKG;IACH,OAAO,CAAC,iBAAiB;IAqBzB;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;YAqBP,QAAQ;IAUtB;;OAEG;IACH,KAAK,IAAI,IAAI,CAIZ;CACF;AAQD;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,gBAAgB,CAKtD;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,IAAI,CAK5C"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Process Waker
3
+ *
4
+ * A waker is a signal/wait primitive for async event loops. Code awaits
5
+ * waker.wait() and is woken by waker.signal().
6
+ *
7
+ * All registered wakers are signaled after event-emitting syscalls so that
8
+ * event loops and runtime code can poll the kernel for new events.
9
+ */
10
+ export interface Waker {
11
+ signal: () => void;
12
+ wait: (timeoutMs: number) => Promise<void>;
13
+ isSignaled: () => boolean;
14
+ __getWaiterCount: () => number;
15
+ }
16
+ export declare const createWaker: (label: string) => Waker;
17
+ export declare const registerWaker: (waker: Waker) => void;
18
+ export declare const unregisterWaker: (waker: Waker) => void;
19
+ /**
20
+ * Signal all registered wakers so they poll the kernel for new events.
21
+ * Called after event-emitting syscalls and kernel.deliver().
22
+ */
23
+ export declare const wakeAllWakers: () => void;
24
+ //# sourceMappingURL=process-waker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-waker.d.ts","sourceRoot":"","sources":["../../src/runtime/process-waker.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,MAAM,WAAW,KAAK;IACpB,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,IAAI,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3C,UAAU,EAAE,MAAM,OAAO,CAAC;IAC1B,gBAAgB,EAAE,MAAM,MAAM,CAAC;CAChC;AAED,eAAO,MAAM,WAAW,0BAoDvB,CAAC;AAQF,eAAO,MAAM,aAAa,wBAEzB,CAAC;AAEF,eAAO,MAAM,eAAe,wBAE3B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,YAIzB,CAAC"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Minimal interface for the event loop methods used by SubstratePromise.
3
+ * This avoids exposing ProcessEventLoop's internal types in the public API.
4
+ */
5
+ export interface PromiseEventLoop {
6
+ pid: number;
7
+ queueMicrotask(callback: () => void): void;
8
+ captureAsyncContext(): unknown;
9
+ runInAsyncContext<T>(context: unknown, fn: () => T): T;
10
+ reportUnhandledRejection(reason: unknown, promise: unknown): void;
11
+ }
12
+ /**
13
+ * Interface for the SubstratePromise class returned by createPromiseClass.
14
+ */
15
+ export interface SubstratePromiseConstructor {
16
+ new <T = unknown>(executor: (resolve: (value: T | SubstratePromiseLike<T>) => void, reject: (reason: unknown) => void) => void): SubstratePromiseLike<T>;
17
+ resolve<T>(value: T | SubstratePromiseLike<T>): SubstratePromiseLike<T>;
18
+ reject<T = never>(reason: unknown): SubstratePromiseLike<T>;
19
+ all<T>(iterable: Iterable<T | SubstratePromiseLike<T>>): SubstratePromiseLike<T[]>;
20
+ race<T>(iterable: Iterable<T | SubstratePromiseLike<T>>): SubstratePromiseLike<T>;
21
+ allSettled<T>(iterable: Iterable<T | SubstratePromiseLike<T>>): SubstratePromiseLike<Array<{
22
+ status: "fulfilled";
23
+ value: T;
24
+ } | {
25
+ status: "rejected";
26
+ reason: unknown;
27
+ }>>;
28
+ any<T>(iterable: Iterable<T | SubstratePromiseLike<T>>): SubstratePromiseLike<T>;
29
+ withResolvers<T>(): {
30
+ promise: SubstratePromiseLike<T>;
31
+ resolve: (value: T | SubstratePromiseLike<T>) => void;
32
+ reject: (reason: unknown) => void;
33
+ };
34
+ }
35
+ export interface SubstratePromiseLike<T> {
36
+ then<TResult1 = T, TResult2 = never>(onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, onRejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): SubstratePromiseLike<TResult1 | TResult2>;
37
+ catch<TResult = never>(onRejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): SubstratePromiseLike<T | TResult>;
38
+ finally(onFinally?: (() => unknown) | null): SubstratePromiseLike<T>;
39
+ }
40
+ export declare const createPromiseClass: (eventLoop: PromiseEventLoop) => {
41
+ PromiseClass: SubstratePromiseConstructor;
42
+ checkUnhandledRejections: () => void;
43
+ };
44
+ //# sourceMappingURL=promise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["../../src/runtime/promise.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Stack Trace Improvements
3
+ *
4
+ * Installs Error.prepareStackTrace (V8 API) to:
5
+ * 1. Filter internal runtime frames (loaders, event loop, promise, async transform)
6
+ * 2. Remap substrate:// positions to original source via the source map registry
7
+ * 3. Filter WASM frames
8
+ * 4. Stitch async continuation stacks from the __async helper
9
+ *
10
+ * Also provides Error.captureStackTrace polyfill if missing.
11
+ */
12
+ interface V8CallSite {
13
+ getFileName(): string | null;
14
+ getLineNumber(): number | null;
15
+ getColumnNumber(): number | null;
16
+ getFunctionName(): string | null;
17
+ getMethodName(): string | null;
18
+ getTypeName(): string | null;
19
+ /** Returns the sourceURL from //# sourceURL= annotation if present, otherwise the script name */
20
+ getScriptNameOrSourceURL(): string | null;
21
+ isNative(): boolean;
22
+ isEval(): boolean;
23
+ isConstructor(): boolean;
24
+ isToplevel(): boolean;
25
+ toString(): string;
26
+ }
27
+ /**
28
+ * Determine if a V8 CallSite represents an internal frame that should
29
+ * be hidden from user-facing stack traces.
30
+ *
31
+ * Uses URL-based filtering: only substrate:// frames are user code.
32
+ * Additionally filters async transform trampoline names that appear
33
+ * inside substrate:// URLs.
34
+ */
35
+ export declare function isInternalFrame(site: V8CallSite): boolean;
36
+ /**
37
+ * Filter a pre-formatted stack line (string) for internal frames.
38
+ * Used when processing __asyncStack strings which are already formatted.
39
+ */
40
+ export declare function isInternalFrameLine(line: string): boolean;
41
+ /**
42
+ * Install the Error.prepareStackTrace hook for cleaning and remapping stacks.
43
+ * Safe to call multiple times — only installs once.
44
+ */
45
+ export declare function installStackTraceHook(): void;
46
+ /**
47
+ * Ensure Error.captureStackTrace is available for guest code.
48
+ * V8 in Chrome already provides it natively; this is a safety net.
49
+ */
50
+ export declare function installCaptureStackTrace(): void;
51
+ export {};
52
+ //# sourceMappingURL=stack-trace.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stack-trace.d.ts","sourceRoot":"","sources":["../../src/runtime/stack-trace.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAQH,UAAU,UAAU;IAClB,WAAW,IAAI,MAAM,GAAG,IAAI,CAAC;IAC7B,aAAa,IAAI,MAAM,GAAG,IAAI,CAAC;IAC/B,eAAe,IAAI,MAAM,GAAG,IAAI,CAAC;IACjC,eAAe,IAAI,MAAM,GAAG,IAAI,CAAC;IACjC,aAAa,IAAI,MAAM,GAAG,IAAI,CAAC;IAC/B,WAAW,IAAI,MAAM,GAAG,IAAI,CAAC;IAC7B,iGAAiG;IACjG,wBAAwB,IAAI,MAAM,GAAG,IAAI,CAAC;IAC1C,QAAQ,IAAI,OAAO,CAAC;IACpB,MAAM,IAAI,OAAO,CAAC;IAClB,aAAa,IAAI,OAAO,CAAC;IACzB,UAAU,IAAI,OAAO,CAAC;IACtB,QAAQ,IAAI,MAAM,CAAC;CACpB;AAyCD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAqBzD;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CA0BzD;AA6DD;;;GAGG;AACH,wBAAgB,qBAAqB,IAAI,IAAI,CAuE5C;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,IAAI,IAAI,CAc/C"}
@@ -0,0 +1,141 @@
1
+ /**
2
+ * WASM Package Interceptor
3
+ *
4
+ * Handles automatic installation of WASM alternatives for native binding packages.
5
+ *
6
+ * When files are written to node_modules that match native binding patterns
7
+ * (e.g., @rollup/rollup-linux-x64-gnu), we:
8
+ * 1. Fetch the WASM alternative package tarball
9
+ * 2. Extract it to /node_aliases/ in the VFS
10
+ * 3. Register aliases so native binding imports resolve to the WASM package
11
+ * 4. Register dynamic process handlers for bin stubs (e.g., esbuild)
12
+ *
13
+ * This allows tools like Vite to work in the browser without native binaries.
14
+ */
15
+ /**
16
+ * Serializable WASM package mapping for use in boot configuration.
17
+ * This is the user-facing type — it can be sent over postMessage.
18
+ */
19
+ export interface WasmPackageMapping {
20
+ /** Main package name (e.g., "rollup") */
21
+ packageName: string;
22
+ /** WASM replacement package name (e.g., "@rollup/wasm-node") — version is appended automatically */
23
+ wasmPackage: string;
24
+ /** Regex pattern strings matching native binding packages (e.g., ["^@rollup/rollup-(linux|darwin|win32)-"]) */
25
+ nativeBindingPatterns: string[];
26
+ /** Optional export subpath within the WASM package (e.g., "dist/native.js") */
27
+ wasmExportPath?: string;
28
+ }
29
+ /**
30
+ * Configuration for a package that has native bindings and a WASM alternative.
31
+ */
32
+ export interface WasmPackageConfig {
33
+ /**
34
+ * The main package name (e.g., 'rollup', 'esbuild', 'rolldown')
35
+ */
36
+ packageName: string;
37
+ /**
38
+ * Function to get the WASM package name given the main package version
39
+ */
40
+ getWasmPackage: (version: string) => string;
41
+ /**
42
+ * Patterns matching native binding package names that should be aliased.
43
+ * These are the platform-specific packages like @rollup/rollup-linux-x64-gnu
44
+ */
45
+ nativeBindingPatterns: RegExp[];
46
+ /**
47
+ * The export path within the WASM package that native bindings should resolve to.
48
+ * If not specified, defaults to the package root.
49
+ */
50
+ wasmExportPath?: string;
51
+ }
52
+ /**
53
+ * Register a WASM package configuration.
54
+ */
55
+ export declare function registerWasmPackage(config: WasmPackageConfig): void;
56
+ /**
57
+ * Get config for a package by name
58
+ */
59
+ export declare function getWasmPackageConfig(packageName: string): WasmPackageConfig | undefined;
60
+ /**
61
+ * Initialize the WASM package interceptor.
62
+ * This sets up the native binding resolver, file write hook, and registers default packages.
63
+ * Custom packages are registered after defaults — if a custom package has the same
64
+ * packageName as a default, it overrides it.
65
+ */
66
+ export declare function initWasmPackageInterceptor(customPackages?: WasmPackageMapping[]): void;
67
+ /**
68
+ * Register default WASM package configurations for common tools.
69
+ */
70
+ export declare function registerDefaultWasmPackages(): void;
71
+ /**
72
+ * Check if a file path indicates a native binding package being written.
73
+ * Returns the detected package info if it's a native binding, null otherwise.
74
+ *
75
+ * Called from fsDriver.writeFile to detect when npm installs native bindings.
76
+ */
77
+ export declare function detectNativeBinding(path: string): {
78
+ packageName: string;
79
+ nativePackage: string;
80
+ nodeModulesPath: string;
81
+ } | null;
82
+ /**
83
+ * Handle detection of a native binding package.
84
+ * Called when fsDriver.writeFile detects a native binding package.json.
85
+ *
86
+ * This function:
87
+ * 1. Finds the version of the main package
88
+ * 2. Fetches and installs the WASM alternative
89
+ * 3. Registers aliases for native binding imports
90
+ * 4. Optionally registers a dynamic process handler
91
+ *
92
+ * Returns a promise that resolves when installation is complete.
93
+ *
94
+ * IMPORTANT: This function intentionally avoids async/await. The worker code
95
+ * is transpiled to ES2016, which converts async/await into generators that use
96
+ * globalThis.Promise. When called inside runWithGlobals(), globalThis.Promise
97
+ * is SubstratePromise, whose .then() continuations are scheduled via the
98
+ * calling process's event loop microtask queue. If the process exits before
99
+ * the WASM download completes, those continuations never fire, the install
100
+ * promise never settles, and waitForPendingInstallations() hangs forever.
101
+ * By using explicit nativePromise chains, all continuations run via the
102
+ * browser's native microtask queue, independent of any process event loop.
103
+ */
104
+ export declare function handleNativeBindingDetected(packageName: string, nativePackage: string, nodeModulesPath: string): Promise<void>;
105
+ /**
106
+ * Try to match a module specifier against registered native binding patterns.
107
+ * Returns the target path if matched, null otherwise.
108
+ */
109
+ export declare function tryResolveNativeBinding(specifier: string): string | null;
110
+ /**
111
+ * Scan /node_aliases/ for previously installed WASM packages and register their aliases.
112
+ *
113
+ * Called after mounting a persistent VFS (e.g., booting from an OPFS store) to
114
+ * restore module resolution aliases without re-downloading WASM packages.
115
+ */
116
+ export declare function scanAndRegisterAliases(): void;
117
+ /**
118
+ * Check if there are any pending WASM package installations.
119
+ */
120
+ export declare function hasPendingInstallations(): boolean;
121
+ /**
122
+ * Wait for all pending WASM package installations to complete.
123
+ *
124
+ * Uses nativePromise.all to avoid depending on SubstratePromise scheduling,
125
+ * which would require a live process event loop. The promises in
126
+ * installingPackages are already native Promises (see handleNativeBindingDetected).
127
+ */
128
+ export declare function waitForPendingInstallations(): Promise<void>;
129
+ /**
130
+ * Clear all state (for testing).
131
+ */
132
+ export declare function clearWasmPackageState(): void;
133
+ /**
134
+ * Check if a WASM package is installed.
135
+ */
136
+ export declare function isWasmPackageInstalled(packageName: string, version: string): boolean;
137
+ /**
138
+ * Get the install path for a WASM package.
139
+ */
140
+ export declare function getWasmPackageInstallPath(packageName: string, version: string): string | undefined;
141
+ //# sourceMappingURL=wasm-package-interceptor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wasm-package-interceptor.d.ts","sourceRoot":"","sources":["../../src/runtime/wasm-package-interceptor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAiBH;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,yCAAyC;IACzC,WAAW,EAAE,MAAM,CAAC;IACpB,sGAAoG;IACpG,WAAW,EAAE,MAAM,CAAC;IACpB,+GAA+G;IAC/G,qBAAqB,EAAE,MAAM,EAAE,CAAC;IAChC,+EAA+E;IAC/E,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAcD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,cAAc,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IAE5C;;;OAGG;IACH,qBAAqB,EAAE,MAAM,EAAE,CAAC;IAEhC;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAcD;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,iBAAiB,GAAG,IAAI,CASnE;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,GAAG,iBAAiB,GAAG,SAAS,CAEvF;AAMD;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,cAAc,CAAC,EAAE,kBAAkB,EAAE,GAAG,IAAI,CA0BtF;AAqBD;;GAEG;AACH,wBAAgB,2BAA2B,IAAI,IAAI,CA+BlD;AAMD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,GACX;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAqChF;AAeD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,MAAM,EACnB,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC,IAAI,CAAC,CA2Df;AA8JD;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAQxE;AAKD;;;;;GAKG;AACH,wBAAgB,sBAAsB,IAAI,IAAI,CAkE7C;AAMD;;GAEG;AACH,wBAAgB,uBAAuB,IAAI,OAAO,CAEjD;AAED;;;;;;GAMG;AACH,wBAAgB,2BAA2B,IAAI,OAAO,CAAC,IAAI,CAAC,CAO3D;AAMD;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,IAAI,CAM5C;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAEpF;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,GACd,MAAM,GAAG,SAAS,CAEpB"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Wrapped Web Stream globals for Substrate.
3
+ *
4
+ * Browser-native ReadableStream, WritableStream, and TransformStream return
5
+ * native V8 Promises from their reader/writer/iterator methods. These Promises
6
+ * bypass SubstratePromise's event loop queue, causing microtask escape bugs
7
+ * (SIGSEGV, premature process exit).
8
+ *
9
+ * This module provides wrapped constructors that intercept Promise-returning
10
+ * methods and route them through SubstratePromise with proper ref/unref to
11
+ * keep the event loop alive. The pattern mirrors `createWrappedFetch` in
12
+ * process-event-loop.ts.
13
+ */
14
+ import type { SubstratePromiseConstructor } from "./promise.js";
15
+ /** Unwrap a potentially Proxy-wrapped stream back to its native counterpart. */
16
+ export declare function unwrapNativeStream<T>(stream: T): T;
17
+ /** Minimal interface for the event loop's ref/unref mechanism. */
18
+ interface RefUnref {
19
+ ref(): void;
20
+ unref(): void;
21
+ }
22
+ /**
23
+ * Wrap a native ReadableStream instance via Proxy. Intercepts all
24
+ * Promise-returning methods and routes them through SubstratePromise.
25
+ */
26
+ export declare function wrapReadableStream<R = unknown>(nativeStream: ReadableStream<R>, SP: SubstratePromiseConstructor, eventLoop: RefUnref): ReadableStream<R>;
27
+ /**
28
+ * Wrap a native Response so that `.body` returns a wrapped ReadableStream and
29
+ * body-consuming methods (`.text()`, `.json()`, etc.) return SubstratePromises.
30
+ */
31
+ export declare function wrapFetchResponse(response: Response, SP: SubstratePromiseConstructor, eventLoop: RefUnref): Response;
32
+ /**
33
+ * Create a wrapped Response constructor that unwraps Proxy-wrapped
34
+ * ReadableStreams passed as the body argument.
35
+ *
36
+ * The native Response constructor checks V8 internal slots to recognize
37
+ * ReadableStream bodies. Proxy-wrapped streams don't have these slots,
38
+ * so the constructor falls back to `.toString()` producing
39
+ * "[object ReadableStream]" instead of streaming the body.
40
+ */
41
+ export declare function createWrappedResponse(): typeof globalThis.Response;
42
+ export interface WrappedStreamClasses {
43
+ ReadableStream: typeof globalThis.ReadableStream;
44
+ WritableStream: typeof globalThis.WritableStream;
45
+ TransformStream: typeof globalThis.TransformStream;
46
+ }
47
+ /**
48
+ * Create per-process wrapped ReadableStream, WritableStream, and
49
+ * TransformStream constructors.
50
+ *
51
+ * Each constructor delegates to the native browser constructor then wraps the
52
+ * returned instance so all Promise-returning methods route through
53
+ * SubstratePromise with proper ref/unref.
54
+ */
55
+ export declare function createWrappedStreamClasses(SP: SubstratePromiseConstructor, eventLoop: RefUnref): WrappedStreamClasses;
56
+ export {};
57
+ //# sourceMappingURL=web-streams.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-streams.d.ts","sourceRoot":"","sources":["../../src/runtime/web-streams.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAOH,OAAO,KAAK,EAAE,2BAA2B,EAAwB,MAAM,cAAc,CAAC;AAgBtF,gFAAgF;AAChF,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAElD;AAMD,kEAAkE;AAClE,UAAU,QAAQ;IAChB,GAAG,IAAI,IAAI,CAAC;IACZ,KAAK,IAAI,IAAI,CAAC;CACf;AA0ID;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,OAAO,EAC5C,YAAY,EAAE,cAAc,CAAC,CAAC,CAAC,EAC/B,EAAE,EAAE,2BAA2B,EAC/B,SAAS,EAAE,QAAQ,GAClB,cAAc,CAAC,CAAC,CAAC,CA8EnB;AA4ED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,QAAQ,EAClB,EAAE,EAAE,2BAA2B,EAC/B,SAAS,EAAE,QAAQ,GAClB,QAAQ,CA2BV;AAQD;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,IAAI,OAAO,UAAU,CAAC,QAAQ,CAkBlE;AAMD,MAAM,WAAW,oBAAoB;IACnC,cAAc,EAAE,OAAO,UAAU,CAAC,cAAc,CAAC;IACjD,cAAc,EAAE,OAAO,UAAU,CAAC,cAAc,CAAC;IACjD,eAAe,EAAE,OAAO,UAAU,CAAC,eAAe,CAAC;CACpD;AAED;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CACxC,EAAE,EAAE,2BAA2B,EAC/B,SAAS,EAAE,QAAQ,GAClB,oBAAoB,CAuGtB"}
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Runtime-Level WebSocket Client
3
+ *
4
+ * This is a WebSocket client for use by runtime/orchestration code.
5
+ * It uses syscalls directly and does NOT depend on node compat code.
6
+ *
7
+ * This is different from the node compat WebSocket (src/node/ws.ts) which
8
+ * is for use by guest Node.js code running inside a process context.
9
+ *
10
+ * Architecture rule: Runtime code uses this. Node compat code uses net.Socket.
11
+ */
12
+ import { type WebSocketReadyState } from "../utils/websocket-protocol.js";
13
+ export interface RuntimeWebSocketEvents {
14
+ open: {
15
+ protocol: string;
16
+ };
17
+ message: {
18
+ data: string | ArrayBuffer;
19
+ };
20
+ close: {
21
+ code: number;
22
+ reason: string;
23
+ wasClean: boolean;
24
+ };
25
+ error: {
26
+ message: string;
27
+ };
28
+ }
29
+ type EventCallback<T> = (event: T) => void;
30
+ /**
31
+ * Runtime-level WebSocket client.
32
+ *
33
+ * Uses syscalls directly.
34
+ * Safe to use from runtime/worker host code.
35
+ */
36
+ export declare class RuntimeWebSocket {
37
+ static readonly CONNECTING: 0;
38
+ static readonly OPEN: 1;
39
+ static readonly CLOSING: 2;
40
+ static readonly CLOSED: 3;
41
+ private _readyState;
42
+ private _url;
43
+ private _protocol;
44
+ private _connId;
45
+ private _key;
46
+ private _protocols;
47
+ onopen: EventCallback<RuntimeWebSocketEvents["open"]> | null;
48
+ onmessage: EventCallback<RuntimeWebSocketEvents["message"]> | null;
49
+ onerror: EventCallback<RuntimeWebSocketEvents["error"]> | null;
50
+ onclose: EventCallback<RuntimeWebSocketEvents["close"]> | null;
51
+ private _receiveBuffer;
52
+ private _handshakeComplete;
53
+ private _eqId;
54
+ private _waker;
55
+ constructor(url: string | URL, protocols?: string | string[]);
56
+ get readyState(): WebSocketReadyState;
57
+ get url(): string;
58
+ get protocol(): string;
59
+ /**
60
+ * Connect to the WebSocket server using syscalls directly
61
+ */
62
+ private _connect;
63
+ private _runEventLoop;
64
+ /**
65
+ * Drain all available data from the connection's inbound buffer.
66
+ */
67
+ private _drainInbound;
68
+ /**
69
+ * Handle incoming data
70
+ */
71
+ private _handleData;
72
+ /**
73
+ * Process WebSocket frames from the buffer
74
+ */
75
+ private _processFrames;
76
+ /**
77
+ * Send a WebSocket frame
78
+ */
79
+ private _sendFrame;
80
+ /**
81
+ * Send a close frame
82
+ */
83
+ private _sendCloseFrame;
84
+ /**
85
+ * Clean up resources
86
+ */
87
+ private _cleanup;
88
+ private _emitOpen;
89
+ private _emitMessage;
90
+ private _emitClose;
91
+ private _emitError;
92
+ /**
93
+ * Send data through the WebSocket connection
94
+ */
95
+ send(data: string | ArrayBuffer | Uint8Array): void;
96
+ /**
97
+ * Close the WebSocket connection
98
+ */
99
+ close(code?: number, reason?: string): void;
100
+ }
101
+ export {};
102
+ //# sourceMappingURL=websocket.d.ts.map