Fork 0
Asynchronous "thread-local storage" for Node.js, using async_hooks
This repository has been archived on 2021-09-16. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Conduitry 9ded47a50d add build infrastructure 2017-06-24 16:44:38 -04:00
src add build infrastructure 2017-06-24 16:44:38 -04:00
.eslintrc.yaml add build infrastructure 2017-06-24 16:44:38 -04:00
.gitignore add build infrastructure 2017-06-24 16:44:38 -04:00
LICENSE Initial commit 2017-06-24 05:00:06 -04:00 rename project 2017-06-24 16:30:25 -04:00
package-lock.json add build infrastructure 2017-06-24 16:44:38 -04:00
package.json add build infrastructure 2017-06-24 16:44:38 -04:00
rollup.config.js add build infrastructure 2017-06-24 16:44:38 -04:00


Contexty is a very simple implementation of a "thread-local context"-esque concept for Node.js, based on asynchronous resources.

For example: Early in handling an HTTP request, you can create a new context. Elsewhere in your code, you can retrieve the current context and get/set values on it. The context is preserved for the duration of that HTTP request, but is kept separate for different HTTP requests.


A current nightly Node.js build, for now. My understanding is that the async_hooks features and fixes that this relies on will be released in Node.js 8.2.0. This section will be updated accordingly as Node.js versions are officially released.


Create an instance of Contexty to create a "namespace" in which you want to share contexts. You should use the same Contexty anywhere you want to have access to the same context. In many applications, you will only need a single Contexty instance, which should be created outside your code which handles requests etc.

let contexty = new Contexty()

When you want to create a new context, retrieve This is a getter which returns a new context (an object with null prototype). Store whatever you want on here. Later in the same or in a descendent asynchronous resource, the contexty.current getter will return that same context object.

Retrieving when there is already an asynchronous context will create a new context with the old one as its prototype, so you have access to all the parent values, but new values you add to the context will not affect the parent context.


new Contexty()

Creates a new object to manage async contexts for a particular purpose.


This getter creates and returns a new context. If a context already existed, the new context will be a child context. You can access values stored on the parent context, and any changes will no longer be accessible once you are out of this asynchronous call tree.


This getter retrieves the context created by the appropriate asynchronously ancestral Contexty#new.


let contexty = new Contexty()

let counter = 0

async function test() { = ++counter
	await sleep(1000)
	setTimeout(test2, 2000)
	await sleep(4000)

function test2() {
	console.log('D', = 'x'
	sleep(1000).then(() => {

for (let i = 0; i < 3; i++) {

function sleep(ms) {
	return new Promise(res => setTimeout(res, ms))


A 1
A 2
A 3
B 1
B 2
B 3
D 1
E x
D 2
E x
D 3
E x
F x
F x
F x
C 1
C 2
C 3


Copyright (c) 2017 Conduitry