Skip to main content

c2pa-wasm

The WebAssembly wrapper for c2pa-rs that powers c2pa-web, built with wasm-bindgen. While these bindings can be used directly, most users will prefer the convenience of c2pa-web.

Installation​

npm install @contentauth/c2pa-wasm

Reusing settings contexts​

Direct WASM callers can create new WasmContext(settingsJson) once, then pass the owned, WASM-instance-local handle to WasmReader.fromBlobWithContext, WasmReader.fromBlobFragmentWithContext, WasmBuilder.newWithContext, WasmBuilder.fromJsonWithContext, or WasmBuilder.fromArchiveWithContext. These constructors do not transfer settings JSON into WASM again.

There is no global WASM context registry or implicit cache. The caller owns the handle and must call context.free() when it is no longer needed, just as with Readers and Builders. Each constructor takes its own native shared reference; freeing the handle does not invalidate existing objects or pending Reader Promises. Native settings are released when the last owner is freed. Each Reader and Builder retains independent manifest state.

const context = new WasmContext(settingsJson);
const pendingReader = WasmReader.fromBlobWithContext(
'image/jpeg',
blob,
context
);
const builder = WasmBuilder.newWithContext(context);
context.free(); // Safe even before pendingReader settles.
const reader = await pendingReader;
// Use reader and builder, then free each when finished.
reader.free();
builder.free();

c2pa-web owns a bounded cache of these handles inside each worker and frees them on eviction. Direct WASM callers choose their own handle lifetimes. All existing JSON-based constructors, including fromBytes, remain supported; those entry points parse their supplied settings per call rather than retaining them globally.

Development​

Prerequisites​

Ensure the repo-wide prerequisites are installed:

See the contributor prerequisites for details.

Then, install all of the following prerequisites:

Rust​

The minimum supported Rust version is 1.96.0.

To install Rust via rustup:

curl https://sh.rustup.rs -sSf | sh

For more information, see Install Rust.

Additionally, you must install the wasm32-unknown-unknown target:

rustup target add wasm32-unknown-unknown

wasm-bindgen-cli​

The wasm-bindgen-cli provides options to tweak JavaScript generated by wasm-bindgen.

For more information, see the wasm-bindgen-cli documentation.

cargo install wasm-bindgen-cli@0.2.129
info

The version of wasm-bindgen-cli must match the version of the wasm-bindgen dependency in Cargo.toml. A version mismatch will cause build failures with errors like "rust Wasm file schema version: X.X.X, this binary schema version: Y.Y.Y". If you encounter this, check Cargo.toml for the correct version and reinstall the CLI accordingly.

wasm-pack​

Wasm-pack is a tool for building and working with Rust-generated WebAssembly that interoperates with JavaScript.

cargo install wasm-pack@0.13.1

For more information, see the wasm-pack documentation.

Building​

To build the library:

nx build c2pa-wasm

This will execute the commands defined in the nx-wasm-bindgen executor to produce the final output in the pkg/ directory.

Testing​

The library relies on wasm-pack to run its tests. As a general principle, the majority of test coverage will come from c2pa-web's test suite.

To run the tests:

nx test c2pa-wasm