@darkobits/adeiu

Yet another POSIX signal handler.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@darkobits/adeiu
0.4.13 months ago5 years agoMinified + gzip package size for @darkobits/adeiu in KB

Readme

<source
  media="(prefers-color-scheme: dark)"
  srcset="https://github.com/darkobits/adeiu/assets/441546/5639dd58-3a6f-4015-98a9-14da5e22a97e"
  width="100%"
>
<img
  src="https://github.com/darkobits/adeiu/assets/441546/80c6679d-1419-4e15-a7f7-480e51c97511"
  width="100%"
>

href="https://www.npmjs.com/package/@darkobits/adeiu" >src="https://img.shields.io/npm/v/@darkobits/adeiu.svg?style=flat-square" > href="https://github.com/darkobits/adeiu/actions?query=workflow%3Aci" >src="https://img.shields.io/github/actions/workflow/status/darkobits/adeiu/ci.yml?style=flat-square" > href="https://depfu.com/repos/github/darkobits/adeiu" >src="https://img.shields.io/depfu/darkobits/adeiu?style=flat-square" > href="https://conventionalcommits.org" >src="https://img.shields.io/static/v1?label=commits&message=conventional&style=flat-square&color=398AFB" > href="https://firstdonoharm.dev" >src="https://img.shields.io/static/v1?label=license&message=hippocratic&style=flat-square&color=753065" >


Yet another POSIX signal handler.

Features

  • Ensures provided functions are called before any other event listeners and are run concurrently,
minimizing shutdown time.
  • Works with any combination of synchronous and asynchronous functions.
  • Ensures a clean exit if all functions resolve/return.
  • Exits with an error if any functions reject/throw.
  • Ensures processes exit cleanly, even when they have asynchronous shut-down functions and the Node
debugger is in use. (See this issue)

Install

npm i @darkobits/adeiu

Use

Adeiu accepts an asynchronous or synchronous handler function. By default, the handler will be registered to respond to the following signals:
  • SIGINT
  • SIGQUIT
  • SIGTERM
  • SIGUSR2

import adeiu from '@darkobits/adeiu';

adeiu(async signal => {
  console.log(`Received signal ${signal}; performing shut-down tasks...`);

  await someAsyncStuff();

  console.log('All done!');
});

Unregistering Handlers

Adeiu returns a function that can be invoked to unregister a handler.
import adeiu from '@darkobits/adeiu';

const unregister = adeiu(() => {
  // Handler implementation here.
});

// Un-register the handler.
unregister();

Customizing Signals

Usually, responding to signals dynamically can be accomplished by inspecting the signal argument passed to your handler. However, if it is important that handlers are only invoked for a particular signal, or if you'd like to respond to signals other than the defaults, you may optionally provide an array of signals as a second argument:
import adeiu from '@darkobits/adeiu';

// Register callback that will _only_ be invoked on SIGINT:
adeiu(() => {
  // SIGINT cleanup tasks.
}, ['SIGINT']);

import adeiu from '@darkobits/adeiu';

// Register callback with the default signals _and_ SIGUSR1:
adeiu(() => {
  // Custom cleanup tasks.
}, [...adeiu.SIGNALS, 'SIGUSR1']);