@breadc/death
TypeScript icon, indicating that this package has built-in type declarations

0.9.7 • Public • Published

@breadc/death

version CI

Easily register termination signals callbacks.

It will listen SIGINT, SIGTERM, SIGQUIT these three signals. When receive any one of them, it will invoke all the callbacks in the reverse order (it also await the asynchronous callbacks), and finally send the original signal again.

In Signal events, it says that SIGTERM and SIGINT have default handlers on non-Windows platforms that reset the terminal mode before exiting with code 128 + signal number. If one of these signals has a listener installed, its default behavior will be removed (Node.js will no longer exit).

Installation

npm i @breadc/death

Usage

import { onDeath } from '@breadc/death'

onDeath((signal) => {
  console.log(`Receive signal: ${signal}`);
  console.log('Process is being killed');
})

You can also use process.exit instead of process.kill, or disable terminating the current process, which is the default behavior of this package. Note that the context object is shared between different callbacks.

onDeath((signal, context) => {
  // Use process.exit(1)
  context.terminate = 'exit'
  context.exit = 1
})
onDeath((signal, context) => {
  // Disable terminate
  context.terminate = false
})

License

MIT License © 2023 XLor

Package Sidebar

Install

npm i @breadc/death

Weekly Downloads

257

Version

0.9.7

License

MIT

Unpacked Size

11.7 kB

Total Files

8

Last publish

Collaborators

  • yjl9903