deepv-code 1.0.182

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 (223) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +3 -0
  3. package/bundle/assets/help/README.md +113 -0
  4. package/bundle/assets/help/cli-help-knowledge.md +1382 -0
  5. package/bundle/assets/sounds/README.md +74 -0
  6. package/bundle/assets/sounds/confirmation-required.wav +0 -0
  7. package/bundle/assets/sounds/response-complete.wav +0 -0
  8. package/bundle/assets/sounds/selection-made.wav +0 -0
  9. package/bundle/dvcode.js +4442 -0
  10. package/bundle/fix-binary-permissions.js +215 -0
  11. package/bundle/login/templates/authSelectPage.html +870 -0
  12. package/bundle/login/templates/deepv.ico +0 -0
  13. package/bundle/login/templates/feishu.ico +0 -0
  14. package/bundle/node_modules/@vscode/ripgrep/bin/darwin-arm64-rg +0 -0
  15. package/bundle/node_modules/@vscode/ripgrep/bin/darwin-x64-rg +0 -0
  16. package/bundle/node_modules/@vscode/ripgrep/bin/linux-arm-rg +0 -0
  17. package/bundle/node_modules/@vscode/ripgrep/bin/linux-arm64-rg +0 -0
  18. package/bundle/node_modules/@vscode/ripgrep/bin/linux-x64-rg +0 -0
  19. package/bundle/node_modules/@vscode/ripgrep/bin/rg.exe +0 -0
  20. package/bundle/node_modules/@vscode/ripgrep/bin/win32-arm64-rg.exe +0 -0
  21. package/bundle/node_modules/@vscode/ripgrep/bin/win32-ia32-rg.exe +0 -0
  22. package/bundle/node_modules/@vscode/ripgrep/bin/win32-x64-rg.exe +0 -0
  23. package/bundle/node_modules/@vscode/ripgrep/lib/download.js +357 -0
  24. package/bundle/node_modules/@vscode/ripgrep/lib/index.d.ts +1 -0
  25. package/bundle/node_modules/@vscode/ripgrep/lib/index.js +42 -0
  26. package/bundle/node_modules/@vscode/ripgrep/lib/postinstall.js +121 -0
  27. package/bundle/node_modules/@vscode/ripgrep/package.json +24 -0
  28. package/bundle/node_modules/undici/LICENSE +21 -0
  29. package/bundle/node_modules/undici/README.md +472 -0
  30. package/bundle/node_modules/undici/docs/docs/api/Agent.md +83 -0
  31. package/bundle/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
  32. package/bundle/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
  33. package/bundle/node_modules/undici/docs/docs/api/CacheStore.md +151 -0
  34. package/bundle/node_modules/undici/docs/docs/api/Client.md +281 -0
  35. package/bundle/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
  36. package/bundle/node_modules/undici/docs/docs/api/Connector.md +115 -0
  37. package/bundle/node_modules/undici/docs/docs/api/ContentType.md +57 -0
  38. package/bundle/node_modules/undici/docs/docs/api/Cookies.md +101 -0
  39. package/bundle/node_modules/undici/docs/docs/api/Debug.md +62 -0
  40. package/bundle/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +204 -0
  41. package/bundle/node_modules/undici/docs/docs/api/Dispatcher.md +1200 -0
  42. package/bundle/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
  43. package/bundle/node_modules/undici/docs/docs/api/Errors.md +49 -0
  44. package/bundle/node_modules/undici/docs/docs/api/EventSource.md +45 -0
  45. package/bundle/node_modules/undici/docs/docs/api/Fetch.md +52 -0
  46. package/bundle/node_modules/undici/docs/docs/api/H2CClient.md +262 -0
  47. package/bundle/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
  48. package/bundle/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
  49. package/bundle/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
  50. package/bundle/node_modules/undici/docs/docs/api/MockClient.md +77 -0
  51. package/bundle/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
  52. package/bundle/node_modules/undici/docs/docs/api/MockPool.md +548 -0
  53. package/bundle/node_modules/undici/docs/docs/api/Pool.md +84 -0
  54. package/bundle/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
  55. package/bundle/node_modules/undici/docs/docs/api/ProxyAgent.md +227 -0
  56. package/bundle/node_modules/undici/docs/docs/api/RedirectHandler.md +96 -0
  57. package/bundle/node_modules/undici/docs/docs/api/RetryAgent.md +45 -0
  58. package/bundle/node_modules/undici/docs/docs/api/RetryHandler.md +117 -0
  59. package/bundle/node_modules/undici/docs/docs/api/Util.md +25 -0
  60. package/bundle/node_modules/undici/docs/docs/api/WebSocket.md +85 -0
  61. package/bundle/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
  62. package/bundle/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
  63. package/bundle/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
  64. package/bundle/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
  65. package/bundle/node_modules/undici/docs/docs/best-practices/writing-tests.md +20 -0
  66. package/bundle/node_modules/undici/index-fetch.js +35 -0
  67. package/bundle/node_modules/undici/index.d.ts +3 -0
  68. package/bundle/node_modules/undici/index.js +183 -0
  69. package/bundle/node_modules/undici/lib/api/abort-signal.js +59 -0
  70. package/bundle/node_modules/undici/lib/api/api-connect.js +110 -0
  71. package/bundle/node_modules/undici/lib/api/api-pipeline.js +252 -0
  72. package/bundle/node_modules/undici/lib/api/api-request.js +199 -0
  73. package/bundle/node_modules/undici/lib/api/api-stream.js +209 -0
  74. package/bundle/node_modules/undici/lib/api/api-upgrade.js +110 -0
  75. package/bundle/node_modules/undici/lib/api/index.js +7 -0
  76. package/bundle/node_modules/undici/lib/api/readable.js +558 -0
  77. package/bundle/node_modules/undici/lib/api/util.js +95 -0
  78. package/bundle/node_modules/undici/lib/cache/memory-cache-store.js +234 -0
  79. package/bundle/node_modules/undici/lib/cache/sqlite-cache-store.js +461 -0
  80. package/bundle/node_modules/undici/lib/core/connect.js +164 -0
  81. package/bundle/node_modules/undici/lib/core/constants.js +143 -0
  82. package/bundle/node_modules/undici/lib/core/diagnostics.js +196 -0
  83. package/bundle/node_modules/undici/lib/core/errors.js +244 -0
  84. package/bundle/node_modules/undici/lib/core/request.js +397 -0
  85. package/bundle/node_modules/undici/lib/core/symbols.js +68 -0
  86. package/bundle/node_modules/undici/lib/core/tree.js +160 -0
  87. package/bundle/node_modules/undici/lib/core/util.js +988 -0
  88. package/bundle/node_modules/undici/lib/dispatcher/agent.js +135 -0
  89. package/bundle/node_modules/undici/lib/dispatcher/balanced-pool.js +206 -0
  90. package/bundle/node_modules/undici/lib/dispatcher/client-h1.js +1615 -0
  91. package/bundle/node_modules/undici/lib/dispatcher/client-h2.js +798 -0
  92. package/bundle/node_modules/undici/lib/dispatcher/client.js +614 -0
  93. package/bundle/node_modules/undici/lib/dispatcher/dispatcher-base.js +161 -0
  94. package/bundle/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
  95. package/bundle/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +151 -0
  96. package/bundle/node_modules/undici/lib/dispatcher/fixed-queue.js +159 -0
  97. package/bundle/node_modules/undici/lib/dispatcher/h2c-client.js +122 -0
  98. package/bundle/node_modules/undici/lib/dispatcher/pool-base.js +191 -0
  99. package/bundle/node_modules/undici/lib/dispatcher/pool.js +118 -0
  100. package/bundle/node_modules/undici/lib/dispatcher/proxy-agent.js +275 -0
  101. package/bundle/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
  102. package/bundle/node_modules/undici/lib/global.js +32 -0
  103. package/bundle/node_modules/undici/lib/handler/cache-handler.js +448 -0
  104. package/bundle/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
  105. package/bundle/node_modules/undici/lib/handler/decorator-handler.js +67 -0
  106. package/bundle/node_modules/undici/lib/handler/redirect-handler.js +227 -0
  107. package/bundle/node_modules/undici/lib/handler/retry-handler.js +342 -0
  108. package/bundle/node_modules/undici/lib/handler/unwrap-handler.js +96 -0
  109. package/bundle/node_modules/undici/lib/handler/wrap-handler.js +95 -0
  110. package/bundle/node_modules/undici/lib/interceptor/cache.js +372 -0
  111. package/bundle/node_modules/undici/lib/interceptor/dns.js +432 -0
  112. package/bundle/node_modules/undici/lib/interceptor/dump.js +111 -0
  113. package/bundle/node_modules/undici/lib/interceptor/redirect.js +21 -0
  114. package/bundle/node_modules/undici/lib/interceptor/response-error.js +95 -0
  115. package/bundle/node_modules/undici/lib/interceptor/retry.js +19 -0
  116. package/bundle/node_modules/undici/lib/llhttp/.gitkeep +0 -0
  117. package/bundle/node_modules/undici/lib/llhttp/constants.d.ts +97 -0
  118. package/bundle/node_modules/undici/lib/llhttp/constants.js +498 -0
  119. package/bundle/node_modules/undici/lib/llhttp/constants.js.map +1 -0
  120. package/bundle/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
  121. package/bundle/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
  122. package/bundle/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
  123. package/bundle/node_modules/undici/lib/llhttp/utils.js +15 -0
  124. package/bundle/node_modules/undici/lib/llhttp/utils.js.map +1 -0
  125. package/bundle/node_modules/undici/lib/mock/mock-agent.js +224 -0
  126. package/bundle/node_modules/undici/lib/mock/mock-call-history.js +248 -0
  127. package/bundle/node_modules/undici/lib/mock/mock-client.js +64 -0
  128. package/bundle/node_modules/undici/lib/mock/mock-errors.js +19 -0
  129. package/bundle/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
  130. package/bundle/node_modules/undici/lib/mock/mock-pool.js +64 -0
  131. package/bundle/node_modules/undici/lib/mock/mock-symbols.js +31 -0
  132. package/bundle/node_modules/undici/lib/mock/mock-utils.js +433 -0
  133. package/bundle/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
  134. package/bundle/node_modules/undici/lib/util/cache.js +368 -0
  135. package/bundle/node_modules/undici/lib/util/date.js +259 -0
  136. package/bundle/node_modules/undici/lib/util/stats.js +32 -0
  137. package/bundle/node_modules/undici/lib/util/timers.js +423 -0
  138. package/bundle/node_modules/undici/lib/web/cache/cache.js +862 -0
  139. package/bundle/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
  140. package/bundle/node_modules/undici/lib/web/cache/util.js +45 -0
  141. package/bundle/node_modules/undici/lib/web/cookies/constants.js +12 -0
  142. package/bundle/node_modules/undici/lib/web/cookies/index.js +199 -0
  143. package/bundle/node_modules/undici/lib/web/cookies/parse.js +322 -0
  144. package/bundle/node_modules/undici/lib/web/cookies/util.js +282 -0
  145. package/bundle/node_modules/undici/lib/web/eventsource/eventsource-stream.js +399 -0
  146. package/bundle/node_modules/undici/lib/web/eventsource/eventsource.js +484 -0
  147. package/bundle/node_modules/undici/lib/web/eventsource/util.js +37 -0
  148. package/bundle/node_modules/undici/lib/web/fetch/LICENSE +21 -0
  149. package/bundle/node_modules/undici/lib/web/fetch/body.js +532 -0
  150. package/bundle/node_modules/undici/lib/web/fetch/constants.js +131 -0
  151. package/bundle/node_modules/undici/lib/web/fetch/data-url.js +744 -0
  152. package/bundle/node_modules/undici/lib/web/fetch/dispatcher-weakref.js +46 -0
  153. package/bundle/node_modules/undici/lib/web/fetch/formdata-parser.js +501 -0
  154. package/bundle/node_modules/undici/lib/web/fetch/formdata.js +263 -0
  155. package/bundle/node_modules/undici/lib/web/fetch/global.js +40 -0
  156. package/bundle/node_modules/undici/lib/web/fetch/headers.js +719 -0
  157. package/bundle/node_modules/undici/lib/web/fetch/index.js +2258 -0
  158. package/bundle/node_modules/undici/lib/web/fetch/request.js +1099 -0
  159. package/bundle/node_modules/undici/lib/web/fetch/response.js +636 -0
  160. package/bundle/node_modules/undici/lib/web/fetch/util.js +1782 -0
  161. package/bundle/node_modules/undici/lib/web/fetch/webidl.js +740 -0
  162. package/bundle/node_modules/undici/lib/web/websocket/connection.js +325 -0
  163. package/bundle/node_modules/undici/lib/web/websocket/constants.js +126 -0
  164. package/bundle/node_modules/undici/lib/web/websocket/events.js +331 -0
  165. package/bundle/node_modules/undici/lib/web/websocket/frame.js +138 -0
  166. package/bundle/node_modules/undici/lib/web/websocket/permessage-deflate.js +70 -0
  167. package/bundle/node_modules/undici/lib/web/websocket/receiver.js +454 -0
  168. package/bundle/node_modules/undici/lib/web/websocket/sender.js +109 -0
  169. package/bundle/node_modules/undici/lib/web/websocket/stream/websocketerror.js +83 -0
  170. package/bundle/node_modules/undici/lib/web/websocket/stream/websocketstream.js +485 -0
  171. package/bundle/node_modules/undici/lib/web/websocket/util.js +338 -0
  172. package/bundle/node_modules/undici/lib/web/websocket/websocket.js +686 -0
  173. package/bundle/node_modules/undici/package.json +149 -0
  174. package/bundle/node_modules/undici/scripts/strip-comments.js +10 -0
  175. package/bundle/node_modules/undici/types/README.md +6 -0
  176. package/bundle/node_modules/undici/types/agent.d.ts +35 -0
  177. package/bundle/node_modules/undici/types/api.d.ts +43 -0
  178. package/bundle/node_modules/undici/types/balanced-pool.d.ts +29 -0
  179. package/bundle/node_modules/undici/types/cache-interceptor.d.ts +172 -0
  180. package/bundle/node_modules/undici/types/cache.d.ts +36 -0
  181. package/bundle/node_modules/undici/types/client-stats.d.ts +15 -0
  182. package/bundle/node_modules/undici/types/client.d.ts +110 -0
  183. package/bundle/node_modules/undici/types/connector.d.ts +34 -0
  184. package/bundle/node_modules/undici/types/content-type.d.ts +21 -0
  185. package/bundle/node_modules/undici/types/cookies.d.ts +30 -0
  186. package/bundle/node_modules/undici/types/diagnostics-channel.d.ts +66 -0
  187. package/bundle/node_modules/undici/types/dispatcher.d.ts +281 -0
  188. package/bundle/node_modules/undici/types/env-http-proxy-agent.d.ts +21 -0
  189. package/bundle/node_modules/undici/types/errors.d.ts +171 -0
  190. package/bundle/node_modules/undici/types/eventsource.d.ts +61 -0
  191. package/bundle/node_modules/undici/types/fetch.d.ts +210 -0
  192. package/bundle/node_modules/undici/types/formdata.d.ts +108 -0
  193. package/bundle/node_modules/undici/types/global-dispatcher.d.ts +9 -0
  194. package/bundle/node_modules/undici/types/global-origin.d.ts +7 -0
  195. package/bundle/node_modules/undici/types/h2c-client.d.ts +75 -0
  196. package/bundle/node_modules/undici/types/handlers.d.ts +15 -0
  197. package/bundle/node_modules/undici/types/header.d.ts +160 -0
  198. package/bundle/node_modules/undici/types/index.d.ts +75 -0
  199. package/bundle/node_modules/undici/types/interceptors.d.ts +34 -0
  200. package/bundle/node_modules/undici/types/mock-agent.d.ts +68 -0
  201. package/bundle/node_modules/undici/types/mock-call-history.d.ts +111 -0
  202. package/bundle/node_modules/undici/types/mock-client.d.ts +25 -0
  203. package/bundle/node_modules/undici/types/mock-errors.d.ts +12 -0
  204. package/bundle/node_modules/undici/types/mock-interceptor.d.ts +93 -0
  205. package/bundle/node_modules/undici/types/mock-pool.d.ts +25 -0
  206. package/bundle/node_modules/undici/types/patch.d.ts +29 -0
  207. package/bundle/node_modules/undici/types/pool-stats.d.ts +19 -0
  208. package/bundle/node_modules/undici/types/pool.d.ts +41 -0
  209. package/bundle/node_modules/undici/types/proxy-agent.d.ts +29 -0
  210. package/bundle/node_modules/undici/types/readable.d.ts +68 -0
  211. package/bundle/node_modules/undici/types/retry-agent.d.ts +8 -0
  212. package/bundle/node_modules/undici/types/retry-handler.d.ts +116 -0
  213. package/bundle/node_modules/undici/types/util.d.ts +18 -0
  214. package/bundle/node_modules/undici/types/utility.d.ts +7 -0
  215. package/bundle/node_modules/undici/types/webidl.d.ts +266 -0
  216. package/bundle/node_modules/undici/types/websocket.d.ts +184 -0
  217. package/bundle/sandbox-macos-permissive-closed.sb +26 -0
  218. package/bundle/sandbox-macos-permissive-open.sb +19 -0
  219. package/bundle/sandbox-macos-permissive-proxied.sb +31 -0
  220. package/bundle/sandbox-macos-restrictive-closed.sb +87 -0
  221. package/bundle/sandbox-macos-restrictive-open.sb +90 -0
  222. package/bundle/sandbox-macos-restrictive-proxied.sb +92 -0
  223. package/package.json +137 -0
@@ -0,0 +1,191 @@
1
+ 'use strict'
2
+
3
+ const { PoolStats } = require('../util/stats.js')
4
+ const DispatcherBase = require('./dispatcher-base')
5
+ const FixedQueue = require('./fixed-queue')
6
+ const { kConnected, kSize, kRunning, kPending, kQueued, kBusy, kFree, kUrl, kClose, kDestroy, kDispatch } = require('../core/symbols')
7
+
8
+ const kClients = Symbol('clients')
9
+ const kNeedDrain = Symbol('needDrain')
10
+ const kQueue = Symbol('queue')
11
+ const kClosedResolve = Symbol('closed resolve')
12
+ const kOnDrain = Symbol('onDrain')
13
+ const kOnConnect = Symbol('onConnect')
14
+ const kOnDisconnect = Symbol('onDisconnect')
15
+ const kOnConnectionError = Symbol('onConnectionError')
16
+ const kGetDispatcher = Symbol('get dispatcher')
17
+ const kAddClient = Symbol('add client')
18
+ const kRemoveClient = Symbol('remove client')
19
+
20
+ class PoolBase extends DispatcherBase {
21
+ constructor () {
22
+ super()
23
+
24
+ this[kQueue] = new FixedQueue()
25
+ this[kClients] = []
26
+ this[kQueued] = 0
27
+
28
+ const pool = this
29
+
30
+ this[kOnDrain] = function onDrain (origin, targets) {
31
+ const queue = pool[kQueue]
32
+
33
+ let needDrain = false
34
+
35
+ while (!needDrain) {
36
+ const item = queue.shift()
37
+ if (!item) {
38
+ break
39
+ }
40
+ pool[kQueued]--
41
+ needDrain = !this.dispatch(item.opts, item.handler)
42
+ }
43
+
44
+ this[kNeedDrain] = needDrain
45
+
46
+ if (!this[kNeedDrain] && pool[kNeedDrain]) {
47
+ pool[kNeedDrain] = false
48
+ pool.emit('drain', origin, [pool, ...targets])
49
+ }
50
+
51
+ if (pool[kClosedResolve] && queue.isEmpty()) {
52
+ Promise
53
+ .all(pool[kClients].map(c => c.close()))
54
+ .then(pool[kClosedResolve])
55
+ }
56
+ }
57
+
58
+ this[kOnConnect] = (origin, targets) => {
59
+ pool.emit('connect', origin, [pool, ...targets])
60
+ }
61
+
62
+ this[kOnDisconnect] = (origin, targets, err) => {
63
+ pool.emit('disconnect', origin, [pool, ...targets], err)
64
+ }
65
+
66
+ this[kOnConnectionError] = (origin, targets, err) => {
67
+ pool.emit('connectionError', origin, [pool, ...targets], err)
68
+ }
69
+ }
70
+
71
+ get [kBusy] () {
72
+ return this[kNeedDrain]
73
+ }
74
+
75
+ get [kConnected] () {
76
+ return this[kClients].filter(client => client[kConnected]).length
77
+ }
78
+
79
+ get [kFree] () {
80
+ return this[kClients].filter(client => client[kConnected] && !client[kNeedDrain]).length
81
+ }
82
+
83
+ get [kPending] () {
84
+ let ret = this[kQueued]
85
+ for (const { [kPending]: pending } of this[kClients]) {
86
+ ret += pending
87
+ }
88
+ return ret
89
+ }
90
+
91
+ get [kRunning] () {
92
+ let ret = 0
93
+ for (const { [kRunning]: running } of this[kClients]) {
94
+ ret += running
95
+ }
96
+ return ret
97
+ }
98
+
99
+ get [kSize] () {
100
+ let ret = this[kQueued]
101
+ for (const { [kSize]: size } of this[kClients]) {
102
+ ret += size
103
+ }
104
+ return ret
105
+ }
106
+
107
+ get stats () {
108
+ return new PoolStats(this)
109
+ }
110
+
111
+ async [kClose] () {
112
+ if (this[kQueue].isEmpty()) {
113
+ await Promise.all(this[kClients].map(c => c.close()))
114
+ } else {
115
+ await new Promise((resolve) => {
116
+ this[kClosedResolve] = resolve
117
+ })
118
+ }
119
+ }
120
+
121
+ async [kDestroy] (err) {
122
+ while (true) {
123
+ const item = this[kQueue].shift()
124
+ if (!item) {
125
+ break
126
+ }
127
+ item.handler.onError(err)
128
+ }
129
+
130
+ await Promise.all(this[kClients].map(c => c.destroy(err)))
131
+ }
132
+
133
+ [kDispatch] (opts, handler) {
134
+ const dispatcher = this[kGetDispatcher]()
135
+
136
+ if (!dispatcher) {
137
+ this[kNeedDrain] = true
138
+ this[kQueue].push({ opts, handler })
139
+ this[kQueued]++
140
+ } else if (!dispatcher.dispatch(opts, handler)) {
141
+ dispatcher[kNeedDrain] = true
142
+ this[kNeedDrain] = !this[kGetDispatcher]()
143
+ }
144
+
145
+ return !this[kNeedDrain]
146
+ }
147
+
148
+ [kAddClient] (client) {
149
+ client
150
+ .on('drain', this[kOnDrain])
151
+ .on('connect', this[kOnConnect])
152
+ .on('disconnect', this[kOnDisconnect])
153
+ .on('connectionError', this[kOnConnectionError])
154
+
155
+ this[kClients].push(client)
156
+
157
+ if (this[kNeedDrain]) {
158
+ queueMicrotask(() => {
159
+ if (this[kNeedDrain]) {
160
+ this[kOnDrain](client[kUrl], [this, client])
161
+ }
162
+ })
163
+ }
164
+
165
+ return this
166
+ }
167
+
168
+ [kRemoveClient] (client) {
169
+ client.close(() => {
170
+ const idx = this[kClients].indexOf(client)
171
+ if (idx !== -1) {
172
+ this[kClients].splice(idx, 1)
173
+ }
174
+ })
175
+
176
+ this[kNeedDrain] = this[kClients].some(dispatcher => (
177
+ !dispatcher[kNeedDrain] &&
178
+ dispatcher.closed !== true &&
179
+ dispatcher.destroyed !== true
180
+ ))
181
+ }
182
+ }
183
+
184
+ module.exports = {
185
+ PoolBase,
186
+ kClients,
187
+ kNeedDrain,
188
+ kAddClient,
189
+ kRemoveClient,
190
+ kGetDispatcher
191
+ }
@@ -0,0 +1,118 @@
1
+ 'use strict'
2
+
3
+ const {
4
+ PoolBase,
5
+ kClients,
6
+ kNeedDrain,
7
+ kAddClient,
8
+ kGetDispatcher,
9
+ kRemoveClient
10
+ } = require('./pool-base')
11
+ const Client = require('./client')
12
+ const {
13
+ InvalidArgumentError
14
+ } = require('../core/errors')
15
+ const util = require('../core/util')
16
+ const { kUrl } = require('../core/symbols')
17
+ const buildConnector = require('../core/connect')
18
+
19
+ const kOptions = Symbol('options')
20
+ const kConnections = Symbol('connections')
21
+ const kFactory = Symbol('factory')
22
+
23
+ function defaultFactory (origin, opts) {
24
+ return new Client(origin, opts)
25
+ }
26
+
27
+ class Pool extends PoolBase {
28
+ constructor (origin, {
29
+ connections,
30
+ factory = defaultFactory,
31
+ connect,
32
+ connectTimeout,
33
+ tls,
34
+ maxCachedSessions,
35
+ socketPath,
36
+ autoSelectFamily,
37
+ autoSelectFamilyAttemptTimeout,
38
+ allowH2,
39
+ clientTtl,
40
+ ...options
41
+ } = {}) {
42
+ if (connections != null && (!Number.isFinite(connections) || connections < 0)) {
43
+ throw new InvalidArgumentError('invalid connections')
44
+ }
45
+
46
+ if (typeof factory !== 'function') {
47
+ throw new InvalidArgumentError('factory must be a function.')
48
+ }
49
+
50
+ if (connect != null && typeof connect !== 'function' && typeof connect !== 'object') {
51
+ throw new InvalidArgumentError('connect must be a function or an object')
52
+ }
53
+
54
+ super()
55
+
56
+ if (typeof connect !== 'function') {
57
+ connect = buildConnector({
58
+ ...tls,
59
+ maxCachedSessions,
60
+ allowH2,
61
+ socketPath,
62
+ timeout: connectTimeout,
63
+ ...(typeof autoSelectFamily === 'boolean' ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
64
+ ...connect
65
+ })
66
+ }
67
+
68
+ this[kConnections] = connections || null
69
+ this[kUrl] = util.parseOrigin(origin)
70
+ this[kOptions] = { ...util.deepClone(options), connect, allowH2, clientTtl }
71
+ this[kOptions].interceptors = options.interceptors
72
+ ? { ...options.interceptors }
73
+ : undefined
74
+ this[kFactory] = factory
75
+
76
+ this.on('connect', (origin, targets) => {
77
+ if (clientTtl != null && clientTtl > 0) {
78
+ for (const target of targets) {
79
+ Object.assign(target, { ttl: Date.now() })
80
+ }
81
+ }
82
+ })
83
+
84
+ this.on('connectionError', (origin, targets, error) => {
85
+ // If a connection error occurs, we remove the client from the pool,
86
+ // and emit a connectionError event. They will not be re-used.
87
+ // Fixes https://github.com/nodejs/undici/issues/3895
88
+ for (const target of targets) {
89
+ // Do not use kRemoveClient here, as it will close the client,
90
+ // but the client cannot be closed in this state.
91
+ const idx = this[kClients].indexOf(target)
92
+ if (idx !== -1) {
93
+ this[kClients].splice(idx, 1)
94
+ }
95
+ }
96
+ })
97
+ }
98
+
99
+ [kGetDispatcher] () {
100
+ const clientTtlOption = this[kOptions].clientTtl
101
+ for (const client of this[kClients]) {
102
+ // check ttl of client and if it's stale, remove it from the pool
103
+ if (clientTtlOption != null && clientTtlOption > 0 && client.ttl && ((Date.now() - client.ttl) > clientTtlOption)) {
104
+ this[kRemoveClient](client)
105
+ } else if (!client[kNeedDrain]) {
106
+ return client
107
+ }
108
+ }
109
+
110
+ if (!this[kConnections] || this[kClients].length < this[kConnections]) {
111
+ const dispatcher = this[kFactory](this[kUrl], this[kOptions])
112
+ this[kAddClient](dispatcher)
113
+ return dispatcher
114
+ }
115
+ }
116
+ }
117
+
118
+ module.exports = Pool
@@ -0,0 +1,275 @@
1
+ 'use strict'
2
+
3
+ const { kProxy, kClose, kDestroy, kDispatch, kConnector } = require('../core/symbols')
4
+ const { URL } = require('node:url')
5
+ const Agent = require('./agent')
6
+ const Pool = require('./pool')
7
+ const DispatcherBase = require('./dispatcher-base')
8
+ const { InvalidArgumentError, RequestAbortedError, SecureProxyConnectionError } = require('../core/errors')
9
+ const buildConnector = require('../core/connect')
10
+ const Client = require('./client')
11
+
12
+ const kAgent = Symbol('proxy agent')
13
+ const kClient = Symbol('proxy client')
14
+ const kProxyHeaders = Symbol('proxy headers')
15
+ const kRequestTls = Symbol('request tls settings')
16
+ const kProxyTls = Symbol('proxy tls settings')
17
+ const kConnectEndpoint = Symbol('connect endpoint function')
18
+ const kTunnelProxy = Symbol('tunnel proxy')
19
+
20
+ function defaultProtocolPort (protocol) {
21
+ return protocol === 'https:' ? 443 : 80
22
+ }
23
+
24
+ function defaultFactory (origin, opts) {
25
+ return new Pool(origin, opts)
26
+ }
27
+
28
+ const noop = () => {}
29
+
30
+ class ProxyClient extends DispatcherBase {
31
+ #client = null
32
+ constructor (origin, opts) {
33
+ if (typeof origin === 'string') {
34
+ origin = new URL(origin)
35
+ }
36
+
37
+ if (origin.protocol !== 'http:' && origin.protocol !== 'https:') {
38
+ throw new InvalidArgumentError('ProxyClient only supports http and https protocols')
39
+ }
40
+
41
+ super()
42
+
43
+ this.#client = new Client(origin, opts)
44
+ }
45
+
46
+ async [kClose] () {
47
+ await this.#client.close()
48
+ }
49
+
50
+ async [kDestroy] () {
51
+ await this.#client.destroy()
52
+ }
53
+
54
+ async [kDispatch] (opts, handler) {
55
+ const { method, origin } = opts
56
+ if (method === 'CONNECT') {
57
+ this.#client[kConnector]({
58
+ origin,
59
+ port: opts.port || defaultProtocolPort(opts.protocol),
60
+ path: opts.host,
61
+ signal: opts.signal,
62
+ headers: {
63
+ ...this[kProxyHeaders],
64
+ host: opts.host
65
+ },
66
+ servername: this[kProxyTls]?.servername || opts.servername
67
+ },
68
+ (err, socket) => {
69
+ if (err) {
70
+ handler.callback(err)
71
+ } else {
72
+ handler.callback(null, { socket, statusCode: 200 })
73
+ }
74
+ }
75
+ )
76
+ return
77
+ }
78
+ if (typeof origin === 'string') {
79
+ opts.origin = new URL(origin)
80
+ }
81
+
82
+ return this.#client.dispatch(opts, handler)
83
+ }
84
+ }
85
+ class ProxyAgent extends DispatcherBase {
86
+ constructor (opts) {
87
+ if (!opts || (typeof opts === 'object' && !(opts instanceof URL) && !opts.uri)) {
88
+ throw new InvalidArgumentError('Proxy uri is mandatory')
89
+ }
90
+
91
+ const { clientFactory = defaultFactory } = opts
92
+ if (typeof clientFactory !== 'function') {
93
+ throw new InvalidArgumentError('Proxy opts.clientFactory must be a function.')
94
+ }
95
+
96
+ const { proxyTunnel = true } = opts
97
+
98
+ super()
99
+
100
+ const url = this.#getUrl(opts)
101
+ const { href, origin, port, protocol, username, password, hostname: proxyHostname } = url
102
+
103
+ this[kProxy] = { uri: href, protocol }
104
+ this[kRequestTls] = opts.requestTls
105
+ this[kProxyTls] = opts.proxyTls
106
+ this[kProxyHeaders] = opts.headers || {}
107
+
108
+ if (opts.auth && opts.token) {
109
+ throw new InvalidArgumentError('opts.auth cannot be used in combination with opts.token')
110
+ } else if (opts.auth) {
111
+ /* @deprecated in favour of opts.token */
112
+ this[kProxyHeaders]['proxy-authorization'] = `Basic ${opts.auth}`
113
+ } else if (opts.token) {
114
+ this[kProxyHeaders]['proxy-authorization'] = opts.token
115
+ } else if (username && password) {
116
+ this[kProxyHeaders]['proxy-authorization'] = `Basic ${Buffer.from(`${decodeURIComponent(username)}:${decodeURIComponent(password)}`).toString('base64')}`
117
+ }
118
+
119
+ const factory = (!proxyTunnel && protocol === 'http:')
120
+ ? (origin, options) => {
121
+ if (origin.protocol === 'http:') {
122
+ return new ProxyClient(origin, options)
123
+ }
124
+ return new Client(origin, options)
125
+ }
126
+ : undefined
127
+
128
+ const connect = buildConnector({ ...opts.proxyTls })
129
+ this[kConnectEndpoint] = buildConnector({ ...opts.requestTls })
130
+ this[kClient] = clientFactory(url, { connect, factory })
131
+ this[kTunnelProxy] = proxyTunnel
132
+ this[kAgent] = new Agent({
133
+ ...opts,
134
+ connect: async (opts, callback) => {
135
+ let requestedPath = opts.host
136
+ if (!opts.port) {
137
+ requestedPath += `:${defaultProtocolPort(opts.protocol)}`
138
+ }
139
+ try {
140
+ const { socket, statusCode } = await this[kClient].connect({
141
+ origin,
142
+ port,
143
+ path: requestedPath,
144
+ signal: opts.signal,
145
+ headers: {
146
+ ...this[kProxyHeaders],
147
+ host: opts.host
148
+ },
149
+ servername: this[kProxyTls]?.servername || proxyHostname
150
+ })
151
+ if (statusCode !== 200) {
152
+ socket.on('error', noop).destroy()
153
+ callback(new RequestAbortedError(`Proxy response (${statusCode}) !== 200 when HTTP Tunneling`))
154
+ }
155
+ if (opts.protocol !== 'https:') {
156
+ callback(null, socket)
157
+ return
158
+ }
159
+ let servername
160
+ if (this[kRequestTls]) {
161
+ servername = this[kRequestTls].servername
162
+ } else {
163
+ servername = opts.servername
164
+ }
165
+ this[kConnectEndpoint]({ ...opts, servername, httpSocket: socket }, callback)
166
+ } catch (err) {
167
+ if (err.code === 'ERR_TLS_CERT_ALTNAME_INVALID') {
168
+ // Throw a custom error to avoid loop in client.js#connect
169
+ callback(new SecureProxyConnectionError(err))
170
+ } else {
171
+ callback(err)
172
+ }
173
+ }
174
+ }
175
+ })
176
+ }
177
+
178
+ dispatch (opts, handler) {
179
+ const headers = buildHeaders(opts.headers)
180
+ throwIfProxyAuthIsSent(headers)
181
+
182
+ if (headers && !('host' in headers) && !('Host' in headers)) {
183
+ const { host } = new URL(opts.origin)
184
+ headers.host = host
185
+ }
186
+
187
+ if (!this.#shouldConnect(new URL(opts.origin))) {
188
+ opts.path = opts.origin + opts.path
189
+ }
190
+
191
+ return this[kAgent].dispatch(
192
+ {
193
+ ...opts,
194
+ headers
195
+ },
196
+ handler
197
+ )
198
+ }
199
+
200
+ /**
201
+ * @param {import('../types/proxy-agent').ProxyAgent.Options | string | URL} opts
202
+ * @returns {URL}
203
+ */
204
+ #getUrl (opts) {
205
+ if (typeof opts === 'string') {
206
+ return new URL(opts)
207
+ } else if (opts instanceof URL) {
208
+ return opts
209
+ } else {
210
+ return new URL(opts.uri)
211
+ }
212
+ }
213
+
214
+ async [kClose] () {
215
+ await this[kAgent].close()
216
+ await this[kClient].close()
217
+ }
218
+
219
+ async [kDestroy] () {
220
+ await this[kAgent].destroy()
221
+ await this[kClient].destroy()
222
+ }
223
+
224
+ #shouldConnect (uri) {
225
+ if (typeof uri === 'string') {
226
+ uri = new URL(uri)
227
+ }
228
+ if (this[kTunnelProxy]) {
229
+ return true
230
+ }
231
+ if (uri.protocol !== 'http:' || this[kProxy].protocol !== 'http:') {
232
+ return true
233
+ }
234
+ return false
235
+ }
236
+ }
237
+
238
+ /**
239
+ * @param {string[] | Record<string, string>} headers
240
+ * @returns {Record<string, string>}
241
+ */
242
+ function buildHeaders (headers) {
243
+ // When using undici.fetch, the headers list is stored
244
+ // as an array.
245
+ if (Array.isArray(headers)) {
246
+ /** @type {Record<string, string>} */
247
+ const headersPair = {}
248
+
249
+ for (let i = 0; i < headers.length; i += 2) {
250
+ headersPair[headers[i]] = headers[i + 1]
251
+ }
252
+
253
+ return headersPair
254
+ }
255
+
256
+ return headers
257
+ }
258
+
259
+ /**
260
+ * @param {Record<string, string>} headers
261
+ *
262
+ * Previous versions of ProxyAgent suggests the Proxy-Authorization in request headers
263
+ * Nevertheless, it was changed and to avoid a security vulnerability by end users
264
+ * this check was created.
265
+ * It should be removed in the next major version for performance reasons
266
+ */
267
+ function throwIfProxyAuthIsSent (headers) {
268
+ const existProxyAuth = headers && Object.keys(headers)
269
+ .find((key) => key.toLowerCase() === 'proxy-authorization')
270
+ if (existProxyAuth) {
271
+ throw new InvalidArgumentError('Proxy-Authorization should be sent in ProxyAgent constructor')
272
+ }
273
+ }
274
+
275
+ module.exports = ProxyAgent
@@ -0,0 +1,35 @@
1
+ 'use strict'
2
+
3
+ const Dispatcher = require('./dispatcher')
4
+ const RetryHandler = require('../handler/retry-handler')
5
+
6
+ class RetryAgent extends Dispatcher {
7
+ #agent = null
8
+ #options = null
9
+ constructor (agent, options = {}) {
10
+ super(options)
11
+ this.#agent = agent
12
+ this.#options = options
13
+ }
14
+
15
+ dispatch (opts, handler) {
16
+ const retry = new RetryHandler({
17
+ ...opts,
18
+ retryOptions: this.#options
19
+ }, {
20
+ dispatch: this.#agent.dispatch.bind(this.#agent),
21
+ handler
22
+ })
23
+ return this.#agent.dispatch(opts, retry)
24
+ }
25
+
26
+ close () {
27
+ return this.#agent.close()
28
+ }
29
+
30
+ destroy () {
31
+ return this.#agent.destroy()
32
+ }
33
+ }
34
+
35
+ module.exports = RetryAgent
@@ -0,0 +1,32 @@
1
+ 'use strict'
2
+
3
+ // We include a version number for the Dispatcher API. In case of breaking changes,
4
+ // this version number must be increased to avoid conflicts.
5
+ const globalDispatcher = Symbol.for('undici.globalDispatcher.1')
6
+ const { InvalidArgumentError } = require('./core/errors')
7
+ const Agent = require('./dispatcher/agent')
8
+
9
+ if (getGlobalDispatcher() === undefined) {
10
+ setGlobalDispatcher(new Agent())
11
+ }
12
+
13
+ function setGlobalDispatcher (agent) {
14
+ if (!agent || typeof agent.dispatch !== 'function') {
15
+ throw new InvalidArgumentError('Argument agent must implement Agent')
16
+ }
17
+ Object.defineProperty(globalThis, globalDispatcher, {
18
+ value: agent,
19
+ writable: true,
20
+ enumerable: false,
21
+ configurable: false
22
+ })
23
+ }
24
+
25
+ function getGlobalDispatcher () {
26
+ return globalThis[globalDispatcher]
27
+ }
28
+
29
+ module.exports = {
30
+ setGlobalDispatcher,
31
+ getGlobalDispatcher
32
+ }