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

@typeservice/core

v1.0.5

Published

A lightweight and pure service startup architecture.

Downloads

7

Readme

@typeservice/core

codecov

A lightweight and pure service startup architecture. It provides the most direct access mode through a stable process lifecycle model. You can use it to build a variety of service frameworks.

It has the following features:

  • Life cycle
  • Process fault tolerance
  • IPC communication protocol
  • Dynamic Agent Process
  • Cluster process support

It is written in TYPESCRIPT and provides a good coding experience. At the same time, it can be used to guard services through any form of process hosting, such as pm2, forever.

Installing

For the latest stable version:

$ npm install @typeservice/core

For our nightly builds:

$ npm install @typeservice/core@next

Try it

import * as path from 'path';
import * as http from 'http';
import { WorkerFactory, IPCException, resolve } from '@typeservice/core';
class HttpFrameworker extends WorkerFactory {
  private readonly server: http.Server;
  constructor() {
    super();
    this.on('setup', () => new Promise(resolve => {
      const server = http.createServer((req, res) => res.end('ok'));
      Object.defineProperty(this, 'server', { value: server });
      console.log('start at 8080');
      server.listen(8080, resolve);
    }));
    this.on('exit', async () => this.server.close());
  }
}

const frameworker = new HttpFrameworker();

frameworker.listen()
  // use ipc communication protocol
  // get abc method result from master by args: { a: 1, b: 2 }
  .then(() => frameworker.messager.invoke('master', 'abc', { a: 1, b: 2 }))
  .then(data => console.log('result:', data))
  .catch((e: IPCException) => console.error(e.message))
  // create a new agent dynamicly
  .then(() => frameworker.messager.create('abc', path.resolve(__dirname, '../agent/abc'), 678))
  .then(() => new Promise(resolve => setTimeout(resolve, 5000)))
  .then(() => console.log('start invoke abc.test'))
  .then(() => frameworker.messager.invoke('abc', 'test', 666, 3000))
  .then((data) => console.log('end invoke abc.test', data))
  .catch(e => console.error(e))

Worker

You can create a secure service by inheriting from the WorkerFactory.

class HttpFrameworker extends WorkerFactory {}

It has 3 lifecycle events:

  • setup Process initial load event
  • exit Process shutdown event
  • error Process unexpected error event

Here, we don't know how to use the worker to create a service architecture. You can use the @typeservice/http project to find out how to create it.

Agent

Maybe this is our focus. The dynamic agent process associates it with the worker service through the IPC communication protocol to form a communication closed loop based on the primary and secondary processes. It provides a series of annotations to help us build communication channels.

The notes are as follows:

  • @method tag it is an IPC communication method
  • @initialize initialization function
  • @terminate process end function
  • @error error handler (uncaught error or unexpected error)
  • @middleware middleware

These methods are sufficient for us to use to form plugins to provide various functions, such as setting up task scheduling.

Here is a simple example:

import { AgentFactory, ipc, AgentContext } from '@typeservice/core';

export default class ABCAgent extends AgentFactory {
  private readonly _max: number;
  constructor(max: number) {
    super();
    this._max = max;
  }

  @ipc.method
  @ipc.middleware<AgentContext>(async (ctx, next) => {
    console.log('ctx', ctx);
    await next();
  })
  async test(ctx: AgentContext) {
    return ctx.data + this._max;
  }

  @ipc.initialize
  setup() {
    console.log('agent init')
  }

  @ipc.terminate
  exit() {
    console.log('agent exit')
  }
}

License

MIT

Copyright (c) 2019-present, yunjie (Evio) shen