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