cntx 0.0.1 → 0.0.2
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.d.ts +6 -0
- package/lib/index.js +22 -0
- package/package.json +37 -6
- package/readme.md +74 -0
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cntx = void 0;
|
|
4
|
+
var node_async_hooks_1 = require("node:async_hooks");
|
|
5
|
+
var context = Symbol("context");
|
|
6
|
+
function enableContext() {
|
|
7
|
+
(0, node_async_hooks_1.createHook)({
|
|
8
|
+
init: function (asyncId, type, triggerAsyncId, resource) {
|
|
9
|
+
var parentResource = (0, node_async_hooks_1.executionAsyncResource)();
|
|
10
|
+
if (parentResource) {
|
|
11
|
+
resource[context] = parentResource[context];
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
}).enable();
|
|
15
|
+
}
|
|
16
|
+
function cntx(init) {
|
|
17
|
+
var resource = (0, node_async_hooks_1.executionAsyncResource)();
|
|
18
|
+
resource[context] = init ? init : resource[context] || {};
|
|
19
|
+
return resource[context];
|
|
20
|
+
}
|
|
21
|
+
exports.cntx = cntx;
|
|
22
|
+
cntx.enable = enableContext;
|
package/package.json
CHANGED
|
@@ -1,11 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cntx",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "create and use context for async functions in node",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib/**/*"
|
|
9
|
+
],
|
|
6
10
|
"scripts": {
|
|
7
|
-
"
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
13
|
+
"lint": "tslint -p tsconfig.json",
|
|
14
|
+
"prepare": "npm run build",
|
|
15
|
+
"prepublishOnly": "npm run lint",
|
|
16
|
+
"preversion": "npm run lint",
|
|
17
|
+
"version": "npm run format && git add -A src",
|
|
18
|
+
"postversion": "git push && git push --tags"
|
|
8
19
|
},
|
|
9
|
-
"
|
|
10
|
-
|
|
20
|
+
"keywords": [
|
|
21
|
+
"async",
|
|
22
|
+
"hook",
|
|
23
|
+
"context",
|
|
24
|
+
"async context",
|
|
25
|
+
"async hook",
|
|
26
|
+
"async global context"
|
|
27
|
+
],
|
|
28
|
+
"author": "Amir Saboury",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/amir-s/cntx.git"
|
|
33
|
+
},
|
|
34
|
+
"readme": "README.md",
|
|
35
|
+
"homepage": "https://github.com/amir-s/cntx#readme",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"prettier": "^2.8.8",
|
|
38
|
+
"tslint": "^6.1.3",
|
|
39
|
+
"tslint-config-prettier": "^1.18.0",
|
|
40
|
+
"typescript": "^5.1.3"
|
|
41
|
+
}
|
|
11
42
|
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Async Context For Node
|
|
2
|
+
|
|
3
|
+
## Why
|
|
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.
|
|
6
|
+
|
|
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
|
+
|
|
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.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install cntx --save
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
You'd need to call `cntx.enable()` once at the beginning of your program to enable async hooks.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import cntx from "cntx";
|
|
23
|
+
|
|
24
|
+
cntx.enable();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
To initialize context, you can use `cntx({key: value})` to initialize context for the current async call chain.
|
|
28
|
+
Calling `cntx()` with no arguments will return the current context.
|
|
29
|
+
|
|
30
|
+
### Example
|
|
31
|
+
|
|
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
|
+
|
|
34
|
+
```js
|
|
35
|
+
import cntx from "cntx";
|
|
36
|
+
import http from "http";
|
|
37
|
+
|
|
38
|
+
cntx.enable(); // enable async hooks
|
|
39
|
+
|
|
40
|
+
const log = (msg) => {
|
|
41
|
+
const { requestId } = cntx(); // retrieve the request id from the context
|
|
42
|
+
console.log(`[${requestId}] ${msg}`);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const queryDB = async () => {
|
|
46
|
+
log(`Querying DB`); // log has access to the request id without passing it as a parameter
|
|
47
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
48
|
+
log(`Done querying DB`);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const server = http
|
|
52
|
+
.createServer(async function (req, res) {
|
|
53
|
+
cntx({ requestId: Math.random() * 1000000 }); // initialize context with request id
|
|
54
|
+
|
|
55
|
+
log(`Handling request`);
|
|
56
|
+
await queryDB(); // call an async fn
|
|
57
|
+
log(`Done handling request`);
|
|
58
|
+
|
|
59
|
+
await res.end("Yo!");
|
|
60
|
+
})
|
|
61
|
+
.listen(3000, "localhost");
|
|
62
|
+
```
|
|
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.
|
|
65
|
+
|
|
66
|
+
## API
|
|
67
|
+
|
|
68
|
+
### `cntx(context)`
|
|
69
|
+
|
|
70
|
+
- `context` - An object containing the context to be stored. If no argument is passed, the current context is returned.
|
|
71
|
+
|
|
72
|
+
### `cntx.enable()`
|
|
73
|
+
|
|
74
|
+
Enables async hooks. This needs to be called once at the beginning of your program.
|