Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created August 3, 2012 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/3252314 to your computer and use it in GitHub Desktop.
Save Raynos/3252314 to your computer and use it in GitHub Desktop.
var shoe = require("./mux-demux-shoe")
, through = require("through")
, multiStreamRegExp = /(multi-stream-)([\w\W]*)/
, streams = {}
module.exports = createShoeConnection
function createShoeConnection(callback) {
var sock = shoe(proxyConnections)
return sock
function proxyConnections(stream) {
var meta = stream.meta
if (typeof meta !== "string") {
return callback(stream)
}
var regExpResult = meta.match(multiStreamRegExp)
if (regExpResult === null) {
return callback(stream)
}
var name = regExpResult[2]
, communicationStream = streams[name]
if (!communicationStream) {
communicationStream = streams[name] = dataOnlyStream()
}
stream.pipe(communicationStream).pipe(stream)
}
}
function dataOnlyStream() {
return through(write, noop)
}
function write(data) {
this.emit("data", data)
}
function noop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment