cntx 0.0.3 → 0.0.5
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 +16 -10
- package/package.json +15 -12
- package/readme.md +25 -9
package/lib/index.js
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.cntx =
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
exports.cntx = cntx;
|
|
4
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
5
|
+
const context = Symbol('context');
|
|
6
|
+
let hook;
|
|
6
7
|
function enableContext() {
|
|
7
|
-
(0, node_async_hooks_1.createHook)({
|
|
8
|
-
init
|
|
9
|
-
|
|
8
|
+
hook ?? (hook = (0, node_async_hooks_1.createHook)({
|
|
9
|
+
init(_asyncId, _type, _triggerAsyncId, resource) {
|
|
10
|
+
const parentResource = (0, node_async_hooks_1.executionAsyncResource)();
|
|
10
11
|
if (parentResource) {
|
|
11
12
|
resource[context] = parentResource[context];
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
|
-
})
|
|
15
|
+
}));
|
|
16
|
+
hook.enable();
|
|
15
17
|
}
|
|
16
18
|
function cntx(init) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
const resource = (0, node_async_hooks_1.executionAsyncResource)();
|
|
20
|
+
if (init !== undefined) {
|
|
21
|
+
resource[context] = init;
|
|
22
|
+
}
|
|
23
|
+
else if (resource[context] === undefined) {
|
|
24
|
+
resource[context] = {};
|
|
25
|
+
}
|
|
19
26
|
return resource[context];
|
|
20
27
|
}
|
|
21
|
-
exports.cntx = cntx;
|
|
22
28
|
cntx.enable = enableContext;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cntx",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
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",
|
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
11
|
"build": "tsc",
|
|
12
|
-
"format": "prettier --write
|
|
13
|
-
"
|
|
12
|
+
"format": "prettier --write .",
|
|
13
|
+
"format:check": "prettier --check .",
|
|
14
|
+
"lint": "eslint . --max-warnings=0",
|
|
15
|
+
"test": "npm run build && node --test test/*.test.cjs",
|
|
16
|
+
"check": "npm run lint && npm run format:check && npm test",
|
|
14
17
|
"prepare": "npm run build",
|
|
15
|
-
"prepublishOnly": "npm run
|
|
16
|
-
"preversion": "npm run
|
|
17
|
-
"version": "npm run format && git add -A src",
|
|
18
|
-
"postversion": "git push && git push --tags"
|
|
18
|
+
"prepublishOnly": "npm run check",
|
|
19
|
+
"preversion": "npm run check"
|
|
19
20
|
},
|
|
20
21
|
"keywords": [
|
|
21
22
|
"async",
|
|
@@ -31,12 +32,14 @@
|
|
|
31
32
|
"type": "git",
|
|
32
33
|
"url": "git+https://github.com/amir-s/cntx.git"
|
|
33
34
|
},
|
|
34
|
-
"
|
|
35
|
+
"packageManager": "yarn@1.22.22",
|
|
35
36
|
"homepage": "https://github.com/amir-s/cntx#readme",
|
|
36
37
|
"devDependencies": {
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
38
|
+
"@eslint/js": "^10.0.1",
|
|
39
|
+
"@types/node": "^22.20.5",
|
|
40
|
+
"eslint": "^10.15.0",
|
|
41
|
+
"prettier": "^3.10.1",
|
|
42
|
+
"typescript": "~5.9.3",
|
|
43
|
+
"typescript-eslint": "^8.73.0"
|
|
41
44
|
}
|
|
42
45
|
}
|
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,7 +18,7 @@ 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
24
|
import { cntx } from 'cntx';
|
|
@@ -24,7 +26,7 @@ import { cntx } from 'cntx';
|
|
|
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
|
|
@@ -61,14 +63,28 @@ const server = http
|
|
|
61
63
|
.listen(3000, 'localhost');
|
|
62
64
|
```
|
|
63
65
|
|
|
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`
|
|
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` function) function without passing it as a parameter.
|
|
65
67
|
|
|
66
68
|
## API
|
|
67
69
|
|
|
68
70
|
### `cntx(context)`
|
|
69
71
|
|
|
70
|
-
- `context` -
|
|
72
|
+
- `context` - The value to store, usually an object. Values such as `false`, `0`, `''`, and `null` are preserved. Omitting the argument or passing `undefined` returns the current context, creating an empty object if none exists.
|
|
73
|
+
|
|
74
|
+
Child async operations inherit the same context value. Initialize a fresh object for each request before starting its async work; mutating an inherited object also affects other operations sharing that object.
|
|
71
75
|
|
|
72
76
|
### `cntx.enable()`
|
|
73
77
|
|
|
74
|
-
Enables async hooks.
|
|
78
|
+
Enables async hooks. Call this at the beginning of your program. Repeated calls reuse the same hook.
|
|
79
|
+
|
|
80
|
+
## Development
|
|
81
|
+
|
|
82
|
+
Use Node.js 22.13+ and Yarn 1.22.22. CI checks Node.js 22, 24, and 26.
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
yarn install --frozen-lockfile
|
|
86
|
+
yarn run check # lint, formatting, build, and regression tests
|
|
87
|
+
yarn audit # check development dependencies for known vulnerabilities
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Run `yarn format` to format source, tests, and configuration. The published package has no runtime dependencies.
|