Comparing version 1.3.7 to 1.4.1
112
index.js
@@ -6,4 +6,7 @@ 'use strict'; // always strict mode | ||
const uuid = require ('uuid'); | ||
const ObjectPath = require ('object-path'); | ||
const { TrannyServer, TrannyClient, genKeyPair: genSocketKeyPair } = require ('cryptotranny'); | ||
const objectPath = ObjectPath.create ({ includeInheritedProps: true }); | ||
// Create RPC events class | ||
@@ -43,2 +46,4 @@ class RpcEvents extends EventEmitter { | ||
this._lastData = {}; | ||
// Set own ID for identification | ||
@@ -48,3 +53,3 @@ this.id = uuid (); | ||
// Set mock for accessing methods | ||
this.class = this._genClass (); | ||
this.class = this._genClass ({}, []); | ||
@@ -54,11 +59,13 @@ // Set child for handling plain events | ||
// Set private variables | ||
this._stream = stream; | ||
this._child = child; | ||
// Give child class instance a copy of this/RPC | ||
if (child != null && child._handleRpc != null) child._handleRpc (this); | ||
// Set private variables | ||
this._stream = stream; | ||
this._child = child; | ||
this._listen (); | ||
} | ||
_genClass () { | ||
_genClass (base, path) { | ||
// Create handler for proxy class | ||
@@ -68,16 +75,20 @@ let proxyHandler = { | ||
get: (target, property) => { | ||
// Return function which takes all trailing params | ||
return (async (...params) => { | ||
// Issue remote call with property name and recieved params | ||
let result = await this._call (property, params); | ||
if (base[property] != null) { | ||
return base[property]; | ||
} | ||
// Check if response is error | ||
if (result.isError) { | ||
// Throw if error to emulate native function | ||
throw result.data; | ||
let propPath = path.slice (0); | ||
propPath.push (property); | ||
let handler = (async (...params) => { | ||
let out = await this._call (propPath, params); | ||
if (out.isError) { | ||
throw out.data; | ||
} else { | ||
// Non error, simply return data | ||
return result.data; | ||
return out.data; | ||
} | ||
}); | ||
return this._genClass (handler, propPath) | ||
}, | ||
@@ -87,3 +98,3 @@ }; | ||
// Create a new `Proxy` to act as our class | ||
let proxy = new Proxy ({}, proxyHandler); | ||
let proxy = new Proxy (base, proxyHandler); | ||
@@ -94,7 +105,48 @@ // Return the proxy | ||
// Listen for RPC calls | ||
listen () { | ||
_listen () { | ||
this._stream.on ('dataUpdate', async (data) => { | ||
this._lastData = data; | ||
this.class = this._genClass (data, []); | ||
}); | ||
this._stream.on ('propUpdate', async (data) => { | ||
objectPath.set (this._lastData, data.path, data.value); | ||
this.class = this._genClass (this._lastData, []); | ||
}); | ||
} | ||
allow () { | ||
// Don't bother if no child | ||
if (this._child == null) return; | ||
this._stream.send ('dataUpdate', this._child); | ||
let traverse = (part, path) => { | ||
Object.keys (part).forEach((key) => { | ||
if (part[key] !== null && part[key] instanceof Object) { | ||
let partPath = path.slice (0); | ||
partPath.push (key); | ||
part[key] = new Proxy (part[key], { | ||
set: (target, prop, value, reciever) => { | ||
let propPath = partPath.slice (0); | ||
propPath.push (prop); | ||
this._stream.send ('propUpdate', { | ||
path: propPath, | ||
value: value, | ||
}); | ||
target[prop] = value; | ||
return true; | ||
}, | ||
}); | ||
traverse(part[key], partPath); | ||
} | ||
}); | ||
}; | ||
traverse (this._child, []); | ||
// Listen for function calls | ||
@@ -106,7 +158,7 @@ this._stream.on ('fnCall', async (call) => { | ||
// Find internal handler | ||
let handler = this._child[call.fnId]; | ||
let handler = objectPath.get (this._child, call.path); | ||
// Handle inexistence | ||
if (handler == null) { | ||
this._stream.send ('fnRes.' + call.resId, { | ||
this._stream.send (call.resId, { | ||
isError : true, | ||
@@ -122,3 +174,3 @@ data : 'No such method', | ||
return (...fnParams) => { | ||
this._stream.send ('cbRes.' + param.funcId, { | ||
this._stream.send (param.funcId, { | ||
params: fnParams, | ||
@@ -129,3 +181,3 @@ }); | ||
return param.data; | ||
} | ||
}; | ||
}) | ||
@@ -139,3 +191,3 @@ | ||
// Emit error as response | ||
this._stream.send ('fnRes.' + call.resId, { | ||
this._stream.send (call.resId, { | ||
isError: true, | ||
@@ -149,3 +201,3 @@ data: err, | ||
// Emit success and function return result | ||
this._stream.send ('fnRes.' + call.resId, { | ||
this._stream.send (call.resId, { | ||
isError: false, | ||
@@ -157,3 +209,3 @@ data: response, | ||
async _call (fnId, params) { | ||
async _call (path, params) { | ||
// Generate ID to listen for responses on | ||
@@ -164,5 +216,5 @@ let resId = uuid (); | ||
if (typeof param == "function") { | ||
let funcId = uuid (); | ||
let cbResId = uuid (); | ||
this._stream.on ('cbRes.' + funcId, (res) => { | ||
this._stream.on (cbResId, (res) => { | ||
param (...res.params); | ||
@@ -173,3 +225,3 @@ }); | ||
isFunc: true, | ||
funcId: funcId, | ||
resId: cbResId, | ||
} | ||
@@ -186,3 +238,3 @@ } else { | ||
this._stream.send ('fnCall', { | ||
fnId: fnId, | ||
path: path, | ||
resId: resId, | ||
@@ -195,3 +247,3 @@ params: parsedParams, | ||
// Listen for a response on generated response ID | ||
this._stream.once ('fnRes.' + resId, (response) => { | ||
this._stream.once (resId, (response) => { | ||
// Resolve promise with recieved data | ||
@@ -198,0 +250,0 @@ resolve (response); |
{ | ||
"name": "arc-rpc", | ||
"version": "1.3.7", | ||
"version": "1.4.1", | ||
"description": "Asynchronous Remote Classes make RPC simple", | ||
@@ -8,2 +8,3 @@ "main": "index.js", | ||
"cryptotranny": "^1.0.1", | ||
"object-path": "^0.11.4", | ||
"tweetnacl": "^1.0.0", | ||
@@ -10,0 +11,0 @@ "uuid": "^3.0.1" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12306
308
4
+ Addedobject-path@^0.11.4
+ Addedobject-path@0.11.8(transitive)