loro-repo 0.5.0 → 0.5.2

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/dist/index.js CHANGED
@@ -1,1088 +1,6 @@
1
1
  import { Flock } from "@loro-dev/flock";
2
2
  import { LoroDoc } from "loro-crdt";
3
- import { promises } from "node:fs";
4
- import * as path from "node:path";
5
- import { randomUUID } from "node:crypto";
6
- import { LoroAdaptor } from "loro-adaptors/loro";
7
- import { bytesToHex } from "loro-protocol";
8
- import { LoroWebsocketClient } from "loro-websocket";
9
- import { FlockAdaptor } from "loro-adaptors/flock";
10
3
 
11
- //#region src/transport/broadcast-channel.ts
12
- function deferred() {
13
- let resolve;
14
- return {
15
- promise: new Promise((res) => {
16
- resolve = res;
17
- }),
18
- resolve
19
- };
20
- }
21
- function randomInstanceId() {
22
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") return crypto.randomUUID();
23
- return Math.random().toString(36).slice(2);
24
- }
25
- function ensureBroadcastChannel() {
26
- if (typeof BroadcastChannel === "undefined") throw new Error("BroadcastChannel API is not available in this environment");
27
- return BroadcastChannel;
28
- }
29
- function encodeDocChannelId(docId) {
30
- try {
31
- return encodeURIComponent(docId);
32
- } catch {
33
- return docId.replace(/[^a-z0-9_-]/gi, "_");
34
- }
35
- }
36
- function postChannelMessage(channel, message) {
37
- channel.postMessage(message);
38
- }
39
- /**
40
- * TransportAdapter that relies on the BroadcastChannel API to fan out metadata
41
- * and document updates between browser tabs within the same origin.
42
- */
43
- var BroadcastChannelTransportAdapter = class {
44
- instanceId = randomInstanceId();
45
- namespace;
46
- metaChannelName;
47
- connected = false;
48
- metaState;
49
- docStates = /* @__PURE__ */ new Map();
50
- constructor(options = {}) {
51
- ensureBroadcastChannel();
52
- this.namespace = options.namespace ?? "loro-repo";
53
- this.metaChannelName = options.metaChannelName ?? `${this.namespace}-meta`;
54
- }
55
- async connect() {
56
- this.connected = true;
57
- }
58
- async close() {
59
- this.connected = false;
60
- if (this.metaState) {
61
- for (const entry of this.metaState.listeners) entry.unsubscribe();
62
- this.metaState.channel.close();
63
- this.metaState = void 0;
64
- }
65
- for (const [docId] of this.docStates) this.teardownDocChannel(docId);
66
- this.docStates.clear();
67
- }
68
- isConnected() {
69
- return this.connected;
70
- }
71
- async syncMeta(flock, _options) {
72
- const subscription = this.joinMetaRoom(flock);
73
- subscription.firstSyncedWithRemote.catch(() => void 0);
74
- await subscription.firstSyncedWithRemote;
75
- subscription.unsubscribe();
76
- return { ok: true };
77
- }
78
- joinMetaRoom(flock, _params) {
79
- const state = this.ensureMetaChannel();
80
- const { promise, resolve } = deferred();
81
- const listener = {
82
- flock,
83
- muted: false,
84
- unsubscribe: flock.subscribe(() => {
85
- if (listener.muted) return;
86
- Promise.resolve(flock.exportJson()).then((bundle) => {
87
- postChannelMessage(state.channel, {
88
- kind: "meta-export",
89
- from: this.instanceId,
90
- bundle
91
- });
92
- });
93
- }),
94
- resolveFirst: resolve,
95
- firstSynced: promise
96
- };
97
- state.listeners.add(listener);
98
- postChannelMessage(state.channel, {
99
- kind: "meta-request",
100
- from: this.instanceId
101
- });
102
- Promise.resolve(flock.exportJson()).then((bundle) => {
103
- postChannelMessage(state.channel, {
104
- kind: "meta-export",
105
- from: this.instanceId,
106
- bundle
107
- });
108
- });
109
- queueMicrotask(() => resolve());
110
- return {
111
- unsubscribe: () => {
112
- listener.unsubscribe();
113
- state.listeners.delete(listener);
114
- if (!state.listeners.size) {
115
- state.channel.removeEventListener("message", state.onMessage);
116
- state.channel.close();
117
- this.metaState = void 0;
118
- }
119
- },
120
- firstSyncedWithRemote: listener.firstSynced,
121
- get connected() {
122
- return true;
123
- }
124
- };
125
- }
126
- async syncDoc(docId, doc, _options) {
127
- const subscription = this.joinDocRoom(docId, doc);
128
- subscription.firstSyncedWithRemote.catch(() => void 0);
129
- await subscription.firstSyncedWithRemote;
130
- subscription.unsubscribe();
131
- return { ok: true };
132
- }
133
- joinDocRoom(docId, doc, _params) {
134
- const state = this.ensureDocChannel(docId);
135
- const { promise, resolve } = deferred();
136
- const listener = {
137
- doc,
138
- muted: false,
139
- unsubscribe: doc.subscribe(() => {
140
- if (listener.muted) return;
141
- const payload = doc.export({ mode: "update" });
142
- postChannelMessage(state.channel, {
143
- kind: "doc-update",
144
- docId,
145
- from: this.instanceId,
146
- mode: "update",
147
- payload
148
- });
149
- }),
150
- resolveFirst: resolve,
151
- firstSynced: promise
152
- };
153
- state.listeners.add(listener);
154
- postChannelMessage(state.channel, {
155
- kind: "doc-request",
156
- docId,
157
- from: this.instanceId
158
- });
159
- postChannelMessage(state.channel, {
160
- kind: "doc-update",
161
- docId,
162
- from: this.instanceId,
163
- mode: "snapshot",
164
- payload: doc.export({ mode: "snapshot" })
165
- });
166
- queueMicrotask(() => resolve());
167
- return {
168
- unsubscribe: () => {
169
- listener.unsubscribe();
170
- state.listeners.delete(listener);
171
- if (!state.listeners.size) this.teardownDocChannel(docId);
172
- },
173
- firstSyncedWithRemote: listener.firstSynced,
174
- get connected() {
175
- return true;
176
- }
177
- };
178
- }
179
- ensureMetaChannel() {
180
- if (this.metaState) return this.metaState;
181
- const channel = new (ensureBroadcastChannel())(this.metaChannelName);
182
- const listeners = /* @__PURE__ */ new Set();
183
- const onMessage = (event) => {
184
- const message = event.data;
185
- if (!message || message.from === this.instanceId) return;
186
- if (message.kind === "meta-export") for (const entry of listeners) {
187
- entry.muted = true;
188
- entry.flock.importJson(message.bundle);
189
- entry.muted = false;
190
- entry.resolveFirst();
191
- }
192
- else if (message.kind === "meta-request") {
193
- const first = listeners.values().next().value;
194
- if (!first) return;
195
- Promise.resolve(first.flock.exportJson()).then((bundle) => {
196
- postChannelMessage(channel, {
197
- kind: "meta-export",
198
- from: this.instanceId,
199
- bundle
200
- });
201
- });
202
- }
203
- };
204
- channel.addEventListener("message", onMessage);
205
- this.metaState = {
206
- channel,
207
- listeners,
208
- onMessage
209
- };
210
- return this.metaState;
211
- }
212
- ensureDocChannel(docId) {
213
- const existing = this.docStates.get(docId);
214
- if (existing) return existing;
215
- const channel = new (ensureBroadcastChannel())(`${this.namespace}-doc-${encodeDocChannelId(docId)}`);
216
- const listeners = /* @__PURE__ */ new Set();
217
- const onMessage = (event) => {
218
- const message = event.data;
219
- if (!message || message.from === this.instanceId) return;
220
- if (message.kind === "doc-update") for (const entry of listeners) {
221
- entry.muted = true;
222
- entry.doc.import(message.payload);
223
- entry.muted = false;
224
- entry.resolveFirst();
225
- }
226
- else if (message.kind === "doc-request") {
227
- const first = listeners.values().next().value;
228
- if (!first) return;
229
- const payload = message.docId === docId ? first.doc.export({ mode: "snapshot" }) : void 0;
230
- if (!payload) return;
231
- postChannelMessage(channel, {
232
- kind: "doc-update",
233
- docId,
234
- from: this.instanceId,
235
- mode: "snapshot",
236
- payload
237
- });
238
- }
239
- };
240
- channel.addEventListener("message", onMessage);
241
- const state = {
242
- channel,
243
- listeners,
244
- onMessage
245
- };
246
- this.docStates.set(docId, state);
247
- return state;
248
- }
249
- teardownDocChannel(docId) {
250
- const state = this.docStates.get(docId);
251
- if (!state) return;
252
- for (const entry of state.listeners) entry.unsubscribe();
253
- state.channel.removeEventListener("message", state.onMessage);
254
- state.channel.close();
255
- this.docStates.delete(docId);
256
- }
257
- };
258
-
259
- //#endregion
260
- //#region src/storage/indexeddb.ts
261
- const DEFAULT_DB_NAME = "loro-repo";
262
- const DEFAULT_DB_VERSION = 1;
263
- const DEFAULT_DOC_STORE = "docs";
264
- const DEFAULT_META_STORE = "meta";
265
- const DEFAULT_ASSET_STORE = "assets";
266
- const DEFAULT_DOC_UPDATE_STORE = "doc-updates";
267
- const DEFAULT_META_KEY = "snapshot";
268
- const textDecoder$1 = new TextDecoder();
269
- function describeUnknown(cause) {
270
- if (typeof cause === "string") return cause;
271
- if (typeof cause === "number" || typeof cause === "boolean") return String(cause);
272
- if (typeof cause === "bigint") return cause.toString();
273
- if (typeof cause === "symbol") return cause.description ?? cause.toString();
274
- if (typeof cause === "function") return `[function ${cause.name ?? "anonymous"}]`;
275
- if (cause && typeof cause === "object") try {
276
- return JSON.stringify(cause);
277
- } catch {
278
- return "[object]";
279
- }
280
- return String(cause);
281
- }
282
- var IndexedDBStorageAdaptor = class {
283
- idb;
284
- dbName;
285
- version;
286
- docStore;
287
- docUpdateStore;
288
- metaStore;
289
- assetStore;
290
- metaKey;
291
- dbPromise;
292
- closed = false;
293
- constructor(options = {}) {
294
- const idbFactory = globalThis.indexedDB;
295
- if (!idbFactory) throw new Error("IndexedDB is not available in this environment");
296
- this.idb = idbFactory;
297
- this.dbName = options.dbName ?? DEFAULT_DB_NAME;
298
- this.version = options.version ?? DEFAULT_DB_VERSION;
299
- this.docStore = options.docStoreName ?? DEFAULT_DOC_STORE;
300
- this.docUpdateStore = options.docUpdateStoreName ?? DEFAULT_DOC_UPDATE_STORE;
301
- this.metaStore = options.metaStoreName ?? DEFAULT_META_STORE;
302
- this.assetStore = options.assetStoreName ?? DEFAULT_ASSET_STORE;
303
- this.metaKey = options.metaKey ?? DEFAULT_META_KEY;
304
- }
305
- async save(payload) {
306
- const db = await this.ensureDb();
307
- switch (payload.type) {
308
- case "doc-snapshot": {
309
- const snapshot = payload.snapshot.slice();
310
- await this.storeMergedSnapshot(db, payload.docId, snapshot);
311
- break;
312
- }
313
- case "doc-update": {
314
- const update = payload.update.slice();
315
- await this.appendDocUpdate(db, payload.docId, update);
316
- break;
317
- }
318
- case "asset": {
319
- const bytes = payload.data.slice();
320
- await this.putBinary(db, this.assetStore, payload.assetId, bytes);
321
- break;
322
- }
323
- case "meta": {
324
- const bytes = payload.update.slice();
325
- await this.putBinary(db, this.metaStore, this.metaKey, bytes);
326
- break;
327
- }
328
- default: throw new Error("Unsupported storage payload type");
329
- }
330
- }
331
- async deleteAsset(assetId) {
332
- const db = await this.ensureDb();
333
- await this.deleteKey(db, this.assetStore, assetId);
334
- }
335
- async loadDoc(docId) {
336
- const db = await this.ensureDb();
337
- const snapshot = await this.getBinaryFromDb(db, this.docStore, docId);
338
- const pendingUpdates = await this.getDocUpdates(db, docId);
339
- if (!snapshot && pendingUpdates.length === 0) return;
340
- let doc;
341
- try {
342
- doc = snapshot ? LoroDoc.fromSnapshot(snapshot) : new LoroDoc();
343
- } catch (error) {
344
- throw this.createError(`Failed to hydrate document snapshot for "${docId}"`, error);
345
- }
346
- let appliedUpdates = false;
347
- for (const update of pendingUpdates) try {
348
- doc.import(update);
349
- appliedUpdates = true;
350
- } catch (error) {
351
- throw this.createError(`Failed to apply queued document update for "${docId}"`, error);
352
- }
353
- if (appliedUpdates) {
354
- let consolidated;
355
- try {
356
- consolidated = doc.export({ mode: "snapshot" });
357
- } catch (error) {
358
- throw this.createError(`Failed to export consolidated snapshot for "${docId}"`, error);
359
- }
360
- await this.writeSnapshot(db, docId, consolidated);
361
- await this.clearDocUpdates(db, docId);
362
- }
363
- return doc;
364
- }
365
- async loadMeta() {
366
- const bytes = await this.getBinary(this.metaStore, this.metaKey);
367
- if (!bytes) return void 0;
368
- try {
369
- const json = textDecoder$1.decode(bytes);
370
- const bundle = JSON.parse(json);
371
- const flock = new Flock();
372
- flock.importJson(bundle);
373
- return flock;
374
- } catch (error) {
375
- throw this.createError("Failed to hydrate metadata snapshot", error);
376
- }
377
- }
378
- async loadAsset(assetId) {
379
- return await this.getBinary(this.assetStore, assetId) ?? void 0;
380
- }
381
- async close() {
382
- this.closed = true;
383
- const db = await this.dbPromise;
384
- if (db) db.close();
385
- this.dbPromise = void 0;
386
- }
387
- async ensureDb() {
388
- if (this.closed) throw new Error("IndexedDBStorageAdaptor has been closed");
389
- if (!this.dbPromise) this.dbPromise = new Promise((resolve, reject) => {
390
- const request = this.idb.open(this.dbName, this.version);
391
- request.addEventListener("upgradeneeded", () => {
392
- const db = request.result;
393
- this.ensureStore(db, this.docStore);
394
- this.ensureStore(db, this.docUpdateStore);
395
- this.ensureStore(db, this.metaStore);
396
- this.ensureStore(db, this.assetStore);
397
- });
398
- request.addEventListener("success", () => resolve(request.result), { once: true });
399
- request.addEventListener("error", () => {
400
- reject(this.createError(`Failed to open IndexedDB database "${this.dbName}"`, request.error));
401
- }, { once: true });
402
- });
403
- return this.dbPromise;
404
- }
405
- ensureStore(db, storeName) {
406
- const names = db.objectStoreNames;
407
- if (this.storeExists(names, storeName)) return;
408
- db.createObjectStore(storeName);
409
- }
410
- storeExists(names, storeName) {
411
- if (typeof names.contains === "function") return names.contains(storeName);
412
- const length = names.length ?? 0;
413
- for (let index = 0; index < length; index += 1) if (names.item?.(index) === storeName) return true;
414
- return false;
415
- }
416
- async storeMergedSnapshot(db, docId, incoming) {
417
- await this.runInTransaction(db, this.docStore, "readwrite", async (store) => {
418
- const existingRaw = await this.wrapRequest(store.get(docId), "read");
419
- const existing = await this.normalizeBinary(existingRaw);
420
- const merged = this.mergeSnapshots(docId, existing, incoming);
421
- await this.wrapRequest(store.put(merged, docId), "write");
422
- });
423
- }
424
- mergeSnapshots(docId, existing, incoming) {
425
- try {
426
- const doc = existing ? LoroDoc.fromSnapshot(existing) : new LoroDoc();
427
- doc.import(incoming);
428
- return doc.export({ mode: "snapshot" });
429
- } catch (error) {
430
- throw this.createError(`Failed to merge snapshot for "${docId}"`, error);
431
- }
432
- }
433
- async appendDocUpdate(db, docId, update) {
434
- await this.runInTransaction(db, this.docUpdateStore, "readwrite", async (store) => {
435
- const raw = await this.wrapRequest(store.get(docId), "read");
436
- const queue = await this.normalizeUpdateQueue(raw);
437
- queue.push(update.slice());
438
- await this.wrapRequest(store.put({ updates: queue }, docId), "write");
439
- });
440
- }
441
- async getDocUpdates(db, docId) {
442
- const raw = await this.runInTransaction(db, this.docUpdateStore, "readonly", (store) => this.wrapRequest(store.get(docId), "read"));
443
- return this.normalizeUpdateQueue(raw);
444
- }
445
- async clearDocUpdates(db, docId) {
446
- await this.runInTransaction(db, this.docUpdateStore, "readwrite", (store) => this.wrapRequest(store.delete(docId), "delete"));
447
- }
448
- async writeSnapshot(db, docId, snapshot) {
449
- await this.putBinary(db, this.docStore, docId, snapshot.slice());
450
- }
451
- async getBinaryFromDb(db, storeName, key) {
452
- const value = await this.runInTransaction(db, storeName, "readonly", (store) => this.wrapRequest(store.get(key), "read"));
453
- return this.normalizeBinary(value);
454
- }
455
- async normalizeUpdateQueue(value) {
456
- if (value == null) return [];
457
- const list = Array.isArray(value) ? value : typeof value === "object" && value !== null ? value.updates : void 0;
458
- if (!Array.isArray(list)) return [];
459
- const queue = [];
460
- for (const entry of list) {
461
- const bytes = await this.normalizeBinary(entry);
462
- if (bytes) queue.push(bytes);
463
- }
464
- return queue;
465
- }
466
- async putBinary(db, storeName, key, value) {
467
- await this.runInTransaction(db, storeName, "readwrite", (store) => this.wrapRequest(store.put(value, key), "write"));
468
- }
469
- async deleteKey(db, storeName, key) {
470
- await this.runInTransaction(db, storeName, "readwrite", (store) => this.wrapRequest(store.delete(key), "delete"));
471
- }
472
- async getBinary(storeName, key) {
473
- const db = await this.ensureDb();
474
- return this.getBinaryFromDb(db, storeName, key);
475
- }
476
- runInTransaction(db, storeName, mode, executor) {
477
- const tx = db.transaction(storeName, mode);
478
- const store = tx.objectStore(storeName);
479
- const completion = new Promise((resolve, reject) => {
480
- tx.addEventListener("complete", () => resolve(), { once: true });
481
- tx.addEventListener("abort", () => reject(this.createError("IndexedDB transaction aborted", tx.error)), { once: true });
482
- tx.addEventListener("error", () => reject(this.createError("IndexedDB transaction failed", tx.error)), { once: true });
483
- });
484
- return Promise.all([executor(store), completion]).then(([result]) => result);
485
- }
486
- wrapRequest(request, action) {
487
- return new Promise((resolve, reject) => {
488
- request.addEventListener("success", () => resolve(request.result), { once: true });
489
- request.addEventListener("error", () => reject(this.createError(`IndexedDB request failed during ${action}`, request.error)), { once: true });
490
- });
491
- }
492
- async normalizeBinary(value) {
493
- if (value == null) return void 0;
494
- if (value instanceof Uint8Array) return value.slice();
495
- if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength).slice();
496
- if (value instanceof ArrayBuffer) return new Uint8Array(value.slice(0));
497
- if (typeof value === "object" && value !== null && "arrayBuffer" in value) {
498
- const candidate = value;
499
- if (typeof candidate.arrayBuffer === "function") {
500
- const buffer = await candidate.arrayBuffer();
501
- return new Uint8Array(buffer);
502
- }
503
- }
504
- }
505
- createError(message, cause) {
506
- if (cause instanceof Error) return new Error(`${message}: ${cause.message}`, { cause });
507
- if (cause !== void 0 && cause !== null) return /* @__PURE__ */ new Error(`${message}: ${describeUnknown(cause)}`);
508
- return new Error(message);
509
- }
510
- };
511
-
512
- //#endregion
513
- //#region src/storage/filesystem.ts
514
- const textDecoder = new TextDecoder();
515
- var FileSystemStorageAdaptor = class {
516
- baseDir;
517
- docsDir;
518
- assetsDir;
519
- metaPath;
520
- initPromise;
521
- updateCounter = 0;
522
- constructor(options = {}) {
523
- this.baseDir = path.resolve(options.baseDir ?? path.join(process.cwd(), ".loro-repo"));
524
- this.docsDir = path.join(this.baseDir, options.docsDirName ?? "docs");
525
- this.assetsDir = path.join(this.baseDir, options.assetsDirName ?? "assets");
526
- this.metaPath = path.join(this.baseDir, options.metaFileName ?? "meta.json");
527
- this.initPromise = this.ensureLayout();
528
- }
529
- async save(payload) {
530
- await this.initPromise;
531
- switch (payload.type) {
532
- case "doc-snapshot":
533
- await this.writeDocSnapshot(payload.docId, payload.snapshot);
534
- return;
535
- case "doc-update":
536
- await this.enqueueDocUpdate(payload.docId, payload.update);
537
- return;
538
- case "asset":
539
- await this.writeAsset(payload.assetId, payload.data);
540
- return;
541
- case "meta":
542
- await writeFileAtomic(this.metaPath, payload.update);
543
- return;
544
- default: throw new Error(`Unsupported payload type: ${payload.type}`);
545
- }
546
- }
547
- async deleteAsset(assetId) {
548
- await this.initPromise;
549
- await removeIfExists(this.assetPath(assetId));
550
- }
551
- async loadDoc(docId) {
552
- await this.initPromise;
553
- const snapshotBytes = await readFileIfExists(this.docSnapshotPath(docId));
554
- const updateDir = this.docUpdatesDir(docId);
555
- const updateFiles = await listFiles(updateDir);
556
- if (!snapshotBytes && updateFiles.length === 0) return;
557
- const doc = snapshotBytes ? LoroDoc.fromSnapshot(snapshotBytes) : new LoroDoc();
558
- if (updateFiles.length === 0) return doc;
559
- const updatePaths = updateFiles.map((file) => path.join(updateDir, file));
560
- for (const updatePath of updatePaths) {
561
- const update = await readFileIfExists(updatePath);
562
- if (!update) continue;
563
- doc.import(update);
564
- }
565
- await Promise.all(updatePaths.map((filePath) => removeIfExists(filePath)));
566
- const consolidated = doc.export({ mode: "snapshot" });
567
- await this.writeDocSnapshot(docId, consolidated);
568
- return doc;
569
- }
570
- async loadMeta() {
571
- await this.initPromise;
572
- const bytes = await readFileIfExists(this.metaPath);
573
- if (!bytes) return void 0;
574
- try {
575
- const bundle = JSON.parse(textDecoder.decode(bytes));
576
- const flock = new Flock();
577
- flock.importJson(bundle);
578
- return flock;
579
- } catch (error) {
580
- throw new Error("Failed to hydrate metadata snapshot", { cause: error });
581
- }
582
- }
583
- async loadAsset(assetId) {
584
- await this.initPromise;
585
- return readFileIfExists(this.assetPath(assetId));
586
- }
587
- async ensureLayout() {
588
- await Promise.all([
589
- ensureDir(this.baseDir),
590
- ensureDir(this.docsDir),
591
- ensureDir(this.assetsDir)
592
- ]);
593
- }
594
- async writeDocSnapshot(docId, snapshot) {
595
- await ensureDir(this.docDir(docId));
596
- await writeFileAtomic(this.docSnapshotPath(docId), snapshot);
597
- }
598
- async enqueueDocUpdate(docId, update) {
599
- const dir = this.docUpdatesDir(docId);
600
- await ensureDir(dir);
601
- const counter = this.updateCounter = (this.updateCounter + 1) % 1e6;
602
- const fileName = `${Date.now().toString().padStart(13, "0")}-${counter.toString().padStart(6, "0")}.bin`;
603
- await writeFileAtomic(path.join(dir, fileName), update);
604
- }
605
- async writeAsset(assetId, data) {
606
- const filePath = this.assetPath(assetId);
607
- await ensureDir(path.dirname(filePath));
608
- await writeFileAtomic(filePath, data);
609
- }
610
- docDir(docId) {
611
- return path.join(this.docsDir, encodeComponent(docId));
612
- }
613
- docSnapshotPath(docId) {
614
- return path.join(this.docDir(docId), "snapshot.bin");
615
- }
616
- docUpdatesDir(docId) {
617
- return path.join(this.docDir(docId), "updates");
618
- }
619
- assetPath(assetId) {
620
- return path.join(this.assetsDir, encodeComponent(assetId));
621
- }
622
- };
623
- function encodeComponent(value) {
624
- return Buffer.from(value, "utf8").toString("base64url");
625
- }
626
- async function ensureDir(dir) {
627
- await promises.mkdir(dir, { recursive: true });
628
- }
629
- async function readFileIfExists(filePath) {
630
- try {
631
- const data = await promises.readFile(filePath);
632
- return new Uint8Array(data.buffer, data.byteOffset, data.byteLength).slice();
633
- } catch (error) {
634
- if (error.code === "ENOENT") return;
635
- throw error;
636
- }
637
- }
638
- async function removeIfExists(filePath) {
639
- try {
640
- await promises.rm(filePath);
641
- } catch (error) {
642
- if (error.code === "ENOENT") return;
643
- throw error;
644
- }
645
- }
646
- async function listFiles(dir) {
647
- try {
648
- return (await promises.readdir(dir)).sort();
649
- } catch (error) {
650
- if (error.code === "ENOENT") return [];
651
- throw error;
652
- }
653
- }
654
- async function writeFileAtomic(targetPath, data) {
655
- const dir = path.dirname(targetPath);
656
- await ensureDir(dir);
657
- const tempPath = path.join(dir, `.tmp-${randomUUID()}`);
658
- await promises.writeFile(tempPath, data);
659
- await promises.rename(tempPath, targetPath);
660
- }
661
-
662
- //#endregion
663
- //#region src/internal/debug.ts
664
- const getEnv = () => {
665
- if (typeof globalThis !== "object" || globalThis === null) return;
666
- return globalThis.process?.env;
667
- };
668
- const rawNamespaceConfig = (getEnv()?.LORO_REPO_DEBUG ?? "").trim();
669
- const normalizedNamespaces = rawNamespaceConfig.length > 0 ? rawNamespaceConfig.split(/[\s,]+/).map((token) => token.toLowerCase()).filter(Boolean) : [];
670
- const wildcardTokens = new Set([
671
- "*",
672
- "1",
673
- "true",
674
- "all"
675
- ]);
676
- const namespaceSet = new Set(normalizedNamespaces);
677
- const hasWildcard = namespaceSet.size > 0 && normalizedNamespaces.some((token) => wildcardTokens.has(token));
678
- const isDebugEnabled = (namespace) => {
679
- if (!namespaceSet.size) return false;
680
- if (!namespace) return hasWildcard;
681
- const normalized = namespace.toLowerCase();
682
- if (hasWildcard) return true;
683
- if (namespaceSet.has(normalized)) return true;
684
- const [root] = normalized.split(":");
685
- return namespaceSet.has(root);
686
- };
687
- const createDebugLogger = (namespace) => {
688
- const normalized = namespace.toLowerCase();
689
- return (...args) => {
690
- if (!isDebugEnabled(normalized)) return;
691
- const prefix = `[loro-repo:${namespace}]`;
692
- if (args.length === 0) {
693
- console.info(prefix);
694
- return;
695
- }
696
- console.info(prefix, ...args);
697
- };
698
- };
699
-
700
- //#endregion
701
- //#region src/transport/websocket.ts
702
- const debug = createDebugLogger("transport:websocket");
703
- function withTimeout(promise, timeoutMs) {
704
- if (!timeoutMs || timeoutMs <= 0) return promise;
705
- return new Promise((resolve, reject) => {
706
- const timer = setTimeout(() => {
707
- reject(/* @__PURE__ */ new Error(`Operation timed out after ${timeoutMs}ms`));
708
- }, timeoutMs);
709
- promise.then((value) => {
710
- clearTimeout(timer);
711
- resolve(value);
712
- }).catch((error) => {
713
- clearTimeout(timer);
714
- reject(error);
715
- });
716
- });
717
- }
718
- function normalizeRoomId(roomId, fallback) {
719
- if (typeof roomId === "string" && roomId.length > 0) return roomId;
720
- if (roomId instanceof Uint8Array && roomId.length > 0) try {
721
- return bytesToHex(roomId);
722
- } catch {
723
- return fallback;
724
- }
725
- return fallback;
726
- }
727
- function bytesEqual(a, b) {
728
- if (a === b) return true;
729
- if (!a || !b) return false;
730
- if (a.length !== b.length) return false;
731
- for (let i = 0; i < a.length; i += 1) if (a[i] !== b[i]) return false;
732
- return true;
733
- }
734
- /**
735
- * loro-websocket backed {@link TransportAdapter} implementation for LoroRepo.
736
- * It uses loro-protocol as the underlying protocol for the transport.
737
- */
738
- var WebSocketTransportAdapter = class {
739
- options;
740
- client;
741
- metadataSession;
742
- docSessions = /* @__PURE__ */ new Map();
743
- constructor(options) {
744
- this.options = options;
745
- }
746
- async connect(_options) {
747
- const client = this.ensureClient();
748
- debug("connect requested", { status: client.getStatus() });
749
- try {
750
- await client.connect();
751
- debug("client.connect resolved");
752
- await client.waitConnected();
753
- debug("client.waitConnected resolved", { status: client.getStatus() });
754
- } catch (error) {
755
- debug("connect failed", error);
756
- throw error;
757
- }
758
- }
759
- async close() {
760
- debug("close requested", {
761
- docSessions: this.docSessions.size,
762
- metadataSession: Boolean(this.metadataSession)
763
- });
764
- for (const [docId] of this.docSessions) await this.leaveDocSession(docId).catch(() => {});
765
- this.docSessions.clear();
766
- await this.teardownMetadataSession().catch(() => {});
767
- if (this.client) {
768
- const client = this.client;
769
- this.client = void 0;
770
- client.destroy();
771
- debug("websocket client destroyed");
772
- }
773
- debug("close completed");
774
- }
775
- isConnected() {
776
- return this.client?.getStatus() === "connected";
777
- }
778
- async syncMeta(flock, options) {
779
- debug("syncMeta requested", { roomId: this.options.metadataRoomId });
780
- try {
781
- let auth;
782
- if (this.options.metadataAuth) if (typeof this.options.metadataAuth === "function") auth = await this.options.metadataAuth();
783
- else auth = this.options.metadataAuth;
784
- await withTimeout((await this.ensureMetadataSession(flock, {
785
- roomId: this.options.metadataRoomId ?? "repo:meta",
786
- auth
787
- })).firstSynced, options?.timeout);
788
- debug("syncMeta completed", { roomId: this.options.metadataRoomId });
789
- return { ok: true };
790
- } catch (error) {
791
- debug("syncMeta failed", error);
792
- return { ok: false };
793
- }
794
- }
795
- joinMetaRoom(flock, params) {
796
- const fallback = this.options.metadataRoomId ?? "";
797
- const roomId = normalizeRoomId(params?.roomId, fallback);
798
- if (!roomId) throw new Error("Metadata room id not configured");
799
- const session = (async () => {
800
- let auth;
801
- const authWay = params?.auth ?? this.options.metadataAuth;
802
- if (typeof authWay === "function") auth = await authWay();
803
- else auth = authWay;
804
- debug("joinMetaRoom requested", {
805
- roomId,
806
- hasAuth: Boolean(auth && auth.length)
807
- });
808
- return this.ensureMetadataSession(flock, {
809
- roomId,
810
- auth
811
- });
812
- })();
813
- const firstSynced = session.then((session$1) => session$1.firstSynced);
814
- const getConnected = () => this.isConnected();
815
- const subscription = {
816
- unsubscribe: () => {
817
- session.then((session$1) => {
818
- session$1.refCount = Math.max(0, session$1.refCount - 1);
819
- debug("metadata session refCount decremented", {
820
- roomId: session$1.roomId,
821
- refCount: session$1.refCount
822
- });
823
- if (session$1.refCount === 0) {
824
- debug("tearing down metadata session due to refCount=0", { roomId: session$1.roomId });
825
- this.teardownMetadataSession(session$1).catch(() => {});
826
- }
827
- });
828
- },
829
- firstSyncedWithRemote: firstSynced,
830
- get connected() {
831
- return getConnected();
832
- }
833
- };
834
- session.then((session$1) => {
835
- session$1.refCount += 1;
836
- debug("metadata session refCount incremented", {
837
- roomId: session$1.roomId,
838
- refCount: session$1.refCount
839
- });
840
- });
841
- return subscription;
842
- }
843
- async syncDoc(docId, doc, options) {
844
- debug("syncDoc requested", { docId });
845
- try {
846
- const session = await this.ensureDocSession(docId, doc, {});
847
- await withTimeout(session.firstSynced, options?.timeout);
848
- debug("syncDoc completed", {
849
- docId,
850
- roomId: session.roomId
851
- });
852
- return { ok: true };
853
- } catch (error) {
854
- debug("syncDoc failed", {
855
- docId,
856
- error
857
- });
858
- return { ok: false };
859
- }
860
- }
861
- joinDocRoom(docId, doc, params) {
862
- debug("joinDocRoom requested", {
863
- docId,
864
- roomParamType: params?.roomId ? typeof params.roomId === "string" ? "string" : "uint8array" : void 0,
865
- hasAuthOverride: Boolean(params?.auth && params.auth.length)
866
- });
867
- const ensure = this.ensureDocSession(docId, doc, params ?? {});
868
- const firstSynced = ensure.then((session) => session.firstSynced);
869
- const getConnected = () => this.isConnected();
870
- const subscription = {
871
- unsubscribe: () => {
872
- ensure.then((session) => {
873
- session.refCount = Math.max(0, session.refCount - 1);
874
- debug("doc session refCount decremented", {
875
- docId,
876
- roomId: session.roomId,
877
- refCount: session.refCount
878
- });
879
- if (session.refCount === 0) this.leaveDocSession(docId).catch(() => {});
880
- });
881
- },
882
- firstSyncedWithRemote: firstSynced,
883
- get connected() {
884
- return getConnected();
885
- }
886
- };
887
- ensure.then((session) => {
888
- session.refCount += 1;
889
- debug("doc session refCount incremented", {
890
- docId,
891
- roomId: session.roomId,
892
- refCount: session.refCount
893
- });
894
- });
895
- return subscription;
896
- }
897
- ensureClient() {
898
- if (this.client) {
899
- debug("reusing websocket client", { status: this.client.getStatus() });
900
- return this.client;
901
- }
902
- const { url, client: clientOptions } = this.options;
903
- debug("creating websocket client", {
904
- url,
905
- clientOptionsKeys: clientOptions ? Object.keys(clientOptions) : []
906
- });
907
- const client = new LoroWebsocketClient({
908
- url,
909
- ...clientOptions
910
- });
911
- this.client = client;
912
- return client;
913
- }
914
- async ensureMetadataSession(flock, params) {
915
- debug("ensureMetadataSession invoked", {
916
- roomId: params.roomId,
917
- hasAuth: Boolean(params.auth && params.auth.length)
918
- });
919
- const client = this.ensureClient();
920
- await client.waitConnected();
921
- debug("websocket client ready for metadata session", { status: client.getStatus() });
922
- if (this.metadataSession && this.metadataSession.flock === flock && this.metadataSession.roomId === params.roomId && bytesEqual(this.metadataSession.auth, params.auth)) {
923
- debug("reusing metadata session", {
924
- roomId: this.metadataSession.roomId,
925
- refCount: this.metadataSession.refCount
926
- });
927
- return this.metadataSession;
928
- }
929
- if (this.metadataSession) {
930
- debug("tearing down previous metadata session", { roomId: this.metadataSession.roomId });
931
- await this.teardownMetadataSession(this.metadataSession).catch(() => {});
932
- }
933
- const adaptor = new FlockAdaptor(flock, this.options.metadataAdaptorConfig);
934
- debug("joining metadata room", {
935
- roomId: params.roomId,
936
- hasAuth: Boolean(params.auth && params.auth.length)
937
- });
938
- const room = await client.join({
939
- roomId: params.roomId,
940
- crdtAdaptor: adaptor,
941
- auth: params.auth
942
- });
943
- const firstSynced = room.waitForReachingServerVersion();
944
- firstSynced.then(() => {
945
- debug("metadata session firstSynced resolved", { roomId: params.roomId });
946
- }, (error) => {
947
- debug("metadata session firstSynced rejected", {
948
- roomId: params.roomId,
949
- error
950
- });
951
- });
952
- const session = {
953
- adaptor,
954
- room,
955
- firstSynced,
956
- flock,
957
- roomId: params.roomId,
958
- auth: params.auth,
959
- refCount: 0
960
- };
961
- this.metadataSession = session;
962
- return session;
963
- }
964
- async teardownMetadataSession(session) {
965
- const target = session ?? this.metadataSession;
966
- if (!target) return;
967
- debug("teardownMetadataSession invoked", { roomId: target.roomId });
968
- if (this.metadataSession === target) this.metadataSession = void 0;
969
- const { adaptor, room } = target;
970
- try {
971
- await room.leave();
972
- debug("metadata room left", { roomId: target.roomId });
973
- } catch (error) {
974
- debug("metadata room leave failed; destroying", {
975
- roomId: target.roomId,
976
- error
977
- });
978
- await room.destroy().catch(() => {});
979
- }
980
- adaptor.destroy();
981
- debug("metadata session destroyed", { roomId: target.roomId });
982
- }
983
- async ensureDocSession(docId, doc, params) {
984
- debug("ensureDocSession invoked", { docId });
985
- const client = this.ensureClient();
986
- await client.waitConnected();
987
- debug("websocket client ready for doc session", {
988
- docId,
989
- status: client.getStatus()
990
- });
991
- const existing = this.docSessions.get(docId);
992
- const derivedRoomId = this.options.docRoomId?.(docId) ?? docId;
993
- const roomId = normalizeRoomId(params.roomId, derivedRoomId);
994
- let auth;
995
- auth = await (params.auth ?? this.options.docAuth?.(docId));
996
- debug("doc session params resolved", {
997
- docId,
998
- roomId,
999
- hasAuth: Boolean(auth && auth.length)
1000
- });
1001
- if (existing && existing.doc === doc && existing.roomId === roomId) {
1002
- debug("reusing doc session", {
1003
- docId,
1004
- roomId,
1005
- refCount: existing.refCount
1006
- });
1007
- return existing;
1008
- }
1009
- if (existing) {
1010
- debug("doc session mismatch; leaving existing session", {
1011
- docId,
1012
- previousRoomId: existing.roomId,
1013
- nextRoomId: roomId
1014
- });
1015
- await this.leaveDocSession(docId).catch(() => {});
1016
- }
1017
- const adaptor = new LoroAdaptor(doc);
1018
- debug("joining doc room", {
1019
- docId,
1020
- roomId,
1021
- hasAuth: Boolean(auth && auth.length)
1022
- });
1023
- const room = await client.join({
1024
- roomId,
1025
- crdtAdaptor: adaptor,
1026
- auth
1027
- });
1028
- const firstSynced = room.waitForReachingServerVersion();
1029
- firstSynced.then(() => {
1030
- debug("doc session firstSynced resolved", {
1031
- docId,
1032
- roomId
1033
- });
1034
- }, (error) => {
1035
- debug("doc session firstSynced rejected", {
1036
- docId,
1037
- roomId,
1038
- error
1039
- });
1040
- });
1041
- const session = {
1042
- adaptor,
1043
- room,
1044
- firstSynced,
1045
- doc,
1046
- roomId,
1047
- refCount: 0
1048
- };
1049
- this.docSessions.set(docId, session);
1050
- return session;
1051
- }
1052
- async leaveDocSession(docId) {
1053
- const session = this.docSessions.get(docId);
1054
- if (!session) {
1055
- debug("leaveDocSession invoked but no session found", { docId });
1056
- return;
1057
- }
1058
- this.docSessions.delete(docId);
1059
- debug("leaving doc session", {
1060
- docId,
1061
- roomId: session.roomId
1062
- });
1063
- try {
1064
- await session.room.leave();
1065
- debug("doc room left", {
1066
- docId,
1067
- roomId: session.roomId
1068
- });
1069
- } catch (error) {
1070
- debug("doc room leave failed; destroying", {
1071
- docId,
1072
- roomId: session.roomId,
1073
- error
1074
- });
1075
- await session.room.destroy().catch(() => {});
1076
- }
1077
- session.adaptor.destroy();
1078
- debug("doc session destroyed", {
1079
- docId,
1080
- roomId: session.roomId
1081
- });
1082
- }
1083
- };
1084
-
1085
- //#endregion
1086
4
  //#region src/internal/event-bus.ts
1087
5
  var RepoEventBus = class {
1088
6
  watchers = /* @__PURE__ */ new Set();
@@ -1262,12 +180,12 @@ var DocManager = class {
1262
180
  this.docPersistedVersions.delete(docId);
1263
181
  }
1264
182
  async flush() {
1265
- const promises$1 = [];
1266
- for (const [docId, doc] of this.docs) promises$1.push((async () => {
183
+ const promises = [];
184
+ for (const [docId, doc] of this.docs) promises.push((async () => {
1267
185
  await this.persistDocUpdate(docId, doc);
1268
186
  await this.flushScheduledDocFrontierUpdate(docId);
1269
187
  })());
1270
- await Promise.all(promises$1);
188
+ await Promise.all(promises);
1271
189
  }
1272
190
  async close() {
1273
191
  await this.flush();
@@ -1435,14 +353,14 @@ async function assetContentToUint8Array(content) {
1435
353
  if (typeof ReadableStream !== "undefined" && content instanceof ReadableStream) return streamToUint8Array(content);
1436
354
  throw new TypeError("Unsupported asset content type");
1437
355
  }
1438
- function bytesToHex$1(bytes) {
356
+ function bytesToHex(bytes) {
1439
357
  return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
1440
358
  }
1441
359
  async function computeSha256(bytes) {
1442
360
  const globalCrypto = globalThis.crypto;
1443
361
  if (globalCrypto?.subtle && typeof globalCrypto.subtle.digest === "function") {
1444
362
  const digest = await globalCrypto.subtle.digest("SHA-256", bytes);
1445
- return bytesToHex$1(new Uint8Array(digest));
363
+ return bytesToHex(new Uint8Array(digest));
1446
364
  }
1447
365
  try {
1448
366
  const { createHash } = await import("node:crypto");
@@ -1596,8 +514,7 @@ var MetadataManager = class {
1596
514
  return this.state.metadata.entries();
1597
515
  }
1598
516
  get(docId) {
1599
- const metadata = this.state.metadata.get(docId);
1600
- return metadata ? cloneJsonObject(metadata) : void 0;
517
+ return this.state.metadata.get(docId);
1601
518
  }
1602
519
  listDoc(query) {
1603
520
  if (query?.limit !== void 0 && query.limit <= 0) return [];
@@ -1642,20 +559,16 @@ var MetadataManager = class {
1642
559
  for (const key of Object.keys(patchObject)) {
1643
560
  const rawValue = patchObject[key];
1644
561
  if (rawValue === void 0) continue;
1645
- let canonical;
1646
- if (key === "tombstone") canonical = Boolean(rawValue);
1647
- else canonical = cloneJsonValue(rawValue);
1648
- if (canonical === void 0) continue;
1649
- if (jsonEquals(base ? base[key] : void 0, canonical)) continue;
562
+ if (jsonEquals(base ? base[key] : void 0, rawValue)) continue;
1650
563
  const storageKey = key === "tombstone" ? "$tombstone" : key;
564
+ console.log("upserting", rawValue);
1651
565
  this.metaFlock.put([
1652
566
  "m",
1653
567
  docId,
1654
568
  storageKey
1655
- ], canonical);
1656
- const stored = cloneJsonValue(canonical) ?? canonical;
1657
- next[key] = stored;
1658
- outPatch[key] = cloneJsonValue(stored) ?? stored;
569
+ ], rawValue);
570
+ next[key] = rawValue;
571
+ outPatch[key] = rawValue;
1659
572
  changed = true;
1660
573
  }
1661
574
  if (!changed) {
@@ -1667,7 +580,7 @@ var MetadataManager = class {
1667
580
  this.eventBus.emit({
1668
581
  kind: "doc-metadata",
1669
582
  docId,
1670
- patch: cloneJsonObject(outPatch),
583
+ patch: outPatch,
1671
584
  by: "local"
1672
585
  });
1673
586
  }
@@ -2828,5 +1741,5 @@ var LoroRepo = class LoroRepo {
2828
1741
  };
2829
1742
 
2830
1743
  //#endregion
2831
- export { BroadcastChannelTransportAdapter, FileSystemStorageAdaptor, IndexedDBStorageAdaptor, LoroRepo, WebSocketTransportAdapter };
1744
+ export { LoroRepo };
2832
1745
  //# sourceMappingURL=index.js.map