Skip to content

Commit

Permalink
A better fix for Automattic#191 error emissions.
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Jul 13, 2013
1 parent 1708bd0 commit ccdc5a8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,21 @@ Client.prototype.putFile = function(src, filename, headers, fn){
}, headers);

var stream = fs.createReadStream(src);
var req = self.putStream(stream, filename, headers, fn);

var fnCalled = false;
function wrappedFn(err, result) {
if (fnCalled) {
if (err) {
emitter.emit('error', err);
}
} else {
fnCalled = true;
fn(err, result);
}
}
var req = self.putStream(stream, filename, headers, wrappedFn);

req.on('progress', emitter.emit.bind(emitter, 'progress'));
req.on('error', emitter.emit.bind(emitter, 'error'));
});

return emitter;
Expand Down

0 comments on commit ccdc5a8

Please sign in to comment.