@hpcc-js/wasm
This repository contains a collection of useful c++ libraries compiled to WASM for (re)use in Web Browsers and JavaScript Libraries.
Installation
The simplest way to include this project is via NPM:
npm install --save @hpcc-js/wasm
Contents
@hpcc-js/wasm includes the following files in its dist
folder:
index.js
/ index.min.js
files: Exposes all the available APIs for all WASM files.- WASM Files:
graphvizlib.wasm
- ...more to follow...
Important: WASM files are dynamically loaded at runtime (this is a browser / emscripten requirement), which has a few implications for the consumer:
Pros:
- While this package has potentially many large WASM files, only the ones being used will ever be downloaded from your CDN / Web Server.
Cons:
- Most browsers don't support
fetch
and loading pages via file://
URN, so for testing / development work you will need to run a test web server. - Bundlers (RollupJS / WebPack) will ignore the WASM files, so you will need to manually ensure they are present in your final distribution (typically they are placed in the same folder as the bundled JS)
API Reference
Common
Utility functions relating to @hpcc-js/wasm as a package
# wasmFolder([url]) · <>
If url is specified, sets the default location for all WASM files. If url is not specified it returns the current url (defaults to undefined
).
# __hpcc_wasmFolder · <>
Global variable for setting default WASM location, this is an alternative to wasmFolder
GraphViz (graphvizlib.wasm
)
GraphViz WASM library, see graphviz.org for c++ details. While this package is similar to Viz.js, it employs a completely different build methodology taken from GraphControl.
The GraphViz library comes in two flavours
- An exported
graphviz
namespace, where each API function is asynchrounous and returns a Promise<string>
. - A
graphvizSync
asynchrounous function which returns a Promise<GraphvizSync>
which is a mirror instance of graphviz
, where each API function is synchrounous and returns a string
.
Hello World
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GraphViz WASM</title>
<script src="https://unpkg.com/@hpcc-js/wasm/dist/index.min.js"></script>
<script>
var hpccWasm = window["@hpcc-js/wasm"];
</script>
</head>
<body>
<div id="placeholder"></div>
<div id="placeholder2"></div>
<script>
const dot = `
digraph G {
node [shape=rect];
subgraph cluster_0 {
style=filled;
color=lightgrey;
node [style=filled,color=white];
a0 -> a1 -> a2 -> a3;
label = "Hello";
}
subgraph cluster_1 {
node [style=filled];
b0 -> b1 -> b2 -> b3;
label = "World";
color=blue
}
start -> a0;
start -> b0;
a1 -> b3;
b2 -> a3;
a3 -> a0;
a3 -> end;
b3 -> end;
start [shape=Mdiamond];
end [shape=Msquare];
}
`;
hpccWasm.graphviz.layout(dot, "svg", "dot").then(svg => {
const div = document.getElementById("placeholder");
div.innerHTML = svg;
});
hpccWasm.graphvizSync().then(graphviz => {
const div = document.getElementById("placeholder2");
div.innerHTML = graphviz.layout(dot, "svg", "dot");
});
</script>
</body>
</html>
GraphViz API
# layout(dotSource[, outputFormat][, layoutEngine]) · <>
Performs layout for the supplied dotSource, see The DOT Language for specification.
outputFormat supports the following options:
- dot
- dot_json
- json
- svg (default)
- xdot_json
See Output Formats for more information.
layoutEngine supports the following options:
- circo
- dot (default)
- fdp
- neato
- osage
- patchwork
- twopi
See Layout manual pages for more information.
# circo(dotSource[, outputFormat]) · <>
Convenience function that performs circo layout, is equivalent to layout(dotSource, outputFormat, "circo");
.
# dot(dotSource[, outputFormat]) · <>
Convenience function that performs dot layout, is equivalent to layout(dotSource, outputFormat, "dot");
.
# fdp(dotSource[, outputFormat]) · <>
Convenience function that performs circo layout, is equivalent to layout(dotSource, outputFormat, "fdp");
.
# neato(dotSource[, outputFormat]) · <>
Convenience function that performs neato layout, is equivalent to layout(dotSource, outputFormat, "neato");
.
# osage(dotSource[, outputFormat]) · <>
Convenience function that performs osage layout, is equivalent to layout(dotSource, outputFormat, "osage");
.
# patchwork(dotSource[, outputFormat]) · <>
Convenience function that performs patchwork layout, is equivalent to layout(dotSource, outputFormat, "patchwork");
.
# twopi(dotSource[, outputFormat]) · <>
Convenience function that performs twopi layout, is equivalent to layout(dotSource, outputFormat, "twopi");
.
Building @hpcc-js/wasm
Building is supported on both Linux (tested with Ubuntu 18.04) and Windows (with WSL installed)
There are several required OS dependencies which can be installed with:
sudo ./scripts/cpp-install-prerequisites.sh
(See ./scripts/cpp-install-prerequisites.sh for details)
Build steps:
git clone https://github.com/hpcc-systems/hpcc-js-wasm.git
cd hpcc-js-wasm
npm ci
npm run install-build-deps
npm run build
Note: The install-build-deps
downloads both the Emscripten SDK and the GraphViz sources. This has been made a manual step as the downloads are quite large and the auto-configuration can be time consuming.