@seanhogg/builderforce-webcontainers-core 2026.10.8 → 2026.10.9

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 (281) hide show
  1. package/dist/build.d.ts +6 -0
  2. package/dist/build.d.ts.map +1 -1
  3. package/dist/build.js +9 -1
  4. package/dist/build.js.map +1 -1
  5. package/dist/buildHost.d.ts +3 -0
  6. package/dist/buildHost.d.ts.map +1 -1
  7. package/dist/buildHost.js +25 -12
  8. package/dist/buildHost.js.map +1 -1
  9. package/dist/compileComponent.d.ts.map +1 -1
  10. package/dist/compileComponent.js +4 -1
  11. package/dist/compileComponent.js.map +1 -1
  12. package/dist/compileScript.d.ts +12 -4
  13. package/dist/compileScript.d.ts.map +1 -1
  14. package/dist/compileScript.js +26 -13
  15. package/dist/compileScript.js.map +1 -1
  16. package/dist/components.d.ts +5 -0
  17. package/dist/components.d.ts.map +1 -1
  18. package/dist/components.js.map +1 -1
  19. package/dist/detectProject.d.ts +2 -0
  20. package/dist/detectProject.d.ts.map +1 -1
  21. package/dist/detectProject.js +13 -7
  22. package/dist/detectProject.js.map +1 -1
  23. package/dist/devServer.d.ts +38 -4
  24. package/dist/devServer.d.ts.map +1 -1
  25. package/dist/devServer.js +140 -24
  26. package/dist/devServer.js.map +1 -1
  27. package/dist/devStyles.d.ts +27 -0
  28. package/dist/devStyles.d.ts.map +1 -0
  29. package/dist/devStyles.js +52 -0
  30. package/dist/devStyles.js.map +1 -0
  31. package/dist/hot/client.d.ts +33 -0
  32. package/dist/hot/client.d.ts.map +1 -0
  33. package/dist/hot/client.js +46 -0
  34. package/dist/hot/client.js.map +1 -0
  35. package/dist/hot/moduleGraph.d.ts +39 -0
  36. package/dist/hot/moduleGraph.d.ts.map +1 -0
  37. package/dist/hot/moduleGraph.js +69 -0
  38. package/dist/hot/moduleGraph.js.map +1 -0
  39. package/dist/hot/planUpdate.d.ts +33 -0
  40. package/dist/hot/planUpdate.d.ts.map +1 -0
  41. package/dist/hot/planUpdate.js +43 -0
  42. package/dist/hot/planUpdate.js.map +1 -0
  43. package/dist/hot/reactRefresh.d.ts +45 -0
  44. package/dist/hot/reactRefresh.d.ts.map +1 -0
  45. package/dist/hot/reactRefresh.js +86 -0
  46. package/dist/hot/reactRefresh.js.map +1 -0
  47. package/dist/html.d.ts +2 -8
  48. package/dist/html.d.ts.map +1 -1
  49. package/dist/html.js +11 -17
  50. package/dist/html.js.map +1 -1
  51. package/dist/index.d.ts +19 -1
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +11 -1
  54. package/dist/index.js.map +1 -1
  55. package/dist/installer/tree.d.ts.map +1 -1
  56. package/dist/installer/tree.js +22 -4
  57. package/dist/installer/tree.js.map +1 -1
  58. package/dist/installer/wasmBuilds.d.ts +27 -0
  59. package/dist/installer/wasmBuilds.d.ts.map +1 -0
  60. package/dist/installer/wasmBuilds.js +32 -0
  61. package/dist/installer/wasmBuilds.js.map +1 -0
  62. package/dist/node/builtins.d.ts +6 -1
  63. package/dist/node/builtins.d.ts.map +1 -1
  64. package/dist/node/builtins.js +15 -10
  65. package/dist/node/builtins.js.map +1 -1
  66. package/dist/node/childProcess.d.ts +3 -3
  67. package/dist/node/childProcess.d.ts.map +1 -1
  68. package/dist/node/childProcess.js +68 -16
  69. package/dist/node/childProcess.js.map +1 -1
  70. package/dist/node/crypto.d.ts.map +1 -1
  71. package/dist/node/crypto.js +5 -4
  72. package/dist/node/crypto.js.map +1 -1
  73. package/dist/node/esm.d.ts +7 -0
  74. package/dist/node/esm.d.ts.map +1 -1
  75. package/dist/node/esm.js +28 -2
  76. package/dist/node/esm.js.map +1 -1
  77. package/dist/node/fs/index.d.ts +3 -0
  78. package/dist/node/fs/index.d.ts.map +1 -1
  79. package/dist/node/fs/index.js +22 -106
  80. package/dist/node/fs/index.js.map +1 -1
  81. package/dist/node/fs/stdinFd.d.ts +10 -0
  82. package/dist/node/fs/stdinFd.d.ts.map +1 -0
  83. package/dist/node/fs/stdinFd.js +40 -0
  84. package/dist/node/fs/stdinFd.js.map +1 -0
  85. package/dist/node/fs/streams.d.ts +24 -0
  86. package/dist/node/fs/streams.d.ts.map +1 -0
  87. package/dist/node/fs/streams.js +144 -0
  88. package/dist/node/fs/streams.js.map +1 -0
  89. package/dist/node/fs/sync.d.ts +2 -0
  90. package/dist/node/fs/sync.d.ts.map +1 -1
  91. package/dist/node/fs/sync.js +17 -0
  92. package/dist/node/fs/sync.js.map +1 -1
  93. package/dist/node/globalObject.d.ts +25 -0
  94. package/dist/node/globalObject.d.ts.map +1 -0
  95. package/dist/node/globalObject.js +83 -0
  96. package/dist/node/globalObject.js.map +1 -0
  97. package/dist/node/http/index.d.ts.map +1 -1
  98. package/dist/node/http/index.js +4 -3
  99. package/dist/node/http/index.js.map +1 -1
  100. package/dist/node/http/server.d.ts.map +1 -1
  101. package/dist/node/http/server.js +8 -5
  102. package/dist/node/http/server.js.map +1 -1
  103. package/dist/node/http/serverResponse.d.ts +3 -0
  104. package/dist/node/http/serverResponse.d.ts.map +1 -1
  105. package/dist/node/http/serverResponse.js +7 -0
  106. package/dist/node/http/serverResponse.js.map +1 -1
  107. package/dist/node/ipc.d.ts +26 -0
  108. package/dist/node/ipc.d.ts.map +1 -0
  109. package/dist/node/ipc.js +76 -0
  110. package/dist/node/ipc.js.map +1 -0
  111. package/dist/node/loader.d.ts +2 -0
  112. package/dist/node/loader.d.ts.map +1 -1
  113. package/dist/node/loader.js +79 -14
  114. package/dist/node/loader.js.map +1 -1
  115. package/dist/node/misc.d.ts.map +1 -1
  116. package/dist/node/misc.js +20 -5
  117. package/dist/node/misc.js.map +1 -1
  118. package/dist/node/process.d.ts +2 -5
  119. package/dist/node/process.d.ts.map +1 -1
  120. package/dist/node/process.js +16 -11
  121. package/dist/node/process.js.map +1 -1
  122. package/dist/node/processExit.d.ts +13 -0
  123. package/dist/node/processExit.d.ts.map +1 -0
  124. package/dist/node/processExit.js +25 -0
  125. package/dist/node/processExit.js.map +1 -0
  126. package/dist/node/program.d.ts.map +1 -1
  127. package/dist/node/program.js +6 -5
  128. package/dist/node/program.js.map +1 -1
  129. package/dist/node/runtime.d.ts +6 -1
  130. package/dist/node/runtime.d.ts.map +1 -1
  131. package/dist/node/runtime.js +23 -15
  132. package/dist/node/runtime.js.map +1 -1
  133. package/dist/node/stream/readable.d.ts +2 -1
  134. package/dist/node/stream/readable.d.ts.map +1 -1
  135. package/dist/node/stream/readable.js +2 -1
  136. package/dist/node/stream/readable.js.map +1 -1
  137. package/dist/node/urlqs.d.ts +1 -1
  138. package/dist/node/urlqs.d.ts.map +1 -1
  139. package/dist/node/urlqs.js +11 -2
  140. package/dist/node/urlqs.js.map +1 -1
  141. package/dist/node/workerThreads.d.ts +31 -0
  142. package/dist/node/workerThreads.d.ts.map +1 -0
  143. package/dist/node/workerThreads.js +165 -0
  144. package/dist/node/workerThreads.js.map +1 -0
  145. package/dist/node/zlib.d.ts.map +1 -1
  146. package/dist/node/zlib.js +23 -2
  147. package/dist/node/zlib.js.map +1 -1
  148. package/dist/shell/context.d.ts.map +1 -1
  149. package/dist/shell/context.js +2 -2
  150. package/dist/shell/context.js.map +1 -1
  151. package/dist/shell/devHandoff.d.ts +10 -4
  152. package/dist/shell/devHandoff.d.ts.map +1 -1
  153. package/dist/shell/devHandoff.js +7 -5
  154. package/dist/shell/devHandoff.js.map +1 -1
  155. package/dist/shell/exec.d.ts.map +1 -1
  156. package/dist/shell/exec.js +6 -5
  157. package/dist/shell/exec.js.map +1 -1
  158. package/dist/shell/npm.js +2 -2
  159. package/dist/shell/npm.js.map +1 -1
  160. package/dist/shell/program.js +5 -5
  161. package/dist/shell/program.js.map +1 -1
  162. package/dist/styleModules.d.ts +2 -5
  163. package/dist/styleModules.d.ts.map +1 -1
  164. package/dist/styleModules.js +23 -14
  165. package/dist/styleModules.js.map +1 -1
  166. package/dist/styles/compile.d.ts +18 -0
  167. package/dist/styles/compile.d.ts.map +1 -0
  168. package/dist/styles/compile.js +34 -0
  169. package/dist/styles/compile.js.map +1 -0
  170. package/dist/styles/compilers.d.ts +75 -0
  171. package/dist/styles/compilers.d.ts.map +1 -0
  172. package/dist/styles/compilers.js +77 -0
  173. package/dist/styles/compilers.js.map +1 -0
  174. package/dist/styles/cssModules.d.ts +40 -0
  175. package/dist/styles/cssModules.d.ts.map +1 -0
  176. package/dist/styles/cssModules.js +239 -0
  177. package/dist/styles/cssModules.js.map +1 -0
  178. package/dist/styles/imports.d.ts +37 -0
  179. package/dist/styles/imports.d.ts.map +1 -0
  180. package/dist/styles/imports.js +88 -0
  181. package/dist/styles/imports.js.map +1 -0
  182. package/dist/styles/languages.d.ts +13 -0
  183. package/dist/styles/languages.d.ts.map +1 -0
  184. package/dist/styles/languages.js +20 -0
  185. package/dist/styles/languages.js.map +1 -0
  186. package/dist/styles/packageStyles.d.ts +19 -0
  187. package/dist/styles/packageStyles.d.ts.map +1 -0
  188. package/dist/styles/packageStyles.js +32 -0
  189. package/dist/styles/packageStyles.js.map +1 -0
  190. package/dist/svelteCompiler.d.ts.map +1 -1
  191. package/dist/svelteCompiler.js +19 -2
  192. package/dist/svelteCompiler.js.map +1 -1
  193. package/dist/system/commands.d.ts +2 -0
  194. package/dist/system/commands.d.ts.map +1 -1
  195. package/dist/system/commands.js +18 -0
  196. package/dist/system/commands.js.map +1 -1
  197. package/dist/system/index.d.ts +3 -1
  198. package/dist/system/index.d.ts.map +1 -1
  199. package/dist/system/index.js +1 -0
  200. package/dist/system/index.js.map +1 -1
  201. package/dist/system/ipc.d.ts +33 -0
  202. package/dist/system/ipc.d.ts.map +1 -0
  203. package/dist/system/ipc.js +65 -0
  204. package/dist/system/ipc.js.map +1 -0
  205. package/dist/system/kernel.d.ts.map +1 -1
  206. package/dist/system/kernel.js +6 -3
  207. package/dist/system/kernel.js.map +1 -1
  208. package/dist/system/process.d.ts +3 -0
  209. package/dist/system/process.d.ts.map +1 -1
  210. package/dist/system/process.js +50 -26
  211. package/dist/system/process.js.map +1 -1
  212. package/dist/system/terminal.d.ts +10 -4
  213. package/dist/system/terminal.d.ts.map +1 -1
  214. package/dist/system/terminal.js +21 -13
  215. package/dist/system/terminal.js.map +1 -1
  216. package/dist/system/types.d.ts +29 -7
  217. package/dist/system/types.d.ts.map +1 -1
  218. package/dist/vueCompiler.d.ts.map +1 -1
  219. package/dist/vueCompiler.js +11 -8
  220. package/dist/vueCompiler.js.map +1 -1
  221. package/package.json +4 -1
  222. package/src/build.ts +13 -1
  223. package/src/buildHost.ts +23 -10
  224. package/src/compileComponent.ts +4 -1
  225. package/src/compileScript.ts +30 -11
  226. package/src/components.ts +5 -0
  227. package/src/detectProject.ts +13 -6
  228. package/src/devServer.ts +148 -23
  229. package/src/devStyles.ts +70 -0
  230. package/src/hot/client.ts +70 -0
  231. package/src/hot/moduleGraph.ts +90 -0
  232. package/src/hot/planUpdate.ts +66 -0
  233. package/src/hot/reactRefresh.ts +93 -0
  234. package/src/html.ts +13 -19
  235. package/src/index.ts +19 -1
  236. package/src/installer/tree.ts +22 -4
  237. package/src/installer/wasmBuilds.ts +45 -0
  238. package/src/node/builtins.ts +21 -12
  239. package/src/node/childProcess.ts +73 -20
  240. package/src/node/crypto.ts +5 -4
  241. package/src/node/esm.ts +26 -2
  242. package/src/node/fs/index.ts +26 -103
  243. package/src/node/fs/stdinFd.ts +44 -0
  244. package/src/node/fs/streams.ts +177 -0
  245. package/src/node/fs/sync.ts +15 -0
  246. package/src/node/globalObject.ts +82 -0
  247. package/src/node/http/index.ts +4 -3
  248. package/src/node/http/server.ts +8 -5
  249. package/src/node/http/serverResponse.ts +7 -0
  250. package/src/node/ipc.ts +91 -0
  251. package/src/node/loader.ts +78 -14
  252. package/src/node/misc.ts +19 -5
  253. package/src/node/process.ts +15 -8
  254. package/src/node/processExit.ts +21 -0
  255. package/src/node/program.ts +12 -9
  256. package/src/node/runtime.ts +27 -15
  257. package/src/node/stream/readable.ts +2 -1
  258. package/src/node/urlqs.ts +10 -2
  259. package/src/node/workerThreads.ts +198 -0
  260. package/src/node/zlib.ts +24 -2
  261. package/src/shell/context.ts +2 -2
  262. package/src/shell/devHandoff.ts +16 -9
  263. package/src/shell/exec.ts +6 -5
  264. package/src/shell/npm.ts +2 -2
  265. package/src/shell/program.ts +5 -5
  266. package/src/styleModules.ts +30 -14
  267. package/src/styles/compile.ts +43 -0
  268. package/src/styles/compilers.ts +143 -0
  269. package/src/styles/cssModules.ts +254 -0
  270. package/src/styles/imports.ts +125 -0
  271. package/src/styles/languages.ts +29 -0
  272. package/src/styles/packageStyles.ts +35 -0
  273. package/src/svelteCompiler.ts +20 -3
  274. package/src/system/commands.ts +17 -0
  275. package/src/system/index.ts +4 -0
  276. package/src/system/ipc.ts +89 -0
  277. package/src/system/kernel.ts +6 -3
  278. package/src/system/process.ts +57 -30
  279. package/src/system/terminal.ts +34 -12
  280. package/src/system/types.ts +31 -7
  281. package/src/vueCompiler.ts +11 -8
@@ -5,8 +5,9 @@
5
5
  * program it hosts. Also {@link RuntimeProcess}, the stream-based handle the
6
6
  * public `run()` returns.
7
7
  */
8
- import type { ChildHandle, Program, ProgramIO, ProcessOptions, System } from './types.js';
9
- import { InputQueue, LineDiscipline, toTerminal } from './terminal.js';
8
+ import type { ChildHandle, Program, ProgramIO, ProcessOptions, StdioChunk, System } from './types.js';
9
+ import { InputQueue, LineDiscipline, textChunks, toTerminal } from './terminal.js';
10
+ import type { IpcEndpoint } from './ipc.js';
10
11
 
11
12
  let nextPid = 1000;
12
13
 
@@ -21,26 +22,21 @@ export interface RunProgramOptions extends ProcessOptions {
21
22
  cwd: string;
22
23
  env: Record<string, string>;
23
24
  pid?: number;
24
- }
25
-
26
- /** Decode byte chunks without splitting multi-byte characters across writes. */
27
- function textSink(emit: (text: string) => void): (chunk: string | Uint8Array) => void {
28
- const decoder = new TextDecoder();
29
- return (chunk) => {
30
- const text = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
31
- if (text) emit(text);
32
- };
25
+ /** The child's end of its message channel to the parent (`ipc`); closed when the program exits. */
26
+ channel?: IpcEndpoint;
33
27
  }
34
28
 
35
29
  export function runProgram(options: RunProgramOptions): ChildHandle {
36
30
  const pid = options.pid ?? allocatePid();
37
- const stdin = new InputQueue();
38
- const stdoutListeners = new Set<(chunk: string) => void>();
39
- const stderrListeners = new Set<(chunk: string) => void>();
31
+ const stdin = new InputQueue<StdioChunk>();
32
+ const stdoutListeners = new Set<(chunk: StdioChunk) => void>();
33
+ const stderrListeners = new Set<(chunk: StdioChunk) => void>();
40
34
  const resizeListeners = new Set<(cols: number, rows: number) => void>();
41
35
  const controller = new AbortController();
42
- const emitOut = (text: string) => stdoutListeners.forEach((l) => l(text));
43
- const emitErr = (text: string) => stderrListeners.forEach((l) => l(text));
36
+ // A dead process prints nothing: a straggling callback that writes after exit is dropped.
37
+ // Chunks pass through as written: bytes stay bytes, so a binary protocol over a child's pipes survives.
38
+ const emitOut = (chunk: StdioChunk) => settled || stdoutListeners.forEach((l) => l(chunk));
39
+ const emitErr = (chunk: StdioChunk) => settled || stderrListeners.forEach((l) => l(chunk));
44
40
  let settled = false;
45
41
  let forceExit: (code: number) => void = () => undefined;
46
42
 
@@ -56,8 +52,8 @@ export function runProgram(options: RunProgramOptions): ChildHandle {
56
52
  : undefined;
57
53
 
58
54
  const io: ProgramIO = {
59
- stdout: textSink(emitOut),
60
- stderr: textSink(emitErr),
55
+ stdout: emitOut,
56
+ stderr: emitErr,
61
57
  onStdin: (listener) => stdin.subscribe(listener),
62
58
  terminal: size && discipline
63
59
  ? {
@@ -78,21 +74,47 @@ export function runProgram(options: RunProgramOptions): ChildHandle {
78
74
  : undefined,
79
75
  };
80
76
 
77
+ // Children die with their parent unless `detached`: an orphan here would hold its
78
+ // ports and timers with nothing left that can reach it to stop it.
79
+ const children = new Set<ChildHandle>();
80
+ const system: System = {
81
+ ...options.system,
82
+ spawn(command, args, spawnOptions) {
83
+ const child = options.system.spawn(command, args, spawnOptions);
84
+ if (!spawnOptions?.detached) {
85
+ children.add(child);
86
+ void child.exit.then(() => children.delete(child));
87
+ }
88
+ return child;
89
+ },
90
+ };
91
+ const settle = (resolve: (code: number) => void, code: number) => {
92
+ options.channel?.disconnect();
93
+ settled = true;
94
+ for (const child of children) child.kill('SIGTERM');
95
+ resolve(code);
96
+ };
97
+
81
98
  const exit = new Promise<number>((resolve) => {
82
- forceExit = (code) => {
83
- settled = true;
84
- resolve(code);
85
- };
99
+ forceExit = (code) => settle(resolve, code);
86
100
  queueMicrotask(async () => {
87
101
  let code: number;
88
102
  try {
89
- code = await options.program({ args: options.args, cwd: options.cwd, env: options.env, io, system: options.system, signal: controller.signal });
103
+ code = await options.program({
104
+ args: options.args,
105
+ cwd: options.cwd,
106
+ env: options.env,
107
+ io,
108
+ system,
109
+ signal: controller.signal,
110
+ ipc: options.channel,
111
+ thread: options.thread,
112
+ });
90
113
  } catch (error) {
91
114
  io.stderr(`${error instanceof Error ? error.stack ?? error.message : String(error)}\n`);
92
115
  code = 1;
93
116
  }
94
- settled = true;
95
- resolve(code);
117
+ settle(resolve, code);
96
118
  });
97
119
  });
98
120
 
@@ -107,7 +129,7 @@ export function runProgram(options: RunProgramOptions): ChildHandle {
107
129
  return () => stderrListeners.delete(listener);
108
130
  },
109
131
  write(data) {
110
- if (discipline) discipline.input(data);
132
+ if (discipline) discipline.input(typeof data === 'string' ? data : new TextDecoder().decode(data));
111
133
  else stdin.push(data);
112
134
  },
113
135
  closeStdin: () => stdin.push(null),
@@ -145,10 +167,15 @@ export function runArguments(args: string[] | ProcessOptions = [], options: Proc
145
167
  export function toRuntimeProcess(child: ChildHandle, terminal: boolean): RuntimeProcess {
146
168
  const combinedOutput = new ReadableStream<string>({
147
169
  start(controller) {
148
- const push = (chunk: string) => controller.enqueue(terminal ? toTerminal(chunk) : chunk);
149
- child.onStdout(push);
150
- child.onStderr(push);
151
- void child.exit.then(() => controller.close());
170
+ let open = true;
171
+ const push = (text: string | null) => text !== null && open && controller.enqueue(terminal ? toTerminal(text) : text);
172
+ // One decoder per stream, so a character split across writes on one is not garbled by the other.
173
+ child.onStdout(textChunks(push));
174
+ child.onStderr(textChunks(push));
175
+ void child.exit.then(() => {
176
+ open = false;
177
+ controller.close();
178
+ });
152
179
  },
153
180
  });
154
181
  const stdin = new WritableStream<string>({
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * What a kernel's TTY layer does between xterm.js and a program:
3
3
  *
4
- * - InputQueue: stdin as a buffered stream of string chunks (data that arrives
4
+ * - InputQueue: stdin as a buffered stream of chunks — text, or bytes (data that arrives
5
5
  * before the program subscribes is kept, not dropped).
6
6
  * - LineDiscipline: "cooked" mode — echo, backspace, Ctrl-U, Enter delivers the
7
7
  * line, Ctrl-C raises SIGINT, Ctrl-D is end-of-file. A program that calls
@@ -9,22 +9,24 @@
9
9
  * - toTerminal: `\n` → `\r\n` on output (the TTY's ONLCR), since xterm.js
10
10
  * only moves to column 0 on `\r`.
11
11
  */
12
+ import type { StdioChunk } from './types.js';
12
13
 
13
- export class InputQueue {
14
- private readonly buffered: Array<string | null> = [];
15
- private readonly listeners = new Set<(chunk: string | null) => void>();
14
+
15
+ export class InputQueue<T extends StdioChunk = string> {
16
+ private readonly buffered: Array<T | null> = [];
17
+ private readonly listeners = new Set<(chunk: T | null) => void>();
16
18
  private ended = false;
17
19
 
18
20
  private draining = false;
19
21
 
20
- push(chunk: string | null): void {
22
+ push(chunk: T | null): void {
21
23
  if (this.ended) return;
22
24
  if (chunk === null) this.ended = true;
23
25
  this.buffered.push(chunk);
24
26
  this.drain();
25
27
  }
26
28
 
27
- subscribe(listener: (chunk: string | null) => void): () => void {
29
+ subscribe(listener: (chunk: T | null) => void): () => void {
28
30
  this.listeners.add(listener);
29
31
  this.drain();
30
32
  return () => this.listeners.delete(listener);
@@ -51,12 +53,14 @@ export class InputQueue {
51
53
  readAll(): Promise<string> {
52
54
  return new Promise((resolve) => {
53
55
  let text = '';
54
- const stop = this.subscribe((chunk) => {
55
- if (chunk === null) {
56
- stop();
57
- resolve(text);
58
- } else text += chunk;
59
- });
56
+ const stop = this.subscribe(
57
+ textChunks((chunk) => {
58
+ if (chunk === null) {
59
+ stop();
60
+ resolve(text);
61
+ } else text += chunk;
62
+ }),
63
+ );
60
64
  });
61
65
  }
62
66
 
@@ -65,6 +69,24 @@ export class InputQueue {
65
69
  }
66
70
  }
67
71
 
72
+ /**
73
+ * A text listener over a bytes-or-text stream: bytes are decoded as UTF-8 with
74
+ * characters split across chunks kept whole; `null` (end) flushes, then passes on.
75
+ */
76
+ export function textChunks(listener: (text: string | null) => void): (chunk: StdioChunk | null) => void {
77
+ const decoder = new TextDecoder();
78
+ return (chunk) => {
79
+ if (chunk === null) {
80
+ const rest = decoder.decode();
81
+ if (rest) listener(rest);
82
+ listener(null);
83
+ return;
84
+ }
85
+ const text = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
86
+ if (text) listener(text);
87
+ };
88
+ }
89
+
68
90
  export function toTerminal(text: string): string {
69
91
  return text.replace(/\r?\n/g, '\r\n');
70
92
  }
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import type { VirtualFileSystem } from '../vfs.js';
8
8
  import type { PackageCache } from '../installer/registry.js';
9
+ import type { IpcEndpoint } from './ipc.js';
9
10
 
10
11
  export interface TerminalPort {
11
12
  cols: number;
@@ -15,11 +16,14 @@ export interface TerminalPort {
15
16
  setRawMode(raw: boolean): void;
16
17
  }
17
18
 
19
+ /** What flows through stdio: text, or bytes kept as bytes (binary protocols over a child's pipes). */
20
+ export type StdioChunk = string | Uint8Array;
21
+
18
22
  export interface ProgramIO {
19
- stdout(chunk: string | Uint8Array): void;
20
- stderr(chunk: string | Uint8Array): void;
21
- /** Stdin chunks as they arrive; `null` is end-of-file. Buffered until the first subscriber. */
22
- onStdin(listener: (chunk: string | null) => void): () => void;
23
+ stdout(chunk: StdioChunk): void;
24
+ stderr(chunk: StdioChunk): void;
25
+ /** Stdin chunks as they arrive, bytes or text as written; `null` is end-of-file. Buffered until the first subscriber. Text consumers wrap the listener in `textChunks`. */
26
+ onStdin(listener: (chunk: StdioChunk | null) => void): () => void;
23
27
  /** Present when attached to a terminal (xterm.js). */
24
28
  terminal?: TerminalPort;
25
29
  }
@@ -29,18 +33,34 @@ export interface ProcessOptions {
29
33
  env?: Record<string, string>;
30
34
  /** Attach to a terminal of this size (`isTTY`, line discipline, `\r\n` output). */
31
35
  terminal?: { cols: number; rows: number };
36
+ /** Open a message channel to the child (`child_process.fork`); the handle's `ipc` is the parent's end. */
37
+ ipc?: boolean;
38
+ /** Run as a `worker_threads` Worker: the child's `worker_threads` reports this, over the `ipc` channel. */
39
+ thread?: ThreadInfo;
40
+ /** Outlive the parent (`spawn(…, { detached: true })`). Otherwise a child still running when its parent exits is killed with it. */
41
+ detached?: boolean;
42
+ }
43
+
44
+ /** What a process started as a worker thread sees in `worker_threads`. */
45
+ export interface ThreadInfo {
46
+ threadId: number;
47
+ /** Structured-cloned from the parent's `new Worker(file, { workerData })`. */
48
+ workerData: unknown;
32
49
  }
33
50
 
34
51
  /** A child as its parent sees it (separate stdout/stderr listeners, unlike the streams of the public `RuntimeProcess`). */
35
52
  export interface ChildHandle {
36
53
  readonly pid: number;
37
- onStdout(listener: (chunk: string) => void): () => void;
38
- onStderr(listener: (chunk: string) => void): () => void;
39
- write(data: string): void;
54
+ /** Output as the child wrote it: text, or bytes kept as bytes. */
55
+ onStdout(listener: (chunk: StdioChunk) => void): () => void;
56
+ onStderr(listener: (chunk: StdioChunk) => void): () => void;
57
+ write(data: StdioChunk): void;
40
58
  closeStdin(): void;
41
59
  kill(signal?: string): void;
42
60
  resize?(cols: number, rows: number): void;
43
61
  readonly exit: Promise<number>;
62
+ /** The parent's end of the message channel, when spawned with `ipc`. */
63
+ readonly ipc?: IpcEndpoint;
44
64
  }
45
65
 
46
66
  export interface HttpRequestData {
@@ -131,6 +151,10 @@ export interface ProgramContext {
131
151
  system: System;
132
152
  /** Aborted by kill(): in-process programs stop at their next await. */
133
153
  signal: AbortSignal;
154
+ /** The channel to the parent, when it spawned this process with `ipc` (`process.send`). */
155
+ ipc?: IpcEndpoint;
156
+ /** Set when this process is a worker thread. */
157
+ thread?: ThreadInfo;
134
158
  }
135
159
 
136
160
  /** A program resolves with its exit code. */
@@ -62,15 +62,18 @@ function fail(path: string, problems: Problem[]): never {
62
62
 
63
63
  export function createVueCompiler(sfc: VueCompilerSfcLike): ComponentCompiler {
64
64
  return {
65
- compile(source, { path, dev }) {
65
+ async compile(source, { path, dev, preprocessStyle }) {
66
66
  const { descriptor, errors } = sfc.parse(source, { filename: path, sourceMap: false });
67
67
  if (errors.length) fail(path, errors);
68
68
 
69
- for (const style of descriptor.styles) {
70
- if (style.lang && style.lang !== 'css') {
71
- throw new Error(`${path}: <style lang="${style.lang}"> needs a CSS preprocessor this runtime does not have yet.`);
72
- }
73
- }
69
+ // Sass/Less blocks become CSS first; scoping then works on plain CSS.
70
+ const styleSources = await Promise.all(
71
+ descriptor.styles.map((style) => {
72
+ if (!style.lang || style.lang === 'css') return style.content;
73
+ if (!preprocessStyle) throw new Error(`${path}: <style lang="${style.lang}"> needs a CSS preprocessor, and none is configured.`);
74
+ return preprocessStyle(style.lang, style.content);
75
+ }),
76
+ );
74
77
 
75
78
  // Stable per file, so the scope attribute does not change between reloads.
76
79
  const id = contentHash(path);
@@ -108,8 +111,8 @@ export function createVueCompiler(sfc: VueCompilerSfcLike): ComponentCompiler {
108
111
  parts.push('export default _sfc_main;');
109
112
 
110
113
  const css = descriptor.styles
111
- .map((style) => {
112
- const out = sfc.compileStyle({ source: style.content, filename: path, id: `data-v-${id}`, scoped: !!style.scoped, isProd: !dev });
114
+ .map((style, index) => {
115
+ const out = sfc.compileStyle({ source: styleSources[index]!, filename: path, id: `data-v-${id}`, scoped: !!style.scoped, isProd: !dev });
113
116
  if (out.errors.length) fail(path, out.errors);
114
117
  return out.code;
115
118
  })