@cerbos/grpc
Version:
Client library for interacting with the Cerbos policy decision point service over gRPC from server-side Node.js applications
94 lines • 3.35 kB
JavaScript
import { ChannelCredentials, Client as GenericClient, compressionAlgorithms, } from "@grpc/grpc-js";
import { Client } from "@cerbos/core";
import { userAgent } from "@cerbos/core/~internal";
import pkg from "../package.json" with { type: "json" };
import { Transport } from "./transport.js";
const defaultUserAgent = `cerbos-sdk-javascript-grpc/${pkg.version}`;
/**
* Compression algorithm to apply to messages exchanged between the client and policy decision point server.
*/
export var Compression;
(function (Compression) {
/**
* Do not compress messages.
*/
Compression["NONE"] = "identity";
/**
* Compress messages with gzip.
*/
Compression["GZIP"] = "gzip";
})(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.
*/
export class GRPC extends 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 GenericClient(target, channelCredentials(options), channelOptions(options));
super(new 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();
}
}
function channelCredentials({ playgroundInstance, tls, }) {
if (!tls) {
if (playgroundInstance) {
throw new Error("TLS is required when connecting to a playground instance");
}
return ChannelCredentials.createInsecure();
}
if (tls === true) {
return ChannelCredentials.createSsl();
}
return ChannelCredentials.createFromSecureContext(tls);
}
function channelOptions({ compression = Compression.NONE, userAgent: customUserAgent, channelOptions: input = {}, }) {
const output = Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined));
output["grpc.default_compression_algorithm"] =
compressionAlgorithms[compression];
output["grpc.primary_user_agent"] = userAgent(customUserAgent, defaultUserAgent);
return output;
}
//# sourceMappingURL=client.js.map