cntx 0.0.4 → 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 +17 -3
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
|
@@ -63,14 +63,28 @@ const server = http
|
|
|
63
63
|
.listen(3000, 'localhost');
|
|
64
64
|
```
|
|
65
65
|
|
|
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`
|
|
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.
|
|
67
67
|
|
|
68
68
|
## API
|
|
69
69
|
|
|
70
70
|
### `cntx(context)`
|
|
71
71
|
|
|
72
|
-
- `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.
|
|
73
75
|
|
|
74
76
|
### `cntx.enable()`
|
|
75
77
|
|
|
76
|
-
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.
|