@daasboard/doc 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1314 @@
1
+ import { decodeImportBlobMeta } from "loro-crdt";
2
+ import { RoomPresence, MAX_PRESENCE_BYTES } from "../presence/RoomPresence.js";
3
+ import { fetchSnapshot } from "./snapshot.js";
4
+ import { resolveRoomToken } from "./token.js";
5
+ import { exportInSlices } from "./slices.js";
6
+ import { emptyPrefixHash, extendPrefixHash, roomBytes, sameBytes } from "./prefix.js";
7
+ export const EPOCH_DIVERGED = 0x01;
8
+ const JOIN = 0x01;
9
+ const UPDATE = 0x02;
10
+ const AUTH = 0x03;
11
+ const EPHEMERAL = 0x04;
12
+ const PRESENCE = 0x1a;
13
+ const PRESENCE_LEAVE = 0x1b;
14
+ const PING = 0x05;
15
+ const ENTRY = 0x12;
16
+ const ACK = 0x13;
17
+ const CAUGHT = 0x14;
18
+ const PONG = 0x15;
19
+ const EPOCH = 0x16;
20
+ const NACK = 0x17;
21
+ const RESYNC = 0x18;
22
+ const GRANT = 0x19;
23
+ const ERROR = 0x1f;
24
+ const MAX_RESYNC_ATTEMPTS = 3;
25
+ const RESYNC_FETCH_WAIT_MS = 120_000;
26
+ const RESYNC_RECOVERY_MS = 10 * 60_000;
27
+ const FLAG_IF_EMPTY = 0x01;
28
+ const NACK_TOO_LARGE = 1;
29
+ const NACK_NOT_LORO = 2;
30
+ const NACK_APPEND_FAILED = 3;
31
+ const NACK_NOT_EMPTY = 4;
32
+ const NACK_READ_ONLY = 5;
33
+ const NACK_NAMES = { [NACK_TOO_LARGE]: "TOO_LARGE", [NACK_NOT_LORO]: "NOT_LORO", [NACK_APPEND_FAILED]: "APPEND_FAILED", [NACK_NOT_EMPTY]: "NOT_EMPTY", [NACK_READ_ONLY]: "READ_ONLY" };
34
+ const CLOSE_REJECTED = 4400;
35
+ const CLOSE_TOKEN_REFUSED = 4401;
36
+ const CLOSE_TOKEN_FORBIDDEN = 4403;
37
+ const CLOSE_TOKEN_EXPIRED = 4408;
38
+ const DEFAULT_MAX_UPDATE = 4 * 1024 * 1024;
39
+ const PING_MS = 20_000;
40
+ const REBUILD_WAIT_MS = 1000;
41
+ const STALL_SLACK_MS = 250;
42
+ const SLICE_WINDOW = 4;
43
+ const REPUSH_SLICE_BYTES = 256 * 1024;
44
+ const PROBE_BYTES = 64 * 1024;
45
+ const SAFE_ENDS_CAP = 4096;
46
+ function spansOf(bytes) {
47
+ try {
48
+ const meta = decodeImportBlobMeta(bytes, true);
49
+ const starts = meta.partialStartVersionVector.toJSON();
50
+ const out = [];
51
+ for (const [peer, end] of meta.partialEndVersionVector.toJSON())
52
+ out.push({ peer, start: starts.get(peer) ?? 0, end });
53
+ return out;
54
+ }
55
+ catch {
56
+ return null;
57
+ }
58
+ }
59
+ class LogCoverage {
60
+ spans = new Map();
61
+ clear() {
62
+ this.spans.clear();
63
+ }
64
+ add(peer, start, end) {
65
+ if (end <= start)
66
+ return;
67
+ const merged = { start, end };
68
+ const rest = [];
69
+ for (const s of this.spans.get(peer) ?? []) {
70
+ if (s.end < merged.start || s.start > merged.end)
71
+ rest.push(s);
72
+ else {
73
+ merged.start = Math.min(merged.start, s.start);
74
+ merged.end = Math.max(merged.end, s.end);
75
+ }
76
+ }
77
+ rest.push(merged);
78
+ rest.sort((a, b) => a.start - b.start);
79
+ this.spans.set(peer, rest);
80
+ }
81
+ covers(peer, start, end) {
82
+ return end <= start || (this.spans.get(peer) ?? []).some((s) => s.start <= start && s.end >= end);
83
+ }
84
+ prefix() {
85
+ const out = new Map();
86
+ for (const [peer, spans] of this.spans) {
87
+ const first = spans[0];
88
+ if (first && first.start === 0)
89
+ out.set(peer, first.end);
90
+ }
91
+ return out;
92
+ }
93
+ coversVersion(version) {
94
+ for (const [peer, end] of version)
95
+ if (!this.covers(peer, 0, end))
96
+ return false;
97
+ return true;
98
+ }
99
+ }
100
+ function frame(type, nums = [], bytes) {
101
+ const n = 1 + 8 * nums.length + (bytes ? bytes.byteLength : 0);
102
+ const buf = new ArrayBuffer(n);
103
+ const view = new DataView(buf);
104
+ view.setUint8(0, type);
105
+ let off = 1;
106
+ for (const v of nums) {
107
+ view.setBigUint64(off, BigInt(v));
108
+ off += 8;
109
+ }
110
+ if (bytes)
111
+ new Uint8Array(buf, off).set(bytes);
112
+ return buf;
113
+ }
114
+ function updateFrame(o) {
115
+ const buf = new ArrayBuffer(10 + o.bytes.byteLength);
116
+ const view = new DataView(buf);
117
+ view.setUint8(0, UPDATE);
118
+ view.setBigUint64(1, BigInt(o.cseq));
119
+ view.setUint8(9, o.ifEmpty ? FLAG_IF_EMPTY : 0);
120
+ new Uint8Array(buf, 10).set(o.bytes);
121
+ return buf;
122
+ }
123
+ const isCommit = (o) => o.kind === "commit";
124
+ export class WsTransport {
125
+ page;
126
+ url;
127
+ room;
128
+ events;
129
+ presence;
130
+ ws = null;
131
+ status = "connecting";
132
+ lastSeq = 0;
133
+ aboveWatermark = new Map();
134
+ unverified = new Map();
135
+ roomUtf8;
136
+ chainHash;
137
+ inflight = new Map();
138
+ epoch = 0n;
139
+ maxUpdate = DEFAULT_MAX_UPDATE;
140
+ maxUpdateHigh = 0;
141
+ largestEntry = 0;
142
+ outbox = [];
143
+ sentCount = 0;
144
+ nextCseq = 1;
145
+ logged = new LogCoverage();
146
+ safeEnds = new Map();
147
+ fullPushPending;
148
+ readOnlyHistoryPending = false;
149
+ historyEnd;
150
+ rebuildRound = null;
151
+ ownSeqs = new Set();
152
+ quietTimer;
153
+ quietExtensions = 0;
154
+ quietGraced = false;
155
+ resetToReport = null;
156
+ claiming = false;
157
+ claim = null;
158
+ claimSpans = null;
159
+ claimRefused = false;
160
+ candidate;
161
+ recoveryCandidate = false;
162
+ resyncAttempts = 0;
163
+ resyncDeadline = null;
164
+ resyncAbort = null;
165
+ stopped = false;
166
+ failure = null;
167
+ backoff = 500;
168
+ tokenProvider;
169
+ cachedToken;
170
+ tokenReusableUntil = 0;
171
+ tokenRequest = null;
172
+ tokenGeneration = 0;
173
+ refreshTimer;
174
+ openGeneration = 0;
175
+ refusals = 0;
176
+ providerFailed = false;
177
+ heldReported = false;
178
+ appendRetrying = false;
179
+ unsubscribe;
180
+ readyWaiters = [];
181
+ pingTimer;
182
+ reconnectTimer;
183
+ lastFrameAt = 0;
184
+ pingSentAt = 0;
185
+ lastServerMessage = null;
186
+ stats = { role: "write", sent: 0, acked: 0, received: 0, reconnects: 0, lastSeq: 0, pending: 0, epoch: "0", resets: 0, maxUpdate: DEFAULT_MAX_UPDATE, rejected: 0, stale: 0, rebuilding: false, bootstrap: "none", diverged: 0, witnessResets: 0, resyncs: 0 };
187
+ constructor(page, url, room, events = {}, options = {}) {
188
+ this.page = page;
189
+ this.url = url;
190
+ this.room = room;
191
+ this.events = events;
192
+ if (options.bootstrap && (page.isInitialized() || page.doc.oplogFrontiers().length > 0))
193
+ throw new Error("WsTransport: a bootstrap snapshot needs an empty page (LoroPageDoc.empty): it becomes the page's document");
194
+ this.presence = new RoomPresence(page, (bytes) => this.sendPresence(bytes), options.presence);
195
+ this.tokenProvider = options.token;
196
+ if (this.tokenProvider !== undefined)
197
+ this.stats.role = null;
198
+ this.roomUtf8 = roomBytes(room);
199
+ this.chainHash = emptyPrefixHash(this.roomUtf8);
200
+ this.candidate = options.bootstrap ?? null;
201
+ if (this.candidate)
202
+ this.stats.bootstrap = "pending";
203
+ this.unsubscribe = page.doc.subscribeLocalUpdates((bytes) => this.enqueue(bytes));
204
+ this.fullPushPending = page.doc.frontiers().length > 0;
205
+ this.historyEnd = this.ownCounter();
206
+ this.stats.rebuilding = this.fullPushPending;
207
+ this.noteState();
208
+ if (this.tokenProvider !== undefined) {
209
+ const target = new URL(url);
210
+ if (target.protocol === "ws:" && !["localhost", "127.0.0.1", "[::1]"].includes(target.hostname)) {
211
+ this.events.onError?.("the room token travels in clear text over ws://; use wss:// for a remote relay");
212
+ }
213
+ }
214
+ this.open();
215
+ }
216
+ ownCounter() {
217
+ return this.page.doc.version().toJSON().get(this.page.doc.peerIdStr) ?? 0;
218
+ }
219
+ noteState() {
220
+ for (const [peer, counter] of this.page.doc.version().toJSON()) {
221
+ const list = this.safeEnds.get(peer);
222
+ if (!list)
223
+ this.safeEnds.set(peer, [counter]);
224
+ else if (list[list.length - 1] !== counter) {
225
+ list.push(counter);
226
+ if (list.length > SAFE_ENDS_CAP)
227
+ this.safeEnds.set(peer, list.filter((_, i) => i % 2 === 1 || i === list.length - 1));
228
+ }
229
+ }
230
+ }
231
+ ready() {
232
+ if (this.status === "live")
233
+ return Promise.resolve();
234
+ if (this.status === "failed")
235
+ return Promise.reject(new Error(this.failure ?? "transport failed"));
236
+ if (this.status === "closed")
237
+ return Promise.reject(new Error("transport closed"));
238
+ return new Promise((resolve, reject) => this.readyWaiters.push({ resolve, reject }));
239
+ }
240
+ get currentStatus() {
241
+ return this.status;
242
+ }
243
+ claimEmpty(create) {
244
+ if (this.stats.role !== "write")
245
+ return Promise.reject(new Error("read-only: cannot create the room"));
246
+ if (this.claim)
247
+ return Promise.reject(new Error("claimEmpty: a claim is already pending"));
248
+ if (this.stopped)
249
+ return Promise.reject(new Error(this.failure ?? "transport closed"));
250
+ return new Promise((resolve, reject) => {
251
+ this.claim = { resolve, reject };
252
+ this.claiming = true;
253
+ try {
254
+ create();
255
+ }
256
+ catch (err) {
257
+ this.claiming = false;
258
+ this.claim = null;
259
+ reject(err instanceof Error ? err : new Error(String(err)));
260
+ return;
261
+ }
262
+ if (this.claiming) {
263
+ this.claiming = false;
264
+ this.claim = null;
265
+ resolve("created");
266
+ }
267
+ });
268
+ }
269
+ sendPresence(bytes) {
270
+ const ws = this.ws;
271
+ if (this.stopped || this.status !== "live" || this.stats.role === null || !ws || ws.readyState !== WebSocket.OPEN ||
272
+ (ws.bufferedAmount ?? 0) > 0 || (this.stats.role === "write" && this.outbox.length > this.sentCount) || bytes.length > MAX_PRESENCE_BYTES)
273
+ return false;
274
+ ws.send(frame(EPHEMERAL, [], bytes));
275
+ return true;
276
+ }
277
+ setStatus(s) {
278
+ this.status = s;
279
+ if (s !== "live")
280
+ this.presence.setConnected(false);
281
+ this.events.onStatus?.(s);
282
+ }
283
+ statsChanged() {
284
+ this.stats.pending = this.outbox.length;
285
+ this.stats.rebuilding = this.fullPushPending || this.quietTimer !== undefined;
286
+ this.events.onStats?.(this.stats);
287
+ }
288
+ async roomToken(force = false) {
289
+ if (!force && this.cachedToken !== undefined && performance.now() < this.tokenReusableUntil)
290
+ return this.cachedToken;
291
+ if (this.tokenRequest)
292
+ return this.tokenRequest;
293
+ const generation = this.tokenGeneration;
294
+ const request = resolveRoomToken(this.tokenProvider, this.room).then((token) => {
295
+ if (generation === this.tokenGeneration && !this.stopped) {
296
+ this.cachedToken = token;
297
+ this.providerFailed = false;
298
+ }
299
+ return token;
300
+ });
301
+ this.tokenRequest = request;
302
+ try {
303
+ return await request;
304
+ }
305
+ finally {
306
+ if (this.tokenRequest === request)
307
+ this.tokenRequest = null;
308
+ }
309
+ }
310
+ providerFailure() {
311
+ if (!this.providerFailed)
312
+ this.events.onError?.("the room token provider failed; keeping local changes and retrying");
313
+ this.providerFailed = true;
314
+ }
315
+ open() {
316
+ if (this.stopped)
317
+ return;
318
+ const generation = ++this.openGeneration;
319
+ if (this.tokenProvider === undefined) {
320
+ this.openSocket();
321
+ return;
322
+ }
323
+ this.stats.role = null;
324
+ void this.roomToken().then((token) => {
325
+ if (!this.stopped && generation === this.openGeneration)
326
+ this.openSocket(token);
327
+ }, () => {
328
+ if (this.stopped || generation !== this.openGeneration)
329
+ return;
330
+ if (typeof this.tokenProvider === "string") {
331
+ this.fail("the static room token is invalid; supply a nonempty ASCII token of at most 4096 bytes", undefined, "token-invalid");
332
+ return;
333
+ }
334
+ this.providerFailure();
335
+ this.scheduleReconnect();
336
+ });
337
+ }
338
+ openSocket(token) {
339
+ if (this.stopped)
340
+ return;
341
+ const ws = new WebSocket(`${this.url.replace(/\/$/, "")}/rooms/${encodeURIComponent(this.room)}`);
342
+ ws.binaryType = "arraybuffer";
343
+ this.ws = ws;
344
+ ws.onopen = () => {
345
+ if (ws !== this.ws)
346
+ return;
347
+ this.sentCount = 0;
348
+ this.lastFrameAt = Date.now();
349
+ this.pingSentAt = 0;
350
+ this.setStatus("catching-up");
351
+ const c = this.candidate;
352
+ this.inflight.clear();
353
+ for (const [seq, bytes] of this.aboveWatermark)
354
+ this.unverified.set(seq, bytes);
355
+ this.aboveWatermark.clear();
356
+ if (token !== undefined)
357
+ ws.send(frame(AUTH, [], new TextEncoder().encode(token)));
358
+ ws.send(frame(JOIN, c ? [c.seq, c.epoch] : [this.lastSeq, this.epoch], c ? c.prefix : this.chainHash));
359
+ this.pingTimer = setInterval(() => this.pingTick(), PING_MS);
360
+ };
361
+ ws.onmessage = (e) => {
362
+ if (ws !== this.ws)
363
+ return;
364
+ this.lastFrameAt = Date.now();
365
+ this.onFrame(new Uint8Array(e.data));
366
+ this.events.onFrame?.();
367
+ };
368
+ ws.onclose = (e) => this.onDisconnected(ws, e?.code, e?.reason);
369
+ ws.onerror = () => {
370
+ };
371
+ }
372
+ pingTick() {
373
+ const ws = this.ws;
374
+ if (!ws)
375
+ return;
376
+ const now = Date.now();
377
+ if (this.pingSentAt > this.lastFrameAt && now - this.pingSentAt >= PING_MS) {
378
+ this.events.onError?.(`ping unanswered for ${Math.round((now - this.pingSentAt) / 1000)} s: dropping the connection to reconnect`);
379
+ this.dropSocket(ws);
380
+ return;
381
+ }
382
+ if (ws.readyState === WebSocket.OPEN) {
383
+ ws.send(frame(PING));
384
+ this.pingSentAt = now;
385
+ }
386
+ }
387
+ dropSocket(ws) {
388
+ ws.onclose = null;
389
+ ws.onmessage = null;
390
+ ws.onerror = null;
391
+ ws.onopen = null;
392
+ try {
393
+ ws.close();
394
+ }
395
+ catch {
396
+ }
397
+ this.onDisconnected(ws);
398
+ }
399
+ onDisconnected(ws, code, reason) {
400
+ if (ws !== this.ws)
401
+ return;
402
+ if (this.pingTimer)
403
+ clearInterval(this.pingTimer);
404
+ this.pingTimer = undefined;
405
+ this.clearRefresh();
406
+ this.clearQuiet();
407
+ this.ws = null;
408
+ if (this.stopped)
409
+ return;
410
+ if (code === CLOSE_REJECTED) {
411
+ this.fail(`the server refused room "${this.room}" (${reason || "no reason given"}); check the room id`, code);
412
+ return;
413
+ }
414
+ if (code === CLOSE_TOKEN_REFUSED || code === CLOSE_TOKEN_FORBIDDEN || code === CLOSE_TOKEN_EXPIRED) {
415
+ this.invalidateToken();
416
+ if (this.tokenProvider === undefined) {
417
+ this.fail("this relay requires a room token; configure the host's token provider", code);
418
+ return;
419
+ }
420
+ if (typeof this.tokenProvider === "string") {
421
+ this.fail(`the static room token ${code === CLOSE_TOKEN_EXPIRED ? "expired" : "was refused"}${reason ? ` (${reason})` : ""}; supply a token provider to obtain fresh tokens`, code);
422
+ return;
423
+ }
424
+ if (code !== CLOSE_TOKEN_EXPIRED && ++this.refusals >= 2) {
425
+ this.fail(`the relay refused two room tokens without a grant (${reason || "token refused"}); the host must check the token's room, scope, signature and claims`, code);
426
+ return;
427
+ }
428
+ this.scheduleReconnect(0);
429
+ return;
430
+ }
431
+ if (reason && reason !== this.lastServerMessage) {
432
+ this.events.onError?.(`server closed the connection (${code}): ${reason}`);
433
+ this.lastServerMessage = reason;
434
+ }
435
+ this.scheduleReconnect();
436
+ }
437
+ scheduleReconnect(immediate) {
438
+ this.stats.reconnects++;
439
+ this.setStatus("reconnecting");
440
+ const delay = immediate ?? Math.round(this.backoff * (0.5 + Math.random() * 0.5));
441
+ if (immediate === undefined)
442
+ this.backoff = Math.min(10_000, this.backoff * 2);
443
+ this.reconnectTimer = setTimeout(() => { this.reconnectTimer = undefined; this.open(); }, delay);
444
+ this.statsChanged();
445
+ }
446
+ markReceived(seq, bytes) {
447
+ if (seq <= this.lastSeq)
448
+ return true;
449
+ const seen = this.unverified.get(seq);
450
+ if (seen !== undefined) {
451
+ if (!sameBytes(seen, bytes))
452
+ return false;
453
+ this.unverified.delete(seq);
454
+ }
455
+ const held = this.aboveWatermark.get(seq);
456
+ if (held !== undefined && !sameBytes(held, bytes))
457
+ return false;
458
+ if (seq === this.lastSeq + 1) {
459
+ this.aboveWatermark.delete(seq);
460
+ this.chainHash = extendPrefixHash(this.roomUtf8, seq, this.chainHash, bytes);
461
+ this.lastSeq = seq;
462
+ for (let next = this.aboveWatermark.get(this.lastSeq + 1); next !== undefined; next = this.aboveWatermark.get(this.lastSeq + 1)) {
463
+ this.aboveWatermark.delete(this.lastSeq + 1);
464
+ this.chainHash = extendPrefixHash(this.roomUtf8, this.lastSeq + 1, this.chainHash, next);
465
+ this.lastSeq++;
466
+ }
467
+ }
468
+ else
469
+ this.aboveWatermark.set(seq, bytes.slice());
470
+ this.stats.lastSeq = this.lastSeq;
471
+ return true;
472
+ }
473
+ highestSeen() {
474
+ let h = this.lastSeq;
475
+ for (const s of this.aboveWatermark.keys())
476
+ if (s > h)
477
+ h = s;
478
+ return h;
479
+ }
480
+ highestHeld() {
481
+ let h = this.highestSeen();
482
+ for (const s of this.unverified.keys())
483
+ if (s > h)
484
+ h = s;
485
+ return h;
486
+ }
487
+ witnessedLoss(why) {
488
+ this.stats.witnessResets++;
489
+ this.events.onError?.(`the log lost history this client saw (${why}): resyncing from the whole log`);
490
+ this.resetLog(this.epoch);
491
+ if (this.ws)
492
+ this.dropSocket(this.ws);
493
+ }
494
+ decideBootstrap(epoch, head, diverged) {
495
+ const c = this.candidate;
496
+ this.candidate = null;
497
+ const recovering = this.recoveryCandidate;
498
+ this.recoveryCandidate = false;
499
+ this.epoch = epoch;
500
+ this.stats.epoch = epoch.toString();
501
+ const outcome = { epoch: epoch.toString(), head, seq: c.seq };
502
+ const discard = (reason) => {
503
+ c.doc.free();
504
+ this.stats.bootstrap = "discarded";
505
+ this.events.onBootstrap?.({ kept: false, ...outcome, reason });
506
+ };
507
+ if (epoch !== c.epoch)
508
+ return discard(`the log's epoch is ${epoch}, the snapshot's ${c.epoch}: the log was reset after the snapshot was published; replaying the whole log`);
509
+ if (head < c.seq)
510
+ return discard(`the log ends at ${head}, below the snapshot's seq ${c.seq} in the same epoch (its storage was rolled back); replaying the whole log`);
511
+ if (diverged)
512
+ return discard(`DIVERGED: the log's history through ${c.seq} is not the snapshot's (its storage was rolled back and regrew past the seq); replaying the whole log`);
513
+ const started = performance.now();
514
+ const snapshotVersion = c.doc.oplogVersion().toJSON();
515
+ if (recovering && this.page.doc.oplogFrontiers().length > 0) {
516
+ try {
517
+ this.page.import(c.doc.export({ mode: "snapshot" }));
518
+ }
519
+ catch (error) {
520
+ c.doc.free();
521
+ this.fail(`the required snapshot could not be imported (${String(error)}); reload the page or contact the operator`);
522
+ return;
523
+ }
524
+ c.doc.free();
525
+ this.lastSeq = c.seq;
526
+ this.stats.lastSeq = c.seq;
527
+ this.chainHash = c.prefix.slice();
528
+ for (const [peer, end] of snapshotVersion)
529
+ this.logged.add(peer, 0, end);
530
+ this.noteState();
531
+ if (this.claim && this.claimCovered())
532
+ this.settleClaim("created");
533
+ for (let i = this.outbox.length - 1; i >= 0; i--) {
534
+ const pending = this.outbox[i];
535
+ if (pending.spans === undefined)
536
+ pending.spans = spansOf(pending.bytes);
537
+ if (pending.spans?.every((span) => this.logged.covers(span.peer, span.start, span.end)))
538
+ this.settle(i);
539
+ }
540
+ this.stats.bootstrap = "kept";
541
+ this.events.onBootstrap?.({ kept: true, ...outcome, adoptMs: performance.now() - started });
542
+ return;
543
+ }
544
+ this.unsubscribe();
545
+ let error = null;
546
+ try {
547
+ this.page.adoptSnapshot(c.doc);
548
+ }
549
+ catch (err) {
550
+ error = String(err);
551
+ }
552
+ this.unsubscribe = this.page.doc.subscribeLocalUpdates((bytes) => this.enqueue(bytes));
553
+ if (error !== null && this.page.doc !== c.doc) {
554
+ discard(`the page could not adopt the snapshot (${error}); rejoining to replay the whole log`);
555
+ if (this.ws)
556
+ this.dropSocket(this.ws);
557
+ return;
558
+ }
559
+ if (error !== null)
560
+ this.events.onError?.(`the page adopted the snapshot, then threw: ${error}`);
561
+ this.lastSeq = c.seq;
562
+ this.stats.lastSeq = c.seq;
563
+ this.chainHash = c.prefix.slice();
564
+ for (const [peer, end] of snapshotVersion)
565
+ this.logged.add(peer, 0, end);
566
+ this.noteState();
567
+ this.stats.bootstrap = "kept";
568
+ this.events.onBootstrap?.({ kept: true, ...outcome, adoptMs: performance.now() - started });
569
+ }
570
+ resetLog(epoch) {
571
+ this.inflight.clear();
572
+ this.lastSeq = 0;
573
+ this.aboveWatermark.clear();
574
+ this.unverified.clear();
575
+ this.chainHash = emptyPrefixHash(this.roomUtf8);
576
+ this.logged.clear();
577
+ this.ownSeqs.clear();
578
+ this.stats.lastSeq = 0;
579
+ this.stats.resets++;
580
+ this.sentCount = 0;
581
+ this.clearQuiet();
582
+ this.resetToReport = { epoch: epoch.toString() };
583
+ if (this.claim) {
584
+ this.outbox = [];
585
+ this.historyEnd = this.ownCounter();
586
+ if (this.claimRefused) {
587
+ this.fullPushPending = false;
588
+ this.endArbitration();
589
+ this.settleClaim("conflict");
590
+ return;
591
+ }
592
+ }
593
+ else
594
+ this.outbox = this.keepOwnCommits();
595
+ this.fullPushPending = true;
596
+ this.rebuildRound = 0;
597
+ this.holdReadOnlyHistory();
598
+ }
599
+ async resync(epoch, head, floor) {
600
+ this.stats.resyncs++;
601
+ if (++this.resyncAttempts > MAX_RESYNC_ATTEMPTS) {
602
+ this.fail(`the server repeatedly requires snapshot resync for room "${this.room}"; no stable snapshot could be joined after ${MAX_RESYNC_ATTEMPTS} attempts; reload the page or contact the operator`);
603
+ return;
604
+ }
605
+ const old = this.candidate;
606
+ this.candidate = null;
607
+ this.recoveryCandidate = false;
608
+ if (old) {
609
+ old.doc.free();
610
+ this.stats.bootstrap = "discarded";
611
+ this.events.onBootstrap?.({ kept: false, epoch: epoch.toString(), head, seq: old.seq, reason: `the relay requires a snapshot at or above retained floor ${floor}; fetching the latest publication` });
612
+ }
613
+ if (this.page.doc.oplogFrontiers().length > 0)
614
+ this.resetLog(epoch);
615
+ this.epoch = epoch;
616
+ this.stats.epoch = epoch.toString();
617
+ this.clearRefresh();
618
+ const ws = this.ws;
619
+ this.ws = null;
620
+ if (ws) {
621
+ ws.onopen = ws.onmessage = ws.onclose = ws.onerror = null;
622
+ try {
623
+ ws.close();
624
+ }
625
+ catch { }
626
+ }
627
+ if (this.pingTimer)
628
+ clearInterval(this.pingTimer);
629
+ this.pingTimer = undefined;
630
+ if (this.reconnectTimer)
631
+ clearTimeout(this.reconnectTimer);
632
+ this.reconnectTimer = undefined;
633
+ this.stats.reconnects++;
634
+ this.setStatus("reconnecting");
635
+ this.statsChanged();
636
+ const abort = new AbortController();
637
+ this.resyncAbort = abort;
638
+ const deadline = this.resyncDeadline ??= Date.now() + RESYNC_RECOVERY_MS;
639
+ const deadlineTimer = setTimeout(() => abort.abort(new Error("required snapshot recovery exceeded 10 minutes")), Math.max(0, deadline - Date.now()));
640
+ const reportRetry = (reason) => {
641
+ const message = `the required snapshot is temporarily unavailable (${reason}); keeping local edits and retrying`;
642
+ if (message !== this.lastServerMessage)
643
+ this.events.onError?.(message);
644
+ this.lastServerMessage = message;
645
+ };
646
+ try {
647
+ for (;;) {
648
+ if (Date.now() >= deadline)
649
+ throw new Error("required snapshot recovery exceeded 10 minutes");
650
+ const result = await fetchSnapshot(this.url, this.room, {
651
+ signal: abort.signal, cache: "reload", maxRetries: 20,
652
+ ...(this.tokenProvider === undefined ? {} : { token: () => this.roomToken() }),
653
+ maxWaitMs: Math.max(0, Math.min(RESYNC_FETCH_WAIT_MS, deadline - Date.now())),
654
+ onRetry: ({ status }) => reportRetry(`the server is busy (${status})`),
655
+ });
656
+ if (this.stopped || abort.signal.aborted) {
657
+ result.snapshot?.doc.free();
658
+ if (abort.signal.aborted)
659
+ throw abort.signal.reason;
660
+ return;
661
+ }
662
+ if (!result.snapshot) {
663
+ if (!result.retryable) {
664
+ this.fail(`room "${this.room}" requires a snapshot because its log was trimmed, but ${result.reason}; reload the page or contact the operator`);
665
+ return;
666
+ }
667
+ reportRetry(result.reason ?? "temporary download failure");
668
+ const delay = Math.max(this.backoff, result.retryAfterMs);
669
+ this.backoff = Math.min(10_000, this.backoff * 2);
670
+ this.stats.reconnects++;
671
+ this.statsChanged();
672
+ await new Promise((resolve, reject) => {
673
+ if (abort.signal.aborted) {
674
+ reject(abort.signal.reason);
675
+ return;
676
+ }
677
+ const onAbort = () => {
678
+ clearTimeout(this.reconnectTimer);
679
+ this.reconnectTimer = undefined;
680
+ reject(abort.signal.reason);
681
+ };
682
+ this.reconnectTimer = setTimeout(() => {
683
+ this.reconnectTimer = undefined;
684
+ abort.signal.removeEventListener("abort", onAbort);
685
+ resolve();
686
+ }, Math.min(delay, Math.max(0, deadline - Date.now())));
687
+ abort.signal.addEventListener("abort", onAbort, { once: true });
688
+ });
689
+ continue;
690
+ }
691
+ const candidate = result.snapshot;
692
+ if (candidate.epoch === epoch && candidate.seq < floor) {
693
+ candidate.doc.free();
694
+ this.fail(`the latest snapshot for room "${this.room}" is below retained floor ${floor}; contact the operator`);
695
+ return;
696
+ }
697
+ this.candidate = candidate;
698
+ this.recoveryCandidate = true;
699
+ this.stats.bootstrap = "pending";
700
+ this.open();
701
+ return;
702
+ }
703
+ }
704
+ catch (error) {
705
+ if (!this.stopped)
706
+ this.fail(`snapshot resync failed (${String(error)}); reload the page or contact the operator`);
707
+ }
708
+ finally {
709
+ clearTimeout(deadlineTimer);
710
+ if (this.resyncAbort === abort)
711
+ this.resyncAbort = null;
712
+ }
713
+ }
714
+ keepOwnCommits() {
715
+ const own = this.page.doc.peerIdStr;
716
+ let end = this.ownCounter();
717
+ const kept = [];
718
+ for (let i = this.outbox.length - 1; i >= 0; i--) {
719
+ const o = this.outbox[i];
720
+ if (!isCommit(o))
721
+ continue;
722
+ if (o.spans === undefined)
723
+ o.spans = spansOf(o.bytes);
724
+ const span = o.spans?.find((s) => s.peer === own);
725
+ if (!span || o.spans.length !== 1 || span.end !== end)
726
+ break;
727
+ kept.unshift(o);
728
+ end = span.start;
729
+ }
730
+ this.historyEnd = end;
731
+ return kept;
732
+ }
733
+ endArbitration() {
734
+ this.rebuildRound = null;
735
+ this.ownSeqs.clear();
736
+ }
737
+ settleClaim(result) {
738
+ const c = this.claim;
739
+ this.claim = null;
740
+ this.claimSpans = null;
741
+ if (result === "conflict" && this.claimRefused)
742
+ this.stats.rejected++;
743
+ this.claimRefused = false;
744
+ c?.resolve(result);
745
+ }
746
+ claimCovered() {
747
+ return this.claimSpans !== null && this.claimSpans.every((s) => this.logged.covers(s.peer, s.start, s.end));
748
+ }
749
+ settle(i) {
750
+ const [o] = this.outbox.splice(i, 1);
751
+ if (i < this.sentCount)
752
+ this.sentCount--;
753
+ if (o.spans === undefined)
754
+ o.spans = spansOf(o.bytes);
755
+ for (const s of o.spans ?? [])
756
+ this.logged.add(s.peer, s.start, s.end);
757
+ if (o.ifEmpty)
758
+ this.settleClaim("created");
759
+ this.stats.acked++;
760
+ return o;
761
+ }
762
+ noteLogged(seq, bytes, held) {
763
+ const spans = spansOf(bytes);
764
+ if (!spans)
765
+ return { settled: false, progress: false };
766
+ let progress = false;
767
+ for (const s of spans) {
768
+ const end = Math.min(s.end, held?.get(s.peer) ?? 0);
769
+ if (end > s.start && !this.logged.covers(s.peer, s.start, end))
770
+ progress = true;
771
+ this.logged.add(s.peer, s.start, s.end);
772
+ }
773
+ if (this.claim && this.claimCovered())
774
+ this.settleClaim("created");
775
+ let settled = false;
776
+ for (let i = this.outbox.length - 1; i >= 0; i--) {
777
+ const o = this.outbox[i];
778
+ if (o.spans === undefined)
779
+ o.spans = spansOf(o.bytes);
780
+ if (!o.spans || !o.spans.every((s) => this.logged.covers(s.peer, s.start, s.end)))
781
+ continue;
782
+ if (this.rebuildRound !== null && o.spans.length === spans.length && o.spans.every((s) => spans.some((x) => x.peer === s.peer && x.start === s.start && x.end === s.end)))
783
+ this.ownSeqs.add(seq);
784
+ const done = this.settle(i);
785
+ if (done.probe || (done.kind === "slice" && this.outbox.some((x) => x.kind === "slice"))) {
786
+ this.follow();
787
+ return { settled: false, progress };
788
+ }
789
+ settled = true;
790
+ }
791
+ return { settled, progress };
792
+ }
793
+ probeLeads(seq, probe) {
794
+ const floor = this.rebuildRound === 0 ? 0 : (probe.probeFloor ?? 0);
795
+ let own = 0;
796
+ for (const s of this.ownSeqs)
797
+ if (s > floor && s < seq)
798
+ own++;
799
+ return seq === floor + 1 + own;
800
+ }
801
+ onFrame(f) {
802
+ const view = new DataView(f.buffer, f.byteOffset, f.byteLength);
803
+ switch (f[0]) {
804
+ case PRESENCE: {
805
+ if (f.byteLength < 12)
806
+ return;
807
+ const subjectLength = view.getUint16(9);
808
+ if (subjectLength > 256 || f.byteLength <= 11 + subjectLength || f.byteLength > 11 + subjectLength + MAX_PRESENCE_BYTES)
809
+ return;
810
+ try {
811
+ const subject = new TextDecoder("utf-8", { fatal: true }).decode(f.subarray(11, 11 + subjectLength));
812
+ this.presence.receive(view.getBigUint64(1).toString(), subject || null, f.subarray(11 + subjectLength));
813
+ }
814
+ catch { }
815
+ return;
816
+ }
817
+ case PRESENCE_LEAVE:
818
+ if (f.byteLength === 9)
819
+ this.presence.leave(view.getBigUint64(1).toString());
820
+ return;
821
+ case GRANT: {
822
+ if (f.byteLength !== 6) {
823
+ this.fail(`invalid GRANT frame length ${f.byteLength}; expected exactly 6 bytes`);
824
+ return;
825
+ }
826
+ const role = f[1] === 2 ? "write" : "read";
827
+ const expiresIn = view.getUint32(2);
828
+ this.refusals = 0;
829
+ this.stats.role = role;
830
+ this.clearRefresh();
831
+ const refreshIn = expiresIn - Math.min(expiresIn / 4, 120);
832
+ this.tokenReusableUntil = expiresIn === 0 ? Infinity : performance.now() + refreshIn * 1000;
833
+ if (expiresIn > 0 && this.tokenProvider !== undefined) {
834
+ this.refreshTimer = setTimeout(() => {
835
+ this.refreshTimer = undefined;
836
+ if (typeof this.tokenProvider === "string")
837
+ this.fail("the static room token expired; supply a token provider to obtain fresh tokens", CLOSE_TOKEN_EXPIRED);
838
+ else
839
+ void this.refresh();
840
+ }, (typeof this.tokenProvider === "string" ? expiresIn : refreshIn) * 1000);
841
+ }
842
+ if (role === "read")
843
+ this.holdReadOnlyHistory();
844
+ else {
845
+ this.heldReported = false;
846
+ if (this.readOnlyHistoryPending) {
847
+ this.readOnlyHistoryPending = false;
848
+ this.fullPushPending = true;
849
+ if (this.status === "live" && !this.repush(this.rebuildRound !== null && this.rebuildRound < 2))
850
+ return;
851
+ }
852
+ }
853
+ this.events.onGrant?.({ role, expiresIn });
854
+ this.statsChanged();
855
+ this.pump();
856
+ break;
857
+ }
858
+ case RESYNC: {
859
+ if (f.byteLength !== 25) {
860
+ this.fail(`invalid RESYNC frame length ${f.byteLength}; expected exactly 25 bytes`);
861
+ return;
862
+ }
863
+ const epoch = view.getBigUint64(1);
864
+ const head = Number(view.getBigUint64(9));
865
+ const floor = Number(view.getBigUint64(17));
866
+ if (epoch === 0n || !Number.isSafeInteger(head) || !Number.isSafeInteger(floor) || floor <= 0 || floor > head) {
867
+ this.fail("invalid RESYNC epoch, head or retained floor");
868
+ return;
869
+ }
870
+ void this.resync(epoch, head, floor);
871
+ break;
872
+ }
873
+ case EPOCH: {
874
+ const epoch = view.getBigUint64(1);
875
+ const head = Number(view.getBigUint64(9));
876
+ if (f.byteLength >= 25) {
877
+ this.maxUpdate = Number(view.getBigUint64(17));
878
+ this.stats.maxUpdate = this.maxUpdate;
879
+ }
880
+ this.maxUpdateHigh = Math.max(this.maxUpdateHigh, this.maxUpdate);
881
+ const diverged = f.byteLength >= 26 && (f[25] & EPOCH_DIVERGED) !== 0;
882
+ if (diverged)
883
+ this.stats.diverged++;
884
+ if (this.candidate)
885
+ this.decideBootstrap(epoch, head, diverged);
886
+ else if ((this.epoch !== 0n && epoch !== this.epoch) || head < this.lastSeq || diverged)
887
+ this.resetLog(epoch);
888
+ else if (head < this.highestHeld()) {
889
+ this.stats.witnessResets++;
890
+ this.resetLog(epoch);
891
+ this.epoch = epoch;
892
+ this.stats.epoch = epoch.toString();
893
+ if (this.ws)
894
+ this.dropSocket(this.ws);
895
+ this.statsChanged();
896
+ break;
897
+ }
898
+ this.epoch = epoch;
899
+ this.stats.epoch = epoch.toString();
900
+ this.statsChanged();
901
+ break;
902
+ }
903
+ case ENTRY: {
904
+ const seq = Number(view.getBigUint64(1));
905
+ const bytes = f.subarray(9);
906
+ const held = this.quietTimer ? this.page.doc.version().toJSON() : null;
907
+ try {
908
+ this.page.import(bytes);
909
+ }
910
+ catch (err) {
911
+ this.fail(`log entry ${seq} of room "${this.room}" cannot be imported (${String(err)}); the room's log is corrupt from that entry on: reload the page, and if this repeats contact the operator (the log needs repair)`);
912
+ return;
913
+ }
914
+ if (!this.markReceived(seq, bytes)) {
915
+ this.witnessedLoss(`entry ${seq} is not the one this client saw at that seq`);
916
+ this.statsChanged();
917
+ return;
918
+ }
919
+ this.stats.received++;
920
+ this.largestEntry = Math.max(this.largestEntry, bytes.byteLength);
921
+ this.noteState();
922
+ const claimed = this.claim !== null;
923
+ const { settled, progress } = this.noteLogged(seq, bytes, held);
924
+ if (this.quietTimer && this.logged.coversVersion(this.page.doc.version().toJSON()))
925
+ this.finishFollowing();
926
+ else if (this.quietTimer && progress)
927
+ this.extendQuiet();
928
+ if (settled || (claimed && !this.claim))
929
+ this.pump();
930
+ this.statsChanged();
931
+ break;
932
+ }
933
+ case ACK: {
934
+ const seq = Number(view.getBigUint64(1));
935
+ if (f.byteLength >= 17) {
936
+ const cseq = Number(view.getBigUint64(9));
937
+ const sentBytes = this.inflight.get(cseq);
938
+ this.inflight.delete(cseq);
939
+ if (sentBytes && !this.markReceived(seq, sentBytes)) {
940
+ this.witnessedLoss(`its update ${cseq} was acked at ${seq}, where it held other bytes`);
941
+ this.statsChanged();
942
+ return;
943
+ }
944
+ const i = this.outbox.findIndex((o) => o.cseq === cseq);
945
+ if (i < 0) {
946
+ this.stats.stale++;
947
+ }
948
+ else {
949
+ const o = this.settle(i);
950
+ if (this.rebuildRound !== null)
951
+ this.ownSeqs.add(seq);
952
+ if (o.probe) {
953
+ if (this.probeLeads(seq, o))
954
+ this.endArbitration();
955
+ else
956
+ this.follow();
957
+ }
958
+ else if (this.quietTimer && this.logged.coversVersion(this.page.doc.version().toJSON())) {
959
+ this.finishFollowing();
960
+ }
961
+ }
962
+ }
963
+ else
964
+ this.stats.acked++;
965
+ this.appendRetrying = false;
966
+ this.lastServerMessage = null;
967
+ this.backoff = 500;
968
+ this.statsChanged();
969
+ this.pump();
970
+ break;
971
+ }
972
+ case NACK: {
973
+ const cseq = Number(view.getBigUint64(1));
974
+ const reason = f[9] ?? 0;
975
+ const msg = new TextDecoder().decode(f.subarray(10));
976
+ this.inflight.delete(cseq);
977
+ const i = this.outbox.findIndex((o) => o.cseq === cseq);
978
+ if (i < 0) {
979
+ this.stats.stale++;
980
+ const why = cseq > 0 && cseq < this.nextCseq ? "the update is no longer pending: settled by the log already, or dropped at a log reset or in favour of another client's re-push" : "this transport never sent an update with that sequence";
981
+ this.events.onError?.(`stale answer for update ${cseq} (NACK ${NACK_NAMES[reason] ?? `reason ${reason}`}${msg ? `: ${msg}` : ""}): ${why}; ignored`);
982
+ this.statsChanged();
983
+ break;
984
+ }
985
+ const o = this.outbox[i];
986
+ if (reason === NACK_READ_ONLY) {
987
+ this.stats.role = "read";
988
+ this.outbox.splice(i, 1);
989
+ if (i < this.sentCount)
990
+ this.sentCount--;
991
+ let heldEnd = this.sentCount;
992
+ while (this.outbox[heldEnd]?.readOnlyHeld)
993
+ heldEnd++;
994
+ o.readOnlyHeld = true;
995
+ this.outbox.splice(heldEnd, 0, o);
996
+ this.stats.rejected++;
997
+ this.holdReadOnlyHistory();
998
+ this.reportHeld();
999
+ this.statsChanged();
1000
+ break;
1001
+ }
1002
+ if (o.ifEmpty && reason === NACK_NOT_EMPTY) {
1003
+ if (this.claim && isCommit(o)) {
1004
+ this.outbox = [];
1005
+ this.sentCount = 0;
1006
+ this.endArbitration();
1007
+ this.claimRefused = true;
1008
+ this.settleClaim("conflict");
1009
+ this.statsChanged();
1010
+ }
1011
+ else {
1012
+ this.claimRefused = this.claim !== null;
1013
+ this.follow();
1014
+ }
1015
+ break;
1016
+ }
1017
+ this.stats.rejected++;
1018
+ if (reason === NACK_APPEND_FAILED) {
1019
+ if (!this.appendRetrying)
1020
+ this.events.onError?.(`the server could not persist update ${cseq}${msg ? ` (${msg})` : ""}: keeping it and retrying with backoff; if this persists, the operator must fix the server's storage`);
1021
+ this.appendRetrying = true;
1022
+ this.statsChanged();
1023
+ if (this.ws)
1024
+ this.dropSocket(this.ws);
1025
+ return;
1026
+ }
1027
+ const size = `${o.bytes.byteLength} bytes`;
1028
+ const advice = reason === NACK_TOO_LARGE
1029
+ ? `${o.kind === "slice" ? "a slice of the history of" : "one commit of"} ${size} exceeds the server's MAX_UPDATE_BYTES (${this.maxUpdate}); reload the page to resync (that change is lost) or have the operator raise the limit`
1030
+ : reason === NACK_NOT_LORO
1031
+ ? "the client sent bytes that are not a Loro update, which is a client bug; reload the page"
1032
+ : "reload the page";
1033
+ this.fail(`server rejected update ${cseq}${msg ? ` (${msg})` : ""}: ${advice}`);
1034
+ return;
1035
+ }
1036
+ case CAUGHT: {
1037
+ const head = Number(view.getBigUint64(1));
1038
+ this.resyncAttempts = 0;
1039
+ this.resyncDeadline = null;
1040
+ if (!this.appendRetrying)
1041
+ this.backoff = 500;
1042
+ this.lastServerMessage = null;
1043
+ this.setStatus("live");
1044
+ this.sentCount = 0;
1045
+ this.clearQuiet();
1046
+ if (this.stats.role === "read")
1047
+ this.holdReadOnlyHistory();
1048
+ if (this.stats.role === "write" && this.fullPushPending && !this.repush(this.rebuildRound !== null && this.rebuildRound < 2))
1049
+ return;
1050
+ if (this.resetToReport) {
1051
+ const { epoch } = this.resetToReport;
1052
+ this.resetToReport = null;
1053
+ this.events.onLogReset?.({ epoch, head, queued: this.outbox.filter((o) => o.kind === "slice").length });
1054
+ }
1055
+ this.statsChanged();
1056
+ this.pump();
1057
+ this.presence.setConnected(true);
1058
+ for (const w of this.readyWaiters.splice(0))
1059
+ w.resolve();
1060
+ break;
1061
+ }
1062
+ case PONG:
1063
+ break;
1064
+ case ERROR: {
1065
+ const msg = new TextDecoder().decode(f.subarray(1));
1066
+ if (msg !== this.lastServerMessage)
1067
+ this.events.onError?.(msg);
1068
+ this.lastServerMessage = msg;
1069
+ if (msg.startsWith("lagged"))
1070
+ this.ws?.close();
1071
+ break;
1072
+ }
1073
+ }
1074
+ }
1075
+ repush(probe) {
1076
+ if (this.stats.role !== "write") {
1077
+ this.holdReadOnlyHistory();
1078
+ return true;
1079
+ }
1080
+ this.fullPushPending = false;
1081
+ if (this.claim) {
1082
+ if (this.claimCovered())
1083
+ this.settleClaim("created");
1084
+ else if (this.claimRefused) {
1085
+ this.outbox = [];
1086
+ this.sentCount = 0;
1087
+ this.endArbitration();
1088
+ this.settleClaim("conflict");
1089
+ return true;
1090
+ }
1091
+ }
1092
+ const slices = exportInSlices(this.page.doc, Math.min(this.maxUpdate, REPUSH_SLICE_BYTES), {
1093
+ safeEnds: this.safeEnds,
1094
+ from: this.logged.prefix(),
1095
+ until: new Map([[this.page.doc.peerIdStr, this.historyEnd]]),
1096
+ ...(probe ? { firstSliceBytes: PROBE_BYTES } : {}),
1097
+ });
1098
+ const over = slices.find((b) => b.byteLength > this.maxUpdate);
1099
+ if (over) {
1100
+ const lowered = this.maxUpdateHigh > this.maxUpdate
1101
+ ? ` The limit was ${this.maxUpdateHigh} when this page synced: MAX_UPDATE_BYTES was lowered on a live deployment, and histories cut for the old limit cannot be re-cut for the new one (see server/README.md).`
1102
+ : this.largestEntry > this.maxUpdate
1103
+ ? ` The log replayed entries of up to ${this.largestEntry} bytes, above the current limit: MAX_UPDATE_BYTES was lowered after they were written, and this page's history was cut for the old limit (see server/README.md).`
1104
+ : "";
1105
+ this.fail(`the page's history has a stretch of ${over.byteLength} bytes (one object's data, or edits with no object creation between them) that cannot be cut below the server's MAX_UPDATE_BYTES (${this.maxUpdate}) and cannot be synced; nothing was sent; have the operator raise the limit, then reload the page.${lowered}`);
1106
+ return false;
1107
+ }
1108
+ if (slices.length === 0 && this.claim)
1109
+ this.settleClaim("created");
1110
+ const arbitrate = probe && slices.length > 0;
1111
+ if (!arbitrate)
1112
+ this.endArbitration();
1113
+ const sent = this.outbox.slice(0, this.sentCount).filter(isCommit);
1114
+ const unsent = this.outbox.slice(this.sentCount).filter(isCommit);
1115
+ const queued = slices.map((bytes, i) => ({ cseq: this.nextCseq++, bytes, ifEmpty: i === 0 && this.claim !== null, kind: "slice", probe: i === 0 && arbitrate }));
1116
+ this.outbox = this.claim ? [...sent, ...queued, ...unsent] : [...sent, ...unsent, ...queued];
1117
+ this.sentCount = sent.length;
1118
+ return true;
1119
+ }
1120
+ follow() {
1121
+ this.sentCount = this.outbox.slice(0, this.sentCount).filter(isCommit).length;
1122
+ this.outbox = this.outbox.filter(isCommit);
1123
+ this.fullPushPending = true;
1124
+ this.rebuildRound = (this.rebuildRound ?? 0) + 1;
1125
+ this.quietExtensions = 0;
1126
+ this.quietGraced = false;
1127
+ if (this.stats.role === "read")
1128
+ this.holdReadOnlyHistory();
1129
+ else
1130
+ this.armQuiet();
1131
+ this.statsChanged();
1132
+ this.pump();
1133
+ }
1134
+ armQuiet() {
1135
+ this.clearQuiet();
1136
+ const armedAt = Date.now();
1137
+ this.quietTimer = setTimeout(() => {
1138
+ this.quietTimer = undefined;
1139
+ if (!this.quietGraced && Date.now() - armedAt > REBUILD_WAIT_MS + STALL_SLACK_MS) {
1140
+ this.quietGraced = true;
1141
+ this.armQuiet();
1142
+ return;
1143
+ }
1144
+ this.finishFollowing();
1145
+ }, REBUILD_WAIT_MS);
1146
+ }
1147
+ extendQuiet() {
1148
+ this.quietExtensions++;
1149
+ this.quietGraced = false;
1150
+ this.armQuiet();
1151
+ }
1152
+ clearQuiet() {
1153
+ if (this.quietTimer)
1154
+ clearTimeout(this.quietTimer);
1155
+ this.quietTimer = undefined;
1156
+ }
1157
+ finishFollowing() {
1158
+ this.clearQuiet();
1159
+ if (this.status !== "live" || !this.fullPushPending)
1160
+ return;
1161
+ if (!this.repush(this.rebuildRound !== null && this.rebuildRound < 2))
1162
+ return;
1163
+ this.statsChanged();
1164
+ this.pump();
1165
+ }
1166
+ pump() {
1167
+ const ws = this.ws;
1168
+ if (this.stats.role !== "write") {
1169
+ this.reportHeld();
1170
+ return;
1171
+ }
1172
+ if (this.status !== "live" || !ws || ws.readyState !== WebSocket.OPEN)
1173
+ return;
1174
+ const claimAt = this.claim ? this.outbox.findIndex((o) => o.ifEmpty) : -1;
1175
+ let slicesInFlight = this.outbox.slice(0, this.sentCount).filter((o) => o.kind === "slice").length;
1176
+ let sentAny = false;
1177
+ while (this.sentCount < this.outbox.length) {
1178
+ const last = this.sentCount > 0 ? this.outbox[this.sentCount - 1] : null;
1179
+ if (last && (last.ifEmpty || last.probe))
1180
+ break;
1181
+ if (this.claim && (claimAt < 0 || this.sentCount > claimAt))
1182
+ break;
1183
+ const o = this.outbox[this.sentCount];
1184
+ if (o.kind === "slice" && slicesInFlight >= SLICE_WINDOW)
1185
+ break;
1186
+ if (o.kind === "slice")
1187
+ slicesInFlight++;
1188
+ if (o.bytes.byteLength > this.maxUpdate) {
1189
+ this.fail(`${o.kind === "slice" ? "a slice of the history of" : "one commit of"} ${o.bytes.byteLength} bytes exceeds the server's MAX_UPDATE_BYTES (${this.maxUpdate}) and cannot be synced; reload the page to resync (that change is lost) or have the operator raise the limit`);
1190
+ return;
1191
+ }
1192
+ if (o.probe)
1193
+ o.probeFloor = this.highestSeen();
1194
+ this.inflight.set(o.cseq, o.bytes);
1195
+ delete o.readOnlyHeld;
1196
+ ws.send(updateFrame(o));
1197
+ this.sentCount++;
1198
+ this.stats.sent++;
1199
+ sentAny = true;
1200
+ if (o.ifEmpty || o.probe)
1201
+ break;
1202
+ }
1203
+ if (sentAny)
1204
+ this.statsChanged();
1205
+ }
1206
+ invalidateToken() {
1207
+ this.tokenGeneration++;
1208
+ this.cachedToken = undefined;
1209
+ this.tokenReusableUntil = 0;
1210
+ this.tokenRequest = null;
1211
+ }
1212
+ clearRefresh() {
1213
+ clearTimeout(this.refreshTimer);
1214
+ this.refreshTimer = undefined;
1215
+ }
1216
+ async refresh() {
1217
+ const ws = this.ws;
1218
+ if (this.stopped || !ws || ws.readyState !== WebSocket.OPEN)
1219
+ return;
1220
+ try {
1221
+ const token = await this.roomToken(true);
1222
+ if (!this.stopped && this.ws === ws && ws.readyState === WebSocket.OPEN)
1223
+ ws.send(frame(AUTH, [], new TextEncoder().encode(token)));
1224
+ }
1225
+ catch {
1226
+ if (this.stopped || this.ws !== ws)
1227
+ return;
1228
+ this.providerFailure();
1229
+ this.refreshTimer = setTimeout(() => { this.refreshTimer = undefined; void this.refresh(); }, 10_000);
1230
+ }
1231
+ }
1232
+ holdReadOnlyHistory() {
1233
+ if (this.stats.role !== "read")
1234
+ return;
1235
+ this.readOnlyHistoryPending ||= this.fullPushPending;
1236
+ this.fullPushPending = false;
1237
+ this.clearQuiet();
1238
+ }
1239
+ reportHeld() {
1240
+ if (this.stats.role === "read" && this.outbox.length > 0 && !this.heldReported) {
1241
+ this.events.onError?.(`read-only: ${this.outbox.length} local change${this.outbox.length === 1 ? " is" : "s are"} held until the host grants write access`);
1242
+ this.heldReported = true;
1243
+ }
1244
+ }
1245
+ enqueue(bytes) {
1246
+ if (this.stopped)
1247
+ return;
1248
+ this.noteState();
1249
+ const ifEmpty = this.claiming;
1250
+ this.claiming = false;
1251
+ if (ifEmpty)
1252
+ this.claimSpans = spansOf(bytes);
1253
+ this.outbox.push({ cseq: this.nextCseq++, bytes, ifEmpty, kind: "commit" });
1254
+ this.statsChanged();
1255
+ this.pump();
1256
+ }
1257
+ fail(message, closeCode, code) {
1258
+ if (this.stopped)
1259
+ return;
1260
+ this.failure = message;
1261
+ this.stop();
1262
+ this.events.onError?.(message);
1263
+ this.events.onFailure?.({ message, ...(closeCode === undefined ? {} : { closeCode }), ...(code === undefined ? {} : { code }) });
1264
+ this.setStatus("failed");
1265
+ const err = new Error(message);
1266
+ for (const w of this.readyWaiters.splice(0))
1267
+ w.reject(err);
1268
+ const c = this.claim;
1269
+ this.claim = null;
1270
+ c?.reject(err);
1271
+ }
1272
+ stop() {
1273
+ this.stopped = true;
1274
+ this.presence.dispose();
1275
+ this.openGeneration++;
1276
+ this.clearRefresh();
1277
+ this.invalidateToken();
1278
+ this.resyncAbort?.abort(new Error("transport stopped"));
1279
+ this.resyncAbort = null;
1280
+ this.unsubscribe();
1281
+ if (this.pingTimer)
1282
+ clearInterval(this.pingTimer);
1283
+ if (this.reconnectTimer)
1284
+ clearTimeout(this.reconnectTimer);
1285
+ this.clearQuiet();
1286
+ this.candidate?.doc.free();
1287
+ this.candidate = null;
1288
+ const ws = this.ws;
1289
+ this.ws = null;
1290
+ if (ws) {
1291
+ ws.onclose = null;
1292
+ ws.onmessage = null;
1293
+ ws.onerror = null;
1294
+ ws.onopen = null;
1295
+ try {
1296
+ ws.close();
1297
+ }
1298
+ catch {
1299
+ }
1300
+ }
1301
+ }
1302
+ close() {
1303
+ if (this.stopped)
1304
+ return;
1305
+ this.stop();
1306
+ this.setStatus("closed");
1307
+ const err = new Error("transport closed");
1308
+ for (const w of this.readyWaiters.splice(0))
1309
+ w.reject(err);
1310
+ const c = this.claim;
1311
+ this.claim = null;
1312
+ c?.reject(err);
1313
+ }
1314
+ }