Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass supported plugins through JSON script tag #39

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/lint-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install pnpm ⚙️
uses: pnpm/action-setup@v2
with:
version: 8.x
version: 9.x

- name: Restore cache 📌
uses: actions/cache@v3
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
- name: Install pnpm ⚙️
uses: pnpm/action-setup@v2
with:
version: 8.x
version: 9.x

- name: Restore cache 📌
uses: actions/cache@v3
Expand Down
3 changes: 1 addition & 2 deletions app/Viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ interface Props {

function Viewer(props: Props) {
const { fileInfo } = props;
const { supportedPlugins } = fileInfo;

const buffer = suspend(async () => {
const res = await fetch(fileInfo.uri);
Expand All @@ -22,7 +21,7 @@ function Viewer(props: Props) {
filename={fileInfo.name}
buffer={buffer}
getExportURL={getExportURL}
getPlugin={async (name) => getPlugin(supportedPlugins[name])}
getPlugin={getPlugin}
>
<App />
</H5WasmProvider>
Expand Down
32 changes: 20 additions & 12 deletions app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import type { GetExportURL } from '@h5web/app';
import { assertNonNull, type GetExportURL } from '@h5web/app';
import { MessageType } from '../src/models.js';
import { vscode } from './vscode-api.js';
import type { Plugin } from '@h5web/h5wasm';

const pluginsScriptElem = document.getElementById('plugins');
assertNonNull(pluginsScriptElem);

const PLUGINS = JSON.parse(pluginsScriptElem.innerHTML);

// 2 GB = 2 * 1024 * 1024 * 1024 B
export const MAX_SIZE_IN_BYTES = 2147483648;

export async function getPlugin(
name: Plugin
): Promise<ArrayBuffer | undefined> {
const url = PLUGINS[name];

if (!url) {
return undefined;
}

const response = await fetch(url);
return response.arrayBuffer();
}

export const getExportURL: GetExportURL = (
format,
dataset,
Expand Down Expand Up @@ -56,14 +75,3 @@ export const getExportURL: GetExportURL = (

return undefined;
};

export async function getPlugin(
url: string | undefined
): Promise<ArrayBuffer | undefined> {
if (!url) {
return undefined;
}

const response = await fetch(url);
return response.arrayBuffer();
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"engines": {
"node": "20.x",
"pnpm": "8.x",
"pnpm": "9.x",
"vscode": ">=1.86.0"
},
"contributes": {
Expand Down
23 changes: 13 additions & 10 deletions src/H5WebViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,15 @@ export default class H5WebViewer
const name = basename(document.uri.fsPath);
const { size } = await workspace.fs.stat(document.uri);

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,
data: { uri, name, size, supportedPlugins },
data: { uri, name, size },
});

function watcher() {
webview.postMessage({
type: MessageType.FileInfo,
data: { uri, name, size, supportedPlugins },
data: { uri, name, size },
});
}

Expand Down Expand Up @@ -97,7 +90,7 @@ export default class H5WebViewer
}

private async getHtmlForWebview(webview: Webview): Promise<string> {
const { extensionPath } = this.context;
const { extensionPath, extensionUri } = this.context;
const { cspSource } = webview;

const manifest = require(join(extensionPath, 'dist/manifest.json'));
Expand All @@ -110,6 +103,15 @@ export default class H5WebViewer
const jsUri = webview.asWebviewUri(jsPathOnDisk);
const cssUri = webview.asWebviewUri(cssPathOnDisk);

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

/*
* CSP policy:
* - `connect-src data:` allows download URLs generated by `getExportURL` when exporting datasets/slices
Expand All @@ -126,6 +128,7 @@ export default class H5WebViewer
>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>H5Web</title>
<script id="plugins" type="application/json">${plugins}</script>
<script type="module" src="${jsUri}"></script>
<link rel="stylesheet" href="${cssUri}">
</head>
Expand Down
1 change: 0 additions & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export interface FileInfo {
uri: string;
name: string;
size: number;
supportedPlugins: Record<Plugin, string>;
}

export interface Export {
Expand Down
Loading