@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,1762 @@
1
+ // Copyright Joyent, Inc. and other Node contributors.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a
4
+ // copy of this software and associated documentation files (the
5
+ // "Software"), to deal in the Software without restriction, including
6
+ // without limitation the rights to use, copy, modify, merge, publish,
7
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ // persons to whom the Software is furnished to do so, subject to the
9
+ // following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be included
12
+ // in all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ 'use strict';
23
+
24
+ const {
25
+ ArrayPrototypeIndexOf,
26
+ NumberIsInteger,
27
+ NumberIsNaN,
28
+ NumberParseInt,
29
+ ObjectDefineProperties,
30
+ ObjectKeys,
31
+ ObjectSetPrototypeOf,
32
+ Promise,
33
+ SafeSet,
34
+ Symbol,
35
+ SymbolAsyncIterator,
36
+ SymbolSpecies,
37
+ TypedArrayPrototypeSet,
38
+ } = primordials;
39
+
40
+ module.exports = Readable;
41
+ Readable.ReadableState = ReadableState;
42
+
43
+ const EE = require('events');
44
+ const { Stream, prependListener } = require('internal/streams/legacy');
45
+ const { Buffer } = require('buffer');
46
+ const {
47
+ SymbolAsyncDispose,
48
+ } = require('internal/util');
49
+
50
+ const {
51
+ addAbortSignal,
52
+ } = require('internal/streams/add-abort-signal');
53
+ const eos = require('internal/streams/end-of-stream');
54
+
55
+ let debug = require('internal/util/debuglog').debuglog('stream', (fn) => {
56
+ debug = fn;
57
+ });
58
+ const destroyImpl = require('internal/streams/destroy');
59
+ const {
60
+ getHighWaterMark,
61
+ getDefaultHighWaterMark,
62
+ } = require('internal/streams/state');
63
+ const {
64
+ kState,
65
+ // bitfields
66
+ kObjectMode,
67
+ kErrorEmitted,
68
+ kAutoDestroy,
69
+ kEmitClose,
70
+ kDestroyed,
71
+ kClosed,
72
+ kCloseEmitted,
73
+ kErrored,
74
+ kConstructed,
75
+ kOnConstructed,
76
+ } = require('internal/streams/utils');
77
+
78
+ const {
79
+ AbortError,
80
+ aggregateTwoErrors,
81
+ codes: {
82
+ ERR_INVALID_ARG_TYPE,
83
+ ERR_METHOD_NOT_IMPLEMENTED,
84
+ ERR_OUT_OF_RANGE,
85
+ ERR_STREAM_PUSH_AFTER_EOF,
86
+ ERR_STREAM_UNSHIFT_AFTER_END_EVENT,
87
+ ERR_UNKNOWN_ENCODING,
88
+ },
89
+ } = require('internal/errors');
90
+ const { validateObject } = require('internal/validators');
91
+
92
+ const FastBuffer = Buffer[SymbolSpecies];
93
+
94
+ const { StringDecoder } = require('string_decoder');
95
+ const from = require('internal/streams/from');
96
+
97
+ ObjectSetPrototypeOf(Readable.prototype, Stream.prototype);
98
+ ObjectSetPrototypeOf(Readable, Stream);
99
+ const nop = () => {};
100
+
101
+ const { errorOrDestroy } = destroyImpl;
102
+
103
+ const kErroredValue = Symbol('kErroredValue');
104
+ const kDefaultEncodingValue = Symbol('kDefaultEncodingValue');
105
+ const kDecoderValue = Symbol('kDecoderValue');
106
+ const kEncodingValue = Symbol('kEncodingValue');
107
+
108
+ const kEnded = 1 << 9;
109
+ const kEndEmitted = 1 << 10;
110
+ const kReading = 1 << 11;
111
+ const kSync = 1 << 12;
112
+ const kNeedReadable = 1 << 13;
113
+ const kEmittedReadable = 1 << 14;
114
+ const kReadableListening = 1 << 15;
115
+ const kResumeScheduled = 1 << 16;
116
+ const kMultiAwaitDrain = 1 << 17;
117
+ const kReadingMore = 1 << 18;
118
+ const kDataEmitted = 1 << 19;
119
+ const kDefaultUTF8Encoding = 1 << 20;
120
+ const kDecoder = 1 << 21;
121
+ const kEncoding = 1 << 22;
122
+ const kHasFlowing = 1 << 23;
123
+ const kFlowing = 1 << 24;
124
+ const kHasPaused = 1 << 25;
125
+ const kPaused = 1 << 26;
126
+ const kDataListening = 1 << 27;
127
+
128
+ // TODO(benjamingr) it is likely slower to do it this way than with free functions
129
+ function makeBitMapDescriptor(bit) {
130
+ return {
131
+ enumerable: false,
132
+ get() { return (this[kState] & bit) !== 0; },
133
+ set(value) {
134
+ if (value) this[kState] |= bit;
135
+ else this[kState] &= ~bit;
136
+ },
137
+ };
138
+ }
139
+ ObjectDefineProperties(ReadableState.prototype, {
140
+ objectMode: makeBitMapDescriptor(kObjectMode),
141
+ ended: makeBitMapDescriptor(kEnded),
142
+ endEmitted: makeBitMapDescriptor(kEndEmitted),
143
+ reading: makeBitMapDescriptor(kReading),
144
+ // Stream is still being constructed and cannot be
145
+ // destroyed until construction finished or failed.
146
+ // Async construction is opt in, therefore we start as
147
+ // constructed.
148
+ constructed: makeBitMapDescriptor(kConstructed),
149
+ // A flag to be able to tell if the event 'readable'/'data' is emitted
150
+ // immediately, or on a later tick. We set this to true at first, because
151
+ // any actions that shouldn't happen until "later" should generally also
152
+ // not happen before the first read call.
153
+ sync: makeBitMapDescriptor(kSync),
154
+ // Whenever we return null, then we set a flag to say
155
+ // that we're awaiting a 'readable' event emission.
156
+ needReadable: makeBitMapDescriptor(kNeedReadable),
157
+ emittedReadable: makeBitMapDescriptor(kEmittedReadable),
158
+ readableListening: makeBitMapDescriptor(kReadableListening),
159
+ resumeScheduled: makeBitMapDescriptor(kResumeScheduled),
160
+ // True if the error was already emitted and should not be thrown again.
161
+ errorEmitted: makeBitMapDescriptor(kErrorEmitted),
162
+ emitClose: makeBitMapDescriptor(kEmitClose),
163
+ autoDestroy: makeBitMapDescriptor(kAutoDestroy),
164
+ // Has it been destroyed.
165
+ destroyed: makeBitMapDescriptor(kDestroyed),
166
+ // Indicates whether the stream has finished destroying.
167
+ closed: makeBitMapDescriptor(kClosed),
168
+ // True if close has been emitted or would have been emitted
169
+ // depending on emitClose.
170
+ closeEmitted: makeBitMapDescriptor(kCloseEmitted),
171
+ multiAwaitDrain: makeBitMapDescriptor(kMultiAwaitDrain),
172
+ // If true, a maybeReadMore has been scheduled.
173
+ readingMore: makeBitMapDescriptor(kReadingMore),
174
+ dataEmitted: makeBitMapDescriptor(kDataEmitted),
175
+
176
+ // Indicates whether the stream has errored. When true no further
177
+ // _read calls, 'data' or 'readable' events should occur. This is needed
178
+ // since when autoDestroy is disabled we need a way to tell whether the
179
+ // stream has failed.
180
+ errored: {
181
+ __proto__: null,
182
+ enumerable: false,
183
+ get() {
184
+ return (this[kState] & kErrored) !== 0 ? this[kErroredValue] : null;
185
+ },
186
+ set(value) {
187
+ if (value) {
188
+ this[kErroredValue] = value;
189
+ this[kState] |= kErrored;
190
+ } else {
191
+ this[kState] &= ~kErrored;
192
+ }
193
+ },
194
+ },
195
+
196
+ defaultEncoding: {
197
+ __proto__: null,
198
+ enumerable: false,
199
+ get() { return (this[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : this[kDefaultEncodingValue]; },
200
+ set(value) {
201
+ if (value === 'utf8' || value === 'utf-8') {
202
+ this[kState] |= kDefaultUTF8Encoding;
203
+ } else {
204
+ this[kState] &= ~kDefaultUTF8Encoding;
205
+ this[kDefaultEncodingValue] = value;
206
+ }
207
+ },
208
+ },
209
+
210
+ decoder: {
211
+ __proto__: null,
212
+ enumerable: false,
213
+ get() {
214
+ return (this[kState] & kDecoder) !== 0 ? this[kDecoderValue] : null;
215
+ },
216
+ set(value) {
217
+ if (value) {
218
+ this[kDecoderValue] = value;
219
+ this[kState] |= kDecoder;
220
+ } else {
221
+ this[kState] &= ~kDecoder;
222
+ }
223
+ },
224
+ },
225
+
226
+ encoding: {
227
+ __proto__: null,
228
+ enumerable: false,
229
+ get() {
230
+ return (this[kState] & kEncoding) !== 0 ? this[kEncodingValue] : null;
231
+ },
232
+ set(value) {
233
+ if (value) {
234
+ this[kEncodingValue] = value;
235
+ this[kState] |= kEncoding;
236
+ } else {
237
+ this[kState] &= ~kEncoding;
238
+ }
239
+ },
240
+ },
241
+
242
+ flowing: {
243
+ __proto__: null,
244
+ enumerable: false,
245
+ get() {
246
+ return (this[kState] & kHasFlowing) !== 0 ? (this[kState] & kFlowing) !== 0 : null;
247
+ },
248
+ set(value) {
249
+ if (value == null) {
250
+ this[kState] &= ~(kHasFlowing | kFlowing);
251
+ } else if (value) {
252
+ this[kState] |= (kHasFlowing | kFlowing);
253
+ } else {
254
+ this[kState] |= kHasFlowing;
255
+ this[kState] &= ~kFlowing;
256
+ }
257
+ },
258
+ },
259
+ });
260
+
261
+
262
+ function ReadableState(options, stream, isDuplex) {
263
+ // Bit map field to store ReadableState more efficiently with 1 bit per field
264
+ // instead of a V8 slot per field.
265
+ this[kState] = kEmitClose | kAutoDestroy | kConstructed | kSync;
266
+
267
+ // Object stream flag. Used to make read(n) ignore n and to
268
+ // make all the buffer merging and length checks go away.
269
+ if (options?.objectMode)
270
+ this[kState] |= kObjectMode;
271
+
272
+ if (isDuplex && options?.readableObjectMode)
273
+ this[kState] |= kObjectMode;
274
+
275
+ // The point at which it stops calling _read() to fill the buffer
276
+ // Note: 0 is a valid value, means "don't call _read preemptively ever"
277
+ this.highWaterMark = options ?
278
+ getHighWaterMark(this, options, 'readableHighWaterMark', isDuplex) :
279
+ getDefaultHighWaterMark(false);
280
+
281
+ this.buffer = [];
282
+ this.bufferIndex = 0;
283
+ this.length = 0;
284
+ this.pipes = [];
285
+
286
+ // Should close be emitted on destroy. Defaults to true.
287
+ if (options && options.emitClose === false) this[kState] &= ~kEmitClose;
288
+
289
+ // Should .destroy() be called after 'end' (and potentially 'finish').
290
+ if (options && options.autoDestroy === false) this[kState] &= ~kAutoDestroy;
291
+
292
+ // Crypto is kind of old and crusty. Historically, its default string
293
+ // encoding is 'binary' so we have to make this configurable.
294
+ // Everything else in the universe uses 'utf8', though.
295
+ const defaultEncoding = options?.defaultEncoding;
296
+ if (defaultEncoding == null || defaultEncoding === 'utf8' || defaultEncoding === 'utf-8') {
297
+ this[kState] |= kDefaultUTF8Encoding;
298
+ } else if (Buffer.isEncoding(defaultEncoding)) {
299
+ this.defaultEncoding = defaultEncoding;
300
+ } else {
301
+ throw new ERR_UNKNOWN_ENCODING(defaultEncoding);
302
+ }
303
+
304
+ // Ref the piped dest which we need a drain event on it
305
+ // type: null | Writable | Set<Writable>.
306
+ this.awaitDrainWriters = null;
307
+
308
+ if (options?.encoding) {
309
+ this.decoder = new StringDecoder(options.encoding);
310
+ this.encoding = options.encoding;
311
+ }
312
+ }
313
+
314
+ ReadableState.prototype[kOnConstructed] = function onConstructed(stream) {
315
+ if ((this[kState] & kNeedReadable) !== 0) {
316
+ maybeReadMore(stream, this);
317
+ }
318
+ };
319
+
320
+ function Readable(options) {
321
+ if (!(this instanceof Readable))
322
+ return new Readable(options);
323
+
324
+ this._events ??= {
325
+ close: undefined,
326
+ error: undefined,
327
+ data: undefined,
328
+ end: undefined,
329
+ readable: undefined,
330
+ // Skip uncommon events...
331
+ // pause: undefined,
332
+ // resume: undefined,
333
+ // pipe: undefined,
334
+ // unpipe: undefined,
335
+ // [destroyImpl.kConstruct]: undefined,
336
+ // [destroyImpl.kDestroy]: undefined,
337
+ };
338
+
339
+ this._readableState = new ReadableState(options, this, false);
340
+
341
+ if (options) {
342
+ if (typeof options.read === 'function')
343
+ this._read = options.read;
344
+
345
+ if (typeof options.destroy === 'function')
346
+ this._destroy = options.destroy;
347
+
348
+ if (typeof options.construct === 'function')
349
+ this._construct = options.construct;
350
+
351
+ if (options.signal)
352
+ addAbortSignal(options.signal, this);
353
+ }
354
+
355
+ Stream.call(this, options);
356
+
357
+ if (this._construct != null) {
358
+ destroyImpl.construct(this, () => {
359
+ this._readableState[kOnConstructed](this);
360
+ });
361
+ }
362
+ }
363
+
364
+ Readable.prototype.destroy = destroyImpl.destroy;
365
+ Readable.prototype._undestroy = destroyImpl.undestroy;
366
+ Readable.prototype._destroy = function(err, cb) {
367
+ cb(err);
368
+ };
369
+
370
+ Readable.prototype[EE.captureRejectionSymbol] = function(err) {
371
+ this.destroy(err);
372
+ };
373
+
374
+ Readable.prototype[SymbolAsyncDispose] = async function() {
375
+ let error;
376
+ if (!this.destroyed) {
377
+ error = this.readableEnded ? null : new AbortError();
378
+ this.destroy(error);
379
+ }
380
+ await new Promise((resolve, reject) => eos(this, (err) => (err && err !== error ? reject(err) : resolve(null))));
381
+ };
382
+
383
+ // Manually shove something into the read() buffer.
384
+ // This returns true if the highWaterMark has not been hit yet,
385
+ // similar to how Writable.write() returns true if you should
386
+ // write() some more.
387
+ Readable.prototype.push = function(chunk, encoding) {
388
+ debug('push', chunk);
389
+
390
+ const state = this._readableState;
391
+ return (state[kState] & kObjectMode) === 0 ?
392
+ readableAddChunkPushByteMode(this, state, chunk, encoding) :
393
+ readableAddChunkPushObjectMode(this, state, chunk, encoding);
394
+ };
395
+
396
+ // Unshift should *always* be something directly out of read().
397
+ Readable.prototype.unshift = function(chunk, encoding) {
398
+ debug('unshift', chunk);
399
+ const state = this._readableState;
400
+ return (state[kState] & kObjectMode) === 0 ?
401
+ readableAddChunkUnshiftByteMode(this, state, chunk, encoding) :
402
+ readableAddChunkUnshiftObjectMode(this, state, chunk);
403
+ };
404
+
405
+
406
+ function readableAddChunkUnshiftByteMode(stream, state, chunk, encoding) {
407
+ if (chunk === null) {
408
+ state[kState] &= ~kReading;
409
+ onEofChunk(stream, state);
410
+
411
+ return false;
412
+ }
413
+
414
+ if (typeof chunk === 'string') {
415
+ encoding ||= state.defaultEncoding;
416
+ if (state.encoding !== encoding) {
417
+ if (state.encoding) {
418
+ // When unshifting, if state.encoding is set, we have to save
419
+ // the string in the BufferList with the state encoding.
420
+ chunk = Buffer.from(chunk, encoding).toString(state.encoding);
421
+ } else {
422
+ chunk = Buffer.from(chunk, encoding);
423
+ }
424
+ }
425
+ } else if (Stream._isArrayBufferView(chunk)) {
426
+ chunk = Stream._uint8ArrayToBuffer(chunk);
427
+ } else if (chunk !== undefined && !(chunk instanceof Buffer)) {
428
+ errorOrDestroy(stream, new ERR_INVALID_ARG_TYPE(
429
+ 'chunk', ['string', 'Buffer', 'TypedArray', 'DataView'], chunk));
430
+ return false;
431
+ }
432
+
433
+
434
+ if (!(chunk && chunk.length > 0)) {
435
+ return canPushMore(state);
436
+ }
437
+
438
+ return readableAddChunkUnshiftValue(stream, state, chunk);
439
+ }
440
+
441
+ function readableAddChunkUnshiftObjectMode(stream, state, chunk) {
442
+ if (chunk === null) {
443
+ state[kState] &= ~kReading;
444
+ onEofChunk(stream, state);
445
+
446
+ return false;
447
+ }
448
+
449
+ return readableAddChunkUnshiftValue(stream, state, chunk);
450
+ }
451
+
452
+ function readableAddChunkUnshiftValue(stream, state, chunk) {
453
+ if ((state[kState] & kEndEmitted) !== 0)
454
+ errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
455
+ else if ((state[kState] & (kDestroyed | kErrored)) !== 0)
456
+ return false;
457
+ else
458
+ addChunk(stream, state, chunk, true);
459
+
460
+ return canPushMore(state);
461
+ }
462
+
463
+ function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
464
+ if (chunk === null) {
465
+ state[kState] &= ~kReading;
466
+ onEofChunk(stream, state);
467
+ return false;
468
+ }
469
+
470
+ if (typeof chunk === 'string') {
471
+ encoding ||= state.defaultEncoding;
472
+ if (state.encoding !== encoding) {
473
+ chunk = Buffer.from(chunk, encoding);
474
+ encoding = '';
475
+ }
476
+ } else if (chunk instanceof Buffer) {
477
+ encoding = '';
478
+ } else if (Stream._isArrayBufferView(chunk)) {
479
+ chunk = Stream._uint8ArrayToBuffer(chunk);
480
+ encoding = '';
481
+ } else if (chunk !== undefined) {
482
+ errorOrDestroy(stream, new ERR_INVALID_ARG_TYPE(
483
+ 'chunk', ['string', 'Buffer', 'TypedArray', 'DataView'], chunk));
484
+ return false;
485
+ }
486
+
487
+ if (!chunk || chunk.length <= 0) {
488
+ state[kState] &= ~kReading;
489
+ maybeReadMore(stream, state);
490
+
491
+ return canPushMore(state);
492
+ }
493
+
494
+ if ((state[kState] & kEnded) !== 0) {
495
+ errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
496
+ return false;
497
+ }
498
+
499
+ if ((state[kState] & (kDestroyed | kErrored)) !== 0) {
500
+ return false;
501
+ }
502
+
503
+ state[kState] &= ~kReading;
504
+ if ((state[kState] & kDecoder) !== 0 && !encoding) {
505
+ chunk = state[kDecoderValue].write(chunk);
506
+ if (chunk.length === 0) {
507
+ maybeReadMore(stream, state);
508
+ return canPushMore(state);
509
+ }
510
+ }
511
+
512
+ addChunk(stream, state, chunk, false);
513
+ return canPushMore(state);
514
+ }
515
+
516
+ function readableAddChunkPushObjectMode(stream, state, chunk, encoding) {
517
+ if (chunk === null) {
518
+ state[kState] &= ~kReading;
519
+ onEofChunk(stream, state);
520
+ return false;
521
+ }
522
+
523
+ if ((state[kState] & kEnded) !== 0) {
524
+ errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
525
+ return false;
526
+ }
527
+
528
+ if ((state[kState] & (kDestroyed | kErrored)) !== 0) {
529
+ return false;
530
+ }
531
+
532
+ state[kState] &= ~kReading;
533
+
534
+ if ((state[kState] & kDecoder) !== 0 && !encoding) {
535
+ chunk = state[kDecoderValue].write(chunk);
536
+ }
537
+
538
+ addChunk(stream, state, chunk, false);
539
+ return canPushMore(state);
540
+ }
541
+
542
+ function canPushMore(state) {
543
+ // We can push more data if we are below the highWaterMark.
544
+ // Also, if we have no data yet, we can stand some more bytes.
545
+ // This is to work around cases where hwm=0, such as the repl.
546
+ return (state[kState] & kEnded) === 0 &&
547
+ (state.length < state.highWaterMark || state.length === 0);
548
+ }
549
+
550
+ function addChunk(stream, state, chunk, addToFront) {
551
+ if ((state[kState] & (kFlowing | kSync | kDataListening)) === (kFlowing | kDataListening) && state.length === 0) {
552
+ // Use the guard to avoid creating `Set()` repeatedly
553
+ // when we have multiple pipes.
554
+ if ((state[kState] & kMultiAwaitDrain) !== 0) {
555
+ state.awaitDrainWriters.clear();
556
+ } else {
557
+ state.awaitDrainWriters = null;
558
+ }
559
+
560
+ state[kState] |= kDataEmitted;
561
+ stream.emit('data', chunk);
562
+ } else {
563
+ // Update the buffer info.
564
+ state.length += (state[kState] & kObjectMode) !== 0 ? 1 : chunk.length;
565
+ if (addToFront) {
566
+ if (state.bufferIndex > 0) {
567
+ state.buffer[--state.bufferIndex] = chunk;
568
+ } else {
569
+ state.buffer.unshift(chunk); // Slow path
570
+ }
571
+ } else {
572
+ state.buffer.push(chunk);
573
+ }
574
+
575
+ if ((state[kState] & kNeedReadable) !== 0)
576
+ emitReadable(stream);
577
+ }
578
+ maybeReadMore(stream, state);
579
+ }
580
+
581
+ Readable.prototype.isPaused = function() {
582
+ const state = this._readableState;
583
+ return (state[kState] & kPaused) !== 0 || (state[kState] & (kHasFlowing | kFlowing)) === kHasFlowing;
584
+ };
585
+
586
+ // Backwards compatibility.
587
+ Readable.prototype.setEncoding = function(enc) {
588
+ const state = this._readableState;
589
+
590
+ const decoder = new StringDecoder(enc);
591
+ state.decoder = decoder;
592
+ // If setEncoding(null), decoder.encoding equals utf8.
593
+ state.encoding = state.decoder.encoding;
594
+
595
+ // Iterate over current buffer to convert already stored Buffers:
596
+ let content = '';
597
+ for (const data of state.buffer.slice(state.bufferIndex)) {
598
+ content += decoder.write(data);
599
+ }
600
+ state.buffer.length = 0;
601
+ state.bufferIndex = 0;
602
+
603
+ if (content !== '')
604
+ state.buffer.push(content);
605
+ state.length = content.length;
606
+ return this;
607
+ };
608
+
609
+ // Don't raise the hwm > 1GB.
610
+ const MAX_HWM = 0x40000000;
611
+ function computeNewHighWaterMark(n) {
612
+ if (n > MAX_HWM) {
613
+ throw new ERR_OUT_OF_RANGE('size', '<= 1GiB', n);
614
+ } else {
615
+ // Get the next highest power of 2 to prevent increasing hwm excessively in
616
+ // tiny amounts.
617
+ n--;
618
+ n |= n >>> 1;
619
+ n |= n >>> 2;
620
+ n |= n >>> 4;
621
+ n |= n >>> 8;
622
+ n |= n >>> 16;
623
+ n++;
624
+ }
625
+ return n;
626
+ }
627
+
628
+ // This function is designed to be inlinable, so please take care when making
629
+ // changes to the function body.
630
+ function howMuchToRead(n, state) {
631
+ if (n <= 0 || (state.length === 0 && (state[kState] & kEnded) !== 0))
632
+ return 0;
633
+ if ((state[kState] & kObjectMode) !== 0)
634
+ return 1;
635
+ if (NumberIsNaN(n)) {
636
+ // Only flow one buffer at a time.
637
+ if ((state[kState] & kFlowing) !== 0 && state.length)
638
+ return state.buffer[state.bufferIndex].length;
639
+ return state.length;
640
+ }
641
+ if (n <= state.length)
642
+ return n;
643
+ return (state[kState] & kEnded) !== 0 ? state.length : 0;
644
+ }
645
+
646
+ // You can override either this method, or the async _read(n) below.
647
+ Readable.prototype.read = function(n) {
648
+ debug('read', n);
649
+ // Same as parseInt(undefined, 10), however V8 7.3 performance regressed
650
+ // in this scenario, so we are doing it manually.
651
+ if (n === undefined) {
652
+ n = NaN;
653
+ } else if (!NumberIsInteger(n)) {
654
+ n = NumberParseInt(n, 10);
655
+ }
656
+ const state = this._readableState;
657
+ const nOrig = n;
658
+
659
+ // If we're asking for more than the current hwm, then raise the hwm.
660
+ if (n > state.highWaterMark)
661
+ state.highWaterMark = computeNewHighWaterMark(n);
662
+
663
+ if (n !== 0)
664
+ state[kState] &= ~kEmittedReadable;
665
+
666
+ // If we're doing read(0) to trigger a readable event, but we
667
+ // already have a bunch of data in the buffer, then just trigger
668
+ // the 'readable' event and move on.
669
+ if (n === 0 &&
670
+ (state[kState] & kNeedReadable) !== 0 &&
671
+ ((state.highWaterMark !== 0 ?
672
+ state.length >= state.highWaterMark :
673
+ state.length > 0) ||
674
+ (state[kState] & kEnded) !== 0)) {
675
+ debug('read: emitReadable');
676
+ if (state.length === 0 && (state[kState] & kEnded) !== 0)
677
+ endReadable(this);
678
+ else
679
+ emitReadable(this);
680
+ return null;
681
+ }
682
+
683
+ n = howMuchToRead(n, state);
684
+
685
+ // If we've ended, and we're now clear, then finish it up.
686
+ if (n === 0 && (state[kState] & kEnded) !== 0) {
687
+ if (state.length === 0)
688
+ endReadable(this);
689
+ return null;
690
+ }
691
+
692
+ // All the actual chunk generation logic needs to be
693
+ // *below* the call to _read. The reason is that in certain
694
+ // synthetic stream cases, such as passthrough streams, _read
695
+ // may be a completely synchronous operation which may change
696
+ // the state of the read buffer, providing enough data when
697
+ // before there was *not* enough.
698
+ //
699
+ // So, the steps are:
700
+ // 1. Figure out what the state of things will be after we do
701
+ // a read from the buffer.
702
+ //
703
+ // 2. If that resulting state will trigger a _read, then call _read.
704
+ // Note that this may be asynchronous, or synchronous. Yes, it is
705
+ // deeply ugly to write APIs this way, but that still doesn't mean
706
+ // that the Readable class should behave improperly, as streams are
707
+ // designed to be sync/async agnostic.
708
+ // Take note if the _read call is sync or async (ie, if the read call
709
+ // has returned yet), so that we know whether or not it's safe to emit
710
+ // 'readable' etc.
711
+ //
712
+ // 3. Actually pull the requested chunks out of the buffer and return.
713
+
714
+ // if we need a readable event, then we need to do some reading.
715
+ let doRead = (state[kState] & kNeedReadable) !== 0;
716
+ debug('need readable', doRead);
717
+
718
+ // If we currently have less than the highWaterMark, then also read some.
719
+ if (state.length === 0 || state.length - n < state.highWaterMark) {
720
+ doRead = true;
721
+ debug('length less than watermark', doRead);
722
+ }
723
+
724
+ // However, if we've ended, then there's no point, if we're already
725
+ // reading, then it's unnecessary, if we're constructing we have to wait,
726
+ // and if we're destroyed or errored, then it's not allowed,
727
+ if ((state[kState] & (kReading | kEnded | kDestroyed | kErrored | kConstructed)) !== kConstructed) {
728
+ doRead = false;
729
+ debug('reading, ended or constructing', doRead);
730
+ } else if (doRead) {
731
+ debug('do read');
732
+ state[kState] |= kReading | kSync;
733
+ // If the length is currently zero, then we *need* a readable event.
734
+ if (state.length === 0)
735
+ state[kState] |= kNeedReadable;
736
+
737
+ // Call internal read method
738
+ try {
739
+ this._read(state.highWaterMark);
740
+ } catch (err) {
741
+ errorOrDestroy(this, err);
742
+ }
743
+ state[kState] &= ~kSync;
744
+
745
+ // If _read pushed data synchronously, then `reading` will be false,
746
+ // and we need to re-evaluate how much data we can return to the user.
747
+ if ((state[kState] & kReading) === 0)
748
+ n = howMuchToRead(nOrig, state);
749
+ }
750
+
751
+ let ret;
752
+ if (n > 0)
753
+ ret = fromList(n, state);
754
+ else
755
+ ret = null;
756
+
757
+ if (ret === null) {
758
+ state[kState] |= state.length <= state.highWaterMark ? kNeedReadable : 0;
759
+ n = 0;
760
+ } else {
761
+ state.length -= n;
762
+ if ((state[kState] & kMultiAwaitDrain) !== 0) {
763
+ state.awaitDrainWriters.clear();
764
+ } else {
765
+ state.awaitDrainWriters = null;
766
+ }
767
+ }
768
+
769
+ if (state.length === 0) {
770
+ // If we have nothing in the buffer, then we want to know
771
+ // as soon as we *do* get something into the buffer.
772
+ if ((state[kState] & kEnded) === 0)
773
+ state[kState] |= kNeedReadable;
774
+
775
+ // If we tried to read() past the EOF, then emit end on the next tick.
776
+ if (nOrig !== n && (state[kState] & kEnded) !== 0)
777
+ endReadable(this);
778
+ }
779
+
780
+ if (ret !== null && (state[kState] & (kErrorEmitted | kCloseEmitted)) === 0) {
781
+ state[kState] |= kDataEmitted;
782
+ this.emit('data', ret);
783
+ }
784
+
785
+ return ret;
786
+ };
787
+
788
+ function onEofChunk(stream, state) {
789
+ debug('onEofChunk');
790
+ if ((state[kState] & kEnded) !== 0) return;
791
+ const decoder = (state[kState] & kDecoder) !== 0 ? state[kDecoderValue] : null;
792
+ if (decoder) {
793
+ const chunk = decoder.end();
794
+ if (chunk?.length) {
795
+ state.buffer.push(chunk);
796
+ state.length += (state[kState] & kObjectMode) !== 0 ? 1 : chunk.length;
797
+ }
798
+ }
799
+ state[kState] |= kEnded;
800
+
801
+ if ((state[kState] & kSync) !== 0) {
802
+ // If we are sync, wait until next tick to emit the data.
803
+ // Otherwise we risk emitting data in the flow()
804
+ // the readable code triggers during a read() call.
805
+ emitReadable(stream);
806
+ } else {
807
+ // Emit 'readable' now to make sure it gets picked up.
808
+ state[kState] &= ~kNeedReadable;
809
+ state[kState] |= kEmittedReadable;
810
+ // We have to emit readable now that we are EOF. Modules
811
+ // in the ecosystem (e.g. dicer) rely on this event being sync.
812
+ emitReadable_(stream);
813
+ }
814
+ }
815
+
816
+ // Don't emit readable right away in sync mode, because this can trigger
817
+ // another read() call => stack overflow. This way, it might trigger
818
+ // a nextTick recursion warning, but that's not so bad.
819
+ function emitReadable(stream) {
820
+ const state = stream._readableState;
821
+ debug('emitReadable');
822
+ state[kState] &= ~kNeedReadable;
823
+ if ((state[kState] & kEmittedReadable) === 0) {
824
+ debug('emitReadable', (state[kState] & kFlowing) !== 0);
825
+ state[kState] |= kEmittedReadable;
826
+ process.nextTick(emitReadable_, stream);
827
+ }
828
+ }
829
+
830
+ function emitReadable_(stream) {
831
+ const state = stream._readableState;
832
+ debug('emitReadable_');
833
+ if ((state[kState] & (kDestroyed | kErrored)) === 0 && (state.length || (state[kState] & kEnded) !== 0)) {
834
+ stream.emit('readable');
835
+ state[kState] &= ~kEmittedReadable;
836
+ }
837
+
838
+ // The stream needs another readable event if:
839
+ // 1. It is not flowing, as the flow mechanism will take
840
+ // care of it.
841
+ // 2. It is not ended.
842
+ // 3. It is below the highWaterMark, so we can schedule
843
+ // another readable later.
844
+ state[kState] |=
845
+ (state[kState] & (kFlowing | kEnded)) === 0 &&
846
+ state.length <= state.highWaterMark ? kNeedReadable : 0;
847
+ flow(stream);
848
+ }
849
+
850
+
851
+ // At this point, the user has presumably seen the 'readable' event,
852
+ // and called read() to consume some data. that may have triggered
853
+ // in turn another _read(n) call, in which case reading = true if
854
+ // it's in progress.
855
+ // However, if we're not ended, or reading, and the length < hwm,
856
+ // then go ahead and try to read some more preemptively.
857
+ function maybeReadMore(stream, state) {
858
+ if ((state[kState] & (kReadingMore | kConstructed)) === kConstructed) {
859
+ state[kState] |= kReadingMore;
860
+ process.nextTick(maybeReadMore_, stream, state);
861
+ }
862
+ }
863
+
864
+ function maybeReadMore_(stream, state) {
865
+ // Attempt to read more data if we should.
866
+ //
867
+ // The conditions for reading more data are (one of):
868
+ // - Not enough data buffered (state.length < state.highWaterMark). The loop
869
+ // is responsible for filling the buffer with enough data if such data
870
+ // is available. If highWaterMark is 0 and we are not in the flowing mode
871
+ // we should _not_ attempt to buffer any extra data. We'll get more data
872
+ // when the stream consumer calls read() instead.
873
+ // - No data in the buffer, and the stream is in flowing mode. In this mode
874
+ // the loop below is responsible for ensuring read() is called. Failing to
875
+ // call read here would abort the flow and there's no other mechanism for
876
+ // continuing the flow if the stream consumer has just subscribed to the
877
+ // 'data' event.
878
+ //
879
+ // In addition to the above conditions to keep reading data, the following
880
+ // conditions prevent the data from being read:
881
+ // - The stream has ended (state.ended).
882
+ // - There is already a pending 'read' operation (state.reading). This is a
883
+ // case where the stream has called the implementation defined _read()
884
+ // method, but they are processing the call asynchronously and have _not_
885
+ // called push() with new data. In this case we skip performing more
886
+ // read()s. The execution ends in this method again after the _read() ends
887
+ // up calling push() with more data.
888
+ while ((state[kState] & (kReading | kEnded)) === 0 &&
889
+ (state.length < state.highWaterMark ||
890
+ ((state[kState] & kFlowing) !== 0 && state.length === 0))) {
891
+ const len = state.length;
892
+ debug('maybeReadMore read 0');
893
+ stream.read(0);
894
+ if (len === state.length)
895
+ // Didn't get any data, stop spinning.
896
+ break;
897
+ }
898
+ state[kState] &= ~kReadingMore;
899
+ }
900
+
901
+ // Abstract method. to be overridden in specific implementation classes.
902
+ // call cb(er, data) where data is <= n in length.
903
+ // for virtual (non-string, non-buffer) streams, "length" is somewhat
904
+ // arbitrary, and perhaps not very meaningful.
905
+ Readable.prototype._read = function(n) {
906
+ throw new ERR_METHOD_NOT_IMPLEMENTED('_read()');
907
+ };
908
+
909
+ Readable.prototype.pipe = function(dest, pipeOpts) {
910
+ const src = this;
911
+ const state = this._readableState;
912
+
913
+ if (state.pipes.length === 1) {
914
+ if ((state[kState] & kMultiAwaitDrain) === 0) {
915
+ state[kState] |= kMultiAwaitDrain;
916
+ state.awaitDrainWriters = new SafeSet(
917
+ state.awaitDrainWriters ? [state.awaitDrainWriters] : [],
918
+ );
919
+ }
920
+ }
921
+
922
+ state.pipes.push(dest);
923
+ debug('pipe count=%d opts=%j', state.pipes.length, pipeOpts);
924
+
925
+ const doEnd = (!pipeOpts || pipeOpts.end !== false) &&
926
+ dest !== process.stdout &&
927
+ dest !== process.stderr;
928
+
929
+ const endFn = doEnd ? onend : unpipe;
930
+ if ((state[kState] & kEndEmitted) !== 0)
931
+ process.nextTick(endFn);
932
+ else
933
+ src.once('end', endFn);
934
+
935
+ dest.on('unpipe', onunpipe);
936
+ function onunpipe(readable, unpipeInfo) {
937
+ debug('onunpipe');
938
+ if (readable === src) {
939
+ if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
940
+ unpipeInfo.hasUnpiped = true;
941
+ cleanup();
942
+ }
943
+ }
944
+ }
945
+
946
+ function onend() {
947
+ debug('onend');
948
+ dest.end();
949
+ }
950
+
951
+ let ondrain;
952
+
953
+ let cleanedUp = false;
954
+ function cleanup() {
955
+ debug('cleanup');
956
+ // Cleanup event handlers once the pipe is broken.
957
+ dest.removeListener('close', onclose);
958
+ dest.removeListener('finish', onfinish);
959
+ if (ondrain) {
960
+ dest.removeListener('drain', ondrain);
961
+ }
962
+ dest.removeListener('error', onerror);
963
+ dest.removeListener('unpipe', onunpipe);
964
+ src.removeListener('end', onend);
965
+ src.removeListener('end', unpipe);
966
+ src.removeListener('data', ondata);
967
+
968
+ cleanedUp = true;
969
+
970
+ // If the reader is waiting for a drain event from this
971
+ // specific writer, then it would cause it to never start
972
+ // flowing again.
973
+ // So, if this is awaiting a drain, then we just call it now.
974
+ // If we don't know, then assume that we are waiting for one.
975
+ if (ondrain && state.awaitDrainWriters &&
976
+ (!dest._writableState || dest._writableState.needDrain))
977
+ ondrain();
978
+ }
979
+
980
+ function pause() {
981
+ // If the user unpiped during `dest.write()`, it is possible
982
+ // to get stuck in a permanently paused state if that write
983
+ // also returned false.
984
+ // => Check whether `dest` is still a piping destination.
985
+ if (!cleanedUp) {
986
+ if (state.pipes.length === 1 && state.pipes[0] === dest) {
987
+ debug('false write response, pause', 0);
988
+ state.awaitDrainWriters = dest;
989
+ state[kState] &= ~kMultiAwaitDrain;
990
+ } else if (state.pipes.length > 1 && state.pipes.includes(dest)) {
991
+ debug('false write response, pause', state.awaitDrainWriters.size);
992
+ state.awaitDrainWriters.add(dest);
993
+ }
994
+ src.pause();
995
+ }
996
+ if (!ondrain) {
997
+ // When the dest drains, it reduces the awaitDrain counter
998
+ // on the source. This would be more elegant with a .once()
999
+ // handler in flow(), but adding and removing repeatedly is
1000
+ // too slow.
1001
+ ondrain = pipeOnDrain(src, dest);
1002
+ dest.on('drain', ondrain);
1003
+ }
1004
+ }
1005
+
1006
+ src.on('data', ondata);
1007
+ function ondata(chunk) {
1008
+ debug('ondata');
1009
+ const ret = dest.write(chunk);
1010
+ debug('dest.write', ret);
1011
+ if (ret === false) {
1012
+ pause();
1013
+ }
1014
+ }
1015
+
1016
+ // If the dest has an error, then stop piping into it.
1017
+ // However, don't suppress the throwing behavior for this.
1018
+ function onerror(er) {
1019
+ debug('onerror', er);
1020
+ unpipe();
1021
+ dest.removeListener('error', onerror);
1022
+ if (dest.listenerCount('error') === 0) {
1023
+ const s = dest._writableState || dest._readableState;
1024
+ if (s && !s.errorEmitted) {
1025
+ // User incorrectly emitted 'error' directly on the stream.
1026
+ errorOrDestroy(dest, er);
1027
+ } else {
1028
+ dest.emit('error', er);
1029
+ }
1030
+ }
1031
+ }
1032
+
1033
+ // Make sure our error handler is attached before userland ones.
1034
+ prependListener(dest, 'error', onerror);
1035
+
1036
+ // Both close and finish should trigger unpipe, but only once.
1037
+ function onclose() {
1038
+ dest.removeListener('finish', onfinish);
1039
+ unpipe();
1040
+ }
1041
+ dest.once('close', onclose);
1042
+ function onfinish() {
1043
+ debug('onfinish');
1044
+ dest.removeListener('close', onclose);
1045
+ unpipe();
1046
+ }
1047
+ dest.once('finish', onfinish);
1048
+
1049
+ function unpipe() {
1050
+ debug('unpipe');
1051
+ src.unpipe(dest);
1052
+ }
1053
+
1054
+ // Tell the dest that it's being piped to.
1055
+ dest.emit('pipe', src);
1056
+
1057
+ // Start the flow if it hasn't been started already.
1058
+
1059
+ if (dest.writableNeedDrain === true) {
1060
+ pause();
1061
+ } else if ((state[kState] & kFlowing) === 0) {
1062
+ debug('pipe resume');
1063
+ src.resume();
1064
+ }
1065
+
1066
+ return dest;
1067
+ };
1068
+
1069
+ function pipeOnDrain(src, dest) {
1070
+ return function pipeOnDrainFunctionResult() {
1071
+ const state = src._readableState;
1072
+
1073
+ // `ondrain` will call directly,
1074
+ // `this` maybe not a reference to dest,
1075
+ // so we use the real dest here.
1076
+ if (state.awaitDrainWriters === dest) {
1077
+ debug('pipeOnDrain', 1);
1078
+ state.awaitDrainWriters = null;
1079
+ } else if ((state[kState] & kMultiAwaitDrain) !== 0) {
1080
+ debug('pipeOnDrain', state.awaitDrainWriters.size);
1081
+ state.awaitDrainWriters.delete(dest);
1082
+ }
1083
+
1084
+ if ((!state.awaitDrainWriters || state.awaitDrainWriters.size === 0) &&
1085
+ (state[kState] & kDataListening) !== 0) {
1086
+ src.resume();
1087
+ }
1088
+ };
1089
+ }
1090
+
1091
+
1092
+ Readable.prototype.unpipe = function(dest) {
1093
+ const state = this._readableState;
1094
+ const unpipeInfo = { hasUnpiped: false };
1095
+
1096
+ // If we're not piping anywhere, then do nothing.
1097
+ if (state.pipes.length === 0)
1098
+ return this;
1099
+
1100
+ if (!dest) {
1101
+ // remove all.
1102
+ const dests = state.pipes;
1103
+ state.pipes = [];
1104
+ this.pause();
1105
+
1106
+ for (let i = 0; i < dests.length; i++)
1107
+ dests[i].emit('unpipe', this, { hasUnpiped: false });
1108
+ return this;
1109
+ }
1110
+
1111
+ // Try to find the right one.
1112
+ const index = ArrayPrototypeIndexOf(state.pipes, dest);
1113
+ if (index === -1)
1114
+ return this;
1115
+
1116
+ state.pipes.splice(index, 1);
1117
+ if (state.pipes.length === 0)
1118
+ this.pause();
1119
+
1120
+ dest.emit('unpipe', this, unpipeInfo);
1121
+
1122
+ return this;
1123
+ };
1124
+
1125
+ // Set up data events if they are asked for
1126
+ // Ensure readable listeners eventually get something.
1127
+ Readable.prototype.on = function(ev, fn) {
1128
+ const res = Stream.prototype.on.call(this, ev, fn);
1129
+ const state = this._readableState;
1130
+
1131
+ if (ev === 'data') {
1132
+ state[kState] |= kDataListening;
1133
+
1134
+ // Update readableListening so that resume() may be a no-op
1135
+ // a few lines down. This is needed to support once('readable').
1136
+ state[kState] |= this.listenerCount('readable') > 0 ? kReadableListening : 0;
1137
+
1138
+ // Try start flowing on next tick if stream isn't explicitly paused.
1139
+ if ((state[kState] & (kHasFlowing | kFlowing)) !== kHasFlowing) {
1140
+ this.resume();
1141
+ }
1142
+ } else if (ev === 'readable') {
1143
+ if ((state[kState] & (kEndEmitted | kReadableListening)) === 0) {
1144
+ state[kState] |= kReadableListening | kNeedReadable | kHasFlowing;
1145
+ state[kState] &= ~(kFlowing | kEmittedReadable);
1146
+ debug('on readable');
1147
+ if (state.length) {
1148
+ emitReadable(this);
1149
+ } else if ((state[kState] & kReading) === 0) {
1150
+ process.nextTick(nReadingNextTick, this);
1151
+ }
1152
+ }
1153
+ }
1154
+
1155
+ return res;
1156
+ };
1157
+ Readable.prototype.addListener = Readable.prototype.on;
1158
+
1159
+ Readable.prototype.removeListener = function(ev, fn) {
1160
+ const state = this._readableState;
1161
+
1162
+ const res = Stream.prototype.removeListener.call(this,
1163
+ ev, fn);
1164
+
1165
+ if (ev === 'readable') {
1166
+ // We need to check if there is someone still listening to
1167
+ // readable and reset the state. However this needs to happen
1168
+ // after readable has been emitted but before I/O (nextTick) to
1169
+ // support once('readable', fn) cycles. This means that calling
1170
+ // resume within the same tick will have no
1171
+ // effect.
1172
+ process.nextTick(updateReadableListening, this);
1173
+ } else if (ev === 'data' && this.listenerCount('data') === 0) {
1174
+ state[kState] &= ~kDataListening;
1175
+ }
1176
+
1177
+ return res;
1178
+ };
1179
+ Readable.prototype.off = Readable.prototype.removeListener;
1180
+
1181
+ Readable.prototype.removeAllListeners = function(ev) {
1182
+ const res = Stream.prototype.removeAllListeners.apply(this,
1183
+ arguments);
1184
+
1185
+ if (ev === 'readable' || ev === undefined) {
1186
+ // We need to check if there is someone still listening to
1187
+ // readable and reset the state. However this needs to happen
1188
+ // after readable has been emitted but before I/O (nextTick) to
1189
+ // support once('readable', fn) cycles. This means that calling
1190
+ // resume within the same tick will have no
1191
+ // effect.
1192
+ process.nextTick(updateReadableListening, this);
1193
+ }
1194
+
1195
+ return res;
1196
+ };
1197
+
1198
+ function updateReadableListening(self) {
1199
+ const state = self._readableState;
1200
+
1201
+ if (self.listenerCount('readable') > 0) {
1202
+ state[kState] |= kReadableListening;
1203
+ } else {
1204
+ state[kState] &= ~kReadableListening;
1205
+ }
1206
+
1207
+ if ((state[kState] & (kHasPaused | kPaused | kResumeScheduled)) === (kHasPaused | kResumeScheduled)) {
1208
+ // Flowing needs to be set to true now, otherwise
1209
+ // the upcoming resume will not flow.
1210
+ state[kState] |= kHasFlowing | kFlowing;
1211
+
1212
+ // Crude way to check if we should resume.
1213
+ } else if ((state[kState] & kDataListening) !== 0) {
1214
+ self.resume();
1215
+ } else if ((state[kState] & kReadableListening) === 0) {
1216
+ state[kState] &= ~(kHasFlowing | kFlowing);
1217
+ }
1218
+ }
1219
+
1220
+ function nReadingNextTick(self) {
1221
+ debug('readable nexttick read 0');
1222
+ self.read(0);
1223
+ }
1224
+
1225
+ // pause() and resume() are remnants of the legacy readable stream API
1226
+ // If the user uses them, then switch into old mode.
1227
+ Readable.prototype.resume = function() {
1228
+ const state = this._readableState;
1229
+ if ((state[kState] & kFlowing) === 0) {
1230
+ debug('resume');
1231
+ // We flow only if there is no one listening
1232
+ // for readable, but we still have to call
1233
+ // resume().
1234
+ state[kState] |= kHasFlowing;
1235
+ if ((state[kState] & kReadableListening) === 0) {
1236
+ state[kState] |= kFlowing;
1237
+ } else {
1238
+ state[kState] &= ~kFlowing;
1239
+ }
1240
+ resume(this, state);
1241
+ }
1242
+ state[kState] |= kHasPaused;
1243
+ state[kState] &= ~kPaused;
1244
+ return this;
1245
+ };
1246
+
1247
+ function resume(stream, state) {
1248
+ if ((state[kState] & kResumeScheduled) === 0) {
1249
+ state[kState] |= kResumeScheduled;
1250
+ process.nextTick(resume_, stream, state);
1251
+ }
1252
+ }
1253
+
1254
+ function resume_(stream, state) {
1255
+ debug('resume', (state[kState] & kReading) !== 0);
1256
+ if ((state[kState] & kReading) === 0) {
1257
+ stream.read(0);
1258
+ }
1259
+
1260
+ state[kState] &= ~kResumeScheduled;
1261
+ stream.emit('resume');
1262
+ flow(stream);
1263
+ if ((state[kState] & (kFlowing | kReading)) === kFlowing)
1264
+ stream.read(0);
1265
+ }
1266
+
1267
+ Readable.prototype.pause = function() {
1268
+ const state = this._readableState;
1269
+ debug('call pause');
1270
+ if ((state[kState] & (kHasFlowing | kFlowing)) !== kHasFlowing) {
1271
+ debug('pause');
1272
+ state[kState] |= kHasFlowing;
1273
+ state[kState] &= ~kFlowing;
1274
+ this.emit('pause');
1275
+ }
1276
+ state[kState] |= kHasPaused | kPaused;
1277
+ return this;
1278
+ };
1279
+
1280
+ function flow(stream) {
1281
+ const state = stream._readableState;
1282
+ debug('flow');
1283
+ while ((state[kState] & kFlowing) !== 0 && stream.read() !== null);
1284
+ }
1285
+
1286
+ // Wrap an old-style stream as the async data source.
1287
+ // This is *not* part of the readable stream interface.
1288
+ // It is an ugly unfortunate mess of history.
1289
+ Readable.prototype.wrap = function(stream) {
1290
+ let paused = false;
1291
+
1292
+ // TODO (ronag): Should this.destroy(err) emit
1293
+ // 'error' on the wrapped stream? Would require
1294
+ // a static factory method, e.g. Readable.wrap(stream).
1295
+
1296
+ stream.on('data', (chunk) => {
1297
+ if (!this.push(chunk) && stream.pause) {
1298
+ paused = true;
1299
+ stream.pause();
1300
+ }
1301
+ });
1302
+
1303
+ stream.on('end', () => {
1304
+ this.push(null);
1305
+ });
1306
+
1307
+ stream.on('error', (err) => {
1308
+ errorOrDestroy(this, err);
1309
+ });
1310
+
1311
+ stream.on('close', () => {
1312
+ this.destroy();
1313
+ });
1314
+
1315
+ stream.on('destroy', () => {
1316
+ this.destroy();
1317
+ });
1318
+
1319
+ this._read = () => {
1320
+ if (paused && stream.resume) {
1321
+ paused = false;
1322
+ stream.resume();
1323
+ }
1324
+ };
1325
+
1326
+ // Proxy all the other methods. Important when wrapping filters and duplexes.
1327
+ const streamKeys = ObjectKeys(stream);
1328
+ for (let j = 1; j < streamKeys.length; j++) {
1329
+ const i = streamKeys[j];
1330
+ if (this[i] === undefined && typeof stream[i] === 'function') {
1331
+ this[i] = stream[i].bind(stream);
1332
+ }
1333
+ }
1334
+
1335
+ return this;
1336
+ };
1337
+
1338
+ Readable.prototype[SymbolAsyncIterator] = function() {
1339
+ return streamToAsyncIterator(this);
1340
+ };
1341
+
1342
+ Readable.prototype.iterator = function(options) {
1343
+ if (options !== undefined) {
1344
+ validateObject(options, 'options');
1345
+ }
1346
+ return streamToAsyncIterator(this, options);
1347
+ };
1348
+
1349
+ function streamToAsyncIterator(stream, options) {
1350
+ if (typeof stream.read !== 'function') {
1351
+ stream = Readable.wrap(stream, { objectMode: true });
1352
+ }
1353
+
1354
+ const iter = createAsyncIterator(stream, options);
1355
+ iter.stream = stream;
1356
+ return iter;
1357
+ }
1358
+
1359
+ async function* createAsyncIterator(stream, options) {
1360
+ let callback = nop;
1361
+
1362
+ function next(resolve) {
1363
+ if (this === stream) {
1364
+ callback();
1365
+ callback = nop;
1366
+ } else {
1367
+ callback = resolve;
1368
+ }
1369
+ }
1370
+
1371
+ stream.on('readable', next);
1372
+
1373
+ let error;
1374
+ const cleanup = eos(stream, { writable: false }, (err) => {
1375
+ error = err ? aggregateTwoErrors(error, err) : null;
1376
+ callback();
1377
+ callback = nop;
1378
+ });
1379
+
1380
+ try {
1381
+ while (true) {
1382
+ const chunk = stream.destroyed ? null : stream.read();
1383
+ if (chunk !== null) {
1384
+ yield chunk;
1385
+ } else if (error) {
1386
+ throw error;
1387
+ } else if (error === null) {
1388
+ return;
1389
+ } else {
1390
+ await new Promise(next);
1391
+ }
1392
+ }
1393
+ } catch (err) {
1394
+ error = aggregateTwoErrors(error, err);
1395
+ throw error;
1396
+ } finally {
1397
+ if (
1398
+ (error || options?.destroyOnReturn !== false) &&
1399
+ (error === undefined || stream._readableState.autoDestroy)
1400
+ ) {
1401
+ destroyImpl.destroyer(stream, null);
1402
+ } else {
1403
+ stream.off('readable', next);
1404
+ cleanup();
1405
+ }
1406
+ }
1407
+ }
1408
+
1409
+ // Making it explicit these properties are not enumerable
1410
+ // because otherwise some prototype manipulation in
1411
+ // userland will fail.
1412
+ ObjectDefineProperties(Readable.prototype, {
1413
+ readable: {
1414
+ __proto__: null,
1415
+ get() {
1416
+ const r = this._readableState;
1417
+ // r.readable === false means that this is part of a Duplex stream
1418
+ // where the readable side was disabled upon construction.
1419
+ // Compat. The user might manually disable readable side through
1420
+ // deprecated setter.
1421
+ return !!r && r.readable !== false && !r.destroyed && !r.errorEmitted &&
1422
+ !r.endEmitted;
1423
+ },
1424
+ set(val) {
1425
+ // Backwards compat.
1426
+ if (this._readableState) {
1427
+ this._readableState.readable = !!val;
1428
+ }
1429
+ },
1430
+ },
1431
+
1432
+ readableDidRead: {
1433
+ __proto__: null,
1434
+ enumerable: false,
1435
+ get: function() {
1436
+ return this._readableState.dataEmitted;
1437
+ },
1438
+ },
1439
+
1440
+ readableAborted: {
1441
+ __proto__: null,
1442
+ enumerable: false,
1443
+ get: function() {
1444
+ return !!(
1445
+ this._readableState.readable !== false &&
1446
+ (this._readableState.destroyed || this._readableState.errored) &&
1447
+ !this._readableState.endEmitted
1448
+ );
1449
+ },
1450
+ },
1451
+
1452
+ readableHighWaterMark: {
1453
+ __proto__: null,
1454
+ enumerable: false,
1455
+ get: function() {
1456
+ return this._readableState.highWaterMark;
1457
+ },
1458
+ },
1459
+
1460
+ readableBuffer: {
1461
+ __proto__: null,
1462
+ enumerable: false,
1463
+ get: function() {
1464
+ return this._readableState?.buffer;
1465
+ },
1466
+ },
1467
+
1468
+ readableFlowing: {
1469
+ __proto__: null,
1470
+ enumerable: false,
1471
+ get: function() {
1472
+ return this._readableState.flowing;
1473
+ },
1474
+ set: function(state) {
1475
+ if (this._readableState) {
1476
+ this._readableState.flowing = state;
1477
+ }
1478
+ },
1479
+ },
1480
+
1481
+ readableLength: {
1482
+ __proto__: null,
1483
+ enumerable: false,
1484
+ get() {
1485
+ return this._readableState.length;
1486
+ },
1487
+ },
1488
+
1489
+ readableObjectMode: {
1490
+ __proto__: null,
1491
+ enumerable: false,
1492
+ get() {
1493
+ return this._readableState ? this._readableState.objectMode : false;
1494
+ },
1495
+ },
1496
+
1497
+ readableEncoding: {
1498
+ __proto__: null,
1499
+ enumerable: false,
1500
+ get() {
1501
+ return this._readableState ? this._readableState.encoding : null;
1502
+ },
1503
+ },
1504
+
1505
+ errored: {
1506
+ __proto__: null,
1507
+ enumerable: false,
1508
+ get() {
1509
+ return this._readableState ? this._readableState.errored : null;
1510
+ },
1511
+ },
1512
+
1513
+ closed: {
1514
+ __proto__: null,
1515
+ get() {
1516
+ return this._readableState ? this._readableState.closed : false;
1517
+ },
1518
+ },
1519
+
1520
+ destroyed: {
1521
+ __proto__: null,
1522
+ enumerable: false,
1523
+ get() {
1524
+ return this._readableState ? this._readableState.destroyed : false;
1525
+ },
1526
+ set(value) {
1527
+ // We ignore the value if the stream
1528
+ // has not been initialized yet.
1529
+ if (!this._readableState) {
1530
+ return;
1531
+ }
1532
+
1533
+ // Backward compatibility, the user is explicitly
1534
+ // managing destroyed.
1535
+ this._readableState.destroyed = value;
1536
+ },
1537
+ },
1538
+
1539
+ readableEnded: {
1540
+ __proto__: null,
1541
+ enumerable: false,
1542
+ get() {
1543
+ return this._readableState ? this._readableState.endEmitted : false;
1544
+ },
1545
+ },
1546
+
1547
+ });
1548
+
1549
+ ObjectDefineProperties(ReadableState.prototype, {
1550
+ // Legacy getter for `pipesCount`.
1551
+ pipesCount: {
1552
+ __proto__: null,
1553
+ get() {
1554
+ return this.pipes.length;
1555
+ },
1556
+ },
1557
+
1558
+ // Legacy property for `paused`.
1559
+ paused: {
1560
+ __proto__: null,
1561
+ get() {
1562
+ return (this[kState] & kPaused) !== 0;
1563
+ },
1564
+ set(value) {
1565
+ this[kState] |= kHasPaused;
1566
+ if (value) {
1567
+ this[kState] |= kPaused;
1568
+ } else {
1569
+ this[kState] &= ~kPaused;
1570
+ }
1571
+ },
1572
+ },
1573
+ });
1574
+
1575
+ // Exposed for testing purposes only.
1576
+ Readable._fromList = fromList;
1577
+
1578
+ // Pluck off n bytes from an array of buffers.
1579
+ // Length is the combined lengths of all the buffers in the list.
1580
+ // This function is designed to be inlinable, so please take care when making
1581
+ // changes to the function body.
1582
+ function fromList(n, state) {
1583
+ // nothing buffered.
1584
+ if (state.length === 0)
1585
+ return null;
1586
+
1587
+ let idx = state.bufferIndex;
1588
+ let ret;
1589
+
1590
+ const buf = state.buffer;
1591
+ const len = buf.length;
1592
+
1593
+ if ((state[kState] & kObjectMode) !== 0) {
1594
+ ret = buf[idx];
1595
+ buf[idx++] = null;
1596
+ } else if (!n || n >= state.length) {
1597
+ // Read it all, truncate the list.
1598
+ if ((state[kState] & kDecoder) !== 0) {
1599
+ ret = '';
1600
+ while (idx < len) {
1601
+ ret += buf[idx];
1602
+ buf[idx++] = null;
1603
+ }
1604
+ } else if (len - idx === 0) {
1605
+ ret = Buffer.alloc(0);
1606
+ } else if (len - idx === 1) {
1607
+ ret = buf[idx];
1608
+ buf[idx++] = null;
1609
+ } else {
1610
+ ret = Buffer.allocUnsafe(state.length);
1611
+
1612
+ let i = 0;
1613
+ while (idx < len) {
1614
+ TypedArrayPrototypeSet(ret, buf[idx], i);
1615
+ i += buf[idx].length;
1616
+ buf[idx++] = null;
1617
+ }
1618
+ }
1619
+ } else if (n < buf[idx].length) {
1620
+ // `slice` is the same for buffers and strings.
1621
+ ret = buf[idx].slice(0, n);
1622
+ buf[idx] = buf[idx].slice(n);
1623
+ } else if (n === buf[idx].length) {
1624
+ // First chunk is a perfect match.
1625
+ ret = buf[idx];
1626
+ buf[idx++] = null;
1627
+ } else if ((state[kState] & kDecoder) !== 0) {
1628
+ ret = '';
1629
+ while (idx < len) {
1630
+ const str = buf[idx];
1631
+ if (n > str.length) {
1632
+ ret += str;
1633
+ n -= str.length;
1634
+ buf[idx++] = null;
1635
+ } else {
1636
+ if (n === buf.length) {
1637
+ ret += str;
1638
+ buf[idx++] = null;
1639
+ } else {
1640
+ ret += str.slice(0, n);
1641
+ buf[idx] = str.slice(n);
1642
+ }
1643
+ break;
1644
+ }
1645
+ }
1646
+ } else {
1647
+ ret = Buffer.allocUnsafe(n);
1648
+
1649
+ const retLen = n;
1650
+ while (idx < len) {
1651
+ const data = buf[idx];
1652
+ if (n > data.length) {
1653
+ TypedArrayPrototypeSet(ret, data, retLen - n);
1654
+ n -= data.length;
1655
+ buf[idx++] = null;
1656
+ } else {
1657
+ if (n === data.length) {
1658
+ TypedArrayPrototypeSet(ret, data, retLen - n);
1659
+ buf[idx++] = null;
1660
+ } else {
1661
+ TypedArrayPrototypeSet(ret, new FastBuffer(data.buffer, data.byteOffset, n), retLen - n);
1662
+ buf[idx] = new FastBuffer(data.buffer, data.byteOffset + n, data.length - n);
1663
+ }
1664
+ break;
1665
+ }
1666
+ }
1667
+ }
1668
+
1669
+ if (idx === len) {
1670
+ state.buffer.length = 0;
1671
+ state.bufferIndex = 0;
1672
+ } else if (idx > 1024) {
1673
+ state.buffer.splice(0, idx);
1674
+ state.bufferIndex = 0;
1675
+ } else {
1676
+ state.bufferIndex = idx;
1677
+ }
1678
+
1679
+ return ret;
1680
+ }
1681
+
1682
+ function endReadable(stream) {
1683
+ const state = stream._readableState;
1684
+
1685
+ debug('endReadable');
1686
+ if ((state[kState] & kEndEmitted) === 0) {
1687
+ state[kState] |= kEnded;
1688
+ process.nextTick(endReadableNT, state, stream);
1689
+ }
1690
+ }
1691
+
1692
+ function endReadableNT(state, stream) {
1693
+ debug('endReadableNT');
1694
+
1695
+ // Check that we didn't get one last unshift.
1696
+ if ((state[kState] & (kErrored | kCloseEmitted | kEndEmitted)) === 0 && state.length === 0) {
1697
+ state[kState] |= kEndEmitted;
1698
+ stream.emit('end');
1699
+
1700
+ if (stream.writable && stream.allowHalfOpen === false) {
1701
+ process.nextTick(endWritableNT, stream);
1702
+ } else if (state.autoDestroy) {
1703
+ // In case of duplex streams we need a way to detect
1704
+ // if the writable side is ready for autoDestroy as well.
1705
+ const wState = stream._writableState;
1706
+ const autoDestroy = !wState || (
1707
+ wState.autoDestroy &&
1708
+ // We don't expect the writable to ever 'finish'
1709
+ // if writable is explicitly set to false.
1710
+ (wState.finished || wState.writable === false)
1711
+ );
1712
+
1713
+ if (autoDestroy) {
1714
+ stream.destroy();
1715
+ }
1716
+ }
1717
+ }
1718
+ }
1719
+
1720
+ function endWritableNT(stream) {
1721
+ const writable = stream.writable && !stream.writableEnded &&
1722
+ !stream.destroyed;
1723
+ if (writable) {
1724
+ stream.end();
1725
+ }
1726
+ }
1727
+
1728
+ Readable.from = function(iterable, opts) {
1729
+ return from(Readable, iterable, opts);
1730
+ };
1731
+
1732
+ let webStreamsAdapters;
1733
+
1734
+ // Lazy to avoid circular references
1735
+ function lazyWebStreams() {
1736
+ if (webStreamsAdapters === undefined)
1737
+ webStreamsAdapters = require('internal/webstreams/adapters');
1738
+ return webStreamsAdapters;
1739
+ }
1740
+
1741
+ Readable.fromWeb = function(readableStream, options) {
1742
+ return lazyWebStreams().newStreamReadableFromReadableStream(
1743
+ readableStream,
1744
+ options);
1745
+ };
1746
+
1747
+ Readable.toWeb = function(streamReadable, options) {
1748
+ return lazyWebStreams().newReadableStreamFromStreamReadable(
1749
+ streamReadable,
1750
+ options);
1751
+ };
1752
+
1753
+ Readable.wrap = function(src, options) {
1754
+ return new Readable({
1755
+ objectMode: src.readableObjectMode ?? src.objectMode ?? true,
1756
+ ...options,
1757
+ destroy(err, callback) {
1758
+ destroyImpl.destroyer(src, err);
1759
+ callback(err);
1760
+ },
1761
+ }).wrap(src);
1762
+ };