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

maus

v0.4.0

Published

A Tiny RPC Framework running in NodeJS or Browser

Downloads

9

Readme

#Maus

A Tiny RPC Framework running in NodeJS or Browser

0.2.1


#Install

npm install maus --save

#QuickStart

###worker.js

var rpcWorker = require('maus').worker;
rpcWorker.create({
    add: (x, y) => x + y,
    divide: (x, y) => x / y,
    newRegExp: (reg, config) => new RegExp(reg, config),
    promiseAsync: () => {
        return new Promise((resolve, reject) => {
            setTimeout(() => resolve(new Date()), 1000)
        })
    },
    calculate: (x, f) => f(x)
}, 'http://localhost:8124');
node worker.js

Or you can webpack it and run it in browser!!!

###manager.js

var rpcManager = require('maus').manager;

var myManager = new rpcManager(8124);

myManager.do(workers => {
	var callback = result => console.log(result);
	
	workers.add(1, 1, callback); // return a number: 2
	workers.divide(100, 0, callback); // return a number: Infinity
	workers.newRegExp("abc", "ig", callback); // return a reg: /abc/ig
	workers.promiseAsync(callback); // return a Date
	
	//To write a recursion, you should use '__this' as the function itself 
	var fib = x => x > 1 ? __this(x - 1) + __this(x - 2) : x;
	workers.calculate(10, fib, callback);
})
node manager.js

#Usage ###1、Worker #####Worker.create(methodObject, path)

  • methodObject:

The methodObject contains some methods for RPC. Methods must return a value or a Promise.

Support return type: Number, NaN, Infinity, Error, Undefined, String, Array, Boolean, Date, RegExp, Object

Please use Async as suffix for some async method, like httpGetAsyncreadFileAsyncsomePromiseAsync:

{
	add: (x, y) => x + y,
	date: () => new Date(),
	promiseAsync: () => {
        return new Promise((resolve, reject) => {
            setTimeout(() => resolve('promise'), 1000)
        })
    }
}
  • path

The path of Manager

#####Worker.registerParkserver(path, workerType, methodObject)

  • path

The path of Parkserver

  • workerType

The type of worker, default value is "default"

  • methodObject

Same as methodObject in Worker.create(methodObject, path).

rpcWorker.registerParkserver('http://localhost:8500', 'common', {
    add: (x, y) => x + y,
    fib: fib,
    do: (v, f) => f(v)
})

###2、Manager

#####Manager(port)

  • port

The port that Manager listens to

var myManager = new Manager(8124);

#####Manager.do(callback)

  • callback

The callback function will be executed after init. It will gets a workers static as arguments, which contains all the methods in Worker

Manager.do(workers => {
    console.log('task start!')
    workers.promiseAsync(result => console.log(result));
    workers.add(1, 1, result => console.log(result));
});

The params of methods in Worker can be a Number, Object, String, Array, or even Function. Please use __this as the function itself in any recursion algorithm, such as:

var fib = x => x > 1 ? __this(x - 1) + __this(x - 2) : x;

#####Manager.connectParkserver(path)

  • path

The path of Parkserver

Manager.connectParkserver('http://localhost:8500');

#####Manager.getWorker(config)

This method should be called after Manager.connectParkserver .

config has 3 attributes:

  • amount [number] optional:

The amount of workers that you want to get. if this is not defined, the Manager will get all the available workers.

  • workerType [string] optional:

The type of workers that you want to get. The default value is "default".

  • address [string] required: The address of Manager
Manager.connectParkserver('http://localhost:8500');
Manager.getWorker({
    amount: 2,
    workerType: 'common',
    address: 'http://localhost:8124'
}).do(workers => {
	//Do Something...
});

#####Manager.end Release connected workers. Those workers will be reassigned by Parkserver.

###3、Parkserver

Parkerver is an optional part of Maus. It will monitor working status of workers, assign suitable workers to Manager. You can use it to handle multiple Managers if needed.

Manager can get workers it wants through parkserver. If the Manager is died, the workers that assigned to it will be recollected by parkserver, and can be reassigned to other Managers.

#####Parkserver(port)

  • port

The port that Parkserver listens to

var parkserver = require('maus').parkserver;
var myParkserver = new parkserver(8500);