@privateaim/server-kit 0.8.14 → 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.
- package/CHANGELOG.md +15 -0
- package/dist/index.cjs +429 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +426 -53
- package/dist/index.mjs.map +1 -1
- package/dist/services/domain-event/index.d.ts +1 -1
- package/dist/services/domain-event/index.d.ts.map +1 -1
- package/dist/services/domain-event/module.d.ts +4 -4
- package/dist/services/domain-event/module.d.ts.map +1 -1
- package/dist/services/domain-event/redis/module.d.ts +2 -2
- package/dist/services/domain-event/redis/module.d.ts.map +1 -1
- package/dist/services/domain-event/singleton.d.ts +1 -0
- package/dist/services/domain-event/singleton.d.ts.map +1 -1
- package/dist/services/domain-event/socket/module.d.ts +2 -2
- package/dist/services/domain-event/socket/module.d.ts.map +1 -1
- package/dist/services/domain-event/types.d.ts +29 -0
- package/dist/services/domain-event/types.d.ts.map +1 -0
- package/dist/services/domain-event/utils.d.ts +2 -2
- package/dist/services/domain-event/utils.d.ts.map +1 -1
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/log-store/entities/base.d.ts +7 -0
- package/dist/services/log-store/entities/base.d.ts.map +1 -0
- package/dist/services/log-store/entities/index.d.ts +3 -0
- package/dist/services/log-store/entities/index.d.ts.map +1 -0
- package/dist/services/log-store/entities/loki.d.ts +13 -0
- package/dist/services/log-store/entities/loki.d.ts.map +1 -0
- package/dist/services/log-store/entities/memory.d.ts +11 -0
- package/dist/services/log-store/entities/memory.d.ts.map +1 -0
- package/dist/services/log-store/index.d.ts +4 -0
- package/dist/services/log-store/index.d.ts.map +1 -0
- package/dist/services/log-store/singleton.d.ts +6 -0
- package/dist/services/log-store/singleton.d.ts.map +1 -0
- package/dist/services/log-store/types.d.ts +27 -0
- package/dist/services/log-store/types.d.ts.map +1 -0
- package/dist/services/logger/module.d.ts.map +1 -1
- package/dist/services/logger/store.d.ts +14 -0
- package/dist/services/logger/store.d.ts.map +1 -0
- package/dist/services/logger/types.d.ts +3 -0
- package/dist/services/logger/types.d.ts.map +1 -1
- package/package.json +9 -7
- package/src/services/domain-event/index.ts +1 -1
- package/src/services/domain-event/module.ts +8 -8
- package/src/services/domain-event/redis/module.ts +28 -9
- package/src/services/domain-event/singleton.ts +6 -2
- package/src/services/domain-event/socket/module.ts +20 -9
- package/src/services/domain-event/types.ts +50 -0
- package/src/services/domain-event/utils.ts +2 -2
- package/src/services/index.ts +1 -0
- package/src/services/log-store/entities/base.ts +25 -0
- package/src/services/log-store/entities/index.ts +9 -0
- package/src/services/log-store/entities/loki.ts +158 -0
- package/src/services/log-store/entities/memory.ts +100 -0
- package/src/services/log-store/index.ts +10 -0
- package/src/services/log-store/singleton.ts +36 -0
- package/src/services/log-store/types.ts +42 -0
- package/src/services/logger/module.ts +20 -5
- package/src/services/logger/store.ts +70 -0
- package/src/services/logger/types.ts +4 -1
- package/test/unit/log-store.spec.ts +21 -0
- package/test/unit/logger.spec.ts +36 -0
- package/dist/services/domain-event/type.d.ts +0 -15
- package/dist/services/domain-event/type.d.ts.map +0 -1
- 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,15 +10,17 @@ 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
|
|
13
|
+
import WinstonTransport from 'winston-transport';
|
|
14
|
+
import { nanoSeconds } from '@hapic/loki';
|
|
14
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';
|
|
16
|
+
import { Emitter } from '@socket.io/redis-emitter';
|
|
15
17
|
|
|
16
18
|
/*
|
|
17
19
|
* Copyright (c) 2023-2024.
|
|
18
20
|
* Author Peter Placzek (tada5hi)
|
|
19
21
|
* For the full copyright and license information,
|
|
20
22
|
* view the LICENSE file that was distributed with this source code.
|
|
21
|
-
*/ function _define_property$
|
|
23
|
+
*/ function _define_property$b(obj, key, value) {
|
|
22
24
|
if (key in obj) {
|
|
23
25
|
Object.defineProperty(obj, key, {
|
|
24
26
|
value: value,
|
|
@@ -35,7 +37,7 @@ class ComponentError extends Error {
|
|
|
35
37
|
constructor(input){
|
|
36
38
|
super(input.message, {
|
|
37
39
|
cause: input.cause
|
|
38
|
-
}), _define_property$
|
|
40
|
+
}), _define_property$b(this, "code", void 0);
|
|
39
41
|
this.code = input.code;
|
|
40
42
|
}
|
|
41
43
|
}
|
|
@@ -204,7 +206,7 @@ function useRedisSubscribeClient() {
|
|
|
204
206
|
* Author Peter Placzek (tada5hi)
|
|
205
207
|
* For the full copyright and license information,
|
|
206
208
|
* view the LICENSE file that was distributed with this source code.
|
|
207
|
-
*/ function _define_property$
|
|
209
|
+
*/ function _define_property$a(obj, key, value) {
|
|
208
210
|
if (key in obj) {
|
|
209
211
|
Object.defineProperty(obj, key, {
|
|
210
212
|
value: value,
|
|
@@ -252,7 +254,7 @@ class MemoryCacheAdapter {
|
|
|
252
254
|
this.instance.clear();
|
|
253
255
|
}
|
|
254
256
|
constructor(options = {}){
|
|
255
|
-
_define_property$
|
|
257
|
+
_define_property$a(this, "instance", void 0);
|
|
256
258
|
this.instance = new TTLCache({
|
|
257
259
|
checkAgeOnGet: true,
|
|
258
260
|
ttl: Infinity,
|
|
@@ -266,7 +268,7 @@ class MemoryCacheAdapter {
|
|
|
266
268
|
* Author Peter Placzek (tada5hi)
|
|
267
269
|
* For the full copyright and license information,
|
|
268
270
|
* view the LICENSE file that was distributed with this source code.
|
|
269
|
-
*/ function _define_property$
|
|
271
|
+
*/ function _define_property$9(obj, key, value) {
|
|
270
272
|
if (key in obj) {
|
|
271
273
|
Object.defineProperty(obj, key, {
|
|
272
274
|
value: value,
|
|
@@ -315,8 +317,8 @@ class RedisCacheAdapter {
|
|
|
315
317
|
await this.client.flushdb();
|
|
316
318
|
}
|
|
317
319
|
constructor(){
|
|
318
|
-
_define_property$
|
|
319
|
-
_define_property$
|
|
320
|
+
_define_property$9(this, "client", void 0);
|
|
321
|
+
_define_property$9(this, "instance", void 0);
|
|
320
322
|
this.client = useRedisClient();
|
|
321
323
|
this.instance = new JsonAdapter(this.client);
|
|
322
324
|
}
|
|
@@ -338,7 +340,7 @@ function buildCacheKey(options) {
|
|
|
338
340
|
* Author Peter Placzek (tada5hi)
|
|
339
341
|
* For the full copyright and license information,
|
|
340
342
|
* view the LICENSE file that was distributed with this source code.
|
|
341
|
-
*/ function _define_property$
|
|
343
|
+
*/ function _define_property$8(obj, key, value) {
|
|
342
344
|
if (key in obj) {
|
|
343
345
|
Object.defineProperty(obj, key, {
|
|
344
346
|
value: value,
|
|
@@ -371,7 +373,7 @@ class Cache {
|
|
|
371
373
|
return this.adapter.clear(options);
|
|
372
374
|
}
|
|
373
375
|
constructor(adapter){
|
|
374
|
-
_define_property$
|
|
376
|
+
_define_property$8(this, "adapter", void 0);
|
|
375
377
|
this.adapter = adapter;
|
|
376
378
|
}
|
|
377
379
|
}
|
|
@@ -392,8 +394,362 @@ function useCache() {
|
|
|
392
394
|
return instance$4.use();
|
|
393
395
|
}
|
|
394
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() {
|
|
461
|
+
return instance$3.use();
|
|
462
|
+
}
|
|
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
|
+
|
|
395
751
|
function createLogger(ctx = {}) {
|
|
396
|
-
const
|
|
752
|
+
const store = ctx.store || useLogStore();
|
|
397
753
|
let loggerTransports;
|
|
398
754
|
if (read('env') === EnvironmentName.PRODUCTION) {
|
|
399
755
|
loggerTransports = [
|
|
@@ -401,50 +757,58 @@ function createLogger(ctx = {}) {
|
|
|
401
757
|
level: 'info'
|
|
402
758
|
}),
|
|
403
759
|
new transports.File({
|
|
404
|
-
filename: path.join(directory || process.cwd(), 'access.log'),
|
|
760
|
+
filename: path.join(ctx.directory || process.cwd(), 'access.log'),
|
|
405
761
|
level: 'http',
|
|
406
762
|
maxsize: 10 * 1024 * 1024,
|
|
407
763
|
maxFiles: 5
|
|
408
764
|
}),
|
|
409
765
|
new transports.File({
|
|
410
|
-
filename: path.join(directory || process.cwd(), 'error.log'),
|
|
766
|
+
filename: path.join(ctx.directory || process.cwd(), 'error.log'),
|
|
411
767
|
level: 'warn',
|
|
412
768
|
maxsize: 10 * 1024 * 1024,
|
|
413
769
|
maxFiles: 5
|
|
770
|
+
}),
|
|
771
|
+
new LogStoreTransport(store, {
|
|
772
|
+
level: 'http',
|
|
773
|
+
labels: ctx.labels
|
|
414
774
|
})
|
|
415
775
|
];
|
|
416
776
|
} else {
|
|
417
777
|
loggerTransports = [
|
|
418
778
|
new transports.Console({
|
|
419
779
|
level: 'debug'
|
|
780
|
+
}),
|
|
781
|
+
new LogStoreTransport(store, {
|
|
782
|
+
level: 'http',
|
|
783
|
+
labels: ctx.labels
|
|
420
784
|
})
|
|
421
785
|
];
|
|
422
786
|
}
|
|
423
787
|
return createLogger$1({
|
|
424
788
|
format: format.combine(format.errors({
|
|
425
789
|
stack: true
|
|
426
|
-
}), format.timestamp(), format.
|
|
790
|
+
}), format.timestamp(), format.simple()),
|
|
427
791
|
level: 'debug',
|
|
428
792
|
transports: loggerTransports,
|
|
429
793
|
// todo: deeply merge options
|
|
430
|
-
...options || {}
|
|
794
|
+
...ctx.options || {}
|
|
431
795
|
});
|
|
432
796
|
}
|
|
433
797
|
|
|
434
|
-
const instance$
|
|
798
|
+
const instance$1 = singa({
|
|
435
799
|
name: 'logger'
|
|
436
800
|
});
|
|
437
801
|
function setLoggerFactory(factory) {
|
|
438
|
-
instance$
|
|
802
|
+
instance$1.setFactory(factory);
|
|
439
803
|
}
|
|
440
804
|
function isLoggerUsable() {
|
|
441
|
-
return instance$
|
|
805
|
+
return instance$1.has() || instance$1.hasFactory();
|
|
442
806
|
}
|
|
443
807
|
function setLogger(input) {
|
|
444
|
-
instance$
|
|
808
|
+
instance$1.set(input);
|
|
445
809
|
}
|
|
446
810
|
function useLogger() {
|
|
447
|
-
return instance$
|
|
811
|
+
return instance$1.use();
|
|
448
812
|
}
|
|
449
813
|
|
|
450
814
|
/*
|
|
@@ -474,14 +838,14 @@ class DomainEventPublisher {
|
|
|
474
838
|
await this.publish(ctx);
|
|
475
839
|
} catch (e) {
|
|
476
840
|
if (isLoggerUsable()) {
|
|
477
|
-
useLogger().error(`Publishing event ${buildDomainEventFullName(ctx.
|
|
841
|
+
useLogger().error(`Publishing event ${buildDomainEventFullName(ctx.metadata.domain, ctx.metadata.event)} failed`);
|
|
478
842
|
useLogger().error(e);
|
|
479
843
|
}
|
|
480
844
|
}
|
|
481
845
|
}
|
|
482
846
|
async publish(ctx) {
|
|
483
847
|
if (isLoggerUsable()) {
|
|
484
|
-
useLogger().info(`Publishing event ${buildDomainEventFullName(ctx.
|
|
848
|
+
useLogger().info(`Publishing event ${buildDomainEventFullName(ctx.metadata.domain, ctx.metadata.event)}`);
|
|
485
849
|
}
|
|
486
850
|
const publishers = this.publishers.values();
|
|
487
851
|
while(true){
|
|
@@ -537,15 +901,29 @@ function buildEventChannelName(input, id) {
|
|
|
537
901
|
}
|
|
538
902
|
class DomainEventRedisPublisher {
|
|
539
903
|
async publish(ctx) {
|
|
540
|
-
const
|
|
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);
|
|
541
910
|
const pipeline = this.driver.pipeline();
|
|
542
911
|
for(let i = 0; i < ctx.destinations.length; i++){
|
|
543
|
-
const
|
|
544
|
-
let
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
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);
|
|
549
927
|
}
|
|
550
928
|
}
|
|
551
929
|
await pipeline.exec();
|
|
@@ -578,23 +956,28 @@ class DomainEventSocketPublisher {
|
|
|
578
956
|
async publish(ctx) {
|
|
579
957
|
ctx.data = transformEventData(ctx.data);
|
|
580
958
|
for(let i = 0; i < ctx.destinations.length; i++){
|
|
959
|
+
const destination = ctx.destinations[i];
|
|
581
960
|
let namespace;
|
|
582
|
-
if (
|
|
583
|
-
namespace = ctx.
|
|
961
|
+
if (destination.namespace) {
|
|
962
|
+
namespace = typeof destination.namespace === 'function' ? destination.namespace(ctx.data) : destination.namespace;
|
|
584
963
|
} else {
|
|
585
964
|
namespace = '/';
|
|
586
965
|
}
|
|
587
966
|
const emitter = new Emitter(this.client, {}, namespace);
|
|
588
|
-
const fullEventName = buildDomainEventFullName(ctx.
|
|
967
|
+
const fullEventName = buildDomainEventFullName(ctx.metadata.domain, ctx.metadata.event);
|
|
589
968
|
const rooms = [
|
|
590
|
-
buildEventChannelName(
|
|
969
|
+
buildEventChannelName(destination.channel)
|
|
591
970
|
];
|
|
592
|
-
if (typeof
|
|
593
|
-
rooms.push(buildEventChannelName(
|
|
971
|
+
if (typeof destination.channel === 'function') {
|
|
972
|
+
rooms.push(buildEventChannelName(destination.channel, ctx.data.id));
|
|
594
973
|
}
|
|
595
974
|
for(let j = 0; j < rooms.length; j++){
|
|
596
975
|
emitter.in(rooms[j]).emit(fullEventName, {
|
|
597
|
-
|
|
976
|
+
data: {
|
|
977
|
+
data: ctx.data,
|
|
978
|
+
type: ctx.metadata.domain,
|
|
979
|
+
event: ctx.metadata.event
|
|
980
|
+
},
|
|
598
981
|
meta: {
|
|
599
982
|
namespace,
|
|
600
983
|
roomName: rooms[j]
|
|
@@ -609,7 +992,7 @@ class DomainEventSocketPublisher {
|
|
|
609
992
|
}
|
|
610
993
|
}
|
|
611
994
|
|
|
612
|
-
const
|
|
995
|
+
const singaInstance = singa({
|
|
613
996
|
name: 'domainEventPublisher',
|
|
614
997
|
factory: ()=>{
|
|
615
998
|
const publisher = new DomainEventPublisher();
|
|
@@ -621,21 +1004,11 @@ const instance$2 = singa({
|
|
|
621
1004
|
return publisher;
|
|
622
1005
|
}
|
|
623
1006
|
});
|
|
624
|
-
function
|
|
625
|
-
return
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
const instance$1 = singa({
|
|
629
|
-
name: 'loki'
|
|
630
|
-
});
|
|
631
|
-
function setLokiFactory(factory) {
|
|
632
|
-
instance$1.setFactory(factory);
|
|
1007
|
+
function useDomainEventPublisherSinga() {
|
|
1008
|
+
return singaInstance;
|
|
633
1009
|
}
|
|
634
|
-
function
|
|
635
|
-
return
|
|
636
|
-
}
|
|
637
|
-
function useLokiClient() {
|
|
638
|
-
return instance$1.use();
|
|
1010
|
+
function useDomainEventPublisher() {
|
|
1011
|
+
return singaInstance.use();
|
|
639
1012
|
}
|
|
640
1013
|
|
|
641
1014
|
/*
|
|
@@ -758,5 +1131,5 @@ function useQueueRouter() {
|
|
|
758
1131
|
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
759
1132
|
}
|
|
760
1133
|
|
|
761
|
-
export { AuthupClient, Cache, ComponentError, DomainEventPublisher, DomainEventRedisPublisher, DomainEventSocketPublisher, MemoryCacheAdapter, QueueRouter, QueueRouterRoutingType, RedisCacheAdapter, buildCacheKey, buildQueueRouterPublishPayload, createCacheAdapter, createLogger, guessAuthupTokenCreatorOptions, hasOwnProperty, isAmqpClientUsable, isAuthupClientUsable, isClientAuthenticationHookUsable, isComponentError, isLoggerUsable, isLokiClientUsable, isQueuePayload, isQueueRouterPayload, isQueueRouterUsable, isRedisClientUsable, isVaultClientUsable, setAmqpClientFactory, setAuthupClientFactory, setClientAuthenticationHookFactory, setLogger, setLoggerFactory, setLokiFactory, setRedisClient, setRedisFactory, setVaultFactory, useAmqpClient, useAuthupClient, useCache, useClientAuthenticationHook, useDomainEventPublisher, useLogger, useLokiClient, 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 };
|
|
762
1135
|
//# sourceMappingURL=index.mjs.map
|