Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

arc-rpc

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

arc-rpc - npm Package Compare versions

Comparing version 1.3.5 to 1.3.6

154

index.js
'use strict'; // always strict mode
// Import dependencies
const EventEmitter = require ('events').EventEmitter;
const uuid = require ('uuid');
const SocketIo = require ('socket.io');
const SocketIoClient = require ('socket.io-client');
const nacl = require ('tweetnacl');
const EventEmitter = require ('events').EventEmitter;
const uuid = require ('uuid');
const { TrannyServer, TrannyClient, genKeyPair: genSocketKeyPair } = require ('cryptotranny');

@@ -20,2 +18,3 @@ // Create RPC events class

// Send new event to remote
send (id, ...params) {

@@ -59,5 +58,2 @@ // Emit event to remote

this._child = child;
// Listen for events if have child class
if (child != null) this._listen ();
}

@@ -94,3 +90,7 @@

_listen () {
// Listen for RPC calls
listen () {
// Don't bother if no child
if (this._child == null) return;
// Listen for function calls

@@ -191,6 +191,11 @@ this._stream.on ('fnCall', async (call) => {

// Destroy underlying stream
destroy () {
this._stream.destroy ();
}
// Induced by implementations of `Rpc`
_disconnect () {
_onDisconnect () {
// Emit disconnect event so users of RPC will be aware
this.emit ('disconnect');
this.emit ('disconnected');
}

@@ -200,9 +205,8 @@ }

class ServerSocketInterface extends EventEmitter {
constructor (socket, cryptKey, onDisconnect) {
constructor (client, onDisconnect) {
super ();
// Set private class variables
this._socket = socket;
this._client = client;
this._onDisconnect = onDisconnect;
this._cryptKey = cryptKey;

@@ -214,14 +218,5 @@ // Listen for IPC data

_listen () {
this._socket.on ('message', (data) => {
// silently ignore bad data
if (data.nonce == null || data.encrypted == null) return;
// open nacl secret box
let decrypted = nacl.secretbox.open (Buffer.from (data.encrypted, 'base64'), Buffer.from (data.nonce, 'base64'), this._cryptKey);
// silently ignore bad crypt, TODO: handle
if (decrypted == false) return;
this._client.on ('message', (data) => {
// decode JSON message
let decoded = JSON.parse (Buffer.from (decrypted).toString ());
let decoded = JSON.parse (data.toString ());

@@ -232,4 +227,3 @@ // emit event

this._socket.on ('disconnect', () => {
// Run self ondisconnect to handle server disconnection
this._client.on ('disconnected', () => {
this._onDisconnect ();

@@ -241,18 +235,14 @@ });

// Encode object to JSON
let encoded = JSON.stringify ({
let encoded = Buffer.from (JSON.stringify ({
'id' : id,
'data' : data
});
}));
// Generate a random nonce for cryptography
let nonce = nacl.randomBytes (nacl.box.nonceLength);
// Send nonce, from secret key, and data over IPC
this._client.send (encoded);
}
// Encrypt encoded object using nonce, hardcoded server public key and our secret key
let encrypted = nacl.secretbox (Buffer.from (encoded), nonce, this._cryptKey);
// Send nonce, from secret key, and encrypted data over IPC
this._socket.emit ('message', {
'nonce' : Buffer.from (nonce).toString ('base64'),
'encrypted' : Buffer.from (encrypted).toString ('base64')
});
// Destroy underlying connection
destroy () {
this._client.destroy ();
}

@@ -262,7 +252,6 @@ }

class ServerSocketRpc extends Rpc {
constructor (socket, cryptKey, child) {
// Create event interface from provided ipc socket
let ipcInterface = new ServerSocketInterface (socket, cryptKey, () => {
// Induce self disconnect when socket interface disconnected
this._disconnect ();
constructor (client, child) {
// Create event interface from provided client
let ipcInterface = new ServerSocketInterface (client, () => {
this._onDisconnect ();
});

@@ -272,2 +261,4 @@

super (ipcInterface, child);
this.pk = client.pk;
}

@@ -277,14 +268,15 @@ }

class ServerSocketRpcMaster extends EventEmitter {
constructor(port, cryptKey, child) {
constructor(port, keyPair, child) {
super ();
let socketIo = new SocketIo ();
// Create new server using cryptotranny
let server = new TrannyServer(keyPair, port);
socketIo.on ('connection', (socket) => {
let clientRpc = new ServerSocketRpc (socket, cryptKey, child);
// Listen for new clients
server.on ('client', (client) => {
// Wrap client in RPC
let clientRpc = new ServerSocketRpc (client, child);
// Emit new client
this.emit ('client', clientRpc);
});
socketIo.listen (port);
}

@@ -294,52 +286,44 @@ }

class ClientSocketInterface extends EventEmitter {
constructor (host, port, cryptKey) {
constructor (host, port, keyPair, remotePk, onDisconnect) {
// Create parent event emitter
super ();
let socket = SocketIoClient ('http://' + host + ':' + port);
// Create new connection to specified host using cryptotranny
let server = new TrannyClient(host, port, keyPair, remotePk)
this._cryptKey = cryptKey;
this._socket = socket;
// Set private properties
this._server = server;
this._onDisconnect = onDisconnect;
// Listen
this._listen ();
}
// Listen for all events
_listen () {
this._socket.on ('message', (data) => {
// silently ignore bad data
if (data.nonce == null || data.encrypted == null) return;
this._server.on ('message', (data) => {
let decoded = JSON.parse (data.toString ());
// open nacl secret box
let decrypted = nacl.secretbox.open (Buffer.from (data.encrypted, 'base64'), Buffer.from (data.nonce, 'base64'), this._cryptKey);
// silently ignore bad crypt, TODO: handle
if (decrypted == null) return;
// decode JSON message
let decoded = JSON.parse (Buffer.from (decrypted).toString ());
// emit event
this.emit (decoded.id, decoded.data);
});
this._server.on ('disconnected', () => {
this._onDisconnect ();
});
}
// Send event to remote
send (id, data) {
// Encode object to JSON
let encoded = JSON.stringify ({
let encoded = Buffer.from (JSON.stringify ({
'id' : id,
'data' : data,
});
}));
// Generate a random nonce for cryptography
let nonce = nacl.randomBytes (nacl.box.nonceLength)
this._server.send (encoded);
}
// Encrypt encoded object using nonce, hardcoded server public key and our secret key
let encrypted = nacl.secretbox (Buffer.from (encoded), nonce, this._cryptKey)
// Send nonce, from secret key, and encrypted data over IPC
this._socket.emit ('message', {
'nonce' : Buffer.from (nonce).toString ('base64'),
'encrypted' : Buffer.from (encrypted).toString ('base64')
});
// Destroy underlying connection
destroy () {
this._server.destroy ();
}

@@ -349,5 +333,7 @@ }

class ClientSocketRpc extends Rpc {
constructor (host, port, cryptKey, client) {
constructor (host, port, keyPair, remotePk, client) {
// Create client IPC interface for provided namespace
let socketInterface = new ClientSocketInterface (host, port, cryptKey);
let socketInterface = new ClientSocketInterface (host, port, keyPair, remotePk, () => {
this._onDisconnect ();
});

@@ -360,2 +346,2 @@ // Create parent RPC module using interface as stream

// Export classes
exports = module.exports = { Rpc, ClientSocketRpc, ServerSocketRpcMaster };
exports = module.exports = { Rpc, ClientSocketRpc, ServerSocketRpcMaster, genSocketKeyPair };
{
"name": "arc-rpc",
"version": "1.3.5",
"version": "1.3.6",
"description": "Asynchronous Remote Classes make RPC simple",
"main": "index.js",
"dependencies": {
"socket.io": "^2.0.3",
"socket.io-client": "^2.0.3",
"cryptotranny": "^1.0.1",
"tweetnacl": "^1.0.0",

@@ -10,0 +9,0 @@ "uuid": "^3.0.1"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc