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.
Here is a simple example with an express request context:
const express = ;const contextualizer = ; const app = ;let id = 0; { // Retrieve current request id. return contextualizerid;} { console;} app; app; app; app;
Summary
Installation
Run the following command to add the package to your dependencies:
$ npm install --save contextor
Use
// CommonJSconst contextor = ; // ES6 modules;
Create a context
You can create a context just calling following method:
contextor;
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;
Get a value in the current context
You can get a value of the current context:
contextor;
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;
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 = ; const memoryUsage = contextor; console;
Testing
Many npm
scripts are available to help testing:
$ npm run {script}
check
: lint and check unit and integration testslint
: lintlint-fix
: try to fix lint automaticallytest
: check unit teststest-coverage
: check coverage of unit teststest-debug
: debug unit teststest-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%.