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

89.47% Statements 34/38
70% Branches 7/10
88.88% Functions 8/9
89.47% Lines 34/38

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    5x 5x 5x 5x     6x 6x     5x     32x 32x 32x 32x     206x     36x     83x 83x 5x 5x 5x 5x   78x 78x   106x   63x 63x                   15x 15x 15x 15x   63x 56x       56x   56x   63x         5x  
"use strict";
/*! Copyright (c) 2023 Siemens AG. Licensed under the MIT License. */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TnBaseService = exports.UnavailableError = void 0;
const grpc_js_1 = require("@grpc/grpc-js");
const rxjs_1 = require("rxjs");
class UnavailableError extends Error {
    constructor(message) {
        super(message);
        this.name = "UnavailableError";
    }
}
exports.UnavailableError = UnavailableError;
class TnBaseService {
    constructor(getTnController, getTnConnectorOptions, isCoatyAgentOnline, debug, debugName) {
        this.getTnController = getTnController;
        this.getTnConnectorOptions = getTnConnectorOptions;
        this.isCoatyAgentOnline = isCoatyAgentOnline;
        this.debug = debug.extend(debugName);
    }
    get failFastIfOffline() {
        return !this.isCoatyAgentOnline() && this.getTnConnectorOptions().coatyFailFastIfOffline === "true";
    }
    async onStopping() {
        await this.getTnController().onServiceStopping();
    }
    handleServerStreamingCall(call, method, nextHandler) {
        const onCancelled$ = new rxjs_1.Subject();
        if (this.failFastIfOffline) {
            const errorMsg = "Coaty agent is offline";
            this.debug("%s failed: %s", method, errorMsg);
            call.emit("error", { code: grpc_js_1.status.UNAVAILABLE, details: errorMsg });
            return;
        }
        try {
            this.getTnController()[method](call.request, onCancelled$)
                .subscribe({
                next: evt => nextHandler(evt),
                complete: () => {
                    this.debug("%s ending rpc as observable completes", method);
                    call.end();
                },
                error: err => {
                    const statusCode = err instanceof UnavailableError ? grpc_js_1.status.UNAVAILABLE : grpc_js_1.status.INVALID_ARGUMENT;
                    this.debug("%s error emitted on observable: %s", method, err);
                    call.emit("error", { code: statusCode, details: `${err}` });
                },
            });
        }
        catch (error) {
            const statusCode = error instanceof UnavailableError ? grpc_js_1.status.UNAVAILABLE : grpc_js_1.status.INVALID_ARGUMENT;
            this.debug("%s failed: %s", method, error.message);
            call.emit("error", { code: statusCode, details: error.message });
            return;
        }
        const onEndOrCancelledOrError = (...args) => {
            Iif (args[0] instanceof Error) {
                this.debug("Error on %s rpc for call event %o: %s", method, call.request, args[0].message);
            }
            else {
                this.debug("%s rpc cancelled or ended for call event %o", method, call.request);
            }
            onCancelled$.next();
        };
        call.once("end", onEndOrCancelledOrError)
            .once("cancelled", onEndOrCancelledOrError)
            .once("error", onEndOrCancelledOrError);
    }
}
exports.TnBaseService = TnBaseService;