Skip to content
Browse files

remove json stream, let people pick their own serialization

  • Loading branch information...
1 parent b6bdb1f commit 99da7d1b8f498a0401defb4d242935f8d82f07e6 @substack committed Aug 27, 2012
Showing with 17 additions and 15 deletions.
  1. +12 −13 index.js
  2. +5 −2 test/emit.js
View
25 index.js
@@ -1,5 +1,5 @@
var EventEmitter = require('events').EventEmitter;
-var JSONStream = require('JSONStream');
+var through = require('through');
exports = module.exports = function (ev) {
if (typeof ev.pipe === 'function') {
@@ -9,7 +9,15 @@ exports = module.exports = function (ev) {
};
exports.toStream = function (ev) {
- var s = JSONStream.stringify();
+ var s = through(
+ function write (args) {
+ this.emit('data', args);
+ },
+ function end () {
+ var ix = ev._emitStreams.indexOf(s);
+ ev._emitStreams.splice(ix, 1);
+ }
+ );
if (!ev._emitStreams) {
ev._emitStreams = [];
@@ -27,24 +35,15 @@ exports.toStream = function (ev) {
}
ev._emitStreams.push(s);
- var end = s.end;
- s.end = function () {
- var ix = ev._emitStreams.indexOf(s);
- ev._emitStreams.splice(ix, 1);
- end.apply(s, arguments);
- };
-
return s;
};
exports.fromStream = function (s) {
var ev = new EventEmitter;
- var p = JSONStream.parse([ true ]);
- p.on('data', function (args) {
+ s.pipe(through(function (args) {
ev.emit.apply(ev, args);
- });
- s.pipe(p);
+ }));
return ev;
};
View
7 test/emit.js
@@ -3,6 +3,7 @@ var test = require('tap').test;
var emitStream = require('../');
var EventEmitter = require('events').EventEmitter;
var net = require('net');
+var JSONStream = require('JSONStream');
test('emit', function (t) {
t.plan(1);
@@ -12,7 +13,9 @@ test('emit', function (t) {
var server = net.createServer(function (stream) {
if (!ev) ev = createEmitter();
- emitStream(ev).pipe(stream);
+ var s = JSONStream.stringify();
+ s.pipe(stream);
+ emitStream(ev).pipe(s);
});
server.on('close', function () { ev.stop() });
return server;
@@ -23,7 +26,7 @@ test('emit', function (t) {
server.on('listening', function () {
var stream = net.connect(5555);
- var ev = emitStream(stream);
+ var ev = emitStream(stream.pipe(JSONStream.parse([true])));
ev.on('ping', function (t) {
collected.push('ping');

0 comments on commit 99da7d1

Please sign in to comment.
Something went wrong with that request. Please try again.