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 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-cleanup

v1.0.0

Published

Asynchronous process cleanup for Node.js and Typescript

Downloads

4,507

Readme

async-cleanup: Asynchronous process cleanup for Node.js and Typescript

npm CircleCI

A common need for Node.js applications is to execute some sort of cleanup code before the process exits, regardless of what caused it to exit. Furthermore, cleanup code often involves asynchronous operations such as file or network I/O. For example, imagine having a lock file that prevents multiple instances of an application from running concurrently. Whether the application completes successfully, crashes from an unhandled exception, or is interrupted with Ctrl-C, the lock file must be deleted before exit. This library makes writing asynchronous cleanup code as easy as calling a single function. It has no runtime dependencies, compiles to ES6 for wide compatibility, and has a package size of 5.1 kB.

Installation

npm install async-cleanup

Usage

Typical usage might look something like this:

import { addCleanupListener, exitAfterCleanup } from "async-cleanup";
import { unlink, writeFile } from "fs/promises";

void (async function () {
  const path = ".lockfile";
  try {
    await writeFile(path, String(process.pid), { flag: "wx" });
  } catch (err) {
    if ((err as { code?: string }).code === "EEXIST") {
      console.log(`${path} already exists`);
    } else {
      console.log(`Error writing ${path}`, err);
    }
    // Can't use process.exit with async cleanup
    await exitAfterCleanup(1);
  }
  console.log(`Created ${path}`);

  addCleanupListener(async () => {
    await unlink(path);
    console.log(`Deleted ${path}`);
  });

  // Do stuff...

  console.log("Stuff done");
})();

The output of the program above might look like this (depending on how concurrently it is run):

$ concurrently 'ts-node examples/readme.ts' 'ts-node examples/readme.ts' 'ts-node examples/readme.ts'

[1] .lockfile already exists
[0] .lockfile already exists
[2] Created .lockfile
[2] Stuff done
[2] Exiting with code 0 due to empty event loop
[2] Deleted .lockfile
[1] ts-node examples/readme.ts exited with code 1
[2] ts-node examples/readme.ts exited with code 0
[0] ts-node examples/readme.ts exited with code 1

API Reference

/**
 * A possibly asynchronous function invoked with the process is about to exit.
 */
type CleanupListener = () => void | Promise<void>;

/**
 * Registers a new cleanup listener.
 * Adding the same listener more than once has no effect.
 */
function addCleanupListener(listener: CleanupListener): void;

/**
 * Removes an existing cleanup listener, and returns whether the listener was registered.
 */
function removeCleanupListener(listener: CleanupListener): boolean;

/**
 * Executes all cleanup listeners and then exits the process.
 * Call this instead of `process.exit` to ensure all listeners are fully executed.
 */
function exitAfterCleanup(code?: number): Promise<never>;

/**
 * Executes all cleanup listeners and then kills the process with the given signal.
 */
function killAfterCleanup(signal: ExitSignal): Promise<void>;

/**
 * Signals that can terminate the process.
 */
type ExitSignal =
  | "SIGBREAK"
  | "SIGHUP"
  | "SIGINT"
  | "SIGTERM"
  | "SIGUSR2"
  | "SIGKILL"
  | "SIGQUIT"
  | "SIGSTOP";

License

async-cleanup is available under the ISC license.