Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial

  • Loading branch information...
commit a68488d61b0528d2c03ca425923cd88fd5831284 0 parents
@dominictarr authored
3  .gitignore
@@ -0,0 +1,3 @@
+node_modules
+node_modules/*
+npm_debug.log
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 'Dominic Tarr'
+
+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.
43 README.md
@@ -0,0 +1,43 @@
+# stream-rpc
+
+[rpc-stream](https://github.com/dominictarr/rpc-stream) but with streams
+
+[![server-tests](https://travis-ci.org/dominictarr/stream-rpc)
+](https://secure.travis-ci.org/dominictarr/stream-rpc.png?branch=master)
+
+[![browser-support](https://ci.testling.com/dominictarr/stream-rpc.png)
+](https://ci.testling.com/dominictarr/stream-rpc)
+
+``` js
+//server
+var RPC = require('rpc-with-streams')
+var net = require('net')
+var fs = require('fs')
+
+var rpc = RPC(fs)
+net.createStream(function (stream) {
+ stream.pipe(rpc).pipe(stream)
+}).listen(PORT)
+```
+
+``` js
+//client
+var RPC = require('rpc-with-streams')
+var net = require('net')
+var rpc = RPC(fs)
+var stream = net.connect(PORT)
+
+stream.pipe(rpc).pipe(stream)
+//create a partial wrapper of the fs module.
+var fs = rpc.wrap(
+ ['readFile', 'writeFile', 'unlinkFile', 'stat'],
+ ['createReadStream', 'createWriteStream']
+)
+```
+
+Note, stuff that depends on properties that get set on streams
+(like the `.path` property on fs streams will not work)
+
+## License
+
+MIT
47 index.js
@@ -0,0 +1,47 @@
+var MuxDemux = require('mux-demux')
+var RPC = require('rpc-stream')
+
+module.exports = function (api, client) {
+ var rpc = RPC(api)
+ var mx = MuxDemux()
+ //connect the api stream from the client side...
+ //there is a better way... but i'll do that later.
+ //this hack will work for now.
+ if(client || 'node' !== process.title) {
+ mx.on('pipe', function () {
+ rpc.pipe(mx.createStream('RPC')).pipe(rpc)
+ })
+ }
+
+ mx.on('connection', function (stream) {
+ if(stream.meta === 'RPC') {
+ return rpc.pipe(stream).pipe(rpc)
+ }
+ if(Array.isArray(stream.meta)) {
+ var args = stream.meta.slice()
+ var name = args.shift()
+ var s = api[name].apply(api, args)
+ if(s.writable) stream.pipe(s)
+ if(s.readable) s.pipe(stream)
+ }
+ })
+
+ function streamCreator (name) {
+ return function () {
+ var args = [].slice.call(arguments)
+ args.unshift(name)
+ return mx.createStream(args)
+ }
+ }
+
+ mx.wrap = function (simple, streams) {
+ var wrapped = rpc.wrap(simple)
+ streams.forEach(function (name) {
+ console.log('wrap', name)
+ wrapped[name] = streamCreator(name)
+ })
+ return wrapped
+ }
+
+ return mx
+}
21 package.json
@@ -0,0 +1,21 @@
+{
+ "name": "rpc-with-streams",
+ "version": "0.0.0",
+ "homepage": "https://github.com/dominictarr/rpc-with-streams",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/dominictarr/rpc-with-streams.git"
+ },
+ "dependencies": {
+ "mux-demux": "~3.4.0",
+ "from": "~0.1.1",
+ "tape": "~0.1.1",
+ "rpc-stream": "~0.1.3"
+ },
+ "devDependencies": {},
+ "scripts": {
+ "test": "set -e; for t in test/*.js; do node $t; done"
+ },
+ "author": "'Dominic Tarr' <dominic.tarr@gmail.com> (http://dominictarr.com)",
+ "license": "MIT"
+}
39 test/index.js
@@ -0,0 +1,39 @@
+var streamer = require('..')
+var from = require('from')
+
+require('tape')('test', function (t) {
+
+ var server = streamer({
+ f: function () {
+ return from(function (i) {
+ if(i < 10) this.emit('data', i)
+ else this.emit('end')
+ return true
+ })
+ },
+ hello: function (name, cb) {
+ process.nextTick(function () {
+ cb(null, 'hello '+name)
+
+ })
+ }
+ })
+
+ var client = streamer(null, true)
+
+ client.pipe(server).pipe(client)
+
+ var w = client.wrap(['hello'], ['f'])
+
+ var i = 0
+ w.f().on('data', function (d) {
+ i++
+ }).on('end', function () {
+ t.equal(i, 10)
+ })
+ w.hello('jim', function (err, message) {
+ t.equal(message, 'hello jim')
+ t.end()
+ })
+
+})
Please sign in to comment.
Something went wrong with that request. Please try again.