Shinu is a lightweight Node.js library that makes it easy to handle signals and other process events in your application. It provides a simple interface for registering signal handlers and logging process information.
- Simple API for adding and removing signal handlers
- Default signal handlers for common signals (SIGINT, SIGTERM, SIGQUIT)
- Option to log process information for debugging purposes
- Option to handle uncaught exceptions and unhandled rejections
You can install Shinu using npm or yarn:
npm install shinu
or
yarn add shinu
Here's an example of how to use Shinu to handle the SIGINT signal:
const { Shinu } = require('shinu');
const shinu = new Shinu();
shinu.addHandler('SIGINT', (signal) => {
console.log(`Received ${signal}`);
process.exit(0);
});
shinu.start();
In this example, we create a new Shinu
instance and add a handler for the SIGINT
signal. The handler simply logs a message and exits the process. We then call the start
method to begin listening for signals.
By default, Shinu includes handlers for the SIGINT
, SIGTERM
, and SIGQUIT
signals, so you don't need to add these manually.
You can also pass options to the Shinu
constructor to customize its behavior:
const { Shinu } = require('shinu');
const shinu = new Shinu({
debug: true,
handlers: [
{
signal: 'SIGUSR1',
handler: (signal) => {
console.log(`Received ${signal}`);
},
},
],
});
shinu.start();
In this example, we pass an options object to the Shinu
constructor to enable debug logging and add a custom signal handler for SIGUSR1
.
Creates a new Shinu
instance with the specified options.
const shinu = new Shinu({
debug: true,
handlers: [
{
signal: 'SIGUSR1',
handler: (signal) => {
console.log(`Received ${signal}`);
},
},
],
});
Type: boolean
Default: false
Enables debug mode. When debug mode is enabled, Shinu will log detailed process information for each signal received.
Type: IHandler[]
Default: []
An array of signal handlers to register when the Shinu
instance is started.
Type: (message: string) => void
Default: console.log
A function that will be called to log messages. You can use your own logger or replace the default console.log
method.
Type: boolean
Default: false
Enables handling of uncaught exceptions. When this option is enabled, Shinu will log any uncaught exceptions and exit the process.
Type: boolean
Default: false
Enables handling of unhandled rejections. When this option is enabled, Shinu will log any unhandled rejections and exit the process.
Adds a handler function for the specified signal.
shinu.addHandler('SIGUSR1', (signal) => {
console.log(`Received ${signal}`);
});
Removes the specified handler function for the specified signal. If no handler function is provided, removes all handlers for the signal.
shinu.removeHandler('SIGUSR1');
Removes all registered signal handlers.
shinu.removeAllHandlers();
Starts listening for registered signals and runs any associated handlers.
shinu.start();
Returns true
if the specified handler function is registered for the specified signal, false
otherwise.
const isHandlerAdded = shinu.isHandlerAdded('SIGUSR1', myHandlerFunction);
This project is licensed under the MIT License. See the LICENSE file for details.