Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynos committed Aug 4, 2012
1 parent c5594c2 commit dcf0380
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
*.log
*.err
19 changes: 19 additions & 0 deletions LICENCE
@@ -0,0 +1,19 @@
Copyright (c) 2012 Raynos.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
@@ -0,0 +1,48 @@
# boot

An automatic reconnect mux-demux-shoe

The mdm connection you get from shoe will magically handle reconnection
logic for you under the hood. You just play with streams

## Client Example

var shoe = require('../browser.js')
, mdm = shoe("/shoe")

var one = window.one = mdm.createStream("one")

one.on("data", function (data) {
console.log("message", data)
})

one.write("hello world")

## Server Example

var shoe = require("..")
, through = require("through")
, echoStream = through()

var sock = shoe(function (stream) {
// stream from MuxDemux with the meta property set
if (stream.meta === "one") {
stream.on("data", console.log.bind(console))
stream.pipe(echoStream).pipe(stream)
}
})

sock.install(server, "/shoe")

## Installation

`npm install boot`

## Contributors

- Raynos

## MIT Licenced

[1]: https://secure.travis-ci.org/Raynos/boot.png
[2]: http://travis-ci.org/Raynos/boot
83 changes: 83 additions & 0 deletions browser.js
@@ -0,0 +1,83 @@
var shoe = require("mux-demux-shoe")
, through = require("through")
, PauseStream = require("pause-stream")
, es = require("event-stream")

module.exports = shoeProxy

function shoeProxy(uri) {
var proxyWrite = PauseStream()
, proxyRead = through()
, proxy = es.duplex(proxyWrite, proxyRead)
, metaStreams = []
, stream

proxy.createStream = createStream
proxy.createWriteStream = createWriteStream
proxy.createReadStream = createReadStream

createShoeStream()

return proxy

function createShoeStream() {
stream = shoe(uri)

metaStreams.forEach(function (details) {
var proxyWrite = details.proxyWrite
, proxyRead = details.proxyRead

var mdm = stream.createStream(details.meta, details.opts)

proxyWrite.pipe(mdm).pipe(proxyRead)
})

stream.on("connect", function () {
proxyWrite.resume()
proxy.emit("connect")
})

proxyWrite.pipe(stream).pipe(proxyRead)

stream.on("end", function () {
console.log("stream ended")
proxyWrite.pause()
proxy.emit("disconnect")
createShoeStream()
})
}

function createStream(meta, opts) {
var proxyWrite = PauseStream()
, proxyRead = through()
, proxy = es.duplex(proxyWrite, proxyRead)

var mdm = stream.createStream(meta, opts)

proxyWrite.pipe(mdm).pipe(proxyRead)

metaStreams.push({
proxy: proxy
, proxyRead: proxyRead
, proxyWrite: proxyWrite
, meta: meta
, opts: opts
})

return proxy
}

function createWriteStream(meta) {
createStream(meta, {
writable: true
, readable: false
})
}

function createReadStream(meta) {
createStream(meta, {
writable: false
, readable: true
})
}
}
10 changes: 10 additions & 0 deletions example/client.js
@@ -0,0 +1,10 @@
var shoe = require('../browser.js')
, mdm = shoe("/shoe")

var one = window.one = mdm.createStream("one")

one.on("data", function (data) {
console.log("message", data)
})

one.write("hello world")
28 changes: 28 additions & 0 deletions example/server.js
@@ -0,0 +1,28 @@
var http = require('http')
, path = require("path")
, through = require("through")
, browserify = require("browserify")

var server = http.createServer(function (req, res) {
if (req.url === "/bundle.js") {
var b = browserify()
b.addEntry(path.join(__dirname, "./client.js"))
res.end(b.bundle())
} else {
res.end("<script src='bundle.js'></script>")
}
})
server.listen(8081)

var shoe = require("..")
, echoStream = through()

var sock = shoe(function (stream) {
// stream from MuxDemux with the meta property set
if (stream.meta === "one") {
stream.on("data", console.log.bind(console))
stream.pipe(echoStream).pipe(stream)
}
})

sock.install(server, "/shoe")
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require("mux-demux-shoe")

0 comments on commit dcf0380

Please sign in to comment.