Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-execution-context

odedglas26.5kISC3.1.0TypeScript support: included

Provides execution context wrapper for node JS, can be used to create execution wrapper for handling requests and more

node, context, execution-context, async-hooks

readme

node-execution-context

A straightforward library that provides a persistent process-level context wrapper using node "async_hooks" feature. This library will try to use by default AsyncLocalStorage implementation based if current node version supports it, otherwise it will fallback to raw async_hooks implementation for lower versions which mimics this behaviour.

Installation

npm i node-execution-context

Getting started

Let's start with creating the context initialisation point of our app, we will take a simple express app for this example

// main.js

const express = require('express');
const Context = require('node-execution-context');
const UserController = require('./controllers/user');
const app = express();
const port = 3000;

const ContextMiddleware = (req, res, next) => {
    Context.run(next, { reference: Math.random() });
};

app.use('/', ContextMiddleware);
app.post('/user', UserController.create);

app.listen(port);

This will expose any point of your code form this point that handles that request.

// ./controller/user/index.js

const Context = require('node-execution-context');
const mongo = require('../service/mongo');
const logger = require('../service/logger');

export class UserController {
    async create (req) {
        const { user } = req.body;

        // This will return the reference number set by out ContextMiddleware (generated by Math.random())
        const { reference } = Context.get();

        logger.info('Created user for reference: ', reference);

        return await mongo.create('user', user);
    }
}

API

run(fn: Function, context: unknown)

Runs given callback that will be exposed to the given context. The context will be exposed to all callbacks and promise chains triggered from the given fn.

get()

Gets the current asynchronous execution context.

The get result is returned by reference meaning if you wish any immutability applied, it will have to be manually applied.

This API may throw CONTEXT_DOES_NOT_EXIST error if accessed without initializing the context properly.

set(context: unknown)

Sets the current asynchronous execution context to given value.

This API may throw CONTEXT_DOES_NOT_EXIST error if accessed without initializing the context properly.

create(context?: unknown)

Creates a given context for the current asynchronous execution. It is recommended to use the run method. This method should be used in special cases in which the run method cannot be used directly.

Note that if this method will be called not within a AsyncResource context, it will effect current execution context and should be used with caution.

Example

const Context = require('node-execution-context');

// context-events.js
const context = { id: Math.random() };

emitter.on('contextual-event', () => {
  Context.create(context);
});

// service.js
emitter.on('contextual-event', () => {
  Context.get(); // Returns { id: random }
});

// main.js
emitter.emit('contextual-event');

Context.get(); // Returns { id: random }

configure(config: ExecutionContextConfig) : void

Configures execution context settings.

Relevant only for node versions lower than v12.17.0.

monitor(): ExecutionMapUsage

Returns an monitoring report over the current execution map resources

Relevant only for node versions lower than v12.17.0.

Before calling monitor, you should configure execution context to monitor it's nodes. by default the data kept is as possible.

Example

const Context = require('node-execution-context');

// Startup
Context.configure({ monitor: true });

// Later on
const usage = Context.monitor();
console.log(usage); // Prints execution context usage report.

Raw API Usage

const Context = require('node-execution-context');

Context.create({
    value: true
});

Promise.resolve().then(() => {
    console.log(Context.get()); // outputs: {"value": true}

    Context.set({
        value: false
    });

    return new Promise((resolve) => {
        setTimeout(() => {
            console.log(Context.get()); // outputs: {"value": false}

            Context.set({
                butter: 'fly'
            });

            process.nextTick(() => {
                console.log(Context.get()); // outputs: {"butter": 'fly'}
                resolve();
            });

        }, 1000);

        console.log(Context.get()); // outputs: {"value": false}
    });
});

The following errors can be thrown while accessing to the context API :

Code When
CONTEXT_DOES_NOT_EXIST When attempting to get / set a context, that has not yet been created.
MONITOR_MISS_CONFIGURATION When try to monitor without calling configure with monitoring option.

changelog

CHANGELOG

3.1.0 (February 12, 2021)

  • Add Context.exists API to validate context presence before accessing it.

3.0.2 (February 12, 2021)

  • Add update API for ease usage in case context is a plain object.

3.0.1 (February 12, 2021)

  • Better types.d.

3.0.0 (January 03, 2021)

  • Introduces AsyncLocaleStorage based implementation for node 12.7.0 and above.
  • AsyncHooksContext domains are now created by default under the hood and no longer require consumers to supply a name.
  • update API changed to set.
  • create API no longer treatscontext as JS objects.

2.0.8 (December 20, 2020)

  • Better reporting, safe child getters.

2.0.7 (December 20, 2020)

  • Preferring setImmediate over nextTick.

2.0.6 (December 2, 2020)

  • Better types definition for get.

2.0.5 (October 27, 2020)

  • publish.

2.0.4 (October 27, 2020)

  • Versions.

2.0.3 (October 27, 2020)

Improvements

  • Domains error logging enhancement.

2.0.2 (October 27, 2020)

Bug Fixes

  • Domains root context fetching, extracted to private getRootContext.

2.0.1 (September 22, 2020)

Improvements

  • Update changelog.

2.0.0 (September 19, 2020)

New features

  • Domains - allow creating a domain under a certain context to split the chain into a standalone context root (parent chain will not depend on its completion for release).
  • Configurations settings.

Improvements

  • Favoring direct assign over spread calls.
  • Monitor now is controlled by config, will not enrich execution context nodes by default.