Files
sashinexists/node_modules/graphviz-wasm/dist/generate.js
2024-12-07 13:18:31 +11:00

31 lines
1.1 KiB
JavaScript

/* IMPORT */
import decode from 'decode-base64';
import once from 'function-once';
import wrapper from '../graphviz/wrapper.js';
import { DEFAULT_ENGINE, DEFAULT_FORMAT } from './constants.js';
/* MAIN */
const generate = (GRAPHVIZ_BASE64) => {
const Graphviz = {
/* LIFECYCLE API */
loadWASM: once(async () => {
const GRAPHVIZ_BUFFER = decode(GRAPHVIZ_BASE64);
const instance = await wrapper({ wasmBinary: GRAPHVIZ_BUFFER });
Graphviz.layout = (source, format = DEFAULT_FORMAT, engine = DEFAULT_ENGINE) => {
const graphviz = new instance.Graphviz();
const result = graphviz.layout(source, format, engine);
instance.destroy(graphviz);
if (!result)
throw new Error(instance.Graphviz.prototype.lastError());
return result;
};
}),
/* API */
layout: (source, format = DEFAULT_FORMAT, engine = DEFAULT_ENGINE) => {
throw new Error('[graphviz] You need to call and await "graphviz.loadWASM" first');
}
};
return Graphviz;
};
/* EXPORT */
export default generate;