@haskou/ddd-kernel 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.
Files changed (191) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +143 -0
  3. package/dist/Constructor-fyZLLIh8.d.cts +5 -0
  4. package/dist/Constructor-fyZLLIh8.d.ts +5 -0
  5. package/dist/Consumer-CC8ZRCsd.d.cts +17 -0
  6. package/dist/Consumer-CeT0Wbxb.d.ts +17 -0
  7. package/dist/DomainEvent-mXWEtr_J.d.cts +26 -0
  8. package/dist/DomainEvent-mXWEtr_J.d.ts +26 -0
  9. package/dist/DomainEventConsumer-3WBMSSr2.d.cts +7 -0
  10. package/dist/DomainEventConsumer-B4hkIUmP.d.ts +7 -0
  11. package/dist/DomainEventPublisher-8G0lvmdy.d.cts +7 -0
  12. package/dist/DomainEventPublisher-DhGgM3f2.d.ts +7 -0
  13. package/dist/HandlerContext-DUUExVo3.d.ts +20 -0
  14. package/dist/HandlerContext-Lm89pSeG.d.cts +20 -0
  15. package/dist/Kernel-BWUOUWWI.d.cts +70 -0
  16. package/dist/Kernel-CUaqHa1s.d.ts +70 -0
  17. package/dist/KernelLogger-BESOFrpW.d.cts +8 -0
  18. package/dist/KernelLogger-BESOFrpW.d.ts +8 -0
  19. package/dist/Log-36L3Z84a.d.cts +8 -0
  20. package/dist/Log-36L3Z84a.d.ts +8 -0
  21. package/dist/NoFailedMessagesError-0YJKRWPF.d.ts +45 -0
  22. package/dist/NoFailedMessagesError-Kz7CYWpT.d.cts +45 -0
  23. package/dist/Repository-D9CuAyCV.d.cts +7 -0
  24. package/dist/Repository-D9CuAyCV.d.ts +7 -0
  25. package/dist/Scheduler-oigqNOUJ.d.cts +27 -0
  26. package/dist/Scheduler-oigqNOUJ.d.ts +27 -0
  27. package/dist/ServiceClass-BmNw8fJj.d.cts +37 -0
  28. package/dist/ServiceClass-C7NCKdSS.d.ts +37 -0
  29. package/dist/ServiceResolver-D2Jz-l_Z.d.cts +5 -0
  30. package/dist/ServiceResolver-D2Jz-l_Z.d.ts +5 -0
  31. package/dist/ShutdownHook-BGskq2-q.d.ts +9 -0
  32. package/dist/ShutdownHook-Dib5uNKB.d.cts +9 -0
  33. package/dist/Subscription-Bwkb_did.d.ts +9 -0
  34. package/dist/Subscription-P9WROD_6.d.cts +9 -0
  35. package/dist/adapters/db/in-memory/index.cjs +46 -0
  36. package/dist/adapters/db/in-memory/index.cjs.map +1 -0
  37. package/dist/adapters/db/in-memory/index.d.cts +12 -0
  38. package/dist/adapters/db/in-memory/index.d.ts +12 -0
  39. package/dist/adapters/db/in-memory/index.js +19 -0
  40. package/dist/adapters/db/in-memory/index.js.map +1 -0
  41. package/dist/adapters/db/index.cjs +80 -0
  42. package/dist/adapters/db/index.cjs.map +1 -0
  43. package/dist/adapters/db/index.d.cts +4 -0
  44. package/dist/adapters/db/index.d.ts +4 -0
  45. package/dist/adapters/db/index.js +52 -0
  46. package/dist/adapters/db/index.js.map +1 -0
  47. package/dist/adapters/db/mongo/index.cjs +62 -0
  48. package/dist/adapters/db/mongo/index.cjs.map +1 -0
  49. package/dist/adapters/db/mongo/index.d.cts +22 -0
  50. package/dist/adapters/db/mongo/index.d.ts +22 -0
  51. package/dist/adapters/db/mongo/index.js +35 -0
  52. package/dist/adapters/db/mongo/index.js.map +1 -0
  53. package/dist/adapters/index.cjs +651 -0
  54. package/dist/adapters/index.cjs.map +1 -0
  55. package/dist/adapters/index.d.cts +26 -0
  56. package/dist/adapters/index.d.ts +26 -0
  57. package/dist/adapters/index.js +609 -0
  58. package/dist/adapters/index.js.map +1 -0
  59. package/dist/adapters/kernel/console/index.cjs +46 -0
  60. package/dist/adapters/kernel/console/index.cjs.map +1 -0
  61. package/dist/adapters/kernel/console/index.d.cts +10 -0
  62. package/dist/adapters/kernel/console/index.d.ts +10 -0
  63. package/dist/adapters/kernel/console/index.js +19 -0
  64. package/dist/adapters/kernel/console/index.js.map +1 -0
  65. package/dist/adapters/kernel/index.cjs +46 -0
  66. package/dist/adapters/kernel/index.cjs.map +1 -0
  67. package/dist/adapters/kernel/index.d.cts +2 -0
  68. package/dist/adapters/kernel/index.d.ts +2 -0
  69. package/dist/adapters/kernel/index.js +19 -0
  70. package/dist/adapters/kernel/index.js.map +1 -0
  71. package/dist/adapters/pubsub/amqp/index.cjs +724 -0
  72. package/dist/adapters/pubsub/amqp/index.cjs.map +1 -0
  73. package/dist/adapters/pubsub/amqp/index.d.cts +42 -0
  74. package/dist/adapters/pubsub/amqp/index.d.ts +42 -0
  75. package/dist/adapters/pubsub/amqp/index.js +691 -0
  76. package/dist/adapters/pubsub/amqp/index.js.map +1 -0
  77. package/dist/adapters/pubsub/in-memory/index.cjs +79 -0
  78. package/dist/adapters/pubsub/in-memory/index.cjs.map +1 -0
  79. package/dist/adapters/pubsub/in-memory/index.d.cts +21 -0
  80. package/dist/adapters/pubsub/in-memory/index.d.ts +21 -0
  81. package/dist/adapters/pubsub/in-memory/index.js +51 -0
  82. package/dist/adapters/pubsub/in-memory/index.js.map +1 -0
  83. package/dist/adapters/pubsub/index.cjs +492 -0
  84. package/dist/adapters/pubsub/index.cjs.map +1 -0
  85. package/dist/adapters/pubsub/index.d.cts +11 -0
  86. package/dist/adapters/pubsub/index.d.ts +11 -0
  87. package/dist/adapters/pubsub/index.js +456 -0
  88. package/dist/adapters/pubsub/index.js.map +1 -0
  89. package/dist/adapters/ui/express/index.cjs +126 -0
  90. package/dist/adapters/ui/express/index.cjs.map +1 -0
  91. package/dist/adapters/ui/express/index.d.cts +49 -0
  92. package/dist/adapters/ui/express/index.d.ts +49 -0
  93. package/dist/adapters/ui/express/index.js +98 -0
  94. package/dist/adapters/ui/express/index.js.map +1 -0
  95. package/dist/adapters/ui/index.cjs +489 -0
  96. package/dist/adapters/ui/index.cjs.map +1 -0
  97. package/dist/adapters/ui/index.d.cts +14 -0
  98. package/dist/adapters/ui/index.d.ts +14 -0
  99. package/dist/adapters/ui/index.js +455 -0
  100. package/dist/adapters/ui/index.js.map +1 -0
  101. package/dist/adapters/ui/routes/index.cjs +393 -0
  102. package/dist/adapters/ui/routes/index.cjs.map +1 -0
  103. package/dist/adapters/ui/routes/index.d.cts +5 -0
  104. package/dist/adapters/ui/routes/index.d.ts +5 -0
  105. package/dist/adapters/ui/routes/index.js +361 -0
  106. package/dist/adapters/ui/routes/index.js.map +1 -0
  107. package/dist/contracts/db/index.cjs +19 -0
  108. package/dist/contracts/db/index.cjs.map +1 -0
  109. package/dist/contracts/db/index.d.cts +7 -0
  110. package/dist/contracts/db/index.d.ts +7 -0
  111. package/dist/contracts/db/index.js +1 -0
  112. package/dist/contracts/db/index.js.map +1 -0
  113. package/dist/contracts/index.cjs +49 -0
  114. package/dist/contracts/index.cjs.map +1 -0
  115. package/dist/contracts/index.d.cts +11 -0
  116. package/dist/contracts/index.d.ts +11 -0
  117. package/dist/contracts/index.js +22 -0
  118. package/dist/contracts/index.js.map +1 -0
  119. package/dist/contracts/kernel/index.cjs +19 -0
  120. package/dist/contracts/kernel/index.cjs.map +1 -0
  121. package/dist/contracts/kernel/index.d.cts +9 -0
  122. package/dist/contracts/kernel/index.d.ts +9 -0
  123. package/dist/contracts/kernel/index.js +1 -0
  124. package/dist/contracts/kernel/index.js.map +1 -0
  125. package/dist/contracts/pubsub/index.cjs +19 -0
  126. package/dist/contracts/pubsub/index.cjs.map +1 -0
  127. package/dist/contracts/pubsub/index.d.cts +26 -0
  128. package/dist/contracts/pubsub/index.d.ts +26 -0
  129. package/dist/contracts/pubsub/index.js +1 -0
  130. package/dist/contracts/pubsub/index.js.map +1 -0
  131. package/dist/contracts/ui/index.cjs +49 -0
  132. package/dist/contracts/ui/index.cjs.map +1 -0
  133. package/dist/contracts/ui/index.d.cts +29 -0
  134. package/dist/contracts/ui/index.d.ts +29 -0
  135. package/dist/contracts/ui/index.js +22 -0
  136. package/dist/contracts/ui/index.js.map +1 -0
  137. package/dist/domain/index.cjs +121 -0
  138. package/dist/domain/index.cjs.map +1 -0
  139. package/dist/domain/index.d.cts +26 -0
  140. package/dist/domain/index.d.ts +26 -0
  141. package/dist/domain/index.js +89 -0
  142. package/dist/domain/index.js.map +1 -0
  143. package/dist/errors/index.cjs +58 -0
  144. package/dist/errors/index.cjs.map +1 -0
  145. package/dist/errors/index.d.cts +13 -0
  146. package/dist/errors/index.d.ts +13 -0
  147. package/dist/errors/index.js +29 -0
  148. package/dist/errors/index.js.map +1 -0
  149. package/dist/index.cjs +442 -0
  150. package/dist/index.cjs.map +1 -0
  151. package/dist/index.d.cts +19 -0
  152. package/dist/index.d.ts +19 -0
  153. package/dist/index.js +405 -0
  154. package/dist/index.js.map +1 -0
  155. package/dist/infrastructure/dependency-injection/index.cjs +169 -0
  156. package/dist/infrastructure/dependency-injection/index.cjs.map +1 -0
  157. package/dist/infrastructure/dependency-injection/index.d.cts +30 -0
  158. package/dist/infrastructure/dependency-injection/index.d.ts +30 -0
  159. package/dist/infrastructure/dependency-injection/index.js +137 -0
  160. package/dist/infrastructure/dependency-injection/index.js.map +1 -0
  161. package/dist/infrastructure/express/index.cjs +126 -0
  162. package/dist/infrastructure/express/index.cjs.map +1 -0
  163. package/dist/infrastructure/express/index.d.cts +14 -0
  164. package/dist/infrastructure/express/index.d.ts +14 -0
  165. package/dist/infrastructure/express/index.js +98 -0
  166. package/dist/infrastructure/express/index.js.map +1 -0
  167. package/dist/infrastructure/lifecycle/index.cjs +19 -0
  168. package/dist/infrastructure/lifecycle/index.cjs.map +1 -0
  169. package/dist/infrastructure/lifecycle/index.d.cts +9 -0
  170. package/dist/infrastructure/lifecycle/index.d.ts +9 -0
  171. package/dist/infrastructure/lifecycle/index.js +1 -0
  172. package/dist/infrastructure/lifecycle/index.js.map +1 -0
  173. package/dist/infrastructure/logs/index.cjs +144 -0
  174. package/dist/infrastructure/logs/index.cjs.map +1 -0
  175. package/dist/infrastructure/logs/index.d.cts +30 -0
  176. package/dist/infrastructure/logs/index.d.ts +30 -0
  177. package/dist/infrastructure/logs/index.js +107 -0
  178. package/dist/infrastructure/logs/index.js.map +1 -0
  179. package/dist/infrastructure/scheduler/index.cjs +461 -0
  180. package/dist/infrastructure/scheduler/index.cjs.map +1 -0
  181. package/dist/infrastructure/scheduler/index.d.cts +17 -0
  182. package/dist/infrastructure/scheduler/index.d.ts +17 -0
  183. package/dist/infrastructure/scheduler/index.js +426 -0
  184. package/dist/infrastructure/scheduler/index.js.map +1 -0
  185. package/dist/infrastructure/websocket/index.cjs +131 -0
  186. package/dist/infrastructure/websocket/index.cjs.map +1 -0
  187. package/dist/infrastructure/websocket/index.d.cts +56 -0
  188. package/dist/infrastructure/websocket/index.d.ts +56 -0
  189. package/dist/infrastructure/websocket/index.js +103 -0
  190. package/dist/infrastructure/websocket/index.js.map +1 -0
  191. package/package.json +248 -0
@@ -0,0 +1,609 @@
1
+ // src/adapters/db/in-memory/InMemoryRepository.ts
2
+ var InMemoryRepository = class {
3
+ entities = /* @__PURE__ */ new Map();
4
+ findById(id) {
5
+ return Promise.resolve(this.entities.get(id) ?? null);
6
+ }
7
+ save(entity) {
8
+ this.entities.set(entity.id, entity);
9
+ return Promise.resolve();
10
+ }
11
+ delete(id) {
12
+ this.entities.delete(id);
13
+ return Promise.resolve();
14
+ }
15
+ };
16
+
17
+ // src/adapters/db/mongo/MongoRepository.ts
18
+ var MongoRepository = class {
19
+ constructor(collection, mapper) {
20
+ this.collection = collection;
21
+ this.mapper = mapper;
22
+ }
23
+ collection;
24
+ mapper;
25
+ async findById(id) {
26
+ const document = await this.collection.findOne({
27
+ id
28
+ });
29
+ if (!document) {
30
+ return null;
31
+ }
32
+ return this.mapper.toEntity(document);
33
+ }
34
+ async save(entity) {
35
+ const document = this.mapper.toDocument(entity);
36
+ await this.collection.updateOne(
37
+ { id: document.id },
38
+ { $set: document },
39
+ { upsert: true }
40
+ );
41
+ }
42
+ async delete(id) {
43
+ await this.collection.deleteOne({
44
+ id
45
+ });
46
+ }
47
+ };
48
+
49
+ // src/adapters/kernel/console/ConsoleKernelLogger.ts
50
+ var ConsoleKernelLogger = class {
51
+ debug(message) {
52
+ console.debug(message);
53
+ }
54
+ error(message) {
55
+ console.error(message);
56
+ }
57
+ info(message) {
58
+ console.info(message);
59
+ }
60
+ warn(message) {
61
+ console.warn(message);
62
+ }
63
+ };
64
+
65
+ // src/Kernel.ts
66
+ import path2 from "path";
67
+
68
+ // src/infrastructure/dependency-injection/DependencyInjection.ts
69
+ import fs from "fs-extra";
70
+ import {
71
+ Autowire,
72
+ ContainerBuilder,
73
+ ServiceFile,
74
+ YamlFileLoader
75
+ } from "node-dependency-injection";
76
+ import path from "path";
77
+ var DependencyInjection = class _DependencyInjection {
78
+ constructor(options = {
79
+ containerBuild: process.env.CONTAINER_BUILD === "true",
80
+ servicesYamlPath: path.resolve(
81
+ process.cwd(),
82
+ "config",
83
+ "container",
84
+ "services.yaml"
85
+ ),
86
+ sourceDirectory: path.resolve(process.cwd(), "src")
87
+ }) {
88
+ this.options = options;
89
+ this.container = new ContainerBuilder(false, this.options.sourceDirectory);
90
+ }
91
+ options;
92
+ static configuredInstance;
93
+ autowire;
94
+ loader;
95
+ container;
96
+ static configure(options) {
97
+ _DependencyInjection.configuredInstance = new _DependencyInjection(options);
98
+ return _DependencyInjection.configuredInstance;
99
+ }
100
+ static get instance() {
101
+ if (!_DependencyInjection.configuredInstance) {
102
+ throw new Error("DependencyInjection has not been configured.");
103
+ }
104
+ return _DependencyInjection.configuredInstance;
105
+ }
106
+ get definitions() {
107
+ const container = this.container;
108
+ return container._definitions || /* @__PURE__ */ new Map();
109
+ }
110
+ get aliases() {
111
+ const container = this.container;
112
+ return container._alias || /* @__PURE__ */ new Map();
113
+ }
114
+ async ensureFolderExists(filePath) {
115
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
116
+ }
117
+ getServiceClassName(serviceName) {
118
+ return typeof serviceName === "function" ? serviceName.name : void 0;
119
+ }
120
+ parentMatchesService(parentId, serviceClassName) {
121
+ if (!parentId) {
122
+ return false;
123
+ }
124
+ const parentName = Buffer.from(parentId, "base64").toString("utf8");
125
+ return parentName.endsWith(`__${serviceClassName}__${serviceClassName}`);
126
+ }
127
+ serviceIdMatchesService(serviceId, serviceClassName) {
128
+ const serviceName = Buffer.from(serviceId, "base64").toString("utf8");
129
+ return serviceName.endsWith(`__${serviceClassName}__${serviceClassName}`);
130
+ }
131
+ findConcreteChildServiceId(serviceName) {
132
+ const serviceClassName = this.getServiceClassName(serviceName);
133
+ if (!serviceClassName) {
134
+ return void 0;
135
+ }
136
+ const matches = [...this.definitions.entries()].filter(([, definition]) => definition._abstract !== true).filter(
137
+ ([, definition]) => this.parentMatchesService(definition._parent, serviceClassName)
138
+ ).map(([id]) => id);
139
+ return matches[matches.length - 1];
140
+ }
141
+ findRegisteredServiceId(serviceName) {
142
+ const serviceClassName = this.getServiceClassName(serviceName);
143
+ if (!serviceClassName) {
144
+ return void 0;
145
+ }
146
+ const matches = [...this.definitions.keys()].filter(
147
+ (id) => this.serviceIdMatchesService(id, serviceClassName)
148
+ );
149
+ return matches[matches.length - 1];
150
+ }
151
+ findAliasServiceId(serviceName) {
152
+ const serviceClassName = this.getServiceClassName(serviceName);
153
+ if (!serviceClassName) {
154
+ return void 0;
155
+ }
156
+ const matches = [...this.aliases.keys()].filter(
157
+ (id) => this.serviceIdMatchesService(id, serviceClassName)
158
+ );
159
+ return matches[matches.length - 1];
160
+ }
161
+ registerParentAliases() {
162
+ for (const [id, definition] of this.definitions.entries()) {
163
+ if (definition._abstract === true || !definition._parent) {
164
+ continue;
165
+ }
166
+ this.container.setAlias(definition._parent, id);
167
+ }
168
+ }
169
+ async compile() {
170
+ if (this.options.containerBuild) {
171
+ await this.ensureFolderExists(this.options.servicesYamlPath);
172
+ this.autowire = new Autowire(this.container);
173
+ this.autowire.serviceFile = new ServiceFile(
174
+ this.options.servicesYamlPath,
175
+ false
176
+ );
177
+ await this.autowire.process();
178
+ } else {
179
+ this.loader = new YamlFileLoader(this.container);
180
+ await this.loader.load(this.options.servicesYamlPath);
181
+ }
182
+ this.registerParentAliases();
183
+ await this.container.compile();
184
+ }
185
+ getService(serviceName) {
186
+ const childServiceId = this.findConcreteChildServiceId(serviceName);
187
+ if (childServiceId) {
188
+ return this.container.get(childServiceId);
189
+ }
190
+ const aliasServiceId = this.findAliasServiceId(serviceName);
191
+ if (aliasServiceId) {
192
+ return this.container.get(aliasServiceId);
193
+ }
194
+ const registeredServiceId = this.findRegisteredServiceId(serviceName);
195
+ if (registeredServiceId) {
196
+ return this.container.get(registeredServiceId);
197
+ }
198
+ return this.container.get(serviceName);
199
+ }
200
+ };
201
+
202
+ // src/Kernel.ts
203
+ var Kernel = class _Kernel {
204
+ constructor(options = {}) {
205
+ this.options = options;
206
+ this.loggerInstance = options.logger ?? new ConsoleKernelLogger();
207
+ this.dependencyInjectionInstance = options.di;
208
+ _Kernel.state.activeKernel = this;
209
+ }
210
+ options;
211
+ static stateKey = /* @__PURE__ */ Symbol.for(
212
+ "@haskou/ddd-kernel/kernel-state"
213
+ );
214
+ consumerMiddlewares = [];
215
+ consumersList = [];
216
+ loggerInstance;
217
+ routesList = [];
218
+ schedulersList = [];
219
+ shutdownHooks = [];
220
+ dependencyInjectionInstance;
221
+ static get state() {
222
+ const stateContainer = globalThis;
223
+ stateContainer[_Kernel.stateKey] = stateContainer[_Kernel.stateKey] ?? {};
224
+ return stateContainer[_Kernel.stateKey];
225
+ }
226
+ static get configDirectory() {
227
+ return path2.resolve(_Kernel.rootDirectory, "config");
228
+ }
229
+ static get consumers() {
230
+ return _Kernel.getActiveKernel().consumers;
231
+ }
232
+ static get consumerMiddleware() {
233
+ return _Kernel.getActiveKernel().consumerMiddleware;
234
+ }
235
+ static get di() {
236
+ return _Kernel.getActiveKernel().di;
237
+ }
238
+ static get logger() {
239
+ return _Kernel.getActiveKernel().logger;
240
+ }
241
+ static get rootDirectory() {
242
+ return process.cwd();
243
+ }
244
+ static get routes() {
245
+ return _Kernel.getActiveKernel().routes;
246
+ }
247
+ static get schedulers() {
248
+ return _Kernel.getActiveKernel().schedulers;
249
+ }
250
+ static get sourceDirectory() {
251
+ return path2.resolve(_Kernel.rootDirectory, "src");
252
+ }
253
+ static getActiveKernel() {
254
+ if (!_Kernel.state.activeKernel) {
255
+ _Kernel.state.activeKernel = new _Kernel();
256
+ }
257
+ return _Kernel.state.activeKernel;
258
+ }
259
+ async closeCandidate(candidate) {
260
+ if (candidate.shutdown) {
261
+ await candidate.shutdown();
262
+ return;
263
+ }
264
+ if (candidate.close) {
265
+ await candidate.close();
266
+ return;
267
+ }
268
+ if (candidate.stop) {
269
+ await candidate.stop();
270
+ return;
271
+ }
272
+ if (candidate.flush) {
273
+ await candidate.flush();
274
+ }
275
+ }
276
+ getConsumerFromClass(ClassDefinition) {
277
+ return this.di.getService(ClassDefinition);
278
+ }
279
+ getInitializerFromClass(ClassDefinition) {
280
+ return this.di.getService(ClassDefinition);
281
+ }
282
+ getRuntimeFromClass(ClassDefinition) {
283
+ return this.di.getService(ClassDefinition);
284
+ }
285
+ getSchedulerFromClass(ClassDefinition) {
286
+ return this.di.getService(ClassDefinition);
287
+ }
288
+ get consumers() {
289
+ return this.consumersList;
290
+ }
291
+ get consumerMiddleware() {
292
+ return this.consumerMiddlewares;
293
+ }
294
+ get di() {
295
+ if (!this.dependencyInjectionInstance) {
296
+ throw new Error("Kernel dependency injection has not been initialized.");
297
+ }
298
+ return this.dependencyInjectionInstance;
299
+ }
300
+ get logger() {
301
+ return this.loggerInstance;
302
+ }
303
+ get routes() {
304
+ return this.routesList;
305
+ }
306
+ get schedulers() {
307
+ return this.schedulersList;
308
+ }
309
+ async dependencyInjection() {
310
+ this.dependencyInjectionInstance = this.dependencyInjectionInstance ?? DependencyInjection.configure({
311
+ containerBuild: process.env.CONTAINER_BUILD === "true",
312
+ servicesYamlPath: this.options.servicesYamlPath ?? path2.resolve(_Kernel.configDirectory, "container", "services.yaml"),
313
+ sourceDirectory: this.options.sourceDirectory ?? _Kernel.sourceDirectory
314
+ });
315
+ await this.dependencyInjectionInstance.compile();
316
+ }
317
+ getRoutes() {
318
+ return this.routes;
319
+ }
320
+ registerConsumerMiddleware(...middlewares) {
321
+ this.consumerMiddlewares.push(...middlewares);
322
+ }
323
+ registerConsumers(...ClassDefinitions) {
324
+ for (const ClassDefinition of ClassDefinitions) {
325
+ this.consumersList.push(this.getConsumerFromClass(ClassDefinition));
326
+ }
327
+ }
328
+ registerConsumerInstances(...consumers) {
329
+ this.consumersList.push(...consumers);
330
+ }
331
+ registerRoutes(...ClassDefinitions) {
332
+ this.routesList.push(...ClassDefinitions);
333
+ }
334
+ registerSchedulers(...ClassDefinitions) {
335
+ for (const ClassDefinition of ClassDefinitions) {
336
+ this.schedulersList.push(this.getSchedulerFromClass(ClassDefinition));
337
+ }
338
+ }
339
+ registerSchedulerInstances(...schedulers) {
340
+ this.schedulersList.push(...schedulers);
341
+ }
342
+ registerShutdownHook(hook) {
343
+ this.shutdownHooks.push(hook);
344
+ }
345
+ removeConsumers() {
346
+ this.consumersList.length = 0;
347
+ }
348
+ removeRoutes() {
349
+ this.routesList.length = 0;
350
+ }
351
+ removeSchedulers() {
352
+ this.schedulersList.length = 0;
353
+ }
354
+ async runConsumers() {
355
+ for (const consumer of this.consumersList) {
356
+ await consumer.init();
357
+ }
358
+ }
359
+ async runInitializers(...ClassDefinitions) {
360
+ for (const ClassDefinition of ClassDefinitions) {
361
+ await this.getInitializerFromClass(ClassDefinition).ensure();
362
+ }
363
+ }
364
+ async runRuntimes(...ClassDefinitions) {
365
+ for (const ClassDefinition of ClassDefinitions) {
366
+ const runtime = this.getRuntimeFromClass(ClassDefinition);
367
+ await runtime.run();
368
+ this.registerShutdownHook(
369
+ () => this.closeCandidate(runtime)
370
+ );
371
+ }
372
+ }
373
+ async runSchedulerNowAndSchedule(ClassDefinition) {
374
+ const scheduler = this.getSchedulerFromClass(ClassDefinition);
375
+ await scheduler.runOnce();
376
+ await scheduler.init();
377
+ this.schedulersList.push(scheduler);
378
+ }
379
+ async runSchedulers() {
380
+ for (const scheduler of this.schedulersList) {
381
+ await scheduler.init();
382
+ }
383
+ }
384
+ async shutdown() {
385
+ for (const consumer of [...this.consumersList].reverse()) {
386
+ await this.closeCandidate(consumer);
387
+ }
388
+ for (const scheduler of [...this.schedulersList].reverse()) {
389
+ await this.closeCandidate(scheduler);
390
+ }
391
+ for (const hook of [...this.shutdownHooks].reverse()) {
392
+ await hook();
393
+ }
394
+ await this.closeCandidate(this.loggerInstance);
395
+ }
396
+ };
397
+
398
+ // src/adapters/pubsub/Consumer.ts
399
+ var Consumer = class {
400
+ constructor(consumer) {
401
+ this.consumer = consumer;
402
+ }
403
+ consumer;
404
+ async runMiddleware(event, middlewares, index) {
405
+ const middleware = middlewares[index];
406
+ if (!middleware) {
407
+ await this.handler(event);
408
+ return;
409
+ }
410
+ await middleware.handle(
411
+ event,
412
+ () => this.runMiddleware(event, middlewares, index + 1)
413
+ );
414
+ }
415
+ async init() {
416
+ await this.consumer.consume(
417
+ this.queueName,
418
+ this.eventName,
419
+ this.domainEvent,
420
+ this.exchange,
421
+ (event) => this.runMiddleware(event, Kernel.consumerMiddleware, 0)
422
+ );
423
+ }
424
+ get(service) {
425
+ return Kernel.di.getService(service);
426
+ }
427
+ };
428
+
429
+ // src/adapters/pubsub/amqp/AmqpMessageBusAdapter.ts
430
+ import amqplib from "amqplib";
431
+ import { randomUUID } from "crypto";
432
+
433
+ // src/adapters/pubsub/amqp/InvalidDomainEventError.ts
434
+ var InvalidDomainEventError = class extends Error {
435
+ constructor(message) {
436
+ super(`Invalid domain event: ${message}`);
437
+ this.name = "InvalidDomainEventError";
438
+ }
439
+ };
440
+
441
+ // src/adapters/pubsub/amqp/NoFailedMessagesError.ts
442
+ var NoFailedMessagesError = class extends Error {
443
+ constructor(queueName) {
444
+ super(`No failed messages found in "${queueName}".`);
445
+ this.name = "NoFailedMessagesError";
446
+ }
447
+ };
448
+
449
+ // src/adapters/pubsub/in-memory/InMemoryEventBus.ts
450
+ var InMemoryEventBus = class {
451
+ constructor(context) {
452
+ this.context = context;
453
+ }
454
+ context;
455
+ handlers = /* @__PURE__ */ new Map();
456
+ subscribe(name, handler) {
457
+ const handlers = this.handlers.get(name) ?? [];
458
+ handlers.push(handler);
459
+ this.handlers.set(name, handlers);
460
+ }
461
+ async publish(event) {
462
+ const handlers = this.handlers.get(event.name) ?? [];
463
+ for (const handler of handlers) {
464
+ await handler(event, this.context);
465
+ }
466
+ }
467
+ };
468
+
469
+ // src/adapters/pubsub/in-memory/InMemoryPubSub.ts
470
+ var InMemoryPubSub = class {
471
+ constructor(context) {
472
+ this.context = context;
473
+ }
474
+ context;
475
+ consumers = /* @__PURE__ */ new Map();
476
+ async publish(topic, message) {
477
+ const consumers = this.consumers.get(topic) ?? /* @__PURE__ */ new Set();
478
+ for (const consumer of consumers) {
479
+ await consumer(message, this.context);
480
+ }
481
+ }
482
+ subscribe(topic, consumer) {
483
+ const consumers = this.consumers.get(topic) ?? /* @__PURE__ */ new Set();
484
+ const castConsumer = consumer;
485
+ consumers.add(castConsumer);
486
+ this.consumers.set(topic, consumers);
487
+ return Promise.resolve({
488
+ close: () => {
489
+ consumers.delete(castConsumer);
490
+ return Promise.resolve();
491
+ }
492
+ });
493
+ }
494
+ };
495
+
496
+ // src/adapters/ui/express/ExpressKernelServer.ts
497
+ import { createExpressServer } from "routing-controllers";
498
+ var ExpressKernelServer = class {
499
+ constructor(options) {
500
+ this.options = options;
501
+ }
502
+ options;
503
+ appInstance;
504
+ serverInstance;
505
+ registerErrorHandlers(app) {
506
+ const handlers = this.options.errorHandlers ?? [this.defaultErrorHandler()];
507
+ for (const handler of handlers) {
508
+ app.use(handler);
509
+ }
510
+ }
511
+ defaultErrorHandler() {
512
+ return (error, request, response) => {
513
+ void request;
514
+ response.status(500).json({
515
+ error: error instanceof Error ? error.message : String(error)
516
+ });
517
+ };
518
+ }
519
+ get app() {
520
+ if (!this.appInstance) {
521
+ throw new Error("HTTP server is not running.");
522
+ }
523
+ return this.appInstance;
524
+ }
525
+ get server() {
526
+ if (!this.serverInstance) {
527
+ throw new Error("HTTP server is not running.");
528
+ }
529
+ return this.serverInstance;
530
+ }
531
+ close() {
532
+ if (!this.serverInstance) {
533
+ return Promise.resolve();
534
+ }
535
+ return new Promise((resolve, reject) => {
536
+ this.serverInstance?.close((error) => {
537
+ if (error) {
538
+ reject(error);
539
+ return;
540
+ }
541
+ this.serverInstance = void 0;
542
+ this.appInstance = void 0;
543
+ resolve();
544
+ });
545
+ });
546
+ }
547
+ run() {
548
+ const app = createExpressServer({
549
+ controllers: this.options.kernel.getRoutes(),
550
+ routePrefix: this.options.routePrefix
551
+ });
552
+ for (const middleware of this.options.middlewares ?? []) {
553
+ app.use(middleware);
554
+ }
555
+ this.registerErrorHandlers(app);
556
+ this.appInstance = app;
557
+ return new Promise((resolve) => {
558
+ this.serverInstance = app.listen(this.options.port ?? 3e3, () => {
559
+ resolve();
560
+ });
561
+ });
562
+ }
563
+ };
564
+
565
+ // src/adapters/ui/express/RoutePrefix.ts
566
+ var RoutePrefix = class _RoutePrefix {
567
+ value;
568
+ static fromEnvironment(value) {
569
+ return new _RoutePrefix(value);
570
+ }
571
+ constructor(value) {
572
+ if (!value || value === "/") {
573
+ this.value = "";
574
+ return;
575
+ }
576
+ const normalizedPrefix = value.trim().replace(/^\/+/, "").replace(/\/+$/, "");
577
+ this.value = normalizedPrefix ? `/${normalizedPrefix}` : "";
578
+ }
579
+ includes(requestPath) {
580
+ return this.value.length > 0 && (requestPath === this.value || requestPath.startsWith(`${this.value}/`));
581
+ }
582
+ isEmpty() {
583
+ return this.value.length === 0;
584
+ }
585
+ toString() {
586
+ return this.value;
587
+ }
588
+ };
589
+
590
+ // src/adapters/ui/routes/Route.ts
591
+ var Route = class {
592
+ get(service) {
593
+ return Kernel.di.getService(service);
594
+ }
595
+ };
596
+ export {
597
+ ConsoleKernelLogger,
598
+ Consumer,
599
+ ExpressKernelServer,
600
+ InMemoryEventBus,
601
+ InMemoryPubSub,
602
+ InMemoryRepository,
603
+ InvalidDomainEventError,
604
+ MongoRepository,
605
+ NoFailedMessagesError,
606
+ Route,
607
+ RoutePrefix
608
+ };
609
+ //# sourceMappingURL=index.js.map