cntx 0.0.2 → 0.0.3

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 +11 -11
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.3",
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## Why
4
4
 
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.
5
+ When dealing with nested 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.
6
6
 
7
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.
8
8
 
@@ -19,7 +19,7 @@ npm install cntx --save
19
19
  You'd need to call `cntx.enable()` once at the beginning of your program to enable async hooks.
20
20
 
21
21
  ```js
22
- import cntx from "cntx";
22
+ import { cntx } from 'cntx';
23
23
 
24
24
  cntx.enable();
25
25
  ```
@@ -31,14 +31,14 @@ Calling `cntx()` with no arguments will return the current context.
31
31
 
32
32
  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
33
 
34
- ```js
35
- import cntx from "cntx";
36
- import http from "http";
34
+ ```typescript
35
+ import { cntx } from 'cntx';
36
+ import http from 'http';
37
37
 
38
38
  cntx.enable(); // enable async hooks
39
39
 
40
- const log = (msg) => {
41
- const { requestId } = cntx(); // retrieve the request id from the context
40
+ const log = (msg: string) => {
41
+ const { requestId } = cntx<{ requestId: string }>(); // retrieve the request id from the context
42
42
  console.log(`[${requestId}] ${msg}`);
43
43
  };
44
44
 
@@ -50,18 +50,18 @@ const queryDB = async () => {
50
50
 
51
51
  const server = http
52
52
  .createServer(async function (req, res) {
53
- cntx({ requestId: Math.random() * 1000000 }); // initialize context with request id
53
+ cntx({ requestId: Math.floor(Math.random() * 1000000) }); // initialize context with request id
54
54
 
55
55
  log(`Handling request`);
56
56
  await queryDB(); // call an async fn
57
57
  log(`Done handling request`);
58
58
 
59
- await res.end("Yo!");
59
+ await res.end('Yo!');
60
60
  })
61
- .listen(3000, "localhost");
61
+ .listen(3000, 'localhost');
62
62
  ```
63
63
 
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.
64
+ 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
65
 
66
66
  ## API
67
67