* Dumps the response body by reading `limit` number of bytes. * @param {object} opts * @param {number} [opts.limit = 131072] Number of bytes to read. * @param {AbortSignal} [opts.signal] An AbortSignal to cancel the dump. * @returns {Promise }
(opts)
| 264 | * @returns {Promise<null>} |
| 265 | */ |
| 266 | dump (opts) { |
| 267 | const signal = opts?.signal |
| 268 | |
| 269 | if (signal != null && (typeof signal !== 'object' || !('aborted' in signal))) { |
| 270 | return Promise.reject(new InvalidArgumentError('signal must be an AbortSignal')) |
| 271 | } |
| 272 | |
| 273 | const limit = opts?.limit && Number.isFinite(opts.limit) |
| 274 | ? opts.limit |
| 275 | : 128 * 1024 |
| 276 | |
| 277 | if (signal?.aborted) { |
| 278 | return Promise.reject(signal.reason ?? new AbortError()) |
| 279 | } |
| 280 | |
| 281 | if (this._readableState.closeEmitted) { |
| 282 | return Promise.resolve(null) |
| 283 | } |
| 284 | |
| 285 | return new Promise((resolve, reject) => { |
| 286 | if ( |
| 287 | (this[kContentLength] && (this[kContentLength] > limit)) || |
| 288 | this[kBytesRead] > limit |
| 289 | ) { |
| 290 | this.destroy(new AbortError()) |
| 291 | } |
| 292 | |
| 293 | if (signal) { |
| 294 | const onAbort = () => { |
| 295 | this.destroy(signal.reason ?? new AbortError()) |
| 296 | } |
| 297 | const abortListener = addAbortListener(signal, onAbort) |
| 298 | this |
| 299 | .on('close', function () { |
| 300 | abortListener[Symbol.dispose]() |
| 301 | if (signal.aborted) { |
| 302 | reject(signal.reason ?? new AbortError()) |
| 303 | } else { |
| 304 | resolve(null) |
| 305 | } |
| 306 | }) |
| 307 | } else { |
| 308 | this.on('close', resolve) |
| 309 | } |
| 310 | |
| 311 | this |
| 312 | .on('error', noop) |
| 313 | .on('data', () => { |
| 314 | if (this[kBytesRead] > limit) { |
| 315 | this.destroy() |
| 316 | } |
| 317 | }) |
| 318 | .resume() |
| 319 | }) |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * @param {BufferEncoding} encoding |