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

@apiratorjs/async-context

v1.1.5

Published

A Node.js async context library leveraging the node:async_hooks module, supporting context merging and overriding.

Readme

@apiratorjs/async-context

NPM version License: MIT

A lightweight Node.js library for managing asynchronous contexts using the built-in node:async_hooks module. This package allows you to share, merge, and override context data throughout your asynchronous code execution. It’s especially useful for per-request or per-transaction state management, logging, and debugging.

Note: Requires Node.js version >=16.4.0


Features

  • Async Context Management: Seamlessly pass context data across asynchronous calls.
  • Context Merging: Automatically merge context data for common data types (plain objects, arrays, Maps, and Sets).
  • Context Overriding: Override existing context data when needed.
  • Typed Context Store: Built with TypeScript to provide type safety.
  • Easy Integration: Works out-of-the-box with Node.js asynchronous operations.
  • Extended Map Functionality: AsyncContextStore extends the native Map class, so all Map methods (such as set, get, has, delete, etc.) are available and it supports any type as a key—including classes.

Installation

Install the package via npm:

npm install @apiratorjs/async-context

Or using yarn:

yarn add @apiratorjs/async-context

Usage

Basic Example

Use withContext to run a callback within an asynchronous context:

import { AsyncContext } from "@apiratorjs/async-context";

async function main() {
  await AsyncContext.withContext("user", { id: 123 }, () => {
    // Inside this callback the context is available.
    const user = AsyncContext.getContext("user");
    console.log("User Context:", user); // { id: 123 }
  });
}

main();

Merging Context

If you nest context calls using the same key, the package will merge the data if they are compatible (e.g., plain objects, arrays, Maps, or Sets):

import { AsyncContext } from "@apiratorjs/async-context";

AsyncContext.withContext("config", { theme: "dark" }, () => {
  console.log(AsyncContext.getContext("config")); // { theme: "dark" }
  
  AsyncContext.withContext("config", { language: "en" }, () => {
    console.log(AsyncContext.getContext("config")); // { theme: "dark", language: "en" }
  });
});

Overriding Context

Use withContextOverride to completely override a value in the context rather than merging:

import { AsyncContext } from "@apiratorjs/async-context";

AsyncContext.withContext("user", { id: 123, name: "Alice" }, () => {
  AsyncContext.withContextOverride("user", { id: 999 }, () => {
    console.log(AsyncContext.getContext("user")); // { id: 999 }
  });
});

Working with Multiple Keys

You can work with multiple context keys at once by passing an AsyncContextStore. For example:

import { AsyncContext, AsyncContextStore } from "@apiratorjs/async-context";

// Create a context store from a plain object.
const contextStore = AsyncContextStore.fromObject({
  user: { id: 1 },
  session: { token: "abc" }
});

AsyncContext.withContext(contextStore, () => {
  const user = AsyncContext.getContext("user");
  const session = AsyncContext.getContext("session");
  console.log("User:", user);       // { id: 1 }
  console.log("Session:", session); // { token: "abc" }
});

Retrieving Context Data

  • Entire Context: Call AsyncContext.getContext() with no arguments.
  • Specific Key: Call AsyncContext.getContext("key") to get the value associated with that key.
  • Partial Context: Use AsyncContext.getMultiContext(["key1", "key2"]) to obtain a subset of the context.
// Get the entire context store.
const store = AsyncContext.getContext();

// Get a specific context value.
const user = AsyncContext.getContext("user");

// Get a partial context containing selected keys.
const partialStore = AsyncContext.getMultiContext(["user", "session"]);

Running Tests

To run the tests locally:

  1. Clone the repository.
  2. Install the dependencies using npm install or yarn install.
  3. Run the tests using npm test or yarn test.

Contributing

Contributions, issues, and feature requests are welcome! Please open an issue or submit a pull request on GitHub.