Socket
Book a DemoInstallSign in
Socket

@borisovg/service-core

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@borisovg/service-core

Micro-framework for Node.js services

2.3.0
latest
Source
npmnpm
Version published
Weekly downloads
1
-94.44%
Maintainers
1
Weekly downloads
 
Created
Source

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');
  });
}

FAQs

Package last updated on 10 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.