loro-repo 0.23.2 → 0.24.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 +57 -0
- package/dist/doc-updates.cjs +108 -0
- package/dist/doc-updates.cjs.map +1 -1
- package/dist/doc-updates.js +90 -1
- package/dist/doc-updates.js.map +1 -1
- package/dist/effect.cjs +1 -1
- package/dist/effect.js +1 -1
- package/dist/index.cjs +120 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +120 -71
- package/dist/index.js.map +1 -1
- package/dist/meta-snapshot.cjs.map +1 -1
- package/dist/meta-snapshot.js.map +1 -1
- package/dist/storage/filesystem.cjs +1 -1
- package/dist/storage/filesystem.js +1 -1
- package/dist/storage/indexeddb.cjs +1 -1
- package/dist/storage/indexeddb.js +1 -1
- package/dist/storage/sqlite.cjs +137 -44
- package/dist/storage/sqlite.cjs.map +1 -1
- package/dist/storage/sqlite.d.cts +38 -0
- package/dist/storage/sqlite.d.ts +38 -0
- package/dist/storage/sqlite.js +137 -44
- package/dist/storage/sqlite.js.map +1 -1
- package/dist/types.d.cts +8 -0
- package/dist/types.d.ts +8 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -148,6 +148,57 @@ Key points:
|
|
|
148
148
|
- `FileSystemStorageAdaptor` (`src/storage/filesystem.ts`)
|
|
149
149
|
- `SqliteRepoStore` (`src/storage/sqlite.ts`, Node.js only)
|
|
150
150
|
|
|
151
|
+
## Pending imported document updates
|
|
152
|
+
|
|
153
|
+
This release requires `loro-crdt ^1.17.0`, which provides
|
|
154
|
+
`LoroDoc.exportPendingUpdates()`. Upgrade Loro together with LoroRepo. Existing
|
|
155
|
+
stored documents and update journals remain compatible.
|
|
156
|
+
|
|
157
|
+
Promise-managed documents automatically persist received updates even when their
|
|
158
|
+
causal dependencies are missing and no applied event or version change occurs.
|
|
159
|
+
This includes `import`, `importBatch`, the legacy `importUpdateBatch`, and
|
|
160
|
+
`importJsonUpdates`. The methods keep their original receiver, return and error
|
|
161
|
+
behavior. Detached reads copy both applied state and pending operations. They
|
|
162
|
+
retry changing export checkpoints at most eight times, then reject if a subscriber
|
|
163
|
+
keeps creating another transaction during every commit.
|
|
164
|
+
|
|
165
|
+
Pending operations use the existing ordinary `doc-update` journal, without a
|
|
166
|
+
second sidecar or storage-format migration. Successful incomplete imports retain
|
|
167
|
+
only their newly received input, rather than appending the entire growing pending
|
|
168
|
+
queue at every save. Complete imports keep the normal applied-version delta path;
|
|
169
|
+
complete snapshots are not stored a second time as raw input. Loro's
|
|
170
|
+
`exportPendingUpdates()` supplies a consistent pending checkpoint and the recovery
|
|
171
|
+
fallback for an import that accepted a prefix before failing. Each save captures
|
|
172
|
+
applied bytes and pending bytes before awaiting storage. An atomic
|
|
173
|
+
`saveMany` writes them together when available; otherwise pending bytes are
|
|
174
|
+
appended first, retaining the previous checkpoint if a later write fails.
|
|
175
|
+
Arrivals during an asynchronous save remain dirty for a later capture. Encoding
|
|
176
|
+
and storage failures reject explicit persistence and retain work for retry.
|
|
177
|
+
An absent pending result clears the in-memory pending baseline; after the missing
|
|
178
|
+
dependencies apply, normal journal consolidation removes the covered records.
|
|
179
|
+
|
|
180
|
+
Custom storage adapters must replay and retain incomplete `doc-update` inputs.
|
|
181
|
+
Saving a `doc-snapshot` must preserve unresolved journal inputs and concurrently
|
|
182
|
+
appended inputs: an ordinary snapshot only contains applied state, even when it
|
|
183
|
+
comes from an explicit persistence barrier. Importing an update and immediately
|
|
184
|
+
saving only an ordinary snapshot loses its pending operations. The bundled SQLite, IndexedDB and filesystem adapters already
|
|
185
|
+
retain such journals, including data written by older versions. Native Effect
|
|
186
|
+
controlled imports keep their existing exact-input journal; they do not store a
|
|
187
|
+
second copy through this Promise observer. Pending persistence does not establish
|
|
188
|
+
remote catch-up or permit cursor advancement; transport guards are unchanged.
|
|
189
|
+
|
|
190
|
+
Binary update inputs are copied before import callbacks can change the caller's
|
|
191
|
+
buffer. Known mode-3 snapshot-only imports skip that copy using Loro's documented
|
|
192
|
+
envelope; the original import still validates their contents. Mixed batches keep
|
|
193
|
+
all their inputs until the aggregate import status is known, then discard copies
|
|
194
|
+
when complete. This preserves the original bytes if only part of a batch applies.
|
|
195
|
+
Unloading retries a changing persistence checkpoint at most eight times; if writes
|
|
196
|
+
continue, it rejects while keeping the cached document dirty for a later retry.
|
|
197
|
+
This limit concerns local persistence capture, independently of network recovery.
|
|
198
|
+
`flush()` retains its existing drain-until-quiescent contract, so it can continue
|
|
199
|
+
while a producer keeps writing. `destroy()` stops rooms and closes transports
|
|
200
|
+
before running the document manager's final flush.
|
|
201
|
+
|
|
151
202
|
## Classifying storage failures
|
|
152
203
|
|
|
153
204
|
`IndexedDBStorageAdaptor` wraps backend failures in a `RepoStorageError`
|
|
@@ -501,6 +552,12 @@ nothing across restarts can use `persistence: { mode: "ephemeral" }`.
|
|
|
501
552
|
| `database` | — | Pass a pre-constructed `better-sqlite3` `Database` to share a connection; the caller owns its lifetime. |
|
|
502
553
|
| `tablePrefix` | `""` | Optional prefix for all table names, useful when multiple repos share one database. |
|
|
503
554
|
| `wal` | `true` | Enables `journal_mode=WAL` + `synchronous=NORMAL` for fast, durable writes. |
|
|
555
|
+
| `busyTimeoutMs` | `0` (`5000` for a supplied `database`) | Longest one SQLite call may block the thread for another connection's lock after opening. Opening itself may wait 5000 ms. |
|
|
556
|
+
| `busyRetryMs` | `5000` | How long after its first attempt an operation that hits `SQLITE_BUSY` keeps retrying, with an asynchronous backoff, before it rejects with the driver's error. Operations queued behind it start their own budget when they reach the head. |
|
|
557
|
+
|
|
558
|
+
While another connection or process holds the write lock, storage operations
|
|
559
|
+
wait without blocking the event loop and still reach SQLite in the order they
|
|
560
|
+
were requested. Closing the store rejects operations that are still waiting.
|
|
504
561
|
|
|
505
562
|
The schema (created on first use) is:
|
|
506
563
|
|
package/dist/doc-updates.cjs
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
const require_chunk = require('./chunk.cjs');
|
|
2
|
+
let loro_crdt = require("loro-crdt");
|
|
3
|
+
loro_crdt = require_chunk.__toESM(loro_crdt);
|
|
1
4
|
|
|
2
5
|
//#region src/storage/doc-updates.ts
|
|
3
6
|
const pending = Symbol.for("loro-repo.document-pending-updates.v1");
|
|
@@ -44,6 +47,93 @@ function applyStoredDocUpdates(doc, updates) {
|
|
|
44
47
|
}
|
|
45
48
|
const storedDocComplete = (doc) => doc[completeness] !== false;
|
|
46
49
|
const storedPendingUpdates = (doc) => doc[pending] ?? [];
|
|
50
|
+
/**
|
|
51
|
+
* The documented Loro envelope has magic `loro` and a big-endian mode at
|
|
52
|
+
* bytes 20..22. Mode 3 snapshots contain applied history, never pending input.
|
|
53
|
+
* This is only a copy optimization: import still validates the entire blob.
|
|
54
|
+
* Unknown/legacy encodings retain the conservative copy path.
|
|
55
|
+
* https://github.com/loro-dev/loro/blob/main/docs/encoding-xxhash32.md#document-envelope-checksum
|
|
56
|
+
*/
|
|
57
|
+
function isCompleteSnapshot(bytes) {
|
|
58
|
+
return bytes.length >= 22 && bytes[0] === 108 && bytes[1] === 111 && bytes[2] === 114 && bytes[3] === 111 && bytes[20] === 0 && bytes[21] === 3;
|
|
59
|
+
}
|
|
60
|
+
/** Preserve exact successful inputs, including pending-only imports with no event. */
|
|
61
|
+
function observeDocImports(doc, onImport) {
|
|
62
|
+
const restorers = [];
|
|
63
|
+
const observe = (key) => {
|
|
64
|
+
const original = doc[key];
|
|
65
|
+
const own = Object.getOwnPropertyDescriptor(doc, key);
|
|
66
|
+
const wrapped = function(...args) {
|
|
67
|
+
let input;
|
|
68
|
+
if (this === doc) try {
|
|
69
|
+
if (key === "importJsonUpdates") input = {
|
|
70
|
+
kind: "json",
|
|
71
|
+
json: typeof args[0] === "string" ? args[0] : structuredClone(args[0])
|
|
72
|
+
};
|
|
73
|
+
else {
|
|
74
|
+
const updates = key === "import" ? [args[0]] : args[0];
|
|
75
|
+
input = {
|
|
76
|
+
kind: "binary",
|
|
77
|
+
updates: updates.every(isCompleteSnapshot) ? [] : updates.map((bytes) => Uint8Array.from(bytes))
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
} catch {}
|
|
81
|
+
let succeeded = false;
|
|
82
|
+
try {
|
|
83
|
+
const result = Reflect.apply(original, this, args);
|
|
84
|
+
succeeded = true;
|
|
85
|
+
if (!result.pending?.size) input = {
|
|
86
|
+
kind: "binary",
|
|
87
|
+
updates: []
|
|
88
|
+
};
|
|
89
|
+
else if (input?.kind === "binary" && input.updates.length === 0) input = void 0;
|
|
90
|
+
return result;
|
|
91
|
+
} finally {
|
|
92
|
+
if (this === doc) onImport(succeeded ? input : void 0);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
Object.defineProperty(doc, key, {
|
|
96
|
+
value: wrapped,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true
|
|
99
|
+
});
|
|
100
|
+
restorers.push(() => {
|
|
101
|
+
if (doc[key] !== wrapped) return;
|
|
102
|
+
if (own) Object.defineProperty(doc, key, own);
|
|
103
|
+
else Reflect.deleteProperty(doc, key);
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
try {
|
|
107
|
+
observe("import");
|
|
108
|
+
observe("importBatch");
|
|
109
|
+
observe("importUpdateBatch");
|
|
110
|
+
observe("importJsonUpdates");
|
|
111
|
+
} catch (error) {
|
|
112
|
+
for (const restore of restorers) restore();
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
return () => {
|
|
116
|
+
for (const restore of restorers) restore();
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/** Convert only this JSON input, not the live document's accumulated pending queue. */
|
|
120
|
+
function receivedDocUpdates(input) {
|
|
121
|
+
if (input.kind === "binary") return input.updates;
|
|
122
|
+
const copy = new loro_crdt.LoroDoc();
|
|
123
|
+
try {
|
|
124
|
+
copy.importJsonUpdates(input.json);
|
|
125
|
+
const updates = [];
|
|
126
|
+
if (copy.oplogFrontiers().length) updates.push(copy.export({ mode: "update" }));
|
|
127
|
+
const pending$1 = copy.exportPendingUpdates();
|
|
128
|
+
if (pending$1 !== void 0) updates.push(pending$1);
|
|
129
|
+
return updates;
|
|
130
|
+
} finally {
|
|
131
|
+
copy.free();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function samePendingUpdates(left, right) {
|
|
135
|
+
return left === right || left !== void 0 && right !== void 0 && left.length === right.length && left.every((byte, index) => byte === right[index]);
|
|
136
|
+
}
|
|
47
137
|
|
|
48
138
|
//#endregion
|
|
49
139
|
Object.defineProperty(exports, 'applyStoredDocUpdates', {
|
|
@@ -58,6 +148,24 @@ Object.defineProperty(exports, 'makeDocImporter', {
|
|
|
58
148
|
return makeDocImporter;
|
|
59
149
|
}
|
|
60
150
|
});
|
|
151
|
+
Object.defineProperty(exports, 'observeDocImports', {
|
|
152
|
+
enumerable: true,
|
|
153
|
+
get: function () {
|
|
154
|
+
return observeDocImports;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
Object.defineProperty(exports, 'receivedDocUpdates', {
|
|
158
|
+
enumerable: true,
|
|
159
|
+
get: function () {
|
|
160
|
+
return receivedDocUpdates;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
Object.defineProperty(exports, 'samePendingUpdates', {
|
|
164
|
+
enumerable: true,
|
|
165
|
+
get: function () {
|
|
166
|
+
return samePendingUpdates;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
61
169
|
Object.defineProperty(exports, 'storedDocComplete', {
|
|
62
170
|
enumerable: true,
|
|
63
171
|
get: function () {
|
package/dist/doc-updates.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"doc-updates.cjs","names":["remaining: Array<{ start: number; end: number }>"],"sources":["../src/storage/doc-updates.ts"],"sourcesContent":["import type { LoroDoc } from 'loro-crdt';\n\nconst pending = Symbol.for('loro-repo.document-pending-updates.v1');\nconst completeness = Symbol.for('loro-repo.document-import-completeness.v1');\ntype TrackedDoc = LoroDoc & { [completeness]?: boolean; [pending]?: readonly Uint8Array[] };\n\n/** Pending input bytes cannot be represented by a snapshot. Keep their journal. */\nexport function makeDocImporter(doc: LoroDoc) {\n const unresolved = new Map<string, Array<{ start: number; end: number }>>();\n return (\n updates: readonly Uint8Array[],\n onImport?: (status: ReturnType<LoroDoc['import']>) => void\n ): boolean => {\n // Preserve sequential import and its observable hydration boundary. Track\n // all unresolved ranges: a later successful import can resolve an earlier one.\n for (const bytes of updates) {\n const status = doc.import(bytes);\n onImport?.(status);\n for (const [peer, span] of status.pending ?? []) {\n const key = String(peer);\n const spans = unresolved.get(key) ?? [];\n spans.push(span);\n unresolved.set(key, spans);\n }\n for (const [peer, applied] of status.success) {\n const key = String(peer);\n const remaining: Array<{ start: number; end: number }> = [];\n for (const span of unresolved.get(key) ?? []) {\n if (applied.end <= span.start || applied.start >= span.end) remaining.push(span);\n else {\n if (span.start < applied.start)\n remaining.push({ start: span.start, end: applied.start });\n if (applied.end < span.end) remaining.push({ start: applied.end, end: span.end });\n }\n }\n if (remaining.length) unresolved.set(key, remaining);\n else unresolved.delete(key);\n }\n }\n return unresolved.size === 0;\n };\n}\n\nexport function applyStoredDocUpdates(doc: LoroDoc, updates: readonly Uint8Array[]): boolean {\n const complete = makeDocImporter(doc)(updates);\n (doc as TrackedDoc)[completeness] = complete;\n (doc as TrackedDoc)[pending] = complete ? [] : updates.map((bytes) => bytes.slice());\n return complete;\n}\nexport const storedDocComplete = (doc: LoroDoc): boolean =>\n (doc as TrackedDoc)[completeness] !== false;\n\nexport const storedPendingUpdates = (doc: LoroDoc): readonly Uint8Array[] =>\n (doc as TrackedDoc)[pending] ?? [];\n"],"mappings":";;AAEA,MAAM,UAAU,OAAO,IAAI,wCAAwC;AACnE,MAAM,eAAe,OAAO,IAAI,4CAA4C;;AAI5E,SAAgB,gBAAgB,KAAc;CAC5C,MAAM,6BAAa,IAAI,KAAoD;AAC3E,SACE,SACA,aACY;AAGZ,OAAK,MAAM,SAAS,SAAS;GAC3B,MAAM,SAAS,IAAI,OAAO,MAAM;AAChC,cAAW,OAAO;AAClB,QAAK,MAAM,CAAC,MAAM,SAAS,OAAO,WAAW,EAAE,EAAE;IAC/C,MAAM,MAAM,OAAO,KAAK;IACxB,MAAM,QAAQ,WAAW,IAAI,IAAI,IAAI,EAAE;AACvC,UAAM,KAAK,KAAK;AAChB,eAAW,IAAI,KAAK,MAAM;;AAE5B,QAAK,MAAM,CAAC,MAAM,YAAY,OAAO,SAAS;IAC5C,MAAM,MAAM,OAAO,KAAK;IACxB,MAAMA,YAAmD,EAAE;AAC3D,SAAK,MAAM,QAAQ,WAAW,IAAI,IAAI,IAAI,EAAE,CAC1C,KAAI,QAAQ,OAAO,KAAK,SAAS,QAAQ,SAAS,KAAK,IAAK,WAAU,KAAK,KAAK;SAC3E;AACH,SAAI,KAAK,QAAQ,QAAQ,MACvB,WAAU,KAAK;MAAE,OAAO,KAAK;MAAO,KAAK,QAAQ;MAAO,CAAC;AAC3D,SAAI,QAAQ,MAAM,KAAK,IAAK,WAAU,KAAK;MAAE,OAAO,QAAQ;MAAK,KAAK,KAAK;MAAK,CAAC;;AAGrF,QAAI,UAAU,OAAQ,YAAW,IAAI,KAAK,UAAU;QAC/C,YAAW,OAAO,IAAI;;;AAG/B,SAAO,WAAW,SAAS;;;AAI/B,SAAgB,sBAAsB,KAAc,SAAyC;CAC3F,MAAM,WAAW,gBAAgB,IAAI,CAAC,QAAQ;AAC9C,CAAC,IAAmB,gBAAgB;AACpC,CAAC,IAAmB,WAAW,WAAW,EAAE,GAAG,QAAQ,KAAK,UAAU,MAAM,OAAO,CAAC;AACpF,QAAO;;AAET,MAAa,qBAAqB,QAC/B,IAAmB,kBAAkB;AAExC,MAAa,wBAAwB,QAClC,IAAmB,YAAY,EAAE"}
|
|
1
|
+
{"version":3,"file":"doc-updates.cjs","names":["remaining: Array<{ start: number; end: number }>","restorers: Array<() => void>","input: ReceivedDocInput | undefined","LoroDoc","updates: Uint8Array[]","pending"],"sources":["../src/storage/doc-updates.ts"],"sourcesContent":["import { LoroDoc, type ImportStatus } from 'loro-crdt';\n\nconst pending = Symbol.for('loro-repo.document-pending-updates.v1');\nconst completeness = Symbol.for('loro-repo.document-import-completeness.v1');\ntype TrackedDoc = LoroDoc & { [completeness]?: boolean; [pending]?: readonly Uint8Array[] };\n\n/** Pending input bytes cannot be represented by a snapshot. Keep their journal. */\nexport function makeDocImporter(doc: LoroDoc) {\n const unresolved = new Map<string, Array<{ start: number; end: number }>>();\n return (\n updates: readonly Uint8Array[],\n onImport?: (status: ReturnType<LoroDoc['import']>) => void\n ): boolean => {\n // Preserve sequential import and its observable hydration boundary. Track\n // all unresolved ranges: a later successful import can resolve an earlier one.\n for (const bytes of updates) {\n const status = doc.import(bytes);\n onImport?.(status);\n for (const [peer, span] of status.pending ?? []) {\n const key = String(peer);\n const spans = unresolved.get(key) ?? [];\n spans.push(span);\n unresolved.set(key, spans);\n }\n for (const [peer, applied] of status.success) {\n const key = String(peer);\n const remaining: Array<{ start: number; end: number }> = [];\n for (const span of unresolved.get(key) ?? []) {\n if (applied.end <= span.start || applied.start >= span.end) remaining.push(span);\n else {\n if (span.start < applied.start)\n remaining.push({ start: span.start, end: applied.start });\n if (applied.end < span.end) remaining.push({ start: applied.end, end: span.end });\n }\n }\n if (remaining.length) unresolved.set(key, remaining);\n else unresolved.delete(key);\n }\n }\n return unresolved.size === 0;\n };\n}\n\nexport function applyStoredDocUpdates(doc: LoroDoc, updates: readonly Uint8Array[]): boolean {\n const complete = makeDocImporter(doc)(updates);\n (doc as TrackedDoc)[completeness] = complete;\n (doc as TrackedDoc)[pending] = complete ? [] : updates.map((bytes) => bytes.slice());\n return complete;\n}\nexport const storedDocComplete = (doc: LoroDoc): boolean =>\n (doc as TrackedDoc)[completeness] !== false;\n\nexport const storedPendingUpdates = (doc: LoroDoc): readonly Uint8Array[] =>\n (doc as TrackedDoc)[pending] ?? [];\n\nexport type ReceivedDocInput =\n | { readonly kind: \"binary\"; readonly updates: readonly Uint8Array[] }\n | { readonly kind: \"json\"; readonly json: Parameters<LoroDoc[\"importJsonUpdates\"]>[0] };\n\n/**\n * The documented Loro envelope has magic `loro` and a big-endian mode at\n * bytes 20..22. Mode 3 snapshots contain applied history, never pending input.\n * This is only a copy optimization: import still validates the entire blob.\n * Unknown/legacy encodings retain the conservative copy path.\n * https://github.com/loro-dev/loro/blob/main/docs/encoding-xxhash32.md#document-envelope-checksum\n */\nfunction isCompleteSnapshot(bytes: Uint8Array): boolean {\n return bytes.length >= 22 && bytes[0] === 108 && bytes[1] === 111 &&\n bytes[2] === 114 && bytes[3] === 111 && bytes[20] === 0 && bytes[21] === 3;\n}\n\n/** Preserve exact successful inputs, including pending-only imports with no event. */\nexport function observeDocImports(\n doc: LoroDoc,\n onImport: (input: ReceivedDocInput | undefined) => void,\n): () => void {\n const restorers: Array<() => void> = [];\n const observe = <K extends \"import\" | \"importBatch\" | \"importUpdateBatch\" | \"importJsonUpdates\">(\n key: K,\n ) => {\n const original = doc[key];\n const own = Object.getOwnPropertyDescriptor(doc, key);\n const wrapped = function (this: LoroDoc, ...args: unknown[]) {\n let input: ReceivedDocInput | undefined;\n if (this === doc) {\n // Copy before native import can synchronously invoke user callbacks.\n // Capture failures must not change the original public import behavior.\n try {\n if (key === \"importJsonUpdates\") {\n input = {\n kind: \"json\",\n json:\n typeof args[0] === \"string\"\n ? args[0]\n : (structuredClone(args[0]) as Parameters<LoroDoc[\"importJsonUpdates\"]>[0]),\n };\n } else {\n const updates = (key === \"import\" ? [args[0]] : args[0]) as Uint8Array[];\n // A batch returns one aggregate status. Copy mixed batches in\n // full so a pending result never relies on classifying which\n // input contributed it. Closed snapshot-only imports avoid the\n // large copy; complete results below discard all copies.\n const copies = updates.every(isCompleteSnapshot)\n ? [] : updates.map((bytes) => Uint8Array.from(bytes));\n input = { kind: \"binary\", updates: copies };\n }\n } catch {\n /* The save falls back to the live pending checkpoint. */\n }\n }\n let succeeded = false;\n try {\n const result = Reflect.apply(original, this, args);\n succeeded = true;\n const status = result as ImportStatus;\n if (!status.pending?.size) input = { kind: \"binary\", updates: [] };\n // Never silently drop pending data if a future snapshot encoding\n // starts retaining it. Preserve the canonical checkpoint instead.\n else if (input?.kind === \"binary\" && input.updates.length === 0) input = undefined;\n return result;\n } finally {\n // A failed batch can have accepted a prefix. Never journal malformed\n // input; retain that accepted prefix through the normal checkpoint.\n if (this === doc) onImport(succeeded ? input : undefined);\n }\n };\n Object.defineProperty(doc, key, { value: wrapped, configurable: true, writable: true });\n restorers.push(() => {\n if (doc[key] !== wrapped) return;\n if (own) Object.defineProperty(doc, key, own);\n else Reflect.deleteProperty(doc, key);\n });\n };\n try {\n observe(\"import\");\n observe(\"importBatch\");\n observe(\"importUpdateBatch\");\n observe(\"importJsonUpdates\");\n } catch (error) {\n for (const restore of restorers) restore();\n throw error;\n }\n return () => {\n for (const restore of restorers) restore();\n };\n}\n\n/** Convert only this JSON input, not the live document's accumulated pending queue. */\nexport function receivedDocUpdates(input: ReceivedDocInput): readonly Uint8Array[] {\n if (input.kind === \"binary\") return input.updates;\n const copy = new LoroDoc();\n try {\n copy.importJsonUpdates(input.json);\n const updates: Uint8Array[] = [];\n if (copy.oplogFrontiers().length) updates.push(copy.export({ mode: \"update\" }));\n const pending = copy.exportPendingUpdates();\n if (pending !== undefined) updates.push(pending);\n return updates;\n } finally {\n copy.free();\n }\n}\n\nexport function samePendingUpdates(\n left: Uint8Array | undefined,\n right: Uint8Array | undefined,\n): boolean {\n return (\n left === right ||\n (left !== undefined &&\n right !== undefined &&\n left.length === right.length &&\n left.every((byte, index) => byte === right[index]))\n );\n}\n"],"mappings":";;;;;AAEA,MAAM,UAAU,OAAO,IAAI,wCAAwC;AACnE,MAAM,eAAe,OAAO,IAAI,4CAA4C;;AAI5E,SAAgB,gBAAgB,KAAc;CAC5C,MAAM,6BAAa,IAAI,KAAoD;AAC3E,SACE,SACA,aACY;AAGZ,OAAK,MAAM,SAAS,SAAS;GAC3B,MAAM,SAAS,IAAI,OAAO,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cAAa;AACX,OAAK,MAAM,WAAW,UAAW,UAAS;;;;AAK9C,SAAgB,mBAAmB,OAAgD;AACjF,KAAI,MAAM,SAAS,SAAU,QAAO,MAAM;CAC1C,MAAM,OAAO,IAAIC,mBAAS;AAC1B,KAAI;AACF,OAAK,kBAAkB,MAAM,KAAK;EAClC,MAAMC,UAAwB,EAAE;AAChC,MAAI,KAAK,gBAAgB,CAAC,OAAQ,SAAQ,KAAK,KAAK,OAAO,EAAE,MAAM,UAAU,CAAC,CAAC;EAC/E,MAAMC,YAAU,KAAK,sBAAsB;AAC3C,MAAIA,cAAY,OAAW,SAAQ,KAAKA,UAAQ;AAChD,SAAO;WACC;AACR,OAAK,MAAM;;;AAIf,SAAgB,mBACd,MACA,OACS;AACT,QACE,SAAS,SACR,SAAS,UACR,UAAU,UACV,KAAK,WAAW,MAAM,UACtB,KAAK,OAAO,MAAM,UAAU,SAAS,MAAM,OAAO"}
|
package/dist/doc-updates.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { LoroDoc } from "loro-crdt";
|
|
2
|
+
|
|
1
3
|
//#region src/storage/doc-updates.ts
|
|
2
4
|
const pending = Symbol.for("loro-repo.document-pending-updates.v1");
|
|
3
5
|
const completeness = Symbol.for("loro-repo.document-import-completeness.v1");
|
|
@@ -43,7 +45,94 @@ function applyStoredDocUpdates(doc, updates) {
|
|
|
43
45
|
}
|
|
44
46
|
const storedDocComplete = (doc) => doc[completeness] !== false;
|
|
45
47
|
const storedPendingUpdates = (doc) => doc[pending] ?? [];
|
|
48
|
+
/**
|
|
49
|
+
* The documented Loro envelope has magic `loro` and a big-endian mode at
|
|
50
|
+
* bytes 20..22. Mode 3 snapshots contain applied history, never pending input.
|
|
51
|
+
* This is only a copy optimization: import still validates the entire blob.
|
|
52
|
+
* Unknown/legacy encodings retain the conservative copy path.
|
|
53
|
+
* https://github.com/loro-dev/loro/blob/main/docs/encoding-xxhash32.md#document-envelope-checksum
|
|
54
|
+
*/
|
|
55
|
+
function isCompleteSnapshot(bytes) {
|
|
56
|
+
return bytes.length >= 22 && bytes[0] === 108 && bytes[1] === 111 && bytes[2] === 114 && bytes[3] === 111 && bytes[20] === 0 && bytes[21] === 3;
|
|
57
|
+
}
|
|
58
|
+
/** Preserve exact successful inputs, including pending-only imports with no event. */
|
|
59
|
+
function observeDocImports(doc, onImport) {
|
|
60
|
+
const restorers = [];
|
|
61
|
+
const observe = (key) => {
|
|
62
|
+
const original = doc[key];
|
|
63
|
+
const own = Object.getOwnPropertyDescriptor(doc, key);
|
|
64
|
+
const wrapped = function(...args) {
|
|
65
|
+
let input;
|
|
66
|
+
if (this === doc) try {
|
|
67
|
+
if (key === "importJsonUpdates") input = {
|
|
68
|
+
kind: "json",
|
|
69
|
+
json: typeof args[0] === "string" ? args[0] : structuredClone(args[0])
|
|
70
|
+
};
|
|
71
|
+
else {
|
|
72
|
+
const updates = key === "import" ? [args[0]] : args[0];
|
|
73
|
+
input = {
|
|
74
|
+
kind: "binary",
|
|
75
|
+
updates: updates.every(isCompleteSnapshot) ? [] : updates.map((bytes) => Uint8Array.from(bytes))
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
} catch {}
|
|
79
|
+
let succeeded = false;
|
|
80
|
+
try {
|
|
81
|
+
const result = Reflect.apply(original, this, args);
|
|
82
|
+
succeeded = true;
|
|
83
|
+
if (!result.pending?.size) input = {
|
|
84
|
+
kind: "binary",
|
|
85
|
+
updates: []
|
|
86
|
+
};
|
|
87
|
+
else if (input?.kind === "binary" && input.updates.length === 0) input = void 0;
|
|
88
|
+
return result;
|
|
89
|
+
} finally {
|
|
90
|
+
if (this === doc) onImport(succeeded ? input : void 0);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
Object.defineProperty(doc, key, {
|
|
94
|
+
value: wrapped,
|
|
95
|
+
configurable: true,
|
|
96
|
+
writable: true
|
|
97
|
+
});
|
|
98
|
+
restorers.push(() => {
|
|
99
|
+
if (doc[key] !== wrapped) return;
|
|
100
|
+
if (own) Object.defineProperty(doc, key, own);
|
|
101
|
+
else Reflect.deleteProperty(doc, key);
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
try {
|
|
105
|
+
observe("import");
|
|
106
|
+
observe("importBatch");
|
|
107
|
+
observe("importUpdateBatch");
|
|
108
|
+
observe("importJsonUpdates");
|
|
109
|
+
} catch (error) {
|
|
110
|
+
for (const restore of restorers) restore();
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
return () => {
|
|
114
|
+
for (const restore of restorers) restore();
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** Convert only this JSON input, not the live document's accumulated pending queue. */
|
|
118
|
+
function receivedDocUpdates(input) {
|
|
119
|
+
if (input.kind === "binary") return input.updates;
|
|
120
|
+
const copy = new LoroDoc();
|
|
121
|
+
try {
|
|
122
|
+
copy.importJsonUpdates(input.json);
|
|
123
|
+
const updates = [];
|
|
124
|
+
if (copy.oplogFrontiers().length) updates.push(copy.export({ mode: "update" }));
|
|
125
|
+
const pending$1 = copy.exportPendingUpdates();
|
|
126
|
+
if (pending$1 !== void 0) updates.push(pending$1);
|
|
127
|
+
return updates;
|
|
128
|
+
} finally {
|
|
129
|
+
copy.free();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function samePendingUpdates(left, right) {
|
|
133
|
+
return left === right || left !== void 0 && right !== void 0 && left.length === right.length && left.every((byte, index) => byte === right[index]);
|
|
134
|
+
}
|
|
46
135
|
|
|
47
136
|
//#endregion
|
|
48
|
-
export {
|
|
137
|
+
export { samePendingUpdates as a, receivedDocUpdates as i, makeDocImporter as n, storedDocComplete as o, observeDocImports as r, storedPendingUpdates as s, applyStoredDocUpdates as t };
|
|
49
138
|
//# sourceMappingURL=doc-updates.js.map
|
package/dist/doc-updates.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"doc-updates.js","names":["remaining: Array<{ start: number; end: number }>"],"sources":["../src/storage/doc-updates.ts"],"sourcesContent":["import type { LoroDoc } from 'loro-crdt';\n\nconst pending = Symbol.for('loro-repo.document-pending-updates.v1');\nconst completeness = Symbol.for('loro-repo.document-import-completeness.v1');\ntype TrackedDoc = LoroDoc & { [completeness]?: boolean; [pending]?: readonly Uint8Array[] };\n\n/** Pending input bytes cannot be represented by a snapshot. Keep their journal. */\nexport function makeDocImporter(doc: LoroDoc) {\n const unresolved = new Map<string, Array<{ start: number; end: number }>>();\n return (\n updates: readonly Uint8Array[],\n onImport?: (status: ReturnType<LoroDoc['import']>) => void\n ): boolean => {\n // Preserve sequential import and its observable hydration boundary. Track\n // all unresolved ranges: a later successful import can resolve an earlier one.\n for (const bytes of updates) {\n const status = doc.import(bytes);\n onImport?.(status);\n for (const [peer, span] of status.pending ?? []) {\n const key = String(peer);\n const spans = unresolved.get(key) ?? [];\n spans.push(span);\n unresolved.set(key, spans);\n }\n for (const [peer, applied] of status.success) {\n const key = String(peer);\n const remaining: Array<{ start: number; end: number }> = [];\n for (const span of unresolved.get(key) ?? []) {\n if (applied.end <= span.start || applied.start >= span.end) remaining.push(span);\n else {\n if (span.start < applied.start)\n remaining.push({ start: span.start, end: applied.start });\n if (applied.end < span.end) remaining.push({ start: applied.end, end: span.end });\n }\n }\n if (remaining.length) unresolved.set(key, remaining);\n else unresolved.delete(key);\n }\n }\n return unresolved.size === 0;\n };\n}\n\nexport function applyStoredDocUpdates(doc: LoroDoc, updates: readonly Uint8Array[]): boolean {\n const complete = makeDocImporter(doc)(updates);\n (doc as TrackedDoc)[completeness] = complete;\n (doc as TrackedDoc)[pending] = complete ? [] : updates.map((bytes) => bytes.slice());\n return complete;\n}\nexport const storedDocComplete = (doc: LoroDoc): boolean =>\n (doc as TrackedDoc)[completeness] !== false;\n\nexport const storedPendingUpdates = (doc: LoroDoc): readonly Uint8Array[] =>\n (doc as TrackedDoc)[pending] ?? [];\n"],"mappings":";AAEA,MAAM,UAAU,OAAO,IAAI,wCAAwC;AACnE,MAAM,eAAe,OAAO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AE"}
|
|
1
|
+
{"version":3,"file":"doc-updates.js","names":["remaining: Array<{ start: number; end: number }>","restorers: Array<() => void>","input: ReceivedDocInput | undefined","updates: Uint8Array[]","pending"],"sources":["../src/storage/doc-updates.ts"],"sourcesContent":["import { LoroDoc, type ImportStatus } from 'loro-crdt';\n\nconst pending = Symbol.for('loro-repo.document-pending-updates.v1');\nconst completeness = Symbol.for('loro-repo.document-import-completeness.v1');\ntype TrackedDoc = LoroDoc & { [completeness]?: boolean; [pending]?: readonly Uint8Array[] };\n\n/** Pending input bytes cannot be represented by a snapshot. Keep their journal. */\nexport function makeDocImporter(doc: LoroDoc) {\n const unresolved = new Map<string, Array<{ start: number; end: number }>>();\n return (\n updates: readonly Uint8Array[],\n onImport?: (status: ReturnType<LoroDoc['import']>) => void\n ): boolean => {\n // Preserve sequential import and its observable hydration boundary. Track\n // all unresolved ranges: a later successful import can resolve an earlier one.\n for (const bytes of updates) {\n const status = doc.import(bytes);\n onImport?.(status);\n for (const [peer, span] of status.pending ?? []) {\n const key = String(peer);\n const spans = unresolved.get(key) ?? [];\n spans.push(span);\n unresolved.set(key, spans);\n }\n for (const [peer, applied] of status.success) {\n const key = String(peer);\n const remaining: Array<{ start: number; end: number }> = [];\n for (const span of unresolved.get(key) ?? []) {\n if (applied.end <= span.start || applied.start >= span.end) remaining.push(span);\n else {\n if (span.start < applied.start)\n remaining.push({ start: span.start, end: applied.start });\n if (applied.end < span.end) remaining.push({ start: applied.end, end: span.end });\n }\n }\n if (remaining.length) unresolved.set(key, remaining);\n else unresolved.delete(key);\n }\n }\n return unresolved.size === 0;\n };\n}\n\nexport function applyStoredDocUpdates(doc: LoroDoc, updates: readonly Uint8Array[]): boolean {\n const complete = makeDocImporter(doc)(updates);\n (doc as TrackedDoc)[completeness] = complete;\n (doc as TrackedDoc)[pending] = complete ? [] : updates.map((bytes) => bytes.slice());\n return complete;\n}\nexport const storedDocComplete = (doc: LoroDoc): boolean =>\n (doc as TrackedDoc)[completeness] !== false;\n\nexport const storedPendingUpdates = (doc: LoroDoc): readonly Uint8Array[] =>\n (doc as TrackedDoc)[pending] ?? [];\n\nexport type ReceivedDocInput =\n | { readonly kind: \"binary\"; readonly updates: readonly Uint8Array[] }\n | { readonly kind: \"json\"; readonly json: Parameters<LoroDoc[\"importJsonUpdates\"]>[0] };\n\n/**\n * The documented Loro envelope has magic `loro` and a big-endian mode at\n * bytes 20..22. Mode 3 snapshots contain applied history, never pending input.\n * This is only a copy optimization: import still validates the entire blob.\n * Unknown/legacy encodings retain the conservative copy path.\n * https://github.com/loro-dev/loro/blob/main/docs/encoding-xxhash32.md#document-envelope-checksum\n */\nfunction isCompleteSnapshot(bytes: Uint8Array): boolean {\n return bytes.length >= 22 && bytes[0] === 108 && bytes[1] === 111 &&\n bytes[2] === 114 && bytes[3] === 111 && bytes[20] === 0 && bytes[21] === 3;\n}\n\n/** Preserve exact successful inputs, including pending-only imports with no event. */\nexport function observeDocImports(\n doc: LoroDoc,\n onImport: (input: ReceivedDocInput | undefined) => void,\n): () => void {\n const restorers: Array<() => void> = [];\n const observe = <K extends \"import\" | \"importBatch\" | \"importUpdateBatch\" | \"importJsonUpdates\">(\n key: K,\n ) => {\n const original = doc[key];\n const own = Object.getOwnPropertyDescriptor(doc, key);\n const wrapped = function (this: LoroDoc, ...args: unknown[]) {\n let input: ReceivedDocInput | undefined;\n if (this === doc) {\n // Copy before native import can synchronously invoke user callbacks.\n // Capture failures must not change the original public import behavior.\n try {\n if (key === \"importJsonUpdates\") {\n input = {\n kind: \"json\",\n json:\n typeof args[0] === \"string\"\n ? args[0]\n : (structuredClone(args[0]) as Parameters<LoroDoc[\"importJsonUpdates\"]>[0]),\n };\n } else {\n const updates = (key === \"import\" ? [args[0]] : args[0]) as Uint8Array[];\n // A batch returns one aggregate status. Copy mixed batches in\n // full so a pending result never relies on classifying which\n // input contributed it. Closed snapshot-only imports avoid the\n // large copy; complete results below discard all copies.\n const copies = updates.every(isCompleteSnapshot)\n ? [] : updates.map((bytes) => Uint8Array.from(bytes));\n input = { kind: \"binary\", updates: copies };\n }\n } catch {\n /* The save falls back to the live pending checkpoint. */\n }\n }\n let succeeded = false;\n try {\n const result = Reflect.apply(original, this, args);\n succeeded = true;\n const status = result as ImportStatus;\n if (!status.pending?.size) input = { kind: \"binary\", updates: [] };\n // Never silently drop pending data if a future snapshot encoding\n // starts retaining it. Preserve the canonical checkpoint instead.\n else if (input?.kind === \"binary\" && input.updates.length === 0) input = undefined;\n return result;\n } finally {\n // A failed batch can have accepted a prefix. Never journal malformed\n // input; retain that accepted prefix through the normal checkpoint.\n if (this === doc) onImport(succeeded ? input : undefined);\n }\n };\n Object.defineProperty(doc, key, { value: wrapped, configurable: true, writable: true });\n restorers.push(() => {\n if (doc[key] !== wrapped) return;\n if (own) Object.defineProperty(doc, key, own);\n else Reflect.deleteProperty(doc, key);\n });\n };\n try {\n observe(\"import\");\n observe(\"importBatch\");\n observe(\"importUpdateBatch\");\n observe(\"importJsonUpdates\");\n } catch (error) {\n for (const restore of restorers) restore();\n throw error;\n }\n return () => {\n for (const restore of restorers) restore();\n };\n}\n\n/** Convert only this JSON input, not the live document's accumulated pending queue. */\nexport function receivedDocUpdates(input: ReceivedDocInput): readonly Uint8Array[] {\n if (input.kind === \"binary\") return input.updates;\n const copy = new LoroDoc();\n try {\n copy.importJsonUpdates(input.json);\n const updates: Uint8Array[] = [];\n if (copy.oplogFrontiers().length) updates.push(copy.export({ mode: \"update\" }));\n const pending = copy.exportPendingUpdates();\n if (pending !== undefined) updates.push(pending);\n return updates;\n } finally {\n copy.free();\n }\n}\n\nexport function samePendingUpdates(\n left: Uint8Array | undefined,\n right: Uint8Array | undefined,\n): boolean {\n return (\n left === right ||\n (left !== undefined &&\n right !== undefined &&\n left.length === right.length &&\n left.every((byte, index) => byte === right[index]))\n );\n}\n"],"mappings":";;;AAEA,MAAM,UAAU,OAAO,IAAI,wCAAwC;AACnE,MAAM,eAAe,OAAO,IAAI,4CAA4C;;AAI5E,SAAgB,g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;AACjB,UAAQ,cAAc;AACtB,UAAQ,oBAAoB;AAC5B,UAAQ,oBAAoB;UACrB,OAAO;AACd,OAAK,MAAM,WAAW,UAAW,UAAS;AAC1C,QAAM;;AAER,cAAa;AACX,OAAK,MAAM,WAAW,UAAW,UAAS;;;;AAK9C,SAAgB,mBAAmB,OAAgD;AACjF,KAAI,MAAM,SAAS,SAAU,QAAO,MAAM;CAC1C,MAAM,OAAO,IAAI,SAAS;AAC1B,KAAI;AACF,OAAK,kBAAkB,MAAM,KAAK;EAClC,MAAMC,UAAwB,EAAE;AAChC,MAAI,KAAK,gBAAgB,CAAC,OAAQ,SAAQ,KAAK,KAAK,OAAO,EAAE,MAAM,UAAU,CAAC,CAAC;EAC/E,MAAMC,YAAU,KAAK,sBAAsB;AAC3C,MAAIA,cAAY,OAAW,SAAQ,KAAKA,UAAQ;AAChD,SAAO;WACC;AACR,OAAK,MAAM;;;AAIf,SAAgB,mBACd,MACA,OACS;AACT,QACE,SAAS,SACR,SAAS,UACR,UAAU,UACV,KAAK,WAAW,MAAM,UACtB,KAAK,OAAO,MAAM,UAAU,SAAS,MAAM,OAAO"}
|
package/dist/effect.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const require_chunk = require('./chunk.cjs');
|
|
2
2
|
const require_meta_snapshot = require('./meta-snapshot.cjs');
|
|
3
|
-
const require_room_state = require('./room-state.cjs');
|
|
4
3
|
const require_doc_updates = require('./doc-updates.cjs');
|
|
4
|
+
const require_room_state = require('./room-state.cjs');
|
|
5
5
|
let __loro_dev_flock_wasm = require("@loro-dev/flock-wasm");
|
|
6
6
|
__loro_dev_flock_wasm = require_chunk.__toESM(__loro_dev_flock_wasm);
|
|
7
7
|
let loro_crdt = require("loro-crdt");
|
package/dist/effect.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { r as isFlockFile, t as encodeMetaJsonUpdate, u as RepoWriterLocked } from "./meta-snapshot.js";
|
|
2
|
+
import { n as makeDocImporter, o as storedDocComplete, s as storedPendingUpdates } from "./doc-updates.js";
|
|
2
3
|
import { a as RepoIOError, c as RepoResnapshotRequired, d as RepoUnsupported, f as scopedUse, i as RepoDocAlreadyExists, l as RepoResourceBusy, n as makeRoomState, o as RepoMigrationIncomplete, r as RepoClosed, s as RepoReleaseFailed, t as initialRoomState, u as RepoSyncFailed } from "./room-state.js";
|
|
3
|
-
import { i as storedPendingUpdates, n as makeDocImporter, r as storedDocComplete } from "./doc-updates.js";
|
|
4
4
|
import { Flock, decodeVersionVector, encodeVersionVector } from "@loro-dev/flock-wasm";
|
|
5
5
|
import { EphemeralStore, LoroDoc, VersionVector, decodeImportBlobMeta } from "loro-crdt";
|
|
6
6
|
import { Cause, Clock, Deferred, Effect, Exit, Layer, PubSub, Queue, RcMap, Schema, Scope, Semaphore, Stream, SubscriptionRef } from "effect";
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const require_chunk = require('./chunk.cjs');
|
|
2
2
|
const require_replica_checkpoints = require('./replica-checkpoints.cjs');
|
|
3
3
|
const require_meta_snapshot = require('./meta-snapshot.cjs');
|
|
4
|
+
const require_doc_updates = require('./doc-updates.cjs');
|
|
4
5
|
const require_logging = require('./logging.cjs');
|
|
5
6
|
const require_flock_snapshot = require('./flock-snapshot.cjs');
|
|
6
7
|
const require_flock_persistence_journal = require('./flock-persistence-journal.cjs');
|
|
@@ -234,6 +235,7 @@ const YIELD_BUDGET_MS$1 = 5;
|
|
|
234
235
|
* loop surfaces as an error rather than hanging.
|
|
235
236
|
*/
|
|
236
237
|
const ACQUIRE_MAX_ATTEMPTS$1 = 8;
|
|
238
|
+
const CAPTURE_MAX_ATTEMPTS = 8;
|
|
237
239
|
const nowMs$1 = typeof globalThis.performance?.now === "function" ? globalThis.performance.now.bind(globalThis.performance) : Date.now;
|
|
238
240
|
var DocManager = class {
|
|
239
241
|
/**
|
|
@@ -249,6 +251,9 @@ var DocManager = class {
|
|
|
249
251
|
docs = /* @__PURE__ */ new Map();
|
|
250
252
|
docSubscriptions = /* @__PURE__ */ new Map();
|
|
251
253
|
docPersistedVersions = /* @__PURE__ */ new Map();
|
|
254
|
+
docPersistedPending = /* @__PURE__ */ new Map();
|
|
255
|
+
docRevisions = /* @__PURE__ */ new Map();
|
|
256
|
+
docReceivedInputs = /* @__PURE__ */ new Map();
|
|
252
257
|
pendingDocLoads = /* @__PURE__ */ new Map();
|
|
253
258
|
dirtyDocs = /* @__PURE__ */ new Set();
|
|
254
259
|
existenceMaterialized = /* @__PURE__ */ new Set();
|
|
@@ -313,30 +318,17 @@ var DocManager = class {
|
|
|
313
318
|
}
|
|
314
319
|
}
|
|
315
320
|
async persistDoc(docId, doc) {
|
|
316
|
-
|
|
321
|
+
doc.commit();
|
|
322
|
+
const version = doc.oplogVersion();
|
|
323
|
+
const revision = this.docRevisions.get(docId);
|
|
324
|
+
if (!this.storage) return this.saveCaptured(docId, doc, version, void 0, revision);
|
|
325
|
+
const pending = doc.exportPendingUpdates();
|
|
317
326
|
const snapshot = doc.export({ mode: "snapshot" });
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
this.clearDirtyIfStable(docId, oplogAtCapture, doc);
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
326
|
-
this.docPersistedVersions.set(docId, nextVersion);
|
|
327
|
-
try {
|
|
328
|
-
await this.storage.save({
|
|
329
|
-
type: "doc-snapshot",
|
|
330
|
-
docId,
|
|
331
|
-
snapshot
|
|
332
|
-
});
|
|
333
|
-
await this.materializeExistence(docId, doc);
|
|
334
|
-
this.clearDirtyIfStable(docId, oplogAtCapture, doc);
|
|
335
|
-
} catch (error) {
|
|
336
|
-
if (previousVersion) this.docPersistedVersions.set(docId, previousVersion);
|
|
337
|
-
else this.docPersistedVersions.delete(docId);
|
|
338
|
-
throw error;
|
|
339
|
-
}
|
|
327
|
+
await this.saveCaptured(docId, doc, version, pending, revision, {
|
|
328
|
+
type: "doc-snapshot",
|
|
329
|
+
docId,
|
|
330
|
+
snapshot
|
|
331
|
+
});
|
|
340
332
|
}
|
|
341
333
|
/**
|
|
342
334
|
* Durably persists everything `doc` has accumulated since its last write,
|
|
@@ -375,7 +367,12 @@ var DocManager = class {
|
|
|
375
367
|
const doc = this.docs.get(docId);
|
|
376
368
|
if (!doc) return;
|
|
377
369
|
this.assertEvictable(docId, options.force);
|
|
378
|
-
|
|
370
|
+
for (let attempt = 0; attempt < CAPTURE_MAX_ATTEMPTS; attempt++) {
|
|
371
|
+
await this.persistDocUpdate(docId, doc);
|
|
372
|
+
this.assertEvictable(docId, options.force);
|
|
373
|
+
if (!this.dirtyDocs.has(docId)) break;
|
|
374
|
+
}
|
|
375
|
+
if (this.dirtyDocs.has(docId)) throw new ResourceBusyError("document", docId, ["writes during persistence"], `Cannot unload document "${docId}" after ${CAPTURE_MAX_ATTEMPTS} attempts: it kept changing during persistence`);
|
|
379
376
|
this.assertEvictable(docId, options.force);
|
|
380
377
|
this.evict(docId);
|
|
381
378
|
this.ownership.invalidate(docId);
|
|
@@ -408,6 +405,9 @@ var DocManager = class {
|
|
|
408
405
|
this.docSubscriptions.delete(docId);
|
|
409
406
|
this.docs.delete(docId);
|
|
410
407
|
this.docPersistedVersions.delete(docId);
|
|
408
|
+
this.docPersistedPending.delete(docId);
|
|
409
|
+
this.docRevisions.delete(docId);
|
|
410
|
+
this.docReceivedInputs.delete(docId);
|
|
411
411
|
this.existenceMaterialized.delete(docId);
|
|
412
412
|
}
|
|
413
413
|
async flush() {
|
|
@@ -431,6 +431,9 @@ var DocManager = class {
|
|
|
431
431
|
this.docSubscriptions.clear();
|
|
432
432
|
this.docs.clear();
|
|
433
433
|
this.docPersistedVersions.clear();
|
|
434
|
+
this.docPersistedPending.clear();
|
|
435
|
+
this.docRevisions.clear();
|
|
436
|
+
this.docReceivedInputs.clear();
|
|
434
437
|
this.pendingDocLoads.clear();
|
|
435
438
|
this.dirtyDocs.clear();
|
|
436
439
|
this.existenceMaterialized.clear();
|
|
@@ -456,75 +459,121 @@ var DocManager = class {
|
|
|
456
459
|
return created;
|
|
457
460
|
}
|
|
458
461
|
registerDoc(docId, doc) {
|
|
462
|
+
const pending = this.storage ? doc.exportPendingUpdates() : void 0;
|
|
459
463
|
this.docs.set(docId, doc);
|
|
460
464
|
this.docPersistedVersions.set(docId, doc.version());
|
|
461
|
-
this.
|
|
465
|
+
this.docPersistedPending.set(docId, pending);
|
|
466
|
+
this.docRevisions.set(docId, 0);
|
|
467
|
+
try {
|
|
468
|
+
this.ensureDocSubscription(docId, doc);
|
|
469
|
+
} catch (error) {
|
|
470
|
+
this.evict(docId);
|
|
471
|
+
throw error;
|
|
472
|
+
}
|
|
462
473
|
}
|
|
463
474
|
ensureDocSubscription(docId, doc) {
|
|
464
475
|
if (this.docSubscriptions.has(docId)) return;
|
|
465
|
-
const
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
this.
|
|
476
|
+
const stopObservingImports = this.storage ? require_doc_updates.observeDocImports(doc, (input) => {
|
|
477
|
+
if (this.docs.get(docId) !== doc) return;
|
|
478
|
+
this.markDocDirty(docId, doc);
|
|
479
|
+
const received = this.docReceivedInputs.get(docId) ?? [];
|
|
480
|
+
received.push({
|
|
481
|
+
revision: this.docRevisions.get(docId),
|
|
482
|
+
input
|
|
483
|
+
});
|
|
484
|
+
this.docReceivedInputs.set(docId, received);
|
|
485
|
+
}) : () => {};
|
|
486
|
+
let unsubscribe;
|
|
487
|
+
try {
|
|
488
|
+
unsubscribe = doc.subscribe((batch) => {
|
|
489
|
+
const stackBy = this.eventBus.resolveEventBy("local");
|
|
490
|
+
const by = stackBy === "local" && batch.by === "import" ? "live" : stackBy;
|
|
491
|
+
this.onDocEvent(docId, doc, batch, by);
|
|
492
|
+
});
|
|
493
|
+
} catch (error) {
|
|
494
|
+
stopObservingImports();
|
|
495
|
+
throw error;
|
|
496
|
+
}
|
|
497
|
+
this.docSubscriptions.set(docId, () => {
|
|
498
|
+
if (typeof unsubscribe === "function") unsubscribe();
|
|
499
|
+
stopObservingImports();
|
|
469
500
|
});
|
|
470
|
-
if (typeof unsubscribe === "function") this.docSubscriptions.set(docId, unsubscribe);
|
|
471
501
|
}
|
|
472
502
|
async materializeDetachedDoc(docId) {
|
|
473
|
-
const
|
|
474
|
-
if (
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
503
|
+
const source = this.docs.get(docId) ?? await this.storage?.loadDoc(docId);
|
|
504
|
+
if (!source) return new loro_crdt.LoroDoc();
|
|
505
|
+
for (let attempt = 0; attempt < CAPTURE_MAX_ATTEMPTS; attempt++) {
|
|
506
|
+
source.commit();
|
|
507
|
+
if (source.getPendingTxnLength() > 0) continue;
|
|
508
|
+
const version = source.oplogVersion();
|
|
509
|
+
const pending = source.exportPendingUpdates();
|
|
510
|
+
const snapshot = source.export({ mode: "snapshot" });
|
|
511
|
+
if (source.getPendingTxnLength() > 0 || version.compare(source.oplogVersion()) !== 0 || !require_doc_updates.samePendingUpdates(pending, source.exportPendingUpdates())) continue;
|
|
512
|
+
const detached = loro_crdt.LoroDoc.fromSnapshot(snapshot);
|
|
513
|
+
if (pending !== void 0) detached.import(pending);
|
|
514
|
+
return detached;
|
|
515
|
+
}
|
|
516
|
+
throw new Error(`Could not capture detached document "${docId}" after ${CAPTURE_MAX_ATTEMPTS} attempts: it kept changing during export`);
|
|
482
517
|
}
|
|
483
518
|
async persistDocUpdate(docId, doc) {
|
|
484
519
|
const previousVersion = this.docPersistedVersions.get(docId);
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
this.
|
|
507
|
-
|
|
508
|
-
|
|
520
|
+
if (!previousVersion) return this.persistDoc(docId, doc);
|
|
521
|
+
doc.commit();
|
|
522
|
+
const version = doc.oplogVersion();
|
|
523
|
+
const revision = this.docRevisions.get(docId);
|
|
524
|
+
if (!this.storage) return this.saveCaptured(docId, doc, version, void 0, revision);
|
|
525
|
+
const pending = doc.exportPendingUpdates();
|
|
526
|
+
let applied;
|
|
527
|
+
if (previousVersion.compare(version) !== 0 || doc.getPendingTxnLength() > 0) applied = {
|
|
528
|
+
type: "doc-update",
|
|
529
|
+
docId,
|
|
530
|
+
update: doc.export({
|
|
531
|
+
mode: "update",
|
|
532
|
+
from: previousVersion
|
|
533
|
+
})
|
|
534
|
+
};
|
|
535
|
+
await this.saveCaptured(docId, doc, version, pending, revision, applied);
|
|
536
|
+
}
|
|
537
|
+
async saveCaptured(docId, doc, version, pending, revision, applied) {
|
|
538
|
+
const payloads = [];
|
|
539
|
+
const received = this.docReceivedInputs.get(docId) ?? [];
|
|
540
|
+
const covered = received.filter((item) => item.revision <= (revision ?? 0));
|
|
541
|
+
if (pending !== void 0 && !require_doc_updates.samePendingUpdates(pending, this.docPersistedPending.get(docId))) {
|
|
542
|
+
let updates = [pending];
|
|
543
|
+
if (covered.length && covered.every((item) => item.input !== void 0)) try {
|
|
544
|
+
updates = covered.flatMap((item) => require_doc_updates.receivedDocUpdates(item.input));
|
|
545
|
+
} catch {}
|
|
546
|
+
for (const update of updates) payloads.push({
|
|
509
547
|
type: "doc-update",
|
|
510
548
|
docId,
|
|
511
549
|
update
|
|
512
550
|
});
|
|
513
|
-
|
|
514
|
-
|
|
551
|
+
}
|
|
552
|
+
if (applied) payloads.push(applied);
|
|
553
|
+
try {
|
|
554
|
+
if (this.storage && payloads.length) if (payloads.length > 1 && this.storage.saveMany) await this.storage.saveMany(payloads);
|
|
555
|
+
else for (const payload of payloads) await this.storage.save(payload);
|
|
556
|
+
if (payloads.length || !this.storage) await this.materializeExistence(docId, doc);
|
|
557
|
+
this.docPersistedVersions.set(docId, version);
|
|
558
|
+
this.docPersistedPending.set(docId, pending);
|
|
559
|
+
received.splice(0, covered.length);
|
|
560
|
+
if (received.length === 0 && this.docReceivedInputs.get(docId) === received) this.docReceivedInputs.delete(docId);
|
|
561
|
+
if (this.docRevisions.get(docId) === revision && doc.oplogVersion().compare(version) === 0 && doc.getPendingTxnLength() === 0) this.dirtyDocs.delete(docId);
|
|
562
|
+
else this.dirtyDocs.add(docId);
|
|
515
563
|
} catch (error) {
|
|
516
|
-
this.
|
|
564
|
+
this.dirtyDocs.add(docId);
|
|
517
565
|
throw error;
|
|
518
566
|
}
|
|
519
567
|
}
|
|
520
|
-
|
|
521
|
-
if (
|
|
522
|
-
|
|
523
|
-
onDocEvent(docId, doc, _batch, _by) {
|
|
524
|
-
if (!this.docs.has(docId)) return;
|
|
568
|
+
markDocDirty(docId, doc) {
|
|
569
|
+
if (this.docs.get(docId) !== doc) return;
|
|
570
|
+
this.docRevisions.set(docId, (this.docRevisions.get(docId) ?? 0) + 1);
|
|
525
571
|
this.dirtyDocs.add(docId);
|
|
526
572
|
this.scheduleDirtyFlush();
|
|
527
573
|
}
|
|
574
|
+
onDocEvent(docId, doc, _batch, _by) {
|
|
575
|
+
this.markDocDirty(docId, doc);
|
|
576
|
+
}
|
|
528
577
|
scheduleDirtyFlush() {
|
|
529
578
|
if (this.destroyed) return;
|
|
530
579
|
if (this.persistDebounceMs > 0) {
|