npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

murpc

v0.1.7

Published

RPC protocol for mudb

Downloads

17

Readme

murpc

Asynchronous RPC protocols for mudb

example

See src/example for a contrived demo which will be explained below. To run the demo

  1. npm i mudo
  2. cd into src/example
  3. tsc ./*.ts
  4. mudo --socket websocket

A mudb instance can have multiple RPC protocols for different sets of behaviors. Naturally each RPC protocol consistes of two sides, one for server and one for client, plus a schema to describe the RPC interfaces (the argument and return).

1 RPC protocol schema

So the first step to define an RPC protocol is to specify a protocol schema using muschema.

Like the protocol to be created, the schema always consists of two sides, the server side and the client side. Each side in turn contains schemas for the RPC interfaces that they implement repectively, under the name of the corresponding function. RPC schemas are created using the MuRPC function, which takes the schemas of the argument and return.

const IntegerSetSchema = new MuArray(new MuInt8());
const TotalSchema = new MuInt32();

const SecretSchema = new MuString();
const DigestSchema = new MuFixedASCII(128);

export const RPCSchema = {
    client: {
        // schema for a function that sums up all integers in a set
        // which takes an array of int8 and returns an int32
        sum: MuRPC(IntegerSetSchema, TotalSchema),
    },
    server: {
        // schema for a function that hashes a secret string
        // which takes a string and always returns an ASCII of length 128
        hash: MuRPC(SecretSchema, DigestSchema),
    },
};

2 server-side protocol

You can define the server side of the RPC protocol by creating and then configuring an instance of MuRPCServer.

export = function (server:MuServer) {
    const protocol = new MuRPCServer(server, RPCSchema);

    // content of the server side of the protocol
    protocol.configure({
        rpc: {
            // a hash function to be called by clients
            hash: (secret, next) => {
                // hash `secret`
                const digest = createHash('sha512').update(secret).digest('hex');
                // pass the digest back to client
                next(undefined, digest);
            },
        },

        // set up server to do something when a client connects
        connect: (client) => {
            const set = [1, 2, 3, 4];
            // call `sum()` on the client to get the total of all numbers in `set`
            client.rpc.sum(set, (total) => {
                console.log(`sum of ${set}: ${total}`);
            });
        },
    });

    server.start();
};

3 client-side protocol

The last missing piece is the client side of the RPC protocol. Similarly, you can define it through an instance of MuRPCClient.

export = function (client:MuClient) {
    const protocol = new MuRPCClient(client, RPCSchema);

    // content of the client side of the protocol
    protocol.configure({
        rpc: {
            // a function to be calle by server
            sum: (set, next) => {
                // calculate the total of all numbers in `set`
                const total = set.reduce((acc, num) => acc + num);
                // pass result back to server
                next(undefined, total);
            },
        },

        // set up client to do something when it is ready to handle messages
        ready: () => {
            // generate a secret string
            const secret = Math.random().toString(36).substr(2, 11);
            // call `hash()` on server to get the digest of `secret`
            protocol.server.rpc.hash(secret, (digest) => {
                console.log('secret digest:', digest);
            });
        },
    });

    client.start();
};

table of contents

install

npm i murpc

api

1 types

Purely instructive types used to describle the API:

  • TableOf<T>: { [name:string]:T } | {}
  • RPCSchema: [ AnyMuSchema, AnyMuSchema ]
  • RPCProtocolSchema: { server:TableOf<RPCSchema>, client:TableOf<RPCSchema> }
  • NextFn: (errorMessage:string|undefined, returnValue?) => undefined
  • ServerRPCHandler: (rpcArgument, next:NextFn, client?:MuRemoteRPCClient) => undefined
  • ClientRPCHandler: (rpcArgument, next:NextFn) => undefined
  • CallbackFn: (returnValue) => undefined
  • RPCCaller: (rpcArgument, callback:CallbackFn) => undefined

2 MuRPC(argumentSchema, returnSchema)

Exported from murpc/rpc, used when creating the RPC protocol schema, to define RPC interfaces in terms of the argument and the return:

  • argumentSchema:AnyMuSchema the data type of the argument
  • returnSchema:AnyMuSchema the data type of the return value

3 MuRPCServer(server, schema)

Used to define the server side of an RPC protocol. Exported from murpc/server, it takes these arguments:

  • server:MuServer
  • schema:RPCProtocolSchema the RPC protocol schema

3.1 clients:MuRemoteRPCClient[]

Mocks of all connected clients, used to initiate RPCs to a specific client

3.2 server:MuServer

The underlying MuServer object

3.3 schema:RPCProtocolSchema

The RPC protocol schema

3.4 configure(spec)

Registers event handlers specifed in spec

spec:{ rpc, ready?, connect?, disconnect?, close? }

  • rpc:TableOf<ServerRPCHandler> an object containing the implementations of the functions to be called by clients
  • ready() called when the server is launched
  • connect(client:MuRemoteRPCClient) called when a client connects
  • disconnect(client:MuRemoteRPCClient) called when a client disconnects
  • close() called when the server is shut down

4 MuRemoteRPCClient

A MuRemoteRPCClient is the server-side mock of a connected client

4.1 sessionId:string

A unique session id identifying the client

4.2 rpc:TableOf

A table of RPC initiators each under the name of the corresponding remote function on a client

5 MuRPCClient(client, schema)

Used to define the client side of an RPC protocol. Exported from murpc/client, it takes these arguments:

  • client:MuClient
  • schema:ProtocolSche4ma the RPC protocol schema

5.1 sessionId:string

A unique session id identifying the client

5.2 server:MuRemoteRPCServer

A mock used to initiate RPCs to the remote server

5.3 client:MuClient

The underlying MuClient object

5.4 schema:RPCProtocolSchema

The RPC protocol schema

5.5 configure(spec)

Registers event handlers specified in spec

spec:{ rpc, ready?, close? }

  • rpc:TableOf<ClientRPCHandler> an object containing the implementations of the functions to be called by the server
  • ready() called when client is ready to handle messages
  • close() called when the client is disconnected

6 MuRemoteRPCServer

A MuRemoteRPCServer is the client-side mock of the server

6.1 rpc:TableOf

A table of RPC initiators each under the name of the corresponding remote function on the server

credits

Copyright (c) 2017 Mikola Lysenko, Shenzhen Dianmao Technology Company Limited