Skip to content

Commit

Permalink
Refactor Request.createReadStream() implementation
Browse files Browse the repository at this point in the history
Resolves behavioral change in Node.js v0.10.29 that caused
download failures when using streams over HTTPS.

Fixes #312
  • Loading branch information
lsegal committed Jul 10, 2014
1 parent f99ecca commit e0f3fce
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
}
});
Expand Down

0 comments on commit e0f3fce

Please sign in to comment.