libqalculate-lite-wasm
v0.1.2
Published
libqalculate compiled to WebAssembly with a single plain-text calculate() binding
Readme
libqalculate-lite-wasm
libqalculate compiled to WebAssembly,
exposing calculate(expression, timeoutMs) with plain-text output plus the
library's exchange-rate feeds. A trimmed fork of
stephtr/libqalculate-wasm for
use inside the unimo desktop launcher, where every platform's webview runs the
same wasm and no native libqalculate build is needed.
Usage
import loadLibqalculate from 'libqalculate-lite-wasm';
import wasmUrl from 'libqalculate-lite-wasm/dist/libqalculate.wasm?url'; // vite
const calc = await loadLibqalculate({ locateFile: () => wasmUrl });
const { expression, result, messages } = calc.calculate('2^10', 300);
// expression: "2^10", result: "1024", messages: []An unknown name is reported in messages as an error. The deadline is
cooperative and covers evaluation only: parsing is not interruptible, a hit
deadline is not reported (the result is then partial, unevaluated, or the
literal timed out), and thousands of nested calls throw. Keep inputs short.
The loader is built for the web (fetch); in Node or bun pass the bytes
instead:
const calc = await loadLibqalculate({ wasmBinary: await Bun.file('.../libqalculate.wasm').arrayBuffer() });Under a Content Security Policy, script-src needs 'wasm-unsafe-eval'.
dist/libqalculate.wasm.br is the same wasm brotli-compressed (about a quarter of the
size). Serve it with Content-Encoding: br where you control the headers; otherwise
decompress it in the page, e.g. with brotli-wasm:
import loadLibqalculate from 'libqalculate-lite-wasm';
import brotliPromise from 'brotli-wasm';
import wasmBrUrl from 'libqalculate-lite-wasm/dist/libqalculate.wasm.br?url'; // vite
const [brotli, compressed] = await Promise.all([brotliPromise, fetch(wasmBrUrl).then(r => r.arrayBuffer())]);
const calc = await loadLibqalculate({ wasmBinary: brotli.decompress(new Uint8Array(compressed)) });Exchange rates
Currencies work immediately from the ECB snapshot that libqalculate embeds at its release; results carry a warning once those rates are more than a week old. For live rates the host fetches libqalculate's own feed files and hands the bodies over:
| feed | source | body |
|---|---|---|
| 1 | European Central Bank, EUR base, about 30 currencies | eurofxref-daily.xml |
| 2 | Coinbase spot price of BTC in EUR | JSON with amount and currency |
| 3 | extra currencies (mycurrency.net, floatrates or currency-api layouts) | JSON |
for (const feed of [1, 2, 3] as const) {
const body = await (await fetch(calc.exchangeRatesUrl(feed))).text(); // or your own proxy
calc.setExchangeRates(feed, body);
}The bodies live in the module's in-memory filesystem, so the host keeps them (and their fetch time) and feeds them again after every load, refetching when older than a day. Inside a webview the fetch is subject to CORS; do it from the host process or through a proxy. Pass the bodies through untouched: libqalculate's XML walk depends on the whitespace between the ECB file's elements, so a proxy that re-serialises or minifies it silently yields no rates.
Consumed as a git dependency pinned to a commit; dist/ is tracked, so a
consumer never needs the toolchain.
Building
toolchain.sh installs emsdk and builds GMP, MPFR, libxml2 and libqalculate
for wasm (versions pinned at its top); compile.sh turns src/libqalculate.cc
into dist/libqalculate.js + dist/libqalculate.wasm, plus dist/libqalculate.wasm.br
for hosts that can serve the wasm with Content-Encoding: br. Locally, ./build-wasm.sh
runs both through Docker (the daemon must be running; the first image build
takes a while, each toolchain step is its own layer, and any edit to
toolchain.sh rebuilds them all). CI runs the same two scripts in a
node:22-bookworm container on every push, tests the committed dist/ first,
caches the built libraries until toolchain.sh changes, tests the fresh build,
and uploads it.
Commit dist/ with the source change that produced it; bun test runs a
smoke check against it.
Defaults set in src/libqalculate.cc: radians, an unknown word is an error
rather than a product of one-letter units, 10 significant digits, and ASCII
operators, no digit grouping and no 0.333... so a copied result pastes anywhere.
Differences from upstream
- libqalculate pinned to a release tarball instead of git master.
- Single
calculate()returning plain text; noset_option, variables or plot data. - Exchange rates through libqalculate's own feed files (ECB, Coinbase BTC, extra currencies) instead of a JS rate table limited to EUR pairs.
- ES module output with typed
locateFile/wasmBinaryoptions. - Built artifacts tracked in
dist/, no npm publishing; Forgejo CI instead of GitHub Actions and a registry.
License
GPL-3.0-only, as upstream. libqalculate itself is GPL-2.0-or-later.
