Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mux-demux

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mux-demux - npm Package Compare versions

Comparing version 0.0.0 to 1.0.0

test/api.js

15

examples/tcp.js

@@ -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 @@

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc