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.
- package/lib/index.js +1 -1
- package/package.json +1 -1
- 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(
|
|
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
package/readme.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# Async Context For Node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Create global context for async function call chain.
|
|
4
|
+
[](https://badge.fury.io/js/cntx)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
## Purpose
|
|
6
8
|
|
|
7
|
-
|
|
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`
|
|
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
|
-
|
|
21
|
+
To enable async hooks, call `cntx.enable()` at the beginning of your program.
|
|
20
22
|
|
|
21
23
|
```js
|
|
22
|
-
import cntx from
|
|
24
|
+
import { cntx } from 'cntx';
|
|
23
25
|
|
|
24
26
|
cntx.enable();
|
|
25
27
|
```
|
|
26
28
|
|
|
27
|
-
|
|
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
|
-
```
|
|
35
|
-
import cntx from
|
|
36
|
-
import http from
|
|
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(
|
|
61
|
+
await res.end('Yo!');
|
|
60
62
|
})
|
|
61
|
-
.listen(3000,
|
|
63
|
+
.listen(3000, 'localhost');
|
|
62
64
|
```
|
|
63
65
|
|
|
64
|
-
In the code above we use `cntx({requestId:
|
|
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
|
|