diff --git a/desktop/flipper-common/src/ServerAddOn.tsx b/desktop/flipper-common/src/ServerAddOn.tsx index 5bd656f90..118c43dd5 100644 --- a/desktop/flipper-common/src/ServerAddOn.tsx +++ b/desktop/flipper-common/src/ServerAddOn.tsx @@ -16,6 +16,11 @@ import { export interface ServerAddOnControls { start: FlipperServerCommands['plugins-server-add-on-start']; stop: FlipperServerCommands['plugins-server-add-on-stop']; + sendMessage: ( + pluginName: string, + method: string, + params?: unknown, + ) => Promise; } // TODO: Share with js-flipper? Is it worth it? diff --git a/desktop/flipper-common/src/index.tsx b/desktop/flipper-common/src/index.tsx index f81ba69b6..d3bc1f5e4 100644 --- a/desktop/flipper-common/src/index.tsx +++ b/desktop/flipper-common/src/index.tsx @@ -45,6 +45,7 @@ export { isAuthError, getStringFromErrorLike, getErrorFromErrorLike, + deserializeRemoteError, } from './utils/errors'; export * from './GK'; export * from './clientUtils'; diff --git a/desktop/flipper-common/src/utils/errors.tsx b/desktop/flipper-common/src/utils/errors.tsx index 1de81ffdb..3eb9be44e 100644 --- a/desktop/flipper-common/src/utils/errors.tsx +++ b/desktop/flipper-common/src/utils/errors.tsx @@ -7,6 +7,8 @@ * @format */ +import {ClientErrorType} from '../server-types'; + export function isAuthError( err: any, ): err is UserNotSignedInError | UserUnauthorizedError { @@ -114,3 +116,10 @@ export function getStringFromErrorLike(e: any): string { } } } + +export const deserializeRemoteError = (serializedError: ClientErrorType) => { + const err = new Error(serializedError.message); + err.name = serializedError.name; + err.stack += `. Caused by: ${serializedError.stacktrace}`; + return err; +}; diff --git a/desktop/flipper-plugin/src/plugin/DevicePlugin.tsx b/desktop/flipper-plugin/src/plugin/DevicePlugin.tsx index c065c6a32..cb629cd9c 100644 --- a/desktop/flipper-plugin/src/plugin/DevicePlugin.tsx +++ b/desktop/flipper-plugin/src/plugin/DevicePlugin.tsx @@ -69,7 +69,7 @@ export class SandyDevicePluginInstance extends BasePluginInstance { } /** client that is bound to this instance */ - readonly client: DevicePluginClient; + readonly client: DevicePluginClient; constructor( serverAddOnControls: ServerAddOnControls, diff --git a/desktop/flipper-plugin/src/plugin/PluginBase.tsx b/desktop/flipper-plugin/src/plugin/PluginBase.tsx index e8b7babe3..51df562e6 100644 --- a/desktop/flipper-plugin/src/plugin/PluginBase.tsx +++ b/desktop/flipper-plugin/src/plugin/PluginBase.tsx @@ -379,9 +379,13 @@ export abstract class BasePluginInstance { this.flipperLib.showNotification(this.pluginKey, notification); }, logger: this.flipperLib.logger, - sendToServerAddOn: (_method, _params): any => { - // TODO: Implement me - }, + sendToServerAddOn: (method, params) => + this.serverAddOnControls.sendMessage( + this.definition.packageName, + // TODO: Remove type cast + method as string, + params, + ), onServerAddOnMessage: (_event, _cb) => { // TODO: Implement me }, diff --git a/desktop/flipper-plugin/src/test-utils/test-utils.tsx b/desktop/flipper-plugin/src/test-utils/test-utils.tsx index 1ed28766f..05a33fd0b 100644 --- a/desktop/flipper-plugin/src/test-utils/test-utils.tsx +++ b/desktop/flipper-plugin/src/test-utils/test-utils.tsx @@ -647,5 +647,6 @@ function createServerAddOnControlsMock(): ServerAddOnControls { return { start: createStubFunction(), stop: createStubFunction(), + sendMessage: createStubFunction(), }; } diff --git a/desktop/flipper-server-core/src/plugins/ServerAddOn.tsx b/desktop/flipper-server-core/src/plugins/ServerAddOn.tsx index 0bba28f47..607162b0c 100644 --- a/desktop/flipper-server-core/src/plugins/ServerAddOn.tsx +++ b/desktop/flipper-server-core/src/plugins/ServerAddOn.tsx @@ -64,7 +64,7 @@ export class ServerAddOn { ); const serverAddOnModuleToDesktopConnection = - new ServerAddOnModuleToDesktopConnection(); + new ServerAddOnModuleToDesktopConnection(pluginName); const cleanup = await serverAddOn(serverAddOnModuleToDesktopConnection, { flipperServer, diff --git a/desktop/flipper-server-core/src/plugins/ServerAddOnModuleToDesktopConnection.tsx b/desktop/flipper-server-core/src/plugins/ServerAddOnModuleToDesktopConnection.tsx index 04bd2ffca..995bc639b 100644 --- a/desktop/flipper-server-core/src/plugins/ServerAddOnModuleToDesktopConnection.tsx +++ b/desktop/flipper-server-core/src/plugins/ServerAddOnModuleToDesktopConnection.tsx @@ -27,6 +27,10 @@ export class ServerAddOnModuleToDesktopConnection { private subscriptions: Map = new Map(); + constructor(private readonly pluginName: string) { + super(); + } + send(method: string, params: unknown) { const event = 'message'; const message: ServerAddOnModuleToDesktopConnectionEvents[typeof event] = { @@ -34,7 +38,7 @@ export class ServerAddOnModuleToDesktopConnection params: { method, params, - api: '', // TODO: Consider using here pluginName and validate it + api: this.pluginName, }, }; this.emit('message', message); diff --git a/desktop/flipper-ui-core/src/utils/createServerAddOnControls.tsx b/desktop/flipper-ui-core/src/utils/createServerAddOnControls.tsx index 623941614..d7a5935a3 100644 --- a/desktop/flipper-ui-core/src/utils/createServerAddOnControls.tsx +++ b/desktop/flipper-ui-core/src/utils/createServerAddOnControls.tsx @@ -7,7 +7,11 @@ * @format */ -import {FlipperServer, ServerAddOnControls} from 'flipper-common'; +import { + FlipperServer, + ServerAddOnControls, + deserializeRemoteError, +} from 'flipper-common'; export const createServerAddOnControls = ( flipperServer: Pick, @@ -16,4 +20,23 @@ export const createServerAddOnControls = ( flipperServer.exec('plugins-server-add-on-start', pluginName, owner), stop: (pluginName, owner) => flipperServer.exec('plugins-server-add-on-stop', pluginName, owner), + sendMessage: async (pluginName, method, params) => { + const res = await flipperServer.exec( + 'plugins-server-add-on-request-response', + { + method: 'execute', + params: { + method, + api: pluginName, + params, + }, + }, + ); + + if (res.error) { + throw deserializeRemoteError(res.error); + } + + return res.success; + }, });