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

async-hooks-context

v4.0.1

Published

Creating request-scoped contexts using the async_hooks lib

Downloads

180

Readme

Async Hooks Context

NPM Version Build Status Quality Gate Status Bugs Code Smells Coverage

This library is designed to create a request scoped context, in which any data about the request can be stored. This was initially created to store a request scoped correlation ID for logging purposes but exposes more generic functionality to allow it to be used for multiple purposes. The library is powered by the native async_hooks lib.

Getting Started

This is how to get a copy of this working locally. The only requirement is that Node is installed on the base machine.

$ git clone [email protected]:ToeFungi/async-hooks-context.git
$ cd async-hooks-context
$ npm i

Installation

Install this Async Hooks Context library via npm.

$ npm i --save async-hooks-context

Usage

To ensure that the context is available, it should be created at the top level of your function.

Correlation IDs - Get / Set

Call the setCorrelationId() function in the global or parent scope. This will generate a new UUID v4 string as the correlation ID for all downstream scopes and become available in all child calls. In some cases, the function might need to be wrapped to ensure the context is set correctly.

import { getCorrelationId, setCorrelationId } from './AsyncHooksContext'

// Generates a new UUID v4 string and sets it to the request scope as the correlation ID
setCorrelationId()

const foo = () => {
  // Returns the correlation ID for the request scope or undefined if not set
  return getCorrelationId()
}

// UUID v4 string
console.log(foo())

In the case that you want to set the correlation ID based on an incoming object, the top level function should be wrapped and the correlation ID set within the wrapper. This ensures that the context is available in the scope of the child function.

import { getCorrelationId, setCorrelationId } from './AsyncHooksContext'

const data = {
  correlationId: 'xxxx-xxxx-xxxx-xxxx'
}

const foo = (data: object) => {
  return getCorrelationId()
}

// Wrapper to ensure that the context is in the proper scope for the child function
const wrapper = (data: object) => {
  // Explicitly set a correlation ID
  setCorrelationId(data.correlationId)
  return foo(data)
}

// UUID v4 string
console.log(wrapper(data))

Context Objects - Get / Set

Call the updateRequestContext() function in the global or parent scope and pass in the object that should be stored. This will create or update the existing context with the new data provided. An object that is passed with the same keys will overwrite duplicate keys. The correlation ID will also be available in the returned context.

import { getRequestContext, updateRequestContext } from './AsyncHooksContext'

const data = {
  meta: 'data'
}

const foo = () => {
  // Returns the current request scoped context
  return getRequestContext()
}

// Wrapper to ensure that the context is in the proper scope for the child function
const wrapper = (data: object) => {
  // Pass the data required in the context, into the update function
  updateRequestContext(data)
  return foo()
}

console.log(wrapper(data))

Cleaning Up

When the request ends, the context will be removed from the current stored contexts. If however, a use case arises in which the current request context needs to be removed, call the cleanContext() function.

import { cleanContext, getCorrelationId, setCorrelationId } from './AsyncHooksContext'

const foo = () => {
  console.log(getCorrelationId()) // UUID v4 string
  cleanContext()
  console.log(getCorrelationId()) // undefined
}

const wrapper = () => {
  setCorrelationId()
  return foo()
}

Running Tests

To run tests, you should be able to simply run be able to run

$ npm run test
$ npm run coverage

Testing tools that are used includes Mocha, Chai, Sinon, and nyc.

Contributions

This project is completely open source and as such, you are invited to make contributions. Fork the project, make some changes and make the pull request. Should you have any feedback regarding the functionality, please don't hesitate to open an issue so this can be resolved. Please ensure that any pull requests have unit tests that cover any additional functionality.

License

MIT License

Copyright (c) 2021 Alex Pickering