@bootloader/context 0.0.1

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/README.md +1 -0
  2. package/index.js +125 -0
  3. package/package.json +19 -0
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # bootjs-context
package/index.js ADDED
@@ -0,0 +1,125 @@
1
+ const { AsyncLocalStorage } = require('async_hooks');
2
+
3
+ const request = require('./request');
4
+
5
+ const crypto = require('crypto');
6
+
7
+ const info = {
8
+ id: 0,
9
+ };
10
+
11
+ class Context {
12
+ constructor() {
13
+ this.asyncLocalStorage = new AsyncLocalStorage();
14
+ }
15
+
16
+ /** Starts a new context for the request */
17
+ run(fn) {
18
+ if (this.asyncLocalStorage.getStore()) {
19
+ return fn(); // Continue using the existing context
20
+ }
21
+ let mp = new Map();
22
+ mp.set('_:id', info.id++);
23
+ return this.asyncLocalStorage.run(mp, async () => {
24
+ return await fn(); // Ensure the async function executes within the context
25
+ });
26
+ }
27
+
28
+ useDefaults({ tenant = '~~~', traceId }) {
29
+ this.setTenant(tenant);
30
+ if (!traceId) {
31
+ traceId = crypto.randomUUID();
32
+ }
33
+ this.setTraceId(traceId);
34
+ return { tenant, traceId };
35
+ }
36
+
37
+ start(...args) {
38
+ let fun = args.find(arg => typeof arg == 'function') || (() => {});
39
+ return (req, res, next) => {
40
+ this.run(async () => {
41
+ let requestContext = request.context(req);
42
+ let tenant = requestContext.headerOrParam('tnt');
43
+ let traceId = requestContext.headerOrParam('x-trace-id');
44
+ await fun(this.useDefaults({ tenant, traceId }));
45
+ next();
46
+ });
47
+ };
48
+ }
49
+
50
+ withRequest(callback) {
51
+ return this.start(callback);
52
+ }
53
+
54
+ init(...args) {
55
+ let fun = args.find(arg => typeof arg == 'function') || (() => {});
56
+ let options = args.find(arg => typeof arg == 'object') || {};
57
+ return this.run(async () => {
58
+ await fun(this.useDefaults(options));
59
+ });
60
+ }
61
+
62
+ /** Set a value in the current request context */
63
+ set(key, value) {
64
+ const store = this.asyncLocalStorage.getStore();
65
+ if (store) store.set(key, value);
66
+ }
67
+
68
+ /** Get a value from the current request context */
69
+ get(key) {
70
+ const store = this.asyncLocalStorage.getStore();
71
+ return store ? store.get(key) : undefined;
72
+ }
73
+
74
+ /** Set the Trace ID */
75
+ setTraceId(traceId) {
76
+ this.set('_:traceId', traceId);
77
+ }
78
+
79
+ /** Get the Trace ID */
80
+ getTraceId() {
81
+ return this.get('_:traceId');
82
+ }
83
+
84
+ /** Set the Tenant ID */
85
+ setTenant(tenant) {
86
+ this.set('_:tenant', tenant);
87
+ }
88
+
89
+ /** Get the Tenant ID */
90
+ getTenant() {
91
+ return this.get('_:tenant');
92
+ }
93
+
94
+ /** Get the ID */
95
+ getId() {
96
+ return this.get('_:id');
97
+ }
98
+
99
+ /** Set the Tenant ID */
100
+ setId(id) {
101
+ this.set('_:id', id);
102
+ }
103
+
104
+ toMap() {
105
+ let tenant = this.getTenant();
106
+ let traceId = this.getTraceId();
107
+ let id = this.getId();
108
+ return { tenant, traceId, id };
109
+ }
110
+
111
+ fromMap(context, fn) {
112
+ this.setTenant(context.tenant);
113
+ this.setTraceId(context.traceId);
114
+ this.setId(context.id);
115
+ //this.asyncLocalStorage.enterWith(store);
116
+ }
117
+
118
+ debug() {
119
+ const store = this.asyncLocalStorage.getStore();
120
+ console.log('Context Store:', store ? Object.fromEntries(store) : 'No active context');
121
+ }
122
+ }
123
+
124
+ // Export a singleton instance
125
+ module.exports = new Context();
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@bootloader/context",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/bootloader/bootjs-context.git"
12
+ },
13
+ "author": "",
14
+ "license": "ISC",
15
+ "bugs": {
16
+ "url": "https://github.com/bootloader/bootjs-context/issues"
17
+ },
18
+ "homepage": "https://github.com/bootloader/bootjs-context#readme"
19
+ }