@clipboard-health/execution-context 0.1.0
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/README.md +82 -0
- package/package.json +20 -0
- package/src/index.d.ts +3 -0
- package/src/index.js +7 -0
- package/src/index.js.map +1 -0
- package/src/lib/contextStore.d.ts +14 -0
- package/src/lib/contextStore.js +62 -0
- package/src/lib/contextStore.js.map +1 -0
- package/src/types/types.d.ts +11 -0
- package/src/types/types.js +3 -0
- package/src/types/types.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# @clipboard-health/execution-context <!-- omit from toc -->
|
|
2
|
+
|
|
3
|
+
`ExecutionContext` is a lightweight Node.js package built with TypeScript that leverages `AsyncLocalStorage` to create a statically available context parallel to any execution. It provides a reliable, thread-safe context for attaching and accessing metadata throughout the lifecycle of an execution, such as API requests, background jobs, or message consumers. This allows various parts of your application to communicate and share metadata without needing to explicitly pass context objects.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Scoped Contexts**: Attach data to a context that is specific to each execution scope.
|
|
8
|
+
- **Static Access**: Access context data statically anywhere in the codebase within an active execution.
|
|
9
|
+
- **Metadata Aggregation**: Store and retrieve metadata across the lifespan of an execution, ideal for logging, tracing, and other observability tasks.
|
|
10
|
+
|
|
11
|
+
## Table of Contents <!-- omit from toc -->
|
|
12
|
+
|
|
13
|
+
- [Install](#install)
|
|
14
|
+
- [Usage](#usage)
|
|
15
|
+
- [Local development commands](#local-development-commands)
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @clipboard-health/execution-context
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
This example demonstrates how to create a logging context, accumulate metadata from various function calls, and then log a single message containing all the gathered metadata.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
// ./examples/executionContext.ts
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
addMetadataToLocalContext,
|
|
32
|
+
getExecutionContext,
|
|
33
|
+
newExecutionContext,
|
|
34
|
+
runWithExecutionContext,
|
|
35
|
+
} from "@clipboard-health/execution-context";
|
|
36
|
+
|
|
37
|
+
export async function processRequest() {
|
|
38
|
+
// Start a context for this request
|
|
39
|
+
await runWithExecutionContext(newExecutionContext("context-name"), async () => {
|
|
40
|
+
const context = getExecutionContext();
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
// Add metadata from the current function
|
|
44
|
+
addMetadataToLocalContext({ userId: "1" });
|
|
45
|
+
|
|
46
|
+
// Simulate calling other functions that add their own context metadata
|
|
47
|
+
callFunctionThatAddsContext();
|
|
48
|
+
callFunctionThatCallsAnotherFunctionThatAddsContext();
|
|
49
|
+
|
|
50
|
+
// Log the successful processing event with accumulated metadata
|
|
51
|
+
console.log("event=MessageProcessed", { ...context?.metadata });
|
|
52
|
+
} catch (error) {
|
|
53
|
+
// Capture and log error metadata if something goes wrong
|
|
54
|
+
addMetadataToLocalContext({ error });
|
|
55
|
+
console.error("event=MessageProcessed", { ...context?.metadata });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Example function that adds its own metadata to the current context
|
|
61
|
+
function callFunctionThatAddsContext() {
|
|
62
|
+
addMetadataToLocalContext({ operation: "dataFetch", status: "success" });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Example function that calls another function, both adding their own metadata
|
|
66
|
+
function callFunctionThatCallsAnotherFunctionThatAddsContext() {
|
|
67
|
+
addMetadataToLocalContext({ operation: "validate", validationStep: "pre-check" });
|
|
68
|
+
callAnotherFunctionThatAddsContext();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function callAnotherFunctionThatAddsContext() {
|
|
72
|
+
addMetadataToLocalContext({
|
|
73
|
+
operation: "validate",
|
|
74
|
+
validationStep: "post-check",
|
|
75
|
+
result: "passed",
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Local development commands
|
|
81
|
+
|
|
82
|
+
See [`package.json`](./package.json) `scripts` for a list of commands.
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@clipboard-health/execution-context",
|
|
3
|
+
"description": "A lightweight Node.js utility for managing execution contexts and metadata aggregation using AsyncLocalStorage.",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"tslib": "2.8.0"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"main": "./src/index.js",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"embed": "embedme README.md"
|
|
16
|
+
},
|
|
17
|
+
"type": "commonjs",
|
|
18
|
+
"typings": "./src/index.d.ts",
|
|
19
|
+
"types": "./src/index.d.ts"
|
|
20
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./lib/contextStore"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./types/global"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./types/types"), exports);
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/execution-context/src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAmC;AACnC,yDAA+B;AAC/B,wDAA8B"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ExecutionContext } from "../types/types";
|
|
2
|
+
export declare function getExecutionContext(): ExecutionContext | undefined;
|
|
3
|
+
export declare function runWithExecutionContext<T = void>(context: ExecutionContext, callback: () => Promise<T>): Promise<T>;
|
|
4
|
+
/**
|
|
5
|
+
* This function is simply a convenience to initialize an empty context object
|
|
6
|
+
* @param source - The class/service that initializes the context
|
|
7
|
+
*/
|
|
8
|
+
export declare function newExecutionContext(source: string): ExecutionContext;
|
|
9
|
+
/**
|
|
10
|
+
* A utility function that will add metadata to the current context
|
|
11
|
+
* @param metadata - the metadata (key-value pair), to be added to the context
|
|
12
|
+
*/
|
|
13
|
+
export declare function addMetadataToLocalContext(metadata: Record<string, unknown>): void;
|
|
14
|
+
export declare function addToMetadataList(key: string, metadata: Record<string, unknown>): void;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getExecutionContext = getExecutionContext;
|
|
4
|
+
exports.runWithExecutionContext = runWithExecutionContext;
|
|
5
|
+
exports.newExecutionContext = newExecutionContext;
|
|
6
|
+
exports.addMetadataToLocalContext = addMetadataToLocalContext;
|
|
7
|
+
exports.addToMetadataList = addToMetadataList;
|
|
8
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
9
|
+
globalThis.threadLocalStorage ||= new node_async_hooks_1.AsyncLocalStorage();
|
|
10
|
+
function getAsyncLocalStorage() {
|
|
11
|
+
return globalThis.threadLocalStorage;
|
|
12
|
+
}
|
|
13
|
+
function getExecutionContext() {
|
|
14
|
+
return getAsyncLocalStorage().getStore();
|
|
15
|
+
}
|
|
16
|
+
async function runWithExecutionContext(context, callback) {
|
|
17
|
+
return await new Promise((resolve, reject) => {
|
|
18
|
+
getAsyncLocalStorage().run(context, () => {
|
|
19
|
+
try {
|
|
20
|
+
Promise.resolve(callback()).then(resolve).catch(reject);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
reject(error);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* This function is simply a convenience to initialize an empty context object
|
|
30
|
+
* @param source - The class/service that initializes the context
|
|
31
|
+
*/
|
|
32
|
+
function newExecutionContext(source) {
|
|
33
|
+
return {
|
|
34
|
+
source,
|
|
35
|
+
metadata: {},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A utility function that will add metadata to the current context
|
|
40
|
+
* @param metadata - the metadata (key-value pair), to be added to the context
|
|
41
|
+
*/
|
|
42
|
+
function addMetadataToLocalContext(metadata) {
|
|
43
|
+
const context = getExecutionContext();
|
|
44
|
+
if (context) {
|
|
45
|
+
context.metadata = { ...context.metadata, ...metadata };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function getMetadataListByKey(key) {
|
|
49
|
+
const context = getExecutionContext();
|
|
50
|
+
if (context?.metadata && key in context.metadata) {
|
|
51
|
+
const metadataForKey = context.metadata[key];
|
|
52
|
+
if (Array.isArray(metadataForKey)) {
|
|
53
|
+
return metadataForKey;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
function addToMetadataList(key, metadata) {
|
|
59
|
+
const metadataList = [...getMetadataListByKey(key), metadata];
|
|
60
|
+
addMetadataToLocalContext({ [key]: metadataList });
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=contextStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contextStore.js","sourceRoot":"","sources":["../../../../../packages/execution-context/src/lib/contextStore.ts"],"names":[],"mappings":";;AAUA,kDAEC;AAED,0DAaC;AAMD,kDAKC;AAMD,8DAKC;AAcD,8CAGC;AAlED,uDAAqD;AAIrD,UAAU,CAAC,kBAAkB,KAAK,IAAI,oCAAiB,EAAE,CAAC;AAE1D,SAAS,oBAAoB;IAC3B,OAAO,UAAU,CAAC,kBAAkB,CAAC;AACvC,CAAC;AAED,SAA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AAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,SAAgB,iBAAiB,CAAC,GAAW,EAAE,QAAiC;IAC9E,MAAM,YAAY,GAAG,CAAC,GAAG,oBAAoB,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC9D,yBAAyB,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type Metadata = Record<string, unknown>;
|
|
2
|
+
export interface ExecutionContext {
|
|
3
|
+
/**
|
|
4
|
+
* The class/file/service that originated the thread that owns this context
|
|
5
|
+
*/
|
|
6
|
+
source: string;
|
|
7
|
+
/**
|
|
8
|
+
* Additional contextual information associated with this execution context
|
|
9
|
+
*/
|
|
10
|
+
metadata: Metadata;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../packages/execution-context/src/types/types.ts"],"names":[],"mappings":""}
|