cf-workers-native 0.0.0-stage → 0.1.2-beta.local.sha2f5e4e6956f4

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 (629) hide show
  1. package/LICENSE +21 -0
  2. package/Package.swift +25 -0
  3. package/README.md +92 -2
  4. package/dist/android.d.ts +2 -0
  5. package/dist/android.js +1 -0
  6. package/dist/browser/abort/index.d.ts +13 -0
  7. package/dist/browser/abort/index.js +66 -0
  8. package/dist/browser/body/index.d.ts +24 -0
  9. package/dist/browser/body/index.js +116 -0
  10. package/dist/browser/body/stream-body.d.ts +13 -0
  11. package/dist/browser/body/stream-body.js +84 -0
  12. package/dist/browser/documents/body.d.ts +3 -0
  13. package/dist/browser/documents/body.js +48 -0
  14. package/dist/browser/documents/form-body.d.ts +1 -0
  15. package/dist/browser/documents/form-body.js +54 -0
  16. package/dist/browser/documents/host.d.ts +11 -0
  17. package/dist/browser/documents/host.js +256 -0
  18. package/dist/browser/documents/links-source.d.ts +1 -0
  19. package/dist/browser/documents/links-source.js +1 -0
  20. package/dist/browser/documents/links.d.ts +1 -0
  21. package/dist/browser/documents/links.js +33 -0
  22. package/dist/browser/documents/page-config.d.ts +5 -0
  23. package/dist/browser/documents/page-config.js +5 -0
  24. package/dist/browser/documents/page-source.d.ts +1 -0
  25. package/dist/browser/documents/page-source.js +1 -0
  26. package/dist/browser/documents/page.d.ts +2 -0
  27. package/dist/browser/documents/page.js +153 -0
  28. package/dist/browser/documents/registration.d.ts +5 -0
  29. package/dist/browser/documents/registration.js +75 -0
  30. package/dist/browser/documents/runtime.d.ts +7 -0
  31. package/dist/browser/documents/runtime.js +161 -0
  32. package/dist/browser/documents/service-worker.js +285 -0
  33. package/dist/browser/encoding/index.d.ts +7 -0
  34. package/dist/browser/encoding/index.js +3 -0
  35. package/dist/browser/fetch/LICENSE-3RD-PARTY.md +9 -0
  36. package/dist/browser/fetch/LICENSE-APACHE +177 -0
  37. package/dist/browser/fetch/host-call.d.ts +3 -0
  38. package/dist/browser/fetch/host-call.js +67 -0
  39. package/dist/browser/fetch/index.d.ts +5 -0
  40. package/dist/browser/fetch/index.js +96 -0
  41. package/dist/browser/fetch/interop.d.ts +6 -0
  42. package/dist/browser/fetch/interop.js +48 -0
  43. package/dist/browser/fetch/native-network.d.ts +20 -0
  44. package/dist/browser/fetch/native-network.js +233 -0
  45. package/dist/browser/fetch/outbound.d.ts +2 -0
  46. package/dist/browser/fetch/outbound.js +11 -0
  47. package/dist/browser/fetch/relay.d.ts +7 -0
  48. package/dist/browser/fetch/relay.js +100 -0
  49. package/dist/browser/fetch/scope.d.ts +8 -0
  50. package/dist/browser/fetch/scope.js +108 -0
  51. package/dist/browser/fetch/transport.d.ts +39 -0
  52. package/dist/browser/fetch/transport.js +136 -0
  53. package/dist/browser/headers/LICENSE-3RD-PARTY.md +9 -0
  54. package/dist/browser/headers/LICENSE-APACHE +177 -0
  55. package/dist/browser/headers/index.d.ts +20 -0
  56. package/dist/browser/headers/index.js +194 -0
  57. package/dist/browser/isolated/bridge.d.ts +8 -0
  58. package/dist/browser/isolated/bridge.js +267 -0
  59. package/dist/browser/isolated/protocol.d.ts +40 -0
  60. package/dist/browser/isolated/protocol.js +1 -0
  61. package/dist/browser/isolated/transport.d.ts +2 -0
  62. package/dist/browser/isolated/transport.js +157 -0
  63. package/dist/browser/request/LICENSE-3RD-PARTY.md +10 -0
  64. package/dist/browser/request/LICENSE-APACHE +177 -0
  65. package/dist/browser/request/index.d.ts +48 -0
  66. package/dist/browser/request/index.js +182 -0
  67. package/dist/browser/response/LICENSE-3RD-PARTY.md +10 -0
  68. package/dist/browser/response/LICENSE-APACHE +177 -0
  69. package/dist/browser/response/index.d.ts +43 -0
  70. package/dist/browser/response/index.js +192 -0
  71. package/dist/browser/response/status.d.ts +1 -0
  72. package/dist/browser/response/status.js +75 -0
  73. package/dist/browser/structured-clone/index.d.ts +1 -0
  74. package/dist/browser/structured-clone/index.js +479 -0
  75. package/dist/cloudflare/apis/cloudflare-node/index.d.ts +18 -0
  76. package/dist/cloudflare/apis/cloudflare-node/index.js +150 -0
  77. package/dist/cloudflare/apis/cloudflare-workers/index.d.ts +8 -0
  78. package/dist/cloudflare/apis/cloudflare-workers/index.js +37 -0
  79. package/dist/cloudflare/apis/types.d.ts +7 -0
  80. package/dist/cloudflare/apis/types.js +1 -0
  81. package/dist/cloudflare/bindings/config.d.ts +42 -0
  82. package/dist/cloudflare/bindings/config.js +280 -0
  83. package/dist/cloudflare/bindings/kv/contract.d.ts +6 -0
  84. package/dist/cloudflare/bindings/kv/contract.js +181 -0
  85. package/dist/cloudflare/bindings/kv/index.d.ts +18 -0
  86. package/dist/cloudflare/bindings/kv/index.js +61 -0
  87. package/dist/cloudflare/bindings/kv/storage.d.ts +25 -0
  88. package/dist/cloudflare/bindings/kv/storage.js +320 -0
  89. package/dist/cloudflare/bindings/kv/types.d.ts +50 -0
  90. package/dist/cloudflare/bindings/kv/types.js +1 -0
  91. package/dist/cloudflare/bindings/remote.d.ts +8 -0
  92. package/dist/cloudflare/bindings/remote.js +261 -0
  93. package/dist/cloudflare/workers/contract.d.ts +8 -0
  94. package/dist/cloudflare/workers/contract.js +65 -0
  95. package/dist/cloudflare/workers/feature.d.ts +10 -0
  96. package/dist/cloudflare/workers/feature.js +28 -0
  97. package/dist/cloudflare/workers/runtime.d.ts +23 -0
  98. package/dist/cloudflare/workers/runtime.js +121 -0
  99. package/dist/cloudflare/workers/types.d.ts +49 -0
  100. package/dist/cloudflare/workers/types.js +1 -0
  101. package/dist/cloudflare/workers/worker-runtime.d.ts +44 -0
  102. package/dist/cloudflare/workers/worker-runtime.js +267 -0
  103. package/dist/compat/assert/LICENSE-3RD-PARTY.md +5 -0
  104. package/dist/compat/assert/LICENSE-APACHE +177 -0
  105. package/dist/compat/assert/assertion-error.d.ts +26 -0
  106. package/dist/compat/assert/assertion-error.js +468 -0
  107. package/dist/compat/assert/diff.d.ts +26 -0
  108. package/dist/compat/assert/diff.js +329 -0
  109. package/dist/compat/assert/errors.d.ts +11 -0
  110. package/dist/compat/assert/errors.js +55 -0
  111. package/dist/compat/assert/index.cjs +24 -0
  112. package/dist/compat/assert/index.d.cts +43 -0
  113. package/dist/compat/assert/index.d.ts +50 -0
  114. package/dist/compat/assert/index.js +858 -0
  115. package/dist/compat/async-hooks/LICENSE-APACHE +177 -0
  116. package/dist/compat/async-hooks/NOTICE.md +8 -0
  117. package/dist/compat/async-hooks/callbacks.d.ts +7 -0
  118. package/dist/compat/async-hooks/callbacks.js +18 -0
  119. package/dist/compat/async-hooks/hooks.d.ts +10 -0
  120. package/dist/compat/async-hooks/hooks.js +111 -0
  121. package/dist/compat/async-hooks/index.d.ts +41 -0
  122. package/dist/compat/async-hooks/index.js +298 -0
  123. package/dist/compat/buffer/LICENSE-3RD-PARTY.md +15 -0
  124. package/dist/compat/buffer/LICENSE-APACHE +177 -0
  125. package/dist/compat/buffer/blob.d.ts +22 -0
  126. package/dist/compat/buffer/blob.js +210 -0
  127. package/dist/compat/buffer/bytes.d.ts +34 -0
  128. package/dist/compat/buffer/bytes.js +228 -0
  129. package/dist/compat/buffer/errors.d.ts +41 -0
  130. package/dist/compat/buffer/errors.js +265 -0
  131. package/dist/compat/buffer/index.d.ts +25 -0
  132. package/dist/compat/buffer/index.js +24 -0
  133. package/dist/compat/buffer/internal-buffer.d.ts +148 -0
  134. package/dist/compat/buffer/internal-buffer.js +1809 -0
  135. package/dist/compat/buffer/support.d.ts +13 -0
  136. package/dist/compat/buffer/support.js +65 -0
  137. package/dist/compat/child-process/LICENSE-3RD-PARTY.md +8 -0
  138. package/dist/compat/child-process/LICENSE-APACHE +177 -0
  139. package/dist/compat/child-process/errors.d.ts +4 -0
  140. package/dist/compat/child-process/errors.js +6 -0
  141. package/dist/compat/child-process/index.d.ts +49 -0
  142. package/dist/compat/child-process/index.js +96 -0
  143. package/dist/compat/cluster/LICENSE-3RD-PARTY.md +40 -0
  144. package/dist/compat/cluster/LICENSE-APACHE +177 -0
  145. package/dist/compat/cluster/errors.d.ts +4 -0
  146. package/dist/compat/cluster/errors.js +6 -0
  147. package/dist/compat/cluster/index.d.ts +47 -0
  148. package/dist/compat/cluster/index.js +88 -0
  149. package/dist/compat/constants/LICENSE-3RD-PARTY.md +6 -0
  150. package/dist/compat/constants/LICENSE-APACHE +177 -0
  151. package/dist/compat/constants/index.d.ts +481 -0
  152. package/dist/compat/constants/index.js +488 -0
  153. package/dist/compat/dgram/LICENSE-3RD-PARTY.md +8 -0
  154. package/dist/compat/dgram/LICENSE-APACHE +177 -0
  155. package/dist/compat/dgram/index.d.ts +10 -0
  156. package/dist/compat/dgram/index.js +132 -0
  157. package/dist/compat/diagnostics-channel/LICENSE-3RD-PARTY.md +13 -0
  158. package/dist/compat/diagnostics-channel/LICENSE-APACHE +177 -0
  159. package/dist/compat/diagnostics-channel/index.d.ts +73 -0
  160. package/dist/compat/diagnostics-channel/index.js +261 -0
  161. package/dist/compat/diagnostics-channel/native.d.ts +26 -0
  162. package/dist/compat/diagnostics-channel/native.js +154 -0
  163. package/dist/compat/dns/LICENSE-3RD-PARTY.md +5 -0
  164. package/dist/compat/dns/LICENSE-APACHE +177 -0
  165. package/dist/compat/dns/client.d.ts +72 -0
  166. package/dist/compat/dns/client.js +161 -0
  167. package/dist/compat/dns/constants.d.ts +27 -0
  168. package/dist/compat/dns/constants.js +31 -0
  169. package/dist/compat/dns/errors.d.ts +8 -0
  170. package/dist/compat/dns/errors.js +12 -0
  171. package/dist/compat/dns/index.d.ts +97 -0
  172. package/dist/compat/dns/index.js +127 -0
  173. package/dist/compat/dns/parser.d.ts +18 -0
  174. package/dist/compat/dns/parser.js +144 -0
  175. package/dist/compat/dns/promise-resolver.d.ts +82 -0
  176. package/dist/compat/dns/promise-resolver.js +113 -0
  177. package/dist/compat/dns/promises.d.ts +50 -0
  178. package/dist/compat/dns/promises.js +53 -0
  179. package/dist/compat/dns/resolver.d.ts +34 -0
  180. package/dist/compat/dns/resolver.js +415 -0
  181. package/dist/compat/domain/LICENSE-3RD-PARTY.md +5 -0
  182. package/dist/compat/domain/LICENSE-APACHE +177 -0
  183. package/dist/compat/domain/index.d.ts +22 -0
  184. package/dist/compat/domain/index.js +79 -0
  185. package/dist/compat/fs/index.d.ts +85 -0
  186. package/dist/compat/fs/index.js +255 -0
  187. package/dist/compat/fs/promises/index.d.ts +3 -0
  188. package/dist/compat/fs/promises/index.js +3 -0
  189. package/dist/compat/http/index.d.ts +322 -0
  190. package/dist/compat/http/index.js +1356 -0
  191. package/dist/compat/http/registry.d.ts +19 -0
  192. package/dist/compat/http/registry.js +13 -0
  193. package/dist/compat/http-agent/LICENSE-3RD-PARTY.md +5 -0
  194. package/dist/compat/http-agent/LICENSE-APACHE +177 -0
  195. package/dist/compat/http-agent/index.d.ts +7 -0
  196. package/dist/compat/http-agent/index.js +10 -0
  197. package/dist/compat/http-client/LICENSE-3RD-PARTY.md +5 -0
  198. package/dist/compat/http-client/LICENSE-APACHE +177 -0
  199. package/dist/compat/http-client/index.d.ts +6 -0
  200. package/dist/compat/http-client/index.js +7 -0
  201. package/dist/compat/http-incoming/LICENSE-3RD-PARTY.md +5 -0
  202. package/dist/compat/http-incoming/LICENSE-APACHE +177 -0
  203. package/dist/compat/http-incoming/index.d.ts +6 -0
  204. package/dist/compat/http-incoming/index.js +9 -0
  205. package/dist/compat/http2/LICENSE-3RD-PARTY.md +29 -0
  206. package/dist/compat/http2/LICENSE-APACHE +177 -0
  207. package/dist/compat/http2/constants.d.ts +255 -0
  208. package/dist/compat/http2/constants.js +515 -0
  209. package/dist/compat/http2/errors.d.ts +4 -0
  210. package/dist/compat/http2/errors.js +6 -0
  211. package/dist/compat/http2/index.d.ts +283 -0
  212. package/dist/compat/http2/index.js +68 -0
  213. package/dist/compat/https/LICENSE-3RD-PARTY.md +5 -0
  214. package/dist/compat/https/LICENSE-APACHE +177 -0
  215. package/dist/compat/https/agent.d.ts +10 -0
  216. package/dist/compat/https/agent.js +99 -0
  217. package/dist/compat/https/errors.d.ts +4 -0
  218. package/dist/compat/https/errors.js +6 -0
  219. package/dist/compat/https/index.d.ts +20 -0
  220. package/dist/compat/https/index.js +50 -0
  221. package/dist/compat/https/server.d.ts +8 -0
  222. package/dist/compat/https/server.js +27 -0
  223. package/dist/compat/immediate/index.d.ts +11 -0
  224. package/dist/compat/immediate/index.js +58 -0
  225. package/dist/compat/inspector/LICENSE-3RD-PARTY.md +34 -0
  226. package/dist/compat/inspector/LICENSE-APACHE +177 -0
  227. package/dist/compat/inspector/errors.d.ts +4 -0
  228. package/dist/compat/inspector/errors.js +6 -0
  229. package/dist/compat/inspector/index.d.ts +18 -0
  230. package/dist/compat/inspector/index.js +117 -0
  231. package/dist/compat/inspector/promises.d.ts +18 -0
  232. package/dist/compat/inspector/promises.js +114 -0
  233. package/dist/compat/module/index.cjs +2 -0
  234. package/dist/compat/module/index.d.cts +2 -0
  235. package/dist/compat/module/index.d.ts +11 -0
  236. package/dist/compat/module/index.js +44 -0
  237. package/dist/compat/module/resolve-unavailable.d.ts +1 -0
  238. package/dist/compat/module/resolve-unavailable.js +3 -0
  239. package/dist/compat/net/LICENSE-3RD-PARTY.md +15 -0
  240. package/dist/compat/net/LICENSE-APACHE +177 -0
  241. package/dist/compat/net/error-base.d.ts +14 -0
  242. package/dist/compat/net/error-base.js +62 -0
  243. package/dist/compat/net/errors.d.ts +76 -0
  244. package/dist/compat/net/errors.js +333 -0
  245. package/dist/compat/net/index.d.ts +23 -0
  246. package/dist/compat/net/index.js +46 -0
  247. package/dist/compat/net/internal-net.d.ts +236 -0
  248. package/dist/compat/net/internal-net.js +2292 -0
  249. package/dist/compat/net/ports.d.ts +32 -0
  250. package/dist/compat/net/ports.js +149 -0
  251. package/dist/compat/net/reader.d.ts +8 -0
  252. package/dist/compat/net/reader.js +36 -0
  253. package/dist/compat/net/sockets.d.ts +20 -0
  254. package/dist/compat/net/sockets.js +195 -0
  255. package/dist/compat/net/validators.d.ts +18 -0
  256. package/dist/compat/net/validators.js +154 -0
  257. package/dist/compat/os/LICENSE-3RD-PARTY.md +8 -0
  258. package/dist/compat/os/LICENSE-APACHE +177 -0
  259. package/dist/compat/os/index.d.ts +329 -0
  260. package/dist/compat/os/index.js +278 -0
  261. package/dist/compat/path/LICENSE-3RD-PARTY.md +35 -0
  262. package/dist/compat/path/LICENSE-APACHE +177 -0
  263. package/dist/compat/path/index.d.ts +4 -0
  264. package/dist/compat/path/index.js +11 -0
  265. package/dist/compat/path/internal-path.d.ts +95 -0
  266. package/dist/compat/path/internal-path.js +1542 -0
  267. package/dist/compat/path/posix/index.d.ts +4 -0
  268. package/dist/compat/path/posix/index.js +5 -0
  269. package/dist/compat/path/win32/index.d.ts +4 -0
  270. package/dist/compat/path/win32/index.js +5 -0
  271. package/dist/compat/perf-hooks/LICENSE-3RD-PARTY.md +5 -0
  272. package/dist/compat/perf-hooks/LICENSE-APACHE +177 -0
  273. package/dist/compat/perf-hooks/index.d.ts +85 -0
  274. package/dist/compat/perf-hooks/index.js +79 -0
  275. package/dist/compat/perf-hooks/performance.d.ts +96 -0
  276. package/dist/compat/perf-hooks/performance.js +445 -0
  277. package/dist/compat/punycode/LICENSE-3RD-PARTY.md +7 -0
  278. package/dist/compat/punycode/LICENSE-APACHE +177 -0
  279. package/dist/compat/punycode/index.d.ts +93 -0
  280. package/dist/compat/punycode/index.js +410 -0
  281. package/dist/compat/querystring/LICENSE-3RD-PARTY.md +35 -0
  282. package/dist/compat/querystring/LICENSE-APACHE +177 -0
  283. package/dist/compat/querystring/index.d.ts +14 -0
  284. package/dist/compat/querystring/index.js +18 -0
  285. package/dist/compat/querystring/internal-querystring.d.ts +22 -0
  286. package/dist/compat/querystring/internal-querystring.js +498 -0
  287. package/dist/compat/readline/LICENSE-3RD-PARTY.md +37 -0
  288. package/dist/compat/readline/LICENSE-APACHE +177 -0
  289. package/dist/compat/readline/index.d.ts +21 -0
  290. package/dist/compat/readline/index.js +56 -0
  291. package/dist/compat/readline/interface.d.ts +22 -0
  292. package/dist/compat/readline/interface.js +85 -0
  293. package/dist/compat/readline/internal-promises.d.ts +32 -0
  294. package/dist/compat/readline/internal-promises.js +104 -0
  295. package/dist/compat/readline/promises.d.ts +8 -0
  296. package/dist/compat/readline/promises.js +32 -0
  297. package/dist/compat/repl/LICENSE-3RD-PARTY.md +5 -0
  298. package/dist/compat/repl/LICENSE-APACHE +177 -0
  299. package/dist/compat/repl/builtin-modules.d.ts +1 -0
  300. package/dist/compat/repl/builtin-modules.js +101 -0
  301. package/dist/compat/repl/errors.d.ts +4 -0
  302. package/dist/compat/repl/errors.js +6 -0
  303. package/dist/compat/repl/index.d.ts +53 -0
  304. package/dist/compat/repl/index.js +106 -0
  305. package/dist/compat/sqlite/LICENSE-3RD-PARTY.md +6 -0
  306. package/dist/compat/sqlite/LICENSE-APACHE +177 -0
  307. package/dist/compat/sqlite/index.d.ts +12 -0
  308. package/dist/compat/sqlite/index.js +101 -0
  309. package/dist/compat/sqlite/native.d.ts +16 -0
  310. package/dist/compat/sqlite/native.js +33 -0
  311. package/dist/compat/stream/LICENSE-3RD-PARTY.md +13 -0
  312. package/dist/compat/stream/LICENSE-APACHE +177 -0
  313. package/dist/compat/stream/add-abort-signal.d.ts +4 -0
  314. package/dist/compat/stream/add-abort-signal.js +73 -0
  315. package/dist/compat/stream/compose.d.ts +1 -0
  316. package/dist/compat/stream/compose.js +313 -0
  317. package/dist/compat/stream/destroy.d.ts +5 -0
  318. package/dist/compat/stream/destroy.js +315 -0
  319. package/dist/compat/stream/duplex.d.ts +48 -0
  320. package/dist/compat/stream/duplex.js +789 -0
  321. package/dist/compat/stream/end-of-stream.d.ts +16 -0
  322. package/dist/compat/stream/end-of-stream.js +269 -0
  323. package/dist/compat/stream/errors.d.ts +40 -0
  324. package/dist/compat/stream/errors.js +86 -0
  325. package/dist/compat/stream/events.d.ts +6 -0
  326. package/dist/compat/stream/events.js +28 -0
  327. package/dist/compat/stream/index.cjs +27 -0
  328. package/dist/compat/stream/index.d.cts +25 -0
  329. package/dist/compat/stream/index.d.ts +20 -0
  330. package/dist/compat/stream/index.js +55 -0
  331. package/dist/compat/stream/legacy.d.ts +8 -0
  332. package/dist/compat/stream/legacy.js +105 -0
  333. package/dist/compat/stream/once.d.ts +7 -0
  334. package/dist/compat/stream/once.js +13 -0
  335. package/dist/compat/stream/pipeline.d.ts +5 -0
  336. package/dist/compat/stream/pipeline.js +567 -0
  337. package/dist/compat/stream/process.d.ts +1 -0
  338. package/dist/compat/stream/process.js +4 -0
  339. package/dist/compat/stream/promises.d.ts +7 -0
  340. package/dist/compat/stream/promises.js +60 -0
  341. package/dist/compat/stream/readable.d.ts +29 -0
  342. package/dist/compat/stream/readable.js +2267 -0
  343. package/dist/compat/stream/state.d.ts +8 -0
  344. package/dist/compat/stream/state.js +63 -0
  345. package/dist/compat/stream/transform.d.ts +2 -0
  346. package/dist/compat/stream/transform.js +202 -0
  347. package/dist/compat/stream/util.d.ts +55 -0
  348. package/dist/compat/stream/util.js +345 -0
  349. package/dist/compat/stream/validators.d.ts +2 -0
  350. package/dist/compat/stream/validators.js +14 -0
  351. package/dist/compat/stream/writable.d.ts +21 -0
  352. package/dist/compat/stream/writable.js +1325 -0
  353. package/dist/compat/stream-consumers/LICENSE-3RD-PARTY.md +5 -0
  354. package/dist/compat/stream-consumers/LICENSE-APACHE +177 -0
  355. package/dist/compat/stream-consumers/index.d.ts +14 -0
  356. package/dist/compat/stream-consumers/index.js +62 -0
  357. package/dist/compat/stream-consumers/iterable.d.ts +1 -0
  358. package/dist/compat/stream-consumers/iterable.js +34 -0
  359. package/dist/compat/stream-duplex/LICENSE-3RD-PARTY.md +13 -0
  360. package/dist/compat/stream-duplex/LICENSE-APACHE +177 -0
  361. package/dist/compat/stream-duplex/index.cjs +3 -0
  362. package/dist/compat/stream-duplex/index.d.cts +2 -0
  363. package/dist/compat/stream-duplex/index.d.ts +3 -0
  364. package/dist/compat/stream-duplex/index.js +8 -0
  365. package/dist/compat/stream-passthrough/LICENSE-3RD-PARTY.md +13 -0
  366. package/dist/compat/stream-passthrough/LICENSE-APACHE +177 -0
  367. package/dist/compat/stream-passthrough/index.cjs +3 -0
  368. package/dist/compat/stream-passthrough/index.d.cts +2 -0
  369. package/dist/compat/stream-passthrough/index.d.ts +3 -0
  370. package/dist/compat/stream-passthrough/index.js +8 -0
  371. package/dist/compat/stream-promises/LICENSE-3RD-PARTY.md +13 -0
  372. package/dist/compat/stream-promises/LICENSE-APACHE +177 -0
  373. package/dist/compat/stream-promises/index.d.ts +1 -0
  374. package/dist/compat/stream-promises/index.js +1 -0
  375. package/dist/compat/stream-readable/LICENSE-3RD-PARTY.md +13 -0
  376. package/dist/compat/stream-readable/LICENSE-APACHE +177 -0
  377. package/dist/compat/stream-readable/index.cjs +3 -0
  378. package/dist/compat/stream-readable/index.d.cts +2 -0
  379. package/dist/compat/stream-readable/index.d.ts +3 -0
  380. package/dist/compat/stream-readable/index.js +8 -0
  381. package/dist/compat/stream-transform/LICENSE-3RD-PARTY.md +13 -0
  382. package/dist/compat/stream-transform/LICENSE-APACHE +177 -0
  383. package/dist/compat/stream-transform/index.cjs +3 -0
  384. package/dist/compat/stream-transform/index.d.cts +2 -0
  385. package/dist/compat/stream-transform/index.d.ts +3 -0
  386. package/dist/compat/stream-transform/index.js +8 -0
  387. package/dist/compat/stream-web/LICENSE-MIT +22 -0
  388. package/dist/compat/stream-web/NOTICE.md +3 -0
  389. package/dist/compat/stream-web/context.d.ts +2 -0
  390. package/dist/compat/stream-web/context.js +35 -0
  391. package/dist/compat/stream-web/index.d.ts +80 -0
  392. package/dist/compat/stream-web/index.js +129 -0
  393. package/dist/compat/stream-web/interop.d.ts +6 -0
  394. package/dist/compat/stream-web/interop.js +72 -0
  395. package/dist/compat/stream-web/ponyfill.d.ts +1 -0
  396. package/dist/compat/stream-web/ponyfill.js +2093 -0
  397. package/dist/compat/stream-web/runtime.d.ts +16 -0
  398. package/dist/compat/stream-web/runtime.js +51 -0
  399. package/dist/compat/stream-writable/LICENSE-3RD-PARTY.md +13 -0
  400. package/dist/compat/stream-writable/LICENSE-APACHE +177 -0
  401. package/dist/compat/stream-writable/index.cjs +3 -0
  402. package/dist/compat/stream-writable/index.d.cts +2 -0
  403. package/dist/compat/stream-writable/index.d.ts +3 -0
  404. package/dist/compat/stream-writable/index.js +8 -0
  405. package/dist/compat/string-decoder/LICENSE-3RD-PARTY.md +38 -0
  406. package/dist/compat/string-decoder/LICENSE-APACHE +177 -0
  407. package/dist/compat/string-decoder/index.d.ts +33 -0
  408. package/dist/compat/string-decoder/index.js +518 -0
  409. package/dist/compat/test/LICENSE-3RD-PARTY.md +37 -0
  410. package/dist/compat/test/LICENSE-APACHE +177 -0
  411. package/dist/compat/test/index.d.ts +9 -0
  412. package/dist/compat/test/index.js +11 -0
  413. package/dist/compat/test/mock.d.ts +103 -0
  414. package/dist/compat/test/mock.js +397 -0
  415. package/dist/compat/test/validators.d.ts +2 -0
  416. package/dist/compat/test/validators.js +14 -0
  417. package/dist/compat/timers/LICENSE-3RD-PARTY.md +7 -0
  418. package/dist/compat/timers/LICENSE-APACHE +177 -0
  419. package/dist/compat/timers/errors.d.ts +28 -0
  420. package/dist/compat/timers/errors.js +206 -0
  421. package/dist/compat/timers/host.d.ts +11 -0
  422. package/dist/compat/timers/host.js +60 -0
  423. package/dist/compat/timers/index.cjs +3 -0
  424. package/dist/compat/timers/index.d.cts +23 -0
  425. package/dist/compat/timers/index.d.ts +36 -0
  426. package/dist/compat/timers/index.js +20 -0
  427. package/dist/compat/timers/installation.d.ts +4 -0
  428. package/dist/compat/timers/installation.js +61 -0
  429. package/dist/compat/timers/internal-promises.d.ts +19 -0
  430. package/dist/compat/timers/internal-promises.js +173 -0
  431. package/dist/compat/timers/promises.cjs +2 -0
  432. package/dist/compat/timers/promises.d.cts +12 -0
  433. package/dist/compat/timers/promises.d.ts +14 -0
  434. package/dist/compat/timers/promises.js +3 -0
  435. package/dist/compat/timers/timers.d.ts +32 -0
  436. package/dist/compat/timers/timers.js +147 -0
  437. package/dist/compat/timers/validation.d.ts +5 -0
  438. package/dist/compat/timers/validation.js +22 -0
  439. package/dist/compat/timers/wait.d.ts +4 -0
  440. package/dist/compat/timers/wait.js +91 -0
  441. package/dist/compat/tls-common/LICENSE-3RD-PARTY.md +11 -0
  442. package/dist/compat/tls-common/LICENSE-APACHE +177 -0
  443. package/dist/compat/tls-common/index.d.ts +14 -0
  444. package/dist/compat/tls-common/index.js +88 -0
  445. package/dist/compat/trace-events/LICENSE-3RD-PARTY.md +5 -0
  446. package/dist/compat/trace-events/LICENSE-APACHE +177 -0
  447. package/dist/compat/trace-events/index.d.ts +18 -0
  448. package/dist/compat/trace-events/index.js +88 -0
  449. package/dist/compat/tty/LICENSE-3RD-PARTY.md +8 -0
  450. package/dist/compat/tty/LICENSE-APACHE +177 -0
  451. package/dist/compat/tty/index.d.ts +17 -0
  452. package/dist/compat/tty/index.js +101 -0
  453. package/dist/compat/url/LICENSE-3RD-PARTY.md +63 -0
  454. package/dist/compat/url/LICENSE-ADA +18 -0
  455. package/dist/compat/url/LICENSE-APACHE +177 -0
  456. package/dist/compat/url/LICENSE-UNICODE +39 -0
  457. package/dist/compat/url/LICENSE-WHATWG-URL +21 -0
  458. package/dist/compat/url/brand.d.ts +4 -0
  459. package/dist/compat/url/brand.js +10 -0
  460. package/dist/compat/url/constants.d.ts +27 -0
  461. package/dist/compat/url/constants.js +28 -0
  462. package/dist/compat/url/errors.d.ts +44 -0
  463. package/dist/compat/url/errors.js +248 -0
  464. package/dist/compat/url/idna-data.d.ts +3958 -0
  465. package/dist/compat/url/idna-data.js +22 -0
  466. package/dist/compat/url/idna.d.ts +9 -0
  467. package/dist/compat/url/idna.js +47 -0
  468. package/dist/compat/url/index.d.ts +48 -0
  469. package/dist/compat/url/index.js +39 -0
  470. package/dist/compat/url/installation.d.ts +4 -0
  471. package/dist/compat/url/installation.js +61 -0
  472. package/dist/compat/url/internal-url.d.ts +34 -0
  473. package/dist/compat/url/internal-url.js +264 -0
  474. package/dist/compat/url/legacy-url.d.ts +28 -0
  475. package/dist/compat/url/legacy-url.js +870 -0
  476. package/dist/compat/url/parser-platform.d.ts +3 -0
  477. package/dist/compat/url/parser-platform.js +26 -0
  478. package/dist/compat/url/standard-url.d.ts +2 -0
  479. package/dist/compat/url/standard-url.js +3189 -0
  480. package/dist/compat/url/unicode.d.ts +2 -0
  481. package/dist/compat/url/unicode.js +119 -0
  482. package/dist/compat/url/validation.d.ts +2 -0
  483. package/dist/compat/url/validation.js +9 -0
  484. package/dist/compat/url/web-url.d.ts +27 -0
  485. package/dist/compat/url/web-url.js +119 -0
  486. package/dist/compat/util/LICENSE-3RD-PARTY.md +5 -0
  487. package/dist/compat/util/LICENSE-APACHE +177 -0
  488. package/dist/compat/util/additional.d.ts +11 -0
  489. package/dist/compat/util/additional.js +121 -0
  490. package/dist/compat/util/comparison-types.d.ts +16 -0
  491. package/dist/compat/util/comparison-types.js +50 -0
  492. package/dist/compat/util/comparisons.d.ts +1 -0
  493. package/dist/compat/util/comparisons.js +527 -0
  494. package/dist/compat/util/core.d.ts +18 -0
  495. package/dist/compat/util/core.js +265 -0
  496. package/dist/compat/util/debuglog.d.ts +1 -0
  497. package/dist/compat/util/debuglog.js +78 -0
  498. package/dist/compat/util/index.d.ts +35 -0
  499. package/dist/compat/util/index.js +77 -0
  500. package/dist/compat/util/inspect-binding.d.ts +13 -0
  501. package/dist/compat/util/inspect-binding.js +39 -0
  502. package/dist/compat/util/inspect-types.d.ts +13 -0
  503. package/dist/compat/util/inspect-types.js +30 -0
  504. package/dist/compat/util/inspect.d.ts +152 -0
  505. package/dist/compat/util/inspect.js +2503 -0
  506. package/dist/compat/util/mime.d.ts +24 -0
  507. package/dist/compat/util/mime.js +284 -0
  508. package/dist/compat/util/properties.d.ts +9 -0
  509. package/dist/compat/util/properties.js +76 -0
  510. package/dist/compat/v8/LICENSE-3RD-PARTY.md +6 -0
  511. package/dist/compat/v8/LICENSE-APACHE +177 -0
  512. package/dist/compat/v8/errors.d.ts +5 -0
  513. package/dist/compat/v8/errors.js +7 -0
  514. package/dist/compat/v8/index.d.ts +106 -0
  515. package/dist/compat/v8/index.js +280 -0
  516. package/dist/compat/vm/LICENSE-3RD-PARTY.md +39 -0
  517. package/dist/compat/vm/LICENSE-APACHE +177 -0
  518. package/dist/compat/vm/errors.d.ts +5 -0
  519. package/dist/compat/vm/errors.js +7 -0
  520. package/dist/compat/vm/index.d.ts +47 -0
  521. package/dist/compat/vm/index.js +231 -0
  522. package/dist/compat/vm/validation.d.ts +3 -0
  523. package/dist/compat/vm/validation.js +14 -0
  524. package/dist/compat/wasi/LICENSE-3RD-PARTY.md +8 -0
  525. package/dist/compat/wasi/LICENSE-APACHE +177 -0
  526. package/dist/compat/wasi/errors.d.ts +4 -0
  527. package/dist/compat/wasi/errors.js +6 -0
  528. package/dist/compat/wasi/index.d.ts +15 -0
  529. package/dist/compat/wasi/index.js +44 -0
  530. package/dist/compat/worker-threads/LICENSE-3RD-PARTY.md +5 -0
  531. package/dist/compat/worker-threads/LICENSE-APACHE +177 -0
  532. package/dist/compat/worker-threads/errors.d.ts +4 -0
  533. package/dist/compat/worker-threads/errors.js +6 -0
  534. package/dist/compat/worker-threads/index.d.ts +71 -0
  535. package/dist/compat/worker-threads/index.js +138 -0
  536. package/dist/compat/worker-threads/message-channel.d.ts +18 -0
  537. package/dist/compat/worker-threads/message-channel.js +157 -0
  538. package/dist/configured.d.ts +3 -0
  539. package/dist/configured.js +4 -0
  540. package/dist/environment.d.ts +4 -0
  541. package/dist/environment.js +18 -0
  542. package/dist/index.d.ts +11 -0
  543. package/dist/index.js +107 -0
  544. package/dist/ios.d.ts +2 -0
  545. package/dist/ios.js +1 -0
  546. package/dist/native-entry.d.ts +8 -0
  547. package/dist/native-entry.js +67 -0
  548. package/dist/shared/brotli/LICENSE-APACHE +201 -0
  549. package/dist/shared/brotli/LICENSE-MIT +9 -0
  550. package/dist/shared/brotli/NOTICE +3 -0
  551. package/dist/shared/brotli/index.d.ts +2 -0
  552. package/dist/shared/brotli/index.js +288 -0
  553. package/dist/shared/contracts/index.d.ts +42 -0
  554. package/dist/shared/contracts/index.js +88 -0
  555. package/dist/shared/contracts/testing.d.ts +22 -0
  556. package/dist/shared/contracts/testing.js +57 -0
  557. package/dist/shared/globals/index.d.ts +3 -0
  558. package/dist/shared/globals/index.js +87 -0
  559. package/dist/shared/globals/inject.d.ts +2 -0
  560. package/dist/shared/globals/inject.js +3 -0
  561. package/dist/shared/globals/install.d.ts +1 -0
  562. package/dist/shared/globals/install.js +15 -0
  563. package/dist/shared/globals/node-install.d.ts +1 -0
  564. package/dist/shared/globals/node-install.js +4 -0
  565. package/dist/shared/globals/platform.d.ts +104 -0
  566. package/dist/shared/globals/platform.js +37 -0
  567. package/dist/shared/globals/runtime.d.ts +6 -0
  568. package/dist/shared/globals/runtime.js +8 -0
  569. package/dist/shared/globals/symbols.d.ts +4 -0
  570. package/dist/shared/globals/symbols.js +35 -0
  571. package/dist/shared/globals/web.d.ts +4 -0
  572. package/dist/shared/globals/web.js +108 -0
  573. package/dist/shared/http-wire/LICENSE.llhttp +22 -0
  574. package/dist/shared/http-wire/LICENSE.undici +21 -0
  575. package/dist/shared/http-wire/NOTICE.md +14 -0
  576. package/dist/shared/http-wire/index.d.ts +27 -0
  577. package/dist/shared/http-wire/index.js +132 -0
  578. package/dist/shared/http-wire/wasm.d.ts +2 -0
  579. package/dist/shared/http-wire/wasm.js +2 -0
  580. package/dist/tooling/async.d.ts +7 -0
  581. package/dist/tooling/async.js +99 -0
  582. package/dist/tooling/build-isolation.d.ts +4 -0
  583. package/dist/tooling/build-isolation.js +34 -0
  584. package/dist/tooling/documents.d.ts +7 -0
  585. package/dist/tooling/documents.js +26 -0
  586. package/dist/tooling/esbuild.d.ts +15 -0
  587. package/dist/tooling/esbuild.js +193 -0
  588. package/dist/tooling/files.d.ts +2 -0
  589. package/dist/tooling/files.js +18 -0
  590. package/dist/tooling/opennext/build.d.ts +1 -0
  591. package/dist/tooling/opennext/build.js +121 -0
  592. package/dist/tooling/opennext/index.d.ts +3 -0
  593. package/dist/tooling/opennext/index.js +213 -0
  594. package/dist/tooling/opennext/plan.d.ts +9 -0
  595. package/dist/tooling/opennext/plan.js +36 -0
  596. package/dist/tooling/opennext/safety.d.ts +4 -0
  597. package/dist/tooling/opennext/safety.js +36 -0
  598. package/dist/tooling/route-permissions.d.ts +3 -0
  599. package/dist/tooling/route-permissions.js +15 -0
  600. package/dist/tooling/server-modules.d.ts +30 -0
  601. package/dist/tooling/server-modules.js +98 -0
  602. package/dist/tooling/server-only-html.d.ts +1 -0
  603. package/dist/tooling/server-only-html.js +66 -0
  604. package/dist/tooling/server-only.d.ts +6 -0
  605. package/dist/tooling/server-only.js +53 -0
  606. package/dist/tooling/server.d.ts +25 -0
  607. package/dist/tooling/server.js +981 -0
  608. package/dist/tooling/shared.d.ts +13 -0
  609. package/dist/tooling/shared.js +130 -0
  610. package/dist/tooling/vite.d.ts +18 -0
  611. package/dist/tooling/vite.js +448 -0
  612. package/dist/tooling/void/index.d.ts +3 -0
  613. package/dist/tooling/void/index.js +205 -0
  614. package/dist/tooling/void/plan.d.ts +17 -0
  615. package/dist/tooling/void/plan.js +288 -0
  616. package/native/android/build.gradle +68 -0
  617. package/native/android/consumer-rules.pro +2 -0
  618. package/native/android/src/main/AndroidManifest.xml +6 -0
  619. package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +202 -0
  620. package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersCapacitorPlugin.java +256 -0
  621. package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersNative.java +22 -0
  622. package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersNativeActivity.java +178 -0
  623. package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersNativeBridge.java +45 -0
  624. package/native/ios/DocumentBridge.swift +330 -0
  625. package/native/ios/NetworkBridge.swift +51 -0
  626. package/native/ios/SocketBridge.swift +180 -0
  627. package/native/ios/WorkersCapacitorPlugin.swift +152 -0
  628. package/native/ios-host/WorkersView.swift +194 -0
  629. package/package.json +154 -4
@@ -0,0 +1,2267 @@
1
+ // @ts-nocheck
2
+ import { asyncDispose } from "../../shared/globals/symbols.js";
3
+ // Copyright (c) 2017-2022 Cloudflare, Inc.
4
+ // Licensed under the Apache 2.0 license found in the LICENSE file or at:
5
+ // https://opensource.org/licenses/Apache-2.0
6
+ //
7
+ // Copyright Joyent, Inc. and other Node contributors.
8
+ //
9
+ // Permission is hereby granted, free of charge, to any person obtaining a
10
+ // copy of this software and associated documentation files (the
11
+ // "Software"), to deal in the Software without restriction, including
12
+ // without limitation the rights to use, copy, modify, merge, publish,
13
+ // distribute, sublicense, and/or sell copies of the Software, and to permit
14
+ // persons to whom the Software is furnished to do so, subject to the
15
+ // following conditions:
16
+ //
17
+ // The above copyright notice and this permission notice shall be included
18
+ // in all copies or substantial portions of the Software.
19
+ //
20
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21
+ // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
23
+ // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
24
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25
+ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26
+ // USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ import { kState,
28
+ // bitfields
29
+ kObjectMode, kErrorEmitted, kAutoDestroy, kEmitClose, kDestroyed, kClosed, kCloseEmitted, kErrored, kConstructed, kOnConstructed, isDestroyed, isReadable, isReadableStream, handleKnownInternalErrors, } from "./util.js";
30
+ import { nextTick } from "./process.js";
31
+ import { destroy, undestroy, errorOrDestroy, destroyer, construct, } from "./destroy.js";
32
+ import { eos, finished, nop } from "./end-of-stream.js";
33
+ import { getHighWaterMark, getDefaultHighWaterMark } from "./state.js";
34
+ import { addAbortSignal } from "./add-abort-signal.js";
35
+ import { EventEmitter } from "./events.js";
36
+ import { Stream } from "./legacy.js";
37
+ import { Buffer } from "node:buffer";
38
+ import { AbortError, aggregateTwoErrors, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_METHOD_NOT_IMPLEMENTED, ERR_MISSING_ARGS, ERR_OUT_OF_RANGE, ERR_STREAM_PUSH_AFTER_EOF, ERR_STREAM_UNSHIFT_AFTER_END_EVENT, ERR_STREAM_NULL_VALUES, ERR_UNKNOWN_ENCODING, } from "./errors.js";
39
+ import { validateObject, validateAbortSignal, validateBoolean, validateInteger, } from "./validators.js";
40
+ import { StringDecoder } from "node:string_decoder";
41
+ const streamsNodejsV24Compat = false;
42
+ const kErroredValue = Symbol("kErroredValue");
43
+ const kDefaultEncodingValue = Symbol("kDefaultEncodingValue");
44
+ const kDecoderValue = Symbol("kDecoderValue");
45
+ const kEncodingValue = Symbol("kEncodingValue");
46
+ // Bitfield flag constants for ReadableState. Each constant uses left-shift (<<) to set a specific
47
+ // bit position, allowing multiple boolean flags to be stored efficiently in a single integer (kState).
48
+ // For example, `1 << 9` creates a value with only bit 9 set (value: 512).
49
+ const kEnded = 1 << 9;
50
+ const kEndEmitted = 1 << 10;
51
+ const kReading = 1 << 11;
52
+ const kSync = 1 << 12;
53
+ const kNeedReadable = 1 << 13;
54
+ const kEmittedReadable = 1 << 14;
55
+ const kReadableListening = 1 << 15;
56
+ const kResumeScheduled = 1 << 16;
57
+ const kMultiAwaitDrain = 1 << 17;
58
+ const kReadingMore = 1 << 18;
59
+ const kDataEmitted = 1 << 19;
60
+ const kDefaultUTF8Encoding = 1 << 20;
61
+ const kDecoder = 1 << 21;
62
+ const kEncoding = 1 << 22;
63
+ const kHasFlowing = 1 << 23;
64
+ const kFlowing = 1 << 24;
65
+ const kHasPaused = 1 << 25;
66
+ const kPaused = 1 << 26;
67
+ const kDataListening = 1 << 27;
68
+ // ======================================================================================
69
+ // ReadableState
70
+ // TODO(benjamingr) it is likely slower to do it this way than with free functions
71
+ function makeBitMapDescriptor(bit) {
72
+ return {
73
+ enumerable: false,
74
+ get() {
75
+ return (this[kState] & bit) !== 0;
76
+ },
77
+ set(value) {
78
+ if (value)
79
+ this[kState] |= bit;
80
+ else
81
+ this[kState] &= ~bit;
82
+ },
83
+ };
84
+ }
85
+ Object.defineProperties(ReadableState.prototype, {
86
+ objectMode: makeBitMapDescriptor(kObjectMode),
87
+ ended: makeBitMapDescriptor(kEnded),
88
+ endEmitted: makeBitMapDescriptor(kEndEmitted),
89
+ reading: makeBitMapDescriptor(kReading),
90
+ // Stream is still being constructed and cannot be
91
+ // destroyed until construction finished or failed.
92
+ // Async construction is opt in, therefore we start as
93
+ // constructed.
94
+ constructed: makeBitMapDescriptor(kConstructed),
95
+ // A flag to be able to tell if the event 'readable'/'data' is emitted
96
+ // immediately, or on a later tick. We set this to true at first, because
97
+ // any actions that shouldn't happen until "later" should generally also
98
+ // not happen before the first read call.
99
+ sync: makeBitMapDescriptor(kSync),
100
+ // Whenever we return null, then we set a flag to say
101
+ // that we're awaiting a 'readable' event emission.
102
+ needReadable: makeBitMapDescriptor(kNeedReadable),
103
+ emittedReadable: makeBitMapDescriptor(kEmittedReadable),
104
+ readableListening: makeBitMapDescriptor(kReadableListening),
105
+ resumeScheduled: makeBitMapDescriptor(kResumeScheduled),
106
+ // True if the error was already emitted and should not be thrown again.
107
+ errorEmitted: makeBitMapDescriptor(kErrorEmitted),
108
+ emitClose: makeBitMapDescriptor(kEmitClose),
109
+ autoDestroy: makeBitMapDescriptor(kAutoDestroy),
110
+ // Has it been destroyed.
111
+ destroyed: makeBitMapDescriptor(kDestroyed),
112
+ // Indicates whether the stream has finished destroying.
113
+ closed: makeBitMapDescriptor(kClosed),
114
+ // True if close has been emitted or would have been emitted
115
+ // depending on emitClose.
116
+ closeEmitted: makeBitMapDescriptor(kCloseEmitted),
117
+ multiAwaitDrain: makeBitMapDescriptor(kMultiAwaitDrain),
118
+ // If true, a maybeReadMore has been scheduled.
119
+ readingMore: makeBitMapDescriptor(kReadingMore),
120
+ dataEmitted: makeBitMapDescriptor(kDataEmitted),
121
+ // Indicates whether the stream has errored. When true no further
122
+ // _read calls, 'data' or 'readable' events should occur. This is needed
123
+ // since when autoDestroy is disabled we need a way to tell whether the
124
+ // stream has failed.
125
+ errored: {
126
+ __proto__: null,
127
+ enumerable: false,
128
+ get() {
129
+ return (this[kState] & kErrored) !== 0 ? this[kErroredValue] : null;
130
+ },
131
+ set(value) {
132
+ if (value) {
133
+ this[kErroredValue] = value;
134
+ this[kState] |= kErrored;
135
+ }
136
+ else {
137
+ this[kState] &= ~kErrored;
138
+ }
139
+ },
140
+ },
141
+ defaultEncoding: {
142
+ __proto__: null,
143
+ enumerable: false,
144
+ get() {
145
+ return (this[kState] & kDefaultUTF8Encoding) !== 0
146
+ ? "utf8"
147
+ : this[kDefaultEncodingValue];
148
+ },
149
+ set(value) {
150
+ if (value === "utf8" || value === "utf-8") {
151
+ this[kState] |= kDefaultUTF8Encoding;
152
+ }
153
+ else {
154
+ this[kState] &= ~kDefaultUTF8Encoding;
155
+ this[kDefaultEncodingValue] = value;
156
+ }
157
+ },
158
+ },
159
+ decoder: {
160
+ __proto__: null,
161
+ enumerable: false,
162
+ get() {
163
+ return (this[kState] & kDecoder) !== 0 ? this[kDecoderValue] : null;
164
+ },
165
+ set(value) {
166
+ if (value) {
167
+ this[kDecoderValue] = value;
168
+ this[kState] |= kDecoder;
169
+ }
170
+ else {
171
+ this[kState] &= ~kDecoder;
172
+ }
173
+ },
174
+ },
175
+ encoding: {
176
+ __proto__: null,
177
+ enumerable: false,
178
+ get() {
179
+ return (this[kState] & kEncoding) !== 0 ? this[kEncodingValue] : null;
180
+ },
181
+ set(value) {
182
+ if (value) {
183
+ this[kEncodingValue] = value;
184
+ this[kState] |= kEncoding;
185
+ }
186
+ else {
187
+ this[kState] &= ~kEncoding;
188
+ }
189
+ },
190
+ },
191
+ flowing: {
192
+ __proto__: null,
193
+ enumerable: false,
194
+ get() {
195
+ return (this[kState] & kHasFlowing) !== 0
196
+ ? (this[kState] & kFlowing) !== 0
197
+ : null;
198
+ },
199
+ set(value) {
200
+ if (value == null) {
201
+ this[kState] &= ~(kHasFlowing | kFlowing);
202
+ }
203
+ else if (value) {
204
+ this[kState] |= kHasFlowing | kFlowing;
205
+ }
206
+ else {
207
+ this[kState] |= kHasFlowing;
208
+ this[kState] &= ~kFlowing;
209
+ }
210
+ },
211
+ },
212
+ });
213
+ export function ReadableState(options, _stream, isDuplex) {
214
+ // Bit map field to store ReadableState more efficiently with 1 bit per field
215
+ // instead of a V8 slot per field.
216
+ this[kState] = kEmitClose | kAutoDestroy | kConstructed | kSync;
217
+ // Object stream flag. Used to make read(n) ignore n and to
218
+ // make all the buffer merging and length checks go away.
219
+ if (options?.objectMode)
220
+ this[kState] |= kObjectMode;
221
+ if (isDuplex && options?.readableObjectMode)
222
+ this[kState] |= kObjectMode;
223
+ // The point at which it stops calling _read() to fill the buffer
224
+ // Note: 0 is a valid value, means "don't call _read preemptively ever"
225
+ this.highWaterMark = options
226
+ ? getHighWaterMark(this, options, "readableHighWaterMark", isDuplex)
227
+ : getDefaultHighWaterMark(false);
228
+ this.buffer = [];
229
+ this.bufferIndex = 0;
230
+ this.length = 0;
231
+ this.pipes = [];
232
+ // Should close be emitted on destroy. Defaults to true.
233
+ if (options && options.emitClose === false)
234
+ this[kState] &= ~kEmitClose;
235
+ // Should .destroy() be called after 'end' (and potentially 'finish').
236
+ if (options && options.autoDestroy === false)
237
+ this[kState] &= ~kAutoDestroy;
238
+ // Crypto is kind of old and crusty. Historically, its default string
239
+ // encoding is 'binary' so we have to make this configurable.
240
+ // Everything else in the universe uses 'utf8', though.
241
+ const defaultEncoding = options?.defaultEncoding;
242
+ if (defaultEncoding == null ||
243
+ defaultEncoding === "utf8" ||
244
+ defaultEncoding === "utf-8") {
245
+ this[kState] |= kDefaultUTF8Encoding;
246
+ }
247
+ else if (Buffer.isEncoding(defaultEncoding)) {
248
+ this.defaultEncoding = defaultEncoding;
249
+ }
250
+ else if (streamsNodejsV24Compat) {
251
+ // This is a semver-major change. Ref: https://github.com/nodejs/node/pull/46430
252
+ throw new ERR_UNKNOWN_ENCODING(defaultEncoding);
253
+ }
254
+ else {
255
+ this.defaultEncoding = defaultEncoding;
256
+ }
257
+ // Ref the piped dest which we need a drain event on it
258
+ // type: null | Writable | Set<Writable>.
259
+ this.awaitDrainWriters = null;
260
+ if (options?.encoding) {
261
+ this.decoder = new StringDecoder(options.encoding);
262
+ this.encoding = options.encoding;
263
+ }
264
+ }
265
+ ReadableState.prototype[kOnConstructed] = function onConstructed(stream) {
266
+ if ((this[kState] & kNeedReadable) !== 0) {
267
+ maybeReadMore(stream, this);
268
+ }
269
+ };
270
+ // ======================================================================================
271
+ // Readable
272
+ Readable.ReadableState = ReadableState;
273
+ Object.setPrototypeOf(Readable.prototype, Stream.prototype);
274
+ Object.setPrototypeOf(Readable, Stream);
275
+ export function Readable(options) {
276
+ if (!(this instanceof Readable))
277
+ return new Readable(options);
278
+ this._events ??= {
279
+ close: undefined,
280
+ error: undefined,
281
+ data: undefined,
282
+ end: undefined,
283
+ readable: undefined,
284
+ // Skip uncommon events...
285
+ // pause: undefined,
286
+ // resume: undefined,
287
+ // pipe: undefined,
288
+ // unpipe: undefined,
289
+ // [destroyImpl.kConstruct]: undefined,
290
+ // [destroyImpl.kDestroy]: undefined,
291
+ };
292
+ this._readableState = new ReadableState(options, this, false);
293
+ if (options) {
294
+ if (typeof options.read === "function")
295
+ this._read = options.read;
296
+ if (typeof options.destroy === "function")
297
+ this._destroy = options.destroy;
298
+ if (typeof options.construct === "function")
299
+ this._construct = options.construct;
300
+ if (options.signal)
301
+ addAbortSignal(options.signal, this);
302
+ }
303
+ Stream.call(this, options);
304
+ if (this._construct != null) {
305
+ construct(this, () => {
306
+ this._readableState[kOnConstructed](this);
307
+ });
308
+ }
309
+ }
310
+ Readable.prototype.destroy = destroy;
311
+ Readable.prototype._undestroy = undestroy;
312
+ Readable.prototype._destroy = function (err, cb) {
313
+ if (cb)
314
+ cb(err);
315
+ };
316
+ Readable.prototype[EventEmitter.captureRejectionSymbol] = function (err) {
317
+ this.destroy(err);
318
+ };
319
+ Readable.prototype[asyncDispose] = async function () {
320
+ let error;
321
+ if (!this.destroyed) {
322
+ error = this.readableEnded ? null : new AbortError();
323
+ this.destroy(error);
324
+ }
325
+ await new Promise((resolve, reject) => eos(this, (err) => (err && err !== error ? reject(err) : resolve(null))));
326
+ };
327
+ // Manually shove something into the read() buffer.
328
+ // This returns true if the highWaterMark has not been hit yet,
329
+ // similar to how Writable.write() returns true if you should
330
+ // write() some more.
331
+ Readable.prototype.push = function (chunk, encoding) {
332
+ const state = this._readableState;
333
+ return (state[kState] & kObjectMode) === 0
334
+ ? readableAddChunkPushByteMode(this, state, chunk, encoding)
335
+ : readableAddChunkPushObjectMode(this, state, chunk, encoding);
336
+ };
337
+ // Unshift should *always* be something directly out of read().
338
+ Readable.prototype.unshift = function (chunk, encoding) {
339
+ const state = this._readableState;
340
+ return (state[kState] & kObjectMode) === 0
341
+ ? readableAddChunkUnshiftByteMode(this, state, chunk, encoding)
342
+ : readableAddChunkUnshiftObjectMode(this, state, chunk);
343
+ };
344
+ function readableAddChunkUnshiftByteMode(stream, state, chunk, encoding) {
345
+ if (chunk === null) {
346
+ state[kState] &= ~kReading;
347
+ onEofChunk(stream, state);
348
+ return false;
349
+ }
350
+ if (typeof chunk === "string") {
351
+ encoding ||= state.defaultEncoding;
352
+ if (state.encoding !== encoding) {
353
+ if (state.encoding) {
354
+ // When unshifting, if state.encoding is set, we have to save
355
+ // the string in the BufferList with the state encoding.
356
+ chunk = Buffer.from(chunk, encoding).toString(state.encoding);
357
+ }
358
+ else {
359
+ chunk = Buffer.from(chunk, encoding);
360
+ }
361
+ }
362
+ }
363
+ else if (Stream._isArrayBufferView(chunk)) {
364
+ chunk = Stream._uint8ArrayToBuffer(chunk);
365
+ }
366
+ else if (chunk !== undefined && !(chunk instanceof Buffer)) {
367
+ errorOrDestroy(stream, new ERR_INVALID_ARG_TYPE("chunk", ["string", "Buffer", "TypedArray", "DataView"], chunk));
368
+ return false;
369
+ }
370
+ if (!(chunk && chunk.length > 0)) {
371
+ return canPushMore(state);
372
+ }
373
+ return readableAddChunkUnshiftValue(stream, state, chunk);
374
+ }
375
+ function readableAddChunkUnshiftObjectMode(stream, state, chunk) {
376
+ if (chunk === null) {
377
+ state[kState] &= ~kReading;
378
+ onEofChunk(stream, state);
379
+ return false;
380
+ }
381
+ return readableAddChunkUnshiftValue(stream, state, chunk);
382
+ }
383
+ function readableAddChunkUnshiftValue(stream, state, chunk) {
384
+ if ((state[kState] & kEndEmitted) !== 0)
385
+ errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
386
+ else if ((state[kState] & (kDestroyed | kErrored)) !== 0)
387
+ return false;
388
+ else
389
+ addChunk(stream, state, chunk, true);
390
+ return canPushMore(state);
391
+ }
392
+ function readableAddChunkPushByteMode(stream, state, chunk, encoding) {
393
+ if (chunk === null) {
394
+ state[kState] &= ~kReading;
395
+ onEofChunk(stream, state);
396
+ return false;
397
+ }
398
+ if (typeof chunk === "string") {
399
+ encoding ||= state.defaultEncoding;
400
+ if (state.encoding !== encoding) {
401
+ chunk = Buffer.from(chunk, encoding);
402
+ encoding = "";
403
+ }
404
+ }
405
+ else if (chunk instanceof Buffer) {
406
+ encoding = "";
407
+ }
408
+ else if (Stream._isArrayBufferView(chunk)) {
409
+ chunk = Stream._uint8ArrayToBuffer(chunk);
410
+ encoding = "";
411
+ }
412
+ else if (chunk !== undefined) {
413
+ errorOrDestroy(stream, new ERR_INVALID_ARG_TYPE("chunk", ["string", "Buffer", "TypedArray", "DataView"], chunk));
414
+ return false;
415
+ }
416
+ if (!chunk || chunk.length <= 0) {
417
+ state[kState] &= ~kReading;
418
+ maybeReadMore(stream, state);
419
+ return canPushMore(state);
420
+ }
421
+ if ((state[kState] & kEnded) !== 0) {
422
+ errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
423
+ return false;
424
+ }
425
+ if ((state[kState] & (kDestroyed | kErrored)) !== 0) {
426
+ return false;
427
+ }
428
+ state[kState] &= ~kReading;
429
+ if ((state[kState] & kDecoder) !== 0 && !encoding) {
430
+ chunk = state[kDecoderValue].write(chunk);
431
+ if (chunk.length === 0) {
432
+ maybeReadMore(stream, state);
433
+ return canPushMore(state);
434
+ }
435
+ }
436
+ addChunk(stream, state, chunk, false);
437
+ return canPushMore(state);
438
+ }
439
+ function readableAddChunkPushObjectMode(stream, state, chunk, encoding) {
440
+ if (chunk === null) {
441
+ state[kState] &= ~kReading;
442
+ onEofChunk(stream, state);
443
+ return false;
444
+ }
445
+ if ((state[kState] & kEnded) !== 0) {
446
+ errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
447
+ return false;
448
+ }
449
+ if ((state[kState] & (kDestroyed | kErrored)) !== 0) {
450
+ return false;
451
+ }
452
+ state[kState] &= ~kReading;
453
+ if ((state[kState] & kDecoder) !== 0 && !encoding) {
454
+ chunk = state[kDecoderValue].write(chunk);
455
+ }
456
+ addChunk(stream, state, chunk, false);
457
+ return canPushMore(state);
458
+ }
459
+ function canPushMore(state) {
460
+ // We can push more data if we are below the highWaterMark.
461
+ // Also, if we have no data yet, we can stand some more bytes.
462
+ // This is to work around cases where hwm=0, such as the repl.
463
+ return ((state[kState] & kEnded) === 0 &&
464
+ (state.length < state.highWaterMark || state.length === 0));
465
+ }
466
+ function addChunk(stream, state, chunk, addToFront) {
467
+ if ((state[kState] & (kFlowing | kSync | kDataListening)) ===
468
+ (kFlowing | kDataListening) &&
469
+ state.length === 0) {
470
+ // Use the guard to avoid creating `Set()` repeatedly
471
+ // when we have multiple pipes.
472
+ if ((state[kState] & kMultiAwaitDrain) !== 0) {
473
+ state.awaitDrainWriters.clear();
474
+ }
475
+ else {
476
+ state.awaitDrainWriters = null;
477
+ }
478
+ state[kState] |= kDataEmitted;
479
+ stream.emit("data", chunk);
480
+ }
481
+ else {
482
+ // Update the buffer info.
483
+ state.length += (state[kState] & kObjectMode) !== 0 ? 1 : chunk.length;
484
+ if (addToFront) {
485
+ if (state.bufferIndex > 0) {
486
+ state.buffer[--state.bufferIndex] = chunk;
487
+ }
488
+ else {
489
+ state.buffer.unshift(chunk); // Slow path
490
+ }
491
+ }
492
+ else {
493
+ state.buffer.push(chunk);
494
+ }
495
+ if ((state[kState] & kNeedReadable) !== 0)
496
+ emitReadable(stream);
497
+ }
498
+ maybeReadMore(stream, state);
499
+ }
500
+ Readable.prototype.isPaused = function () {
501
+ const state = this._readableState;
502
+ return ((state[kState] & kPaused) !== 0 ||
503
+ (state[kState] & (kHasFlowing | kFlowing)) === kHasFlowing);
504
+ };
505
+ // Backwards compatibility.
506
+ Readable.prototype.setEncoding = function (enc) {
507
+ const state = this._readableState;
508
+ const decoder = new StringDecoder(enc);
509
+ state.decoder = decoder;
510
+ // If setEncoding(null), decoder.encoding equals utf8.
511
+ state.encoding = state.decoder.encoding;
512
+ // Iterate over current buffer to convert already stored Buffers:
513
+ let content = "";
514
+ for (const data of state.buffer.slice(state.bufferIndex)) {
515
+ content += decoder.write(data);
516
+ }
517
+ state.buffer.length = 0;
518
+ state.bufferIndex = 0;
519
+ if (content !== "")
520
+ state.buffer.push(content);
521
+ state.length = content.length;
522
+ return this;
523
+ };
524
+ // Don't raise the hwm > 1GB.
525
+ const MAX_HWM = 0x40000000;
526
+ function computeNewHighWaterMark(n) {
527
+ if (n > MAX_HWM) {
528
+ throw new ERR_OUT_OF_RANGE("size", "<= 1GiB", n);
529
+ }
530
+ else {
531
+ // Get the next highest power of 2 to prevent increasing hwm excessively in
532
+ // tiny amounts.
533
+ n--;
534
+ n |= n >>> 1;
535
+ n |= n >>> 2;
536
+ n |= n >>> 4;
537
+ n |= n >>> 8;
538
+ n |= n >>> 16;
539
+ n++;
540
+ }
541
+ return n;
542
+ }
543
+ // This function is designed to be inlinable, so please take care when making
544
+ // changes to the function body.
545
+ function howMuchToRead(n, state) {
546
+ if (n <= 0 || (state.length === 0 && (state[kState] & kEnded) !== 0))
547
+ return 0;
548
+ if ((state[kState] & kObjectMode) !== 0)
549
+ return 1;
550
+ if (Number.isNaN(n)) {
551
+ // Only flow one buffer at a time.
552
+ if ((state[kState] & kFlowing) !== 0 && state.length)
553
+ return state.buffer[state.bufferIndex].length;
554
+ return state.length;
555
+ }
556
+ if (n <= state.length)
557
+ return n;
558
+ return (state[kState] & kEnded) !== 0 ? state.length : 0;
559
+ }
560
+ // You can override either this method, or the async _read(n) below.
561
+ Readable.prototype.read = function (n) {
562
+ // Same as parseInt(undefined, 10), however V8 7.3 performance regressed
563
+ // in this scenario, so we are doing it manually.
564
+ if (n === undefined) {
565
+ n = NaN;
566
+ }
567
+ else if (!Number.isInteger(n)) {
568
+ n = Number.parseInt(n, 10);
569
+ }
570
+ const state = this._readableState;
571
+ const nOrig = n;
572
+ // If we're asking for more than the current hwm, then raise the hwm.
573
+ if (n > state.highWaterMark)
574
+ state.highWaterMark = computeNewHighWaterMark(n);
575
+ if (n !== 0)
576
+ state[kState] &= ~kEmittedReadable;
577
+ // If we're doing read(0) to trigger a readable event, but we
578
+ // already have a bunch of data in the buffer, then just trigger
579
+ // the 'readable' event and move on.
580
+ if (n === 0 &&
581
+ (state[kState] & kNeedReadable) !== 0 &&
582
+ ((state.highWaterMark !== 0
583
+ ? state.length >= state.highWaterMark
584
+ : state.length > 0) ||
585
+ (state[kState] & kEnded) !== 0)) {
586
+ if (state.length === 0 && (state[kState] & kEnded) !== 0)
587
+ endReadable(this);
588
+ else
589
+ emitReadable(this);
590
+ return null;
591
+ }
592
+ n = howMuchToRead(n, state);
593
+ // If we've ended, and we're now clear, then finish it up.
594
+ if (n === 0 && (state[kState] & kEnded) !== 0) {
595
+ if (state.length === 0)
596
+ endReadable(this);
597
+ return null;
598
+ }
599
+ // All the actual chunk generation logic needs to be
600
+ // *below* the call to _read. The reason is that in certain
601
+ // synthetic stream cases, such as passthrough streams, _read
602
+ // may be a completely synchronous operation which may change
603
+ // the state of the read buffer, providing enough data when
604
+ // before there was *not* enough.
605
+ //
606
+ // So, the steps are:
607
+ // 1. Figure out what the state of things will be after we do
608
+ // a read from the buffer.
609
+ //
610
+ // 2. If that resulting state will trigger a _read, then call _read.
611
+ // Note that this may be asynchronous, or synchronous. Yes, it is
612
+ // deeply ugly to write APIs this way, but that still doesn't mean
613
+ // that the Readable class should behave improperly, as streams are
614
+ // designed to be sync/async agnostic.
615
+ // Take note if the _read call is sync or async (ie, if the read call
616
+ // has returned yet), so that we know whether or not it's safe to emit
617
+ // 'readable' etc.
618
+ //
619
+ // 3. Actually pull the requested chunks out of the buffer and return.
620
+ // if we need a readable event, then we need to do some reading.
621
+ let doRead = (state[kState] & kNeedReadable) !== 0;
622
+ // If we currently have less than the highWaterMark, then also read some.
623
+ if (state.length === 0 || state.length - n < state.highWaterMark) {
624
+ doRead = true;
625
+ }
626
+ // However, if we've ended, then there's no point, if we're already
627
+ // reading, then it's unnecessary, if we're constructing we have to wait,
628
+ // and if we're destroyed or errored, then it's not allowed,
629
+ if ((state[kState] &
630
+ (kReading | kEnded | kDestroyed | kErrored | kConstructed)) !==
631
+ kConstructed) {
632
+ doRead = false;
633
+ }
634
+ else if (doRead) {
635
+ state[kState] |= kReading | kSync;
636
+ // If the length is currently zero, then we *need* a readable event.
637
+ if (state.length === 0)
638
+ state[kState] |= kNeedReadable;
639
+ // Call internal read method
640
+ try {
641
+ this._read(state.highWaterMark);
642
+ }
643
+ catch (err) {
644
+ errorOrDestroy(this, err);
645
+ }
646
+ state[kState] &= ~kSync;
647
+ // If _read pushed data synchronously, then `reading` will be false,
648
+ // and we need to re-evaluate how much data we can return to the user.
649
+ if ((state[kState] & kReading) === 0)
650
+ n = howMuchToRead(nOrig, state);
651
+ }
652
+ let ret;
653
+ if (n > 0)
654
+ ret = fromList(n, state);
655
+ else
656
+ ret = null;
657
+ if (ret === null) {
658
+ state[kState] |= state.length <= state.highWaterMark ? kNeedReadable : 0;
659
+ n = 0;
660
+ }
661
+ else {
662
+ state.length -= n;
663
+ if ((state[kState] & kMultiAwaitDrain) !== 0) {
664
+ state.awaitDrainWriters.clear();
665
+ }
666
+ else {
667
+ state.awaitDrainWriters = null;
668
+ }
669
+ }
670
+ if (state.length === 0) {
671
+ // If we have nothing in the buffer, then we want to know
672
+ // as soon as we *do* get something into the buffer.
673
+ if ((state[kState] & kEnded) === 0)
674
+ state[kState] |= kNeedReadable;
675
+ // If we tried to read() past the EOF, then emit end on the next tick.
676
+ if (nOrig !== n && (state[kState] & kEnded) !== 0)
677
+ endReadable(this);
678
+ }
679
+ if (ret !== null && (state[kState] & (kErrorEmitted | kCloseEmitted)) === 0) {
680
+ state[kState] |= kDataEmitted;
681
+ this.emit("data", ret);
682
+ }
683
+ return ret;
684
+ };
685
+ function onEofChunk(stream, state) {
686
+ if ((state[kState] & kEnded) !== 0)
687
+ return;
688
+ const decoder = (state[kState] & kDecoder) !== 0 ? state[kDecoderValue] : null;
689
+ if (decoder) {
690
+ const chunk = decoder.end();
691
+ if (chunk?.length) {
692
+ state.buffer.push(chunk);
693
+ state.length += (state[kState] & kObjectMode) !== 0 ? 1 : chunk.length;
694
+ }
695
+ }
696
+ state[kState] |= kEnded;
697
+ if ((state[kState] & kSync) !== 0) {
698
+ // If we are sync, wait until next tick to emit the data.
699
+ // Otherwise we risk emitting data in the flow()
700
+ // the readable code triggers during a read() call.
701
+ emitReadable(stream);
702
+ }
703
+ else {
704
+ // Emit 'readable' now to make sure it gets picked up.
705
+ state[kState] &= ~kNeedReadable;
706
+ state[kState] |= kEmittedReadable;
707
+ // We have to emit readable now that we are EOF. Modules
708
+ // in the ecosystem (e.g. dicer) rely on this event being sync.
709
+ emitReadable_(stream);
710
+ }
711
+ }
712
+ // Don't emit readable right away in sync mode, because this can trigger
713
+ // another read() call => stack overflow. This way, it might trigger
714
+ // a nextTick recursion warning, but that's not so bad.
715
+ function emitReadable(stream) {
716
+ const state = stream._readableState;
717
+ state[kState] &= ~kNeedReadable;
718
+ if ((state[kState] & kEmittedReadable) === 0) {
719
+ state[kState] |= kEmittedReadable;
720
+ nextTick(emitReadable_, stream);
721
+ }
722
+ }
723
+ function emitReadable_(stream) {
724
+ const state = stream._readableState;
725
+ if ((state[kState] & (kDestroyed | kErrored)) === 0 &&
726
+ (state.length || (state[kState] & kEnded) !== 0)) {
727
+ stream.emit("readable");
728
+ state[kState] &= ~kEmittedReadable;
729
+ }
730
+ // The stream needs another readable event if:
731
+ // 1. It is not flowing, as the flow mechanism will take
732
+ // care of it.
733
+ // 2. It is not ended.
734
+ // 3. It is below the highWaterMark, so we can schedule
735
+ // another readable later.
736
+ state[kState] |=
737
+ (state[kState] & (kFlowing | kEnded)) === 0 &&
738
+ state.length <= state.highWaterMark
739
+ ? kNeedReadable
740
+ : 0;
741
+ flow(stream);
742
+ }
743
+ // At this point, the user has presumably seen the 'readable' event,
744
+ // and called read() to consume some data. that may have triggered
745
+ // in turn another _read(n) call, in which case reading = true if
746
+ // it's in progress.
747
+ // However, if we're not ended, or reading, and the length < hwm,
748
+ // then go ahead and try to read some more preemptively.
749
+ function maybeReadMore(stream, state) {
750
+ if ((state[kState] & (kReadingMore | kConstructed)) === kConstructed) {
751
+ state[kState] |= kReadingMore;
752
+ nextTick(maybeReadMore_, stream, state);
753
+ }
754
+ }
755
+ function maybeReadMore_(stream, state) {
756
+ // Attempt to read more data if we should.
757
+ //
758
+ // The conditions for reading more data are (one of):
759
+ // - Not enough data buffered (state.length < state.highWaterMark). The loop
760
+ // is responsible for filling the buffer with enough data if such data
761
+ // is available. If highWaterMark is 0 and we are not in the flowing mode
762
+ // we should _not_ attempt to buffer any extra data. We'll get more data
763
+ // when the stream consumer calls read() instead.
764
+ // - No data in the buffer, and the stream is in flowing mode. In this mode
765
+ // the loop below is responsible for ensuring read() is called. Failing to
766
+ // call read here would abort the flow and there's no other mechanism for
767
+ // continuing the flow if the stream consumer has just subscribed to the
768
+ // 'data' event.
769
+ //
770
+ // In addition to the above conditions to keep reading data, the following
771
+ // conditions prevent the data from being read:
772
+ // - The stream has ended (state.ended).
773
+ // - There is already a pending 'read' operation (state.reading). This is a
774
+ // case where the stream has called the implementation defined _read()
775
+ // method, but they are processing the call asynchronously and have _not_
776
+ // called push() with new data. In this case we skip performing more
777
+ // read()s. The execution ends in this method again after the _read() ends
778
+ // up calling push() with more data.
779
+ while ((state[kState] & (kReading | kEnded)) === 0 &&
780
+ (state.length < state.highWaterMark ||
781
+ ((state[kState] & kFlowing) !== 0 && state.length === 0))) {
782
+ const len = state.length;
783
+ stream.read(0);
784
+ if (len === state.length)
785
+ // Didn't get any data, stop spinning.
786
+ break;
787
+ }
788
+ state[kState] &= ~kReadingMore;
789
+ }
790
+ // Abstract method. to be overridden in specific implementation classes.
791
+ // call cb(er, data) where data is <= n in length.
792
+ // for virtual (non-string, non-buffer) streams, "length" is somewhat
793
+ // arbitrary, and perhaps not very meaningful.
794
+ Readable.prototype._read = function (_size) {
795
+ throw new ERR_METHOD_NOT_IMPLEMENTED("_read()");
796
+ };
797
+ Readable.prototype.pipe = function (dest, pipeOpts) {
798
+ const src = this; // eslint-disable-line @typescript-eslint/no-this-alias
799
+ const state = this._readableState;
800
+ if (state.pipes.length === 1) {
801
+ if ((state[kState] & kMultiAwaitDrain) === 0) {
802
+ state[kState] |= kMultiAwaitDrain;
803
+ state.awaitDrainWriters = new Set(state.awaitDrainWriters ? [state.awaitDrainWriters] : []);
804
+ }
805
+ }
806
+ state.pipes.push(dest);
807
+ const doEnd = !pipeOpts || pipeOpts.end !== false;
808
+ const endFn = doEnd ? onend : unpipe;
809
+ if ((state[kState] & kEndEmitted) !== 0)
810
+ nextTick(endFn);
811
+ else
812
+ src.once("end", endFn);
813
+ dest.on("unpipe", onunpipe);
814
+ function onunpipe(readable, unpipeInfo) {
815
+ if (readable === src) {
816
+ if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
817
+ unpipeInfo.hasUnpiped = true;
818
+ cleanup();
819
+ }
820
+ }
821
+ }
822
+ function onend() {
823
+ dest.end();
824
+ }
825
+ let ondrain;
826
+ let cleanedUp = false;
827
+ function cleanup() {
828
+ // Cleanup event handlers once the pipe is broken.
829
+ dest.removeListener("close", onclose);
830
+ dest.removeListener("finish", onfinish);
831
+ if (ondrain) {
832
+ dest.removeListener("drain", ondrain);
833
+ }
834
+ dest.removeListener("error", onerror);
835
+ dest.removeListener("unpipe", onunpipe);
836
+ src.removeListener("end", onend);
837
+ src.removeListener("end", unpipe);
838
+ src.removeListener("data", ondata);
839
+ cleanedUp = true;
840
+ // If the reader is waiting for a drain event from this
841
+ // specific writer, then it would cause it to never start
842
+ // flowing again.
843
+ // So, if this is awaiting a drain, then we just call it now.
844
+ // If we don't know, then assume that we are waiting for one.
845
+ if (ondrain &&
846
+ state.awaitDrainWriters &&
847
+ (!dest._writableState || dest._writableState.needDrain))
848
+ ondrain();
849
+ }
850
+ function pause() {
851
+ // If the user unpiped during `dest.write()`, it is possible
852
+ // to get stuck in a permanently paused state if that write
853
+ // also returned false.
854
+ // => Check whether `dest` is still a piping destination.
855
+ if (!cleanedUp) {
856
+ if (state.pipes.length === 1 && state.pipes[0] === dest) {
857
+ state.awaitDrainWriters = dest;
858
+ state[kState] &= ~kMultiAwaitDrain;
859
+ }
860
+ else if (state.pipes.length > 1 && state.pipes.includes(dest)) {
861
+ state.awaitDrainWriters.add(dest);
862
+ }
863
+ src.pause();
864
+ }
865
+ if (!ondrain) {
866
+ // When the dest drains, it reduces the awaitDrain counter
867
+ // on the source. This would be more elegant with a .once()
868
+ // handler in flow(), but adding and removing repeatedly is
869
+ // too slow.
870
+ ondrain = pipeOnDrain(src, dest);
871
+ dest.on("drain", ondrain);
872
+ }
873
+ }
874
+ src.on("data", ondata);
875
+ function ondata(chunk) {
876
+ // This is a semver-major change. Ref: https://github.com/nodejs/node/pull/55270
877
+ if (streamsNodejsV24Compat) {
878
+ try {
879
+ const ret = dest.write(chunk);
880
+ if (ret === false) {
881
+ pause();
882
+ }
883
+ }
884
+ catch (error) {
885
+ dest.destroy(error);
886
+ }
887
+ }
888
+ else {
889
+ const ret = dest.write(chunk);
890
+ if (ret === false) {
891
+ pause();
892
+ }
893
+ }
894
+ }
895
+ // If the dest has an error, then stop piping into it.
896
+ // However, don't suppress the throwing behavior for this.
897
+ function onerror(er) {
898
+ unpipe();
899
+ dest.removeListener("error", onerror);
900
+ if (dest.listenerCount("error") === 0) {
901
+ const s = dest._writableState || dest._readableState;
902
+ if (s && !s.errorEmitted) {
903
+ // User incorrectly emitted 'error' directly on the stream.
904
+ errorOrDestroy(dest, er);
905
+ }
906
+ else {
907
+ dest.emit("error", er);
908
+ }
909
+ }
910
+ }
911
+ // Make sure our error handler is attached before userland ones.
912
+ dest.prependListener("error", onerror);
913
+ // Both close and finish should trigger unpipe, but only once.
914
+ function onclose() {
915
+ dest.removeListener("finish", onfinish);
916
+ unpipe();
917
+ }
918
+ dest.once("close", onclose);
919
+ function onfinish() {
920
+ dest.removeListener("close", onclose);
921
+ unpipe();
922
+ }
923
+ dest.once("finish", onfinish);
924
+ function unpipe() {
925
+ src.unpipe(dest);
926
+ }
927
+ // Tell the dest that it's being piped to.
928
+ dest.emit("pipe", src);
929
+ // Start the flow if it hasn't been started already.
930
+ if (dest.writableNeedDrain === true) {
931
+ pause();
932
+ }
933
+ else if ((state[kState] & kFlowing) === 0) {
934
+ src.resume();
935
+ }
936
+ return dest;
937
+ };
938
+ function pipeOnDrain(src, dest) {
939
+ return function pipeOnDrainFunctionResult() {
940
+ const state = src._readableState;
941
+ // `ondrain` will call directly,
942
+ // `this` maybe not a reference to dest,
943
+ // so we use the real dest here.
944
+ if (state.awaitDrainWriters === dest) {
945
+ state.awaitDrainWriters = null;
946
+ }
947
+ else if ((state[kState] & kMultiAwaitDrain) !== 0) {
948
+ state.awaitDrainWriters.delete(dest);
949
+ }
950
+ if ((!state.awaitDrainWriters || state.awaitDrainWriters.size === 0) &&
951
+ (state[kState] & kDataListening) !== 0) {
952
+ src.resume();
953
+ }
954
+ };
955
+ }
956
+ Readable.prototype.unpipe = function (dest) {
957
+ const state = this._readableState;
958
+ const unpipeInfo = { hasUnpiped: false };
959
+ // If we're not piping anywhere, then do nothing.
960
+ if (state.pipes.length === 0)
961
+ return this;
962
+ if (!dest) {
963
+ // remove all.
964
+ const dests = state.pipes;
965
+ state.pipes = [];
966
+ this.pause();
967
+ for (let i = 0; i < dests.length; i++)
968
+ dests[i].emit("unpipe", this, { hasUnpiped: false });
969
+ return this;
970
+ }
971
+ // Try to find the right one.
972
+ const index = state.pipes.indexOf(dest);
973
+ if (index === -1)
974
+ return this;
975
+ state.pipes.splice(index, 1);
976
+ if (state.pipes.length === 0)
977
+ this.pause();
978
+ dest.emit("unpipe", this, unpipeInfo);
979
+ return this;
980
+ };
981
+ // Set up data events if they are asked for
982
+ // Ensure readable listeners eventually get something.
983
+ Readable.prototype.on = function (ev, fn) {
984
+ const res = Stream.prototype.on.call(this, ev, fn);
985
+ const state = this._readableState;
986
+ if (ev === "data") {
987
+ state[kState] |= kDataListening;
988
+ // Update readableListening so that resume() may be a no-op
989
+ // a few lines down. This is needed to support once('readable').
990
+ state[kState] |=
991
+ this.listenerCount("readable") > 0 ? kReadableListening : 0;
992
+ // Try start flowing on next tick if stream isn't explicitly paused.
993
+ if ((state[kState] & (kHasFlowing | kFlowing)) !== kHasFlowing) {
994
+ this.resume();
995
+ }
996
+ }
997
+ else if (ev === "readable") {
998
+ if ((state[kState] & (kEndEmitted | kReadableListening)) === 0) {
999
+ state[kState] |= kReadableListening | kNeedReadable | kHasFlowing;
1000
+ state[kState] &= ~(kFlowing | kEmittedReadable);
1001
+ if (state.length) {
1002
+ emitReadable(this);
1003
+ }
1004
+ else if ((state[kState] & kReading) === 0) {
1005
+ nextTick(nReadingNextTick, this);
1006
+ }
1007
+ }
1008
+ }
1009
+ return res;
1010
+ };
1011
+ Readable.prototype.addListener = Readable.prototype.on;
1012
+ Readable.prototype.removeListener = function (ev, fn) {
1013
+ const state = this._readableState;
1014
+ const res = Stream.prototype.removeListener.call(this, ev, fn);
1015
+ if (ev === "readable") {
1016
+ // We need to check if there is someone still listening to
1017
+ // readable and reset the state. However this needs to happen
1018
+ // after readable has been emitted but before I/O (nextTick) to
1019
+ // support once('readable', fn) cycles. This means that calling
1020
+ // resume within the same tick will have no
1021
+ // effect.
1022
+ nextTick(updateReadableListening, this);
1023
+ }
1024
+ else if (ev === "data" && this.listenerCount("data") === 0) {
1025
+ state[kState] &= ~kDataListening;
1026
+ }
1027
+ return res;
1028
+ };
1029
+ Readable.prototype.off = Readable.prototype.removeListener;
1030
+ Readable.prototype.removeAllListeners = function (ev) {
1031
+ const res = Stream.prototype.removeAllListeners.apply(this, arguments);
1032
+ if (ev === "readable" || ev === undefined) {
1033
+ // We need to check if there is someone still listening to
1034
+ // readable and reset the state. However this needs to happen
1035
+ // after readable has been emitted but before I/O (nextTick) to
1036
+ // support once('readable', fn) cycles. This means that calling
1037
+ // resume within the same tick will have no
1038
+ // effect.
1039
+ nextTick(updateReadableListening, this);
1040
+ }
1041
+ return res;
1042
+ };
1043
+ function updateReadableListening(self) {
1044
+ const state = self._readableState;
1045
+ if (self.listenerCount("readable") > 0) {
1046
+ state[kState] |= kReadableListening;
1047
+ }
1048
+ else {
1049
+ state[kState] &= ~kReadableListening;
1050
+ }
1051
+ if ((state[kState] & (kHasPaused | kPaused | kResumeScheduled)) ===
1052
+ (kHasPaused | kResumeScheduled)) {
1053
+ // Flowing needs to be set to true now, otherwise
1054
+ // the upcoming resume will not flow.
1055
+ state[kState] |= kHasFlowing | kFlowing;
1056
+ // Crude way to check if we should resume.
1057
+ }
1058
+ else if ((state[kState] & kDataListening) !== 0) {
1059
+ self.resume();
1060
+ }
1061
+ else if ((state[kState] & kReadableListening) === 0) {
1062
+ state[kState] &= ~(kHasFlowing | kFlowing);
1063
+ }
1064
+ }
1065
+ function nReadingNextTick(self) {
1066
+ self.read(0);
1067
+ }
1068
+ // pause() and resume() are remnants of the legacy readable stream API
1069
+ // If the user uses them, then switch into old mode.
1070
+ Readable.prototype.resume = function () {
1071
+ const state = this._readableState;
1072
+ if ((state[kState] & kFlowing) === 0) {
1073
+ // We flow only if there is no one listening
1074
+ // for readable, but we still have to call
1075
+ // resume().
1076
+ state[kState] |= kHasFlowing;
1077
+ if ((state[kState] & kReadableListening) === 0) {
1078
+ state[kState] |= kFlowing;
1079
+ }
1080
+ else {
1081
+ state[kState] &= ~kFlowing;
1082
+ }
1083
+ resume(this, state);
1084
+ }
1085
+ state[kState] |= kHasPaused;
1086
+ state[kState] &= ~kPaused;
1087
+ return this;
1088
+ };
1089
+ function resume(stream, state) {
1090
+ if ((state[kState] & kResumeScheduled) === 0) {
1091
+ state[kState] |= kResumeScheduled;
1092
+ nextTick(resume_, stream, state);
1093
+ }
1094
+ }
1095
+ function resume_(stream, state) {
1096
+ if ((state[kState] & kReading) === 0) {
1097
+ stream.read(0);
1098
+ }
1099
+ state[kState] &= ~kResumeScheduled;
1100
+ stream.emit("resume");
1101
+ flow(stream);
1102
+ if ((state[kState] & (kFlowing | kReading)) === kFlowing)
1103
+ stream.read(0);
1104
+ }
1105
+ Readable.prototype.pause = function () {
1106
+ const state = this._readableState;
1107
+ if ((state[kState] & (kHasFlowing | kFlowing)) !== kHasFlowing) {
1108
+ state[kState] |= kHasFlowing;
1109
+ state[kState] &= ~kFlowing;
1110
+ this.emit("pause");
1111
+ }
1112
+ state[kState] |= kHasPaused | kPaused;
1113
+ return this;
1114
+ };
1115
+ function flow(stream) {
1116
+ const state = stream._readableState;
1117
+ while ((state[kState] & kFlowing) !== 0 && stream.read() !== null)
1118
+ ;
1119
+ }
1120
+ // Wrap an old-style stream as the async data source.
1121
+ // This is *not* part of the readable stream interface.
1122
+ // It is an ugly unfortunate mess of history.
1123
+ Readable.prototype.wrap = function (stream) {
1124
+ let paused = false;
1125
+ // TODO (ronag): Should this.destroy(err) emit
1126
+ // 'error' on the wrapped stream? Would require
1127
+ // a static factory method, e.g. Readable.wrap(stream).
1128
+ stream.on("data", (chunk) => {
1129
+ if (!this.push(chunk) && stream.pause) {
1130
+ paused = true;
1131
+ stream.pause();
1132
+ }
1133
+ });
1134
+ stream.on("end", () => {
1135
+ this.push(null);
1136
+ });
1137
+ stream.on("error", (err) => {
1138
+ errorOrDestroy(this, err);
1139
+ });
1140
+ stream.on("close", () => {
1141
+ this.destroy();
1142
+ });
1143
+ stream.on("destroy", () => {
1144
+ this.destroy();
1145
+ });
1146
+ this._read = () => {
1147
+ if (paused && stream.resume) {
1148
+ paused = false;
1149
+ stream.resume();
1150
+ }
1151
+ };
1152
+ // Proxy all the other methods. Important when wrapping filters and duplexes.
1153
+ const streamKeys = Object.keys(stream);
1154
+ for (let j = 1; j < streamKeys.length; j++) {
1155
+ const i = streamKeys[j];
1156
+ if (this[i] === undefined && typeof stream[i] === "function") {
1157
+ this[i] = stream[i].bind(stream);
1158
+ }
1159
+ }
1160
+ return this;
1161
+ };
1162
+ Readable.prototype[Symbol.asyncIterator] = function () {
1163
+ return streamToAsyncIterator(this);
1164
+ };
1165
+ Readable.prototype.iterator = function (options) {
1166
+ if (options !== undefined) {
1167
+ validateObject(options, "options");
1168
+ }
1169
+ return streamToAsyncIterator(this, options);
1170
+ };
1171
+ function streamToAsyncIterator(stream, options) {
1172
+ if (typeof stream.read !== "function") {
1173
+ stream = Readable.wrap(stream, { objectMode: true });
1174
+ }
1175
+ const iter = createAsyncIterator(stream, options);
1176
+ iter.stream = stream;
1177
+ return iter;
1178
+ }
1179
+ async function* createAsyncIterator(stream, options) {
1180
+ let callback = nop;
1181
+ function next(resolve) {
1182
+ if (this === stream) {
1183
+ callback();
1184
+ callback = nop;
1185
+ }
1186
+ else {
1187
+ callback = resolve;
1188
+ }
1189
+ }
1190
+ stream.on("readable", next);
1191
+ let error;
1192
+ const cleanup = eos(stream, { writable: false }, (err) => {
1193
+ error = err ? aggregateTwoErrors(error, err) : null;
1194
+ callback();
1195
+ callback = nop;
1196
+ });
1197
+ try {
1198
+ while (true) {
1199
+ const chunk = stream.destroyed ? null : stream.read();
1200
+ if (chunk !== null) {
1201
+ yield chunk;
1202
+ }
1203
+ else if (error) {
1204
+ throw error;
1205
+ }
1206
+ else if (error === null) {
1207
+ return;
1208
+ }
1209
+ else {
1210
+ await new Promise(next);
1211
+ }
1212
+ }
1213
+ }
1214
+ catch (err) {
1215
+ error = aggregateTwoErrors(error, err);
1216
+ throw error;
1217
+ }
1218
+ finally {
1219
+ if ((error || options?.destroyOnReturn !== false) &&
1220
+ (error === undefined || stream._readableState.autoDestroy)) {
1221
+ destroyer(stream, null);
1222
+ }
1223
+ else {
1224
+ stream.off("readable", next);
1225
+ cleanup();
1226
+ }
1227
+ }
1228
+ }
1229
+ // Making it explicit these properties are not enumerable
1230
+ // because otherwise some prototype manipulation in
1231
+ // userland will fail.
1232
+ Object.defineProperties(Readable.prototype, {
1233
+ readable: {
1234
+ __proto__: null,
1235
+ get() {
1236
+ const r = this._readableState;
1237
+ // r.readable === false means that this is part of a Duplex stream
1238
+ // where the readable side was disabled upon construction.
1239
+ // Compat. The user might manually disable readable side through
1240
+ // deprecated setter.
1241
+ return (!!r &&
1242
+ r.readable !== false &&
1243
+ !r.destroyed &&
1244
+ !r.errorEmitted &&
1245
+ !r.endEmitted);
1246
+ },
1247
+ set(val) {
1248
+ // Backwards compat.
1249
+ if (this._readableState) {
1250
+ this._readableState.readable = !!val;
1251
+ }
1252
+ },
1253
+ },
1254
+ readableDidRead: {
1255
+ __proto__: null,
1256
+ enumerable: false,
1257
+ get: function () {
1258
+ return this._readableState.dataEmitted;
1259
+ },
1260
+ },
1261
+ readableAborted: {
1262
+ __proto__: null,
1263
+ enumerable: false,
1264
+ get: function () {
1265
+ return !!(this._readableState.readable !== false &&
1266
+ (this._readableState.destroyed || this._readableState.errored) &&
1267
+ !this._readableState.endEmitted);
1268
+ },
1269
+ },
1270
+ readableHighWaterMark: {
1271
+ __proto__: null,
1272
+ enumerable: false,
1273
+ get: function () {
1274
+ return this._readableState.highWaterMark;
1275
+ },
1276
+ },
1277
+ readableBuffer: {
1278
+ __proto__: null,
1279
+ enumerable: false,
1280
+ get: function () {
1281
+ return this._readableState?.buffer;
1282
+ },
1283
+ },
1284
+ readableFlowing: {
1285
+ __proto__: null,
1286
+ enumerable: false,
1287
+ get: function () {
1288
+ return this._readableState.flowing;
1289
+ },
1290
+ set: function (state) {
1291
+ if (this._readableState) {
1292
+ this._readableState.flowing = state;
1293
+ }
1294
+ },
1295
+ },
1296
+ readableLength: {
1297
+ __proto__: null,
1298
+ enumerable: false,
1299
+ get() {
1300
+ return this._readableState.length;
1301
+ },
1302
+ },
1303
+ readableObjectMode: {
1304
+ __proto__: null,
1305
+ enumerable: false,
1306
+ get() {
1307
+ return this._readableState ? this._readableState.objectMode : false;
1308
+ },
1309
+ },
1310
+ readableEncoding: {
1311
+ __proto__: null,
1312
+ enumerable: false,
1313
+ get() {
1314
+ return this._readableState ? this._readableState.encoding : null;
1315
+ },
1316
+ },
1317
+ errored: {
1318
+ __proto__: null,
1319
+ enumerable: false,
1320
+ get() {
1321
+ return this._readableState ? this._readableState.errored : null;
1322
+ },
1323
+ },
1324
+ closed: {
1325
+ __proto__: null,
1326
+ get() {
1327
+ return this._readableState ? this._readableState.closed : false;
1328
+ },
1329
+ },
1330
+ destroyed: {
1331
+ __proto__: null,
1332
+ enumerable: false,
1333
+ get() {
1334
+ return this._readableState ? this._readableState.destroyed : false;
1335
+ },
1336
+ set(value) {
1337
+ // We ignore the value if the stream
1338
+ // has not been initialized yet.
1339
+ if (!this._readableState) {
1340
+ return;
1341
+ }
1342
+ // Backward compatibility, the user is explicitly
1343
+ // managing destroyed.
1344
+ this._readableState.destroyed = value;
1345
+ },
1346
+ },
1347
+ readableEnded: {
1348
+ __proto__: null,
1349
+ enumerable: false,
1350
+ get() {
1351
+ return this._readableState ? this._readableState.endEmitted : false;
1352
+ },
1353
+ },
1354
+ });
1355
+ Object.defineProperties(ReadableState.prototype, {
1356
+ // Legacy getter for `pipesCount`.
1357
+ pipesCount: {
1358
+ __proto__: null,
1359
+ get() {
1360
+ return this.pipes.length;
1361
+ },
1362
+ },
1363
+ // Legacy property for `paused`.
1364
+ paused: {
1365
+ __proto__: null,
1366
+ get() {
1367
+ return (this[kState] & kPaused) !== 0;
1368
+ },
1369
+ set(value) {
1370
+ this[kState] |= kHasPaused;
1371
+ if (value) {
1372
+ this[kState] |= kPaused;
1373
+ }
1374
+ else {
1375
+ this[kState] &= ~kPaused;
1376
+ }
1377
+ },
1378
+ },
1379
+ });
1380
+ // Exposed for testing purposes only.
1381
+ Readable._fromList = fromList;
1382
+ // Pluck off n bytes from an array of buffers.
1383
+ // Length is the combined lengths of all the buffers in the list.
1384
+ // This function is designed to be inlinable, so please take care when making
1385
+ // changes to the function body.
1386
+ function fromList(n, state) {
1387
+ // nothing buffered.
1388
+ if (state.length === 0)
1389
+ return null;
1390
+ let idx = state.bufferIndex;
1391
+ let ret;
1392
+ const buf = state.buffer;
1393
+ const len = buf.length;
1394
+ if ((state[kState] & kObjectMode) !== 0) {
1395
+ ret = buf[idx];
1396
+ buf[idx++] = null;
1397
+ }
1398
+ else if (!n || n >= state.length) {
1399
+ // Read it all, truncate the list.
1400
+ if ((state[kState] & kDecoder) !== 0) {
1401
+ ret = "";
1402
+ while (idx < len) {
1403
+ ret += buf[idx];
1404
+ buf[idx++] = null;
1405
+ }
1406
+ }
1407
+ else if (len - idx === 0) {
1408
+ ret = Buffer.alloc(0);
1409
+ }
1410
+ else if (len - idx === 1) {
1411
+ ret = buf[idx];
1412
+ buf[idx++] = null;
1413
+ }
1414
+ else {
1415
+ ret = Buffer.allocUnsafe(state.length);
1416
+ let i = 0;
1417
+ while (idx < len) {
1418
+ ret.set(buf[idx], i);
1419
+ i += buf[idx].length;
1420
+ buf[idx++] = null;
1421
+ }
1422
+ }
1423
+ }
1424
+ else if (n < buf[idx].length) {
1425
+ // `slice` is the same for buffers and strings.
1426
+ ret = buf[idx].slice(0, n);
1427
+ buf[idx] = buf[idx].slice(n);
1428
+ }
1429
+ else if (n === buf[idx].length) {
1430
+ // First chunk is a perfect match.
1431
+ ret = buf[idx];
1432
+ buf[idx++] = null;
1433
+ }
1434
+ else if ((state[kState] & kDecoder) !== 0) {
1435
+ ret = "";
1436
+ while (idx < len) {
1437
+ const str = buf[idx];
1438
+ if (n > str.length) {
1439
+ ret += str;
1440
+ n -= str.length;
1441
+ buf[idx++] = null;
1442
+ }
1443
+ else {
1444
+ if (n === buf.length) {
1445
+ ret += str;
1446
+ buf[idx++] = null;
1447
+ }
1448
+ else {
1449
+ ret += str.slice(0, n);
1450
+ buf[idx] = str.slice(n);
1451
+ }
1452
+ break;
1453
+ }
1454
+ }
1455
+ }
1456
+ else {
1457
+ ret = Buffer.allocUnsafe(n);
1458
+ const retLen = n;
1459
+ while (idx < len) {
1460
+ const data = buf[idx];
1461
+ if (n > data.length) {
1462
+ ret.set(data, retLen - n);
1463
+ n -= data.length;
1464
+ buf[idx++] = null;
1465
+ }
1466
+ else {
1467
+ if (n === data.length) {
1468
+ ret.set(data, retLen - n);
1469
+ buf[idx++] = null;
1470
+ }
1471
+ else {
1472
+ ret.set(Buffer.from(data.buffer, data.byteOffset, n), retLen - n);
1473
+ buf[idx] = Buffer.from(data.buffer, data.byteOffset + n, data.length - n);
1474
+ }
1475
+ break;
1476
+ }
1477
+ }
1478
+ }
1479
+ if (idx === len) {
1480
+ state.buffer.length = 0;
1481
+ state.bufferIndex = 0;
1482
+ }
1483
+ else if (idx > 1024) {
1484
+ state.buffer.splice(0, idx);
1485
+ state.bufferIndex = 0;
1486
+ }
1487
+ else {
1488
+ state.bufferIndex = idx;
1489
+ }
1490
+ return ret;
1491
+ }
1492
+ function endReadable(stream) {
1493
+ const state = stream._readableState;
1494
+ if ((state[kState] & kEndEmitted) === 0) {
1495
+ state[kState] |= kEnded;
1496
+ nextTick(endReadableNT, state, stream);
1497
+ }
1498
+ }
1499
+ function endReadableNT(state, stream) {
1500
+ // Check that we didn't get one last unshift.
1501
+ if ((state[kState] & (kErrored | kCloseEmitted | kEndEmitted)) === 0 &&
1502
+ state.length === 0) {
1503
+ state[kState] |= kEndEmitted;
1504
+ stream.emit("end");
1505
+ if (stream.writable && stream.allowHalfOpen === false) {
1506
+ nextTick(endWritableNT, stream);
1507
+ }
1508
+ else if (state.autoDestroy) {
1509
+ // In case of duplex streams we need a way to detect
1510
+ // if the writable side is ready for autoDestroy as well.
1511
+ const wState = stream._writableState;
1512
+ const autoDestroy = !wState ||
1513
+ (wState.autoDestroy &&
1514
+ // We don't expect the writable to ever 'finish'
1515
+ // if writable is explicitly set to false.
1516
+ (wState.finished || wState.writable === false));
1517
+ if (autoDestroy) {
1518
+ stream.destroy();
1519
+ }
1520
+ }
1521
+ }
1522
+ }
1523
+ function endWritableNT(stream) {
1524
+ const writable = stream.writable && !stream.writableEnded && !stream.destroyed;
1525
+ if (writable) {
1526
+ stream.end();
1527
+ }
1528
+ }
1529
+ export function fromWeb(readableStream, options) {
1530
+ return newStreamReadableFromReadableStream(readableStream, options);
1531
+ }
1532
+ export function toWeb(streamReadable, options) {
1533
+ return newReadableStreamFromStreamReadable(streamReadable, options);
1534
+ }
1535
+ export function wrap(src, options) {
1536
+ let _ref, _src$readableObjectMo;
1537
+ return new Readable({
1538
+ objectMode: (_ref =
1539
+ (_src$readableObjectMo = src.readableObjectMode) !== null &&
1540
+ _src$readableObjectMo !== undefined
1541
+ ? _src$readableObjectMo
1542
+ : src.objectMode) !== null && _ref !== undefined
1543
+ ? _ref
1544
+ : true,
1545
+ ...options,
1546
+ destroy(err, callback) {
1547
+ destroyer(src, err);
1548
+ callback(err);
1549
+ },
1550
+ }).wrap(src);
1551
+ }
1552
+ Readable.toWeb = toWeb;
1553
+ Readable.fromWeb = fromWeb;
1554
+ Readable.wrap = wrap;
1555
+ // ======================================================================================
1556
+ //
1557
+ Readable.from = function (iterable, opts) {
1558
+ return from(Readable, iterable, opts);
1559
+ };
1560
+ export function from(Readable, iterable, opts) {
1561
+ let iterator;
1562
+ if (typeof iterable === "string" || iterable instanceof Buffer) {
1563
+ return new Readable({
1564
+ objectMode: true,
1565
+ ...opts,
1566
+ read() {
1567
+ this.push(iterable);
1568
+ this.push(null);
1569
+ },
1570
+ });
1571
+ }
1572
+ let isAsync;
1573
+ if (iterable && iterable[Symbol.asyncIterator]) {
1574
+ isAsync = true;
1575
+ iterator = iterable[Symbol.asyncIterator]();
1576
+ }
1577
+ else if (iterable && iterable[Symbol.iterator]) {
1578
+ isAsync = false;
1579
+ iterator = iterable[Symbol.iterator]();
1580
+ }
1581
+ else {
1582
+ throw new ERR_INVALID_ARG_TYPE("iterable", ["Iterable"], iterable);
1583
+ }
1584
+ const readable = new Readable({
1585
+ objectMode: true,
1586
+ highWaterMark: 1,
1587
+ // TODO(ronag): What options should be allowed?
1588
+ ...opts,
1589
+ });
1590
+ // Flag to protect against _read
1591
+ // being called before last iteration completion.
1592
+ let reading = false;
1593
+ readable._read = function () {
1594
+ if (!reading) {
1595
+ reading = true;
1596
+ next();
1597
+ }
1598
+ };
1599
+ readable._destroy = function (error, cb) {
1600
+ close(error).then(() => nextTick(cb, error), (err) => nextTick(cb, err || error));
1601
+ };
1602
+ async function close(error) {
1603
+ const hadError = error !== undefined && error !== null;
1604
+ const hasThrow = typeof iterator.throw === "function";
1605
+ if (hadError && hasThrow) {
1606
+ const { value, done } = await iterator.throw(error);
1607
+ await value;
1608
+ if (done) {
1609
+ return;
1610
+ }
1611
+ }
1612
+ if (typeof iterator.return === "function") {
1613
+ const { value } = await iterator.return();
1614
+ await value;
1615
+ }
1616
+ }
1617
+ async function next() {
1618
+ for (;;) {
1619
+ try {
1620
+ const { value, done } = isAsync
1621
+ ? await iterator.next()
1622
+ : iterator.next();
1623
+ if (done) {
1624
+ readable.push(null);
1625
+ }
1626
+ else {
1627
+ const res = value && typeof value.then === "function" ? await value : value;
1628
+ if (res === null) {
1629
+ reading = false;
1630
+ throw new ERR_STREAM_NULL_VALUES();
1631
+ }
1632
+ else if (readable.push(res)) {
1633
+ continue;
1634
+ }
1635
+ else {
1636
+ reading = false;
1637
+ }
1638
+ }
1639
+ }
1640
+ catch (err) {
1641
+ readable.destroy(err);
1642
+ }
1643
+ break;
1644
+ }
1645
+ }
1646
+ return readable;
1647
+ }
1648
+ // ======================================================================================
1649
+ // Operators
1650
+ const kWeakHandler = Symbol("kWeak");
1651
+ const kEmpty = Symbol("kEmpty");
1652
+ const kEof = Symbol("kEof");
1653
+ function map(fn, options) {
1654
+ if (typeof fn !== "function") {
1655
+ throw new ERR_INVALID_ARG_TYPE("fn", ["Function", "AsyncFunction"], fn);
1656
+ }
1657
+ if (options != null) {
1658
+ validateObject(options, "options", options);
1659
+ }
1660
+ if (options?.signal != null) {
1661
+ validateAbortSignal(options.signal, "options.signal");
1662
+ }
1663
+ let concurrency = 1;
1664
+ if (options?.concurrency != null) {
1665
+ concurrency = Math.floor(options.concurrency);
1666
+ }
1667
+ validateInteger(concurrency, "concurrency", 1);
1668
+ return async function* map() {
1669
+ let _options$signal, _options$signal2;
1670
+ const ac = new globalThis.AbortController();
1671
+ const stream = this; // eslint-disable-line @typescript-eslint/no-this-alias
1672
+ const queue = [];
1673
+ const signal = ac.signal;
1674
+ const signalOpt = {
1675
+ signal,
1676
+ };
1677
+ const abort = () => ac.abort();
1678
+ if (options != null &&
1679
+ (_options$signal = options.signal) !== null &&
1680
+ _options$signal !== undefined &&
1681
+ _options$signal.aborted) {
1682
+ abort();
1683
+ }
1684
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
1685
+ options == null
1686
+ ? undefined
1687
+ : (_options$signal2 = options.signal) === null ||
1688
+ _options$signal2 === undefined
1689
+ ? undefined
1690
+ : _options$signal2.addEventListener("abort", abort);
1691
+ let next;
1692
+ let resume;
1693
+ let done = false;
1694
+ function onDone() {
1695
+ done = true;
1696
+ }
1697
+ async function pump() {
1698
+ try {
1699
+ for await (let val of stream) {
1700
+ let _val;
1701
+ if (done) {
1702
+ return;
1703
+ }
1704
+ if (signal.aborted) {
1705
+ throw new AbortError();
1706
+ }
1707
+ try {
1708
+ val = fn(val, signalOpt);
1709
+ }
1710
+ catch (err) {
1711
+ val = Promise.reject(err);
1712
+ }
1713
+ if (val === kEmpty) {
1714
+ continue;
1715
+ }
1716
+ if (typeof ((_val = val) === null || _val === undefined
1717
+ ? undefined
1718
+ : _val.catch) === "function") {
1719
+ val.catch(onDone);
1720
+ }
1721
+ queue.push(val);
1722
+ if (next) {
1723
+ next();
1724
+ next = null;
1725
+ }
1726
+ if (!done && queue.length && queue.length >= concurrency) {
1727
+ await new Promise((resolve) => {
1728
+ resume = resolve;
1729
+ });
1730
+ }
1731
+ }
1732
+ queue.push(kEof);
1733
+ }
1734
+ catch (err) {
1735
+ const val = Promise.reject(err);
1736
+ val.then(undefined, onDone);
1737
+ queue.push(val);
1738
+ }
1739
+ finally {
1740
+ let _options$signal3;
1741
+ done = true;
1742
+ if (next) {
1743
+ next();
1744
+ next = null;
1745
+ }
1746
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
1747
+ options == null
1748
+ ? undefined
1749
+ : (_options$signal3 = options.signal) === null ||
1750
+ _options$signal3 === undefined
1751
+ ? undefined
1752
+ : _options$signal3.removeEventListener("abort", abort);
1753
+ }
1754
+ }
1755
+ pump();
1756
+ try {
1757
+ while (true) {
1758
+ while (queue.length > 0) {
1759
+ const val = await queue[0];
1760
+ if (val === kEof) {
1761
+ return;
1762
+ }
1763
+ if (signal.aborted) {
1764
+ throw new AbortError();
1765
+ }
1766
+ if (val !== kEmpty) {
1767
+ yield val;
1768
+ }
1769
+ queue.shift();
1770
+ if (resume) {
1771
+ resume();
1772
+ resume = null;
1773
+ }
1774
+ }
1775
+ await new Promise((resolve) => {
1776
+ next = resolve;
1777
+ });
1778
+ }
1779
+ }
1780
+ finally {
1781
+ ac.abort();
1782
+ done = true;
1783
+ if (resume) {
1784
+ resume();
1785
+ resume = null;
1786
+ }
1787
+ }
1788
+ }.call(this);
1789
+ }
1790
+ function asIndexedPairs(options) {
1791
+ if (options != null) {
1792
+ validateObject(options, "options", options);
1793
+ }
1794
+ if ((options == null ? undefined : options.signal) != null) {
1795
+ validateAbortSignal(options.signal, "options.signal");
1796
+ }
1797
+ return async function* asIndexedPairs() {
1798
+ let index = 0;
1799
+ for await (const val of this) {
1800
+ let _options$signal4;
1801
+ if (options !== null &&
1802
+ options !== undefined &&
1803
+ (_options$signal4 = options.signal) !== null &&
1804
+ _options$signal4 !== undefined &&
1805
+ _options$signal4.aborted) {
1806
+ throw new AbortError("Aborted", {
1807
+ cause: options.signal?.reason,
1808
+ });
1809
+ }
1810
+ yield [index++, val];
1811
+ }
1812
+ }.call(this);
1813
+ }
1814
+ async function some(fn, options) {
1815
+ if (typeof fn !== "function") {
1816
+ throw new ERR_INVALID_ARG_TYPE("fn", ["Function", "AsyncFunction"], fn);
1817
+ }
1818
+ for await (const _ of filter.call(this, fn, options)) {
1819
+ return true;
1820
+ }
1821
+ return false;
1822
+ }
1823
+ async function every(fn, options) {
1824
+ if (typeof fn !== "function") {
1825
+ throw new ERR_INVALID_ARG_TYPE("fn", ["Function", "AsyncFunction"], fn);
1826
+ }
1827
+ // https://en.wikipedia.org/wiki/De_Morgan%27s_laws
1828
+ return !(await some.call(this, async (...args) => {
1829
+ return !(await fn(...args));
1830
+ }, options));
1831
+ }
1832
+ async function find(fn, options) {
1833
+ for await (const result of filter.call(this, fn, options)) {
1834
+ return result;
1835
+ }
1836
+ return undefined;
1837
+ }
1838
+ async function forEach(fn, options) {
1839
+ if (typeof fn !== "function") {
1840
+ throw new ERR_INVALID_ARG_TYPE("fn", ["Function", "AsyncFunction"], fn);
1841
+ }
1842
+ async function forEachFn(value, options) {
1843
+ await fn(value, options);
1844
+ return kEmpty;
1845
+ }
1846
+ for await (const _ of map.call(this, forEachFn, options))
1847
+ ;
1848
+ }
1849
+ function filter(fn, options) {
1850
+ if (typeof fn !== "function") {
1851
+ throw new ERR_INVALID_ARG_TYPE("fn", ["Function", "AsyncFunction"], fn);
1852
+ }
1853
+ async function filterFn(value, options) {
1854
+ if (await fn(value, options)) {
1855
+ return value;
1856
+ }
1857
+ return kEmpty;
1858
+ }
1859
+ return map.call(this, filterFn, options);
1860
+ }
1861
+ // Specific to provide better error to reduce since the argument is only
1862
+ // missing if the stream has no items in it - but the code is still appropriate
1863
+ class ReduceAwareErrMissingArgs extends ERR_MISSING_ARGS {
1864
+ constructor() {
1865
+ super("reduce");
1866
+ this.message = "Reduce of an empty stream requires an initial value";
1867
+ }
1868
+ }
1869
+ async function reduce(reducer, initialValue, options) {
1870
+ let _options$signal5;
1871
+ if (typeof reducer !== "function") {
1872
+ throw new ERR_INVALID_ARG_TYPE("reducer", ["Function", "AsyncFunction"], reducer);
1873
+ }
1874
+ if (options != null) {
1875
+ validateObject(options, "options", options);
1876
+ }
1877
+ if (options?.signal != null) {
1878
+ validateAbortSignal(options?.signal, "options.signal");
1879
+ }
1880
+ let hasInitialValue = arguments.length > 1;
1881
+ if (options !== null &&
1882
+ options !== undefined &&
1883
+ (_options$signal5 = options.signal) !== null &&
1884
+ _options$signal5 !== undefined &&
1885
+ _options$signal5.aborted) {
1886
+ const err = new AbortError(undefined, {
1887
+ cause: options.signal?.reason,
1888
+ });
1889
+ this.once("error", () => { }); // The error is already propagated
1890
+ await finished(this.destroy(err));
1891
+ throw err;
1892
+ }
1893
+ const ac = new globalThis.AbortController();
1894
+ const signal = ac.signal;
1895
+ if (options?.signal) {
1896
+ const opts = {
1897
+ once: true,
1898
+ [kWeakHandler]: this,
1899
+ };
1900
+ options.signal.addEventListener("abort", () => ac.abort(), opts);
1901
+ }
1902
+ let gotAnyItemFromStream = false;
1903
+ try {
1904
+ for await (const value of this) {
1905
+ let _options$signal6;
1906
+ gotAnyItemFromStream = true;
1907
+ if (options !== null &&
1908
+ options !== undefined &&
1909
+ (_options$signal6 = options.signal) !== null &&
1910
+ _options$signal6 !== undefined &&
1911
+ _options$signal6.aborted) {
1912
+ throw new AbortError();
1913
+ }
1914
+ if (!hasInitialValue) {
1915
+ initialValue = value;
1916
+ hasInitialValue = true;
1917
+ }
1918
+ else {
1919
+ initialValue = await reducer(initialValue, value, {
1920
+ signal,
1921
+ });
1922
+ }
1923
+ }
1924
+ if (!gotAnyItemFromStream && !hasInitialValue) {
1925
+ throw new ReduceAwareErrMissingArgs();
1926
+ }
1927
+ }
1928
+ finally {
1929
+ ac.abort();
1930
+ }
1931
+ return initialValue;
1932
+ }
1933
+ async function toArray(options) {
1934
+ if (options != null) {
1935
+ validateObject(options, "options", options);
1936
+ }
1937
+ if (options?.signal != null) {
1938
+ validateAbortSignal(options?.signal, "options.signal");
1939
+ }
1940
+ const result = [];
1941
+ for await (const val of this) {
1942
+ let _options$signal7;
1943
+ if (options !== null &&
1944
+ options !== undefined &&
1945
+ (_options$signal7 = options.signal) !== null &&
1946
+ _options$signal7 !== undefined &&
1947
+ _options$signal7.aborted) {
1948
+ throw new AbortError(undefined, {
1949
+ cause: options.signal?.reason,
1950
+ });
1951
+ }
1952
+ result.push(val);
1953
+ }
1954
+ return result;
1955
+ }
1956
+ function flatMap(fn, options) {
1957
+ const values = map.call(this, fn, options);
1958
+ return async function* flatMap() {
1959
+ for await (const val of values) {
1960
+ yield* val;
1961
+ }
1962
+ }.call(this);
1963
+ }
1964
+ function toIntegerOrInfinity(number) {
1965
+ // We coerce here to align with the spec
1966
+ // https://github.com/tc39/proposal-iterator-helpers/issues/169
1967
+ number = Number(number);
1968
+ if (Number.isNaN(number)) {
1969
+ return 0;
1970
+ }
1971
+ if (number < 0) {
1972
+ throw new ERR_OUT_OF_RANGE("number", ">= 0", number);
1973
+ }
1974
+ return number;
1975
+ }
1976
+ function drop(number, options) {
1977
+ if (options != null) {
1978
+ validateObject(options, "options", options);
1979
+ }
1980
+ if (options?.signal != null) {
1981
+ validateAbortSignal(options?.signal, "options.signal");
1982
+ }
1983
+ number = toIntegerOrInfinity(number);
1984
+ return async function* drop() {
1985
+ let _options$signal8;
1986
+ if (options !== null &&
1987
+ options !== undefined &&
1988
+ (_options$signal8 = options.signal) !== null &&
1989
+ _options$signal8 !== undefined &&
1990
+ _options$signal8.aborted) {
1991
+ throw new AbortError();
1992
+ }
1993
+ for await (const val of this) {
1994
+ let _options$signal9;
1995
+ if (options !== null &&
1996
+ options !== undefined &&
1997
+ (_options$signal9 = options.signal) !== null &&
1998
+ _options$signal9 !== undefined &&
1999
+ _options$signal9.aborted) {
2000
+ throw new AbortError();
2001
+ }
2002
+ if (number-- <= 0) {
2003
+ yield val;
2004
+ }
2005
+ }
2006
+ }.call(this);
2007
+ }
2008
+ function take(number, options) {
2009
+ if (options != null) {
2010
+ validateObject(options, "options", options);
2011
+ }
2012
+ if (options?.signal != null) {
2013
+ validateAbortSignal(options?.signal, "options.signal");
2014
+ }
2015
+ number = toIntegerOrInfinity(number);
2016
+ return async function* take() {
2017
+ let _options$signal10;
2018
+ if (options !== null &&
2019
+ options !== undefined &&
2020
+ (_options$signal10 = options.signal) !== null &&
2021
+ _options$signal10 !== undefined &&
2022
+ _options$signal10.aborted) {
2023
+ throw new AbortError();
2024
+ }
2025
+ for await (const val of this) {
2026
+ let _options$signal11;
2027
+ if (options !== null &&
2028
+ options !== undefined &&
2029
+ (_options$signal11 = options.signal) !== null &&
2030
+ _options$signal11 !== undefined &&
2031
+ _options$signal11.aborted) {
2032
+ throw new AbortError();
2033
+ }
2034
+ if (number-- > 0) {
2035
+ yield val;
2036
+ }
2037
+ else {
2038
+ return;
2039
+ }
2040
+ }
2041
+ }.call(this);
2042
+ }
2043
+ Readable.prototype.map = function (fn, options) {
2044
+ return from(Readable, map.call(this, fn, options));
2045
+ };
2046
+ Readable.prototype.asIndexedPairs = function (options) {
2047
+ return from(Readable, asIndexedPairs.call(this, options));
2048
+ };
2049
+ Readable.prototype.drop = function (number, options) {
2050
+ return from(Readable, drop.call(this, number, options));
2051
+ };
2052
+ Readable.prototype.filter = function (fn, options) {
2053
+ return from(Readable, filter.call(this, fn, options));
2054
+ };
2055
+ Readable.prototype.flatMap = function (fn, options) {
2056
+ return from(Readable, flatMap.call(this, fn, options));
2057
+ };
2058
+ Readable.prototype.take = function (number, options) {
2059
+ return from(Readable, take.call(this, number, options));
2060
+ };
2061
+ Readable.prototype.every = every;
2062
+ Readable.prototype.forEach = forEach;
2063
+ Readable.prototype.reduce = reduce;
2064
+ Readable.prototype.toArray = toArray;
2065
+ Readable.prototype.some = some;
2066
+ Readable.prototype.find = find;
2067
+ /**
2068
+ * @typedef {import('./queuingstrategies').QueuingStrategy} QueuingStrategy
2069
+ * @param {Readable} streamReadable
2070
+ * @param {{
2071
+ * strategy : QueuingStrategy
2072
+ * }} [options]
2073
+ * @returns {ReadableStream}
2074
+ */
2075
+ // The web streams each node readable has been adapted to (one per
2076
+ // Readable.toWeb() over it), kept alive by the node readable. The C++
2077
+ // streams implementation's controller does not keep its stream alive: once
2078
+ // the stream is collected, enqueue() drops the chunk and desiredSize stays
2079
+ // put, so a source holding only the controller (this adapter's 'data'
2080
+ // listener) would never be paused, and a reader awaiting a read could be
2081
+ // collected with the stream. The entries live exactly as long as the node
2082
+ // readable does.
2083
+ const adaptedReadableStreams = new WeakMap();
2084
+ export function newReadableStreamFromStreamReadable(streamReadable, options = {}, createTypeBytes = false) {
2085
+ // Not using the internal/streams/utils isReadableNodeStream utility
2086
+ // here because it will return false if streamReadable is a Duplex
2087
+ // whose readable option is false. For a Duplex that is not readable,
2088
+ // we want it to pass this check but return a closed ReadableStream.
2089
+ if (typeof streamReadable?._readableState !== "object") {
2090
+ throw new ERR_INVALID_ARG_TYPE("streamReadable", "stream.Readable", streamReadable);
2091
+ }
2092
+ if (isDestroyed(streamReadable) || !isReadable(streamReadable)) {
2093
+ const readable = new globalThis.ReadableStream();
2094
+ readable.cancel();
2095
+ return readable;
2096
+ }
2097
+ const objectMode = streamReadable.readableObjectMode;
2098
+ const highWaterMark = streamReadable.readableHighWaterMark;
2099
+ const evaluateStrategyOrFallback = (strategy) => {
2100
+ // If there is a strategy available, use it
2101
+ if (strategy)
2102
+ return strategy;
2103
+ if (objectMode) {
2104
+ // When running in objectMode explicitly but no strategy, we just fall
2105
+ // back to CountQueuingStrategy
2106
+ return new globalThis.CountQueuingStrategy({ highWaterMark });
2107
+ }
2108
+ return new globalThis.ByteLengthQueuingStrategy({ highWaterMark });
2109
+ };
2110
+ const strategy = evaluateStrategyOrFallback(options?.strategy);
2111
+ let controller;
2112
+ let wasCanceled = false;
2113
+ function onData(chunk) {
2114
+ // Copy the Buffer to detach it from the pool.
2115
+ if (Buffer.isBuffer(chunk) && !objectMode)
2116
+ chunk = new Uint8Array(chunk);
2117
+ // An enqueue that fails — the strategy's size() throwing or returning
2118
+ // an invalid size, or the stream having just been cancelled from an
2119
+ // earlier 'data' listener — has errored the stream (or found it
2120
+ // closed); thrown from here it would escape the source's 'data'
2121
+ // emission as an uncaught exception. The source has no consumer left:
2122
+ // destroy it with the error (a no-op for a source the cancel already
2123
+ // destroyed).
2124
+ try {
2125
+ controller.enqueue(chunk);
2126
+ }
2127
+ catch (err) {
2128
+ destroyer(streamReadable, err);
2129
+ return;
2130
+ }
2131
+ if (controller.desiredSize <= 0)
2132
+ streamReadable.pause();
2133
+ }
2134
+ // The web stream is constructed before the source is touched: its
2135
+ // constructor rejects an invalid high-water mark, and a throw there must
2136
+ // leave the source as it was, not paused with this adapter's listeners
2137
+ // on it. The first pull() comes no earlier than a later microtask.
2138
+ const stream = new globalThis.ReadableStream({
2139
+ start(c) {
2140
+ controller = c;
2141
+ },
2142
+ pull() {
2143
+ streamReadable.resume();
2144
+ },
2145
+ cancel(reason) {
2146
+ wasCanceled = true;
2147
+ destroyer(streamReadable, reason);
2148
+ },
2149
+ type: createTypeBytes ? "bytes" : undefined,
2150
+ }, strategy);
2151
+ streamReadable.pause();
2152
+ // The ReadableStream mirrors the readable side only: when the source is a
2153
+ // Duplex, its EOF closes the stream without waiting for a half-open
2154
+ // writable side to finish as well.
2155
+ const cleanup = eos(streamReadable, { writable: false }, (error) => {
2156
+ error = handleKnownInternalErrors(error);
2157
+ cleanup();
2158
+ // This is a protection against non-standard, legacy streams
2159
+ // that happen to emit an error event again after finished is called.
2160
+ streamReadable.on("error", () => { });
2161
+ if (error)
2162
+ return controller.error(error);
2163
+ // Was already canceled
2164
+ if (wasCanceled) {
2165
+ return;
2166
+ }
2167
+ controller.close();
2168
+ });
2169
+ streamReadable.on("data", onData);
2170
+ let adapted = adaptedReadableStreams.get(streamReadable);
2171
+ if (adapted === undefined) {
2172
+ adapted = new Set();
2173
+ adaptedReadableStreams.set(streamReadable, adapted);
2174
+ }
2175
+ adapted.add(stream);
2176
+ return stream;
2177
+ }
2178
+ /**
2179
+ * @param {ReadableStream} readableStream
2180
+ * @param {{
2181
+ * highWaterMark? : number,
2182
+ * encoding? : string,
2183
+ * objectMode? : boolean,
2184
+ * signal? : AbortSignal,
2185
+ * }} [options]
2186
+ * @returns {Readable}
2187
+ */
2188
+ export function newStreamReadableFromReadableStream(readableStream, options = {}) {
2189
+ if (!isReadableStream(readableStream)) {
2190
+ throw new ERR_INVALID_ARG_TYPE("readableStream", "ReadableStream", readableStream);
2191
+ }
2192
+ validateObject(options, "options");
2193
+ const { highWaterMark, encoding, objectMode = false, signal } = options;
2194
+ if (encoding !== undefined && !Buffer.isEncoding(encoding))
2195
+ throw new ERR_INVALID_ARG_VALUE("options.encoding", encoding);
2196
+ validateBoolean(objectMode, "options.objectMode");
2197
+ const reader = readableStream.getReader();
2198
+ let closed = false;
2199
+ const readable = new Readable({
2200
+ objectMode,
2201
+ highWaterMark,
2202
+ encoding,
2203
+ signal,
2204
+ read() {
2205
+ reader.read().then((chunk) => {
2206
+ if (chunk.done) {
2207
+ // Value should always be undefined here.
2208
+ readable.push(null);
2209
+ return;
2210
+ }
2211
+ // A chunk the Readable cannot take (a view over a detached
2212
+ // ArrayBuffer cannot become a Buffer) fails it; the throw would
2213
+ // otherwise be lost in this promise and the stream never read
2214
+ // again.
2215
+ try {
2216
+ readable.push(chunk.value);
2217
+ }
2218
+ catch (error) {
2219
+ destroy.call(readable, error);
2220
+ }
2221
+ }, (error) => destroy.call(readable, error));
2222
+ },
2223
+ destroy(error, callback) {
2224
+ function done() {
2225
+ try {
2226
+ callback(error);
2227
+ }
2228
+ catch (error) {
2229
+ // In a next tick because this is happening within
2230
+ // a promise context, and if there are any errors
2231
+ // thrown we don't want those to cause an unhandled
2232
+ // rejection. Let's just escape the promise and
2233
+ // handle it separately.
2234
+ nextTick(() => {
2235
+ throw error;
2236
+ });
2237
+ }
2238
+ }
2239
+ if (!closed) {
2240
+ reader.cancel(error).then(done, done);
2241
+ return;
2242
+ }
2243
+ done();
2244
+ },
2245
+ });
2246
+ try {
2247
+ reader.closed.then(() => {
2248
+ closed = true;
2249
+ }, (error) => {
2250
+ closed = true;
2251
+ destroy.call(readable, error);
2252
+ });
2253
+ }
2254
+ catch (err) {
2255
+ // A Promise.prototype.then that throws (the adapter calls the live one):
2256
+ // the caller gets the throw, not a stream left locked for a Readable
2257
+ // nobody will have. That Readable is destroyed quietly first (the web
2258
+ // stream, marked closed here, is not cancelled), so that the release —
2259
+ // which rejects the reader's closed promise, possibly into a handler
2260
+ // the throwing then did register — has nothing left to destroy.
2261
+ closed = true;
2262
+ readable.destroy();
2263
+ reader.releaseLock();
2264
+ throw err;
2265
+ }
2266
+ return readable;
2267
+ }