starta.microservice 0.1.5220 → 0.1.5223
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.
|
@@ -1,26 +1,58 @@
|
|
|
1
1
|
import ServerLogger from '../adapters/logger';
|
|
2
2
|
/**
|
|
3
|
-
* Class for managing
|
|
4
|
-
* This allows each asynchronous operation
|
|
3
|
+
* Class for managing execution context using AsyncLocalStorage.
|
|
4
|
+
* This allows each asynchronous operation (like HTTP requests in a server)
|
|
5
|
+
* to have its own isolated context for logging and other data.
|
|
5
6
|
*/
|
|
6
7
|
declare class ExecutionContext {
|
|
7
8
|
private asyncLocalStorage;
|
|
9
|
+
/**
|
|
10
|
+
* Sets the correlation ID in the current context.
|
|
11
|
+
* Additionally updates the logger with this new correlation ID.
|
|
12
|
+
* @param {string} correlationId - The correlation ID to be set.
|
|
13
|
+
*/
|
|
8
14
|
setCorrelationId(correlationId: string): void;
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves the correlation ID from the current context.
|
|
17
|
+
* @returns {string | undefined} The correlation ID, if present.
|
|
18
|
+
*/
|
|
9
19
|
getCorrelationId(): string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Sets the authorized user's login in the current context.
|
|
22
|
+
* Also updates the logger with this user information.
|
|
23
|
+
* @param {string} authorizedUserLogin - The authorized user's login.
|
|
24
|
+
*/
|
|
10
25
|
setAuthorizedUserLogin(authorizedUserLogin: string): void;
|
|
26
|
+
/**
|
|
27
|
+
* Retrieves the authorized user's login from the current context.
|
|
28
|
+
* @returns {string | undefined} The user's login, if present.
|
|
29
|
+
*/
|
|
11
30
|
getAuthorizedUserLogin(): string | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Sets the logger in the current context.
|
|
33
|
+
* Throws an error if the provided logger is not an instance of ServerLogger.
|
|
34
|
+
* @param {ServerLogger} logger - The logger to set in the context.
|
|
35
|
+
* @returns {ServerLogger} The logger instance.
|
|
36
|
+
*/
|
|
12
37
|
private setLogger;
|
|
13
38
|
/**
|
|
14
|
-
*
|
|
39
|
+
* Retrieves the logger from the current context.
|
|
15
40
|
* If no logger is found, a new ServerLogger is created, set, and returned.
|
|
16
41
|
* @returns {ServerLogger} The logger instance for the current context.
|
|
17
42
|
*/
|
|
18
43
|
getLogger(): ServerLogger;
|
|
44
|
+
/**
|
|
45
|
+
* Adds additional parameters to the current logger.
|
|
46
|
+
* Useful for adding dynamic data to the logger.
|
|
47
|
+
* @param {Object} params - Additional parameters to add to the logger.
|
|
48
|
+
* @returns {ServerLogger} The updated logger instance.
|
|
49
|
+
*/
|
|
19
50
|
addLoggerParams(params: Object): ServerLogger;
|
|
20
51
|
/**
|
|
21
|
-
* Runs a
|
|
52
|
+
* Runs a function within a new asynchronous context.
|
|
22
53
|
* A new context (store) is created for this function's execution.
|
|
23
54
|
* @param {() => Promise<void>} fn - The function to execute in the new context.
|
|
55
|
+
* @returns {Promise<void>} A promise that resolves when the function completes.
|
|
24
56
|
*/
|
|
25
57
|
runNewContext(fn: () => Promise<void>): Promise<void>;
|
|
26
58
|
/**
|
|
@@ -6,39 +6,65 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const async_hooks_1 = require("async_hooks");
|
|
7
7
|
const logger_1 = __importDefault(require("../adapters/logger"));
|
|
8
8
|
/**
|
|
9
|
-
* Class for managing
|
|
10
|
-
* This allows each asynchronous operation
|
|
9
|
+
* Class for managing execution context using AsyncLocalStorage.
|
|
10
|
+
* This allows each asynchronous operation (like HTTP requests in a server)
|
|
11
|
+
* to have its own isolated context for logging and other data.
|
|
11
12
|
*/
|
|
12
13
|
class ExecutionContext {
|
|
13
14
|
constructor() {
|
|
15
|
+
// Instance of AsyncLocalStorage to maintain separate data stores for each async operation.
|
|
14
16
|
this.asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();
|
|
15
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Sets the correlation ID in the current context.
|
|
20
|
+
* Additionally updates the logger with this new correlation ID.
|
|
21
|
+
* @param {string} correlationId - The correlation ID to be set.
|
|
22
|
+
*/
|
|
16
23
|
setCorrelationId(correlationId) {
|
|
17
24
|
const store = this.ensureStore();
|
|
18
25
|
store.set('correlationId', correlationId);
|
|
19
26
|
this.setLogger(this.getLogger().withParams({ correlationId }));
|
|
20
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves the correlation ID from the current context.
|
|
30
|
+
* @returns {string | undefined} The correlation ID, if present.
|
|
31
|
+
*/
|
|
21
32
|
getCorrelationId() {
|
|
22
33
|
return this.getContextValue('correlationId');
|
|
23
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Sets the authorized user's login in the current context.
|
|
37
|
+
* Also updates the logger with this user information.
|
|
38
|
+
* @param {string} authorizedUserLogin - The authorized user's login.
|
|
39
|
+
*/
|
|
24
40
|
setAuthorizedUserLogin(authorizedUserLogin) {
|
|
25
41
|
const store = this.ensureStore();
|
|
26
42
|
store.set('authorizedUserLogin', authorizedUserLogin);
|
|
27
43
|
this.setLogger(this.getLogger().withParams({ login: authorizedUserLogin }));
|
|
28
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves the authorized user's login from the current context.
|
|
47
|
+
* @returns {string | undefined} The user's login, if present.
|
|
48
|
+
*/
|
|
29
49
|
getAuthorizedUserLogin() {
|
|
30
50
|
return this.getContextValue('authorizedUserLogin');
|
|
31
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Sets the logger in the current context.
|
|
54
|
+
* Throws an error if the provided logger is not an instance of ServerLogger.
|
|
55
|
+
* @param {ServerLogger} logger - The logger to set in the context.
|
|
56
|
+
* @returns {ServerLogger} The logger instance.
|
|
57
|
+
*/
|
|
32
58
|
setLogger(logger) {
|
|
33
59
|
if (!(logger instanceof logger_1.default)) {
|
|
34
|
-
throw new Error("logger must be
|
|
60
|
+
throw new Error("logger must be an instance of ServerLogger");
|
|
35
61
|
}
|
|
36
62
|
const store = this.ensureStore();
|
|
37
63
|
store.set('logger', logger);
|
|
38
64
|
return logger;
|
|
39
65
|
}
|
|
40
66
|
/**
|
|
41
|
-
*
|
|
67
|
+
* Retrieves the logger from the current context.
|
|
42
68
|
* If no logger is found, a new ServerLogger is created, set, and returned.
|
|
43
69
|
* @returns {ServerLogger} The logger instance for the current context.
|
|
44
70
|
*/
|
|
@@ -46,13 +72,20 @@ class ExecutionContext {
|
|
|
46
72
|
return this.getContextValue('logger')
|
|
47
73
|
?? this.setLogger(new logger_1.default());
|
|
48
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Adds additional parameters to the current logger.
|
|
77
|
+
* Useful for adding dynamic data to the logger.
|
|
78
|
+
* @param {Object} params - Additional parameters to add to the logger.
|
|
79
|
+
* @returns {ServerLogger} The updated logger instance.
|
|
80
|
+
*/
|
|
49
81
|
addLoggerParams(params) {
|
|
50
82
|
return this.setLogger(this.getLogger().withParams(params));
|
|
51
83
|
}
|
|
52
84
|
/**
|
|
53
|
-
* Runs a
|
|
85
|
+
* Runs a function within a new asynchronous context.
|
|
54
86
|
* A new context (store) is created for this function's execution.
|
|
55
87
|
* @param {() => Promise<void>} fn - The function to execute in the new context.
|
|
88
|
+
* @returns {Promise<void>} A promise that resolves when the function completes.
|
|
56
89
|
*/
|
|
57
90
|
runNewContext(fn) {
|
|
58
91
|
return this.asyncLocalStorage.run(new Map(), fn);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/executionContext/index.ts"],"names":[],"mappings":";;;;;AAAA,6CAAgD;AAChD,gEAA8C;AAE9C
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/executionContext/index.ts"],"names":[],"mappings":";;;;;AAAA,6CAAgD;AAChD,gEAA8C;AAE9C;;;;GAIG;AACH,MAAM,gBAAgB;IAAtB;QACE,2FAA2F;QACnF,sBAAiB,GAAG,IAAI,+BAAiB,EAAoB,CAAC;IA6GxE,CAAC;IA3GC;;;;OAIG;IACH,gBAAgB,CAAC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