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

@mondaydotcomorg/node-execution-context

v1.0.4

Published

Persistent execution context allowing you to get/set the context anywhere implemented using async hooks. Can be used to create request level execution context, a stack trace that persists through async resources, or anything else you need to survive the e

Downloads

2,764

Readme

node-execution-context

A simple, straightforward library that allows you to create persistent request-level execution context using the async_hooks module that will be accessible anywhere in the code scoped to the current request you're handling at any given moment.

Table of Contents

Install

$ npm install @mondaydotcomorg/node-execution-context

or with yarn:

$ yarn add @mondaydotcomorg/node-execution-context

Getting Started

Let's create a service that will use our library in order to create and get context.

const contextProvider = require('@mondaydotcomorg/node-execution-context');

function createExecutionContext(contextData) {
    contextProvider.createExecutionContext(contextData);
};

function getExecutionContext() {
  const context = contextProvider.getExecutionContext();
  return context;
};

module.exports = {
  getExecutionContext,
  createExecutionContext
};

Now wherever we want in our code we can pass an object to createExecutionContext and it will be saved and accesible for any async resources descendant from that place forward.

For example let's do this in a middleware that is the first thing that runs on a new request.

const executionContextService = require('services/execution-context-service');

async function authenticationMiddleware(req, res, next) {
    const { accountPermissions } = req.body
    
    executionContextService.createExecutionContext({
      accountPermissions,
      method: req.method,
    });

    next();
}

Now we can use this context later and be certain that the request being handled is the same one for which we are getting our context.

const executionContextService = require('services/execution-context-service');
const eventModelService =  require('services/event-model-service');

async function createNewEvent(eventData) {
    const { accountPermissions } = executionContextService.getExecutionContext();
    eventModelService.createEvent(accountPermissions, eventData);
}

Available Functions

createExecutionContext(contextObject, traceOptions = { enabled: false, initialData: {} })

Creates an execution context identified by the asyncId of the current asyncResource. This will be available anywhere in the execution that is inside the async chain of this resource. Context passed must be an object. You cannot create an execution context twice within the same async resource. If you want to update after creation use set ot update. This check will fail only in non-prod environments for performance purposes.

Optional Params: traceOptions can be passed if you want to set some initial trace data into the trace and have the context add a trace detailing async Id and resource type each time context is updated. If you do not pass this object the trace is never created. This can be used for debugging or for enriching logs, however should not be passed if not needed as this will be added fro every async resource created.

getExecutionContext()

Returns only the context object given as the first param to createExecutionContext.

getExecutionTrace()

Returns only the trace array collected if enabled in traceOptions.

getExecutionData()

Returns entire context data including both context object and trace array.

setExecutionContext(newContext)

Allows you to completly override context saved for current async resource.

updateExecutionContext(contextUpdates)

Allows you update specific keys in the context saved for current async resource.