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.
Files changed (3) hide show
  1. package/lib/index.js +16 -10
  2. package/package.json +15 -12
  3. 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 = void 0;
4
- var node_async_hooks_1 = require("node:async_hooks");
5
- var context = Symbol('context');
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: function (asyncId, type, triggerAsyncId, resource) {
9
- var parentResource = (0, node_async_hooks_1.executionAsyncResource)();
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
- }).enable();
15
+ }));
16
+ hook.enable();
15
17
  }
16
18
  function cntx(init) {
17
- var resource = (0, node_async_hooks_1.executionAsyncResource)();
18
- resource[context] = init ? init : resource[context] || {};
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.4",
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 \"src/**/*.ts\"",
13
- "lint": "tslint -p tsconfig.json",
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 lint",
16
- "preversion": "npm run lint",
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
- "readme": "README.md",
35
+ "packageManager": "yarn@1.22.22",
35
36
  "homepage": "https://github.com/amir-s/cntx#readme",
36
37
  "devDependencies": {
37
- "prettier": "^2.8.8",
38
- "tslint": "^6.1.3",
39
- "tslint-config-prettier": "^1.18.0",
40
- "typescript": "^5.1.3"
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` funciton) function without passing it as a parameter.
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` - An object containing the context to be stored. If no argument is passed, the current context is returned.
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. This needs to be called once at the beginning of your program.
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.