Extend Flipper commands to support server add-on messaging

Reviewed By: mweststrate

Differential Revision: D34046466

fbshipit-source-id: 9acc172c1805ec724b8709999bacf9e899c39e6b
This commit is contained in:
Andrey Goncharov
2022-02-28 03:50:34 -08:00
committed by Facebook GitHub Bot
parent 3b390b74ff
commit 12151e4a71
4 changed files with 44 additions and 0 deletions

View File

@@ -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<void>;
'plugins-server-add-on-request-response': (
payload: ExecuteMessage,
) => Promise<ClientResponseType>;
'doctor-get-healthchecks': (
settings: FlipperDoctor.HealthcheckSettings,
) => Promise<FlipperDoctor.Healthchecks>;

View File

@@ -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,

View File

@@ -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<string>;
const isExecuteMessage = (message: object): message is ExecuteMessage =>
(message as ExecuteMessage).method === 'execute';
export class PluginManager {
private readonly serverAddOns = new Map<string, ServerAddOn>();
@@ -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);

View File

@@ -8,6 +8,7 @@
*/
import assert from 'assert';
import {ClientResponseType, ExecuteMessage} from 'flipper-common';
import {assertNotNull} from '../comms/Utilities';
type ServerAddOnCleanup = () => Promise<void>;
@@ -20,9 +21,15 @@ const loadPlugin = (_pluginName: string): ServerAddOnModule => {
return {serverAddOn: async () => async () => {}};
};
interface ServerAddOnConnection {
sendExpectResponse(payload: ExecuteMessage): Promise<ClientResponseType>;
}
// TODO: Fix potential race conditions when starting/stopping concurrently
export class ServerAddOn {
private owners: Set<string>;
// TODO: Implement connection
public readonly connection!: ServerAddOnConnection;
constructor(
public readonly pluginName: string,