diff --git a/src/H5WebViewer.ts b/src/H5WebViewer.ts index 2c21bdf..d87a454 100644 --- a/src/H5WebViewer.ts +++ b/src/H5WebViewer.ts @@ -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 @@ -27,17 +27,17 @@ export default class H5WebViewer webviewPanel: WebviewPanel ): Promise { 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); @@ -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, @@ -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 ` - + H5Web diff --git a/src/plugins.ts b/src/plugins.ts index c25a00d..71a46aa 100644 --- a/src/plugins.ts +++ b/src/plugins.ts @@ -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'; @@ -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 { - 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 };