@loro-dev/json-patch 0.0.0-stage → 0.1.0
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 +314 -2
- package/dist/diff.cjs +1666 -0
- package/dist/diff.d.cts +126 -0
- package/dist/diff.d.ts +126 -0
- package/dist/diff.js +1463 -0
- package/dist/index.cjs +1124 -0
- package/dist/index.d.cts +74 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +1097 -0
- package/dist/loro.cjs +2722 -0
- package/dist/loro.d.cts +115 -0
- package/dist/loro.d.ts +115 -0
- package/dist/loro.js +2710 -0
- package/package.json +70 -3
package/dist/loro.d.cts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { _ as JsonOp, a as PatternSeg, b as JsonValue, g as JsonObject, v as JsonPatch, y as JsonPath } from "./diff.cjs";
|
|
2
|
+
import { Container, ContainerID, ContainerType, LoroDoc, OpId, PeerID, Value } from "loro-crdt";
|
|
3
|
+
|
|
4
|
+
//#region src/loro.d.ts
|
|
5
|
+
/** Supported Loro container types at root or nested paths. */
|
|
6
|
+
type LoroContainerType = ContainerType;
|
|
7
|
+
/** Node representation kind inside {@link LoroSchema}: either a CRDT container or an atomic `LoroValue`. */
|
|
8
|
+
type LoroNodeKind = {
|
|
9
|
+
readonly kind: "Container";
|
|
10
|
+
readonly containerType: LoroContainerType;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: "Value";
|
|
13
|
+
};
|
|
14
|
+
/** Sentinel {@link LoroNodeKind} storing a subtree or string as an atomic `LoroValue` instead of a CRDT container. */
|
|
15
|
+
declare const LoroValueKind: LoroNodeKind;
|
|
16
|
+
/** Helper returning `{ kind: "Container", containerType }` for {@link LoroSchema} rules. */
|
|
17
|
+
declare function containerKind(containerType: LoroContainerType): LoroNodeKind;
|
|
18
|
+
/** Path-matched override rule mapping matching paths to a specific {@link LoroNodeKind}. */
|
|
19
|
+
interface LoroSchemaRule {
|
|
20
|
+
/** Pattern segments (`string`, `number`, {@link ANY_SEG}, {@link ANY_REST_SEG}). */
|
|
21
|
+
readonly pattern: readonly PatternSeg[];
|
|
22
|
+
readonly kind: LoroNodeKind;
|
|
23
|
+
}
|
|
24
|
+
/** Configuration passed to {@link LoroSchema}. */
|
|
25
|
+
interface LoroSchemaConfig {
|
|
26
|
+
/** Root container types keyed by root name (e.g. `{ session: "Map", history: "List", mq: "MovableList" }`). */
|
|
27
|
+
readonly roots?: ReadonlyMap<string, LoroContainerType> | Readonly<Record<string, LoroContainerType>>;
|
|
28
|
+
/** Ordered path override rules (first match wins). */
|
|
29
|
+
readonly rules?: readonly LoroSchemaRule[];
|
|
30
|
+
/** Default container/value kind for plain objects. Defaults to `LoroMap`. */
|
|
31
|
+
readonly defaultObject?: LoroNodeKind;
|
|
32
|
+
/** Default container/value kind for arrays. Defaults to `LoroList`. */
|
|
33
|
+
readonly defaultArray?: LoroNodeKind;
|
|
34
|
+
/** Default container/value kind for strings. Defaults to {@link LoroValueKind} (primitive `LoroValue::String`). */
|
|
35
|
+
readonly defaultString?: LoroNodeKind;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Declarative schema controlling how JSON paths map to `LoroDoc` containers vs atomic `LoroValue`s
|
|
39
|
+
* during {@link applyJsonPatchToLoro}, {@link loroToJson}, and {@link diffLoroJson}.
|
|
40
|
+
*/
|
|
41
|
+
declare class LoroSchema {
|
|
42
|
+
readonly roots: ReadonlyMap<string, LoroContainerType>;
|
|
43
|
+
readonly rules: readonly LoroSchemaRule[];
|
|
44
|
+
readonly defaultObject: LoroNodeKind;
|
|
45
|
+
readonly defaultArray: LoroNodeKind;
|
|
46
|
+
readonly defaultString: LoroNodeKind;
|
|
47
|
+
constructor(config?: LoroSchemaConfig);
|
|
48
|
+
root(name: string, containerType: LoroContainerType): LoroSchema;
|
|
49
|
+
rule(pattern: readonly PatternSeg[], kind: LoroNodeKind): LoroSchema;
|
|
50
|
+
withDefaultObject(kind: LoroNodeKind): LoroSchema;
|
|
51
|
+
withDefaultArray(kind: LoroNodeKind): LoroSchema;
|
|
52
|
+
withDefaultString(kind: LoroNodeKind): LoroSchema;
|
|
53
|
+
static discover(doc: LoroDoc): LoroSchema;
|
|
54
|
+
explicitKindAt(path: JsonPath): LoroNodeKind | undefined;
|
|
55
|
+
kindForNewValue(path: JsonPath, val: JsonValue): LoroNodeKind;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Schema mismatch policy when applying JSON patches to a `LoroDoc`:
|
|
59
|
+
* - `"strict"`: rejects structural mismatches (e.g. replacing a root container with wrong kind).
|
|
60
|
+
* - `"permissive"`: allows coercions/fallbacks where safe.
|
|
61
|
+
*/
|
|
62
|
+
type ApplyMode = "strict" | "permissive";
|
|
63
|
+
/** Options for {@link applyJsonPatchToLoro} and {@link applyJsonPatchAt}. */
|
|
64
|
+
interface LoroApplyOptions {
|
|
65
|
+
/** Schema governing container vs `LoroValue` creation. */
|
|
66
|
+
readonly schema: LoroSchema;
|
|
67
|
+
/** Structural validation mode. Defaults to `"strict"`. */
|
|
68
|
+
readonly mode?: ApplyMode;
|
|
69
|
+
}
|
|
70
|
+
/** Result metadata returned by {@link applyJsonPatchToLoro} and {@link applyJsonPatchAt}. */
|
|
71
|
+
interface LoroApplied {
|
|
72
|
+
/** `true` if at least one CRDT mutation was written to the `LoroDoc`. */
|
|
73
|
+
readonly changed: boolean;
|
|
74
|
+
/** Mapping of client-specified tree IDs to assigned `LoroTree` node IDs. */
|
|
75
|
+
readonly createdTreeIds: readonly {
|
|
76
|
+
readonly specifiedId?: string;
|
|
77
|
+
readonly assignedId: string;
|
|
78
|
+
}[];
|
|
79
|
+
}
|
|
80
|
+
/** Failure classification for {@link LoroApplyError}. */
|
|
81
|
+
type LoroApplyErrorKind = "SchemaViolation" | "Patch" | "Value" | "Loro" | "PeerIdConflict";
|
|
82
|
+
/** Error thrown when applying JSON patches to or projecting from a `LoroDoc`. */
|
|
83
|
+
declare class LoroApplyError extends Error {
|
|
84
|
+
readonly kind: LoroApplyErrorKind;
|
|
85
|
+
readonly opIndex?: number;
|
|
86
|
+
constructor(kind: LoroApplyErrorKind, message: string, opIndex?: number);
|
|
87
|
+
}
|
|
88
|
+
/** Projects an atomic `loro-crdt` `Value` into a {@link JsonValue}. */
|
|
89
|
+
declare function projectLoroValue(val: Value): JsonValue;
|
|
90
|
+
/** Recursively projects a `LoroDoc` container (`Map`, `List`, `MovableList`, `Text`, `Tree`, `Counter`) into JSON. */
|
|
91
|
+
declare function projectContainer(container: Container): JsonValue;
|
|
92
|
+
/** Projects a container identified by `cid`, returning the empty default JSON shape if not yet materialized. */
|
|
93
|
+
declare function projectContainerById(doc: LoroDoc, cid: ContainerID, fallbackType?: LoroContainerType): JsonValue;
|
|
94
|
+
/**
|
|
95
|
+
* Projects all roots in `doc` matching `schema` (or discovered shallow roots if omitted) into a plain {@link JsonObject}.
|
|
96
|
+
*/
|
|
97
|
+
declare function loroToJson(doc: LoroDoc, schema?: LoroSchema): JsonObject;
|
|
98
|
+
/** Converts a {@link JsonValue} into a primitive/nested `loro-crdt` `Value`. */
|
|
99
|
+
declare function jsonToLoroValue(val: JsonValue): Value;
|
|
100
|
+
/**
|
|
101
|
+
* Computes minimal CRDT-aware {@link JsonOp}s describing state changes between `fromFrontiers` and `toFrontiers`.
|
|
102
|
+
*/
|
|
103
|
+
declare function diffLoroJson(doc: LoroDoc, fromFrontiers: readonly OpId[], toFrontiers: readonly OpId[], schema: LoroSchema): JsonOp[];
|
|
104
|
+
/**
|
|
105
|
+
* Validates `patch` against `doc` and `options.schema`, then applies all CRDT mutations atomically.
|
|
106
|
+
* @throws {@link LoroApplyError} on schema violations, invalid patches, or detached `LoroDoc`.
|
|
107
|
+
*/
|
|
108
|
+
declare function applyJsonPatchToLoro(doc: LoroDoc, patch: JsonPatch, options: LoroApplyOptions): LoroApplied;
|
|
109
|
+
/**
|
|
110
|
+
* Rebases/applies `patch` starting from `baseFrontiers` on `doc`, committing CRDT ops under optional `peerId`.
|
|
111
|
+
* @throws {@link LoroApplyError} on conflicts, schema violations, or peer ID conflict.
|
|
112
|
+
*/
|
|
113
|
+
declare function applyJsonPatchAt(doc: LoroDoc, baseFrontiers: readonly OpId[], patch: JsonPatch, options: LoroApplyOptions, peerId?: number | bigint | PeerID): LoroApplied;
|
|
114
|
+
//#endregion
|
|
115
|
+
export { ApplyMode, LoroApplied, LoroApplyError, LoroApplyErrorKind, LoroApplyOptions, LoroContainerType, LoroNodeKind, LoroSchema, LoroSchemaConfig, LoroSchemaRule, LoroValueKind, applyJsonPatchAt, applyJsonPatchToLoro, containerKind, diffLoroJson, jsonToLoroValue, loroToJson, projectContainer, projectContainerById, projectLoroValue };
|
package/dist/loro.d.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { _ as JsonOp, a as PatternSeg, b as JsonValue, g as JsonObject, v as JsonPatch, y as JsonPath } from "./diff.js";
|
|
2
|
+
import { Container, ContainerID, ContainerType, LoroDoc, OpId, PeerID, Value } from "loro-crdt";
|
|
3
|
+
|
|
4
|
+
//#region src/loro.d.ts
|
|
5
|
+
/** Supported Loro container types at root or nested paths. */
|
|
6
|
+
type LoroContainerType = ContainerType;
|
|
7
|
+
/** Node representation kind inside {@link LoroSchema}: either a CRDT container or an atomic `LoroValue`. */
|
|
8
|
+
type LoroNodeKind = {
|
|
9
|
+
readonly kind: "Container";
|
|
10
|
+
readonly containerType: LoroContainerType;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: "Value";
|
|
13
|
+
};
|
|
14
|
+
/** Sentinel {@link LoroNodeKind} storing a subtree or string as an atomic `LoroValue` instead of a CRDT container. */
|
|
15
|
+
declare const LoroValueKind: LoroNodeKind;
|
|
16
|
+
/** Helper returning `{ kind: "Container", containerType }` for {@link LoroSchema} rules. */
|
|
17
|
+
declare function containerKind(containerType: LoroContainerType): LoroNodeKind;
|
|
18
|
+
/** Path-matched override rule mapping matching paths to a specific {@link LoroNodeKind}. */
|
|
19
|
+
interface LoroSchemaRule {
|
|
20
|
+
/** Pattern segments (`string`, `number`, {@link ANY_SEG}, {@link ANY_REST_SEG}). */
|
|
21
|
+
readonly pattern: readonly PatternSeg[];
|
|
22
|
+
readonly kind: LoroNodeKind;
|
|
23
|
+
}
|
|
24
|
+
/** Configuration passed to {@link LoroSchema}. */
|
|
25
|
+
interface LoroSchemaConfig {
|
|
26
|
+
/** Root container types keyed by root name (e.g. `{ session: "Map", history: "List", mq: "MovableList" }`). */
|
|
27
|
+
readonly roots?: ReadonlyMap<string, LoroContainerType> | Readonly<Record<string, LoroContainerType>>;
|
|
28
|
+
/** Ordered path override rules (first match wins). */
|
|
29
|
+
readonly rules?: readonly LoroSchemaRule[];
|
|
30
|
+
/** Default container/value kind for plain objects. Defaults to `LoroMap`. */
|
|
31
|
+
readonly defaultObject?: LoroNodeKind;
|
|
32
|
+
/** Default container/value kind for arrays. Defaults to `LoroList`. */
|
|
33
|
+
readonly defaultArray?: LoroNodeKind;
|
|
34
|
+
/** Default container/value kind for strings. Defaults to {@link LoroValueKind} (primitive `LoroValue::String`). */
|
|
35
|
+
readonly defaultString?: LoroNodeKind;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Declarative schema controlling how JSON paths map to `LoroDoc` containers vs atomic `LoroValue`s
|
|
39
|
+
* during {@link applyJsonPatchToLoro}, {@link loroToJson}, and {@link diffLoroJson}.
|
|
40
|
+
*/
|
|
41
|
+
declare class LoroSchema {
|
|
42
|
+
readonly roots: ReadonlyMap<string, LoroContainerType>;
|
|
43
|
+
readonly rules: readonly LoroSchemaRule[];
|
|
44
|
+
readonly defaultObject: LoroNodeKind;
|
|
45
|
+
readonly defaultArray: LoroNodeKind;
|
|
46
|
+
readonly defaultString: LoroNodeKind;
|
|
47
|
+
constructor(config?: LoroSchemaConfig);
|
|
48
|
+
root(name: string, containerType: LoroContainerType): LoroSchema;
|
|
49
|
+
rule(pattern: readonly PatternSeg[], kind: LoroNodeKind): LoroSchema;
|
|
50
|
+
withDefaultObject(kind: LoroNodeKind): LoroSchema;
|
|
51
|
+
withDefaultArray(kind: LoroNodeKind): LoroSchema;
|
|
52
|
+
withDefaultString(kind: LoroNodeKind): LoroSchema;
|
|
53
|
+
static discover(doc: LoroDoc): LoroSchema;
|
|
54
|
+
explicitKindAt(path: JsonPath): LoroNodeKind | undefined;
|
|
55
|
+
kindForNewValue(path: JsonPath, val: JsonValue): LoroNodeKind;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Schema mismatch policy when applying JSON patches to a `LoroDoc`:
|
|
59
|
+
* - `"strict"`: rejects structural mismatches (e.g. replacing a root container with wrong kind).
|
|
60
|
+
* - `"permissive"`: allows coercions/fallbacks where safe.
|
|
61
|
+
*/
|
|
62
|
+
type ApplyMode = "strict" | "permissive";
|
|
63
|
+
/** Options for {@link applyJsonPatchToLoro} and {@link applyJsonPatchAt}. */
|
|
64
|
+
interface LoroApplyOptions {
|
|
65
|
+
/** Schema governing container vs `LoroValue` creation. */
|
|
66
|
+
readonly schema: LoroSchema;
|
|
67
|
+
/** Structural validation mode. Defaults to `"strict"`. */
|
|
68
|
+
readonly mode?: ApplyMode;
|
|
69
|
+
}
|
|
70
|
+
/** Result metadata returned by {@link applyJsonPatchToLoro} and {@link applyJsonPatchAt}. */
|
|
71
|
+
interface LoroApplied {
|
|
72
|
+
/** `true` if at least one CRDT mutation was written to the `LoroDoc`. */
|
|
73
|
+
readonly changed: boolean;
|
|
74
|
+
/** Mapping of client-specified tree IDs to assigned `LoroTree` node IDs. */
|
|
75
|
+
readonly createdTreeIds: readonly {
|
|
76
|
+
readonly specifiedId?: string;
|
|
77
|
+
readonly assignedId: string;
|
|
78
|
+
}[];
|
|
79
|
+
}
|
|
80
|
+
/** Failure classification for {@link LoroApplyError}. */
|
|
81
|
+
type LoroApplyErrorKind = "SchemaViolation" | "Patch" | "Value" | "Loro" | "PeerIdConflict";
|
|
82
|
+
/** Error thrown when applying JSON patches to or projecting from a `LoroDoc`. */
|
|
83
|
+
declare class LoroApplyError extends Error {
|
|
84
|
+
readonly kind: LoroApplyErrorKind;
|
|
85
|
+
readonly opIndex?: number;
|
|
86
|
+
constructor(kind: LoroApplyErrorKind, message: string, opIndex?: number);
|
|
87
|
+
}
|
|
88
|
+
/** Projects an atomic `loro-crdt` `Value` into a {@link JsonValue}. */
|
|
89
|
+
declare function projectLoroValue(val: Value): JsonValue;
|
|
90
|
+
/** Recursively projects a `LoroDoc` container (`Map`, `List`, `MovableList`, `Text`, `Tree`, `Counter`) into JSON. */
|
|
91
|
+
declare function projectContainer(container: Container): JsonValue;
|
|
92
|
+
/** Projects a container identified by `cid`, returning the empty default JSON shape if not yet materialized. */
|
|
93
|
+
declare function projectContainerById(doc: LoroDoc, cid: ContainerID, fallbackType?: LoroContainerType): JsonValue;
|
|
94
|
+
/**
|
|
95
|
+
* Projects all roots in `doc` matching `schema` (or discovered shallow roots if omitted) into a plain {@link JsonObject}.
|
|
96
|
+
*/
|
|
97
|
+
declare function loroToJson(doc: LoroDoc, schema?: LoroSchema): JsonObject;
|
|
98
|
+
/** Converts a {@link JsonValue} into a primitive/nested `loro-crdt` `Value`. */
|
|
99
|
+
declare function jsonToLoroValue(val: JsonValue): Value;
|
|
100
|
+
/**
|
|
101
|
+
* Computes minimal CRDT-aware {@link JsonOp}s describing state changes between `fromFrontiers` and `toFrontiers`.
|
|
102
|
+
*/
|
|
103
|
+
declare function diffLoroJson(doc: LoroDoc, fromFrontiers: readonly OpId[], toFrontiers: readonly OpId[], schema: LoroSchema): JsonOp[];
|
|
104
|
+
/**
|
|
105
|
+
* Validates `patch` against `doc` and `options.schema`, then applies all CRDT mutations atomically.
|
|
106
|
+
* @throws {@link LoroApplyError} on schema violations, invalid patches, or detached `LoroDoc`.
|
|
107
|
+
*/
|
|
108
|
+
declare function applyJsonPatchToLoro(doc: LoroDoc, patch: JsonPatch, options: LoroApplyOptions): LoroApplied;
|
|
109
|
+
/**
|
|
110
|
+
* Rebases/applies `patch` starting from `baseFrontiers` on `doc`, committing CRDT ops under optional `peerId`.
|
|
111
|
+
* @throws {@link LoroApplyError} on conflicts, schema violations, or peer ID conflict.
|
|
112
|
+
*/
|
|
113
|
+
declare function applyJsonPatchAt(doc: LoroDoc, baseFrontiers: readonly OpId[], patch: JsonPatch, options: LoroApplyOptions, peerId?: number | bigint | PeerID): LoroApplied;
|
|
114
|
+
//#endregion
|
|
115
|
+
export { ApplyMode, LoroApplied, LoroApplyError, LoroApplyErrorKind, LoroApplyOptions, LoroContainerType, LoroNodeKind, LoroSchema, LoroSchemaConfig, LoroSchemaRule, LoroValueKind, applyJsonPatchAt, applyJsonPatchToLoro, containerKind, diffLoroJson, jsonToLoroValue, loroToJson, projectContainer, projectContainerById, projectLoroValue };
|