Comparing version 0.0.2 to 0.1.0
@@ -5,3 +5,3 @@ { | ||
"description": "Forward one (emitter|stream)'s events to another", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"keywords": [], | ||
@@ -8,0 +8,0 @@ "dependencies": { |
30
index.js
@@ -5,15 +5,23 @@ var Stream = require('stream'); | ||
var emit = src.emit; | ||
if (src.pipe && dest.pipe) { | ||
var transformer = new Stream(); | ||
transformer.readable = true; | ||
transformer.writable = true; | ||
transformer.write = function(data) { | ||
data = fwd.applyRules(rules, ['data'].concat(data)); | ||
if (!data) return; | ||
this.emit.apply(this, data); | ||
}; | ||
transformer.end = function() { this.emit('end') }; | ||
transformer.destroy = function() { this.emit('close') }; | ||
return src.pipe(transformer).pipe(dest); | ||
} | ||
src.emit = function() { | ||
var args = [].slice.call(arguments); | ||
if (!(args = fwd.applyRules(rules, args))) return; | ||
if (src.pipe && dest.pipe) { | ||
var transmitter = new Stream(); | ||
transmitter.readable = true; | ||
transmitter.pipe(dest); | ||
transmitter.emit('data', args[1]); | ||
} else if (dest.pipe) { | ||
dest.write(args[1]); | ||
} else { | ||
dest.emit.apply(dest, args); | ||
} | ||
args = fwd.applyRules(rules, args); | ||
if ((src.pipe || dest.pipe) && args[0] == 'end') src.emit = emit; | ||
if (dest.pipe && args[0] == 'end') return dest.end.apply(args.splice(1)); | ||
if (!args) return; | ||
if (dest.pipe) return dest.write(args[1]); | ||
dest.emit.apply(dest, args); | ||
emit.apply(src, arguments); | ||
@@ -20,0 +28,0 @@ } |
{ | ||
"name": "fwd", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Forward one (emitter|stream)'s events to another", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,2 +20,3 @@ | ||
var EventEmitter = require('events').EventEmitter; | ||
// or: var EventEmitter = require('emitter') | ||
@@ -123,2 +124,10 @@ var src = new EventEmitter(); | ||
## Tests | ||
```bash | ||
$ git clone https://github.com/juliangruber/fwd.git && cd fwd | ||
$ npm install | ||
$ mocha | ||
``` | ||
## License | ||
@@ -125,0 +134,0 @@ |
113
test/test.js
@@ -131,6 +131,9 @@ var fwd = require('../index'); | ||
describe('fwd(s, ee)', function() { | ||
var src, dest; | ||
beforeEach(function() { | ||
src = new Stream(); | ||
src.readable = true; | ||
dest = new Emitter(); | ||
}) | ||
it('should forward all data', function(done) { | ||
var src = new Stream(); | ||
src.readable = true; | ||
var dest = new Emitter(); | ||
fwd(src, dest, {'data': 'event'}); | ||
@@ -143,2 +146,10 @@ dest.on('event', function(data) { | ||
}); | ||
it('should end', function() { | ||
fwd(src, dest); | ||
src.emit('end'); | ||
dest.on('data', function() { | ||
throw new Error("didn't end"); | ||
}); | ||
src.emit('data', 'whoo'); | ||
}); | ||
}); | ||
@@ -160,8 +171,10 @@ describe('fwd(ee, s)', function() { | ||
describe('fwd(s, s)', function() { | ||
it('should forward all data', function(done) { | ||
var src = new Stream(); | ||
var src, dest; | ||
beforeEach(function() { | ||
src = new Stream(); | ||
src.readable = true; | ||
var dest = new Stream(); | ||
dest = new Stream(); | ||
dest.writable = true; | ||
}) | ||
it('should forward all data', function(done) { | ||
fwd(src, dest); | ||
@@ -174,4 +187,88 @@ | ||
src.emit('data', 'my-data'); | ||
}) | ||
}); | ||
it('should end', function(done) { | ||
fwd(src, dest); | ||
dest.end = done; | ||
src.emit('end'); | ||
}); | ||
it('should apply a function', function(done) { | ||
fwd(src, dest, function(data) { | ||
return data+'-changed'; | ||
}); | ||
dest.write = function(data) { | ||
expect(data).to.be('value-changed'); | ||
done(); | ||
} | ||
src.emit('data', 'value'); | ||
}); | ||
}); | ||
describe('acceptance', function() { | ||
var src, dest; | ||
beforeEach(function() { | ||
src = new Stream; | ||
src.readable = true | ||
var times = 0; | ||
var iv = setInterval(function () { | ||
src.emit('data', times + '\n'); | ||
if (++times === 5) { | ||
src.emit('end'); | ||
clearInterval(iv); | ||
} | ||
}, 1); | ||
dest = new Stream; | ||
dest.writable = true; | ||
dest.bytes = 0; | ||
dest.write = function (buf) { | ||
if (typeof buf == 'undefined') return; | ||
dest.bytes += buf.length; | ||
}; | ||
dest.end = function (buf) { | ||
if (arguments.length) dest.write(buf); | ||
dest.writable = false; | ||
}; | ||
dest.destroy = function () { | ||
dest.writable = false; | ||
}; | ||
}); | ||
it('should pipe', function(done) { | ||
dest.end = function(buf) { | ||
if (arguments.length) dest.write(buf); | ||
dest.writable = false; | ||
expect(dest.bytes).to.be(20); | ||
done(); | ||
}; | ||
fwd(src, dest, function(data) { | ||
return data+data; | ||
}); | ||
}); | ||
it('should fwd stream to emitter', function(done) { | ||
var dest = new Emitter(); | ||
var bytes = 0; | ||
dest.on('data', function(data) { | ||
bytes += data.length; | ||
if (bytes==10) done(); | ||
}); | ||
fwd(src, dest, ['data', {'end': function(event, data) { | ||
if (data) event = 'data'; | ||
return {event:event, data:data}; | ||
}}]); | ||
}); | ||
it('should fwd emitter to stream', function(done) { | ||
var src = new Emitter(); | ||
dest.end = function(buf) { | ||
if (arguments.length) dest.write(buf); | ||
expect(dest.bytes).to.be(2); | ||
done(); | ||
}; | ||
fwd(src, dest); | ||
src.emit('data', '13'); | ||
src.emit('end'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14078
347
140
0