Skip to content

Commit 68fb744

Browse files
authored
Merge pull request #2085 from github/koesie10/fix-graphviz-wasm
Fix Graphviz WASM module not loading for graph viewer
2 parents 3de6a11 + 3043300 commit 68fb744

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

extensions/ql-vscode/src/abstract-webview.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type WebviewPanelConfig = {
2424
view: WebviewView;
2525
preserveFocus?: boolean;
2626
additionalOptions?: WebviewPanelOptions & WebviewOptions;
27+
allowWasmEval?: boolean;
2728
};
2829

2930
export abstract class AbstractWebview<
@@ -116,6 +117,7 @@ export abstract class AbstractWebview<
116117
config.view,
117118
{
118119
allowInlineStyles: true,
120+
allowWasmEval: !!config.allowWasmEval,
119121
},
120122
);
121123
this.push(

extensions/ql-vscode/src/interface-utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ export function getHtmlForWebview(
125125
view: WebviewView,
126126
{
127127
allowInlineStyles,
128+
allowWasmEval,
128129
}: {
129130
allowInlineStyles?: boolean;
131+
allowWasmEval?: boolean;
130132
} = {
131133
allowInlineStyles: false,
134+
allowWasmEval: false,
132135
},
133136
): string {
134137
const scriptUriOnDisk = Uri.file(ctx.asAbsolutePath("out/webview.js"));
@@ -159,7 +162,9 @@ export function getHtmlForWebview(
159162
/*
160163
* Content security policy:
161164
* default-src: allow nothing by default.
162-
* script-src: allow only the given script, using the nonce.
165+
* script-src:
166+
* - allow the given script, using the nonce.
167+
* - 'wasm-unsafe-eval: allow loading WebAssembly modules if necessary.
163168
* style-src: allow only the given stylesheet, using the nonce.
164169
* connect-src: only allow fetch calls to webview resource URIs
165170
* (this is used to load BQRS result files).
@@ -168,7 +173,9 @@ export function getHtmlForWebview(
168173
<html>
169174
<head>
170175
<meta http-equiv="Content-Security-Policy"
171-
content="default-src 'none'; script-src 'nonce-${nonce}'; font-src ${fontSrc}; style-src ${styleSrc}; connect-src ${
176+
content="default-src 'none'; script-src 'nonce-${nonce}'${
177+
allowWasmEval ? " 'wasm-unsafe-eval'" : ""
178+
}; font-src ${fontSrc}; style-src ${styleSrc}; connect-src ${
172179
webview.cspSource
173180
};">
174181
${stylesheetsHtmlLines.join(` ${EOL}`)}

extensions/ql-vscode/src/interface.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ import {
6868
ResultSetSchema,
6969
} from "./pure/bqrs-cli-types";
7070
import { AbstractWebview, WebviewPanelConfig } from "./abstract-webview";
71-
import { PAGE_SIZE } from "./config";
71+
import { isCanary, PAGE_SIZE } from "./config";
7272
import { HistoryItemLabelProvider } from "./query-history/history-item-label-provider";
7373
import { telemetryListener } from "./telemetry";
7474
import { redactableError } from "./pure/errors";
@@ -225,6 +225,8 @@ export class ResultsView extends AbstractWebview<
225225
viewColumn: this.chooseColumnForWebview(),
226226
preserveFocus: true,
227227
view: "results",
228+
// Required for the graph viewer which is using d3-graphviz WASM module. Only supported in canary mode.
229+
allowWasmEval: isCanary(),
228230
};
229231
}
230232

@@ -660,7 +662,8 @@ export class ResultsView extends AbstractWebview<
660662
}
661663
let data;
662664
let numTotalResults;
663-
if (metadata?.kind === GRAPH_TABLE_NAME) {
665+
// Graph results are only supported in canary mode because the graph viewer is not actively supported
666+
if (metadata?.kind === GRAPH_TABLE_NAME && isCanary()) {
664667
data = await interpretGraphResults(
665668
this.cliServer,
666669
metadata,

extensions/ql-vscode/src/view/results/graph.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
InterpretedResultSet,
66
GraphInterpretationData,
77
} from "../../pure/interface-types";
8-
import { graphviz } from "d3-graphviz";
8+
import { graphviz, GraphvizOptions } from "d3-graphviz";
99
import { tryGetLocationFromString } from "../../pure/bqrs-utils";
1010
export type GraphProps = ResultTableProps & {
1111
resultSet: InterpretedResultSet<GraphInterpretationData>;
@@ -59,11 +59,12 @@ export class Graph extends React.Component<GraphProps> {
5959
return;
6060
}
6161

62-
const options = {
62+
const options: GraphvizOptions = {
6363
fit: true,
6464
fade: false,
6565
growEnteringEdges: false,
6666
zoom: true,
67+
useWorker: false,
6768
};
6869

6970
const element = document.querySelector(`#${graphId}`);
@@ -77,8 +78,7 @@ export class Graph extends React.Component<GraphProps> {
7778
const borderColor = getComputedStyle(element).borderColor;
7879
let firstPolygon = true;
7980

80-
graphviz(`#${graphId}`)
81-
.options(options)
81+
graphviz(`#${graphId}`, options)
8282
.attributer(function (d) {
8383
if (d.tag === "a") {
8484
const url = d.attributes["xlink:href"] || d.attributes["href"];

0 commit comments

Comments
 (0)