@privateaim/server-kit 0.8.13 → 0.8.15

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 (73) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/index.cjs +494 -63
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.mjs +452 -65
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/services/domain-event/index.d.ts +1 -1
  7. package/dist/services/domain-event/index.d.ts.map +1 -1
  8. package/dist/services/domain-event/module.d.ts +4 -4
  9. package/dist/services/domain-event/module.d.ts.map +1 -1
  10. package/dist/services/domain-event/redis/module.d.ts +2 -2
  11. package/dist/services/domain-event/redis/module.d.ts.map +1 -1
  12. package/dist/services/domain-event/singleton.d.ts +1 -0
  13. package/dist/services/domain-event/singleton.d.ts.map +1 -1
  14. package/dist/services/domain-event/socket/module.d.ts +2 -2
  15. package/dist/services/domain-event/socket/module.d.ts.map +1 -1
  16. package/dist/services/domain-event/types.d.ts +29 -0
  17. package/dist/services/domain-event/types.d.ts.map +1 -0
  18. package/dist/services/domain-event/utils.d.ts +2 -2
  19. package/dist/services/domain-event/utils.d.ts.map +1 -1
  20. package/dist/services/index.d.ts +2 -0
  21. package/dist/services/index.d.ts.map +1 -1
  22. package/dist/services/log-store/entities/base.d.ts +7 -0
  23. package/dist/services/log-store/entities/base.d.ts.map +1 -0
  24. package/dist/services/log-store/entities/index.d.ts +3 -0
  25. package/dist/services/log-store/entities/index.d.ts.map +1 -0
  26. package/dist/services/log-store/entities/loki.d.ts +13 -0
  27. package/dist/services/log-store/entities/loki.d.ts.map +1 -0
  28. package/dist/services/log-store/entities/memory.d.ts +11 -0
  29. package/dist/services/log-store/entities/memory.d.ts.map +1 -0
  30. package/dist/services/log-store/index.d.ts +4 -0
  31. package/dist/services/log-store/index.d.ts.map +1 -0
  32. package/dist/services/log-store/singleton.d.ts +6 -0
  33. package/dist/services/log-store/singleton.d.ts.map +1 -0
  34. package/dist/services/log-store/types.d.ts +27 -0
  35. package/dist/services/log-store/types.d.ts.map +1 -0
  36. package/dist/services/logger/module.d.ts.map +1 -1
  37. package/dist/services/logger/store.d.ts +14 -0
  38. package/dist/services/logger/store.d.ts.map +1 -0
  39. package/dist/services/logger/types.d.ts +3 -0
  40. package/dist/services/logger/types.d.ts.map +1 -1
  41. package/dist/services/loki/index.d.ts +3 -0
  42. package/dist/services/loki/index.d.ts.map +1 -0
  43. package/dist/services/loki/module.d.ts +3 -0
  44. package/dist/services/loki/module.d.ts.map +1 -0
  45. package/dist/services/loki/singleton.d.ts +6 -0
  46. package/dist/services/loki/singleton.d.ts.map +1 -0
  47. package/package.json +12 -9
  48. package/src/services/domain-event/index.ts +1 -1
  49. package/src/services/domain-event/module.ts +8 -8
  50. package/src/services/domain-event/redis/module.ts +28 -9
  51. package/src/services/domain-event/singleton.ts +6 -2
  52. package/src/services/domain-event/socket/module.ts +20 -9
  53. package/src/services/domain-event/types.ts +50 -0
  54. package/src/services/domain-event/utils.ts +2 -2
  55. package/src/services/index.ts +2 -0
  56. package/src/services/log-store/entities/base.ts +25 -0
  57. package/src/services/log-store/entities/index.ts +9 -0
  58. package/src/services/log-store/entities/loki.ts +158 -0
  59. package/src/services/log-store/entities/memory.ts +100 -0
  60. package/src/services/log-store/index.ts +10 -0
  61. package/src/services/log-store/singleton.ts +36 -0
  62. package/src/services/log-store/types.ts +42 -0
  63. package/src/services/logger/module.ts +20 -5
  64. package/src/services/logger/store.ts +70 -0
  65. package/src/services/logger/types.ts +4 -1
  66. package/src/services/loki/index.ts +9 -0
  67. package/src/services/loki/module.ts +30 -0
  68. package/src/services/loki/singleton.ts +26 -0
  69. package/test/unit/log-store.spec.ts +21 -0
  70. package/test/unit/logger.spec.ts +36 -0
  71. package/dist/services/domain-event/type.d.ts +0 -15
  72. package/dist/services/domain-event/type.d.ts.map +0 -1
  73. package/src/services/domain-event/type.ts +0 -27
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { isObject, hasOwnProperty as hasOwnProperty$1, createNanoID, buildDomainEventFullName } from '@privateaim/kit';
1
+ import { isObject, hasOwnProperty as hasOwnProperty$1, createNanoID, LogLevel, buildDomainEventFullName } from '@privateaim/kit';
2
2
  import { ExchangeType } from 'amqp-extension';
3
3
  import { singa } from 'singa';
4
4
  export { VaultClient, createClient as createVaultClient } from '@hapic/vault';
@@ -10,6 +10,9 @@ import { read } from 'envix';
10
10
  import path from 'node:path';
11
11
  import { EnvironmentName } from 'typeorm-extension';
12
12
  import { transports, createLogger as createLogger$1, format } from 'winston';
13
+ import WinstonTransport from 'winston-transport';
14
+ import { nanoSeconds } from '@hapic/loki';
15
+ export { LokiClient, CompactorDeletionRequestCreate as LokiCompactorDeletionRequestCreate, Config as LokiConfig, ConfigInput as LokiConfigInput, DistributorPushStream as LokiDistributorPushStream, QuerierQueryRangeOptions as LokiQuerierQueryRangeOptions, QuerierQueryResult as LokiQuerierQueryResult, createClient as createLokiClient, nanoSeconds } from '@hapic/loki';
13
16
  import { Emitter } from '@socket.io/redis-emitter';
14
17
 
15
18
  /*
@@ -17,7 +20,7 @@ import { Emitter } from '@socket.io/redis-emitter';
17
20
  * Author Peter Placzek (tada5hi)
18
21
  * For the full copyright and license information,
19
22
  * view the LICENSE file that was distributed with this source code.
20
- */ function _define_property$7(obj, key, value) {
23
+ */ function _define_property$b(obj, key, value) {
21
24
  if (key in obj) {
22
25
  Object.defineProperty(obj, key, {
23
26
  value: value,
@@ -34,7 +37,7 @@ class ComponentError extends Error {
34
37
  constructor(input){
35
38
  super(input.message, {
36
39
  cause: input.cause
37
- }), _define_property$7(this, "code", void 0);
40
+ }), _define_property$b(this, "code", void 0);
38
41
  this.code = input.code;
39
42
  }
40
43
  }
@@ -82,30 +85,30 @@ function isQueueRouterPayload(input) {
82
85
  return isObject(input) && typeof input.id === 'string' && typeof input.type === 'string' && hasOwnProperty$1(input, 'data') && isObject(input.data) && hasOwnProperty$1(input, 'metadata') && isObject(input.metadata);
83
86
  }
84
87
 
85
- const instance$a = singa({
88
+ const instance$b = singa({
86
89
  name: 'amqp'
87
90
  });
88
91
  function setAmqpClientFactory(factory) {
89
- instance$a.setFactory(factory);
92
+ instance$b.setFactory(factory);
90
93
  }
91
94
  function isAmqpClientUsable() {
92
- return instance$a.has() || instance$a.hasFactory();
95
+ return instance$b.has() || instance$b.hasFactory();
93
96
  }
94
97
  function useAmqpClient() {
95
- return instance$a.use();
98
+ return instance$b.use();
96
99
  }
97
100
 
98
- const instance$9 = singa({
101
+ const instance$a = singa({
99
102
  name: 'vault'
100
103
  });
101
104
  function setVaultFactory(factory) {
102
- instance$9.setFactory(factory);
105
+ instance$a.setFactory(factory);
103
106
  }
104
107
  function isVaultClientUsable() {
105
- return instance$9.has() || instance$9.hasFactory();
108
+ return instance$a.has() || instance$a.hasFactory();
106
109
  }
107
110
  function useVaultClient() {
108
- return instance$9.use();
111
+ return instance$a.use();
109
112
  }
110
113
 
111
114
  function guessAuthupTokenCreatorOptions() {
@@ -126,17 +129,17 @@ function guessAuthupTokenCreatorOptions() {
126
129
  return options;
127
130
  }
128
131
 
129
- const instance$8 = singa({
132
+ const instance$9 = singa({
130
133
  name: 'clientAuthenticationHook'
131
134
  });
132
135
  function setClientAuthenticationHookFactory(factory) {
133
- instance$8.setFactory(factory);
136
+ instance$9.setFactory(factory);
134
137
  }
135
138
  function isClientAuthenticationHookUsable() {
136
- return instance$8.has() || instance$8.hasFactory();
139
+ return instance$9.has() || instance$9.hasFactory();
137
140
  }
138
141
  function useClientAuthenticationHook() {
139
- return instance$8.use();
142
+ return instance$9.use();
140
143
  }
141
144
 
142
145
  class AuthupClient extends Client {
@@ -153,49 +156,49 @@ class AuthupClient extends Client {
153
156
  }
154
157
  }
155
158
 
156
- const instance$7 = singa({
159
+ const instance$8 = singa({
157
160
  name: 'authup'
158
161
  });
159
162
  function useAuthupClient() {
160
- return instance$7.use();
163
+ return instance$8.use();
161
164
  }
162
165
  function isAuthupClientUsable() {
163
- return instance$7.has() || instance$7.hasFactory();
166
+ return instance$8.has() || instance$8.hasFactory();
164
167
  }
165
168
  function setAuthupClientFactory(factory) {
166
- instance$7.setFactory(factory);
169
+ instance$8.setFactory(factory);
167
170
  }
168
171
 
169
- const instance$6 = singa({
172
+ const instance$7 = singa({
170
173
  name: 'redis'
171
174
  });
172
175
  function setRedisFactory(factory) {
173
- instance$6.setFactory(factory);
176
+ instance$7.setFactory(factory);
174
177
  }
175
178
  function isRedisClientUsable() {
176
- return instance$6.has() || instance$6.hasFactory();
179
+ return instance$7.has() || instance$7.hasFactory();
177
180
  }
178
181
  function setRedisClient(input) {
179
- instance$6.set(input);
182
+ instance$7.set(input);
180
183
  }
181
184
  function useRedisClient() {
182
- return instance$6.use();
185
+ return instance$7.use();
183
186
  }
184
187
 
185
- const instance$5 = singa({
188
+ const instance$6 = singa({
186
189
  name: 'redisPublish',
187
190
  factory: ()=>useRedisClient().duplicate()
188
191
  });
189
192
  function useRedisPublishClient() {
190
- return instance$5.use();
193
+ return instance$6.use();
191
194
  }
192
195
 
193
- const instance$4 = singa({
196
+ const instance$5 = singa({
194
197
  name: 'redisSubscribe',
195
198
  factory: ()=>useRedisClient().duplicate()
196
199
  });
197
200
  function useRedisSubscribeClient() {
198
- return instance$4.use();
201
+ return instance$5.use();
199
202
  }
200
203
 
201
204
  /*
@@ -203,7 +206,7 @@ function useRedisSubscribeClient() {
203
206
  * Author Peter Placzek (tada5hi)
204
207
  * For the full copyright and license information,
205
208
  * view the LICENSE file that was distributed with this source code.
206
- */ function _define_property$6(obj, key, value) {
209
+ */ function _define_property$a(obj, key, value) {
207
210
  if (key in obj) {
208
211
  Object.defineProperty(obj, key, {
209
212
  value: value,
@@ -251,7 +254,7 @@ class MemoryCacheAdapter {
251
254
  this.instance.clear();
252
255
  }
253
256
  constructor(options = {}){
254
- _define_property$6(this, "instance", void 0);
257
+ _define_property$a(this, "instance", void 0);
255
258
  this.instance = new TTLCache({
256
259
  checkAgeOnGet: true,
257
260
  ttl: Infinity,
@@ -265,7 +268,7 @@ class MemoryCacheAdapter {
265
268
  * Author Peter Placzek (tada5hi)
266
269
  * For the full copyright and license information,
267
270
  * view the LICENSE file that was distributed with this source code.
268
- */ function _define_property$5(obj, key, value) {
271
+ */ function _define_property$9(obj, key, value) {
269
272
  if (key in obj) {
270
273
  Object.defineProperty(obj, key, {
271
274
  value: value,
@@ -314,8 +317,8 @@ class RedisCacheAdapter {
314
317
  await this.client.flushdb();
315
318
  }
316
319
  constructor(){
317
- _define_property$5(this, "client", void 0);
318
- _define_property$5(this, "instance", void 0);
320
+ _define_property$9(this, "client", void 0);
321
+ _define_property$9(this, "instance", void 0);
319
322
  this.client = useRedisClient();
320
323
  this.instance = new JsonAdapter(this.client);
321
324
  }
@@ -337,7 +340,7 @@ function buildCacheKey(options) {
337
340
  * Author Peter Placzek (tada5hi)
338
341
  * For the full copyright and license information,
339
342
  * view the LICENSE file that was distributed with this source code.
340
- */ function _define_property$4(obj, key, value) {
343
+ */ function _define_property$8(obj, key, value) {
341
344
  if (key in obj) {
342
345
  Object.defineProperty(obj, key, {
343
346
  value: value,
@@ -370,12 +373,12 @@ class Cache {
370
373
  return this.adapter.clear(options);
371
374
  }
372
375
  constructor(adapter){
373
- _define_property$4(this, "adapter", void 0);
376
+ _define_property$8(this, "adapter", void 0);
374
377
  this.adapter = adapter;
375
378
  }
376
379
  }
377
380
 
378
- const instance$3 = singa({
381
+ const instance$4 = singa({
379
382
  name: 'cache',
380
383
  factory: ()=>{
381
384
  let adapter;
@@ -388,11 +391,365 @@ const instance$3 = singa({
388
391
  }
389
392
  });
390
393
  function useCache() {
394
+ return instance$4.use();
395
+ }
396
+
397
+ /*
398
+ * Copyright (c) 2025.
399
+ * Author Peter Placzek (tada5hi)
400
+ * For the full copyright and license information,
401
+ * view the LICENSE file that was distributed with this source code.
402
+ */ function _define_property$7(obj, key, value) {
403
+ if (key in obj) {
404
+ Object.defineProperty(obj, key, {
405
+ value: value,
406
+ enumerable: true,
407
+ configurable: true,
408
+ writable: true
409
+ });
410
+ } else {
411
+ obj[key] = value;
412
+ }
413
+ return obj;
414
+ }
415
+ class LogStoreTransport extends WinstonTransport {
416
+ log(info, next) {
417
+ const { message, timestamp, stack, ...data } = info;
418
+ const labels = {
419
+ ...this.labels
420
+ };
421
+ const keys = Object.keys(data);
422
+ for(let i = 0; i < keys.length; i++){
423
+ if (typeof keys[i] !== 'string') {
424
+ continue;
425
+ }
426
+ const value = data[keys[i]];
427
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
428
+ labels[keys[i]] = `${value}`;
429
+ }
430
+ }
431
+ let date;
432
+ if (typeof timestamp === 'string') {
433
+ date = new Date(`${timestamp}`);
434
+ } else {
435
+ date = new Date();
436
+ }
437
+ const payload = {
438
+ message: stack || message,
439
+ time: BigInt(date.getTime()) * 1000000n,
440
+ labels
441
+ };
442
+ Promise.resolve().then(()=>this.instance.write(payload)).then(()=>next());
443
+ }
444
+ constructor(store, options = {}){
445
+ super(options), _define_property$7(this, "instance", void 0), _define_property$7(this, "labels", void 0);
446
+ this.instance = store;
447
+ this.labels = options.labels || {};
448
+ }
449
+ }
450
+
451
+ const instance$3 = singa({
452
+ name: 'loki'
453
+ });
454
+ function setLokiFactory(factory) {
455
+ instance$3.setFactory(factory);
456
+ }
457
+ function isLokiClientUsable() {
458
+ return instance$3.has() || instance$3.hasFactory();
459
+ }
460
+ function useLokiClient() {
391
461
  return instance$3.use();
392
462
  }
393
463
 
464
+ /*
465
+ * Copyright (c) 2025.
466
+ * Author Peter Placzek (tada5hi)
467
+ * For the full copyright and license information,
468
+ * view the LICENSE file that was distributed with this source code.
469
+ */ function _define_property$6(obj, key, value) {
470
+ if (key in obj) {
471
+ Object.defineProperty(obj, key, {
472
+ value: value,
473
+ enumerable: true,
474
+ configurable: true,
475
+ writable: true
476
+ });
477
+ } else {
478
+ obj[key] = value;
479
+ }
480
+ return obj;
481
+ }
482
+ class BaseLogStore {
483
+ setLabels(labels) {
484
+ this.labels = labels;
485
+ }
486
+ getLabels() {
487
+ return this.labels;
488
+ }
489
+ extendLabels(labels) {
490
+ const keys = Object.keys(labels);
491
+ for(let i = 0; i < keys.length; i++){
492
+ this.labels[keys[i]] = labels[keys[i]];
493
+ }
494
+ }
495
+ constructor(){
496
+ _define_property$6(this, "labels", void 0);
497
+ }
498
+ }
499
+
500
+ /*
501
+ * Copyright (c) 2025.
502
+ * Author Peter Placzek (tada5hi)
503
+ * For the full copyright and license information,
504
+ * view the LICENSE file that was distributed with this source code.
505
+ */ function _define_property$5(obj, key, value) {
506
+ if (key in obj) {
507
+ Object.defineProperty(obj, key, {
508
+ value: value,
509
+ enumerable: true,
510
+ configurable: true,
511
+ writable: true
512
+ });
513
+ } else {
514
+ obj[key] = value;
515
+ }
516
+ return obj;
517
+ }
518
+ class LokiLogStore extends BaseLogStore {
519
+ async write(message, labels) {
520
+ let data;
521
+ if (typeof message === 'string') {
522
+ const labelsNormalized = {
523
+ ...this.labels,
524
+ ...labels || {}
525
+ };
526
+ const level = labelsNormalized.level || LogLevel.DEBUG;
527
+ delete labelsNormalized.level;
528
+ data = {
529
+ message,
530
+ level,
531
+ time: nanoSeconds(),
532
+ labels: labelsNormalized
533
+ };
534
+ } else {
535
+ const labelsNormalized = {
536
+ ...this.labels,
537
+ ...message.labels || {},
538
+ ...labels || {}
539
+ };
540
+ const level = message.level || labelsNormalized.level || LogLevel.DEBUG;
541
+ delete labelsNormalized.level;
542
+ data = {
543
+ ...message,
544
+ level,
545
+ time: message.time || nanoSeconds(),
546
+ labels: labelsNormalized
547
+ };
548
+ }
549
+ const stream = {
550
+ stream: data.labels,
551
+ values: [
552
+ [
553
+ data.time,
554
+ data.message,
555
+ {
556
+ level: data.level
557
+ }
558
+ ]
559
+ ]
560
+ };
561
+ await this.instance.distributor.push(stream);
562
+ return data;
563
+ }
564
+ async delete(options) {
565
+ await this.instance.compactor.createDeletionRequest({
566
+ start: options.start,
567
+ ...options.end ? {
568
+ end: options.end
569
+ } : {},
570
+ query: this.buildQuery({
571
+ ...this.labels,
572
+ ...options.labels || {}
573
+ })
574
+ });
575
+ }
576
+ async query(input) {
577
+ const options = {
578
+ query: this.buildQuery({
579
+ ...this.labels,
580
+ ...input.labels || {}
581
+ })
582
+ };
583
+ if (input.sort) {
584
+ options.direction = input.sort === 'DESC' ? 'forward' : 'backward';
585
+ }
586
+ if (input.limit) {
587
+ options.limit = input.limit;
588
+ }
589
+ if (input.start) {
590
+ options.start = BigInt(new Date(`${input.start}`).getTime()) * 1000000n;
591
+ }
592
+ if (input.end) {
593
+ options.end = BigInt(new Date(`${input.end}`).getTime()) * 1000000n;
594
+ }
595
+ const output = [];
596
+ const response = await this.instance.querier.queryRange(options);
597
+ if (response.data.resultType === 'streams') {
598
+ for(let i = 0; i < response.data.result.length; i++){
599
+ const set = response.data.result[i];
600
+ const labels = set.stream;
601
+ let level;
602
+ if (labels.level) {
603
+ level = labels.level;
604
+ } else {
605
+ level = labels.detected_level;
606
+ }
607
+ delete labels.level;
608
+ delete labels.detected_level;
609
+ if (!labels.service) {
610
+ labels.service = labels.service_name;
611
+ }
612
+ delete labels.service_name;
613
+ for(let j = 0; j < set.values.length; j++){
614
+ output.push({
615
+ time: BigInt(set.values[j][0]),
616
+ message: set.values[j][1],
617
+ level,
618
+ labels
619
+ });
620
+ }
621
+ }
622
+ }
623
+ return [
624
+ output,
625
+ output.length
626
+ ];
627
+ }
628
+ // ----------------------------------------------
629
+ buildQuery(labels) {
630
+ const output = [];
631
+ const keys = Object.keys(labels);
632
+ for(let i = 0; i < keys.length; i++){
633
+ output.push(`${keys[i]}="${labels[keys[i]]}"`);
634
+ }
635
+ return `{${output.join(',')}}`;
636
+ }
637
+ constructor(instance, labels){
638
+ super(), _define_property$5(this, "instance", void 0);
639
+ this.instance = instance;
640
+ this.labels = labels || {};
641
+ }
642
+ }
643
+
644
+ /*
645
+ * Copyright (c) 2025.
646
+ * Author Peter Placzek (tada5hi)
647
+ * For the full copyright and license information,
648
+ * view the LICENSE file that was distributed with this source code.
649
+ */ function _define_property$4(obj, key, value) {
650
+ if (key in obj) {
651
+ Object.defineProperty(obj, key, {
652
+ value: value,
653
+ enumerable: true,
654
+ configurable: true,
655
+ writable: true
656
+ });
657
+ } else {
658
+ obj[key] = value;
659
+ }
660
+ return obj;
661
+ }
662
+ class MemoryLogStore extends BaseLogStore {
663
+ // todo: apply query options
664
+ async delete() {
665
+ return Promise.resolve();
666
+ }
667
+ async query(options = {}) {
668
+ // todo: apply all query options
669
+ const data = this.items.filter((item)=>{
670
+ if (options.labels) {
671
+ if (!item.labels) {
672
+ return false;
673
+ }
674
+ const labelKeys = Object.keys(options.labels);
675
+ for(let i = 0; i < labelKeys.length; i++){
676
+ if (!item.labels[labelKeys[i]]) {
677
+ return false;
678
+ }
679
+ if (item.labels[labelKeys[i]] !== options.labels[labelKeys[i]]) {
680
+ return false;
681
+ }
682
+ }
683
+ }
684
+ return true;
685
+ });
686
+ return [
687
+ data,
688
+ data.length
689
+ ];
690
+ }
691
+ async write(message, labels) {
692
+ let data;
693
+ if (typeof message === 'string') {
694
+ const labelsNormalized = {
695
+ ...this.labels,
696
+ ...labels || {}
697
+ };
698
+ const level = labelsNormalized.level || LogLevel.DEBUG;
699
+ delete labelsNormalized.level;
700
+ data = {
701
+ message,
702
+ level,
703
+ time: nanoSeconds(),
704
+ labels: labelsNormalized
705
+ };
706
+ } else {
707
+ const labelsNormalized = {
708
+ ...this.labels,
709
+ ...message.labels || {},
710
+ ...labels || {}
711
+ };
712
+ const level = message.level || labelsNormalized.level || LogLevel.DEBUG;
713
+ delete labelsNormalized.level;
714
+ data = {
715
+ ...message,
716
+ level,
717
+ time: message.time || nanoSeconds(),
718
+ labels: labelsNormalized
719
+ };
720
+ }
721
+ this.items.push(data);
722
+ return data;
723
+ }
724
+ constructor(labels){
725
+ super(), _define_property$4(this, "items", void 0);
726
+ this.items = [];
727
+ this.labels = labels;
728
+ }
729
+ }
730
+
731
+ const instance$2 = singa({
732
+ name: 'logStore',
733
+ factory: ()=>{
734
+ if (isLokiClientUsable()) {
735
+ const loki = useLokiClient();
736
+ return new LokiLogStore(loki);
737
+ }
738
+ return new MemoryLogStore();
739
+ }
740
+ });
741
+ function isLogStoreUsable() {
742
+ return instance$2.has() || instance$2.hasFactory();
743
+ }
744
+ function setLogStoreFactory(input) {
745
+ instance$2.setFactory(input);
746
+ }
747
+ function useLogStore() {
748
+ return instance$2.use();
749
+ }
750
+
394
751
  function createLogger(ctx = {}) {
395
- const { directory, options = {} } = ctx;
752
+ const store = ctx.store || useLogStore();
396
753
  let loggerTransports;
397
754
  if (read('env') === EnvironmentName.PRODUCTION) {
398
755
  loggerTransports = [
@@ -400,50 +757,58 @@ function createLogger(ctx = {}) {
400
757
  level: 'info'
401
758
  }),
402
759
  new transports.File({
403
- filename: path.join(directory || process.cwd(), 'access.log'),
760
+ filename: path.join(ctx.directory || process.cwd(), 'access.log'),
404
761
  level: 'http',
405
762
  maxsize: 10 * 1024 * 1024,
406
763
  maxFiles: 5
407
764
  }),
408
765
  new transports.File({
409
- filename: path.join(directory || process.cwd(), 'error.log'),
766
+ filename: path.join(ctx.directory || process.cwd(), 'error.log'),
410
767
  level: 'warn',
411
768
  maxsize: 10 * 1024 * 1024,
412
769
  maxFiles: 5
770
+ }),
771
+ new LogStoreTransport(store, {
772
+ level: 'http',
773
+ labels: ctx.labels
413
774
  })
414
775
  ];
415
776
  } else {
416
777
  loggerTransports = [
417
778
  new transports.Console({
418
779
  level: 'debug'
780
+ }),
781
+ new LogStoreTransport(store, {
782
+ level: 'http',
783
+ labels: ctx.labels
419
784
  })
420
785
  ];
421
786
  }
422
787
  return createLogger$1({
423
788
  format: format.combine(format.errors({
424
789
  stack: true
425
- }), format.timestamp(), format.colorize(), format.simple()),
790
+ }), format.timestamp(), format.simple()),
426
791
  level: 'debug',
427
792
  transports: loggerTransports,
428
793
  // todo: deeply merge options
429
- ...options || {}
794
+ ...ctx.options || {}
430
795
  });
431
796
  }
432
797
 
433
- const instance$2 = singa({
798
+ const instance$1 = singa({
434
799
  name: 'logger'
435
800
  });
436
801
  function setLoggerFactory(factory) {
437
- instance$2.setFactory(factory);
802
+ instance$1.setFactory(factory);
438
803
  }
439
804
  function isLoggerUsable() {
440
- return instance$2.has() || instance$2.hasFactory();
805
+ return instance$1.has() || instance$1.hasFactory();
441
806
  }
442
807
  function setLogger(input) {
443
- instance$2.set(input);
808
+ instance$1.set(input);
444
809
  }
445
810
  function useLogger() {
446
- return instance$2.use();
811
+ return instance$1.use();
447
812
  }
448
813
 
449
814
  /*
@@ -473,14 +838,14 @@ class DomainEventPublisher {
473
838
  await this.publish(ctx);
474
839
  } catch (e) {
475
840
  if (isLoggerUsable()) {
476
- useLogger().error(`Publishing event ${buildDomainEventFullName(ctx.data.type, ctx.data.event)} failed`);
841
+ useLogger().error(`Publishing event ${buildDomainEventFullName(ctx.metadata.domain, ctx.metadata.event)} failed`);
477
842
  useLogger().error(e);
478
843
  }
479
844
  }
480
845
  }
481
846
  async publish(ctx) {
482
847
  if (isLoggerUsable()) {
483
- useLogger().info(`Publishing event ${buildDomainEventFullName(ctx.data.type, ctx.data.event)}`);
848
+ useLogger().info(`Publishing event ${buildDomainEventFullName(ctx.metadata.domain, ctx.metadata.event)}`);
484
849
  }
485
850
  const publishers = this.publishers.values();
486
851
  while(true){
@@ -536,15 +901,29 @@ function buildEventChannelName(input, id) {
536
901
  }
537
902
  class DomainEventRedisPublisher {
538
903
  async publish(ctx) {
539
- const data = JSON.stringify(transformEventData(ctx.data));
904
+ const payload = {
905
+ type: ctx.metadata.domain,
906
+ event: ctx.metadata.event,
907
+ data: transformEventData(ctx.data)
908
+ };
909
+ const payloadSerialized = JSON.stringify(payload);
540
910
  const pipeline = this.driver.pipeline();
541
911
  for(let i = 0; i < ctx.destinations.length; i++){
542
- const keyPrefix = ctx.destinations[i].namespace ? `${ctx.destinations[i].namespace}:` : '';
543
- let key = keyPrefix + buildEventChannelName(ctx.destinations[i].channel);
544
- pipeline.publish(key, data);
545
- if (typeof ctx.destinations[i].channel === 'function') {
546
- key = keyPrefix + buildEventChannelName(ctx.destinations[i].channel, ctx.data.data.id);
547
- pipeline.publish(key, data);
912
+ const destination = ctx.destinations[i];
913
+ let keyPrefix;
914
+ if (destination.namespace) {
915
+ keyPrefix = typeof destination.namespace === 'function' ? destination.namespace(ctx.data) : destination.namespace;
916
+ }
917
+ let key;
918
+ if (keyPrefix) {
919
+ key = keyPrefix + buildEventChannelName(destination.channel);
920
+ } else {
921
+ key = buildEventChannelName(destination.channel);
922
+ }
923
+ pipeline.publish(key, payloadSerialized);
924
+ if (typeof destination.channel === 'function') {
925
+ key = keyPrefix + buildEventChannelName(destination.channel, ctx.data.id);
926
+ pipeline.publish(key, payloadSerialized);
548
927
  }
549
928
  }
550
929
  await pipeline.exec();
@@ -577,23 +956,28 @@ class DomainEventSocketPublisher {
577
956
  async publish(ctx) {
578
957
  ctx.data = transformEventData(ctx.data);
579
958
  for(let i = 0; i < ctx.destinations.length; i++){
959
+ const destination = ctx.destinations[i];
580
960
  let namespace;
581
- if (ctx.destinations[i].namespace) {
582
- namespace = ctx.destinations[i].namespace;
961
+ if (destination.namespace) {
962
+ namespace = typeof destination.namespace === 'function' ? destination.namespace(ctx.data) : destination.namespace;
583
963
  } else {
584
964
  namespace = '/';
585
965
  }
586
966
  const emitter = new Emitter(this.client, {}, namespace);
587
- const fullEventName = buildDomainEventFullName(ctx.data.type, ctx.data.event);
967
+ const fullEventName = buildDomainEventFullName(ctx.metadata.domain, ctx.metadata.event);
588
968
  const rooms = [
589
- buildEventChannelName(ctx.destinations[i].channel)
969
+ buildEventChannelName(destination.channel)
590
970
  ];
591
- if (typeof ctx.destinations[i].channel === 'function') {
592
- rooms.push(buildEventChannelName(ctx.destinations[i].channel, ctx.data.data.id));
971
+ if (typeof destination.channel === 'function') {
972
+ rooms.push(buildEventChannelName(destination.channel, ctx.data.id));
593
973
  }
594
974
  for(let j = 0; j < rooms.length; j++){
595
975
  emitter.in(rooms[j]).emit(fullEventName, {
596
- ...ctx.data,
976
+ data: {
977
+ data: ctx.data,
978
+ type: ctx.metadata.domain,
979
+ event: ctx.metadata.event
980
+ },
597
981
  meta: {
598
982
  namespace,
599
983
  roomName: rooms[j]
@@ -608,7 +992,7 @@ class DomainEventSocketPublisher {
608
992
  }
609
993
  }
610
994
 
611
- const instance$1 = singa({
995
+ const singaInstance = singa({
612
996
  name: 'domainEventPublisher',
613
997
  factory: ()=>{
614
998
  const publisher = new DomainEventPublisher();
@@ -620,8 +1004,11 @@ const instance$1 = singa({
620
1004
  return publisher;
621
1005
  }
622
1006
  });
1007
+ function useDomainEventPublisherSinga() {
1008
+ return singaInstance;
1009
+ }
623
1010
  function useDomainEventPublisher() {
624
- return instance$1.use();
1011
+ return singaInstance.use();
625
1012
  }
626
1013
 
627
1014
  /*
@@ -744,5 +1131,5 @@ function useQueueRouter() {
744
1131
  return Object.prototype.hasOwnProperty.call(obj, prop);
745
1132
  }
746
1133
 
747
- export { AuthupClient, Cache, ComponentError, DomainEventPublisher, DomainEventRedisPublisher, DomainEventSocketPublisher, MemoryCacheAdapter, QueueRouter, QueueRouterRoutingType, RedisCacheAdapter, buildCacheKey, buildQueueRouterPublishPayload, createCacheAdapter, createLogger, guessAuthupTokenCreatorOptions, hasOwnProperty, isAmqpClientUsable, isAuthupClientUsable, isClientAuthenticationHookUsable, isComponentError, isLoggerUsable, isQueuePayload, isQueueRouterPayload, isQueueRouterUsable, isRedisClientUsable, isVaultClientUsable, setAmqpClientFactory, setAuthupClientFactory, setClientAuthenticationHookFactory, setLogger, setLoggerFactory, setRedisClient, setRedisFactory, setVaultFactory, useAmqpClient, useAuthupClient, useCache, useClientAuthenticationHook, useDomainEventPublisher, useLogger, useQueueRouter, useRedisClient, useRedisPublishClient, useRedisSubscribeClient, useVaultClient };
1134
+ export { AuthupClient, Cache, ComponentError, DomainEventPublisher, DomainEventRedisPublisher, DomainEventSocketPublisher, LokiLogStore, MemoryCacheAdapter, MemoryLogStore, QueueRouter, QueueRouterRoutingType, RedisCacheAdapter, buildCacheKey, buildQueueRouterPublishPayload, createCacheAdapter, createLogger, guessAuthupTokenCreatorOptions, hasOwnProperty, isAmqpClientUsable, isAuthupClientUsable, isClientAuthenticationHookUsable, isComponentError, isLogStoreUsable, isLoggerUsable, isLokiClientUsable, isQueuePayload, isQueueRouterPayload, isQueueRouterUsable, isRedisClientUsable, isVaultClientUsable, setAmqpClientFactory, setAuthupClientFactory, setClientAuthenticationHookFactory, setLogStoreFactory, setLogger, setLoggerFactory, setLokiFactory, setRedisClient, setRedisFactory, setVaultFactory, useAmqpClient, useAuthupClient, useCache, useClientAuthenticationHook, useDomainEventPublisher, useDomainEventPublisherSinga, useLogStore, useLogger, useLokiClient, useQueueRouter, useRedisClient, useRedisPublishClient, useRedisSubscribeClient, useVaultClient };
748
1135
  //# sourceMappingURL=index.mjs.map