Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

contextor

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

contextor

Package allowing to pass a context along an asynchronous process

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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');

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

Keywords

FAQs

Package last updated on 15 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc