Skip to content

Change handling of end override so finish gets called at a more appropriate time #40

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions lib/s3-upload-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -311,8 +312,8 @@ 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;
}
}
Expand Down