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

contextor

v0.6.1

Published

Package allowing to pass a context along an asynchronous process

Downloads

24

Readme

contextor

contextor is a powerful but simple tool helping you to pass a context along an asynchronous process.

Note that, contextor is build on async hooks available since version 8 of Node.js and still in experimental state as of version 12.

Build Coverage Status Version Downloads Dependencies Dev Dependencies

Here is a simple example with an express request context:

const express = require('express');
const contextualizer = require('./index');

const app = express();
let id = 0;

function getCurrentRequestId() {
  // Retrieve current request id.
  return contextualizer.get('request').id;
}

function logSomething(message) {
  console.log({
    requestId: getCurrentRequestId(),
    message
  });
}

app.use((req, res, next) => {
  req.id = id++;
  // Create a new context and add current request and response objects to it.
  contextualizer.create()
    .set('request', req)
    .set('response', res);
  next();
});

app.use((req, res, next) => {
  logSomething('something');
  next();
});

app.get('/', (req, res) => {
  res.send({
    requestId: req.id,
    contextRequestId: getCurrentRequestId()
  });
  // `requestId` and `contextRequestId` should be the same!
});

app.listen(3000);

Summary

Installation

Run the following command to add the package to your dependencies:

$ npm install --save contextor

Use

// CommonJS
const contextor = require('contextor');

// ES6 modules
import contextor from 'contextor';

Create a context

You can create a context just calling following method:

contextor.create();

This will create a context associated with the current asynchronous resource processing and all its descendants, overriding the one of its ancestors.

Set a value in the current context

You can set a value in the current context:

contextor.set('foo', 'bar');

Get a value in the current context

You can get a value of the current context:

contextor.get('foo');

This will throw a ReferenceError if the key does not exist.

Instead, you can specify a default value in case the key does not exist:

contextor.get('foo', 'bar');

Customize cleaning

Some environment variables are available in order to customize context cleaning:

  • CONTEXTOR_CLEAN_CHECK_CONTEXT_SIZE: number of created contexts before a cleaning is executed (default: 100)
  • CONTEXTOR_CONTEXT_TTL: TTL of contexts in ms; set to 0 to make it infinite (default: 6e4)

Debugging

Contextor create context for async hooks. A bad usage can lead to memory leaks. Function getMemoryUsage has been build to help you investigate that kind of issue:

const { inspect } = require('util');

const memoryUsage = contextor.getMemoryUsage();

console.log(inspect(memoryUsage, {
  compact: false,
  colors: true,
  depth: 6,
}));

Testing

Many npm scripts are available to help testing:

$ npm run {script}
  • check: lint and check unit and integration tests
  • lint: lint
  • lint-fix: try to fix lint automatically
  • test: check unit tests
  • test-coverage: check coverage of unit tests
  • test-debug: debug unit tests
  • test-watch: work in TDD!

Use npm run check to check that everything is ok.

Contributing

If you want to contribute, just fork this repository and make a pull request!

Your development must respect these rules:

  • fast
  • easy
  • light

You must keep test coverage at 100%.

License

MIT