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

kaptan

v0.0.13

Published

All in one micro-service framework

Downloads

19

Readme

Kaptan NPM License Build Status Codecov

All in one micro-service framework with minimum dependencies and simplistic design. Most suitable for distributed applications and applications that serve multiple endpoints. The main concept behind Kaptan framework is using services as parts to build application.

Documentation is generated by TypeDoc and it's available at GitHub Pages of this repository.

Available Services

| Package | Repository | Description | | ---------- | ---------- |:-----------:| kaptan-http | ibrahimduran/node-kaptan-http | Simple HTTP service with utilities. |

Getting Started

You can install Kaptan framework and its services easily from NPM using your favorite tool.

$ npm install --save kaptan
# or
$ yarn add kaptan

TypeScript users don't need to install any package for type definitions, they're served in the main package.

Creating an application

First parameter of Kaptan.constructor is label for the application. Having a label prevents mess in logs when you have multiple instances of kaptan.

import { Kaptan } from 'kaptan';

const kaptan = new Kaptan('my-app');

Or feel free to extend Kaptan class:

import { Kaptan } from 'kaptan';

class MyApp extends Kaptan {
  constructor() {
    super();

    // (lines below are only visible to who believes in unicorns)
    // My secret code - start


    // My secret code - end
  }
}

const app = new MyApp();

Adding services to application

Services can be installed from NPM. You can use Kaptan.use method to add service and service options.

import { CustomService } from 'kaptan-foo-bar';

kaptan.use(CustomService, { XYZ: false });

Starting application

This will fire up application and create service instances.

kaptan.start()
  .then(() => console.log('Application started!'))
  .catch(err => console.error(err));

Developing Services

Services are created by extending Service class. Service constructor takes kaptan intance as first parameter and options object as second. Also kaptan, logger and options properties get set in extended Service constructor.

import { Service } from 'kaptan';

class MyService extends Service {
  constructor(kaptan, options) {
    super(kaptan, {
      XYZ: 'some_default_val',
      ...options
    });

    // theese are automatically set
    console.log(this.kaptan);
    console.log(this.options);
    console.log(this.logger);

    // service dependencies/injections
    const fooBar = kaptan.services.spawn('FooBar');
  }

  async start() {
  }

  async stop() {
  }
}

You can use kaptan.services.spawn method to access service instances.

Network

Kaptan framework comes with a built-in Network service to simplify networking. All features of network service is async/await - promise compatible. It currently supports three protocols for sending packets; Request/Response for sending request and receiving response synchronously (using promises) and Raw for sending data without waiting for response.

Informations below are just a brief description for understanding the concept. Don't forget to check documentation to get more information about classes or methods.

Using the service

Kaptan package exports Network namespace which contains Network service. So you'll need to add Network.Network as service.

import { Network } from 'kaptan';

kaptan.use(Network.Network, { PORT: 5000 });

Packets, packet handlers and packet filters

Network.Packet is basically a data holder class with serialization logic in it. Network.PacketHandler instances can be passed to sockets and network service itself. By default, every one of packet handlers run when a packet arrives to the local socket but you can add filters to the handler using the Network.PacketFilter class.

Also you can use literal objects to create PacketHandler and PacketFilter instances in order simplify usage.

const filter = Network.PacketFilter.from({ ref: 'myPacket' });

const handler = new Network.PacketHandler({
    filter: filter,
    onParsed(socket, packet) {
    }
    
    onReceive(socket, raw) {
    }
});

Sockets and addresses

Network.Address is a common class that contains IP address utilities. Network.Socket is a wrapper for net's socket object. You can pass packet handlers to sockets as mentioned above. There are two main methods in socket; wait(filter) and send(packet). Since wait method returns a promise, you can create synchronized communications between server and client with power of async/await syntax of ES2017.

const filter = new Network.PacketFilter()
    .require('message');
    
const response = await socket.wait(filter);
socket.send({ data: { message: response.data.message } }); // mirror message

License

MIT