src/body.js
function consumeBody() {
	if (this[INTERNALS].disturbed) {
		return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`));
	}

	this[INTERNALS].disturbed = true;

	if (this[INTERNALS].error) {
		return Body.Promise.reject(this[INTERNALS].error);
	}

	let body = this.body;

	// body is null
	if (body === null) {
		return Body.Promise.resolve(Buffer.alloc(0));
	}

	// body is blob
	if (isBlob(body)) {
		body = body.stream();
	}

	// body is buffer
	if (Buffer.isBuffer(body)) {
		return Body.Promise.resolve(body);
	}

	// istanbul ignore if: should never happen
	if (!(body instanceof Stream)) {
		return Body.Promise.resolve(Buffer.alloc(0));
	}

	// body is stream
	// get ready to actually consume the body
	let accum = [];
	let accumBytes = 0;
	let abort = false;

	return new Body.Promise((resolve, reject) => {
		let resTimeout;

		// allow timeout on slow response body
		if (this.timeout) {
			resTimeout = setTimeout(() => {
				abort = true;
				reject(new FetchError(`Response timeout while trying to fetch ${this.url} (over ${this.timeout}ms)`, 'body-timeout'));
			}, this.timeout);
		}

		// handle stream errors
		body.on('error', err => {
			if (err.name === 'AbortError') {
				// if the request was aborted, reject with this Error
				abort = true;
				reject(err);
			} else {
				// other errors, such as incorrect content-encoding
				reject(new FetchError(`Invalid response body while trying to fetch ${this.url}: ${err.message}`, 'system', err));
			}
		});

		body.on('data', chunk => {
			if (abort || chunk === null) {
				return;
			}

			if (this.size && accumBytes + chunk.length > this.size) {
				abort = true;
				reject(new FetchError(`content size at ${this.url} over limit: ${this.size}`, 'max-size'));
				return;
			}

			accumBytes += chunk.length;
			accum.push(chunk);
		});

		body.on('end', () => {
			if (abort) {
				return;
			}

			clearTimeout(resTimeout);

			try {
				resolve(Buffer.concat(accum, accumBytes));
			} catch (err) {
				// handle streams that have accumulated too much data (issue #414)
				reject(new FetchError(`Could not create Buffer from response body for ${this.url}: ${err.message}`, 'system', err));
			}
		});
	});
}
function consumeBody() {
	if (this[INTERNALS].disturbed) {
		return Body.Promise.reject(new TypeError(`body used already for: ${this.url}`));
	}

	this[INTERNALS].disturbed = true;

	if (this[INTERNALS].error) {
		return Body.Promise.reject(this[INTERNALS].error);
	}

	let body = this.body;

	// body is null
	if (body === null) {
		return Body.Promise.resolve(Buffer.alloc(0));
	}

	// body is blob
	if (isBlob(body)) {
		body = body.stream();
	}

	// body is buffer
	if (Buffer.isBuffer(body)) {
		return Body.Promise.resolve(body);
	}

	// istanbul ignore if: should never happen
	if (!(body instanceof Stream)) {
		return Body.Promise.resolve(Buffer.alloc(0));
	}

	// body is stream
	// get ready to actually consume the body
	let accum = [];
	let accumBytes = 0;
	let abort = false;

	return new Body.Promise((resolve, reject) => {
		let resTimeout;

		// allow timeout on slow response body
		if (this.timeout) {
			resTimeout = setTimeout(() => {
				abort = true;
				reject(new FetchError(`Response timeout while trying to fetch ${this.url} (over ${this.timeout}ms)`, 'body-timeout'));
			}, this.timeout);
		}

		// handle stream errors
		body.on('error', err => {
			if (err.name === 'AbortError') {
				// if the request was aborted, reject with this Error
				abort = true;
				reject(err);
			} else {
				// other errors, such as incorrect content-encoding
				reject(new FetchError(`Invalid response body while trying to fetch ${this.url}: ${err.message}`, 'system', err));
			}
		});

		body.on('data', chunk => {
			if (abort || chunk === null) {
				return;
			}

			if (this.size && accumBytes + chunk.length > this.size) {
				abort = true;
				reject(new FetchError(`content size at ${this.url} over limit: ${this.size}`, 'max-size'));
				return;
			}

			accumBytes += chunk.length;
			accum.push(chunk);
		});

		body.on('end', () => {
			if (abort) {
				return;
			}

			clearTimeout(resTimeout);

			try {
				resolve(Buffer.concat(accum, accumBytes));
			} catch (err) {
				// handle streams that have accumulated too much data (issue #414)
				reject(new FetchError(`Could not create Buffer from response body for ${this.url}: ${err.message}`, 'system', err));
			}
		});
	});
}
function consumeBody ( ) { if ( this [ INTERNALS ] . disturbed ) { return Body . Promise . reject ( new TypeError ( ` ${ this . url } ` ) ) ; } this [ INTERNALS ] . disturbed = true ; if ( this [ INTERNALS ] . error ) { return Body . Promise . reject ( this [ INTERNALS ] . error ) ; } let body = this . body ; // body is null if ( body === null ) { return Body . Promise . resolve ( Buffer . alloc ( 0 ) ) ; } // body is blob if ( isBlob ( body ) ) { body = body . stream ( ) ; } // body is buffer if ( Buffer . isBuffer ( body ) ) { return Body . Promise . resolve ( body ) ; } // istanbul ignore if: should never happen if ( ! ( body instanceof Stream ) ) { return Body . Promise . resolve ( Buffer . alloc ( 0 ) ) ; } // body is stream // get ready to actually consume the body let accum = [ ] ; let accumBytes = 0 ; let abort = false ; return new Body . Promise ( ( resolve , reject ) => { let resTimeout ; // allow timeout on slow response body if ( this . timeout ) { resTimeout = setTimeout ( ( ) => { abort = true ; reject ( new FetchError ( ` ${ this . url } ${ this . timeout } ` , 'body-timeout' ) ) ; } , this . timeout ) ; } // handle stream errors body . on ( 'error' , err => { if ( err . name === 'AbortError' ) { // if the request was aborted, reject with this Error abort = true ; reject ( err ) ; } else { // other errors, such as incorrect content-encoding reject ( new FetchError ( ` ${ this . url } ${ err . message } ` , 'system' , err ) ) ; } } ) ; body . on ( 'data' , chunk => { if ( abort || chunk === null ) { return ; } if ( this . size && accumBytes + chunk . length > this . size ) { abort = true ; reject ( new FetchError ( ` ${ this . url } ${ this . size } ` , 'max-size' ) ) ; return ; } accumBytes += chunk . length ; accum . push ( chunk ) ; } ) ; body . on ( 'end' , ( ) => { if ( abort ) { return ; } clearTimeout ( resTimeout ) ; try { resolve ( Buffer . concat ( accum , accumBytes ) ) ; } catch ( err ) { // handle streams that have accumulated too much data (issue #414) reject ( new FetchError ( ` ${ this . url } ${ err . message } ` , 'system' , err ) ) ; } } ) ; } ) ; }
Consume and convert an entire Body to a Buffer.

Ref: https://fetch.spec.whatwg.org/#concept-body-consume-body

@return  Promise
Consume and convert an entire Body to a Buffer .