diff --git a/desktop/flipper-common/src/server-types.tsx b/desktop/flipper-common/src/server-types.tsx index 02e6dca5f..2395a2f8d 100644 --- a/desktop/flipper-common/src/server-types.tsx +++ b/desktop/flipper-common/src/server-types.tsx @@ -125,6 +125,9 @@ export type FlipperServerEvents = { id: string; message: string; }; + 'plugins-server-add-on-message': { + payload: ExecuteMessage; + }; 'download-file-update': DownloadFileUpdate; }; @@ -257,6 +260,9 @@ export type FlipperServerCommands = { pluginName: string, owner: string, ) => Promise; + 'plugins-server-add-on-request-response': ( + payload: ExecuteMessage, + ) => Promise; 'doctor-get-healthchecks': ( settings: FlipperDoctor.HealthcheckSettings, ) => Promise; diff --git a/desktop/flipper-server-core/src/FlipperServerImpl.tsx b/desktop/flipper-server-core/src/FlipperServerImpl.tsx index 1aac53fa5..3abd61d62 100644 --- a/desktop/flipper-server-core/src/FlipperServerImpl.tsx +++ b/desktop/flipper-server-core/src/FlipperServerImpl.tsx @@ -424,6 +424,22 @@ export class FlipperServerImpl implements FlipperServer { // TODO: Figure out if it needs to be async 'plugins-server-add-on-stop': async (pluginName, owner) => this.pluginManager.stopServerAddOn(pluginName, owner), + 'plugins-server-add-on-request-response': async (payload) => { + const serverAddOn = this.pluginManager.getServerAddOnForMessage(payload); + if (serverAddOn) { + return await serverAddOn.connection.sendExpectResponse(payload); + } + return { + length: 0, + error: { + message: `Server add-on for message '${JSON.stringify( + payload, + )} is no longer running.`, + name: 'SERVER_ADDON_STOPPED', + stacktrace: '', + }, + }; + }, 'doctor-get-healthchecks': getHealthChecks, 'doctor-run-healthcheck': runHealthcheck, 'open-file': openFile, diff --git a/desktop/flipper-server-core/src/plugins/PluginManager.tsx b/desktop/flipper-server-core/src/plugins/PluginManager.tsx index 9f5ed424b..1dddee932 100644 --- a/desktop/flipper-server-core/src/plugins/PluginManager.tsx +++ b/desktop/flipper-server-core/src/plugins/PluginManager.tsx @@ -15,6 +15,7 @@ import {default as axios} from 'axios'; import { BundledPluginDetails, DownloadablePluginDetails, + ExecuteMessage, InstalledPluginDetails, } from 'flipper-common'; import {getStaticPath} from '../utils/pathUtils'; @@ -43,6 +44,9 @@ const getTempDirName = promisify(tmp.dir) as ( options?: tmp.DirOptions, ) => Promise; +const isExecuteMessage = (message: object): message is ExecuteMessage => + (message as ExecuteMessage).method === 'execute'; + export class PluginManager { private readonly serverAddOns = new Map(); @@ -155,6 +159,17 @@ export class PluginManager { } } + getServerAddOnForMessage(message: object) { + if (!isExecuteMessage(message)) { + throw new Error( + `PluginManager.getServerAddOnForMessage supports only "execute" messages. Received ${JSON.stringify( + message, + )}`, + ); + } + return this.serverAddOns.get(message.params.api); + } + async startServerAddOn(pluginName: string, owner: string) { console.debug('PluginManager.startServerAddOn', pluginName); const existingServerAddOn = this.serverAddOns.get(pluginName); diff --git a/desktop/flipper-server-core/src/plugins/ServerAddOn.tsx b/desktop/flipper-server-core/src/plugins/ServerAddOn.tsx index 5d3ffd25b..c44e8d7ee 100644 --- a/desktop/flipper-server-core/src/plugins/ServerAddOn.tsx +++ b/desktop/flipper-server-core/src/plugins/ServerAddOn.tsx @@ -8,6 +8,7 @@ */ import assert from 'assert'; +import {ClientResponseType, ExecuteMessage} from 'flipper-common'; import {assertNotNull} from '../comms/Utilities'; type ServerAddOnCleanup = () => Promise; @@ -20,9 +21,15 @@ const loadPlugin = (_pluginName: string): ServerAddOnModule => { return {serverAddOn: async () => async () => {}}; }; +interface ServerAddOnConnection { + sendExpectResponse(payload: ExecuteMessage): Promise; +} + // TODO: Fix potential race conditions when starting/stopping concurrently export class ServerAddOn { private owners: Set; + // TODO: Implement connection + public readonly connection!: ServerAddOnConnection; constructor( public readonly pluginName: string,