All files / dist/service tn-communication-service.js

87.14% Statements 61/70
66% Branches 33/50
100% Functions 19/19
86.76% Lines 59/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111    5x 5x 5x 5x     8x 8x 8x     8x 8x 18x 14x 18x 14x 18x 15x         8x 8x 8x 8x 8x   8x 8x   8x     8x     8x     8x     8x 4x   8x 2x   8x 2x   8x 8x     18x 1x 1x 1x 1x   17x 17x 15x     2x 2x       16x     18x     14x 15x 15x 15x       18x 18x     18x             18x   18x     15x 15x 15x     5x  
"use strict";
/*! Copyright (c) 2023 Siemens AG. Licensed under the MIT License. */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TnCommunicationService = void 0;
const grpc_js_1 = require("@grpc/grpc-js");
const tn_base_service_1 = require("./tn-base-service");
class TnCommunicationService extends tn_base_service_1.TnBaseService {
    constructor(getTnController, getTnConnectorOptions, isCoatyAgentOnline, debug, _updateConfiguration) {
        super(getTnController, getTnConnectorOptions, isCoatyAgentOnline, debug, "TnCommunicationService");
        this._updateConfiguration = _updateConfiguration;
        this._observedResponseCallbacks = new Map();
    }
    get handlers() {
        return {
            configure: (call, callback) => this._configureCoatyCommunication(call, callback),
            publishChannel: (call, callback) => this._publishChannel(call, callback),
            observeChannel: (call) => this._observeChannel(call),
            publishCall: (call) => this._publishCall(call),
            observeCall: (call) => this._observeCall(call),
            publishReturn: (call, callback) => this._publishReturn(call, callback),
            publishComplete: (call, callback) => this._publishComplete(call, callback),
        };
    }
    async _configureCoatyCommunication(call, callback) {
        var _a, _b, _c, _d, _e, _f;
        const serviceOpts = call.request;
        const comOpts = {};
        this.debug("configure CoatyCommunicationOptions: %o", serviceOpts);
        Eif (serviceOpts.brokerUrl) {
            comOpts.coatyBrokerUrl = serviceOpts.brokerUrl;
        }
        Eif (serviceOpts.namespace) {
            comOpts.coatyNamespace = serviceOpts.namespace;
        }
        Iif ((_a = serviceOpts.userAuth) === null || _a === void 0 ? void 0 : _a.username) {
            comOpts.coatyUsername = serviceOpts.userAuth.username;
        }
        Iif ((_b = serviceOpts.userAuth) === null || _b === void 0 ? void 0 : _b.password) {
            comOpts.coatyPassword = serviceOpts.userAuth.password;
        }
        Iif ((_c = serviceOpts.tlsAuth) === null || _c === void 0 ? void 0 : _c.cert) {
            comOpts.coatyTlsCert = serviceOpts.tlsAuth.cert;
        }
        Iif ((_d = serviceOpts.tlsAuth) === null || _d === void 0 ? void 0 : _d.key) {
            comOpts.coatyTlsKey = serviceOpts.tlsAuth.key;
        }
        if ("notFailFastIfOffline" in serviceOpts) {
            comOpts.coatyFailFastIfOffline = serviceOpts.notFailFastIfOffline ? "false" : "true";
        }
        if ((_e = serviceOpts.agentIdentity) === null || _e === void 0 ? void 0 : _e.name) {
            comOpts.coatyAgentIdentityName = serviceOpts.agentIdentity.name;
        }
        if ((_f = serviceOpts.agentIdentity) === null || _f === void 0 ? void 0 : _f.id) {
            comOpts.coatyAgentIdentityId = serviceOpts.agentIdentity.id;
        }
        await this._updateConfiguration(comOpts);
        callback(null, {});
    }
    _publishChannel(call, callback) {
        if (this.failFastIfOffline) {
            const errorMsg = "Coaty agent is offline";
            this.debug("PublishChannel failed: %s", errorMsg);
            callback({ code: grpc_js_1.status.UNAVAILABLE, details: errorMsg });
            return;
        }
        try {
            this.getTnController().publishChannel(call.request);
            callback(null, {});
        }
        catch (error) {
            this.debug("PublishChannel failed: %s", error.message);
            callback({ code: grpc_js_1.status.INVALID_ARGUMENT, details: error.message });
        }
    }
    _observeChannel(call) {
        this.handleServerStreamingCall(call, "observeChannel", evt => call.write(evt));
    }
    _publishCall(call) {
        this.handleServerStreamingCall(call, "publishCall", evt => call.write(evt));
    }
    _observeCall(call) {
        this.handleServerStreamingCall(call, "observeCall", evt => {
            this._observedResponseCallbacks.set(evt.correlationId, evt.responseCallback);
            delete evt.responseCallback;
            call.write(evt);
        });
    }
    _publishReturn(call, callback) {
        const responseCallback = this._observedResponseCallbacks.get(call.request.correlationId);
        Iif (responseCallback === undefined) {
            this.debug("PublishReturn event discarded as it is not correlated with a pending request: %o", call.request);
        }
        else Iif (this.failFastIfOffline) {
            const errorMsg = "Coaty agent is offline";
            this.debug("PublishReturn failed: %s", errorMsg);
            callback({ code: grpc_js_1.status.UNAVAILABLE, details: errorMsg });
            return;
        }
        else {
            responseCallback(call.request);
        }
        callback(null, {});
    }
    _publishComplete(call, callback) {
        this.debug("PublishComplete cleaning up correlated response callback: %o", call.request);
        this._observedResponseCallbacks.delete(call.request.correlationId);
        callback(null, {});
    }
}
exports.TnCommunicationService = TnCommunicationService;