UNPKG

@authzed/authzed-js-node

Version:
152 lines 6.26 kB
import * as grpc from "@grpc/grpc-js"; // NOTE: Copied from channel-credentials.ts in gRPC Node package because its not exported: // https://github.com/grpc/grpc-node/blob/3106057f5ad8f79a71d2ae411e116ad308a2e835/packages/grpc-js/src/call-credentials.ts#L143 class ComposedChannelCredentials extends grpc.ChannelCredentials { channelCredentials; callCreds; constructor(channelCredentials, callCreds) { super(); this.channelCredentials = channelCredentials; this.callCreds = callCreds; // NOTE: leaving this here to show what changed from the upstream. /* if (!channelCredentials._isSecure()) { throw new Error('Cannot compose insecure credentials'); } */ } compose(callCredentials) { const combinedCallCredentials = this.callCreds.compose(callCredentials); return new ComposedChannelCredentials(this.channelCredentials, combinedCallCredentials); } _isSecure() { return false; } // NOTE: this is copied from the InsecureChannelCredentialsImpl class _createSecureConnector(channelTarget, options, callCredentials) { return { connect: async (socket) => { return { socket, secure: false }; }, waitForReady: async () => { }, getCallCredentials: () => callCredentials || this.callCreds, destroy: () => { }, }; } _equals(other) { if (this === other) { return true; } if (other instanceof ComposedChannelCredentials) { return (this.channelCredentials._equals(other.channelCredentials) && this.callCreds._equals(other.callCreds)); } else { return false; } } } // Create our own known insecure channel creds. // See https://github.com/grpc/grpc-node/issues/543 for why this is necessary. class KnownInsecureChannelCredentialsImpl extends grpc.ChannelCredentials { compose(callCredentials) { return new ComposedChannelCredentials(this, callCredentials); } _isSecure() { return false; } _equals(other) { return other instanceof KnownInsecureChannelCredentialsImpl; } _createSecureConnector(channelTarget, options, callCredentials) { return { connect: async (socket) => { return { socket, secure: false }; }, waitForReady: async () => { }, getCallCredentials: () => callCredentials, destroy: () => { }, }; } } export var ClientSecurity; (function (ClientSecurity) { ClientSecurity[ClientSecurity["SECURE"] = 0] = "SECURE"; ClientSecurity[ClientSecurity["INSECURE_LOCALHOST_ALLOWED"] = 1] = "INSECURE_LOCALHOST_ALLOWED"; ClientSecurity[ClientSecurity["INSECURE_PLAINTEXT_CREDENTIALS"] = 2] = "INSECURE_PLAINTEXT_CREDENTIALS"; })(ClientSecurity || (ClientSecurity = {})); export var PreconnectServices; (function (PreconnectServices) { PreconnectServices[PreconnectServices["NONE"] = 0] = "NONE"; PreconnectServices[PreconnectServices["PERMISSIONS_SERVICE"] = 1] = "PERMISSIONS_SERVICE"; PreconnectServices[PreconnectServices["SCHEMA_SERVICE"] = 2] = "SCHEMA_SERVICE"; PreconnectServices[PreconnectServices["WATCH_SERVICE"] = 4] = "WATCH_SERVICE"; PreconnectServices[PreconnectServices["EXPERIMENTAL_SERVICE"] = 8] = "EXPERIMENTAL_SERVICE"; PreconnectServices[PreconnectServices["WATCH_PERMISSIONS_SERVICE"] = 9] = "WATCH_PERMISSIONS_SERVICE"; PreconnectServices[PreconnectServices["WATCH_PERMISSIONSETS_SERVICE"] = 10] = "WATCH_PERMISSIONSETS_SERVICE"; })(PreconnectServices || (PreconnectServices = {})); function createClientCreds(endpoint, token, security = ClientSecurity.SECURE) { const metadata = new grpc.Metadata(); metadata.set("authorization", "Bearer " + token); const creds = []; if (security === ClientSecurity.SECURE || security === ClientSecurity.INSECURE_PLAINTEXT_CREDENTIALS || (security === ClientSecurity.INSECURE_LOCALHOST_ALLOWED && endpoint.startsWith("localhost:"))) { creds.push(grpc.credentials.createFromMetadataGenerator((_, callback) => { callback(null, metadata); })); } return grpc.credentials.combineChannelCredentials(security === ClientSecurity.SECURE ? grpc.credentials.createSsl() : new KnownInsecureChannelCredentialsImpl(), ...creds); } function createClientCredsWithCustomCert(token, certificate) { const metadata = new grpc.Metadata(); metadata.set("authorization", "Bearer " + token); const creds = []; creds.push(grpc.credentials.createFromMetadataGenerator((_, callback) => { callback(null, metadata); })); return grpc.credentials.combineChannelCredentials(grpc.credentials.createSsl(certificate), ...creds); } function promisifyStream(fn, bind) { return (req) => { return new Promise((resolve, reject) => { const results = []; const stream = fn.bind(bind)(req); stream.on("data", function (response) { results.push(response); }); stream.on("error", function (e) { return reject(e); }); stream.on("end", function () { return resolve(results); }); stream.on("status", function (status) { if (status.code !== grpc.status.OK) { return reject(status); } }); }); }; } // Based on: https://github.com/grpc/grpc-node/issues/541#issuecomment-631191356 export function deadlineInterceptor(timeoutInMS) { return (options, nextCall) => { if (!options.deadline) { options.deadline = Date.now() + (timeoutInMS ? timeoutInMS : 60000); } return new grpc.InterceptingCall(nextCall(options)); }; } const authzedEndpoint = "grpc.authzed.com:443"; export { createClientCreds, createClientCredsWithCustomCert, authzedEndpoint, promisifyStream, }; export default { createClientCreds, createClientCredsWithCustomCert, authzedEndpoint, promisifyStream, }; //# sourceMappingURL=util.js.map