@platformatic/kafka
Version:
Modern and performant client for Apache Kafka
43 lines (42 loc) • 1.78 kB
JavaScript
import { createPromisifiedCallback, kCallbackPromise } from "../../apis/index.js";
import { AuthenticationError } from "../../errors.js";
export function getCredential(label, credentialOrProvider, callback) {
if (!callback) {
callback = createPromisifiedCallback();
}
if (typeof credentialOrProvider === 'undefined') {
callback(new AuthenticationError(`The ${label} should be a value or a function.`), undefined);
return callback[kCallbackPromise];
}
else if (typeof credentialOrProvider !== 'function') {
callback(null, credentialOrProvider);
return callback[kCallbackPromise];
}
try {
const credential = credentialOrProvider();
if (credential == null) {
callback(new AuthenticationError(`The ${label} provider should return a string or a promise that resolves to a value.`), undefined);
return callback[kCallbackPromise];
}
else if (typeof credential?.then !== 'function') {
callback(null, credential);
return callback[kCallbackPromise];
}
;
credential
.then((result) => {
if (result == null) {
process.nextTick(callback, new AuthenticationError(`The ${label} provider should resolve to a value.`), undefined);
return;
}
process.nextTick(callback, null, result);
})
.catch((error) => {
process.nextTick(callback, new AuthenticationError(`The ${label} provider threw an error.`, { cause: error }));
});
}
catch (error) {
callback(new AuthenticationError(`The ${label} provider threw an error.`, { cause: error }), undefined);
}
return callback[kCallbackPromise];
}