diff --git a/deno-runtime/handlers/app/construct.ts b/deno-runtime/handlers/app/construct.ts index d87efc682..85e1ce482 100644 --- a/deno-runtime/handlers/app/construct.ts +++ b/deno-runtime/handlers/app/construct.ts @@ -40,7 +40,7 @@ function wrapAppCode(code: string): (require: (module: string) => unknown) => Pr ) as (require: (module: string) => unknown) => Promise>; } -export async function handlInitializeApp(params: unknown): Promise { +export default async function handleConstructApp(params: unknown): Promise { if (!Array.isArray(params)) { throw new Error('Invalid params', { cause: 'invalid_param_type' }); } diff --git a/deno-runtime/handlers/app/handler.ts b/deno-runtime/handlers/app/handler.ts index 465ffc87f..a8f7d9bf6 100644 --- a/deno-runtime/handlers/app/handler.ts +++ b/deno-runtime/handlers/app/handler.ts @@ -1,16 +1,18 @@ -import { Defined, JsonRpcError } from "jsonrpc-lite"; -import { handlInitializeApp } from "./construct.ts"; +import { Defined, JsonRpcError } from 'jsonrpc-lite'; +import handleConstructApp from './construct.ts'; +import handleInitialize from './initialize.ts'; export default async function handleApp(method: string, params: unknown): Promise { const [, appMethod] = method.split(':'); - let result: Defined; - try { - if (appMethod === 'construct') { - result = await handlInitializeApp(params); - } else { - result = null; + switch (appMethod) { + case 'construct': + return await handleConstructApp(params); + case 'initialize': + return await handleInitialize(); + default: + throw new JsonRpcError('Method not found', -32601); } } catch (e: unknown) { if (!(e instanceof Error)) { @@ -21,8 +23,10 @@ export default async function handleApp(method: string, params: unknown): Promis return JsonRpcError.invalidParams(null); } + if ((e.cause as string)?.includes('invalid_app')) { + return JsonRpcError.internalError({ message: 'App unavailable' }); + } + return new JsonRpcError(e.message, -32000, e); } - - return result; } diff --git a/deno-runtime/handlers/app/initialize.ts b/deno-runtime/handlers/app/initialize.ts new file mode 100644 index 000000000..d592d2970 --- /dev/null +++ b/deno-runtime/handlers/app/initialize.ts @@ -0,0 +1,18 @@ +import type { App } from '@rocket.chat/apps-engine/definition/App.ts'; + +import { AppObjectRegistry } from '../../AppObjectRegistry.ts'; +import { AppAccessorsInstance } from '../../lib/accessors/mod.ts'; + +export default async function handleInitialize(): Promise { + const app = AppObjectRegistry.get('app'); + + if (typeof app?.initialize !== 'function') { + throw new Error('App must contain an initialize function', { + cause: 'invalid_app', + }); + } + + await app.initialize(AppAccessorsInstance.getConfigurationExtend(), AppAccessorsInstance.getEnvironmentRead()); + + return true; +} diff --git a/deno-runtime/main.ts b/deno-runtime/main.ts index 9fc469549..342246322 100644 --- a/deno-runtime/main.ts +++ b/deno-runtime/main.ts @@ -19,7 +19,7 @@ import handleApp from './handlers/app/handler.ts'; AppObjectRegistry.set('MESSAGE_SEPARATOR', Deno.args.at(-1)); -async function handleRequest({ type, payload }: Messenger.JsonRpcRequest): Promise { +async function requestRouter({ type, payload }: Messenger.JsonRpcRequest): Promise { // We're not handling notifications at the moment if (type === 'notification') { return Messenger.sendInvalidRequestError(); @@ -99,7 +99,7 @@ async function main() { } if (Messenger.isRequest(JSONRPCMessage)) { - await handleRequest(JSONRPCMessage); + await requestRouter(JSONRPCMessage); } if (Messenger.isResponse(JSONRPCMessage)) { diff --git a/src/server/AppManager.ts b/src/server/AppManager.ts index 6c6183b4e..96471290b 100644 --- a/src/server/AppManager.ts +++ b/src/server/AppManager.ts @@ -914,14 +914,12 @@ export class AppManager { private async initializeApp(storageItem: IAppStorageItem, app: ProxiedApp, saveToDb = true, silenceStatus = false): Promise { let result: boolean; - const configExtend = this.getAccessorManager().getConfigurationExtend(storageItem.id); - const envRead = this.getAccessorManager().getEnvironmentRead(storageItem.id); try { await app.validateLicense(); await app.validateInstallation(); - await app.call(AppMethod.INITIALIZE, configExtend, envRead); + await app.call(AppMethod.INITIALIZE); await app.setStatus(AppStatus.INITIALIZED, silenceStatus); result = true; diff --git a/src/server/ProxiedApp.ts b/src/server/ProxiedApp.ts index 69c84a50e..c53976b21 100644 --- a/src/server/ProxiedApp.ts +++ b/src/server/ProxiedApp.ts @@ -55,21 +55,7 @@ export class ProxiedApp implements IApp { } public async call(method: `${AppMethod}`, ...args: Array): Promise { - const logger = this.setupLogger(method); - - try { - const result = await this.appRuntime.sendRequest({ method, params: args }); - - logger.debug('Result:', result); - - return result; - } catch (e) { - logger.error('Error:', e); - - throw e; - } finally { - await this.manager.getLogStorage().storeEntries(AppConsole.toStorageEntry(this.getID(), logger)); - } + return this.appRuntime.sendRequest({ method: `app:${method}`, params: args }); } public getStatus(): AppStatus {