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

  this[DISTURBED] = true;

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

  // body is string
  if (typeof this.body === 'string') {
    return Body.Promise.resolve(Buffer.from(this.body));
  }

  // body is blob
  if (this.body instanceof Blob) {
    return Body.Promise.resolve(this.body.buffer);
  }

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

  // istanbul ignore if: should never happen
  if (!(this.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 error, such as incorrect content-encoding
    this.body.on('error', err => {
      reject(new FetchError(`Invalid response body while trying to fetch ${this.url}: ${err.message}`, 'system', err));
    });

    this.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);
    });

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

      clearTimeout(resTimeout);
      resolve(Buffer.concat(accum));
    });
  });
}
function consumeBody(body) {
  if (this[DISTURBED]) {
    return Body.Promise.reject(new Error(`body used already for: ${this.url}`));
  }

  this[DISTURBED] = true;

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

  // body is string
  if (typeof this.body === 'string') {
    return Body.Promise.resolve(Buffer.from(this.body));
  }

  // body is blob
  if (this.body instanceof Blob) {
    return Body.Promise.resolve(this.body.buffer);
  }

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

  // istanbul ignore if: should never happen
  if (!(this.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 error, such as incorrect content-encoding
    this.body.on('error', err => {
      reject(new FetchError(`Invalid response body while trying to fetch ${this.url}: ${err.message}`, 'system', err));
    });

    this.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);
    });

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

      clearTimeout(resTimeout);
      resolve(Buffer.concat(accum));
    });
  });
}
function consumeBody ( body ) { if ( this [ DISTURBED ] ) { return Body . Promise . reject ( new Error ( ` ${ this . url } ` ) ) ; } this [ DISTURBED ] = true ; // body is null if ( this . body === null ) { return Body . Promise . resolve ( Buffer . alloc ( 0 ) ) ; } // body is string if ( typeof this . body === 'string' ) { return Body . Promise . resolve ( Buffer . from ( this . body ) ) ; } // body is blob if ( this . body instanceof Blob ) { return Body . Promise . resolve ( this . body . buffer ) ; } // body is buffer if ( Buffer . isBuffer ( this . body ) ) { return Body . Promise . resolve ( this . body ) ; } // istanbul ignore if: should never happen if ( ! ( this . 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 error, such as incorrect content-encoding this . body . on ( 'error' , err => { reject ( new FetchError ( ` ${ this . url } ${ err . message } ` , 'system' , err ) ) ; } ) ; this . 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 ) ; } ) ; this . body . on ( 'end' , ( ) => { if ( abort ) { return ; } clearTimeout ( resTimeout ) ; resolve ( Buffer . concat ( accum ) ) ; } ) ; } ) ; }
Decode buffers into utf-8 string

@return  Promise
Decode buffers into utf - 8 string