@5minds/processcube_engine_client
Advanced tools
Comparing version 6.0.0-alpha.7 to 6.0.0-alpha.8
@@ -122,2 +122,9 @@ "use strict"; | ||
} | ||
if (error.message?.match(/expired/i) && this.identityAccessor.canIdentityBeRefreshed()) { | ||
this.logger.info('Identity has expired. Refreshing identity to get a new auth token...'); | ||
this.identityAccessor | ||
.forceImmediateIdentityRefresh() | ||
.then(() => this.logger.info('Identity refresh completed. Fetch and Lock will continue.')) | ||
.catch((refreshError) => this.logger.error('Identity refresh failed', { err: refreshError })); | ||
} | ||
} | ||
@@ -124,0 +131,0 @@ } |
@@ -122,2 +122,7 @@ "use strict"; | ||
logger.error('Failed to fetch and lock External Tasks.', { err: error }); | ||
if (error.message?.match(/expired/i) && this.identityAccessor.canIdentityBeRefreshed()) { | ||
logger.info('Identity has expired. Refreshing identity to get a new auth token...'); | ||
await this.identityAccessor.forceImmediateIdentityRefresh(); | ||
logger.info('Identity refresh completed. Fetch and Lock will continue.'); | ||
} | ||
errorCount++; | ||
@@ -124,0 +129,0 @@ const timeoutOffset = (Math.floor(Math.random() * 10) + 1) * 30; |
@@ -86,2 +86,3 @@ "use strict"; | ||
identityAvailableReject; | ||
refreshFailedCritically = false; | ||
refreshTimeout; | ||
@@ -115,3 +116,3 @@ disposed = false; | ||
initialize() { | ||
this.refreshIdentityIfNeeded(); | ||
this.fetchIdentity().then(() => this.refreshIdentityPeriodically()); | ||
} | ||
@@ -124,2 +125,5 @@ getId() { | ||
} | ||
canIdentityBeRefreshed() { | ||
return this.isDynamicRefreshObject && this.expirationTimeInMs > 0 && !this.refreshFailedCritically && !this.disposed; | ||
} | ||
async getIdentity() { | ||
@@ -129,2 +133,10 @@ await this.waitUntilIdentityIsAvailable(); | ||
} | ||
async forceImmediateIdentityRefresh() { | ||
if (!this.isDynamicRefreshObject) { | ||
return; | ||
} | ||
clearTimeout(this.refreshTimeout); | ||
await this.fetchIdentity(); | ||
this.refreshIdentityPeriodically(); | ||
} | ||
/** | ||
@@ -166,6 +178,20 @@ * This method only waits the first time the identity is retrieved from any remote source to ensure no missing references occur. | ||
} | ||
async refreshIdentityIfNeeded() { | ||
refreshIdentityPeriodically() { | ||
clearTimeout(this.refreshTimeout); | ||
if (this.canIdentityBeRefreshed()) { | ||
const timeUntilNextRefresh = Math.max((this.expirationTimeInMs ?? 0) - Date.now() - this.getBaseMinRefreshTimeInMs(), this.getBaseMinRefreshTimeInMs()); | ||
const timeUntilNextRefresh32Bit = Math.min(timeUntilNextRefresh, 2147483647); | ||
this.refreshTimeout = setTimeout(async () => { | ||
await this.fetchIdentity(); | ||
this.refreshIdentityPeriodically(); | ||
}, timeUntilNextRefresh32Bit); | ||
} | ||
else if (this.refreshFailedCritically) { | ||
this.logger.error(`Identity refresh failed critically and won't be refreshed anymore.`); | ||
this.identityAvailableReject(new Error('Identity refresh failed critically and wont be refreshed anymore.')); | ||
} | ||
} | ||
async fetchIdentity() { | ||
await this.asyncLock.acquire('refreshIdentity', async () => { | ||
clearTimeout(this.refreshTimeout); | ||
let failedCritically = false; | ||
this.refreshFailedCritically = false; | ||
try { | ||
@@ -181,11 +207,8 @@ this.latestIdentity = await this.identityGetter(); | ||
this.identityAvailableResolve(); | ||
const announceNewIdentity = async () => { | ||
try { | ||
await Promise.all(this.newIdentityCallbacks.map((cb) => cb(this.latestIdentity))); | ||
} | ||
catch (err) { | ||
this.logger.warn('Error while refreshing identity', { err }); | ||
} | ||
}; | ||
announceNewIdentity(); | ||
try { | ||
await Promise.all(this.newIdentityCallbacks.map((cb) => cb(this.latestIdentity))); | ||
} | ||
catch (err) { | ||
this.logger.warn('Error while publishing new identity to listeners', { err }); | ||
} | ||
} | ||
@@ -198,18 +221,6 @@ else { | ||
this.logger.error('Error while refreshing identity', { err }); | ||
failedCritically = | ||
this.refreshFailedCritically = | ||
!err.message || | ||
!['ETIMEDOUT', 'ECONNRESET', 'ECONNREFUSED', 'other side closed', 'SocketError', 'EAI_AGAIN', 'EHOSTUNREACH', 'EPIPE', 'ECONNABORTED', 'EHOSTDOWN'].some((errorString) => err.message.includes(errorString)); | ||
} | ||
const canBeRefreshed = this.expirationTimeInMs > 0 && !failedCritically && !this.disposed && this.isDynamicRefreshObject; | ||
if (canBeRefreshed) { | ||
const timeUntilNextRefresh = Math.max((this.expirationTimeInMs ?? 0) - Date.now() - this.getBaseMinRefreshTimeInMs(), this.getBaseMinRefreshTimeInMs()); | ||
const timeUntilNextRefresh32Bit = Math.min(timeUntilNextRefresh, 2147483647); | ||
this.refreshTimeout = setTimeout(() => { | ||
this.refreshIdentityIfNeeded(); | ||
}, timeUntilNextRefresh32Bit); | ||
} | ||
else if (failedCritically) { | ||
this.logger.error('Identity refresh failed critically and wont be refreshed anymore.'); | ||
this.identityAvailableReject(new Error('Identity refresh failed critically and wont be refreshed anymore.')); | ||
} | ||
}); | ||
@@ -216,0 +227,0 @@ } |
@@ -25,4 +25,4 @@ "use strict"; | ||
Object.defineProperty(exports, "IdentityAccessor", { enumerable: true, get: function () { return IdentityAccessor_1.IdentityAccessor; } }); | ||
var ClientCredentialsIdentityAccessor_1 = require("./ClientCredentialsIdentityAccessor"); | ||
Object.defineProperty(exports, "ClientCredentialsIdentityAccessor", { enumerable: true, get: function () { return ClientCredentialsIdentityAccessor_1.ClientCredentialsIdentityAccessor; } }); | ||
var IdentityAccessorClientCredentials_1 = require("./IdentityAccessorClientCredentials"); | ||
Object.defineProperty(exports, "ClientCredentialsIdentityAccessor", { enumerable: true, get: function () { return IdentityAccessorClientCredentials_1.ClientCredentialsIdentityAccessor; } }); | ||
var processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk"); | ||
@@ -29,0 +29,0 @@ Object.defineProperty(exports, "BpmnType", { enumerable: true, get: function () { return processcube_engine_sdk_1.BpmnType; } }); |
@@ -20,5 +20,6 @@ import { Identity, Logger } from '@5minds/processcube_engine_sdk'; | ||
private identityAvailableReject; | ||
private refreshTimeout; | ||
private disposed; | ||
private isDynamicRefreshObject; | ||
protected refreshFailedCritically: boolean; | ||
protected refreshTimeout: NodeJS.Timeout; | ||
protected disposed: boolean; | ||
protected isDynamicRefreshObject: boolean; | ||
protected logger: Logger; | ||
@@ -31,3 +32,5 @@ private newIdentityCallbacks; | ||
getIdentitySync(): Identity; | ||
canIdentityBeRefreshed(): boolean; | ||
getIdentity(): Promise<Identity>; | ||
forceImmediateIdentityRefresh(): Promise<void>; | ||
/** | ||
@@ -50,4 +53,5 @@ * This method only waits the first time the identity is retrieved from any remote source to ensure no missing references occur. | ||
protected getBaseMinRefreshTimeInMs(): number; | ||
private refreshIdentityIfNeeded; | ||
private refreshIdentityPeriodically; | ||
private fetchIdentity; | ||
private getDecodedToken; | ||
} |
@@ -7,3 +7,3 @@ export * from './Client'; | ||
export { ClientCredentialsConfig, IdentityAccessor, IdentityLike } from './IdentityAccessor'; | ||
export { ClientCredentialsIdentityAccessor } from './ClientCredentialsIdentityAccessor'; | ||
export { ClientCredentialsIdentityAccessor } from './IdentityAccessorClientCredentials'; | ||
export { BpmnType, DataModels, EventType, Identity, Model, EventReceivedCallback, Subscription } from '@5minds/processcube_engine_sdk'; |
@@ -7,3 +7,3 @@ { | ||
"name": "@5minds/processcube_engine_client", | ||
"version": "6.0.0-alpha.7", | ||
"version": "6.0.0-alpha.8", | ||
"description": "Contains a typescript based client for accessing the Engine.", | ||
@@ -10,0 +10,0 @@ "main": "dist/commonjs/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
350016
4985