Skip to content

SignalRouter

do- edited this page Aug 25, 2026 · 19 revisions

SignalRouter is a Router that listens to system signals and forwards event names to registered destinations.

It may appear to be just a needless cumbersome wrapper over the standard event handling, but indeed it do have some sense when using events-to-winston for general logging.

Constructor

const {SignalRouter} = require ('protocol-agnostic-router')

const router = new SignalRouter ({
    name: 'sig',
    logger: myLogger,
    handler: {
        signals : ['SIGTERM', 'SIGINT', 'SIGBREAK'],
        handler : myApp.onExit,
        once    : true
    },
/* OR
    handlers: [
      {
        signal  : 'SIGPIPE'
        handler : myApp.onBrokenPipe,
      },
      {
        signals : ['SIGFPE', 'SIGSEGV']
        handler : sig => console.log (sig + ' happened!'),
      },
    ],
*/
})

router.listen ()

Options

Name Description
hander a single handler definition
handers complete list of handler definitions (if used, overrides hander)
logger a winston logger
name the name of this router to use in logs etc.
server options for the server construstor. May be omitted.

hander options

Name Description
hander the signal handler function
once it truthy, .once will be used instead of .on
signal the signal name to listen to
signals complete list of signal names (if used, overrides signal)

Events

Name Description
start emitted at the end of the listen () method
data emitted on each signal received
finish emitted at the end of the close () method

Methods

listen ()

Starts listening to system signals by setting up listeners according to the handler[s] configuration.

close ()

This (nominally) asynchronous method:

  • removes all listeners for configured signals (including those set otherwise);
  • sets no-op handlers for them (to prevent double invocation of exit procedures);
  • emits the 'finish' event.

Clone this wiki locally