@nocobase/plugin-idp-oauth 2.1.0-alpha.17 → 2.1.0-alpha.18

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 (214) hide show
  1. package/build.config.ts +1 -1
  2. package/dist/externalVersion.js +4 -4
  3. package/dist/node_modules/light-my-request/package.json +1 -1
  4. package/dist/node_modules/undici/LICENSE +21 -0
  5. package/dist/node_modules/undici/README.md +741 -0
  6. package/dist/node_modules/undici/docs/docs/api/Agent.md +84 -0
  7. package/dist/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
  8. package/dist/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
  9. package/dist/node_modules/undici/docs/docs/api/CacheStore.md +164 -0
  10. package/dist/node_modules/undici/docs/docs/api/Client.md +285 -0
  11. package/dist/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
  12. package/dist/node_modules/undici/docs/docs/api/Connector.md +115 -0
  13. package/dist/node_modules/undici/docs/docs/api/ContentType.md +57 -0
  14. package/dist/node_modules/undici/docs/docs/api/Cookies.md +101 -0
  15. package/dist/node_modules/undici/docs/docs/api/Debug.md +62 -0
  16. package/dist/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +315 -0
  17. package/dist/node_modules/undici/docs/docs/api/Dispatcher.md +1392 -0
  18. package/dist/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
  19. package/dist/node_modules/undici/docs/docs/api/Errors.md +49 -0
  20. package/dist/node_modules/undici/docs/docs/api/EventSource.md +45 -0
  21. package/dist/node_modules/undici/docs/docs/api/Fetch.md +60 -0
  22. package/dist/node_modules/undici/docs/docs/api/GlobalInstallation.md +139 -0
  23. package/dist/node_modules/undici/docs/docs/api/H2CClient.md +263 -0
  24. package/dist/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
  25. package/dist/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
  26. package/dist/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
  27. package/dist/node_modules/undici/docs/docs/api/MockClient.md +81 -0
  28. package/dist/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
  29. package/dist/node_modules/undici/docs/docs/api/MockPool.md +555 -0
  30. package/dist/node_modules/undici/docs/docs/api/Pool.md +84 -0
  31. package/dist/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
  32. package/dist/node_modules/undici/docs/docs/api/ProxyAgent.md +229 -0
  33. package/dist/node_modules/undici/docs/docs/api/RedirectHandler.md +93 -0
  34. package/dist/node_modules/undici/docs/docs/api/RetryAgent.md +50 -0
  35. package/dist/node_modules/undici/docs/docs/api/RetryHandler.md +118 -0
  36. package/dist/node_modules/undici/docs/docs/api/RoundRobinPool.md +145 -0
  37. package/dist/node_modules/undici/docs/docs/api/SnapshotAgent.md +616 -0
  38. package/dist/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +274 -0
  39. package/dist/node_modules/undici/docs/docs/api/Util.md +25 -0
  40. package/dist/node_modules/undici/docs/docs/api/WebSocket.md +141 -0
  41. package/dist/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
  42. package/dist/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
  43. package/dist/node_modules/undici/docs/docs/best-practices/crawling.md +58 -0
  44. package/dist/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
  45. package/dist/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
  46. package/dist/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +224 -0
  47. package/dist/node_modules/undici/docs/docs/best-practices/writing-tests.md +63 -0
  48. package/dist/node_modules/undici/index-fetch.js +65 -0
  49. package/dist/node_modules/undici/index.d.ts +3 -0
  50. package/dist/node_modules/undici/index.js +234 -0
  51. package/dist/node_modules/undici/lib/api/abort-signal.js +59 -0
  52. package/dist/node_modules/undici/lib/api/api-connect.js +110 -0
  53. package/dist/node_modules/undici/lib/api/api-pipeline.js +252 -0
  54. package/dist/node_modules/undici/lib/api/api-request.js +214 -0
  55. package/dist/node_modules/undici/lib/api/api-stream.js +209 -0
  56. package/dist/node_modules/undici/lib/api/api-upgrade.js +111 -0
  57. package/dist/node_modules/undici/lib/api/index.js +7 -0
  58. package/dist/node_modules/undici/lib/api/readable.js +580 -0
  59. package/dist/node_modules/undici/lib/cache/memory-cache-store.js +234 -0
  60. package/dist/node_modules/undici/lib/cache/sqlite-cache-store.js +461 -0
  61. package/dist/node_modules/undici/lib/core/connect.js +137 -0
  62. package/dist/node_modules/undici/lib/core/constants.js +143 -0
  63. package/dist/node_modules/undici/lib/core/diagnostics.js +227 -0
  64. package/dist/node_modules/undici/lib/core/errors.js +477 -0
  65. package/dist/node_modules/undici/lib/core/request.js +438 -0
  66. package/dist/node_modules/undici/lib/core/socks5-client.js +407 -0
  67. package/dist/node_modules/undici/lib/core/socks5-utils.js +203 -0
  68. package/dist/node_modules/undici/lib/core/symbols.js +75 -0
  69. package/dist/node_modules/undici/lib/core/tree.js +160 -0
  70. package/dist/node_modules/undici/lib/core/util.js +992 -0
  71. package/dist/node_modules/undici/lib/dispatcher/agent.js +158 -0
  72. package/dist/node_modules/undici/lib/dispatcher/balanced-pool.js +219 -0
  73. package/dist/node_modules/undici/lib/dispatcher/client-h1.js +1610 -0
  74. package/dist/node_modules/undici/lib/dispatcher/client-h2.js +995 -0
  75. package/dist/node_modules/undici/lib/dispatcher/client.js +659 -0
  76. package/dist/node_modules/undici/lib/dispatcher/dispatcher-base.js +165 -0
  77. package/dist/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
  78. package/dist/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +146 -0
  79. package/dist/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
  80. package/dist/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
  81. package/dist/node_modules/undici/lib/dispatcher/pool-base.js +214 -0
  82. package/dist/node_modules/undici/lib/dispatcher/pool.js +118 -0
  83. package/dist/node_modules/undici/lib/dispatcher/proxy-agent.js +318 -0
  84. package/dist/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
  85. package/dist/node_modules/undici/lib/dispatcher/round-robin-pool.js +137 -0
  86. package/dist/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +249 -0
  87. package/dist/node_modules/undici/lib/encoding/index.js +33 -0
  88. package/dist/node_modules/undici/lib/global.js +50 -0
  89. package/dist/node_modules/undici/lib/handler/cache-handler.js +578 -0
  90. package/dist/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
  91. package/dist/node_modules/undici/lib/handler/decorator-handler.js +67 -0
  92. package/dist/node_modules/undici/lib/handler/deduplication-handler.js +460 -0
  93. package/dist/node_modules/undici/lib/handler/redirect-handler.js +238 -0
  94. package/dist/node_modules/undici/lib/handler/retry-handler.js +394 -0
  95. package/dist/node_modules/undici/lib/handler/unwrap-handler.js +100 -0
  96. package/dist/node_modules/undici/lib/handler/wrap-handler.js +105 -0
  97. package/dist/node_modules/undici/lib/interceptor/cache.js +495 -0
  98. package/dist/node_modules/undici/lib/interceptor/decompress.js +259 -0
  99. package/dist/node_modules/undici/lib/interceptor/deduplicate.js +117 -0
  100. package/dist/node_modules/undici/lib/interceptor/dns.js +571 -0
  101. package/dist/node_modules/undici/lib/interceptor/dump.js +112 -0
  102. package/dist/node_modules/undici/lib/interceptor/redirect.js +21 -0
  103. package/dist/node_modules/undici/lib/interceptor/response-error.js +95 -0
  104. package/dist/node_modules/undici/lib/interceptor/retry.js +19 -0
  105. package/dist/node_modules/undici/lib/llhttp/.gitkeep +0 -0
  106. package/dist/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
  107. package/dist/node_modules/undici/lib/llhttp/constants.js +531 -0
  108. package/dist/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
  109. package/dist/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
  110. package/dist/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
  111. package/dist/node_modules/undici/lib/llhttp/utils.js +12 -0
  112. package/dist/node_modules/undici/lib/mock/mock-agent.js +232 -0
  113. package/dist/node_modules/undici/lib/mock/mock-call-history.js +248 -0
  114. package/dist/node_modules/undici/lib/mock/mock-client.js +68 -0
  115. package/dist/node_modules/undici/lib/mock/mock-errors.js +29 -0
  116. package/dist/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
  117. package/dist/node_modules/undici/lib/mock/mock-pool.js +68 -0
  118. package/dist/node_modules/undici/lib/mock/mock-symbols.js +32 -0
  119. package/dist/node_modules/undici/lib/mock/mock-utils.js +486 -0
  120. package/dist/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
  121. package/dist/node_modules/undici/lib/mock/snapshot-agent.js +353 -0
  122. package/dist/node_modules/undici/lib/mock/snapshot-recorder.js +588 -0
  123. package/dist/node_modules/undici/lib/mock/snapshot-utils.js +158 -0
  124. package/dist/node_modules/undici/lib/util/cache.js +407 -0
  125. package/dist/node_modules/undici/lib/util/date.js +653 -0
  126. package/dist/node_modules/undici/lib/util/promise.js +28 -0
  127. package/dist/node_modules/undici/lib/util/runtime-features.js +124 -0
  128. package/dist/node_modules/undici/lib/util/stats.js +32 -0
  129. package/dist/node_modules/undici/lib/util/timers.js +425 -0
  130. package/dist/node_modules/undici/lib/web/cache/cache.js +864 -0
  131. package/dist/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
  132. package/dist/node_modules/undici/lib/web/cache/util.js +45 -0
  133. package/dist/node_modules/undici/lib/web/cookies/constants.js +12 -0
  134. package/dist/node_modules/undici/lib/web/cookies/index.js +199 -0
  135. package/dist/node_modules/undici/lib/web/cookies/parse.js +322 -0
  136. package/dist/node_modules/undici/lib/web/cookies/util.js +282 -0
  137. package/dist/node_modules/undici/lib/web/eventsource/eventsource-stream.js +399 -0
  138. package/dist/node_modules/undici/lib/web/eventsource/eventsource.js +501 -0
  139. package/dist/node_modules/undici/lib/web/eventsource/util.js +29 -0
  140. package/dist/node_modules/undici/lib/web/fetch/LICENSE +21 -0
  141. package/dist/node_modules/undici/lib/web/fetch/body.js +509 -0
  142. package/dist/node_modules/undici/lib/web/fetch/constants.js +131 -0
  143. package/dist/node_modules/undici/lib/web/fetch/data-url.js +596 -0
  144. package/dist/node_modules/undici/lib/web/fetch/formdata-parser.js +575 -0
  145. package/dist/node_modules/undici/lib/web/fetch/formdata.js +259 -0
  146. package/dist/node_modules/undici/lib/web/fetch/global.js +40 -0
  147. package/dist/node_modules/undici/lib/web/fetch/headers.js +719 -0
  148. package/dist/node_modules/undici/lib/web/fetch/index.js +2397 -0
  149. package/dist/node_modules/undici/lib/web/fetch/request.js +1115 -0
  150. package/dist/node_modules/undici/lib/web/fetch/response.js +641 -0
  151. package/dist/node_modules/undici/lib/web/fetch/util.js +1520 -0
  152. package/dist/node_modules/undici/lib/web/infra/index.js +229 -0
  153. package/dist/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
  154. package/dist/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
  155. package/dist/node_modules/undici/lib/web/webidl/index.js +1006 -0
  156. package/dist/node_modules/undici/lib/web/websocket/connection.js +329 -0
  157. package/dist/node_modules/undici/lib/web/websocket/constants.js +126 -0
  158. package/dist/node_modules/undici/lib/web/websocket/events.js +331 -0
  159. package/dist/node_modules/undici/lib/web/websocket/frame.js +133 -0
  160. package/dist/node_modules/undici/lib/web/websocket/permessage-deflate.js +118 -0
  161. package/dist/node_modules/undici/lib/web/websocket/receiver.js +450 -0
  162. package/dist/node_modules/undici/lib/web/websocket/sender.js +109 -0
  163. package/dist/node_modules/undici/lib/web/websocket/stream/websocketerror.js +104 -0
  164. package/dist/node_modules/undici/lib/web/websocket/stream/websocketstream.js +497 -0
  165. package/dist/node_modules/undici/lib/web/websocket/util.js +347 -0
  166. package/dist/node_modules/undici/lib/web/websocket/websocket.js +751 -0
  167. package/dist/node_modules/undici/package.json +152 -0
  168. package/dist/node_modules/undici/scripts/strip-comments.js +10 -0
  169. package/dist/node_modules/undici/types/README.md +6 -0
  170. package/dist/node_modules/undici/types/agent.d.ts +32 -0
  171. package/dist/node_modules/undici/types/api.d.ts +43 -0
  172. package/dist/node_modules/undici/types/balanced-pool.d.ts +30 -0
  173. package/dist/node_modules/undici/types/cache-interceptor.d.ts +179 -0
  174. package/dist/node_modules/undici/types/cache.d.ts +36 -0
  175. package/dist/node_modules/undici/types/client-stats.d.ts +15 -0
  176. package/dist/node_modules/undici/types/client.d.ts +123 -0
  177. package/dist/node_modules/undici/types/connector.d.ts +36 -0
  178. package/dist/node_modules/undici/types/content-type.d.ts +21 -0
  179. package/dist/node_modules/undici/types/cookies.d.ts +30 -0
  180. package/dist/node_modules/undici/types/diagnostics-channel.d.ts +74 -0
  181. package/dist/node_modules/undici/types/dispatcher.d.ts +273 -0
  182. package/dist/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
  183. package/dist/node_modules/undici/types/errors.d.ts +177 -0
  184. package/dist/node_modules/undici/types/eventsource.d.ts +66 -0
  185. package/dist/node_modules/undici/types/fetch.d.ts +231 -0
  186. package/dist/node_modules/undici/types/formdata.d.ts +114 -0
  187. package/dist/node_modules/undici/types/global-dispatcher.d.ts +9 -0
  188. package/dist/node_modules/undici/types/global-origin.d.ts +7 -0
  189. package/dist/node_modules/undici/types/h2c-client.d.ts +73 -0
  190. package/dist/node_modules/undici/types/handlers.d.ts +14 -0
  191. package/dist/node_modules/undici/types/header.d.ts +160 -0
  192. package/dist/node_modules/undici/types/index.d.ts +91 -0
  193. package/dist/node_modules/undici/types/interceptors.d.ts +80 -0
  194. package/dist/node_modules/undici/types/mock-agent.d.ts +68 -0
  195. package/dist/node_modules/undici/types/mock-call-history.d.ts +111 -0
  196. package/dist/node_modules/undici/types/mock-client.d.ts +27 -0
  197. package/dist/node_modules/undici/types/mock-errors.d.ts +12 -0
  198. package/dist/node_modules/undici/types/mock-interceptor.d.ts +94 -0
  199. package/dist/node_modules/undici/types/mock-pool.d.ts +27 -0
  200. package/dist/node_modules/undici/types/patch.d.ts +29 -0
  201. package/dist/node_modules/undici/types/pool-stats.d.ts +19 -0
  202. package/dist/node_modules/undici/types/pool.d.ts +41 -0
  203. package/dist/node_modules/undici/types/proxy-agent.d.ts +29 -0
  204. package/dist/node_modules/undici/types/readable.d.ts +68 -0
  205. package/dist/node_modules/undici/types/retry-agent.d.ts +8 -0
  206. package/dist/node_modules/undici/types/retry-handler.d.ts +125 -0
  207. package/dist/node_modules/undici/types/round-robin-pool.d.ts +41 -0
  208. package/dist/node_modules/undici/types/snapshot-agent.d.ts +109 -0
  209. package/dist/node_modules/undici/types/socks5-proxy-agent.d.ts +25 -0
  210. package/dist/node_modules/undici/types/util.d.ts +18 -0
  211. package/dist/node_modules/undici/types/utility.d.ts +7 -0
  212. package/dist/node_modules/undici/types/webidl.d.ts +347 -0
  213. package/dist/node_modules/undici/types/websocket.d.ts +188 -0
  214. package/package.json +2 -2
@@ -0,0 +1,1610 @@
1
+ 'use strict'
2
+
3
+ /* global WebAssembly */
4
+
5
+ const assert = require('node:assert')
6
+ const util = require('../core/util.js')
7
+ const { channels } = require('../core/diagnostics.js')
8
+ const timers = require('../util/timers.js')
9
+ const {
10
+ RequestContentLengthMismatchError,
11
+ ResponseContentLengthMismatchError,
12
+ RequestAbortedError,
13
+ HeadersTimeoutError,
14
+ HeadersOverflowError,
15
+ SocketError,
16
+ InformationalError,
17
+ BodyTimeoutError,
18
+ HTTPParserError,
19
+ ResponseExceededMaxSizeError
20
+ } = require('../core/errors.js')
21
+ const {
22
+ kUrl,
23
+ kReset,
24
+ kClient,
25
+ kParser,
26
+ kBlocking,
27
+ kRunning,
28
+ kPending,
29
+ kSize,
30
+ kWriting,
31
+ kQueue,
32
+ kNoRef,
33
+ kKeepAliveDefaultTimeout,
34
+ kHostHeader,
35
+ kPendingIdx,
36
+ kRunningIdx,
37
+ kError,
38
+ kPipelining,
39
+ kSocket,
40
+ kKeepAliveTimeoutValue,
41
+ kMaxHeadersSize,
42
+ kKeepAliveMaxTimeout,
43
+ kKeepAliveTimeoutThreshold,
44
+ kHeadersTimeout,
45
+ kBodyTimeout,
46
+ kStrictContentLength,
47
+ kMaxRequests,
48
+ kCounter,
49
+ kMaxResponseSize,
50
+ kOnError,
51
+ kResume,
52
+ kHTTPContext,
53
+ kClosed
54
+ } = require('../core/symbols.js')
55
+
56
+ const constants = require('../llhttp/constants.js')
57
+ const EMPTY_BUF = Buffer.alloc(0)
58
+ const FastBuffer = Buffer[Symbol.species]
59
+ const removeAllListeners = util.removeAllListeners
60
+
61
+ let extractBody
62
+
63
+ function lazyllhttp () {
64
+ const llhttpWasmData = process.env.JEST_WORKER_ID ? require('../llhttp/llhttp-wasm.js') : undefined
65
+
66
+ let mod
67
+
68
+ // We disable wasm SIMD on ppc64 as it seems to be broken on Power 9 architectures.
69
+ let useWasmSIMD = process.arch !== 'ppc64'
70
+ // The Env Variable UNDICI_NO_WASM_SIMD allows explicitly overriding the default behavior
71
+ if (process.env.UNDICI_NO_WASM_SIMD === '1') {
72
+ useWasmSIMD = true
73
+ } else if (process.env.UNDICI_NO_WASM_SIMD === '0') {
74
+ useWasmSIMD = false
75
+ }
76
+
77
+ if (useWasmSIMD) {
78
+ try {
79
+ mod = new WebAssembly.Module(require('../llhttp/llhttp_simd-wasm.js'))
80
+ } catch {
81
+ }
82
+ }
83
+
84
+ if (!mod) {
85
+ // We could check if the error was caused by the simd option not
86
+ // being enabled, but the occurring of this other error
87
+ // * https://github.com/emscripten-core/emscripten/issues/11495
88
+ // got me to remove that check to avoid breaking Node 12.
89
+ mod = new WebAssembly.Module(llhttpWasmData || require('../llhttp/llhttp-wasm.js'))
90
+ }
91
+
92
+ return new WebAssembly.Instance(mod, {
93
+ env: {
94
+ /**
95
+ * @param {number} p
96
+ * @param {number} at
97
+ * @param {number} len
98
+ * @returns {number}
99
+ */
100
+ wasm_on_url: (p, at, len) => {
101
+ return 0
102
+ },
103
+ /**
104
+ * @param {number} p
105
+ * @param {number} at
106
+ * @param {number} len
107
+ * @returns {number}
108
+ */
109
+ wasm_on_status: (p, at, len) => {
110
+ assert(currentParser.ptr === p)
111
+ const start = at - currentBufferPtr + currentBufferRef.byteOffset
112
+ return currentParser.onStatus(new FastBuffer(currentBufferRef.buffer, start, len))
113
+ },
114
+ /**
115
+ * @param {number} p
116
+ * @returns {number}
117
+ */
118
+ wasm_on_message_begin: (p) => {
119
+ assert(currentParser.ptr === p)
120
+ return currentParser.onMessageBegin()
121
+ },
122
+ /**
123
+ * @param {number} p
124
+ * @param {number} at
125
+ * @param {number} len
126
+ * @returns {number}
127
+ */
128
+ wasm_on_header_field: (p, at, len) => {
129
+ assert(currentParser.ptr === p)
130
+ const start = at - currentBufferPtr + currentBufferRef.byteOffset
131
+ return currentParser.onHeaderField(new FastBuffer(currentBufferRef.buffer, start, len))
132
+ },
133
+ /**
134
+ * @param {number} p
135
+ * @param {number} at
136
+ * @param {number} len
137
+ * @returns {number}
138
+ */
139
+ wasm_on_header_value: (p, at, len) => {
140
+ assert(currentParser.ptr === p)
141
+ const start = at - currentBufferPtr + currentBufferRef.byteOffset
142
+ return currentParser.onHeaderValue(new FastBuffer(currentBufferRef.buffer, start, len))
143
+ },
144
+ /**
145
+ * @param {number} p
146
+ * @param {number} statusCode
147
+ * @param {0|1} upgrade
148
+ * @param {0|1} shouldKeepAlive
149
+ * @returns {number}
150
+ */
151
+ wasm_on_headers_complete: (p, statusCode, upgrade, shouldKeepAlive) => {
152
+ assert(currentParser.ptr === p)
153
+ return currentParser.onHeadersComplete(statusCode, upgrade === 1, shouldKeepAlive === 1)
154
+ },
155
+ /**
156
+ * @param {number} p
157
+ * @param {number} at
158
+ * @param {number} len
159
+ * @returns {number}
160
+ */
161
+ wasm_on_body: (p, at, len) => {
162
+ assert(currentParser.ptr === p)
163
+ const start = at - currentBufferPtr + currentBufferRef.byteOffset
164
+ return currentParser.onBody(new FastBuffer(currentBufferRef.buffer, start, len))
165
+ },
166
+ /**
167
+ * @param {number} p
168
+ * @returns {number}
169
+ */
170
+ wasm_on_message_complete: (p) => {
171
+ assert(currentParser.ptr === p)
172
+ return currentParser.onMessageComplete()
173
+ }
174
+
175
+ }
176
+ })
177
+ }
178
+
179
+ let llhttpInstance = null
180
+
181
+ /**
182
+ * @type {Parser|null}
183
+ */
184
+ let currentParser = null
185
+ let currentBufferRef = null
186
+ /**
187
+ * @type {number}
188
+ */
189
+ let currentBufferSize = 0
190
+ let currentBufferPtr = null
191
+
192
+ const USE_NATIVE_TIMER = 0
193
+ const USE_FAST_TIMER = 1
194
+
195
+ // Use fast timers for headers and body to take eventual event loop
196
+ // latency into account.
197
+ const TIMEOUT_HEADERS = 2 | USE_FAST_TIMER
198
+ const TIMEOUT_BODY = 4 | USE_FAST_TIMER
199
+
200
+ // Use native timers to ignore event loop latency for keep-alive
201
+ // handling.
202
+ const TIMEOUT_KEEP_ALIVE = 8 | USE_NATIVE_TIMER
203
+
204
+ class Parser {
205
+ /**
206
+ * @param {import('./client.js')} client
207
+ * @param {import('net').Socket} socket
208
+ * @param {*} llhttp
209
+ */
210
+ constructor (client, socket, { exports }) {
211
+ this.llhttp = exports
212
+ this.ptr = this.llhttp.llhttp_alloc(constants.TYPE.RESPONSE)
213
+ this.client = client
214
+ /**
215
+ * @type {import('net').Socket}
216
+ */
217
+ this.socket = socket
218
+ this.timeout = null
219
+ this.timeoutValue = null
220
+ this.timeoutType = null
221
+ this.statusCode = 0
222
+ this.statusText = ''
223
+ this.upgrade = false
224
+ this.headers = []
225
+ this.headersSize = 0
226
+ this.headersMaxSize = client[kMaxHeadersSize]
227
+ this.shouldKeepAlive = false
228
+ this.paused = false
229
+ this.resume = this.resume.bind(this)
230
+
231
+ this.bytesRead = 0
232
+
233
+ this.keepAlive = ''
234
+ this.contentLength = ''
235
+ this.connection = ''
236
+ this.maxResponseSize = client[kMaxResponseSize]
237
+ }
238
+
239
+ setTimeout (delay, type) {
240
+ // If the existing timer and the new timer are of different timer type
241
+ // (fast or native) or have different delay, we need to clear the existing
242
+ // timer and set a new one.
243
+ if (
244
+ delay !== this.timeoutValue ||
245
+ (type & USE_FAST_TIMER) ^ (this.timeoutType & USE_FAST_TIMER)
246
+ ) {
247
+ // If a timeout is already set, clear it with clearTimeout of the fast
248
+ // timer implementation, as it can clear fast and native timers.
249
+ if (this.timeout) {
250
+ timers.clearTimeout(this.timeout)
251
+ this.timeout = null
252
+ }
253
+
254
+ if (delay) {
255
+ if (type & USE_FAST_TIMER) {
256
+ this.timeout = timers.setFastTimeout(onParserTimeout, delay, new WeakRef(this))
257
+ } else {
258
+ this.timeout = setTimeout(onParserTimeout, delay, new WeakRef(this))
259
+ this.timeout?.unref()
260
+ }
261
+ }
262
+
263
+ this.timeoutValue = delay
264
+ } else if (this.timeout) {
265
+ if (this.timeout.refresh) {
266
+ this.timeout.refresh()
267
+ }
268
+ }
269
+
270
+ this.timeoutType = type
271
+ }
272
+
273
+ resume () {
274
+ if (this.socket.destroyed || !this.paused) {
275
+ return
276
+ }
277
+
278
+ assert(this.ptr != null)
279
+ assert(currentParser === null)
280
+
281
+ this.llhttp.llhttp_resume(this.ptr)
282
+
283
+ assert(this.timeoutType === TIMEOUT_BODY)
284
+ if (this.timeout) {
285
+ if (this.timeout.refresh) {
286
+ this.timeout.refresh()
287
+ }
288
+ }
289
+
290
+ this.paused = false
291
+ this.execute(this.socket.read() || EMPTY_BUF) // Flush parser.
292
+ this.readMore()
293
+ }
294
+
295
+ readMore () {
296
+ while (!this.paused && this.ptr) {
297
+ const chunk = this.socket.read()
298
+ if (chunk === null) {
299
+ break
300
+ }
301
+ this.execute(chunk)
302
+ }
303
+ }
304
+
305
+ /**
306
+ * @param {Buffer} chunk
307
+ */
308
+ execute (chunk) {
309
+ assert(currentParser === null)
310
+ assert(this.ptr != null)
311
+ assert(!this.paused)
312
+
313
+ const { socket, llhttp } = this
314
+
315
+ // Allocate a new buffer if the current buffer is too small.
316
+ if (chunk.length > currentBufferSize) {
317
+ if (currentBufferPtr) {
318
+ llhttp.free(currentBufferPtr)
319
+ }
320
+ // Allocate a buffer that is a multiple of 4096 bytes.
321
+ currentBufferSize = Math.ceil(chunk.length / 4096) * 4096
322
+ currentBufferPtr = llhttp.malloc(currentBufferSize)
323
+ }
324
+
325
+ new Uint8Array(llhttp.memory.buffer, currentBufferPtr, currentBufferSize).set(chunk)
326
+
327
+ // Call `execute` on the wasm parser.
328
+ // We pass the `llhttp_parser` pointer address, the pointer address of buffer view data,
329
+ // and finally the length of bytes to parse.
330
+ // The return value is an error code or `constants.ERROR.OK`.
331
+ try {
332
+ let ret
333
+
334
+ try {
335
+ currentBufferRef = chunk
336
+ currentParser = this
337
+ ret = llhttp.llhttp_execute(this.ptr, currentBufferPtr, chunk.length)
338
+ } finally {
339
+ currentParser = null
340
+ currentBufferRef = null
341
+ }
342
+
343
+ if (ret !== constants.ERROR.OK) {
344
+ const data = chunk.subarray(llhttp.llhttp_get_error_pos(this.ptr) - currentBufferPtr)
345
+
346
+ if (ret === constants.ERROR.PAUSED_UPGRADE) {
347
+ this.onUpgrade(data)
348
+ } else if (ret === constants.ERROR.PAUSED) {
349
+ this.paused = true
350
+ socket.unshift(data)
351
+ } else {
352
+ const ptr = llhttp.llhttp_get_error_reason(this.ptr)
353
+ let message = ''
354
+ if (ptr) {
355
+ const len = new Uint8Array(llhttp.memory.buffer, ptr).indexOf(0)
356
+ message =
357
+ 'Response does not match the HTTP/1.1 protocol (' +
358
+ Buffer.from(llhttp.memory.buffer, ptr, len).toString() +
359
+ ')'
360
+ }
361
+ throw new HTTPParserError(message, constants.ERROR[ret], data)
362
+ }
363
+ }
364
+ } catch (err) {
365
+ util.destroy(socket, err)
366
+ }
367
+ }
368
+
369
+ destroy () {
370
+ assert(currentParser === null)
371
+ assert(this.ptr != null)
372
+
373
+ this.llhttp.llhttp_free(this.ptr)
374
+ this.ptr = null
375
+
376
+ this.timeout && timers.clearTimeout(this.timeout)
377
+ this.timeout = null
378
+ this.timeoutValue = null
379
+ this.timeoutType = null
380
+
381
+ this.paused = false
382
+ }
383
+
384
+ /**
385
+ * @param {Buffer} buf
386
+ * @returns {0}
387
+ */
388
+ onStatus (buf) {
389
+ this.statusText = buf.toString()
390
+ return 0
391
+ }
392
+
393
+ /**
394
+ * @returns {0|-1}
395
+ */
396
+ onMessageBegin () {
397
+ const { socket, client } = this
398
+
399
+ if (socket.destroyed) {
400
+ return -1
401
+ }
402
+
403
+ const request = client[kQueue][client[kRunningIdx]]
404
+ if (!request) {
405
+ return -1
406
+ }
407
+ request.onResponseStarted()
408
+
409
+ return 0
410
+ }
411
+
412
+ /**
413
+ * @param {Buffer} buf
414
+ * @returns {number}
415
+ */
416
+ onHeaderField (buf) {
417
+ const len = this.headers.length
418
+
419
+ if ((len & 1) === 0) {
420
+ this.headers.push(buf)
421
+ } else {
422
+ this.headers[len - 1] = Buffer.concat([this.headers[len - 1], buf])
423
+ }
424
+
425
+ this.trackHeader(buf.length)
426
+
427
+ return 0
428
+ }
429
+
430
+ /**
431
+ * @param {Buffer} buf
432
+ * @returns {number}
433
+ */
434
+ onHeaderValue (buf) {
435
+ let len = this.headers.length
436
+
437
+ if ((len & 1) === 1) {
438
+ this.headers.push(buf)
439
+ len += 1
440
+ } else {
441
+ this.headers[len - 1] = Buffer.concat([this.headers[len - 1], buf])
442
+ }
443
+
444
+ const key = this.headers[len - 2]
445
+ if (key.length === 10) {
446
+ const headerName = util.bufferToLowerCasedHeaderName(key)
447
+ if (headerName === 'keep-alive') {
448
+ this.keepAlive += buf.toString()
449
+ } else if (headerName === 'connection') {
450
+ this.connection += buf.toString()
451
+ }
452
+ } else if (key.length === 14 && util.bufferToLowerCasedHeaderName(key) === 'content-length') {
453
+ this.contentLength += buf.toString()
454
+ }
455
+
456
+ this.trackHeader(buf.length)
457
+
458
+ return 0
459
+ }
460
+
461
+ /**
462
+ * @param {number} len
463
+ */
464
+ trackHeader (len) {
465
+ this.headersSize += len
466
+ if (this.headersSize >= this.headersMaxSize) {
467
+ util.destroy(this.socket, new HeadersOverflowError())
468
+ }
469
+ }
470
+
471
+ /**
472
+ * @param {Buffer} head
473
+ */
474
+ onUpgrade (head) {
475
+ const { upgrade, client, socket, headers, statusCode } = this
476
+
477
+ assert(upgrade)
478
+ assert(client[kSocket] === socket)
479
+ assert(!socket.destroyed)
480
+ assert(!this.paused)
481
+ assert((headers.length & 1) === 0)
482
+
483
+ const request = client[kQueue][client[kRunningIdx]]
484
+ assert(request)
485
+ assert(request.upgrade || request.method === 'CONNECT')
486
+
487
+ this.statusCode = 0
488
+ this.statusText = ''
489
+ this.shouldKeepAlive = false
490
+
491
+ this.headers = []
492
+ this.headersSize = 0
493
+
494
+ socket.unshift(head)
495
+
496
+ socket[kParser].destroy()
497
+ socket[kParser] = null
498
+
499
+ socket[kClient] = null
500
+ socket[kError] = null
501
+
502
+ removeAllListeners(socket)
503
+
504
+ client[kSocket] = null
505
+ client[kHTTPContext] = null // TODO (fix): This is hacky...
506
+ client[kQueue][client[kRunningIdx]++] = null
507
+ client.emit('disconnect', client[kUrl], [client], new InformationalError('upgrade'))
508
+
509
+ try {
510
+ request.onUpgrade(statusCode, headers, socket)
511
+ } catch (err) {
512
+ util.destroy(socket, err)
513
+ }
514
+
515
+ client[kResume]()
516
+ }
517
+
518
+ /**
519
+ * @param {number} statusCode
520
+ * @param {boolean} upgrade
521
+ * @param {boolean} shouldKeepAlive
522
+ * @returns {number}
523
+ */
524
+ onHeadersComplete (statusCode, upgrade, shouldKeepAlive) {
525
+ const { client, socket, headers, statusText } = this
526
+
527
+ if (socket.destroyed) {
528
+ return -1
529
+ }
530
+
531
+ const request = client[kQueue][client[kRunningIdx]]
532
+
533
+ if (!request) {
534
+ return -1
535
+ }
536
+
537
+ assert(!this.upgrade)
538
+ assert(this.statusCode < 200)
539
+
540
+ if (statusCode === 100) {
541
+ util.destroy(socket, new SocketError('bad response', util.getSocketInfo(socket)))
542
+ return -1
543
+ }
544
+
545
+ /* this can only happen if server is misbehaving */
546
+ if (upgrade && !request.upgrade) {
547
+ util.destroy(socket, new SocketError('bad upgrade', util.getSocketInfo(socket)))
548
+ return -1
549
+ }
550
+
551
+ assert(this.timeoutType === TIMEOUT_HEADERS)
552
+
553
+ this.statusCode = statusCode
554
+ this.shouldKeepAlive = (
555
+ shouldKeepAlive ||
556
+ // Override llhttp value which does not allow keepAlive for HEAD.
557
+ (request.method === 'HEAD' && !socket[kReset] && this.connection.toLowerCase() === 'keep-alive')
558
+ )
559
+
560
+ if (this.statusCode >= 200) {
561
+ const bodyTimeout = request.bodyTimeout != null
562
+ ? request.bodyTimeout
563
+ : client[kBodyTimeout]
564
+ this.setTimeout(bodyTimeout, TIMEOUT_BODY)
565
+ } else if (this.timeout) {
566
+ if (this.timeout.refresh) {
567
+ this.timeout.refresh()
568
+ }
569
+ }
570
+
571
+ if (request.method === 'CONNECT') {
572
+ assert(client[kRunning] === 1)
573
+ this.upgrade = true
574
+ return 2
575
+ }
576
+
577
+ if (upgrade) {
578
+ assert(client[kRunning] === 1)
579
+ this.upgrade = true
580
+ return 2
581
+ }
582
+
583
+ assert((this.headers.length & 1) === 0)
584
+ this.headers = []
585
+ this.headersSize = 0
586
+
587
+ if (this.shouldKeepAlive && client[kPipelining]) {
588
+ const keepAliveTimeout = this.keepAlive ? util.parseKeepAliveTimeout(this.keepAlive) : null
589
+
590
+ if (keepAliveTimeout != null) {
591
+ const timeout = Math.min(
592
+ keepAliveTimeout - client[kKeepAliveTimeoutThreshold],
593
+ client[kKeepAliveMaxTimeout]
594
+ )
595
+ if (timeout <= 0) {
596
+ socket[kReset] = true
597
+ } else {
598
+ client[kKeepAliveTimeoutValue] = timeout
599
+ }
600
+ } else {
601
+ client[kKeepAliveTimeoutValue] = client[kKeepAliveDefaultTimeout]
602
+ }
603
+ } else {
604
+ // Stop more requests from being dispatched.
605
+ socket[kReset] = true
606
+ }
607
+
608
+ const pause = request.onHeaders(statusCode, headers, this.resume, statusText) === false
609
+
610
+ if (request.aborted) {
611
+ return -1
612
+ }
613
+
614
+ if (request.method === 'HEAD') {
615
+ return 1
616
+ }
617
+
618
+ if (statusCode < 200) {
619
+ return 1
620
+ }
621
+
622
+ if (socket[kBlocking]) {
623
+ socket[kBlocking] = false
624
+ client[kResume]()
625
+ }
626
+
627
+ return pause ? constants.ERROR.PAUSED : 0
628
+ }
629
+
630
+ /**
631
+ * @param {Buffer} buf
632
+ * @returns {number}
633
+ */
634
+ onBody (buf) {
635
+ const { client, socket, statusCode, maxResponseSize } = this
636
+
637
+ if (socket.destroyed) {
638
+ return -1
639
+ }
640
+
641
+ const request = client[kQueue][client[kRunningIdx]]
642
+ assert(request)
643
+
644
+ assert(this.timeoutType === TIMEOUT_BODY)
645
+ if (this.timeout) {
646
+ if (this.timeout.refresh) {
647
+ this.timeout.refresh()
648
+ }
649
+ }
650
+
651
+ assert(statusCode >= 200)
652
+
653
+ if (maxResponseSize > -1 && this.bytesRead + buf.length > maxResponseSize) {
654
+ util.destroy(socket, new ResponseExceededMaxSizeError())
655
+ return -1
656
+ }
657
+
658
+ this.bytesRead += buf.length
659
+
660
+ if (request.onData(buf) === false) {
661
+ return constants.ERROR.PAUSED
662
+ }
663
+
664
+ return 0
665
+ }
666
+
667
+ /**
668
+ * @returns {number}
669
+ */
670
+ onMessageComplete () {
671
+ const { client, socket, statusCode, upgrade, headers, contentLength, bytesRead, shouldKeepAlive } = this
672
+
673
+ if (socket.destroyed && (!statusCode || shouldKeepAlive)) {
674
+ return -1
675
+ }
676
+
677
+ if (upgrade) {
678
+ return 0
679
+ }
680
+
681
+ assert(statusCode >= 100)
682
+ assert((this.headers.length & 1) === 0)
683
+
684
+ const request = client[kQueue][client[kRunningIdx]]
685
+ assert(request)
686
+
687
+ this.statusCode = 0
688
+ this.statusText = ''
689
+ this.bytesRead = 0
690
+ this.contentLength = ''
691
+ this.keepAlive = ''
692
+ this.connection = ''
693
+
694
+ this.headers = []
695
+ this.headersSize = 0
696
+
697
+ if (statusCode < 200) {
698
+ return 0
699
+ }
700
+
701
+ if (request.method !== 'HEAD' && contentLength && bytesRead !== parseInt(contentLength, 10)) {
702
+ util.destroy(socket, new ResponseContentLengthMismatchError())
703
+ return -1
704
+ }
705
+
706
+ request.onComplete(headers)
707
+
708
+ client[kQueue][client[kRunningIdx]++] = null
709
+
710
+ if (socket[kWriting]) {
711
+ assert(client[kRunning] === 0)
712
+ // Response completed before request.
713
+ util.destroy(socket, new InformationalError('reset'))
714
+ return constants.ERROR.PAUSED
715
+ } else if (!shouldKeepAlive) {
716
+ util.destroy(socket, new InformationalError('reset'))
717
+ return constants.ERROR.PAUSED
718
+ } else if (socket[kReset] && client[kRunning] === 0) {
719
+ // Destroy socket once all requests have completed.
720
+ // The request at the tail of the pipeline is the one
721
+ // that requested reset and no further requests should
722
+ // have been queued since then.
723
+ util.destroy(socket, new InformationalError('reset'))
724
+ return constants.ERROR.PAUSED
725
+ } else if (client[kPipelining] == null || client[kPipelining] === 1) {
726
+ // We must wait a full event loop cycle to reuse this socket to make sure
727
+ // that non-spec compliant servers are not closing the connection even if they
728
+ // said they won't.
729
+ setImmediate(client[kResume])
730
+ } else {
731
+ client[kResume]()
732
+ }
733
+
734
+ return 0
735
+ }
736
+ }
737
+
738
+ function onParserTimeout (parserWeakRef) {
739
+ const parser = parserWeakRef.deref()
740
+ if (!parser) {
741
+ return
742
+ }
743
+
744
+ const { socket, timeoutType, client, paused } = parser
745
+
746
+ if (timeoutType === TIMEOUT_HEADERS) {
747
+ if (!socket[kWriting] || socket.writableNeedDrain || client[kRunning] > 1) {
748
+ assert(!paused, 'cannot be paused while waiting for headers')
749
+ util.destroy(socket, new HeadersTimeoutError())
750
+ }
751
+ } else if (timeoutType === TIMEOUT_BODY) {
752
+ if (!paused) {
753
+ util.destroy(socket, new BodyTimeoutError())
754
+ }
755
+ } else if (timeoutType === TIMEOUT_KEEP_ALIVE) {
756
+ assert(client[kRunning] === 0 && client[kKeepAliveTimeoutValue])
757
+ util.destroy(socket, new InformationalError('socket idle timeout'))
758
+ }
759
+ }
760
+
761
+ /**
762
+ * @param {import ('./client.js')} client
763
+ * @param {import('net').Socket} socket
764
+ * @returns
765
+ */
766
+ function connectH1 (client, socket) {
767
+ client[kSocket] = socket
768
+
769
+ if (!llhttpInstance) {
770
+ llhttpInstance = lazyllhttp()
771
+ }
772
+
773
+ if (socket.errored) {
774
+ throw socket.errored
775
+ }
776
+
777
+ if (socket.destroyed) {
778
+ throw new SocketError('destroyed')
779
+ }
780
+
781
+ socket[kNoRef] = false
782
+ socket[kWriting] = false
783
+ socket[kReset] = false
784
+ socket[kBlocking] = false
785
+ socket[kParser] = new Parser(client, socket, llhttpInstance)
786
+
787
+ util.addListener(socket, 'error', onHttpSocketError)
788
+ util.addListener(socket, 'readable', onHttpSocketReadable)
789
+ util.addListener(socket, 'end', onHttpSocketEnd)
790
+ util.addListener(socket, 'close', onHttpSocketClose)
791
+
792
+ socket[kClosed] = false
793
+ socket.on('close', onSocketClose)
794
+
795
+ return {
796
+ version: 'h1',
797
+ defaultPipelining: 1,
798
+ write (request) {
799
+ return writeH1(client, request)
800
+ },
801
+ resume () {
802
+ resumeH1(client)
803
+ },
804
+ /**
805
+ * @param {Error|undefined} err
806
+ * @param {() => void} callback
807
+ */
808
+ destroy (err, callback) {
809
+ if (socket[kClosed]) {
810
+ queueMicrotask(callback)
811
+ } else {
812
+ socket.on('close', callback)
813
+ socket.destroy(err)
814
+ }
815
+ },
816
+ /**
817
+ * @returns {boolean}
818
+ */
819
+ get destroyed () {
820
+ return socket.destroyed
821
+ },
822
+ /**
823
+ * @param {import('../core/request.js')} request
824
+ * @returns {boolean}
825
+ */
826
+ busy (request) {
827
+ if (socket[kWriting] || socket[kReset] || socket[kBlocking]) {
828
+ return true
829
+ }
830
+
831
+ if (request) {
832
+ if (client[kRunning] > 0 && !request.idempotent) {
833
+ // Non-idempotent request cannot be retried.
834
+ // Ensure that no other requests are inflight and
835
+ // could cause failure.
836
+ return true
837
+ }
838
+
839
+ if (client[kRunning] > 0 && (request.upgrade || request.method === 'CONNECT')) {
840
+ // Don't dispatch an upgrade until all preceding requests have completed.
841
+ // A misbehaving server might upgrade the connection before all pipelined
842
+ // request has completed.
843
+ return true
844
+ }
845
+
846
+ if (client[kRunning] > 0 && util.bodyLength(request.body) !== 0 &&
847
+ (util.isStream(request.body) || util.isAsyncIterable(request.body) || util.isFormDataLike(request.body))) {
848
+ // Request with stream or iterator body can error while other requests
849
+ // are inflight and indirectly error those as well.
850
+ // Ensure this doesn't happen by waiting for inflight
851
+ // to complete before dispatching.
852
+
853
+ // Request with stream or iterator body cannot be retried.
854
+ // Ensure that no other requests are inflight and
855
+ // could cause failure.
856
+ return true
857
+ }
858
+ }
859
+
860
+ return false
861
+ }
862
+ }
863
+ }
864
+
865
+ function onHttpSocketError (err) {
866
+ assert(err.code !== 'ERR_TLS_CERT_ALTNAME_INVALID')
867
+
868
+ const parser = this[kParser]
869
+
870
+ // On Mac OS, we get an ECONNRESET even if there is a full body to be forwarded
871
+ // to the user.
872
+ if (err.code === 'ECONNRESET' && parser.statusCode && !parser.shouldKeepAlive) {
873
+ // We treat all incoming data so for as a valid response.
874
+ parser.onMessageComplete()
875
+ return
876
+ }
877
+
878
+ this[kError] = err
879
+
880
+ this[kClient][kOnError](err)
881
+ }
882
+
883
+ function onHttpSocketReadable () {
884
+ this[kParser]?.readMore()
885
+ }
886
+
887
+ function onHttpSocketEnd () {
888
+ const parser = this[kParser]
889
+
890
+ if (parser.statusCode && !parser.shouldKeepAlive) {
891
+ // We treat all incoming data so far as a valid response.
892
+ parser.onMessageComplete()
893
+ return
894
+ }
895
+
896
+ util.destroy(this, new SocketError('other side closed', util.getSocketInfo(this)))
897
+ }
898
+
899
+ function onHttpSocketClose () {
900
+ const parser = this[kParser]
901
+
902
+ if (parser) {
903
+ if (!this[kError] && parser.statusCode && !parser.shouldKeepAlive) {
904
+ // We treat all incoming data so far as a valid response.
905
+ parser.onMessageComplete()
906
+ }
907
+
908
+ this[kParser].destroy()
909
+ this[kParser] = null
910
+ }
911
+
912
+ const err = this[kError] || new SocketError('closed', util.getSocketInfo(this))
913
+
914
+ const client = this[kClient]
915
+
916
+ client[kSocket] = null
917
+ client[kHTTPContext] = null // TODO (fix): This is hacky...
918
+
919
+ if (client.destroyed) {
920
+ assert(client[kPending] === 0)
921
+
922
+ // Fail entire queue.
923
+ const requests = client[kQueue].splice(client[kRunningIdx])
924
+ for (let i = 0; i < requests.length; i++) {
925
+ const request = requests[i]
926
+ util.errorRequest(client, request, err)
927
+ }
928
+ } else if (client[kRunning] > 0 && err.code !== 'UND_ERR_INFO') {
929
+ // Fail head of pipeline.
930
+ const request = client[kQueue][client[kRunningIdx]]
931
+ client[kQueue][client[kRunningIdx]++] = null
932
+
933
+ util.errorRequest(client, request, err)
934
+ }
935
+
936
+ client[kPendingIdx] = client[kRunningIdx]
937
+
938
+ assert(client[kRunning] === 0)
939
+
940
+ client.emit('disconnect', client[kUrl], [client], err)
941
+
942
+ client[kResume]()
943
+ }
944
+
945
+ function onSocketClose () {
946
+ this[kClosed] = true
947
+ }
948
+
949
+ /**
950
+ * @param {import('./client.js')} client
951
+ */
952
+ function resumeH1 (client) {
953
+ const socket = client[kSocket]
954
+
955
+ if (socket && !socket.destroyed) {
956
+ if (client[kSize] === 0) {
957
+ if (!socket[kNoRef] && socket.unref) {
958
+ socket.unref()
959
+ socket[kNoRef] = true
960
+ }
961
+ } else if (socket[kNoRef] && socket.ref) {
962
+ socket.ref()
963
+ socket[kNoRef] = false
964
+ }
965
+
966
+ if (client[kSize] === 0) {
967
+ if (socket[kParser].timeoutType !== TIMEOUT_KEEP_ALIVE) {
968
+ socket[kParser].setTimeout(client[kKeepAliveTimeoutValue], TIMEOUT_KEEP_ALIVE)
969
+ }
970
+ } else if (client[kRunning] > 0 && socket[kParser].statusCode < 200) {
971
+ if (socket[kParser].timeoutType !== TIMEOUT_HEADERS) {
972
+ const request = client[kQueue][client[kRunningIdx]]
973
+ const headersTimeout = request.headersTimeout != null
974
+ ? request.headersTimeout
975
+ : client[kHeadersTimeout]
976
+ socket[kParser].setTimeout(headersTimeout, TIMEOUT_HEADERS)
977
+ }
978
+ }
979
+ }
980
+ }
981
+
982
+ // https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2
983
+ function shouldSendContentLength (method) {
984
+ return method !== 'GET' && method !== 'HEAD' && method !== 'OPTIONS' && method !== 'TRACE' && method !== 'CONNECT'
985
+ }
986
+
987
+ /**
988
+ * @param {import('./client.js')} client
989
+ * @param {import('../core/request.js')} request
990
+ * @returns
991
+ */
992
+ function writeH1 (client, request) {
993
+ const { method, path, host, upgrade, blocking, reset } = request
994
+
995
+ let { body, headers, contentLength } = request
996
+
997
+ // https://tools.ietf.org/html/rfc7231#section-4.3.1
998
+ // https://tools.ietf.org/html/rfc7231#section-4.3.2
999
+ // https://tools.ietf.org/html/rfc7231#section-4.3.5
1000
+
1001
+ // Sending a payload body on a request that does not
1002
+ // expect it can cause undefined behavior on some
1003
+ // servers and corrupt connection state. Do not
1004
+ // re-use the connection for further requests.
1005
+
1006
+ const expectsPayload = (
1007
+ method === 'PUT' ||
1008
+ method === 'POST' ||
1009
+ method === 'PATCH' ||
1010
+ method === 'QUERY' ||
1011
+ method === 'PROPFIND' ||
1012
+ method === 'PROPPATCH'
1013
+ )
1014
+
1015
+ if (util.isFormDataLike(body)) {
1016
+ if (!extractBody) {
1017
+ extractBody = require('../web/fetch/body.js').extractBody
1018
+ }
1019
+
1020
+ const [bodyStream, contentType] = extractBody(body)
1021
+ if (request.contentType == null) {
1022
+ headers.push('content-type', contentType)
1023
+ }
1024
+ body = bodyStream.stream
1025
+ contentLength = bodyStream.length
1026
+ } else if (util.isBlobLike(body) && request.contentType == null && body.type) {
1027
+ headers.push('content-type', body.type)
1028
+ }
1029
+
1030
+ if (body && typeof body.read === 'function') {
1031
+ // Try to read EOF in order to get length.
1032
+ body.read(0)
1033
+ }
1034
+
1035
+ const bodyLength = util.bodyLength(body)
1036
+
1037
+ contentLength = bodyLength ?? contentLength
1038
+
1039
+ if (contentLength === null) {
1040
+ contentLength = request.contentLength
1041
+ }
1042
+
1043
+ if (contentLength === 0 && !expectsPayload) {
1044
+ // https://tools.ietf.org/html/rfc7230#section-3.3.2
1045
+ // A user agent SHOULD NOT send a Content-Length header field when
1046
+ // the request message does not contain a payload body and the method
1047
+ // semantics do not anticipate such a body.
1048
+
1049
+ contentLength = null
1050
+ }
1051
+
1052
+ // https://github.com/nodejs/undici/issues/2046
1053
+ // A user agent may send a Content-Length header with 0 value, this should be allowed.
1054
+ if (shouldSendContentLength(method) && contentLength > 0 && request.contentLength !== null && request.contentLength !== contentLength) {
1055
+ if (client[kStrictContentLength]) {
1056
+ util.errorRequest(client, request, new RequestContentLengthMismatchError())
1057
+ return false
1058
+ }
1059
+
1060
+ process.emitWarning(new RequestContentLengthMismatchError())
1061
+ }
1062
+
1063
+ const socket = client[kSocket]
1064
+
1065
+ /**
1066
+ * @param {Error} [err]
1067
+ * @returns {void}
1068
+ */
1069
+ const abort = (err) => {
1070
+ if (request.aborted || request.completed) {
1071
+ return
1072
+ }
1073
+
1074
+ util.errorRequest(client, request, err || new RequestAbortedError())
1075
+
1076
+ util.destroy(body)
1077
+ util.destroy(socket, new InformationalError('aborted'))
1078
+ }
1079
+
1080
+ try {
1081
+ request.onConnect(abort)
1082
+ } catch (err) {
1083
+ util.errorRequest(client, request, err)
1084
+ }
1085
+
1086
+ if (request.aborted) {
1087
+ return false
1088
+ }
1089
+
1090
+ if (method === 'HEAD') {
1091
+ // https://github.com/mcollina/undici/issues/258
1092
+ // Close after a HEAD request to interop with misbehaving servers
1093
+ // that may send a body in the response.
1094
+
1095
+ socket[kReset] = true
1096
+ }
1097
+
1098
+ if (upgrade || method === 'CONNECT') {
1099
+ // On CONNECT or upgrade, block pipeline from dispatching further
1100
+ // requests on this connection.
1101
+
1102
+ socket[kReset] = true
1103
+ }
1104
+
1105
+ if (reset != null) {
1106
+ socket[kReset] = reset
1107
+ }
1108
+
1109
+ if (client[kMaxRequests] && socket[kCounter]++ >= client[kMaxRequests]) {
1110
+ socket[kReset] = true
1111
+ }
1112
+
1113
+ if (blocking) {
1114
+ socket[kBlocking] = true
1115
+ }
1116
+
1117
+ if (socket.setTypeOfService) {
1118
+ socket.setTypeOfService(request.typeOfService)
1119
+ }
1120
+
1121
+ let header = `${method} ${path} HTTP/1.1\r\n`
1122
+
1123
+ if (typeof host === 'string') {
1124
+ header += `host: ${host}\r\n`
1125
+ } else {
1126
+ header += client[kHostHeader]
1127
+ }
1128
+
1129
+ if (upgrade) {
1130
+ header += `connection: upgrade\r\nupgrade: ${upgrade}\r\n`
1131
+ } else if (client[kPipelining] && !socket[kReset]) {
1132
+ header += 'connection: keep-alive\r\n'
1133
+ } else {
1134
+ header += 'connection: close\r\n'
1135
+ }
1136
+
1137
+ if (Array.isArray(headers)) {
1138
+ for (let n = 0; n < headers.length; n += 2) {
1139
+ const key = headers[n + 0]
1140
+ const val = headers[n + 1]
1141
+
1142
+ if (Array.isArray(val)) {
1143
+ for (let i = 0; i < val.length; i++) {
1144
+ header += `${key}: ${val[i]}\r\n`
1145
+ }
1146
+ } else {
1147
+ header += `${key}: ${val}\r\n`
1148
+ }
1149
+ }
1150
+ }
1151
+
1152
+ if (channels.sendHeaders.hasSubscribers) {
1153
+ channels.sendHeaders.publish({ request, headers: header, socket })
1154
+ }
1155
+
1156
+ if (!body || bodyLength === 0) {
1157
+ writeBuffer(abort, null, client, request, socket, contentLength, header, expectsPayload)
1158
+ } else if (util.isBuffer(body)) {
1159
+ writeBuffer(abort, body, client, request, socket, contentLength, header, expectsPayload)
1160
+ } else if (util.isBlobLike(body)) {
1161
+ if (typeof body.stream === 'function') {
1162
+ writeIterable(abort, body.stream(), client, request, socket, contentLength, header, expectsPayload)
1163
+ } else {
1164
+ writeBlob(abort, body, client, request, socket, contentLength, header, expectsPayload)
1165
+ }
1166
+ } else if (util.isStream(body)) {
1167
+ writeStream(abort, body, client, request, socket, contentLength, header, expectsPayload)
1168
+ } else if (util.isIterable(body)) {
1169
+ writeIterable(abort, body, client, request, socket, contentLength, header, expectsPayload)
1170
+ } else {
1171
+ assert(false)
1172
+ }
1173
+
1174
+ return true
1175
+ }
1176
+
1177
+ /**
1178
+ * @param {AbortCallback} abort
1179
+ * @param {import('stream').Stream} body
1180
+ * @param {import('./client.js')} client
1181
+ * @param {import('../core/request.js')} request
1182
+ * @param {import('net').Socket} socket
1183
+ * @param {number} contentLength
1184
+ * @param {string} header
1185
+ * @param {boolean} expectsPayload
1186
+ */
1187
+ function writeStream (abort, body, client, request, socket, contentLength, header, expectsPayload) {
1188
+ assert(contentLength !== 0 || client[kRunning] === 0, 'stream body cannot be pipelined')
1189
+
1190
+ let finished = false
1191
+
1192
+ const writer = new AsyncWriter({ abort, socket, request, contentLength, client, expectsPayload, header })
1193
+
1194
+ /**
1195
+ * @param {Buffer} chunk
1196
+ * @returns {void}
1197
+ */
1198
+ const onData = function (chunk) {
1199
+ if (finished) {
1200
+ return
1201
+ }
1202
+
1203
+ try {
1204
+ if (!writer.write(chunk) && this.pause) {
1205
+ this.pause()
1206
+ }
1207
+ } catch (err) {
1208
+ util.destroy(this, err)
1209
+ }
1210
+ }
1211
+
1212
+ /**
1213
+ * @returns {void}
1214
+ */
1215
+ const onDrain = function () {
1216
+ if (finished) {
1217
+ return
1218
+ }
1219
+
1220
+ if (body.resume) {
1221
+ body.resume()
1222
+ }
1223
+ }
1224
+
1225
+ /**
1226
+ * @returns {void}
1227
+ */
1228
+ const onClose = function () {
1229
+ // 'close' might be emitted *before* 'error' for
1230
+ // broken streams. Wait a tick to avoid this case.
1231
+ queueMicrotask(() => {
1232
+ // It's only safe to remove 'error' listener after
1233
+ // 'close'.
1234
+ body.removeListener('error', onFinished)
1235
+ })
1236
+
1237
+ if (!finished) {
1238
+ const err = new RequestAbortedError()
1239
+ queueMicrotask(() => onFinished(err))
1240
+ }
1241
+ }
1242
+
1243
+ /**
1244
+ * @param {Error} [err]
1245
+ * @returns
1246
+ */
1247
+ const onFinished = function (err) {
1248
+ if (finished) {
1249
+ return
1250
+ }
1251
+
1252
+ finished = true
1253
+
1254
+ assert(socket.destroyed || (socket[kWriting] && client[kRunning] <= 1))
1255
+
1256
+ socket
1257
+ .off('drain', onDrain)
1258
+ .off('error', onFinished)
1259
+
1260
+ body
1261
+ .removeListener('data', onData)
1262
+ .removeListener('end', onFinished)
1263
+ .removeListener('close', onClose)
1264
+
1265
+ if (!err) {
1266
+ try {
1267
+ writer.end()
1268
+ } catch (er) {
1269
+ err = er
1270
+ }
1271
+ }
1272
+
1273
+ writer.destroy(err)
1274
+
1275
+ if (err && (err.code !== 'UND_ERR_INFO' || err.message !== 'reset')) {
1276
+ util.destroy(body, err)
1277
+ } else {
1278
+ util.destroy(body)
1279
+ }
1280
+ }
1281
+
1282
+ body
1283
+ .on('data', onData)
1284
+ .on('end', onFinished)
1285
+ .on('error', onFinished)
1286
+ .on('close', onClose)
1287
+
1288
+ if (body.resume) {
1289
+ body.resume()
1290
+ }
1291
+
1292
+ socket
1293
+ .on('drain', onDrain)
1294
+ .on('error', onFinished)
1295
+
1296
+ if (body.errorEmitted ?? body.errored) {
1297
+ setImmediate(onFinished, body.errored)
1298
+ } else if (body.endEmitted ?? body.readableEnded) {
1299
+ setImmediate(onFinished, null)
1300
+ }
1301
+
1302
+ if (body.closeEmitted ?? body.closed) {
1303
+ setImmediate(onClose)
1304
+ }
1305
+ }
1306
+
1307
+ /**
1308
+ * @typedef AbortCallback
1309
+ * @type {Function}
1310
+ * @param {Error} [err]
1311
+ * @returns {void}
1312
+ */
1313
+
1314
+ /**
1315
+ * @param {AbortCallback} abort
1316
+ * @param {Uint8Array|null} body
1317
+ * @param {import('./client.js')} client
1318
+ * @param {import('../core/request.js')} request
1319
+ * @param {import('net').Socket} socket
1320
+ * @param {number} contentLength
1321
+ * @param {string} header
1322
+ * @param {boolean} expectsPayload
1323
+ * @returns {void}
1324
+ */
1325
+ function writeBuffer (abort, body, client, request, socket, contentLength, header, expectsPayload) {
1326
+ try {
1327
+ if (!body) {
1328
+ if (contentLength === 0) {
1329
+ socket.write(`${header}content-length: 0\r\n\r\n`, 'latin1')
1330
+ } else {
1331
+ assert(contentLength === null, 'no body must not have content length')
1332
+ socket.write(`${header}\r\n`, 'latin1')
1333
+ }
1334
+ } else if (util.isBuffer(body)) {
1335
+ assert(contentLength === body.byteLength, 'buffer body must have content length')
1336
+
1337
+ socket.cork()
1338
+ socket.write(`${header}content-length: ${contentLength}\r\n\r\n`, 'latin1')
1339
+ socket.write(body)
1340
+ socket.uncork()
1341
+ request.onBodySent(body)
1342
+
1343
+ if (!expectsPayload && request.reset !== false) {
1344
+ socket[kReset] = true
1345
+ }
1346
+ }
1347
+ request.onRequestSent()
1348
+
1349
+ client[kResume]()
1350
+ } catch (err) {
1351
+ abort(err)
1352
+ }
1353
+ }
1354
+
1355
+ /**
1356
+ * @param {AbortCallback} abort
1357
+ * @param {Blob} body
1358
+ * @param {import('./client.js')} client
1359
+ * @param {import('../core/request.js')} request
1360
+ * @param {import('net').Socket} socket
1361
+ * @param {number} contentLength
1362
+ * @param {string} header
1363
+ * @param {boolean} expectsPayload
1364
+ * @returns {Promise<void>}
1365
+ */
1366
+ async function writeBlob (abort, body, client, request, socket, contentLength, header, expectsPayload) {
1367
+ assert(contentLength === body.size, 'blob body must have content length')
1368
+
1369
+ try {
1370
+ if (contentLength != null && contentLength !== body.size) {
1371
+ throw new RequestContentLengthMismatchError()
1372
+ }
1373
+
1374
+ const buffer = Buffer.from(await body.arrayBuffer())
1375
+
1376
+ socket.cork()
1377
+ socket.write(`${header}content-length: ${contentLength}\r\n\r\n`, 'latin1')
1378
+ socket.write(buffer)
1379
+ socket.uncork()
1380
+
1381
+ request.onBodySent(buffer)
1382
+ request.onRequestSent()
1383
+
1384
+ if (!expectsPayload && request.reset !== false) {
1385
+ socket[kReset] = true
1386
+ }
1387
+
1388
+ client[kResume]()
1389
+ } catch (err) {
1390
+ abort(err)
1391
+ }
1392
+ }
1393
+
1394
+ /**
1395
+ * @param {AbortCallback} abort
1396
+ * @param {Iterable} body
1397
+ * @param {import('./client.js')} client
1398
+ * @param {import('../core/request.js')} request
1399
+ * @param {import('net').Socket} socket
1400
+ * @param {number} contentLength
1401
+ * @param {string} header
1402
+ * @param {boolean} expectsPayload
1403
+ * @returns {Promise<void>}
1404
+ */
1405
+ async function writeIterable (abort, body, client, request, socket, contentLength, header, expectsPayload) {
1406
+ assert(contentLength !== 0 || client[kRunning] === 0, 'iterator body cannot be pipelined')
1407
+
1408
+ let callback = null
1409
+ function onDrain () {
1410
+ if (callback) {
1411
+ const cb = callback
1412
+ callback = null
1413
+ cb()
1414
+ }
1415
+ }
1416
+
1417
+ const waitForDrain = () => new Promise((resolve, reject) => {
1418
+ assert(callback === null)
1419
+
1420
+ if (socket[kError]) {
1421
+ reject(socket[kError])
1422
+ } else {
1423
+ callback = resolve
1424
+ }
1425
+ })
1426
+
1427
+ socket
1428
+ .on('close', onDrain)
1429
+ .on('drain', onDrain)
1430
+
1431
+ const writer = new AsyncWriter({ abort, socket, request, contentLength, client, expectsPayload, header })
1432
+ try {
1433
+ // It's up to the user to somehow abort the async iterable.
1434
+ for await (const chunk of body) {
1435
+ if (socket[kError]) {
1436
+ throw socket[kError]
1437
+ }
1438
+
1439
+ if (!writer.write(chunk)) {
1440
+ await waitForDrain()
1441
+ }
1442
+ }
1443
+
1444
+ writer.end()
1445
+ } catch (err) {
1446
+ writer.destroy(err)
1447
+ } finally {
1448
+ socket
1449
+ .off('close', onDrain)
1450
+ .off('drain', onDrain)
1451
+ }
1452
+ }
1453
+
1454
+ class AsyncWriter {
1455
+ /**
1456
+ *
1457
+ * @param {object} arg
1458
+ * @param {AbortCallback} arg.abort
1459
+ * @param {import('net').Socket} arg.socket
1460
+ * @param {import('../core/request.js')} arg.request
1461
+ * @param {number} arg.contentLength
1462
+ * @param {import('./client.js')} arg.client
1463
+ * @param {boolean} arg.expectsPayload
1464
+ * @param {string} arg.header
1465
+ */
1466
+ constructor ({ abort, socket, request, contentLength, client, expectsPayload, header }) {
1467
+ this.socket = socket
1468
+ this.request = request
1469
+ this.contentLength = contentLength
1470
+ this.client = client
1471
+ this.bytesWritten = 0
1472
+ this.expectsPayload = expectsPayload
1473
+ this.header = header
1474
+ this.abort = abort
1475
+
1476
+ socket[kWriting] = true
1477
+ }
1478
+
1479
+ /**
1480
+ * @param {Buffer} chunk
1481
+ * @returns
1482
+ */
1483
+ write (chunk) {
1484
+ const { socket, request, contentLength, client, bytesWritten, expectsPayload, header } = this
1485
+
1486
+ if (socket[kError]) {
1487
+ throw socket[kError]
1488
+ }
1489
+
1490
+ if (socket.destroyed) {
1491
+ return false
1492
+ }
1493
+
1494
+ const len = Buffer.byteLength(chunk)
1495
+ if (!len) {
1496
+ return true
1497
+ }
1498
+
1499
+ // We should defer writing chunks.
1500
+ if (contentLength !== null && bytesWritten + len > contentLength) {
1501
+ if (client[kStrictContentLength]) {
1502
+ throw new RequestContentLengthMismatchError()
1503
+ }
1504
+
1505
+ process.emitWarning(new RequestContentLengthMismatchError())
1506
+ }
1507
+
1508
+ socket.cork()
1509
+
1510
+ if (bytesWritten === 0) {
1511
+ if (!expectsPayload && request.reset !== false) {
1512
+ socket[kReset] = true
1513
+ }
1514
+
1515
+ if (contentLength === null) {
1516
+ socket.write(`${header}transfer-encoding: chunked\r\n`, 'latin1')
1517
+ } else {
1518
+ socket.write(`${header}content-length: ${contentLength}\r\n\r\n`, 'latin1')
1519
+ }
1520
+ }
1521
+
1522
+ if (contentLength === null) {
1523
+ socket.write(`\r\n${len.toString(16)}\r\n`, 'latin1')
1524
+ }
1525
+
1526
+ this.bytesWritten += len
1527
+
1528
+ const ret = socket.write(chunk)
1529
+
1530
+ socket.uncork()
1531
+
1532
+ request.onBodySent(chunk)
1533
+
1534
+ if (!ret) {
1535
+ if (socket[kParser].timeout && socket[kParser].timeoutType === TIMEOUT_HEADERS) {
1536
+ if (socket[kParser].timeout.refresh) {
1537
+ socket[kParser].timeout.refresh()
1538
+ }
1539
+ }
1540
+ }
1541
+
1542
+ return ret
1543
+ }
1544
+
1545
+ /**
1546
+ * @returns {void}
1547
+ */
1548
+ end () {
1549
+ const { socket, contentLength, client, bytesWritten, expectsPayload, header, request } = this
1550
+ request.onRequestSent()
1551
+
1552
+ socket[kWriting] = false
1553
+
1554
+ if (socket[kError]) {
1555
+ throw socket[kError]
1556
+ }
1557
+
1558
+ if (socket.destroyed) {
1559
+ return
1560
+ }
1561
+
1562
+ if (bytesWritten === 0) {
1563
+ if (expectsPayload) {
1564
+ // https://tools.ietf.org/html/rfc7230#section-3.3.2
1565
+ // A user agent SHOULD send a Content-Length in a request message when
1566
+ // no Transfer-Encoding is sent and the request method defines a meaning
1567
+ // for an enclosed payload body.
1568
+
1569
+ socket.write(`${header}content-length: 0\r\n\r\n`, 'latin1')
1570
+ } else {
1571
+ socket.write(`${header}\r\n`, 'latin1')
1572
+ }
1573
+ } else if (contentLength === null) {
1574
+ socket.write('\r\n0\r\n\r\n', 'latin1')
1575
+ }
1576
+
1577
+ if (contentLength !== null && bytesWritten !== contentLength) {
1578
+ if (client[kStrictContentLength]) {
1579
+ throw new RequestContentLengthMismatchError()
1580
+ } else {
1581
+ process.emitWarning(new RequestContentLengthMismatchError())
1582
+ }
1583
+ }
1584
+
1585
+ if (socket[kParser].timeout && socket[kParser].timeoutType === TIMEOUT_HEADERS) {
1586
+ if (socket[kParser].timeout.refresh) {
1587
+ socket[kParser].timeout.refresh()
1588
+ }
1589
+ }
1590
+
1591
+ client[kResume]()
1592
+ }
1593
+
1594
+ /**
1595
+ * @param {Error} [err]
1596
+ * @returns {void}
1597
+ */
1598
+ destroy (err) {
1599
+ const { socket, client, abort } = this
1600
+
1601
+ socket[kWriting] = false
1602
+
1603
+ if (err) {
1604
+ assert(client[kRunning] <= 1, 'pipeline should only contain this request')
1605
+ abort(err)
1606
+ }
1607
+ }
1608
+ }
1609
+
1610
+ module.exports = connectH1