# @darkobits/adeiu

> Yet another POSIX signal handler.

Latest version **0.5.0** (published 2025-02-24) · Hippocratic license · 0 weekly downloads

## Install

```sh
npm install @darkobits/adeiu
pnpm add @darkobits/adeiu
yarn add @darkobits/adeiu
bun add @darkobits/adeiu
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2025-02-24 |
| First published | 2019-04-18 |
| Weekly downloads | 0 |
| License | Hippocratic |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 14.0.0 |
| Dependencies | 0 |
| Unpacked size | 29.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | darkobits |
| Maintainers | darkobits |
| Keywords | clean, event, exception, exit, graceful, handler, hook, kill, kill, process, process, quit, shutdown, sigint, sigint, sigkill, signal, sigquit, sigterm, sigterm, stop, terminate |

## Links

- npm: https://www.npmjs.com/package/@darkobits/adeiu
- Repository: https://github.com/darkobits/adeiu
- Homepage: https://github.com/darkobits/adeiu#readme
- Issues: https://github.com/darkobits/adeiu/issues
- npm.io page: https://npm.io/package/@darkobits/adeiu

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.5.0 (latest) — 2025-02-24
- 0.4.4 — 2025-02-22
- 0.4.3 — 2025-02-22
- 0.4.2 — 2025-02-22
- 0.4.1 — 2024-02-01
- 0.4.0 — 2023-07-10
- 0.3.1 — 2023-02-22
- 0.3.0 — 2023-02-22
- 0.2.16 — 2023-02-22
- 0.2.15 — 2022-09-12
- 0.2.14 — 2021-08-07
- 0.2.13 — 2021-05-13
- 0.2.12 — 2020-12-24
- 0.2.11 — 2020-12-21
- 0.2.10 — 2020-12-16
- … 11 more at https://npm.io/package/@darkobits/adeiu/versions

## README

<p align="center">
  <picture>
    <source
      media="(prefers-color-scheme: dark)"
      srcset="https://github.com/darkobits/adeiu/assets/441546/5639dd58-3a6f-4015-98a9-14da5e22a97e"
      width="100%"
    >
    <img
      src="https://github.com/darkobits/adeiu/assets/441546/80c6679d-1419-4e15-a7f7-480e51c97511"
      width="100%"
    >
  </picture>
</p>
<p align="center">
  <a
    href="https://www.npmjs.com/package/@darkobits/adeiu"
  ><img
    src="https://img.shields.io/npm/v/@darkobits/adeiu.svg?style=flat-square"
  ></a>
  <a
    href="https://github.com/darkobits/adeiu/actions?query=workflow%3Aci"
  ><img
    src="https://img.shields.io/github/actions/workflow/status/darkobits/adeiu/ci.yml?style=flat-square"
  ></a>
  <a
    href="https://depfu.com/repos/github/darkobits/adeiu"
  ><img
    src="https://img.shields.io/depfu/darkobits/adeiu?style=flat-square"
  ></a>
  <a
    href="https://conventionalcommits.org"
  ><img
    src="https://img.shields.io/static/v1?label=commits&message=conventional&style=flat-square&color=398AFB"
  ></a>
  <a
    href="https://firstdonoharm.dev"
  ><img
    src="https://img.shields.io/static/v1?label=license&message=hippocratic&style=flat-square&color=753065"
  ></a>
</p>

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](https://github.com/nodejs/node/issues/7742))

## Install

```
npm i @darkobits/adeiu
```

## Use

```ts
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`

```ts
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:

```ts
import adeiu from '@darkobits/adeiu'

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

```ts
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:

```ts
import adeiu from '@darkobits/adeiu'

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

---
_Source: https://npm.io/package/@darkobits/adeiu · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
