Getting started in JavaScript
cadkit-wasm is the cadkit file and document API compiled to WebAssembly. Parsing and export
run in the JavaScript process: in a browser, files never leave the page. One npm package
ships two builds:
| Import | Build | Use |
|---|---|---|
cadkit-wasm |
ES module (web/) |
browsers and bundlers; call await init() first |
cadkit-wasm/node |
CommonJS (node/) |
Node; ready on load, works with import and require |
TypeScript declarations are included for both.
Install
Section titled “Install”npm install cadkit-wasmimport { readFileSync, writeFileSync } from "node:fs";import { read } from "cadkit-wasm/node";
const doc = read(readFileSync("plan.dxf")); // DWG, DGN, DXF or CityGML; the format is detectedconst info = doc.info();console.log(info.format, info.version, info.units);for (const model of info.models) { console.log(`${model.name}: ${model.entities} entities`);}for (const warning of doc.warnings()) { console.log("warning:", warning.code, warning.message);}writeFileSync("plan.svg", doc.toSvg({ widthPx: 1200 }));doc.free(); // release the WebAssembly memory of this documentread takes a Uint8Array (a Node Buffer is one) and returns a CadDocument. info(),
layers() and warnings() return plain objects; toJSON() returns the whole
document JSON. Call free() when you are done: the
document lives in WebAssembly memory, which the JavaScript garbage collector does not manage.
Browser
Section titled “Browser”import init, { read } from "cadkit-wasm";
await init(); // loads cadkit_wasm_bg.wasm next to the module (bundlers resolve the URL)
const input = document.querySelector('input[type="file"]');input.addEventListener("change", async () => { const doc = read(new Uint8Array(await input.files[0].arrayBuffer())); try { const svg = doc.toSvg({ widthPx: 1200 }); // As an image: no scripts, no external requests, whatever the drawing contains. const img = document.createElement("img"); img.alt = "Drawing preview"; img.src = URL.createObjectURL(new Blob([svg], { type: "image/svg+xml" })); document.body.append(img); } finally { doc.free(); }});init() fetches cadkit_wasm_bg.wasm relative to the module. Bundlers such as Vite resolve
that URL and copy the file; without a bundler, serve node_modules/cadkit-wasm/web/ and import
cadkit_wasm.js from there. Pages with a Content Security Policy need 'wasm-unsafe-eval' in
script-src to instantiate WebAssembly. For anything larger than a quick preview, run cadkit
in a Web Worker as in Build a browser viewer.
Errors and memory
Section titled “Errors and memory”Errors are Error objects with a kind: unknown_format, unsupported, invalid,
truncated and limit_exceeded from the readers, plus io and bad_argument. Because
wasm32 aborts on allocation failure, read uses conservative default limits (512 MiB input,
1 GiB decompressed, 5 million objects, depth 64). If a call still traps with a
WebAssembly.RuntimeError, the module instance is unusable and must be created again.
Next steps
Section titled “Next steps”- Convert DXF to SVG and inspect a drawing
- Build a browser viewer with a Web Worker and bounded picking
- The complete JavaScript API reference