pp-command-bus 1.5.0 → 2.0.1
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/README.md +400 -1219
- package/dist/command-bus/command-bus.spec.js +138 -359
- package/dist/command-bus/command-bus.spec.js.map +1 -1
- package/dist/command-bus/command.d.ts +3 -4
- package/dist/command-bus/command.js +3 -32
- package/dist/command-bus/command.js.map +1 -1
- package/dist/command-bus/config/command-bus-config.d.ts +80 -21
- package/dist/command-bus/config/command-bus-config.js +104 -58
- package/dist/command-bus/config/command-bus-config.js.map +1 -1
- package/dist/command-bus/config/command-bus-config.spec.js +174 -100
- package/dist/command-bus/config/command-bus-config.spec.js.map +1 -1
- package/dist/command-bus/index.d.ts +41 -50
- package/dist/command-bus/index.js +143 -127
- package/dist/command-bus/index.js.map +1 -1
- package/dist/command-bus/interceptors/index.d.ts +5 -0
- package/dist/command-bus/interceptors/index.js +22 -0
- package/dist/command-bus/interceptors/index.js.map +1 -0
- package/dist/command-bus/interceptors/interceptor.interface.d.ts +35 -0
- package/dist/command-bus/interceptors/interceptor.interface.js +3 -0
- package/dist/command-bus/interceptors/interceptor.interface.js.map +1 -0
- package/dist/command-bus/interceptors/performance-interceptor.d.ts +24 -0
- package/dist/command-bus/interceptors/performance-interceptor.js +86 -0
- package/dist/command-bus/interceptors/performance-interceptor.js.map +1 -0
- package/dist/command-bus/interceptors/performance-interceptor.spec.js +124 -0
- package/dist/command-bus/interceptors/performance-interceptor.spec.js.map +1 -0
- package/dist/command-bus/logging/command-logger.d.ts +2 -0
- package/dist/command-bus/logging/command-logger.js +7 -0
- package/dist/command-bus/logging/command-logger.js.map +1 -1
- package/dist/command-bus/logging/command-logger.spec.js +36 -0
- package/dist/command-bus/logging/command-logger.spec.js.map +1 -1
- package/dist/command-bus/serialization/index.d.ts +6 -0
- package/dist/command-bus/serialization/index.js +9 -0
- package/dist/command-bus/serialization/index.js.map +1 -0
- package/dist/command-bus/serialization/msgpack-serializer.d.ts +26 -0
- package/dist/command-bus/serialization/msgpack-serializer.js +70 -0
- package/dist/command-bus/serialization/msgpack-serializer.js.map +1 -0
- package/dist/command-bus/serialization/msgpack-serializer.spec.js +223 -0
- package/dist/command-bus/serialization/msgpack-serializer.spec.js.map +1 -0
- package/dist/command-bus/serialization/serializer.interface.d.ts +21 -0
- package/dist/command-bus/serialization/serializer.interface.js +3 -0
- package/dist/command-bus/serialization/serializer.interface.js.map +1 -0
- package/dist/command-bus/transport/consumer-loop.d.ts +45 -0
- package/dist/command-bus/transport/consumer-loop.js +90 -0
- package/dist/command-bus/transport/consumer-loop.js.map +1 -0
- package/dist/command-bus/transport/consumer-loop.spec.js +216 -0
- package/dist/command-bus/transport/consumer-loop.spec.js.map +1 -0
- package/dist/command-bus/transport/index.d.ts +21 -0
- package/dist/command-bus/transport/index.js +23 -0
- package/dist/command-bus/transport/index.js.map +1 -0
- package/dist/command-bus/transport/message-processor.d.ts +70 -0
- package/dist/command-bus/transport/message-processor.js +158 -0
- package/dist/command-bus/transport/message-processor.js.map +1 -0
- package/dist/command-bus/transport/message-processor.spec.js +185 -0
- package/dist/command-bus/transport/message-processor.spec.js.map +1 -0
- package/dist/command-bus/transport/pending-recovery.d.ts +54 -0
- package/dist/command-bus/transport/pending-recovery.js +139 -0
- package/dist/command-bus/transport/pending-recovery.js.map +1 -0
- package/dist/command-bus/transport/pending-recovery.spec.js +176 -0
- package/dist/command-bus/transport/pending-recovery.spec.js.map +1 -0
- package/dist/command-bus/transport/redis-codec.d.ts +24 -0
- package/dist/command-bus/transport/redis-codec.js +33 -0
- package/dist/command-bus/transport/redis-codec.js.map +1 -0
- package/dist/command-bus/transport/redis-codec.spec.js +53 -0
- package/dist/command-bus/transport/redis-codec.spec.js.map +1 -0
- package/dist/command-bus/transport/redis-streams-transport.d.ts +94 -0
- package/dist/command-bus/transport/redis-streams-transport.js +143 -0
- package/dist/command-bus/transport/redis-streams-transport.js.map +1 -0
- package/dist/command-bus/transport/redis-streams-transport.spec.js +420 -0
- package/dist/command-bus/transport/redis-streams-transport.spec.js.map +1 -0
- package/dist/command-bus/transport/rpc-handler.d.ts +39 -0
- package/dist/command-bus/transport/rpc-handler.js +87 -0
- package/dist/command-bus/transport/rpc-handler.js.map +1 -0
- package/dist/command-bus/transport/rpc-handler.spec.js +157 -0
- package/dist/command-bus/transport/rpc-handler.spec.js.map +1 -0
- package/dist/command-bus/transport/stream-consumer.d.ts +91 -0
- package/dist/command-bus/transport/stream-consumer.js +182 -0
- package/dist/command-bus/transport/stream-consumer.js.map +1 -0
- package/dist/command-bus/transport/stream-consumer.spec.js +284 -0
- package/dist/command-bus/transport/stream-consumer.spec.js.map +1 -0
- package/dist/command-bus/transport/stream-producer.d.ts +23 -0
- package/dist/command-bus/transport/stream-producer.js +70 -0
- package/dist/command-bus/transport/stream-producer.js.map +1 -0
- package/dist/command-bus/transport/stream-producer.spec.js +125 -0
- package/dist/command-bus/transport/stream-producer.spec.js.map +1 -0
- package/dist/command-bus/transport/transport.interface.d.ts +87 -0
- package/dist/command-bus/transport/transport.interface.js +3 -0
- package/dist/command-bus/transport/transport.interface.js.map +1 -0
- package/dist/command-bus/types/index.d.ts +0 -84
- package/dist/examples/rpc.demo.js +1 -1
- package/dist/examples/rpc.demo.js.map +1 -1
- package/dist/index.d.ts +8 -5
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/dist/pp-command-bus-2.0.1.tgz +0 -0
- package/dist/shared/redis/connection-pool.d.ts +54 -0
- package/dist/shared/redis/connection-pool.js +123 -0
- package/dist/shared/redis/connection-pool.js.map +1 -0
- package/dist/shared/redis/connection-pool.spec.d.ts +1 -0
- package/dist/shared/redis/connection-pool.spec.js +114 -0
- package/dist/shared/redis/connection-pool.spec.js.map +1 -0
- package/dist/shared/redis/index.d.ts +5 -3
- package/dist/shared/redis/index.js +6 -4
- package/dist/shared/redis/index.js.map +1 -1
- package/dist/shared/redis/rpc-connection-pool.d.ts +61 -0
- package/dist/shared/redis/rpc-connection-pool.js +154 -0
- package/dist/shared/redis/rpc-connection-pool.js.map +1 -0
- package/dist/shared/redis/rpc-connection-pool.spec.d.ts +1 -0
- package/dist/shared/redis/rpc-connection-pool.spec.js +173 -0
- package/dist/shared/redis/rpc-connection-pool.spec.js.map +1 -0
- package/dist/shared/types.d.ts +0 -4
- package/dist/shared/utils/error-utils.d.ts +8 -0
- package/dist/shared/utils/error-utils.js +14 -0
- package/dist/shared/utils/error-utils.js.map +1 -0
- package/package.json +12 -12
- package/dist/command-bus/config/auto-config-optimizer.d.ts +0 -35
- package/dist/command-bus/config/auto-config-optimizer.js +0 -52
- package/dist/command-bus/config/auto-config-optimizer.js.map +0 -1
- package/dist/command-bus/config/auto-config-optimizer.spec.js +0 -42
- package/dist/command-bus/config/auto-config-optimizer.spec.js.map +0 -1
- package/dist/command-bus/job/index.d.ts +0 -6
- package/dist/command-bus/job/index.js +0 -15
- package/dist/command-bus/job/index.js.map +0 -1
- package/dist/command-bus/job/job-options-builder.d.ts +0 -21
- package/dist/command-bus/job/job-options-builder.js +0 -58
- package/dist/command-bus/job/job-options-builder.js.map +0 -1
- package/dist/command-bus/job/job-options-builder.spec.js +0 -156
- package/dist/command-bus/job/job-options-builder.spec.js.map +0 -1
- package/dist/command-bus/job/job-processor.d.ts +0 -39
- package/dist/command-bus/job/job-processor.js +0 -203
- package/dist/command-bus/job/job-processor.js.map +0 -1
- package/dist/command-bus/job/job-processor.spec.js +0 -436
- package/dist/command-bus/job/job-processor.spec.js.map +0 -1
- package/dist/command-bus/queue/index.d.ts +0 -5
- package/dist/command-bus/queue/index.js +0 -13
- package/dist/command-bus/queue/index.js.map +0 -1
- package/dist/command-bus/queue/queue-manager.d.ts +0 -56
- package/dist/command-bus/queue/queue-manager.js +0 -163
- package/dist/command-bus/queue/queue-manager.js.map +0 -1
- package/dist/command-bus/queue/queue-manager.spec.js +0 -371
- package/dist/command-bus/queue/queue-manager.spec.js.map +0 -1
- package/dist/command-bus/rpc/index.d.ts +0 -11
- package/dist/command-bus/rpc/index.js +0 -19
- package/dist/command-bus/rpc/index.js.map +0 -1
- package/dist/command-bus/rpc/payload-compression.service.d.ts +0 -50
- package/dist/command-bus/rpc/payload-compression.service.js +0 -215
- package/dist/command-bus/rpc/payload-compression.service.js.map +0 -1
- package/dist/command-bus/rpc/payload-compression.service.spec.js +0 -376
- package/dist/command-bus/rpc/payload-compression.service.spec.js.map +0 -1
- package/dist/command-bus/rpc/rpc-coordinator.d.ts +0 -96
- package/dist/command-bus/rpc/rpc-coordinator.js +0 -500
- package/dist/command-bus/rpc/rpc-coordinator.js.map +0 -1
- package/dist/command-bus/rpc/rpc-coordinator.spec.js +0 -621
- package/dist/command-bus/rpc/rpc-coordinator.spec.js.map +0 -1
- package/dist/command-bus/rpc/rpc-job-cancellation.service.d.ts +0 -82
- package/dist/command-bus/rpc/rpc-job-cancellation.service.js +0 -180
- package/dist/command-bus/rpc/rpc-job-cancellation.service.js.map +0 -1
- package/dist/command-bus/rpc/rpc-job-cancellation.service.spec.js +0 -286
- package/dist/command-bus/rpc/rpc-job-cancellation.service.spec.js.map +0 -1
- package/dist/command-bus/worker/index.d.ts +0 -10
- package/dist/command-bus/worker/index.js +0 -19
- package/dist/command-bus/worker/index.js.map +0 -1
- package/dist/command-bus/worker/worker-benchmark.d.ts +0 -71
- package/dist/command-bus/worker/worker-benchmark.js +0 -202
- package/dist/command-bus/worker/worker-benchmark.js.map +0 -1
- package/dist/command-bus/worker/worker-benchmark.spec.js +0 -310
- package/dist/command-bus/worker/worker-benchmark.spec.js.map +0 -1
- package/dist/command-bus/worker/worker-metrics-collector.d.ts +0 -98
- package/dist/command-bus/worker/worker-metrics-collector.js +0 -242
- package/dist/command-bus/worker/worker-metrics-collector.js.map +0 -1
- package/dist/command-bus/worker/worker-orchestrator.d.ts +0 -70
- package/dist/command-bus/worker/worker-orchestrator.js +0 -339
- package/dist/command-bus/worker/worker-orchestrator.js.map +0 -1
- package/dist/command-bus/worker/worker-orchestrator.spec.js +0 -712
- package/dist/command-bus/worker/worker-orchestrator.spec.js.map +0 -1
- package/dist/examples/auto-config.demo.d.ts +0 -9
- package/dist/examples/auto-config.demo.js +0 -106
- package/dist/examples/auto-config.demo.js.map +0 -1
- package/dist/examples/rpc-compression.demo.d.ts +0 -5
- package/dist/examples/rpc-compression.demo.js +0 -358
- package/dist/examples/rpc-compression.demo.js.map +0 -1
- package/dist/examples/rpc-resilience.demo.d.ts +0 -15
- package/dist/examples/rpc-resilience.demo.js +0 -233
- package/dist/examples/rpc-resilience.demo.js.map +0 -1
- package/dist/pp-command-bus-1.5.0.tgz +0 -0
- package/dist/shared/config/base-config.d.ts +0 -54
- package/dist/shared/config/base-config.js +0 -114
- package/dist/shared/config/base-config.js.map +0 -1
- package/dist/shared/config/base-config.spec.js +0 -204
- package/dist/shared/config/base-config.spec.js.map +0 -1
- package/dist/shared/config/index.d.ts +0 -1
- package/dist/shared/config/index.js +0 -9
- package/dist/shared/config/index.js.map +0 -1
- package/dist/shared/redis/redis-connection-factory.d.ts +0 -66
- package/dist/shared/redis/redis-connection-factory.js +0 -113
- package/dist/shared/redis/redis-connection-factory.js.map +0 -1
- /package/dist/command-bus/{config/auto-config-optimizer.spec.d.ts → interceptors/performance-interceptor.spec.d.ts} +0 -0
- /package/dist/command-bus/{job/job-options-builder.spec.d.ts → serialization/msgpack-serializer.spec.d.ts} +0 -0
- /package/dist/command-bus/{job/job-processor.spec.d.ts → transport/consumer-loop.spec.d.ts} +0 -0
- /package/dist/command-bus/{queue/queue-manager.spec.d.ts → transport/message-processor.spec.d.ts} +0 -0
- /package/dist/command-bus/{rpc/payload-compression.service.spec.d.ts → transport/pending-recovery.spec.d.ts} +0 -0
- /package/dist/command-bus/{rpc/rpc-coordinator.spec.d.ts → transport/redis-codec.spec.d.ts} +0 -0
- /package/dist/command-bus/{rpc/rpc-job-cancellation.service.spec.d.ts → transport/redis-streams-transport.spec.d.ts} +0 -0
- /package/dist/command-bus/{worker/worker-benchmark.spec.d.ts → transport/rpc-handler.spec.d.ts} +0 -0
- /package/dist/command-bus/{worker/worker-orchestrator.spec.d.ts → transport/stream-consumer.spec.d.ts} +0 -0
- /package/dist/{shared/config/base-config.spec.d.ts → command-bus/transport/stream-producer.spec.d.ts} +0 -0
|
@@ -8,73 +8,101 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
15
|
+
const serialization_1 = require("./serialization");
|
|
16
|
+
const transport_1 = require("./transport");
|
|
17
|
+
const connection_pool_1 = __importDefault(require("../shared/redis/connection-pool"));
|
|
18
|
+
const rpc_connection_pool_1 = __importDefault(require("../shared/redis/rpc-connection-pool"));
|
|
16
19
|
const logging_1 = require("./logging");
|
|
17
|
-
const
|
|
20
|
+
const interceptors_1 = require("./interceptors");
|
|
21
|
+
const error_utils_1 = require("../shared/utils/error-utils");
|
|
18
22
|
/**
|
|
19
|
-
* Command Bus
|
|
23
|
+
* Command Bus oparty na Redis Streams + DragonflyDB
|
|
24
|
+
*
|
|
20
25
|
* Obsługuje komendy w relacji 1:1 (jedna komenda = jeden handler)
|
|
26
|
+
* API: dispatch(), dispatchBatch(), call(), handle(), close()
|
|
27
|
+
*
|
|
28
|
+
* Technologie:
|
|
29
|
+
* - Redis Streams (XADD/XREADGROUP) — natywny transport
|
|
30
|
+
* - MessagePack — binarna serializacja z obsługą Date
|
|
31
|
+
* - LPUSH/BRPOP — point-to-point RPC
|
|
32
|
+
* - Connection pool z round-robin — wielowątkowy DragonflyDB
|
|
21
33
|
*/
|
|
22
34
|
class CommandBus {
|
|
23
35
|
constructor(config) {
|
|
24
36
|
this.config = config;
|
|
25
37
|
/**
|
|
26
|
-
* Handlery komend
|
|
38
|
+
* Handlery komend — tylko jeden per typ komendy
|
|
27
39
|
*/
|
|
28
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
40
|
this.commandHandlers = {};
|
|
30
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Promises z transport.consume() — do graceful shutdown
|
|
43
|
+
*/
|
|
44
|
+
this.consumePromises = [];
|
|
31
45
|
this.logger = config.logger;
|
|
32
46
|
this.logger.debug('Konstruktor CommandBus', {
|
|
33
|
-
config:
|
|
47
|
+
config: {
|
|
48
|
+
poolSize: config.poolSize,
|
|
49
|
+
maxConcurrentRpc: config.maxConcurrentRpc,
|
|
50
|
+
batchSize: config.batchSize,
|
|
51
|
+
maxAttempts: config.maxAttempts,
|
|
52
|
+
claimTimeout: config.claimTimeout,
|
|
53
|
+
maxRetained: config.maxRetained,
|
|
54
|
+
},
|
|
34
55
|
timestamp: new Date().toISOString(),
|
|
35
56
|
});
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
// getRedisOptions() zwraca ConnectionOptions z BullMQ, które są kompatybilne z RedisConnectionOptions
|
|
57
|
+
// Serializer: MessagePack z natywną obsługą Date (eliminuje reconstructDates)
|
|
58
|
+
this.serializer = new serialization_1.MsgpackSerializer();
|
|
59
|
+
// Opcje Redis z konfiguracji
|
|
40
60
|
const redisOptions = this.config.getRedisOptions();
|
|
41
|
-
//
|
|
42
|
-
this.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
//
|
|
48
|
-
this.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
//
|
|
54
|
-
this.
|
|
55
|
-
//
|
|
56
|
-
this.
|
|
57
|
-
|
|
61
|
+
// Pula połączeń Redis — round-robin dla XADD, XACK, DEL, pipeline
|
|
62
|
+
this.connectionPool = new connection_pool_1.default({
|
|
63
|
+
size: config.poolSize,
|
|
64
|
+
redisOptions,
|
|
65
|
+
logger: this.logger,
|
|
66
|
+
});
|
|
67
|
+
// Pula połączeń RPC — bounded, lazy, dla blocking BRPOP
|
|
68
|
+
this.rpcConnectionPool = new rpc_connection_pool_1.default({
|
|
69
|
+
maxSize: config.maxConcurrentRpc,
|
|
70
|
+
redisOptions,
|
|
71
|
+
logger: this.logger,
|
|
72
|
+
});
|
|
73
|
+
// Interceptor wydajności z konfiguracją
|
|
74
|
+
this.performanceInterceptor = new interceptors_1.PerformanceInterceptor(this.logger, config.slowHandlerThreshold);
|
|
75
|
+
// Transport: Redis Streams
|
|
76
|
+
this.transport = new transport_1.RedisStreamsTransport({
|
|
77
|
+
pool: this.connectionPool,
|
|
78
|
+
rpcPool: this.rpcConnectionPool,
|
|
79
|
+
serializer: this.serializer,
|
|
80
|
+
logger: this.logger,
|
|
81
|
+
maxRetained: config.maxRetained,
|
|
82
|
+
maxAttempts: config.maxAttempts,
|
|
83
|
+
claimTimeout: config.claimTimeout,
|
|
84
|
+
batchSize: config.batchSize,
|
|
85
|
+
concurrency: config.concurrency,
|
|
86
|
+
interceptor: this.performanceInterceptor,
|
|
87
|
+
});
|
|
88
|
+
// CommandLogger jeśli commandLog jest skonfigurowane
|
|
58
89
|
if (this.config.commandLog) {
|
|
59
90
|
this.commandLogger = new logging_1.CommandLogger(this.config.commandLog, this.logger);
|
|
60
91
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
this.workerOrchestrator = new worker_1.WorkerOrchestrator(this.workerRedisConnection, this.jobProcessor, this.config.concurrency, this.config.maxAttempts, this.logger);
|
|
65
|
-
this.logger.debug('CommandBus gotowy do użycia', {
|
|
92
|
+
this.logger.log('CommandBus gotowy do użycia', {
|
|
93
|
+
poolSize: this.config.poolSize,
|
|
94
|
+
maxConcurrentRpc: this.config.maxConcurrentRpc,
|
|
66
95
|
timestamp: new Date().toISOString(),
|
|
67
96
|
});
|
|
68
97
|
}
|
|
69
98
|
/**
|
|
70
|
-
* Wysyła komendę do
|
|
71
|
-
*
|
|
72
|
-
*
|
|
99
|
+
* Wysyła komendę do strumienia (fire-and-forget)
|
|
100
|
+
* XADD = 1 natywne polecenie Redis
|
|
101
|
+
*
|
|
73
102
|
* @param command - Komenda do wysłania
|
|
74
103
|
*/
|
|
75
104
|
dispatch(command) {
|
|
76
105
|
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
-
var _a;
|
|
78
106
|
const commandName = command.__name;
|
|
79
107
|
const commandId = command.__id;
|
|
80
108
|
const timestamp = new Date().toISOString();
|
|
@@ -83,141 +111,129 @@ class CommandBus {
|
|
|
83
111
|
commandId,
|
|
84
112
|
timestamp,
|
|
85
113
|
});
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
const job = yield queue.add(commandName, processedCommand, this.jobOptionsBuilder.buildStandardOptions());
|
|
90
|
-
this.logger.debug('Komenda dodana do kolejki', {
|
|
114
|
+
const data = this.serializer.serialize(command);
|
|
115
|
+
const messageId = yield this.transport.enqueue(`cmd:${commandName}`, data);
|
|
116
|
+
this.logger.debug('Komenda dodana do strumienia', {
|
|
91
117
|
commandName,
|
|
92
118
|
commandId,
|
|
93
|
-
|
|
94
|
-
compressed: processedCommand.__compressed,
|
|
95
|
-
queuePosition: ((_a = job.opts) === null || _a === void 0 ? void 0 : _a.delay) ? 'delayed' : 'active',
|
|
119
|
+
messageId,
|
|
96
120
|
timestamp,
|
|
97
121
|
});
|
|
98
122
|
});
|
|
99
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Wysyła wiele komend naraz z użyciem pipelining
|
|
126
|
+
* DragonflyDB przetwarza pipeline równolegle na wielu wątkach
|
|
127
|
+
*
|
|
128
|
+
* @param commands - Tablica komend do wysłania
|
|
129
|
+
*/
|
|
130
|
+
dispatchBatch(commands) {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
if (commands.length === 0)
|
|
133
|
+
return;
|
|
134
|
+
this.logger.debug('Wysyłanie batch komend', {
|
|
135
|
+
count: commands.length,
|
|
136
|
+
timestamp: new Date().toISOString(),
|
|
137
|
+
});
|
|
138
|
+
const entries = commands.map((cmd) => ({
|
|
139
|
+
streamName: `cmd:${cmd.__name}`,
|
|
140
|
+
data: this.serializer.serialize(cmd),
|
|
141
|
+
}));
|
|
142
|
+
yield this.transport.enqueueBatch(entries);
|
|
143
|
+
this.logger.debug('Batch komend wysłany', {
|
|
144
|
+
count: commands.length,
|
|
145
|
+
timestamp: new Date().toISOString(),
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
}
|
|
100
149
|
/**
|
|
101
150
|
* Rejestruje handler dla komendy
|
|
102
|
-
*
|
|
151
|
+
*
|
|
103
152
|
* @param commandDefinition - Definicja komendy (klasa)
|
|
104
153
|
* @param handler - Handler do obsługi komendy
|
|
105
154
|
*/
|
|
106
|
-
handle(
|
|
107
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
108
|
-
commandDefinition, handler) {
|
|
155
|
+
handle(commandDefinition, handler) {
|
|
109
156
|
const commandName = commandDefinition.name;
|
|
110
157
|
if (this.commandHandlers[commandName]) {
|
|
111
158
|
throw new Error(`Handler dla komendy ${commandName} już istnieje`);
|
|
112
159
|
}
|
|
113
160
|
this.commandHandlers[commandName] = handler;
|
|
114
|
-
|
|
161
|
+
// Zarejestruj konsumenta strumienia — śledź promise do graceful shutdown
|
|
162
|
+
const consumePromise = this.transport
|
|
163
|
+
.consume(`cmd:${commandName}`, 'workers', (data) => __awaiter(this, void 0, void 0, function* () {
|
|
164
|
+
const command = this.serializer.deserialize(data);
|
|
165
|
+
// Opcjonalne logowanie komendy
|
|
166
|
+
if (this.commandLogger) {
|
|
167
|
+
void this.commandLogger.logCommand(command);
|
|
168
|
+
}
|
|
169
|
+
return yield handler(command);
|
|
170
|
+
}))
|
|
171
|
+
.catch((error) => {
|
|
172
|
+
this.logger.error('Błąd konsumenta strumienia', {
|
|
173
|
+
commandName,
|
|
174
|
+
error: (0, error_utils_1.getErrorMessage)(error),
|
|
175
|
+
timestamp: new Date().toISOString(),
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
this.consumePromises.push(consumePromise);
|
|
115
179
|
this.logger.debug(`Zarejestrowano handler dla komendy ${commandName}`);
|
|
116
180
|
}
|
|
117
181
|
/**
|
|
118
182
|
* Synchroniczne wywołanie komendy z oczekiwaniem na wynik (RPC)
|
|
119
|
-
* Request przez
|
|
120
|
-
* Automatycznie kompresuje dane jeśli przekraczają threshold
|
|
183
|
+
* Request przez Redis Streams, odpowiedź przez LPUSH/BRPOP
|
|
121
184
|
*
|
|
122
|
-
* Flow
|
|
123
|
-
* 1.
|
|
124
|
-
* 2.
|
|
125
|
-
* 3.
|
|
126
|
-
* 4.
|
|
185
|
+
* Flow (4 komendy Redis):
|
|
186
|
+
* 1. XADD — komenda z metadanymi RPC do strumienia
|
|
187
|
+
* 2. BRPOP — czekaj na odpowiedź na dedykowanym połączeniu
|
|
188
|
+
* 3. Worker: LPUSH — odpowiedź na klucz
|
|
189
|
+
* 4. DEL — cleanup klucza (TTL safety net)
|
|
127
190
|
*
|
|
128
191
|
* @param command - Komenda do wykonania
|
|
129
192
|
* @param timeout - Timeout w milisekundach (domyślnie 30s)
|
|
130
193
|
*/
|
|
131
194
|
call(command_1) {
|
|
132
195
|
return __awaiter(this, arguments, void 0, function* (command, timeout = 30000) {
|
|
133
|
-
var _a, _b;
|
|
134
196
|
const commandName = command.__name;
|
|
135
197
|
const correlationId = command.__id;
|
|
136
198
|
const timestamp = new Date().toISOString();
|
|
137
|
-
this.logger.debug('Rozpoczynam wywołanie RPC
|
|
199
|
+
this.logger.debug('Rozpoczynam wywołanie RPC', {
|
|
138
200
|
commandName,
|
|
139
201
|
commandId: correlationId,
|
|
140
202
|
timeout: `${timeout}ms`,
|
|
141
203
|
timestamp,
|
|
142
204
|
});
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
this.logger.debug('Komenda RPC dodana do kolejki', {
|
|
205
|
+
const startTime = Date.now();
|
|
206
|
+
const data = this.serializer.serialize(command);
|
|
207
|
+
const resultBuffer = yield this.transport.rpcCall(commandName, data, timeout);
|
|
208
|
+
const duration = Date.now() - startTime;
|
|
209
|
+
// Deserializuj odpowiedź RPC
|
|
210
|
+
const response = this.serializer.deserialize(resultBuffer);
|
|
211
|
+
if (response.error) {
|
|
212
|
+
throw new Error(response.error);
|
|
213
|
+
}
|
|
214
|
+
this.logger.log('Wywołanie RPC zakończone', {
|
|
154
215
|
commandName,
|
|
155
216
|
commandId: correlationId,
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
responseChannel: (_a = commandWithMetadata.__rpcMetadata) === null || _a === void 0 ? void 0 : _a.responseChannel,
|
|
159
|
-
queuePosition: ((_b = job.opts) === null || _b === void 0 ? void 0 : _b.delay) ? 'delayed' : 'active',
|
|
160
|
-
timestamp,
|
|
217
|
+
duration,
|
|
218
|
+
timestamp: new Date().toISOString(),
|
|
161
219
|
});
|
|
162
|
-
|
|
163
|
-
if (job.id) {
|
|
164
|
-
this.rpcCoordinator.setJobId(correlationId, job.id, commandName);
|
|
165
|
-
}
|
|
166
|
-
// Krok 5: Zwróć responsePromise - zostanie resolved gdy worker wyśle odpowiedź przez Pub/Sub
|
|
167
|
-
return promise;
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Tworzy callback do usuwania jobów z kolejki
|
|
172
|
-
* Callback jest przekazywany do RpcCoordinator dla możliwości usunięcia joba przy timeout
|
|
173
|
-
* Nie rzuca wyjątków - zwraca boolean sukcesu
|
|
174
|
-
*/
|
|
175
|
-
createRemoveJobCallback() {
|
|
176
|
-
return (queueName, jobId) => __awaiter(this, void 0, void 0, function* () {
|
|
177
|
-
try {
|
|
178
|
-
const queue = this.queueManager.getOrCreateQueue(queueName);
|
|
179
|
-
const job = yield queue.getJob(jobId);
|
|
180
|
-
if (job) {
|
|
181
|
-
yield job.remove();
|
|
182
|
-
this.logger.debug('Job usunięty z kolejki (RPC timeout)', {
|
|
183
|
-
queueName,
|
|
184
|
-
jobId,
|
|
185
|
-
timestamp: new Date().toISOString(),
|
|
186
|
-
});
|
|
187
|
-
return true;
|
|
188
|
-
}
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
catch (error) {
|
|
192
|
-
this.logger.debug('Nie udało się usunąć joba z kolejki (prawdopodobnie już przetwarzany)', {
|
|
193
|
-
queueName,
|
|
194
|
-
jobId,
|
|
195
|
-
error: error instanceof Error ? error.message : String(error),
|
|
196
|
-
timestamp: new Date().toISOString(),
|
|
197
|
-
});
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
220
|
+
return response.result;
|
|
200
221
|
});
|
|
201
222
|
}
|
|
202
223
|
/**
|
|
203
224
|
* Zamyka wszystkie połączenia
|
|
204
|
-
* Czyści cache kolejek dla optymalizacji pamięci
|
|
205
225
|
*/
|
|
206
226
|
close() {
|
|
207
227
|
return __awaiter(this, void 0, void 0, function* () {
|
|
208
228
|
this.logger.debug('Zamykanie połączeń CommandBus', {
|
|
209
229
|
timestamp: new Date().toISOString(),
|
|
210
230
|
});
|
|
211
|
-
//
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
yield this.
|
|
217
|
-
// Zamknij pozostałe Redis connections
|
|
218
|
-
yield this.queueRedisConnection.quit();
|
|
219
|
-
yield this.workerRedisConnection.quit();
|
|
220
|
-
yield this.rpcRedisConnection.quit();
|
|
231
|
+
// Czekaj na zakończenie rejestracji konsumentów
|
|
232
|
+
if (this.consumePromises.length > 0) {
|
|
233
|
+
yield Promise.allSettled(this.consumePromises);
|
|
234
|
+
this.consumePromises.length = 0;
|
|
235
|
+
}
|
|
236
|
+
yield this.transport.close();
|
|
221
237
|
});
|
|
222
238
|
}
|
|
223
239
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/command-bus/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/command-bus/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAGA,mDAAoD;AAEpD,2CAAoD;AAEpD,sFAAkE;AAClE,8FAAoE;AACpE,uCAA0C;AAC1C,iDAAwD;AACxD,6DAA8D;AAE9D;;;;;;;;;;;GAWG;AACH,MAAqB,UAAU;IA8C7B,YAA2B,MAAwB;QAAxB,WAAM,GAAN,MAAM,CAAkB;QA7CnD;;WAEG;QACK,oBAAe,GAA4D,EAAE,CAAC;QAqCtF;;WAEG;QACc,oBAAe,GAAoB,EAAE,CAAC;QAGrD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAE5B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,wBAAwB,EAAE;YAC1C,MAAM,EAAE;gBACN,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,gBAAgB,EAAE,MAAM,CAAC,gBAAgB;gBACzC,SAAS,EAAE,MAAM,CAAC,SAAS;gBAC3B,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,YAAY,EAAE,MAAM,CAAC,YAAY;gBACjC,WAAW,EAAE,MAAM,CAAC,WAAW;aAChC;YACD,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;SACpC,CAAC,CAAC;QAEH,8EAA8E;QAC9E,IAAI,CAAC,UAAU,GAAG,IAAI,iCAAiB,EAAE,CAAC;QAE1C,6BAA6B;QAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;QAEnD,kEAAkE;QAClE,IAAI,CAAC,cAAc,GAAG,IAAI,yBAAmB,CAAC;YAC5C,IAAI,EAAE,MAAM,CAAC,QAAQ;YACrB,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,CAAC;KAAA;IAED;;OAEG;IACU,KAAK;;YAChB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,+BAA+B,EAAE;gBACjD,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aACpC,CAAC,CAAC;YAEH,gDAAgD;YAChD,IAAI,IAAI,CAAC,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACpC,MAAM,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;gBAC/C,IAAI,CAAC,eAAe,CAAC,MAAM,GAAG,CAAC,CAAC;YAClC,CAAC;YAED,MAAM,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QAC/B,CAAC;KAAA;CACF;AAjRD,6BAiRC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
/**
|
|
18
|
+
* Export publiczny interceptorów
|
|
19
|
+
*/
|
|
20
|
+
__exportStar(require("./interceptor.interface"), exports);
|
|
21
|
+
__exportStar(require("./performance-interceptor"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/command-bus/interceptors/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;GAEG;AACH,0DAAwC;AACxC,4DAA0C"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context przetwarzania komendy - przekazywany do interceptora
|
|
3
|
+
*/
|
|
4
|
+
export interface CommandExecutionContext {
|
|
5
|
+
commandId: string;
|
|
6
|
+
commandName: string;
|
|
7
|
+
streamName: string;
|
|
8
|
+
messageId: string;
|
|
9
|
+
timestamp: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Wynik wykonania handlera - zwracany przez interceptor
|
|
13
|
+
*/
|
|
14
|
+
export interface CommandExecutionResult<T = unknown> {
|
|
15
|
+
result: T;
|
|
16
|
+
duration: number;
|
|
17
|
+
success: boolean;
|
|
18
|
+
error?: Error;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Interface interceptora komend
|
|
22
|
+
*
|
|
23
|
+
* Interceptor umożliwia dodanie logiki przed/po wykonaniu handlera
|
|
24
|
+
* bez naruszania SRP MessageProcessor.
|
|
25
|
+
*/
|
|
26
|
+
export interface ICommandInterceptor {
|
|
27
|
+
/**
|
|
28
|
+
* Wykonuje handler z logiką interceptora
|
|
29
|
+
*
|
|
30
|
+
* @param context - Kontekst wykonania komendy
|
|
31
|
+
* @param handler - Handler do wykonania
|
|
32
|
+
* @param commandData - Dane komendy
|
|
33
|
+
*/
|
|
34
|
+
intercept<T>(context: CommandExecutionContext, handler: (data: Buffer) => Promise<T>, commandData: Buffer): Promise<CommandExecutionResult<T>>;
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interceptor.interface.js","sourceRoot":"","sources":["../../../src/command-bus/interceptors/interceptor.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ILogger } from '../../shared/types';
|
|
2
|
+
import type { ICommandInterceptor, CommandExecutionContext, CommandExecutionResult } from './interceptor.interface';
|
|
3
|
+
/**
|
|
4
|
+
* Interceptor mierzący czas wykonania handlera
|
|
5
|
+
*
|
|
6
|
+
* Odpowiedzialności (SRP):
|
|
7
|
+
* - Pomiar czasu wykonania handlera
|
|
8
|
+
* - Logowanie metryk wydajności
|
|
9
|
+
* - Obsługa błędów z zachowaniem kontekstu
|
|
10
|
+
*
|
|
11
|
+
* NIE odpowiada za:
|
|
12
|
+
* - Przetwarzanie komendy (MessageProcessor)
|
|
13
|
+
* - XACK/XTRIM (MessageProcessor)
|
|
14
|
+
* - Deduplicję (StreamConsumer)
|
|
15
|
+
*/
|
|
16
|
+
export declare class PerformanceInterceptor implements ICommandInterceptor {
|
|
17
|
+
private readonly logger;
|
|
18
|
+
private readonly slowThreshold;
|
|
19
|
+
constructor(logger: ILogger, slowThreshold?: number);
|
|
20
|
+
/**
|
|
21
|
+
* Wykonuje handler z pomiarem czasu
|
|
22
|
+
*/
|
|
23
|
+
intercept<T>(context: CommandExecutionContext, handler: (data: Buffer) => Promise<T>, commandData: Buffer): Promise<CommandExecutionResult<T>>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PerformanceInterceptor = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Interceptor mierzący czas wykonania handlera
|
|
15
|
+
*
|
|
16
|
+
* Odpowiedzialności (SRP):
|
|
17
|
+
* - Pomiar czasu wykonania handlera
|
|
18
|
+
* - Logowanie metryk wydajności
|
|
19
|
+
* - Obsługa błędów z zachowaniem kontekstu
|
|
20
|
+
*
|
|
21
|
+
* NIE odpowiada za:
|
|
22
|
+
* - Przetwarzanie komendy (MessageProcessor)
|
|
23
|
+
* - XACK/XTRIM (MessageProcessor)
|
|
24
|
+
* - Deduplicję (StreamConsumer)
|
|
25
|
+
*/
|
|
26
|
+
class PerformanceInterceptor {
|
|
27
|
+
constructor(logger, slowThreshold = 1000) {
|
|
28
|
+
this.logger = logger;
|
|
29
|
+
this.slowThreshold = slowThreshold;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Wykonuje handler z pomiarem czasu
|
|
33
|
+
*/
|
|
34
|
+
intercept(context, handler, commandData) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const startTime = performance.now();
|
|
37
|
+
try {
|
|
38
|
+
const result = yield handler(commandData);
|
|
39
|
+
const duration = Math.round(performance.now() - startTime);
|
|
40
|
+
// KOMPROMIS: Log tylko jeśli przekroczono próg (unikanie log spam)
|
|
41
|
+
if (duration >= this.slowThreshold) {
|
|
42
|
+
this.logger.warn('Wolne wykonanie handlera', {
|
|
43
|
+
commandId: context.commandId,
|
|
44
|
+
commandName: context.commandName,
|
|
45
|
+
duration,
|
|
46
|
+
threshold: this.slowThreshold,
|
|
47
|
+
timestamp: new Date().toISOString(),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
// Zawsze loguj na debug (dla profiling)
|
|
51
|
+
this.logger.debug('Handler wykonany', {
|
|
52
|
+
commandId: context.commandId,
|
|
53
|
+
commandName: context.commandName,
|
|
54
|
+
streamName: context.streamName,
|
|
55
|
+
messageId: context.messageId,
|
|
56
|
+
duration,
|
|
57
|
+
timestamp: new Date().toISOString(),
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
result,
|
|
61
|
+
duration,
|
|
62
|
+
success: true,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
const duration = Math.round(performance.now() - startTime);
|
|
67
|
+
// Błędy loguj na debug (ERROR już jest w StreamConsumer)
|
|
68
|
+
this.logger.debug('Handler zakończony błędem', {
|
|
69
|
+
commandId: context.commandId,
|
|
70
|
+
commandName: context.commandName,
|
|
71
|
+
duration,
|
|
72
|
+
error: error instanceof Error ? error.message : String(error),
|
|
73
|
+
timestamp: new Date().toISOString(),
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
result: undefined,
|
|
77
|
+
duration,
|
|
78
|
+
success: false,
|
|
79
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.PerformanceInterceptor = PerformanceInterceptor;
|
|
86
|
+
//# sourceMappingURL=performance-interceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"performance-interceptor.js","sourceRoot":"","sources":["../../../src/command-bus/interceptors/performance-interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA;;;;;;;;;;;;GAYG;AACH,MAAa,sBAAsB;IACjC,YACmB,MAAe,EACf,gBAAwB,IAAI;QAD5B,WAAM,GAAN,MAAM,CAAS;QACf,kBAAa,GAAb,aAAa,CAAe;IAC5C,CAAC;IAEJ;;OAEG;IACG,SAAS,CACb,OAAgC,EAChC,OAAqC,EACrC,WAAmB;;YAEnB,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;YAEpC,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,MAA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