Skip to content

Latest commit

 

History

History
95 lines (71 loc) · 3.52 KB

README.md

File metadata and controls

95 lines (71 loc) · 3.52 KB

ycpp

Documentation

A Yjs-compatible CRDT library in C++23, for desktops and microcontrollers.

ycpp reads and writes Yjs 13 updates. It talks to any Yjs peer: Yjs in a browser, Yrs, pycrdt, or a y-websocket server. Compatibility is tested, not assumed. Random multi-peer sessions recorded with real Yjs are replayed through ycpp, and the encoded documents must match byte for byte.

Every Yjs shared type is there: Map, Array, Text (with formatting and embeds), the Xml types and subdocuments. So are v1 and v2 updates, the sync and awareness protocols, snapshots, relative positions and the undo manager. A Python binding with a pycrdt-like API lives in python/.

Documentation: ycpp.readthedocs.io, with a user guide and the design of the internals. The sources are in docs/.

Design points

  • C++23. No exceptions, no RTTI, no threads, no iostreams. Errors are std::expected.
  • Memory comes from a std::pmr::memory_resource you choose, so a board can use a pool.
  • Every update is treated as untrusted input. Nesting depth is capped (Meson option max_any_depth, default 32). Decoding work grows in proportion to the input, never quadratically. A fuzzer runs on apply_update.
  • Text positions are counted in UTF-16 code units, as Yjs counts them. Strings are stored as UTF-8.

Use

#include <ycpp/doc.hpp>
#include <ycpp/map.hpp>

ycpp::Doc doc(1);  // the client id: unique per replica, as in Yjs
auto settings = doc.map("settings");
settings.set("gain", ycpp::Any{0.5});

// A plain function and a context pointer: no std::function, so no allocation.
auto sub = doc.on_update([](void* peer, std::span<const ycpp::byte> update) {
    /* send `update` to the peer */
}, &my_peer);
// ...and on the other side:
// other.apply_update(update);

Sync

ycpp/sync.hpp speaks the y-protocols sync protocol over any transport that carries binary frames, so a ycpp document talks to a browser Yjs, a y-websocket server or pycrdt:

#include <ycpp/sync.hpp>

// Every message goes out in a frame that starts with its type (sync or awareness).
void send_sync(std::span<const ycpp::byte> message) {
  ycpp::Encoder frame;
  ycpp::write_message(frame, ycpp::MessageType::sync, message);
  send(frame.bytes());
}

// Once connected: say what we have, and the peer answers with what we miss.
ycpp::Encoder hello;
ycpp::write_sync_step1(hello, doc);
send_sync(hello.bytes());

// Every frame from the wire: apply it, and send the reply back when there is one.
// The reply is already a whole frame.
auto reply = ycpp::handle_message(frame, doc);
if (reply && !reply->empty()) {
  send(*reply);
}

ycpp/awareness.hpp carries the ephemeral per-client state (cursor, selection, user name) that goes with it. Both take a frame or an update as untrusted input: malformed bytes come back as an Error, never as a partially applied document.

Build

meson setup builddir
ninja -C builddir
meson test -C builddir

-Dfuzz=true builds the libFuzzer target with clang. scripts/size_check.sh cross-compiles a map-only build for Cortex-M7 and reports its flash size.

The differential tests replay fixtures committed under tests/fixtures. To regenerate them, run npm ci in tests/differential, then run its gen_*.mjs scripts with Node.

License

MIT. ycpp ports algorithms from Yjs and Yrs, which are both MIT-licensed; see LICENSE.