From e0f3fce96acc46b4be28cb8e982955ec19ecf274 Mon Sep 17 00:00:00 2001 From: Loren Segal Date: Thu, 10 Jul 2014 01:27:15 -0700 Subject: [PATCH] Refactor Request.createReadStream() implementation Resolves behavioral change in Node.js v0.10.29 that caused download failures when using streams over HTTPS. Fixes #312 --- lib/request.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/request.js b/lib/request.js index 0ab5505d1b..3cca95d9dc 100644 --- a/lib/request.js +++ b/lib/request.js @@ -517,7 +517,7 @@ AWS.Request = inherit({ if (AWS.HttpClient.streamsApiVersion === 2) { stream = new streams.Readable(); - stream._read = function() { stream.push(''); }; + stream._read = function() {}; } else { stream = new streams.Stream(); stream.readable = true; @@ -544,21 +544,27 @@ AWS.Request = inherit({ }); var httpStream = resp.httpResponse.stream; - stream.response = resp; - stream._read = function() { - var data; - do { - data = httpStream.read(); - if (data) stream.push(data); - } while (data); - stream.push(''); - }; - - var events = ['end', 'error', (legacyStreams ? 'data' : 'readable')]; - AWS.util.arrayEach(events, function(event) { - httpStream.on(event, function(arg) { - stream.emit(event, arg); + if (legacyStreams) { + httpStream.on('data', function(arg) { + stream.emit('data', arg); }); + httpStream.on('end', function() { + stream.emit('end'); + }); + } else { + httpStream.on('readable', function() { + while (null !== (chunk = httpStream.read())) { + stream.push(chunk); + } + stream.read(0); + }); + httpStream.on('end', function() { + stream.push(null); + }); + } + + httpStream.on('error', function(err) { + stream.emit('error', err); }); } });