@mojaloop/event-sdk
Version:
Shared code for Event Logging
96 lines (90 loc) • 4.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventLoggingServiceServer = exports.EVENT_RECEIVED = void 0;
/*****
License
--------------
Copyright © 2020-2025 Mojaloop Foundation
The Mojaloop files are made available by the Mojaloop Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributors
--------------
This is the official list of the Mojaloop project contributors for this file.
Names of the original copyright holders (individuals or organizations)
should be listed with a '*' in the first column. People who have
contributed from an organization can be listed under the organization
that actually holds the copyright for their contributions (see the
Mojaloop Foundation for an example). Those individuals should have
their names indented and be marked with a '-'. Email address can be added
optionally within square brackets <email>.
* Mojaloop Foundation
- Name Surname <name.surname@mojaloop.io>
- Ramiro González Maciel <ramiro@modusbox.com>
--------------
******/
const EventMessage_1 = require("../model/EventMessage");
const MessageMapper_1 = require("./MessageMapper");
const EventLoggerServiceLoader_1 = require("./EventLoggerServiceLoader");
const events_1 = __importDefault(require("events"));
const grpc = require('@grpc/grpc-js');
const Logger = require('@mojaloop/central-services-logger');
const EVENT_RECEIVED = 'eventReceived';
exports.EVENT_RECEIVED = EVENT_RECEIVED;
class EventLoggingServiceServer extends events_1.default.EventEmitter {
server;
host;
port;
constructor(host, port) {
super();
const eventLoggerService = (0, EventLoggerServiceLoader_1.loadEventLoggerService)();
const server = new grpc.Server();
server.addService(eventLoggerService.service, {
log: this.logEventReceivedHandler.bind(this)
});
this.server = server;
this.host = host;
this.port = port;
}
start() {
this.server.bindAsync(`${this.host}:${this.port}`, grpc.ServerCredentials.createInsecure(), (err, port) => {
if (err) {
throw err;
}
this.server.start();
if (Logger.isInfoEnabled) {
Logger.info(`Server listening on ${this.host}:${port}...`);
}
});
}
logEventReceivedHandler(call, callback) {
const event = call.request;
// We're on plain JavaScript, so although this *should* be a EventMessage since gRPC is typed, let's be sure
if (!event.id) {
return callback(new Error(`Couldn't parse message parameter. It doesn't have an id property. parameter: ${event}`));
}
if (Logger.isDebugEnabled) {
Logger.debug(`Server.logEventReceivedHandler event: ${JSON.stringify(event, null, 2)}`);
}
let response;
try {
// Convert it to a EventMessage
const eventMessage = Object.assign({}, event);
// Convert the event.content which is a Struct to a plain object
if (eventMessage.content) {
eventMessage.content = (0, MessageMapper_1.fromAny)(eventMessage.content);
}
this.emit(EVENT_RECEIVED, eventMessage);
response = new EventMessage_1.LogResponse(EventMessage_1.LogResponseStatus.accepted);
}
catch (error) {
response = new EventMessage_1.LogResponse(EventMessage_1.LogResponseStatus.error);
}
callback(null, response);
}
}
exports.EventLoggingServiceServer = EventLoggingServiceServer;
//# sourceMappingURL=EventLoggingServiceServer.js.map