@seanhogg/builderforce-webcontainers-core 2026.10.2 → 2026.10.4

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 (426) hide show
  1. package/dist/build.d.ts +42 -0
  2. package/dist/build.d.ts.map +1 -0
  3. package/dist/build.js +65 -0
  4. package/dist/build.js.map +1 -0
  5. package/dist/buildHost.d.ts +31 -0
  6. package/dist/buildHost.d.ts.map +1 -0
  7. package/dist/buildHost.js +125 -0
  8. package/dist/buildHost.js.map +1 -0
  9. package/dist/buildHtml.d.ts +28 -0
  10. package/dist/buildHtml.d.ts.map +1 -0
  11. package/dist/buildHtml.js +74 -0
  12. package/dist/buildHtml.js.map +1 -0
  13. package/dist/bundler.d.ts +129 -0
  14. package/dist/bundler.d.ts.map +1 -0
  15. package/dist/bundler.js +79 -0
  16. package/dist/bundler.js.map +1 -0
  17. package/dist/check/acquire.d.ts +46 -0
  18. package/dist/check/acquire.d.ts.map +1 -0
  19. package/dist/check/acquire.js +179 -0
  20. package/dist/check/acquire.js.map +1 -0
  21. package/dist/check/compilerHost.d.ts +22 -0
  22. package/dist/check/compilerHost.d.ts.map +1 -0
  23. package/dist/check/compilerHost.js +128 -0
  24. package/dist/check/compilerHost.js.map +1 -0
  25. package/dist/check/diagnostics.d.ts +23 -0
  26. package/dist/check/diagnostics.d.ts.map +1 -0
  27. package/dist/check/diagnostics.js +28 -0
  28. package/dist/check/diagnostics.js.map +1 -0
  29. package/dist/check/index.d.ts +19 -0
  30. package/dist/check/index.d.ts.map +1 -0
  31. package/dist/check/index.js +13 -0
  32. package/dist/check/index.js.map +1 -0
  33. package/dist/check/projects.d.ts +26 -0
  34. package/dist/check/projects.d.ts.map +1 -0
  35. package/dist/check/projects.js +124 -0
  36. package/dist/check/projects.js.map +1 -0
  37. package/dist/check/shims.d.ts +29 -0
  38. package/dist/check/shims.d.ts.map +1 -0
  39. package/dist/check/shims.js +76 -0
  40. package/dist/check/shims.js.map +1 -0
  41. package/dist/check/typeStore.d.ts +57 -0
  42. package/dist/check/typeStore.d.ts.map +1 -0
  43. package/dist/check/typeStore.js +89 -0
  44. package/dist/check/typeStore.js.map +1 -0
  45. package/dist/check/typecheck.d.ts +29 -0
  46. package/dist/check/typecheck.d.ts.map +1 -0
  47. package/dist/check/typecheck.js +91 -0
  48. package/dist/check/typecheck.js.map +1 -0
  49. package/dist/compileComponent.d.ts +8 -0
  50. package/dist/compileComponent.d.ts.map +1 -0
  51. package/dist/compileComponent.js +22 -0
  52. package/dist/compileComponent.js.map +1 -0
  53. package/dist/compileScript.d.ts +19 -4
  54. package/dist/compileScript.d.ts.map +1 -1
  55. package/dist/compileScript.js +37 -12
  56. package/dist/compileScript.js.map +1 -1
  57. package/dist/components.d.ts +41 -0
  58. package/dist/components.d.ts.map +1 -0
  59. package/dist/components.js +14 -0
  60. package/dist/components.js.map +1 -0
  61. package/dist/detectProject.d.ts +3 -0
  62. package/dist/detectProject.d.ts.map +1 -1
  63. package/dist/detectProject.js +16 -6
  64. package/dist/detectProject.js.map +1 -1
  65. package/dist/devServer.d.ts +5 -2
  66. package/dist/devServer.d.ts.map +1 -1
  67. package/dist/devServer.js +12 -8
  68. package/dist/devServer.js.map +1 -1
  69. package/dist/hash.d.ts +2 -0
  70. package/dist/hash.d.ts.map +1 -0
  71. package/dist/hash.js +22 -0
  72. package/dist/hash.js.map +1 -0
  73. package/dist/index.d.ts +24 -6
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +14 -3
  76. package/dist/index.js.map +1 -1
  77. package/dist/installer/bins.d.ts +24 -0
  78. package/dist/installer/bins.d.ts.map +1 -0
  79. package/dist/installer/bins.js +56 -0
  80. package/dist/installer/bins.js.map +1 -0
  81. package/dist/installer/index.d.ts +14 -0
  82. package/dist/installer/index.d.ts.map +1 -0
  83. package/dist/installer/index.js +9 -0
  84. package/dist/installer/index.js.map +1 -0
  85. package/dist/installer/install.d.ts +46 -0
  86. package/dist/installer/install.d.ts.map +1 -0
  87. package/dist/installer/install.js +245 -0
  88. package/dist/installer/install.js.map +1 -0
  89. package/dist/installer/lockfile.d.ts +49 -0
  90. package/dist/installer/lockfile.d.ts.map +1 -0
  91. package/dist/installer/lockfile.js +110 -0
  92. package/dist/installer/lockfile.js.map +1 -0
  93. package/dist/installer/registry.d.ts +84 -0
  94. package/dist/installer/registry.d.ts.map +1 -0
  95. package/dist/installer/registry.js +130 -0
  96. package/dist/installer/registry.js.map +1 -0
  97. package/dist/installer/semver.d.ts +38 -0
  98. package/dist/installer/semver.d.ts.map +1 -0
  99. package/dist/installer/semver.js +187 -0
  100. package/dist/installer/semver.js.map +1 -0
  101. package/dist/installer/spec.d.ts +30 -0
  102. package/dist/installer/spec.d.ts.map +1 -0
  103. package/dist/installer/spec.js +30 -0
  104. package/dist/installer/spec.js.map +1 -0
  105. package/dist/installer/tar.d.ts +17 -0
  106. package/dist/installer/tar.d.ts.map +1 -0
  107. package/dist/installer/tar.js +150 -0
  108. package/dist/installer/tar.js.map +1 -0
  109. package/dist/installer/tree.d.ts +48 -0
  110. package/dist/installer/tree.d.ts.map +1 -0
  111. package/dist/installer/tree.js +179 -0
  112. package/dist/installer/tree.js.map +1 -0
  113. package/dist/node/assert.d.ts +15 -0
  114. package/dist/node/assert.d.ts.map +1 -0
  115. package/dist/node/assert.js +137 -0
  116. package/dist/node/assert.js.map +1 -0
  117. package/dist/node/buffer.d.ts +84 -0
  118. package/dist/node/buffer.d.ts.map +1 -0
  119. package/dist/node/buffer.js +304 -0
  120. package/dist/node/buffer.js.map +1 -0
  121. package/dist/node/builtins.d.ts +25 -0
  122. package/dist/node/builtins.d.ts.map +1 -0
  123. package/dist/node/builtins.js +101 -0
  124. package/dist/node/builtins.js.map +1 -0
  125. package/dist/node/callable.d.ts +13 -0
  126. package/dist/node/callable.d.ts.map +1 -0
  127. package/dist/node/callable.js +26 -0
  128. package/dist/node/callable.js.map +1 -0
  129. package/dist/node/childProcess.d.ts +13 -0
  130. package/dist/node/childProcess.d.ts.map +1 -0
  131. package/dist/node/childProcess.js +143 -0
  132. package/dist/node/childProcess.js.map +1 -0
  133. package/dist/node/crypto.d.ts +3 -0
  134. package/dist/node/crypto.d.ts.map +1 -0
  135. package/dist/node/crypto.js +180 -0
  136. package/dist/node/crypto.js.map +1 -0
  137. package/dist/node/esm.d.ts +9 -0
  138. package/dist/node/esm.d.ts.map +1 -0
  139. package/dist/node/esm.js +202 -0
  140. package/dist/node/esm.js.map +1 -0
  141. package/dist/node/events.d.ts +40 -0
  142. package/dist/node/events.d.ts.map +1 -0
  143. package/dist/node/events.js +160 -0
  144. package/dist/node/events.js.map +1 -0
  145. package/dist/node/fs/index.d.ts +7 -0
  146. package/dist/node/fs/index.d.ts.map +1 -0
  147. package/dist/node/fs/index.js +270 -0
  148. package/dist/node/fs/index.js.map +1 -0
  149. package/dist/node/fs/stats.d.ts +93 -0
  150. package/dist/node/fs/stats.d.ts.map +1 -0
  151. package/dist/node/fs/stats.js +104 -0
  152. package/dist/node/fs/stats.js.map +1 -0
  153. package/dist/node/fs/sync.d.ts +98 -0
  154. package/dist/node/fs/sync.d.ts.map +1 -0
  155. package/dist/node/fs/sync.js +348 -0
  156. package/dist/node/fs/sync.js.map +1 -0
  157. package/dist/node/hashes.d.ts +15 -0
  158. package/dist/node/hashes.d.ts.map +1 -0
  159. package/dist/node/hashes.js +195 -0
  160. package/dist/node/hashes.js.map +1 -0
  161. package/dist/node/http/client.d.ts +35 -0
  162. package/dist/node/http/client.d.ts.map +1 -0
  163. package/dist/node/http/client.js +161 -0
  164. package/dist/node/http/client.js.map +1 -0
  165. package/dist/node/http/index.d.ts +5 -0
  166. package/dist/node/http/index.d.ts.map +1 -0
  167. package/dist/node/http/index.js +125 -0
  168. package/dist/node/http/index.js.map +1 -0
  169. package/dist/node/http/server.d.ts +135 -0
  170. package/dist/node/http/server.d.ts.map +1 -0
  171. package/dist/node/http/server.js +308 -0
  172. package/dist/node/http/server.js.map +1 -0
  173. package/dist/node/http/status.d.ts +3 -0
  174. package/dist/node/http/status.d.ts.map +1 -0
  175. package/dist/node/http/status.js +13 -0
  176. package/dist/node/http/status.js.map +1 -0
  177. package/dist/node/inspect.d.ts +22 -0
  178. package/dist/node/inspect.d.ts.map +1 -0
  179. package/dist/node/inspect.js +172 -0
  180. package/dist/node/inspect.js.map +1 -0
  181. package/dist/node/loader.d.ts +91 -0
  182. package/dist/node/loader.d.ts.map +1 -0
  183. package/dist/node/loader.js +265 -0
  184. package/dist/node/loader.js.map +1 -0
  185. package/dist/node/loop.d.ts +71 -0
  186. package/dist/node/loop.d.ts.map +1 -0
  187. package/dist/node/loop.js +189 -0
  188. package/dist/node/loop.js.map +1 -0
  189. package/dist/node/misc.d.ts +10 -0
  190. package/dist/node/misc.d.ts.map +1 -0
  191. package/dist/node/misc.js +256 -0
  192. package/dist/node/misc.js.map +1 -0
  193. package/dist/node/path.d.ts +14 -0
  194. package/dist/node/path.d.ts.map +1 -0
  195. package/dist/node/path.js +128 -0
  196. package/dist/node/path.js.map +1 -0
  197. package/dist/node/process.d.ts +31 -0
  198. package/dist/node/process.d.ts.map +1 -0
  199. package/dist/node/process.js +309 -0
  200. package/dist/node/process.js.map +1 -0
  201. package/dist/node/program.d.ts +19 -0
  202. package/dist/node/program.d.ts.map +1 -0
  203. package/dist/node/program.js +165 -0
  204. package/dist/node/program.js.map +1 -0
  205. package/dist/node/readline.d.ts +2 -0
  206. package/dist/node/readline.d.ts.map +1 -0
  207. package/dist/node/readline.js +155 -0
  208. package/dist/node/readline.js.map +1 -0
  209. package/dist/node/resolve.d.ts +45 -0
  210. package/dist/node/resolve.d.ts.map +1 -0
  211. package/dist/node/resolve.js +206 -0
  212. package/dist/node/resolve.js.map +1 -0
  213. package/dist/node/runtime.d.ts +40 -0
  214. package/dist/node/runtime.d.ts.map +1 -0
  215. package/dist/node/runtime.js +149 -0
  216. package/dist/node/runtime.js.map +1 -0
  217. package/dist/node/stream/index.d.ts +52 -0
  218. package/dist/node/stream/index.d.ts.map +1 -0
  219. package/dist/node/stream/index.js +184 -0
  220. package/dist/node/stream/index.js.map +1 -0
  221. package/dist/node/stream/readable.d.ts +83 -0
  222. package/dist/node/stream/readable.d.ts.map +1 -0
  223. package/dist/node/stream/readable.js +325 -0
  224. package/dist/node/stream/readable.js.map +1 -0
  225. package/dist/node/stream/writable.d.ts +80 -0
  226. package/dist/node/stream/writable.d.ts.map +1 -0
  227. package/dist/node/stream/writable.js +224 -0
  228. package/dist/node/stream/writable.js.map +1 -0
  229. package/dist/node/urlqs.d.ts +22 -0
  230. package/dist/node/urlqs.d.ts.map +1 -0
  231. package/dist/node/urlqs.js +165 -0
  232. package/dist/node/urlqs.js.map +1 -0
  233. package/dist/node/util.d.ts +37 -0
  234. package/dist/node/util.d.ts.map +1 -0
  235. package/dist/node/util.js +160 -0
  236. package/dist/node/util.js.map +1 -0
  237. package/dist/node/zlib.d.ts +2 -0
  238. package/dist/node/zlib.d.ts.map +1 -0
  239. package/dist/node/zlib.js +133 -0
  240. package/dist/node/zlib.js.map +1 -0
  241. package/dist/packageCdn.d.ts +40 -0
  242. package/dist/packageCdn.d.ts.map +1 -1
  243. package/dist/packageCdn.js +54 -7
  244. package/dist/packageCdn.js.map +1 -1
  245. package/dist/projectConfig.d.ts +6 -2
  246. package/dist/projectConfig.d.ts.map +1 -1
  247. package/dist/projectConfig.js +32 -8
  248. package/dist/projectConfig.js.map +1 -1
  249. package/dist/shell/builtins.d.ts +23 -0
  250. package/dist/shell/builtins.d.ts.map +1 -0
  251. package/dist/shell/builtins.js +131 -0
  252. package/dist/shell/builtins.js.map +1 -0
  253. package/dist/shell/context.d.ts +41 -0
  254. package/dist/shell/context.d.ts.map +1 -0
  255. package/dist/shell/context.js +65 -0
  256. package/dist/shell/context.js.map +1 -0
  257. package/dist/shell/devHandoff.d.ts +18 -0
  258. package/dist/shell/devHandoff.d.ts.map +1 -0
  259. package/dist/shell/devHandoff.js +36 -0
  260. package/dist/shell/devHandoff.js.map +1 -0
  261. package/dist/shell/exec.d.ts +67 -0
  262. package/dist/shell/exec.d.ts.map +1 -0
  263. package/dist/shell/exec.js +247 -0
  264. package/dist/shell/exec.js.map +1 -0
  265. package/dist/shell/expand.d.ts +28 -0
  266. package/dist/shell/expand.d.ts.map +1 -0
  267. package/dist/shell/expand.js +149 -0
  268. package/dist/shell/expand.js.map +1 -0
  269. package/dist/shell/fileCommands.d.ts +10 -0
  270. package/dist/shell/fileCommands.d.ts.map +1 -0
  271. package/dist/shell/fileCommands.js +208 -0
  272. package/dist/shell/fileCommands.js.map +1 -0
  273. package/dist/shell/lineEditor.d.ts +43 -0
  274. package/dist/shell/lineEditor.d.ts.map +1 -0
  275. package/dist/shell/lineEditor.js +234 -0
  276. package/dist/shell/lineEditor.js.map +1 -0
  277. package/dist/shell/npm.d.ts +6 -0
  278. package/dist/shell/npm.d.ts.map +1 -0
  279. package/dist/shell/npm.js +234 -0
  280. package/dist/shell/npm.js.map +1 -0
  281. package/dist/shell/parse.d.ts +73 -0
  282. package/dist/shell/parse.d.ts.map +1 -0
  283. package/dist/shell/parse.js +300 -0
  284. package/dist/shell/parse.js.map +1 -0
  285. package/dist/shell/program.d.ts +12 -0
  286. package/dist/shell/program.d.ts.map +1 -0
  287. package/dist/shell/program.js +219 -0
  288. package/dist/shell/program.js.map +1 -0
  289. package/dist/shell/textCommands.d.ts +15 -0
  290. package/dist/shell/textCommands.d.ts.map +1 -0
  291. package/dist/shell/textCommands.js +185 -0
  292. package/dist/shell/textCommands.js.map +1 -0
  293. package/dist/styleModules.d.ts +6 -0
  294. package/dist/styleModules.d.ts.map +1 -1
  295. package/dist/styleModules.js +14 -4
  296. package/dist/styleModules.js.map +1 -1
  297. package/dist/svelteCompiler.d.ts +28 -0
  298. package/dist/svelteCompiler.d.ts.map +1 -0
  299. package/dist/svelteCompiler.js +13 -0
  300. package/dist/svelteCompiler.js.map +1 -0
  301. package/dist/system/commands.d.ts +26 -0
  302. package/dist/system/commands.d.ts.map +1 -0
  303. package/dist/system/commands.js +53 -0
  304. package/dist/system/commands.js.map +1 -0
  305. package/dist/system/index.d.ts +23 -0
  306. package/dist/system/index.d.ts.map +1 -0
  307. package/dist/system/index.js +16 -0
  308. package/dist/system/index.js.map +1 -0
  309. package/dist/system/kernel.d.ts +54 -0
  310. package/dist/system/kernel.d.ts.map +1 -0
  311. package/dist/system/kernel.js +81 -0
  312. package/dist/system/kernel.js.map +1 -0
  313. package/dist/system/ports.d.ts +32 -0
  314. package/dist/system/ports.d.ts.map +1 -0
  315. package/dist/system/ports.js +57 -0
  316. package/dist/system/ports.js.map +1 -0
  317. package/dist/system/process.d.ts +32 -0
  318. package/dist/system/process.d.ts.map +1 -0
  319. package/dist/system/process.js +130 -0
  320. package/dist/system/process.js.map +1 -0
  321. package/dist/system/programs.d.ts +10 -0
  322. package/dist/system/programs.d.ts.map +1 -0
  323. package/dist/system/programs.js +22 -0
  324. package/dist/system/programs.js.map +1 -0
  325. package/dist/system/terminal.d.ts +45 -0
  326. package/dist/system/terminal.d.ts.map +1 -0
  327. package/dist/system/terminal.js +127 -0
  328. package/dist/system/terminal.js.map +1 -0
  329. package/dist/system/types.d.ts +100 -0
  330. package/dist/system/types.d.ts.map +1 -0
  331. package/dist/system/types.js +2 -0
  332. package/dist/system/types.js.map +1 -0
  333. package/dist/vfs.d.ts +28 -4
  334. package/dist/vfs.d.ts.map +1 -1
  335. package/dist/vfs.js +95 -32
  336. package/dist/vfs.js.map +1 -1
  337. package/dist/vueCompiler.d.ts +75 -0
  338. package/dist/vueCompiler.d.ts.map +1 -0
  339. package/dist/vueCompiler.js +67 -0
  340. package/dist/vueCompiler.js.map +1 -0
  341. package/package.json +18 -2
  342. package/src/build.ts +105 -0
  343. package/src/buildHost.ts +154 -0
  344. package/src/buildHtml.ts +101 -0
  345. package/src/bundler.ts +190 -0
  346. package/src/check/acquire.ts +212 -0
  347. package/src/check/compilerHost.ts +138 -0
  348. package/src/check/diagnostics.ts +48 -0
  349. package/src/check/index.ts +28 -0
  350. package/src/check/projects.ts +149 -0
  351. package/src/check/shims.ts +88 -0
  352. package/src/check/typeStore.ts +103 -0
  353. package/src/check/typecheck.ts +134 -0
  354. package/src/compileComponent.ts +21 -0
  355. package/src/compileScript.ts +42 -13
  356. package/src/components.ts +59 -0
  357. package/src/detectProject.ts +18 -6
  358. package/src/devServer.ts +16 -11
  359. package/src/hash.ts +22 -0
  360. package/src/index.ts +35 -6
  361. package/src/installer/bins.ts +64 -0
  362. package/src/installer/index.ts +13 -0
  363. package/src/installer/install.ts +275 -0
  364. package/src/installer/lockfile.ts +146 -0
  365. package/src/installer/registry.ts +175 -0
  366. package/src/installer/semver.ts +190 -0
  367. package/src/installer/spec.ts +53 -0
  368. package/src/installer/tar.ts +155 -0
  369. package/src/installer/tree.ts +236 -0
  370. package/src/node/assert.ts +127 -0
  371. package/src/node/buffer.ts +304 -0
  372. package/src/node/builtins.ts +123 -0
  373. package/src/node/callable.ts +25 -0
  374. package/src/node/childProcess.ts +164 -0
  375. package/src/node/crypto.ts +187 -0
  376. package/src/node/esm.ts +196 -0
  377. package/src/node/events.ts +176 -0
  378. package/src/node/fs/index.ts +277 -0
  379. package/src/node/fs/stats.ts +110 -0
  380. package/src/node/fs/sync.ts +335 -0
  381. package/src/node/hashes.ts +171 -0
  382. package/src/node/http/client.ts +173 -0
  383. package/src/node/http/index.ts +122 -0
  384. package/src/node/http/server.ts +308 -0
  385. package/src/node/http/status.ts +13 -0
  386. package/src/node/inspect.ts +166 -0
  387. package/src/node/loader.ts +293 -0
  388. package/src/node/loop.ts +193 -0
  389. package/src/node/misc.ts +250 -0
  390. package/src/node/path.ts +135 -0
  391. package/src/node/process.ts +324 -0
  392. package/src/node/program.ts +170 -0
  393. package/src/node/readline.ts +157 -0
  394. package/src/node/resolve.ts +216 -0
  395. package/src/node/runtime.ts +184 -0
  396. package/src/node/stream/index.ts +196 -0
  397. package/src/node/stream/readable.ts +345 -0
  398. package/src/node/stream/writable.ts +244 -0
  399. package/src/node/urlqs.ts +162 -0
  400. package/src/node/util.ts +145 -0
  401. package/src/node/zlib.ts +145 -0
  402. package/src/packageCdn.ts +91 -7
  403. package/src/projectConfig.ts +34 -7
  404. package/src/shell/builtins.ts +141 -0
  405. package/src/shell/context.ts +89 -0
  406. package/src/shell/devHandoff.ts +49 -0
  407. package/src/shell/exec.ts +268 -0
  408. package/src/shell/expand.ts +145 -0
  409. package/src/shell/fileCommands.ts +206 -0
  410. package/src/shell/lineEditor.ts +233 -0
  411. package/src/shell/npm.ts +229 -0
  412. package/src/shell/parse.ts +295 -0
  413. package/src/shell/program.ts +223 -0
  414. package/src/shell/textCommands.ts +177 -0
  415. package/src/styleModules.ts +15 -4
  416. package/src/svelteCompiler.ts +31 -0
  417. package/src/system/commands.ts +72 -0
  418. package/src/system/index.ts +22 -0
  419. package/src/system/kernel.ts +106 -0
  420. package/src/system/ports.ts +73 -0
  421. package/src/system/process.ts +158 -0
  422. package/src/system/programs.ts +31 -0
  423. package/src/system/terminal.ts +135 -0
  424. package/src/system/types.ts +103 -0
  425. package/src/vfs.ts +92 -28
  426. package/src/vueCompiler.ts +121 -0
@@ -0,0 +1,324 @@
1
+ /**
2
+ * The `process` object of one runtime: argv/env/cwd, `exit` (unwinds via
3
+ * {@link ProcessExit}), `nextTick`, stdio streams wired to the program's IO
4
+ * port, and the identity a Linux Node 20 reports — packages branch on
5
+ * `process.platform`, `process.versions.node` and `process.stdout.isTTY`.
6
+ */
7
+ import { EventEmitterBase } from './events.js';
8
+ import { Readable } from './stream/readable.js';
9
+ import { Writable } from './stream/writable.js';
10
+ import type { EventLoop } from './loop.js';
11
+ import type { ProgramIO } from '../system/types.js';
12
+ import type { VirtualFileSystem } from '../vfs.js';
13
+ import { join, normalizePath } from '../paths.js';
14
+ import { fsError } from './fs/stats.js';
15
+ import { format } from './inspect.js';
16
+
17
+ export const NODE_VERSION = '20.18.0';
18
+
19
+ /** Thrown by `process.exit()` to unwind user code; the runtime catches it. */
20
+ export class ProcessExit {
21
+ constructor(readonly code: number) {}
22
+ }
23
+
24
+ let nextPid = 100;
25
+
26
+ export interface ProcessOptions {
27
+ argv: string[];
28
+ env: Record<string, string>;
29
+ cwd: string;
30
+ fs: VirtualFileSystem;
31
+ io: ProgramIO;
32
+ loop: EventLoop;
33
+ /** Called once with the exit code; the runtime tears down. */
34
+ onExit(code: number): void;
35
+ }
36
+
37
+ class TtyWriteStream extends Writable {
38
+ isTTY: boolean;
39
+ fd: number;
40
+ columns?: number;
41
+ rows?: number;
42
+
43
+ constructor(fd: number, private readonly sink: (chunk: string | Uint8Array) => void, terminal: ProgramIO['terminal']) {
44
+ super({ decodeStrings: false });
45
+ this.fd = fd;
46
+ this.isTTY = !!terminal;
47
+ if (terminal) {
48
+ this.columns = terminal.cols;
49
+ this.rows = terminal.rows;
50
+ terminal.onResize((cols, rows) => {
51
+ this.columns = cols;
52
+ this.rows = rows;
53
+ this.emit('resize');
54
+ });
55
+ }
56
+ }
57
+
58
+ override _write(chunk: string | Uint8Array, _encoding: string, callback: () => void): void {
59
+ this.sink(chunk);
60
+ callback();
61
+ }
62
+
63
+ /** Synchronous, like Node's stdout to a TTY or file: output order matches call order. */
64
+ override write(chunk: unknown, encoding?: any, callback?: any): boolean {
65
+ if (typeof encoding === 'function') callback = encoding;
66
+ this.sink(typeof chunk === 'string' || chunk instanceof Uint8Array ? chunk : String(chunk));
67
+ if (typeof callback === 'function') queueMicrotask(callback);
68
+ return true;
69
+ }
70
+
71
+ getColorDepth(): number {
72
+ return this.isTTY ? 8 : 1;
73
+ }
74
+ hasColors(count = 16): boolean {
75
+ return this.isTTY && count <= 256;
76
+ }
77
+ getWindowSize(): [number, number] {
78
+ return [this.columns ?? 80, this.rows ?? 24];
79
+ }
80
+ cursorTo(x: number, y?: number | (() => void), cb?: () => void): boolean {
81
+ this.write(typeof y === 'number' ? `\x1b[${y + 1};${x + 1}H` : `\x1b[${x + 1}G`);
82
+ (typeof y === 'function' ? y : cb)?.();
83
+ return true;
84
+ }
85
+ moveCursor(dx: number, dy: number, cb?: () => void): boolean {
86
+ this.write((dx < 0 ? `\x1b[${-dx}D` : dx > 0 ? `\x1b[${dx}C` : '') + (dy < 0 ? `\x1b[${-dy}A` : dy > 0 ? `\x1b[${dy}B` : ''));
87
+ cb?.();
88
+ return true;
89
+ }
90
+ clearLine(dir = 0, cb?: () => void): boolean {
91
+ this.write(dir < 0 ? '\x1b[1K' : dir > 0 ? '\x1b[0K' : '\x1b[2K');
92
+ cb?.();
93
+ return true;
94
+ }
95
+ clearScreenDown(cb?: () => void): boolean {
96
+ this.write('\x1b[0J');
97
+ cb?.();
98
+ return true;
99
+ }
100
+ }
101
+
102
+ class StdinStream extends Readable {
103
+ isTTY: boolean;
104
+ isRaw = false;
105
+ fd = 0;
106
+ private subscription?: () => void;
107
+ private release?: () => void;
108
+
109
+ constructor(private readonly io: ProgramIO, private readonly loop: EventLoop) {
110
+ super({ encoding: 'utf8' });
111
+ this.isTTY = !!io.terminal;
112
+ }
113
+
114
+ override _read(): void {
115
+ this.listen();
116
+ }
117
+
118
+ override resume(): this {
119
+ this.listen();
120
+ return super.resume();
121
+ }
122
+
123
+ override pause(): this {
124
+ this.stopListening();
125
+ return super.pause();
126
+ }
127
+
128
+ setRawMode(raw: boolean): this {
129
+ this.isRaw = raw;
130
+ this.io.terminal?.setRawMode(raw);
131
+ return this;
132
+ }
133
+
134
+ ref(): this {
135
+ if (this.subscription && !this.release) this.release = this.loop.hold();
136
+ return this;
137
+ }
138
+
139
+ unref(): this {
140
+ this.release?.();
141
+ this.release = undefined;
142
+ return this;
143
+ }
144
+
145
+ private listen(): void {
146
+ if (this.subscription) return;
147
+ this.release = this.loop.hold();
148
+ this.subscription = this.io.onStdin((chunk) => {
149
+ if (chunk === null) {
150
+ this.stopListening();
151
+ this.push(null);
152
+ } else this.push(chunk);
153
+ });
154
+ }
155
+
156
+ private stopListening(): void {
157
+ this.subscription?.();
158
+ this.subscription = undefined;
159
+ this.release?.();
160
+ this.release = undefined;
161
+ }
162
+ }
163
+
164
+ export type NodeProcess = EventEmitterBase & Record<string, any>;
165
+
166
+ export function createProcess(options: ProcessOptions): NodeProcess {
167
+ const { io, loop, fs } = options;
168
+ let cwd = normalizePath(options.cwd);
169
+ let exited = false;
170
+ const started = performance.now();
171
+ const proc = new EventEmitterBase() as NodeProcess;
172
+ const pid = nextPid++;
173
+
174
+ const exit = (code?: number) => {
175
+ if (code !== undefined) proc.exitCode = code;
176
+ if (!exited) {
177
+ exited = true;
178
+ const final = Number(proc.exitCode ?? 0) || 0;
179
+ try {
180
+ proc.emit('exit', final);
181
+ } catch {
182
+ // an 'exit' listener that throws must not stop the exit
183
+ }
184
+ options.onExit(Number(proc.exitCode ?? final) || 0);
185
+ }
186
+ throw new ProcessExit(Number(proc.exitCode ?? 0) || 0);
187
+ };
188
+
189
+ const hrtime = (previous?: [number, number]): [number, number] => {
190
+ const now = performance.now();
191
+ let seconds = Math.floor(now / 1000);
192
+ let nanos = Math.floor((now % 1000) * 1e6);
193
+ if (previous) {
194
+ seconds -= previous[0];
195
+ nanos -= previous[1];
196
+ if (nanos < 0) {
197
+ seconds--;
198
+ nanos += 1e9;
199
+ }
200
+ }
201
+ return [seconds, nanos];
202
+ };
203
+ hrtime.bigint = () => BigInt(Math.floor(performance.now() * 1e6));
204
+
205
+ const memoryUsage = () => ({ rss: 50e6, heapTotal: 30e6, heapUsed: 20e6, external: 1e6, arrayBuffers: 1e5 });
206
+ memoryUsage.rss = () => 50e6;
207
+
208
+ Object.assign(proc, {
209
+ title: 'node',
210
+ version: `v${NODE_VERSION}`,
211
+ versions: { node: NODE_VERSION, v8: '11.3.244.8-node.23', uv: '1.48.0', zlib: '1.3.0.1', modules: '115', napi: '9', unicode: '15.1' },
212
+ release: { name: 'node', lts: 'Iron' },
213
+ platform: 'linux',
214
+ arch: 'x64',
215
+ pid,
216
+ ppid: pid - 1,
217
+ argv: options.argv,
218
+ argv0: 'node',
219
+ execPath: '/usr/local/bin/node',
220
+ execArgv: [],
221
+ env: options.env,
222
+ exitCode: undefined as number | undefined,
223
+ config: { variables: {} },
224
+ features: { inspector: false, ipv6: true, tls: true, typescript: false },
225
+ allowedNodeEnvironmentFlags: new Set<string>(),
226
+ stdout: new TtyWriteStream(1, io.stdout, io.terminal),
227
+ stderr: new TtyWriteStream(2, io.stderr, io.terminal),
228
+ stdin: new StdinStream(io, loop),
229
+ cwd: () => cwd,
230
+ chdir(dir: string) {
231
+ const target = dir.startsWith('/') ? normalizePath(dir) : join(cwd, dir);
232
+ if (!fs.isDirectory(target)) throw fsError(fs.isFile(target) ? 'ENOTDIR' : 'ENOENT', 'chdir', target);
233
+ cwd = target;
234
+ },
235
+ exit,
236
+ reallyExit: exit,
237
+ abort: () => exit(134),
238
+ kill(target: number, signal: string | number = 'SIGTERM') {
239
+ if (target !== pid) throw Object.assign(new Error('kill ESRCH'), { code: 'ESRCH' });
240
+ if (proc.listenerCount(String(signal))) proc.emit(String(signal), String(signal));
241
+ else exit(143);
242
+ return true;
243
+ },
244
+ nextTick(fn: (...args: unknown[]) => void, ...args: unknown[]) {
245
+ queueMicrotask(() => loop._run(() => fn(...args)));
246
+ },
247
+ hrtime,
248
+ memoryUsage,
249
+ cpuUsage: () => ({ user: Math.floor((performance.now() - started) * 1000), system: 0 }),
250
+ resourceUsage: () => ({ userCPUTime: 0, systemCPUTime: 0, maxRSS: 50000 }),
251
+ uptime: () => (performance.now() - started) / 1000,
252
+ umask: () => 0o22,
253
+ getuid: () => 1000,
254
+ geteuid: () => 1000,
255
+ getgid: () => 1000,
256
+ getegid: () => 1000,
257
+ getgroups: () => [1000],
258
+ emitWarning(warning: string | Error, type?: string | { type?: string }) {
259
+ const name = typeof type === 'string' ? type : type?.type ?? (warning instanceof Error ? warning.name : 'Warning');
260
+ const message = warning instanceof Error ? warning.message : warning;
261
+ proc.emit('warning', Object.assign(new Error(message), { name }));
262
+ io.stderr(`(node:${pid}) ${name}: ${message}\n`);
263
+ },
264
+ binding(name: string) {
265
+ throw new Error(`No such module: ${name}`);
266
+ },
267
+ getActiveResourcesInfo: () => [],
268
+ setUncaughtExceptionCaptureCallback: () => undefined,
269
+ hasUncaughtExceptionCaptureCallback: () => false,
270
+ setSourceMapsEnabled: () => undefined,
271
+ report: { getReport: () => ({ header: { glibcVersionRuntime: '2.36' } }) },
272
+ connected: false,
273
+ });
274
+ return proc;
275
+ }
276
+
277
+ /** console bound to a process's stdio (`console.log` in user code prints to the terminal). */
278
+ export function createConsole(proc: NodeProcess): Console {
279
+ const out = (stream: 'stdout' | 'stderr') => (...args: unknown[]) => proc[stream].write(format(...args) + '\n');
280
+ const counts = new Map<string, number>();
281
+ const timers = new Map<string, number>();
282
+ let indent = '';
283
+ const withIndent = (fn: (...args: unknown[]) => void) => (...args: unknown[]) => (indent ? fn(indent + format(...args).replace(/\n/g, '\n' + indent)) : fn(...args));
284
+ const log = withIndent(out('stdout'));
285
+ const error = withIndent(out('stderr'));
286
+ return {
287
+ log,
288
+ info: log,
289
+ debug: log,
290
+ warn: error,
291
+ error,
292
+ trace: (...args: unknown[]) => error(`Trace: ${format(...args)}\n${new Error().stack?.split('\n').slice(2).join('\n') ?? ''}`),
293
+ dir: (value: unknown) => log(value),
294
+ dirxml: log,
295
+ table: (value: unknown) => log(value),
296
+ assert: (condition: unknown, ...args: unknown[]) => {
297
+ if (!condition) error('Assertion failed' + (args.length ? `: ${format(...args)}` : ''));
298
+ },
299
+ count: (label = 'default') => {
300
+ counts.set(label, (counts.get(label) ?? 0) + 1);
301
+ log(`${label}: ${counts.get(label)}`);
302
+ },
303
+ countReset: (label = 'default') => void counts.delete(label),
304
+ group: (...args: unknown[]) => {
305
+ if (args.length) log(...args);
306
+ indent += ' ';
307
+ },
308
+ groupCollapsed: (...args: unknown[]) => {
309
+ if (args.length) log(...args);
310
+ indent += ' ';
311
+ },
312
+ groupEnd: () => void (indent = indent.slice(2)),
313
+ time: (label = 'default') => void timers.set(label, performance.now()),
314
+ timeLog: (label = 'default', ...args: unknown[]) => log(`${label}: ${(performance.now() - (timers.get(label) ?? 0)).toFixed(3)}ms`, ...args),
315
+ timeEnd: (label = 'default') => {
316
+ log(`${label}: ${(performance.now() - (timers.get(label) ?? 0)).toFixed(3)}ms`);
317
+ timers.delete(label);
318
+ },
319
+ clear: () => undefined,
320
+ profile: () => undefined,
321
+ profileEnd: () => undefined,
322
+ timeStamp: () => undefined,
323
+ } as unknown as Console;
324
+ }
@@ -0,0 +1,170 @@
1
+ /**
2
+ * The `node` command: CLI flags, the entry script (or `-e`/`-p`, or stdin), and
3
+ * a small REPL when attached to a terminal with nothing to run.
4
+ */
5
+ import type { Program, ProgramContext } from '../system/types.js';
6
+ import { join } from '../paths.js';
7
+ import { readBinLink } from '../installer/bins.js';
8
+ import { startNode, type NodeInstance } from './runtime.js';
9
+ import { NODE_VERSION } from './process.js';
10
+ import { inspect } from './inspect.js';
11
+ import { parseDotenv } from '../projectConfig.js';
12
+
13
+ interface NodeCli {
14
+ entry?: string;
15
+ args: string[];
16
+ evaluate?: string;
17
+ print?: boolean;
18
+ preload: string[];
19
+ envFiles: string[];
20
+ version?: boolean;
21
+ help?: boolean;
22
+ }
23
+
24
+ /** Flags that take the next argument as their value. */
25
+ const WITH_VALUE = new Set(['-r', '--require', '--import', '--loader', '--experimental-loader', '-C', '--conditions', '--input-type', '--title', '--env-file', '--stack-size']);
26
+
27
+ export function parseNodeArgs(argv: string[]): NodeCli {
28
+ const cli: NodeCli = { args: [], preload: [], envFiles: [] };
29
+ for (let i = 0; i < argv.length; i++) {
30
+ const arg = argv[i]!;
31
+ if (arg === '--') {
32
+ [cli.entry, ...cli.args] = argv.slice(i + 1);
33
+ break;
34
+ }
35
+ if (!arg.startsWith('-') || arg === '-') {
36
+ cli.entry = arg;
37
+ cli.args = argv.slice(i + 1);
38
+ break;
39
+ }
40
+ const [flag, inline] = arg.includes('=') ? [arg.slice(0, arg.indexOf('=')), arg.slice(arg.indexOf('=') + 1)] : [arg, undefined];
41
+ const value = () => inline ?? argv[++i] ?? '';
42
+ if (flag === '-v' || flag === '--version') cli.version = true;
43
+ else if (flag === '-h' || flag === '--help') cli.help = true;
44
+ else if (flag === '-e' || flag === '--eval') cli.evaluate = value();
45
+ else if (flag === '-p' || flag === '--print') {
46
+ cli.evaluate = value();
47
+ cli.print = true;
48
+ } else if (flag === '-r' || flag === '--require' || flag === '--import') cli.preload.push(value());
49
+ else if (flag === '--env-file') cli.envFiles.push(value());
50
+ else if (WITH_VALUE.has(flag) && inline === undefined) i++;
51
+ // everything else (--inspect, --no-warnings, --max-old-space-size=…) is accepted and ignored
52
+ }
53
+ if (cli.evaluate !== undefined && cli.entry !== undefined) {
54
+ cli.args = [cli.entry, ...cli.args];
55
+ cli.entry = undefined;
56
+ }
57
+ return cli;
58
+ }
59
+
60
+ const HELP = `Usage: node [options] [ script.js ] [arguments]
61
+ node -e "script" evaluate script
62
+ node -p "expr" evaluate and print
63
+ Options: -v, -r <module>, --env-file=<file>. Running in BuilderForce WebContainers.
64
+ `;
65
+
66
+ async function readAllStdin(ctx: ProgramContext): Promise<string> {
67
+ let text = '';
68
+ await new Promise<void>((resolve) => {
69
+ const stop = ctx.io.onStdin((chunk) => {
70
+ if (chunk === null) {
71
+ stop();
72
+ resolve();
73
+ } else text += chunk;
74
+ });
75
+ });
76
+ return text;
77
+ }
78
+
79
+ /** Evaluate REPL lines in one persistent sloppy-mode scope (`const`/`let` become `var` so they survive the line). */
80
+ function startRepl(instance: NodeInstance, ctx: ProgramContext): void {
81
+ const { io } = ctx;
82
+ const scope = evalInProcess(instance, ctx.cwd, '[repl]', `(function* () { let __r; while (true) { const __line = yield __r; try { __r = { value: eval(__line) }; } catch (e) { __r = { error: e }; } } })()`) as Generator<{ value?: unknown; error?: unknown } | undefined, never, string>;
83
+ scope.next();
84
+ const release = instance.hold();
85
+ io.stdout(`Welcome to Node.js v${NODE_VERSION}.\nType ".exit" to leave.\n> `);
86
+ const leave = () => {
87
+ stop();
88
+ release();
89
+ instance.run(() => instance.process.exit(0));
90
+ };
91
+ const stop = io.onStdin((chunk) => {
92
+ if (chunk === null) return leave();
93
+ for (const raw of chunk.split(/\r?\n/)) {
94
+ const line = raw.trim();
95
+ if (!line) continue;
96
+ if (line === '.exit') return leave();
97
+ const result = scope.next(line.replace(/^(const|let)\s/, 'var ')).value!;
98
+ if ('error' in result) io.stdout(`Uncaught ${result.error instanceof Error ? result.error.stack ?? result.error.message : inspect(result.error)}\n`);
99
+ else io.stdout(inspect(result.value, { colors: false }) + '\n');
100
+ }
101
+ io.stdout('> ');
102
+ });
103
+ }
104
+
105
+ /** Evaluate `expression` inside the CommonJS wrapper, so it sees require, process, timers… of this process. */
106
+ function evalInProcess(instance: NodeInstance, cwd: string, name: string, expression: string): unknown {
107
+ const filename = join(cwd, name);
108
+ const module = new instance.modules.Module(filename);
109
+ return module._compile(`return eval(${JSON.stringify(expression)});`, filename);
110
+ }
111
+
112
+ export const nodeProgram: Program = async (ctx) => {
113
+ const cli = parseNodeArgs(ctx.args);
114
+ if (cli.version) {
115
+ ctx.io.stdout(`v${NODE_VERSION}\n`);
116
+ return 0;
117
+ }
118
+ if (cli.help) {
119
+ ctx.io.stdout(HELP);
120
+ return 0;
121
+ }
122
+ const env = { ...ctx.env };
123
+ for (const file of cli.envFiles) {
124
+ const text = ctx.system.fs.readText(join(ctx.cwd, file));
125
+ if (text === undefined) {
126
+ ctx.io.stderr(`node: ${file}: not found\n`);
127
+ return 9;
128
+ }
129
+ Object.assign(env, parseDotenv(text));
130
+ }
131
+
132
+ let entry: string | undefined;
133
+ if (cli.entry !== undefined && cli.entry !== '-') {
134
+ entry = cli.entry.startsWith('/') ? cli.entry : join(ctx.cwd, cli.entry);
135
+ entry = readBinLink(ctx.system.fs, entry) ?? entry;
136
+ }
137
+ const source = cli.evaluate ?? (cli.entry === '-' || (!entry && !ctx.io.terminal) ? await readAllStdin(ctx) : undefined);
138
+ const scriptPath = entry ?? (source !== undefined ? '[eval]' : undefined);
139
+ const instance = await startNode({ system: ctx.system, io: ctx.io, cwd: ctx.cwd, env, installGlobals: ctx.system.ownsRealm, argv: scriptPath && scriptPath !== '[eval]' ? [scriptPath, ...cli.args] : cli.args });
140
+ const onAbort = () => instance.kill(typeof ctx.signal.reason === 'string' ? ctx.signal.reason : 'SIGTERM');
141
+ if (ctx.signal.aborted) onAbort();
142
+ ctx.signal.addEventListener('abort', onAbort);
143
+ const untrap = ctx.system.trapUncaught?.((error) => instance.reportUncaught(error));
144
+
145
+ instance.run(() => {
146
+ const require = instance.modules.createRequire(join(ctx.cwd, '[eval]'));
147
+ for (const id of cli.preload) require(id.startsWith('.') ? join(ctx.cwd, id) : id);
148
+ if (entry) {
149
+ let resolved: string;
150
+ try {
151
+ resolved = instance.modules.Module._resolveFilename(entry, undefined);
152
+ } catch (error) {
153
+ if ((error as { code?: string }).code === 'MODULE_NOT_FOUND') throw Object.assign(new Error(`Cannot find module '${entry}'`), { code: 'MODULE_NOT_FOUND', requireStack: [] });
154
+ throw error;
155
+ }
156
+ return instance.modules.runMain(resolved).__tla;
157
+ }
158
+ if (source !== undefined) {
159
+ const result = evalInProcess(instance, ctx.cwd, '[eval]', source);
160
+ if (cli.print) instance.process.stdout.write(inspect(result) + '\n');
161
+ return undefined;
162
+ }
163
+ startRepl(instance, ctx);
164
+ return undefined;
165
+ });
166
+ const code = await instance.exit;
167
+ ctx.signal.removeEventListener('abort', onAbort);
168
+ untrap?.();
169
+ return code;
170
+ };
@@ -0,0 +1,157 @@
1
+ /**
2
+ * `readline` and `readline/promises`: line splitting over any input stream,
3
+ * `question`, prompts and async iteration — what interactive CLIs (create-*,
4
+ * inquirer's simple paths, REPL-ish scripts) use. Input arrives already
5
+ * line-edited by the terminal's line discipline, so no keypress decoding is
6
+ * needed beyond `emitKeypressEvents` delivering characters.
7
+ */
8
+ import { EventEmitterBase } from './events.js';
9
+
10
+ interface Input extends EventEmitterBase {
11
+ resume?(): unknown;
12
+ pause?(): unknown;
13
+ setEncoding?(e: string): unknown;
14
+ }
15
+ interface Output {
16
+ write(chunk: string): unknown;
17
+ }
18
+
19
+ class Interface extends EventEmitterBase {
20
+ line = '';
21
+ cursor = 0;
22
+ terminal: boolean;
23
+ closed = false;
24
+ private buffer = '';
25
+ private promptText = '> ';
26
+ private pendingQuestion?: (answer: string) => void;
27
+ private readonly onData = (chunk: unknown) => this.feed(String(chunk));
28
+ private readonly onEnd = () => {
29
+ if (this.buffer) this.emitLine(this.buffer);
30
+ this.buffer = '';
31
+ this.close();
32
+ };
33
+
34
+ constructor(readonly input: Input, readonly output?: Output, options: { prompt?: string; terminal?: boolean } = {}) {
35
+ super();
36
+ this.terminal = options.terminal ?? !!(output as { isTTY?: boolean } | undefined)?.isTTY;
37
+ if (options.prompt !== undefined) this.promptText = options.prompt;
38
+ input.setEncoding?.('utf8');
39
+ input.on('data', this.onData);
40
+ input.on('end', this.onEnd);
41
+ input.resume?.();
42
+ }
43
+
44
+ setPrompt(prompt: string): void {
45
+ this.promptText = prompt;
46
+ }
47
+ getPrompt(): string {
48
+ return this.promptText;
49
+ }
50
+ prompt(): void {
51
+ this.output?.write(this.promptText);
52
+ }
53
+ question(query: string, options: unknown, callback?: (answer: string) => void): void {
54
+ const cb = (typeof options === 'function' ? options : callback) as (answer: string) => void;
55
+ this.output?.write(query);
56
+ this.pendingQuestion = cb;
57
+ }
58
+ write(data: string): void {
59
+ this.feed(data);
60
+ }
61
+ pause(): this {
62
+ this.input.pause?.();
63
+ this.emit('pause');
64
+ return this;
65
+ }
66
+ resume(): this {
67
+ this.input.resume?.();
68
+ this.emit('resume');
69
+ return this;
70
+ }
71
+ close(): void {
72
+ if (this.closed) return;
73
+ this.closed = true;
74
+ this.input.removeListener('data', this.onData);
75
+ this.input.removeListener('end', this.onEnd);
76
+ this.input.pause?.();
77
+ this.emit('close');
78
+ }
79
+ getCursorPos(): { rows: number; cols: number } {
80
+ return { rows: 0, cols: this.cursor };
81
+ }
82
+
83
+ async *[Symbol.asyncIterator](): AsyncGenerator<string> {
84
+ const queue: string[] = [];
85
+ let wake: (() => void) | undefined;
86
+ this.on('line', (line: string) => {
87
+ queue.push(line);
88
+ wake?.();
89
+ });
90
+ this.on('close', () => wake?.());
91
+ while (true) {
92
+ if (queue.length) yield queue.shift()!;
93
+ else if (this.closed) return;
94
+ else await new Promise<void>((resolve) => (wake = resolve));
95
+ }
96
+ }
97
+
98
+ private feed(text: string): void {
99
+ this.buffer += text;
100
+ for (;;) {
101
+ const nl = this.buffer.search(/\r\n|\n|\r/);
102
+ if (nl < 0) break;
103
+ const line = this.buffer.slice(0, nl);
104
+ this.buffer = this.buffer.slice(this.buffer[nl] === '\r' && this.buffer[nl + 1] === '\n' ? nl + 2 : nl + 1);
105
+ this.emitLine(line);
106
+ }
107
+ this.line = this.buffer;
108
+ this.cursor = this.buffer.length;
109
+ }
110
+
111
+ private emitLine(line: string): void {
112
+ const question = this.pendingQuestion;
113
+ if (question) {
114
+ this.pendingQuestion = undefined;
115
+ question(line);
116
+ } else this.emit('line', line);
117
+ }
118
+ }
119
+
120
+ const ansi = (stream: Output | undefined, sequence: string, cb?: () => void) => {
121
+ stream?.write(sequence);
122
+ cb?.();
123
+ return true;
124
+ };
125
+
126
+ export function createReadlineModule(promises = false): Record<string, unknown> {
127
+ const createInterface = (input: Input | { input: Input; output?: Output; prompt?: string; terminal?: boolean }, output?: Output) => {
128
+ const opts = 'input' in input && (input as { input?: unknown }).input ? (input as { input: Input; output?: Output; prompt?: string; terminal?: boolean }) : { input: input as Input, output };
129
+ const rl = new Interface(opts.input, opts.output, opts);
130
+ if (promises) {
131
+ const ask = rl.question.bind(rl);
132
+ (rl as unknown as { question: (q: string) => Promise<string> }).question = (query: string) => new Promise((resolve) => ask(query, undefined, resolve));
133
+ }
134
+ return rl;
135
+ };
136
+ const mod: Record<string, unknown> = {
137
+ createInterface,
138
+ Interface,
139
+ clearLine: (stream: Output, dir: number, cb?: () => void) => ansi(stream, dir < 0 ? '\x1b[1K' : dir > 0 ? '\x1b[0K' : '\x1b[2K', cb),
140
+ clearScreenDown: (stream: Output, cb?: () => void) => ansi(stream, '\x1b[0J', cb),
141
+ cursorTo: (stream: Output, x: number, y?: number | (() => void), cb?: () => void) => ansi(stream, typeof y === 'number' ? `\x1b[${y + 1};${x + 1}H` : `\x1b[${x + 1}G`, typeof y === 'function' ? y : cb),
142
+ moveCursor: (stream: Output, dx: number, dy: number, cb?: () => void) =>
143
+ ansi(stream, (dx < 0 ? `\x1b[${-dx}D` : dx > 0 ? `\x1b[${dx}C` : '') + (dy < 0 ? `\x1b[${-dy}A` : dy > 0 ? `\x1b[${dy}B` : ''), cb),
144
+ emitKeypressEvents(stream: EventEmitterBase) {
145
+ if ((stream as { __keypress?: boolean }).__keypress) return;
146
+ (stream as { __keypress?: boolean }).__keypress = true;
147
+ stream.on('data', (chunk: unknown) => {
148
+ for (const ch of String(chunk)) {
149
+ const name = ch === '\r' || ch === '\n' ? 'return' : ch === '\x7f' ? 'backspace' : ch === '\x03' ? 'c' : /[a-z]/i.test(ch) ? ch.toLowerCase() : undefined;
150
+ stream.emit('keypress', ch, { sequence: ch, name, ctrl: ch === '\x03', meta: false, shift: /[A-Z]/.test(ch) });
151
+ }
152
+ });
153
+ },
154
+ };
155
+ if (!promises) mod.promises = createReadlineModule(true);
156
+ return mod;
157
+ }