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

@borisovg/service-core

v2.3.0

Published

Micro-framework for Node.js services

Downloads

4

Readme

node-service-core

This is a micro-framework for building Node.js services.

Usage example

npm install @borisovg/service-core pino
import { load, setLogger } from '@borisovg/service-core';
import { pino } from 'pino';

setLogger(pino());
void load(`${__dirname}/modules`);

Plugin pattern

The basic idea behind the framework is the ability to (recursively) auto-load program files from a given directory without having explicitly know about the existence of individual files. This allows for rapid development of autonomous or loosely-coupled components. An example of where this might be immediately useful is a collection of HTTP API handlers, that can be split into individual files, each housed in a domain-specific sub-directory - something that normally would require tedious boilerplate to load at startup.

In addition to auto-loading, each file can optionally export some hook methods:

  • $onBind(sr: ServiceRegistry, name: string) => Promise<void> which are run first, blocking start-up until they complete
  • $onLoad(sr: ServiceRegistry, name: string) => Promise<void> which are run after, blocking start-up until they complete
  • $onRun(sr: ServiceRegistry, name:string) => Promise<void> which are run after, blocking start-up until they complete
  • $onShutdown(sr: ServiceRegistry, name: string) => Promise<void> which are run at shutdown, blocking exit until they complete

As an contrived example, we can add the following under modules/loop.ts:

import { getLogger } from '@borisovg/service-core';

const log = getLogger();
let timeout: NodeJS.Timeout;

export function $onLoad() {
  timeout = setInterval(() => {
    log.info({ message: 'TICK' });
  }, 1000);
}

export function $onShutdown() {
  clearInterval(timeout);
}

This module will start logging messages on startup and will clear the timeout before exiting. Adding $onShutdown() hooks is particularly useful for stopping various event loops during testing, as these may otherwise prevent the test runner from exiting.

Service Registry

The module hook methods will be called with a service registry object. Modules can add new methods and call methods defined by other modules. During testing this also makes it easy to mock these methods as required.

As a contrived example our modules/foo.ts will add a method to the service registry and re-implement the loop from the example above using built-in loops module:

import type { CoreServiceRegistry } from '@borisovg/service-core';

// defined this in top-level "types.ts" file to include all methods your application adds
export type ServiceRegistry = CoreServiceRegistry & {
  hello: {
    greet: (name: string) => void;
  };
};

// only use "$onBind" to add methods to the registry
export function $onBind(sr: ServiceRegistry) {
  sr.hello = {
    greet(name) {
      console.log(`Hello ${name}!`);
    },
  };
}

export function $onRun(sr: ServiceRegistry, name: string) {
  sr.core.loops.add(name, 1000, () => {
    sr.hello.greet('World');
  });
}