Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix (really unlikely) 'count' bug #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ function get(obj, path) {
return obj[path]
}

module.exports = function (obj, raw) {
module.exports = function (obj, opts) {
//backward compatibility
if (!(opts instanceof Object)) opts = { isRaw: opts }
var cbs = {}, count = 1, local = obj || {}
function flattenError(err) {
if(!(err instanceof Error)) return err
var err2 = { message: err.message }
for(var k in err)
err2[k] = err[k]
err2[k] = err[k]
return err2
}
function expandError(err) {
Expand All @@ -27,21 +29,24 @@ module.exports = function (obj, raw) {
return err2
}
var s = through(function (data) {
//write - on incoming call
//write - on incoming call
data = data.slice()
var i = data.pop(), args = data.pop(), name = data.pop()
//if(~i) then there was no callback.

if (args[0]) args[0] = expandError(args[0])
//if(~i) then there was no callback.

if(name != null) {
var cb = function () {
if(!~i) return;
var args = [].slice.call(arguments)
args[0] = flattenError(args[0])
if(~i) s.emit('data', [args, i]) //responses don't have a name.
s.emit('data', [args, i]) //responses don't have a name.
}
try {
local[name].call(obj, args, cb)
if (opts.sameSigs && args instanceof Array) {
//use apply to use same signatures
args.push(cb);
local[name].apply(obj, args);
} else local[name].call(obj, args, cb);
} catch (err) {
if(~i) s.emit('data', [[flattenError(err)], i])
}
Expand All @@ -54,6 +59,7 @@ module.exports = function (obj, raw) {

return console.error('ERROR: unknown callback id: '+i, data)
} else {
if (args[0]) args[0] = expandError(args[0])
//call the callback.
cbs[i].apply(null, args)
delete cbs[i] //no longer need this
Expand All @@ -63,8 +69,8 @@ module.exports = function (obj, raw) {
var rpc = s.rpc = function (name, args, cb) {
if(cb) cbs[++count] = cb
if(count == 9007199254740992) count = 0 //reset if max
//that is 900 million million.
//if you reach that, dm me,
//that is 900 million million.
//if you reach that, dm me,
//i'll buy you a beer. @dominictarr
if('string' !== typeof name)
throw new Error('name *must* be string')
Expand Down Expand Up @@ -102,7 +108,7 @@ module.exports = function (obj, raw) {
})
return w
}
if(raw)
if(opts.isRaw)
return s

return serialize(s)
Expand Down
1 change: 1 addition & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#rpc-stream

`RpcStream` is a dead simple (75 loc) rpc system that works over any full-duplex text/byte/json stream.
There's also a [Python port](https://github.com/riga/rpyc-stream), e.g. for cross-language RPC-over-SSH.

## rant

Expand Down