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,558 @@
1
+ // Ported from https://github.com/nodejs/undici/pull/907
2
+
3
+ 'use strict'
4
+
5
+ const assert = require('node:assert')
6
+ const { Readable } = require('node:stream')
7
+ const { RequestAbortedError, NotSupportedError, InvalidArgumentError, AbortError } = require('../core/errors')
8
+ const util = require('../core/util')
9
+ const { ReadableStreamFrom } = require('../core/util')
10
+
11
+ const kConsume = Symbol('kConsume')
12
+ const kReading = Symbol('kReading')
13
+ const kBody = Symbol('kBody')
14
+ const kAbort = Symbol('kAbort')
15
+ const kContentType = Symbol('kContentType')
16
+ const kContentLength = Symbol('kContentLength')
17
+ const kUsed = Symbol('kUsed')
18
+ const kBytesRead = Symbol('kBytesRead')
19
+
20
+ const noop = () => {}
21
+
22
+ /**
23
+ * @class
24
+ * @extends {Readable}
25
+ * @see https://fetch.spec.whatwg.org/#body
26
+ */
27
+ class BodyReadable extends Readable {
28
+ /**
29
+ * @param {object} opts
30
+ * @param {(this: Readable, size: number) => void} opts.resume
31
+ * @param {() => (void | null)} opts.abort
32
+ * @param {string} [opts.contentType = '']
33
+ * @param {number} [opts.contentLength]
34
+ * @param {number} [opts.highWaterMark = 64 * 1024]
35
+ */
36
+ constructor ({
37
+ resume,
38
+ abort,
39
+ contentType = '',
40
+ contentLength,
41
+ highWaterMark = 64 * 1024 // Same as nodejs fs streams.
42
+ }) {
43
+ super({
44
+ autoDestroy: true,
45
+ read: resume,
46
+ highWaterMark
47
+ })
48
+
49
+ this._readableState.dataEmitted = false
50
+
51
+ this[kAbort] = abort
52
+
53
+ /**
54
+ * @type {Consume | null}
55
+ */
56
+ this[kConsume] = null
57
+ this[kBytesRead] = 0
58
+ /**
59
+ * @type {ReadableStream|null}
60
+ */
61
+ this[kBody] = null
62
+ this[kUsed] = false
63
+ this[kContentType] = contentType
64
+ this[kContentLength] = Number.isFinite(contentLength) ? contentLength : null
65
+
66
+ // Is stream being consumed through Readable API?
67
+ // This is an optimization so that we avoid checking
68
+ // for 'data' and 'readable' listeners in the hot path
69
+ // inside push().
70
+ this[kReading] = false
71
+ }
72
+
73
+ /**
74
+ * @param {Error|null} err
75
+ * @param {(error:(Error|null)) => void} callback
76
+ * @returns {void}
77
+ */
78
+ _destroy (err, callback) {
79
+ if (!err && !this._readableState.endEmitted) {
80
+ err = new RequestAbortedError()
81
+ }
82
+
83
+ if (err) {
84
+ this[kAbort]()
85
+ }
86
+
87
+ // Workaround for Node "bug". If the stream is destroyed in same
88
+ // tick as it is created, then a user who is waiting for a
89
+ // promise (i.e micro tick) for installing an 'error' listener will
90
+ // never get a chance and will always encounter an unhandled exception.
91
+ if (!this[kUsed]) {
92
+ setImmediate(() => {
93
+ callback(err)
94
+ })
95
+ } else {
96
+ callback(err)
97
+ }
98
+ }
99
+
100
+ /**
101
+ * @param {string} event
102
+ * @param {(...args: any[]) => void} listener
103
+ * @returns {this}
104
+ */
105
+ on (event, listener) {
106
+ if (event === 'data' || event === 'readable') {
107
+ this[kReading] = true
108
+ this[kUsed] = true
109
+ }
110
+ return super.on(event, listener)
111
+ }
112
+
113
+ /**
114
+ * @param {string} event
115
+ * @param {(...args: any[]) => void} listener
116
+ * @returns {this}
117
+ */
118
+ addListener (event, listener) {
119
+ return this.on(event, listener)
120
+ }
121
+
122
+ /**
123
+ * @param {string|symbol} event
124
+ * @param {(...args: any[]) => void} listener
125
+ * @returns {this}
126
+ */
127
+ off (event, listener) {
128
+ const ret = super.off(event, listener)
129
+ if (event === 'data' || event === 'readable') {
130
+ this[kReading] = (
131
+ this.listenerCount('data') > 0 ||
132
+ this.listenerCount('readable') > 0
133
+ )
134
+ }
135
+ return ret
136
+ }
137
+
138
+ /**
139
+ * @param {string|symbol} event
140
+ * @param {(...args: any[]) => void} listener
141
+ * @returns {this}
142
+ */
143
+ removeListener (event, listener) {
144
+ return this.off(event, listener)
145
+ }
146
+
147
+ /**
148
+ * @param {Buffer|null} chunk
149
+ * @returns {boolean}
150
+ */
151
+ push (chunk) {
152
+ this[kBytesRead] += chunk ? chunk.length : 0
153
+
154
+ if (this[kConsume] && chunk !== null) {
155
+ consumePush(this[kConsume], chunk)
156
+ return this[kReading] ? super.push(chunk) : true
157
+ }
158
+ return super.push(chunk)
159
+ }
160
+
161
+ /**
162
+ * Consumes and returns the body as a string.
163
+ *
164
+ * @see https://fetch.spec.whatwg.org/#dom-body-text
165
+ * @returns {Promise<string>}
166
+ */
167
+ text () {
168
+ return consume(this, 'text')
169
+ }
170
+
171
+ /**
172
+ * Consumes and returns the body as a JavaScript Object.
173
+ *
174
+ * @see https://fetch.spec.whatwg.org/#dom-body-json
175
+ * @returns {Promise<unknown>}
176
+ */
177
+ json () {
178
+ return consume(this, 'json')
179
+ }
180
+
181
+ /**
182
+ * Consumes and returns the body as a Blob
183
+ *
184
+ * @see https://fetch.spec.whatwg.org/#dom-body-blob
185
+ * @returns {Promise<Blob>}
186
+ */
187
+ blob () {
188
+ return consume(this, 'blob')
189
+ }
190
+
191
+ /**
192
+ * Consumes and returns the body as an Uint8Array.
193
+ *
194
+ * @see https://fetch.spec.whatwg.org/#dom-body-bytes
195
+ * @returns {Promise<Uint8Array>}
196
+ */
197
+ bytes () {
198
+ return consume(this, 'bytes')
199
+ }
200
+
201
+ /**
202
+ * Consumes and returns the body as an ArrayBuffer.
203
+ *
204
+ * @see https://fetch.spec.whatwg.org/#dom-body-arraybuffer
205
+ * @returns {Promise<ArrayBuffer>}
206
+ */
207
+ arrayBuffer () {
208
+ return consume(this, 'arrayBuffer')
209
+ }
210
+
211
+ /**
212
+ * Not implemented
213
+ *
214
+ * @see https://fetch.spec.whatwg.org/#dom-body-formdata
215
+ * @throws {NotSupportedError}
216
+ */
217
+ async formData () {
218
+ // TODO: Implement.
219
+ throw new NotSupportedError()
220
+ }
221
+
222
+ /**
223
+ * Returns true if the body is not null and the body has been consumed.
224
+ * Otherwise, returns false.
225
+ *
226
+ * @see https://fetch.spec.whatwg.org/#dom-body-bodyused
227
+ * @readonly
228
+ * @returns {boolean}
229
+ */
230
+ get bodyUsed () {
231
+ return util.isDisturbed(this)
232
+ }
233
+
234
+ /**
235
+ * @see https://fetch.spec.whatwg.org/#dom-body-body
236
+ * @readonly
237
+ * @returns {ReadableStream}
238
+ */
239
+ get body () {
240
+ if (!this[kBody]) {
241
+ this[kBody] = ReadableStreamFrom(this)
242
+ if (this[kConsume]) {
243
+ // TODO: Is this the best way to force a lock?
244
+ this[kBody].getReader() // Ensure stream is locked.
245
+ assert(this[kBody].locked)
246
+ }
247
+ }
248
+ return this[kBody]
249
+ }
250
+
251
+ /**
252
+ * Dumps the response body by reading `limit` number of bytes.
253
+ * @param {object} opts
254
+ * @param {number} [opts.limit = 131072] Number of bytes to read.
255
+ * @param {AbortSignal} [opts.signal] An AbortSignal to cancel the dump.
256
+ * @returns {Promise<null>}
257
+ */
258
+ async dump (opts) {
259
+ const signal = opts?.signal
260
+
261
+ if (signal != null && (typeof signal !== 'object' || !('aborted' in signal))) {
262
+ throw new InvalidArgumentError('signal must be an AbortSignal')
263
+ }
264
+
265
+ const limit = opts?.limit && Number.isFinite(opts.limit)
266
+ ? opts.limit
267
+ : 128 * 1024
268
+
269
+ signal?.throwIfAborted()
270
+
271
+ if (this._readableState.closeEmitted) {
272
+ return null
273
+ }
274
+
275
+ return await new Promise((resolve, reject) => {
276
+ if (
277
+ (this[kContentLength] && (this[kContentLength] > limit)) ||
278
+ this[kBytesRead] > limit
279
+ ) {
280
+ this.destroy(new AbortError())
281
+ }
282
+
283
+ if (signal) {
284
+ const onAbort = () => {
285
+ this.destroy(signal.reason ?? new AbortError())
286
+ }
287
+ signal.addEventListener('abort', onAbort)
288
+ this
289
+ .on('close', function () {
290
+ signal.removeEventListener('abort', onAbort)
291
+ if (signal.aborted) {
292
+ reject(signal.reason ?? new AbortError())
293
+ } else {
294
+ resolve(null)
295
+ }
296
+ })
297
+ } else {
298
+ this.on('close', resolve)
299
+ }
300
+
301
+ this
302
+ .on('error', noop)
303
+ .on('data', () => {
304
+ if (this[kBytesRead] > limit) {
305
+ this.destroy()
306
+ }
307
+ })
308
+ .resume()
309
+ })
310
+ }
311
+
312
+ /**
313
+ * @param {BufferEncoding} encoding
314
+ * @returns {this}
315
+ */
316
+ setEncoding (encoding) {
317
+ if (Buffer.isEncoding(encoding)) {
318
+ this._readableState.encoding = encoding
319
+ }
320
+ return this
321
+ }
322
+ }
323
+
324
+ /**
325
+ * @see https://streams.spec.whatwg.org/#readablestream-locked
326
+ * @param {BodyReadable} bodyReadable
327
+ * @returns {boolean}
328
+ */
329
+ function isLocked (bodyReadable) {
330
+ // Consume is an implicit lock.
331
+ return bodyReadable[kBody]?.locked === true || bodyReadable[kConsume] !== null
332
+ }
333
+
334
+ /**
335
+ * @see https://fetch.spec.whatwg.org/#body-unusable
336
+ * @param {BodyReadable} bodyReadable
337
+ * @returns {boolean}
338
+ */
339
+ function isUnusable (bodyReadable) {
340
+ return util.isDisturbed(bodyReadable) || isLocked(bodyReadable)
341
+ }
342
+
343
+ /**
344
+ * @typedef {object} Consume
345
+ * @property {string} type
346
+ * @property {BodyReadable} stream
347
+ * @property {((value?: any) => void)} resolve
348
+ * @property {((err: Error) => void)} reject
349
+ * @property {number} length
350
+ * @property {Buffer[]} body
351
+ */
352
+
353
+ /**
354
+ * @param {BodyReadable} stream
355
+ * @param {string} type
356
+ * @returns {Promise<any>}
357
+ */
358
+ function consume (stream, type) {
359
+ assert(!stream[kConsume])
360
+
361
+ return new Promise((resolve, reject) => {
362
+ if (isUnusable(stream)) {
363
+ const rState = stream._readableState
364
+ if (rState.destroyed && rState.closeEmitted === false) {
365
+ stream
366
+ .on('error', err => {
367
+ reject(err)
368
+ })
369
+ .on('close', () => {
370
+ reject(new TypeError('unusable'))
371
+ })
372
+ } else {
373
+ reject(rState.errored ?? new TypeError('unusable'))
374
+ }
375
+ } else {
376
+ queueMicrotask(() => {
377
+ stream[kConsume] = {
378
+ type,
379
+ stream,
380
+ resolve,
381
+ reject,
382
+ length: 0,
383
+ body: []
384
+ }
385
+
386
+ stream
387
+ .on('error', function (err) {
388
+ consumeFinish(this[kConsume], err)
389
+ })
390
+ .on('close', function () {
391
+ if (this[kConsume].body !== null) {
392
+ consumeFinish(this[kConsume], new RequestAbortedError())
393
+ }
394
+ })
395
+
396
+ consumeStart(stream[kConsume])
397
+ })
398
+ }
399
+ })
400
+ }
401
+
402
+ /**
403
+ * @param {Consume} consume
404
+ * @returns {void}
405
+ */
406
+ function consumeStart (consume) {
407
+ if (consume.body === null) {
408
+ return
409
+ }
410
+
411
+ const { _readableState: state } = consume.stream
412
+
413
+ if (state.bufferIndex) {
414
+ const start = state.bufferIndex
415
+ const end = state.buffer.length
416
+ for (let n = start; n < end; n++) {
417
+ consumePush(consume, state.buffer[n])
418
+ }
419
+ } else {
420
+ for (const chunk of state.buffer) {
421
+ consumePush(consume, chunk)
422
+ }
423
+ }
424
+
425
+ if (state.endEmitted) {
426
+ consumeEnd(this[kConsume], this._readableState.encoding)
427
+ } else {
428
+ consume.stream.on('end', function () {
429
+ consumeEnd(this[kConsume], this._readableState.encoding)
430
+ })
431
+ }
432
+
433
+ consume.stream.resume()
434
+
435
+ while (consume.stream.read() != null) {
436
+ // Loop
437
+ }
438
+ }
439
+
440
+ /**
441
+ * @param {Buffer[]} chunks
442
+ * @param {number} length
443
+ * @param {BufferEncoding} encoding
444
+ * @returns {string}
445
+ */
446
+ function chunksDecode (chunks, length, encoding) {
447
+ if (chunks.length === 0 || length === 0) {
448
+ return ''
449
+ }
450
+ const buffer = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks, length)
451
+ const bufferLength = buffer.length
452
+
453
+ // Skip BOM.
454
+ const start =
455
+ bufferLength > 2 &&
456
+ buffer[0] === 0xef &&
457
+ buffer[1] === 0xbb &&
458
+ buffer[2] === 0xbf
459
+ ? 3
460
+ : 0
461
+ if (!encoding || encoding === 'utf8' || encoding === 'utf-8') {
462
+ return buffer.utf8Slice(start, bufferLength)
463
+ } else {
464
+ return buffer.subarray(start, bufferLength).toString(encoding)
465
+ }
466
+ }
467
+
468
+ /**
469
+ * @param {Buffer[]} chunks
470
+ * @param {number} length
471
+ * @returns {Uint8Array}
472
+ */
473
+ function chunksConcat (chunks, length) {
474
+ if (chunks.length === 0 || length === 0) {
475
+ return new Uint8Array(0)
476
+ }
477
+ if (chunks.length === 1) {
478
+ // fast-path
479
+ return new Uint8Array(chunks[0])
480
+ }
481
+ const buffer = new Uint8Array(Buffer.allocUnsafeSlow(length).buffer)
482
+
483
+ let offset = 0
484
+ for (let i = 0; i < chunks.length; ++i) {
485
+ const chunk = chunks[i]
486
+ buffer.set(chunk, offset)
487
+ offset += chunk.length
488
+ }
489
+
490
+ return buffer
491
+ }
492
+
493
+ /**
494
+ * @param {Consume} consume
495
+ * @param {BufferEncoding} encoding
496
+ * @returns {void}
497
+ */
498
+ function consumeEnd (consume, encoding) {
499
+ const { type, body, resolve, stream, length } = consume
500
+
501
+ try {
502
+ if (type === 'text') {
503
+ resolve(chunksDecode(body, length, encoding))
504
+ } else if (type === 'json') {
505
+ resolve(JSON.parse(chunksDecode(body, length, encoding)))
506
+ } else if (type === 'arrayBuffer') {
507
+ resolve(chunksConcat(body, length).buffer)
508
+ } else if (type === 'blob') {
509
+ resolve(new Blob(body, { type: stream[kContentType] }))
510
+ } else if (type === 'bytes') {
511
+ resolve(chunksConcat(body, length))
512
+ }
513
+
514
+ consumeFinish(consume)
515
+ } catch (err) {
516
+ stream.destroy(err)
517
+ }
518
+ }
519
+
520
+ /**
521
+ * @param {Consume} consume
522
+ * @param {Buffer} chunk
523
+ * @returns {void}
524
+ */
525
+ function consumePush (consume, chunk) {
526
+ consume.length += chunk.length
527
+ consume.body.push(chunk)
528
+ }
529
+
530
+ /**
531
+ * @param {Consume} consume
532
+ * @param {Error} [err]
533
+ * @returns {void}
534
+ */
535
+ function consumeFinish (consume, err) {
536
+ if (consume.body === null) {
537
+ return
538
+ }
539
+
540
+ if (err) {
541
+ consume.reject(err)
542
+ } else {
543
+ consume.resolve()
544
+ }
545
+
546
+ // Reset the consume object to allow for garbage collection.
547
+ consume.type = null
548
+ consume.stream = null
549
+ consume.resolve = null
550
+ consume.reject = null
551
+ consume.length = 0
552
+ consume.body = null
553
+ }
554
+
555
+ module.exports = {
556
+ Readable: BodyReadable,
557
+ chunksDecode
558
+ }
@@ -0,0 +1,95 @@
1
+ 'use strict'
2
+
3
+ const assert = require('node:assert')
4
+ const {
5
+ ResponseStatusCodeError
6
+ } = require('../core/errors')
7
+
8
+ const { chunksDecode } = require('./readable')
9
+ const CHUNK_LIMIT = 128 * 1024
10
+
11
+ async function getResolveErrorBodyCallback ({ callback, body, contentType, statusCode, statusMessage, headers }) {
12
+ assert(body)
13
+
14
+ let chunks = []
15
+ let length = 0
16
+
17
+ try {
18
+ for await (const chunk of body) {
19
+ chunks.push(chunk)
20
+ length += chunk.length
21
+ if (length > CHUNK_LIMIT) {
22
+ chunks = []
23
+ length = 0
24
+ break
25
+ }
26
+ }
27
+ } catch {
28
+ chunks = []
29
+ length = 0
30
+ // Do nothing....
31
+ }
32
+
33
+ const message = `Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`
34
+
35
+ if (statusCode === 204 || !contentType || !length) {
36
+ queueMicrotask(() => callback(new ResponseStatusCodeError(message, statusCode, headers)))
37
+ return
38
+ }
39
+
40
+ const stackTraceLimit = Error.stackTraceLimit
41
+ Error.stackTraceLimit = 0
42
+ let payload
43
+
44
+ try {
45
+ if (isContentTypeApplicationJson(contentType)) {
46
+ payload = JSON.parse(chunksDecode(chunks, length))
47
+ } else if (isContentTypeText(contentType)) {
48
+ payload = chunksDecode(chunks, length)
49
+ }
50
+ } catch {
51
+ // process in a callback to avoid throwing in the microtask queue
52
+ } finally {
53
+ Error.stackTraceLimit = stackTraceLimit
54
+ }
55
+ queueMicrotask(() => callback(new ResponseStatusCodeError(message, statusCode, headers, payload)))
56
+ }
57
+
58
+ const isContentTypeApplicationJson = (contentType) => {
59
+ return (
60
+ contentType.length > 15 &&
61
+ contentType[11] === '/' &&
62
+ contentType[0] === 'a' &&
63
+ contentType[1] === 'p' &&
64
+ contentType[2] === 'p' &&
65
+ contentType[3] === 'l' &&
66
+ contentType[4] === 'i' &&
67
+ contentType[5] === 'c' &&
68
+ contentType[6] === 'a' &&
69
+ contentType[7] === 't' &&
70
+ contentType[8] === 'i' &&
71
+ contentType[9] === 'o' &&
72
+ contentType[10] === 'n' &&
73
+ contentType[12] === 'j' &&
74
+ contentType[13] === 's' &&
75
+ contentType[14] === 'o' &&
76
+ contentType[15] === 'n'
77
+ )
78
+ }
79
+
80
+ const isContentTypeText = (contentType) => {
81
+ return (
82
+ contentType.length > 4 &&
83
+ contentType[4] === '/' &&
84
+ contentType[0] === 't' &&
85
+ contentType[1] === 'e' &&
86
+ contentType[2] === 'x' &&
87
+ contentType[3] === 't'
88
+ )
89
+ }
90
+
91
+ module.exports = {
92
+ getResolveErrorBodyCallback,
93
+ isContentTypeApplicationJson,
94
+ isContentTypeText
95
+ }