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 🙏

© 2025 – Pkg Stats / Ryan Hefner

als-registry

v0.0.0

Published

A lightweight utility for managing multiple AsyncLocalStorage instances in Node.js, simplifying context propagation.

Readme

ALS Registry

A lightweight utility for managing multiple AsyncLocalStorage instances in Node.js. This module simplifies the registration, retrieval, and application of asynchronous contexts, making it easier to propagate context across different parts of your application.

NPM version MIT License

Features

  • Register and manage multiple AsyncLocalStorage instances with unique identifiers.
  • Check for registered contexts.
  • Retrieve current store values from all registered contexts.
  • Apply contexts to existing asynchronous flows.
  • Run callbacks within a new asynchronous execution context with specified context data.
  • Lightweight and dependency-free.

Installation

npm install als-registry

Usage

First, import the necessary functions from the module:

import {
  registerContext,
  isContextRegistered,
  getAllContextData,
  applyContexts,
  runWithContexts
} from 'als-registry';
import { AsyncLocalStorage } from 'node:async_hooks';

// 1. Create and register your AsyncLocalStorage instances
const requestIdContext = new AsyncLocalStorage();
const userContext = new AsyncLocalStorage();

registerContext('requestId', requestIdContext);
registerContext('user', userContext);

runWithContexts(contextData, callback)

Use this to start a new asynchronous execution context. This is ideal for entry points like handling an incoming HTTP request.

async function handleRequest(req, res) {
  const contextData = {
    requestId: req.headers['x-request-id'],
    user: { id: 'user-123', name: 'John Doe' }
  };

  await runWithContexts(contextData, async () => {
    // The context is now set and available in any function called from here
    await processOrder();
    res.send('Request processed');
  });
}

async function processOrder() {
  // You can retrieve the context data anywhere in the call stack
  const allData = getAllContextData();
  console.log(allData); // { requestId: '...', user: { ... } }

  // Or get a specific context's store
  console.log(userContext.getStore()); // { id: 'user-123', name: 'John Doe' }
}

applyContexts(contextData)

Use this when you need to apply context within an existing asynchronous flow, without creating a new top-level execution context. This is useful for middleware or functions that enrich an already established context.

// Middleware that adds authentication data to the context
async function authMiddleware() {
  // Assume some authentication logic here...
  const authData = {
    traceId: 'trace-abc-123'
    // Note: We are not re-applying 'requestId' or 'user' here
  };

  // Create and register the traceId context if it doesn't exist
  if (!isContextRegistered('traceId')) {
      registerContext('traceId', new AsyncLocalStorage());
  }

  applyContexts(authData);

  // Now, any subsequent function in this async flow can access 'traceId'
  await next();
}

getAllContextData()

This function collects the store from every registered AsyncLocalStorage instance and returns them in a single object. This is useful for logging, debugging, or passing the entire context to another system.

function logCurrentContext() {
  const contextData = getAllContextData();
  console.log('Current async context:', contextData);
  // Example output: { requestId: '...', user: { ... }, traceId: '...' }
}

API Reference

registerContext(uid, context)

Registers an AsyncLocalStorage instance with a unique identifier.

  • uid (string): The unique identifier for the context.
  • context (AsyncLocalStorage): The AsyncLocalStorage instance to register.

isContextRegistered(uid)

Checks if a context with the given uid has been registered.

  • uid (string): The unique identifier to check.
  • Returns: boolean

getAllContextData()

Gets the current store value from all registered contexts.

  • Returns: Record<string, any> - An object where keys are the uids and values are the corresponding store values.

applyContexts(contextData)

Enters a new asynchronous state for all matching contexts without creating a new execution boundary.

  • contextData (Record<string, any>): An object where keys are uids and values are the store values to set.

runWithContexts(contextData, callback)

Creates a new asynchronous execution context boundary and runs the callback within it.

  • contextData (Record<string, any>): The context data to set for the callback's execution.
  • callback (() => Promise<T>): The asynchronous function to execute.
  • Returns: Promise<T>

Contributing

Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.