FIX: CSP unsafe-eval errors when using graphviz on client side take 2

FIX: CSP unsafe-eval errors when using graphviz on client side take 2

diff --git a/assets/javascripts/initializers/discourse-graphviz.js.es6 b/assets/javascripts/initializers/discourse-graphviz.js.es6
index 4d03a09..968a7b7 100644
--- a/assets/javascripts/initializers/discourse-graphviz.js.es6
+++ b/assets/javascripts/initializers/discourse-graphviz.js.es6
@@ -1,7 +1,9 @@
 import loadScript from "discourse/lib/load-script";
 import { withPluginApi } from "discourse/lib/plugin-api";
 import { escape } from "pretty-text/sanitizer";
+import { getURLWithCDN } from "discourse-common/lib/get-url";
 const { run } = Ember;
+let worker = undefined;
 
 export default {
   name: "discourse-graphviz",
@@ -25,42 +27,51 @@ export default {
     const $spinner = $("<div class='spinner tiny'></div>");
     $container.html($spinner);
 
-    loadScript(
-      "/plugins/discourse-graphviz/javascripts/@hpcc-js/wasm@0.3.14/dist/index.min.js"
-    ).then(() => {
+    if (worker === undefined) {
+      worker = new Worker(
+        getURLWithCDN("/plugins/discourse-graphviz/javascripts/worker.js")
+      );
+
+      worker.postMessage({
+        scriptURL: getURLWithCDN(
+          "/plugins/discourse-graphviz/javascripts/@hpcc-js/wasm@0.3.14/dist/index.min.js"
+        )
+      });
+    }
+
+    worker.addEventListener("message", event => {
+      const data = event.data;
       $container.removeClass("is-loading");
 
-      let hpccWasm = self["@hpcc-js/wasm"];
-      hpccWasm.graphviz
-        .layout(graphDefinition, "svg", engine)
-        .then(svgChart => {
-          $container.html(svgChart);
-        })
-        .catch(e => {
-          // graphviz errors are very helpful so we just show them as is
-          const $error = $(
-            "<div class='graph-error'>" + escape(e.message) + "</div>"
-          );
-          $container.html($error);
-        });
+      if (data.svgChart) {
+        $container.html(data.svgChart);
+      } else {
+        // graphviz errors are very helpful so we just show them as is
+        const $error = $(
+          "<div class='graph-error'>" + escape(data.errorMessage) + "</div>"
+        );
+        $container.html($error);
+      }
     });
+
+    worker.postMessage({ graphDefinition, engine });
   },
 
-  initialize() {
-    withPluginApi("0.8.22", api => {
-      api.decorateCooked(
-        $elem => {
-          if (!Discourse.SiteSettings.discourse_graphviz_enabled) {
-            return;
-          }
+  initialize(container) {
+    const siteSettings = container.lookup("site-settings:main");
 
-          const $graphviz = $elem.find(".graphviz");
-          if ($graphviz.length) {
-            run.debounce(this, this.renderGraphs, $graphviz, 200);
-          }
-        },
-        { id: "graphviz" }
-      );
-    });
+    if (siteSettings.discourse_graphviz_enabled) {
+      withPluginApi("0.8.22", api => {
+        api.decorateCooked(
+          $elem => {
+            const $graphviz = $elem.find(".graphviz");
+            if ($graphviz.length) {
+              run.debounce(this, this.renderGraphs, $graphviz, 200);
+            }
+          },
+          { id: "graphviz" }
+        );
+      });
+    }
   }
 };
diff --git a/public/javascripts/@hpcc-js/wasm@0.3.14/dist/index.min.js b/public/javascripts/@hpcc-js/wasm@0.3.14/dist/index.min.js
index 202b30d..45a263d 100644
--- a/public/javascripts/@hpcc-js/wasm@0.3.14/dist/index.min.js
+++ b/public/javascripts/@hpcc-js/wasm@0.3.14/dist/index.min.js
@@ -1 +1 @@

[... diff too long, it was truncated ...]

GitHub sha: 9d723f93