@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,413 @@
1
+ /**
2
+ * `internalBinding('stream_wrap')`: the libuv stream surface, and nothing above it.
3
+ *
4
+ * `internal/stream_base_commons.js` is Node's own and is vendored unmodified;
5
+ * it writes through `handle.writeBuffer(req, buf)` and its string cousins and
6
+ * reads through `handle.onread(arrayBuffer)` with the byte count in
7
+ * `streamBaseState[kReadBytesOrError]`. That is the whole contract, and this
8
+ * file implements exactly it over the engine's loopback pairing: two handles
9
+ * whose writes are each other's reads.
10
+ *
11
+ * Nothing here implements anything Node's files implement. There is no
12
+ * buffering policy, no half-open rule, no timeout: those are `net.js`'s, and
13
+ * `net.js` is vendored.
14
+ */
15
+ import { bytesOfString } from './buffer';
16
+ import { UV_EOF, UV_EBADF, UV_ECONNRESET } from './uv';
17
+
18
+ /**
19
+ * The bytes this view names. Node's `Buffer.from` of a short string is a
20
+ * window on an 8 KB pool (`Buffer.poolSize`); taking `view.buffer` without
21
+ * `byteOffset` / `byteLength` carries the neighbours that sit beside it.
22
+ */
23
+ function ownedBytes(view: Uint8Array): Uint8Array {
24
+ const copy = new Uint8Array(view.byteLength);
25
+ copy.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength));
26
+ return copy;
27
+ }
28
+ import {
29
+ registerHandle, refHandle, unrefHandle, handleHasRef, releaseHandle, stopHandle,
30
+ type OwnedHandle,
31
+ } from './handles';
32
+
33
+ /** The four slots libuv's `StreamBase` reports a read or a write through. */
34
+ export const kReadBytesOrError = 0;
35
+ export const kArrayBufferOffset = 1;
36
+ export const kBytesWritten = 2;
37
+ export const kLastWriteWasAsync = 3;
38
+ export const streamBaseState = new Int32Array(4);
39
+
40
+ /** Node's `WriteWrap`: the request `writeGeneric` fills and hands to a write. */
41
+ export class WriteWrap {
42
+ handle: LibuvStreamWrap | null = null;
43
+ oncomplete: ((status: number) => void) | null = null;
44
+ async = false;
45
+ bytes = 0;
46
+ buffer: unknown = null;
47
+ callback: unknown = null;
48
+ error: unknown = undefined;
49
+ _chunks: unknown = null;
50
+ }
51
+
52
+ /** Node's `ShutdownWrap`: the request a `_final` hands to `shutdown`. */
53
+ export class ShutdownWrap {
54
+ handle: LibuvStreamWrap | null = null;
55
+ oncomplete: ((status: number) => void) | null = null;
56
+ callback: (() => void) | null = null;
57
+ }
58
+
59
+ let nextAsyncId = 1;
60
+
61
+ /**
62
+ * The bytes one direction of a pairing is carrying, oldest first, and the
63
+ * handle a write of them carried. A descriptor crosses an IPC channel beside
64
+ * the bytes of the message it belongs to and is read out of the receiver just
65
+ * before those bytes reach it, which is the order `setupChannel` in Node's
66
+ * `internal/child_process.js` reads them in.
67
+ */
68
+ interface Inbound {
69
+ bytes: Uint8Array;
70
+ handle?: unknown;
71
+ }
72
+
73
+ /**
74
+ * The base every `TCP` and `Pipe` extends, as `LibuvStreamWrap` is the base of
75
+ * libuv's `uv_stream_t` wrappers.
76
+ *
77
+ * A pairing is two of these with `peer` pointing at each other. A write copies
78
+ * its bytes and hands them to the peer on a microtask, because libuv never
79
+ * delivers a read inside the write that caused it; the peer delivers them to
80
+ * `onread` while it is reading and holds them while it is not, which is what
81
+ * `readStart` and `readStop` mean.
82
+ */
83
+ export class LibuvStreamWrap implements OwnedHandle {
84
+ /** Set by `net.js` to `onStreamRead`; the only way bytes reach a stream. */
85
+ onread: ((arrayBuffer: ArrayBuffer | null) => unknown) | null = null;
86
+ /** `net.js` owns this flag; `readStart`/`readStop` follow it. */
87
+ reading = false;
88
+ bytesRead = 0;
89
+ bytesWritten = 0;
90
+ /** Nothing is ever queued: a write is taken whole by the peer. */
91
+ writeQueueSize = 0;
92
+ /** The engine has no descriptors for a paired handle, as libuv reports -1 for one it has no fd for. */
93
+ fd = -1;
94
+
95
+ /** The other end of the pairing, once there is one. */
96
+ peer: LibuvStreamWrap | null = null;
97
+ /** True once `close()` has been called; a closed handle carries nothing. */
98
+ closed = false;
99
+ /**
100
+ * The handle the read now being delivered carried, which `setupChannel`
101
+ * takes off the channel at the top of its `onread`. libuv keeps the same
102
+ * thing in a pipe's pending-handle queue.
103
+ */
104
+ pendingHandle: unknown = null;
105
+
106
+ protected inbound: Inbound[] = [];
107
+ protected inboundEof = false;
108
+ protected inboundError: number | null = null;
109
+ protected eofDelivered = false;
110
+ protected userBuffer: Uint8Array | null = null;
111
+ protected readonly asyncId = nextAsyncId++;
112
+
113
+ constructor() {
114
+ registerHandle(this);
115
+ }
116
+
117
+ getAsyncId(): number {
118
+ return this.asyncId;
119
+ }
120
+
121
+ ref(): void {
122
+ refHandle(this);
123
+ }
124
+
125
+ unref(): void {
126
+ unrefHandle(this);
127
+ }
128
+
129
+ hasRef(): boolean {
130
+ return handleHasRef(this);
131
+ }
132
+
133
+ readStart(): number {
134
+ if (this.closed) return UV_EBADF;
135
+ this.reading = true;
136
+ this.flushInbound();
137
+ return 0;
138
+ }
139
+
140
+ readStop(): number {
141
+ this.reading = false;
142
+ return 0;
143
+ }
144
+
145
+ /** Node's `onread` option: reads land in the program's own buffer. */
146
+ useUserBuffer(buffer: Uint8Array): void {
147
+ this.userBuffer = buffer;
148
+ }
149
+
150
+ /**
151
+ * libuv's `uv_shutdown`: this end says it will write no more, and the peer
152
+ * reads EOF. The request completes on its own turn, as libuv's does.
153
+ */
154
+ shutdown(req: ShutdownWrap): number {
155
+ this.sendEof();
156
+ queueMicrotask(() => { req.oncomplete?.(0); });
157
+ return 0;
158
+ }
159
+
160
+ writeBuffer(req: WriteWrap, buffer: Uint8Array): number {
161
+ return this.dispatchWrite(req, buffer);
162
+ }
163
+
164
+ writev(req: WriteWrap, chunks: unknown[], allBuffers: boolean): number {
165
+ const parts: Uint8Array[] = [];
166
+ if (allBuffers) {
167
+ for (const chunk of chunks) parts.push(chunk as Uint8Array);
168
+ } else {
169
+ for (let index = 0; index < chunks.length; index += 2) {
170
+ const chunk = chunks[index];
171
+ const encoding = chunks[index + 1] as string;
172
+ parts.push(typeof chunk === 'string' ? bytesOfString(chunk, encoding) : (chunk as Uint8Array));
173
+ }
174
+ }
175
+ let total = 0;
176
+ for (const part of parts) total += part.byteLength;
177
+ const joined = new Uint8Array(total);
178
+ let at = 0;
179
+ for (const part of parts) {
180
+ joined.set(new Uint8Array(part.buffer, part.byteOffset, part.byteLength), at);
181
+ at += part.byteLength;
182
+ }
183
+ return this.dispatchWrite(req, joined);
184
+ }
185
+
186
+ writeUtf8String(req: WriteWrap, text: string, handle?: unknown): number {
187
+ return this.dispatchWrite(req, bytesOfString(text, 'utf8'), handle);
188
+ }
189
+
190
+ writeAsciiString(req: WriteWrap, text: string): number {
191
+ return this.dispatchWrite(req, bytesOfString(text, 'ascii'));
192
+ }
193
+
194
+ writeLatin1String(req: WriteWrap, text: string): number {
195
+ return this.dispatchWrite(req, bytesOfString(text, 'latin1'));
196
+ }
197
+
198
+ writeUcs2String(req: WriteWrap, text: string): number {
199
+ return this.dispatchWrite(req, bytesOfString(text, 'ucs2'));
200
+ }
201
+
202
+ /**
203
+ * libuv's `uv_close`. The peer reads EOF, as it does when the other end of a
204
+ * socket goes; the callback runs on its own turn, because `net.js` emits
205
+ * `close` from it and Node emits that after the turn that destroyed.
206
+ */
207
+ close(callback?: () => void): void {
208
+ if (!this.closed) {
209
+ this.closed = true;
210
+ releaseHandle(this);
211
+ const peer = this.peer;
212
+ this.peer = null;
213
+ // Writes land in the peer's inbound queue at once; flushing here
214
+ // delivers those bytes and then EOF, in that order, on this turn. A
215
+ // child's setupChannel then disconnects (`connected` false) before the
216
+ // next `process.send`, so that send is ERR_IPC_CHANNEL_CLOSED rather
217
+ // than a write on a handle a later release would close (`write EBADF`).
218
+ if (peer) { peer.peer = null; peer.receiveEof(); peer.flushInbound(); }
219
+ this.onCloseHandle();
220
+ }
221
+ if (callback) queueMicrotask(callback);
222
+ }
223
+
224
+ /** What a `TCP` or a `Pipe` gives up beyond the stream: a port, a path. */
225
+ protected onCloseHandle(): void {}
226
+
227
+ /**
228
+ * libuv's `dup()`: a second handle on the same connection.
229
+ *
230
+ * A descriptor sent over IPC arrives in the receiver as a NEW descriptor
231
+ * referring to the same socket, which is what `SCM_RIGHTS` gives it, and
232
+ * Node's own `child_process.js` counts on exactly that: on the receiver's
233
+ * `NODE_HANDLE_ACK` the sender CLOSES the handle it sent. With one handle
234
+ * for both, that close ended the connection the child had just been given
235
+ * and its first write was `EBADF`.
236
+ *
237
+ * The duplicate takes over this end of the pairing and this end is left
238
+ * peerless, so closing it announces nothing -- the sender has already given
239
+ * the socket up (`handleConversion`'s `send` sets `socket._handle = null`
240
+ * and silences its reads before the write).
241
+ */
242
+ duplicate(): LibuvStreamWrap {
243
+ const copy = new LibuvStreamWrap();
244
+ copy.takeOverFrom(this);
245
+ return copy;
246
+ }
247
+
248
+ /** Two handles become each other's peer; what one writes the other reads. */
249
+ static pair(a: LibuvStreamWrap, b: LibuvStreamWrap): void {
250
+ a.peer = b;
251
+ b.peer = a;
252
+ }
253
+
254
+ /**
255
+ * Take over another handle's end of a pairing, with everything that arrived
256
+ * on it while nobody was holding it. This is what opening a descriptor is
257
+ * here: a forked child's channel is opened by the child after its parent has
258
+ * already written to it -- `fork(…)` and `send(…)` are one synchronous turn
259
+ * in every program that does it -- and the bytes waiting at that descriptor
260
+ * are the child's. A pairing whose bytes stayed behind left the child
261
+ * waiting for a message its parent had already sent.
262
+ */
263
+ takeOverFrom(other: LibuvStreamWrap): void {
264
+ const peer = other.peer;
265
+ other.peer = null;
266
+ if (peer) LibuvStreamWrap.pair(this, peer);
267
+ if (other.inbound.length > 0) {
268
+ this.inbound = [...this.inbound, ...other.inbound];
269
+ other.inbound = [];
270
+ }
271
+ if (other.inboundEof) this.inboundEof = true;
272
+ if (other.inboundError !== null && this.inboundError === null) this.inboundError = other.inboundError;
273
+ this.flushInbound();
274
+ }
275
+
276
+ /**
277
+ * A write: the bytes are copied (the caller may reuse its buffer) and put in
278
+ * the peer's queue at once, where anything the writer does afterwards lands
279
+ * behind them. Delivery is still the peer's own turn, because libuv never
280
+ * delivers a read inside the write that caused it. It completes
281
+ * synchronously — `kBytesWritten` set, `kLastWriteWasAsync` 0 — because the
282
+ * engine's writes are taken whole and there is no queue for a `drain` to
283
+ * empty.
284
+ *
285
+ * Handing the bytes over on a microtask instead put them BEHIND an end
286
+ * announced from the same turn: `stdin.write('hello'); stdin.end()` is one
287
+ * synchronous block in every program that does it, the peer read EOF first
288
+ * and the bytes were dropped after it, and `spawn('cat')` fed that way
289
+ * produced nothing.
290
+ */
291
+ protected dispatchWrite(req: WriteWrap, buffer: Uint8Array, handle?: unknown): number {
292
+ void req;
293
+ const length = buffer.byteLength;
294
+ streamBaseState[kBytesWritten] = length;
295
+ streamBaseState[kLastWriteWasAsync] = 0;
296
+ if (this.closed) return UV_EBADF;
297
+ this.bytesWritten += length;
298
+ const peer = this.peer;
299
+ if (!peer || peer.closed) return 0;
300
+ if (length === 0) return 0;
301
+ peer.receive(ownedBytes(buffer), handle);
302
+ return 0;
303
+ }
304
+
305
+ /**
306
+ * The peer will write no more; this end reads EOF once it has read the rest.
307
+ * Announced on a microtask of its own, so it lands behind the writes that
308
+ * were handed over before it: `stdin.write('hello'); stdin.end()` is one
309
+ * turn in every program, and an EOF announced from inside `end` reached the
310
+ * reader first and the bytes never reached it at all.
311
+ */
312
+ protected sendEof(): void {
313
+ const peer = this.peer;
314
+ if (peer) peer.receiveEof();
315
+ }
316
+
317
+ /**
318
+ * libuv's close-with-reset: the peer's next read is `ECONNRESET`, not EOF,
319
+ * which is how `socket.resetAndDestroy()` differs from `socket.destroy()`.
320
+ */
321
+ protected sendReset(): void {
322
+ const peer = this.peer;
323
+ if (peer) peer.receiveError(UV_ECONNRESET);
324
+ }
325
+
326
+ /** An error arriving on the read side, delivered as libuv delivers one. */
327
+ receiveError(code: number): void {
328
+ if (this.closed) return;
329
+ this.inboundError = code;
330
+ this.scheduleFlush();
331
+ }
332
+
333
+ /** Bytes arriving from the peer, and the handle their write carried. */
334
+ receive(bytes: Uint8Array, handle?: unknown): void {
335
+ if (this.closed) return;
336
+ this.inbound.push(handle === undefined || handle === null ? { bytes } : { bytes, handle });
337
+ this.scheduleFlush();
338
+ }
339
+
340
+ /** The peer is done writing. */
341
+ receiveEof(): void {
342
+ if (this.closed) return;
343
+ this.inboundEof = true;
344
+ this.scheduleFlush();
345
+ }
346
+
347
+ /**
348
+ * What arrived is recorded the moment it arrives and delivered on a turn of
349
+ * its own, once, however many arrivals that turn holds. This is the order
350
+ * libuv's loop reads in: everything a writer did in one turn is read in the
351
+ * order it did it, and never from inside the call that did it.
352
+ */
353
+ private flushScheduled = false;
354
+ protected scheduleFlush(): void {
355
+ if (this.flushScheduled) return;
356
+ this.flushScheduled = true;
357
+ queueMicrotask(() => { this.flushScheduled = false; this.flushInbound(); });
358
+ }
359
+
360
+ /**
361
+ * Hand what has arrived to `onStreamRead`, in libuv's shape: the byte count
362
+ * in `streamBaseState[kReadBytesOrError]`, the offset beside it, the
363
+ * `ArrayBuffer` as the argument. A reader that pushes back calls `readStop`
364
+ * from inside `onread`, so the loop re-reads `reading` every time.
365
+ */
366
+ protected flushInbound(): void {
367
+ while (this.reading && !this.closed && this.inbound.length > 0) {
368
+ const next = this.inbound.shift() as Inbound;
369
+ const bytes = next.bytes;
370
+ // The handle is on the channel before the bytes it belongs to reach the
371
+ // reader, which is where `setupChannel` takes it off.
372
+ if (next.handle !== undefined) this.pendingHandle = next.handle;
373
+ this.bytesRead += bytes.byteLength;
374
+ const target = this.userBuffer;
375
+ if (target) {
376
+ // `net.Socket({ onread })`: the bytes land in the program's buffer and
377
+ // `onStreamRead` hands the count to its callback.
378
+ const taken = Math.min(target.byteLength, bytes.byteLength);
379
+ target.set(bytes.subarray(0, taken));
380
+ if (taken < bytes.byteLength) this.inbound.unshift({ bytes: bytes.subarray(taken) });
381
+ streamBaseState[kReadBytesOrError] = taken;
382
+ streamBaseState[kArrayBufferOffset] = 0;
383
+ this.onread?.(null);
384
+ continue;
385
+ }
386
+ streamBaseState[kReadBytesOrError] = bytes.byteLength;
387
+ streamBaseState[kArrayBufferOffset] = bytes.byteOffset;
388
+ this.onread?.(bytes.buffer as ArrayBuffer);
389
+ }
390
+ if (this.reading && !this.closed && this.inbound.length === 0 && !this.eofDelivered &&
391
+ (this.inboundError !== null || this.inboundEof)) {
392
+ this.eofDelivered = true;
393
+ // libuv stops a stream when it reads EOF, and a stopped handle does not
394
+ // hold the loop: a socket whose peer has gone is not work, however much
395
+ // unread data is still in its buffer.
396
+ stopHandle(this);
397
+ streamBaseState[kReadBytesOrError] = this.inboundError ?? UV_EOF;
398
+ streamBaseState[kArrayBufferOffset] = 0;
399
+ this.onread?.(null);
400
+ }
401
+ }
402
+ }
403
+
404
+ export default {
405
+ LibuvStreamWrap,
406
+ WriteWrap,
407
+ ShutdownWrap,
408
+ streamBaseState,
409
+ kReadBytesOrError,
410
+ kArrayBufferOffset,
411
+ kBytesWritten,
412
+ kLastWriteWasAsync,
413
+ };
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The two bindings `internal/child_process.js` names only to ask an
3
+ * `instanceof` of.
4
+ *
5
+ * `udp_wrap`'s `UDP` tells a datagram handle from a stream one when a handle
6
+ * is sent over IPC, and `http_parser`'s `HTTPParser` tells a parser handle
7
+ * from a socket when one is released. The engine has no datagram handle and
8
+ * parses HTTP in JavaScript, so nothing is ever an instance of either; the
9
+ * classes exist so the question can be asked, and answered no.
10
+ */
11
+
12
+ /** libuv's `uv_udp_t`, which this engine never makes one of. */
13
+ export class UDP {}
14
+
15
+ /** The HTTP parser handle a C++ Node keeps on a socket; this one has none. */
16
+ export class HTTPParser {}
17
+
18
+ export const udpWrapBinding = { UDP };
19
+ export const httpParserBinding = { HTTPParser };
@@ -0,0 +1,254 @@
1
+ /**
2
+ * `internalBinding('tcp_wrap')`: the engine's loopback, in libuv's shape.
3
+ *
4
+ * One engine is one host. A `bind` reserves a port, a `listen` starts
5
+ * answering on it, and a `connect` to a loopback address on a port something
6
+ * is listening on pairs two handles whose writes are each other's reads —
7
+ * which is what the engine has always done for a `net` connect, moved here so
8
+ * that Node's own `net.js` runs on it. A connect to anything that is not this
9
+ * host is `ECONNREFUSED`, because there is nothing else to reach: a page's
10
+ * request to the outside goes through `fetch`, not through a socket.
11
+ *
12
+ * A wildcard bind holds the port for both families, as a dual-stack `::` bind
13
+ * does on Linux, so `connect('127.0.0.1')` reaches a server that bound `::`.
14
+ */
15
+ import { LibuvStreamWrap } from './stream_wrap';
16
+ import { __adoptHandle, ownerOf } from './handles';
17
+ import { UV_EADDRINUSE, UV_ECONNREFUSED, UV_EINVAL, UV_ENOTSUP } from './uv';
18
+
19
+ /** libuv's `uv_tcp_t` flavours, and the two flags `getFlags` builds. */
20
+ export const constants = {
21
+ SOCKET: 0,
22
+ SERVER: 1,
23
+ UV_TCP_IPV6ONLY: 1,
24
+ UV_TCP_REUSEPORT: 4,
25
+ };
26
+
27
+ /** What `getsockname`/`getpeername` fill in. */
28
+ export interface SockName {
29
+ address: string;
30
+ family: string;
31
+ port: number;
32
+ }
33
+
34
+ /** Node's `TCPConnectWrap`: the request a connect fills and is answered through. */
35
+ export class TCPConnectWrap {
36
+ oncomplete: ((status: number, handle: unknown, req: unknown, readable: boolean, writable: boolean) => void) | null = null;
37
+ address = '';
38
+ port = 0;
39
+ localAddress: string | undefined = undefined;
40
+ localPort: number | undefined = undefined;
41
+ addressType: number | undefined = undefined;
42
+ }
43
+
44
+ /**
45
+ * Every port this engine holds: a handle that has bound one, listening or
46
+ * not, and every ephemeral port a client end was given. One space, because
47
+ * one engine is one host and a port is taken once.
48
+ */
49
+ const boundPorts = new Map<number, TCP>();
50
+ const ephemeralPorts = new Set<number>();
51
+
52
+ /** Linux's ephemeral range, walked in order as a kernel walks it. */
53
+ let nextEphemeral = 49152;
54
+ function allocatePort(): number {
55
+ for (let tries = 0; tries < 16384; tries += 1) {
56
+ const port = nextEphemeral;
57
+ nextEphemeral = nextEphemeral >= 65535 ? 49152 : nextEphemeral + 1;
58
+ if (!boundPorts.has(port) && !ephemeralPorts.has(port)) return port;
59
+ }
60
+ return 0;
61
+ }
62
+
63
+ /** Loopback and the wildcards: what this engine answers for. */
64
+ function isThisHost(address: string): boolean {
65
+ if (!address) return true;
66
+ const bare = address.startsWith('[') && address.endsWith(']') ? address.slice(1, -1) : address;
67
+ const name = bare.split('%')[0].toLowerCase();
68
+ return name === 'localhost' || name === '0.0.0.0' || name === '::' || name === '::1' ||
69
+ name === '0:0:0:0:0:0:0:1' || name === '0:0:0:0:0:0:0:0' || name.startsWith('127.');
70
+ }
71
+
72
+ /**
73
+ * What the page bridge is told when a guest starts or stops listening. The
74
+ * bridge used to learn this from the engine's own `http.Server`; `http` is
75
+ * Node's own file now and knows nothing of a page, so the news comes from
76
+ * where a port is actually taken.
77
+ */
78
+ type ListenWatcher = (port: number, address: string) => void;
79
+ // eslint-disable-next-line no-var, vars-on-top
80
+ var onListenWatcher: ListenWatcher | null = null;
81
+ // eslint-disable-next-line no-var, vars-on-top
82
+ var onCloseWatcher: ((port: number) => void) | null = null;
83
+ export function setPortWatchers(onListen: ListenWatcher | null, onClose: ((port: number) => void) | null): void {
84
+ onListenWatcher = onListen;
85
+ onCloseWatcher = onClose;
86
+ }
87
+
88
+ /** The listening handle a connect to this port reaches, if any. */
89
+ export function listenerOnPort(port: number): TCP | undefined {
90
+ const bound = boundPorts.get(port);
91
+ return bound && bound.listening ? bound : undefined;
92
+ }
93
+
94
+ export class TCP extends LibuvStreamWrap {
95
+ /** `constants.SOCKET` or `constants.SERVER`. */
96
+ readonly type: number;
97
+ listening = false;
98
+ /** Set by `net.js` on a server handle; how an accepted pairing is announced. */
99
+ onconnection: ((status: number, clientHandle: TCP) => void) | null = null;
100
+
101
+ private local: SockName | null = null;
102
+ private remote: SockName | null = null;
103
+ private ephemeral: number | null = null;
104
+
105
+ constructor(type: number = constants.SOCKET) {
106
+ super();
107
+ this.type = type;
108
+ }
109
+
110
+ bind(address: string, port: number, _flags?: number): number {
111
+ return this.bindTo(address || '0.0.0.0', port, 'IPv4');
112
+ }
113
+
114
+ bind6(address: string, port: number, _flags?: number): number {
115
+ return this.bindTo(address || '::', port, 'IPv6');
116
+ }
117
+
118
+ private bindTo(address: string, port: number, family: string): number {
119
+ if (!isThisHost(address)) return UV_EADDRINUSE;
120
+ const wanted = Number(port) || 0;
121
+ if (wanted !== 0 && boundPorts.has(wanted)) return UV_EADDRINUSE;
122
+ if (wanted !== 0 && ephemeralPorts.has(wanted)) return UV_EADDRINUSE;
123
+ const bound = wanted === 0 ? allocatePort() : wanted;
124
+ if (this.local && boundPorts.get(this.local.port) === this) boundPorts.delete(this.local.port);
125
+ this.local = { address, family, port: bound };
126
+ boundPorts.set(bound, this);
127
+ return 0;
128
+ }
129
+
130
+ listen(_backlog: number): number {
131
+ if (!this.local) {
132
+ const err = this.bindTo('0.0.0.0', 0, 'IPv4');
133
+ if (err) return err;
134
+ }
135
+ this.listening = true;
136
+ if (this.local) { try { onListenWatcher?.(this.local.port, this.local.address); } catch { /* a watcher that throws is not the listen's business */ } }
137
+ return 0;
138
+ }
139
+
140
+ connect(req: TCPConnectWrap, address: string, port: number): number {
141
+ return this.connectTo(req, address, port, 'IPv4');
142
+ }
143
+
144
+ connect6(req: TCPConnectWrap, address: string, port: number): number {
145
+ return this.connectTo(req, address, port, 'IPv6');
146
+ }
147
+
148
+ private connectTo(req: TCPConnectWrap, address: string, port: number, family: string): number {
149
+ const target = Number(port);
150
+ const reachable = isThisHost(address);
151
+ if (!this.local) {
152
+ const mine = allocatePort();
153
+ ephemeralPorts.add(mine);
154
+ this.ephemeral = mine;
155
+ this.local = { address: family === 'IPv6' ? '::1' : '127.0.0.1', family, port: mine };
156
+ }
157
+ this.remote = { address, family, port: target };
158
+ // libuv answers a connect through the request, never from the call: a
159
+ // refused connection is `oncomplete(UV_ECONNREFUSED)`, which is what
160
+ // `afterConnect` turns into the socket's `ECONNREFUSED` error.
161
+ queueMicrotask(() => {
162
+ if (this.closed) return;
163
+ const server = reachable ? listenerOnPort(target) : undefined;
164
+ if (!server) {
165
+ req.oncomplete?.(UV_ECONNREFUSED, this, req, true, true);
166
+ return;
167
+ }
168
+ const accepted = new TCP(constants.SOCKET);
169
+ accepted.local = { ...(server.local as SockName), port: target };
170
+ accepted.remote = { ...(this.local as SockName) };
171
+ // An accepted connection is a handle of the loop that is serving, not of
172
+ // the one that connected: the pairing is made inside the connector's own
173
+ // turn, so without this the server's end of every connection would hold
174
+ // the connecting run alive until the server closed it.
175
+ __adoptHandle(accepted, ownerOf(server));
176
+ LibuvStreamWrap.pair(this, accepted);
177
+ server.onconnection?.(0, accepted);
178
+ req.oncomplete?.(0, this, req, true, true);
179
+ });
180
+ return 0;
181
+ }
182
+
183
+ /**
184
+ * Name both ends of a pairing the engine made without a connect. The page
185
+ * bridge's end of an upgraded connection is one: there was no `connect`, and
186
+ * the socket an `upgrade` listener is handed still has to answer
187
+ * `remoteAddress` and `localPort` as a connection's socket does.
188
+ */
189
+ adoptNames(local: SockName, remote: SockName): void {
190
+ this.local = { ...local };
191
+ this.remote = { ...remote };
192
+ }
193
+
194
+ /** libuv's `dup()`: the same connection, under both its names. */
195
+ override duplicate(): TCP {
196
+ const copy = new TCP(constants.SOCKET);
197
+ if (this.local && this.remote) copy.adoptNames(this.local, this.remote);
198
+ copy.takeOverFrom(this);
199
+ return copy;
200
+ }
201
+
202
+ getsockname(out: Partial<SockName>): number {
203
+ if (!this.local) return UV_EINVAL;
204
+ out.address = this.local.address;
205
+ out.family = this.local.family;
206
+ out.port = this.local.port;
207
+ return 0;
208
+ }
209
+
210
+ getpeername(out: Partial<SockName>): number {
211
+ if (!this.remote) return UV_EINVAL;
212
+ out.address = this.remote.address;
213
+ out.family = this.remote.family;
214
+ out.port = this.remote.port;
215
+ return 0;
216
+ }
217
+
218
+ /** Nothing is buffered between two handles in one realm, so there is nothing to delay. */
219
+ setNoDelay(_enable: boolean): number {
220
+ return 0;
221
+ }
222
+
223
+ /** There is no idle connection to probe: a pairing is alive while both ends are. */
224
+ setKeepAlive(_enable: boolean, _delay: number): number {
225
+ return 0;
226
+ }
227
+
228
+ setSimultaneousAccepts(_enable: boolean): number {
229
+ return 0;
230
+ }
231
+
232
+ /** An engine handle carries no descriptor a program can hand it. */
233
+ open(_fd: number): number {
234
+ return UV_ENOTSUP;
235
+ }
236
+
237
+ /** libuv's `uv_tcp_close_reset`: the peer reads ECONNRESET rather than EOF. */
238
+ reset(callback?: () => void): number {
239
+ this.sendReset();
240
+ this.close(callback);
241
+ return 0;
242
+ }
243
+
244
+ protected override onCloseHandle(): void {
245
+ const wasListening = this.listening;
246
+ this.listening = false;
247
+ this.onconnection = null;
248
+ if (wasListening && this.local) { try { onCloseWatcher?.(this.local.port); } catch { /* as above */ } }
249
+ if (this.local && boundPorts.get(this.local.port) === this) boundPorts.delete(this.local.port);
250
+ if (this.ephemeral !== null) { ephemeralPorts.delete(this.ephemeral); this.ephemeral = null; }
251
+ }
252
+ }
253
+
254
+ export default { TCP, TCPConnectWrap, constants };