All files / dist/common grpc-package.js

95.45% Statements 42/44
92.85% Branches 26/28
100% Functions 9/9
95.34% Lines 41/43

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    6x 6x 6x 6x 6x     100x 100x                       100x 100x 100x 100x     1x     91x     184x     120x 119x     119x 1x   119x           108x     108x 108x 108x 1x   107x 107x 107x 1x   106x 105x     2x 2x 1x   1x     2x 2x 1x   1x 1x     6x 6x  
"use strict";
/*! Copyright (c) 2023 Siemens AG. Licensed under the MIT License. */
Object.defineProperty(exports, "__esModule", { value: true });
exports.GrpcPackage = void 0;
const grpc_js_1 = require("@grpc/grpc-js");
const proto_loader_1 = require("@grpc/proto-loader");
const protobufjs_1 = require("protobufjs");
class GrpcPackage {
    constructor(protoFile, options) {
        this._options = options || {};
        this._grpcProtobufOptions = {
            keepCase: false,
            longs: undefined,
            enums: Number,
            bytes: undefined,
            defaults: true,
            arrays: true,
            objects: false,
            oneofs: false,
            json: false,
            includeDirs: [],
        };
        this._root = new protobufjs_1.Root(this._grpcProtobufOptions);
        this._root.loadSync(protoFile, this._grpcProtobufOptions);
        this._root.resolveAll();
        this._grpcRoot = (0, grpc_js_1.loadPackageDefinition)((0, proto_loader_1.fromJSON)(this._root.toJSON({ keepComments: false }), this._grpcProtobufOptions));
    }
    get options() {
        return this._options;
    }
    get grpcRoot() {
        return this._grpcRoot;
    }
    getServiceDefinition(serviceName) {
        return serviceName.split(".").reduce((o, p) => o[p], this._grpcRoot)["service"];
    }
    pack(object, typeName, typeUrlPrefix) {
        const objectType = this._root.lookupType(typeName);
        Iif (!objectType) {
            throw new TypeError("Cannot pack as given type name is not an existing Protobuf definition");
        }
        if (typeUrlPrefix && !typeUrlPrefix.endsWith("/")) {
            typeUrlPrefix += "/";
        }
        return {
            type_url: (typeUrlPrefix || GrpcPackage.GOOGLEAPI_TYPE_URL_PREFIX) + typeName,
            value: objectType.encode(objectType.fromObject(object)).finish(),
        };
    }
    unpack(anyObject, typeName) {
        Iif (anyObject === null) {
            throw new TypeError("Cannot unpack as null is not compatible with Any type");
        }
        const anyTypeUrl = anyObject["type_url"];
        const anyValue = anyObject["value"];
        if (!anyTypeUrl || !(anyValue instanceof Uint8Array)) {
            throw new TypeError("Cannot unpack as given object is not compatible with Any type");
        }
        const sep = anyTypeUrl.lastIndexOf("/") + 1;
        const name = anyTypeUrl.substring(sep);
        if (typeName !== undefined && typeName !== name) {
            throw new TypeError(`Packed Any type_url ${anyTypeUrl} doesn't match type ${typeName}`);
        }
        const objectType = this._root.lookupType(name);
        return objectType.toObject(objectType.decode(anyValue), this._grpcProtobufOptions);
    }
    getAnyTypeUrl(anyObject) {
        const anyTypeUrl = anyObject["type_url"];
        if (!anyTypeUrl || !(anyObject["value"] instanceof Uint8Array)) {
            throw new TypeError("Cannot getAnyTypeName as given object is not compatible with Any type");
        }
        return anyTypeUrl;
    }
    getAnyTypeName(anyObject) {
        const anyTypeUrl = anyObject["type_url"];
        if (!anyTypeUrl || !(anyObject["value"] instanceof Uint8Array)) {
            throw new TypeError("Cannot getAnyTypeName as given object is not compatible with Any type");
        }
        const sep = anyTypeUrl.lastIndexOf("/") + 1;
        return anyTypeUrl.substring(sep);
    }
}
exports.GrpcPackage = GrpcPackage;
GrpcPackage.GOOGLEAPI_TYPE_URL_PREFIX = "type.googleapis.com/";