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

Function consumeBody

lib/web/fetch/body.js:455–501  ·  view source on GitHub ↗

* @see https://fetch.spec.whatwg.org/#concept-body-consume-body * @param {any} object internal state * @param {(value: unknown) => unknown} convertBytesToJSValue * @param {any} instance * @param {(target: any) => any} getInternalState

(object, convertBytesToJSValue, instance, getInternalState)

Source from the content-addressed store, hash-verified

453 * @param {(target: any) => any} getInternalState
454 */
455function consumeBody (object, convertBytesToJSValue, instance, getInternalState) {
456 try {
457 webidl.brandCheck(object, instance)
458 } catch (e) {
459 return Promise.reject(e)
460 }
461
462 object = getInternalState(object)
463
464 // 1. If object is unusable, then return a promise rejected
465 // with a TypeError.
466 if (bodyUnusable(object)) {
467 return Promise.reject(new TypeError('Body is unusable: Body has already been read'))
468 }
469
470 // 2. Let promise be a new promise.
471 const promise = Promise.withResolvers()
472
473 // 3. Let errorSteps given error be to reject promise with error.
474 const errorSteps = promise.reject
475
476 // 4. Let successSteps given a byte sequence data be to resolve
477 // promise with the result of running convertBytesToJSValue
478 // with data. If that threw an exception, then run errorSteps
479 // with that exception.
480 const successSteps = (data) => {
481 try {
482 promise.resolve(convertBytesToJSValue(data))
483 } catch (e) {
484 errorSteps(e)
485 }
486 }
487
488 // 5. If object’s body is null, then run successSteps with an
489 // empty byte sequence.
490 if (object.body == null) {
491 successSteps(Buffer.allocUnsafe(0))
492 return promise.promise
493 }
494
495 // 6. Otherwise, fully read object’s body given successSteps,
496 // errorSteps, and object’s relevant global object.
497 fullyReadBody(object.body, successSteps, errorSteps)
498
499 // 7. Return promise.
500 return promise.promise
501}
502
503/**
504 * @see https://fetch.spec.whatwg.org/#body-unusable

Callers 6

blobFunction · 0.85
arrayBufferFunction · 0.85
textFunction · 0.85
jsonFunction · 0.85
formDataFunction · 0.85
bytesFunction · 0.85

Calls 4

bodyUnusableFunction · 0.85
successStepsFunction · 0.85
fullyReadBodyFunction · 0.85
brandCheckMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…