Compare commits

..

No commits in common. "v0.0.2" and "main" have entirely different histories.
v0.0.2 ... main

437 changed files with 343329 additions and 1559 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

17
.github/workflows/advisory-cron.yaml vendored Normal file
View file

@ -0,0 +1,17 @@
name: Advisories
on:
schedule:
- cron: '0 18 * * *'
jobs:
cargo-deny:
runs-on: ubuntu-latest
strategy:
matrix:
checks:
- advisories
- bans licenses sources
steps:
- uses: actions/checkout@v2
- uses: EmbarkStudios/cargo-deny-action@v1
with:
command: check ${{ matrix.checks }}

177
.github/workflows/ci.yaml vendored Normal file
View file

@ -0,0 +1,177 @@
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.67.0
default: true
components: rustfmt
- uses: Swatinem/rust-cache@v1
- run: ./scripts/ci/fmt
shell: bash
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.67.0
default: true
components: clippy
- uses: Swatinem/rust-cache@v1
- run: ./scripts/ci/lint
shell: bash
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.67.0
default: true
- uses: Swatinem/rust-cache@v1
- name: Build rust docs
run: ./scripts/ci/rust-docs
shell: bash
- name: Install doxygen
run: sudo apt-get install -y doxygen
shell: bash
cargo-deny:
runs-on: ubuntu-latest
strategy:
matrix:
checks:
- advisories
- bans licenses sources
continue-on-error: ${{ matrix.checks == 'advisories' }}
steps:
- uses: actions/checkout@v2
- uses: EmbarkStudios/cargo-deny-action@v1
with:
arguments: '--manifest-path ./rust/Cargo.toml'
command: check ${{ matrix.checks }}
wasm_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install wasm-bindgen-cli
run: cargo install wasm-bindgen-cli wasm-opt
- name: Install wasm32 target
run: rustup target add wasm32-unknown-unknown
- name: run tests
run: ./scripts/ci/wasm_tests
deno_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Install wasm-bindgen-cli
run: cargo install wasm-bindgen-cli wasm-opt
- name: Install wasm32 target
run: rustup target add wasm32-unknown-unknown
- name: run tests
run: ./scripts/ci/deno_tests
js_fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: install
run: yarn global add prettier
- name: format
run: prettier -c javascript/.prettierrc javascript
js_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install wasm-bindgen-cli
run: cargo install wasm-bindgen-cli wasm-opt
- name: Install wasm32 target
run: rustup target add wasm32-unknown-unknown
- name: run tests
run: ./scripts/ci/js_tests
cmake_build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2023-01-26
default: true
- uses: Swatinem/rust-cache@v1
- name: Install CMocka
run: sudo apt-get install -y libcmocka-dev
- name: Install/update CMake
uses: jwlawson/actions-setup-cmake@v1.12
with:
cmake-version: latest
- name: Install rust-src
run: rustup component add rust-src
- name: Build and test C bindings
run: ./scripts/ci/cmake-build Release Static
shell: bash
linux:
runs-on: ubuntu-latest
strategy:
matrix:
toolchain:
- 1.67.0
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.toolchain }}
default: true
- uses: Swatinem/rust-cache@v1
- run: ./scripts/ci/build-test
shell: bash
macos:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.67.0
default: true
- uses: Swatinem/rust-cache@v1
- run: ./scripts/ci/build-test
shell: bash
windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.67.0
default: true
- uses: Swatinem/rust-cache@v1
- run: ./scripts/ci/build-test
shell: bash

52
.github/workflows/docs.yaml vendored Normal file
View file

@ -0,0 +1,52 @@
on:
push:
branches:
- main
name: Documentation
jobs:
deploy-docs:
concurrency: deploy-docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Cache
uses: Swatinem/rust-cache@v1
- name: Clean docs dir
run: rm -rf docs
shell: bash
- name: Clean Rust docs dir
uses: actions-rs/cargo@v1
with:
command: clean
args: --manifest-path ./rust/Cargo.toml --doc
- name: Build Rust docs
uses: actions-rs/cargo@v1
with:
command: doc
args: --manifest-path ./rust/Cargo.toml --workspace --all-features --no-deps
- name: Move Rust docs
run: mkdir -p docs && mv rust/target/doc/* docs/.
shell: bash
- name: Configure root page
run: echo '<meta http-equiv="refresh" content="0; url=automerge">' > docs/index.html
- name: Deploy docs
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs

214
.github/workflows/release.yaml vendored Normal file
View file

@ -0,0 +1,214 @@
name: Release
on:
push:
branches:
- main
jobs:
check_if_wasm_version_upgraded:
name: Check if WASM version has been upgraded
runs-on: ubuntu-latest
outputs:
wasm_version: ${{ steps.version-updated.outputs.current-package-version }}
wasm_has_updated: ${{ steps.version-updated.outputs.has-updated }}
steps:
- uses: JiPaix/package-json-updated-action@v1.0.5
id: version-updated
with:
path: rust/automerge-wasm/package.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish-wasm:
name: Publish WASM package
runs-on: ubuntu-latest
needs:
- check_if_wasm_version_upgraded
# We create release only if the version in the package.json has been upgraded
if: needs.check_if_wasm_version_upgraded.outputs.wasm_has_updated == 'true'
steps:
- uses: actions/setup-node@v3
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
- uses: denoland/setup-deno@v1
- uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ github.ref }}
- name: Get rid of local github workflows
run: rm -r .github/workflows
- name: Remove tmp_branch if it exists
run: git push origin :tmp_branch || true
- run: git checkout -b tmp_branch
- name: Install wasm-bindgen-cli
run: cargo install wasm-bindgen-cli wasm-opt
- name: Install wasm32 target
run: rustup target add wasm32-unknown-unknown
- name: run wasm js tests
id: wasm_js_tests
run: ./scripts/ci/wasm_tests
- name: run wasm deno tests
id: wasm_deno_tests
run: ./scripts/ci/deno_tests
- name: build release
id: build_release
run: |
npm --prefix $GITHUB_WORKSPACE/rust/automerge-wasm run release
- name: Collate deno release files
if: steps.wasm_js_tests.outcome == 'success' && steps.wasm_deno_tests.outcome == 'success'
run: |
mkdir $GITHUB_WORKSPACE/deno_wasm_dist
cp $GITHUB_WORKSPACE/rust/automerge-wasm/deno/* $GITHUB_WORKSPACE/deno_wasm_dist
cp $GITHUB_WORKSPACE/rust/automerge-wasm/index.d.ts $GITHUB_WORKSPACE/deno_wasm_dist
cp $GITHUB_WORKSPACE/rust/automerge-wasm/README.md $GITHUB_WORKSPACE/deno_wasm_dist
cp $GITHUB_WORKSPACE/rust/automerge-wasm/LICENSE $GITHUB_WORKSPACE/deno_wasm_dist
sed -i '1i /// <reference types="./index.d.ts" />' $GITHUB_WORKSPACE/deno_wasm_dist/automerge_wasm.js
- name: Create npm release
if: steps.wasm_js_tests.outcome == 'success' && steps.wasm_deno_tests.outcome == 'success'
run: |
if [ "$(npm --prefix $GITHUB_WORKSPACE/rust/automerge-wasm show . version)" = "$VERSION" ]; then
echo "This version is already published"
exit 0
fi
EXTRA_ARGS="--access public"
if [[ $VERSION == *"alpha."* ]] || [[ $VERSION == *"beta."* ]] || [[ $VERSION == *"rc."* ]]; then
echo "Is pre-release version"
EXTRA_ARGS="$EXTRA_ARGS --tag next"
fi
if [ "$NODE_AUTH_TOKEN" = "" ]; then
echo "Can't publish on NPM, You need a NPM_TOKEN secret."
false
fi
npm publish $GITHUB_WORKSPACE/rust/automerge-wasm $EXTRA_ARGS
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
VERSION: ${{ needs.check_if_wasm_version_upgraded.outputs.wasm_version }}
- name: Commit wasm deno release files
run: |
git config --global user.name "actions"
git config --global user.email actions@github.com
git add $GITHUB_WORKSPACE/deno_wasm_dist
git commit -am "Add deno release files"
git push origin tmp_branch
- name: Tag wasm release
if: steps.wasm_js_tests.outcome == 'success' && steps.wasm_deno_tests.outcome == 'success'
uses: softprops/action-gh-release@v1
with:
name: Automerge Wasm v${{ needs.check_if_wasm_version_upgraded.outputs.wasm_version }}
tag_name: js/automerge-wasm-${{ needs.check_if_wasm_version_upgraded.outputs.wasm_version }}
target_commitish: tmp_branch
generate_release_notes: false
draft: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Remove tmp_branch
run: git push origin :tmp_branch
check_if_js_version_upgraded:
name: Check if JS version has been upgraded
runs-on: ubuntu-latest
outputs:
js_version: ${{ steps.version-updated.outputs.current-package-version }}
js_has_updated: ${{ steps.version-updated.outputs.has-updated }}
steps:
- uses: JiPaix/package-json-updated-action@v1.0.5
id: version-updated
with:
path: javascript/package.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish-js:
name: Publish JS package
runs-on: ubuntu-latest
needs:
- check_if_js_version_upgraded
- check_if_wasm_version_upgraded
- publish-wasm
# We create release only if the version in the package.json has been upgraded and after the WASM release
if: |
(always() && ! cancelled()) &&
(needs.publish-wasm.result == 'success' || needs.publish-wasm.result == 'skipped') &&
needs.check_if_js_version_upgraded.outputs.js_has_updated == 'true'
steps:
- uses: actions/setup-node@v3
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
- uses: denoland/setup-deno@v1
- uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ github.ref }}
- name: Get rid of local github workflows
run: rm -r .github/workflows
- name: Remove js_tmp_branch if it exists
run: git push origin :js_tmp_branch || true
- run: git checkout -b js_tmp_branch
- name: check js formatting
run: |
yarn global add prettier
prettier -c javascript/.prettierrc javascript
- name: run js tests
id: js_tests
run: |
cargo install wasm-bindgen-cli wasm-opt
rustup target add wasm32-unknown-unknown
./scripts/ci/js_tests
- name: build js release
id: build_release
run: |
npm --prefix $GITHUB_WORKSPACE/javascript run build
- name: build js deno release
id: build_deno_release
run: |
VERSION=$WASM_VERSION npm --prefix $GITHUB_WORKSPACE/javascript run deno:build
env:
WASM_VERSION: ${{ needs.check_if_wasm_version_upgraded.outputs.wasm_version }}
- name: run deno tests
id: deno_tests
run: |
npm --prefix $GITHUB_WORKSPACE/javascript run deno:test
- name: Collate deno release files
if: steps.js_tests.outcome == 'success' && steps.deno_tests.outcome == 'success'
run: |
mkdir $GITHUB_WORKSPACE/deno_js_dist
cp $GITHUB_WORKSPACE/javascript/deno_dist/* $GITHUB_WORKSPACE/deno_js_dist
- name: Create npm release
if: steps.js_tests.outcome == 'success' && steps.deno_tests.outcome == 'success'
run: |
if [ "$(npm --prefix $GITHUB_WORKSPACE/javascript show . version)" = "$VERSION" ]; then
echo "This version is already published"
exit 0
fi
EXTRA_ARGS="--access public"
if [[ $VERSION == *"alpha."* ]] || [[ $VERSION == *"beta."* ]] || [[ $VERSION == *"rc."* ]]; then
echo "Is pre-release version"
EXTRA_ARGS="$EXTRA_ARGS --tag next"
fi
if [ "$NODE_AUTH_TOKEN" = "" ]; then
echo "Can't publish on NPM, You need a NPM_TOKEN secret."
false
fi
npm publish $GITHUB_WORKSPACE/javascript $EXTRA_ARGS
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
VERSION: ${{ needs.check_if_js_version_upgraded.outputs.js_version }}
- name: Commit js deno release files
run: |
git config --global user.name "actions"
git config --global user.email actions@github.com
git add $GITHUB_WORKSPACE/deno_js_dist
git commit -am "Add deno js release files"
git push origin js_tmp_branch
- name: Tag JS release
if: steps.js_tests.outcome == 'success' && steps.deno_tests.outcome == 'success'
uses: softprops/action-gh-release@v1
with:
name: Automerge v${{ needs.check_if_js_version_upgraded.outputs.js_version }}
tag_name: js/automerge-${{ needs.check_if_js_version_upgraded.outputs.js_version }}
target_commitish: js_tmp_branch
generate_release_notes: false
draft: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Remove js_tmp_branch
run: git push origin :js_tmp_branch

8
.gitignore vendored
View file

@ -1,4 +1,6 @@
/.direnv
perf.*
/Cargo.lock
build/
.vim/*
/target
**/*.rs.bk
Cargo.lock
libtest.rmeta

View file

@ -1,24 +0,0 @@
language: rust
rust:
- stable
- beta
- nightly
cache: cargo
before_script:
- rustup component add clippy
- rustup component add rustfmt
script:
- cargo fmt -- --check
- cargo clippy --all-targets --all-features -- -D warnings
- cargo test
jobs:
allow_failures:
- rust: nightly
fast_finish: true
deploy:
provider: cargo
on:
tags: true
condition: "$TRAVIS_RUST_VERSION = stable"
token:
secure: FWmUT2NJTcy3ccw8B1RYgvlg5SxnkEAeBU2hxXeKLmEBAjzhVPVHjwaQ5RktMRHsyKYJEfDpLD0EHUZknhyDxzCuUKzKYlGgRmtlnsCKS+gDM4j88e/OEnDvxZ2d8ag3Jp8+3GCvv2yjUHFs2JpclqR4ib8LmL6d6x+1+1uxaMOgaDhxQCDLV0eZwX5mTdGAWJl/CpxziFXHYN8/j+e58dJgWN6TUO6BBZeZmkp4xQ6iggEUgIKLLYynG5cM2XtS/j/qbL2ObloamIv9p0SNtj8wTQupJZW3JPBc77gimfeXVQd2+4B/31lJ3GW1310gVBZ9EA7BTbC3M3AkHJFPUIgfEn803zrZhm4WxGg2B+2kENWPpSRUMjhxaPuxAVStHOBl2WSsQTmTRrSUf1nvZUdixTARr6BkKakiNPqts7X/HbxE0cxkk5gtobTyNb4HFbaM/8449U8+KbX7mDXv50FGmRrKxkepOzfRdoEz4h9LnCFWweyle2bpFCQlnro+1SnBRSVmH+c1YUZbIl+He53GUEAwObcHGk+TlhVCGMtmGj/g1THOf4VcWh8C3XoO2yWIu9FoJKvJbd7qm0+dOv+QY8fxgrs4JRSSnt8rXBXhxLKe/ZXl5fHOmLca8T6i/PRfbQ9AzFSCPcz8o4hNO/lVQPSrNrkvxSF39buuYGU=

View file

@ -1,18 +0,0 @@
[package]
name = "automerge"
version = "0.0.2"
authors = ["Alex Good <alex@memoryandthought.me>"]
edition = "2018"
license = "MIT"
homepage = "https://github.com/alexjg/automerge-rs"
repository = "https://github.com/alexjg/automerge-rs"
categories = ["data-structures"]
description = "Rust implementation of the Automerge replicated JSON datatype"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "automerge"
[dependencies]
serde = { version = "^1.0", features=["derive"] }
serde_json = "^1.0"

20
LICENSE
View file

@ -1,7 +1,19 @@
Copyright 2019 Alex Good
Copyright (c) 2019-2021 the Automerge contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

157
README.md
View file

@ -1,34 +1,147 @@
# Automerge
[![docs](https://docs.rs/automerge/badge.svg)](docs.rs/automerge)
<img src='./img/sign.svg' width='500' alt='Automerge logo' />
This is a very early, very much work in progress implementation of [automerge](https://github.com/automerge/automerge) in rust. At the moment it barely implements a read only view of operations received, with very little testing that it works. Objectives for it are:
[![homepage](https://img.shields.io/badge/homepage-published-informational)](https://automerge.org/)
[![main docs](https://img.shields.io/badge/docs-main-informational)](https://automerge.org/automerge-rs/automerge/)
[![ci](https://github.com/automerge/automerge-rs/actions/workflows/ci.yaml/badge.svg)](https://github.com/automerge/automerge-rs/actions/workflows/ci.yaml)
[![docs](https://github.com/automerge/automerge-rs/actions/workflows/docs.yaml/badge.svg)](https://github.com/automerge/automerge-rs/actions/workflows/docs.yaml)
- Full read and write replication
- `no_std` support to make it easy to use in WASM environments
- Model based testing to ensure compatibility with the JS library
Automerge is a library which provides fast implementations of several different
CRDTs, a compact compression format for these CRDTs, and a sync protocol for
efficiently transmitting those changes over the network. The objective of the
project is to support [local-first](https://www.inkandswitch.com/local-first/) applications in the same way that relational
databases support server applications - by providing mechanisms for persistence
which allow application developers to avoid thinking about hard distributed
computing problems. Automerge aims to be PostgreSQL for your local-first app.
If you're looking for documentation on the JavaScript implementation take a look
at https://automerge.org/docs/hello/. There are other implementations in both
Rust and C, but they are earlier and don't have documentation yet. You can find
them in `rust/automerge` and `rust/automerge-c` if you are comfortable
reading the code and tests to figure out how to use them.
## How to use
If you're familiar with CRDTs and interested in the design of Automerge in
particular take a look at https://automerge.org/docs/how-it-works/backend/
You'll need to export changes from automerge as JSON rather than using the encoding that `Automerge.save` uses. So first do this:
Finally, if you want to talk to us about this project please [join the
Slack](https://join.slack.com/t/automerge/shared_invite/zt-e4p3760n-kKh7r3KRH1YwwNfiZM8ktw)
```javascript
const doc = <your automerge document>
const changes = Automerge.getHistory(doc).map(h => h.change)
console.log(JSON.stringify(changes, null, 4))
## Status
This project is formed of a core Rust implementation which is exposed via FFI in
javascript+WASM, C, and soon other languages. Alex
([@alexjg](https://github.com/alexjg/)]) is working full time on maintaining
automerge, other members of Ink and Switch are also contributing time and there
are several other maintainers. The focus is currently on shipping the new JS
package. We expect to be iterating the API and adding new features over the next
six months so there will likely be several major version bumps in all packages
in that time.
In general we try and respect semver.
### JavaScript
A stable release of the javascript package is currently available as
`@automerge/automerge@2.0.0` where. pre-release verisions of the `2.0.1` are
available as `2.0.1-alpha.n`. `2.0.1*` packages are also available for Deno at
https://deno.land/x/automerge
### Rust
The rust codebase is currently oriented around producing a performant backend
for the Javascript wrapper and as such the API for Rust code is low level and
not well documented. We will be returning to this over the next few months but
for now you will need to be comfortable reading the tests and asking questions
to figure out how to use it. If you are looking to build rust applications which
use automerge you may want to look into
[autosurgeon](https://github.com/alexjg/autosurgeon)
## Repository Organisation
- `./rust` - the rust rust implementation and also the Rust components of
platform specific wrappers (e.g. `automerge-wasm` for the WASM API or
`automerge-c` for the C FFI bindings)
- `./javascript` - The javascript library which uses `automerge-wasm`
internally but presents a more idiomatic javascript interface
- `./scripts` - scripts which are useful to maintenance of the repository.
This includes the scripts which are run in CI.
- `./img` - static assets for use in `.md` files
## Building
To build this codebase you will need:
- `rust`
- `node`
- `yarn`
- `cmake`
- `cmocka`
You will also need to install the following with `cargo install`
- `wasm-bindgen-cli`
- `wasm-opt`
- `cargo-deny`
And ensure you have added the `wasm32-unknown-unknown` target for rust cross-compilation.
The various subprojects (the rust code, the wrapper projects) have their own
build instructions, but to run the tests that will be run in CI you can run
`./scripts/ci/run`.
### For macOS
These instructions worked to build locally on macOS 13.1 (arm64) as of
Nov 29th 2022.
```bash
# clone the repo
git clone https://github.com/automerge/automerge-rs
cd automerge-rs
# install rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# install homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# install cmake, node, cmocka
brew install cmake node cmocka
# install yarn
npm install --global yarn
# install javascript dependencies
yarn --cwd ./javascript
# install rust dependencies
cargo install wasm-bindgen-cli wasm-opt cargo-deny
# get nightly rust to produce optimized automerge-c builds
rustup toolchain install nightly
rustup component add rust-src --toolchain nightly
# add wasm target in addition to current architecture
rustup target add wasm32-unknown-unknown
# Run ci script
./scripts/ci/run
```
Now you can load these changes into automerge like so:
If your build fails to find `cmocka.h` you may need to teach it about homebrew's
installation location:
```rust,no_run
extern crate automerge;
fn main() {
let changes: Vec<automerge::Change> = serde_json::from_str("<paste the changes JSON here>").unwrap();
let document = automerge::Document::load(changes).unwrap();
let state: serde_json::Value = document.state().unwrap();
println!("{:?}", state);
}
```
export CPATH=/opt/homebrew/include
export LIBRARY_PATH=/opt/homebrew/lib
./scripts/ci/run
```
## Contributing
Please try and split your changes up into relatively independent commits which
change one subsystem at a time and add good commit messages which describe what
the change is and why you're making it (err on the side of longer commit
messages). `git blame` should give future maintainers a good idea of why
something is the way it is.

94
flake.lock generated Normal file
View file

@ -0,0 +1,94 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"locked": {
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1669542132,
"narHash": "sha256-DRlg++NJAwPh8io3ExBJdNW7Djs3plVI5jgYQ+iXAZQ=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "a115bb9bd56831941be3776c8a94005867f316a7",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1665296151,
"narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1669775522,
"narHash": "sha256-6xxGArBqssX38DdHpDoPcPvB/e79uXyQBwpBcaO/BwY=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "3158e47f6b85a288d12948aeb9a048e0ed4434d6",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

69
flake.nix Normal file
View file

@ -0,0 +1,69 @@
{
description = "automerge-rs";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs = {
self,
nixpkgs,
flake-utils,
rust-overlay,
}:
flake-utils.lib.eachDefaultSystem
(system: let
pkgs = import nixpkgs {
overlays = [rust-overlay.overlays.default];
inherit system;
};
rust = pkgs.rust-bin.stable.latest.default;
in {
formatter = pkgs.alejandra;
packages = {
deadnix = pkgs.runCommand "deadnix" {} ''
${pkgs.deadnix}/bin/deadnix --fail ${./.}
mkdir $out
'';
};
checks = {
inherit (self.packages.${system}) deadnix;
};
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
(rust.override {
extensions = ["rust-src"];
targets = ["wasm32-unknown-unknown"];
})
cargo-edit
cargo-watch
cargo-criterion
cargo-fuzz
cargo-flamegraph
cargo-deny
crate2nix
wasm-pack
pkgconfig
openssl
gnuplot
nodejs
yarn
deno
# c deps
cmake
cmocka
doxygen
rnix-lsp
nixpkgs-fmt
];
};
});
}

BIN
img/brandmark.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

1
img/brandmark.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80.46 80.46"><defs><style>.cls-1{fill:#fc3;}.cls-1,.cls-2{fill-rule:evenodd;}.cls-2{fill:#2a1e20;}</style></defs><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path class="cls-1" d="M79.59,38.12a3,3,0,0,1,0,4.21L42.34,79.58a3,3,0,0,1-4.22,0L.88,42.33a3,3,0,0,1,0-4.2L38.12.87a3,3,0,0,1,4.22,0"/><path class="cls-2" d="M76.87,38.76,41.71,3.59a2.09,2.09,0,0,0-2.93,0L3.62,38.76a2.07,2.07,0,0,0,0,2.93L38.78,76.85a2.07,2.07,0,0,0,2.93,0L76.87,41.69a2.07,2.07,0,0,0,0-2.93m-2,.79a.93.93,0,0,1,0,1.34l-33.94,34a1,1,0,0,1-1.33,0l-34-33.95a.94.94,0,0,1,0-1.32l34-34a1,1,0,0,1,1.33,0Z"/><path class="cls-2" d="M36.25,32.85v1.71c0,6.35-5.05,11.38-9.51,16.45l4.08,4.07c2.48-2.6,4.72-5.24,5.43-6.19V60.14h7.94V32.88l4.25,1.3a1.68,1.68,0,0,0,2.25-2.24L40.27,16.7,29.75,31.94A1.68,1.68,0,0,0,32,34.18"/></g></g></svg>

After

Width:  |  Height:  |  Size: 885 B

BIN
img/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 KiB

BIN
img/lockup.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

1
img/lockup.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400.72 80.46"><defs><style>.cls-1{fill:#fc3;}.cls-1,.cls-2{fill-rule:evenodd;}.cls-2{fill:#2a1e20;}</style></defs><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path class="cls-1" d="M79.59,38.12a3,3,0,0,1,0,4.21L42.34,79.58a3,3,0,0,1-4.22,0L.88,42.33a3,3,0,0,1,0-4.2L38.12.87a3,3,0,0,1,4.22,0"/><path class="cls-2" d="M76.87,38.76,41.71,3.59a2.09,2.09,0,0,0-2.93,0L3.62,38.76a2.07,2.07,0,0,0,0,2.93L38.78,76.85a2.07,2.07,0,0,0,2.93,0L76.87,41.69a2.07,2.07,0,0,0,0-2.93m-2,.79a.93.93,0,0,1,0,1.34l-33.94,34a1,1,0,0,1-1.33,0l-34-33.95a.94.94,0,0,1,0-1.32l34-34a1,1,0,0,1,1.33,0Z"/><path class="cls-2" d="M36.25,32.85v1.71c0,6.35-5.05,11.38-9.51,16.45l4.08,4.07c2.48-2.6,4.72-5.24,5.43-6.19V60.14h7.94V32.88l4.25,1.3a1.68,1.68,0,0,0,2.25-2.24L40.27,16.7,29.75,31.94A1.68,1.68,0,0,0,32,34.18"/><path d="M124.14,60.08,120.55,50h-17L100,60.08H93.34l15.34-42.61h6.75L131,60.08Zm-9-25.63c-1-3-2.74-8-3.22-9.8-.49,1.83-2,6.7-3.11,9.86l-3.41,9.74H118.6Z"/><path d="M156.7,60.08V57c-1.58,2.32-4.74,3.72-8,3.72-7.43,0-11.38-4.87-11.38-14.31V28.12h6.27V46.2c0,6.45,2.43,8.76,6.57,8.76s6.57-3,6.57-8.15V28.12H163v32Z"/><path d="M187.5,59.29a12.74,12.74,0,0,1-6.15,1.46c-4.44,0-7.18-2.74-7.18-8.46V33.84h-4.56V28.12h4.56V19l6.15-3.29V28.12h7.91v5.72h-7.91V51.19c0,3,1,3.83,3.29,3.83a10,10,0,0,0,4.62-1.27Z"/><path d="M208.08,60.75c-8,0-14.06-6.64-14.06-16.62,0-10.47,6.2-16.68,14.24-16.68S222.5,34,222.5,44C222.5,54.54,216.29,60.75,208.08,60.75ZM208,33.42c-4.75,0-7.67,4.2-7.67,10.53,0,7,3.22,10.83,8,10.83s7.85-4.81,7.85-10.65C216.17,37.62,213.07,33.42,208,33.42Z"/><path d="M267.36,60.08V42c0-6.45-2-8.77-6.15-8.77s-6.14,3-6.14,8.16V60.08H248.8V42c0-6.45-2-8.77-6.15-8.77s-6.15,3-6.15,8.16V60.08h-6.27v-32h6.27v3a9,9,0,0,1,7.61-3.71c4.32,0,7.06,1.65,8.76,4.69,2.32-2.86,4.81-4.69,9.8-4.69,7.43,0,11,4.87,11,14.31V60.08Z"/><path d="M308.39,46.32H287.27c.66,6.15,4.13,8.77,8,8.77a11.22,11.22,0,0,0,6.94-2.56l3.71,4a14.9,14.9,0,0,1-11,4.2c-7.48,0-13.81-6-13.81-16.62,0-10.84,5.72-16.68,14-16.68,9.07,0,13.45,7.37,13.45,16C308.57,44.62,308.45,45.65,308.39,46.32Zm-13.7-13.21c-4.2,0-6.76,2.92-7.3,8h14.85C301.93,36.76,299.86,33.11,294.69,33.11Z"/><path d="M333.71,34.76a9.37,9.37,0,0,0-4.81-1.16c-4,0-6.27,2.8-6.27,8.22V60.08h-6.27v-32h6.27v3a8.86,8.86,0,0,1,7.3-3.71,9.22,9.22,0,0,1,5.42,1.34Z"/><path d="M350.45,71.82l-2.14-4.74c9-.43,11-2.86,11-9.5V57c-2.31,2.13-4.93,3.72-8.28,3.72-6.81,0-12.29-5-12.29-17.17,0-10.95,6-16.13,12.6-16.13a11.11,11.11,0,0,1,8,3.65v-3h6.27V57C365.54,66.77,362,71.46,350.45,71.82Zm8.94-34.39c-1.4-1.88-4.32-4.2-7.48-4.2-4.51,0-6.94,3.41-6.94,10.17,0,8,2.55,11.56,7.18,11.56,3,0,5.6-2,7.24-4.07Z"/><path d="M400.54,46.32H379.42c.67,6.15,4.14,8.77,8,8.77a11.22,11.22,0,0,0,6.94-2.56l3.71,4a14.87,14.87,0,0,1-11,4.2c-7.49,0-13.82-6-13.82-16.62,0-10.84,5.72-16.68,14-16.68,9.07,0,13.45,7.37,13.45,16C400.72,44.62,400.6,45.65,400.54,46.32Zm-13.7-13.21c-4.2,0-6.75,2.92-7.3,8h14.85C394.09,36.76,392,33.11,386.84,33.11Z"/></g></g></svg>

After

Width:  |  Height:  |  Size: 3 KiB

BIN
img/sign.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

1
img/sign.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 485 108"><defs><style>.cls-1{fill:#fff;}.cls-2{fill:#fc3;}.cls-3{fill:#2a1e20;fill-rule:evenodd;}</style></defs><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path class="cls-1" d="M465,5a15,15,0,0,1,15,15V88a15,15,0,0,1-15,15H20A15,15,0,0,1,5,88V20A15,15,0,0,1,20,5H465m0-5H20A20,20,0,0,0,0,20V88a20,20,0,0,0,20,20H465a20,20,0,0,0,20-20V20A20,20,0,0,0,465,0Z"/><rect class="cls-2" x="3.7" y="3.7" width="477.6" height="100.6" rx="16.3"/><path class="cls-2" d="M465,5a15,15,0,0,1,15,15V88a15,15,0,0,1-15,15H20A15,15,0,0,1,5,88V20A15,15,0,0,1,20,5H465m0-2.6H20A17.63,17.63,0,0,0,2.4,20V88A17.63,17.63,0,0,0,20,105.6H465A17.63,17.63,0,0,0,482.6,88V20A17.63,17.63,0,0,0,465,2.4Z"/><path d="M465,7.6A12.41,12.41,0,0,1,477.4,20V88A12.41,12.41,0,0,1,465,100.4H20A12.41,12.41,0,0,1,7.6,88V20A12.41,12.41,0,0,1,20,7.6H465M465,5H20A15,15,0,0,0,5,20V88a15,15,0,0,0,15,15H465a15,15,0,0,0,15-15V20A15,15,0,0,0,465,5Z"/><path class="cls-3" d="M106.1,51.48l-34-34a2,2,0,0,0-2.83,0l-34,34a2,2,0,0,0,0,2.82l34,34a2,2,0,0,0,2.83,0l34-34a2,2,0,0,0,0-2.82m-.76.74a.93.93,0,0,1,0,1.34L71.4,87.5a1,1,0,0,1-1.33,0l-34-33.94a.94.94,0,0,1,0-1.32l34-34a1,1,0,0,1,1.33,0Z"/><path class="cls-3" d="M67,45.62V47c0,6.2-5.1,11.11-9.59,16.06l4.11,4C64,64.52,66.28,61.94,67,61V72h8V45.37l4.29,1.27a1.67,1.67,0,0,0,2.27-2.19L71,29.56,60.45,44.45a1.67,1.67,0,0,0,2.27,2.19"/><path d="M162.62,72.74,159,62.64H142l-3.53,10.1h-6.63l15.34-42.61h6.75l15.53,42.61Zm-9-25.62c-1-3-2.74-8-3.22-9.8-.49,1.82-2,6.69-3.11,9.86l-3.41,9.73h13.15Z"/><path d="M195.18,72.74v-3c-1.58,2.31-4.74,3.71-8,3.71-7.43,0-11.38-4.87-11.38-14.3V40.78H182V58.86c0,6.45,2.43,8.77,6.57,8.77s6.57-3,6.57-8.16V40.78h6.27v32Z"/><path d="M226,72a12.74,12.74,0,0,1-6.15,1.46c-4.44,0-7.18-2.74-7.18-8.46V46.51h-4.56V40.78h4.56V31.65l6.15-3.28V40.78h7.91v5.73H218.8V63.85c0,3,1,3.84,3.29,3.84a10,10,0,0,0,4.62-1.28Z"/><path d="M246.56,73.41c-8,0-14.06-6.63-14.06-16.62,0-10.47,6.2-16.67,14.24-16.67S261,46.63,261,56.61C261,67.2,254.77,73.41,246.56,73.41Zm-.07-27.33c-4.74,0-7.66,4.2-7.66,10.53,0,7,3.22,10.83,8,10.83s7.85-4.8,7.85-10.65C254.65,50.28,251.55,46.08,246.49,46.08Z"/><path d="M305.84,72.74V54.66c0-6.45-2-8.76-6.15-8.76s-6.14,3-6.14,8.15V72.74h-6.27V54.66c0-6.45-2-8.76-6.15-8.76s-6.15,3-6.15,8.15V72.74h-6.27v-32H275v3a9,9,0,0,1,7.61-3.71c4.32,0,7.06,1.64,8.76,4.68,2.32-2.86,4.81-4.68,9.8-4.68,7.43,0,11,4.86,11,14.3V72.74Z"/><path d="M346.87,59H325.74c.67,6.15,4.14,8.77,8,8.77a11.16,11.16,0,0,0,6.94-2.56l3.71,4a14.86,14.86,0,0,1-11,4.2c-7.48,0-13.81-6-13.81-16.62,0-10.83,5.72-16.67,14-16.67,9.07,0,13.45,7.36,13.45,16C347.05,57.28,346.93,58.31,346.87,59Zm-13.7-13.2c-4.2,0-6.76,2.92-7.3,8h14.85C340.41,49.43,338.34,45.78,333.17,45.78Z"/><path d="M372.19,47.42a9.37,9.37,0,0,0-4.81-1.16c-4,0-6.27,2.8-6.27,8.22V72.74h-6.27v-32h6.27v3a8.86,8.86,0,0,1,7.3-3.71,9.22,9.22,0,0,1,5.42,1.33Z"/><path d="M388.92,84.49l-2.13-4.75c9-.43,11-2.86,11-9.5V69.7c-2.31,2.13-4.93,3.71-8.28,3.71-6.81,0-12.29-5-12.29-17.16,0-11,6-16.13,12.6-16.13a11.07,11.07,0,0,1,8,3.65v-3H404V69.7C404,79.44,400.49,84.12,388.92,84.49Zm8.95-34.39c-1.4-1.89-4.32-4.2-7.48-4.2-4.51,0-6.94,3.41-6.94,10.16,0,8,2.55,11.57,7.18,11.57,3,0,5.6-2,7.24-4.08Z"/><path d="M439,59H417.9c.67,6.15,4.14,8.77,8,8.77a11.16,11.16,0,0,0,6.94-2.56l3.71,4a14.84,14.84,0,0,1-11,4.2c-7.49,0-13.82-6-13.82-16.62,0-10.83,5.72-16.67,14-16.67,9.07,0,13.45,7.36,13.45,16C439.2,57.28,439.08,58.31,439,59Zm-13.7-13.2c-4.2,0-6.75,2.92-7.3,8h14.85C432.57,49.43,430.5,45.78,425.32,45.78Z"/></g></g></svg>

After

Width:  |  Height:  |  Size: 3.5 KiB

View file

@ -0,0 +1,3 @@
{
"replacer": "scripts/denoify-replacer.mjs"
}

2
javascript/.eslintignore Normal file
View file

@ -0,0 +1,2 @@
dist
examples

15
javascript/.eslintrc.cjs Normal file
View file

@ -0,0 +1,15 @@
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
rules: {
"@typescript-eslint/no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
},
}

6
javascript/.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
/node_modules
/yarn.lock
dist
docs/
.vim
deno_dist/

View file

@ -0,0 +1,4 @@
e2e/verdacciodb
dist
docs
deno_dist

4
javascript/.prettierrc Normal file
View file

@ -0,0 +1,4 @@
{
"semi": false,
"arrowParens": "avoid"
}

39
javascript/HACKING.md Normal file
View file

@ -0,0 +1,39 @@
## Architecture
The `@automerge/automerge` package is a set of
[`Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
objects which provide an idiomatic javascript interface built on top of the
lower level `@automerge/automerge-wasm` package (which is in turn built from the
Rust codebase and can be found in `~/automerge-wasm`). I.e. the responsibility
of this codebase is
- To map from the javascript data model to the underlying `set`, `make`,
`insert`, and `delete` operations of Automerge.
- To expose a more convenient interface to functions in `automerge-wasm` which
generate messages to send over the network or compressed file formats to store
on disk
## Building and testing
Much of the functionality of this package depends on the
`@automerge/automerge-wasm` package and frequently you will be working on both
of them at the same time. It would be frustrating to have to push
`automerge-wasm` to NPM every time you want to test a change but I (Alex) also
don't trust `yarn link` to do the right thing here. Therefore, the `./e2e`
folder contains a little yarn package which spins up a local NPM registry. See
`./e2e/README` for details. In brief though:
To build `automerge-wasm` and install it in the local `node_modules`
```bash
cd e2e && yarn install && yarn run e2e buildjs
```
NOw that you've done this you can run the tests
```bash
yarn test
```
If you make changes to the `automerge-wasm` package you will need to re-run
`yarn e2e buildjs`

10
javascript/LICENSE Normal file
View file

@ -0,0 +1,10 @@
MIT License
Copyright 2022, Ink & Switch LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

109
javascript/README.md Normal file
View file

@ -0,0 +1,109 @@
## Automerge
Automerge is a library of data structures for building collaborative
applications, this package is the javascript implementation.
Detailed documentation is available at [automerge.org](http://automerge.org/)
but see the following for a short getting started guid.
## Quickstart
First, install the library.
```
yarn add @automerge/automerge
```
If you're writing a `node` application, you can skip straight to [Make some
data](#make-some-data). If you're in a browser you need a bundler
### Bundler setup
`@automerge/automerge` is a wrapper around a core library which is written in
rust, compiled to WebAssembly and distributed as a separate package called
`@automerge/automerge-wasm`. Browsers don't currently support WebAssembly
modules taking part in ESM module imports, so you must use a bundler to import
`@automerge/automerge` in the browser. There are a lot of bundlers out there, we
have examples for common bundlers in the `examples` folder. Here is a short
example using Webpack 5.
Assuming a standard setup of a new webpack project, you'll need to enable the
`asyncWebAssembly` experiment. In a typical webpack project that means adding
something like this to `webpack.config.js`
```javascript
module.exports = {
...
experiments: { asyncWebAssembly: true },
performance: { // we dont want the wasm blob to generate warnings
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000
}
};
```
### Make some data
Automerge allows to separate threads of execution to make changes to some data
and always be able to merge their changes later.
```javascript
import * as automerge from "@automerge/automerge"
import * as assert from "assert"
let doc1 = automerge.from({
tasks: [
{ description: "feed fish", done: false },
{ description: "water plants", done: false },
],
})
// Create a new thread of execution
let doc2 = automerge.clone(doc1)
// Now we concurrently make changes to doc1 and doc2
// Complete a task in doc2
doc2 = automerge.change(doc2, d => {
d.tasks[0].done = true
})
// Add a task in doc1
doc1 = automerge.change(doc1, d => {
d.tasks.push({
description: "water fish",
done: false,
})
})
// Merge changes from both docs
doc1 = automerge.merge(doc1, doc2)
doc2 = automerge.merge(doc2, doc1)
// Both docs are merged and identical
assert.deepEqual(doc1, {
tasks: [
{ description: "feed fish", done: true },
{ description: "water plants", done: false },
{ description: "water fish", done: false },
],
})
assert.deepEqual(doc2, {
tasks: [
{ description: "feed fish", done: true },
{ description: "water plants", done: false },
{ description: "water fish", done: false },
],
})
```
## Development
See [HACKING.md](./HACKING.md)
## Meta
Copyright 2017present, the Automerge contributors. Released under the terms of the
MIT license (see `LICENSE`).

View file

@ -0,0 +1,12 @@
{
"extends": "../tsconfig.json",
"exclude": [
"../dist/**/*",
"../node_modules",
"../test/**/*",
"../src/**/*.deno.ts"
],
"compilerOptions": {
"outDir": "../dist/cjs"
}
}

View file

@ -0,0 +1,13 @@
{
"extends": "../tsconfig.json",
"exclude": [
"../dist/**/*",
"../node_modules",
"../test/**/*",
"../src/**/*.deno.ts"
],
"emitDeclarationOnly": true,
"compilerOptions": {
"outDir": "../dist"
}
}

View file

@ -0,0 +1,14 @@
{
"extends": "../tsconfig.json",
"exclude": [
"../dist/**/*",
"../node_modules",
"../test/**/*",
"../src/**/*.deno.ts"
],
"compilerOptions": {
"target": "es6",
"module": "es6",
"outDir": "../dist/mjs"
}
}

View file

@ -0,0 +1,10 @@
import * as Automerge from "../deno_dist/index.ts"
Deno.test("It should create, clone and free", () => {
let doc1 = Automerge.init()
let doc2 = Automerge.clone(doc1)
// this is only needed if weakrefs are not supported
Automerge.free(doc1)
Automerge.free(doc2)
})

3
javascript/e2e/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules/
verdacciodb/
htpasswd

70
javascript/e2e/README.md Normal file
View file

@ -0,0 +1,70 @@
#End to end testing for javascript packaging
The network of packages and bundlers we rely on to get the `automerge` package
working is a little complex. We have the `automerge-wasm` package, which the
`automerge` package depends upon, which means that anyone who depends on
`automerge` needs to either a) be using node or b) use a bundler in order to
load the underlying WASM module which is packaged in `automerge-wasm`.
The various bundlers involved are complicated and capricious and so we need an
easy way of testing that everything is in fact working as expected. To do this
we run a custom NPM registry (namely [Verdaccio](https://verdaccio.org/)) and
build the `automerge-wasm` and `automerge` packages and publish them to this
registry. Once we have this registry running we are able to build the example
projects which depend on these packages and check that everything works as
expected.
## Usage
First, install everything:
```
yarn install
```
### Build `automerge-js`
This builds the `automerge-wasm` package and then runs `yarn build` in the
`automerge-js` project with the `--registry` set to the verdaccio registry. The
end result is that you can run `yarn test` in the resulting `automerge-js`
directory in order to run tests against the current `automerge-wasm`.
```
yarn e2e buildjs
```
### Build examples
This either builds or the examples in `automerge-js/examples` or just a subset
of them. Once this is complete you can run the relevant scripts (e.g. `vite dev`
for the Vite example) to check everything works.
```
yarn e2e buildexamples
```
Or, to just build the webpack example
```
yarn e2e buildexamples -e webpack
```
### Run Registry
If you're experimenting with a project which is not in the `examples` folder
you'll need a running registry. `run-registry` builds and publishes
`automerge-js` and `automerge-wasm` and then runs the registry at
`localhost:4873`.
```
yarn e2e run-registry
```
You can now run `yarn install --registry http://localhost:4873` to experiment
with the built packages.
## Using the `dev` build of `automerge-wasm`
All the commands above take a `-p` flag which can be either `release` or
`debug`. The `debug` builds with additional debug symbols which makes errors
less cryptic.

534
javascript/e2e/index.ts Normal file
View file

@ -0,0 +1,534 @@
import { once } from "events"
import { setTimeout } from "timers/promises"
import { spawn, ChildProcess } from "child_process"
import * as child_process from "child_process"
import {
command,
subcommands,
run,
array,
multioption,
option,
Type,
} from "cmd-ts"
import * as path from "path"
import * as fsPromises from "fs/promises"
import fetch from "node-fetch"
const VERDACCIO_DB_PATH = path.normalize(`${__dirname}/verdacciodb`)
const VERDACCIO_CONFIG_PATH = path.normalize(`${__dirname}/verdaccio.yaml`)
const AUTOMERGE_WASM_PATH = path.normalize(
`${__dirname}/../../rust/automerge-wasm`
)
const AUTOMERGE_JS_PATH = path.normalize(`${__dirname}/..`)
const EXAMPLES_DIR = path.normalize(path.join(__dirname, "../", "examples"))
// The different example projects in "../examples"
type Example = "webpack" | "vite" | "create-react-app"
// Type to parse strings to `Example` so the types line up for the `buildExamples` commmand
const ReadExample: Type<string, Example> = {
async from(str) {
if (str === "webpack") {
return "webpack"
} else if (str === "vite") {
return "vite"
} else if (str === "create-react-app") {
return "create-react-app"
} else {
throw new Error(`Unknown example type ${str}`)
}
},
}
type Profile = "dev" | "release"
const ReadProfile: Type<string, Profile> = {
async from(str) {
if (str === "dev") {
return "dev"
} else if (str === "release") {
return "release"
} else {
throw new Error(`Unknown profile ${str}`)
}
},
}
const buildjs = command({
name: "buildjs",
args: {
profile: option({
type: ReadProfile,
long: "profile",
short: "p",
defaultValue: () => "dev" as Profile,
}),
},
handler: ({ profile }) => {
console.log("building js")
withPublishedWasm(profile, async (registryUrl: string) => {
await buildAndPublishAutomergeJs(registryUrl)
})
},
})
const buildWasm = command({
name: "buildwasm",
args: {
profile: option({
type: ReadProfile,
long: "profile",
short: "p",
defaultValue: () => "dev" as Profile,
}),
},
handler: ({ profile }) => {
console.log("building automerge-wasm")
withRegistry(buildAutomergeWasm(profile))
},
})
const buildexamples = command({
name: "buildexamples",
args: {
examples: multioption({
long: "example",
short: "e",
type: array(ReadExample),
}),
profile: option({
type: ReadProfile,
long: "profile",
short: "p",
defaultValue: () => "dev" as Profile,
}),
},
handler: ({ examples, profile }) => {
if (examples.length === 0) {
examples = ["webpack", "vite", "create-react-app"]
}
buildExamples(examples, profile)
},
})
const runRegistry = command({
name: "run-registry",
args: {
profile: option({
type: ReadProfile,
long: "profile",
short: "p",
defaultValue: () => "dev" as Profile,
}),
},
handler: ({ profile }) => {
withPublishedWasm(profile, async (registryUrl: string) => {
await buildAndPublishAutomergeJs(registryUrl)
console.log("\n************************")
console.log(` Verdaccio NPM registry is running at ${registryUrl}`)
console.log(" press CTRL-C to exit ")
console.log("************************")
await once(process, "SIGINT")
}).catch(e => {
console.error(`Failed: ${e}`)
})
},
})
const app = subcommands({
name: "e2e",
cmds: {
buildjs,
buildexamples,
buildwasm: buildWasm,
"run-registry": runRegistry,
},
})
run(app, process.argv.slice(2))
async function buildExamples(examples: Array<Example>, profile: Profile) {
await withPublishedWasm(profile, async registryUrl => {
printHeader("building and publishing automerge")
await buildAndPublishAutomergeJs(registryUrl)
for (const example of examples) {
printHeader(`building ${example} example`)
if (example === "webpack") {
const projectPath = path.join(EXAMPLES_DIR, example)
await removeExistingAutomerge(projectPath)
await fsPromises.rm(path.join(projectPath, "yarn.lock"), {
force: true,
})
await spawnAndWait(
"yarn",
[
"--cwd",
projectPath,
"install",
"--registry",
registryUrl,
"--check-files",
],
{ stdio: "inherit" }
)
await spawnAndWait("yarn", ["--cwd", projectPath, "build"], {
stdio: "inherit",
})
} else if (example === "vite") {
const projectPath = path.join(EXAMPLES_DIR, example)
await removeExistingAutomerge(projectPath)
await fsPromises.rm(path.join(projectPath, "yarn.lock"), {
force: true,
})
await spawnAndWait(
"yarn",
[
"--cwd",
projectPath,
"install",
"--registry",
registryUrl,
"--check-files",
],
{ stdio: "inherit" }
)
await spawnAndWait("yarn", ["--cwd", projectPath, "build"], {
stdio: "inherit",
})
} else if (example === "create-react-app") {
const projectPath = path.join(EXAMPLES_DIR, example)
await removeExistingAutomerge(projectPath)
await fsPromises.rm(path.join(projectPath, "yarn.lock"), {
force: true,
})
await spawnAndWait(
"yarn",
[
"--cwd",
projectPath,
"install",
"--registry",
registryUrl,
"--check-files",
],
{ stdio: "inherit" }
)
await spawnAndWait("yarn", ["--cwd", projectPath, "build"], {
stdio: "inherit",
})
}
}
})
}
type WithRegistryAction = (registryUrl: string) => Promise<void>
async function withRegistry(
action: WithRegistryAction,
...actions: Array<WithRegistryAction>
) {
// First, start verdaccio
printHeader("Starting verdaccio NPM server")
const verd = await VerdaccioProcess.start()
actions.unshift(action)
for (const action of actions) {
try {
type Step = "verd-died" | "action-completed"
const verdDied: () => Promise<Step> = async () => {
await verd.died()
return "verd-died"
}
const actionComplete: () => Promise<Step> = async () => {
await action("http://localhost:4873")
return "action-completed"
}
const result = await Promise.race([verdDied(), actionComplete()])
if (result === "verd-died") {
throw new Error("verdaccio unexpectedly exited")
}
} catch (e) {
await verd.kill()
throw e
}
}
await verd.kill()
}
async function withPublishedWasm(profile: Profile, action: WithRegistryAction) {
await withRegistry(buildAutomergeWasm(profile), publishAutomergeWasm, action)
}
function buildAutomergeWasm(profile: Profile): WithRegistryAction {
return async (registryUrl: string) => {
printHeader("building automerge-wasm")
await spawnAndWait(
"yarn",
["--cwd", AUTOMERGE_WASM_PATH, "--registry", registryUrl, "install"],
{ stdio: "inherit" }
)
const cmd = profile === "release" ? "release" : "debug"
await spawnAndWait("yarn", ["--cwd", AUTOMERGE_WASM_PATH, cmd], {
stdio: "inherit",
})
}
}
async function publishAutomergeWasm(registryUrl: string) {
printHeader("Publishing automerge-wasm to verdaccio")
await fsPromises.rm(
path.join(VERDACCIO_DB_PATH, "@automerge/automerge-wasm"),
{ recursive: true, force: true }
)
await yarnPublish(registryUrl, AUTOMERGE_WASM_PATH)
}
async function buildAndPublishAutomergeJs(registryUrl: string) {
// Build the js package
printHeader("Building automerge")
await removeExistingAutomerge(AUTOMERGE_JS_PATH)
await removeFromVerdaccio("@automerge/automerge")
await fsPromises.rm(path.join(AUTOMERGE_JS_PATH, "yarn.lock"), {
force: true,
})
await spawnAndWait(
"yarn",
[
"--cwd",
AUTOMERGE_JS_PATH,
"install",
"--registry",
registryUrl,
"--check-files",
],
{ stdio: "inherit" }
)
await spawnAndWait("yarn", ["--cwd", AUTOMERGE_JS_PATH, "build"], {
stdio: "inherit",
})
await yarnPublish(registryUrl, AUTOMERGE_JS_PATH)
}
/**
* A running verdaccio process
*
*/
class VerdaccioProcess {
child: ChildProcess
stdout: Array<Buffer>
stderr: Array<Buffer>
constructor(child: ChildProcess) {
this.child = child
// Collect stdout/stderr otherwise the subprocess gets blocked writing
this.stdout = []
this.stderr = []
this.child.stdout &&
this.child.stdout.on("data", data => this.stdout.push(data))
this.child.stderr &&
this.child.stderr.on("data", data => this.stderr.push(data))
const errCallback = (e: any) => {
console.error("!!!!!!!!!ERROR IN VERDACCIO PROCESS!!!!!!!!!")
console.error(" ", e)
if (this.stdout.length > 0) {
console.log("\n**Verdaccio stdout**")
const stdout = Buffer.concat(this.stdout)
process.stdout.write(stdout)
}
if (this.stderr.length > 0) {
console.log("\n**Verdaccio stderr**")
const stdout = Buffer.concat(this.stderr)
process.stdout.write(stdout)
}
process.exit(-1)
}
this.child.on("error", errCallback)
}
/**
* Spawn a verdaccio process and wait for it to respond succesfully to http requests
*
* The returned `VerdaccioProcess` can be used to control the subprocess
*/
static async start() {
const child = spawn(
"yarn",
["verdaccio", "--config", VERDACCIO_CONFIG_PATH],
{ env: { ...process.env, FORCE_COLOR: "true" } }
)
// Forward stdout and stderr whilst waiting for startup to complete
const stdoutCallback = (data: Buffer) => process.stdout.write(data)
const stderrCallback = (data: Buffer) => process.stderr.write(data)
child.stdout && child.stdout.on("data", stdoutCallback)
child.stderr && child.stderr.on("data", stderrCallback)
const healthCheck = async () => {
while (true) {
try {
const resp = await fetch("http://localhost:4873")
if (resp.status === 200) {
return
} else {
console.log(`Healthcheck failed: bad status ${resp.status}`)
}
} catch (e) {
console.error(`Healthcheck failed: ${e}`)
}
await setTimeout(500)
}
}
await withTimeout(healthCheck(), 10000)
// Stop forwarding stdout/stderr
child.stdout && child.stdout.off("data", stdoutCallback)
child.stderr && child.stderr.off("data", stderrCallback)
return new VerdaccioProcess(child)
}
/**
* Send a SIGKILL to the process and wait for it to stop
*/
async kill() {
this.child.stdout && this.child.stdout.destroy()
this.child.stderr && this.child.stderr.destroy()
this.child.kill()
try {
await withTimeout(once(this.child, "close"), 500)
} catch (e) {
console.error("unable to kill verdaccio subprocess, trying -9")
this.child.kill(9)
await withTimeout(once(this.child, "close"), 500)
}
}
/**
* A promise which resolves if the subprocess exits for some reason
*/
async died(): Promise<number | null> {
const [exit, _signal] = await once(this.child, "exit")
return exit
}
}
function printHeader(header: string) {
console.log("\n===============================")
console.log(` ${header}`)
console.log("===============================")
}
/**
* Removes the automerge, @automerge/automerge-wasm, and @automerge/automerge packages from
* `$packageDir/node_modules`
*
* This is useful to force refreshing a package by use in combination with
* `yarn install --check-files`, which checks if a package is present in
* `node_modules` and if it is not forces a reinstall.
*
* @param packageDir - The directory containing the package.json of the target project
*/
async function removeExistingAutomerge(packageDir: string) {
await fsPromises.rm(path.join(packageDir, "node_modules", "@automerge"), {
recursive: true,
force: true,
})
await fsPromises.rm(path.join(packageDir, "node_modules", "automerge"), {
recursive: true,
force: true,
})
}
type SpawnResult = {
stdout?: Buffer
stderr?: Buffer
}
async function spawnAndWait(
cmd: string,
args: Array<string>,
options: child_process.SpawnOptions
): Promise<SpawnResult> {
const child = spawn(cmd, args, options)
let stdout = null
let stderr = null
if (child.stdout) {
stdout = []
child.stdout.on("data", data => stdout.push(data))
}
if (child.stderr) {
stderr = []
child.stderr.on("data", data => stderr.push(data))
}
const [exit, _signal] = await once(child, "exit")
if (exit && exit !== 0) {
throw new Error("nonzero exit code")
}
return {
stderr: stderr ? Buffer.concat(stderr) : null,
stdout: stdout ? Buffer.concat(stdout) : null,
}
}
/**
* Remove a package from the verdaccio registry. This is necessary because we
* often want to _replace_ a version rather than update the version number.
* Obviously this is very bad and verboten in normal circumastances, but the
* whole point here is to be able to test the entire packaging story so it's
* okay I Promise.
*/
async function removeFromVerdaccio(packageName: string) {
await fsPromises.rm(path.join(VERDACCIO_DB_PATH, packageName), {
force: true,
recursive: true,
})
}
async function yarnPublish(registryUrl: string, cwd: string) {
await spawnAndWait(
"yarn",
["--registry", registryUrl, "--cwd", cwd, "publish", "--non-interactive"],
{
stdio: "inherit",
env: {
...process.env,
FORCE_COLOR: "true",
// This is a fake token, it just has to be the right format
npm_config__auth:
"//localhost:4873/:_authToken=Gp2Mgxm4faa/7wp0dMSuRA==",
},
}
)
}
/**
* Wait for a given delay to resolve a promise, throwing an error if the
* promise doesn't resolve with the timeout
*
* @param promise - the promise to wait for @param timeout - the delay in
* milliseconds to wait before throwing
*/
async function withTimeout<T>(
promise: Promise<T>,
timeout: number
): Promise<T> {
type Step = "timed-out" | { result: T }
const timedOut: () => Promise<Step> = async () => {
await setTimeout(timeout)
return "timed-out"
}
const succeeded: () => Promise<Step> = async () => {
const result = await promise
return { result }
}
const result = await Promise.race([timedOut(), succeeded()])
if (result === "timed-out") {
throw new Error("timed out")
} else {
return result.result
}
}

View file

@ -0,0 +1,23 @@
{
"name": "e2e",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"e2e": "ts-node index.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^18.7.18",
"cmd-ts": "^0.11.0",
"node-fetch": "^2",
"ts-node": "^10.9.1",
"typed-emitter": "^2.1.0",
"typescript": "^4.8.3",
"verdaccio": "5"
},
"devDependencies": {
"@types/node-fetch": "2.x"
}
}

View file

@ -0,0 +1,6 @@
{
"compilerOptions": {
"types": ["node"]
},
"module": "nodenext"
}

View file

@ -0,0 +1,25 @@
storage: "./verdacciodb"
auth:
htpasswd:
file: ./htpasswd
publish:
allow_offline: true
logs: { type: stdout, format: pretty, level: info }
packages:
"@automerge/automerge-wasm":
access: "$all"
publish: "$all"
"@automerge/automerge":
access: "$all"
publish: "$all"
"*":
access: "$all"
publish: "$all"
proxy: npmjs
"@*/*":
access: "$all"
publish: "$all"
proxy: npmjs
uplinks:
npmjs:
url: https://registry.npmjs.org/

2130
javascript/e2e/yarn.lock Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1 @@
node_modules/

View file

@ -0,0 +1,59 @@
# Automerge + `create-react-app`
This is a little fiddly to get working. The problem is that `create-react-app`
hard codes a webpack configuration which does not support WASM modules, which we
require in order to bundle the WASM implementation of automerge. To get around
this we use [`craco`](https://github.com/dilanx/craco) which does some monkey
patching to allow us to modify the webpack config that `create-react-app`
bundles. Then we use a craco plugin called
[`craco-wasm`](https://www.npmjs.com/package/craco-wasm) to perform the
necessary modifications to the webpack config. It should be noted that this is
all quite fragile and ideally you probably don't want to use `create-react-app`
to do this in production.
## Setup
Assuming you have already run `create-react-app` and your working directory is
the project.
### Install craco and craco-wasm
```bash
yarn add craco craco-wasm
```
### Modify `package.json` to use `craco` for scripts
In `package.json` the `scripts` section will look like this:
```json
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "craco eject"
},
```
Replace that section with:
```json
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "craco eject"
},
```
### Create `craco.config.js`
In the root of the project add the following contents to `craco.config.js`
```javascript
const cracoWasm = require("craco-wasm")
module.exports = {
plugins: [cracoWasm()],
}
```

View file

@ -0,0 +1,5 @@
const cracoWasm = require("craco-wasm")
module.exports = {
plugins: [cracoWasm()],
}

View file

@ -0,0 +1,41 @@
{
"name": "automerge-create-react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@craco/craco": "^7.0.0-alpha.8",
"craco-wasm": "0.0.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@automerge/automerge": "2.0.0-alpha.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "craco eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

View file

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

View file

@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

View file

@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:

View file

@ -0,0 +1,38 @@
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

View file

@ -0,0 +1,20 @@
import * as Automerge from "@automerge/automerge"
import logo from "./logo.svg"
import "./App.css"
let doc = Automerge.init()
doc = Automerge.change(doc, d => (d.hello = "from automerge"))
const result = JSON.stringify(doc)
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>{result}</p>
</header>
</div>
)
}
export default App

View file

@ -0,0 +1,8 @@
import { render, screen } from "@testing-library/react"
import App from "./App"
test("renders learn react link", () => {
render(<App />)
const linkElement = screen.getByText(/learn react/i)
expect(linkElement).toBeInTheDocument()
})

View file

@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}

View file

@ -0,0 +1,17 @@
import React from "react"
import ReactDOM from "react-dom/client"
import "./index.css"
import App from "./App"
import reportWebVitals from "./reportWebVitals"
const root = ReactDOM.createRoot(document.getElementById("root"))
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
)
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals()

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3"><g fill="#61DAFB"><path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/><circle cx="420.9" cy="296.5" r="45.7"/><path d="M520.5 78.1z"/></g></svg>

After

Width:  |  Height:  |  Size: 2.6 KiB

View file

@ -0,0 +1,13 @@
const reportWebVitals = onPerfEntry => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import("web-vitals").then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
getCLS(onPerfEntry)
getFID(onPerfEntry)
getFCP(onPerfEntry)
getLCP(onPerfEntry)
getTTFB(onPerfEntry)
})
}
}
export default reportWebVitals

View file

@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import "@testing-library/jest-dom"

File diff suppressed because it is too large Load diff

2
javascript/examples/vite/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules/
yarn.lock

View file

@ -0,0 +1,54 @@
# Vite + Automerge
There are three things you need to do to get WASM packaging working with vite:
1. Install the top level await plugin
2. Install the `vite-plugin-wasm` plugin
3. Exclude `automerge-wasm` from the optimizer
First, install the packages we need:
```bash
yarn add vite-plugin-top-level-await
yarn add vite-plugin-wasm
```
In `vite.config.js`
```javascript
import { defineConfig } from "vite"
import wasm from "vite-plugin-wasm"
import topLevelAwait from "vite-plugin-top-level-await"
export default defineConfig({
plugins: [topLevelAwait(), wasm()],
// This is only necessary if you are using `SharedWorker` or `WebWorker`, as
// documented in https://vitejs.dev/guide/features.html#import-with-constructors
worker: {
format: "es",
plugins: [topLevelAwait(), wasm()],
},
optimizeDeps: {
// This is necessary because otherwise `vite dev` includes two separate
// versions of the JS wrapper. This causes problems because the JS
// wrapper has a module level variable to track JS side heap
// allocations, initializing this twice causes horrible breakage
exclude: ["@automerge/automerge-wasm"],
},
})
```
Now start the dev server:
```bash
yarn vite
```
## Running the example
```bash
yarn install
yarn dev
```

View file

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

View file

@ -0,0 +1,15 @@
import * as Automerge from "/node_modules/.vite/deps/automerge-js.js?v=6e973f28"
console.log(Automerge)
let doc = Automerge.init()
doc = Automerge.change(doc, d => (d.hello = "from automerge-js"))
console.log(doc)
const result = JSON.stringify(doc)
if (typeof document !== "undefined") {
const element = document.createElement("div")
element.innerHTML = JSON.stringify(result)
document.body.appendChild(element)
} else {
console.log("node:", result)
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9ob21lL2FsZXgvUHJvamVjdHMvYXV0b21lcmdlL2F1dG9tZXJnZS1ycy9hdXRvbWVyZ2UtanMvZXhhbXBsZXMvdml0ZS9zcmMvbWFpbi50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgKiBhcyBBdXRvbWVyZ2UgZnJvbSBcImF1dG9tZXJnZS1qc1wiXG5cbi8vIGhlbGxvIHdvcmxkIGNvZGUgdGhhdCB3aWxsIHJ1biBjb3JyZWN0bHkgb24gd2ViIG9yIG5vZGVcblxuY29uc29sZS5sb2coQXV0b21lcmdlKVxubGV0IGRvYyA9IEF1dG9tZXJnZS5pbml0KClcbmRvYyA9IEF1dG9tZXJnZS5jaGFuZ2UoZG9jLCAoZDogYW55KSA9PiBkLmhlbGxvID0gXCJmcm9tIGF1dG9tZXJnZS1qc1wiKVxuY29uc29sZS5sb2coZG9jKVxuY29uc3QgcmVzdWx0ID0gSlNPTi5zdHJpbmdpZnkoZG9jKVxuXG5pZiAodHlwZW9mIGRvY3VtZW50ICE9PSAndW5kZWZpbmVkJykge1xuICAgIC8vIGJyb3dzZXJcbiAgICBjb25zdCBlbGVtZW50ID0gZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgnZGl2Jyk7XG4gICAgZWxlbWVudC5pbm5lckhUTUwgPSBKU09OLnN0cmluZ2lmeShyZXN1bHQpXG4gICAgZG9jdW1lbnQuYm9keS5hcHBlbmRDaGlsZChlbGVtZW50KTtcbn0gZWxzZSB7XG4gICAgLy8gc2VydmVyXG4gICAgY29uc29sZS5sb2coXCJub2RlOlwiLCByZXN1bHQpXG59XG5cbiJdLCJtYXBwaW5ncyI6IkFBQUEsWUFBWSxlQUFlO0FBSTNCLFFBQVEsSUFBSSxTQUFTO0FBQ3JCLElBQUksTUFBTSxVQUFVLEtBQUs7QUFDekIsTUFBTSxVQUFVLE9BQU8sS0FBSyxDQUFDLE1BQVcsRUFBRSxRQUFRLG1CQUFtQjtBQUNyRSxRQUFRLElBQUksR0FBRztBQUNmLE1BQU0sU0FBUyxLQUFLLFVBQVUsR0FBRztBQUVqQyxJQUFJLE9BQU8sYUFBYSxhQUFhO0FBRWpDLFFBQU0sVUFBVSxTQUFTLGNBQWMsS0FBSztBQUM1QyxVQUFRLFlBQVksS0FBSyxVQUFVLE1BQU07QUFDekMsV0FBUyxLQUFLLFlBQVksT0FBTztBQUNyQyxPQUFPO0FBRUgsVUFBUSxJQUFJLFNBQVMsTUFBTTtBQUMvQjsiLCJuYW1lcyI6W119

View file

@ -0,0 +1,20 @@
{
"name": "autovite",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@automerge/automerge": "2.0.0-alpha.7"
},
"devDependencies": {
"typescript": "^4.6.4",
"vite": "^3.1.0",
"vite-plugin-top-level-await": "^1.1.1",
"vite-plugin-wasm": "^2.1.0"
}
}

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

@ -0,0 +1,9 @@
export function setupCounter(element: HTMLButtonElement) {
let counter = 0
const setCounter = (count: number) => {
counter = count
element.innerHTML = `count is ${counter}`
}
element.addEventListener("click", () => setCounter(++counter))
setCounter(0)
}

View file

@ -0,0 +1,17 @@
import * as Automerge from "@automerge/automerge"
// hello world code that will run correctly on web or node
let doc = Automerge.init()
doc = Automerge.change(doc, (d: any) => (d.hello = "from automerge"))
const result = JSON.stringify(doc)
if (typeof document !== "undefined") {
// browser
const element = document.createElement("div")
element.innerHTML = JSON.stringify(result)
document.body.appendChild(element)
} else {
// server
console.log("node:", result)
}

View file

@ -0,0 +1,97 @@
:root {
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
font-size: 16px;
line-height: 24px;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vanilla:hover {
filter: drop-shadow(0 0 2em #3178c6aa);
}
.card {
padding: 2em;
}
.read-the-docs {
color: #888;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="32" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path fill="#007ACC" d="M0 128v128h256V0H0z"></path><path fill="#FFF" d="m56.612 128.85l-.081 10.483h33.32v94.68h23.568v-94.68h33.321v-10.28c0-5.69-.122-10.444-.284-10.566c-.122-.162-20.4-.244-44.983-.203l-44.74.122l-.121 10.443Zm149.955-10.742c6.501 1.625 11.459 4.51 16.01 9.224c2.357 2.52 5.851 7.111 6.136 8.208c.08.325-11.053 7.802-17.798 11.988c-.244.162-1.22-.894-2.317-2.52c-3.291-4.795-6.745-6.867-12.028-7.233c-7.76-.528-12.759 3.535-12.718 10.321c0 1.992.284 3.17 1.097 4.795c1.707 3.536 4.876 5.649 14.832 9.956c18.326 7.883 26.168 13.084 31.045 20.48c5.445 8.249 6.664 21.415 2.966 31.208c-4.063 10.646-14.14 17.879-28.323 20.276c-4.388.772-14.79.65-19.504-.203c-10.28-1.828-20.033-6.908-26.047-13.572c-2.357-2.6-6.949-9.387-6.664-9.874c.122-.163 1.178-.813 2.356-1.504c1.138-.65 5.446-3.129 9.509-5.485l7.355-4.267l1.544 2.276c2.154 3.29 6.867 7.801 9.712 9.305c8.167 4.307 19.383 3.698 24.909-1.26c2.357-2.153 3.332-4.388 3.332-7.68c0-2.966-.366-4.266-1.91-6.501c-1.99-2.845-6.054-5.242-17.595-10.24c-13.206-5.69-18.895-9.224-24.096-14.832c-3.007-3.25-5.852-8.452-7.03-12.8c-.975-3.617-1.22-12.678-.447-16.335c2.723-12.76 12.353-21.659 26.25-24.3c4.51-.853 14.994-.528 19.424.569Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -0,0 +1 @@
/// <reference types="vite/client" />

View file

@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
"strict": true,
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"skipLibCheck": true
},
"include": ["src"]
}

View file

@ -0,0 +1,22 @@
import { defineConfig } from "vite"
import wasm from "vite-plugin-wasm"
import topLevelAwait from "vite-plugin-top-level-await"
export default defineConfig({
plugins: [topLevelAwait(), wasm()],
// This is only necessary if you are using `SharedWorker` or `WebWorker`, as
// documented in https://vitejs.dev/guide/features.html#import-with-constructors
worker: {
format: "es",
plugins: [topLevelAwait(), wasm()],
},
optimizeDeps: {
// This is necessary because otherwise `vite dev` includes two separate
// versions of the JS wrapper. This causes problems because the JS
// wrapper has a module level variable to track JS side heap
// allocations, initializing this twice causes horrible breakage
exclude: ["@automerge/automerge-wasm"],
},
})

View file

@ -0,0 +1,5 @@
yarn.lock
node_modules
public/*.wasm
public/main.js
dist

View file

@ -0,0 +1,35 @@
# Webpack + Automerge
Getting WASM working in webpack 5 is very easy. You just need to enable the
`asyncWebAssembly`
[experiment](https://webpack.js.org/configuration/experiments/). For example:
```javascript
const path = require("path")
const clientConfig = {
experiments: { asyncWebAssembly: true },
target: "web",
entry: "./src/index.js",
output: {
filename: "main.js",
path: path.resolve(__dirname, "public"),
},
mode: "development", // or production
performance: {
// we dont want the wasm blob to generate warnings
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
}
module.exports = clientConfig
```
## Running the example
```bash
yarn install
yarn start
```

View file

@ -0,0 +1,22 @@
{
"name": "webpack-automerge-example",
"version": "0.1.0",
"description": "",
"private": true,
"scripts": {
"build": "webpack",
"start": "serve public",
"test": "node dist/node.js"
},
"author": "",
"dependencies": {
"@automerge/automerge": "2.0.0-alpha.7"
},
"devDependencies": {
"serve": "^13.0.2",
"webpack": "^5.72.1",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.11.1",
"webpack-node-externals": "^3.0.0"
}
}

View file

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Simple Webpack for automerge-wasm</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>

View file

@ -0,0 +1,17 @@
import * as Automerge from "@automerge/automerge"
// hello world code that will run correctly on web or node
let doc = Automerge.init()
doc = Automerge.change(doc, d => (d.hello = "from automerge"))
const result = JSON.stringify(doc)
if (typeof document !== "undefined") {
// browser
const element = document.createElement("div")
element.innerHTML = JSON.stringify(result)
document.body.appendChild(element)
} else {
// server
console.log("node:", result)
}

View file

@ -0,0 +1,37 @@
const path = require("path")
const nodeExternals = require("webpack-node-externals")
// the most basic webpack config for node or web targets for automerge-wasm
const serverConfig = {
// basic setup for bundling a node package
target: "node",
externals: [nodeExternals()],
externalsPresets: { node: true },
entry: "./src/index.js",
output: {
filename: "node.js",
path: path.resolve(__dirname, "dist"),
},
mode: "development", // or production
}
const clientConfig = {
experiments: { asyncWebAssembly: true },
target: "web",
entry: "./src/index.js",
output: {
filename: "main.js",
path: path.resolve(__dirname, "public"),
},
mode: "development", // or production
performance: {
// we dont want the wasm blob to generate warnings
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
}
module.exports = [serverConfig, clientConfig]

53
javascript/package.json Normal file
View file

@ -0,0 +1,53 @@
{
"name": "@automerge/automerge",
"collaborators": [
"Orion Henry <orion@inkandswitch.com>",
"Martin Kleppmann"
],
"version": "2.0.2",
"description": "Javascript implementation of automerge, backed by @automerge/automerge-wasm",
"homepage": "https://github.com/automerge/automerge-rs/tree/main/wrappers/javascript",
"repository": "github:automerge/automerge-rs",
"files": [
"README.md",
"LICENSE",
"package.json",
"dist/index.d.ts",
"dist/cjs/**/*.js",
"dist/mjs/**/*.js",
"dist/*.d.ts"
],
"types": "./dist/index.d.ts",
"module": "./dist/mjs/index.js",
"main": "./dist/cjs/index.js",
"license": "MIT",
"scripts": {
"lint": "eslint src",
"build": "tsc -p config/mjs.json && tsc -p config/cjs.json && tsc -p config/declonly.json --emitDeclarationOnly",
"test": "ts-mocha test/*.ts",
"deno:build": "denoify && node ./scripts/deno-prefixer.mjs",
"deno:test": "deno test ./deno-tests/deno.ts --allow-read --allow-net",
"watch-docs": "typedoc src/index.ts --watch --readme none"
},
"devDependencies": {
"@types/expect": "^24.3.0",
"@types/mocha": "^10.0.1",
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.46.0",
"@typescript-eslint/parser": "^5.46.0",
"denoify": "^1.4.5",
"eslint": "^8.29.0",
"fast-sha256": "^1.3.0",
"mocha": "^10.2.0",
"pako": "^2.1.0",
"prettier": "^2.8.1",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.1",
"typedoc": "^0.23.22",
"typescript": "^4.9.4"
},
"dependencies": {
"@automerge/automerge-wasm": "0.1.25",
"uuid": "^9.0.0"
}
}

View file

@ -0,0 +1,9 @@
import * as fs from "fs"
const files = ["./deno_dist/proxies.ts"]
for (const filepath of files) {
const data = fs.readFileSync(filepath)
fs.writeFileSync(filepath, "// @ts-nocheck \n" + data)
console.log('Prepended "// @ts-nocheck" to ' + filepath)
}

View file

@ -0,0 +1,42 @@
// @denoify-ignore
import { makeThisModuleAnExecutableReplacer } from "denoify"
// import { assert } from "tsafe";
// import * as path from "path";
makeThisModuleAnExecutableReplacer(
async ({ parsedImportExportStatement, destDirPath, version }) => {
version = process.env.VERSION || version
switch (parsedImportExportStatement.parsedArgument.nodeModuleName) {
case "@automerge/automerge-wasm":
{
const moduleRoot =
process.env.ROOT_MODULE ||
`https://deno.land/x/automerge_wasm@${version}`
/*
*We expect not to run against statements like
*import(..).then(...)
*or
*export * from "..."
*in our code.
*/
if (
!parsedImportExportStatement.isAsyncImport &&
(parsedImportExportStatement.statementType === "import" ||
parsedImportExportStatement.statementType === "export")
) {
if (parsedImportExportStatement.isTypeOnly) {
return `${parsedImportExportStatement.statementType} type ${parsedImportExportStatement.target} from "${moduleRoot}/index.d.ts";`
} else {
return `${parsedImportExportStatement.statementType} ${parsedImportExportStatement.target} from "${moduleRoot}/automerge_wasm.js";`
}
}
}
break
}
//The replacer should return undefined when we want to let denoify replace the statement
return undefined
}
)

100
javascript/src/conflicts.ts Normal file
View file

@ -0,0 +1,100 @@
import { Counter, type AutomergeValue } from "./types"
import { Text } from "./text"
import { type AutomergeValue as UnstableAutomergeValue } from "./unstable_types"
import { type Target, Text1Target, Text2Target } from "./proxies"
import { mapProxy, listProxy, ValueType } from "./proxies"
import type { Prop, ObjID } from "@automerge/automerge-wasm"
import { Automerge } from "@automerge/automerge-wasm"
export type ConflictsF<T extends Target> = { [key: string]: ValueType<T> }
export type Conflicts = ConflictsF<Text1Target>
export type UnstableConflicts = ConflictsF<Text2Target>
export function stableConflictAt(
context: Automerge,
objectId: ObjID,
prop: Prop
): Conflicts | undefined {
return conflictAt<Text1Target>(
context,
objectId,
prop,
true,
(context: Automerge, conflictId: ObjID): AutomergeValue => {
return new Text(context.text(conflictId))
}
)
}
export function unstableConflictAt(
context: Automerge,
objectId: ObjID,
prop: Prop
): UnstableConflicts | undefined {
return conflictAt<Text2Target>(
context,
objectId,
prop,
true,
(context: Automerge, conflictId: ObjID): UnstableAutomergeValue => {
return context.text(conflictId)
}
)
}
function conflictAt<T extends Target>(
context: Automerge,
objectId: ObjID,
prop: Prop,
textV2: boolean,
handleText: (a: Automerge, conflictId: ObjID) => ValueType<T>
): ConflictsF<T> | undefined {
const values = context.getAll(objectId, prop)
if (values.length <= 1) {
return
}
const result: ConflictsF<T> = {}
for (const fullVal of values) {
switch (fullVal[0]) {
case "map":
result[fullVal[1]] = mapProxy<T>(
context,
fullVal[1],
textV2,
[prop],
true
)
break
case "list":
result[fullVal[1]] = listProxy<T>(
context,
fullVal[1],
textV2,
[prop],
true
)
break
case "text":
result[fullVal[1]] = handleText(context, fullVal[1] as ObjID)
break
case "str":
case "uint":
case "int":
case "f64":
case "boolean":
case "bytes":
case "null":
result[fullVal[2]] = fullVal[1] as ValueType<T>
break
case "counter":
result[fullVal[2]] = new Counter(fullVal[1]) as ValueType<T>
break
case "timestamp":
result[fullVal[2]] = new Date(fullVal[1]) as ValueType<T>
break
default:
throw RangeError(`datatype ${fullVal[0]} unimplemented`)
}
}
return result
}

View file

@ -0,0 +1,12 @@
// Properties of the document root object
export const STATE = Symbol.for("_am_meta") // symbol used to hide application metadata on automerge objects
export const TRACE = Symbol.for("_am_trace") // used for debugging
export const OBJECT_ID = Symbol.for("_am_objectId") // symbol used to hide the object id on automerge objects
export const IS_PROXY = Symbol.for("_am_isProxy") // symbol used to test if the document is a proxy object
export const UINT = Symbol.for("_am_uint")
export const INT = Symbol.for("_am_int")
export const F64 = Symbol.for("_am_f64")
export const COUNTER = Symbol.for("_am_counter")
export const TEXT = Symbol.for("_am_text")

107
javascript/src/counter.ts Normal file
View file

@ -0,0 +1,107 @@
import { Automerge, type ObjID, type Prop } from "@automerge/automerge-wasm"
import { COUNTER } from "./constants"
/**
* The most basic CRDT: an integer value that can be changed only by
* incrementing and decrementing. Since addition of integers is commutative,
* the value trivially converges.
*/
export class Counter {
value: number
constructor(value?: number) {
this.value = value || 0
Reflect.defineProperty(this, COUNTER, { value: true })
}
/**
* A peculiar JavaScript language feature from its early days: if the object
* `x` has a `valueOf()` method that returns a number, you can use numerical
* operators on the object `x` directly, such as `x + 1` or `x < 4`.
* This method is also called when coercing a value to a string by
* concatenating it with another string, as in `x + ''`.
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
*/
valueOf(): number {
return this.value
}
/**
* Returns the counter value as a decimal string. If `x` is a counter object,
* this method is called e.g. when you do `['value: ', x].join('')` or when
* you use string interpolation: `value: ${x}`.
*/
toString(): string {
return this.valueOf().toString()
}
/**
* Returns the counter value, so that a JSON serialization of an Automerge
* document represents the counter simply as an integer.
*/
toJSON(): number {
return this.value
}
}
/**
* An instance of this class is used when a counter is accessed within a change
* callback.
*/
class WriteableCounter extends Counter {
context: Automerge
path: Prop[]
objectId: ObjID
key: Prop
constructor(
value: number,
context: Automerge,
path: Prop[],
objectId: ObjID,
key: Prop
) {
super(value)
this.context = context
this.path = path
this.objectId = objectId
this.key = key
}
/**
* Increases the value of the counter by `delta`. If `delta` is not given,
* increases the value of the counter by 1.
*/
increment(delta: number): number {
delta = typeof delta === "number" ? delta : 1
this.context.increment(this.objectId, this.key, delta)
this.value += delta
return this.value
}
/**
* Decreases the value of the counter by `delta`. If `delta` is not given,
* decreases the value of the counter by 1.
*/
decrement(delta: number): number {
return this.increment(typeof delta === "number" ? -delta : -1)
}
}
/**
* Returns an instance of `WriteableCounter` for use in a change callback.
* `context` is the proxy context that keeps track of the mutations.
* `objectId` is the ID of the object containing the counter, and `key` is
* the property name (key in map, or index in list) where the counter is
* located.
*/
export function getWriteableCounter(
value: number,
context: Automerge,
path: Prop[],
objectId: ObjID,
key: Prop
): WriteableCounter {
return new WriteableCounter(value, context, path, objectId, key)
}
//module.exports = { Counter, getWriteableCounter }

242
javascript/src/index.ts Normal file
View file

@ -0,0 +1,242 @@
/**
* # Automerge
*
* This library provides the core automerge data structure and sync algorithms.
* Other libraries can be built on top of this one which provide IO and
* persistence.
*
* An automerge document can be though of an immutable POJO (plain old javascript
* object) which `automerge` tracks the history of, allowing it to be merged with
* any other automerge document.
*
* ## Creating and modifying a document
*
* You can create a document with {@link init} or {@link from} and then make
* changes to it with {@link change}, you can merge two documents with {@link
* merge}.
*
* ```ts
* import * as automerge from "@automerge/automerge"
*
* type DocType = {ideas: Array<automerge.Text>}
*
* let doc1 = automerge.init<DocType>()
* doc1 = automerge.change(doc1, d => {
* d.ideas = [new automerge.Text("an immutable document")]
* })
*
* let doc2 = automerge.init<DocType>()
* doc2 = automerge.merge(doc2, automerge.clone(doc1))
* doc2 = automerge.change<DocType>(doc2, d => {
* d.ideas.push(new automerge.Text("which records it's history"))
* })
*
* // Note the `automerge.clone` call, see the "cloning" section of this readme for
* // more detail
* doc1 = automerge.merge(doc1, automerge.clone(doc2))
* doc1 = automerge.change(doc1, d => {
* d.ideas[0].deleteAt(13, 8)
* d.ideas[0].insertAt(13, "object")
* })
*
* let doc3 = automerge.merge(doc1, doc2)
* // doc3 is now {ideas: ["an immutable object", "which records it's history"]}
* ```
*
* ## Applying changes from another document
*
* You can get a representation of the result of the last {@link change} you made
* to a document with {@link getLastLocalChange} and you can apply that change to
* another document using {@link applyChanges}.
*
* If you need to get just the changes which are in one document but not in another
* you can use {@link getHeads} to get the heads of the document without the
* changes and then {@link getMissingDeps}, passing the result of {@link getHeads}
* on the document with the changes.
*
* ## Saving and loading documents
*
* You can {@link save} a document to generate a compresed binary representation of
* the document which can be loaded with {@link load}. If you have a document which
* you have recently made changes to you can generate recent changes with {@link
* saveIncremental}, this will generate all the changes since you last called
* `saveIncremental`, the changes generated can be applied to another document with
* {@link loadIncremental}.
*
* ## Viewing different versions of a document
*
* Occasionally you may wish to explicitly step to a different point in a document
* history. One common reason to do this is if you need to obtain a set of changes
* which take the document from one state to another in order to send those changes
* to another peer (or to save them somewhere). You can use {@link view} to do this.
*
* ```ts
* import * as automerge from "@automerge/automerge"
* import * as assert from "assert"
*
* let doc = automerge.from({
* key1: "value1",
* })
*
* // Make a clone of the document at this point, maybe this is actually on another
* // peer.
* let doc2 = automerge.clone < any > doc
*
* let heads = automerge.getHeads(doc)
*
* doc =
* automerge.change <
* any >
* (doc,
* d => {
* d.key2 = "value2"
* })
*
* doc =
* automerge.change <
* any >
* (doc,
* d => {
* d.key3 = "value3"
* })
*
* // At this point we've generated two separate changes, now we want to send
* // just those changes to someone else
*
* // view is a cheap reference based copy of a document at a given set of heads
* let before = automerge.view(doc, heads)
*
* // This view doesn't show the last two changes in the document state
* assert.deepEqual(before, {
* key1: "value1",
* })
*
* // Get the changes to send to doc2
* let changes = automerge.getChanges(before, doc)
*
* // Apply the changes at doc2
* doc2 = automerge.applyChanges < any > (doc2, changes)[0]
* assert.deepEqual(doc2, {
* key1: "value1",
* key2: "value2",
* key3: "value3",
* })
* ```
*
* If you have a {@link view} of a document which you want to make changes to you
* can {@link clone} the viewed document.
*
* ## Syncing
*
* The sync protocol is stateful. This means that we start by creating a {@link
* SyncState} for each peer we are communicating with using {@link initSyncState}.
* Then we generate a message to send to the peer by calling {@link
* generateSyncMessage}. When we receive a message from the peer we call {@link
* receiveSyncMessage}. Here's a simple example of a loop which just keeps two
* peers in sync.
*
* ```ts
* let sync1 = automerge.initSyncState()
* let msg: Uint8Array | null
* ;[sync1, msg] = automerge.generateSyncMessage(doc1, sync1)
*
* while (true) {
* if (msg != null) {
* network.send(msg)
* }
* let resp: Uint8Array =
* (network.receive()[(doc1, sync1, _ignore)] =
* automerge.receiveSyncMessage(doc1, sync1, resp)[(sync1, msg)] =
* automerge.generateSyncMessage(doc1, sync1))
* }
* ```
*
* ## Conflicts
*
* The only time conflicts occur in automerge documents is in concurrent
* assignments to the same key in an object. In this case automerge
* deterministically chooses an arbitrary value to present to the application but
* you can examine the conflicts using {@link getConflicts}.
*
* ```
* import * as automerge from "@automerge/automerge"
*
* type Profile = {
* pets: Array<{name: string, type: string}>
* }
*
* let doc1 = automerge.init<Profile>("aaaa")
* doc1 = automerge.change(doc1, d => {
* d.pets = [{name: "Lassie", type: "dog"}]
* })
* let doc2 = automerge.init<Profile>("bbbb")
* doc2 = automerge.merge(doc2, automerge.clone(doc1))
*
* doc2 = automerge.change(doc2, d => {
* d.pets[0].name = "Beethoven"
* })
*
* doc1 = automerge.change(doc1, d => {
* d.pets[0].name = "Babe"
* })
*
* const doc3 = automerge.merge(doc1, doc2)
*
* // Note that here we pass `doc3.pets`, not `doc3`
* let conflicts = automerge.getConflicts(doc3.pets[0], "name")
*
* // The two conflicting values are the keys of the conflicts object
* assert.deepEqual(Object.values(conflicts), ["Babe", Beethoven"])
* ```
*
* ## Actor IDs
*
* By default automerge will generate a random actor ID for you, but most methods
* for creating a document allow you to set the actor ID. You can get the actor ID
* associated with the document by calling {@link getActorId}. Actor IDs must not
* be used in concurrent threads of executiong - all changes by a given actor ID
* are expected to be sequential.
*
* ## Listening to patches
*
* Sometimes you want to respond to changes made to an automerge document. In this
* case you can use the {@link PatchCallback} type to receive notifications when
* changes have been made.
*
* ## Cloning
*
* Currently you cannot make mutating changes (i.e. call {@link change}) to a
* document which you have two pointers to. For example, in this code:
*
* ```javascript
* let doc1 = automerge.init()
* let doc2 = automerge.change(doc1, d => (d.key = "value"))
* ```
*
* `doc1` and `doc2` are both pointers to the same state. Any attempt to call
* mutating methods on `doc1` will now result in an error like
*
* Attempting to change an out of date document
*
* If you encounter this you need to clone the original document, the above sample
* would work as:
*
* ```javascript
* let doc1 = automerge.init()
* let doc2 = automerge.change(automerge.clone(doc1), d => (d.key = "value"))
* ```
* @packageDocumentation
*
* ## The {@link unstable} module
*
* We are working on some changes to automerge which are not yet complete and
* will result in backwards incompatible API changes. Once these changes are
* ready for production use we will release a new major version of automerge.
* However, until that point you can use the {@link unstable} module to try out
* the new features, documents from the {@link unstable} module are
* interoperable with documents from the main module. Please see the docs for
* the {@link unstable} module for more details.
*/
export * from "./stable"
import * as unstable from "./unstable"
export { unstable }

View file

@ -0,0 +1,43 @@
import { type ObjID, type Heads, Automerge } from "@automerge/automerge-wasm"
import { STATE, OBJECT_ID, TRACE, IS_PROXY } from "./constants"
import type { Doc, PatchCallback } from "./types"
export interface InternalState<T> {
handle: Automerge
heads: Heads | undefined
freeze: boolean
patchCallback?: PatchCallback<T>
textV2: boolean
}
export function _state<T>(doc: Doc<T>, checkroot = true): InternalState<T> {
if (typeof doc !== "object") {
throw new RangeError("must be the document root")
}
const state = Reflect.get(doc, STATE) as InternalState<T>
if (
state === undefined ||
state == null ||
(checkroot && _obj(doc) !== "_root")
) {
throw new RangeError("must be the document root")
}
return state
}
export function _trace<T>(doc: Doc<T>): string | undefined {
return Reflect.get(doc, TRACE) as string
}
export function _obj<T>(doc: Doc<T>): ObjID | null {
if (!(typeof doc === "object") || doc === null) {
return null
}
return Reflect.get(doc, OBJECT_ID) as ObjID
}
export function _is_proxy<T>(doc: Doc<T>): boolean {
return !!Reflect.get(doc, IS_PROXY)
}

View file

@ -0,0 +1,58 @@
import {
type API,
Automerge,
type Change,
type DecodedChange,
type Actor,
SyncState,
type SyncMessage,
type JsSyncState,
type DecodedSyncMessage,
type ChangeToEncode,
} from "@automerge/automerge-wasm"
export type { ChangeToEncode } from "@automerge/automerge-wasm"
export function UseApi(api: API) {
for (const k in api) {
// eslint-disable-next-line @typescript-eslint/no-extra-semi,@typescript-eslint/no-explicit-any
;(ApiHandler as any)[k] = (api as any)[k]
}
}
/* eslint-disable */
export const ApiHandler: API = {
create(textV2: boolean, actor?: Actor): Automerge {
throw new RangeError("Automerge.use() not called")
},
load(data: Uint8Array, textV2: boolean, actor?: Actor): Automerge {
throw new RangeError("Automerge.use() not called (load)")
},
encodeChange(change: ChangeToEncode): Change {
throw new RangeError("Automerge.use() not called (encodeChange)")
},
decodeChange(change: Change): DecodedChange {
throw new RangeError("Automerge.use() not called (decodeChange)")
},
initSyncState(): SyncState {
throw new RangeError("Automerge.use() not called (initSyncState)")
},
encodeSyncMessage(message: DecodedSyncMessage): SyncMessage {
throw new RangeError("Automerge.use() not called (encodeSyncMessage)")
},
decodeSyncMessage(msg: SyncMessage): DecodedSyncMessage {
throw new RangeError("Automerge.use() not called (decodeSyncMessage)")
},
encodeSyncState(state: SyncState): Uint8Array {
throw new RangeError("Automerge.use() not called (encodeSyncState)")
},
decodeSyncState(data: Uint8Array): SyncState {
throw new RangeError("Automerge.use() not called (decodeSyncState)")
},
exportSyncState(state: SyncState): JsSyncState {
throw new RangeError("Automerge.use() not called (exportSyncState)")
},
importSyncState(state: JsSyncState): SyncState {
throw new RangeError("Automerge.use() not called (importSyncState)")
},
}
/* eslint-enable */

54
javascript/src/numbers.ts Normal file
View file

@ -0,0 +1,54 @@
// Convenience classes to allow users to strictly specify the number type they want
import { INT, UINT, F64 } from "./constants"
export class Int {
value: number
constructor(value: number) {
if (
!(
Number.isInteger(value) &&
value <= Number.MAX_SAFE_INTEGER &&
value >= Number.MIN_SAFE_INTEGER
)
) {
throw new RangeError(`Value ${value} cannot be a uint`)
}
this.value = value
Reflect.defineProperty(this, INT, { value: true })
Object.freeze(this)
}
}
export class Uint {
value: number
constructor(value: number) {
if (
!(
Number.isInteger(value) &&
value <= Number.MAX_SAFE_INTEGER &&
value >= 0
)
) {
throw new RangeError(`Value ${value} cannot be a uint`)
}
this.value = value
Reflect.defineProperty(this, UINT, { value: true })
Object.freeze(this)
}
}
export class Float64 {
value: number
constructor(value: number) {
if (typeof value !== "number") {
throw new RangeError(`Value ${value} cannot be a float64`)
}
this.value = value || 0.0
Reflect.defineProperty(this, F64, { value: true })
Object.freeze(this)
}
}

1005
javascript/src/proxies.ts Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,6 @@
export class RawString {
val: string
constructor(val: string) {
this.val = val
}
}

944
javascript/src/stable.ts Normal file
View file

@ -0,0 +1,944 @@
/** @hidden **/
export { /** @hidden */ uuid } from "./uuid"
import { rootProxy } from "./proxies"
import { STATE } from "./constants"
import {
type AutomergeValue,
Counter,
type Doc,
type PatchCallback,
} from "./types"
export {
type AutomergeValue,
Counter,
type Doc,
Int,
Uint,
Float64,
type Patch,
type PatchCallback,
type ScalarValue,
} from "./types"
import { Text } from "./text"
export { Text } from "./text"
import type {
API as WasmAPI,
Actor as ActorId,
Prop,
ObjID,
Change,
DecodedChange,
Heads,
MaterializeValue,
JsSyncState,
SyncMessage,
DecodedSyncMessage,
} from "@automerge/automerge-wasm"
export type {
PutPatch,
DelPatch,
SpliceTextPatch,
InsertPatch,
IncPatch,
SyncMessage,
} from "@automerge/automerge-wasm"
/** @hidden **/
type API = WasmAPI
const SyncStateSymbol = Symbol("_syncstate")
/**
* An opaque type tracking the state of sync with a remote peer
*/
type SyncState = JsSyncState & { _opaque: typeof SyncStateSymbol }
import { ApiHandler, type ChangeToEncode, UseApi } from "./low_level"
import { Automerge } from "@automerge/automerge-wasm"
import { RawString } from "./raw_string"
import { _state, _is_proxy, _trace, _obj } from "./internal_state"
import { stableConflictAt } from "./conflicts"
/** Options passed to {@link change}, and {@link emptyChange}
* @typeParam T - The type of value contained in the document
*/
export type ChangeOptions<T> = {
/** A message which describes the changes */
message?: string
/** The unix timestamp of the change (purely advisory, not used in conflict resolution) */
time?: number
/** A callback which will be called to notify the caller of any changes to the document */
patchCallback?: PatchCallback<T>
}
/** Options passed to {@link loadIncremental}, {@link applyChanges}, and {@link receiveSyncMessage}
* @typeParam T - The type of value contained in the document
*/
export type ApplyOptions<T> = { patchCallback?: PatchCallback<T> }
/**
* A List is an extended Array that adds the two helper methods `deleteAt` and `insertAt`.
*/
export interface List<T> extends Array<T> {
insertAt(index: number, ...args: T[]): List<T>
deleteAt(index: number, numDelete?: number): List<T>
}
/**
* To extend an arbitrary type, we have to turn any arrays that are part of the type's definition into Lists.
* So we recurse through the properties of T, turning any Arrays we find into Lists.
*/
export type Extend<T> =
// is it an array? make it a list (we recursively extend the type of the array's elements as well)
T extends Array<infer T>
? List<Extend<T>>
: // is it an object? recursively extend all of its properties
// eslint-disable-next-line @typescript-eslint/ban-types
T extends Object
? { [P in keyof T]: Extend<T[P]> }
: // otherwise leave the type alone
T
/**
* Function which is called by {@link change} when making changes to a `Doc<T>`
* @typeParam T - The type of value contained in the document
*
* This function may mutate `doc`
*/
export type ChangeFn<T> = (doc: Extend<T>) => void
/** @hidden **/
export interface State<T> {
change: DecodedChange
snapshot: T
}
/** @hidden **/
export function use(api: API) {
UseApi(api)
}
import * as wasm from "@automerge/automerge-wasm"
use(wasm)
/**
* Options to be passed to {@link init} or {@link load}
* @typeParam T - The type of the value the document contains
*/
export type InitOptions<T> = {
/** The actor ID to use for this document, a random one will be generated if `null` is passed */
actor?: ActorId
freeze?: boolean
/** A callback which will be called with the initial patch once the document has finished loading */
patchCallback?: PatchCallback<T>
/** @hidden */
enableTextV2?: boolean
}
/** @hidden */
export function getBackend<T>(doc: Doc<T>): Automerge {
return _state(doc).handle
}
function importOpts<T>(_actor?: ActorId | InitOptions<T>): InitOptions<T> {
if (typeof _actor === "object") {
return _actor
} else {
return { actor: _actor }
}
}
/**
* Create a new automerge document
*
* @typeParam T - The type of value contained in the document. This will be the
* type that is passed to the change closure in {@link change}
* @param _opts - Either an actorId or an {@link InitOptions} (which may
* contain an actorId). If this is null the document will be initialised with a
* random actor ID
*/
export function init<T>(_opts?: ActorId | InitOptions<T>): Doc<T> {
const opts = importOpts(_opts)
const freeze = !!opts.freeze
const patchCallback = opts.patchCallback
const handle = ApiHandler.create(opts.enableTextV2 || false, opts.actor)
handle.enablePatches(true)
handle.enableFreeze(!!opts.freeze)
handle.registerDatatype("counter", (n: number) => new Counter(n))
const textV2 = opts.enableTextV2 || false
if (textV2) {
handle.registerDatatype("str", (n: string) => new RawString(n))
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handle.registerDatatype("text", (n: any) => new Text(n))
}
const doc = handle.materialize("/", undefined, {
handle,
heads: undefined,
freeze,
patchCallback,
textV2,
}) as Doc<T>
return doc
}
/**
* Make an immutable view of an automerge document as at `heads`
*
* @remarks
* The document returned from this function cannot be passed to {@link change}.
* This is because it shares the same underlying memory as `doc`, but it is
* consequently a very cheap copy.
*
* Note that this function will throw an error if any of the hashes in `heads`
* are not in the document.
*
* @typeParam T - The type of the value contained in the document
* @param doc - The document to create a view of
* @param heads - The hashes of the heads to create a view at
*/
export function view<T>(doc: Doc<T>, heads: Heads): Doc<T> {
const state = _state(doc)
const handle = state.handle
return state.handle.materialize("/", heads, {
...state,
handle,
heads,
}) as Doc<T>
}
/**
* Make a full writable copy of an automerge document
*
* @remarks
* Unlike {@link view} this function makes a full copy of the memory backing
* the document and can thus be passed to {@link change}. It also generates a
* new actor ID so that changes made in the new document do not create duplicate
* sequence numbers with respect to the old document. If you need control over
* the actor ID which is generated you can pass the actor ID as the second
* argument
*
* @typeParam T - The type of the value contained in the document
* @param doc - The document to clone
* @param _opts - Either an actor ID to use for the new doc or an {@link InitOptions}
*/
export function clone<T>(
doc: Doc<T>,
_opts?: ActorId | InitOptions<T>
): Doc<T> {
const state = _state(doc)
const heads = state.heads
const opts = importOpts(_opts)
const handle = state.handle.fork(opts.actor, heads)
// `change` uses the presence of state.heads to determine if we are in a view
// set it to undefined to indicate that this is a full fat document
const { heads: _oldHeads, ...stateSansHeads } = state
return handle.applyPatches(doc, { ...stateSansHeads, handle })
}
/** Explicity free the memory backing a document. Note that this is note
* necessary in environments which support
* [`FinalizationRegistry`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry)
*/
export function free<T>(doc: Doc<T>) {
return _state(doc).handle.free()
}
/**
* Create an automerge document from a POJO
*
* @param initialState - The initial state which will be copied into the document
* @typeParam T - The type of the value passed to `from` _and_ the type the resulting document will contain
* @typeParam actor - The actor ID of the resulting document, if this is null a random actor ID will be used
*
* @example
* ```
* const doc = automerge.from({
* tasks: [
* {description: "feed dogs", done: false}
* ]
* })
* ```
*/
export function from<T extends Record<string, unknown>>(
initialState: T | Doc<T>,
_opts?: ActorId | InitOptions<T>
): Doc<T> {
return change(init(_opts), d => Object.assign(d, initialState))
}
/**
* Update the contents of an automerge document
* @typeParam T - The type of the value contained in the document
* @param doc - The document to update
* @param options - Either a message, an {@link ChangeOptions}, or a {@link ChangeFn}
* @param callback - A `ChangeFn` to be used if `options` was a `string`
*
* Note that if the second argument is a function it will be used as the `ChangeFn` regardless of what the third argument is.
*
* @example A simple change
* ```
* let doc1 = automerge.init()
* doc1 = automerge.change(doc1, d => {
* d.key = "value"
* })
* assert.equal(doc1.key, "value")
* ```
*
* @example A change with a message
*
* ```
* doc1 = automerge.change(doc1, "add another value", d => {
* d.key2 = "value2"
* })
* ```
*
* @example A change with a message and a timestamp
*
* ```
* doc1 = automerge.change(doc1, {message: "add another value", time: 1640995200}, d => {
* d.key2 = "value2"
* })
* ```
*
* @example responding to a patch callback
* ```
* let patchedPath
* let patchCallback = patch => {
* patchedPath = patch.path
* }
* doc1 = automerge.change(doc1, {message, "add another value", time: 1640995200, patchCallback}, d => {
* d.key2 = "value2"
* })
* assert.equal(patchedPath, ["key2"])
* ```
*/
export function change<T>(
doc: Doc<T>,
options: string | ChangeOptions<T> | ChangeFn<T>,
callback?: ChangeFn<T>
): Doc<T> {
if (typeof options === "function") {
return _change(doc, {}, options)
} else if (typeof callback === "function") {
if (typeof options === "string") {
options = { message: options }
}
return _change(doc, options, callback)
} else {
throw RangeError("Invalid args for change")
}
}
function progressDocument<T>(
doc: Doc<T>,
heads: Heads | null,
callback?: PatchCallback<T>
): Doc<T> {
if (heads == null) {
return doc
}
const state = _state(doc)
const nextState = { ...state, heads: undefined }
const nextDoc = state.handle.applyPatches(doc, nextState, callback)
state.heads = heads
return nextDoc
}
function _change<T>(
doc: Doc<T>,
options: ChangeOptions<T>,
callback: ChangeFn<T>
): Doc<T> {
if (typeof callback !== "function") {
throw new RangeError("invalid change function")
}
const state = _state(doc)
if (doc === undefined || state === undefined) {
throw new RangeError("must be the document root")
}
if (state.heads) {
throw new RangeError(
"Attempting to change an outdated document. Use Automerge.clone() if you wish to make a writable copy."
)
}
if (_is_proxy(doc)) {
throw new RangeError("Calls to Automerge.change cannot be nested")
}
const heads = state.handle.getHeads()
try {
state.heads = heads
const root: T = rootProxy(state.handle, state.textV2)
callback(root as Extend<T>)
if (state.handle.pendingOps() === 0) {
state.heads = undefined
return doc
} else {
state.handle.commit(options.message, options.time)
return progressDocument(
doc,
heads,
options.patchCallback || state.patchCallback
)
}
} catch (e) {
state.heads = undefined
state.handle.rollback()
throw e
}
}
/**
* Make a change to a document which does not modify the document
*
* @param doc - The doc to add the empty change to
* @param options - Either a message or a {@link ChangeOptions} for the new change
*
* Why would you want to do this? One reason might be that you have merged
* changes from some other peers and you want to generate a change which
* depends on those merged changes so that you can sign the new change with all
* of the merged changes as part of the new change.
*/
export function emptyChange<T>(
doc: Doc<T>,
options: string | ChangeOptions<T> | void
) {
if (options === undefined) {
options = {}
}
if (typeof options === "string") {
options = { message: options }
}
const state = _state(doc)
if (state.heads) {
throw new RangeError(
"Attempting to change an outdated document. Use Automerge.clone() if you wish to make a writable copy."
)
}
if (_is_proxy(doc)) {
throw new RangeError("Calls to Automerge.change cannot be nested")
}
const heads = state.handle.getHeads()
state.handle.emptyChange(options.message, options.time)
return progressDocument(doc, heads)
}
/**
* Load an automerge document from a compressed document produce by {@link save}
*
* @typeParam T - The type of the value which is contained in the document.
* Note that no validation is done to make sure this type is in
* fact the type of the contained value so be a bit careful
* @param data - The compressed document
* @param _opts - Either an actor ID or some {@link InitOptions}, if the actor
* ID is null a random actor ID will be created
*
* Note that `load` will throw an error if passed incomplete content (for
* example if you are receiving content over the network and don't know if you
* have the complete document yet). If you need to handle incomplete content use
* {@link init} followed by {@link loadIncremental}.
*/
export function load<T>(
data: Uint8Array,
_opts?: ActorId | InitOptions<T>
): Doc<T> {
const opts = importOpts(_opts)
const actor = opts.actor
const patchCallback = opts.patchCallback
const handle = ApiHandler.load(data, opts.enableTextV2 || false, actor)
handle.enablePatches(true)
handle.enableFreeze(!!opts.freeze)
handle.registerDatatype("counter", (n: number) => new Counter(n))
const textV2 = opts.enableTextV2 || false
if (textV2) {
handle.registerDatatype("str", (n: string) => new RawString(n))
} else {
handle.registerDatatype("text", (n: string) => new Text(n))
}
const doc = handle.materialize("/", undefined, {
handle,
heads: undefined,
patchCallback,
textV2,
}) as Doc<T>
return doc
}
/**
* Load changes produced by {@link saveIncremental}, or partial changes
*
* @typeParam T - The type of the value which is contained in the document.
* Note that no validation is done to make sure this type is in
* fact the type of the contained value so be a bit careful
* @param data - The compressedchanges
* @param opts - an {@link ApplyOptions}
*
* This function is useful when staying up to date with a connected peer.
* Perhaps the other end sent you a full compresed document which you loaded
* with {@link load} and they're sending you the result of
* {@link getLastLocalChange} every time they make a change.
*
* Note that this function will succesfully load the results of {@link save} as
* well as {@link getLastLocalChange} or any other incremental change.
*/
export function loadIncremental<T>(
doc: Doc<T>,
data: Uint8Array,
opts?: ApplyOptions<T>
): Doc<T> {
if (!opts) {
opts = {}
}
const state = _state(doc)
if (state.heads) {
throw new RangeError(
"Attempting to change an out of date document - set at: " + _trace(doc)
)
}
if (_is_proxy(doc)) {
throw new RangeError("Calls to Automerge.change cannot be nested")
}
const heads = state.handle.getHeads()
state.handle.loadIncremental(data)
return progressDocument(doc, heads, opts.patchCallback || state.patchCallback)
}
/**
* Export the contents of a document to a compressed format
*
* @param doc - The doc to save
*
* The returned bytes can be passed to {@link load} or {@link loadIncremental}
*/
export function save<T>(doc: Doc<T>): Uint8Array {
return _state(doc).handle.save()
}
/**
* Merge `local` into `remote`
* @typeParam T - The type of values contained in each document
* @param local - The document to merge changes into
* @param remote - The document to merge changes from
*
* @returns - The merged document
*
* Often when you are merging documents you will also need to clone them. Both
* arguments to `merge` are frozen after the call so you can no longer call
* mutating methods (such as {@link change}) on them. The symtom of this will be
* an error which says "Attempting to change an out of date document". To
* overcome this call {@link clone} on the argument before passing it to {@link
* merge}.
*/
export function merge<T>(local: Doc<T>, remote: Doc<T>): Doc<T> {
const localState = _state(local)
if (localState.heads) {
throw new RangeError(
"Attempting to change an out of date document - set at: " + _trace(local)
)
}
const heads = localState.handle.getHeads()
const remoteState = _state(remote)
const changes = localState.handle.getChangesAdded(remoteState.handle)
localState.handle.applyChanges(changes)
return progressDocument(local, heads, localState.patchCallback)
}
/**
* Get the actor ID associated with the document
*/
export function getActorId<T>(doc: Doc<T>): ActorId {
const state = _state(doc)
return state.handle.getActorId()
}
/**
* The type of conflicts for particular key or index
*
* Maps and sequences in automerge can contain conflicting values for a
* particular key or index. In this case {@link getConflicts} can be used to
* obtain a `Conflicts` representing the multiple values present for the property
*
* A `Conflicts` is a map from a unique (per property or index) key to one of
* the possible conflicting values for the given property.
*/
type Conflicts = { [key: string]: AutomergeValue }
/**
* Get the conflicts associated with a property
*
* The values of properties in a map in automerge can be conflicted if there
* are concurrent "put" operations to the same key. Automerge chooses one value
* arbitrarily (but deterministically, any two nodes who have the same set of
* changes will choose the same value) from the set of conflicting values to
* present as the value of the key.
*
* Sometimes you may want to examine these conflicts, in this case you can use
* {@link getConflicts} to get the conflicts for the key.
*
* @example
* ```
* import * as automerge from "@automerge/automerge"
*
* type Profile = {
* pets: Array<{name: string, type: string}>
* }
*
* let doc1 = automerge.init<Profile>("aaaa")
* doc1 = automerge.change(doc1, d => {
* d.pets = [{name: "Lassie", type: "dog"}]
* })
* let doc2 = automerge.init<Profile>("bbbb")
* doc2 = automerge.merge(doc2, automerge.clone(doc1))
*
* doc2 = automerge.change(doc2, d => {
* d.pets[0].name = "Beethoven"
* })
*
* doc1 = automerge.change(doc1, d => {
* d.pets[0].name = "Babe"
* })
*
* const doc3 = automerge.merge(doc1, doc2)
*
* // Note that here we pass `doc3.pets`, not `doc3`
* let conflicts = automerge.getConflicts(doc3.pets[0], "name")
*
* // The two conflicting values are the keys of the conflicts object
* assert.deepEqual(Object.values(conflicts), ["Babe", Beethoven"])
* ```
*/
export function getConflicts<T>(
doc: Doc<T>,
prop: Prop
): Conflicts | undefined {
const state = _state(doc, false)
if (state.textV2) {
throw new Error("use unstable.getConflicts for an unstable document")
}
const objectId = _obj(doc)
if (objectId != null) {
return stableConflictAt(state.handle, objectId, prop)
} else {
return undefined
}
}
/**
* Get the binary representation of the last change which was made to this doc
*
* This is most useful when staying in sync with other peers, every time you
* make a change locally via {@link change} you immediately call {@link
* getLastLocalChange} and send the result over the network to other peers.
*/
export function getLastLocalChange<T>(doc: Doc<T>): Change | undefined {
const state = _state(doc)
return state.handle.getLastLocalChange() || undefined
}
/**
* Return the object ID of an arbitrary javascript value
*
* This is useful to determine if something is actually an automerge document,
* if `doc` is not an automerge document this will return null.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getObjectId(doc: any, prop?: Prop): ObjID | null {
if (prop) {
const state = _state(doc, false)
const objectId = _obj(doc)
if (!state || !objectId) {
return null
}
return state.handle.get(objectId, prop) as ObjID
} else {
return _obj(doc)
}
}
/**
* Get the changes which are in `newState` but not in `oldState`. The returned
* changes can be loaded in `oldState` via {@link applyChanges}.
*
* Note that this will crash if there are changes in `oldState` which are not in `newState`.
*/
export function getChanges<T>(oldState: Doc<T>, newState: Doc<T>): Change[] {
const n = _state(newState)
return n.handle.getChanges(getHeads(oldState))
}
/**
* Get all the changes in a document
*
* This is different to {@link save} because the output is an array of changes
* which can be individually applied via {@link applyChanges}`
*
*/
export function getAllChanges<T>(doc: Doc<T>): Change[] {
const state = _state(doc)
return state.handle.getChanges([])
}
/**
* Apply changes received from another document
*
* `doc` will be updated to reflect the `changes`. If there are changes which
* we do not have dependencies for yet those will be stored in the document and
* applied when the depended on changes arrive.
*
* You can use the {@link ApplyOptions} to pass a patchcallback which will be
* informed of any changes which occur as a result of applying the changes
*
*/
export function applyChanges<T>(
doc: Doc<T>,
changes: Change[],
opts?: ApplyOptions<T>
): [Doc<T>] {
const state = _state(doc)
if (!opts) {
opts = {}
}
if (state.heads) {
throw new RangeError(
"Attempting to change an outdated document. Use Automerge.clone() if you wish to make a writable copy."
)
}
if (_is_proxy(doc)) {
throw new RangeError("Calls to Automerge.change cannot be nested")
}
const heads = state.handle.getHeads()
state.handle.applyChanges(changes)
state.heads = heads
return [
progressDocument(doc, heads, opts.patchCallback || state.patchCallback),
]
}
/** @hidden */
export function getHistory<T>(doc: Doc<T>): State<T>[] {
const textV2 = _state(doc).textV2
const history = getAllChanges(doc)
return history.map((change, index) => ({
get change() {
return decodeChange(change)
},
get snapshot() {
const [state] = applyChanges(
init({ enableTextV2: textV2 }),
history.slice(0, index + 1)
)
return <T>state
},
}))
}
/** @hidden */
// FIXME : no tests
// FIXME can we just use deep equals now?
export function equals(val1: unknown, val2: unknown): boolean {
if (!isObject(val1) || !isObject(val2)) return val1 === val2
const keys1 = Object.keys(val1).sort(),
keys2 = Object.keys(val2).sort()
if (keys1.length !== keys2.length) return false
for (let i = 0; i < keys1.length; i++) {
if (keys1[i] !== keys2[i]) return false
if (!equals(val1[keys1[i]], val2[keys2[i]])) return false
}
return true
}
/**
* encode a {@link SyncState} into binary to send over the network
*
* @group sync
* */
export function encodeSyncState(state: SyncState): Uint8Array {
const sync = ApiHandler.importSyncState(state)
const result = ApiHandler.encodeSyncState(sync)
sync.free()
return result
}
/**
* Decode some binary data into a {@link SyncState}
*
* @group sync
*/
export function decodeSyncState(state: Uint8Array): SyncState {
const sync = ApiHandler.decodeSyncState(state)
const result = ApiHandler.exportSyncState(sync)
sync.free()
return result as SyncState
}
/**
* Generate a sync message to send to the peer represented by `inState`
* @param doc - The doc to generate messages about
* @param inState - The {@link SyncState} representing the peer we are talking to
*
* @group sync
*
* @returns An array of `[newSyncState, syncMessage | null]` where
* `newSyncState` should replace `inState` and `syncMessage` should be sent to
* the peer if it is not null. If `syncMessage` is null then we are up to date.
*/
export function generateSyncMessage<T>(
doc: Doc<T>,
inState: SyncState
): [SyncState, SyncMessage | null] {
const state = _state(doc)
const syncState = ApiHandler.importSyncState(inState)
const message = state.handle.generateSyncMessage(syncState)
const outState = ApiHandler.exportSyncState(syncState) as SyncState
return [outState, message]
}
/**
* Update a document and our sync state on receiving a sync message
*
* @group sync
*
* @param doc - The doc the sync message is about
* @param inState - The {@link SyncState} for the peer we are communicating with
* @param message - The message which was received
* @param opts - Any {@link ApplyOption}s, used for passing a
* {@link PatchCallback} which will be informed of any changes
* in `doc` which occur because of the received sync message.
*
* @returns An array of `[newDoc, newSyncState, syncMessage | null]` where
* `newDoc` is the updated state of `doc`, `newSyncState` should replace
* `inState` and `syncMessage` should be sent to the peer if it is not null. If
* `syncMessage` is null then we are up to date.
*/
export function receiveSyncMessage<T>(
doc: Doc<T>,
inState: SyncState,
message: SyncMessage,
opts?: ApplyOptions<T>
): [Doc<T>, SyncState, null] {
const syncState = ApiHandler.importSyncState(inState)
if (!opts) {
opts = {}
}
const state = _state(doc)
if (state.heads) {
throw new RangeError(
"Attempting to change an outdated document. Use Automerge.clone() if you wish to make a writable copy."
)
}
if (_is_proxy(doc)) {
throw new RangeError("Calls to Automerge.change cannot be nested")
}
const heads = state.handle.getHeads()
state.handle.receiveSyncMessage(syncState, message)
const outSyncState = ApiHandler.exportSyncState(syncState) as SyncState
return [
progressDocument(doc, heads, opts.patchCallback || state.patchCallback),
outSyncState,
null,
]
}
/**
* Create a new, blank {@link SyncState}
*
* When communicating with a peer for the first time use this to generate a new
* {@link SyncState} for them
*
* @group sync
*/
export function initSyncState(): SyncState {
return ApiHandler.exportSyncState(ApiHandler.initSyncState()) as SyncState
}
/** @hidden */
export function encodeChange(change: ChangeToEncode): Change {
return ApiHandler.encodeChange(change)
}
/** @hidden */
export function decodeChange(data: Change): DecodedChange {
return ApiHandler.decodeChange(data)
}
/** @hidden */
export function encodeSyncMessage(message: DecodedSyncMessage): SyncMessage {
return ApiHandler.encodeSyncMessage(message)
}
/** @hidden */
export function decodeSyncMessage(message: SyncMessage): DecodedSyncMessage {
return ApiHandler.decodeSyncMessage(message)
}
/**
* Get any changes in `doc` which are not dependencies of `heads`
*/
export function getMissingDeps<T>(doc: Doc<T>, heads: Heads): Heads {
const state = _state(doc)
return state.handle.getMissingDeps(heads)
}
/**
* Get the hashes of the heads of this document
*/
export function getHeads<T>(doc: Doc<T>): Heads {
const state = _state(doc)
return state.heads || state.handle.getHeads()
}
/** @hidden */
export function dump<T>(doc: Doc<T>) {
const state = _state(doc)
state.handle.dump()
}
/** @hidden */
export function toJS<T>(doc: Doc<T>): T {
const state = _state(doc)
const enabled = state.handle.enableFreeze(false)
const result = state.handle.materialize()
state.handle.enableFreeze(enabled)
return result as T
}
export function isAutomerge(doc: unknown): boolean {
if (typeof doc == "object" && doc !== null) {
return getObjectId(doc) === "_root" && !!Reflect.get(doc, STATE)
} else {
return false
}
}
function isObject(obj: unknown): obj is Record<string, unknown> {
return typeof obj === "object" && obj !== null
}
export type {
API,
SyncState,
ActorId,
Conflicts,
Prop,
Change,
ObjID,
DecodedChange,
DecodedSyncMessage,
Heads,
MaterializeValue,
}

224
javascript/src/text.ts Normal file
View file

@ -0,0 +1,224 @@
import type { Value } from "@automerge/automerge-wasm"
import { TEXT, STATE } from "./constants"
import type { InternalState } from "./internal_state"
export class Text {
//eslint-disable-next-line @typescript-eslint/no-explicit-any
elems: Array<any>
str: string | undefined
//eslint-disable-next-line @typescript-eslint/no-explicit-any
spans: Array<any> | undefined;
//eslint-disable-next-line @typescript-eslint/no-explicit-any
[STATE]?: InternalState<any>
constructor(text?: string | string[] | Value[]) {
if (typeof text === "string") {
this.elems = [...text]
} else if (Array.isArray(text)) {
this.elems = text
} else if (text === undefined) {
this.elems = []
} else {
throw new TypeError(`Unsupported initial value for Text: ${text}`)
}
Reflect.defineProperty(this, TEXT, { value: true })
}
get length(): number {
return this.elems.length
}
//eslint-disable-next-line @typescript-eslint/no-explicit-any
get(index: number): any {
return this.elems[index]
}
/**
* Iterates over the text elements character by character, including any
* inline objects.
*/
[Symbol.iterator]() {
const elems = this.elems
let index = -1
return {
next() {
index += 1
if (index < elems.length) {
return { done: false, value: elems[index] }
} else {
return { done: true }
}
},
}
}
/**
* Returns the content of the Text object as a simple string, ignoring any
* non-character elements.
*/
toString(): string {
if (!this.str) {
// Concatting to a string is faster than creating an array and then
// .join()ing for small (<100KB) arrays.
// https://jsperf.com/join-vs-loop-w-type-test
this.str = ""
for (const elem of this.elems) {
if (typeof elem === "string") this.str += elem
else this.str += "\uFFFC"
}
}
return this.str
}
/**
* Returns the content of the Text object as a sequence of strings,
* interleaved with non-character elements.
*
* For example, the value `['a', 'b', {x: 3}, 'c', 'd']` has spans:
* `=> ['ab', {x: 3}, 'cd']`
*/
toSpans(): Array<Value | object> {
if (!this.spans) {
this.spans = []
let chars = ""
for (const elem of this.elems) {
if (typeof elem === "string") {
chars += elem
} else {
if (chars.length > 0) {
this.spans.push(chars)
chars = ""
}
this.spans.push(elem)
}
}
if (chars.length > 0) {
this.spans.push(chars)
}
}
return this.spans
}
/**
* Returns the content of the Text object as a simple string, so that the
* JSON serialization of an Automerge document represents text nicely.
*/
toJSON(): string {
return this.toString()
}
/**
* Updates the list item at position `index` to a new value `value`.
*/
set(index: number, value: Value) {
if (this[STATE]) {
throw new RangeError(
"object cannot be modified outside of a change block"
)
}
this.elems[index] = value
}
/**
* Inserts new list items `values` starting at position `index`.
*/
insertAt(index: number, ...values: Array<Value | object>) {
if (this[STATE]) {
throw new RangeError(
"object cannot be modified outside of a change block"
)
}
this.elems.splice(index, 0, ...values)
}
/**
* Deletes `numDelete` list items starting at position `index`.
* if `numDelete` is not given, one item is deleted.
*/
deleteAt(index: number, numDelete = 1) {
if (this[STATE]) {
throw new RangeError(
"object cannot be modified outside of a change block"
)
}
this.elems.splice(index, numDelete)
}
map<T>(callback: (e: Value | object) => T) {
this.elems.map(callback)
}
lastIndexOf(searchElement: Value, fromIndex?: number) {
this.elems.lastIndexOf(searchElement, fromIndex)
}
concat(other: Text): Text {
return new Text(this.elems.concat(other.elems))
}
every(test: (v: Value) => boolean): boolean {
return this.elems.every(test)
}
filter(test: (v: Value) => boolean): Text {
return new Text(this.elems.filter(test))
}
find(test: (v: Value) => boolean): Value | undefined {
return this.elems.find(test)
}
findIndex(test: (v: Value) => boolean): number | undefined {
return this.elems.findIndex(test)
}
forEach(f: (v: Value) => undefined) {
this.elems.forEach(f)
}
includes(elem: Value): boolean {
return this.elems.includes(elem)
}
indexOf(elem: Value) {
return this.elems.indexOf(elem)
}
join(sep?: string): string {
return this.elems.join(sep)
}
reduce(
f: (
previousValue: Value,
currentValue: Value,
currentIndex: number,
array: Value[]
) => Value
) {
this.elems.reduce(f)
}
reduceRight(
f: (
previousValue: Value,
currentValue: Value,
currentIndex: number,
array: Value[]
) => Value
) {
this.elems.reduceRight(f)
}
slice(start?: number, end?: number) {
new Text(this.elems.slice(start, end))
}
some(test: (arg: Value) => boolean): boolean {
return this.elems.some(test)
}
toLocaleString() {
this.toString()
}
}

46
javascript/src/types.ts Normal file
View file

@ -0,0 +1,46 @@
export { Text } from "./text"
import { Text } from "./text"
export { Counter } from "./counter"
export { Int, Uint, Float64 } from "./numbers"
import { Counter } from "./counter"
import type { Patch } from "@automerge/automerge-wasm"
export type { Patch } from "@automerge/automerge-wasm"
export type AutomergeValue =
| ScalarValue
| { [key: string]: AutomergeValue }
| Array<AutomergeValue>
| Text
export type MapValue = { [key: string]: AutomergeValue }
export type ListValue = Array<AutomergeValue>
export type ScalarValue =
| string
| number
| null
| boolean
| Date
| Counter
| Uint8Array
/**
* An automerge document.
* @typeParam T - The type of the value contained in this document
*
* Note that this provides read only access to the fields of the value. To
* modify the value use {@link change}
*/
export type Doc<T> = { readonly [P in keyof T]: T[P] }
/**
* Callback which is called by various methods in this library to notify the
* user of what changes have been made.
* @param patch - A description of the changes made
* @param before - The document before the change was made
* @param after - The document after the change was made
*/
export type PatchCallback<T> = (
patches: Array<Patch>,
before: Doc<T>,
after: Doc<T>
) => void

294
javascript/src/unstable.ts Normal file
View file

@ -0,0 +1,294 @@
/**
* # The unstable API
*
* This module contains new features we are working on which are either not yet
* ready for a stable release and/or which will result in backwards incompatible
* API changes. The API of this module may change in arbitrary ways between
* point releases - we will always document what these changes are in the
* [CHANGELOG](#changelog) below, but only depend on this module if you are prepared to deal
* with frequent changes.
*
* ## Differences from stable
*
* In the stable API text objects are represented using the {@link Text} class.
* This means you must decide up front whether your string data might need
* concurrent merges in the future and if you change your mind you have to
* figure out how to migrate your data. In the unstable API the `Text` class is
* gone and all `string`s are represented using the text CRDT, allowing for
* concurrent changes. Modifying a string is done using the {@link splice}
* function. You can still access the old behaviour of strings which do not
* support merging behaviour via the {@link RawString} class.
*
* This leads to the following differences from `stable`:
*
* * There is no `unstable.Text` class, all strings are text objects
* * Reading strings in an `unstable` document is the same as reading any other
* javascript string
* * To modify strings in an `unstable` document use {@link splice}
* * The {@link AutomergeValue} type does not include the {@link Text}
* class but the {@link RawString} class is included in the {@link ScalarValue}
* type
*
* ## CHANGELOG
* * Introduce this module to expose the new API which has no `Text` class
*
*
* @module
*/
export {
Counter,
type Doc,
Int,
Uint,
Float64,
type Patch,
type PatchCallback,
type AutomergeValue,
type ScalarValue,
} from "./unstable_types"
import type { PatchCallback } from "./stable"
import { type UnstableConflicts as Conflicts } from "./conflicts"
import { unstableConflictAt } from "./conflicts"
export type {
PutPatch,
DelPatch,
SpliceTextPatch,
InsertPatch,
IncPatch,
SyncMessage,
} from "@automerge/automerge-wasm"
export type { ChangeOptions, ApplyOptions, ChangeFn } from "./stable"
export {
view,
free,
getHeads,
change,
emptyChange,
loadIncremental,
save,
merge,
getActorId,
getLastLocalChange,
getChanges,
getAllChanges,
applyChanges,
getHistory,
equals,
encodeSyncState,
decodeSyncState,
generateSyncMessage,
receiveSyncMessage,
initSyncState,
encodeChange,
decodeChange,
encodeSyncMessage,
decodeSyncMessage,
getMissingDeps,
dump,
toJS,
isAutomerge,
getObjectId,
} from "./stable"
export type InitOptions<T> = {
/** The actor ID to use for this document, a random one will be generated if `null` is passed */
actor?: ActorId
freeze?: boolean
/** A callback which will be called with the initial patch once the document has finished loading */
patchCallback?: PatchCallback<T>
}
import { ActorId, Doc } from "./stable"
import * as stable from "./stable"
export { RawString } from "./raw_string"
/** @hidden */
export const getBackend = stable.getBackend
import { _is_proxy, _state, _obj } from "./internal_state"
/**
* Create a new automerge document
*
* @typeParam T - The type of value contained in the document. This will be the
* type that is passed to the change closure in {@link change}
* @param _opts - Either an actorId or an {@link InitOptions} (which may
* contain an actorId). If this is null the document will be initialised with a
* random actor ID
*/
export function init<T>(_opts?: ActorId | InitOptions<T>): Doc<T> {
const opts = importOpts(_opts)
opts.enableTextV2 = true
return stable.init(opts)
}
/**
* Make a full writable copy of an automerge document
*
* @remarks
* Unlike {@link view} this function makes a full copy of the memory backing
* the document and can thus be passed to {@link change}. It also generates a
* new actor ID so that changes made in the new document do not create duplicate
* sequence numbers with respect to the old document. If you need control over
* the actor ID which is generated you can pass the actor ID as the second
* argument
*
* @typeParam T - The type of the value contained in the document
* @param doc - The document to clone
* @param _opts - Either an actor ID to use for the new doc or an {@link InitOptions}
*/
export function clone<T>(
doc: Doc<T>,
_opts?: ActorId | InitOptions<T>
): Doc<T> {
const opts = importOpts(_opts)
opts.enableTextV2 = true
return stable.clone(doc, opts)
}
/**
* Create an automerge document from a POJO
*
* @param initialState - The initial state which will be copied into the document
* @typeParam T - The type of the value passed to `from` _and_ the type the resulting document will contain
* @typeParam actor - The actor ID of the resulting document, if this is null a random actor ID will be used
*
* @example
* ```
* const doc = automerge.from({
* tasks: [
* {description: "feed dogs", done: false}
* ]
* })
* ```
*/
export function from<T extends Record<string, unknown>>(
initialState: T | Doc<T>,
_opts?: ActorId | InitOptions<T>
): Doc<T> {
const opts = importOpts(_opts)
opts.enableTextV2 = true
return stable.from(initialState, opts)
}
/**
* Load an automerge document from a compressed document produce by {@link save}
*
* @typeParam T - The type of the value which is contained in the document.
* Note that no validation is done to make sure this type is in
* fact the type of the contained value so be a bit careful
* @param data - The compressed document
* @param _opts - Either an actor ID or some {@link InitOptions}, if the actor
* ID is null a random actor ID will be created
*
* Note that `load` will throw an error if passed incomplete content (for
* example if you are receiving content over the network and don't know if you
* have the complete document yet). If you need to handle incomplete content use
* {@link init} followed by {@link loadIncremental}.
*/
export function load<T>(
data: Uint8Array,
_opts?: ActorId | InitOptions<T>
): Doc<T> {
const opts = importOpts(_opts)
opts.enableTextV2 = true
return stable.load(data, opts)
}
function importOpts<T>(
_actor?: ActorId | InitOptions<T>
): stable.InitOptions<T> {
if (typeof _actor === "object") {
return _actor
} else {
return { actor: _actor }
}
}
export function splice<T>(
doc: Doc<T>,
prop: stable.Prop,
index: number,
del: number,
newText?: string
) {
if (!_is_proxy(doc)) {
throw new RangeError("object cannot be modified outside of a change block")
}
const state = _state(doc, false)
const objectId = _obj(doc)
if (!objectId) {
throw new RangeError("invalid object for splice")
}
const value = `${objectId}/${prop}`
try {
return state.handle.splice(value, index, del, newText)
} catch (e) {
throw new RangeError(`Cannot splice: ${e}`)
}
}
/**
* Get the conflicts associated with a property
*
* The values of properties in a map in automerge can be conflicted if there
* are concurrent "put" operations to the same key. Automerge chooses one value
* arbitrarily (but deterministically, any two nodes who have the same set of
* changes will choose the same value) from the set of conflicting values to
* present as the value of the key.
*
* Sometimes you may want to examine these conflicts, in this case you can use
* {@link getConflicts} to get the conflicts for the key.
*
* @example
* ```
* import * as automerge from "@automerge/automerge"
*
* type Profile = {
* pets: Array<{name: string, type: string}>
* }
*
* let doc1 = automerge.init<Profile>("aaaa")
* doc1 = automerge.change(doc1, d => {
* d.pets = [{name: "Lassie", type: "dog"}]
* })
* let doc2 = automerge.init<Profile>("bbbb")
* doc2 = automerge.merge(doc2, automerge.clone(doc1))
*
* doc2 = automerge.change(doc2, d => {
* d.pets[0].name = "Beethoven"
* })
*
* doc1 = automerge.change(doc1, d => {
* d.pets[0].name = "Babe"
* })
*
* const doc3 = automerge.merge(doc1, doc2)
*
* // Note that here we pass `doc3.pets`, not `doc3`
* let conflicts = automerge.getConflicts(doc3.pets[0], "name")
*
* // The two conflicting values are the keys of the conflicts object
* assert.deepEqual(Object.values(conflicts), ["Babe", Beethoven"])
* ```
*/
export function getConflicts<T>(
doc: Doc<T>,
prop: stable.Prop
): Conflicts | undefined {
const state = _state(doc, false)
if (!state.textV2) {
throw new Error("use getConflicts for a stable document")
}
const objectId = _obj(doc)
if (objectId != null) {
return unstableConflictAt(state.handle, objectId, prop)
} else {
return undefined
}
}

View file

@ -0,0 +1,30 @@
import { Counter } from "./types"
export {
Counter,
type Doc,
Int,
Uint,
Float64,
type Patch,
type PatchCallback,
} from "./types"
import { RawString } from "./raw_string"
export { RawString } from "./raw_string"
export type AutomergeValue =
| ScalarValue
| { [key: string]: AutomergeValue }
| Array<AutomergeValue>
export type MapValue = { [key: string]: AutomergeValue }
export type ListValue = Array<AutomergeValue>
export type ScalarValue =
| string
| number
| null
| boolean
| Date
| Counter
| Uint8Array
| RawString

View file

@ -0,0 +1,26 @@
import * as v4 from "https://deno.land/x/uuid@v0.1.2/mod.ts"
// this file is a deno only port of the uuid module
function defaultFactory() {
return v4.uuid().replace(/-/g, "")
}
let factory = defaultFactory
interface UUIDFactory extends Function {
setFactory(f: typeof factory): void
reset(): void
}
export const uuid: UUIDFactory = () => {
return factory()
}
uuid.setFactory = newFactory => {
factory = newFactory
}
uuid.reset = () => {
factory = defaultFactory
}

24
javascript/src/uuid.ts Normal file
View file

@ -0,0 +1,24 @@
import { v4 } from "uuid"
function defaultFactory() {
return v4().replace(/-/g, "")
}
let factory = defaultFactory
interface UUIDFactory extends Function {
setFactory(f: typeof factory): void
reset(): void
}
export const uuid: UUIDFactory = () => {
return factory()
}
uuid.setFactory = newFactory => {
factory = newFactory
}
uuid.reset = () => {
factory = defaultFactory
}

View file

@ -0,0 +1,488 @@
import * as assert from "assert"
import { unstable as Automerge } from "../src"
import * as WASM from "@automerge/automerge-wasm"
describe("Automerge", () => {
describe("basics", () => {
it("should init clone and free", () => {
let doc1 = Automerge.init()
let doc2 = Automerge.clone(doc1)
// this is only needed if weakrefs are not supported
Automerge.free(doc1)
Automerge.free(doc2)
})
it("should be able to make a view with specifc heads", () => {
let doc1 = Automerge.init<any>()
let doc2 = Automerge.change(doc1, d => (d.value = 1))
let heads2 = Automerge.getHeads(doc2)
let doc3 = Automerge.change(doc2, d => (d.value = 2))
let doc2_v2 = Automerge.view(doc3, heads2)
assert.deepEqual(doc2, doc2_v2)
let doc2_v2_clone = Automerge.clone(doc2, "aabbcc")
assert.deepEqual(doc2, doc2_v2_clone)
assert.equal(Automerge.getActorId(doc2_v2_clone), "aabbcc")
})
it("should allow you to change a clone of a view", () => {
let doc1 = Automerge.init<any>()
doc1 = Automerge.change(doc1, d => (d.key = "value"))
let heads = Automerge.getHeads(doc1)
doc1 = Automerge.change(doc1, d => (d.key = "value2"))
let fork = Automerge.clone(Automerge.view(doc1, heads))
assert.deepEqual(fork, { key: "value" })
fork = Automerge.change(fork, d => (d.key = "value3"))
assert.deepEqual(fork, { key: "value3" })
})
it("handle basic set and read on root object", () => {
let doc1 = Automerge.init<any>()
let doc2 = Automerge.change(doc1, d => {
d.hello = "world"
d.big = "little"
d.zip = "zop"
d.app = "dap"
assert.deepEqual(d, {
hello: "world",
big: "little",
zip: "zop",
app: "dap",
})
})
assert.deepEqual(doc2, {
hello: "world",
big: "little",
zip: "zop",
app: "dap",
})
})
it("should be able to insert and delete a large number of properties", () => {
let doc = Automerge.init<any>()
doc = Automerge.change(doc, doc => {
doc["k1"] = true
})
for (let idx = 1; idx <= 200; idx++) {
doc = Automerge.change(doc, doc => {
delete doc["k" + idx]
doc["k" + (idx + 1)] = true
assert(Object.keys(doc).length == 1)
})
}
})
it("can detect an automerge doc with isAutomerge()", () => {
const doc1 = Automerge.from({ sub: { object: true } })
assert(Automerge.isAutomerge(doc1))
assert(!Automerge.isAutomerge(doc1.sub))
assert(!Automerge.isAutomerge("String"))
assert(!Automerge.isAutomerge({ sub: { object: true } }))
assert(!Automerge.isAutomerge(undefined))
const jsObj = Automerge.toJS(doc1)
assert(!Automerge.isAutomerge(jsObj))
assert.deepEqual(jsObj, doc1)
})
it("it should recursively freeze the document if requested", () => {
let doc1 = Automerge.init<any>({ freeze: true })
let doc2 = Automerge.init<any>()
assert(Object.isFrozen(doc1))
assert(!Object.isFrozen(doc2))
// will also freeze sub objects
doc1 = Automerge.change(
doc1,
doc => (doc.book = { title: "how to win friends" })
)
doc2 = Automerge.merge(doc2, doc1)
assert(Object.isFrozen(doc1))
assert(Object.isFrozen(doc1.book))
assert(!Object.isFrozen(doc2))
assert(!Object.isFrozen(doc2.book))
// works on from
let doc3 = Automerge.from({ sub: { obj: "inner" } }, { freeze: true })
assert(Object.isFrozen(doc3))
assert(Object.isFrozen(doc3.sub))
// works on load
let doc4 = Automerge.load<any>(Automerge.save(doc3), { freeze: true })
assert(Object.isFrozen(doc4))
assert(Object.isFrozen(doc4.sub))
// follows clone
let doc5 = Automerge.clone(doc4)
assert(Object.isFrozen(doc5))
assert(Object.isFrozen(doc5.sub))
// toJS does not freeze
let exported = Automerge.toJS(doc5)
assert(!Object.isFrozen(exported))
})
it("handle basic sets over many changes", () => {
let doc1 = Automerge.init<any>()
let timestamp = new Date()
let counter = new Automerge.Counter(100)
let bytes = new Uint8Array([10, 11, 12])
let doc2 = Automerge.change(doc1, d => {
d.hello = "world"
})
let doc3 = Automerge.change(doc2, d => {
d.counter1 = counter
})
let doc4 = Automerge.change(doc3, d => {
d.timestamp1 = timestamp
})
let doc5 = Automerge.change(doc4, d => {
d.app = null
})
let doc6 = Automerge.change(doc5, d => {
d.bytes1 = bytes
})
let doc7 = Automerge.change(doc6, d => {
d.uint = new Automerge.Uint(1)
d.int = new Automerge.Int(-1)
d.float64 = new Automerge.Float64(5.5)
d.number1 = 100
d.number2 = -45.67
d.true = true
d.false = false
})
assert.deepEqual(doc7, {
hello: "world",
true: true,
false: false,
int: -1,
uint: 1,
float64: 5.5,
number1: 100,
number2: -45.67,
counter1: counter,
timestamp1: timestamp,
bytes1: bytes,
app: null,
})
let changes = Automerge.getAllChanges(doc7)
let t1 = Automerge.init()
let [t2] = Automerge.applyChanges(t1, changes)
assert.deepEqual(doc7, t2)
})
it("handle overwrites to values", () => {
let doc1 = Automerge.init<any>()
let doc2 = Automerge.change(doc1, d => {
d.hello = "world1"
})
let doc3 = Automerge.change(doc2, d => {
d.hello = "world2"
})
let doc4 = Automerge.change(doc3, d => {
d.hello = "world3"
})
let doc5 = Automerge.change(doc4, d => {
d.hello = "world4"
})
assert.deepEqual(doc5, { hello: "world4" })
})
it("handle set with object value", () => {
let doc1 = Automerge.init<any>()
let doc2 = Automerge.change(doc1, d => {
d.subobj = { hello: "world", subsubobj: { zip: "zop" } }
})
assert.deepEqual(doc2, {
subobj: { hello: "world", subsubobj: { zip: "zop" } },
})
})
it("handle simple list creation", () => {
let doc1 = Automerge.init<any>()
let doc2 = Automerge.change(doc1, d => (d.list = []))
assert.deepEqual(doc2, { list: [] })
})
it("handle simple lists", () => {
let doc1 = Automerge.init<any>()
let doc2 = Automerge.change(doc1, d => {
d.list = [1, 2, 3]
})
assert.deepEqual(doc2.list.length, 3)
assert.deepEqual(doc2.list[0], 1)
assert.deepEqual(doc2.list[1], 2)
assert.deepEqual(doc2.list[2], 3)
assert.deepEqual(doc2, { list: [1, 2, 3] })
// assert.deepStrictEqual(Automerge.toJS(doc2), { list: [1,2,3] })
let doc3 = Automerge.change(doc2, d => {
d.list[1] = "a"
})
assert.deepEqual(doc3.list.length, 3)
assert.deepEqual(doc3.list[0], 1)
assert.deepEqual(doc3.list[1], "a")
assert.deepEqual(doc3.list[2], 3)
assert.deepEqual(doc3, { list: [1, "a", 3] })
})
it("handle simple lists", () => {
let doc1 = Automerge.init<any>()
let doc2 = Automerge.change(doc1, d => {
d.list = [1, 2, 3]
})
let changes = Automerge.getChanges(doc1, doc2)
let docB1 = Automerge.init()
let [docB2] = Automerge.applyChanges(docB1, changes)
assert.deepEqual(docB2, doc2)
})
it("handle text", () => {
let doc1 = Automerge.init<any>()
let doc2 = Automerge.change(doc1, d => {
d.list = "hello"
Automerge.splice(d, "list", 2, 0, "Z")
})
let changes = Automerge.getChanges(doc1, doc2)
let docB1 = Automerge.init()
let [docB2] = Automerge.applyChanges(docB1, changes)
assert.deepEqual(docB2, doc2)
})
it("handle non-text strings", () => {
let doc1 = WASM.create(true)
doc1.put("_root", "text", "hello world")
let doc2 = Automerge.load<any>(doc1.save())
assert.throws(() => {
Automerge.change(doc2, d => {
Automerge.splice(d, "text", 1, 0, "Z")
})
}, /Cannot splice/)
})
it("have many list methods", () => {
let doc1 = Automerge.from({ list: [1, 2, 3] })
assert.deepEqual(doc1, { list: [1, 2, 3] })
let doc2 = Automerge.change(doc1, d => {
d.list.splice(1, 1, 9, 10)
})
assert.deepEqual(doc2, { list: [1, 9, 10, 3] })
let doc3 = Automerge.change(doc2, d => {
d.list.push(11, 12)
})
assert.deepEqual(doc3, { list: [1, 9, 10, 3, 11, 12] })
let doc4 = Automerge.change(doc3, d => {
d.list.unshift(2, 2)
})
assert.deepEqual(doc4, { list: [2, 2, 1, 9, 10, 3, 11, 12] })
let doc5 = Automerge.change(doc4, d => {
d.list.shift()
})
assert.deepEqual(doc5, { list: [2, 1, 9, 10, 3, 11, 12] })
let doc6 = Automerge.change(doc5, d => {
d.list.insertAt(3, 100, 101)
})
assert.deepEqual(doc6, { list: [2, 1, 9, 100, 101, 10, 3, 11, 12] })
})
it("allows access to the backend", () => {
let doc = Automerge.init()
assert.deepEqual(Object.keys(Automerge.getBackend(doc)), ["ptr"])
})
it("lists and text have indexof", () => {
let doc = Automerge.from({
list: [0, 1, 2, 3, 4, 5, 6],
text: "hello world",
})
assert.deepEqual(doc.list.indexOf(5), 5)
assert.deepEqual(doc.text.indexOf("world"), 6)
})
})
describe("emptyChange", () => {
it("should generate a hash", () => {
let doc = Automerge.init()
doc = Automerge.change<any>(doc, d => {
d.key = "value"
})
Automerge.save(doc)
let headsBefore = Automerge.getHeads(doc)
headsBefore.sort()
doc = Automerge.emptyChange(doc, "empty change")
let headsAfter = Automerge.getHeads(doc)
headsAfter.sort()
assert.notDeepEqual(headsBefore, headsAfter)
})
})
describe("proxy lists", () => {
it("behave like arrays", () => {
let doc = Automerge.from({
chars: ["a", "b", "c"],
numbers: [20, 3, 100],
repeats: [20, 20, 3, 3, 3, 3, 100, 100],
})
let r1: Array<number> = []
doc = Automerge.change(doc, d => {
assert.deepEqual((d.chars as any[]).concat([1, 2]), [
"a",
"b",
"c",
1,
2,
])
assert.deepEqual(
d.chars.map(n => n + "!"),
["a!", "b!", "c!"]
)
assert.deepEqual(
d.numbers.map(n => n + 10),
[30, 13, 110]
)
assert.deepEqual(d.numbers.toString(), "20,3,100")
assert.deepEqual(d.numbers.toLocaleString(), "20,3,100")
assert.deepEqual(
d.numbers.forEach((n: number) => r1.push(n)),
undefined
)
assert.deepEqual(
d.numbers.every(n => n > 1),
true
)
assert.deepEqual(
d.numbers.every(n => n > 10),
false
)
assert.deepEqual(
d.numbers.filter(n => n > 10),
[20, 100]
)
assert.deepEqual(
d.repeats.find(n => n < 10),
3
)
assert.deepEqual(
d.repeats.find(n => n < 10),
3
)
assert.deepEqual(
d.repeats.find(n => n < 0),
undefined
)
assert.deepEqual(
d.repeats.findIndex(n => n < 10),
2
)
assert.deepEqual(
d.repeats.findIndex(n => n < 0),
-1
)
assert.deepEqual(
d.repeats.findIndex(n => n < 10),
2
)
assert.deepEqual(
d.repeats.findIndex(n => n < 0),
-1
)
assert.deepEqual(d.numbers.includes(3), true)
assert.deepEqual(d.numbers.includes(-3), false)
assert.deepEqual(d.numbers.join("|"), "20|3|100")
assert.deepEqual(d.numbers.join(), "20,3,100")
assert.deepEqual(
d.numbers.some(f => f === 3),
true
)
assert.deepEqual(
d.numbers.some(f => f < 0),
false
)
assert.deepEqual(
d.numbers.reduce((sum, n) => sum + n, 100),
223
)
assert.deepEqual(
d.repeats.reduce((sum, n) => sum + n, 100),
352
)
assert.deepEqual(
d.chars.reduce((sum, n) => sum + n, "="),
"=abc"
)
assert.deepEqual(
d.chars.reduceRight((sum, n) => sum + n, "="),
"=cba"
)
assert.deepEqual(
d.numbers.reduceRight((sum, n) => sum + n, 100),
223
)
assert.deepEqual(d.repeats.lastIndexOf(3), 5)
assert.deepEqual(d.repeats.lastIndexOf(3, 3), 3)
})
doc = Automerge.change(doc, d => {
assert.deepEqual(d.numbers.fill(-1, 1, 2), [20, -1, 100])
assert.deepEqual(d.chars.fill("z", 1, 100), ["a", "z", "z"])
})
assert.deepEqual(r1, [20, 3, 100])
assert.deepEqual(doc.numbers, [20, -1, 100])
assert.deepEqual(doc.chars, ["a", "z", "z"])
})
})
it("should obtain the same conflicts, regardless of merge order", () => {
let s1 = Automerge.init<any>()
let s2 = Automerge.init<any>()
s1 = Automerge.change(s1, doc => {
doc.x = 1
doc.y = 2
})
s2 = Automerge.change(s2, doc => {
doc.x = 3
doc.y = 4
})
const m1 = Automerge.merge(Automerge.clone(s1), Automerge.clone(s2))
const m2 = Automerge.merge(Automerge.clone(s2), Automerge.clone(s1))
assert.deepStrictEqual(
Automerge.getConflicts(m1, "x"),
Automerge.getConflicts(m2, "x")
)
})
describe("getObjectId", () => {
let s1 = Automerge.from({
string: "string",
number: 1,
null: null,
date: new Date(),
counter: new Automerge.Counter(),
bytes: new Uint8Array(10),
text: "",
list: [],
map: {},
})
it("should return null for scalar values", () => {
assert.equal(Automerge.getObjectId(s1.string), null)
assert.equal(Automerge.getObjectId(s1.number), null)
assert.equal(Automerge.getObjectId(s1.null!), null)
assert.equal(Automerge.getObjectId(s1.date), null)
assert.equal(Automerge.getObjectId(s1.counter), null)
assert.equal(Automerge.getObjectId(s1.bytes), null)
})
it("should return _root for the root object", () => {
assert.equal(Automerge.getObjectId(s1), "_root")
})
it("should return non-null for map, list, text, and objects", () => {
assert.equal(Automerge.getObjectId(s1.text), null)
assert.notEqual(Automerge.getObjectId(s1.list), null)
assert.notEqual(Automerge.getObjectId(s1.map), null)
})
})
})

View file

@ -0,0 +1,28 @@
import * as assert from "assert"
import { unstable as Automerge } from "../src"
describe("Automerge", () => {
describe("basics", () => {
it("should allow you to load incrementally", () => {
let doc1 = Automerge.from<any>({ foo: "bar" })
let doc2 = Automerge.init<any>()
doc2 = Automerge.loadIncremental(doc2, Automerge.save(doc1))
doc1 = Automerge.change(doc1, d => (d.foo2 = "bar2"))
doc2 = Automerge.loadIncremental(
doc2,
Automerge.getBackend(doc1).saveIncremental()
)
doc1 = Automerge.change(doc1, d => (d.foo = "bar2"))
doc2 = Automerge.loadIncremental(
doc2,
Automerge.getBackend(doc1).saveIncremental()
)
doc1 = Automerge.change(doc1, d => (d.x = "y"))
doc2 = Automerge.loadIncremental(
doc2,
Automerge.getBackend(doc1).saveIncremental()
)
assert.deepEqual(doc1, doc2)
})
})
})

View file

@ -0,0 +1,36 @@
import * as assert from "assert"
import { Encoder } from "./legacy/encoding"
// Assertion that succeeds if the first argument deepStrictEquals at least one of the
// subsequent arguments (but we don't care which one)
export function assertEqualsOneOf(actual, ...expected) {
assert(expected.length > 0)
for (let i = 0; i < expected.length; i++) {
try {
assert.deepStrictEqual(actual, expected[i])
return // if we get here without an exception, that means success
} catch (e) {
if (e instanceof assert.AssertionError) {
if (!e.name.match(/^AssertionError/) || i === expected.length - 1)
throw e
} else {
throw e
}
}
}
}
/**
* Asserts that the byte array maintained by `encoder` contains the same byte
* sequence as the array `bytes`.
*/
export function checkEncoded(encoder, bytes, detail?) {
const encoded = encoder instanceof Encoder ? encoder.buffer : encoder
const expected = new Uint8Array(bytes)
const message =
(detail ? `${detail}: ` : "") + `${encoded} expected to equal ${expected}`
assert(encoded.byteLength === expected.byteLength, message)
for (let i = 0; i < encoded.byteLength; i++) {
assert(encoded[i] === expected[i], message)
}
}

File diff suppressed because it is too large Load diff

Some files were not shown because too many files have changed in this diff Show more