Created
August 3, 2012 22:43
-
-
Save Raynos/3252314 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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