Automerge Documentation

repository·main·Indexed 27 days ago

https://github.com/automerge/automerge

A library providing CRDT implementations, a compact compression format, and a sync protocol to support local-first applications. It includes a JavaScript implementation (@automerge/automerge) backed by WebAssembly, a core Rust implementation, and a C library (automerge-c) via FFI bindings.

Tokens
26.7K
Snippets
62
Records
186
Agent score
91%

What's inside Automerge

  1. Overview of Automerge

    main

    Automerge is a library providing fast implementations of several different CRDTs (Conflict-free Replicated Data Types), a compact compression format for these CRDTs, and a sync protocol for efficient network transmission. It is designed to support local-first applications by providing persistence mechanisms that abstract away distributed computing complexities.

    Key components include:

    • JavaScript: A stable package @automerge/automerge is available for web and Node.js environments.
    • Rust: A core implementation that serves as a performant backend, often compiled to WebAssembly.
    • C: A C library available in rust/automerge-c via FFI bindings.
  2. Validate cross-platform interop using the exemplar file

    main

    To validate that different Automerge implementations (e.g., Rust, JavaScript, WASM) correctly interpret values across platforms, use the exemplar file. This file contains a document with a diverse set of data types including scalars, text objects, timestamps, and counters.

    The exemplar document contains the following key-value pairs:

    • title: String ("Hello 🇬🇧👨‍👨‍👧‍👦😀")
    • notes: Automerge Text object ("🇬🇧👨‍👨‍👧‍👦😀")
    • timestamp: ISO8601 timestamp (1941-04-26T08:17:00.123Z)
    • location: URL ("https://automerge.org/")
    • counter: Automerge counter (5)
    • int: Integer (-4)
    • uint: Unsigned integer (18446744073709551615)
    • fp: Floating point (3.14159267)
    • bytes: Hex data (0x856f4a83)
    • bool: Boolean (true)
  3. Configure Webpack 5 for browser usage

    main

    Because @automerge/automerge wraps a Rust core compiled to WebAssembly, browsers require a bundler to handle the WebAssembly modules. If using Webpack 5, you must enable the asyncWebAssembly experiment and adjust performance hints to prevent warnings regarding the WASM blob size.

    module.exports = {
      ...
      experiments: { asyncWebAssembly: true },
      performance: {       // we dont want the wasm blob to generate warnings
         hints: false,
         maxEntrypointSize: 512000,
         maxAssetSize: 512000
      }
    };
  4. Migrate from Hexane v0 `ColumnData<C>` to v1

    main

    The ColumnData<C> API from version 0.2 has been removed. In v1, the API is available at the crate root. Use hexane::PrefixColumn<T> if you need prefix sums (counting, seeking by accumulated value, range sums) or hexane::Column<T> if you only need random access, iteration, and splicing without the overhead of a Fenwick tree (BIT).

    Type Requirements: PrefixColumn<T> requires T: PrefixValue. Supported types include bool, u32, u64, i64, Option<u32>, Option<u64>, Option<i64>, NonZeroU32, etc.

  5. Install automerge-c

    main

    To build and install the automerge-c library, ensure you have the required prerequisites installed, then use CMake to build and install it to a directory of your choice (e.g., /usr/local).

    Prerequisites

    • Cargo >= 1.71.0
    • CMake >= 3.25
    • CMocka >= 1.1.5
    • Doxygen >= 1.9.1
    • Ninja >= 1.10.1

    Note: It is highly recommended to use the installation process because generated headers like automerge-c/config.h and automerge-c/utils/enum_string.h are only guaranteed to be found in their installed locations.

    cmake -E make_directory automerge-c/build
    cmake -S automerge-c -B automerge-c/build
    cmake --build automerge-c/build
    cmake --install automerge-c/build --prefix "/usr/local"
  6. Build and test the Automerge JavaScript package

    main

    To build and test the Automerge JavaScript package from source, run the following commands in the javascript directory. Note that if you modify the Rust code in ../rust/*, you must re-run the build command.

    npm install
    npm run build
    npm test
  7. Develop with Automerge using Nix

    main

    If you have Nix installed, you can use the provided Nix flake to set up a development environment with all dependencies pre-configured. Use nix develop to enter the shell, which provides various helper scripts for building different targets (Deno, Node.js, WASI, etc.).

    $ nix develop