Giter Club home page Giter Club logo

shinu's Introduction

Shinu - Elegant Process Exception Handling

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.

Features

  • 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

Installation

You can install Shinu using npm or yarn:

npm install shinu

or

yarn add shinu

Usage

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.

API

new Shinu(options: IShinuOptions)

Creates a new Shinu instance with the specified options.

const shinu = new Shinu({
  debug: true,
  handlers: [
    {
      signal: 'SIGUSR1',
      handler: (signal) => {
        console.log(`Received ${signal}`);
      },
    },
  ],
});

options.debug

Type: boolean Default: false

Enables debug mode. When debug mode is enabled, Shinu will log detailed process information for each signal received.

options.handlers

Type: IHandler[] Default: []

An array of signal handlers to register when the Shinu instance is started.

options.logger

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.

options.handleUncaughtException

Type: boolean Default: false

Enables handling of uncaught exceptions. When this option is enabled, Shinu will log any uncaught exceptions and exit the process.

options.handleUnhandledRejection

Type: boolean Default: false

Enables handling of unhandled rejections. When this option is enabled, Shinu will log any unhandled rejections and exit the process.

shinu.addHandler(signal: signals, handler: handler)

Adds a handler function for the specified signal.

shinu.addHandler('SIGUSR1', (signal) => {
  console.log(`Received ${signal}`);
});

shinu.removeHandler(signal: signals, handler?: handler)

Removes the specified handler function for the specified signal. If no handler function is provided, removes all handlers for the signal.

shinu.removeHandler('SIGUSR1');

shinu.removeAllHandlers()

Removes all registered signal handlers.

shinu.removeAllHandlers();

shinu.start()

Starts listening for registered signals and runs any associated handlers.

shinu.start();

shinu.isHandlerAdded(signal: signals, handler: handler)

Returns true if the specified handler function is registered for the specified signal, false otherwise.

const isHandlerAdded = shinu.isHandlerAdded('SIGUSR1', myHandlerFunction);

License

This project is licensed under the MIT License. See the LICENSE file for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.