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.
Files changed (3) hide show
  1. package/lib/index.js +16 -10
  2. package/package.json +15 -12
  3. 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 = 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.3",
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
@@ -1,12 +1,14 @@
1
1
  # Async Context For Node
2
2
 
3
- ## Why
3
+ Create global context for async function call chain.
4
+ [![npm version](https://badge.fury.io/js/cntx.svg)](https://badge.fury.io/js/cntx)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
6
 
5
- When dealing with nested 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.
7
+ ## Purpose
6
8
 
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.
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` 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.
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
- You'd need to call `cntx.enable()` once at the beginning of your program to enable async hooks.
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
- To initialize context, you can use `cntx({key: value})` to initialize context for the current async call chain.
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` 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.
65
67
 
66
68
  ## API
67
69
 
68
70
  ### `cntx(context)`
69
71
 
70
- - `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.
71
75
 
72
76
  ### `cntx.enable()`
73
77
 
74
- 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.