json-patch-to-crdt 0.0.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -58,28 +58,15 @@ try {
58
58
  Two peers can start from a shared state, apply patches independently, and merge:
59
59
 
60
60
  ```ts
61
- import {
62
- applyPatch,
63
- cloneDoc,
64
- createClock,
65
- createState,
66
- mergeState,
67
- toJson,
68
- type CrdtState,
69
- } from "json-patch-to-crdt";
61
+ import { applyPatch, createState, forkState, mergeState, toJson } from "json-patch-to-crdt";
70
62
 
71
63
  // Both peers start from the same origin state.
72
64
  const origin = createState({ count: 0, items: ["a"] }, { actor: "origin" });
73
65
 
74
- // Each peer gets a clone of the document with its own clock.
75
- const peerA: CrdtState = {
76
- doc: cloneDoc(origin.doc),
77
- clock: createClock("A", origin.clock.ctr),
78
- };
79
- const peerB: CrdtState = {
80
- doc: cloneDoc(origin.doc),
81
- clock: createClock("B", origin.clock.ctr),
82
- };
66
+ // Fork shared-origin replicas with local actor identities.
67
+ // Actor IDs must be unique per live peer (same-actor reuse is rejected by default).
68
+ const peerA = forkState(origin, "A");
69
+ const peerB = forkState(origin, "B");
83
70
 
84
71
  // Peers diverge with independent edits.
85
72
  const a1 = applyPatch(peerA, [
@@ -112,9 +99,9 @@ console.log(toJson(converged));
112
99
 
113
100
  ## Concepts
114
101
 
115
- - **Doc**: CRDT document node graph.
116
- - **State**: `{ doc, clock }`, where `clock` yields new dots.
117
- - **Base snapshot**: explicit JSON or CRDT doc used to interpret array indices and compute deltas.
102
+ - **Doc**: CRDT document node graph (primarily an internals concept).
103
+ - **State**: `{ doc, clock }`, used by the main API.
104
+ - **Base snapshot**: for `applyPatch`, pass a prior `CrdtState`; internals APIs may use raw `Doc` snapshots.
118
105
 
119
106
  ## Ordered Event Log Server Pattern
120
107
 
@@ -126,7 +113,7 @@ If your service contract is "JSON Patch in / JSON Patch out", and your backend k
126
113
  - Append the accepted event to your ordered log.
127
114
  - For downstream clients, emit `crdtToJsonPatch(clientBaseDoc, currentHeadDoc)`.
128
115
 
129
- Minimal shape:
116
+ Minimal shape (advanced API via `json-patch-to-crdt/internals`):
130
117
 
131
118
  ```ts
132
119
  import {
@@ -137,7 +124,7 @@ import {
137
124
  type Doc,
138
125
  type JsonPatchOp,
139
126
  type VersionVector,
140
- } from "json-patch-to-crdt";
127
+ } from "json-patch-to-crdt/internals";
141
128
 
142
129
  let head: Doc = createState({ list: [] }, { actor: "server" }).doc;
143
130
  let vv: VersionVector = {};
@@ -165,24 +152,23 @@ function applyIncomingPatch(
165
152
  }
166
153
  ```
167
154
 
168
- If you prefer a non-throwing low-level compile+apply path, use `jsonPatchToCrdtSafe`.
155
+ If you prefer a non-throwing low-level compile+apply path, use `jsonPatchToCrdtSafe` from `json-patch-to-crdt/internals`.
169
156
 
170
157
  ## Patch Semantics
171
158
 
172
159
  - Patches are interpreted relative to a base snapshot.
173
- - `applyPatch` defaults to a safe snapshot of the current state as its base.
174
- - You can pass an explicit base doc via `applyPatch(state, patch, { base })`.
175
- - Patch semantics are configurable: `semantics: "base"` (default) or `"sequential"`.
160
+ - `applyPatch` defaults to RFC-style sequential patch execution.
161
+ - You can pass an explicit base state via `applyPatch(state, patch, { base })`.
162
+ - Patch semantics are configurable: `semantics: "sequential"` (default) or `"base"`.
176
163
  - In `sequential` mode with an explicit `base`, operations are interpreted against a rolling base snapshot while being applied step-by-step to the evolving head.
177
164
  - Array indexes are mapped to element IDs based on the base snapshot.
178
165
  - `"-"` is treated as append for array inserts.
179
- - Missing arrays in the base snapshot only allow inserts at index `0` or `"-"`; other indexes throw a `PatchError` with code `409`.
180
166
  - `test` operations can be evaluated against `head` or `base` using the `testAgainst` option.
181
167
 
182
168
  ### Semantics Modes
183
169
 
184
- - `semantics: "base"` (default): interprets the full patch relative to one fixed snapshot.
185
- - `semantics: "sequential"`: applies operations one-by-one against the evolving head (closest to RFC 6902 execution style).
170
+ - `semantics: "sequential"` (default): applies operations one-by-one against the evolving head (RFC-like execution).
171
+ - `semantics: "base"`: interprets the full patch relative to one fixed snapshot.
186
172
 
187
173
  #### Which Mode Should You Use?
188
174
 
@@ -206,26 +192,26 @@ const sequentialMode = applyPatch(state, [{ op: "add", path: "/list/0", value: "
206
192
 
207
193
  ## Delta Patches (First-Class)
208
194
 
209
- If you want a JSON Patch delta, you must provide a base snapshot. This is a first-class API:
195
+ For most applications, diff JSON values directly:
210
196
 
211
197
  ```ts
212
- import { crdtToJsonPatch } from "json-patch-to-crdt";
198
+ import { diffJsonPatch } from "json-patch-to-crdt";
213
199
 
214
- const delta = crdtToJsonPatch(baseDoc, headDoc);
200
+ const delta = diffJsonPatch(baseJson, nextJson);
215
201
  ```
216
202
 
217
- You can also diff JSON directly:
203
+ If you already keep CRDT documents and need doc-level deltas, use the internals entry point:
218
204
 
219
205
  ```ts
220
- import { diffJsonPatch } from "json-patch-to-crdt";
206
+ import { crdtToJsonPatch } from "json-patch-to-crdt/internals";
221
207
 
222
- const delta = diffJsonPatch(baseJson, nextJson);
208
+ const delta = crdtToJsonPatch(baseDoc, headDoc);
223
209
  ```
224
210
 
225
- If you need a full-state root `replace` patch (no delta), use `crdtToFullReplace`:
211
+ If you need a full-state root `replace` patch (no delta), use internals:
226
212
 
227
213
  ```ts
228
- import { crdtToFullReplace } from "json-patch-to-crdt";
214
+ import { crdtToFullReplace } from "json-patch-to-crdt/internals";
229
215
 
230
216
  const fullPatch = crdtToFullReplace(doc);
231
217
  // [{ op: "replace", path: "", value: { ... } }]
@@ -238,7 +224,7 @@ By default, arrays are diffed with deterministic LCS edits.
238
224
  If you want atomic array replacement, pass `{ arrayStrategy: "atomic" }`:
239
225
 
240
226
  ```ts
241
- const delta = crdtToJsonPatch(baseDoc, headDoc, { arrayStrategy: "atomic" });
227
+ const delta = diffJsonPatch(baseJson, nextJson, { arrayStrategy: "atomic" });
242
228
  ```
243
229
 
244
230
  Notes:
@@ -248,22 +234,23 @@ Notes:
248
234
 
249
235
  ## Merging
250
236
 
251
- Merge two divergent CRDT documents or states:
237
+ Merge full states:
252
238
 
253
239
  ```ts
254
- import { mergeDoc, mergeState } from "json-patch-to-crdt";
255
-
256
- // Merge documents (low-level):
257
- const mergedDoc = mergeDoc(docA, docB);
240
+ import { mergeState } from "json-patch-to-crdt";
258
241
 
259
242
  // Merge full states (preserve local actor identity):
260
243
  const mergedState = mergeState(stateA, stateB, { actor: "A" });
261
244
  ```
262
245
 
246
+ If you need low-level document-only merging, use `mergeDoc` from `json-patch-to-crdt/internals`.
247
+
263
248
  By default, merge checks that non-empty arrays share lineage (common element IDs).
264
249
  If you intentionally need best-effort merging of unrelated array histories, disable this guard:
265
250
 
266
251
  ```ts
252
+ import { mergeDoc } from "json-patch-to-crdt/internals";
253
+
267
254
  const mergedDoc = mergeDoc(docA, docB, { requireSharedOrigin: false });
268
255
  ```
269
256
 
@@ -314,67 +301,69 @@ try {
314
301
  const next = applyPatch(state, patch);
315
302
  } catch (err) {
316
303
  if (err instanceof PatchError) {
317
- console.error(err.code, err.message);
304
+ console.error(err.code, err.reason, err.message);
318
305
  }
319
306
  }
320
307
  ```
321
308
 
322
- Low-level APIs (`applyIntentsToCrdt`, `jsonPatchToCrdt`) return `{ ok: false, code: 409, message }` for apply-time conflicts.
323
- Compile-time patch issues (invalid pointers, missing object parents/targets) throw errors unless you use `jsonPatchToCrdtSafe`.
309
+ Non-throwing APIs (`tryApplyPatch`, `tryApplyPatchInPlace`, `tryMergeState`) return structured conflicts.
310
+ Internals helpers like `jsonPatchToCrdtSafe` and `tryMergeDoc` return the same shape:
311
+
312
+ - `{ ok: false, code: 409, reason, message, path?, opIndex? }`
324
313
 
325
314
  ## API Summary
326
315
 
327
316
  ### State helpers
328
317
 
329
318
  - `createState(initial, { actor, start? })` - Create a new CRDT state from JSON.
330
- - `applyPatch(state, patch, options?)` - Apply a patch immutably, returning a new state (`semantics: "base"` by default).
331
- - `applyPatchInPlace(state, patch, options?)` - Apply a patch by mutating state in place (atomic by default, `atomic: false` for legacy behavior).
332
- - `applyPatchAsActor(doc, vv, actor, patch, options?)` - Apply a patch for a server-tracked actor and return updated `{ state, vv }`.
319
+ - `forkState(origin, actor, options?)` - Fork a shared-origin replica with a new local actor ID. Reusing `origin` actor IDs is rejected by default (`options.allowActorReuse: true` to opt in explicitly).
320
+ - `applyPatch(state, patch, options?)` - Apply a patch immutably, returning a new state (`semantics: "sequential"` by default).
321
+ - `applyPatchInPlace(state, patch, options?)` - Apply a patch by mutating state in place (`atomic: true` by default).
322
+ - `tryApplyPatch(state, patch, options?)` - Non-throwing immutable apply (`{ ok: true, state }` or `{ ok: false, error }`).
323
+ - `tryApplyPatchInPlace(state, patch, options?)` - Non-throwing in-place apply result.
324
+ - `validateJsonPatch(baseJson, patch, options?)` - Preflight patch validation (non-mutating).
333
325
  - `toJson(docOrState)` - Materialize a JSON value from a doc or state.
334
- - `PatchError` - Error class thrown for failed patches (code `409`).
335
-
336
- ### Clock helpers
337
-
338
- - `createClock(actor, start?)` - Create a new clock for dot generation.
339
- - `cloneClock(clock)` - Clone a clock independently.
340
- - `nextDotForActor(vv, actor)` - Generate a dot for any actor from a shared version-vector map.
341
- - `observeDot(vv, dot)` - Record observed dots into that map.
342
-
343
- ### Document helpers
344
-
345
- - `docFromJson(value, nextDot)` - Create a CRDT doc using fresh dots per node.
346
- - `cloneDoc(doc)` - Deep-clone a document.
347
- - `materialize(node)` - Convert a CRDT node to a JSON value.
326
+ - `applyPatch`/`tryApplyPatch` options: `base` expects a prior `CrdtState` snapshot (not a raw doc), plus `semantics` and `testAgainst`.
327
+ - `PatchError` - Error class thrown for failed patches (`code`, `reason`, `message`, optional `path`/`opIndex`).
348
328
 
349
329
  ### Merge helpers
350
330
 
351
- - `mergeDoc(a, b, options?)` - Merge two CRDT documents (`options.requireSharedOrigin` defaults to `true`).
352
331
  - `mergeState(a, b, options?)` - Merge two CRDT states (doc + clock), preserving actor identity (`options.actor`) and optional shared-origin checks.
332
+ - `tryMergeState(a, b, options?)` - Non-throwing merge-state result.
333
+ - `MergeError` - Error class thrown by throwing merge helpers.
353
334
 
354
335
  ### Patch helpers
355
336
 
356
- - `compileJsonPatchToIntent(baseJson, patch)` - Compile JSON Patch to intent operations.
357
- - `applyIntentsToCrdt(base, head, intents, newDot, evalTestAgainst?, bumpCounterAbove?)` - Apply intents to a document.
358
- - `jsonPatchToCrdt(base, head, patch, newDot, evalTestAgainst?, bumpCounterAbove?)` - Compile and apply in one step.
359
- - `jsonPatchToCrdtSafe(base, head, patch, newDot, evalTestAgainst?, bumpCounterAbove?)` - Safe compile+apply wrapper that returns `409` results instead of throwing on compile-time patch issues.
360
337
  - `diffJsonPatch(baseJson, nextJson, options?)` - Compute a JSON Patch delta between two JSON values.
361
- - `crdtToJsonPatch(baseDoc, headDoc, options?)` - Compute a JSON Patch delta between two CRDT docs.
362
- - `crdtToFullReplace(doc)` - Emit a full-state root `replace` patch.
363
338
 
364
339
  ### Serialization
365
340
 
366
- - `serializeDoc(doc)` / `deserializeDoc(payload)` - Serialize/restore a document.
367
341
  - `serializeState(state)` / `deserializeState(payload)` - Serialize/restore a full state.
368
342
 
369
343
  ### Internals (`json-patch-to-crdt/internals`)
370
344
 
371
- Low-level helpers are available via a separate entry point for advanced use:
345
+ Advanced helpers are available via a separate entry point:
372
346
 
373
347
  ```ts
374
- import { compareDot, rgaInsertAfter, objSet, HEAD } from "json-patch-to-crdt/internals";
348
+ import {
349
+ applyPatchAsActor,
350
+ createClock,
351
+ docFromJson,
352
+ mergeDoc,
353
+ jsonPatchToCrdtSafe,
354
+ compareDot,
355
+ rgaInsertAfter,
356
+ HEAD,
357
+ } from "json-patch-to-crdt/internals";
375
358
  ```
376
359
 
377
- This includes: `compareDot`, `vvHasDot`, `vvMerge`, `dotToElemId`, `newObj`, `newSeq`, `newReg`, `lwwSet`, `objSet`, `objRemove`, `HEAD`, `rgaInsertAfter`, `rgaDelete`, `rgaLinearizeIds`, `rgaPrevForInsertAtIndex`, `rgaIdAtIndex`, `docFromJsonWithDot`.
360
+ Internals includes low-level helpers such as:
361
+
362
+ - Actor/version-vector helpers: `applyPatchAsActor`, `createClock`, `cloneClock`, `nextDotForActor`, `observeDot`.
363
+ - Doc-level APIs: `docFromJson`, `docFromJsonWithDot`, `cloneDoc`, `materialize`, `mergeDoc`, `tryMergeDoc`.
364
+ - Intent compiler/apply pipeline: `compileJsonPatchToIntent`, `applyIntentsToCrdt`, `jsonPatchToCrdt`, `jsonPatchToCrdtSafe`, `tryJsonPatchToCrdt`.
365
+ - Doc delta/serialization helpers: `crdtToJsonPatch`, `crdtToFullReplace`, `serializeDoc`, `deserializeDoc`.
366
+ - CRDT primitives/utilities: `compareDot`, `vvHasDot`, `vvMerge`, `dotToElemId`, `newObj`, `newSeq`, `newReg`, `lwwSet`, `objSet`, `objRemove`, `HEAD`, `rgaInsertAfter`, `rgaDelete`, `rgaLinearizeIds`, `rgaPrevForInsertAtIndex`, `rgaIdAtIndex`.
378
367
 
379
368
  ## Determinism
380
369
 
@@ -392,10 +381,10 @@ This typically means the patch could not be applied against the base snapshot. C
392
381
  - Base array missing for a non-append insert.
393
382
 
394
383
  **How do I avoid `409` for arrays?**
395
- Always pass a base snapshot that matches the array you are patching. If the array may be missing, only insert at index `0` or `"-"` (append) to allow auto-creation.
384
+ Always pass a base state snapshot that matches the array you are patching. If the array may be missing, create the parent path explicitly before inserting into it.
396
385
 
397
386
  **How do I get a full-state patch instead of a delta?**
398
- Use `crdtToFullReplace(doc)` which emits a single root `replace` patch.
387
+ Use `crdtToFullReplace(doc)` from `json-patch-to-crdt/internals`, which emits a single root `replace` patch.
399
388
 
400
389
  **Why do array deltas look bigger than expected?**
401
390
  LCS diffs are deterministic, not minimal. If you prefer one-op array replacement, use `{ arrayStrategy: "atomic" }`.
@@ -404,7 +393,10 @@ LCS diffs are deterministic, not minimal. If you prefer one-op array replacement
404
393
  No. It is deterministic and usually compact, but not guaranteed to be minimal.
405
394
 
406
395
  **How do I merge states from two peers?**
407
- Use `mergeState(local, remote, { actor: localActorId })`. Both peers should start from a shared origin state (same document, different clocks), and each peer should keep its own unique actor ID across merges. See the [Multi-Peer Sync](#multi-peer-sync) example above.
396
+ Use `forkState(origin, actor)` to create each peer from the same origin, then `mergeState(local, remote, { actor: localActorId })`. Each peer should keep a stable unique actor ID across merges. See the [Multi-Peer Sync](#multi-peer-sync) example above.
397
+
398
+ **Why did `forkState` throw about actor uniqueness?**
399
+ By default, `forkState` blocks reusing `origin.clock.actor` because same-actor forks can mint duplicate dots and produce order-dependent merges. If you intentionally need same-actor cloning, pass `forkState(origin, actor, { allowActorReuse: true })`.
408
400
 
409
401
  **Why can my local counter jump after a merge?**
410
402
  Array inserts that target an existing predecessor may need to outrank sibling insert dots for deterministic ordering. The library can fast-forward the local counter in constant time to avoid expensive loops, but the resulting counter value may still jump upward when merging with peers that already have high counters.
package/dist/index.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { $ as PatchSemantics, A as createState, B as Dot, C as createClock, D as applyPatch, E as PatchError, F as ApplyResult, G as JsonPrimitive, H as IntentOp, I as Clock, J as MergeDocOptions, K as JsonValue, L as CrdtState, M as ActorId, N as ApplyPatchAsActorResult, O as applyPatchAsActor, P as ApplyPatchOptions, Q as ObjNode, R as DiffOptions, S as cloneClock, T as observeDot, U as JsonPatch, V as ElemId, W as JsonPatchOp, X as Node, Y as MergeStateOptions, Z as ObjEntry, _ as crdtToJsonPatch, a as deserializeState, at as SerializedState, b as jsonPatchToCrdt, c as compileJsonPatchToIntent, d as jsonEquals, f as parseJsonPointer, g as crdtToFullReplace, h as cloneDoc, i as deserializeDoc, it as SerializedNode, j as toJson, k as applyPatchInPlace, l as diffJsonPatch, m as applyIntentsToCrdt, n as mergeState, nt as RgaSeq, o as serializeDoc, ot as VersionVector, p as stringifyJsonPointer, q as LwwReg, r as materialize, rt as SerializedDoc, s as serializeState, t as mergeDoc, tt as RgaElem, u as getAtJson, v as docFromJson, w as nextDotForActor, x as jsonPatchToCrdtSafe, z as Doc } from "./merge-BpAUNaPe.mjs";
2
- export { type ActorId, type ApplyPatchAsActorResult, type ApplyPatchOptions, type ApplyResult, type Clock, type CrdtState, type DiffOptions, type Doc, type Dot, type ElemId, type IntentOp, type JsonPatch, type JsonPatchOp, type JsonPrimitive, type JsonValue, type LwwReg, type MergeDocOptions, type MergeStateOptions, type Node, type ObjEntry, type ObjNode, PatchError, type PatchSemantics, type RgaElem, type RgaSeq, type SerializedDoc, type SerializedNode, type SerializedState, type VersionVector, applyIntentsToCrdt, applyPatch, applyPatchAsActor, applyPatchInPlace, cloneClock, cloneDoc, compileJsonPatchToIntent, crdtToFullReplace, crdtToJsonPatch, createClock, createState, deserializeDoc, deserializeState, diffJsonPatch, docFromJson, getAtJson, jsonEquals, jsonPatchToCrdt, jsonPatchToCrdtSafe, materialize, mergeDoc, mergeState, nextDotForActor, observeDot, parseJsonPointer, serializeDoc, serializeState, stringifyJsonPointer, toJson };
1
+ import { C as toJson, D as ActorId, E as validateJsonPatch, I as CrdtState, K as JsonPrimitive, L as DiffOptions, M as ApplyPatchOptions, O as ApplyError, S as forkState, T as tryApplyPatchInPlace, U as JsonPatch, V as ForkStateOptions, W as JsonPatchOp, X as MergeStateOptions, _ as PatchError, a as tryMergeState, b as applyPatchInPlace, dt as TryApplyPatchResult, et as PatchErrorReason, f as diffJsonPatch, j as ApplyPatchInPlaceOptions, l as serializeState, lt as SerializedState, mt as ValidatePatchResult, pt as TryMergeStateResult, q as JsonValue, r as mergeState, s as deserializeState, t as MergeError, tt as PatchSemantics, ut as TryApplyPatchInPlaceResult, v as applyPatch, w as tryApplyPatch, x as createState } from "./merge-DQ_KDtnE.mjs";
2
+ export { type ActorId, type ApplyError, type ApplyPatchInPlaceOptions, type ApplyPatchOptions, type CrdtState, type DiffOptions, type ForkStateOptions, type JsonPatch, type JsonPatchOp, type JsonPrimitive, type JsonValue, MergeError, type MergeStateOptions, PatchError, type PatchErrorReason, type PatchSemantics, type SerializedState, type TryApplyPatchInPlaceResult, type TryApplyPatchResult, type TryMergeStateResult, type ValidatePatchResult, applyPatch, applyPatchInPlace, createState, deserializeState, diffJsonPatch, forkState, mergeState, serializeState, toJson, tryApplyPatch, tryApplyPatchInPlace, tryMergeState, validateJsonPatch };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { $ as PatchSemantics, A as createState, B as Dot, C as createClock, D as applyPatch, E as PatchError, F as ApplyResult, G as JsonPrimitive, H as IntentOp, I as Clock, J as MergeDocOptions, K as JsonValue, L as CrdtState, M as ActorId, N as ApplyPatchAsActorResult, O as applyPatchAsActor, P as ApplyPatchOptions, Q as ObjNode, R as DiffOptions, S as cloneClock, T as observeDot, U as JsonPatch, V as ElemId, W as JsonPatchOp, X as Node, Y as MergeStateOptions, Z as ObjEntry, _ as crdtToJsonPatch, a as deserializeState, at as SerializedState, b as jsonPatchToCrdt, c as compileJsonPatchToIntent, d as jsonEquals, f as parseJsonPointer, g as crdtToFullReplace, h as cloneDoc, i as deserializeDoc, it as SerializedNode, j as toJson, k as applyPatchInPlace, l as diffJsonPatch, m as applyIntentsToCrdt, n as mergeState, nt as RgaSeq, o as serializeDoc, ot as VersionVector, p as stringifyJsonPointer, q as LwwReg, r as materialize, rt as SerializedDoc, s as serializeState, t as mergeDoc, tt as RgaElem, u as getAtJson, v as docFromJson, w as nextDotForActor, x as jsonPatchToCrdtSafe, z as Doc } from "./merge-QmPXxE6_.js";
2
- export { type ActorId, type ApplyPatchAsActorResult, type ApplyPatchOptions, type ApplyResult, type Clock, type CrdtState, type DiffOptions, type Doc, type Dot, type ElemId, type IntentOp, type JsonPatch, type JsonPatchOp, type JsonPrimitive, type JsonValue, type LwwReg, type MergeDocOptions, type MergeStateOptions, type Node, type ObjEntry, type ObjNode, PatchError, type PatchSemantics, type RgaElem, type RgaSeq, type SerializedDoc, type SerializedNode, type SerializedState, type VersionVector, applyIntentsToCrdt, applyPatch, applyPatchAsActor, applyPatchInPlace, cloneClock, cloneDoc, compileJsonPatchToIntent, crdtToFullReplace, crdtToJsonPatch, createClock, createState, deserializeDoc, deserializeState, diffJsonPatch, docFromJson, getAtJson, jsonEquals, jsonPatchToCrdt, jsonPatchToCrdtSafe, materialize, mergeDoc, mergeState, nextDotForActor, observeDot, parseJsonPointer, serializeDoc, serializeState, stringifyJsonPointer, toJson };
1
+ import { C as toJson, D as ActorId, E as validateJsonPatch, I as CrdtState, K as JsonPrimitive, L as DiffOptions, M as ApplyPatchOptions, O as ApplyError, S as forkState, T as tryApplyPatchInPlace, U as JsonPatch, V as ForkStateOptions, W as JsonPatchOp, X as MergeStateOptions, _ as PatchError, a as tryMergeState, b as applyPatchInPlace, dt as TryApplyPatchResult, et as PatchErrorReason, f as diffJsonPatch, j as ApplyPatchInPlaceOptions, l as serializeState, lt as SerializedState, mt as ValidatePatchResult, pt as TryMergeStateResult, q as JsonValue, r as mergeState, s as deserializeState, t as MergeError, tt as PatchSemantics, ut as TryApplyPatchInPlaceResult, v as applyPatch, w as tryApplyPatch, x as createState } from "./merge-B8nmGV-o.js";
2
+ export { type ActorId, type ApplyError, type ApplyPatchInPlaceOptions, type ApplyPatchOptions, type CrdtState, type DiffOptions, type ForkStateOptions, type JsonPatch, type JsonPatchOp, type JsonPrimitive, type JsonValue, MergeError, type MergeStateOptions, PatchError, type PatchErrorReason, type PatchSemantics, type SerializedState, type TryApplyPatchInPlaceResult, type TryApplyPatchResult, type TryMergeStateResult, type ValidatePatchResult, applyPatch, applyPatchInPlace, createState, deserializeState, diffJsonPatch, forkState, mergeState, serializeState, toJson, tryApplyPatch, tryApplyPatchInPlace, tryMergeState, validateJsonPatch };
package/dist/index.js CHANGED
@@ -1,33 +1,18 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_merge = require('./merge-B1BFMhJJ.js');
2
+ const require_merge = require('./merge-BAfuC6bf.js');
3
3
 
4
+ exports.MergeError = require_merge.MergeError;
4
5
  exports.PatchError = require_merge.PatchError;
5
- exports.applyIntentsToCrdt = require_merge.applyIntentsToCrdt;
6
6
  exports.applyPatch = require_merge.applyPatch;
7
- exports.applyPatchAsActor = require_merge.applyPatchAsActor;
8
7
  exports.applyPatchInPlace = require_merge.applyPatchInPlace;
9
- exports.cloneClock = require_merge.cloneClock;
10
- exports.cloneDoc = require_merge.cloneDoc;
11
- exports.compileJsonPatchToIntent = require_merge.compileJsonPatchToIntent;
12
- exports.crdtToFullReplace = require_merge.crdtToFullReplace;
13
- exports.crdtToJsonPatch = require_merge.crdtToJsonPatch;
14
- exports.createClock = require_merge.createClock;
15
8
  exports.createState = require_merge.createState;
16
- exports.deserializeDoc = require_merge.deserializeDoc;
17
9
  exports.deserializeState = require_merge.deserializeState;
18
10
  exports.diffJsonPatch = require_merge.diffJsonPatch;
19
- exports.docFromJson = require_merge.docFromJson;
20
- exports.getAtJson = require_merge.getAtJson;
21
- exports.jsonEquals = require_merge.jsonEquals;
22
- exports.jsonPatchToCrdt = require_merge.jsonPatchToCrdt;
23
- exports.jsonPatchToCrdtSafe = require_merge.jsonPatchToCrdtSafe;
24
- exports.materialize = require_merge.materialize;
25
- exports.mergeDoc = require_merge.mergeDoc;
11
+ exports.forkState = require_merge.forkState;
26
12
  exports.mergeState = require_merge.mergeState;
27
- exports.nextDotForActor = require_merge.nextDotForActor;
28
- exports.observeDot = require_merge.observeDot;
29
- exports.parseJsonPointer = require_merge.parseJsonPointer;
30
- exports.serializeDoc = require_merge.serializeDoc;
31
13
  exports.serializeState = require_merge.serializeState;
32
- exports.stringifyJsonPointer = require_merge.stringifyJsonPointer;
33
- exports.toJson = require_merge.toJson;
14
+ exports.toJson = require_merge.toJson;
15
+ exports.tryApplyPatch = require_merge.tryApplyPatch;
16
+ exports.tryApplyPatchInPlace = require_merge.tryApplyPatchInPlace;
17
+ exports.tryMergeState = require_merge.tryMergeState;
18
+ exports.validateJsonPatch = require_merge.validateJsonPatch;
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { C as getAtJson, E as stringifyJsonPointer, G as cloneClock, J as observeDot, K as createClock, P as materialize, S as diffJsonPatch, T as parseJsonPointer, _ as docFromJson, a as serializeDoc, b as jsonPatchToCrdtSafe, c as applyPatch, d as createState, f as toJson, g as crdtToJsonPatch, h as crdtToFullReplace, i as deserializeState, l as applyPatchAsActor, m as cloneDoc, n as mergeState, o as serializeState, p as applyIntentsToCrdt, q as nextDotForActor, r as deserializeDoc, s as PatchError, t as mergeDoc, u as applyPatchInPlace, w as jsonEquals, x as compileJsonPatchToIntent, y as jsonPatchToCrdt } from "./merge-DikOFBWc.mjs";
1
+ import { _ as tryApplyPatch, a as tryMergeState, d as applyPatch, g as toJson, h as forkState, j as diffJsonPatch, l as serializeState, m as createState, p as applyPatchInPlace, r as mergeState, s as deserializeState, t as MergeError, u as PatchError, v as tryApplyPatchInPlace, y as validateJsonPatch } from "./merge-CKcP1ZPt.mjs";
2
2
 
3
- export { PatchError, applyIntentsToCrdt, applyPatch, applyPatchAsActor, applyPatchInPlace, cloneClock, cloneDoc, compileJsonPatchToIntent, crdtToFullReplace, crdtToJsonPatch, createClock, createState, deserializeDoc, deserializeState, diffJsonPatch, docFromJson, getAtJson, jsonEquals, jsonPatchToCrdt, jsonPatchToCrdtSafe, materialize, mergeDoc, mergeState, nextDotForActor, observeDot, parseJsonPointer, serializeDoc, serializeState, stringifyJsonPointer, toJson };
3
+ export { MergeError, PatchError, applyPatch, applyPatchInPlace, createState, deserializeState, diffJsonPatch, forkState, mergeState, serializeState, toJson, tryApplyPatch, tryApplyPatchInPlace, tryMergeState, validateJsonPatch };
@@ -1,5 +1,83 @@
1
- import { $ as PatchSemantics, A as createState, B as Dot, C as createClock, D as applyPatch, E as PatchError, F as ApplyResult, G as JsonPrimitive, H as IntentOp, I as Clock, J as MergeDocOptions, K as JsonValue, L as CrdtState, M as ActorId, N as ApplyPatchAsActorResult, O as applyPatchAsActor, P as ApplyPatchOptions, Q as ObjNode, R as DiffOptions, S as cloneClock, T as observeDot, U as JsonPatch, V as ElemId, W as JsonPatchOp, X as Node, Y as MergeStateOptions, Z as ObjEntry, _ as crdtToJsonPatch, a as deserializeState, at as SerializedState, b as jsonPatchToCrdt, c as compileJsonPatchToIntent, d as jsonEquals, et as ROOT_KEY, f as parseJsonPointer, g as crdtToFullReplace, h as cloneDoc, i as deserializeDoc, it as SerializedNode, j as toJson, k as applyPatchInPlace, l as diffJsonPatch, m as applyIntentsToCrdt, n as mergeState, nt as RgaSeq, o as serializeDoc, ot as VersionVector, p as stringifyJsonPointer, q as LwwReg, r as materialize, rt as SerializedDoc, s as serializeState, t as mergeDoc, tt as RgaElem, u as getAtJson, v as docFromJson, w as nextDotForActor, x as jsonPatchToCrdtSafe, y as docFromJsonWithDot, z as Doc } from "./merge-BpAUNaPe.mjs";
1
+ import { $ as ObjNode, A as ApplyPatchAsActorResult, B as ElemId, C as toJson, D as ActorId, E as validateJsonPatch, F as CompilePatchOptions, G as JsonPatchToCrdtOptions, H as IntentOp, I as CrdtState, J as LwwReg, K as JsonPrimitive, L as DiffOptions, M as ApplyPatchOptions, N as ApplyResult, O as ApplyError, P as Clock, Q as ObjEntry, R as Doc, S as forkState, T as tryApplyPatchInPlace, U as JsonPatch, V as ForkStateOptions, W as JsonPatchOp, X as MergeStateOptions, Y as MergeDocOptions, Z as Node, _ as PatchError, a as tryMergeState, at as SerializedClock, b as applyPatchInPlace, c as serializeDoc, ct as SerializedRgaElem, d as compileJsonPatchToIntent, dt as TryApplyPatchResult, et as PatchErrorReason, f as diffJsonPatch, ft as TryMergeDocResult, g as stringifyJsonPointer, h as parseJsonPointer, ht as VersionVector, i as tryMergeDoc, it as RgaSeq, j as ApplyPatchInPlaceOptions, k as ApplyPatchAsActorOptions, l as serializeState, lt as SerializedState, m as jsonEquals, mt as ValidatePatchResult, n as mergeDoc, nt as ROOT_KEY, o as deserializeDoc, ot as SerializedDoc, p as getAtJson, pt as TryMergeStateResult, q as JsonValue, r as mergeState, rt as RgaElem, s as deserializeState, st as SerializedNode, t as MergeError, tt as PatchSemantics, u as PatchCompileError, ut as TryApplyPatchInPlaceResult, v as applyPatch, w as tryApplyPatch, x as createState, y as applyPatchAsActor, z as Dot } from "./merge-DQ_KDtnE.mjs";
2
2
 
3
+ //#region src/clock.d.ts
4
+ /**
5
+ * Create a new clock for the given actor. Each call to `clock.next()` yields a fresh `Dot`.
6
+ * @param actor - Unique identifier for this peer.
7
+ * @param start - Initial counter value (defaults to 0).
8
+ */
9
+ declare function createClock(actor: ActorId, start?: number): Clock;
10
+ /** Create an independent copy of a clock at the same counter position. */
11
+ declare function cloneClock(clock: Clock): Clock;
12
+ /**
13
+ * Generate the next per-actor dot from a mutable version vector.
14
+ * Useful when a server needs to mint dots for many actors.
15
+ */
16
+ declare function nextDotForActor(vv: VersionVector, actor: ActorId): Dot;
17
+ /** Record an observed dot in a version vector. */
18
+ declare function observeDot(vv: VersionVector, dot: Dot): void;
19
+ //#endregion
20
+ //#region src/doc.d.ts
21
+ /**
22
+ * Create a CRDT document from a JSON value, using fresh dots for each node.
23
+ * @param value - The JSON value to convert.
24
+ * @param nextDot - A function that generates a unique `Dot` on each call.
25
+ * @returns A new CRDT `Doc`.
26
+ */
27
+ declare function docFromJson(value: JsonValue, nextDot: () => Dot): Doc;
28
+ /**
29
+ * Legacy: create a doc using a single dot with counter offsets for array children.
30
+ * Prefer `docFromJson(value, nextDot)` to ensure unique dots per node.
31
+ */
32
+ declare function docFromJsonWithDot(value: JsonValue, dot: Dot): Doc;
33
+ /** Deep-clone a CRDT document. The clone is fully independent of the original. */
34
+ declare function cloneDoc(doc: Doc): Doc;
35
+ /**
36
+ * Apply compiled intent operations to a CRDT document.
37
+ * Array indices are resolved against the base document.
38
+ * @param base - The base document snapshot used for index mapping and test evaluation.
39
+ * @param head - The target document to mutate.
40
+ * @param intents - Compiled intent operations from `compileJsonPatchToIntent`.
41
+ * @param newDot - A function that generates a unique `Dot` per mutation.
42
+ * @param evalTestAgainst - Whether `test` ops are evaluated against `"head"` or `"base"`.
43
+ * @param bumpCounterAbove - Optional hook that can fast-forward the underlying counter before inserts.
44
+ * @returns `{ ok: true }` on success, or `{ ok: false, code: 409, message }` on conflict.
45
+ */
46
+ declare function applyIntentsToCrdt(base: Doc, head: Doc, intents: IntentOp[], newDot: () => Dot, evalTestAgainst?: "head" | "base", bumpCounterAbove?: (ctr: number) => void): ApplyResult;
47
+ /**
48
+ * Convenience wrapper: compile a JSON Patch and apply it to a CRDT document.
49
+ * Overloads:
50
+ * - positional: `jsonPatchToCrdt(base, head, patch, newDot, evalTestAgainst?, bumpCounterAbove?)`
51
+ * - object: `jsonPatchToCrdt({ base, head, patch, newDot, evalTestAgainst?, bumpCounterAbove?, semantics? })`
52
+ */
53
+ declare function jsonPatchToCrdt(options: JsonPatchToCrdtOptions): ApplyResult;
54
+ declare function jsonPatchToCrdt(base: Doc, head: Doc, patch: JsonPatchOp[], newDot: () => Dot, evalTestAgainst?: "head" | "base", bumpCounterAbove?: (ctr: number) => void): ApplyResult;
55
+ /**
56
+ * Safe wrapper around `jsonPatchToCrdt`.
57
+ * This function never throws and always returns an `ApplyResult`.
58
+ */
59
+ declare function jsonPatchToCrdtSafe(options: JsonPatchToCrdtOptions): ApplyResult;
60
+ declare function jsonPatchToCrdtSafe(base: Doc, head: Doc, patch: JsonPatchOp[], newDot: () => Dot, evalTestAgainst?: "head" | "base", bumpCounterAbove?: (ctr: number) => void): ApplyResult;
61
+ /** Alias for codebases that prefer `try*` naming for non-throwing APIs. */
62
+ declare const tryJsonPatchToCrdt: typeof jsonPatchToCrdtSafe;
63
+ /**
64
+ * Generate a JSON Patch delta between two CRDT documents.
65
+ * @param base - The base document snapshot.
66
+ * @param head - The current document state.
67
+ * @param options - Diff options (e.g. `{ arrayStrategy: "lcs" }`).
68
+ * @returns An array of JSON Patch operations that transform base into head.
69
+ */
70
+ declare function crdtToJsonPatch(base: Doc, head: Doc, options?: DiffOptions): JsonPatchOp[];
71
+ /**
72
+ * Emit a single root `replace` patch representing the full document state.
73
+ * Use `crdtToJsonPatch(base, head)` for delta patches instead.
74
+ */
75
+ declare function crdtToFullReplace(doc: Doc): JsonPatchOp[];
76
+ //#endregion
77
+ //#region src/materialize.d.ts
78
+ /** Recursively convert a CRDT node graph into a plain JSON value. */
79
+ declare function materialize(node: Node): JsonValue;
80
+ //#endregion
3
81
  //#region src/dot.d.ts
4
82
  declare function compareDot(a: Dot, b: Dot): number;
5
83
  declare function vvHasDot(vv: VersionVector, d: Dot): boolean;
@@ -22,4 +100,4 @@ declare function rgaDelete(seq: RgaSeq, id: ElemId): void;
22
100
  declare function rgaIdAtIndex(seq: RgaSeq, index: number): ElemId | undefined;
23
101
  declare function rgaPrevForInsertAtIndex(seq: RgaSeq, index: number): ElemId;
24
102
  //#endregion
25
- export { ActorId, ApplyPatchAsActorResult, ApplyPatchOptions, ApplyResult, Clock, CrdtState, DiffOptions, Doc, Dot, ElemId, HEAD, IntentOp, JsonPatch, JsonPatchOp, JsonPrimitive, JsonValue, LwwReg, MergeDocOptions, MergeStateOptions, Node, ObjEntry, ObjNode, PatchError, PatchSemantics, ROOT_KEY, RgaElem, RgaSeq, SerializedDoc, SerializedNode, SerializedState, VersionVector, applyIntentsToCrdt, applyPatch, applyPatchAsActor, applyPatchInPlace, cloneClock, cloneDoc, compareDot, compileJsonPatchToIntent, crdtToFullReplace, crdtToJsonPatch, createClock, createState, deserializeDoc, deserializeState, diffJsonPatch, docFromJson, docFromJsonWithDot, dotToElemId, getAtJson, jsonEquals, jsonPatchToCrdt, jsonPatchToCrdtSafe, lwwSet, materialize, mergeDoc, mergeState, newObj, newReg, newSeq, nextDotForActor, objRemove, objSet, observeDot, parseJsonPointer, rgaDelete, rgaIdAtIndex, rgaInsertAfter, rgaLinearizeIds, rgaPrevForInsertAtIndex, serializeDoc, serializeState, stringifyJsonPointer, toJson, vvHasDot, vvMerge };
103
+ export { ActorId, ApplyError, type ApplyPatchAsActorOptions, type ApplyPatchAsActorResult, ApplyPatchInPlaceOptions, ApplyPatchOptions, type ApplyResult, type Clock, type CompilePatchOptions, CrdtState, DiffOptions, type Doc, type Dot, type ElemId, ForkStateOptions, HEAD, type IntentOp, JsonPatch, JsonPatchOp, type JsonPatchToCrdtOptions, JsonPrimitive, JsonValue, type LwwReg, type MergeDocOptions, MergeError, MergeStateOptions, type Node, type ObjEntry, type ObjNode, PatchCompileError, PatchError, PatchErrorReason, PatchSemantics, ROOT_KEY, type RgaElem, type RgaSeq, type SerializedClock, type SerializedDoc, type SerializedNode, type SerializedRgaElem, SerializedState, TryApplyPatchInPlaceResult, TryApplyPatchResult, type TryMergeDocResult, TryMergeStateResult, ValidatePatchResult, type VersionVector, applyIntentsToCrdt, applyPatch, applyPatchAsActor, applyPatchInPlace, cloneClock, cloneDoc, compareDot, compileJsonPatchToIntent, crdtToFullReplace, crdtToJsonPatch, createClock, createState, deserializeDoc, deserializeState, diffJsonPatch, docFromJson, docFromJsonWithDot, dotToElemId, forkState, getAtJson, jsonEquals, jsonPatchToCrdt, jsonPatchToCrdtSafe, lwwSet, materialize, mergeDoc, mergeState, newObj, newReg, newSeq, nextDotForActor, objRemove, objSet, observeDot, parseJsonPointer, rgaDelete, rgaIdAtIndex, rgaInsertAfter, rgaLinearizeIds, rgaPrevForInsertAtIndex, serializeDoc, serializeState, stringifyJsonPointer, toJson, tryApplyPatch, tryApplyPatchInPlace, tryJsonPatchToCrdt, tryMergeDoc, tryMergeState, validateJsonPatch, vvHasDot, vvMerge };
@@ -1,5 +1,83 @@
1
- import { $ as PatchSemantics, A as createState, B as Dot, C as createClock, D as applyPatch, E as PatchError, F as ApplyResult, G as JsonPrimitive, H as IntentOp, I as Clock, J as MergeDocOptions, K as JsonValue, L as CrdtState, M as ActorId, N as ApplyPatchAsActorResult, O as applyPatchAsActor, P as ApplyPatchOptions, Q as ObjNode, R as DiffOptions, S as cloneClock, T as observeDot, U as JsonPatch, V as ElemId, W as JsonPatchOp, X as Node, Y as MergeStateOptions, Z as ObjEntry, _ as crdtToJsonPatch, a as deserializeState, at as SerializedState, b as jsonPatchToCrdt, c as compileJsonPatchToIntent, d as jsonEquals, et as ROOT_KEY, f as parseJsonPointer, g as crdtToFullReplace, h as cloneDoc, i as deserializeDoc, it as SerializedNode, j as toJson, k as applyPatchInPlace, l as diffJsonPatch, m as applyIntentsToCrdt, n as mergeState, nt as RgaSeq, o as serializeDoc, ot as VersionVector, p as stringifyJsonPointer, q as LwwReg, r as materialize, rt as SerializedDoc, s as serializeState, t as mergeDoc, tt as RgaElem, u as getAtJson, v as docFromJson, w as nextDotForActor, x as jsonPatchToCrdtSafe, y as docFromJsonWithDot, z as Doc } from "./merge-QmPXxE6_.js";
1
+ import { $ as ObjNode, A as ApplyPatchAsActorResult, B as ElemId, C as toJson, D as ActorId, E as validateJsonPatch, F as CompilePatchOptions, G as JsonPatchToCrdtOptions, H as IntentOp, I as CrdtState, J as LwwReg, K as JsonPrimitive, L as DiffOptions, M as ApplyPatchOptions, N as ApplyResult, O as ApplyError, P as Clock, Q as ObjEntry, R as Doc, S as forkState, T as tryApplyPatchInPlace, U as JsonPatch, V as ForkStateOptions, W as JsonPatchOp, X as MergeStateOptions, Y as MergeDocOptions, Z as Node, _ as PatchError, a as tryMergeState, at as SerializedClock, b as applyPatchInPlace, c as serializeDoc, ct as SerializedRgaElem, d as compileJsonPatchToIntent, dt as TryApplyPatchResult, et as PatchErrorReason, f as diffJsonPatch, ft as TryMergeDocResult, g as stringifyJsonPointer, h as parseJsonPointer, ht as VersionVector, i as tryMergeDoc, it as RgaSeq, j as ApplyPatchInPlaceOptions, k as ApplyPatchAsActorOptions, l as serializeState, lt as SerializedState, m as jsonEquals, mt as ValidatePatchResult, n as mergeDoc, nt as ROOT_KEY, o as deserializeDoc, ot as SerializedDoc, p as getAtJson, pt as TryMergeStateResult, q as JsonValue, r as mergeState, rt as RgaElem, s as deserializeState, st as SerializedNode, t as MergeError, tt as PatchSemantics, u as PatchCompileError, ut as TryApplyPatchInPlaceResult, v as applyPatch, w as tryApplyPatch, x as createState, y as applyPatchAsActor, z as Dot } from "./merge-B8nmGV-o.js";
2
2
 
3
+ //#region src/clock.d.ts
4
+ /**
5
+ * Create a new clock for the given actor. Each call to `clock.next()` yields a fresh `Dot`.
6
+ * @param actor - Unique identifier for this peer.
7
+ * @param start - Initial counter value (defaults to 0).
8
+ */
9
+ declare function createClock(actor: ActorId, start?: number): Clock;
10
+ /** Create an independent copy of a clock at the same counter position. */
11
+ declare function cloneClock(clock: Clock): Clock;
12
+ /**
13
+ * Generate the next per-actor dot from a mutable version vector.
14
+ * Useful when a server needs to mint dots for many actors.
15
+ */
16
+ declare function nextDotForActor(vv: VersionVector, actor: ActorId): Dot;
17
+ /** Record an observed dot in a version vector. */
18
+ declare function observeDot(vv: VersionVector, dot: Dot): void;
19
+ //#endregion
20
+ //#region src/doc.d.ts
21
+ /**
22
+ * Create a CRDT document from a JSON value, using fresh dots for each node.
23
+ * @param value - The JSON value to convert.
24
+ * @param nextDot - A function that generates a unique `Dot` on each call.
25
+ * @returns A new CRDT `Doc`.
26
+ */
27
+ declare function docFromJson(value: JsonValue, nextDot: () => Dot): Doc;
28
+ /**
29
+ * Legacy: create a doc using a single dot with counter offsets for array children.
30
+ * Prefer `docFromJson(value, nextDot)` to ensure unique dots per node.
31
+ */
32
+ declare function docFromJsonWithDot(value: JsonValue, dot: Dot): Doc;
33
+ /** Deep-clone a CRDT document. The clone is fully independent of the original. */
34
+ declare function cloneDoc(doc: Doc): Doc;
35
+ /**
36
+ * Apply compiled intent operations to a CRDT document.
37
+ * Array indices are resolved against the base document.
38
+ * @param base - The base document snapshot used for index mapping and test evaluation.
39
+ * @param head - The target document to mutate.
40
+ * @param intents - Compiled intent operations from `compileJsonPatchToIntent`.
41
+ * @param newDot - A function that generates a unique `Dot` per mutation.
42
+ * @param evalTestAgainst - Whether `test` ops are evaluated against `"head"` or `"base"`.
43
+ * @param bumpCounterAbove - Optional hook that can fast-forward the underlying counter before inserts.
44
+ * @returns `{ ok: true }` on success, or `{ ok: false, code: 409, message }` on conflict.
45
+ */
46
+ declare function applyIntentsToCrdt(base: Doc, head: Doc, intents: IntentOp[], newDot: () => Dot, evalTestAgainst?: "head" | "base", bumpCounterAbove?: (ctr: number) => void): ApplyResult;
47
+ /**
48
+ * Convenience wrapper: compile a JSON Patch and apply it to a CRDT document.
49
+ * Overloads:
50
+ * - positional: `jsonPatchToCrdt(base, head, patch, newDot, evalTestAgainst?, bumpCounterAbove?)`
51
+ * - object: `jsonPatchToCrdt({ base, head, patch, newDot, evalTestAgainst?, bumpCounterAbove?, semantics? })`
52
+ */
53
+ declare function jsonPatchToCrdt(options: JsonPatchToCrdtOptions): ApplyResult;
54
+ declare function jsonPatchToCrdt(base: Doc, head: Doc, patch: JsonPatchOp[], newDot: () => Dot, evalTestAgainst?: "head" | "base", bumpCounterAbove?: (ctr: number) => void): ApplyResult;
55
+ /**
56
+ * Safe wrapper around `jsonPatchToCrdt`.
57
+ * This function never throws and always returns an `ApplyResult`.
58
+ */
59
+ declare function jsonPatchToCrdtSafe(options: JsonPatchToCrdtOptions): ApplyResult;
60
+ declare function jsonPatchToCrdtSafe(base: Doc, head: Doc, patch: JsonPatchOp[], newDot: () => Dot, evalTestAgainst?: "head" | "base", bumpCounterAbove?: (ctr: number) => void): ApplyResult;
61
+ /** Alias for codebases that prefer `try*` naming for non-throwing APIs. */
62
+ declare const tryJsonPatchToCrdt: typeof jsonPatchToCrdtSafe;
63
+ /**
64
+ * Generate a JSON Patch delta between two CRDT documents.
65
+ * @param base - The base document snapshot.
66
+ * @param head - The current document state.
67
+ * @param options - Diff options (e.g. `{ arrayStrategy: "lcs" }`).
68
+ * @returns An array of JSON Patch operations that transform base into head.
69
+ */
70
+ declare function crdtToJsonPatch(base: Doc, head: Doc, options?: DiffOptions): JsonPatchOp[];
71
+ /**
72
+ * Emit a single root `replace` patch representing the full document state.
73
+ * Use `crdtToJsonPatch(base, head)` for delta patches instead.
74
+ */
75
+ declare function crdtToFullReplace(doc: Doc): JsonPatchOp[];
76
+ //#endregion
77
+ //#region src/materialize.d.ts
78
+ /** Recursively convert a CRDT node graph into a plain JSON value. */
79
+ declare function materialize(node: Node): JsonValue;
80
+ //#endregion
3
81
  //#region src/dot.d.ts
4
82
  declare function compareDot(a: Dot, b: Dot): number;
5
83
  declare function vvHasDot(vv: VersionVector, d: Dot): boolean;
@@ -22,4 +100,4 @@ declare function rgaDelete(seq: RgaSeq, id: ElemId): void;
22
100
  declare function rgaIdAtIndex(seq: RgaSeq, index: number): ElemId | undefined;
23
101
  declare function rgaPrevForInsertAtIndex(seq: RgaSeq, index: number): ElemId;
24
102
  //#endregion
25
- export { ActorId, ApplyPatchAsActorResult, ApplyPatchOptions, ApplyResult, Clock, CrdtState, DiffOptions, Doc, Dot, ElemId, HEAD, IntentOp, JsonPatch, JsonPatchOp, JsonPrimitive, JsonValue, LwwReg, MergeDocOptions, MergeStateOptions, Node, ObjEntry, ObjNode, PatchError, PatchSemantics, ROOT_KEY, RgaElem, RgaSeq, SerializedDoc, SerializedNode, SerializedState, VersionVector, applyIntentsToCrdt, applyPatch, applyPatchAsActor, applyPatchInPlace, cloneClock, cloneDoc, compareDot, compileJsonPatchToIntent, crdtToFullReplace, crdtToJsonPatch, createClock, createState, deserializeDoc, deserializeState, diffJsonPatch, docFromJson, docFromJsonWithDot, dotToElemId, getAtJson, jsonEquals, jsonPatchToCrdt, jsonPatchToCrdtSafe, lwwSet, materialize, mergeDoc, mergeState, newObj, newReg, newSeq, nextDotForActor, objRemove, objSet, observeDot, parseJsonPointer, rgaDelete, rgaIdAtIndex, rgaInsertAfter, rgaLinearizeIds, rgaPrevForInsertAtIndex, serializeDoc, serializeState, stringifyJsonPointer, toJson, vvHasDot, vvMerge };
103
+ export { ActorId, ApplyError, type ApplyPatchAsActorOptions, type ApplyPatchAsActorResult, ApplyPatchInPlaceOptions, ApplyPatchOptions, type ApplyResult, type Clock, type CompilePatchOptions, CrdtState, DiffOptions, type Doc, type Dot, type ElemId, ForkStateOptions, HEAD, type IntentOp, JsonPatch, JsonPatchOp, type JsonPatchToCrdtOptions, JsonPrimitive, JsonValue, type LwwReg, type MergeDocOptions, MergeError, MergeStateOptions, type Node, type ObjEntry, type ObjNode, PatchCompileError, PatchError, PatchErrorReason, PatchSemantics, ROOT_KEY, type RgaElem, type RgaSeq, type SerializedClock, type SerializedDoc, type SerializedNode, type SerializedRgaElem, SerializedState, TryApplyPatchInPlaceResult, TryApplyPatchResult, type TryMergeDocResult, TryMergeStateResult, ValidatePatchResult, type VersionVector, applyIntentsToCrdt, applyPatch, applyPatchAsActor, applyPatchInPlace, cloneClock, cloneDoc, compareDot, compileJsonPatchToIntent, crdtToFullReplace, crdtToJsonPatch, createClock, createState, deserializeDoc, deserializeState, diffJsonPatch, docFromJson, docFromJsonWithDot, dotToElemId, forkState, getAtJson, jsonEquals, jsonPatchToCrdt, jsonPatchToCrdtSafe, lwwSet, materialize, mergeDoc, mergeState, newObj, newReg, newSeq, nextDotForActor, objRemove, objSet, observeDot, parseJsonPointer, rgaDelete, rgaIdAtIndex, rgaInsertAfter, rgaLinearizeIds, rgaPrevForInsertAtIndex, serializeDoc, serializeState, stringifyJsonPointer, toJson, tryApplyPatch, tryApplyPatchInPlace, tryJsonPatchToCrdt, tryMergeDoc, tryMergeState, validateJsonPatch, vvHasDot, vvMerge };
package/dist/internals.js CHANGED
@@ -1,7 +1,9 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- const require_merge = require('./merge-B1BFMhJJ.js');
2
+ const require_merge = require('./merge-BAfuC6bf.js');
3
3
 
4
4
  exports.HEAD = require_merge.HEAD;
5
+ exports.MergeError = require_merge.MergeError;
6
+ exports.PatchCompileError = require_merge.PatchCompileError;
5
7
  exports.PatchError = require_merge.PatchError;
6
8
  exports.ROOT_KEY = require_merge.ROOT_KEY;
7
9
  exports.applyIntentsToCrdt = require_merge.applyIntentsToCrdt;
@@ -22,6 +24,7 @@ exports.diffJsonPatch = require_merge.diffJsonPatch;
22
24
  exports.docFromJson = require_merge.docFromJson;
23
25
  exports.docFromJsonWithDot = require_merge.docFromJsonWithDot;
24
26
  exports.dotToElemId = require_merge.dotToElemId;
27
+ exports.forkState = require_merge.forkState;
25
28
  exports.getAtJson = require_merge.getAtJson;
26
29
  exports.jsonEquals = require_merge.jsonEquals;
27
30
  exports.jsonPatchToCrdt = require_merge.jsonPatchToCrdt;
@@ -47,5 +50,11 @@ exports.serializeDoc = require_merge.serializeDoc;
47
50
  exports.serializeState = require_merge.serializeState;
48
51
  exports.stringifyJsonPointer = require_merge.stringifyJsonPointer;
49
52
  exports.toJson = require_merge.toJson;
53
+ exports.tryApplyPatch = require_merge.tryApplyPatch;
54
+ exports.tryApplyPatchInPlace = require_merge.tryApplyPatchInPlace;
55
+ exports.tryJsonPatchToCrdt = require_merge.tryJsonPatchToCrdt;
56
+ exports.tryMergeDoc = require_merge.tryMergeDoc;
57
+ exports.tryMergeState = require_merge.tryMergeState;
58
+ exports.validateJsonPatch = require_merge.validateJsonPatch;
50
59
  exports.vvHasDot = require_merge.vvHasDot;
51
60
  exports.vvMerge = require_merge.vvMerge;
@@ -1,3 +1,3 @@
1
- import { A as newReg, B as rgaPrevForInsertAtIndex, C as getAtJson, D as ROOT_KEY, E as stringifyJsonPointer, F as HEAD, G as cloneClock, H as dotToElemId, I as rgaDelete, J as observeDot, K as createClock, L as rgaIdAtIndex, M as objRemove, N as objSet, O as lwwSet, P as materialize, R as rgaInsertAfter, S as diffJsonPatch, T as parseJsonPointer, U as vvHasDot, V as compareDot, W as vvMerge, _ as docFromJson, a as serializeDoc, b as jsonPatchToCrdtSafe, c as applyPatch, d as createState, f as toJson, g as crdtToJsonPatch, h as crdtToFullReplace, i as deserializeState, j as newSeq, k as newObj, l as applyPatchAsActor, m as cloneDoc, n as mergeState, o as serializeState, p as applyIntentsToCrdt, q as nextDotForActor, r as deserializeDoc, s as PatchError, t as mergeDoc, u as applyPatchInPlace, v as docFromJsonWithDot, w as jsonEquals, x as compileJsonPatchToIntent, y as jsonPatchToCrdt, z as rgaLinearizeIds } from "./merge-DikOFBWc.mjs";
1
+ import { $ as vvMerge, A as compileJsonPatchToIntent, B as newSeq, C as crdtToJsonPatch, D as jsonPatchToCrdtSafe, E as jsonPatchToCrdt, F as stringifyJsonPointer, G as rgaDelete, H as objSet, I as ROOT_KEY, J as rgaLinearizeIds, K as rgaIdAtIndex, L as lwwSet, M as getAtJson, N as jsonEquals, O as tryJsonPatchToCrdt, P as parseJsonPointer, Q as vvHasDot, R as newObj, S as crdtToFullReplace, T as docFromJsonWithDot, U as materialize, V as objRemove, W as HEAD, X as compareDot, Y as rgaPrevForInsertAtIndex, Z as dotToElemId, _ as tryApplyPatch, a as tryMergeState, b as applyIntentsToCrdt, c as serializeDoc, d as applyPatch, et as cloneClock, f as applyPatchAsActor, g as toJson, h as forkState, i as tryMergeDoc, j as diffJsonPatch, k as PatchCompileError, l as serializeState, m as createState, n as mergeDoc, nt as nextDotForActor, o as deserializeDoc, p as applyPatchInPlace, q as rgaInsertAfter, r as mergeState, rt as observeDot, s as deserializeState, t as MergeError, tt as createClock, u as PatchError, v as tryApplyPatchInPlace, w as docFromJson, x as cloneDoc, y as validateJsonPatch, z as newReg } from "./merge-CKcP1ZPt.mjs";
2
2
 
3
- export { HEAD, PatchError, ROOT_KEY, applyIntentsToCrdt, applyPatch, applyPatchAsActor, applyPatchInPlace, cloneClock, cloneDoc, compareDot, compileJsonPatchToIntent, crdtToFullReplace, crdtToJsonPatch, createClock, createState, deserializeDoc, deserializeState, diffJsonPatch, docFromJson, docFromJsonWithDot, dotToElemId, getAtJson, jsonEquals, jsonPatchToCrdt, jsonPatchToCrdtSafe, lwwSet, materialize, mergeDoc, mergeState, newObj, newReg, newSeq, nextDotForActor, objRemove, objSet, observeDot, parseJsonPointer, rgaDelete, rgaIdAtIndex, rgaInsertAfter, rgaLinearizeIds, rgaPrevForInsertAtIndex, serializeDoc, serializeState, stringifyJsonPointer, toJson, vvHasDot, vvMerge };
3
+ export { HEAD, MergeError, PatchCompileError, PatchError, ROOT_KEY, applyIntentsToCrdt, applyPatch, applyPatchAsActor, applyPatchInPlace, cloneClock, cloneDoc, compareDot, compileJsonPatchToIntent, crdtToFullReplace, crdtToJsonPatch, createClock, createState, deserializeDoc, deserializeState, diffJsonPatch, docFromJson, docFromJsonWithDot, dotToElemId, forkState, getAtJson, jsonEquals, jsonPatchToCrdt, jsonPatchToCrdtSafe, lwwSet, materialize, mergeDoc, mergeState, newObj, newReg, newSeq, nextDotForActor, objRemove, objSet, observeDot, parseJsonPointer, rgaDelete, rgaIdAtIndex, rgaInsertAfter, rgaLinearizeIds, rgaPrevForInsertAtIndex, serializeDoc, serializeState, stringifyJsonPointer, toJson, tryApplyPatch, tryApplyPatchInPlace, tryJsonPatchToCrdt, tryMergeDoc, tryMergeState, validateJsonPatch, vvHasDot, vvMerge };