Skip to content

Commit

Permalink
work in node
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Jan 15, 2025
1 parent 13fde4d commit 091074c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions d2js/js/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async function buildAndCopy(buildType) {
resolve(ROOT_DIR, "wasm/wasm_exec.js"),
join(config.outdir, "wasm_exec.js")
);
await copyFile(resolve(ROOT_DIR, "src/elk.js"), join(config.outdir, "elk.js"));
}
}

Expand Down
2 changes: 0 additions & 2 deletions d2js/js/src/elk.js
Original file line number Diff line number Diff line change
Expand Up @@ -105803,5 +105803,3 @@
[3]
)(3);
});

export default globalThis.ELK;
2 changes: 2 additions & 0 deletions d2js/js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class D2 {
async init() {
this.worker = await createWorker();

const elkContent = await loadFile("./elk.js");
const wasmExecContent = await loadFile("./wasm_exec.js");
const wasmBinary = await loadFile("./d2.wasm");

Expand All @@ -63,6 +64,7 @@ export class D2 {
data: {
wasm: wasmBinary,
wasmExecContent: isNode ? wasmExecContent.toString() : null,
elkContent: isNode ? elkContent.toString() : null,
wasmExecUrl: isNode
? null
: URL.createObjectURL(
Expand Down
4 changes: 3 additions & 1 deletion d2js/js/src/platform.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { wasmBinary, wasmExecJs } from "./wasm-loader.browser.js";
import workerScript from "./worker.js" with { type: "text" };
import elkScript from "./elk.js" with { type: "text" };

// For the browser version, we build the wasm files into a file (wasm-loader.browser.js)
// and loading a file just reads the text, so there's no external dependency calls
export async function loadFile(path) {
if (path === "./d2.wasm") {
return wasmBinary.buffer;
}
if (path === "./wasm_exec.js") {
return new TextEncoder().encode(wasmExecJs).buffer;
}
throw new Error(`Unexpected file request: ${path}`);
return null;
}

export async function createWorker() {
Expand Down
9 changes: 9 additions & 0 deletions d2js/js/src/worker.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parentPort } from "node:worker_threads";

let currentPort;
let d2;
let elk;

export function setupMessageHandler(isNode, port, initWasm) {
currentPort = port;
Expand All @@ -14,8 +15,10 @@ export function setupMessageHandler(isNode, port, initWasm) {
try {
if (isNode) {
eval(data.wasmExecContent);
eval(data.elkContent);
}
d2 = await initWasm(data.wasm);
elk = new ELK();
currentPort.postMessage({ type: "ready" });
} catch (err) {
currentPort.postMessage({ type: "error", error: err.message });
Expand All @@ -24,6 +27,12 @@ export function setupMessageHandler(isNode, port, initWasm) {

case "compile":
try {
if (data.options.layout === "elk") {
const elkGraph = await d2.getELKGraph(JSON.stringify(data));
const elkGraph2 = JSON.parse(elkGraph).data;
const layout = await elk.layout(elkGraph2);
globalThis.elkResult = layout;
}
const result = await d2.compile(JSON.stringify(data));
const response = JSON.parse(result);
if (response.error) throw new Error(response.error.message);
Expand Down
7 changes: 7 additions & 0 deletions d2js/js/test/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ describe("D2 Unit Tests", () => {
await d2.worker.terminate();
}, 20000);

test("elk layout works", async () => {
const d2 = new D2();
const result = await d2.compile("x -> y", { layout: "elk" });
expect(result.diagram).toBeDefined();
await d2.worker.terminate();
}, 20000);

test("render works", async () => {
const d2 = new D2();
const result = await d2.compile("x -> y");
Expand Down
5 changes: 1 addition & 4 deletions d2layouts/d2elklayout/elk.js
Original file line number Diff line number Diff line change
Expand Up @@ -105666,10 +105666,7 @@
// -------------- RUN GWT INITIALIZATION CODE --------------
gwtOnLoad(null, "elk", null);
}.call(this));
}.call(
this,
{}
));
}.call(this, {}));
},
{},
],
Expand Down

0 comments on commit 091074c

Please sign in to comment.