byproxy-link

Simple solution to proxy objects on the server to the client


Keywords
proxy, rest, express, javascript-proxy, rpc
License
MIT
Install
npm install byproxy-link@0.1.0

Documentation

ByProxy

ByProxy allows the client to access an object or module on the server as if it were a local object. The object's API can then be invoked directly on the client.

For example, a calculator server:

On the server:

class Calculator {
  constructor() {
    this.opCount = 0;
  }
  add(a, b) {
    this.opCount++;
    return a + b;
  }
  subtract(a, b) {
    this.opCount++;
    return a - b;
  }
}
// Serve an instance of Calculator via Express
byproxy.serve(app, '/calculator', new Calculator()); 

On the client:

const calculator = byproxy.link('/calculator');

const sum = await calculator.add(2, 6);       // 8
const diff = await calculator.subtract(6, 2); // 4
console.log(await calculator.opCount);        // 2

Advantages

  • No need to configure different rest methods for each function call. It is handled automatically.
  • When you want to add a new feature, just add a new method. Since the client is interacting with a proxy of the object on the server, no other setup is needed.
  • If using types, both the server and client will have the same interfaces - avoids bugs and duplication of definitions.

Other features

Seamlessly deal with async functions

Server:

byproxy.serve(app, '/updater', {
  async delayedUpdate() {
    return new Promise(........);
  }
}); 

Client:

const result = await updater.delayedUpdate();

Proxy a whole module

const mymod = require('my-awesome-mod');
byproxy.serve(app, '/updater', mymod);

Setup

ByProxy is composed of two modules byproxy-serve for the server and byproxy-link for the client. They are both available on NPM.

npm install --save byproxy-serve
npm install --save byproxy-link

Note: ByProxy is not a rest library. It integrates with Express on the server. There is no dependency required on the client.

Examples

ByProxy demo project is a simple example.

License

MIT License (c) Preet Shihn