Comparing version 0.0.0 to 1.0.0
@@ -7,4 +7,2 @@ var MuxDemux = require('..') | ||
var ds = mdm1.createWriteStream('times') | ||
mdm2.on('connection', function (stream) { | ||
@@ -16,11 +14,12 @@ stream.on('data', function (date) { | ||
setInterval(function () { | ||
ds.write(new Date().toString()) | ||
}, 1e3) | ||
net.createServer(function (con) { | ||
con.pipe(mdm2.getMuxDemuxStream()).pipe(con) | ||
con.pipe(mdm2).pipe(con) | ||
}).listen(8642, function () { | ||
var con = net.connect(8642) | ||
con.pipe(mdm1.getMuxDemuxStream()).pipe(con) | ||
con.pipe(mdm1).pipe(con) | ||
var ds = mdm1.createWriteStream('times') | ||
setInterval(function () { | ||
ds.write(new Date().toString()) | ||
}, 1e3) | ||
}) | ||
@@ -27,0 +26,0 @@ |
117
index.js
var es = require('event-stream') | ||
var RemoteEventEmitter = require('remote-events') | ||
var _bs = require('browser-stream') | ||
function MuxDemux () { | ||
var ree = new RemoteEventEmitter() | ||
var bs = _bs(ree) | ||
bs.getMuxDemuxStream = function () { | ||
return es.connect( | ||
es.split(), | ||
es.parse(), | ||
ree.getStream(), | ||
es.stringify() | ||
function MuxDemux (name) { | ||
function createID() { | ||
return ( | ||
Math.random().toString(16).slice(2) + | ||
Math.random().toString(16).slice(2) | ||
) | ||
} | ||
return bs | ||
var streams = {}, streamCount = 0 | ||
var md = es.through(function (data) { | ||
var id = data.shift() | ||
var event = data[0] | ||
var s = streams[id] | ||
if(!s) { | ||
if(event != 'new') | ||
return md.emit('error', new Error('does not have stream:' + id)) | ||
md.emit('connection', createStream(id, data[1].meta, data[1].opts)) | ||
} | ||
else if (event === 'pause') | ||
s.paused = true | ||
else if (event === 'resume') { | ||
var p = s.paused | ||
s.paused = false | ||
if(p) s.emit('drain') | ||
} | ||
else { | ||
s.emit.apply(s, data) | ||
} | ||
}) | ||
function destroyAll (_err) { | ||
md.removeListener('end', destroyAll) | ||
md.removeListener('error', destroyAll) | ||
md.removeListener('close', destroyAll) | ||
var err = _err || new Error ('unexpected disconnection') | ||
for (var i in streams) { | ||
var s = streams[i] | ||
s.emit('error', err) | ||
s.destroy() | ||
} | ||
} | ||
//the problem here, is that this is registering the first | ||
//event listener. | ||
//and so in this test, the close message is | ||
//getting to the other side first | ||
md.pause = function () {} | ||
md.resume = function () {} | ||
function createStream(id, meta, opts) { | ||
var s = es.through(function (data) { | ||
if(!this.writable) | ||
throw new Error('stream is not writable') | ||
md.emit('data', [s.id, 'data', data]) | ||
}, function () { | ||
md.emit('data', [s.id, 'end']) | ||
}) | ||
s.pause = function () { | ||
md.emit('data', [s.id, 'pause']) | ||
} | ||
s.resume = function () { | ||
md.emit('data', [s.id, 'resume']) | ||
} | ||
s.once('close', function () { | ||
md.emit('data', [s.id, 'close']) | ||
delete streams[id] | ||
}) | ||
s.writable = opts.writable | ||
s.readable = opts.readable | ||
streams[s.id = id] = s | ||
s.meta = meta | ||
return s | ||
} | ||
var outer = es.connect(es.split(), es.parse(), md, es.stringify()) | ||
if(md !== outer) | ||
md.on('connection', function (stream) { | ||
outer.emit('connection', stream) | ||
}) | ||
var pipe = outer.pipe | ||
outer.pipe = function (dest, opts) { | ||
pipe.call(outer, dest, opts) | ||
md.on('end', destroyAll) | ||
md.on('close', destroyAll) | ||
md.on('error', destroyAll) | ||
return dest | ||
} | ||
outer.createStream = function (meta, opts) { | ||
opts = opts || {writable: true, readable: true} | ||
var s = createStream(createID(), meta, opts) | ||
var _opts = {writable: opts.readable, readable: opts.writable} | ||
md.emit('data', [s.id, 'new', {meta: meta, opts: _opts}]) | ||
return s | ||
} | ||
outer.createWriteStream = function (meta) { | ||
return outer.createStream(meta, {writable: true, readable: false}) | ||
} | ||
outer.createReadStream = function (meta) { | ||
return outer.createStream(meta, {writable: false, readable: true}) | ||
} | ||
return outer | ||
} | ||
module.exports = MuxDemux |
{ | ||
"name": "mux-demux", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "", | ||
@@ -8,11 +8,18 @@ "homepage": "http://github.com/dominictarr/mux-demux", | ||
"type": "git", | ||
"url": "https://github.com/dominictarr/mux-demux.git" | ||
"url": "git://github.com/dominictarr/mux-demux.git" | ||
}, | ||
"dependencies": { | ||
"remote-events": "1", | ||
"browser-stream": "~0.1.10", | ||
"event-stream": "~2.0.4" | ||
"event-stream": "~2.1.15" | ||
}, | ||
"devDependencies": {}, | ||
"author": "Dominic Tarr <dominic.tarr@gmail.com> (dominictarr.com)" | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
}, | ||
"devDependencies": { | ||
"assertions": "2.3" | ||
}, | ||
"author": "Dominic Tarr <dominic.tarr@gmail.com> (dominictarr.com)", | ||
"optionalDependencies": {}, | ||
"engines": { | ||
"node": "*" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
16701
1
10
525
0
75
1
1
2
- Removedbrowser-stream@~0.1.10
- Removedremote-events@1
- Removedbrowser-stream@0.1.10(transitive)
- Removedevent-stream@2.0.10(transitive)
- Removedoptimist@0.2.8(transitive)
- Removedremote-events@1.1.4(transitive)
- Removedstream-serializer@0.0.3(transitive)
- Removedthrough@1.1.2(transitive)
- Removedwordwrap@0.0.3(transitive)
Updatedevent-stream@~2.1.15