All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.dist.src.internal.grpc.headers-interceptor.js Maven / Gradle / Ivy

The newest version!
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HeaderInterceptor = exports.Header = void 0;
const grpc_js_1 = require("@grpc/grpc-js");
class Header {
    /**
     * @param {string} name
     * @param {string} value
     */
    constructor(name, value) {
        this.onceOnlyHeaders = ['agent', 'runtime-version'];
        this.name = name;
        this.value = value;
    }
}
exports.Header = Header;
class HeaderInterceptor {
    static createHeadersInterceptor(headers) {
        const headersToAddOnce = headers.filter(header => header.onceOnlyHeaders.includes(header.name));
        const headersToAddEveryTime = headers.filter(header => !header.onceOnlyHeaders.includes(header.name));
        let areOnlyOnceHeadersSent = false;
        return (options, nextCall) => {
            return new grpc_js_1.InterceptingCall(nextCall(options), {
                start: (metadata, listener, next) => {
                    headersToAddEveryTime.forEach(h => {
                        metadata.set(h.name, h.value);
                    });
                    if (!areOnlyOnceHeadersSent) {
                        areOnlyOnceHeadersSent = true;
                        headersToAddOnce.forEach(h => metadata.add(h.name, h.value));
                    }
                    next(metadata, {});
                },
            });
        };
    }
}
exports.HeaderInterceptor = HeaderInterceptor;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaGVhZGVycy1pbnRlcmNlcHRvci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9pbnRlcm5hbC9ncnBjL2hlYWRlcnMtaW50ZXJjZXB0b3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEsMkNBQTREO0FBRTVELE1BQWEsTUFBTTtJQUtqQjs7O09BR0c7SUFDSCxZQUFZLElBQVksRUFBRSxLQUFhO1FBUnZCLG9CQUFlLEdBQWEsQ0FBQyxPQUFPLEVBQUUsaUJBQWlCLENBQUMsQ0FBQztRQVN2RSxJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQ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