UNPKG

@cerbos/grpc

Version:

Client library for interacting with the Cerbos policy decision point service over gRPC from server-side Node.js applications

101 lines 3.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GRPC = exports.Compression = void 0; const grpc_js_1 = require("@grpc/grpc-js"); const core_1 = require("@cerbos/core"); const transport_1 = require("./transport"); const { version } = require("../package.json"); const defaultUserAgent = `cerbos-sdk-javascript-grpc/${version}`; /** * Compression algorithm to apply to messages exchanged between the client and policy decision point server. * * @public */ var Compression; (function (Compression) { /** * Do not compress messages. */ Compression["NONE"] = "identity"; /** * Compress messages with gzip. */ Compression["GZIP"] = "gzip"; })(Compression || (exports.Compression = Compression = {})); /** * A client for interacting with the Cerbos policy decision point server over gRPC. * * @remarks * Not supported in browsers. * * See {@link @cerbos/core#Client | the parent class} for available methods. * * @public */ class GRPC extends core_1.Client { client; /** * Create a client for interacting with the Cerbos policy decision point (PDP) server over gRPC. * * @param target - Cerbos PDP server address (`"host"`, `"host:port"`, or `"unix:/path/to/socket"`). * @param options - additional client settings. * * @example * Connect via TCP with no encryption: * * ```typescript * const cerbos = new GRPC("localhost:3593", { tls: false }); * ``` * * @example * Connect via a Unix socket with no encryption: * * ```typescript * const cerbos = new GRPC("unix:/var/run/cerbos.grpc.sock", { tls: false }); * ``` * * @example * Connect to the hosted demo PDP to experiment {@link https://play.cerbos.dev | in the playground}: * * ```typescript * const cerbos = new GRPC("demo-pdp.cerbos.cloud", { tls: true, playgroundInstance: "gE623b0180QlsG5a4QIN6UOZ6f3iSFW2" }); * ``` */ constructor(target, options) { const client = new grpc_js_1.Client(target, channelCredentials(options), channelOptions(options)); super(new transport_1.Transport(client), options); this.client = client; } /** * Disconnect from the Cerbos policy decision point server and clean up resources. * * @remarks * It is safe to call `close` more than once. * * Any interactions with the server after calling `close` will throw an error. */ close() { this.client.close(); } } exports.GRPC = GRPC; function channelCredentials({ playgroundInstance, tls, }) { if (!tls) { if (playgroundInstance) { throw new Error("TLS is required when connecting to a playground instance"); } return grpc_js_1.ChannelCredentials.createInsecure(); } if (tls === true) { return grpc_js_1.ChannelCredentials.createSsl(); } return grpc_js_1.ChannelCredentials.createFromSecureContext(tls); } function channelOptions({ compression = Compression.NONE, userAgent, channelOptions: input = {}, }) { const output = Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined)); output["grpc.default_compression_algorithm"] = grpc_js_1.compressionAlgorithms[compression]; output["grpc.primary_user_agent"] = `${userAgent ? `${userAgent} ` : ""}${defaultUserAgent}`; return output; } //# sourceMappingURL=client.js.map