@volter/tabnode 0.2.14-volter.88

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 (429) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +78 -0
  3. package/THIRD-PARTY-NOTICES.md +12 -0
  4. package/dist/__sw__.js +526 -0
  5. package/dist/assets/runtime-worker-4MKaHFVb.js +75221 -0
  6. package/dist/assets/runtime-worker-4MKaHFVb.js.map +1 -0
  7. package/dist/code-transforms.d.ts +21 -0
  8. package/dist/code-transforms.d.ts.map +1 -0
  9. package/dist/config/cdn.d.ts +12 -0
  10. package/dist/config/cdn.d.ts.map +1 -0
  11. package/dist/create-runtime.d.ts +22 -0
  12. package/dist/create-runtime.d.ts.map +1 -0
  13. package/dist/host-globals.d.ts +62 -0
  14. package/dist/host-globals.d.ts.map +1 -0
  15. package/dist/index.cjs +36021 -0
  16. package/dist/index.cjs.map +1 -0
  17. package/dist/index.d.ts +127 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.mjs +36020 -0
  20. package/dist/index.mjs.map +1 -0
  21. package/dist/node-internals.d.ts +68 -0
  22. package/dist/node-internals.d.ts.map +1 -0
  23. package/dist/node-lib/binding/blob.d.ts +12 -0
  24. package/dist/node-lib/binding/blob.d.ts.map +1 -0
  25. package/dist/node-lib/binding/buffer.d.ts +144 -0
  26. package/dist/node-lib/binding/buffer.d.ts.map +1 -0
  27. package/dist/node-lib/binding/cares_wrap.d.ts +14 -0
  28. package/dist/node-lib/binding/cares_wrap.d.ts.map +1 -0
  29. package/dist/node-lib/binding/fds.d.ts +30 -0
  30. package/dist/node-lib/binding/fds.d.ts.map +1 -0
  31. package/dist/node-lib/binding/fs.d.ts +157 -0
  32. package/dist/node-lib/binding/fs.d.ts.map +1 -0
  33. package/dist/node-lib/binding/handles.d.ts +81 -0
  34. package/dist/node-lib/binding/handles.d.ts.map +1 -0
  35. package/dist/node-lib/binding/http_parser.d.ts +122 -0
  36. package/dist/node-lib/binding/http_parser.d.ts.map +1 -0
  37. package/dist/node-lib/binding/index.d.ts +3 -0
  38. package/dist/node-lib/binding/index.d.ts.map +1 -0
  39. package/dist/node-lib/binding/llhttp-wasm.d.ts +22 -0
  40. package/dist/node-lib/binding/llhttp-wasm.d.ts.map +1 -0
  41. package/dist/node-lib/binding/misc.d.ts +123 -0
  42. package/dist/node-lib/binding/misc.d.ts.map +1 -0
  43. package/dist/node-lib/binding/pipe_wrap.d.ts +84 -0
  44. package/dist/node-lib/binding/pipe_wrap.d.ts.map +1 -0
  45. package/dist/node-lib/binding/process_wrap.d.ts +173 -0
  46. package/dist/node-lib/binding/process_wrap.d.ts.map +1 -0
  47. package/dist/node-lib/binding/spawn_sync.d.ts +30 -0
  48. package/dist/node-lib/binding/spawn_sync.d.ts.map +1 -0
  49. package/dist/node-lib/binding/stream_wrap.d.ts +191 -0
  50. package/dist/node-lib/binding/stream_wrap.d.ts.map +1 -0
  51. package/dist/node-lib/binding/stubs.d.ts +23 -0
  52. package/dist/node-lib/binding/stubs.d.ts.map +1 -0
  53. package/dist/node-lib/binding/tcp_wrap.d.ts +98 -0
  54. package/dist/node-lib/binding/tcp_wrap.d.ts.map +1 -0
  55. package/dist/node-lib/binding/tty_wrap.d.ts +36 -0
  56. package/dist/node-lib/binding/tty_wrap.d.ts.map +1 -0
  57. package/dist/node-lib/binding/util.d.ts +126 -0
  58. package/dist/node-lib/binding/util.d.ts.map +1 -0
  59. package/dist/node-lib/binding/uv.d.ts +32 -0
  60. package/dist/node-lib/binding/uv.d.ts.map +1 -0
  61. package/dist/node-lib/binding/zlib-pako.d.ts +22 -0
  62. package/dist/node-lib/binding/zlib-pako.d.ts.map +1 -0
  63. package/dist/node-lib/binding/zlib.d.ts +38 -0
  64. package/dist/node-lib/binding/zlib.d.ts.map +1 -0
  65. package/dist/node-lib/buffer-module.d.ts +51 -0
  66. package/dist/node-lib/buffer-module.d.ts.map +1 -0
  67. package/dist/node-lib/child-process-module.d.ts +26 -0
  68. package/dist/node-lib/child-process-module.d.ts.map +1 -0
  69. package/dist/node-lib/events-module.d.ts +58 -0
  70. package/dist/node-lib/events-module.d.ts.map +1 -0
  71. package/dist/node-lib/fs-module.d.ts +5 -0
  72. package/dist/node-lib/fs-module.d.ts.map +1 -0
  73. package/dist/node-lib/http-bridge.d.ts +43 -0
  74. package/dist/node-lib/http-bridge.d.ts.map +1 -0
  75. package/dist/node-lib/http-module.d.ts +35 -0
  76. package/dist/node-lib/http-module.d.ts.map +1 -0
  77. package/dist/node-lib/internals/addresses.d.ts +40 -0
  78. package/dist/node-lib/internals/addresses.d.ts.map +1 -0
  79. package/dist/node-lib/internals/buffer-and-streams.d.ts +96 -0
  80. package/dist/node-lib/internals/buffer-and-streams.d.ts.map +1 -0
  81. package/dist/node-lib/internals/child-process.d.ts +27 -0
  82. package/dist/node-lib/internals/child-process.d.ts.map +1 -0
  83. package/dist/node-lib/internals/events-util.d.ts +83 -0
  84. package/dist/node-lib/internals/events-util.d.ts.map +1 -0
  85. package/dist/node-lib/internals/index.d.ts +2 -0
  86. package/dist/node-lib/internals/index.d.ts.map +1 -0
  87. package/dist/node-lib/internals/modules.d.ts +67 -0
  88. package/dist/node-lib/internals/modules.d.ts.map +1 -0
  89. package/dist/node-lib/internals/readline-access.d.ts +2 -0
  90. package/dist/node-lib/internals/readline-access.d.ts.map +1 -0
  91. package/dist/node-lib/internals/runtime.d.ts +150 -0
  92. package/dist/node-lib/internals/runtime.d.ts.map +1 -0
  93. package/dist/node-lib/internals/util.d.ts +33 -0
  94. package/dist/node-lib/internals/util.d.ts.map +1 -0
  95. package/dist/node-lib/lazy.d.ts +31 -0
  96. package/dist/node-lib/lazy.d.ts.map +1 -0
  97. package/dist/node-lib/load.d.ts +27 -0
  98. package/dist/node-lib/load.d.ts.map +1 -0
  99. package/dist/node-lib/module-hooks.d.ts +89 -0
  100. package/dist/node-lib/module-hooks.d.ts.map +1 -0
  101. package/dist/node-lib/net-module.d.ts +181 -0
  102. package/dist/node-lib/net-module.d.ts.map +1 -0
  103. package/dist/node-lib/node-versions.d.ts +27 -0
  104. package/dist/node-lib/node-versions.d.ts.map +1 -0
  105. package/dist/node-lib/public-modules.d.ts +2 -0
  106. package/dist/node-lib/public-modules.d.ts.map +1 -0
  107. package/dist/node-lib/require-hook.d.ts +15 -0
  108. package/dist/node-lib/require-hook.d.ts.map +1 -0
  109. package/dist/node-lib/small-modules.d.ts +16 -0
  110. package/dist/node-lib/small-modules.d.ts.map +1 -0
  111. package/dist/node-lib/sources.d.ts +4 -0
  112. package/dist/node-lib/sources.d.ts.map +1 -0
  113. package/dist/node-lib/stream-module.d.ts +77 -0
  114. package/dist/node-lib/stream-module.d.ts.map +1 -0
  115. package/dist/node-lib/timers.d.ts +19 -0
  116. package/dist/node-lib/timers.d.ts.map +1 -0
  117. package/dist/node-lib/util-module.d.ts +76 -0
  118. package/dist/node-lib/util-module.d.ts.map +1 -0
  119. package/dist/node-resolution.d.ts +50 -0
  120. package/dist/node-resolution.d.ts.map +1 -0
  121. package/dist/node-resolver.d.ts +7 -0
  122. package/dist/node-resolver.d.ts.map +1 -0
  123. package/dist/npm/index.d.ts +73 -0
  124. package/dist/npm/index.d.ts.map +1 -0
  125. package/dist/npm/registry.d.ts +111 -0
  126. package/dist/npm/registry.d.ts.map +1 -0
  127. package/dist/npm/resolver.d.ts +79 -0
  128. package/dist/npm/resolver.d.ts.map +1 -0
  129. package/dist/npm/tarball.d.ts +28 -0
  130. package/dist/npm/tarball.d.ts.map +1 -0
  131. package/dist/process-tokens.d.ts +77 -0
  132. package/dist/process-tokens.d.ts.map +1 -0
  133. package/dist/punycode-source.d.ts +3 -0
  134. package/dist/punycode-source.d.ts.map +1 -0
  135. package/dist/runtime-interface.d.ts +74 -0
  136. package/dist/runtime-interface.d.ts.map +1 -0
  137. package/dist/runtime.d.ts +144 -0
  138. package/dist/runtime.d.ts.map +1 -0
  139. package/dist/server-bridge.d.ts +147 -0
  140. package/dist/server-bridge.d.ts.map +1 -0
  141. package/dist/shims/async_hooks.d.ts +50 -0
  142. package/dist/shims/async_hooks.d.ts.map +1 -0
  143. package/dist/shims/child_process.d.ts +104 -0
  144. package/dist/shims/child_process.d.ts.map +1 -0
  145. package/dist/shims/cluster.d.ts +52 -0
  146. package/dist/shims/cluster.d.ts.map +1 -0
  147. package/dist/shims/command-line.d.ts +43 -0
  148. package/dist/shims/command-line.d.ts.map +1 -0
  149. package/dist/shims/constants.d.ts +222 -0
  150. package/dist/shims/constants.d.ts.map +1 -0
  151. package/dist/shims/crypto-constants.d.ts +12 -0
  152. package/dist/shims/crypto-constants.d.ts.map +1 -0
  153. package/dist/shims/crypto.d.ts +144 -0
  154. package/dist/shims/crypto.d.ts.map +1 -0
  155. package/dist/shims/dgram.d.ts +34 -0
  156. package/dist/shims/dgram.d.ts.map +1 -0
  157. package/dist/shims/dns.d.ts +91 -0
  158. package/dist/shims/dns.d.ts.map +1 -0
  159. package/dist/shims/domain.d.ts +25 -0
  160. package/dist/shims/domain.d.ts.map +1 -0
  161. package/dist/shims/esbuild.d.ts +205 -0
  162. package/dist/shims/esbuild.d.ts.map +1 -0
  163. package/dist/shims/fs.d.ts +25 -0
  164. package/dist/shims/fs.d.ts.map +1 -0
  165. package/dist/shims/http2.d.ts +81 -0
  166. package/dist/shims/http2.d.ts.map +1 -0
  167. package/dist/shims/inspector.d.ts +25 -0
  168. package/dist/shims/inspector.d.ts.map +1 -0
  169. package/dist/shims/module.d.ts +22 -0
  170. package/dist/shims/module.d.ts.map +1 -0
  171. package/dist/shims/os.d.ts +159 -0
  172. package/dist/shims/os.d.ts.map +1 -0
  173. package/dist/shims/path.d.ts +52 -0
  174. package/dist/shims/path.d.ts.map +1 -0
  175. package/dist/shims/perf_hooks.d.ts +50 -0
  176. package/dist/shims/perf_hooks.d.ts.map +1 -0
  177. package/dist/shims/process.d.ts +226 -0
  178. package/dist/shims/process.d.ts.map +1 -0
  179. package/dist/shims/rollup.d.ts +71 -0
  180. package/dist/shims/rollup.d.ts.map +1 -0
  181. package/dist/shims/string_decoder.d.ts +41 -0
  182. package/dist/shims/string_decoder.d.ts.map +1 -0
  183. package/dist/shims/sync-child.d.ts +64 -0
  184. package/dist/shims/sync-child.d.ts.map +1 -0
  185. package/dist/shims/tls.d.ts +53 -0
  186. package/dist/shims/tls.d.ts.map +1 -0
  187. package/dist/shims/url.d.ts +84 -0
  188. package/dist/shims/url.d.ts.map +1 -0
  189. package/dist/shims/v8.d.ts +73 -0
  190. package/dist/shims/v8.d.ts.map +1 -0
  191. package/dist/shims/vfs-adapter.d.ts +126 -0
  192. package/dist/shims/vfs-adapter.d.ts.map +1 -0
  193. package/dist/shims/vm.d.ts +52 -0
  194. package/dist/shims/vm.d.ts.map +1 -0
  195. package/dist/shims/wasi.d.ts +78 -0
  196. package/dist/shims/wasi.d.ts.map +1 -0
  197. package/dist/shims/worker_threads.d.ts +66 -0
  198. package/dist/shims/worker_threads.d.ts.map +1 -0
  199. package/dist/shims/zlib-constants.d.ts +66 -0
  200. package/dist/shims/zlib-constants.d.ts.map +1 -0
  201. package/dist/shims/zlib.d.ts +102 -0
  202. package/dist/shims/zlib.d.ts.map +1 -0
  203. package/dist/transform.d.ts +24 -0
  204. package/dist/transform.d.ts.map +1 -0
  205. package/dist/types/package-json.d.ts +18 -0
  206. package/dist/types/package-json.d.ts.map +1 -0
  207. package/dist/utils/binary-encoding.d.ts +13 -0
  208. package/dist/utils/binary-encoding.d.ts.map +1 -0
  209. package/dist/utils/glob.d.ts +19 -0
  210. package/dist/utils/glob.d.ts.map +1 -0
  211. package/dist/utils/hash.d.ts +6 -0
  212. package/dist/utils/hash.d.ts.map +1 -0
  213. package/dist/virtual-fs.d.ts +259 -0
  214. package/dist/virtual-fs.d.ts.map +1 -0
  215. package/dist/vite-plugin.cjs +50 -0
  216. package/dist/vite-plugin.cjs.map +1 -0
  217. package/dist/vite-plugin.d.ts +36 -0
  218. package/dist/vite-plugin.d.ts.map +1 -0
  219. package/dist/vite-plugin.mjs +31 -0
  220. package/dist/vite-plugin.mjs.map +1 -0
  221. package/dist/worker/runtime-worker.d.ts +8 -0
  222. package/dist/worker/runtime-worker.d.ts.map +1 -0
  223. package/dist/worker-runtime.d.ts +50 -0
  224. package/dist/worker-runtime.d.ts.map +1 -0
  225. package/package.json +88 -0
  226. package/src/code-transforms.ts +560 -0
  227. package/src/config/cdn.ts +13 -0
  228. package/src/create-runtime.ts +89 -0
  229. package/src/host-globals.ts +121 -0
  230. package/src/index.ts +235 -0
  231. package/src/node-internals.ts +283 -0
  232. package/src/node-lib/LICENSE +47 -0
  233. package/src/node-lib/_http_agent.js +547 -0
  234. package/src/node-lib/_http_client.js +996 -0
  235. package/src/node-lib/_http_common.js +269 -0
  236. package/src/node-lib/_http_incoming.js +453 -0
  237. package/src/node-lib/_http_outgoing.js +1262 -0
  238. package/src/node-lib/_http_server.js +1230 -0
  239. package/src/node-lib/assert/strict.js +3 -0
  240. package/src/node-lib/assert.js +844 -0
  241. package/src/node-lib/binding/blob.ts +35 -0
  242. package/src/node-lib/binding/buffer.ts +465 -0
  243. package/src/node-lib/binding/cares_wrap.ts +57 -0
  244. package/src/node-lib/binding/fds.ts +80 -0
  245. package/src/node-lib/binding/fs.ts +857 -0
  246. package/src/node-lib/binding/handles.ts +187 -0
  247. package/src/node-lib/binding/http_parser.ts +536 -0
  248. package/src/node-lib/binding/index.ts +77 -0
  249. package/src/node-lib/binding/llhttp-wasm.ts +670 -0
  250. package/src/node-lib/binding/misc.ts +252 -0
  251. package/src/node-lib/binding/pipe_wrap.ts +166 -0
  252. package/src/node-lib/binding/process_wrap.ts +371 -0
  253. package/src/node-lib/binding/spawn_sync.ts +141 -0
  254. package/src/node-lib/binding/stream_wrap.ts +413 -0
  255. package/src/node-lib/binding/stubs.ts +19 -0
  256. package/src/node-lib/binding/tcp_wrap.ts +254 -0
  257. package/src/node-lib/binding/tty_wrap.ts +56 -0
  258. package/src/node-lib/binding/util.ts +274 -0
  259. package/src/node-lib/binding/uv.ts +102 -0
  260. package/src/node-lib/binding/zlib-pako.ts +50 -0
  261. package/src/node-lib/binding/zlib.ts +259 -0
  262. package/src/node-lib/buffer-module.ts +70 -0
  263. package/src/node-lib/buffer.js +1373 -0
  264. package/src/node-lib/child-process-module.ts +58 -0
  265. package/src/node-lib/child_process.js +1046 -0
  266. package/src/node-lib/constants.js +41 -0
  267. package/src/node-lib/diagnostics_channel.js +438 -0
  268. package/src/node-lib/events-module.ts +80 -0
  269. package/src/node-lib/events.js +1223 -0
  270. package/src/node-lib/fs-module.ts +43 -0
  271. package/src/node-lib/fs.js +3377 -0
  272. package/src/node-lib/http-bridge.ts +299 -0
  273. package/src/node-lib/http-module.ts +53 -0
  274. package/src/node-lib/http.js +198 -0
  275. package/src/node-lib/https.js +429 -0
  276. package/src/node-lib/internal/assert/assertion_error.js +417 -0
  277. package/src/node-lib/internal/assert/calltracker.js +153 -0
  278. package/src/node-lib/internal/assert/myers_diff.js +177 -0
  279. package/src/node-lib/internal/assert/utils.js +278 -0
  280. package/src/node-lib/internal/buffer.js +1120 -0
  281. package/src/node-lib/internal/child_process/serialization.js +169 -0
  282. package/src/node-lib/internal/child_process.js +1135 -0
  283. package/src/node-lib/internal/constants.js +56 -0
  284. package/src/node-lib/internal/deps/minimatch/index.js +1628 -0
  285. package/src/node-lib/internal/errors.js +1912 -0
  286. package/src/node-lib/internal/events/abort_listener.js +54 -0
  287. package/src/node-lib/internal/events/symbols.js +11 -0
  288. package/src/node-lib/internal/fixed_queue.js +119 -0
  289. package/src/node-lib/internal/freelist.js +30 -0
  290. package/src/node-lib/internal/fs/cp/cp-sync.js +242 -0
  291. package/src/node-lib/internal/fs/cp/cp.js +382 -0
  292. package/src/node-lib/internal/fs/dir.js +369 -0
  293. package/src/node-lib/internal/fs/glob.js +763 -0
  294. package/src/node-lib/internal/fs/promises.js +1312 -0
  295. package/src/node-lib/internal/fs/read/context.js +129 -0
  296. package/src/node-lib/internal/fs/recursive_watch.js +275 -0
  297. package/src/node-lib/internal/fs/rimraf.js +311 -0
  298. package/src/node-lib/internal/fs/streams.js +571 -0
  299. package/src/node-lib/internal/fs/sync_write_stream.js +49 -0
  300. package/src/node-lib/internal/fs/utils.js +987 -0
  301. package/src/node-lib/internal/fs/watchers.js +418 -0
  302. package/src/node-lib/internal/http.js +62 -0
  303. package/src/node-lib/internal/mime.js +389 -0
  304. package/src/node-lib/internal/modules/customization_hooks.js +406 -0
  305. package/src/node-lib/internal/net.js +95 -0
  306. package/src/node-lib/internal/querystring.js +119 -0
  307. package/src/node-lib/internal/readline/callbacks.js +132 -0
  308. package/src/node-lib/internal/readline/emitKeypressEvents.js +99 -0
  309. package/src/node-lib/internal/readline/interface.js +1418 -0
  310. package/src/node-lib/internal/readline/promises.js +144 -0
  311. package/src/node-lib/internal/readline/utils.js +405 -0
  312. package/src/node-lib/internal/socket_list.js +108 -0
  313. package/src/node-lib/internal/stream_base_commons.js +280 -0
  314. package/src/node-lib/internal/streams/add-abort-signal.js +60 -0
  315. package/src/node-lib/internal/streams/compose.js +257 -0
  316. package/src/node-lib/internal/streams/destroy.js +361 -0
  317. package/src/node-lib/internal/streams/duplex.js +203 -0
  318. package/src/node-lib/internal/streams/duplexify.js +379 -0
  319. package/src/node-lib/internal/streams/duplexpair.js +62 -0
  320. package/src/node-lib/internal/streams/end-of-stream.js +331 -0
  321. package/src/node-lib/internal/streams/from.js +208 -0
  322. package/src/node-lib/internal/streams/lazy_transform.js +57 -0
  323. package/src/node-lib/internal/streams/legacy.js +125 -0
  324. package/src/node-lib/internal/streams/operators.js +436 -0
  325. package/src/node-lib/internal/streams/passthrough.js +47 -0
  326. package/src/node-lib/internal/streams/pipeline.js +465 -0
  327. package/src/node-lib/internal/streams/readable.js +1762 -0
  328. package/src/node-lib/internal/streams/state.js +51 -0
  329. package/src/node-lib/internal/streams/transform.js +204 -0
  330. package/src/node-lib/internal/streams/utils.js +363 -0
  331. package/src/node-lib/internal/streams/writable.js +1163 -0
  332. package/src/node-lib/internal/tty.js +260 -0
  333. package/src/node-lib/internal/util/colors.js +40 -0
  334. package/src/node-lib/internal/util/comparisons.js +1028 -0
  335. package/src/node-lib/internal/util/debuglog.js +524 -0
  336. package/src/node-lib/internal/util/inspect.js +2535 -0
  337. package/src/node-lib/internal/util/parse_args/parse_args.js +412 -0
  338. package/src/node-lib/internal/util/parse_args/utils.js +197 -0
  339. package/src/node-lib/internal/util.js +1003 -0
  340. package/src/node-lib/internal/validators.js +654 -0
  341. package/src/node-lib/internals/addresses.ts +159 -0
  342. package/src/node-lib/internals/buffer-and-streams.ts +224 -0
  343. package/src/node-lib/internals/child-process.ts +64 -0
  344. package/src/node-lib/internals/events-util.ts +105 -0
  345. package/src/node-lib/internals/index.ts +66 -0
  346. package/src/node-lib/internals/modules.ts +117 -0
  347. package/src/node-lib/internals/readline-access.ts +16 -0
  348. package/src/node-lib/internals/runtime.ts +277 -0
  349. package/src/node-lib/internals/util.ts +98 -0
  350. package/src/node-lib/lazy.ts +158 -0
  351. package/src/node-lib/load.ts +390 -0
  352. package/src/node-lib/module-hooks.ts +281 -0
  353. package/src/node-lib/net-module.ts +248 -0
  354. package/src/node-lib/net.js +2540 -0
  355. package/src/node-lib/node-versions.ts +34 -0
  356. package/src/node-lib/os.js +356 -0
  357. package/src/node-lib/path.js +1694 -0
  358. package/src/node-lib/primordials.js +743 -0
  359. package/src/node-lib/public-modules.ts +109 -0
  360. package/src/node-lib/punycode.js +455 -0
  361. package/src/node-lib/querystring.js +484 -0
  362. package/src/node-lib/readline/promises.js +73 -0
  363. package/src/node-lib/readline.js +523 -0
  364. package/src/node-lib/require-hook.ts +27 -0
  365. package/src/node-lib/small-modules.ts +35 -0
  366. package/src/node-lib/sources.ts +200 -0
  367. package/src/node-lib/stream/promises.js +44 -0
  368. package/src/node-lib/stream-module.ts +101 -0
  369. package/src/node-lib/stream.js +152 -0
  370. package/src/node-lib/timers.ts +33 -0
  371. package/src/node-lib/tty.js +168 -0
  372. package/src/node-lib/util-module.ts +104 -0
  373. package/src/node-lib/util.js +689 -0
  374. package/src/node-lib/wasi.js +179 -0
  375. package/src/node-lib/zlib.js +1036 -0
  376. package/src/node-resolution.ts +241 -0
  377. package/src/node-resolver.ts +33 -0
  378. package/src/npm/index.ts +425 -0
  379. package/src/npm/registry.ts +235 -0
  380. package/src/npm/resolver.ts +589 -0
  381. package/src/npm/tarball.ts +246 -0
  382. package/src/process-tokens.ts +156 -0
  383. package/src/punycode-source.ts +6 -0
  384. package/src/runtime-interface.ts +85 -0
  385. package/src/runtime.ts +3073 -0
  386. package/src/server-bridge.ts +971 -0
  387. package/src/shims/async_hooks.ts +181 -0
  388. package/src/shims/child_process.ts +1591 -0
  389. package/src/shims/cluster.ts +79 -0
  390. package/src/shims/command-line.ts +79 -0
  391. package/src/shims/constants.ts +68 -0
  392. package/src/shims/crypto-constants.ts +11 -0
  393. package/src/shims/crypto.ts +1061 -0
  394. package/src/shims/dgram.ts +47 -0
  395. package/src/shims/dns.ts +185 -0
  396. package/src/shims/domain.ts +58 -0
  397. package/src/shims/esbuild.ts +1638 -0
  398. package/src/shims/fs.ts +82 -0
  399. package/src/shims/http2.ts +96 -0
  400. package/src/shims/inspector.ts +30 -0
  401. package/src/shims/module.ts +86 -0
  402. package/src/shims/os.ts +195 -0
  403. package/src/shims/path.ts +246 -0
  404. package/src/shims/perf_hooks.ts +92 -0
  405. package/src/shims/process.ts +804 -0
  406. package/src/shims/rollup.ts +149 -0
  407. package/src/shims/string_decoder.ts +396 -0
  408. package/src/shims/sync-child.ts +469 -0
  409. package/src/shims/tls.ts +94 -0
  410. package/src/shims/url.ts +217 -0
  411. package/src/shims/v8.ts +113 -0
  412. package/src/shims/vfs-adapter.ts +408 -0
  413. package/src/shims/vm.ts +132 -0
  414. package/src/shims/wasi.ts +1440 -0
  415. package/src/shims/worker_threads.ts +114 -0
  416. package/src/shims/zlib-constants.ts +65 -0
  417. package/src/shims/zlib.ts +228 -0
  418. package/src/transform.ts +324 -0
  419. package/src/types/external.d.ts +55 -0
  420. package/src/types/package-json.ts +17 -0
  421. package/src/types/pako-zlib.d.ts +30 -0
  422. package/src/types/sha.js.d.ts +14 -0
  423. package/src/utils/binary-encoding.ts +43 -0
  424. package/src/utils/glob.ts +54 -0
  425. package/src/utils/hash.ts +12 -0
  426. package/src/virtual-fs.ts +1048 -0
  427. package/src/vite-plugin.ts +76 -0
  428. package/src/worker/runtime-worker.ts +128 -0
  429. package/src/worker-runtime.ts +145 -0
@@ -0,0 +1,390 @@
1
+ /**
2
+ * One loader for Node's own files.
3
+ *
4
+ * `src/shims/path.ts` is the precedent: a vendored file binds, it does not
5
+ * implement, and a bug in it is fixed by moving the file to a newer Node.
6
+ * `path` bound its own primordials and its own three internals by hand, which
7
+ * is affordable for one file that names fourteen names. `net.js` names two
8
+ * hundred, and `child_process.js` after it, so the binding is written once
9
+ * here instead: Node's own `internal/per_context/primordials.js` builds the
10
+ * whole `primordials` object over this realm's built-ins the way Node's
11
+ * bootstrap does, and a vendored file is evaluated in the scope Node's
12
+ * `BuiltinModule` gives one —
13
+ *
14
+ * (function (exports, require, module, process, internalBinding, primordials) { … })
15
+ *
16
+ * — so nothing in a vendored file is hand-written and nothing in it is edited.
17
+ *
18
+ * Resolution, in the order Node's own `require` inside a builtin resolves: an
19
+ * `internal/…` name is a vendored file when one is vendored under that name,
20
+ * else the small hand-bound object in `./internals/` that holds exactly the
21
+ * names the vendored files destructure; a public name is the engine's own
22
+ * shim, the same object a guest's `require` answers with.
23
+ * `internalBinding(name)` is the libuv-shaped surface in `./binding/`.
24
+ */
25
+ import PRIMORDIALS_SOURCE from './primordials.js?raw';
26
+ import { NODE_LIB_SOURCES } from './sources';
27
+ import { nodeLibInternal } from './internals';
28
+ import { nodeLibBinding } from './binding';
29
+ import { nodeLibPublic } from './public-modules';
30
+ import { setLibRequire } from './require-hook';
31
+ import { NODE_LTS_VERSION, nodeVersions } from './node-versions';
32
+
33
+ /**
34
+ * The realm's built-ins, captured as Node captures them: before a program can
35
+ * monkey-patch them, in the shape every vendored file destructures. Node runs
36
+ * this file once per context with a `primordials` object to fill and the
37
+ * realm's `globalThis` around it; so does this.
38
+ */
39
+ // `var`, not `let`: hoisted and already `undefined` when a cycle calls in.
40
+ // eslint-disable-next-line no-var, vars-on-top
41
+ var builtPrimordials: Record<string, unknown> | undefined;
42
+ export function primordialsOf(): Record<string, unknown> {
43
+ if (builtPrimordials === undefined) {
44
+ const built: Record<string, unknown> = {};
45
+ // eslint-disable-next-line no-new-func
46
+ new Function('primordials', `${PRIMORDIALS_SOURCE}\n//# sourceURL=node:internal/per_context/primordials`)(built);
47
+ builtPrimordials = built;
48
+ }
49
+ return builtPrimordials;
50
+ }
51
+
52
+ /** A vendored file's module record, as Node's `BuiltinModule` holds one. */
53
+ interface NodeLibModule {
54
+ exports: unknown;
55
+ id: string;
56
+ filename: string;
57
+ loaded: boolean;
58
+ }
59
+
60
+ /**
61
+ * The loader's own state, built on first use rather than when this module is
62
+ * evaluated. A vendored file is asked for from inside the import graph that
63
+ * leads here -- the engine's `string_decoder` shim wants Node's `Buffer`, and
64
+ * `Buffer` is a vendored file -- so the loader must answer whatever order the
65
+ * bundler settles on. Module-scope `const`s left it in its own temporal dead
66
+ * zone and the engine failed to load at all.
67
+ */
68
+ /* eslint-disable no-var, vars-on-top */
69
+ var cache: Map<string, NodeLibModule> | undefined;
70
+ var builtInternals: Map<string, unknown> | undefined;
71
+ var builtBindings: Map<string, unknown> | undefined;
72
+ /* eslint-enable no-var, vars-on-top */
73
+
74
+ /** The libuv-shaped surface, built once per name, on the first file that asks. */
75
+ function internalBinding(name: string): unknown {
76
+ builtBindings ??= new Map<string, unknown>();
77
+ if (builtBindings.has(name)) return builtBindings.get(name);
78
+ const build = nodeLibBinding(name);
79
+ if (!build) throw new Error(`node-lib: no binding for internalBinding('${name}')`);
80
+ const value = build();
81
+ builtBindings.set(name, value);
82
+ return value;
83
+ }
84
+
85
+ /**
86
+ * The process a vendored file is handed where the realm has none: the engine
87
+ * loaded as a library, before any guest has run.
88
+ *
89
+ * Node compiles a builtin inside a bootstrapped process, and Node's own files
90
+ * read that process while they compile -- `internal/util` reads
91
+ * `process.versions.openssl` and `process.versions.amaro` at its top, and
92
+ * `internal/streams/state` reads `process.platform` -- so a realm with no
93
+ * process must still answer the static facts of one. It answers only those:
94
+ * everything a run owns -- its argv, its environment, its streams, its exit --
95
+ * belongs to that run's own process and is answered there.
96
+ *
97
+ * What made this a wall: the engine's `net-module` compiles Node's `net.js`
98
+ * while the bundle is still evaluating, `net` requires `events` and `events`
99
+ * requires `internal/util`, and in a page or a worker -- a realm with no
100
+ * `process` global -- `process.versions` was `undefined`, so the whole engine
101
+ * failed to import with `Cannot read properties of undefined (reading
102
+ * 'openssl')`.
103
+ */
104
+ const NODE_LIB_LIBRARY_PROCESS: Record<string, unknown> = {
105
+ platform: 'linux',
106
+ arch: 'x64',
107
+ version: `v${NODE_LTS_VERSION}`,
108
+ versions: nodeVersions(),
109
+ env: {},
110
+ argv: ['node'],
111
+ argv0: 'node',
112
+ execArgv: [],
113
+ execPath: '/usr/local/bin/node',
114
+ nextTick: (fn: (...args: unknown[]) => void, ...args: unknown[]) => queueMicrotask(() => fn(...args)),
115
+ };
116
+
117
+ /**
118
+ * The `process` a vendored file is handed, built by a hoisted function for
119
+ * the reason `nodeLibInternalRequire` gives: a file can be compiled while the
120
+ * bundle is still evaluating, when a module-scope `var` is still undefined.
121
+ * Node hands a builtin the process of
122
+ * the realm it runs in; so does this, read at call time, because a guest's
123
+ * process is installed on the realm after this module is loaded and every run
124
+ * replaces it. Where the realm has no process at all the answer is
125
+ * {@link NODE_LIB_LIBRARY_PROCESS}.
126
+ */
127
+ var nodeLibProcessCache: Record<string, unknown> | undefined;
128
+ export function nodeLibProcessOf(): Record<string, unknown> {
129
+ return nodeLibProcessCache ??= new Proxy({} as Record<string, unknown>, {
130
+ get(_target, key) {
131
+ const realm = (globalThis as unknown as { process?: Record<string, unknown> }).process;
132
+ if (!realm) return NODE_LIB_LIBRARY_PROCESS[key as string];
133
+ const value = realm[key as string];
134
+ if (key === 'nextTick' && typeof value !== 'function') {
135
+ return (fn: (...args: unknown[]) => void, ...args: unknown[]) => queueMicrotask(() => fn(...args));
136
+ }
137
+ if (key === 'platform' && typeof value !== 'string') return 'linux';
138
+ return value;
139
+ },
140
+ set(_target, key, value) {
141
+ const realm = (globalThis as unknown as { process?: Record<string, unknown> }).process;
142
+ if (realm) realm[key as string] = value;
143
+ return true;
144
+ },
145
+ has(_target, key) {
146
+ const realm = (globalThis as unknown as { process?: Record<string, unknown> }).process;
147
+ return realm ? key in realm : key in NODE_LIB_LIBRARY_PROCESS;
148
+ },
149
+ });
150
+ }
151
+
152
+ function nodeLibRequire(specifier: string): unknown {
153
+ const name = specifier.startsWith('node:') ? specifier.slice(5) : specifier;
154
+ if (NODE_LIB_SOURCES[name] !== undefined) return loadNodeLib(name);
155
+ const internal = nodeLibInternal(name);
156
+ if (internal !== undefined) {
157
+ builtInternals ??= new Map<string, unknown>();
158
+ if (!builtInternals.has(name)) builtInternals.set(name, internal());
159
+ return builtInternals.get(name);
160
+ }
161
+ const shim = nodeLibPublic(name);
162
+ if (shim !== undefined) return shim();
163
+ throw new Error(`node-lib: a vendored Node file asked for '${specifier}', which the binding does not provide`);
164
+ }
165
+
166
+ /**
167
+ * A vendored file, evaluated as Node's `BuiltinModule.compileForInternalLoader`
168
+ * evaluates one and cached under its name. The record is cached before the
169
+ * body runs, as Node's is, so a cycle between two vendored files sees a
170
+ * partly-filled `exports` rather than looping.
171
+ */
172
+ export function loadNodeLib(name: string): unknown {
173
+ cache ??= new Map<string, NodeLibModule>();
174
+ const cached = cache.get(name);
175
+ if (cached) return cached.exports;
176
+ const record = newNodeLibRecord(name);
177
+ // Cached before compile, as Node's is, so a cycle sees a partly-filled
178
+ // `exports`. A throw used to leave that empty record in the cache:
179
+ // `https.js` calls `assertCrypto()` at the top, the first read threw
180
+ // ERR_NO_CRYPTO, and every later `import("https")` answered `{}`, so
181
+ // `.request` was undefined (`r is not a function`).
182
+ cache.set(name, record);
183
+ try {
184
+ compileNodeLib(name, record);
185
+ } catch (error) {
186
+ cache.delete(name);
187
+ throw error;
188
+ }
189
+ return record.exports;
190
+ }
191
+
192
+ /**
193
+ * A vendored file evaluated into a module scope of its own, uncached.
194
+ *
195
+ * Node has one module graph per process and lets a builtin keep its state in
196
+ * its own file scope. The engine runs many guests in one realm, so a file
197
+ * whose scope IS the state is instantiated per run rather than shared:
198
+ * `internal/modules/customization_hooks` holds the hooks a program registered
199
+ * in two module-scope arrays, and one run's hooks must not resolve another
200
+ * run's imports. The caller holds the instance for as long as the run does.
201
+ */
202
+ /**
203
+ * Node's ESM namespace for a CommonJS builtin: `default` is the module
204
+ * object, and every own key of `module.exports` is a named export, live.
205
+ * `import("https").then(m => m.request)` is `require("https").request`.
206
+ * Spread of a lazy proxy copied nothing; `Object.getOwnPropertyNames` reads
207
+ * the proxy's ownKeys trap, which is the module's keys.
208
+ */
209
+ const builtinNamespaces: Array<{ cjs: object; ns: Record<string, unknown> }> = [];
210
+
211
+ export function esmNamespaceOf(mod: unknown): Record<string, unknown> {
212
+ const ns: Record<string, unknown> = { default: mod };
213
+ if (mod !== null && (typeof mod === 'object' || typeof mod === 'function')) {
214
+ const cjs = mod as object;
215
+ for (const key of Object.getOwnPropertyNames(cjs)) {
216
+ if (key === 'default') continue;
217
+ Object.defineProperty(ns, key, {
218
+ enumerable: true,
219
+ configurable: true,
220
+ get(): unknown { return (cjs as Record<string, unknown>)[key]; },
221
+ set(value: unknown) { (cjs as Record<string, unknown>)[key] = value; },
222
+ });
223
+ }
224
+ builtinNamespaces.push({ cjs, ns });
225
+ }
226
+ return ns;
227
+ }
228
+
229
+ /** Node's `module.syncBuiltinESMExports`: newly assigned CJS keys appear on the namespace. */
230
+ export function syncBuiltinESMExports(): void {
231
+ for (const { cjs, ns } of builtinNamespaces) {
232
+ for (const key of Object.getOwnPropertyNames(cjs)) {
233
+ if (key === 'default' || Object.prototype.hasOwnProperty.call(ns, key)) continue;
234
+ Object.defineProperty(ns, key, {
235
+ enumerable: true,
236
+ configurable: true,
237
+ get(): unknown { return (cjs as Record<string, unknown>)[key]; },
238
+ set(value: unknown) { (cjs as Record<string, unknown>)[key] = value; },
239
+ });
240
+ }
241
+ }
242
+ }
243
+
244
+ export function loadNodeLibInstance(name: string): unknown {
245
+ const record = newNodeLibRecord(name);
246
+ compileNodeLib(name, record);
247
+ return record.exports;
248
+ }
249
+
250
+ /**
251
+ * A vendored file cached on one engine instance rather than the realm.
252
+ *
253
+ * Node has one builtin cache per process. The engine runs many processes in
254
+ * one realm, and the realm cache is keyed only by name, so a second
255
+ * `Runtime`'s `require('fs')` was the first's module object: a `Symbol.for`
256
+ * tag written by one guest was readable by the other. The first instance
257
+ * still uses {@link loadNodeLib}; a later instance asks here, and the file
258
+ * is compiled once for that owner. Its `require` of other vendored files
259
+ * still hits the realm cache, so `net` and `events` stay one class.
260
+ */
261
+ let byOwner: WeakMap<object, Map<string, NodeLibModule>> | undefined;
262
+ export function loadNodeLibFor(owner: object, name: string): unknown {
263
+ byOwner ??= new WeakMap();
264
+ let owned = byOwner.get(owner);
265
+ if (!owned) {
266
+ owned = new Map();
267
+ byOwner.set(owner, owned);
268
+ }
269
+ const cached = owned.get(name);
270
+ if (cached) return cached.exports;
271
+ const record = newNodeLibRecord(name);
272
+ owned.set(name, record);
273
+ try {
274
+ compileNodeLib(name, record);
275
+ } catch (error) {
276
+ owned.delete(name);
277
+ throw error;
278
+ }
279
+ return record.exports;
280
+ }
281
+
282
+ function newNodeLibRecord(name: string): NodeLibModule {
283
+ if (NODE_LIB_SOURCES[name] === undefined) throw new Error(`node-lib: no vendored file for '${name}'`);
284
+ return { exports: {}, id: name, filename: `node:${name}`, loaded: false };
285
+ }
286
+
287
+ /** The scope Node's `BuiltinModule.compileForInternalLoader` gives a builtin. */
288
+ function compileNodeLib(name: string, record: NodeLibModule): void {
289
+ // eslint-disable-next-line no-new-func
290
+ const compiled = new Function(
291
+ 'exports', 'require', 'module', 'process', 'internalBinding', 'primordials',
292
+ `${NODE_LIB_SOURCES[name]}\n//# sourceURL=node:${name}`,
293
+ );
294
+ compiled(record.exports, nodeLibRequire, record, nodeLibProcessOf(), internalBinding, primordialsOf());
295
+ record.loaded = true;
296
+ bootstrapNodeLib(name, record.exports);
297
+ }
298
+
299
+ /**
300
+ * What Node's own bootstrap does to a builtin once it is compiled.
301
+ *
302
+ * A few of Node's files are compiled in a state that cannot answer yet and
303
+ * are finished by `internal/bootstrap/node.js`. `internal/util/debuglog.js`
304
+ * says so in its own comment: "calls to `debuglog()` before
305
+ * `initializeDebugEnv()` is called will throw", and it threw -- the first
306
+ * `readable.push` in the engine logged through a `debuglog` with no
307
+ * `testEnabled`. The engine has no bootstrap file, so the step lives here,
308
+ * beside the compile it belongs to.
309
+ */
310
+ function bootstrapNodeLib(name: string, exports: unknown): void {
311
+ if (name === 'events') {
312
+ // Node's own `EventTarget` sets its listener ceiling on itself in its
313
+ // constructor, and `events.getMaxListeners` reads that field back. The
314
+ // realm's `EventTarget` -- the DOM's in a tab, Node's own where the
315
+ // engine runs inside a Node -- has no such field, and either way it is
316
+ // keyed by a symbol THIS `events.js` minted, not the one the realm's
317
+ // class was built against. So the default goes on the prototype under
318
+ // this file's own keys, where `getMaxListeners` looks, and a target given
319
+ // its own ceiling shadows it, which is what an own property does.
320
+ const events = exports as { kMaxEventTargetListeners?: symbol; kMaxEventTargetListenersWarned?: symbol };
321
+ const target = (globalThis as { EventTarget?: { prototype: object } }).EventTarget;
322
+ const defaults: Array<[symbol | undefined, unknown]> = [
323
+ [events.kMaxEventTargetListeners, 10],
324
+ [events.kMaxEventTargetListenersWarned, false],
325
+ ];
326
+ if (target) {
327
+ for (const [key, fallback] of defaults) {
328
+ if (!key || key in target.prototype) continue;
329
+ Object.defineProperty(target.prototype, key, {
330
+ configurable: true,
331
+ get(): unknown { return fallback; },
332
+ set(this: object, value: unknown) {
333
+ Object.defineProperty(this, key, { value, writable: true, configurable: true });
334
+ },
335
+ });
336
+ }
337
+ }
338
+ return;
339
+ }
340
+ if (name === 'internal/util/debuglog') {
341
+ const env = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env;
342
+ (exports as { initializeDebugEnv?: (value: string) => void }).initializeDebugEnv?.(env?.NODE_DEBUG ?? '');
343
+ return;
344
+ }
345
+ if (name === 'assert') {
346
+ // Node 23 hangs `Assert` off the module; the vendored 22.18 file does not.
347
+ const assert = exports as { Assert?: unknown; strictEqual?: (a: unknown, b: unknown) => void };
348
+ if (typeof assert.Assert !== 'function') {
349
+ class Assert {
350
+ diff: string;
351
+ constructor(options?: { diff?: string }) { this.diff = options?.diff ?? 'simple'; }
352
+ strictEqual(a: unknown, b: unknown): void { assert.strictEqual?.(a, b); }
353
+ }
354
+ (assert as { Assert: unknown }).Assert = Assert;
355
+ }
356
+ return;
357
+ }
358
+ if (name === 'fs') {
359
+ // Node 23 hangs `mkdtempDisposableSync` off `fs`; the vendored 22.18
360
+ // file does not, and `openAsBlob`'s neighbour in the gate calls it.
361
+ const fs = exports as {
362
+ mkdtempSync?: (prefix: string, options?: unknown) => string;
363
+ mkdtempDisposableSync?: unknown;
364
+ rmSync?: (path: string, options?: { recursive?: boolean; force?: boolean }) => void;
365
+ };
366
+ if (typeof fs.mkdtempDisposableSync !== 'function' && typeof fs.mkdtempSync === 'function') {
367
+ fs.mkdtempDisposableSync = (prefix: string, options?: unknown) => {
368
+ const tempPath = fs.mkdtempSync!(prefix, options);
369
+ const remove = (): void => { try { fs.rmSync?.(tempPath, { recursive: true, force: true }); } catch { /* gone */ } };
370
+ return { path: tempPath, remove, [Symbol.dispose]: remove };
371
+ };
372
+ }
373
+ }
374
+ }
375
+
376
+ /**
377
+ * What a hand-bound internal, a binding or `./lazy.ts` calls to reach a
378
+ * vendored file.
379
+ *
380
+ * A function DECLARATION, not a `const`: a name that stands for a vendored
381
+ * class is read while the bundle is still evaluating -- `class Foo extends
382
+ * EventEmitter` runs `getPrototypeOf` on the proxy at the moment the class is
383
+ * defined, which is module-evaluation time, before this file's own bindings
384
+ * are initialized. A hoisted function is already callable then; a `const` is
385
+ * in its temporal dead zone and the engine failed to load at all.
386
+ */
387
+ export function nodeLibInternalRequire(specifier: string): unknown {
388
+ return nodeLibRequire(specifier);
389
+ }
390
+ setLibRequire(nodeLibRequire);
@@ -0,0 +1,281 @@
1
+ /**
2
+ * A run's module-customization hooks: `module.registerHooks` and `module.register`.
3
+ *
4
+ * Node has two of these systems and so does this. The synchronous one is
5
+ * Node's own file, `internal/modules/customization_hooks.js`, vendored and
6
+ * instantiated once per run: it owns the chain, the context merging, the
7
+ * `shortCircuit` rule and every validation message, and the engine's loader
8
+ * calls it exactly where Node's `cjs/loader.js` calls it -- around the resolve
9
+ * of a specifier and around the read of a source. The asynchronous one,
10
+ * `module.register`, is the engine's, because Node's is a second thread: a
11
+ * hooks worker, a `SharedArrayBuffer` and an `Atomics.wait` on the main thread
12
+ * for every resolution. A tab has no thread to block and nothing to block it
13
+ * for, so the registered hooks run in this realm, on this loop, and are
14
+ * awaited rather than waited on. The chain they form is the one
15
+ * `internal/modules/esm/hooks.js` builds -- last registered runs first, each
16
+ * hook gets the `next` below it, the bottom is the default step, a hook that
17
+ * returns without calling next must say `shortCircuit: true` -- transcribed
18
+ * here because that file cannot be vendored without the thread it is about.
19
+ *
20
+ * WHAT THIS COSTS, named rather than hidden:
21
+ *
22
+ * - An asynchronous hook is consulted where the engine can await: a dynamic
23
+ * `import()`. The engine lowers a static `import` to a synchronous
24
+ * `require`, so a hook registered with `register()` is not consulted for
25
+ * one, where Node's is (its main thread blocks on the hooks thread). A
26
+ * program that needs its hook on every specifier registers it with
27
+ * `registerHooks`, which is synchronous and is consulted everywhere.
28
+ * - `data` reaches `initialize` by reference and `transferList` transfers
29
+ * nothing, because there is no second thread to clone or transfer to. A
30
+ * `MessagePort` in `data` is the same port object the caller holds; its
31
+ * messages still cross on the loop, which is what a port is for.
32
+ * - `initialize` is awaited before the first resolution the chain serves,
33
+ * which is where Node's synchronous `register()` has already blocked for it.
34
+ */
35
+ import { loadNodeLibInstance } from './load';
36
+
37
+ /** A resolve hook's answer, as `internal/modules/esm/hooks.js` validates one. */
38
+ export interface ResolveResult {
39
+ url: string;
40
+ format?: string;
41
+ importAttributes?: Record<string, string>;
42
+ shortCircuit?: boolean;
43
+ }
44
+
45
+ /** A load hook's answer. */
46
+ export interface LoadResult {
47
+ format?: string;
48
+ source?: string | ArrayBuffer | ArrayBufferView | null;
49
+ shortCircuit?: boolean;
50
+ }
51
+
52
+ type ResolveContext = { parentURL?: string; importAttributes?: Record<string, string>; conditions?: string[] };
53
+ type LoadContext = { format?: string; importAttributes?: Record<string, string>; conditions?: string[] };
54
+ type NextResolve = (specifier: string, context?: ResolveContext) => Promise<ResolveResult> | ResolveResult;
55
+ type NextLoad = (url: string, context?: LoadContext) => Promise<LoadResult> | LoadResult;
56
+ type AsyncResolveHook = (specifier: string, context: ResolveContext, next: NextResolve) => Promise<ResolveResult> | ResolveResult;
57
+ type AsyncLoadHook = (url: string, context: LoadContext, next: NextLoad) => Promise<LoadResult> | LoadResult;
58
+
59
+ /** What the vendored `internal/modules/customization_hooks.js` exports. */
60
+ interface CustomizationHooks {
61
+ resolveHooks: unknown[];
62
+ loadHooks: unknown[];
63
+ registerHooks(hooks: { resolve?: unknown; load?: unknown }): { deregister(): void };
64
+ resolveWithHooks(
65
+ specifier: string, parentURL: string | undefined, importAttributes: Record<string, string> | undefined,
66
+ conditions: string[], defaultResolve: (specifier: string, context: ResolveContext) => ResolveResult,
67
+ ): ResolveResult;
68
+ loadWithHooks(
69
+ url: string, format: string | undefined, importAttributes: Record<string, string> | undefined,
70
+ conditions: string[], defaultLoad: (url: string, context: LoadContext) => LoadResult,
71
+ ): LoadResult;
72
+ convertCJSFilenameToURL(filename: string): string;
73
+ convertURLToCJSFilename(url: string): string;
74
+ }
75
+
76
+ /** A link of an asynchronous chain, the shape `hooks.js` pushes. */
77
+ interface Link<Hook> { fn: Hook; url: string; next?: Link<Hook> }
78
+
79
+ /**
80
+ * A hook of either chain, seen by the walk that runs one. The walk hands a
81
+ * hook its context and the link below it and reads back an object; which
82
+ * context and which next belong to the resolve chain or the load chain is
83
+ * what the two typed hooks above state, and the walk is the same either way.
84
+ */
85
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
86
+ type ChainHook = (arg0: string, context: any, next: any) => unknown;
87
+
88
+ /** The conditions a `require` resolves under, as Node's `getCjsConditions()` reports them. */
89
+ const CJS_CONDITIONS = ['node', 'require', 'module-sync'];
90
+
91
+ /**
92
+ * Node's own `import()` conditions. The engine resolves an import the way it
93
+ * resolves a require, so this is what a hook is told, not a second resolver.
94
+ */
95
+ const ESM_CONDITIONS = ['node', 'import'];
96
+
97
+ export class RunModuleHooks {
98
+ /** The vendored file's own instance, this run's alone. */
99
+ readonly sync: CustomizationHooks = loadNodeLibInstance('internal/modules/customization_hooks') as CustomizationHooks;
100
+ #resolveChain: Link<AsyncResolveHook>[] = [];
101
+ #loadChain: Link<AsyncLoadHook>[] = [];
102
+ #initializing: Promise<unknown>[] = [];
103
+ #initializeFailure: unknown;
104
+
105
+ /**
106
+ * Which chain has anything in it. The loader asks before it builds
107
+ * anything: a run that registered no hook pays for none, which is the fast
108
+ * path Node keeps in `Module._load` and `loadSource` too.
109
+ */
110
+ get hasSyncResolve(): boolean { return this.sync.resolveHooks.length > 0; }
111
+ get hasSyncLoad(): boolean { return this.sync.loadHooks.length > 0; }
112
+ get hasAsyncResolve(): boolean { return this.#resolveChain.length > 0; }
113
+ get hasAsyncLoad(): boolean { return this.#loadChain.length > 0; }
114
+
115
+ /**
116
+ * `module.register(specifier[, parentURL][, options])`.
117
+ *
118
+ * The argument shuffle is Node's own (`internal/modules/esm/loader.js`): a
119
+ * second argument that is an object and not a URL is the options. The hook
120
+ * module is loaded through the engine's own loader, which is what answers a
121
+ * `data:` URL, a `file:` URL and a bare specifier alike.
122
+ */
123
+ register(
124
+ specifier: unknown,
125
+ parentURL: unknown,
126
+ options: { parentURL?: unknown; data?: unknown; transferList?: unknown } | undefined,
127
+ load: (specifier: string, from: string) => unknown,
128
+ ): void {
129
+ if (parentURL != null && typeof parentURL === 'object' && !(parentURL instanceof URL)) {
130
+ options = parentURL as { parentURL?: unknown; data?: unknown };
131
+ parentURL = (options as { parentURL?: unknown }).parentURL;
132
+ }
133
+ const from = parentURL === undefined || parentURL === null ? 'data:' : String(parentURL);
134
+ const url = String(specifier);
135
+ const exports = load(url, from) as Record<string, unknown> | undefined;
136
+ this.addLoader(url, exports, options?.data);
137
+ }
138
+
139
+ /**
140
+ * `Hooks.addCustomLoader`: the three names a hook module may export, each
141
+ * onto its chain, and `initialize` called with the data.
142
+ */
143
+ addLoader(url: string, exports: Record<string, unknown> | undefined, data: unknown): void {
144
+ const initialize = exports?.initialize as ((data: unknown) => unknown) | undefined;
145
+ const resolve = exports?.resolve as AsyncResolveHook | undefined;
146
+ const load = exports?.load as AsyncLoadHook | undefined;
147
+ if (typeof resolve === 'function') {
148
+ this.#resolveChain.push({ fn: resolve, url, next: this.#resolveChain[this.#resolveChain.length - 1] });
149
+ }
150
+ if (typeof load === 'function') {
151
+ this.#loadChain.push({ fn: load, url, next: this.#loadChain[this.#loadChain.length - 1] });
152
+ }
153
+ if (typeof initialize === 'function') {
154
+ const settling = initialize(data);
155
+ if (settling && typeof (settling as Promise<unknown>).then === 'function') {
156
+ // Node's `register()` has already blocked on the hooks thread by the
157
+ // time it returns, so an `initialize` that throws throws there. Here
158
+ // the first resolution the chain serves waits for it instead, and a
159
+ // rejection is kept rather than left to reach the process as an
160
+ // unhandled rejection nothing is waiting on: every resolution from
161
+ // then on fails with it, which is the state Node would have refused
162
+ // to register in.
163
+ this.#initializing.push((settling as Promise<unknown>).then(
164
+ () => void 0,
165
+ (error: unknown) => { this.#initializeFailure ??= error; },
166
+ ));
167
+ }
168
+ }
169
+ }
170
+
171
+ /** Every `initialize` still settling, awaited before the chain serves anything. */
172
+ async ready(): Promise<void> {
173
+ while (this.#initializing.length > 0) {
174
+ const settling = this.#initializing.slice();
175
+ this.#initializing.length = 0;
176
+ await Promise.all(settling);
177
+ }
178
+ if (this.#initializeFailure !== undefined) throw this.#initializeFailure;
179
+ }
180
+
181
+ /**
182
+ * The resolve chain of `module.register`, awaited. `defaultResolve` is the
183
+ * engine's own resolution, the bottom of the chain.
184
+ */
185
+ async resolve(
186
+ specifier: string,
187
+ parentURL: string | undefined,
188
+ importAttributes: Record<string, string> | undefined,
189
+ defaultResolve: (specifier: string, context: ResolveContext) => ResolveResult | Promise<ResolveResult>,
190
+ ): Promise<ResolveResult> {
191
+ await this.ready();
192
+ const context: ResolveContext = { conditions: ESM_CONDITIONS, importAttributes, parentURL };
193
+ const result = await this.#run(this.#resolveChain, 'resolve', context, specifier, defaultResolve as never);
194
+ if (typeof (result as ResolveResult).url !== 'string') {
195
+ throw invalidReturnProperty('a URL string', 'resolve', 'url', (result as ResolveResult).url);
196
+ }
197
+ return result as ResolveResult;
198
+ }
199
+
200
+ /** The load chain of `module.register`, awaited. */
201
+ async load(
202
+ url: string,
203
+ format: string | undefined,
204
+ importAttributes: Record<string, string> | undefined,
205
+ defaultLoad: (url: string, context: LoadContext) => LoadResult | Promise<LoadResult>,
206
+ ): Promise<LoadResult> {
207
+ await this.ready();
208
+ const context: LoadContext = { conditions: ESM_CONDITIONS, format, importAttributes };
209
+ return await this.#run(this.#loadChain, 'load', context, url, defaultLoad as never) as LoadResult;
210
+ }
211
+
212
+ /**
213
+ * `nextHookFactory`: the chain walked from the top, each hook handed the one
214
+ * below it, the bottom the default step. A hook that returns without calling
215
+ * the next one and without `shortCircuit: true` has broken the chain, and
216
+ * Node says so rather than quietly honouring it.
217
+ */
218
+ async #run<T extends object>(
219
+ chain: Link<ChainHook>[],
220
+ name: 'resolve' | 'load',
221
+ context: object,
222
+ arg0: string,
223
+ defaultStep: (arg0: string, context: object) => T | Promise<T>,
224
+ ): Promise<T> {
225
+ const meta = { chainFinished: false, shortCircuited: false, identifier: '' };
226
+ const nextName = name === 'resolve' ? 'nextResolve' : 'nextLoad';
227
+ const build = (current?: Link<ChainHook>): (arg0: string, ctx?: object) => Promise<T> => {
228
+ if (!current) {
229
+ return async (value: string, ctx?: object): Promise<T> => {
230
+ meta.chainFinished = true;
231
+ if (ctx && ctx !== context) Object.assign(context, ctx);
232
+ return await defaultStep(value, context);
233
+ };
234
+ }
235
+ const below = build(current.next);
236
+ return async (value: string, ctx?: object): Promise<T> => {
237
+ meta.identifier = `${current.url} '${name}'`;
238
+ if (typeof value !== 'string') {
239
+ throw invalidArgType(`${meta.identifier} hook's ${nextName}() specifier`, 'string', value);
240
+ }
241
+ if (ctx && ctx !== context) Object.assign(context, ctx);
242
+ const output = await current.fn(value, context, below) as T & { shortCircuit?: boolean };
243
+ if (typeof output !== 'object' || output === null) {
244
+ throw invalidReturnValue('an object', `${meta.identifier} hook's ${nextName}()`, output);
245
+ }
246
+ if (output.shortCircuit === true) meta.shortCircuited = true;
247
+ return output;
248
+ };
249
+ };
250
+ const top = build(chain[chain.length - 1]);
251
+ const output = await top(arg0, context);
252
+ if (!meta.chainFinished && !meta.shortCircuited) {
253
+ const error = new Error(
254
+ `The ${name} hook from ${meta.identifier} did not call the next hook in its chain and did not ` +
255
+ "explicitly signal a short circuit. If this is intentional, include `shortCircuit: true` in the hook's return.",
256
+ );
257
+ throw Object.assign(error, { code: 'ERR_LOADER_CHAIN_INCOMPLETE' });
258
+ }
259
+ return output;
260
+ }
261
+ }
262
+
263
+ function invalidReturnProperty(expected: string, name: string, property: string, value: unknown): Error {
264
+ const error = new TypeError(
265
+ `Expected ${expected} to be returned for the "${property}" from the "${name}" function but got ${String(value)}.`,
266
+ );
267
+ return Object.assign(error, { code: 'ERR_INVALID_RETURN_PROPERTY_VALUE' });
268
+ }
269
+
270
+ function invalidReturnValue(expected: string, name: string, value: unknown): Error {
271
+ const error = new TypeError(`Expected ${expected} to be returned from the "${name}" function but got ${String(value)}.`);
272
+ return Object.assign(error, { code: 'ERR_INVALID_RETURN_VALUE' });
273
+ }
274
+
275
+ function invalidArgType(name: string, expected: string, value: unknown): Error {
276
+ const error = new TypeError(`The "${name}" argument must be of type ${expected}. Received ${String(value)}`);
277
+ return Object.assign(error, { code: 'ERR_INVALID_ARG_TYPE' });
278
+ }
279
+
280
+ export { CJS_CONDITIONS, ESM_CONDITIONS };
281
+ export type { ResolveContext, LoadContext };