@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.mjs CHANGED
@@ -1,12 +1,12 @@
1
- import { isObject, hasOwnProperty as hasOwnProperty$1, createNanoID, buildDomainEventFullName } from '@privateaim/kit';
1
+ import { createNanoID, isObject, hasOwnProperty as hasOwnProperty$1, buildDomainEventFullName, WEEK_IN_MS, HOUR_IN_MS } from '@privateaim/kit';
2
2
  import { ExchangeType } from 'amqp-extension';
3
3
  export { Client as AmqpClient } from 'amqp-extension';
4
4
  import { singa } from 'singa';
5
5
  export { VaultClient, createClient as createVaultClient } from '@hapic/vault';
6
6
  import { Client, ClientAuthenticationHook } from '@authup/core-http-kit';
7
7
  import { JsonAdapter, buildKeyPath } from 'redis-extension';
8
- export { Client as RedisClient, ClientOptions as RedisClientOptions, buildKeyPath as buildRedisKeyPath, createClient as createRedisClient } from 'redis-extension';
9
- import TTLCache from '@isaacs/ttlcache';
8
+ export { Client as RedisClient, buildKeyPath as buildRedisKeyPath, createClient as createRedisClient } from 'redis-extension';
9
+ import { TTLCache } from '@isaacs/ttlcache';
10
10
  import { transports, createLogger as createLogger$1, format } from 'winston';
11
11
  import { read } from 'envix';
12
12
  import Transport from 'winston-transport';
@@ -24,129 +24,68 @@ import { Emitter } from '@socket.io/redis-emitter';
24
24
  return EnvironmentName;
25
25
  }({});
26
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
27
  /*
68
28
  * Copyright (c) 2025.
69
29
  * Author Peter Placzek (tada5hi)
70
30
  * For the full copyright and license information,
71
31
  * 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();
32
+ */ class CompoundComponentCaller {
33
+ callers;
34
+ constructor(callers){
35
+ this.callers = callers;
36
+ }
37
+ async call(key, ...payload) {
38
+ const [data, metadata] = payload;
39
+ for(let i = 0; i < this.callers.length; i++){
40
+ const caller = this.callers[i];
41
+ try {
42
+ await caller.call(key, data, metadata);
43
+ return;
44
+ } catch (e) {
45
+ // continue;
102
46
  }
103
47
  }
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 = {};
48
+ throw new Error('No caller could call component.');
132
49
  }
133
50
  }
134
51
 
135
- function isComponentError(input) {
136
- if (!isObject(input)) {
137
- return false;
52
+ class DirectComponentCaller {
53
+ component;
54
+ constructor(component){
55
+ this.component = component;
138
56
  }
139
- if (typeof input.message !== 'undefined' && typeof input.message !== 'string') {
140
- return false;
57
+ /**
58
+ * Call a specific handler and collect all unhandled events.
59
+ *
60
+ * @param key
61
+ * @param payload
62
+ */ async call(key, ...payload) {
63
+ const [data, metadata] = payload;
64
+ await this.callAndWait(key, data, metadata);
141
65
  }
142
- if (typeof input.code === 'undefined') {
143
- return true;
66
+ async callWith(key, data, metadata, options) {
67
+ if (!this.component.handle) {
68
+ throw new Error(`Component ${this.component.constructor.name} can not be called.`);
69
+ }
70
+ metadata.correlationId = metadata.correlationId || createNanoID();
71
+ await this.component.handle(key, data, metadata, options);
72
+ }
73
+ /**
74
+ * Call a specific handler and collect all unhandled events.
75
+ *
76
+ * @param key
77
+ * @param payload
78
+ */ async callAndWait(key, ...payload) {
79
+ const [data, metadata] = payload;
80
+ const events = {};
81
+ const options = {
82
+ handle: (childValue, childContext)=>{
83
+ events[childContext.key] = childValue;
84
+ }
85
+ };
86
+ await this.callWith(key, data, metadata, options);
87
+ return events;
144
88
  }
145
- return typeof input.code === 'string' || input.code === null;
146
- }
147
-
148
- function isQueuePayload(input) {
149
- return isObject(input) && hasOwnProperty$1(input, 'data') && isObject(input.data) && hasOwnProperty$1(input, 'metadata') && isObject(input.metadata);
150
89
  }
151
90
 
152
91
  /*
@@ -291,25 +230,15 @@ function useRedisSubscribeClient() {
291
230
  return instance$4.use();
292
231
  }
293
232
 
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
233
+ class MemoryCacheAdapter {
234
+ instance;
235
+ constructor(options = {}){
236
+ this.instance = new TTLCache({
237
+ checkAgeOnGet: true,
238
+ ttl: Infinity,
239
+ ...options || {}
306
240
  });
307
- } else {
308
- obj[key] = value;
309
241
  }
310
- return obj;
311
- }
312
- class MemoryCacheAdapter {
313
242
  async get(key) {
314
243
  return this.instance.get(key);
315
244
  }
@@ -334,7 +263,7 @@ class MemoryCacheAdapter {
334
263
  const keys = this.instance.keys();
335
264
  let iterator = keys.next();
336
265
  while(!iterator.done){
337
- if (iterator.value.startsWith(options.prefix)) {
266
+ if (typeof iterator.value === 'string' && iterator.value.startsWith(options.prefix)) {
338
267
  this.instance.delete(iterator.value);
339
268
  }
340
269
  iterator = keys.next();
@@ -343,35 +272,15 @@ class MemoryCacheAdapter {
343
272
  }
344
273
  this.instance.clear();
345
274
  }
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
275
  }
355
276
 
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
277
  class RedisCacheAdapter {
278
+ client;
279
+ instance;
280
+ constructor(){
281
+ this.client = useRedisClient();
282
+ this.instance = new JsonAdapter(this.client);
283
+ }
375
284
  async get(key) {
376
285
  return this.instance.get(key);
377
286
  }
@@ -406,12 +315,6 @@ class RedisCacheAdapter {
406
315
  }
407
316
  await this.client.flushdb();
408
317
  }
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 JsonAdapter(this.client);
414
- }
415
318
  }
416
319
 
417
320
  function createCacheAdapter() {
@@ -430,20 +333,11 @@ function buildCacheKey(options) {
430
333
  * Author Peter Placzek (tada5hi)
431
334
  * For the full copyright and license information,
432
335
  * 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;
336
+ */ class Cache {
337
+ adapter;
338
+ constructor(adapter){
339
+ this.adapter = adapter;
443
340
  }
444
- return obj;
445
- }
446
- class Cache {
447
341
  async set(key, value, options = {}) {
448
342
  await this.adapter.set(key, value, options);
449
343
  }
@@ -462,10 +356,6 @@ class Cache {
462
356
  async clear(options = {}) {
463
357
  return this.adapter.clear(options);
464
358
  }
465
- constructor(adapter){
466
- _define_property$5(this, "adapter", void 0);
467
- this.adapter = adapter;
468
- }
469
359
  }
470
360
 
471
361
  const instance$3 = singa({
@@ -496,32 +386,12 @@ function createLoggerConsoleTransport(options = {}) {
496
386
  return new LoggerConsoleTransport(options);
497
387
  }
498
388
 
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
389
  class LoggerMemoryTransport extends Transport {
390
+ items = [];
518
391
  log(info, next) {
519
392
  this.items.push(info);
520
393
  next();
521
394
  }
522
- constructor(...args){
523
- super(...args), _define_property$4(this, "items", []);
524
- }
525
395
  }
526
396
 
527
397
  function createLogger(ctx = {}) {
@@ -558,25 +428,11 @@ function useLogger() {
558
428
  return instance$2.use();
559
429
  }
560
430
 
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
431
  class EntityEventPublisher {
432
+ handlers;
433
+ constructor(){
434
+ this.handlers = new Set();
435
+ }
580
436
  register(consumer) {
581
437
  this.handlers.add(consumer);
582
438
  }
@@ -613,10 +469,6 @@ class EntityEventPublisher {
613
469
  await handler.value.handle(consumeContext);
614
470
  }
615
471
  }
616
- constructor(){
617
- _define_property$3(this, "handlers", void 0);
618
- this.handlers = new Set();
619
- }
620
472
  }
621
473
 
622
474
  function transformEntityEventData(input) {
@@ -652,25 +504,11 @@ function buildEntityEventRedisChannel(channel, namespace) {
652
504
  return `${namespaceNormalized}/${channelNormalized}`;
653
505
  }
654
506
 
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
507
  class EntityEventRedisHandler {
508
+ driver;
509
+ constructor(client){
510
+ this.driver = client;
511
+ }
674
512
  async handle(ctx) {
675
513
  const payload = {
676
514
  type: ctx.metadata.ref_type,
@@ -686,31 +524,13 @@ class EntityEventRedisHandler {
686
524
  }
687
525
  await pipeline.exec();
688
526
  }
689
- constructor(client){
690
- _define_property$2(this, "driver", void 0);
691
- this.driver = client;
692
- }
693
527
  }
694
528
 
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
529
  class EntityEventSocketHandler {
530
+ client;
531
+ constructor(client){
532
+ this.client = client;
533
+ }
714
534
  async handle(ctx) {
715
535
  ctx.data = transformEntityEventData(ctx.data);
716
536
  for(let i = 0; i < ctx.destinations.length; i++){
@@ -746,10 +566,6 @@ class EntityEventSocketHandler {
746
566
  }
747
567
  return channel.join('/');
748
568
  }
749
- constructor(client){
750
- _define_property$1(this, "client", void 0);
751
- this.client = client;
752
- }
753
569
  }
754
570
 
755
571
  const instance$1 = singa({
@@ -762,29 +578,102 @@ function useEntityEventPublisher() {
762
578
  return instance$1.use();
763
579
  }
764
580
 
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
581
+ function isTaskEntry(input) {
582
+ return isObject(input) && typeof input.type === 'string' && isObject(input.data);
583
+ }
584
+
585
+ class TaskManager {
586
+ driver;
587
+ constructor(driver){
588
+ this.driver = driver;
589
+ }
590
+ /**
591
+ * Store a specific job by type.
592
+ *
593
+ * @param type
594
+ * @param data
595
+ * @param options
596
+ */ async create(type, data, options = {}) {
597
+ let id;
598
+ if (options.key) {
599
+ id = `task:${options.key(type, data)}`;
600
+ } else {
601
+ id = `task:${createNanoID()}`;
602
+ }
603
+ if (options.lock) {
604
+ const isLocked = await this.isLocked(id);
605
+ if (isLocked) {
606
+ return id;
607
+ }
608
+ }
609
+ const entity = {
610
+ data,
611
+ type
612
+ };
613
+ await this.driver.set(id, entity, {
614
+ ttl: WEEK_IN_MS
777
615
  });
778
- } else {
779
- obj[key] = value;
616
+ if (options.lock) {
617
+ await this.createLock(id);
618
+ }
619
+ return id;
620
+ }
621
+ /**
622
+ * Get a job by id and type
623
+ *
624
+ * @param type
625
+ * @param id
626
+ * @param autoDelete
627
+ */ async get(type, id, autoDelete = true) {
628
+ const entity = await this.resolve(id, autoDelete);
629
+ if (!entity) {
630
+ throw new Error(`Job with id ${id} could not be found.`);
631
+ }
632
+ if (type && entity.type !== type) {
633
+ throw new Error(`Job type ${entity.type} does not not match with ${type}`);
634
+ }
635
+ return entity.data;
636
+ }
637
+ /**
638
+ * Find a random job.
639
+ *
640
+ * @param id
641
+ * @param autoDelete
642
+ */ async resolve(id, autoDelete = true) {
643
+ const entity = await this.driver.get(id);
644
+ if (!entity || !isTaskEntry(entity)) {
645
+ return undefined;
646
+ }
647
+ await this.clearLock(id);
648
+ if (autoDelete) {
649
+ await this.driver.drop(id);
650
+ }
651
+ return entity;
652
+ }
653
+ // -------------------------------------------------
654
+ async createLock(id) {
655
+ await this.driver.set(`taskLock:${id}`, true, {
656
+ ttl: HOUR_IN_MS
657
+ });
658
+ }
659
+ async clearLock(id) {
660
+ await this.driver.drop(`taskLock:${id}`);
661
+ }
662
+ async isLocked(id) {
663
+ const isActive = await this.driver.get(`taskLock:${id}`);
664
+ return !!isActive;
780
665
  }
781
- return obj;
782
666
  }
667
+
783
668
  class QueueRouter {
669
+ driver;
670
+ //----------------------------------------------------------------
671
+ constructor(driver){
672
+ this.driver = driver;
673
+ }
784
674
  //----------------------------------------------------------------
785
675
  publish(message, options = {}) {
786
- var _options;
787
- (_options = options).logging ?? (_options.logging = true);
676
+ options.logging ??= true;
788
677
  let exchange;
789
678
  if (message.metadata.routing.type === 'work') {
790
679
  exchange = this.driver.of({
@@ -866,11 +755,6 @@ class QueueRouter {
866
755
  }
867
756
  });
868
757
  }
869
- //----------------------------------------------------------------
870
- constructor(driver){
871
- _define_property(this, "driver", void 0);
872
- this.driver = driver;
873
- }
874
758
  }
875
759
 
876
760
  const instance = singa({
@@ -887,5 +771,219 @@ function useQueueRouter() {
887
771
  return instance.use();
888
772
  }
889
773
 
890
- export { AuthupClient, Cache, ComponentError, ComponentHandlers, EntityEventPublisher, EntityEventRedisHandler, EntityEventSocketHandler, EnvironmentName, LoggerConsoleTransport, LoggerMemoryTransport, MemoryCacheAdapter, QueueRouter, QueueRouterRoutingType, RedisCacheAdapter, buildCacheKey, buildEntityEventRedisChannel, buildQueueRouterPublishPayload, createCacheAdapter, createLogger, createLoggerConsoleTransport, guessAuthupTokenCreatorOptions, hasOwnProperty, isAmqpClientUsable, isAuthupClientUsable, isClientAuthenticationHookUsable, isComponentError, isLoggerUsable, isQueuePayload, isQueueRouterPayload, isQueueRouterUsable, isRedisClientUsable, isVaultClientUsable, setAmqpClientFactory, setAuthupClientFactory, setClientAuthenticationHookFactory, setEntityEventPublisherFactory, setLogger, setLoggerFactory, setRedisClient, setRedisFactory, setVaultFactory, useAmqpClient, useAuthupClient, useCache, useClientAuthenticationHook, useEntityEventPublisher, useLogger, useQueueRouter, useRedisClient, useRedisPublishClient, useRedisSubscribeClient, useVaultClient };
774
+ class QueueDispatchComponentCaller {
775
+ options;
776
+ constructor(options){
777
+ this.options = options;
778
+ }
779
+ async call(type, ...payload) {
780
+ const [data, metadata] = payload;
781
+ if (!isQueueRouterUsable()) {
782
+ useLogger().warn(`Can not publish ${type} event.`);
783
+ return;
784
+ }
785
+ const client = useQueueRouter();
786
+ await client.publish(buildQueueRouterPublishPayload({
787
+ type,
788
+ data,
789
+ metadata: {
790
+ ...metadata,
791
+ routing: this.options.queue
792
+ }
793
+ }), {
794
+ logging: this.options.logging ?? true
795
+ });
796
+ }
797
+ }
798
+
799
+ class QueueWorkerComponentCaller {
800
+ component;
801
+ options;
802
+ constructor(component, options){
803
+ this.component = component;
804
+ this.options = options;
805
+ }
806
+ async start() {
807
+ await this.component.start();
808
+ if (!isQueueRouterUsable()) {
809
+ useLogger().warn(`Can not consume queue for component ${this.component.constructor.name}`);
810
+ return;
811
+ }
812
+ const client = useQueueRouter();
813
+ await client.consumeAny(this.options.consumeQueue, async (payload)=>{
814
+ await this.call(payload.type, payload.data, payload.metadata);
815
+ });
816
+ }
817
+ /**
818
+ * Call a specific handler and collect all unhandled events.
819
+ *
820
+ * @param key
821
+ * @param payload
822
+ */ async call(key, ...payload) {
823
+ const [data, metadata] = payload;
824
+ if (!this.component.handle) {
825
+ throw new Error(`Component ${this.component.constructor.name} can not be called.`);
826
+ }
827
+ const client = useQueueRouter();
828
+ const options = {
829
+ handle: async (childValue, childContext)=>{
830
+ if (this.options.publishQueue) {
831
+ /**
832
+ *
833
+ * publish unhandled requests to publish queue.
834
+ */ await client.publish(buildQueueRouterPublishPayload({
835
+ type: childContext.key,
836
+ data: childValue,
837
+ metadata: {
838
+ ...metadata || {},
839
+ ...childContext.metadata,
840
+ routing: this.options.publishQueue
841
+ }
842
+ }));
843
+ } else {
844
+ useLogger().warn(`Component ${this.component.constructor.name} event ${childContext.key} could not be handled.`);
845
+ }
846
+ }
847
+ };
848
+ await this.component.handle(key, data, metadata, options);
849
+ }
850
+ }
851
+
852
+ /*
853
+ * Copyright (c) 2023-2024.
854
+ * Author Peter Placzek (tada5hi)
855
+ * For the full copyright and license information,
856
+ * view the LICENSE file that was distributed with this source code.
857
+ */ class ComponentError extends Error {
858
+ code;
859
+ constructor(input){
860
+ super(input.message, {
861
+ cause: input.cause
862
+ });
863
+ this.code = input.code;
864
+ }
865
+ }
866
+
867
+ /*
868
+ * Copyright (c) 2022-2024.
869
+ * Author Peter Placzek (tada5hi)
870
+ * For the full copyright and license information,
871
+ * view the LICENSE file that was distributed with this source code.
872
+ */ function hasOwnProperty(obj, prop) {
873
+ return Object.prototype.hasOwnProperty.call(obj, prop);
874
+ }
875
+
876
+ function isComponentHandler(input) {
877
+ if (!isObject(input)) {
878
+ return false;
879
+ }
880
+ return !(!hasOwnProperty(input, 'execute') || typeof input.execute !== 'function');
881
+ }
882
+ function isComponentHandlerFn(input) {
883
+ return typeof input === 'function';
884
+ }
885
+
886
+ /*
887
+ * Copyright (c) 2025.
888
+ * Author Peter Placzek (tada5hi)
889
+ * For the full copyright and license information,
890
+ * view the LICENSE file that was distributed with this source code.
891
+ */ class BaseComponent {
892
+ initializePromise;
893
+ initialized;
894
+ handlers;
895
+ constructor(){
896
+ this.initializePromise = undefined;
897
+ this.initialized = false;
898
+ this.handlers = new Map();
899
+ }
900
+ mount(key, fn) {
901
+ this.handlers.set(key, fn);
902
+ }
903
+ unmount(key) {
904
+ this.handlers.delete(key);
905
+ }
906
+ async initialize() {
907
+ if (this.initialized) {
908
+ return Promise.resolve();
909
+ }
910
+ if (this.initializePromise) {
911
+ return this.initializePromise;
912
+ }
913
+ const promises = [];
914
+ const keys = Array.from(this.handlers.keys());
915
+ for(let i = 0; i < keys.length; i++){
916
+ const handler = this.handlers.get(keys[i]);
917
+ if (typeof handler !== 'function' && handler.initialize) {
918
+ promises.push(Promise.resolve().then(()=>handler.initialize()));
919
+ }
920
+ }
921
+ this.initializePromise = new Promise((resolve, reject)=>{
922
+ Promise.all(promises).then(()=>resolve()).catch((err)=>reject(err));
923
+ });
924
+ this.initializePromise.finally(()=>{
925
+ setTimeout(()=>{
926
+ this.initialized = true;
927
+ this.initializePromise = undefined;
928
+ }, 0);
929
+ });
930
+ return this.initializePromise;
931
+ }
932
+ /**
933
+ * Handle specific component event.
934
+ *
935
+ * @param key
936
+ * @param data
937
+ * @param metadata
938
+ * @param options
939
+ */ async handle(key, data, metadata = {}, options = {}) {
940
+ await this.initialize();
941
+ const context = {
942
+ key,
943
+ metadata,
944
+ handle: (childKey, childData, childMetadata)=>{
945
+ this.handle(childKey, childData, {
946
+ ...metadata,
947
+ ...childMetadata || {}
948
+ }, {
949
+ ...options,
950
+ ...options || {}
951
+ });
952
+ }
953
+ };
954
+ let handler = this.handlers.get(key);
955
+ if (handler) {
956
+ if (typeof handler === 'function') {
957
+ return handler(data, context);
958
+ }
959
+ return handler.handle(data, context);
960
+ }
961
+ handler = this.handlers.get('*');
962
+ if (handler) {
963
+ if (typeof handler === 'function') {
964
+ return Promise.resolve().then(()=>handler(data, context));
965
+ }
966
+ return Promise.resolve().then(()=>handler.handle(data, context));
967
+ }
968
+ if (options.handle) {
969
+ return options.handle(data, context);
970
+ }
971
+ return Promise.resolve();
972
+ }
973
+ }
974
+
975
+ function isComponentError(input) {
976
+ if (!isObject(input)) {
977
+ return false;
978
+ }
979
+ if (typeof input.message !== 'undefined' && typeof input.message !== 'string') {
980
+ return false;
981
+ }
982
+ if (typeof input.code === 'undefined') {
983
+ return true;
984
+ }
985
+ return typeof input.code === 'string' || input.code === null;
986
+ }
987
+
988
+ export { AuthupClient, BaseComponent, Cache, ComponentError, CompoundComponentCaller, DirectComponentCaller, EntityEventPublisher, EntityEventRedisHandler, EntityEventSocketHandler, EnvironmentName, LoggerConsoleTransport, LoggerMemoryTransport, MemoryCacheAdapter, QueueDispatchComponentCaller, QueueRouter, QueueRouterRoutingType, QueueWorkerComponentCaller, RedisCacheAdapter, TaskManager, buildCacheKey, buildEntityEventRedisChannel, buildQueueRouterPublishPayload, createCacheAdapter, createLogger, createLoggerConsoleTransport, guessAuthupTokenCreatorOptions, hasOwnProperty, isAmqpClientUsable, isAuthupClientUsable, isClientAuthenticationHookUsable, isComponentError, isComponentHandler, isComponentHandlerFn, isLoggerUsable, isQueueRouterPayload, isQueueRouterUsable, isRedisClientUsable, isVaultClientUsable, setAmqpClientFactory, setAuthupClientFactory, setClientAuthenticationHookFactory, setEntityEventPublisherFactory, setLogger, setLoggerFactory, setRedisClient, setRedisFactory, setVaultFactory, useAmqpClient, useAuthupClient, useCache, useClientAuthenticationHook, useEntityEventPublisher, useLogger, useQueueRouter, useRedisClient, useRedisPublishClient, useRedisSubscribeClient, useVaultClient };
891
989
  //# sourceMappingURL=index.mjs.map