get-response.js 935 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. const decompressResponse = require('decompress-response');
  3. const is = require('@sindresorhus/is');
  4. const mimicResponse = require('mimic-response');
  5. const progress = require('./progress');
  6. module.exports = (response, options, emitter) => {
  7. const downloadBodySize = Number(response.headers['content-length']) || null;
  8. const progressStream = progress.download(response, emitter, downloadBodySize);
  9. mimicResponse(response, progressStream);
  10. const newResponse = options.decompress === true &&
  11. is.function(decompressResponse) &&
  12. options.method !== 'HEAD' ? decompressResponse(progressStream) : progressStream;
  13. if (!options.decompress && ['gzip', 'deflate'].includes(response.headers['content-encoding'])) {
  14. options.encoding = null;
  15. }
  16. emitter.emit('response', newResponse);
  17. emitter.emit('downloadProgress', {
  18. percent: 0,
  19. transferred: 0,
  20. total: downloadBodySize
  21. });
  22. response.pipe(progressStream);
  23. };