diff --git a/src/main/index.ts b/src/main/index.ts index a7549ae1d3..58c2b29ad2 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -26,9 +26,8 @@ import { InstalledExtension, extensionDiscovery } from "../extensions/extension- import type { LensExtensionId } from "../extensions/lens-extension"; import { installDeveloperTools } from "./developer-tools"; import { filesystemProvisionerStore } from "./extension-filesystem"; -import { LensProtocolRouter, RoutingError } from "./protocol-handler"; -import Url from "url-parse"; - +import { lensProtocolChannel } from "./protocol-handler"; +import { broadcastMessage } from "../common/ipc"; const workingDir = path.join(app.getPath("appData"), appName); let proxyPort: number; let proxyServer: LensProxy; @@ -131,15 +130,8 @@ app // protocol handler for macOS event.preventDefault(); - try { - LensProtocolRouter.getInstance().route(Url(rawUrl, true)); - } catch (error) { - if (error instanceof RoutingError) { - logger.error(`[PROTOCOL ROUTER]: ${error}`, { url: error.url }); - } else { - logger.error(`[PROTOCOL ROUTER]: ${error}`, { rawUrl }); - } - } + // Broadcast "open-url" events to renderer + broadcastMessage(lensProtocolChannel, { rawUrl }); }); // Extensions-api runtime exports diff --git a/src/main/protocol-handler/router.ts b/src/main/protocol-handler/router.ts index f89f6f69da..b7addf8fb7 100644 --- a/src/main/protocol-handler/router.ts +++ b/src/main/protocol-handler/router.ts @@ -2,6 +2,8 @@ import { Singleton } from "../../common/utils"; import Url from "url-parse"; import { match, matchPath } from "react-router"; import { pathToRegexp } from "path-to-regexp"; +import { subscribeToBroadcast } from "../../common/ipc"; +import logger from "../logger"; export enum RoutingErrorType { INVALID_PROTOCOL = "invalid-protocol", @@ -46,6 +48,9 @@ export type ExtensionId = string; const EXT_ID_MATCH = "LENS_INTERNAL_EXTENSION_ID_MATCH"; +// IPC channel for protocol actions. Main broadcasts the open-url events to this channel. +export const lensProtocolChannel = "protocol-handler"; + interface ExtensionIdMatch { [EXT_ID_MATCH]: string; } @@ -56,6 +61,20 @@ export class LensProtocolRouter extends Singleton { private static ExtensionIDSchema = `/:${EXT_ID_MATCH}`; + public init() { + subscribeToBroadcast(lensProtocolChannel, ((_event, { rawUrl }) => { + try { + this.route(Url(rawUrl, true)); + } catch (error) { + if (error instanceof RoutingError) { + logger.error(`[PROTOCOL ROUTER]: ${error}`, { url: error.url }); + } else { + logger.error(`[PROTOCOL ROUTER]: ${error}`, { rawUrl }); + } + } + })); + } + /** * route */ diff --git a/src/renderer/api/protocol-endpoints/install-extension.ts b/src/renderer/api/protocol-endpoints/install-extension.ts index a2b29f134f..3b787e68f9 100644 --- a/src/renderer/api/protocol-endpoints/install-extension.ts +++ b/src/renderer/api/protocol-endpoints/install-extension.ts @@ -3,14 +3,14 @@ import { RouteParams } from "../../../main/protocol-handler"; import { installFromNpm } from "../../components/+extensions"; export async function installExtension(params: RouteParams): Promise { - const { extId } = params.search; - - if (!extId) { - return void logger.info("installExtension handler: missing 'extId' from search params"); + const { name } = params.search; + + if (!name) { + return void logger.info("installExtension handler: missing 'name' from search params"); } try { - await installFromNpm(extId); + await installFromNpm(name); } catch (error) { logger.error("[PH - Install Extension]: failed to install from NPM", error); } diff --git a/src/renderer/bootstrap.tsx b/src/renderer/bootstrap.tsx index fa80cdc035..f6cff633af 100644 --- a/src/renderer/bootstrap.tsx +++ b/src/renderer/bootstrap.tsx @@ -18,6 +18,7 @@ import { i18nStore } from "./i18n"; import { LensApp } from "./lens-app"; import { themeStore } from "./theme.store"; import protocolEndpoints from "./api/protocol-endpoints"; +import { LensProtocolRouter } from "../main/protocol-handler"; type AppComponent = React.ComponentType & { init?(): Promise; @@ -37,6 +38,7 @@ export async function bootstrap(App: AppComponent) { extensionLoader.init(); extensionDiscovery.init(); + LensProtocolRouter.getInstance().init(); protocolEndpoints.registerHandlers(); // preload common stores