From 10569fa2ad3d22fe773e6720be6fe923a076975c Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 22 Oct 2024 15:50:36 -0300 Subject: [PATCH] chore: remove proxifyWithWait and waitAndCall --- apps/meteor/ee/server/sdk/index.ts | 6 +- .../authorization/canAccessRoomLivechat.ts | 4 +- .../authorization/canAccessRoomVoip.ts | 4 +- .../meteor/tests/mocks/server/BrokerMocked.ts | 4 - .../network-broker/src/NetworkBroker.ts | 24 ------ .../omnichannel-services/src/QueueWorker.ts | 2 +- packages/core-services/src/LocalBroker.ts | 4 - packages/core-services/src/index.ts | 76 +++++++++---------- packages/core-services/src/lib/Api.ts | 4 - packages/core-services/src/lib/proxify.ts | 19 +---- .../core-services/src/types/IApiService.ts | 2 - packages/core-services/src/types/IBroker.ts | 1 - 12 files changed, 49 insertions(+), 101 deletions(-) diff --git a/apps/meteor/ee/server/sdk/index.ts b/apps/meteor/ee/server/sdk/index.ts index 5c0cea1b2fbf2..f8b00779bff3c 100644 --- a/apps/meteor/ee/server/sdk/index.ts +++ b/apps/meteor/ee/server/sdk/index.ts @@ -1,7 +1,7 @@ -import { proxifyWithWait } from '@rocket.chat/core-services'; +import { proxify } from '@rocket.chat/core-services'; import type { IInstanceService } from './types/IInstanceService'; import type { ILDAPEEService } from './types/ILDAPEEService'; -export const LDAPEE = proxifyWithWait('ldap-enterprise'); -export const Instance = proxifyWithWait('instance'); +export const LDAPEE = proxify('ldap-enterprise'); +export const Instance = proxify('instance'); diff --git a/apps/meteor/server/services/authorization/canAccessRoomLivechat.ts b/apps/meteor/server/services/authorization/canAccessRoomLivechat.ts index d326ddbea0b40..9a3d7d9be9f8a 100644 --- a/apps/meteor/server/services/authorization/canAccessRoomLivechat.ts +++ b/apps/meteor/server/services/authorization/canAccessRoomLivechat.ts @@ -1,9 +1,9 @@ import type { IAuthorizationLivechat, RoomAccessValidator } from '@rocket.chat/core-services'; -import { proxifyWithWait } from '@rocket.chat/core-services'; +import { proxify } from '@rocket.chat/core-services'; import type { IOmnichannelRoom } from '@rocket.chat/core-typings'; import { Rooms } from '@rocket.chat/models'; -const AuthorizationLivechat = proxifyWithWait('authorization-livechat'); +const AuthorizationLivechat = proxify('authorization-livechat'); export const canAccessRoomLivechat: RoomAccessValidator = async (room, user, extraData): Promise => { // If we received a partial room and its type is not `l` or `v`, skip all checks. diff --git a/apps/meteor/server/services/authorization/canAccessRoomVoip.ts b/apps/meteor/server/services/authorization/canAccessRoomVoip.ts index 81c9a5702f782..9720910b96a6f 100644 --- a/apps/meteor/server/services/authorization/canAccessRoomVoip.ts +++ b/apps/meteor/server/services/authorization/canAccessRoomVoip.ts @@ -1,8 +1,8 @@ import type { IAuthorizationVoip, RoomAccessValidator } from '@rocket.chat/core-services'; -import { proxifyWithWait } from '@rocket.chat/core-services'; +import { proxify } from '@rocket.chat/core-services'; import { Rooms } from '@rocket.chat/models'; -const AuthorizationVoip = proxifyWithWait('authorization-livechat'); +const AuthorizationVoip = proxify('authorization-livechat'); export const canAccessRoomVoip: RoomAccessValidator = async (room, user, extraData): Promise => { // room can be sent as `null` but in that case a `rid` is also sent on extraData diff --git a/apps/meteor/tests/mocks/server/BrokerMocked.ts b/apps/meteor/tests/mocks/server/BrokerMocked.ts index fd89044bc781e..bf8ed617da228 100644 --- a/apps/meteor/tests/mocks/server/BrokerMocked.ts +++ b/apps/meteor/tests/mocks/server/BrokerMocked.ts @@ -15,10 +15,6 @@ export class BrokerMocked { return this.actions[method](data); } - async waitAndCall(method: string, data: any): Promise { - return this.actions[method](data); - } - async broadcastToServices(): Promise { // no op } diff --git a/ee/packages/network-broker/src/NetworkBroker.ts b/ee/packages/network-broker/src/NetworkBroker.ts index 64b690c3eff62..e84745034c8b2 100644 --- a/ee/packages/network-broker/src/NetworkBroker.ts +++ b/ee/packages/network-broker/src/NetworkBroker.ts @@ -61,30 +61,6 @@ export class NetworkBroker implements IBroker { }); } - async waitAndCall(method: string, data: any): Promise { - if (!(await this.started)) { - return; - } - - try { - await this.broker.waitForServices(method.split('.')[0], waitForServicesTimeout); - } catch (err) { - console.error(err); - throw new Error('Dependent services not available'); - } - - const context = asyncLocalStorage.getStore(); - if (context?.ctx?.call) { - return context.ctx.call(method, data); - } - - return this.broker.call(method, data, { - meta: { - optl: injectCurrentContext(), - }, - }); - } - async destroyService(instance: IServiceClass): Promise { const name = instance.getName(); if (!name) { diff --git a/ee/packages/omnichannel-services/src/QueueWorker.ts b/ee/packages/omnichannel-services/src/QueueWorker.ts index 23604c46eca47..2b59ba9421c35 100644 --- a/ee/packages/omnichannel-services/src/QueueWorker.ts +++ b/ee/packages/omnichannel-services/src/QueueWorker.ts @@ -76,7 +76,7 @@ export class QueueWorker extends ServiceClass implements IQueueWorkerService { this.logger.info(`Processing queue item ${queueItem._id} for work`); this.logger.info(`Queue item is trying to call ${queueItem.message.to}`); try { - await api.waitAndCall(queueItem.message.to, [queueItem.message]); + await api.call(queueItem.message.to, [queueItem.message]); this.logger.info(`Queue item ${queueItem._id} completed`); return 'Completed' as const; } catch (err: unknown) { diff --git a/packages/core-services/src/LocalBroker.ts b/packages/core-services/src/LocalBroker.ts index 451ced1cf39f1..5a89e44659a9a 100644 --- a/packages/core-services/src/LocalBroker.ts +++ b/packages/core-services/src/LocalBroker.ts @@ -30,10 +30,6 @@ export class LocalBroker implements IBroker { return result; } - async waitAndCall(method: string, data: any): Promise { - return this.call(method, data); - } - async destroyService(instance: ServiceClass): Promise { const namespace = instance.getName(); diff --git a/packages/core-services/src/index.ts b/packages/core-services/src/index.ts index c9cfa82caabb8..a0b3f65ded0c8 100644 --- a/packages/core-services/src/index.ts +++ b/packages/core-services/src/index.ts @@ -1,4 +1,4 @@ -import { proxify, proxifyWithWait } from './lib/proxify'; +import { proxify } from './lib/proxify'; import type { IAccount, ILoginResult } from './types/IAccount'; import type { IAnalyticsService } from './types/IAnalyticsService'; import { IApiService } from './types/IApiService'; @@ -124,7 +124,7 @@ export { IVoipFreeSwitchService, NPSCreatePayload, NPSVotePayload, - proxifyWithWait, + proxify, ResizeResult, RoomAccessValidator, TelemetryEvents, @@ -155,42 +155,42 @@ export const dbWatchersDisabled = (process.env.NODE_ENV !== 'production' && !['no', 'false'].includes(disabledEnvVar)); // TODO think in a way to not have to pass the service name to proxify here as well -export const Authorization = proxifyWithWait('authorization'); -export const Apps = proxifyWithWait('apps-engine'); -export const Presence = proxifyWithWait('presence'); -export const Account = proxifyWithWait('accounts'); -export const License = proxifyWithWait('license'); -export const MeteorService = proxifyWithWait('meteor'); -export const Banner = proxifyWithWait('banner'); -export const UiKitCoreApp = proxifyWithWait('uikit-core-app'); -export const NPS = proxifyWithWait('nps'); -export const Team = proxifyWithWait('team'); -export const MessageReads = proxifyWithWait('message-reads'); -export const Room = proxifyWithWait('room'); -export const Media = proxifyWithWait('media'); -export const VoipAsterisk = proxifyWithWait('voip-asterisk'); -export const VoipFreeSwitch = proxifyWithWait('voip-freeswitch'); -export const LivechatVoip = proxifyWithWait('omnichannel-voip'); -export const Analytics = proxifyWithWait('analytics'); -export const LDAP = proxifyWithWait('ldap'); -export const SAUMonitor = proxifyWithWait('sau-monitor'); -export const DeviceManagement = proxifyWithWait('device-management'); -export const VideoConf = proxifyWithWait('video-conference'); -export const Upload = proxifyWithWait('upload'); -export const Calendar = proxifyWithWait('calendar'); -export const QueueWorker = proxifyWithWait('queue-worker'); -export const OmnichannelTranscript = proxifyWithWait('omnichannel-transcript'); -export const Message = proxifyWithWait('message'); -export const Translation = proxifyWithWait('translation'); -export const Settings = proxifyWithWait('settings'); -export const OmnichannelIntegration = proxifyWithWait('omnichannel-integration'); -export const Federation = proxifyWithWait('federation'); -export const FederationEE = proxifyWithWait('federation-enterprise'); -export const Omnichannel = proxifyWithWait('omnichannel'); -export const OmnichannelEEService = proxifyWithWait('omnichannel-ee'); -export const Import = proxifyWithWait('import'); -export const OmnichannelAnalytics = proxifyWithWait('omnichannel-analytics'); -export const User = proxifyWithWait('user'); +export const Authorization = proxify('authorization'); +export const Apps = proxify('apps-engine'); +export const Presence = proxify('presence'); +export const Account = proxify('accounts'); +export const License = proxify('license'); +export const MeteorService = proxify('meteor'); +export const Banner = proxify('banner'); +export const UiKitCoreApp = proxify('uikit-core-app'); +export const NPS = proxify('nps'); +export const Team = proxify('team'); +export const MessageReads = proxify('message-reads'); +export const Room = proxify('room'); +export const Media = proxify('media'); +export const VoipAsterisk = proxify('voip-asterisk'); +export const VoipFreeSwitch = proxify('voip-freeswitch'); +export const LivechatVoip = proxify('omnichannel-voip'); +export const Analytics = proxify('analytics'); +export const LDAP = proxify('ldap'); +export const SAUMonitor = proxify('sau-monitor'); +export const DeviceManagement = proxify('device-management'); +export const VideoConf = proxify('video-conference'); +export const Upload = proxify('upload'); +export const Calendar = proxify('calendar'); +export const QueueWorker = proxify('queue-worker'); +export const OmnichannelTranscript = proxify('omnichannel-transcript'); +export const Message = proxify('message'); +export const Translation = proxify('translation'); +export const Settings = proxify('settings'); +export const OmnichannelIntegration = proxify('omnichannel-integration'); +export const Federation = proxify('federation'); +export const FederationEE = proxify('federation-enterprise'); +export const Omnichannel = proxify('omnichannel'); +export const OmnichannelEEService = proxify('omnichannel-ee'); +export const Import = proxify('import'); +export const OmnichannelAnalytics = proxify('omnichannel-analytics'); +export const User = proxify('user'); // Calls without wait. Means that the service is optional and the result may be an error // of service/method not available diff --git a/packages/core-services/src/lib/Api.ts b/packages/core-services/src/lib/Api.ts index d09b821b601ef..de028dcafd1d5 100644 --- a/packages/core-services/src/lib/Api.ts +++ b/packages/core-services/src/lib/Api.ts @@ -41,10 +41,6 @@ export class Api implements IApiService { return this.broker?.call(method, data); } - async waitAndCall(method: string, data: any): Promise { - return this.broker?.waitAndCall(method, data); - } - async broadcast(event: T, ...args: Parameters): Promise { if (!this.broker) { throw new Error(`No broker set to broadcast: ${event}, ${JSON.stringify(args)}`); diff --git a/packages/core-services/src/lib/proxify.ts b/packages/core-services/src/lib/proxify.ts index 2d712b8fd1490..690ec6256ce0a 100644 --- a/packages/core-services/src/lib/proxify.ts +++ b/packages/core-services/src/lib/proxify.ts @@ -1,13 +1,5 @@ import { api } from '../api'; -type Prom = { - [K in keyof T as T[K] extends (...params: any) => any ? K : never]: T[K] extends (...params: any) => Promise - ? T[K] - : T[K] extends (...params: infer P) => infer R - ? (...params: P) => Promise - : never; -}; - type PromOrError = { [K in keyof T as T[K] extends (...params: any) => any ? K : never]: T[K] extends (...params: any) => Promise ? T[K] @@ -16,20 +8,15 @@ type PromOrError = { : never; }; -function handler(namespace: string, waitService: boolean): ProxyHandler { +function handler(namespace: string): ProxyHandler { return { get: (_target: T, prop: string): any => (...params: any): Promise => - api[waitService ? 'waitAndCall' : 'call'](`${namespace}.${prop}`, params), + api.call(`${namespace}.${prop}`, params), }; } -// TODO remove the need to wait for a service, if that is really needed it should have a dependency on startup -export function proxifyWithWait(namespace: string): Prom { - return new Proxy({}, handler(namespace, true)) as unknown as Prom; -} - export function proxify(namespace: string): PromOrError { - return new Proxy({}, handler(namespace, false)) as unknown as PromOrError; + return new Proxy({}, handler(namespace)) as unknown as PromOrError; } diff --git a/packages/core-services/src/types/IApiService.ts b/packages/core-services/src/types/IApiService.ts index 802ae04ab5e29..0914c6ec3e3c6 100644 --- a/packages/core-services/src/types/IApiService.ts +++ b/packages/core-services/src/types/IApiService.ts @@ -11,8 +11,6 @@ export interface IApiService { call(method: string, data?: unknown): Promise; - waitAndCall(method: string, data: any): Promise; - broadcast(event: T, ...args: Parameters): Promise; broadcastToServices( diff --git a/packages/core-services/src/types/IBroker.ts b/packages/core-services/src/types/IBroker.ts index d75b25de3452b..aee25d096f8ac 100644 --- a/packages/core-services/src/types/IBroker.ts +++ b/packages/core-services/src/types/IBroker.ts @@ -51,7 +51,6 @@ export interface IBroker { destroyService(service: IServiceClass): Promise; createService(service: IServiceClass, serviceDependencies?: string[]): void; call(method: string, data: any): Promise; - waitAndCall(method: string, data: any): Promise; broadcastToServices( services: string[], event: T,