feat: run Curve25519 crypto in a Web Worker, with a wasm-free worker-client build - #47
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.