包详细信息

@darkobits/adeiu

darkobits467Hippocratic0.5.0

Yet another POSIX signal handler.

clean, event, exception, exit

自述文件

<picture> <source media="(prefers-color-scheme: dark)" srcset="https://github.com/darkobits/adeiu/assets/441546/5639dd58-3a6f-4015-98a9-14da5e22a97e" width="100%" > </picture>

Adeiu is a POSIX signal handler designed to for applications with asynchronous cleanup / shutdown requirements, such as gracefully shutting-down an HTTP server or closing a database connection.

Features

  • Zero dependencies.
  • Ensures provided handlers are called before any other event listeners and are run concurrently, minimizing shutdown time.
  • Works with any combination of synchronous and asynchronous handlers.
  • Automatically exits with code 0 once all handlers resolve/return, or 1 if any reject/throw.
  • Supports edge cases related to the Node debugger being attached to a process. (See this issue)

Install

npm i @darkobits/adeiu

Use

adeiu(handler: AdeiuHandler, options?: AdeiuOptions): () => void

Adeiu accepts an asynchronous or synchronous handler function and returns an unregister function. By default, the handler will be registered to respond to the following signals:

  • SIGINT
  • SIGQUIT
  • SIGTERM
  • SIGUSR2
import adeiu from '@darkobits/adeiu'

const unregister = adeiu(async signal => {
  console.log(`Received signal ${signal}; shutting down...`)
  await asyncCleanup()
  console.log('Done.')
})

If multiple handlers are registered, they will be invoked in parallel.

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 installed 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:

import adeiu from '@darkobits/adeiu'

// Register handler that will _only_ be invoked on SIGINT:
adeiu(() => {
  // ...
}, { signals: ['SIGINT'] })
import adeiu, { DEFAULT_SIGNALS } from '@darkobits/adeiu'

// Register handler with the default signals _and_ SIGUSR1:
adeiu(() => {
  // ...
}, { signals: [...DEFAULT_SIGNALS, 'SIGUSR1'] })

Specifying a Timeout

By default, handlers will have no timeout imposed. If, however, you wish to only wait a specific amount of time for a handler to run, the timeout option may be used:

import adeiu from '@darkobits/adeiu'

// Register a handler that will have 5 seconds to execute.
adeiu(() => {
  // ...
}, { timeout: 5000 })

更新日志

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.5.0 (2025-02-24)

⚠ BREAKING CHANGES

  • adeiu.SIGNALS has been moved to a named export, DEFAULT_SIGNALS.

Before:

import adeiu from '@darkobits/adeiu'

// Default signals.
adeiu.SIGNALS

After:

import adeiu, { DEFAULT_SIGNALS } from '@darkobits/adeiu'

// Default signals.
DEFAULT_SIGNALS
  • Export default signals. (ecbf785)

0.4.4 (2025-02-22)

0.4.3 (2025-02-22)

0.4.2 (2025-02-22)

Bug Fixes

  • Use prependListener instead of prependOnceListener. (935129d)

0.4.1 (2024-02-01)

🏗 Chores

  • deps: Update dependencies. (d0a9792)

0.4.0 (2023-07-10)

⚠ BREAKING CHANGES

  • This module is now published as ESM.

🏗 Chores

🛠 Refactoring

0.3.1 (2023-02-22)

🐞 Bug Fixes

  • Fix ow default import issue. (4dd8743)

0.3.0 (2023-02-22)

⚠ BREAKING CHANGES

  • Before:
import adeiu, { SIGNALS } from '@darkobits/adeiu';

After:

import adeiu from '@darkobits/adeiu';

adeiu.SIGNALS;

🛠 Refactoring

  • Attach SIGNALS to adeiu export. (7fbb753)

0.2.16 (2023-02-22)

📖 Documentation

🏗 Chores

  • deps: Update dependencies. (de1861a)

0.2.15 (2022-09-12)

🏗 Chores

📖 Documentation

0.2.14 (2021-08-07)

📖 Documentation

🏗 Chores

0.2.13 (2021-05-13)

📖 Documentation

🏗 Chores

  • deps: Update dependencies. (c7e6e07)

0.2.12 (2020-12-24)

🏗 Chores

0.2.11 (2020-12-21)

🏗 Chores

0.2.10 (2020-12-16)

📖 Documentation

🏗 Chores

0.2.9 (2020-12-09)

🏗 Chores

📖 Documentation

0.2.8 (2020-11-12)

0.2.7 (2020-06-07)

0.2.6 (2019-10-29)

0.2.5 (2019-09-27)

0.2.4 (2019-06-29)

0.2.3 (2019-06-29)

0.2.2 (2019-06-07)

0.2.1 (2019-04-23)

0.2.0 (2019-04-23)

Features

  • Callbacks may be registered with a custom list of signals. (eba691b)
  • Log errors thrown by callbacks to stderr. (efd9576)

0.1.0 (2019-04-18)

Features