swls-wasm
v0.3.5
Published
The [Semantic Web Language Server](https://github.com/SemanticWebLanguageServer/swls) (Turtle, TriG, SPARQL, JSON-LD) compiled to WebAssembly, plus thin helpers for running it as a worker. Built with `wasm-pack --target web`, so it loads in browsers, bund
Readme
swls-wasm
The Semantic Web Language Server
(Turtle, TriG, SPARQL, JSON-LD) compiled to WebAssembly, plus thin helpers for
running it as a worker. Built with wasm-pack --target web, so it loads in
browsers, bundlers, Web Workers and Node without any bundler plugins.
Both directions exchange whole JSON-RPC messages over postMessage with no
Content-Length framing.
Browser / bundler — createSwlsWorker()
The easiest path: get a ready-to-use Web Worker. Works with Vite, webpack, etc.
(the bundler picks up the worker and its .wasm automatically).
import { createSwlsWorker } from "swls-wasm/client";
const worker = createSwlsWorker();
// hand `worker` to your editor's LSP client (e.g. swls-codemirror, monaco)The worker emits { jsonrpc: "2.0", method: "swls/ready" } once the wasm has
finished initializing.
From a CDN — no build step
worker.js and its sibling swls_wasm_bg.wasm are both published, so a worker
can be loaded straight from a CDN:
const worker = new Worker(
"https://cdn.jsdelivr.net/npm/[email protected]/worker.js",
{ type: "module" },
);The worker resolves swls_wasm_bg.wasm relative to its own URL, so it is fetched
from the same CDN path. Cross-origin module workers need permissive CORS (jsDelivr
and unpkg send it); Safari is stricter about cross-origin worker scripts, so if you
need to support it, load a small same-origin bootstrap that import()s the CDN URL.
Custom host — createSwlsServer()
For hosts that aren't a DOM Worker (a Node worker_threads worker, the VS Code
web-extension host, …): wire the server to your own transport. post receives
each outgoing message (already parsed); feed incoming messages (object or string)
into the returned function.
import { createSwlsServer } from "swls-wasm";
const feed = await createSwlsServer((message) => sendToClient(message), {
debug: (line) => console.log(line),
// Optional: override how the wasm is loaded. Defaults to fetching the sibling
// `swls_wasm_bg.wasm`; pass bytes/URL when the host can't fetch that itself
// (e.g. Node, which can't `fetch` a `file://` URL).
wasm: undefined,
});
feed({ jsonrpc: "2.0", id: 1, method: "initialize", params: { /* … */ } });Low-level — WasmLsp
WasmLsp (and the wasm init default export) are also re-exported for full
control. createSwlsServer is just a thin wrapper around them.
