Skip to content

Commit a99f2d3

Browse files
authored
Merge pull request #129 from GordonSmith/SFX
feat: Add self extracting wasm "sfx-graphviz"
2 parents 1bcbbf8 + 2bc80f8 commit a99f2d3

File tree

11 files changed

+310
-27
lines changed

11 files changed

+310
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/node_modules
1313
/rust
1414
/src-*
15+
/src/*.wasm.ts
1516
/tmp
1617
/types
1718
/vcpkg

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ SET(EM_FLAGS
1919
"-s IGNORE_CLOSURE_COMPILER_ERRORS=0"
2020
"-s USE_ES6_IMPORT_META=0"
2121
# "-s WASM_BIGINT=1"
22+
"-s STANDALONE_WASM=0"
23+
# "-s DYNAMIC_EXECUTION=0"
2224
"--pre-js ${CMAKE_CURRENT_SOURCE_DIR}/cpp/src/pre.js"
2325
"--post-js ${CMAKE_CURRENT_SOURCE_DIR}/cpp/src/post.js"
2426
)

index.html

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
var wasmBinaryFile;
1010
switch (window.location.hostname) {
1111
case "localhost":
12-
document.write('<script type="text/javascript" src="./dist/index.js"><' + '/script>');
13-
wasmBinaryFile = "./dist/graphvizlib.wasm";
12+
document.write('<script type="text/javascript" src="./dist/sfx-graphviz.js"><' + '/script>');
1413
break;
1514
default:
1615
document.write('<script src="https://cdn.jsdelivr.net/npm/@hpcc-js/wasm/dist/index.min.js"><' + '/script>');
@@ -21,7 +20,6 @@
2120

2221
<script>
2322
var hpccWasm = window["@hpcc-js/wasm"];
24-
// hpccWasm.wasmFolder("build/cpp/graphviz/graphvizlib");
2523
</script>
2624
</head>
2725

@@ -36,11 +34,10 @@ <h3>Two</h3>
3634
<h3>Sync DOT</h3>
3735
<div id="placeholder3"></div>
3836
<hr>
39-
<h3>Cached wasmBinary</h3>
37+
<h3>ESM wasmBinary</h3>
4038
<div id="placeholder4"></div>
4139
<hr>
4240
<script>
43-
hpccWasm.expatVersion().then(v => alert(v));
4441
const dot = `
4542
digraph G {
4643
node [shape=rect];
@@ -112,19 +109,27 @@ <h3>Cached wasmBinary</h3>
112109
}
113110
});
114111

115-
fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(response => {
116-
if (!response.ok) {
117-
throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";
118-
}
119-
return response.arrayBuffer();
120-
}).then(wasmBinary => {
121-
hpccWasm.graphviz.layout(dot, "svg", "dot", { wasmBinary: wasmBinary }).then(svg => {
122-
const div = document.getElementById("placeholder4");
123-
div.innerHTML = svg;
124-
}).catch(err => console.error(err.message));
125-
});
126112
</script>
127113

114+
<script type="module">
115+
import { Graphviz } from "https://cdn.jsdelivr.net/npm/@hpcc-js/wasm/dist/sfx-graphviz.esm.js";
116+
if (Graphviz) {
117+
const graphviz = await Graphviz.load();
118+
const svg = graphviz.layout(dot, "svg", "dot");
119+
const div = document.getElementById("placeholder4");
120+
div.innerHTML = svg;
121+
}
122+
</script>
123+
124+
<script type="module">
125+
import { Graphviz } from "./dist/sfx-graphviz.esm.js";
126+
if (Graphviz) {
127+
const graphviz = await Graphviz.load();
128+
const svg = graphviz.layout(dot, "svg", "dot");
129+
const div = document.getElementById("placeholder4");
130+
div.innerHTML = svg;
131+
}
132+
</script>
128133
</body>
129134

130135
</html>

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
"import": "./dist/graphviz.es6.js",
3939
"default": "./dist/graphviz.js"
4040
},
41+
"sfx-graphviz": {
42+
"import": "./dist/sfx-graphviz.esm.js",
43+
"default": "./dist/sfx-graphviz.js"
44+
},
4145
"zstd": {
4246
"import": "./dist/zstd.es6.js",
4347
"default": "./dist/zstd.js"
@@ -80,8 +84,14 @@
8084
"compile-cpp:win32": "wsl -e ./scripts/cpp-build.sh",
8185
"bundle": "rollup -c",
8286
"bundle-watch": "npm run bundle -- -w",
83-
"minimize": "terser dist/index.js -c -m --source-map -o dist/index.min.js",
84-
"build": "run-s compile-cpp compile bundle minimize",
87+
"bundle-sfx": "rollup -c ./rollup-sfx.config.mjs",
88+
"gen-sfx-base91": "node ./bin/sfx-wasm.mjs ./build/cpp/base91/base91lib.wasm > ./lib-es6/base91lib.wasm.js",
89+
"gen-sfx-expat": "node ./bin/sfx-wasm.mjs ./build/cpp/expat/expatlib/expatlib.wasm > ./lib-es6/expatlib.wasm.js",
90+
"gen-sfx-graphviz": "node ./bin/sfx-wasm.mjs ./build/cpp/graphviz/graphvizlib/graphvizlib.wasm > ./lib-es6/graphvizlib.wasm.js",
91+
"gen-sfx-zstd": "node ./bin/sfx-wasm.mjs ./build/cpp/zstd/zstdlib.wasm > ./lib-es6/zstdlib.wasm.js",
92+
"gen-sfx": "run-p gen-sfx-base91 gen-sfx-expat gen-sfx-graphviz gen-sfx-graphviz",
93+
"minimize": "terser dist/index.js -c -m --source-map -o dist/index.min.js && terser dist/sfx-graphviz.js -c -m --source-map -o dist/sfx-graphviz.min.js",
94+
"build": "run-s compile-cpp compile bundle gen-sfx bundle-sfx minimize",
8595
"build-docker": "docker build --rm --progress plain -f \"./docker/ubuntu-dev.dockerfile\" -t hpcc-js-wasm-build:latest \".\"",
8696
"standard-version": "standard-version",
8797
"standard-version-dryrun": "standard-version --dry-run",

rollup-sfx.config.mjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import commonjs from "@rollup/plugin-commonjs";
2+
import nodeResolve from "@rollup/plugin-node-resolve";
3+
import sourcemaps from "rollup-plugin-sourcemaps";
4+
import { createRequire } from "module";
5+
6+
const require = createRequire(import.meta.url);
7+
const pkg = require("./package.json");
8+
const browserTpl = (input, umdOutput, esOutput) => ({
9+
input: input,
10+
output: [{
11+
file: umdOutput + ".js",
12+
format: "umd",
13+
sourcemap: true,
14+
name: pkg.name
15+
}, {
16+
file: esOutput + ".js",
17+
format: "es",
18+
sourcemap: true
19+
}],
20+
plugins: [
21+
nodeResolve({
22+
preferBuiltins: true
23+
}),
24+
commonjs({}),
25+
sourcemaps()
26+
]
27+
});
28+
29+
export default [
30+
browserTpl("lib-es6/sfx-graphviz", "dist/sfx-graphviz", "dist/sfx-graphviz.esm"),
31+
];

rollup.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ export default [
9696
browserTpl("lib-es6/graphviz", "dist/graphviz", "dist/graphviz.es6"),
9797
browserTpl("lib-es6/expat", "dist/expat", "dist/expat.es6"),
9898
browserTpl("lib-es6/zstd", "dist/zstd", "dist/zstd.es6"),
99-
browserTpl("lib-es6/extract", "dist/extract", "dist/extract.es6"),
10099

101100
browserTpl("lib-es6/__tests__/index", "dist-test/index", "dist-test/index.es6"),
102101
nodeTpl("lib-es6/__tests__/index-node", "dist-test/index.node", "dist-test/index.node.es6"),

src/__bin__/sfx-wasm.mts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs";
22
import yargs from "yargs";
33
import { hideBin } from 'yargs/helpers'
4-
import { Zstd, Base91, extract } from "../index-node";
4+
import { Zstd, Base91 } from "../index-node";
55

66
const myYargs = yargs(hideBin(process.argv)) as yargs.Argv<{}>;
77
myYargs
@@ -28,13 +28,21 @@ try {
2828
const str = base91.encode(compressed);
2929
console.log(`\
3030
import { extract } from "./extract";
31+
import wrapper from "../${wasmPath.replace(".wasm", ".js")}";
32+
3133
const blobStr = '${str}';
32-
export const wasmBinary = extract(blobStr);
34+
35+
let g_module;
36+
export function loadWasm() {
37+
if (!g_module) {
38+
g_module = wrapper({
39+
wasmBinary: extract(blobStr)
40+
});
41+
}
42+
return g_module;
43+
}
44+
3345
`);
34-
const test = extract(str);
35-
if (test.length !== wasmContent.length) {
36-
throw new Error("Oh oh");
37-
}
3846
} else {
3947
throw new Error("'filePath' is required.")
4048
}

src/extract.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as fzstd from 'fzstd';
22

3-
43
// See: https://github.com/Equim-chan/base91
54
const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
65

src/graphviz.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ function createFiles(graphviz: any, _ext?: Ext) {
4848
}
4949

5050
export function graphvizVersion(wasmFolder?: string, wasmBinary?: ArrayBuffer) {
51+
console.warn("Deprecation Warning: 'graphvizVersion' will be refactored into 'Graphviz' in 2.0.0");
5152
return loadWasm(graphvizlib, "graphvizlib", wasmFolder, wasmBinary).then(module => {
5253
return module.Graphviz.prototype.version();
5354
});
5455
}
5556

5657
export const graphviz = {
5758
layout(dotSource: string, outputFormat: Format = "svg", layoutEngine: Engine = "dot", ext?: Ext): Promise<string> {
59+
console.warn("Deprecation Warning: 'graphviz' will be replaced with 'Graphviz' in 2.0.0");
5860
if (!dotSource) return Promise.resolve("");
5961
return loadWasm(graphvizlib, "graphvizlib", ext?.wasmFolder, ext?.wasmBinary).then(module => {
6062
const graphViz = new module.Graphviz(ext?.yInvert ? 1 : 0, ext?.nop ? ext?.nop : 0);
@@ -105,6 +107,7 @@ export class GraphvizSync {
105107
}
106108

107109
layout(dotSource: string, outputFormat: Format = "svg", layoutEngine: Engine = "dot", ext?: Ext): string {
110+
console.warn("Deprecation Warning: 'GraphvizSync' will be replaced with 'Graphviz' in 2.0.0");
108111
if (!dotSource) return "";
109112
const graphViz = new this._wasm.Graphviz(ext?.yInvert ? 1 : 0, ext?.nop ? ext?.nop : 0);
110113
createFiles(graphViz, ext);

src/index-common.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export { wasmFolder } from "./util";
22
export * from "./base91";
33
export * from "./expat";
4-
export * from "./extract";
54
export * from "./graphviz";
65
export * from "./zstd";

0 commit comments

Comments
 (0)