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

multilevel-agent

v0.0.6

Published

makes multilevel server/client simpler and stronger, with auto restart and reconnect.

Downloads

6

Readme

multilevel-agent NPM version Build Status

makes multilevel server/client simpler, with auto restart and reconnect.

Installation

npm install multilevel-agent

Usage

var multilevelAgent = require('multilevel-agent');

Server

var server = multilevelAgent.Server([options]);

The options could have below properties:

  • location The directory where to storage your data(required).
  • autostart A value indicates whether automatic start the SERVER or not, false as default.
  • port Port of TCP server, 8081 as default.
  • auth and access from multilevel
  • options from levelup

Example

var server = multilevelAgent.Server({
  location: __dirname + '/db/',
  port: 8090,
  valueEncoding: 'json',
  auth: function (user, cb) {
    if (user.name == 'root' && user.pass == 'p@ss') {
      cb(null, {name: 'root'});
    } else {
      cb(new Error('not authorized'));
    }
  },
  access: function (user, db, method, args) {
    if (!user || user.name !== 'root') {
      if (/^put|^del|^batch|write/i.test(method)) {
        throw new Error('read-only access');
      }
    }
  }
});

See more from example/server.js.

Events

error

Emitted when an error occurs, e.g.:

server.on('error', function(err){

});

The err is an instance of Error, and in addition, err has one more property err.source, see more from sources.

connect

Emitted when a new TCP connection is made, e.g.:

server.on('connect', function(){

});
close

Emitted when the server/client/database closes, e.g.:

server.on('close', function(agent){

});

agent including two properties:

  • source One of sources.
  • event The original event.

This event is very important, and you can use agent.source to identify who emitted 'close' event, it could be one of sources. If it is 'tcp_server' or 'database', it means server or database has just crashed, you'd better restart your server. If you are using pm2, just exist the process then pm2 will auto restart your server:

server.on('close', function(agent){
  if(agent.source == multiAgent.Server.source.TCP_SERVER || agent.source == multiAgent.Server.source.DATABASE){
    process.exit(0);
    return;
  }
  // tcp_client closed? screw it.
});

Methods

start

Just start the server, if autostart is set to true, this effects nothing.

server.start();
stop

If current state equals multiAgent.Server.state.RUNNING, stops the server. Notes: The server is finally closed when all connections are ended and the server emits a 'close' event. So, if you really wanna shut down the server, just try process.exit(0).

server.stop();

Properties

options

The restructuring options.

console.log(server.options);
state

The current state of server, see more from states.

port

The port of TCP server.

Client

var client = multilevelAgent.Client([options]);

The options could have below properties:

Example

var client = multilevelAgent.Client({
  host: 'localhost',
  port: 8090
});

See more from example/client.js.

API

Access multilevel's APIs through client.db, e.g.:

client.db.put('KEY', 'VALUE');
client.db.get('KEY', function(err, value){

});
// ...

Events

error

Emitted when an error occurs, e.g.:

server.on('error', function(err){

});

The err is an instance of Error, and in addition, err has one more property err.source, see more from sources.

connect

Emitted when a new TCP connection or Database is made, e.g.:

server.on('connect', function(){

});
reconnecting

Emitted when reconnecting to datababase/tcp server, e.g.:

client.on('reconnecting', function(recon){

})

The reconn indluding two properties:

  • attempts The attempts number that CLIENT has tried reconnect to server.
  • delay The next retry delay(milliseconds).

Properties

options

The restructuring options.

console.log(server.options);
state

The current state of server, see more from states.

Sources

Server

  • 'database' equals multiAgent.Server.source.DATABASE.
  • 'tcp_client' equals multiAgent.Server.source.TCP_CLIENT.
  • 'tcp_server' equals multiAgent.Server.source.TCP_SERVER.
  • 'level_server' equals multiAgent.Server.source.LEVEL_SERVER.

Client

  • 'database' equals multiAgent.Client.source.DATABASE.
  • 'tcp_client' equals multiAgent.Client.source.TCP_CLIENT.
  • 'rpc_stream' equals multiAgent.Client.source.RPC_STREAM.
  • 'level_server' equals multiAgent.Client.source.LEVEL_SERVER.

States

Server

  • 'init' equals multiAgent.Server.state.INIT.
  • 'running' equals multiAgent.Server.state.RUNNING.
  • 'stop' equals multiAgent.Server.state.STOP.

Client

  • 'init' equals multiAgent.Client.state.INIT.
  • 'running' equals multiAgent.Client.state.RUNNING.
  • 'stop' equals multiAgent.Client.state.STOP.

Test

Only test options.

npm test

Manual test:

  • First: start server

    node example/server
  • Second: run client

    node example/client

If you wanna make different tests, just edit the scripts under example folder.

License

Copyright 2014 Tjatse

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.