pi-cursor-sdk 0.1.47 → 0.1.49

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 (291) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +2 -2
  3. package/node_modules/@connectrpc/connect-node/README.md +141 -0
  4. package/node_modules/@connectrpc/connect-node/dist/cjs/compression.d.ts +15 -0
  5. package/node_modules/@connectrpc/connect-node/dist/cjs/compression.js +89 -0
  6. package/node_modules/@connectrpc/connect-node/dist/cjs/connect-node-adapter.d.ts +42 -0
  7. package/node_modules/@connectrpc/connect-node/dist/cjs/connect-node-adapter.js +61 -0
  8. package/node_modules/@connectrpc/connect-node/dist/cjs/connect-transport.d.ts +100 -0
  9. package/node_modules/@connectrpc/connect-node/dist/cjs/connect-transport.js +25 -0
  10. package/node_modules/@connectrpc/connect-node/dist/cjs/grpc-transport.d.ts +96 -0
  11. package/node_modules/@connectrpc/connect-node/dist/cjs/grpc-transport.js +25 -0
  12. package/node_modules/@connectrpc/connect-node/dist/cjs/grpc-web-transport.d.ts +96 -0
  13. package/node_modules/@connectrpc/connect-node/dist/cjs/grpc-web-transport.js +25 -0
  14. package/node_modules/@connectrpc/connect-node/dist/cjs/http2-session-manager.d.ts +146 -0
  15. package/node_modules/@connectrpc/connect-node/dist/cjs/http2-session-manager.js +536 -0
  16. package/node_modules/@connectrpc/connect-node/dist/cjs/index.d.ts +15 -0
  17. package/node_modules/@connectrpc/connect-node/dist/cjs/index.js +36 -0
  18. package/node_modules/@connectrpc/connect-node/dist/cjs/node-error.d.ts +45 -0
  19. package/node_modules/@connectrpc/connect-node/dist/cjs/node-error.js +154 -0
  20. package/node_modules/@connectrpc/connect-node/dist/cjs/node-headers-polyfill.d.ts +1 -0
  21. package/node_modules/@connectrpc/connect-node/dist/cjs/node-headers-polyfill.js +29 -0
  22. package/node_modules/@connectrpc/connect-node/dist/cjs/node-transport-options.d.ts +89 -0
  23. package/node_modules/@connectrpc/connect-node/dist/cjs/node-transport-options.js +58 -0
  24. package/node_modules/@connectrpc/connect-node/dist/cjs/node-universal-client.d.ts +54 -0
  25. package/node_modules/@connectrpc/connect-node/dist/cjs/node-universal-client.js +326 -0
  26. package/node_modules/@connectrpc/connect-node/dist/cjs/node-universal-handler.d.ts +41 -0
  27. package/node_modules/@connectrpc/connect-node/dist/cjs/node-universal-handler.js +234 -0
  28. package/node_modules/@connectrpc/connect-node/dist/cjs/node-universal-header.d.ts +19 -0
  29. package/node_modules/@connectrpc/connect-node/dist/cjs/node-universal-header.js +97 -0
  30. package/node_modules/@connectrpc/connect-node/dist/cjs/package.json +1 -0
  31. package/node_modules/@connectrpc/connect-node/dist/esm/compression.d.ts +15 -0
  32. package/node_modules/@connectrpc/connect-node/dist/esm/compression.js +86 -0
  33. package/node_modules/@connectrpc/connect-node/dist/esm/connect-node-adapter.d.ts +42 -0
  34. package/node_modules/@connectrpc/connect-node/dist/esm/connect-node-adapter.js +58 -0
  35. package/node_modules/@connectrpc/connect-node/dist/esm/connect-transport.d.ts +100 -0
  36. package/node_modules/@connectrpc/connect-node/dist/esm/connect-transport.js +22 -0
  37. package/node_modules/@connectrpc/connect-node/dist/esm/grpc-transport.d.ts +96 -0
  38. package/node_modules/@connectrpc/connect-node/dist/esm/grpc-transport.js +22 -0
  39. package/node_modules/@connectrpc/connect-node/dist/esm/grpc-web-transport.d.ts +96 -0
  40. package/node_modules/@connectrpc/connect-node/dist/esm/grpc-web-transport.js +22 -0
  41. package/node_modules/@connectrpc/connect-node/dist/esm/http2-session-manager.d.ts +146 -0
  42. package/node_modules/@connectrpc/connect-node/dist/esm/http2-session-manager.js +532 -0
  43. package/node_modules/@connectrpc/connect-node/dist/esm/index.d.ts +15 -0
  44. package/node_modules/@connectrpc/connect-node/dist/esm/index.js +23 -0
  45. package/node_modules/@connectrpc/connect-node/dist/esm/node-error.d.ts +45 -0
  46. package/node_modules/@connectrpc/connect-node/dist/esm/node-error.js +147 -0
  47. package/node_modules/@connectrpc/connect-node/dist/esm/node-headers-polyfill.d.ts +1 -0
  48. package/node_modules/@connectrpc/connect-node/dist/esm/node-headers-polyfill.js +27 -0
  49. package/node_modules/@connectrpc/connect-node/dist/esm/node-transport-options.d.ts +89 -0
  50. package/node_modules/@connectrpc/connect-node/dist/esm/node-transport-options.js +58 -0
  51. package/node_modules/@connectrpc/connect-node/dist/esm/node-universal-client.d.ts +54 -0
  52. package/node_modules/@connectrpc/connect-node/dist/esm/node-universal-client.js +324 -0
  53. package/node_modules/@connectrpc/connect-node/dist/esm/node-universal-handler.d.ts +41 -0
  54. package/node_modules/@connectrpc/connect-node/dist/esm/node-universal-handler.js +230 -0
  55. package/node_modules/@connectrpc/connect-node/dist/esm/node-universal-header.d.ts +19 -0
  56. package/node_modules/@connectrpc/connect-node/dist/esm/node-universal-header.js +93 -0
  57. package/node_modules/@connectrpc/connect-node/package.json +46 -0
  58. package/node_modules/undici/LICENSE +21 -0
  59. package/node_modules/undici/README.md +741 -0
  60. package/node_modules/undici/docs/docs/api/Agent.md +84 -0
  61. package/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
  62. package/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
  63. package/node_modules/undici/docs/docs/api/CacheStore.md +164 -0
  64. package/node_modules/undici/docs/docs/api/Client.md +288 -0
  65. package/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
  66. package/node_modules/undici/docs/docs/api/Connector.md +115 -0
  67. package/node_modules/undici/docs/docs/api/ContentType.md +57 -0
  68. package/node_modules/undici/docs/docs/api/Cookies.md +128 -0
  69. package/node_modules/undici/docs/docs/api/Debug.md +62 -0
  70. package/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +315 -0
  71. package/node_modules/undici/docs/docs/api/Dispatcher.md +1392 -0
  72. package/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
  73. package/node_modules/undici/docs/docs/api/Errors.md +49 -0
  74. package/node_modules/undici/docs/docs/api/EventSource.md +45 -0
  75. package/node_modules/undici/docs/docs/api/Fetch.md +60 -0
  76. package/node_modules/undici/docs/docs/api/GlobalInstallation.md +139 -0
  77. package/node_modules/undici/docs/docs/api/H2CClient.md +263 -0
  78. package/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
  79. package/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
  80. package/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
  81. package/node_modules/undici/docs/docs/api/MockClient.md +81 -0
  82. package/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
  83. package/node_modules/undici/docs/docs/api/MockPool.md +555 -0
  84. package/node_modules/undici/docs/docs/api/Pool.md +84 -0
  85. package/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
  86. package/node_modules/undici/docs/docs/api/ProxyAgent.md +229 -0
  87. package/node_modules/undici/docs/docs/api/RedirectHandler.md +93 -0
  88. package/node_modules/undici/docs/docs/api/RetryAgent.md +50 -0
  89. package/node_modules/undici/docs/docs/api/RetryHandler.md +118 -0
  90. package/node_modules/undici/docs/docs/api/RoundRobinPool.md +145 -0
  91. package/node_modules/undici/docs/docs/api/SnapshotAgent.md +616 -0
  92. package/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +275 -0
  93. package/node_modules/undici/docs/docs/api/Util.md +25 -0
  94. package/node_modules/undici/docs/docs/api/WebSocket.md +141 -0
  95. package/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
  96. package/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
  97. package/node_modules/undici/docs/docs/best-practices/crawling.md +58 -0
  98. package/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
  99. package/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
  100. package/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +224 -0
  101. package/node_modules/undici/docs/docs/best-practices/writing-tests.md +63 -0
  102. package/node_modules/undici/index-fetch.js +65 -0
  103. package/node_modules/undici/index.d.ts +3 -0
  104. package/node_modules/undici/index.js +234 -0
  105. package/node_modules/undici/lib/api/abort-signal.js +59 -0
  106. package/node_modules/undici/lib/api/api-connect.js +110 -0
  107. package/node_modules/undici/lib/api/api-pipeline.js +252 -0
  108. package/node_modules/undici/lib/api/api-request.js +214 -0
  109. package/node_modules/undici/lib/api/api-stream.js +209 -0
  110. package/node_modules/undici/lib/api/api-upgrade.js +111 -0
  111. package/node_modules/undici/lib/api/index.js +7 -0
  112. package/node_modules/undici/lib/api/readable.js +580 -0
  113. package/node_modules/undici/lib/cache/memory-cache-store.js +234 -0
  114. package/node_modules/undici/lib/cache/sqlite-cache-store.js +461 -0
  115. package/node_modules/undici/lib/core/connect.js +153 -0
  116. package/node_modules/undici/lib/core/constants.js +143 -0
  117. package/node_modules/undici/lib/core/diagnostics.js +227 -0
  118. package/node_modules/undici/lib/core/errors.js +477 -0
  119. package/node_modules/undici/lib/core/request.js +453 -0
  120. package/node_modules/undici/lib/core/socks5-client.js +422 -0
  121. package/node_modules/undici/lib/core/socks5-utils.js +212 -0
  122. package/node_modules/undici/lib/core/symbols.js +75 -0
  123. package/node_modules/undici/lib/core/tree.js +160 -0
  124. package/node_modules/undici/lib/core/util.js +992 -0
  125. package/node_modules/undici/lib/dispatcher/agent.js +158 -0
  126. package/node_modules/undici/lib/dispatcher/balanced-pool.js +219 -0
  127. package/node_modules/undici/lib/dispatcher/client-h1.js +1722 -0
  128. package/node_modules/undici/lib/dispatcher/client-h2.js +995 -0
  129. package/node_modules/undici/lib/dispatcher/client.js +664 -0
  130. package/node_modules/undici/lib/dispatcher/dispatcher-base.js +184 -0
  131. package/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
  132. package/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +146 -0
  133. package/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
  134. package/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
  135. package/node_modules/undici/lib/dispatcher/pool-base.js +214 -0
  136. package/node_modules/undici/lib/dispatcher/pool.js +118 -0
  137. package/node_modules/undici/lib/dispatcher/proxy-agent.js +319 -0
  138. package/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
  139. package/node_modules/undici/lib/dispatcher/round-robin-pool.js +137 -0
  140. package/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +260 -0
  141. package/node_modules/undici/lib/encoding/index.js +33 -0
  142. package/node_modules/undici/lib/global.js +59 -0
  143. package/node_modules/undici/lib/handler/cache-handler.js +578 -0
  144. package/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
  145. package/node_modules/undici/lib/handler/decorator-handler.js +67 -0
  146. package/node_modules/undici/lib/handler/deduplication-handler.js +460 -0
  147. package/node_modules/undici/lib/handler/redirect-handler.js +238 -0
  148. package/node_modules/undici/lib/handler/retry-handler.js +394 -0
  149. package/node_modules/undici/lib/handler/unwrap-handler.js +106 -0
  150. package/node_modules/undici/lib/handler/wrap-handler.js +105 -0
  151. package/node_modules/undici/lib/interceptor/cache.js +495 -0
  152. package/node_modules/undici/lib/interceptor/decompress.js +259 -0
  153. package/node_modules/undici/lib/interceptor/deduplicate.js +117 -0
  154. package/node_modules/undici/lib/interceptor/dns.js +571 -0
  155. package/node_modules/undici/lib/interceptor/dump.js +112 -0
  156. package/node_modules/undici/lib/interceptor/redirect.js +21 -0
  157. package/node_modules/undici/lib/interceptor/response-error.js +95 -0
  158. package/node_modules/undici/lib/interceptor/retry.js +19 -0
  159. package/node_modules/undici/lib/llhttp/.gitkeep +0 -0
  160. package/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
  161. package/node_modules/undici/lib/llhttp/constants.js +531 -0
  162. package/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
  163. package/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
  164. package/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
  165. package/node_modules/undici/lib/llhttp/utils.js +12 -0
  166. package/node_modules/undici/lib/mock/mock-agent.js +232 -0
  167. package/node_modules/undici/lib/mock/mock-call-history.js +248 -0
  168. package/node_modules/undici/lib/mock/mock-client.js +68 -0
  169. package/node_modules/undici/lib/mock/mock-errors.js +29 -0
  170. package/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
  171. package/node_modules/undici/lib/mock/mock-pool.js +68 -0
  172. package/node_modules/undici/lib/mock/mock-symbols.js +32 -0
  173. package/node_modules/undici/lib/mock/mock-utils.js +486 -0
  174. package/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
  175. package/node_modules/undici/lib/mock/snapshot-agent.js +353 -0
  176. package/node_modules/undici/lib/mock/snapshot-recorder.js +588 -0
  177. package/node_modules/undici/lib/mock/snapshot-utils.js +158 -0
  178. package/node_modules/undici/lib/util/cache.js +414 -0
  179. package/node_modules/undici/lib/util/date.js +653 -0
  180. package/node_modules/undici/lib/util/promise.js +28 -0
  181. package/node_modules/undici/lib/util/runtime-features.js +124 -0
  182. package/node_modules/undici/lib/util/stats.js +32 -0
  183. package/node_modules/undici/lib/util/timers.js +425 -0
  184. package/node_modules/undici/lib/web/cache/cache.js +864 -0
  185. package/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
  186. package/node_modules/undici/lib/web/cache/util.js +45 -0
  187. package/node_modules/undici/lib/web/cookies/constants.js +12 -0
  188. package/node_modules/undici/lib/web/cookies/index.js +199 -0
  189. package/node_modules/undici/lib/web/cookies/parse.js +314 -0
  190. package/node_modules/undici/lib/web/cookies/util.js +282 -0
  191. package/node_modules/undici/lib/web/eventsource/eventsource-stream.js +399 -0
  192. package/node_modules/undici/lib/web/eventsource/eventsource.js +501 -0
  193. package/node_modules/undici/lib/web/eventsource/util.js +29 -0
  194. package/node_modules/undici/lib/web/fetch/LICENSE +21 -0
  195. package/node_modules/undici/lib/web/fetch/body.js +509 -0
  196. package/node_modules/undici/lib/web/fetch/constants.js +131 -0
  197. package/node_modules/undici/lib/web/fetch/data-url.js +596 -0
  198. package/node_modules/undici/lib/web/fetch/formdata-parser.js +586 -0
  199. package/node_modules/undici/lib/web/fetch/formdata.js +259 -0
  200. package/node_modules/undici/lib/web/fetch/global.js +40 -0
  201. package/node_modules/undici/lib/web/fetch/headers.js +719 -0
  202. package/node_modules/undici/lib/web/fetch/index.js +2413 -0
  203. package/node_modules/undici/lib/web/fetch/request.js +1115 -0
  204. package/node_modules/undici/lib/web/fetch/response.js +641 -0
  205. package/node_modules/undici/lib/web/fetch/util.js +1522 -0
  206. package/node_modules/undici/lib/web/infra/index.js +229 -0
  207. package/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
  208. package/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
  209. package/node_modules/undici/lib/web/webidl/index.js +1006 -0
  210. package/node_modules/undici/lib/web/websocket/connection.js +329 -0
  211. package/node_modules/undici/lib/web/websocket/constants.js +126 -0
  212. package/node_modules/undici/lib/web/websocket/events.js +331 -0
  213. package/node_modules/undici/lib/web/websocket/frame.js +133 -0
  214. package/node_modules/undici/lib/web/websocket/permessage-deflate.js +100 -0
  215. package/node_modules/undici/lib/web/websocket/receiver.js +507 -0
  216. package/node_modules/undici/lib/web/websocket/sender.js +109 -0
  217. package/node_modules/undici/lib/web/websocket/stream/websocketerror.js +104 -0
  218. package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +498 -0
  219. package/node_modules/undici/lib/web/websocket/util.js +347 -0
  220. package/node_modules/undici/lib/web/websocket/websocket.js +758 -0
  221. package/node_modules/undici/package.json +152 -0
  222. package/node_modules/undici/scripts/strip-comments.js +10 -0
  223. package/node_modules/undici/types/README.md +6 -0
  224. package/node_modules/undici/types/agent.d.ts +32 -0
  225. package/node_modules/undici/types/api.d.ts +43 -0
  226. package/node_modules/undici/types/balanced-pool.d.ts +30 -0
  227. package/node_modules/undici/types/cache-interceptor.d.ts +179 -0
  228. package/node_modules/undici/types/cache.d.ts +36 -0
  229. package/node_modules/undici/types/client-stats.d.ts +15 -0
  230. package/node_modules/undici/types/client.d.ts +139 -0
  231. package/node_modules/undici/types/connector.d.ts +36 -0
  232. package/node_modules/undici/types/content-type.d.ts +21 -0
  233. package/node_modules/undici/types/cookies.d.ts +30 -0
  234. package/node_modules/undici/types/diagnostics-channel.d.ts +74 -0
  235. package/node_modules/undici/types/dispatcher.d.ts +275 -0
  236. package/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
  237. package/node_modules/undici/types/errors.d.ts +177 -0
  238. package/node_modules/undici/types/eventsource.d.ts +66 -0
  239. package/node_modules/undici/types/fetch.d.ts +231 -0
  240. package/node_modules/undici/types/formdata.d.ts +114 -0
  241. package/node_modules/undici/types/global-dispatcher.d.ts +9 -0
  242. package/node_modules/undici/types/global-origin.d.ts +7 -0
  243. package/node_modules/undici/types/h2c-client.d.ts +73 -0
  244. package/node_modules/undici/types/handlers.d.ts +14 -0
  245. package/node_modules/undici/types/header.d.ts +160 -0
  246. package/node_modules/undici/types/index.d.ts +91 -0
  247. package/node_modules/undici/types/interceptors.d.ts +80 -0
  248. package/node_modules/undici/types/mock-agent.d.ts +68 -0
  249. package/node_modules/undici/types/mock-call-history.d.ts +111 -0
  250. package/node_modules/undici/types/mock-client.d.ts +27 -0
  251. package/node_modules/undici/types/mock-errors.d.ts +12 -0
  252. package/node_modules/undici/types/mock-interceptor.d.ts +94 -0
  253. package/node_modules/undici/types/mock-pool.d.ts +27 -0
  254. package/node_modules/undici/types/patch.d.ts +29 -0
  255. package/node_modules/undici/types/pool-stats.d.ts +19 -0
  256. package/node_modules/undici/types/pool.d.ts +41 -0
  257. package/node_modules/undici/types/proxy-agent.d.ts +29 -0
  258. package/node_modules/undici/types/readable.d.ts +68 -0
  259. package/node_modules/undici/types/retry-agent.d.ts +8 -0
  260. package/node_modules/undici/types/retry-handler.d.ts +125 -0
  261. package/node_modules/undici/types/round-robin-pool.d.ts +41 -0
  262. package/node_modules/undici/types/snapshot-agent.d.ts +109 -0
  263. package/node_modules/undici/types/socks5-proxy-agent.d.ts +25 -0
  264. package/node_modules/undici/types/util.d.ts +18 -0
  265. package/node_modules/undici/types/utility.d.ts +7 -0
  266. package/node_modules/undici/types/webidl.d.ts +347 -0
  267. package/node_modules/undici/types/websocket.d.ts +188 -0
  268. package/package.json +161 -156
  269. package/src/context.ts +1 -1
  270. package/src/cursor-context-tools.ts +1 -1
  271. package/src/cursor-live-run-accounting.ts +1 -1
  272. package/src/cursor-live-run-coordinator.ts +1 -1
  273. package/src/cursor-native-replay-routing.ts +1 -1
  274. package/src/cursor-partial-content-emitter.ts +1 -1
  275. package/src/cursor-pi-tool-bridge-mcp.ts +1 -1
  276. package/src/cursor-pi-tool-bridge-run.ts +1 -1
  277. package/src/cursor-pi-tool-bridge-types.ts +1 -1
  278. package/src/cursor-provider-lazy.ts +1 -1
  279. package/src/cursor-provider-live-run-drain.ts +1 -1
  280. package/src/cursor-provider-run-finalizer.ts +1 -1
  281. package/src/cursor-provider-turn-coordinator.ts +1 -1
  282. package/src/cursor-provider-turn-lifecycle-emitter.ts +1 -1
  283. package/src/cursor-provider-turn-prepare.ts +1 -1
  284. package/src/cursor-provider-turn-types.ts +1 -1
  285. package/src/cursor-provider.ts +1 -1
  286. package/src/cursor-run-final-text.ts +1 -1
  287. package/src/cursor-sdk-event-debug.ts +1 -1
  288. package/src/cursor-session-agent.ts +1 -1
  289. package/src/cursor-session-send-policy.ts +1 -1
  290. package/src/cursor-usage-accounting.ts +1 -1
  291. package/src/model-discovery.ts +1 -1
@@ -0,0 +1,324 @@
1
+ // Copyright 2021-2024 The Connect Authors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import * as http2 from "http2";
15
+ import * as http from "http";
16
+ import * as https from "https";
17
+ import { Code, ConnectError } from "@connectrpc/connect";
18
+ import { nodeHeaderToWebHeader, webHeaderToNodeHeaders, } from "./node-universal-header.js";
19
+ import { connectErrorFromH2ResetCode, connectErrorFromNodeReason, getNodeErrorProps, H2Code, unwrapNodeErrorChain, } from "./node-error.js";
20
+ import { getAbortSignalReason } from "@connectrpc/connect/protocol";
21
+ import { Http2SessionManager } from "./http2-session-manager.js";
22
+ /**
23
+ * Create a universal client function, a minimal abstraction of an HTTP client,
24
+ * using the Node.js `http`, `https`, or `http2` module.
25
+ *
26
+ * @private Internal code, does not follow semantic versioning.
27
+ */
28
+ export function createNodeHttpClient(options) {
29
+ var _a;
30
+ if (options.httpVersion == "1.1") {
31
+ return createNodeHttp1Client(options.nodeOptions);
32
+ }
33
+ const sessionProvider = (_a = options.sessionProvider) !== null && _a !== void 0 ? _a : ((url) => new Http2SessionManager(url));
34
+ return createNodeHttp2Client(sessionProvider);
35
+ }
36
+ /**
37
+ * Create an HTTP client using the Node.js `http` or `https` package.
38
+ *
39
+ * The HTTP client is a simple function conforming to the type UniversalClientFn.
40
+ * It takes an UniversalClientRequest as an argument, and returns a promise for
41
+ * an UniversalClientResponse.
42
+ */
43
+ function createNodeHttp1Client(httpOptions) {
44
+ return async function request(req) {
45
+ const sentinel = createSentinel(req.signal);
46
+ return new Promise((resolve, reject) => {
47
+ sentinel.catch((e) => {
48
+ reject(e);
49
+ });
50
+ h1Request(sentinel, req.url, Object.assign(Object.assign({}, httpOptions), { headers: webHeaderToNodeHeaders(req.header, httpOptions === null || httpOptions === void 0 ? void 0 : httpOptions.headers), method: req.method }), (request) => {
51
+ void sinkRequest(req, request, sentinel);
52
+ request.on("response", (response) => {
53
+ var _a;
54
+ response.on("error", sentinel.reject);
55
+ sentinel.catch((reason) => response.destroy(connectErrorFromNodeReason(reason)));
56
+ const trailer = new Headers();
57
+ resolve({
58
+ status: (_a = response.statusCode) !== null && _a !== void 0 ? _a : 0,
59
+ header: nodeHeaderToWebHeader(response.headers),
60
+ body: h1ResponseIterable(sentinel, response, trailer),
61
+ trailer,
62
+ });
63
+ });
64
+ });
65
+ });
66
+ };
67
+ }
68
+ /**
69
+ * Create an HTTP client using the Node.js `http2` package.
70
+ *
71
+ * The HTTP client is a simple function conforming to the type UniversalClientFn.
72
+ * It takes an UniversalClientRequest as an argument, and returns a promise for
73
+ * an UniversalClientResponse.
74
+ */
75
+ function createNodeHttp2Client(sessionProvider) {
76
+ return function request(req) {
77
+ const sentinel = createSentinel(req.signal);
78
+ const sessionManager = sessionProvider(req.url);
79
+ return new Promise((resolve, reject) => {
80
+ sentinel.catch((e) => {
81
+ reject(e);
82
+ });
83
+ h2Request(sentinel, sessionManager, req.url, req.method, webHeaderToNodeHeaders(req.header), {}, (stream) => {
84
+ void sinkRequest(req, stream, sentinel);
85
+ stream.on("response", (headers) => {
86
+ var _a;
87
+ const response = {
88
+ status: (_a = headers[":status"]) !== null && _a !== void 0 ? _a : 0,
89
+ header: nodeHeaderToWebHeader(headers),
90
+ body: h2ResponseIterable(sentinel, stream, sessionManager),
91
+ trailer: h2ResponseTrailer(stream),
92
+ };
93
+ resolve(response);
94
+ });
95
+ });
96
+ });
97
+ };
98
+ }
99
+ function h1Request(sentinel, url, options, onRequest) {
100
+ let request;
101
+ if (new URL(url).protocol.startsWith("https")) {
102
+ request = https.request(url, options);
103
+ }
104
+ else {
105
+ request = http.request(url, options);
106
+ }
107
+ sentinel.catch((reason) => request.destroy(connectErrorFromNodeReason(reason)));
108
+ // Node.js will only send headers with the first request body byte by default.
109
+ // We force it to send headers right away for consistent behavior between
110
+ // HTTP/1.1 and HTTP/2.2 clients.
111
+ request.flushHeaders();
112
+ request.on("error", sentinel.reject);
113
+ request.on("socket", function onRequestSocket(socket) {
114
+ function onSocketConnect() {
115
+ socket.off("connect", onSocketConnect);
116
+ onRequest(request);
117
+ }
118
+ // If readyState is open, then socket is already open due to keepAlive, so
119
+ // the 'connect' event will never fire so call onRequest explicitly
120
+ if (socket.readyState === "open") {
121
+ onRequest(request);
122
+ }
123
+ else {
124
+ socket.on("connect", onSocketConnect);
125
+ }
126
+ });
127
+ }
128
+ function h1ResponseIterable(sentinel, response, trailer) {
129
+ const inner = response[Symbol.asyncIterator]();
130
+ return {
131
+ [Symbol.asyncIterator]() {
132
+ return {
133
+ async next() {
134
+ const r = await sentinel.race(inner.next());
135
+ if (r.done === true) {
136
+ nodeHeaderToWebHeader(response.trailers).forEach((value, key) => {
137
+ trailer.set(key, value);
138
+ });
139
+ sentinel.resolve();
140
+ await sentinel;
141
+ }
142
+ return r;
143
+ },
144
+ throw(e) {
145
+ sentinel.reject(e);
146
+ throw e;
147
+ },
148
+ };
149
+ },
150
+ };
151
+ }
152
+ function h2Request(sentinel, sm, url, method, headers, options, onStream) {
153
+ const requestUrl = new URL(url);
154
+ if (requestUrl.origin !== sm.authority) {
155
+ const message = `cannot make a request to ${requestUrl.origin}: the http2 session is connected to ${sm.authority}`;
156
+ sentinel.reject(new ConnectError(message, Code.Internal));
157
+ return;
158
+ }
159
+ sm.request(method, requestUrl.pathname + requestUrl.search, headers, {}).then((stream) => {
160
+ sentinel.catch((reason) => {
161
+ if (stream.closed) {
162
+ return;
163
+ }
164
+ // Node.js http2 streams that are aborted via an AbortSignal close with
165
+ // an RST_STREAM with code INTERNAL_ERROR.
166
+ // To comply with the mapping between gRPC and HTTP/2 codes, we need to
167
+ // close with code CANCEL.
168
+ // See https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#errors
169
+ // See https://www.rfc-editor.org/rfc/rfc7540#section-7
170
+ const rstCode = reason instanceof ConnectError && reason.code == Code.Canceled
171
+ ? H2Code.CANCEL
172
+ : H2Code.INTERNAL_ERROR;
173
+ return new Promise((resolve) => stream.close(rstCode, resolve));
174
+ });
175
+ stream.on("error", function h2StreamError(e) {
176
+ if (stream.writableEnded &&
177
+ unwrapNodeErrorChain(e)
178
+ .map(getNodeErrorProps)
179
+ .some((p) => p.code == "ERR_STREAM_WRITE_AFTER_END")) {
180
+ return;
181
+ }
182
+ sentinel.reject(e);
183
+ });
184
+ stream.on("close", function h2StreamClose() {
185
+ const err = connectErrorFromH2ResetCode(stream.rstCode);
186
+ if (err) {
187
+ sentinel.reject(err);
188
+ }
189
+ });
190
+ onStream(stream);
191
+ }, (reason) => {
192
+ sentinel.reject(reason);
193
+ });
194
+ }
195
+ function h2ResponseTrailer(response) {
196
+ const trailer = new Headers();
197
+ response.on("trailers", (args) => {
198
+ nodeHeaderToWebHeader(args).forEach((value, key) => {
199
+ trailer.set(key, value);
200
+ });
201
+ });
202
+ return trailer;
203
+ }
204
+ function h2ResponseIterable(sentinel, response, sm) {
205
+ const inner = response[Symbol.asyncIterator]();
206
+ return {
207
+ [Symbol.asyncIterator]() {
208
+ return {
209
+ async next() {
210
+ const r = await sentinel.race(inner.next());
211
+ if (r.done === true) {
212
+ sentinel.resolve();
213
+ await sentinel;
214
+ }
215
+ sm === null || sm === void 0 ? void 0 : sm.notifyResponseByteRead(response);
216
+ return r;
217
+ },
218
+ throw(e) {
219
+ sentinel.reject(e);
220
+ throw e;
221
+ },
222
+ };
223
+ },
224
+ };
225
+ }
226
+ async function sinkRequest(request, nodeRequest, sentinel) {
227
+ if (request.body === undefined) {
228
+ await new Promise((resolve) => nodeRequest.end(resolve));
229
+ return;
230
+ }
231
+ const it = request.body[Symbol.asyncIterator]();
232
+ return new Promise((resolve) => {
233
+ writeNext();
234
+ function writeNext() {
235
+ if (sentinel.isRejected()) {
236
+ return;
237
+ }
238
+ it.next().then((r) => {
239
+ if (r.done === true) {
240
+ nodeRequest.end(resolve);
241
+ return;
242
+ }
243
+ nodeRequest.write(r.value, "binary", function (e) {
244
+ if (e === null || e === undefined) {
245
+ writeNext();
246
+ return;
247
+ }
248
+ if (it.throw !== undefined) {
249
+ it.throw(connectErrorFromNodeReason(e)).catch(() => {
250
+ //
251
+ });
252
+ }
253
+ // If the server responds and closes the connection before the client has written the entire response
254
+ // body, we get an ERR_STREAM_WRITE_AFTER_END error code from Node.js here.
255
+ // We do want to notify the iterable of the error condition, but we do not want to reject our sentinel,
256
+ // because that would also affect the reading side.
257
+ if (nodeRequest.writableEnded &&
258
+ unwrapNodeErrorChain(e)
259
+ .map(getNodeErrorProps)
260
+ .some((p) => p.code == "ERR_STREAM_WRITE_AFTER_END")) {
261
+ return;
262
+ }
263
+ sentinel.reject(e);
264
+ });
265
+ }, (e) => {
266
+ sentinel.reject(e);
267
+ });
268
+ }
269
+ });
270
+ }
271
+ function createSentinel(signal) {
272
+ let res;
273
+ let rej;
274
+ let resolved = false;
275
+ let rejected = false;
276
+ const p = new Promise((resolve, reject) => {
277
+ res = resolve;
278
+ rej = reject;
279
+ });
280
+ const c = {
281
+ resolve() {
282
+ if (!resolved && !rejected) {
283
+ resolved = true;
284
+ res === null || res === void 0 ? void 0 : res();
285
+ }
286
+ },
287
+ isResolved() {
288
+ return resolved;
289
+ },
290
+ reject(reason) {
291
+ if (!resolved && !rejected) {
292
+ rejected = true;
293
+ rej === null || rej === void 0 ? void 0 : rej(connectErrorFromNodeReason(reason));
294
+ }
295
+ },
296
+ isRejected() {
297
+ return rejected;
298
+ },
299
+ async race(promise) {
300
+ const r = await Promise.race([promise, p]);
301
+ if (r === undefined && resolved) {
302
+ throw new ConnectError("sentinel completed early", Code.Internal);
303
+ }
304
+ return r;
305
+ },
306
+ };
307
+ const s = Object.assign(p, c);
308
+ function onSignalAbort() {
309
+ c.reject(getAbortSignalReason(this));
310
+ }
311
+ if (signal) {
312
+ if (signal.aborted) {
313
+ c.reject(getAbortSignalReason(signal));
314
+ }
315
+ else {
316
+ signal.addEventListener("abort", onSignalAbort);
317
+ }
318
+ p.finally(() => signal.removeEventListener("abort", onSignalAbort)).catch(() => {
319
+ // We intentionally swallow sentinel rejection - errors must
320
+ // propagate through the request or response iterables.
321
+ });
322
+ }
323
+ return s;
324
+ }
@@ -0,0 +1,41 @@
1
+ import type * as http from "http";
2
+ import type * as http2 from "http2";
3
+ import type { JsonValue } from "@bufbuild/protobuf";
4
+ import type { UniversalServerRequest, UniversalServerResponse } from "@connectrpc/connect/protocol";
5
+ import type { ContextValues } from "@connectrpc/connect";
6
+ /**
7
+ * NodeHandlerFn is compatible with http.RequestListener and its equivalent
8
+ * for http2.
9
+ */
10
+ export type NodeHandlerFn = (request: NodeServerRequest, response: NodeServerResponse) => void;
11
+ /**
12
+ * A Node.js server request from the http, https, or the http2 module.
13
+ */
14
+ export type NodeServerRequest = http.IncomingMessage | http2.Http2ServerRequest;
15
+ /**
16
+ * A Node.js server response from the http, https, or the http2 module.
17
+ * Note that we are taking the liberty to patch the type of write() so
18
+ * that they are compatible with each other.
19
+ */
20
+ export type NodeServerResponse = (Omit<http.ServerResponse, "write"> | Omit<http2.Http2ServerResponse, "write">) & {
21
+ write(chunk: string | Uint8Array, callback?: (err: Error | null | undefined) => void): boolean;
22
+ write(chunk: string | Uint8Array, encoding: BufferEncoding, callback?: (err: Error | null | undefined) => void): boolean;
23
+ };
24
+ /**
25
+ * Converts a Node.js server request to a UniversalServerRequest.
26
+ * This function helps to implement adapters to server frameworks running
27
+ * on Node.js. Please be careful using this function in your own code, as we
28
+ * may have to make changes to it in the future.
29
+ */
30
+ export declare function universalRequestFromNodeRequest(nodeRequest: NodeServerRequest, nodeResponse: NodeServerResponse, parsedJsonBody: JsonValue | undefined, contextValues: ContextValues | undefined): UniversalServerRequest;
31
+ /**
32
+ * @deprecated
33
+ */
34
+ export declare function universalRequestFromNodeRequest(nodeRequest: NodeServerRequest, parsedJsonBody: JsonValue | undefined, contextValues: ContextValues | undefined): UniversalServerRequest;
35
+ /**
36
+ * Writes a UniversalServerResponse to a Node.js server response.
37
+ * This function helps to implement adapters to server frameworks running
38
+ * on Node.js. Please be careful using this function in your own code, as we
39
+ * may have to make changes to it in the future.
40
+ */
41
+ export declare function universalResponseToNodeResponse(universalResponse: UniversalServerResponse, nodeResponse: NodeServerResponse): Promise<void>;
@@ -0,0 +1,230 @@
1
+ // Copyright 2021-2024 The Connect Authors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
15
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
16
+ var m = o[Symbol.asyncIterator], i;
17
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
18
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
19
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
20
+ };
21
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
22
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
23
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
24
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
25
+ return i = {}, verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
26
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
27
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
28
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
29
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
30
+ function fulfill(value) { resume("next", value); }
31
+ function reject(value) { resume("throw", value); }
32
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
33
+ };
34
+ import { Code, ConnectError } from "@connectrpc/connect";
35
+ import { nodeHeaderToWebHeader, webHeaderToNodeHeaders, } from "./node-universal-header.js";
36
+ import { connectErrorFromH2ResetCode, connectErrorFromNodeReason, } from "./node-error.js";
37
+ export function universalRequestFromNodeRequest(nodeRequest, ...rest) {
38
+ var _a, _b;
39
+ const nodeResponse = rest.length === 3 ? rest[0] : undefined;
40
+ const parsedJsonBody = rest.length === 3 ? rest[1] : rest[0];
41
+ const contextValues = rest.length === 3 ? rest[2] : rest[1];
42
+ const encrypted = "encrypted" in nodeRequest.socket && nodeRequest.socket.encrypted;
43
+ const protocol = encrypted ? "https" : "http";
44
+ const authority = "authority" in nodeRequest
45
+ ? nodeRequest.authority
46
+ : nodeRequest.headers.host;
47
+ const pathname = (_a = nodeRequest.url) !== null && _a !== void 0 ? _a : "";
48
+ if (authority === undefined) {
49
+ throw new ConnectError("unable to determine request authority from Node.js server request", Code.Internal);
50
+ }
51
+ const body = parsedJsonBody !== undefined
52
+ ? parsedJsonBody
53
+ : asyncIterableFromNodeServerRequest(nodeRequest);
54
+ const abortController = new AbortController();
55
+ if ("stream" in nodeRequest) {
56
+ // HTTP/2 has error codes we want to honor
57
+ nodeRequest.once("close", () => {
58
+ const err = connectErrorFromH2ResetCode(nodeRequest.stream.rstCode);
59
+ if (err !== undefined) {
60
+ abortController.abort(err);
61
+ }
62
+ else {
63
+ abortController.abort();
64
+ }
65
+ });
66
+ }
67
+ else {
68
+ // HTTP/1.1 does not have error codes, but Node.js has ECONNRESET
69
+ const nodeResponsOrRequest = nodeResponse !== null && nodeResponse !== void 0 ? nodeResponse : nodeRequest;
70
+ const onH1Error = (e) => {
71
+ nodeRequest.off("error", onH1Error);
72
+ nodeResponsOrRequest.off("close", onH1Close);
73
+ abortController.abort(connectErrorFromNodeReason(e));
74
+ };
75
+ const onH1Close = () => {
76
+ nodeRequest.off("error", onH1Error);
77
+ nodeResponsOrRequest.off("close", onH1Close);
78
+ // When subscribed to the response, this can get called before "error"
79
+ abortController.abort(nodeRequest.errored
80
+ ? connectErrorFromNodeReason(nodeRequest.errored)
81
+ : undefined);
82
+ };
83
+ nodeRequest.once("error", onH1Error);
84
+ // Node emits close on the request as soon as all data is read.
85
+ // We instead subscribe to the response (if available)
86
+ //
87
+ // Ref: https://github.com/nodejs/node/issues/40775
88
+ nodeResponsOrRequest.once("close", onH1Close);
89
+ }
90
+ return {
91
+ httpVersion: nodeRequest.httpVersion,
92
+ method: (_b = nodeRequest.method) !== null && _b !== void 0 ? _b : "",
93
+ url: new URL(pathname, `${protocol}://${authority}`).toString(),
94
+ header: nodeHeaderToWebHeader(nodeRequest.headers),
95
+ body,
96
+ signal: abortController.signal,
97
+ contextValues: contextValues,
98
+ };
99
+ }
100
+ /**
101
+ * Writes a UniversalServerResponse to a Node.js server response.
102
+ * This function helps to implement adapters to server frameworks running
103
+ * on Node.js. Please be careful using this function in your own code, as we
104
+ * may have to make changes to it in the future.
105
+ */
106
+ export async function universalResponseToNodeResponse(universalResponse, nodeResponse) {
107
+ var _a, _b, _c;
108
+ const it = (_a = universalResponse.body) === null || _a === void 0 ? void 0 : _a[Symbol.asyncIterator]();
109
+ let isWriteError = false;
110
+ try {
111
+ if (it !== undefined) {
112
+ let chunk = await it.next();
113
+ isWriteError = true;
114
+ // we deliberately send headers after first read, not before,
115
+ // because we have to give the implementation a chance to
116
+ // set response headers
117
+ nodeResponse.writeHead(universalResponse.status, webHeaderToNodeHeaders(universalResponse.header));
118
+ isWriteError = false;
119
+ for (; chunk.done !== true; chunk = await it.next()) {
120
+ isWriteError = true;
121
+ await write(nodeResponse, chunk.value);
122
+ if ("flush" in nodeResponse &&
123
+ typeof nodeResponse.flush == "function") {
124
+ // The npm package "compression" is an express middleware that is widely used,
125
+ // for example in next.js. It uses the npm package "compressible" to determine
126
+ // whether to apply compression to a response. Unfortunately, "compressible"
127
+ // matches every mime type that ends with "+json", causing our server-streaming
128
+ // RPCs to be buffered.
129
+ // The package modifies the response object, and adds a flush() method, which
130
+ // flushes the underlying gzip or deflate stream from the Node.js zlib module.
131
+ // The method is added here:
132
+ // https://github.com/expressjs/compression/blob/ad5113b98cafe1382a0ece30bb4673707ac59ce7/index.js#L70
133
+ nodeResponse.flush();
134
+ }
135
+ isWriteError = false;
136
+ }
137
+ }
138
+ if (!nodeResponse.headersSent) {
139
+ nodeResponse.writeHead(universalResponse.status, webHeaderToNodeHeaders(universalResponse.header));
140
+ }
141
+ if (universalResponse.trailer) {
142
+ nodeResponse.addTrailers(webHeaderToNodeHeaders(universalResponse.trailer));
143
+ }
144
+ await new Promise((resolve) => {
145
+ // The npm package "compression" crashes when a callback is passed to end()
146
+ // https://github.com/expressjs/compression/blob/ad5113b98cafe1382a0ece30bb4673707ac59ce7/index.js#L115
147
+ nodeResponse.once("end", resolve);
148
+ nodeResponse.end();
149
+ });
150
+ }
151
+ catch (e) {
152
+ // Report write errors to the handler.
153
+ if (isWriteError) {
154
+ (_b = it === null || it === void 0 ? void 0 : it.throw) === null || _b === void 0 ? void 0 : _b.call(it, e).catch(() => { });
155
+ }
156
+ throw connectErrorFromNodeReason(e);
157
+ }
158
+ finally {
159
+ (_c = it === null || it === void 0 ? void 0 : it.return) === null || _c === void 0 ? void 0 : _c.call(it).catch(() => { });
160
+ }
161
+ }
162
+ function asyncIterableFromNodeServerRequest(request) {
163
+ return __asyncGenerator(this, arguments, function* asyncIterableFromNodeServerRequest_1() {
164
+ var _a, e_1, _b, _c;
165
+ const it = request.iterator({
166
+ // Node.js v16 closes request and response when this option isn't disabled.
167
+ // When one of our handlers receives invalid data (such as an unexpected
168
+ // compression flag in a streaming request), we're unable to write the error
169
+ // response.
170
+ // Later major versions have a more sensible behavior - we can revert this
171
+ // workaround once we stop supporting v16.
172
+ destroyOnReturn: false,
173
+ });
174
+ try {
175
+ for (var _d = true, it_1 = __asyncValues(it), it_1_1; it_1_1 = yield __await(it_1.next()), _a = it_1_1.done, !_a; _d = true) {
176
+ _c = it_1_1.value;
177
+ _d = false;
178
+ const chunk = _c;
179
+ yield yield __await(chunk);
180
+ }
181
+ }
182
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
183
+ finally {
184
+ try {
185
+ if (!_d && !_a && (_b = it_1.return)) yield __await(_b.call(it_1));
186
+ }
187
+ finally { if (e_1) throw e_1.error; }
188
+ }
189
+ });
190
+ }
191
+ function write(stream, data) {
192
+ return new Promise((resolve, reject) => {
193
+ if (stream.errored) {
194
+ return error(stream.errored);
195
+ }
196
+ stream.once("error", error);
197
+ stream.once("drain", drain);
198
+ // flushed == false: the stream wishes for the calling code to wait for
199
+ // the 'drain' event to be emitted before continuing to write additional
200
+ // data.
201
+ const flushed = stream.write(data, "binary", function (err) {
202
+ if (err && !flushed) {
203
+ // We are never getting a "drain" nor an "error" event if the stream
204
+ // has already ended (ERR_STREAM_WRITE_AFTER_END), so we have to
205
+ // resolve our promise in this callback.
206
+ error(err);
207
+ // However, once we do that (and remove our event listeners), we _do_
208
+ // get an "error" event, which ends up as an uncaught exception.
209
+ // We silence this error specifically with the following listener.
210
+ // All of this seems very fragile.
211
+ stream.once("error", () => {
212
+ //
213
+ });
214
+ }
215
+ });
216
+ if (flushed) {
217
+ drain();
218
+ }
219
+ function error(err) {
220
+ stream.off("error", error);
221
+ stream.off("drain", drain);
222
+ reject(err);
223
+ }
224
+ function drain() {
225
+ stream.off("error", error);
226
+ stream.off("drain", drain);
227
+ resolve();
228
+ }
229
+ });
230
+ }
@@ -0,0 +1,19 @@
1
+ import type * as http from "http";
2
+ import type * as http2 from "http2";
3
+ /**
4
+ * Convert a Node.js header object to a fetch API Headers object.
5
+ *
6
+ * This function works for Node.js incoming and outgoing headers, and for the
7
+ * http and the http2 package.
8
+ *
9
+ * HTTP/2 pseudo-headers (:method, :path, etc.) are stripped.
10
+ */
11
+ export declare function nodeHeaderToWebHeader(nodeHeaders: http.OutgoingHttpHeaders | http.IncomingHttpHeaders | http2.IncomingHttpHeaders | http.IncomingMessage["trailers"]): Headers;
12
+ /**
13
+ * Convert a fetch API Headers object to a Node.js headers object.
14
+ *
15
+ * Optionally accepts default Node.js headers. If provided, fetch API headers
16
+ * are appended to the defaults. The original defaults headers are not modified.
17
+ */
18
+ export declare function webHeaderToNodeHeaders(headersInit: HeadersInit, defaultNodeHeaders?: http.OutgoingHttpHeaders): http.OutgoingHttpHeaders;
19
+ export declare function webHeaderToNodeHeaders(headersInit: HeadersInit | undefined): http.OutgoingHttpHeaders | undefined;