npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@hardlydifficult/teardown

v1.0.2

Published

Idempotent resource teardown with signal trapping. Register cleanup functions once at resource creation time — all exit paths call a single `run()`.

Readme

@hardlydifficult/teardown

Idempotent resource teardown with signal trapping. Register cleanup functions once at resource creation time — all exit paths call a single run().

Installation

npm install @hardlydifficult/teardown

Quick Start

import { createTeardown } from "@hardlydifficult/teardown";

const teardown = createTeardown();
teardown.add(() => server.stop());
teardown.add(() => db.close());
teardown.trapSignals();

// Any manual exit path:
await teardown.run();

Creating a Teardown Registry

Use createTeardown() to create a new teardown registry that manages cleanup functions.

import { createTeardown } from "@hardlydifficult/teardown";

const teardown = createTeardown();

Registering Cleanup Functions

Call add() to register a cleanup function. Functions run in LIFO order (last added runs first). The add() method returns an unregister function for selective cleanup.

const teardown = createTeardown();

// Register sync cleanup
teardown.add(() => {
  console.log("Closing server");
  server.stop();
});

// Register async cleanup
teardown.add(async () => {
  console.log("Closing database");
  await db.close();
});

// Unregister a specific function
const unregister = teardown.add(() => {
  console.log("This won't run");
});
unregister();

await teardown.run();
// Output:
// Closing database
// Closing server

Running Teardown

Call run() to execute all registered cleanup functions in LIFO order. The method is idempotent — subsequent calls are no-ops.

const teardown = createTeardown();
teardown.add(() => console.log("cleanup"));

await teardown.run();
// Output: cleanup

await teardown.run();
// No output (idempotent)

Signal Trapping

Call trapSignals() to automatically run teardown when the process receives SIGTERM or SIGINT signals. Returns an untrap function to remove signal handlers.

const teardown = createTeardown();
teardown.add(() => server.stop());

// Wire SIGTERM/SIGINT to run() then process.exit(0)
const untrap = teardown.trapSignals();

// Later, if needed:
untrap();

Error Resilience

Each teardown function is wrapped in try/catch. Errors don't block remaining teardowns — all functions run regardless of failures.

const teardown = createTeardown();

teardown.add(() => {
  throw new Error("First cleanup fails");
});
teardown.add(() => {
  console.log("Second cleanup still runs");
});

await teardown.run();
// Output: Second cleanup still runs

Behavior Reference

| Behavior | Details | |----------|---------| | LIFO order | Teardowns run in reverse registration order (last added runs first) | | Idempotent | run() executes once; subsequent calls are no-ops | | Error resilient | Each function is wrapped in try/catch; failures don't block remaining teardowns | | Safe unregister | add() returns an unregister function; safe to call multiple times | | Post-run add | add() after run() is a silent no-op | | Duplicate safe | Same function added twice runs twice; unregister only removes its own registration |