@5minds/processcube_engine_client
Advanced tools
Comparing version 5.1.2-hotfix-f8b2d2-m4mhq5y0 to 5.1.2
@@ -21,2 +21,3 @@ import * as Clients from './Clients/index'; | ||
private readonly userTaskClient; | ||
private readonly socketIoManager; | ||
constructor(engineUrl: string, identity?: IdentityLike); | ||
@@ -23,0 +24,0 @@ dispose(): void; |
import { Identity } from '@5minds/processcube_engine_sdk'; | ||
import { ClientCredentialsConfig, IdentityAccessor } from './'; | ||
import { ClientCredentialsConfig, IdentityAccessor } from './index'; | ||
export declare class ClientCredentialsIdentityAccessor extends IdentityAccessor { | ||
@@ -12,2 +12,3 @@ private config; | ||
private getAuthorityUrlFromEngineUrl; | ||
private hash; | ||
} |
@@ -15,3 +15,5 @@ import { Socket } from 'socket.io-client'; | ||
getSocketForIdentity(accessor: IdentityAccessor): Socket; | ||
private createSocketForIdentitySync; | ||
private createSocketForIdentity; | ||
private buildSocketWithContext; | ||
private getSocketContext; | ||
@@ -18,0 +20,0 @@ private ensureSocketContextIsinitialized; |
@@ -25,21 +25,22 @@ "use strict"; | ||
userTaskClient; | ||
socketIoManager; | ||
constructor(engineUrl, identity) { | ||
this.engineUrl = engineUrl; | ||
this.identityAccessor = IdentityAccessor_1.IdentityAccessor.wrap(identity, engineUrl); | ||
const socketIoManager = new SocketIoManager_1.SocketIoManager(this.engineUrl); | ||
this.socketIoManager = new SocketIoManager_1.SocketIoManager(this.engineUrl); | ||
this.anonymousSessionClient = ClientFactory_1.ClientFactory.createAnonymousSessionClient(this.engineUrl, this.identityAccessor); | ||
this.applicationInfoClient = ClientFactory_1.ClientFactory.createApplicationInfoClient(this.engineUrl, this.identityAccessor, socketIoManager); | ||
this.applicationInfoClient = ClientFactory_1.ClientFactory.createApplicationInfoClient(this.engineUrl, this.identityAccessor, this.socketIoManager); | ||
this.correlationClient = ClientFactory_1.ClientFactory.createCorrelationClient(this.engineUrl, this.identityAccessor); | ||
this.cronjobClient = ClientFactory_1.ClientFactory.createCronJobClient(this.engineUrl, this.identityAccessor); | ||
this.dataObjectInstanceClient = ClientFactory_1.ClientFactory.createDataObjectInstanceClient(this.engineUrl, this.identityAccessor); | ||
this.eventClient = ClientFactory_1.ClientFactory.createEventClient(this.engineUrl, this.identityAccessor, socketIoManager); | ||
this.eventClient = ClientFactory_1.ClientFactory.createEventClient(this.engineUrl, this.identityAccessor, this.socketIoManager); | ||
this.externalTaskClient = ClientFactory_1.ClientFactory.createExternalTaskClient(this.engineUrl, this.identityAccessor); | ||
this.flowNodeInstanceClient = ClientFactory_1.ClientFactory.createFlowNodeInstanceClient(this.engineUrl, this.identityAccessor); | ||
this.manualTaskClient = ClientFactory_1.ClientFactory.createManualTaskClient(this.engineUrl, this.identityAccessor, socketIoManager); | ||
this.notificationClient = ClientFactory_1.ClientFactory.createNotificationClient(this.engineUrl, this.identityAccessor, socketIoManager); | ||
this.manualTaskClient = ClientFactory_1.ClientFactory.createManualTaskClient(this.engineUrl, this.identityAccessor, this.socketIoManager); | ||
this.notificationClient = ClientFactory_1.ClientFactory.createNotificationClient(this.engineUrl, this.identityAccessor, this.socketIoManager); | ||
this.processDefinitionClient = ClientFactory_1.ClientFactory.createProcessDefinitionClient(this.engineUrl, this.identityAccessor); | ||
this.processInstanceClient = ClientFactory_1.ClientFactory.createProcessInstanceClient(this.engineUrl, this.identityAccessor); | ||
this.processModelClient = ClientFactory_1.ClientFactory.createProcessModelClient(this.engineUrl, this.identityAccessor); | ||
this.untypedTaskClient = ClientFactory_1.ClientFactory.createUntypedTaskClient(this.engineUrl, this.identityAccessor, socketIoManager); | ||
this.userTaskClient = ClientFactory_1.ClientFactory.createUserTaskClient(this.engineUrl, this.identityAccessor, socketIoManager); | ||
this.untypedTaskClient = ClientFactory_1.ClientFactory.createUntypedTaskClient(this.engineUrl, this.identityAccessor, this.socketIoManager); | ||
this.userTaskClient = ClientFactory_1.ClientFactory.createUserTaskClient(this.engineUrl, this.identityAccessor, this.socketIoManager); | ||
} | ||
@@ -62,2 +63,3 @@ dispose() { | ||
this.identityAccessor.dispose(); | ||
this.socketIoManager.dispose(); | ||
} | ||
@@ -64,0 +66,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ClientCredentialsIdentityAccessor = void 0; | ||
const _1 = require("./"); | ||
const ClientFactory_1 = require("./ClientFactory"); | ||
const Retry_1 = require("./Retry"); | ||
class ClientCredentialsIdentityAccessor extends _1.IdentityAccessor { | ||
const index_1 = require("./index"); | ||
class ClientCredentialsIdentityAccessor extends index_1.IdentityAccessor { | ||
config; | ||
@@ -15,2 +15,3 @@ constructor(clientCredentialsConfig) { | ||
this.config = clientCredentialsConfig; | ||
this.id = this.hash(JSON.stringify(clientCredentialsConfig ?? {})); | ||
} | ||
@@ -78,4 +79,15 @@ async getFreshIdentity(clientCredentialsConfig) { | ||
} | ||
hash(str) { | ||
// djb2 constants | ||
let hash = 5381; | ||
const product = 33; | ||
let i = str.length; | ||
while (i) { | ||
hash = (hash * product) ^ str.charCodeAt(--i); | ||
} | ||
const numericalHash = hash >>> 0; | ||
return numericalHash + ''; | ||
} | ||
} | ||
exports.ClientCredentialsIdentityAccessor = ClientCredentialsIdentityAccessor; | ||
//# sourceMappingURL=ClientCredentialsIdentityAccessor.js.map |
@@ -44,3 +44,3 @@ "use strict"; | ||
socketForIdentity.disconnect(); | ||
socketForIdentity.close(); | ||
socketForIdentity.removeAllListeners(); | ||
if (this.socketCollection[accessor.getId()].identityRefreshSubscription) { | ||
@@ -52,6 +52,5 @@ accessor.removeOnNewIdentityListener(this.socketCollection[accessor.getId()].identityRefreshSubscription); | ||
dispose() { | ||
const sockets = Object.keys(this.socketCollection); | ||
for (const socketId of sockets) { | ||
for (const socketId of Object.keys(this.socketCollection)) { | ||
this.socketCollection[socketId]?.socket?.disconnect(); | ||
this.socketCollection[socketId]?.socket?.close(); | ||
this.socketCollection[socketId]?.socket?.removeAllListeners(); | ||
if (this.socketCollection[socketId]?.identityRefreshSubscription) { | ||
@@ -67,38 +66,14 @@ this.socketCollection[socketId].identityAccessor.removeOnNewIdentityListener(this.socketCollection[socketId].identityRefreshSubscription); | ||
createSocketIoSubscriptionSync(accessor, eventName, callback, subscribeOnce) { | ||
const subscription = this.addNewSubscriptionToSocketContext(accessor, eventName, callback, subscribeOnce); | ||
const createSocketInBackground = async () => { | ||
try { | ||
const socketForIdentity = await this.createSocketForIdentity(accessor); | ||
if (subscribeOnce) { | ||
socketForIdentity.once(eventName, callback); | ||
} | ||
else { | ||
socketForIdentity.on(eventName, callback); | ||
} | ||
} | ||
catch (error) { | ||
this.logger.error('Error while creating socket.io subscription, aborting subscription', { err: error, eventName: eventName }); | ||
this.removeSocketIoSubscription(accessor, subscription); | ||
} | ||
}; | ||
createSocketInBackground(); | ||
return subscription; | ||
this.createSocketForIdentitySync(accessor); | ||
return this.addNewSubscriptionToSocketContext(accessor, eventName, callback, subscribeOnce); | ||
} | ||
async createSocketIoSubscription(accessor, eventName, callback, subscribeOnce) { | ||
const socketForIdentity = await this.createSocketForIdentity(accessor); | ||
if (subscribeOnce) { | ||
socketForIdentity.once(eventName, callback); | ||
} | ||
else { | ||
socketForIdentity.on(eventName, callback); | ||
} | ||
const subscription = this.addNewSubscriptionToSocketContext(accessor, eventName, callback, subscribeOnce); | ||
return subscription; | ||
await this.createSocketForIdentity(accessor); | ||
return this.addNewSubscriptionToSocketContext(accessor, eventName, callback, subscribeOnce); | ||
} | ||
removeSocketIoSubscription(accessor, subscription) { | ||
const socketForIdentity = this.getSocketForIdentity(accessor); | ||
if (!socketForIdentity) { | ||
const socketContext = this.getSocketContext(accessor); | ||
if (!socketContext.socket) { | ||
return; | ||
} | ||
const socketContext = this.getSocketContext(accessor); | ||
const eventSubscriptions = socketContext.subscriptions[subscription.eventName]; | ||
@@ -112,8 +87,5 @@ if (!eventSubscriptions) { | ||
} | ||
const callbackToRemove = subscriptionContext.callback; | ||
socketContext.socket.off(subscription.eventName, subscriptionContext.callback); | ||
delete socketContext.subscriptions[subscription.eventName]; | ||
delete eventSubscriptions[subscription.id]; | ||
if (Object.keys(eventSubscriptions).length === 0) { | ||
socketForIdentity.off(subscription.eventName, callbackToRemove); | ||
delete socketContext.subscriptions[subscription.eventName]; | ||
} | ||
} | ||
@@ -123,3 +95,11 @@ getSocketForIdentity(accessor) { | ||
} | ||
createSocketForIdentitySync(accessor) { | ||
const identity = accessor.getIdentitySync(); | ||
return this.buildSocketWithContext(accessor, identity); | ||
} | ||
async createSocketForIdentity(accessor) { | ||
const identity = await accessor.getIdentity(); | ||
return this.buildSocketWithContext(accessor, identity); | ||
} | ||
buildSocketWithContext(accessor, identity) { | ||
const existingSocket = this.getSocketForIdentity(accessor); | ||
@@ -129,3 +109,2 @@ if (existingSocket) { | ||
} | ||
const identity = await accessor.getIdentity(); | ||
const noAuthTokenProvided = !(typeof identity?.token === 'string'); | ||
@@ -154,3 +133,3 @@ if (noAuthTokenProvided) { | ||
socketContext.identityAccessor = accessor; | ||
this.applySubscriptionsToSocket(socketContext); | ||
// this.applySubscriptionsToSocket(socketContext); | ||
return socket; | ||
@@ -163,8 +142,10 @@ } | ||
ensureSocketContextIsinitialized(accessor) { | ||
this.socketCollection[accessor.getId()] = this.socketCollection[accessor.getId()] || { | ||
subscriptions: {}, | ||
identityAccessor: accessor, | ||
socket: null, | ||
identityRefreshSubscription: null, | ||
}; | ||
if (!Object.hasOwn(this.socketCollection, accessor.getId())) { | ||
this.socketCollection[accessor.getId()] = { | ||
subscriptions: {}, | ||
identityAccessor: accessor, | ||
socket: null, | ||
identityRefreshSubscription: null, | ||
}; | ||
} | ||
if (!this.socketCollection[accessor.getId()].identityRefreshSubscription) { | ||
@@ -183,3 +164,11 @@ this.socketCollection[accessor.getId()].identityRefreshSubscription = this.getRecreateWhenIdentityIsRefreshedFunction(this.socketCollection[accessor.getId()]); | ||
const socketContext = this.getSocketContext(accessor); | ||
socketContext.subscriptions[eventName] = socketContext.subscriptions[eventName] || {}; | ||
if (subscribeOnce) { | ||
socketContext.socket.once(eventName, callback); | ||
} | ||
else { | ||
socketContext.socket.on(eventName, callback); | ||
} | ||
if (!socketContext.subscriptions[eventName]) { | ||
socketContext.subscriptions[eventName] = {}; | ||
} | ||
socketContext.subscriptions[eventName][subscriptionId] = { | ||
@@ -191,14 +180,14 @@ subscription: subscription, | ||
} | ||
getRecreateWhenIdentityIsRefreshedFunction(SocketContext) { | ||
getRecreateWhenIdentityIsRefreshedFunction(socketContext) { | ||
return async (identity) => { | ||
this.logger.info(`Recreating socket.io connection for identity ${identity?.userId ?? SocketContext.identityAccessor.getId()}`); | ||
const oldSocket = SocketContext.socket; | ||
this.socketCollection[SocketContext.identityAccessor.getId()].socket = undefined; | ||
const newSocket = await this.createSocketForIdentity(SocketContext.identityAccessor); | ||
this.logger.info(`Recreating socket.io connection for identity ${identity?.userId ?? socketContext.identityAccessor.getId()}`); | ||
const oldSocket = socketContext.socket; | ||
if (oldSocket) { | ||
oldSocket.removeAllListeners(); | ||
oldSocket.disconnect(); | ||
oldSocket.close(); | ||
} | ||
SocketContext.socket = newSocket; | ||
this.socketCollection[socketContext.identityAccessor.getId()].socket = undefined; | ||
const newSocket = await this.createSocketForIdentity(socketContext.identityAccessor); | ||
this.applySubscriptionsToSocket(socketContext); | ||
socketContext.socket = newSocket; | ||
}; | ||
@@ -205,0 +194,0 @@ } |
@@ -77,3 +77,3 @@ "use strict"; | ||
this.stopLockingInterval(); | ||
this.identityAccessor?.dispose(); | ||
// we explicitly do not dispose the identityAccessor here, since it is shared with the ExternalTaskWorker | ||
} | ||
@@ -80,0 +80,0 @@ stopLockingInterval() { |
@@ -132,3 +132,3 @@ "use strict"; | ||
async fetchAndLockExternalTasks() { | ||
return this.externalTaskClient.fetchAndLockExternalTasks(this.workerId, this.topic, this.maxTasks, this.longpollingTimeout, this.lockDuration, this.payloadFilter, await this.identityAccessor.getIdentity(), this.abortSignal); | ||
return this.externalTaskClient.fetchAndLockExternalTasks(this.workerId, this.topic, this.maxTasks, this.longpollingTimeout, this.lockDuration, this.payloadFilter, this.identityAccessor, this.abortSignal); | ||
} | ||
@@ -135,0 +135,0 @@ async startExecutingExternalTask(externalTask) { |
@@ -33,3 +33,3 @@ "use strict"; | ||
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk"); | ||
const _1 = require("./"); | ||
const index_1 = require("./index"); | ||
const dummyIdentity = { | ||
@@ -44,14 +44,18 @@ token: 'ZHVtbXlfdG9rZW4=', | ||
} | ||
else if (identity instanceof IdentityAccessor) { | ||
if (identity instanceof IdentityAccessor) { | ||
return identity; | ||
} | ||
else if (isClientCredentialsConfig(identity)) { | ||
if (isClientCredentialsConfig(identity)) { | ||
if (!identity.engineUrl && engineUrl) { | ||
identity.engineUrl = engineUrl; | ||
} | ||
return new _1.ClientCredentialsIdentityAccessor(identity); | ||
return new index_1.ClientCredentialsIdentityAccessor(identity); | ||
} | ||
else { | ||
return new IdentityAccessor(identity); | ||
if (!identity.token) { | ||
const invalidIdentity = new processcube_engine_sdk_1.BadRequestError(`The given identity is invalid. Required property 'token' is missing.`); | ||
invalidIdentity.additionalInformation = { | ||
configuredIdentity: identity, | ||
}; | ||
} | ||
return new IdentityAccessor(identity); | ||
} | ||
@@ -73,2 +77,3 @@ identityGetter; | ||
if (typeof staticOrDynamicIdentity === 'function') { | ||
this.id = uuid.v4(); | ||
this.identityGetter = staticOrDynamicIdentity; | ||
@@ -78,5 +83,9 @@ this.isDynamicRefreshObject = true; | ||
else if (staticOrDynamicIdentity) { | ||
this.id = staticOrDynamicIdentity?.userId ?? uuid.v4(); | ||
this.latestIdentity = staticOrDynamicIdentity; | ||
this.identityGetter = async () => staticOrDynamicIdentity; | ||
} | ||
else { | ||
this.id = dummyIdentity.userId; | ||
this.latestIdentity = dummyIdentity; | ||
this.identityGetter = async () => dummyIdentity; | ||
@@ -88,5 +97,3 @@ } | ||
}); | ||
setTimeout(() => this.refreshIdentityIfNeeded(), 0); | ||
// If any non asynchronous identity is provided, we can immediately use its userId, otherwise we generate a new one to use for socketIo-Management | ||
this.id = this.latestIdentity ? this.latestIdentity.userId : uuid.v4(); | ||
this.refreshIdentityIfNeeded(); | ||
} | ||
@@ -96,2 +103,5 @@ getId() { | ||
} | ||
getIdentitySync() { | ||
return this.latestIdentity; | ||
} | ||
async getIdentity() { | ||
@@ -139,2 +149,3 @@ await this.waitUntilIdentityIsAvailable(); | ||
await this.asyncLock.acquire('refreshIdentity', async () => { | ||
clearTimeout(this.refreshTimeout); | ||
let failedCritically = false; | ||
@@ -144,3 +155,3 @@ try { | ||
if (this.latestIdentity == dummyIdentity || this.latestIdentity?.token == dummyIdentity.token) { | ||
this.expirationTimeInMs = Number.MAX_SAFE_INTEGER; | ||
this.expirationTimeInMs = 0; | ||
this.identityAvailableResolve(); | ||
@@ -150,3 +161,3 @@ } | ||
const decodedToken = this.getDecodedToken(this.latestIdentity.token); | ||
this.expirationTimeInMs = decodedToken?.exp ? decodedToken.exp * 1000 : Number.MAX_SAFE_INTEGER; | ||
this.expirationTimeInMs = decodedToken?.exp ? decodedToken.exp * 1000 : 0; | ||
this.identityAvailableResolve(); | ||
@@ -163,2 +174,5 @@ const announceNewIdentity = async () => { | ||
} | ||
else { | ||
this.identityAvailableResolve(); | ||
} | ||
} | ||
@@ -171,7 +185,9 @@ catch (err) { | ||
} | ||
const canBeRefreshed = this.expirationTimeInMs !== Number.MAX_SAFE_INTEGER && !failedCritically && !this.disposed && this.isDynamicRefreshObject; | ||
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.bind(this), timeUntilNextRefresh32Bit); | ||
this.refreshTimeout = setTimeout(() => { | ||
this.refreshIdentityIfNeeded(); | ||
}, timeUntilNextRefresh32Bit); | ||
} | ||
@@ -178,0 +194,0 @@ else if (failedCritically) { |
import { Identity, Logger } from '@5minds/processcube_engine_sdk'; | ||
export type IdentityLike = Identity | (() => Promise<Identity>) | (() => Identity) | IdentityAccessor | ClientCredentialsConfig; | ||
export type ClientCredentialsConfig = { | ||
authorityUrl?: string; | ||
engineUrl?: string; | ||
baseMinRefreshTimeInMs?: number; | ||
clientId: string; | ||
clientSecret: string; | ||
scope: string; | ||
}; | ||
export declare class IdentityAccessor { | ||
@@ -7,3 +16,3 @@ static wrap(identity?: IdentityLike, engineUrl?: string): IdentityAccessor; | ||
private expirationTimeInMs; | ||
private id; | ||
protected id: string; | ||
private identityAvailablePromise; | ||
@@ -20,2 +29,3 @@ private identityAvailableResolve; | ||
getId(): string; | ||
getIdentitySync(): Identity; | ||
getIdentity(): Promise<Identity>; | ||
@@ -42,10 +52,1 @@ /** | ||
} | ||
export type IdentityLike = Identity | (() => Promise<Identity>) | (() => Identity) | IdentityAccessor | ClientCredentialsConfig; | ||
export type ClientCredentialsConfig = { | ||
authorityUrl?: string; | ||
engineUrl?: string; | ||
baseMinRefreshTimeInMs?: number; | ||
clientId: string; | ||
clientSecret: string; | ||
scope: string; | ||
}; |
@@ -7,3 +7,3 @@ { | ||
"name": "@5minds/processcube_engine_client", | ||
"version": "5.1.2-hotfix-f8b2d2-m4mhq5y0", | ||
"version": "5.1.2", | ||
"description": "Contains a typescript based client for accessing the Engine.", | ||
@@ -56,4 +56,5 @@ "main": "dist/commonjs/index.js", | ||
"prettier": "^3.3.2", | ||
"typescript": "^5.5.3" | ||
"typescript": "^5.5.3", | ||
"why-is-node-running": "^3.2.1" | ||
} | ||
} |
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
351941
5000
1
5