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

@aviation/context

v0.1.0

Published

A WinterCG-compatible context utility

Downloads

14

Readme

@aviation/context

A WinterCG-compatible context utility

Installation

npm install @aviation/context

Usage

@aviation/context allows you to easily pass a value through to child code without needing to directly pass a variable reference. This can be useful in a few of circumstances. For example:

  • when you have a value that you frequently refer back to, potentially from various places in your application (e.g. information about the current user), or,
  • when you want to be able to pass a variable through a section of code you don't have control over (e.g. through a library or framework).

Behind the scenes, @aviation/context uses the AsyncLocalStorage API. If you're not already familiar with this AsyncLocalStorage API, it has parallels with React's Context API, so you can think of @aviation/context as having similar functionality: you can, at the top of your application, define some context value, and then retrieve that value in some child component, without needing to "prop drill".

@aviation/context

runWithContext(value, callback) and runWithContext(key, value, callback)

The runWithContext function is how you set a context value with @avaition/context. You pass in any value you want to store and it'll be made available to the callback function if it calls the getContext function.

If you need to namespace the context value, you can pass a key parameter to the runWithContext function which will store the context value under that key.

Parameters
  • key: (optional), a symbol
  • value: anything
  • callback: a Function — it can be asynchronous! — which accepts no parameters and returns anything
Return value

runWithContext(value, callback) and runWithContext(key, value, callback) return whatever the callback function returns.

getContext() and getContext(key)

If you used a key parameter when you called runWithContext, you'll want to pass that same key parameter to getContext in order to retrieve that context value.

Parameters
  • key: (optional), a symbol
Return value

The value parameter you gave to runWithContext(value, callback) or runWithContext(key, value, callback).

Security

Note, this library makes no attempt to restrict access to context values (stored with a key or otherwise). If you have malicious code running in your application, you should assume it has access any context values.

Examples

Store information about the currently logged in user with Cloudflare Workers

import { runWithContext, getContext } from "@aviation/context";

interface User {
	id: number;
	name: string;
}

export default {
	async fetch(request, environment, executionContext) {
		const user: User = await getUser(request.headers.get("Cookie"));

		return runWithContext(user, () => {
			// do lots of other stuff
			const user = getContext<User>();
			return new Response(`Hi ${user.name}!`);
		});
	},
};

Chain together multiple contexts with Cloudflare Workers

import { runWithContext, getContext } from "@aviation/context";
import { Toucan } from "toucan-js"; // a Sentry client for Cloudflare Workers

interface User {
	id: number;
	name: string;
}

const sentryContext = Symbol("sentry");
const userContext = Symbol("user");

export default {
	async fetch(request, environment, executionContext) {
		const sentry = new Toucan({ dsn: environment.SENTRY_DSN });

		return runWithContext(sentryContext, sentry, async () => {
			const user: User = await getUser(request.headers.get("Cookie"));

			return runWithContext(userContext, user, () => {
				// do lots of other stuff
				const user = getContext<User>(userContext);

				try {
					throw new Error("an unexpected error! :(");

					return new Response(`Hi ${user.name}`);
				} catch (thrown) {
					const sentry = getContext<Toucan>(sentryContext);
					sentry.setUser({ id: user.id });
					sentry.captureException(thrown);

					return new Response("Something went wrong.", { status: 500 });
				}
			});
		});
	},
};

Use Symbol.for(key) to reference a context by a well-known string rather than with a single shared symbol instance

// entrypoint.ts
import { runWithContext } from "@aviation/context";
import { doStuff } from "./other-file.js";

interface User {
	id: number;
	name: string;
}

export default {
	async fetch(request, environment, executionContext) {
		const user: User = await getUser(request.headers.get("Cookie"));

		return runWithContext(Symbol.for("user"), user, doStuff);
	},
};

// other-file.ts
import { getContext } from "@aviation/context";

export const doStuff = () => {
	const user = getContext<User>(Symbol.for("user"));
	return new Response(`Hi ${user.name}!`);
};