@privateaim/server-kit 0.8.21 → 0.8.23

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 (105) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/dist/core/component/caller/compound/index.d.ts +2 -0
  3. package/dist/core/component/caller/compound/index.d.ts.map +1 -0
  4. package/dist/core/component/caller/compound/module.d.ts +9 -0
  5. package/dist/core/component/caller/compound/module.d.ts.map +1 -0
  6. package/dist/core/component/caller/direct/index.d.ts +3 -0
  7. package/dist/core/component/caller/direct/index.d.ts.map +1 -0
  8. package/dist/core/component/caller/direct/module.d.ts +23 -0
  9. package/dist/core/component/caller/direct/module.d.ts.map +1 -0
  10. package/dist/core/component/caller/direct/types.d.ts +5 -0
  11. package/dist/core/component/caller/direct/types.d.ts.map +1 -0
  12. package/dist/core/component/caller/index.d.ts +6 -0
  13. package/dist/core/component/caller/index.d.ts.map +1 -0
  14. package/dist/core/component/caller/queue-dispatch/index.d.ts +2 -0
  15. package/dist/core/component/caller/queue-dispatch/index.d.ts.map +1 -0
  16. package/dist/core/component/caller/queue-dispatch/module.d.ts +9 -0
  17. package/dist/core/component/caller/queue-dispatch/module.d.ts.map +1 -0
  18. package/dist/core/component/caller/queue-dispatch/types.d.ts +6 -0
  19. package/dist/core/component/caller/queue-dispatch/types.d.ts.map +1 -0
  20. package/dist/core/component/caller/queue-worker/index.d.ts +2 -0
  21. package/dist/core/component/caller/queue-worker/index.d.ts.map +1 -0
  22. package/dist/core/component/caller/queue-worker/module.d.ts +17 -0
  23. package/dist/core/component/caller/queue-worker/module.d.ts.map +1 -0
  24. package/dist/core/component/caller/queue-worker/types.d.ts +6 -0
  25. package/dist/core/component/caller/queue-worker/types.d.ts.map +1 -0
  26. package/dist/core/component/caller/types.d.ts +13 -0
  27. package/dist/core/component/caller/types.d.ts.map +1 -0
  28. package/dist/core/component/error.d.ts +1 -1
  29. package/dist/core/component/error.d.ts.map +1 -1
  30. package/dist/core/component/handler/index.d.ts +1 -1
  31. package/dist/core/component/handler/index.d.ts.map +1 -1
  32. package/dist/core/component/handler/types.d.ts +8 -7
  33. package/dist/core/component/handler/types.d.ts.map +1 -1
  34. package/dist/core/component/index.d.ts +2 -0
  35. package/dist/core/component/index.d.ts.map +1 -1
  36. package/dist/core/component/module.d.ts +23 -0
  37. package/dist/core/component/module.d.ts.map +1 -0
  38. package/dist/core/component/type.d.ts +26 -9
  39. package/dist/core/component/type.d.ts.map +1 -1
  40. package/dist/core/index.d.ts +0 -1
  41. package/dist/core/index.d.ts.map +1 -1
  42. package/dist/core/queue-router/types.d.ts +2 -1
  43. package/dist/core/queue-router/types.d.ts.map +1 -1
  44. package/dist/index.mjs +388 -290
  45. package/dist/index.mjs.map +1 -1
  46. package/dist/services/cache/adapters/memory.d.ts +3 -3
  47. package/dist/services/cache/adapters/memory.d.ts.map +1 -1
  48. package/dist/services/index.d.ts +1 -0
  49. package/dist/services/index.d.ts.map +1 -1
  50. package/dist/services/redis/module.d.ts +4 -2
  51. package/dist/services/redis/module.d.ts.map +1 -1
  52. package/dist/services/task-manager/helpers.d.ts +3 -0
  53. package/dist/services/task-manager/helpers.d.ts.map +1 -0
  54. package/dist/services/task-manager/index.d.ts +3 -0
  55. package/dist/services/task-manager/index.d.ts.map +1 -0
  56. package/dist/services/task-manager/module.d.ts +33 -0
  57. package/dist/services/task-manager/module.d.ts.map +1 -0
  58. package/dist/services/task-manager/types.d.ts +16 -0
  59. package/dist/services/task-manager/types.d.ts.map +1 -0
  60. package/package.json +16 -15
  61. package/src/core/{queue → component/caller/compound}/index.ts +2 -3
  62. package/src/core/component/caller/compound/module.ts +42 -0
  63. package/src/core/component/caller/direct/index.ts +9 -0
  64. package/src/core/component/caller/direct/module.ts +81 -0
  65. package/src/core/component/caller/direct/types.ts +13 -0
  66. package/src/core/component/caller/index.ts +13 -0
  67. package/src/core/component/caller/queue-dispatch/index.ts +8 -0
  68. package/src/core/component/caller/queue-dispatch/module.ts +49 -0
  69. package/src/core/component/caller/queue-dispatch/types.ts +13 -0
  70. package/src/core/component/caller/queue-worker/index.ts +8 -0
  71. package/src/core/component/caller/queue-worker/module.ts +93 -0
  72. package/src/core/component/caller/queue-worker/types.ts +13 -0
  73. package/src/core/component/caller/types.ts +33 -0
  74. package/src/core/component/error.ts +1 -1
  75. package/src/core/component/handler/index.ts +1 -1
  76. package/src/core/component/handler/types.ts +12 -10
  77. package/src/core/component/index.ts +2 -0
  78. package/src/core/component/module.ts +160 -0
  79. package/src/core/component/type.ts +45 -19
  80. package/src/core/index.ts +0 -1
  81. package/src/core/queue-router/types.ts +4 -2
  82. package/src/services/cache/adapters/memory.ts +7 -4
  83. package/src/services/index.ts +1 -0
  84. package/src/services/redis/module.ts +4 -1
  85. package/src/services/task-manager/helpers.ts +15 -0
  86. package/src/services/task-manager/index.ts +9 -0
  87. package/src/services/task-manager/module.ts +131 -0
  88. package/src/services/task-manager/types.ts +32 -0
  89. package/test/unit/logger.spec.ts +1 -0
  90. package/test/unit/memory-cache.spec.ts +1 -0
  91. package/test/vitest.config.ts +17 -0
  92. package/dist/core/component/handler/module.d.ts +0 -13
  93. package/dist/core/component/handler/module.d.ts.map +0 -1
  94. package/dist/core/queue/index.d.ts +0 -3
  95. package/dist/core/queue/index.d.ts.map +0 -1
  96. package/dist/core/queue/type.d.ts +0 -12
  97. package/dist/core/queue/type.d.ts.map +0 -1
  98. package/dist/core/queue/utils.d.ts +0 -3
  99. package/dist/core/queue/utils.d.ts.map +0 -1
  100. package/dist/index.cjs +0 -970
  101. package/dist/index.cjs.map +0 -1
  102. package/src/core/component/handler/module.ts +0 -74
  103. package/src/core/queue/type.ts +0 -23
  104. package/src/core/queue/utils.ts +0 -17
  105. package/test/jest.config.js +0 -33
package/dist/index.cjs DELETED
@@ -1,970 +0,0 @@
1
- 'use strict';
2
-
3
- var kit = require('@privateaim/kit');
4
- var amqpExtension = require('amqp-extension');
5
- var singa = require('singa');
6
- var vault = require('@hapic/vault');
7
- var coreHttpKit = require('@authup/core-http-kit');
8
- var redisExtension = require('redis-extension');
9
- var TTLCache = require('@isaacs/ttlcache');
10
- var winston = require('winston');
11
- var envix = require('envix');
12
- var Transport = require('winston-transport');
13
- var redisEmitter = require('@socket.io/redis-emitter');
14
-
15
- /*
16
- * Copyright (c) 2025.
17
- * Author Peter Placzek (tada5hi)
18
- * For the full copyright and license information,
19
- * view the LICENSE file that was distributed with this source code.
20
- */ var EnvironmentName = /*#__PURE__*/ function(EnvironmentName) {
21
- EnvironmentName["TEST"] = "test";
22
- EnvironmentName["DEVELOPMENT"] = "development";
23
- EnvironmentName["PRODUCTION"] = "production";
24
- return EnvironmentName;
25
- }({});
26
-
27
- /*
28
- * Copyright (c) 2023-2024.
29
- * Author Peter Placzek (tada5hi)
30
- * For the full copyright and license information,
31
- * view the LICENSE file that was distributed with this source code.
32
- */ function _define_property$9(obj, key, value) {
33
- if (key in obj) {
34
- Object.defineProperty(obj, key, {
35
- value: value,
36
- enumerable: true,
37
- configurable: true,
38
- writable: true
39
- });
40
- } else {
41
- obj[key] = value;
42
- }
43
- return obj;
44
- }
45
- class ComponentError extends Error {
46
- constructor(input){
47
- super(input.message, {
48
- cause: input.cause
49
- }), _define_property$9(this, "code", void 0);
50
- this.code = input.code;
51
- }
52
- }
53
-
54
- /*
55
- * Copyright (c) 2022-2024.
56
- * Author Peter Placzek (tada5hi)
57
- * For the full copyright and license information,
58
- * view the LICENSE file that was distributed with this source code.
59
- */ function hasOwnProperty(obj, prop) {
60
- return Object.prototype.hasOwnProperty.call(obj, prop);
61
- }
62
-
63
- function isComponentHandlerFn(input) {
64
- return typeof input === 'function';
65
- }
66
-
67
- /*
68
- * Copyright (c) 2025.
69
- * Author Peter Placzek (tada5hi)
70
- * For the full copyright and license information,
71
- * view the LICENSE file that was distributed with this source code.
72
- */ function _define_property$8(obj, key, value) {
73
- if (key in obj) {
74
- Object.defineProperty(obj, key, {
75
- value: value,
76
- enumerable: true,
77
- configurable: true,
78
- writable: true
79
- });
80
- } else {
81
- obj[key] = value;
82
- }
83
- return obj;
84
- }
85
- class ComponentHandlers {
86
- mount(key, fn) {
87
- this.handlers[key] = fn;
88
- }
89
- unmount(key) {
90
- delete this.handlers[key];
91
- }
92
- async initialize() {
93
- if (this.initializing || this.initialized) {
94
- return;
95
- }
96
- this.initializing = true;
97
- const keys = Object.keys(this.handlers);
98
- for(let i = 0; i < keys.length; i++){
99
- const handler = this.handlers[keys[i]];
100
- if (!isComponentHandlerFn(handler) && handler.initialize) {
101
- await handler.initialize();
102
- }
103
- }
104
- this.initialized = true;
105
- this.initializing = false;
106
- }
107
- async execute(key, value = {}, metadata = {}) {
108
- await this.initialize();
109
- const handler = this.handlers[key];
110
- if (!handler) {
111
- return;
112
- }
113
- if (isComponentHandlerFn(handler)) {
114
- await handler(value, {
115
- key,
116
- metadata
117
- });
118
- } else {
119
- await handler.handle(value, {
120
- key,
121
- metadata
122
- });
123
- }
124
- }
125
- constructor(){
126
- _define_property$8(this, "initializing", void 0);
127
- _define_property$8(this, "initialized", void 0);
128
- _define_property$8(this, "handlers", void 0);
129
- this.initializing = false;
130
- this.initialized = false;
131
- this.handlers = {};
132
- }
133
- }
134
-
135
- function isComponentError(input) {
136
- if (!kit.isObject(input)) {
137
- return false;
138
- }
139
- if (typeof input.message !== 'undefined' && typeof input.message !== 'string') {
140
- return false;
141
- }
142
- if (typeof input.code === 'undefined') {
143
- return true;
144
- }
145
- return typeof input.code === 'string' || input.code === null;
146
- }
147
-
148
- function isQueuePayload(input) {
149
- return kit.isObject(input) && kit.hasOwnProperty(input, 'data') && kit.isObject(input.data) && kit.hasOwnProperty(input, 'metadata') && kit.isObject(input.metadata);
150
- }
151
-
152
- /*
153
- * Copyright (c) 2024.
154
- * Author Peter Placzek (tada5hi)
155
- * For the full copyright and license information,
156
- * view the LICENSE file that was distributed with this source code.
157
- */ var QueueRouterRoutingType = /*#__PURE__*/ function(QueueRouterRoutingType) {
158
- QueueRouterRoutingType["WORK"] = "work";
159
- QueueRouterRoutingType["PUB_SUB"] = "pubSub";
160
- return QueueRouterRoutingType;
161
- }({});
162
-
163
- function buildQueueRouterPublishPayload(input) {
164
- return {
165
- id: input.id || kit.createNanoID(),
166
- type: input.type,
167
- data: input.data || {},
168
- metadata: {
169
- timestamp: Date.now(),
170
- ...input.metadata
171
- }
172
- };
173
- }
174
- function isQueueRouterPayload(input) {
175
- return kit.isObject(input) && typeof input.id === 'string' && typeof input.type === 'string' && kit.hasOwnProperty(input, 'data') && kit.isObject(input.data) && kit.hasOwnProperty(input, 'metadata') && kit.isObject(input.metadata);
176
- }
177
-
178
- const instance$a = singa.singa({
179
- name: 'amqp'
180
- });
181
- function setAmqpClientFactory(factory) {
182
- instance$a.setFactory(factory);
183
- }
184
- function isAmqpClientUsable() {
185
- return instance$a.has() || instance$a.hasFactory();
186
- }
187
- function useAmqpClient() {
188
- return instance$a.use();
189
- }
190
-
191
- const instance$9 = singa.singa({
192
- name: 'vault'
193
- });
194
- function setVaultFactory(factory) {
195
- instance$9.setFactory(factory);
196
- }
197
- function isVaultClientUsable() {
198
- return instance$9.has() || instance$9.hasFactory();
199
- }
200
- function useVaultClient() {
201
- return instance$9.use();
202
- }
203
-
204
- function guessAuthupTokenCreatorOptions() {
205
- let options;
206
- if (isVaultClientUsable()) {
207
- options = {
208
- type: 'robotInVault',
209
- name: 'system',
210
- vault: useVaultClient()
211
- };
212
- } else {
213
- options = {
214
- type: 'user',
215
- name: 'admin',
216
- password: 'start123'
217
- };
218
- }
219
- return options;
220
- }
221
-
222
- const instance$8 = singa.singa({
223
- name: 'clientAuthenticationHook'
224
- });
225
- function setClientAuthenticationHookFactory(factory) {
226
- instance$8.setFactory(factory);
227
- }
228
- function isClientAuthenticationHookUsable() {
229
- return instance$8.has() || instance$8.hasFactory();
230
- }
231
- function useClientAuthenticationHook() {
232
- return instance$8.use();
233
- }
234
-
235
- class AuthupClient extends coreHttpKit.Client {
236
- constructor(options = {}){
237
- super(options);
238
- if (!isClientAuthenticationHookUsable()) {
239
- setClientAuthenticationHookFactory(()=>new coreHttpKit.ClientAuthenticationHook({
240
- baseURL: options.baseURL,
241
- tokenCreator: options.tokenCreator || guessAuthupTokenCreatorOptions()
242
- }));
243
- }
244
- const hook = useClientAuthenticationHook();
245
- hook.attach(this);
246
- }
247
- }
248
-
249
- const instance$7 = singa.singa({
250
- name: 'authup'
251
- });
252
- function useAuthupClient() {
253
- return instance$7.use();
254
- }
255
- function isAuthupClientUsable() {
256
- return instance$7.has() || instance$7.hasFactory();
257
- }
258
- function setAuthupClientFactory(factory) {
259
- instance$7.setFactory(factory);
260
- }
261
-
262
- const instance$6 = singa.singa({
263
- name: 'redis'
264
- });
265
- function setRedisFactory(factory) {
266
- instance$6.setFactory(factory);
267
- }
268
- function isRedisClientUsable() {
269
- return instance$6.has() || instance$6.hasFactory();
270
- }
271
- function setRedisClient(input) {
272
- instance$6.set(input);
273
- }
274
- function useRedisClient() {
275
- return instance$6.use();
276
- }
277
-
278
- const instance$5 = singa.singa({
279
- name: 'redisPublish',
280
- factory: ()=>useRedisClient().duplicate()
281
- });
282
- function useRedisPublishClient() {
283
- return instance$5.use();
284
- }
285
-
286
- const instance$4 = singa.singa({
287
- name: 'redisSubscribe',
288
- factory: ()=>useRedisClient().duplicate()
289
- });
290
- function useRedisSubscribeClient() {
291
- return instance$4.use();
292
- }
293
-
294
- /*
295
- * Copyright (c) 2024-2024.
296
- * Author Peter Placzek (tada5hi)
297
- * For the full copyright and license information,
298
- * view the LICENSE file that was distributed with this source code.
299
- */ function _define_property$7(obj, key, value) {
300
- if (key in obj) {
301
- Object.defineProperty(obj, key, {
302
- value: value,
303
- enumerable: true,
304
- configurable: true,
305
- writable: true
306
- });
307
- } else {
308
- obj[key] = value;
309
- }
310
- return obj;
311
- }
312
- class MemoryCacheAdapter {
313
- async get(key) {
314
- return this.instance.get(key);
315
- }
316
- async has(key) {
317
- return this.instance.has(key);
318
- }
319
- async set(key, value, options) {
320
- this.instance.set(key, value, {
321
- ttl: options.ttl
322
- });
323
- }
324
- async drop(key) {
325
- this.instance.delete(key);
326
- }
327
- async dropMany(keys) {
328
- for(let i = 0; i < keys.length; i++){
329
- this.instance.delete(keys[i]);
330
- }
331
- }
332
- async clear(options = {}) {
333
- if (options.prefix) {
334
- const keys = this.instance.keys();
335
- let iterator = keys.next();
336
- while(!iterator.done){
337
- if (iterator.value.startsWith(options.prefix)) {
338
- this.instance.delete(iterator.value);
339
- }
340
- iterator = keys.next();
341
- }
342
- return;
343
- }
344
- this.instance.clear();
345
- }
346
- constructor(options = {}){
347
- _define_property$7(this, "instance", void 0);
348
- this.instance = new TTLCache({
349
- checkAgeOnGet: true,
350
- ttl: Infinity,
351
- ...options || {}
352
- });
353
- }
354
- }
355
-
356
- /*
357
- * Copyright (c) 2024-2024.
358
- * Author Peter Placzek (tada5hi)
359
- * For the full copyright and license information,
360
- * view the LICENSE file that was distributed with this source code.
361
- */ function _define_property$6(obj, key, value) {
362
- if (key in obj) {
363
- Object.defineProperty(obj, key, {
364
- value: value,
365
- enumerable: true,
366
- configurable: true,
367
- writable: true
368
- });
369
- } else {
370
- obj[key] = value;
371
- }
372
- return obj;
373
- }
374
- class RedisCacheAdapter {
375
- async get(key) {
376
- return this.instance.get(key);
377
- }
378
- async has(key) {
379
- const exists = await this.client.exists(key);
380
- return !!exists;
381
- }
382
- async set(key, value, options) {
383
- await this.instance.set(key, value, {
384
- milliseconds: options.ttl
385
- });
386
- }
387
- async drop(key) {
388
- await this.instance.drop(key);
389
- }
390
- async dropMany(keys) {
391
- const pipeline = this.client.pipeline();
392
- for(let i = 0; i < keys.length; i++){
393
- pipeline.del(keys[i]);
394
- }
395
- await pipeline.exec();
396
- }
397
- async clear(options = {}) {
398
- if (options.prefix) {
399
- const pipeline = this.client.pipeline();
400
- const keys = await this.client.keys(`${options.prefix}*`);
401
- for(let i = 0; i < keys.length; i++){
402
- pipeline.del(keys[i]);
403
- }
404
- await pipeline.exec();
405
- return;
406
- }
407
- await this.client.flushdb();
408
- }
409
- constructor(){
410
- _define_property$6(this, "client", void 0);
411
- _define_property$6(this, "instance", void 0);
412
- this.client = useRedisClient();
413
- this.instance = new redisExtension.JsonAdapter(this.client);
414
- }
415
- }
416
-
417
- function createCacheAdapter() {
418
- if (isRedisClientUsable()) {
419
- return new RedisCacheAdapter();
420
- }
421
- return new MemoryCacheAdapter();
422
- }
423
-
424
- function buildCacheKey(options) {
425
- return redisExtension.buildKeyPath(options);
426
- }
427
-
428
- /*
429
- * Copyright (c) 2024.
430
- * Author Peter Placzek (tada5hi)
431
- * For the full copyright and license information,
432
- * view the LICENSE file that was distributed with this source code.
433
- */ function _define_property$5(obj, key, value) {
434
- if (key in obj) {
435
- Object.defineProperty(obj, key, {
436
- value: value,
437
- enumerable: true,
438
- configurable: true,
439
- writable: true
440
- });
441
- } else {
442
- obj[key] = value;
443
- }
444
- return obj;
445
- }
446
- class Cache {
447
- async set(key, value, options = {}) {
448
- await this.adapter.set(key, value, options);
449
- }
450
- async get(key) {
451
- return this.adapter.get(key);
452
- }
453
- async has(key) {
454
- return this.adapter.has(key);
455
- }
456
- async drop(key) {
457
- return this.adapter.drop(key);
458
- }
459
- async dropMany(keys) {
460
- return this.adapter.dropMany(keys);
461
- }
462
- async clear(options = {}) {
463
- return this.adapter.clear(options);
464
- }
465
- constructor(adapter){
466
- _define_property$5(this, "adapter", void 0);
467
- this.adapter = adapter;
468
- }
469
- }
470
-
471
- const instance$3 = singa.singa({
472
- name: 'cache',
473
- factory: ()=>{
474
- let adapter;
475
- if (isRedisClientUsable()) {
476
- adapter = new RedisCacheAdapter();
477
- } else {
478
- adapter = new MemoryCacheAdapter();
479
- }
480
- return new Cache(adapter);
481
- }
482
- });
483
- function useCache() {
484
- return instance$3.use();
485
- }
486
-
487
- class LoggerConsoleTransport extends winston.transports.Console {
488
- constructor(options = {}){
489
- super({
490
- level: envix.read('env') === EnvironmentName.PRODUCTION ? 'info' : 'debug',
491
- ...options
492
- });
493
- }
494
- }
495
- function createLoggerConsoleTransport(options = {}) {
496
- return new LoggerConsoleTransport(options);
497
- }
498
-
499
- /*
500
- * Copyright (c) 2025.
501
- * Author Peter Placzek (tada5hi)
502
- * For the full copyright and license information,
503
- * view the LICENSE file that was distributed with this source code.
504
- */ function _define_property$4(obj, key, value) {
505
- if (key in obj) {
506
- Object.defineProperty(obj, key, {
507
- value: value,
508
- enumerable: true,
509
- configurable: true,
510
- writable: true
511
- });
512
- } else {
513
- obj[key] = value;
514
- }
515
- return obj;
516
- }
517
- class LoggerMemoryTransport extends Transport {
518
- log(info, next) {
519
- this.items.push(info);
520
- next();
521
- }
522
- constructor(...args){
523
- super(...args), _define_property$4(this, "items", []);
524
- }
525
- }
526
-
527
- function createLogger(ctx = {}) {
528
- const transports = [
529
- ...ctx.transports || []
530
- ];
531
- if (transports.length === 0) {
532
- transports.push(createLoggerConsoleTransport());
533
- }
534
- return winston.createLogger({
535
- format: winston.format.combine(winston.format.errors({
536
- stack: true
537
- }), winston.format.timestamp(), winston.format.simple()),
538
- level: 'debug',
539
- transports,
540
- // todo: deeply merge options
541
- ...ctx.options || {}
542
- });
543
- }
544
-
545
- const instance$2 = singa.singa({
546
- name: 'logger'
547
- });
548
- function setLoggerFactory(factory) {
549
- instance$2.setFactory(factory);
550
- }
551
- function isLoggerUsable() {
552
- return instance$2.has() || instance$2.hasFactory();
553
- }
554
- function setLogger(input) {
555
- instance$2.set(input);
556
- }
557
- function useLogger() {
558
- return instance$2.use();
559
- }
560
-
561
- /*
562
- * Copyright (c) 2024-2025.
563
- * Author Peter Placzek (tada5hi)
564
- * For the full copyright and license information,
565
- * view the LICENSE file that was distributed with this source code.
566
- */ function _define_property$3(obj, key, value) {
567
- if (key in obj) {
568
- Object.defineProperty(obj, key, {
569
- value: value,
570
- enumerable: true,
571
- configurable: true,
572
- writable: true
573
- });
574
- } else {
575
- obj[key] = value;
576
- }
577
- return obj;
578
- }
579
- class EntityEventPublisher {
580
- register(consumer) {
581
- this.handlers.add(consumer);
582
- }
583
- async safePublish(ctx) {
584
- try {
585
- await this.publish(ctx);
586
- } catch (e) {
587
- if (isLoggerUsable()) {
588
- useLogger().error(`Publishing event ${kit.buildDomainEventFullName(ctx.metadata.ref_type, ctx.metadata.event)} failed`);
589
- useLogger().error(e);
590
- }
591
- }
592
- }
593
- async publish(ctx) {
594
- if (isLoggerUsable()) {
595
- useLogger().debug(`Publishing event ${kit.buildDomainEventFullName(ctx.metadata.ref_type, ctx.metadata.event)}`);
596
- }
597
- let destinations = [];
598
- if (typeof ctx.destinations === 'function') {
599
- destinations = ctx.destinations(ctx.data);
600
- } else {
601
- destinations = ctx.destinations;
602
- }
603
- const consumeContext = {
604
- ...ctx,
605
- destinations
606
- };
607
- const handlers = this.handlers.values();
608
- while(true){
609
- const handler = handlers.next();
610
- if (handler.done) {
611
- return;
612
- }
613
- await handler.value.handle(consumeContext);
614
- }
615
- }
616
- constructor(){
617
- _define_property$3(this, "handlers", void 0);
618
- this.handlers = new Set();
619
- }
620
- }
621
-
622
- function transformEntityEventData(input) {
623
- if (kit.isObject(input)) {
624
- const keys = Object.keys(input);
625
- for(let i = 0; i < keys.length; i++){
626
- const value = input[keys[i]];
627
- if (value instanceof Date) {
628
- input[keys[i]] = value.toISOString();
629
- }
630
- }
631
- }
632
- return input;
633
- }
634
-
635
- /*
636
- * Copyright (c) 2025.
637
- * Author Peter Placzek (tada5hi)
638
- * For the full copyright and license information,
639
- * view the LICENSE file that was distributed with this source code.
640
- */ function stringify(input) {
641
- return typeof input === 'string' ? input : input.join('/');
642
- }
643
- function buildEntityEventRedisChannel(channel, namespace) {
644
- const channelNormalized = stringify(channel);
645
- let namespaceNormalized;
646
- if (namespace) {
647
- namespaceNormalized = stringify(namespace);
648
- }
649
- if (typeof namespaceNormalized === 'undefined') {
650
- return channelNormalized;
651
- }
652
- return `${namespaceNormalized}/${channelNormalized}`;
653
- }
654
-
655
- /*
656
- * Copyright (c) 2023-2024.
657
- * Author Peter Placzek (tada5hi)
658
- * For the full copyright and license information,
659
- * view the LICENSE file that was distributed with this source code.
660
- */ function _define_property$2(obj, key, value) {
661
- if (key in obj) {
662
- Object.defineProperty(obj, key, {
663
- value: value,
664
- enumerable: true,
665
- configurable: true,
666
- writable: true
667
- });
668
- } else {
669
- obj[key] = value;
670
- }
671
- return obj;
672
- }
673
- class EntityEventRedisHandler {
674
- async handle(ctx) {
675
- const payload = {
676
- type: ctx.metadata.ref_type,
677
- event: ctx.metadata.event,
678
- data: transformEntityEventData(ctx.data)
679
- };
680
- const payloadSerialized = JSON.stringify(payload);
681
- const pipeline = this.driver.pipeline();
682
- for(let i = 0; i < ctx.destinations.length; i++){
683
- const destination = ctx.destinations[i];
684
- const key = buildEntityEventRedisChannel(destination.channel, destination.namespace);
685
- pipeline.publish(key, payloadSerialized);
686
- }
687
- await pipeline.exec();
688
- }
689
- constructor(client){
690
- _define_property$2(this, "driver", void 0);
691
- this.driver = client;
692
- }
693
- }
694
-
695
- /*
696
- * Copyright (c) 2022-2024.
697
- * Author Peter Placzek (tada5hi)
698
- * For the full copyright and license information,
699
- * view the LICENSE file that was distributed with this source code.
700
- */ function _define_property$1(obj, key, value) {
701
- if (key in obj) {
702
- Object.defineProperty(obj, key, {
703
- value: value,
704
- enumerable: true,
705
- configurable: true,
706
- writable: true
707
- });
708
- } else {
709
- obj[key] = value;
710
- }
711
- return obj;
712
- }
713
- class EntityEventSocketHandler {
714
- async handle(ctx) {
715
- ctx.data = transformEntityEventData(ctx.data);
716
- for(let i = 0; i < ctx.destinations.length; i++){
717
- const destination = ctx.destinations[i];
718
- const namespace = this.buildNamespace(destination.namespace);
719
- const roomName = this.buildChannel(destination.channel);
720
- const fullEventName = kit.buildDomainEventFullName(ctx.metadata.ref_type, ctx.metadata.event);
721
- const emitter = new redisEmitter.Emitter(this.client, {}, namespace);
722
- emitter.in(roomName).emit(fullEventName, {
723
- data: ctx.data,
724
- meta: {
725
- refType: ctx.metadata.ref_type,
726
- refId: ctx.metadata.ref_id,
727
- event: ctx.metadata.event,
728
- namespace,
729
- roomName
730
- }
731
- });
732
- }
733
- }
734
- buildNamespace(namespace) {
735
- if (typeof namespace === 'undefined') {
736
- return '/';
737
- }
738
- if (typeof namespace === 'string') {
739
- return namespace.startsWith('/') ? namespace : `/${namespace}`;
740
- }
741
- return `/${namespace.join('/')}`;
742
- }
743
- buildChannel(channel) {
744
- if (typeof channel === 'string') {
745
- return channel;
746
- }
747
- return channel.join('/');
748
- }
749
- constructor(client){
750
- _define_property$1(this, "client", void 0);
751
- this.client = client;
752
- }
753
- }
754
-
755
- const instance$1 = singa.singa({
756
- name: 'entityEventPublisher'
757
- });
758
- function setEntityEventPublisherFactory(factory) {
759
- instance$1.setFactory(factory);
760
- }
761
- function useEntityEventPublisher() {
762
- return instance$1.use();
763
- }
764
-
765
- /*
766
- * Copyright (c) 2024.
767
- * Author Peter Placzek (tada5hi)
768
- * For the full copyright and license information,
769
- * view the LICENSE file that was distributed with this source code.
770
- */ function _define_property(obj, key, value) {
771
- if (key in obj) {
772
- Object.defineProperty(obj, key, {
773
- value: value,
774
- enumerable: true,
775
- configurable: true,
776
- writable: true
777
- });
778
- } else {
779
- obj[key] = value;
780
- }
781
- return obj;
782
- }
783
- class QueueRouter {
784
- //----------------------------------------------------------------
785
- publish(message, options = {}) {
786
- var _options;
787
- (_options = options).logging ?? (_options.logging = true);
788
- let exchange;
789
- if (message.metadata.routing.type === 'work') {
790
- exchange = this.driver.of({
791
- type: amqpExtension.ExchangeType.DIRECT,
792
- name: message.metadata.routing.namespace || ''
793
- });
794
- } else {
795
- exchange = this.driver.of({
796
- type: amqpExtension.ExchangeType.TOPIC,
797
- name: message.metadata.routing.namespace || 'FLAME'
798
- });
799
- }
800
- if (options.logging && isLoggerUsable()) {
801
- useLogger().debug(`Publishing queue message ${message.type} in ${message.metadata.routing.key}`);
802
- }
803
- return exchange.publish(message.metadata.routing.key, message, {
804
- type: message.type,
805
- messageId: message.id,
806
- persistent: message.metadata.persistent ?? message.metadata.routing.type === QueueRouterRoutingType.WORK
807
- });
808
- }
809
- consumeAny(routing, fn) {
810
- return this.consume(routing, {
811
- $any: (payload)=>fn(payload)
812
- });
813
- }
814
- consume(routing, handlers) {
815
- let exchange;
816
- if (routing.type === 'work') {
817
- exchange = this.driver.of({
818
- type: amqpExtension.ExchangeType.DIRECT,
819
- name: routing.namespace || ''
820
- });
821
- } else {
822
- exchange = this.driver.of({
823
- type: amqpExtension.ExchangeType.TOPIC,
824
- name: routing.namespace || 'FLAME'
825
- });
826
- }
827
- return exchange.consume(routing.key, {
828
- prefetchCount: routing.type === QueueRouterRoutingType.WORK ? 1 : undefined,
829
- // noAck: routing.type !== QueueRouterRoutingType.WORK,
830
- requeueOnFailure: routing.type === QueueRouterRoutingType.WORK
831
- }, {
832
- $any: async (input)=>{
833
- const payload = JSON.parse(input.content.toString('utf-8'));
834
- if (!isQueueRouterPayload(payload)) {
835
- return;
836
- }
837
- if (input.fields.redelivered) {
838
- if (isLoggerUsable()) {
839
- useLogger().debug(`Queue message ${input.properties.type} in ${routing.key} is not processed again.`);
840
- }
841
- return;
842
- }
843
- if (isLoggerUsable()) {
844
- useLogger().debug(`Consuming queue message ${input.properties.type} in ${routing.key}`);
845
- }
846
- let handler;
847
- if (typeof input.properties.type === 'string' && handlers[input.properties.type]) {
848
- handler = handlers[input.properties.type];
849
- } else {
850
- handler = handlers.$any;
851
- }
852
- if (typeof handler !== 'function') {
853
- if (isLoggerUsable()) {
854
- useLogger().debug(`No queue handler to consume message ${input.properties.type} in ${routing.key}`);
855
- }
856
- return;
857
- }
858
- try {
859
- await handler(payload);
860
- } catch (e) {
861
- if (isLoggerUsable()) {
862
- useLogger().error(e);
863
- }
864
- throw e;
865
- }
866
- }
867
- });
868
- }
869
- //----------------------------------------------------------------
870
- constructor(driver){
871
- _define_property(this, "driver", void 0);
872
- this.driver = driver;
873
- }
874
- }
875
-
876
- const instance = singa.singa({
877
- name: 'queueRouter',
878
- factory: ()=>{
879
- const amqp = useAmqpClient();
880
- return new QueueRouter(amqp);
881
- }
882
- });
883
- function isQueueRouterUsable() {
884
- return instance.has() || isAmqpClientUsable();
885
- }
886
- function useQueueRouter() {
887
- return instance.use();
888
- }
889
-
890
- Object.defineProperty(exports, "AmqpClient", {
891
- enumerable: true,
892
- get: function () { return amqpExtension.Client; }
893
- });
894
- Object.defineProperty(exports, "VaultClient", {
895
- enumerable: true,
896
- get: function () { return vault.VaultClient; }
897
- });
898
- Object.defineProperty(exports, "createVaultClient", {
899
- enumerable: true,
900
- get: function () { return vault.createClient; }
901
- });
902
- Object.defineProperty(exports, "RedisClient", {
903
- enumerable: true,
904
- get: function () { return redisExtension.Client; }
905
- });
906
- Object.defineProperty(exports, "RedisClientOptions", {
907
- enumerable: true,
908
- get: function () { return redisExtension.ClientOptions; }
909
- });
910
- Object.defineProperty(exports, "buildRedisKeyPath", {
911
- enumerable: true,
912
- get: function () { return redisExtension.buildKeyPath; }
913
- });
914
- Object.defineProperty(exports, "createRedisClient", {
915
- enumerable: true,
916
- get: function () { return redisExtension.createClient; }
917
- });
918
- exports.AuthupClient = AuthupClient;
919
- exports.Cache = Cache;
920
- exports.ComponentError = ComponentError;
921
- exports.ComponentHandlers = ComponentHandlers;
922
- exports.EntityEventPublisher = EntityEventPublisher;
923
- exports.EntityEventRedisHandler = EntityEventRedisHandler;
924
- exports.EntityEventSocketHandler = EntityEventSocketHandler;
925
- exports.EnvironmentName = EnvironmentName;
926
- exports.LoggerConsoleTransport = LoggerConsoleTransport;
927
- exports.LoggerMemoryTransport = LoggerMemoryTransport;
928
- exports.MemoryCacheAdapter = MemoryCacheAdapter;
929
- exports.QueueRouter = QueueRouter;
930
- exports.QueueRouterRoutingType = QueueRouterRoutingType;
931
- exports.RedisCacheAdapter = RedisCacheAdapter;
932
- exports.buildCacheKey = buildCacheKey;
933
- exports.buildEntityEventRedisChannel = buildEntityEventRedisChannel;
934
- exports.buildQueueRouterPublishPayload = buildQueueRouterPublishPayload;
935
- exports.createCacheAdapter = createCacheAdapter;
936
- exports.createLogger = createLogger;
937
- exports.createLoggerConsoleTransport = createLoggerConsoleTransport;
938
- exports.guessAuthupTokenCreatorOptions = guessAuthupTokenCreatorOptions;
939
- exports.hasOwnProperty = hasOwnProperty;
940
- exports.isAmqpClientUsable = isAmqpClientUsable;
941
- exports.isAuthupClientUsable = isAuthupClientUsable;
942
- exports.isClientAuthenticationHookUsable = isClientAuthenticationHookUsable;
943
- exports.isComponentError = isComponentError;
944
- exports.isLoggerUsable = isLoggerUsable;
945
- exports.isQueuePayload = isQueuePayload;
946
- exports.isQueueRouterPayload = isQueueRouterPayload;
947
- exports.isQueueRouterUsable = isQueueRouterUsable;
948
- exports.isRedisClientUsable = isRedisClientUsable;
949
- exports.isVaultClientUsable = isVaultClientUsable;
950
- exports.setAmqpClientFactory = setAmqpClientFactory;
951
- exports.setAuthupClientFactory = setAuthupClientFactory;
952
- exports.setClientAuthenticationHookFactory = setClientAuthenticationHookFactory;
953
- exports.setEntityEventPublisherFactory = setEntityEventPublisherFactory;
954
- exports.setLogger = setLogger;
955
- exports.setLoggerFactory = setLoggerFactory;
956
- exports.setRedisClient = setRedisClient;
957
- exports.setRedisFactory = setRedisFactory;
958
- exports.setVaultFactory = setVaultFactory;
959
- exports.useAmqpClient = useAmqpClient;
960
- exports.useAuthupClient = useAuthupClient;
961
- exports.useCache = useCache;
962
- exports.useClientAuthenticationHook = useClientAuthenticationHook;
963
- exports.useEntityEventPublisher = useEntityEventPublisher;
964
- exports.useLogger = useLogger;
965
- exports.useQueueRouter = useQueueRouter;
966
- exports.useRedisClient = useRedisClient;
967
- exports.useRedisPublishClient = useRedisPublishClient;
968
- exports.useRedisSubscribeClient = useRedisSubscribeClient;
969
- exports.useVaultClient = useVaultClient;
970
- //# sourceMappingURL=index.cjs.map