Skip to content

Commit

Permalink
enh: finish callback
Browse files Browse the repository at this point in the history
  • Loading branch information
schnittstabil committed Jul 10, 2014
1 parent 9a342a6 commit 7417b44
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ gulp.task('coverage', function (done) {
reporter: 'dot'
}))
.pipe(istanbul.writeReports({
reporters: ['lcovonly', 'text-summary']
reporters: ['lcovonly', 'text-summary', 'html']
}))
.on('end', done);
});
Expand Down
50 changes: 43 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@
var Transform = require('readable-stream/transform'),
inherits = require('util').inherits;

function StreamRecorder(options) {
function noop() {}

function sanitizeArguments(/* [options], [done] */){
var options = {},
done = noop;

if (arguments.length === 1) {
if (typeof arguments[0] === 'function') {
done = arguments[0];
} else {
options = arguments[0];
}
} else if (arguments.length > 1) {
if (arguments[0]) {
options = arguments[0];
}
done = arguments[1];
}
return [options, done];
}

function StreamRecorder(/* [options], [done] */) {
if (!(this instanceof StreamRecorder)) {
return new StreamRecorder(options);
return new StreamRecorder(arguments[0], arguments[1]);
}
options = options || {};

var args = sanitizeArguments.apply(null, arguments),
options = args[0],
done = args[1];

Transform.call(this, options);
this.objectMode = options.objectMode;
Expand All @@ -16,6 +40,18 @@ function StreamRecorder(options) {
} else {
this.data = new Buffer('', options.encoding);
}

this.errors = [];

var self = this;
this.on('error', function(err){
self.errors.push(err);
});

this.on('finish', function(){
var errors = self.errors.length > 0 ? self.errors : null;
done.call(done, errors, self.data);
});
}
inherits(StreamRecorder, Transform);

Expand All @@ -32,10 +68,10 @@ StreamRecorder.prototype._transform = function(chunk, encoding, done) {
done();
};

StreamRecorder.obj = function (options) {
options = options || {};
options.objectMode = true;
return new StreamRecorder(options);
StreamRecorder.obj = function (/* [options], [done] */) {
var args = sanitizeArguments.apply(null, arguments);
args[0].objectMode = true;
return StreamRecorder.apply(null, args);
};

module.exports = StreamRecorder;
27 changes: 24 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,38 @@ describe('String streams', function() {
assert.strictEqual(sut.data.toString(), input.join(''));
});

it('should be recorded with decodeStrings:false option', function() {
var sut = new Recorder({decodeStrings: false});
it('should be recorded with decodeStrings:false option', function(done) {
var sut = new Recorder({decodeStrings: false}, function(err, data){
assert.ifError(err);
assert.strictEqual(data.toString(), input.join(''));
done();
});
input.forEach(function(i) {
sut.write(i);
}, sut);
sut.end();
});
});

describe('Erroneous streams', function() {
var input = ['foo', 'bar'];
it('done Callbac should be called', function(done) {
var sut = new Recorder.obj(function(err, data){
assert.ok(err);
assert.strictEqual(err.length, 1);
assert.strictEqual(data.join(''), input.join(''));
done();
});
input.forEach(function(i) {
sut.write(i);
}, sut);
sut.emit('error', new Error('I\'m soooo bad!'));
sut.end();
sut.resume();
assert.strictEqual(sut.data.toString(), input.join(''));
});
});


describe('String object streams', function() {
var input = ['foo', 'bar'];
it('should be recorded', function() {
Expand Down

0 comments on commit 7417b44

Please sign in to comment.