Skip to content

Commit

Permalink
Handling errors on ffprobe input stream; making sure callback only ge…
Browse files Browse the repository at this point in the history
…ts called once
  • Loading branch information
timdp authored and Nicolas Joyard committed Apr 30, 2016
1 parent 63877fd commit 39869d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/ffprobe.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ module.exports = function(proto) {
var input, index = null, options = [], callback;

// the last argument should be the callback
callback = arguments[arguments.length - 1];
var origCallback = arguments[arguments.length - 1];
callback = function() {
if (origCallback) {
var cb = origCallback;
origCallback = null;
cb.apply(this, arguments);
}
};

// map the arguments to the correct variable names
switch (arguments.length) {
Expand Down Expand Up @@ -144,11 +151,11 @@ module.exports = function(proto) {
if (input.isStream) {
input.source.pipe(ffprobe.stdin);
input.source.resume();

input.source.on('error', callback);
}

ffprobe.on('error', function(err) {
callback(err);
});
ffprobe.on('error', callback);

// Ensure we wait for captured streams to end before calling callback
var exitError = null;
Expand Down
17 changes: 17 additions & 0 deletions test/metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var Ffmpeg = require('../index'),
path = require('path'),
fs = require('fs'),
Readable = require('stream').Readable,
assert = require('assert'),
exec = require('child_process').exec,
testhelper = require('./helpers');
Expand Down Expand Up @@ -133,4 +134,20 @@ describe('Metadata', function() {
done();
});
});

it('should handle errors on stream input to ffprobe', function(done) {
var faultyStream = new Readable({objectMode: true});
faultyStream._read = function() {};
setTimeout(function() {
faultyStream.emit('error', new Error('Dummy stream read error'));
}, 50);

new Ffmpeg()
.addInput(faultyStream)
.ffprobe(function(err, data) {
assert.ok(!!err);
err.message.should.equal('Dummy stream read error');
done();
});
});
});

0 comments on commit 39869d6

Please sign in to comment.