Skip to content

Commit

Permalink
Fix compression plugins failing to load under symlinked user directory
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Feb 6, 2024
1 parent 570fb38 commit 9361425
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
24 changes: 19 additions & 5 deletions src/H5WebViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { join, basename, dirname } from 'node:path';
import { writeFileSync, watchFile, unwatchFile } from 'node:fs';
import { Message, MessageType } from './models';
import { getSupportedPlugins } from './plugins';
import { PLUGINS } from './plugins';

export default class H5WebViewer
implements CustomReadonlyEditorProvider<CustomDocument>
Expand All @@ -27,17 +27,17 @@ export default class H5WebViewer
webviewPanel: WebviewPanel
): Promise<void> {
const { webview } = webviewPanel;
const { extensionUri } = this.context;

// Allow opening files outside of workspace
// https://github.com/ucodkr/vscode-tiff/blob/9a4f976584fcba24e9f25680fcdb47fc8f97493f/src/tiffPreview.ts#L27-L30
const extensionRoot = Uri.file(this.context.extensionPath);
const resourceRoot = document.uri.with({
path: document.uri.path.replace(/\/[^/]+?\.\w+$/, '/'),
});

webview.options = {
enableScripts: true,
localResourceRoots: [resourceRoot, extensionRoot],
localResourceRoots: [extensionUri, resourceRoot],
};

webview.html = await this.getHtmlForWebview(webview);
Expand All @@ -47,7 +47,13 @@ export default class H5WebViewer
const uri = webview.asWebviewUri(document.uri).toString();
const name = basename(document.uri.fsPath);
const { size } = await workspace.fs.stat(document.uri);
const supportedPlugins = getSupportedPlugins(webview);

const supportedPlugins = Object.fromEntries(
Object.entries(PLUGINS).map(([name, relativePath]) => {
const pluginUri = Uri.joinPath(extensionUri, 'out', relativePath);
return [name, webview.asWebviewUri(pluginUri).toString()];
})
);

webview.postMessage({
type: MessageType.FileInfo,
Expand Down Expand Up @@ -104,12 +110,20 @@ export default class H5WebViewer
const jsUri = webview.asWebviewUri(jsPathOnDisk);
const cssUri = webview.asWebviewUri(cssPathOnDisk);

/*
* CSP policy:
* - `connect-src data:` allows download URLs generated by `getExportURL` when exporting datasets/slices
* - `script-src 'unsafe-eval` allows `eval()` because of `cwise` dependency in H5Web
*/
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; connect-src data: ${cspSource}; script-src ${cspSource} 'unsafe-eval'; style-src ${cspSource};">
<meta
http-equiv="Content-Security-Policy"
content="default-src 'none'; connect-src ${cspSource} data:; script-src ${cspSource} 'unsafe-eval'; style-src ${cspSource};"
>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>H5Web</title>
<script type="module" src="${jsUri}"></script>
Expand Down
20 changes: 1 addition & 19 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { resolve } from 'node:path';
import { Uri, Webview } from 'vscode';

import blosc from 'h5wasm-plugins/plugins/libH5Zblosc.so';
import bz2 from 'h5wasm-plugins/plugins/libH5Zbz2.so';
import lz4 from 'h5wasm-plugins/plugins/libH5Zlz4.so';
Expand All @@ -9,19 +6,4 @@ import szf from 'h5wasm-plugins/plugins/libH5Zszf.so';
import zfp from 'h5wasm-plugins/plugins/libH5Zzfp.so';
import zstd from 'h5wasm-plugins/plugins/libH5Zzstd.so';

function getPluginUri(webview: Webview, plugin: string) {
const absPath = resolve(__dirname, '../out', plugin);
return webview.asWebviewUri(Uri.file(absPath)).toString();
}

export function getSupportedPlugins(webview: Webview): Record<string, string> {
return {
blosc: getPluginUri(webview, blosc),
bz2: getPluginUri(webview, bz2),
lz4: getPluginUri(webview, lz4),
lzf: getPluginUri(webview, lzf),
szf: getPluginUri(webview, szf),
zfp: getPluginUri(webview, zfp),
zstd: getPluginUri(webview, zstd),
};
}
export const PLUGINS = { blosc, bz2, lz4, lzf, szf, zfp, zstd };

0 comments on commit 9361425

Please sign in to comment.