From d2c3febf0ed1d7c6ab652eff64604f71140d72f0 Mon Sep 17 00:00:00 2001 From: Wyatt Pearsall Date: Wed, 17 Jun 2015 17:25:30 -0400 Subject: [PATCH 1/2] Fix end override to call finish only when actually finished --- lib/s3-upload-stream.js | 46 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/s3-upload-stream.js b/lib/s3-upload-stream.js index 3e83ab3..39019cd 100644 --- a/lib/s3-upload-stream.js +++ b/lib/s3-upload-stream.js @@ -265,35 +265,36 @@ Client.prototype.upload = function (destinationDetails, sessionDetails) { // Overwrite the end method so that we can hijack it to flush the last part and then complete // the multipart upload ws.originalEnd = ws.end; - ws.end = function (Part, encoding, callback) { - ws.originalEnd(Part, encoding, function afterDoneWithOriginalEnd() { - if (Part) - absorbBuffer(Part); - - // Upload any remaining data - var uploadRemainingData = function () { - if (receivedBuffersLength > 0) { - uploadHandler(uploadRemainingData); - return; - } + ws.end = function (Part, encoding, callback) { + if (Part) + absorbBuffer(Part); + + // Upload any remaining data + var uploadRemainingData = function (cb) { + if (receivedBuffersLength > 0) { + uploadHandler(uploadThenCall); + return; + } - if (pendingParts > 0) { - setTimeout(uploadRemainingData, 50); // Wait 50 ms for the pending uploads to finish before trying again. - return; - } + if (pendingParts > 0) { + setTimeout(uploadThenCall, 50); // Wait 50 ms for the pending uploads to finish before trying again. + return; + } - completeUpload(); - }; + return completeUpload(cb); + }; - uploadRemainingData(); + var callOriginal = function () { + return ws.originalEnd(Part, encoding, callback); + }; - if (typeof callback == 'function') - callback(); - }); + var uploadThenCall = uploadRemainingData.bind(null, callOriginal); + + uploadThenCall(); }; // Turn all the individual parts we uploaded to S3 into a finalized upload. - var completeUpload = function () { + var completeUpload = function (cb) { // There is a possibility that the incoming stream was empty, therefore the MPU never started // and cannot be finalized. if (multipartUploadID) { @@ -311,6 +312,7 @@ Client.prototype.upload = function (destinationDetails, sessionDetails) { abortUpload('Failed to complete the multipart upload on S3: ' + JSON.stringify(err)); else { // Emit both events for backwards compatibility, and to follow the spec. + if(cb) cb(err, result); ws.emit('uploaded', result); ws.emit('finish', result); started = false; From 0ec2550ad164516df0c75155d6cb55db5f1fec46 Mon Sep 17 00:00:00 2001 From: Wyatt Pearsall Date: Wed, 17 Jun 2015 17:34:57 -0400 Subject: [PATCH 2/2] Remove redundant finish event. --- lib/s3-upload-stream.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/s3-upload-stream.js b/lib/s3-upload-stream.js index 39019cd..6a49292 100644 --- a/lib/s3-upload-stream.js +++ b/lib/s3-upload-stream.js @@ -314,7 +314,6 @@ Client.prototype.upload = function (destinationDetails, sessionDetails) { // Emit both events for backwards compatibility, and to follow the spec. if(cb) cb(err, result); ws.emit('uploaded', result); - ws.emit('finish', result); started = false; } }