@efesto-cloud/teardown
v0.0.4
Published
Teardown type for efesto-cloud
Readme
@efesto-cloud/teardown
Register cleanup functions that run once on SIGINT / SIGTERM before the process exits.
Installation
pnpm add @efesto-cloud/teardownRequires Node ≥ 24.
Quick Start
import TeardownUtil from "@efesto-cloud/teardown";
const teardown = new TeardownUtil();
teardown
.register(async () => {
await server.close();
})
.register(async () => {
await db.close();
});On SIGINT or SIGTERM:
- Each registered function runs sequentially, in the order they were registered.
- The process then exits with code
0. - A second signal while shutdown is already in progress forces exit with code
1.
API
class TeardownUtil {
constructor();
register(fn?: () => Promise<void>): this;
}register(fn)— adds a cleanup function. Returnsthisso calls can be chained.- Signal handlers are attached on construction — typically you want a single instance per process.
Notes
- Failing cleanup functions (rejected promises) will bubble up and bypass later handlers. Wrap with
try/catchinside eachfnif you need best-effort shutdown. - Registration order matters: close things in the reverse order you opened them (HTTP server first, database last, etc.).
- Only
SIGINTandSIGTERMare handled. Uncaught exceptions or other signals do not trigger the handlers.
