MCPcopy
hub / github.com/nodejs/undici / consume

Function consume

lib/api/readable.js:414–454  ·  view source on GitHub ↗

* @template {ConsumeType} T * @param {BodyReadable} stream * @param {T} type * @returns {Promise >}

(stream, type)

Source from the content-addressed store, hash-verified

412 * @returns {Promise<ConsumeReturnType<T>>}
413 */
414function consume (stream, type) {
415 assert(!stream[kConsume])
416
417 return new Promise((resolve, reject) => {
418 if (isUnusable(stream)) {
419 const rState = stream._readableState
420 if (rState.destroyed && rState.closeEmitted === false) {
421 stream
422 .on('error', reject)
423 .on('close', () => {
424 reject(new TypeError('unusable'))
425 })
426 } else {
427 reject(rState.errored ?? new TypeError('unusable'))
428 }
429 } else {
430 queueMicrotask(() => {
431 stream[kConsume] = {
432 type,
433 stream,
434 resolve,
435 reject,
436 length: 0,
437 body: []
438 }
439
440 stream
441 .on('error', function (err) {
442 consumeFinish(this[kConsume], err)
443 })
444 .on('close', function () {
445 if (this[kConsume].body !== null) {
446 consumeFinish(this[kConsume], new RequestAbortedError())
447 }
448 })
449
450 consumeStart(stream[kConsume])
451 })
452 }
453 })
454}
455
456/**
457 * @param {Consume} consume

Callers 5

textMethod · 0.85
jsonMethod · 0.85
blobMethod · 0.85
bytesMethod · 0.85
arrayBufferMethod · 0.85

Calls 5

isUnusableFunction · 0.85
rejectFunction · 0.85
consumeFinishFunction · 0.85
consumeStartFunction · 0.85
onMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…