cntx 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/lib/index.js +1 -1
  2. package/package.json +1 -1
  3. package/readme.md +18 -16
package/lib/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cntx = void 0;
4
4
  var node_async_hooks_1 = require("node:async_hooks");
5
- var context = Symbol("context");
5
+ var context = Symbol('context');
6
6
  function enableContext() {
7
7
  (0, node_async_hooks_1.createHook)({
8
8
  init: function (asyncId, type, triggerAsyncId, resource) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cntx",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "create and use context for async functions in node",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
package/readme.md CHANGED
@@ -1,12 +1,14 @@
1
1
  # Async Context For Node
2
2
 
3
- ## Why
3
+ Create global context for async function call chain.
4
+ [![npm version](https://badge.fury.io/js/cntx.svg)](https://badge.fury.io/js/cntx)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
6
 
5
- When dealing with nexted async functions (like in request handlers), it is often necessary to pass context from one function to the next. This can be cumbersome and error prone. You can't use a global variable because it will be overwritten by concurrent requests.
7
+ ## Purpose
6
8
 
7
- With the power of node's async hooks, `cntx` provides a way to store and retrieve context without passing it around as a parameter. With every async function call, the context is automatically passed to the next function in the call chain.
9
+ `cntx` is a lightweight npm package that simplifies the management of context in nested async functions, particularly in scenarios like request handlers. It eliminates the need for passing context as parameters or relying on global variables, which can lead to errors and conflicts in concurrent environments.
8
10
 
9
- `cntx` is meant to be simple and lightweight with easy to use API. If you need multiple namespaces, you can build on top of `cntx` to create your own context manager.
11
+ By utilizing Node's async hooks, `cntx` automatically propagates context to the subsequent async functions in the call chain. It offers an easy-to-use API and can serve as a foundation for building custom context managers with multiple namespaces.
10
12
 
11
13
  ## Installation
12
14
 
@@ -16,29 +18,29 @@ npm install cntx --save
16
18
 
17
19
  ## Usage
18
20
 
19
- You'd need to call `cntx.enable()` once at the beginning of your program to enable async hooks.
21
+ To enable async hooks, call `cntx.enable()` at the beginning of your program.
20
22
 
21
23
  ```js
22
- import cntx from "cntx";
24
+ import { cntx } from 'cntx';
23
25
 
24
26
  cntx.enable();
25
27
  ```
26
28
 
27
- To initialize context, you can use `cntx({key: value})` to initialize context for the current async call chain.
29
+ You can use `cntx({key: value})` to initialize context for the current async call chain.
28
30
  Calling `cntx()` with no arguments will return the current context.
29
31
 
30
32
  ### Example
31
33
 
32
34
  Assume you have a web server that handles requests. You have many function calls (async or not) to deal with the request and you want to associate the logs deep in the call chain with the request id. You can use `cntx` to store the request id in the context and retrieve it in the log function. You can even put the `req` and `res` objects in the context and retrieve them in whatever function you need them.
33
35
 
34
- ```js
35
- import cntx from "cntx";
36
- import http from "http";
36
+ ```typescript
37
+ import { cntx } from 'cntx';
38
+ import http from 'http';
37
39
 
38
40
  cntx.enable(); // enable async hooks
39
41
 
40
- const log = (msg) => {
41
- const { requestId } = cntx(); // retrieve the request id from the context
42
+ const log = (msg: string) => {
43
+ const { requestId } = cntx<{ requestId: string }>(); // retrieve the request id from the context
42
44
  console.log(`[${requestId}] ${msg}`);
43
45
  };
44
46
 
@@ -50,18 +52,18 @@ const queryDB = async () => {
50
52
 
51
53
  const server = http
52
54
  .createServer(async function (req, res) {
53
- cntx({ requestId: Math.random() * 1000000 }); // initialize context with request id
55
+ cntx({ requestId: Math.floor(Math.random() * 1000000) }); // initialize context with request id
54
56
 
55
57
  log(`Handling request`);
56
58
  await queryDB(); // call an async fn
57
59
  log(`Done handling request`);
58
60
 
59
- await res.end("Yo!");
61
+ await res.end('Yo!');
60
62
  })
61
- .listen(3000, "localhost");
63
+ .listen(3000, 'localhost');
62
64
  ```
63
65
 
64
- In the code above we use `cntx({requestId: Math.random() * 1000000})` to initialize the context with a random request id. We then use `cntx()` to retrieve the request id in the `log` (which is used deep in the `queryDB` funciton) function without passing it as a parameter.
66
+ In the code above we use `cntx({requestId: ...})` to initialize the context with a random request id. We then use `cntx()` to retrieve the request id in the `log` (which is used deep in the `queryDB` funciton) function without passing it as a parameter.
65
67
 
66
68
  ## API
67
69