Browse Source

Use pipe instead of manually piping inside data

cache-clone-stream
Luke Childs 8 years ago
parent
commit
159c3994a8
  1. 6
      index.js

6
index.js

@ -212,21 +212,19 @@ function asStream(opts) {
if (opts.cache && policy.storable()) { if (opts.cache && policy.storable()) {
const bodyChunks = []; const bodyChunks = [];
res.on('data', chunk => { res.on('data', chunk => {
output.push(chunk);
bodyChunks.push(chunk); bodyChunks.push(chunk);
}); });
res.on('end', () => { res.on('end', () => {
output.push(null);
res.body = Buffer.concat(bodyChunks); res.body = Buffer.concat(bodyChunks);
if (opts.encoding !== null) { if (opts.encoding !== null) {
res.body = res.body.toString(opts.encoding); res.body = res.body.toString(opts.encoding);
} }
cacheResponse(res, policy, opts); cacheResponse(res, policy, opts);
}); });
} else {
res.pipe(output);
} }
res.pipe(output);
if (statusCode !== 304 && (statusCode < 200 || statusCode > 299)) { if (statusCode !== 304 && (statusCode < 200 || statusCode > 299)) {
proxy.emit('error', new got.HTTPError(statusCode, opts), null, res); proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
return; return;

Loading…
Cancel
Save