Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Register extension resource URL scheme as "standard"
Browse files Browse the repository at this point in the history
> Registering a scheme as standard allows relative and absolute resources to be
  resolved correctly when served.

If, for example, a CSS file does a relative import, the imported path can be
resolved within the extension bundle.
  • Loading branch information
rgov committed May 20, 2022
1 parent a5f1e96 commit ee42986
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
18 changes: 15 additions & 3 deletions desktop/main/extensionResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ const NET_ERROR_FAILED = -2;
export function registerExtensionProtocolHandlers(): void {
protocol.registerFileProtocol("x-foxglove-extension-rsrc", (request, callback) => {
// Split the URL into an extension ID and the resource path
const urlPathParts = new URL(request.url).pathname.replace(/^\/+/, "").split("/");
const extId = urlPathParts[0] ?? "";
const rsrcPath = urlPathParts.slice(1).join("/");
const { host: extId, pathname: rsrcPath } = new URL(request.url);

const homePath = app.getPath("home");
const userExtensionRoot = pathJoin(homePath, ".foxglove-studio", "extensions");
Expand All @@ -39,3 +37,17 @@ export function registerExtensionProtocolHandlers(): void {
});
});
}

export function registerExtensionProtocolSchemes(): void {
protocol.registerSchemesAsPrivileged([
{
scheme: "x-foxglove-extension-rsrc",
privileges: {
// The URL scheme adheres to "generic URI syntax", with a host (i.e.,
// the extension's ID) and a path. This also allows resolving relative
// resources, like a CSS file that uses url("./icon.png")
standard: true,
},
},
]);
}
6 changes: 5 additions & 1 deletion desktop/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { AppSetting } from "@foxglove/studio-base/src/AppSetting";
import pkgInfo from "../../package.json";
import StudioAppUpdater from "./StudioAppUpdater";
import StudioWindow from "./StudioWindow";
import { registerExtensionProtocolHandlers } from "./extensionResources";
import {
registerExtensionProtocolHandlers,
registerExtensionProtocolSchemes,
} from "./extensionResources";
import getDevModeIcon from "./getDevModeIcon";
import injectFilesToOpen from "./injectFilesToOpen";
import installChromeExtensions from "./installChromeExtensions";
Expand Down Expand Up @@ -194,6 +197,7 @@ function main() {
ipcMain.handle("getHomePath", () => app.getPath("home"));

// Must be called before app.ready event
registerExtensionProtocolSchemes();
registerRosPackageProtocolSchemes();

ipcMain.handle("updateNativeColorScheme", () => {
Expand Down

0 comments on commit ee42986

Please sign in to comment.