@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,335 @@
1
+ /**
2
+ * The synchronous fs API over the VirtualFileSystem. Everything else (callback,
3
+ * promise and stream forms) is layered on these in index.ts, so error codes and
4
+ * edge cases live in exactly one place.
5
+ */
6
+ import type { VirtualFileSystem } from '../../vfs.js';
7
+ import { normalizePath, dirname, join } from '../../paths.js';
8
+ import { Buffer, encode } from '../buffer.js';
9
+ import { Dirent, fsError, Stats, constants } from './stats.js';
10
+
11
+ export interface FsContext {
12
+ vfs: VirtualFileSystem;
13
+ cwd(): string;
14
+ /** fd 1 and 2: `fs.writeSync(1, …)` is how some CLIs print. */
15
+ stdout(chunk: string | Uint8Array): void;
16
+ stderr(chunk: string | Uint8Array): void;
17
+ }
18
+
19
+ type PathLike = string | Uint8Array | URL;
20
+ type Data = string | ArrayBufferView;
21
+ type EncodingOption = string | null | undefined | { encoding?: string | null; flag?: string; withFileTypes?: boolean; recursive?: boolean; force?: boolean; mode?: number; throwIfNoEntry?: boolean };
22
+
23
+ interface OpenFile {
24
+ path: string;
25
+ flags: string;
26
+ position: number;
27
+ }
28
+
29
+ const encodingOf = (options: EncodingOption): string | null | undefined => (typeof options === 'string' ? options : options?.encoding);
30
+
31
+ export function createSyncFs(ctx: FsContext) {
32
+ const { vfs } = ctx;
33
+ const fds = new Map<number, OpenFile>();
34
+ let nextFd = 20;
35
+
36
+ const resolve = (p: PathLike): string => {
37
+ if (p instanceof URL) {
38
+ if (p.protocol !== 'file:') throw new TypeError('The URL must be of scheme file');
39
+ return normalizePath(decodeURIComponent(p.pathname));
40
+ }
41
+ const text = typeof p === 'string' ? p : new TextDecoder().decode(p);
42
+ if (typeof text !== 'string') throw new TypeError('The "path" argument must be of type string, Buffer or URL');
43
+ if (text.startsWith('file://')) return normalizePath(decodeURIComponent(new URL(text).pathname));
44
+ return text.startsWith('/') ? normalizePath(text) : join(ctx.cwd(), text);
45
+ };
46
+
47
+ const bytesOf = (path: string): Uint8Array => {
48
+ const contents = vfs.readFile(path)!;
49
+ return typeof contents === 'string' ? encode(contents) : contents;
50
+ };
51
+
52
+ const requireParent = (path: string, syscall: string) => {
53
+ const parent = dirname(path);
54
+ if (vfs.isFile(parent)) throw fsError('ENOTDIR', syscall, path);
55
+ if (!vfs.isDirectory(parent)) throw fsError('ENOENT', syscall, path);
56
+ };
57
+
58
+ const toStored = (data: Data, encoding?: string | null): string | Uint8Array => {
59
+ if (typeof data === 'string') return !encoding || /^utf-?8$/i.test(encoding) ? data : encode(data, encoding).slice();
60
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength).slice();
61
+ };
62
+
63
+ const stat = (p: PathLike, options?: { throwIfNoEntry?: boolean }): Stats | undefined => {
64
+ const path = resolve(p);
65
+ if (vfs.isFile(path)) return new Stats(path, 'file', bytesOf(path).length, vfs.mtime(path) ?? Date.now());
66
+ if (vfs.isDirectory(path)) return new Stats(path, 'directory', 4096, 0);
67
+ if (options?.throwIfNoEntry === false) return undefined;
68
+ throw fsError('ENOENT', 'stat', path);
69
+ };
70
+
71
+ const writeFile = (p: PathLike | number, data: Data, options?: EncodingOption) => {
72
+ if (typeof p === 'number') return writeFd(p, data);
73
+ const path = resolve(p);
74
+ const flag = typeof options === 'object' && options ? options.flag ?? 'w' : 'w';
75
+ if (vfs.isDirectory(path)) throw fsError('EISDIR', 'open', path);
76
+ requireParent(path, 'open');
77
+ if (flag.includes('x') && vfs.isFile(path)) throw fsError('EEXIST', 'open', path);
78
+ if (flag.startsWith('a') && vfs.isFile(path)) {
79
+ const before = vfs.readFile(path)!;
80
+ const add = toStored(data, encodingOf(options));
81
+ vfs.writeFile(path, typeof before === 'string' && typeof add === 'string' ? before + add : Buffer.concat([typeof before === 'string' ? encode(before) : before, typeof add === 'string' ? encode(add) : add]));
82
+ return;
83
+ }
84
+ vfs.writeFile(path, toStored(data, encodingOf(options)));
85
+ };
86
+
87
+ const writeFd = (fd: number, data: Data | Uint8Array, offset?: number | string | null, length?: number, position?: number | null): number => {
88
+ const bytes = typeof data === 'string' ? encode(data, typeof offset === 'string' ? offset : undefined) : new Uint8Array(data.buffer, data.byteOffset + (typeof offset === 'number' ? offset : 0), length ?? data.byteLength - (typeof offset === 'number' ? offset : 0));
89
+ if (fd === 1 || fd === 2) {
90
+ (fd === 1 ? ctx.stdout : ctx.stderr)(bytes.slice());
91
+ return bytes.length;
92
+ }
93
+ const file = fds.get(fd);
94
+ if (!file || file.flags === 'r') throw fsError('EBADF', 'write');
95
+ const current = vfs.isFile(file.path) ? bytesOf(file.path) : new Uint8Array();
96
+ const at = file.flags.startsWith('a') ? current.length : position ?? file.position;
97
+ const next = new Uint8Array(Math.max(current.length, at + bytes.length));
98
+ next.set(current);
99
+ next.set(bytes, at);
100
+ vfs.writeFile(file.path, next);
101
+ if (position === undefined || position === null) file.position = at + bytes.length;
102
+ return bytes.length;
103
+ };
104
+
105
+ const readdirEntries = (dir: string, recursive: boolean, prefix = ''): Array<{ name: string; parent: string; kind: 'file' | 'directory' }> => {
106
+ const out: Array<{ name: string; parent: string; kind: 'file' | 'directory' }> = [];
107
+ for (const name of vfs.readdir(dir)) {
108
+ const full = join(dir, name);
109
+ const kind = vfs.isDirectory(full) ? 'directory' : 'file';
110
+ out.push({ name: prefix + name, parent: dir, kind });
111
+ if (recursive && kind === 'directory') out.push(...readdirEntries(full, true, `${prefix}${name}/`));
112
+ }
113
+ return out;
114
+ };
115
+
116
+ const mkdir = (p: PathLike, options?: EncodingOption | number): string | undefined => {
117
+ const path = resolve(p);
118
+ const recursive = typeof options === 'object' && !!options?.recursive;
119
+ if (vfs.isFile(path)) throw fsError('EEXIST', 'mkdir', path);
120
+ if (vfs.isDirectory(path)) {
121
+ if (recursive) return undefined;
122
+ throw fsError('EEXIST', 'mkdir', path);
123
+ }
124
+ if (!recursive) requireParent(path, 'mkdir');
125
+ let first: string | undefined;
126
+ for (let dir = path; !vfs.isDirectory(dir); dir = dirname(dir)) {
127
+ if (vfs.isFile(dir)) throw fsError('ENOTDIR', 'mkdir', path);
128
+ first = dir;
129
+ }
130
+ vfs.mkdir(path);
131
+ return recursive ? first : undefined;
132
+ };
133
+
134
+ const rm = (p: PathLike, options?: { recursive?: boolean; force?: boolean }) => {
135
+ const path = resolve(p);
136
+ if (!vfs.exists(path)) {
137
+ if (options?.force) return;
138
+ throw fsError('ENOENT', 'rm', path);
139
+ }
140
+ if (vfs.isDirectory(path) && !options?.recursive) throw Object.assign(fsError('EISDIR', 'rm', path), { code: 'ERR_FS_EISDIR' });
141
+ vfs.rm(path);
142
+ };
143
+
144
+ const copyFile = (src: PathLike, dest: PathLike, mode = 0) => {
145
+ const from = resolve(src);
146
+ const to = resolve(dest);
147
+ if (!vfs.isFile(from)) throw fsError(vfs.isDirectory(from) ? 'EISDIR' : 'ENOENT', 'copyfile', from, to);
148
+ if (mode & constants.COPYFILE_EXCL && vfs.exists(to)) throw fsError('EEXIST', 'copyfile', from, to);
149
+ requireParent(to, 'copyfile');
150
+ const contents = vfs.readFile(from)!;
151
+ vfs.writeFile(to, typeof contents === 'string' ? contents : contents.slice());
152
+ };
153
+
154
+ const copyTree = (from: string, to: string) => {
155
+ if (vfs.isFile(from)) {
156
+ const contents = vfs.readFile(from)!;
157
+ vfs.writeFile(to, typeof contents === 'string' ? contents : contents.slice());
158
+ return;
159
+ }
160
+ vfs.mkdir(to);
161
+ for (const name of vfs.readdir(from)) copyTree(join(from, name), join(to, name));
162
+ };
163
+
164
+ const open = (p: PathLike, flags: string | number = 'r'): number => {
165
+ const path = resolve(p);
166
+ const f = typeof flags === 'number' ? (flags & constants.O_APPEND ? 'a' : flags & (constants.O_WRONLY | constants.O_RDWR) ? 'w' : 'r') : flags;
167
+ if (vfs.isDirectory(path) && f !== 'r') throw fsError('EISDIR', 'open', path);
168
+ if (f.startsWith('r')) {
169
+ if (!vfs.exists(path)) throw fsError('ENOENT', 'open', path);
170
+ } else {
171
+ if (f.includes('x') && vfs.exists(path)) throw fsError('EEXIST', 'open', path);
172
+ requireParent(path, 'open');
173
+ if (f.startsWith('w') || !vfs.isFile(path)) vfs.writeFile(path, '');
174
+ }
175
+ const fd = nextFd++;
176
+ fds.set(fd, { path, flags: f, position: 0 });
177
+ return fd;
178
+ };
179
+
180
+ const fileOf = (fd: number, syscall: string) => {
181
+ const file = fds.get(fd);
182
+ if (!file) throw fsError('EBADF', syscall);
183
+ return file;
184
+ };
185
+
186
+ const api = {
187
+ existsSync: (p: PathLike) => {
188
+ try {
189
+ return vfs.exists(resolve(p));
190
+ } catch {
191
+ return false;
192
+ }
193
+ },
194
+ accessSync(p: PathLike) {
195
+ const path = resolve(p);
196
+ if (!vfs.exists(path)) throw fsError('ENOENT', 'access', path);
197
+ },
198
+ statSync: stat,
199
+ lstatSync: stat,
200
+ fstatSync: (fd: number) => stat(fileOf(fd, 'fstat').path)!,
201
+ readFileSync(p: PathLike | number, options?: EncodingOption): string | Buffer {
202
+ if (p === 0) return Buffer.alloc(0); // stdin is not readable synchronously here
203
+ const path = typeof p === 'number' ? fileOf(p, 'read').path : resolve(p);
204
+ if (vfs.isDirectory(path)) throw fsError('EISDIR', 'read', path);
205
+ const contents = vfs.readFile(path);
206
+ if (contents === undefined) throw fsError('ENOENT', 'open', path);
207
+ const encoding = encodingOf(options);
208
+ if (encoding) return typeof contents === 'string' && /^utf-?8$/i.test(encoding) ? contents : Buffer.from(typeof contents === 'string' ? encode(contents) : contents).toString(encoding as 'utf8');
209
+ return Buffer.from(typeof contents === 'string' ? encode(contents) : contents);
210
+ },
211
+ writeFileSync: writeFile,
212
+ appendFileSync: (p: PathLike | number, data: Data, options?: EncodingOption) => writeFile(p, data, { ...(typeof options === 'string' ? { encoding: options } : options ?? {}), flag: 'a' }),
213
+ mkdirSync: mkdir,
214
+ mkdtempSync(prefix: string) {
215
+ const path = `${prefix}${Math.random().toString(36).slice(2, 8)}`;
216
+ mkdir(path);
217
+ return resolve(path);
218
+ },
219
+ readdirSync(p: PathLike, options?: EncodingOption) {
220
+ const path = resolve(p);
221
+ if (vfs.isFile(path)) throw fsError('ENOTDIR', 'scandir', path);
222
+ if (!vfs.isDirectory(path)) throw fsError('ENOENT', 'scandir', path);
223
+ const opts = typeof options === 'object' ? options : undefined;
224
+ const entries = readdirEntries(path, !!opts?.recursive);
225
+ if (opts?.withFileTypes) return entries.map((e) => new Dirent(e.name.slice(e.name.lastIndexOf('/') + 1), e.name.includes('/') ? join(path, e.name, '..') : e.parent, e.kind));
226
+ return entries.map((e) => (encodingOf(options) === 'buffer' ? Buffer.from(e.name) : e.name));
227
+ },
228
+ rmSync: rm,
229
+ rmdirSync(p: PathLike, options?: { recursive?: boolean }) {
230
+ const path = resolve(p);
231
+ if (vfs.isFile(path)) throw fsError('ENOTDIR', 'rmdir', path);
232
+ if (!vfs.isDirectory(path)) throw fsError('ENOENT', 'rmdir', path);
233
+ if (vfs.readdir(path).length && !options?.recursive) throw fsError('ENOTEMPTY', 'rmdir', path);
234
+ vfs.rm(path);
235
+ },
236
+ unlinkSync(p: PathLike) {
237
+ const path = resolve(p);
238
+ if (vfs.isDirectory(path)) throw fsError('EISDIR', 'unlink', path);
239
+ if (!vfs.isFile(path)) throw fsError('ENOENT', 'unlink', path);
240
+ vfs.rm(path);
241
+ },
242
+ renameSync(a: PathLike, b: PathLike) {
243
+ const from = resolve(a);
244
+ const to = resolve(b);
245
+ if (!vfs.exists(from)) throw fsError('ENOENT', 'rename', from, to);
246
+ if (from === to) return;
247
+ if (vfs.isDirectory(to) && vfs.isFile(from)) throw fsError('EISDIR', 'rename', from, to);
248
+ requireParent(to, 'rename');
249
+ if (vfs.isFile(to) && vfs.isDirectory(from)) throw fsError('ENOTDIR', 'rename', from, to);
250
+ vfs.rm(to);
251
+ copyTree(from, to);
252
+ vfs.rm(from);
253
+ },
254
+ copyFileSync: copyFile,
255
+ cpSync(src: PathLike, dest: PathLike, options?: { recursive?: boolean; force?: boolean }) {
256
+ const from = resolve(src);
257
+ if (vfs.isDirectory(from) && !options?.recursive) throw Object.assign(fsError('EISDIR', 'cp', from), { code: 'ERR_FS_EISDIR' });
258
+ if (!vfs.exists(from)) throw fsError('ENOENT', 'cp', from);
259
+ copyTree(from, resolve(dest));
260
+ },
261
+ realpathSync: Object.assign((p: PathLike) => {
262
+ const path = resolve(p);
263
+ if (!vfs.exists(path)) throw fsError('ENOENT', 'realpath', path);
264
+ return path;
265
+ }, { native: (p: PathLike) => api.realpathSync(p) }),
266
+ readlinkSync: (p: PathLike) => {
267
+ throw fsError('EINVAL', 'readlink', resolve(p));
268
+ },
269
+ /** No symlinks in the VFS: a link to a file becomes a copy of it. */
270
+ symlinkSync(target: PathLike, p: PathLike) {
271
+ const path = resolve(p);
272
+ const from = typeof target === 'string' && !target.startsWith('/') ? join(dirname(path), target) : resolve(target);
273
+ copyTree(from, path);
274
+ },
275
+ linkSync: (a: PathLike, b: PathLike) => copyFile(a, b),
276
+ truncateSync(p: PathLike, length = 0) {
277
+ const path = resolve(p);
278
+ if (!vfs.isFile(path)) throw fsError('ENOENT', 'open', path);
279
+ const bytes = bytesOf(path);
280
+ const next = new Uint8Array(length);
281
+ next.set(bytes.subarray(0, length));
282
+ vfs.writeFile(path, next);
283
+ },
284
+ chmodSync: (p: PathLike) => void api.accessSync(p),
285
+ lchmodSync: (p: PathLike) => void api.accessSync(p),
286
+ chownSync: (p: PathLike) => void api.accessSync(p),
287
+ lchownSync: (p: PathLike) => void api.accessSync(p),
288
+ utimesSync: (p: PathLike) => void api.accessSync(p),
289
+ lutimesSync: (p: PathLike) => void api.accessSync(p),
290
+ fchmodSync: (fd: number) => void fileOf(fd, 'fchmod'),
291
+ fchownSync: (fd: number) => void fileOf(fd, 'fchown'),
292
+ futimesSync: (fd: number) => void fileOf(fd, 'futimes'),
293
+ fsyncSync: (fd: number) => void fileOf(fd, 'fsync'),
294
+ fdatasyncSync: (fd: number) => void fileOf(fd, 'fdatasync'),
295
+ ftruncateSync: (fd: number, length = 0) => api.truncateSync(fileOf(fd, 'ftruncate').path, length),
296
+ openSync: open,
297
+ closeSync(fd: number) {
298
+ if (fd > 2) fileOf(fd, 'close');
299
+ fds.delete(fd);
300
+ },
301
+ readSync(fd: number, buffer: ArrayBufferView, offsetOrOptions?: number | { offset?: number; length?: number; position?: number | null }, length?: number, position?: number | null): number {
302
+ const opts = typeof offsetOrOptions === 'object' ? offsetOrOptions : { offset: offsetOrOptions, length, position };
303
+ const file = fileOf(fd, 'read');
304
+ const bytes = bytesOf(file.path);
305
+ const target = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
306
+ const offset = opts.offset ?? 0;
307
+ const from = opts.position ?? file.position;
308
+ const chunk = bytes.subarray(from, from + Math.min(opts.length ?? target.length - offset, target.length - offset));
309
+ target.set(chunk, offset);
310
+ if (opts.position === undefined || opts.position === null) file.position = from + chunk.length;
311
+ return chunk.length;
312
+ },
313
+ writeSync: writeFd,
314
+ opendirSync(p: PathLike) {
315
+ const path = resolve(p);
316
+ const entries = api.readdirSync(path, { withFileTypes: true }) as Dirent[];
317
+ let i = 0;
318
+ return {
319
+ path,
320
+ readSync: () => entries[i++] ?? null,
321
+ read: async () => entries[i++] ?? null,
322
+ closeSync: () => undefined,
323
+ close: async () => undefined,
324
+ async *[Symbol.asyncIterator]() {
325
+ while (i < entries.length) yield entries[i++]!;
326
+ },
327
+ };
328
+ },
329
+ /** For the stream and watch layers. */
330
+ _resolve: resolve,
331
+ };
332
+ return api;
333
+ }
334
+
335
+ export type SyncFs = ReturnType<typeof createSyncFs>;
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Synchronous message digests for `crypto.createHash` — WebCrypto's digest is
3
+ * async-only, and Node's API is sync. md5, sha1, sha256, sha224, sha384, sha512.
4
+ * Inputs are buffered and hashed at `digest()`, which keeps each algorithm a
5
+ * single pure function.
6
+ */
7
+
8
+ const rotl = (x: number, n: number) => (x << n) | (x >>> (32 - n));
9
+ const rotr = (x: number, n: number) => (x >>> n) | (x << (32 - n));
10
+
11
+ function pad(bytes: Uint8Array, blockSize: number, lengthBytes: number, littleEndian: boolean): Uint8Array {
12
+ const bitLength = bytes.length * 8;
13
+ const total = Math.ceil((bytes.length + 1 + lengthBytes) / blockSize) * blockSize;
14
+ const out = new Uint8Array(total);
15
+ out.set(bytes);
16
+ out[bytes.length] = 0x80;
17
+ const view = new DataView(out.buffer);
18
+ const hi = Math.floor(bitLength / 2 ** 32);
19
+ const lo = bitLength >>> 0;
20
+ if (littleEndian) {
21
+ view.setUint32(total - 8, lo, true);
22
+ view.setUint32(total - 4, hi, true);
23
+ } else {
24
+ view.setUint32(total - 4, lo);
25
+ view.setUint32(total - 8, hi);
26
+ }
27
+ return out;
28
+ }
29
+
30
+ const MD5_S = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21];
31
+ const MD5_K = Array.from({ length: 64 }, (_, i) => Math.floor(Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0);
32
+
33
+ function md5(input: Uint8Array): Uint8Array {
34
+ const data = pad(input, 64, 8, true);
35
+ const view = new DataView(data.buffer);
36
+ let a0 = 0x67452301, b0 = 0xefcdab89, c0 = 0x98badcfe, d0 = 0x10325476;
37
+ for (let off = 0; off < data.length; off += 64) {
38
+ let a = a0, b = b0, c = c0, d = d0;
39
+ for (let i = 0; i < 64; i++) {
40
+ let f: number, g: number;
41
+ if (i < 16) [f, g] = [(b & c) | (~b & d), i];
42
+ else if (i < 32) [f, g] = [(d & b) | (~d & c), (5 * i + 1) % 16];
43
+ else if (i < 48) [f, g] = [b ^ c ^ d, (3 * i + 5) % 16];
44
+ else [f, g] = [c ^ (b | ~d), (7 * i) % 16];
45
+ const tmp = d;
46
+ d = c;
47
+ c = b;
48
+ b = (b + rotl((a + f + MD5_K[i]! + view.getUint32(off + g * 4, true)) | 0, MD5_S[(i >> 4) * 4 + (i % 4)]!)) | 0;
49
+ a = tmp;
50
+ }
51
+ a0 = (a0 + a) | 0; b0 = (b0 + b) | 0; c0 = (c0 + c) | 0; d0 = (d0 + d) | 0;
52
+ }
53
+ const out = new DataView(new ArrayBuffer(16));
54
+ [a0, b0, c0, d0].forEach((v, i) => out.setUint32(i * 4, v, true));
55
+ return new Uint8Array(out.buffer);
56
+ }
57
+
58
+ function sha1(input: Uint8Array): Uint8Array {
59
+ const data = pad(input, 64, 8, false);
60
+ const view = new DataView(data.buffer);
61
+ const h = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
62
+ const w = new Int32Array(80);
63
+ for (let off = 0; off < data.length; off += 64) {
64
+ for (let i = 0; i < 16; i++) w[i] = view.getInt32(off + i * 4);
65
+ for (let i = 16; i < 80; i++) w[i] = rotl(w[i - 3]! ^ w[i - 8]! ^ w[i - 14]! ^ w[i - 16]!, 1);
66
+ let [a, b, c, d, e] = h as [number, number, number, number, number];
67
+ for (let i = 0; i < 80; i++) {
68
+ const [f, k] = i < 20 ? [(b & c) | (~b & d), 0x5a827999] : i < 40 ? [b ^ c ^ d, 0x6ed9eba1] : i < 60 ? [(b & c) | (b & d) | (c & d), 0x8f1bbcdc] : [b ^ c ^ d, 0xca62c1d6];
69
+ const t = (rotl(a, 5) + f + e + k + w[i]!) | 0;
70
+ e = d; d = c; c = rotl(b, 30); b = a; a = t;
71
+ }
72
+ h[0] = (h[0]! + a) | 0; h[1] = (h[1]! + b) | 0; h[2] = (h[2]! + c) | 0; h[3] = (h[3]! + d) | 0; h[4] = (h[4]! + e) | 0;
73
+ }
74
+ const out = new DataView(new ArrayBuffer(20));
75
+ h.forEach((v, i) => out.setUint32(i * 4, v >>> 0));
76
+ return new Uint8Array(out.buffer);
77
+ }
78
+
79
+ const K256 = [
80
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
81
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
82
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
83
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
84
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
85
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
86
+ ];
87
+
88
+ function sha256(input: Uint8Array, init = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19], outWords = 8): Uint8Array {
89
+ const data = pad(input, 64, 8, false);
90
+ const view = new DataView(data.buffer);
91
+ const h = [...init];
92
+ const w = new Int32Array(64);
93
+ for (let off = 0; off < data.length; off += 64) {
94
+ for (let i = 0; i < 16; i++) w[i] = view.getInt32(off + i * 4);
95
+ for (let i = 16; i < 64; i++) {
96
+ const s0 = rotr(w[i - 15]!, 7) ^ rotr(w[i - 15]!, 18) ^ (w[i - 15]! >>> 3);
97
+ const s1 = rotr(w[i - 2]!, 17) ^ rotr(w[i - 2]!, 19) ^ (w[i - 2]! >>> 10);
98
+ w[i] = (w[i - 16]! + s0 + w[i - 7]! + s1) | 0;
99
+ }
100
+ let [a, b, c, d, e, f, g, hh] = h as [number, number, number, number, number, number, number, number];
101
+ for (let i = 0; i < 64; i++) {
102
+ const t1 = (hh + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + K256[i]! + w[i]!) | 0;
103
+ const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
104
+ hh = g; g = f; f = e; e = (d + t1) | 0; d = c; c = b; b = a; a = (t1 + t2) | 0;
105
+ }
106
+ [a, b, c, d, e, f, g, hh].forEach((v, i) => (h[i] = (h[i]! + v) | 0));
107
+ }
108
+ const out = new DataView(new ArrayBuffer(outWords * 4));
109
+ for (let i = 0; i < outWords; i++) out.setUint32(i * 4, h[i]! >>> 0);
110
+ return new Uint8Array(out.buffer);
111
+ }
112
+
113
+ const K512 = [
114
+ '428a2f98d728ae22', '7137449123ef65cd', 'b5c0fbcfec4d3b2f', 'e9b5dba58189dbbc', '3956c25bf348b538', '59f111f1b605d019', '923f82a4af194f9b', 'ab1c5ed5da6d8118',
115
+ 'd807aa98a3030242', '12835b0145706fbe', '243185be4ee4b28c', '550c7dc3d5ffb4e2', '72be5d74f27b896f', '80deb1fe3b1696b1', '9bdc06a725c71235', 'c19bf174cf692694',
116
+ 'e49b69c19ef14ad2', 'efbe4786384f25e3', '0fc19dc68b8cd5b5', '240ca1cc77ac9c65', '2de92c6f592b0275', '4a7484aa6ea6e483', '5cb0a9dcbd41fbd4', '76f988da831153b5',
117
+ '983e5152ee66dfab', 'a831c66d2db43210', 'b00327c898fb213f', 'bf597fc7beef0ee4', 'c6e00bf33da88fc2', 'd5a79147930aa725', '06ca6351e003826f', '142929670a0e6e70',
118
+ '27b70a8546d22ffc', '2e1b21385c26c926', '4d2c6dfc5ac42aed', '53380d139d95b3df', '650a73548baf63de', '766a0abb3c77b2a8', '81c2c92e47edaee6', '92722c851482353b',
119
+ 'a2bfe8a14cf10364', 'a81a664bbc423001', 'c24b8b70d0f89791', 'c76c51a30654be30', 'd192e819d6ef5218', 'd69906245565a910', 'f40e35855771202a', '106aa07032bbd1b8',
120
+ '19a4c116b8d2d0c8', '1e376c085141ab53', '2748774cdf8eeb99', '34b0bcb5e19b48a8', '391c0cb3c5c95a63', '4ed8aa4ae3418acb', '5b9cca4f7763e373', '682e6ff3d6b2b8a3',
121
+ '748f82ee5defb2fc', '78a5636f43172f60', '84c87814a1f0ab72', '8cc702081a6439ec', '90befffa23631e28', 'a4506cebde82bde9', 'bef9a3f7b2c67915', 'c67178f2e372532b',
122
+ 'ca273eceea26619c', 'd186b8c721c0c207', 'eada7dd6cde0eb1e', 'f57d4f7fee6ed178', '06f067aa72176fba', '0a637dc5a2c898a6', '113f9804bef90dae', '1b710b35131c471b',
123
+ '28db77f523047d84', '32caab7b40c72493', '3c9ebe0a15c9bebc', '431d67c49c100d4c', '4cc5d4becb3e42b6', '597f299cfc657e2a', '5fcb6fab3ad6faec', '6c44198c4a475817',
124
+ ].map((h) => BigInt('0x' + h));
125
+
126
+ const M64 = (1n << 64n) - 1n;
127
+ const rotr64 = (x: bigint, n: bigint) => ((x >> n) | (x << (64n - n))) & M64;
128
+
129
+ function sha512(input: Uint8Array, init: string[], outBytes: number): Uint8Array {
130
+ const data = pad(input, 128, 16, false);
131
+ const view = new DataView(data.buffer);
132
+ const h = init.map((v) => BigInt('0x' + v));
133
+ const w: bigint[] = new Array(80);
134
+ for (let off = 0; off < data.length; off += 128) {
135
+ for (let i = 0; i < 16; i++) w[i] = view.getBigUint64(off + i * 8);
136
+ for (let i = 16; i < 80; i++) {
137
+ const s0 = rotr64(w[i - 15]!, 1n) ^ rotr64(w[i - 15]!, 8n) ^ (w[i - 15]! >> 7n);
138
+ const s1 = rotr64(w[i - 2]!, 19n) ^ rotr64(w[i - 2]!, 61n) ^ (w[i - 2]! >> 6n);
139
+ w[i] = (w[i - 16]! + s0 + w[i - 7]! + s1) & M64;
140
+ }
141
+ let [a, b, c, d, e, f, g, hh] = h as [bigint, bigint, bigint, bigint, bigint, bigint, bigint, bigint];
142
+ for (let i = 0; i < 80; i++) {
143
+ const t1 = (hh + (rotr64(e, 14n) ^ rotr64(e, 18n) ^ rotr64(e, 41n)) + ((e & f) ^ (~e & M64 & g)) + K512[i]! + w[i]!) & M64;
144
+ const t2 = ((rotr64(a, 28n) ^ rotr64(a, 34n) ^ rotr64(a, 39n)) + ((a & b) ^ (a & c) ^ (b & c))) & M64;
145
+ hh = g; g = f; f = e; e = (d + t1) & M64; d = c; c = b; b = a; a = (t1 + t2) & M64;
146
+ }
147
+ [a, b, c, d, e, f, g, hh].forEach((v, i) => (h[i] = (h[i]! + v) & M64));
148
+ }
149
+ const out = new DataView(new ArrayBuffer(64));
150
+ h.forEach((v, i) => out.setBigUint64(i * 8, v));
151
+ return new Uint8Array(out.buffer).slice(0, outBytes);
152
+ }
153
+
154
+ const SHA512_INIT = ['6a09e667f3bcc908', 'bb67ae8584caa73b', '3c6ef372fe94f82b', 'a54ff53a5f1d36f1', '510e527fade682d1', '9b05688c2b3e6c1f', '1f83d9abfb41bd6b', '5be0cd19137e2179'];
155
+ const SHA384_INIT = ['cbbb9d5dc1059ed8', '629a292a367cd507', '9159015a3070dd17', '152fecd8f70e5939', '67332667ffc00b31', '8eb44a8768581511', 'db0c2e0d64f98fa7', '47b5481dbefa4fa4'];
156
+
157
+ export const HASHES: Record<string, { digest(data: Uint8Array): Uint8Array; blockSize: number }> = {
158
+ md5: { digest: md5, blockSize: 64 },
159
+ sha1: { digest: sha1, blockSize: 64 },
160
+ sha256: { digest: (d) => sha256(d), blockSize: 64 },
161
+ sha224: { digest: (d) => sha256(d, [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4], 7), blockSize: 64 },
162
+ sha384: { digest: (d) => sha512(d, SHA384_INIT, 48), blockSize: 128 },
163
+ sha512: { digest: (d) => sha512(d, SHA512_INIT, 64), blockSize: 128 },
164
+ };
165
+
166
+ export function hashAlgorithm(name: string): { digest(data: Uint8Array): Uint8Array; blockSize: number } {
167
+ const key = name.toLowerCase().replace(/^rsa-/, '').replace('-', '');
168
+ const algo = HASHES[key];
169
+ if (!algo) throw new Error(`Digest method not supported: ${name}`);
170
+ return algo;
171
+ }