UNPKG

eventstore-interconnect

Version:
75 lines 4.41 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpReaderService = void 0; const common_1 = require("@nestjs/common"); const constants_1 = require("../constants"); const constants_2 = require("../../../constants"); const event_handler_1 = require("../../../event-handler"); const connections_initializers_1 = require("../../../connections-initializers"); const logger_1 = require("../../../logger"); let HttpReaderService = class HttpReaderService { constructor(configuration, subscriptions, eventHandler, httpClientProvider, esClientInitializer, logger) { this.configuration = configuration; this.subscriptions = subscriptions; this.eventHandler = eventHandler; this.httpClientProvider = httpClientProvider; this.esClientInitializer = esClientInitializer; this.logger = logger; } async onModuleInit() { await this.httpClientProvider.init(); await this.esClientInitializer.init(); await this.upsertPersistantSubscriptions(); this.logger.log(`READER : connected to ${this.configuration.tcp.host}:${this.configuration.tcp.port}`); } async upsertPersistantSubscriptions() { const tcpClient = this.esClientInitializer.getConnectedClient(); const httpClient = this.httpClientProvider.getHttpClient(); for (const subscription of this.subscriptions) { const options = { ...subscription.options, ...{ resolveLinkTos: subscription.options.resolveLinkTos || subscription.options.resolveLinktos, }, }; await httpClient.persistentSubscriptions.assert(subscription.group, subscription.stream, options); await this.connectToPersistentSubscription(tcpClient, subscription); } } async connectToPersistentSubscription(esClient, subscription) { this.logger.log(`Connecting to persistent subscription "${subscription.group}" on stream ${subscription.stream}...`); await esClient.connectToPersistentSubscription(subscription.stream, subscription.group, async (subscription, event) => { await this.eventHandler.handle(event).catch((e) => { subscription.fail(event, 3, 'An error occurred'); this.logger.error(`Unexpected error while handling an event (${JSON.stringify(event)})... Details : ${e.message}`); }); subscription.acknowledge(event); }, subscription.onSubscriptionDropped, this.configuration.credentials, subscription.bufferSize, subscription.autoAck); this.logger.log(`Connected to "${subscription.group}" on stream ${subscription.stream}.`); } }; HttpReaderService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(constants_2.CONNECTION_CONFIGURATION)), __param(1, (0, common_1.Inject)(constants_1.SUBSCRIPTIONS)), __param(2, (0, common_1.Inject)(event_handler_1.EVENT_HANDLER)), __param(3, (0, common_1.Inject)(connections_initializers_1.HTTP_CLIENT_CONNECTION_INITIALIZER)), __param(4, (0, common_1.Inject)(connections_initializers_1.TCP_EVENTSTORE_CLIENT_CONNECTION_INITIALIZER)), __param(5, (0, common_1.Inject)(logger_1.LOGGER)), __metadata("design:paramtypes", [Object, Array, Object, Object, Object, common_1.Logger]) ], HttpReaderService); exports.HttpReaderService = HttpReaderService; //# sourceMappingURL=http-reader.service.js.map