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

context-inject

v0.0.5

Published

Inject values and access them from nested scopes in async context.

Readme

context-inject

Inject values from a parent scope into child scopes.

Motivation

You have an object you need to pass into a deep call stack. It can be a singleton or a resource, like a database, a browser for automated scraping, some commandline flag a token for an api, or any other value that you get early and is needed later on.

The common way to handle this is either by passing the value down the call stack - a safe and readable approach but not too ergonomic, as it results in signature duplication (i.e. the same parameters are written again and again along the call stack), or using a global variable is set - which is unsafe, hard to test (because you can't mock it for testing) and limits your ability to use the same code in different contexts.

This library solves this problem if you're working in an async context. By leveraging the NodeJS async_hooks API, it lets you wrap any async function with an injector, and everything within the execution of this function will have access to the value using a global getter function.

While the getter itself is global, the value it gets varies according to what was injected in this specific call stack.

So you get the flexibility of a global variable, with the locality of a value passed down a specific stack.

Installation

node:

npm i async-context

deno:

import { context } from "https://deno.land/x/context_inject/src/index.ts";

example

type MyDatabase = {}; // Some object.

const { inject, access } = context((): MyDatabase => {
  throw new Error("not injected!");
});

const g = () => {
  // Arbitrarily complex deep nested code...
  // Suddenly you need to access the db. Great!
  const dbInstance = access();
  // Do something with it...
};

const f = () => {
  // Arbitrarily complex deep nested code...
  return g();
};

// In production

const myDb: MyDatabase = await initDb();
const fWithDbAccess = inject(() => myDb)(f);
await fWithDbAccess();

// In your test

const myDb: MyDatabase = await mockDbForTesting();
const fWithMockDb = inject(() => myDb)(f);
await fWithMockDb();

// If you forget to inject, you will get an explicit exception.
await f(); // throws new Error("not injected!")