Skip to content

feat: run Curve25519 crypto in a Web Worker, with a wasm-free worker-client build - #47

Merged
jcbrand merged 6 commits into
masterfrom
wasm-single-file-node-esm
Aug 4, 2026
Merged

feat: run Curve25519 crypto in a Web Worker, with a wasm-free worker-client build#47
jcbrand merged 6 commits into
masterfrom
wasm-single-file-node-esm

Conversation

@jcbrand

@jcbrand jcbrand commented Aug 4, 2026

Copy link
Copy Markdown
Member

No description provided.

jcbrand added 6 commits July 23, 2026 12:34
Compile the curve25519 Emscripten module with `-s SINGLE_FILE=1`, so the
wasm ships as an in-JS base64 data URI and is decoded in-JS. This drops
the sibling `.wasm` path resolution and Emscripten's CommonJS `require('fs')`
and `fetch(path)` loaders, so the library loads identically in browsers,
web workers, Node ESM, and Node CJS.
Refactor internalCrypto to delegate its six curve operations to a
swappable CurveBackend instead of constructing a fresh Curve25519 (and
thus a fresh wasm instance) per call. The default LocalCurveBackend runs
on the main thread and lazily caches a single Curve25519.

Adds setCurveBackend/resetCurveBackend/getLocalCurveBackend so a later
worker backend can take over dispatch and fall back to the local one.

No behaviour change yet. With no worker started, everything runs locally
exactly as before.
Add curvePubKeyToEd25519PubKey and ed25519PubKeyToCurvePubKey to the
worker's ALLOWED_METHODS. They are already public Curve25519 methods and
the worker dispatches by name, so no other change is needed. This completes
the worker's curve surface to all six operations internalCrypto needs,
which is the prerequisite for a worker-only (wasm-free) main build.
Wire startWorker/stopWorker into the CurveBackend seam. startWorker now
installs a WorkerCurveBackend that maps the six curve operations onto the
worker message protocol (ECDHE->calculateAgreement,
Ed25519Sign->calculateSignature, Ed25519Verify->verifySignature), so all
OMEMO crypto runs off the main thread once a worker is started.

Fallback is transport-aware. A worker-level failure (bad URL, CSP block,
script load error, crash) rejects in-flight jobs, logs once, latches the
active backend to the local one, and completes the operation there.
Ship a second build, libomemo.js/worker-client, that has the same public
API as the default build but no bundled WebAssembly.

Rollup aliases the wasm-backed ./curve to a stub whose methods reject with
a clear "call startWorker() first" error.
Also document worker offload and worker-client build and bump to 2.2.0
@jcbrand
jcbrand merged commit ce32f89 into master Aug 4, 2026
3 checks passed
@jcbrand
jcbrand deleted the wasm-single-file-node-esm branch August 4, 2026 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant