@rapidrest/core 1.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 (135) hide show
  1. package/LICENSE +23 -0
  2. package/README.rst +37 -0
  3. package/dist/lib/AlertUtils.js +239 -0
  4. package/dist/lib/AlertUtils.js.map +1 -0
  5. package/dist/lib/ApiError.js +34 -0
  6. package/dist/lib/ApiError.js.map +1 -0
  7. package/dist/lib/ClassLoader.js +159 -0
  8. package/dist/lib/ClassLoader.js.map +1 -0
  9. package/dist/lib/FileUtils.js +190 -0
  10. package/dist/lib/FileUtils.js.map +1 -0
  11. package/dist/lib/JWTUtils.js +111 -0
  12. package/dist/lib/JWTUtils.js.map +1 -0
  13. package/dist/lib/Logger.js +28 -0
  14. package/dist/lib/Logger.js.map +1 -0
  15. package/dist/lib/MessagingUtils.js +218 -0
  16. package/dist/lib/MessagingUtils.js.map +1 -0
  17. package/dist/lib/NotificationsUtils.js +52 -0
  18. package/dist/lib/NotificationsUtils.js.map +1 -0
  19. package/dist/lib/OASUtils.js +207 -0
  20. package/dist/lib/OASUtils.js.map +1 -0
  21. package/dist/lib/ObjectFactory.js +302 -0
  22. package/dist/lib/ObjectFactory.js.map +1 -0
  23. package/dist/lib/ObjectUtils.js +49 -0
  24. package/dist/lib/ObjectUtils.js.map +1 -0
  25. package/dist/lib/StringUtils.js +104 -0
  26. package/dist/lib/StringUtils.js.map +1 -0
  27. package/dist/lib/TelemetryUtils.js +159 -0
  28. package/dist/lib/TelemetryUtils.js.map +1 -0
  29. package/dist/lib/UserUtils.js +138 -0
  30. package/dist/lib/UserUtils.js.map +1 -0
  31. package/dist/lib/ValidationUtils.js +130 -0
  32. package/dist/lib/ValidationUtils.js.map +1 -0
  33. package/dist/lib/decorators/ObjectDecorators.js +95 -0
  34. package/dist/lib/decorators/ObjectDecorators.js.map +1 -0
  35. package/dist/lib/decorators/index.js +2 -0
  36. package/dist/lib/decorators/index.js.map +1 -0
  37. package/dist/lib/index.js +18 -0
  38. package/dist/lib/index.js.map +1 -0
  39. package/dist/lib/sleep.js +15 -0
  40. package/dist/lib/sleep.js.map +1 -0
  41. package/dist/lib/threads/ThreadLogger.js +43 -0
  42. package/dist/lib/threads/ThreadLogger.js.map +1 -0
  43. package/dist/lib/threads/ThreadPool.js +270 -0
  44. package/dist/lib/threads/ThreadPool.js.map +1 -0
  45. package/dist/lib/threads/ThreadWorker.js +25 -0
  46. package/dist/lib/threads/ThreadWorker.js.map +1 -0
  47. package/dist/lib/threads/ThreadWorkerEntry.js +77 -0
  48. package/dist/lib/threads/ThreadWorkerEntry.js.map +1 -0
  49. package/dist/lib/threads/index.js +3 -0
  50. package/dist/lib/threads/index.js.map +1 -0
  51. package/dist/types/AlertUtils.d.ts +132 -0
  52. package/dist/types/ApiError.d.ts +13 -0
  53. package/dist/types/ClassLoader.d.ts +87 -0
  54. package/dist/types/FileUtils.d.ts +46 -0
  55. package/dist/types/JWTUtils.d.ts +139 -0
  56. package/dist/types/Logger.d.ts +7 -0
  57. package/dist/types/MessagingUtils.d.ts +77 -0
  58. package/dist/types/NotificationsUtils.d.ts +32 -0
  59. package/dist/types/OASUtils.d.ts +54 -0
  60. package/dist/types/ObjectFactory.d.ts +77 -0
  61. package/dist/types/ObjectUtils.d.ts +17 -0
  62. package/dist/types/StringUtils.d.ts +52 -0
  63. package/dist/types/TelemetryUtils.d.ts +84 -0
  64. package/dist/types/UserUtils.d.ts +61 -0
  65. package/dist/types/ValidationUtils.d.ts +66 -0
  66. package/dist/types/decorators/ObjectDecorators.d.ts +55 -0
  67. package/dist/types/decorators/index.d.ts +1 -0
  68. package/dist/types/index.d.ts +17 -0
  69. package/dist/types/sleep.d.ts +5 -0
  70. package/dist/types/threads/ThreadLogger.d.ts +15 -0
  71. package/dist/types/threads/ThreadPool.d.ts +115 -0
  72. package/dist/types/threads/ThreadWorker.d.ts +41 -0
  73. package/dist/types/threads/ThreadWorkerEntry.d.ts +1 -0
  74. package/dist/types/threads/index.d.ts +2 -0
  75. package/docs/Makefile +20 -0
  76. package/docs/conf.py +58 -0
  77. package/docs/index.rst +17 -0
  78. package/docs/make.bat +35 -0
  79. package/docs/reference/@rapidrest/namespaces/ObjectDecorators/README.md +21 -0
  80. package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Config.md +47 -0
  81. package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Destroy.md +27 -0
  82. package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Init.md +33 -0
  83. package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Inject.md +45 -0
  84. package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Logger.md +27 -0
  85. package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Nullable.md +27 -0
  86. package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Validator.md +39 -0
  87. package/docs/reference/@rapidrest/namespaces/ObjectDecorators/type-aliases/ValidatorFunction.md +31 -0
  88. package/docs/reference/README.md +73 -0
  89. package/docs/reference/classes/AlertUtils.md +158 -0
  90. package/docs/reference/classes/ApiError.md +263 -0
  91. package/docs/reference/classes/ClassLoader.md +224 -0
  92. package/docs/reference/classes/Event.md +109 -0
  93. package/docs/reference/classes/EventUtils.md +123 -0
  94. package/docs/reference/classes/FileUtils.md +193 -0
  95. package/docs/reference/classes/JWTUtils.md +91 -0
  96. package/docs/reference/classes/MessagingUtils.md +145 -0
  97. package/docs/reference/classes/OASUtils.md +213 -0
  98. package/docs/reference/classes/ObjectFactory.md +274 -0
  99. package/docs/reference/classes/ObjectUtils.md +60 -0
  100. package/docs/reference/classes/StringUtils.md +169 -0
  101. package/docs/reference/classes/ThreadPool.md +249 -0
  102. package/docs/reference/classes/ThreadWorker.md +91 -0
  103. package/docs/reference/classes/UserUtils.md +239 -0
  104. package/docs/reference/classes/ValidationUtils.md +294 -0
  105. package/docs/reference/enumerations/AlertPriority.md +61 -0
  106. package/docs/reference/enumerations/JWTUtilsCompressionMethods.md +21 -0
  107. package/docs/reference/enumerations/WorkerMessageType.md +43 -0
  108. package/docs/reference/functions/sleep.md +25 -0
  109. package/docs/reference/interfaces/Alert.md +101 -0
  110. package/docs/reference/interfaces/AlertAttachment.md +51 -0
  111. package/docs/reference/interfaces/AlertClose.md +31 -0
  112. package/docs/reference/interfaces/AlertUtilsAttachmentOptions.md +42 -0
  113. package/docs/reference/interfaces/AlertUtilsOptions.md +41 -0
  114. package/docs/reference/interfaces/InstanceOptions.md +35 -0
  115. package/docs/reference/interfaces/JWTPayload.md +143 -0
  116. package/docs/reference/interfaces/JWTUser.md +55 -0
  117. package/docs/reference/interfaces/JWTUtilsConfig.md +45 -0
  118. package/docs/reference/interfaces/JWTUtilsPayloadKeyOptions.md +68 -0
  119. package/docs/reference/interfaces/JWTUtilsPayloadOptions.md +40 -0
  120. package/docs/reference/interfaces/JWTUtilsPayloadPasswordOptions.md +78 -0
  121. package/docs/reference/interfaces/NewEvent.md +25 -0
  122. package/docs/reference/interfaces/OriginSettings.md +25 -0
  123. package/docs/reference/interfaces/SlackConfig.md +33 -0
  124. package/docs/reference/interfaces/SmtpAuth.md +25 -0
  125. package/docs/reference/interfaces/SmtpConfig.md +41 -0
  126. package/docs/reference/interfaces/Template.md +81 -0
  127. package/docs/reference/interfaces/TemplateMapBase.md +13 -0
  128. package/docs/reference/interfaces/TwilioConfig.md +33 -0
  129. package/docs/reference/interfaces/WorkerMessage.md +31 -0
  130. package/docs/reference/interfaces/WorkerOptions.md +61 -0
  131. package/docs/reference/type-aliases/BooleanFunc.md +23 -0
  132. package/docs/reference/type-aliases/TemplateMap.md +17 -0
  133. package/docs/reference/type-aliases/WorkerCallback.md +31 -0
  134. package/docs/reference/variables/Logger.md +21 -0
  135. package/package.json +96 -0
@@ -0,0 +1,2 @@
1
+ export * as ObjectDecorators from "./ObjectDecorators.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,gBAAgB,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,18 @@
1
+ export * from "./ApiError.js";
2
+ export * from "./AlertUtils.js";
3
+ export * from "./ClassLoader.js";
4
+ export * from "./decorators/index.js";
5
+ export * from "./FileUtils.js";
6
+ export * from "./JWTUtils.js";
7
+ export * from "./Logger.js";
8
+ export * from "./MessagingUtils.js";
9
+ export * from "./ObjectFactory.js";
10
+ export * from "./ObjectUtils.js";
11
+ export * from "./OASUtils.js";
12
+ export * from "./StringUtils.js";
13
+ export * from "./threads/index.js";
14
+ export * from "./sleep.js";
15
+ export * from "./TelemetryUtils.js";
16
+ export * from "./UserUtils.js";
17
+ export * from "./ValidationUtils.js";
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC"}
@@ -0,0 +1,15 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2020-2026 Jean-Philippe Steinmetz
3
+ ///////////////////////////////////////////////////////////////////////////////
4
+ /**
5
+ * Starts a timer to suspend execution for a given number of milliseconds.
6
+ * @param ms The number of milliseconds to suspend. Default is `1`.
7
+ */
8
+ export async function sleep(ms = 1) {
9
+ return await new Promise((resolve) => {
10
+ setTimeout(async () => {
11
+ resolve();
12
+ }, ms);
13
+ });
14
+ }
15
+ //# sourceMappingURL=sleep.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sleep.js","sourceRoot":"","sources":["../../src/sleep.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAC/E;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,KAAa,CAAC;IACtC,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QACjC,UAAU,CAAC,KAAK,IAAI,EAAE;YAClB,OAAO,EAAE,CAAC;QACd,CAAC,EAAE,EAAE,CAAC,CAAC;IACX,CAAC,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,43 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2020-2026 Jean-Philippe Steinmetz
3
+ ///////////////////////////////////////////////////////////////////////////////
4
+ const { parentPort, threadId } = require("worker_threads");
5
+ /**
6
+ * Provides a Winston transport for forwarding logs from this thread to the parent's logger instance.
7
+ *
8
+ * Constructor requires two properties to be set in the `opts` argument:
9
+ * - `parentPort`
10
+ * - `threadId`
11
+ */
12
+ class ThreadLogger {
13
+ log(data) {
14
+ if (typeof data !== "object") {
15
+ data = {
16
+ level: "info",
17
+ message: data,
18
+ };
19
+ }
20
+ // Prefix the thread ID to the message
21
+ data.message = `[Thread-${threadId}]: ${data.message}`;
22
+ // Forward the log to the parent thread
23
+ parentPort?.postMessage({
24
+ type: "_WorkerLog",
25
+ data,
26
+ });
27
+ }
28
+ debug(message) {
29
+ this.log({ level: "debug", message });
30
+ }
31
+ error(message) {
32
+ this.log({ level: "error", message });
33
+ }
34
+ info(message) {
35
+ this.log({ level: "info", message });
36
+ }
37
+ warn(message) {
38
+ this.log({ level: "warn", message });
39
+ }
40
+ }
41
+ module.exports = ThreadLogger;
42
+ export {};
43
+ //# sourceMappingURL=ThreadLogger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ThreadLogger.js","sourceRoot":"","sources":["../../../src/threads/ThreadLogger.js"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAC/E,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAE3D;;;;;;GAMG;AACH,MAAM,YAAY;IACd,GAAG,CAAC,IAAI;QACJ,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC3B,IAAI,GAAG;gBACH,KAAK,EAAE,MAAM;gBACb,OAAO,EAAE,IAAI;aAChB,CAAC;QACN,CAAC;QAED,sCAAsC;QACtC,IAAI,CAAC,OAAO,GAAG,WAAW,QAAQ,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;QAEvD,uCAAuC;QACvC,UAAU,EAAE,WAAW,CAAC;YACpB,IAAI,EAAE,YAAY;YAClB,IAAI;SACP,CAAC,CAAC;IACP,CAAC;IAED,KAAK,CAAC,OAAO;QACT,IAAI,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;IAC1C,CAAC;IAED,KAAK,CAAC,OAAO;QACT,IAAI,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;IAC1C,CAAC;IAED,IAAI,CAAC,OAAO;QACR,IAAI,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,IAAI,CAAC,OAAO;QACR,IAAI,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IACzC,CAAC;CACJ;AAED,MAAM,CAAC,OAAO,GAAG,YAAY,CAAC"}
@@ -0,0 +1,270 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ ///////////////////////////////////////////////////////////////////////////////
11
+ // Copyright (C) 2020-2026 Jean-Philippe Steinmetz
12
+ ///////////////////////////////////////////////////////////////////////////////
13
+ import { Worker } from "worker_threads";
14
+ import os from "os";
15
+ import path from "path";
16
+ import { WorkerMessageType } from "./ThreadWorker.js";
17
+ import { sleep } from "../sleep.js";
18
+ import { Config, Destroy, Logger } from "../decorators/ObjectDecorators.js";
19
+ /**
20
+ * The `ThreadPool` class provides an interface for managing a pool of execution threads that can be used for parallel
21
+ * code execution. `ThreadPool` is a wrapper to the `worker_threads` API to add support for multiple `Worker` instances.
22
+ * By default, the pool will create *n* workers corresponding to the number of CPUs (physical + virtual) on the system.
23
+ *
24
+ * There are two ways to start worker threads with the pool.
25
+ *
26
+ * The first way is to set the `entry` option when calling the `start()` function. This will create a instance of the
27
+ * `worker_threads` APIs `Worker` class with the specified file as the entry point. Note that this file must be of
28
+ * type JavaScript (`.js` extension) as the underlying system does not support loading TypeScript. If the `args` option
29
+ * is set, the value will be passed in as the `workerData` to the entry file.
30
+ *
31
+ * The second way to start a worker thread is by setting the `worker` argument when calling the `start()` function. The
32
+ * specified `worker` file must contain a `default` export. The `default` export must be a class definition which
33
+ * extends the `ThreadWorker` abstract class interface. The file can be either JavaScript or TypeScript. When using
34
+ * this method the `start()` function will return only when all worker instances in each thread of the pool has
35
+ * successfully returned from its `start()` function. When the `args` option is set, the value(s) will be passed in to
36
+ * the constructor on instantiation by the thread executor.
37
+ *
38
+ * When the `restartOnExit` option is specified, the pool will automatically recreate and start a worker thread on
39
+ * the `exit event`.
40
+ *
41
+ * This class exposes worker messages via the `on()` callback handler function. Registering a callback handler via
42
+ * `on()` will propogate all messages from all underlying threads in the pool.
43
+ *
44
+ * @author Jean-Philippe Steinmetz
45
+ */
46
+ export class ThreadPool {
47
+ /** The map of event types to a list of callback functions. */
48
+ callbacks;
49
+ /** The index of the last worker that was assigned work. */
50
+ lastThread;
51
+ /** The instance of Winston logger to forward thread logs to. */
52
+ logger;
53
+ /** The maximum number of threads allowed. */
54
+ maxThreads = os.cpus().length;
55
+ /** The list of active worker threads. */
56
+ workers;
57
+ /** Used to indicate that the pool is shutting down. */
58
+ shutdown;
59
+ /**
60
+ * The maximum number of threads that can be created by the pool.
61
+ */
62
+ get max() {
63
+ return this.maxThreads;
64
+ }
65
+ /**
66
+ * The number of active threads in the pool.
67
+ */
68
+ get size() {
69
+ return this.workers.length;
70
+ }
71
+ /**
72
+ * Creates a new `ThreadPool` instance with the specified defaults.
73
+ *
74
+ * @param max The maximum number of threads to create. Default is `os.cpus().length`.
75
+ * @param logger The Winston logger instance to forward all worker thread logs to.
76
+ */
77
+ constructor(max = 0, logger) {
78
+ this.maxThreads = max || this.maxThreads || os.cpus().length;
79
+ this.lastThread = 0;
80
+ this.logger = logger;
81
+ this.callbacks = new Map();
82
+ this.shutdown = false;
83
+ this.workers = new Array();
84
+ }
85
+ createWorker(idx, options) {
86
+ if (!options.entry || !!options.worker) {
87
+ options.entry = path.join(__dirname, "ThreadWorkerEntry.js");
88
+ }
89
+ const workerOptions = {
90
+ workerData: options && options.worker ? options : options?.args,
91
+ };
92
+ this.logger?.debug(`Creating thread worker: ${JSON.stringify(options)}`);
93
+ const worker = new Worker(options.entry, workerOptions);
94
+ worker.on("error", (error) => {
95
+ const listeners = this.callbacks.get("error");
96
+ if (listeners) {
97
+ for (const callback of listeners) {
98
+ callback(idx, error);
99
+ }
100
+ }
101
+ });
102
+ worker.on("exit", async (code) => {
103
+ const listeners = this.callbacks.get("exit");
104
+ if (listeners) {
105
+ for (const callback of listeners) {
106
+ callback(idx, code);
107
+ }
108
+ }
109
+ // Restart worker thread
110
+ if (options?.restartOnExit && !this.shutdown) {
111
+ const worker = this.createWorker(idx, options);
112
+ this.workers[idx] = worker;
113
+ }
114
+ });
115
+ worker.on("message", (msg) => {
116
+ switch (msg.type) {
117
+ case WorkerMessageType.LOG:
118
+ this.logger?.log(msg.data);
119
+ break;
120
+ case WorkerMessageType.ONLINE:
121
+ {
122
+ const listeners = this.callbacks.get("online");
123
+ if (listeners) {
124
+ for (const callback of listeners) {
125
+ callback(idx);
126
+ }
127
+ }
128
+ }
129
+ break;
130
+ default:
131
+ {
132
+ const listeners = this.callbacks.get("message");
133
+ if (listeners) {
134
+ for (const callback of listeners) {
135
+ callback(idx, msg);
136
+ }
137
+ }
138
+ }
139
+ break;
140
+ }
141
+ });
142
+ return worker;
143
+ }
144
+ /**
145
+ * Initializes the thread pool with the initial worker threads and begins execution.
146
+ *
147
+ * @param entry The path of the entry create a worker thread from. Default is `ThreadWorkerEntry.js`.
148
+ * @param options The options to use when creating the worker thread.
149
+ * @param num The number of initial threads to create, cannot be greater than max. Default is `max`.
150
+ */
151
+ start(options, num = this.max) {
152
+ this.shutdown = false;
153
+ return new Promise((resolve, reject) => {
154
+ let numReady = 0;
155
+ if (!options) {
156
+ options = {};
157
+ }
158
+ options.allowTs = "allowTs" in options ? options.allowTs : true;
159
+ for (let i = 0; i < num && i < this.maxThreads; i++) {
160
+ const worker = this.createWorker(i, options);
161
+ worker.on("online", () => {
162
+ if (options && !options.worker) {
163
+ numReady++;
164
+ }
165
+ // When all workers have finished being created resolve
166
+ if (numReady >= num || numReady >= this.maxThreads) {
167
+ resolve();
168
+ }
169
+ });
170
+ worker.on("message", (msg) => {
171
+ switch (msg.type) {
172
+ case WorkerMessageType.ERROR:
173
+ reject(msg.data);
174
+ break;
175
+ case WorkerMessageType.ONLINE:
176
+ numReady++;
177
+ break;
178
+ }
179
+ // When all workers have finished being created resolve
180
+ if (numReady >= num || numReady >= this.maxThreads) {
181
+ resolve();
182
+ }
183
+ });
184
+ this.workers[i] = worker;
185
+ }
186
+ this.logger?.debug(`Created ${this.workers.length} worker(s)`);
187
+ });
188
+ }
189
+ /**
190
+ * Stops all running thread executions.
191
+ */
192
+ async stop() {
193
+ const listeners = this.callbacks.get("exit");
194
+ this.shutdown = true;
195
+ // Send the stop signal so the workers know to terminate
196
+ this.sendAll({ type: WorkerMessageType.STOP });
197
+ // Give the message above a chance to propogate
198
+ await sleep(10);
199
+ for (let idx = 0; idx < this.workers.length; idx++) {
200
+ const worker = this.workers[idx];
201
+ const exitCode = await worker.terminate();
202
+ if (listeners) {
203
+ for (const callback of listeners) {
204
+ callback(idx, exitCode);
205
+ }
206
+ }
207
+ }
208
+ }
209
+ /**
210
+ * Registers a new callback function to be notified when the given event type is fired.
211
+ *
212
+ * @param type The event type to be notified of. Possible values are: `error`, `exit` and `message`.
213
+ * @param func The callback function to register.
214
+ */
215
+ on(type, func) {
216
+ let listeners = this.callbacks.get(type);
217
+ if (!listeners) {
218
+ listeners = new Array();
219
+ }
220
+ listeners.push(func);
221
+ this.callbacks.set(type, listeners);
222
+ }
223
+ /**
224
+ * Sends the provided message to the next available worker thread. Messages are sent in a round-robin order.
225
+ * @param msg The message to send the next available worker thread.
226
+ */
227
+ send(msg) {
228
+ const startIdx = (this.lastThread + 1) % this.workers.length;
229
+ let worker = undefined;
230
+ do {
231
+ worker = this.workers[startIdx];
232
+ this.lastThread = startIdx;
233
+ } while (!worker);
234
+ worker.postMessage(msg);
235
+ }
236
+ /**
237
+ * Sends the provided message to all worker threads in the pool.
238
+ * @param msg The message to send to all workers.
239
+ */
240
+ sendAll(msg) {
241
+ for (const worker of this.workers) {
242
+ worker.postMessage(msg);
243
+ }
244
+ }
245
+ /**
246
+ * Sends the provided message to the worker thread with the specified id.
247
+ * @param id The id of the thread to send the message to.
248
+ * @param msg The message to send the next available worker thread.
249
+ */
250
+ sendTo(id, msg) {
251
+ if (this.workers[id]) {
252
+ this.workers[id].postMessage(msg);
253
+ }
254
+ }
255
+ }
256
+ __decorate([
257
+ Logger,
258
+ __metadata("design:type", Object)
259
+ ], ThreadPool.prototype, "logger", void 0);
260
+ __decorate([
261
+ Config("thread_pool:max_threads", os.cpus().length),
262
+ __metadata("design:type", Number)
263
+ ], ThreadPool.prototype, "maxThreads", void 0);
264
+ __decorate([
265
+ Destroy,
266
+ __metadata("design:type", Function),
267
+ __metadata("design:paramtypes", []),
268
+ __metadata("design:returntype", Promise)
269
+ ], ThreadPool.prototype, "stop", null);
270
+ //# sourceMappingURL=ThreadPool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ThreadPool.js","sourceRoot":"","sources":["../../../src/threads/ThreadPool.ts"],"names":[],"mappings":";;;;;;;;;AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAC/E,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAiB,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AA0B5E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAO,UAAU;IACnB,8DAA8D;IACtD,SAAS,CAAqC;IACtD,2DAA2D;IACnD,UAAU,CAAS;IAC3B,gEAAgE;IAExD,MAAM,CAAO;IACrB,6CAA6C;IAErC,UAAU,GAAW,EAAE,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC;IAC9C,yCAAyC;IACzB,OAAO,CAAgB;IACvC,u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CAAC,CAAC;QAC5B,CAAC;IACL,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,EAAU,EAAE,GAAQ;QAC9B,IAAI,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC;YACnB,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;QACtC,CAAC;IACL,CAAC;CACJ;AAlOW;IADP,MAAM;;0CACc;AAGb;IADP,MAAM,CAAC,yBAAyB,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC;;8CACN;AAwJjC;IADZ,OAAO;;;;sCAoBP"}
@@ -0,0 +1,25 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2020-2026 Jean-Philippe Steinmetz
3
+ ///////////////////////////////////////////////////////////////////////////////
4
+ /**
5
+ * Describes the types of messages passed between the parent thread pool manager and a worker thread.
6
+ */
7
+ export var WorkerMessageType;
8
+ (function (WorkerMessageType) {
9
+ WorkerMessageType["ERROR"] = "_WorkerError";
10
+ WorkerMessageType["LOG"] = "_WorkerLog";
11
+ WorkerMessageType["ONLINE"] = "_WorkerOnline";
12
+ WorkerMessageType["STOP"] = "_StopWorker";
13
+ })(WorkerMessageType || (WorkerMessageType = {}));
14
+ /**
15
+ * Provides a simple abstract interface for creating thread workers when using the `ThreadPool` system.
16
+ *
17
+ * @author Jean-Philippe Steinmetz
18
+ */
19
+ export class ThreadWorker {
20
+ logger;
21
+ constructor(logger) {
22
+ this.logger = logger;
23
+ }
24
+ }
25
+ //# sourceMappingURL=ThreadWorker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ThreadWorker.js","sourceRoot":"","sources":["../../../src/threads/ThreadWorker.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAC/E;;GAEG;AACH,MAAM,CAAN,IAAY,iBAKX;AALD,WAAY,iBAAiB;IACzB,2CAAsB,CAAA;IACtB,uCAAkB,CAAA;IAClB,6CAAwB,CAAA;IACxB,yCAAoB,CAAA;AACxB,CAAC,EALW,iBAAiB,KAAjB,iBAAiB,QAK5B;AAYD;;;;GAIG;AACH,MAAM,OAAgB,YAAY;IACpB,MAAM,CAAM;IAEtB,YAAY,MAAW;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;CAkBJ"}
@@ -0,0 +1,77 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2020-2026 Jean-Philippe Steinmetz
3
+ ///////////////////////////////////////////////////////////////////////////////
4
+ const { parentPort, workerData } = require("worker_threads");
5
+ const Logger = require("./ThreadLogger");
6
+ const logger = new Logger();
7
+ logger.debug(`Starting ThreadWorkerEntry...`);
8
+ logger.debug(`WorkerData: ${JSON.stringify(workerData)}`);
9
+ // Register for parent messages
10
+ if (workerData) {
11
+ let worker = null;
12
+ try {
13
+ // Load the TypeScript environment if enabled
14
+ if (workerData.allowTs) {
15
+ require("ts-node").register();
16
+ }
17
+ // Import the worker class and initialize
18
+ logger.debug(`Loading worker: ${workerData.worker}`);
19
+ const mod = require(workerData.worker);
20
+ if (typeof mod.default !== "function") {
21
+ throw new Error(`No default export found for worker: ${workerPath}`);
22
+ }
23
+ logger.debug(`Initializing worker...`);
24
+ const clazz = mod.default;
25
+ const args = workerData.args || [];
26
+ worker = new clazz(logger, ...args);
27
+ void worker.start();
28
+ parentPort.on("message", async (msg) => {
29
+ try {
30
+ switch (msg.type) {
31
+ case "_StartWorker":
32
+ await worker?.start();
33
+ break;
34
+ case "_StopWorker":
35
+ logger.debug(`Stopping worker...`);
36
+ await worker?.stop();
37
+ process.exit(0);
38
+ break;
39
+ default:
40
+ await worker?.onMessage(msg);
41
+ break;
42
+ }
43
+ }
44
+ catch (error) {
45
+ parentPort?.postMessage({
46
+ type: "_WorkerError",
47
+ data: error,
48
+ });
49
+ }
50
+ });
51
+ parentPort.on("close", async () => {
52
+ logger.debug(`Stopping worker...`);
53
+ await worker?.stop();
54
+ process.exit(0);
55
+ });
56
+ // Notify the coordinator that we're ready
57
+ parentPort?.postMessage({ type: "_WorkerOnline" });
58
+ logger.debug(`Worker is online!`);
59
+ }
60
+ catch (error) {
61
+ parentPort?.postMessage({
62
+ type: "_WorkerError",
63
+ data: error,
64
+ });
65
+ logger.error(`An error occurred initializing worker.`);
66
+ logger.error(error);
67
+ }
68
+ }
69
+ else {
70
+ logger.error(`WorkerData is missing.`);
71
+ parentPort?.postMessage({
72
+ type: "_WorkerError",
73
+ data: new Error("WorkerData is missing."),
74
+ });
75
+ }
76
+ export {};
77
+ //# sourceMappingURL=ThreadWorkerEntry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ThreadWorkerEntry.js","sourceRoot":"","sources":["../../../src/threads/ThreadWorkerEntry.js"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAC/E,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAC7D,MAAM,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAEzC,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;AAE5B,MAAM,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAC;AAC9C,MAAM,CAAC,KAAK,CAAC,eAAe,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;AAE1D,+BAA+B;AAC/B,IAAI,UAAU,EAAE,CAAC;IACb,IAAI,MAAM,GAAG,IAAI,CAAC;IAElB,IAAI,CAAC;QACD,6CAA6C;QAC7C,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;YACrB,OAAO,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,CAAC;QAClC,CAAC;QAED,yCAAyC;QACzC,MAAM,CAAC,KAAK,CAAC,mBAAmB,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC;QACrD,MAAM,GAAG,GAAG,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,EAAE,CAAC;YACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;QAEH,0CAA0C;QAC1C,UAAU,EAAE,WAAW,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC,CAAC;QACnD,MAAM,CAAC,KAAK,CAAC,mBAAmB,CAAC,CAAC;IACtC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,UAAU,EAAE,WAAW,CAAC;YACpB,IAAI,EAAE,cAAc;YACpB,IAAI,EAAE,KAAK;SACd,CAAC,CAAC;QACH,MAAM,CAAC,KAAK,CAAC,wCAAwC,CAAC,CAAC;QACvD,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC;AACL,CAAC;KAAM,CAAC;IACJ,MAAM,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC;IACvC,UAAU,EAAE,WAAW,CAAC;QACpB,IAAI,EAAE,cAAc;QACpB,IAAI,EAAE,IAAI,KAAK,CAAC,wBAAwB,CAAC;KAC5C,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from "./ThreadPool.js";
2
+ export * from "./ThreadWorker.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/threads/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC"}
@@ -0,0 +1,132 @@
1
+ import { ReadStream } from "fs";
2
+ /**
3
+ * Describes the level of priority for a given alert and an importance for triaging the problem.
4
+ */
5
+ export declare enum AlertPriority {
6
+ /** Describes an alert that affects critical operating functionality or infrastructure. */
7
+ Critical = "P1",
8
+ /** Describes an alert that affects key systems that have great impact on end-user experiences. */
9
+ Severe = "P2",
10
+ /** Describes an alert that affects basic systems that may create a poor end-user experience. */
11
+ Important = "P3",
12
+ /** Describes an alert that has no material affect on the end-user experience but with potential to escalate. */
13
+ Warning = "P4",
14
+ /** Describes an alert that has no material affect on end-user experience and no potential for escalation. */
15
+ Notice = "P5"
16
+ }
17
+ /**
18
+ * Describes a single system failure or other similar critical event that requires immediate attention.
19
+ */
20
+ export interface Alert {
21
+ /** The name of the alert that uniquely identifies the report for purposes of de-duplication. */
22
+ alias: string;
23
+ /** The detailed information about the event that has occurred. */
24
+ description: string;
25
+ /** A key-value map of custom information about the alert. */
26
+ details?: any;
27
+ /** Used to specify a category or problem domain for filtering. */
28
+ entity?: string;
29
+ /** The basic summary of the alert that describes the event. */
30
+ message: string;
31
+ /** Additional detail about the event. */
32
+ note?: string;
33
+ /** The priority level of the alert. */
34
+ priority: AlertPriority;
35
+ /** The source of the alert. Can be a pod/service name, IP address or other unique identifier. */
36
+ source: string;
37
+ /** A list of unique tags to associate with the event. */
38
+ tags?: string[];
39
+ }
40
+ /**
41
+ * Describes a single attachment that can be added to an alert.
42
+ */
43
+ export interface AlertAttachment {
44
+ /** The content-type of the attachment. */
45
+ contentType: string;
46
+ /** The contents of the attachment. */
47
+ data: Buffer | ReadStream;
48
+ /** The name of the attachment. */
49
+ filename: string;
50
+ /** The size of the attachment. */
51
+ size?: number;
52
+ }
53
+ /**
54
+ * Describes a request to close an alert.
55
+ */
56
+ export interface AlertClose {
57
+ /** Display name of the request source. */
58
+ source?: string;
59
+ /** Additional alert note to add. */
60
+ note?: string;
61
+ }
62
+ /**
63
+ * The set of configuration options to pass to AlertUtils when creating an instance.
64
+ */
65
+ export interface AlertUtilsOptions {
66
+ /** The value of the Authorization header to apply to all outgoing HTTP requests. */
67
+ auth: string;
68
+ /** The full url of the REST API service that alerts will be sent to. */
69
+ serviceUrl: string;
70
+ /** The logging utility to use. */
71
+ logger?: any;
72
+ }
73
+ /**
74
+ * Describes the alert attachment options to use when creating an alert.
75
+ */
76
+ export interface AlertUtilsAttachmentOptions {
77
+ /** The attachment files to upload with the alert. */
78
+ files: AlertAttachment[];
79
+ /** The name of the index file when the attachment is a zip file. */
80
+ indexFile?: string;
81
+ /**
82
+ * Set to `true` to to package all files into a single zip before being uploaded, otherwise set to `false` to
83
+ * upload each file individually.
84
+ */
85
+ zip?: boolean;
86
+ }
87
+ /**
88
+ * The `AlertUtils` class is used to send alerts about important system events that have occurred
89
+ * and require further monitoring or intervention.
90
+ */
91
+ export declare class AlertUtils {
92
+ /** The logging utility to use. */
93
+ private logger;
94
+ /** The value of the Authorization header to apply to all outgoing HTTP requests. */
95
+ private auth;
96
+ /** The full url of the REST API service that alerts will be sent to. */
97
+ private serviceUrl;
98
+ /**
99
+ * Creates a new instance of `AuthUtils` with the provided defaults.
100
+ * @param options The configuration options to use.
101
+ */
102
+ constructor(options: AlertUtilsOptions);
103
+ /**
104
+ * Attempts to close the existing alert with the given identifier.
105
+ * @param id The unique identifier of the alert to close.
106
+ * @returns True if the operation was successful, otherwise false.
107
+ */
108
+ close(id: string, data?: AlertClose): Promise<boolean>;
109
+ /**
110
+ * Attempts to retrieve the existing alert with the given identifier.
111
+ * @param id The unique identifier of the alert to retrieve.
112
+ * @returns The retrieved alert if successful, otherwise `null`.
113
+ */
114
+ get(id: string): Promise<Alert | null>;
115
+ /**
116
+ * Sends the provided alert to the configured monitoring service.
117
+ *
118
+ * @param alert The alert to send.
119
+ * @param vars A map of vars to perform replacement on for the alert's various properties.
120
+ * @param attachments The attachments to upload along with the alert.
121
+ * @returns The unique identifier of the created alert if the operation was successful, otherwise `null`.
122
+ */
123
+ send(alert: Alert, vars?: any, attachments?: AlertUtilsAttachmentOptions): Promise<string | null>;
124
+ /**
125
+ * Uploads a single attachment to the alert with the given unique identifier.
126
+ * @param id The unique identifier of the alert to add an attachment for.
127
+ * @param attachment The file to upload as an attachment.
128
+ * @param indexFile Sets the indexFile parameter of the request.
129
+ * @returns True if the operation was successful, otherwise false.
130
+ */
131
+ addAttachment(id: string, attachment: AlertAttachment, indexFile?: string): Promise<boolean>;
132
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Describes an error that originates from a API service. This class extends the standard `Error` class to include
3
+ * a unique code that can be used to trace an error to source code as well as the HTTP response status returned with
4
+ * the error.
5
+ */
6
+ export declare class ApiError extends Error {
7
+ /** The unique code of the error. */
8
+ code: string;
9
+ /** The HTTP status associated with the error. */
10
+ status: number;
11
+ ApiMessageTemplate(message: string | undefined, templateVariables?: any): string;
12
+ constructor(code: string, status: number, message?: string, templateVariables?: any);
13
+ }