@privateaim/server-kit 0.8.20 → 0.8.22

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 +38 -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 +390 -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 +84 -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 +91 -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 +158 -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,71 @@ 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
+ if (!this.component.handle) {
81
+ throw new Error(`Component ${this.component.constructor.name} can not be called.`);
82
+ }
83
+ const events = {};
84
+ const options = {
85
+ handle: (childValue, childContext)=>{
86
+ events[childContext.key] = childValue;
87
+ }
88
+ };
89
+ await this.callWith(key, data, metadata, options);
90
+ return events;
144
91
  }
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
92
  }
151
93
 
152
94
  /*
@@ -291,25 +233,15 @@ function useRedisSubscribeClient() {
291
233
  return instance$4.use();
292
234
  }
293
235
 
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
236
+ class MemoryCacheAdapter {
237
+ instance;
238
+ constructor(options = {}){
239
+ this.instance = new TTLCache({
240
+ checkAgeOnGet: true,
241
+ ttl: Infinity,
242
+ ...options || {}
306
243
  });
307
- } else {
308
- obj[key] = value;
309
244
  }
310
- return obj;
311
- }
312
- class MemoryCacheAdapter {
313
245
  async get(key) {
314
246
  return this.instance.get(key);
315
247
  }
@@ -334,7 +266,7 @@ class MemoryCacheAdapter {
334
266
  const keys = this.instance.keys();
335
267
  let iterator = keys.next();
336
268
  while(!iterator.done){
337
- if (iterator.value.startsWith(options.prefix)) {
269
+ if (typeof iterator.value === 'string' && iterator.value.startsWith(options.prefix)) {
338
270
  this.instance.delete(iterator.value);
339
271
  }
340
272
  iterator = keys.next();
@@ -343,35 +275,15 @@ class MemoryCacheAdapter {
343
275
  }
344
276
  this.instance.clear();
345
277
  }
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
278
  }
355
279
 
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
280
  class RedisCacheAdapter {
281
+ client;
282
+ instance;
283
+ constructor(){
284
+ this.client = useRedisClient();
285
+ this.instance = new JsonAdapter(this.client);
286
+ }
375
287
  async get(key) {
376
288
  return this.instance.get(key);
377
289
  }
@@ -406,12 +318,6 @@ class RedisCacheAdapter {
406
318
  }
407
319
  await this.client.flushdb();
408
320
  }
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
321
  }
416
322
 
417
323
  function createCacheAdapter() {
@@ -430,20 +336,11 @@ function buildCacheKey(options) {
430
336
  * Author Peter Placzek (tada5hi)
431
337
  * For the full copyright and license information,
432
338
  * 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;
339
+ */ class Cache {
340
+ adapter;
341
+ constructor(adapter){
342
+ this.adapter = adapter;
443
343
  }
444
- return obj;
445
- }
446
- class Cache {
447
344
  async set(key, value, options = {}) {
448
345
  await this.adapter.set(key, value, options);
449
346
  }
@@ -462,10 +359,6 @@ class Cache {
462
359
  async clear(options = {}) {
463
360
  return this.adapter.clear(options);
464
361
  }
465
- constructor(adapter){
466
- _define_property$5(this, "adapter", void 0);
467
- this.adapter = adapter;
468
- }
469
362
  }
470
363
 
471
364
  const instance$3 = singa({
@@ -496,32 +389,12 @@ function createLoggerConsoleTransport(options = {}) {
496
389
  return new LoggerConsoleTransport(options);
497
390
  }
498
391
 
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
392
  class LoggerMemoryTransport extends Transport {
393
+ items = [];
518
394
  log(info, next) {
519
395
  this.items.push(info);
520
396
  next();
521
397
  }
522
- constructor(...args){
523
- super(...args), _define_property$4(this, "items", []);
524
- }
525
398
  }
526
399
 
527
400
  function createLogger(ctx = {}) {
@@ -558,25 +431,11 @@ function useLogger() {
558
431
  return instance$2.use();
559
432
  }
560
433
 
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
434
  class EntityEventPublisher {
435
+ handlers;
436
+ constructor(){
437
+ this.handlers = new Set();
438
+ }
580
439
  register(consumer) {
581
440
  this.handlers.add(consumer);
582
441
  }
@@ -613,10 +472,6 @@ class EntityEventPublisher {
613
472
  await handler.value.handle(consumeContext);
614
473
  }
615
474
  }
616
- constructor(){
617
- _define_property$3(this, "handlers", void 0);
618
- this.handlers = new Set();
619
- }
620
475
  }
621
476
 
622
477
  function transformEntityEventData(input) {
@@ -652,25 +507,11 @@ function buildEntityEventRedisChannel(channel, namespace) {
652
507
  return `${namespaceNormalized}/${channelNormalized}`;
653
508
  }
654
509
 
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
510
  class EntityEventRedisHandler {
511
+ driver;
512
+ constructor(client){
513
+ this.driver = client;
514
+ }
674
515
  async handle(ctx) {
675
516
  const payload = {
676
517
  type: ctx.metadata.ref_type,
@@ -686,31 +527,13 @@ class EntityEventRedisHandler {
686
527
  }
687
528
  await pipeline.exec();
688
529
  }
689
- constructor(client){
690
- _define_property$2(this, "driver", void 0);
691
- this.driver = client;
692
- }
693
530
  }
694
531
 
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
532
  class EntityEventSocketHandler {
533
+ client;
534
+ constructor(client){
535
+ this.client = client;
536
+ }
714
537
  async handle(ctx) {
715
538
  ctx.data = transformEntityEventData(ctx.data);
716
539
  for(let i = 0; i < ctx.destinations.length; i++){
@@ -746,10 +569,6 @@ class EntityEventSocketHandler {
746
569
  }
747
570
  return channel.join('/');
748
571
  }
749
- constructor(client){
750
- _define_property$1(this, "client", void 0);
751
- this.client = client;
752
- }
753
572
  }
754
573
 
755
574
  const instance$1 = singa({
@@ -762,29 +581,102 @@ function useEntityEventPublisher() {
762
581
  return instance$1.use();
763
582
  }
764
583
 
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
584
+ function isTaskEntry(input) {
585
+ return isObject(input) && typeof input.type === 'string' && isObject(input.data);
586
+ }
587
+
588
+ class TaskManager {
589
+ driver;
590
+ constructor(driver){
591
+ this.driver = driver;
592
+ }
593
+ /**
594
+ * Store a specific job by type.
595
+ *
596
+ * @param type
597
+ * @param data
598
+ * @param options
599
+ */ async create(type, data, options = {}) {
600
+ let id;
601
+ if (options.key) {
602
+ id = `task:${options.key(type, data)}`;
603
+ } else {
604
+ id = `task:${createNanoID()}`;
605
+ }
606
+ if (options.lock) {
607
+ const isLocked = await this.isLocked(id);
608
+ if (isLocked) {
609
+ return id;
610
+ }
611
+ }
612
+ const entity = {
613
+ data,
614
+ type
615
+ };
616
+ await this.driver.set(id, entity, {
617
+ ttl: WEEK_IN_MS
777
618
  });
778
- } else {
779
- obj[key] = value;
619
+ if (options.lock) {
620
+ await this.createLock(id);
621
+ }
622
+ return id;
623
+ }
624
+ /**
625
+ * Get a job by id and type
626
+ *
627
+ * @param type
628
+ * @param id
629
+ * @param autoDelete
630
+ */ async get(type, id, autoDelete = true) {
631
+ const entity = await this.resolve(id, autoDelete);
632
+ if (!entity) {
633
+ throw new Error(`Job with id ${id} could not be found.`);
634
+ }
635
+ if (type && entity.type !== type) {
636
+ throw new Error(`Job type ${entity.type} does not not match with ${type}`);
637
+ }
638
+ return entity.data;
639
+ }
640
+ /**
641
+ * Find a random job.
642
+ *
643
+ * @param id
644
+ * @param autoDelete
645
+ */ async resolve(id, autoDelete = true) {
646
+ const entity = await this.driver.get(id);
647
+ if (!entity || !isTaskEntry(entity)) {
648
+ return undefined;
649
+ }
650
+ await this.clearLock(id);
651
+ if (autoDelete) {
652
+ await this.driver.drop(id);
653
+ }
654
+ return entity;
655
+ }
656
+ // -------------------------------------------------
657
+ async createLock(id) {
658
+ await this.driver.set(`taskLock:${id}`, true, {
659
+ ttl: HOUR_IN_MS
660
+ });
661
+ }
662
+ async clearLock(id) {
663
+ await this.driver.drop(`taskLock:${id}`);
664
+ }
665
+ async isLocked(id) {
666
+ const isActive = await this.driver.get(`taskLock:${id}`);
667
+ return !!isActive;
780
668
  }
781
- return obj;
782
669
  }
670
+
783
671
  class QueueRouter {
672
+ driver;
673
+ //----------------------------------------------------------------
674
+ constructor(driver){
675
+ this.driver = driver;
676
+ }
784
677
  //----------------------------------------------------------------
785
678
  publish(message, options = {}) {
786
- var _options;
787
- (_options = options).logging ?? (_options.logging = true);
679
+ options.logging ??= true;
788
680
  let exchange;
789
681
  if (message.metadata.routing.type === 'work') {
790
682
  exchange = this.driver.of({
@@ -866,11 +758,6 @@ class QueueRouter {
866
758
  }
867
759
  });
868
760
  }
869
- //----------------------------------------------------------------
870
- constructor(driver){
871
- _define_property(this, "driver", void 0);
872
- this.driver = driver;
873
- }
874
761
  }
875
762
 
876
763
  const instance = singa({
@@ -887,5 +774,218 @@ function useQueueRouter() {
887
774
  return instance.use();
888
775
  }
889
776
 
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 };
777
+ class QueueDispatchComponentCaller {
778
+ options;
779
+ constructor(options){
780
+ this.options = options;
781
+ }
782
+ async call(type, ...payload) {
783
+ const [data, metadata] = payload;
784
+ if (!isQueueRouterUsable()) {
785
+ useLogger().warn(`Can not publish ${type} event.`);
786
+ return;
787
+ }
788
+ const client = useQueueRouter();
789
+ await client.publish(buildQueueRouterPublishPayload({
790
+ type,
791
+ data,
792
+ metadata: {
793
+ ...metadata,
794
+ routing: this.options.queue
795
+ }
796
+ }), {
797
+ logging: this.options.logging ?? true
798
+ });
799
+ }
800
+ }
801
+
802
+ class QueueWorkerComponentCaller {
803
+ component;
804
+ options;
805
+ constructor(component, options){
806
+ this.component = component;
807
+ this.options = options;
808
+ }
809
+ async start() {
810
+ if (!isQueueRouterUsable()) {
811
+ useLogger().warn(`Can not consume queue for component ${this.component.constructor.name}`);
812
+ return;
813
+ }
814
+ const client = useQueueRouter();
815
+ await client.consumeAny(this.options.consumeQueue, async (payload)=>{
816
+ await this.call(payload.type, payload.data, payload.metadata);
817
+ });
818
+ }
819
+ /**
820
+ * Call a specific handler and collect all unhandled events.
821
+ *
822
+ * @param key
823
+ * @param payload
824
+ */ async call(key, ...payload) {
825
+ const [data, metadata] = payload;
826
+ if (!this.component.handle) {
827
+ throw new Error(`Component ${this.component.constructor.name} can not be called.`);
828
+ }
829
+ const client = useQueueRouter();
830
+ const options = {
831
+ handle: async (childValue, childContext)=>{
832
+ if (this.options.publishQueue) {
833
+ /**
834
+ *
835
+ * publish unhandled requests to publish queue.
836
+ */ await client.publish(buildQueueRouterPublishPayload({
837
+ type: childContext.key,
838
+ data: childValue,
839
+ metadata: {
840
+ ...metadata || {},
841
+ ...childContext.metadata,
842
+ routing: this.options.publishQueue
843
+ }
844
+ }));
845
+ } else {
846
+ useLogger().warn(`Component ${this.component.constructor.name} event ${childContext.key} could not be handled.`);
847
+ }
848
+ }
849
+ };
850
+ await this.component.handle(key, data, metadata, options);
851
+ }
852
+ }
853
+
854
+ /*
855
+ * Copyright (c) 2023-2024.
856
+ * Author Peter Placzek (tada5hi)
857
+ * For the full copyright and license information,
858
+ * view the LICENSE file that was distributed with this source code.
859
+ */ class ComponentError extends Error {
860
+ code;
861
+ constructor(input){
862
+ super(input.message, {
863
+ cause: input.cause
864
+ });
865
+ this.code = input.code;
866
+ }
867
+ }
868
+
869
+ /*
870
+ * Copyright (c) 2022-2024.
871
+ * Author Peter Placzek (tada5hi)
872
+ * For the full copyright and license information,
873
+ * view the LICENSE file that was distributed with this source code.
874
+ */ function hasOwnProperty(obj, prop) {
875
+ return Object.prototype.hasOwnProperty.call(obj, prop);
876
+ }
877
+
878
+ function isComponentHandler(input) {
879
+ if (!isObject(input)) {
880
+ return false;
881
+ }
882
+ return !(!hasOwnProperty(input, 'execute') || typeof input.execute !== 'function');
883
+ }
884
+ function isComponentHandlerFn(input) {
885
+ return typeof input === 'function';
886
+ }
887
+
888
+ /*
889
+ * Copyright (c) 2025.
890
+ * Author Peter Placzek (tada5hi)
891
+ * For the full copyright and license information,
892
+ * view the LICENSE file that was distributed with this source code.
893
+ */ class BaseComponent {
894
+ initializePromise;
895
+ initialized;
896
+ handlers;
897
+ constructor(){
898
+ this.initializePromise = undefined;
899
+ this.initialized = false;
900
+ this.handlers = new Map();
901
+ }
902
+ mount(key, fn) {
903
+ this.handlers.set(key, fn);
904
+ }
905
+ unmount(key) {
906
+ this.handlers.delete(key);
907
+ }
908
+ async initialize() {
909
+ if (this.initialized) {
910
+ return Promise.resolve();
911
+ }
912
+ if (this.initializePromise) {
913
+ return this.initializePromise;
914
+ }
915
+ const promises = [];
916
+ const keys = Array.from(this.handlers.keys());
917
+ for(let i = 0; i < keys.length; i++){
918
+ const handler = this.handlers.get(keys[i]);
919
+ if (typeof handler !== 'function' && handler.initialize) {
920
+ promises.push(Promise.resolve().then(()=>handler.initialize()));
921
+ }
922
+ }
923
+ this.initializePromise = new Promise((resolve, reject)=>{
924
+ Promise.all(promises).then(()=>resolve()).catch((err)=>reject(err));
925
+ });
926
+ this.initializePromise.finally(()=>{
927
+ setTimeout(()=>{
928
+ this.initialized = true;
929
+ this.initializePromise = undefined;
930
+ }, 0);
931
+ });
932
+ return this.initializePromise;
933
+ }
934
+ /**
935
+ * Handle specific component event.
936
+ *
937
+ * @param key
938
+ * @param data
939
+ * @param metadata
940
+ * @param options
941
+ */ async handle(key, data, metadata = {}, options = {}) {
942
+ await this.initialize();
943
+ const context = {
944
+ key,
945
+ metadata,
946
+ handle: (childKey, childData, childMetadata)=>{
947
+ this.handle(childKey, childData, {
948
+ ...metadata,
949
+ ...childMetadata || {}
950
+ }, {
951
+ ...options,
952
+ ...options || {}
953
+ });
954
+ }
955
+ };
956
+ let handler = this.handlers.get(key);
957
+ if (handler) {
958
+ if (typeof handler === 'function') {
959
+ return handler(data, context);
960
+ }
961
+ return handler.handle(data, context);
962
+ }
963
+ handler = this.handlers.get('*');
964
+ if (handler) {
965
+ if (typeof handler === 'function') {
966
+ return handler(data, context);
967
+ }
968
+ return handler.handle(data, context);
969
+ }
970
+ if (options.handle) {
971
+ return options.handle(data, context);
972
+ }
973
+ return Promise.resolve();
974
+ }
975
+ }
976
+
977
+ function isComponentError(input) {
978
+ if (!isObject(input)) {
979
+ return false;
980
+ }
981
+ if (typeof input.message !== 'undefined' && typeof input.message !== 'string') {
982
+ return false;
983
+ }
984
+ if (typeof input.code === 'undefined') {
985
+ return true;
986
+ }
987
+ return typeof input.code === 'string' || input.code === null;
988
+ }
989
+
990
+ 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
991
  //# sourceMappingURL=index.mjs.map