MCPcopy Index your code
hub / github.com/di-sukharev/opencommit / createTokenCycler

Function createTokenCycler

out/cli.cjs:77000–77060  ·  view source on GitHub ↗
(credential, tokenCyclerOptions)

Source from the content-addressed store, hash-verified

76998 }
76999 let token = await tryGetAccessToken();
77000 while (token === null) {
77001 await delay2(retryIntervalInMs);
77002 token = await tryGetAccessToken();
77003 }
77004 return token;
77005}
77006function createTokenCycler(credential, tokenCyclerOptions) {
77007 let refreshWorker = null;
77008 let token = null;
77009 let tenantId;
77010 const options = Object.assign(Object.assign({}, DEFAULT_CYCLER_OPTIONS), tokenCyclerOptions);
77011 const cycler = {
77012 /**
77013 * Produces true if a refresh job is currently in progress.
77014 */
77015 get isRefreshing() {
77016 return refreshWorker !== null;
77017 },
77018 /**
77019 * Produces true if the cycler SHOULD refresh (we are within the refresh
77020 * window and not already refreshing)
77021 */
77022 get shouldRefresh() {
77023 var _a5;
77024 return !cycler.isRefreshing && ((_a5 = token === null || token === void 0 ? void 0 : token.expiresOnTimestamp) !== null && _a5 !== void 0 ? _a5 : 0) - options.refreshWindowInMs < Date.now();
77025 },
77026 /**
77027 * Produces true if the cycler MUST refresh (null or nearly-expired
77028 * token).
77029 */
77030 get mustRefresh() {
77031 return token === null || token.expiresOnTimestamp - options.forcedRefreshWindowInMs < Date.now();
77032 }
77033 };
77034 function refresh(scopes, getTokenOptions) {
77035 var _a5;
77036 if (!cycler.isRefreshing) {
77037 const tryGetAccessToken = () => credential.getToken(scopes, getTokenOptions);
77038 refreshWorker = beginRefresh(
77039 tryGetAccessToken,
77040 options.retryIntervalInMs,
77041 // If we don't have a token, then we should timeout immediately
77042 (_a5 = token === null || token === void 0 ? void 0 : token.expiresOnTimestamp) !== null && _a5 !== void 0 ? _a5 : Date.now()
77043 ).then((_token) => {
77044 refreshWorker = null;
77045 token = _token;
77046 tenantId = getTokenOptions.tenantId;
77047 return token;
77048 }).catch((reason) => {
77049 refreshWorker = null;
77050 token = null;
77051 tenantId = void 0;
77052 throw reason;
77053 });
77054 }
77055 return refreshWorker;
77056 }
77057 return async (scopes, tokenOptions) => {

Callers 1

Calls 1

refreshFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…