Load installed server add-ons

Reviewed By: nikoant

Differential Revision: D34300475

fbshipit-source-id: 6bb6c0ab811e28806a0924b3487931bdb0dd2c59
This commit is contained in:
Andrey Goncharov
2022-02-28 03:50:34 -08:00
committed by Facebook GitHub Bot
parent b4b9c0ab28
commit 8b94186783
7 changed files with 62 additions and 22 deletions

View File

@@ -13,6 +13,10 @@ import {
FlipperServerEvents, FlipperServerEvents,
} from './server-types'; } from './server-types';
export type ServerAddOnStartDetails =
| {isBundled: true; path?: never}
| {isBundled?: false; path: string};
export interface ServerAddOnControls { export interface ServerAddOnControls {
start: FlipperServerCommands['plugins-server-add-on-start']; start: FlipperServerCommands['plugins-server-add-on-start'];
stop: FlipperServerCommands['plugins-server-add-on-stop']; stop: FlipperServerCommands['plugins-server-add-on-stop'];

View File

@@ -17,6 +17,7 @@ import {
OS as PluginOS, OS as PluginOS,
UpdatablePluginDetails, UpdatablePluginDetails,
} from './PluginDetails'; } from './PluginDetails';
import {ServerAddOnStartDetails} from './ServerAddOn';
import { import {
EnvironmentInfo, EnvironmentInfo,
LauncherSettings, LauncherSettings,
@@ -252,6 +253,7 @@ export type FlipperServerCommands = {
'plugins-remove-plugins': (names: string[]) => Promise<void>; 'plugins-remove-plugins': (names: string[]) => Promise<void>;
'plugins-server-add-on-start': ( 'plugins-server-add-on-start': (
pluginName: string, pluginName: string,
details: ServerAddOnStartDetails,
owner: string, owner: string,
) => Promise<void>; ) => Promise<void>;
'plugins-server-add-on-stop': ( 'plugins-server-add-on-stop': (

View File

@@ -510,12 +510,20 @@ export abstract class BasePluginInstance {
protected abstract serverAddOnOwner: string; protected abstract serverAddOnOwner: string;
protected startServerAddOn() { protected startServerAddOn() {
const {serverAddOn, name} = this.definition.details; const pluginDetails = this.definition.details;
if (serverAddOn) { if (pluginDetails.serverAddOn) {
this.serverAddOnControls.start(name, this.serverAddOnOwner).catch((e) => { this.serverAddOnControls
.start(
pluginDetails.name,
pluginDetails.isBundled
? {isBundled: true}
: {path: pluginDetails.serverAddOnEntry!},
this.serverAddOnOwner,
)
.catch((e) => {
console.warn( console.warn(
'Failed to start a server add on', 'Failed to start a server add on',
name, pluginDetails.name,
this.serverAddOnOwner, this.serverAddOnOwner,
e, e,
); );

View File

@@ -419,8 +419,8 @@ export class FlipperServerImpl implements FlipperServer {
'plugins-install-from-npm': (name) => 'plugins-install-from-npm': (name) =>
this.pluginManager.installPluginFromNpm(name), this.pluginManager.installPluginFromNpm(name),
'plugin-source': (path) => this.pluginManager.loadSource(path), 'plugin-source': (path) => this.pluginManager.loadSource(path),
'plugins-server-add-on-start': (pluginName, owner) => 'plugins-server-add-on-start': (pluginName, details, owner) =>
this.pluginManager.startServerAddOn(pluginName, owner), this.pluginManager.startServerAddOn(pluginName, details, owner),
// TODO: Figure out if it needs to be async // TODO: Figure out if it needs to be async
'plugins-server-add-on-stop': async (pluginName, owner) => 'plugins-server-add-on-stop': async (pluginName, owner) =>
this.pluginManager.stopServerAddOn(pluginName, owner), this.pluginManager.stopServerAddOn(pluginName, owner),

View File

@@ -18,6 +18,7 @@ import {
ExecuteMessage, ExecuteMessage,
FlipperServerForServerAddOn, FlipperServerForServerAddOn,
InstalledPluginDetails, InstalledPluginDetails,
ServerAddOnStartDetails,
} from 'flipper-common'; } from 'flipper-common';
import {getStaticPath} from '../utils/pathUtils'; import {getStaticPath} from '../utils/pathUtils';
import {loadDynamicPlugins} from './loadDynamicPlugins'; import {loadDynamicPlugins} from './loadDynamicPlugins';
@@ -173,7 +174,11 @@ export class PluginManager {
return this.serverAddOns.get(message.params.api); return this.serverAddOns.get(message.params.api);
} }
async startServerAddOn(pluginName: string, owner: string) { async startServerAddOn(
pluginName: string,
details: ServerAddOnStartDetails,
owner: string,
) {
console.debug('PluginManager.startServerAddOn', pluginName); console.debug('PluginManager.startServerAddOn', pluginName);
const existingServerAddOn = this.serverAddOns.get(pluginName); const existingServerAddOn = this.serverAddOns.get(pluginName);
if (existingServerAddOn) { if (existingServerAddOn) {
@@ -188,6 +193,7 @@ export class PluginManager {
const newServerAddOn = await ServerAddOn.start( const newServerAddOn = await ServerAddOn.start(
pluginName, pluginName,
details,
owner, owner,
() => this.serverAddOns.delete(pluginName), () => this.serverAddOns.delete(pluginName),
this.flipperServer, this.flipperServer,

View File

@@ -13,6 +13,7 @@ import {
FlipperServerForServerAddOn, FlipperServerForServerAddOn,
ServerAddOnCleanup, ServerAddOnCleanup,
ServerAddOn as ServerAddOnFn, ServerAddOn as ServerAddOnFn,
ServerAddOnStartDetails,
} from 'flipper-common'; } from 'flipper-common';
import {ServerAddOnDesktopToModuleConnection} from './ServerAddOnDesktopToModuleConnection'; import {ServerAddOnDesktopToModuleConnection} from './ServerAddOnDesktopToModuleConnection';
import {ServerAddOnModuleToDesktopConnection} from './ServerAddOnModuleToDesktopConnection'; import {ServerAddOnModuleToDesktopConnection} from './ServerAddOnModuleToDesktopConnection';
@@ -23,16 +24,29 @@ interface ServerAddOnModule {
default: ServerAddOnFn; default: ServerAddOnFn;
} }
const loadPlugin = (pluginName: string): ServerAddOnModule => { const loadPlugin = (
console.debug('loadPlugin', pluginName); pluginName: string,
details: ServerAddOnStartDetails,
): ServerAddOnModule => {
console.debug('loadPlugin', pluginName, details);
if (details.isBundled) {
const bundledPlugin = defaultPlugins[pluginName]; const bundledPlugin = defaultPlugins[pluginName];
if (bundledPlugin) { assertNotNull(
bundledPlugin,
`loadPlugin (isBundled = true) -> plugin ${pluginName} not found.`,
);
return bundledPlugin; return bundledPlugin;
} }
// TODO: Use getInstalledPlugin assertNotNull(
return {default: async () => async () => {}}; details.path,
`loadPlugin (isBundled = false) -> server add-on path is empty plugin ${pluginName}.`,
);
// eslint-disable-next-line no-eval
const serverAddOnModule = eval(`require("${details.path}")`);
return serverAddOnModule;
}; };
// TODO: Fix potential race conditions when starting/stopping concurrently // TODO: Fix potential race conditions when starting/stopping concurrently
@@ -50,13 +64,14 @@ export class ServerAddOn {
static async start( static async start(
pluginName: string, pluginName: string,
details: ServerAddOnStartDetails,
initialOwner: string, initialOwner: string,
onStop: () => void, onStop: () => void,
flipperServer: FlipperServerForServerAddOn, flipperServer: FlipperServerForServerAddOn,
): Promise<ServerAddOn> { ): Promise<ServerAddOn> {
console.info('ServerAddOn.start', pluginName); console.info('ServerAddOn.start', pluginName, details);
const {default: serverAddOn} = loadPlugin(pluginName); const {default: serverAddOn} = loadPlugin(pluginName, details);
assertNotNull(serverAddOn); assertNotNull(serverAddOn);
assert( assert(
typeof serverAddOn === 'function', typeof serverAddOn === 'function',

View File

@@ -45,8 +45,13 @@ export const createServerAddOnControls = (
}; };
return { return {
start: (pluginName, owner) => start: (pluginName, details, owner) =>
flipperServer.exec('plugins-server-add-on-start', pluginName, owner), flipperServer.exec(
'plugins-server-add-on-start',
pluginName,
details,
owner,
),
stop: (pluginName, owner) => stop: (pluginName, owner) =>
flipperServer.exec('plugins-server-add-on-stop', pluginName, owner), flipperServer.exec('plugins-server-add-on-stop', pluginName, owner),
sendMessage: async (pluginName, method, params) => { sendMessage: async (pluginName, method, params) => {