@5minds/processcube_engine_client
Advanced tools
Comparing version 5.1.2-hotfix-75af87-m4r31qgh to 5.1.2-hotfix-94fe08-m4s7bb3g
import { Identity } from '@5minds/processcube_engine_sdk'; | ||
import { ClientCredentialsConfig, IdentityAccessor } from './'; | ||
import { ClientCredentialsConfig, IdentityAccessor } from './index'; | ||
export declare class ClientCredentialsIdentityAccessor extends IdentityAccessor { | ||
@@ -4,0 +4,0 @@ private config; |
@@ -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; |
"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; | ||
@@ -9,0 +9,0 @@ constructor(clientCredentialsConfig) { |
@@ -44,2 +44,3 @@ "use strict"; | ||
socketForIdentity.disconnect(); | ||
socketForIdentity.removeAllListeners(); | ||
if (this.socketCollection[accessor.getId()].identityRefreshSubscription) { | ||
@@ -51,4 +52,3 @@ 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(); | ||
@@ -66,38 +66,14 @@ this.socketCollection[socketId]?.socket?.removeAllListeners(); | ||
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]; | ||
@@ -111,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]; | ||
} | ||
} | ||
@@ -122,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); | ||
@@ -128,3 +109,2 @@ if (existingSocket) { | ||
} | ||
const identity = await accessor.getIdentity(); | ||
const noAuthTokenProvided = !(typeof identity?.token === 'string'); | ||
@@ -153,3 +133,3 @@ if (noAuthTokenProvided) { | ||
socketContext.identityAccessor = accessor; | ||
this.applySubscriptionsToSocket(socketContext); | ||
// this.applySubscriptionsToSocket(socketContext); | ||
return socket; | ||
@@ -162,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) { | ||
@@ -182,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] = { | ||
@@ -190,8 +180,6 @@ 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) { | ||
@@ -201,3 +189,6 @@ oldSocket.removeAllListeners(); | ||
} | ||
SocketContext.socket = newSocket; | ||
this.socketCollection[socketContext.identityAccessor.getId()].socket = undefined; | ||
const newSocket = await this.createSocketForIdentity(socketContext.identityAccessor); | ||
this.applySubscriptionsToSocket(socketContext); | ||
socketContext.socket = newSocket; | ||
}; | ||
@@ -204,0 +195,0 @@ } |
@@ -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,18 +44,19 @@ 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); | ||
} | ||
static cleanupRegistry = new FinalizationRegistry((timeout) => { | ||
clearTimeout(timeout); | ||
}); | ||
identityGetter; | ||
@@ -69,3 +70,2 @@ latestIdentity; | ||
refreshTimeout; | ||
initialTimeout; | ||
disposed = false; | ||
@@ -78,2 +78,3 @@ isDynamicRefreshObject = false; | ||
if (typeof staticOrDynamicIdentity === 'function') { | ||
this.id = uuid.v4(); | ||
this.identityGetter = staticOrDynamicIdentity; | ||
@@ -83,5 +84,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; | ||
@@ -93,7 +98,3 @@ } | ||
}); | ||
this.initialTimeout = 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(); | ||
IdentityAccessor.cleanupRegistry.register(this, this.initialTimeout, this); | ||
IdentityAccessor.cleanupRegistry.register(this, this.refreshTimeout, this); | ||
this.refreshIdentityIfNeeded(); | ||
} | ||
@@ -103,2 +104,5 @@ getId() { | ||
} | ||
getIdentitySync() { | ||
return this.latestIdentity; | ||
} | ||
async getIdentity() { | ||
@@ -136,5 +140,3 @@ await this.waitUntilIdentityIsAvailable(); | ||
clearTimeout(this.refreshTimeout); | ||
clearTimeout(this.initialTimeout); | ||
this.refreshTimeout = undefined; | ||
this.initialTimeout = undefined; | ||
this.identityAvailableResolve(); | ||
@@ -149,3 +151,2 @@ this.disposed = true; | ||
await this.asyncLock.acquire('refreshIdentity', async () => { | ||
clearTimeout(this.initialTimeout); | ||
clearTimeout(this.refreshTimeout); | ||
@@ -156,3 +157,3 @@ let failedCritically = false; | ||
if (this.latestIdentity == dummyIdentity || this.latestIdentity?.token == dummyIdentity.token) { | ||
this.expirationTimeInMs = Number.MAX_SAFE_INTEGER; | ||
this.expirationTimeInMs = 0; | ||
this.identityAvailableResolve(); | ||
@@ -162,3 +163,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(); | ||
@@ -175,2 +176,5 @@ const announceNewIdentity = async () => { | ||
} | ||
else { | ||
this.identityAvailableResolve(); | ||
} | ||
} | ||
@@ -183,7 +187,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); | ||
} | ||
@@ -190,0 +196,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 { | ||
static wrap(identity?: IdentityLike, engineUrl?: string): IdentityAccessor; | ||
private static cleanupRegistry; | ||
private identityGetter; | ||
@@ -13,3 +21,2 @@ private latestIdentity; | ||
private refreshTimeout; | ||
private initialTimeout; | ||
private disposed; | ||
@@ -22,2 +29,3 @@ private isDynamicRefreshObject; | ||
getId(): string; | ||
getIdentitySync(): Identity; | ||
getIdentity(): Promise<Identity>; | ||
@@ -44,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-75af87-m4r31qgh", | ||
"version": "5.1.2-hotfix-94fe08-m4s7bb3g", | ||
"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
351964
5000