@loro-dev/streams-crdt 0.7.1 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -0
- package/dist/flock.d.ts +2 -2
- package/dist/flock.js +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/loro.d.ts +25 -2
- package/dist/loro.js +37 -2
- package/dist/loro.js.map +1 -1
- package/dist/{stream-id-CS7SzuLW.d.ts → stream-id-B6FzWgkM.d.ts} +106 -2
- package/dist/{stream-id-D0qcbI5c.js → stream-id-CCPEBb6G.js} +1011 -400
- package/dist/stream-id-CCPEBb6G.js.map +1 -0
- package/package.json +4 -2
- package/dist/stream-id-D0qcbI5c.js.map +0 -1
|
@@ -1,211 +1,4 @@
|
|
|
1
|
-
import { StreamsClient } from "@loro-dev/streams-client";
|
|
2
|
-
//#region src/checkpoint.ts
|
|
3
|
-
function composeBeforeRemoteCursorSaveHooks(...hooks) {
|
|
4
|
-
const active = hooks.filter((hook) => hook != null);
|
|
5
|
-
if (active.length === 0) return;
|
|
6
|
-
return async (context) => {
|
|
7
|
-
for (const hook of active) await hook(context);
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
async function persistRemoteCursor(input) {
|
|
11
|
-
const next = {
|
|
12
|
-
...input.cursor,
|
|
13
|
-
updatedAtMs: (input.now ?? Date.now)()
|
|
14
|
-
};
|
|
15
|
-
if (input.beforeRemoteCursorSave != null) await input.beforeRemoteCursorSave({
|
|
16
|
-
cursor: next,
|
|
17
|
-
source: input.source
|
|
18
|
-
});
|
|
19
|
-
await input.remoteCursorStore.save(next);
|
|
20
|
-
return next;
|
|
21
|
-
}
|
|
22
|
-
function key(streamUrl) {
|
|
23
|
-
return streamUrl;
|
|
24
|
-
}
|
|
25
|
-
function isJsonObject(value) {
|
|
26
|
-
return typeof value === "object" && value != null && !Array.isArray(value);
|
|
27
|
-
}
|
|
28
|
-
function cloneJsonValue(value) {
|
|
29
|
-
return structuredClone(value);
|
|
30
|
-
}
|
|
31
|
-
function cloneRemoteCursor(cursor) {
|
|
32
|
-
return {
|
|
33
|
-
...cursor,
|
|
34
|
-
serverLowerBoundVersion: cloneJsonValue(cursor.serverLowerBoundVersion)
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
function fromStoredRemoteCursor(record) {
|
|
38
|
-
if (!isJsonObject(record.serverLowerBoundVersion)) throw new Error("invalid remote cursor record: serverLowerBoundVersion is not a JSON object");
|
|
39
|
-
return {
|
|
40
|
-
streamUrl: record.streamUrl,
|
|
41
|
-
nextOffset: record.nextOffset,
|
|
42
|
-
serverLowerBoundVersion: cloneJsonValue(record.serverLowerBoundVersion),
|
|
43
|
-
updatedAtMs: record.updatedAtMs
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
function toStoredRemoteCursor(cursor) {
|
|
47
|
-
return {
|
|
48
|
-
streamUrl: cursor.streamUrl,
|
|
49
|
-
nextOffset: cursor.nextOffset,
|
|
50
|
-
serverLowerBoundVersion: cloneJsonValue(cursor.serverLowerBoundVersion),
|
|
51
|
-
updatedAtMs: cursor.updatedAtMs
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* In-memory cursor store useful for tests or ephemeral runtimes.
|
|
56
|
-
*/
|
|
57
|
-
var InMemoryRemoteCursorStore = class {
|
|
58
|
-
data = /* @__PURE__ */ new Map();
|
|
59
|
-
async load(streamUrl) {
|
|
60
|
-
const value = this.data.get(key(streamUrl));
|
|
61
|
-
return value == null ? null : cloneRemoteCursor(value);
|
|
62
|
-
}
|
|
63
|
-
async save(cursor) {
|
|
64
|
-
this.data.set(key(cursor.streamUrl), cloneRemoteCursor(cursor));
|
|
65
|
-
}
|
|
66
|
-
async delete(streamUrl) {
|
|
67
|
-
this.data.delete(key(streamUrl));
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
const DEFAULT_DB_NAME = "streams-crdt";
|
|
71
|
-
const DEFAULT_STORE_NAME = "remote-cursors";
|
|
72
|
-
const DEFAULT_DB_VERSION = 1;
|
|
73
|
-
/**
|
|
74
|
-
* IndexedDB-backed cursor store for browser clients that need replay progress persistence.
|
|
75
|
-
*
|
|
76
|
-
* This store only persists `RemoteCursor` metadata. It does not persist CRDT
|
|
77
|
-
* payloads. Use it only when the application also persists the corresponding
|
|
78
|
-
* local document state and restores that state before calling `sync()` or
|
|
79
|
-
* `join()` after a reload.
|
|
80
|
-
*/
|
|
81
|
-
var IndexedDbRemoteCursorStore = class {
|
|
82
|
-
dbName;
|
|
83
|
-
storeName;
|
|
84
|
-
dbVersion;
|
|
85
|
-
indexedDb;
|
|
86
|
-
db = null;
|
|
87
|
-
initPromise = null;
|
|
88
|
-
constructor(options) {
|
|
89
|
-
this.dbName = options?.dbName ?? DEFAULT_DB_NAME;
|
|
90
|
-
this.storeName = options?.storeName ?? DEFAULT_STORE_NAME;
|
|
91
|
-
this.dbVersion = options?.dbVersion ?? DEFAULT_DB_VERSION;
|
|
92
|
-
this.indexedDb = options?.indexedDb ?? globalThis.indexedDB;
|
|
93
|
-
}
|
|
94
|
-
async load(streamUrl) {
|
|
95
|
-
const db = await this.ensureDb();
|
|
96
|
-
const record = await new Promise((resolve, reject) => {
|
|
97
|
-
const request = db.transaction([this.storeName], "readonly").objectStore(this.storeName).get(key(streamUrl));
|
|
98
|
-
request.onsuccess = () => {
|
|
99
|
-
resolve(request.result ?? null);
|
|
100
|
-
};
|
|
101
|
-
request.onerror = () => {
|
|
102
|
-
reject(request.error ?? /* @__PURE__ */ new Error("indexeddb load failed"));
|
|
103
|
-
};
|
|
104
|
-
});
|
|
105
|
-
return record == null ? null : fromStoredRemoteCursor(record);
|
|
106
|
-
}
|
|
107
|
-
async save(cursor) {
|
|
108
|
-
const db = await this.ensureDb();
|
|
109
|
-
const record = toStoredRemoteCursor(cursor);
|
|
110
|
-
await new Promise((resolve, reject) => {
|
|
111
|
-
const tx = db.transaction([this.storeName], "readwrite");
|
|
112
|
-
const request = tx.objectStore(this.storeName).put(record, key(cursor.streamUrl));
|
|
113
|
-
tx.oncomplete = () => resolve();
|
|
114
|
-
tx.onerror = () => reject(tx.error ?? request.error ?? /* @__PURE__ */ new Error("indexeddb save failed"));
|
|
115
|
-
tx.onabort = () => reject(tx.error ?? request.error ?? /* @__PURE__ */ new Error("indexeddb save transaction aborted"));
|
|
116
|
-
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("indexeddb save failed"));
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
async delete(streamUrl) {
|
|
120
|
-
const db = await this.ensureDb();
|
|
121
|
-
await new Promise((resolve, reject) => {
|
|
122
|
-
const tx = db.transaction([this.storeName], "readwrite");
|
|
123
|
-
const request = tx.objectStore(this.storeName).delete(key(streamUrl));
|
|
124
|
-
tx.oncomplete = () => resolve();
|
|
125
|
-
tx.onerror = () => reject(tx.error ?? request.error ?? /* @__PURE__ */ new Error("indexeddb delete failed"));
|
|
126
|
-
tx.onabort = () => reject(tx.error ?? request.error ?? /* @__PURE__ */ new Error("indexeddb delete transaction aborted"));
|
|
127
|
-
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("indexeddb delete failed"));
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
async ensureDb() {
|
|
131
|
-
if (this.db != null) return this.db;
|
|
132
|
-
if (this.initPromise != null) {
|
|
133
|
-
await this.initPromise;
|
|
134
|
-
if (this.db == null) throw new Error("indexeddb is not available");
|
|
135
|
-
return this.db;
|
|
136
|
-
}
|
|
137
|
-
if (this.indexedDb == null) throw new Error("indexeddb is not available in this environment");
|
|
138
|
-
this.initPromise = new Promise((resolve, reject) => {
|
|
139
|
-
const request = this.indexedDb.open(this.dbName, this.dbVersion);
|
|
140
|
-
request.onupgradeneeded = () => {
|
|
141
|
-
const db = request.result;
|
|
142
|
-
if (!db.objectStoreNames.contains(this.storeName)) db.createObjectStore(this.storeName);
|
|
143
|
-
};
|
|
144
|
-
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("indexeddb open failed"));
|
|
145
|
-
request.onsuccess = () => {
|
|
146
|
-
this.db = request.result;
|
|
147
|
-
this.db.onversionchange = () => {
|
|
148
|
-
this.db?.close();
|
|
149
|
-
this.db = null;
|
|
150
|
-
};
|
|
151
|
-
resolve();
|
|
152
|
-
};
|
|
153
|
-
}).finally(() => {
|
|
154
|
-
this.initPromise = null;
|
|
155
|
-
});
|
|
156
|
-
await this.initPromise;
|
|
157
|
-
if (this.db == null) throw new Error("indexeddb open succeeded without a database handle");
|
|
158
|
-
return this.db;
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
/**
|
|
162
|
-
* Creates a cursor value suitable for seeding a `RemoteCursorStore`.
|
|
163
|
-
*/
|
|
164
|
-
function createInitialRemoteCursor(input) {
|
|
165
|
-
return {
|
|
166
|
-
streamUrl: input.streamUrl,
|
|
167
|
-
nextOffset: input.nextOffset,
|
|
168
|
-
serverLowerBoundVersion: cloneJsonValue(input.serverLowerBoundVersion),
|
|
169
|
-
updatedAtMs: Date.now()
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
//#endregion
|
|
173
|
-
//#region src/reconnect.ts
|
|
174
|
-
const DEFAULT_RECONNECT_CONFIG = {
|
|
175
|
-
delays: [
|
|
176
|
-
0,
|
|
177
|
-
500,
|
|
178
|
-
1e3,
|
|
179
|
-
2e3,
|
|
180
|
-
4e3,
|
|
181
|
-
8e3,
|
|
182
|
-
15e3
|
|
183
|
-
],
|
|
184
|
-
jitterFraction: .2,
|
|
185
|
-
maxAttempts: Infinity,
|
|
186
|
-
connectTimeoutMs: 1e4,
|
|
187
|
-
pollTimeoutMs: 3e4,
|
|
188
|
-
liveInactivityTimeoutMs: 45e3
|
|
189
|
-
};
|
|
190
|
-
/**
|
|
191
|
-
* Apply jitter of +/- fraction to a base delay.
|
|
192
|
-
* E.g. applyJitter(1000, 0.2) returns a value in [800, 1200].
|
|
193
|
-
*/
|
|
194
|
-
function applyJitter(baseMs, fraction) {
|
|
195
|
-
if (baseMs === 0) return 0;
|
|
196
|
-
const jitter = baseMs * fraction * (2 * Math.random() - 1);
|
|
197
|
-
return Math.max(0, Math.round(baseMs + jitter));
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Compute the retry delay for a given attempt number.
|
|
201
|
-
* Uses the delays table from config, clamping at the last entry.
|
|
202
|
-
*/
|
|
203
|
-
function computeRetryDelay(attempt, config = DEFAULT_RECONNECT_CONFIG) {
|
|
204
|
-
const { delays, jitterFraction } = config;
|
|
205
|
-
const base = delays[Math.min(attempt, delays.length - 1)];
|
|
206
|
-
return applyJitter(base, jitterFraction);
|
|
207
|
-
}
|
|
208
|
-
//#endregion
|
|
1
|
+
import { StreamsClient, decodeSsePayload, readSseEvents } from "@loro-dev/streams-client";
|
|
209
2
|
//#region src/deque.ts
|
|
210
3
|
const DEFAULT_COMPACT_THRESHOLD = 64;
|
|
211
4
|
const DEFAULT_MIN_CAPACITY = 16;
|
|
@@ -327,6 +120,42 @@ var Deque = class {
|
|
|
327
120
|
}
|
|
328
121
|
};
|
|
329
122
|
//#endregion
|
|
123
|
+
//#region src/reconnect.ts
|
|
124
|
+
const DEFAULT_RECONNECT_CONFIG = {
|
|
125
|
+
delays: [
|
|
126
|
+
0,
|
|
127
|
+
500,
|
|
128
|
+
1e3,
|
|
129
|
+
2e3,
|
|
130
|
+
4e3,
|
|
131
|
+
8e3,
|
|
132
|
+
15e3
|
|
133
|
+
],
|
|
134
|
+
jitterFraction: .2,
|
|
135
|
+
maxAttempts: Infinity,
|
|
136
|
+
connectTimeoutMs: 1e4,
|
|
137
|
+
pollTimeoutMs: 3e4,
|
|
138
|
+
liveInactivityTimeoutMs: 45e3
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Apply jitter of +/- fraction to a base delay.
|
|
142
|
+
* E.g. applyJitter(1000, 0.2) returns a value in [800, 1200].
|
|
143
|
+
*/
|
|
144
|
+
function applyJitter(baseMs, fraction) {
|
|
145
|
+
if (baseMs === 0) return 0;
|
|
146
|
+
const jitter = baseMs * fraction * (2 * Math.random() - 1);
|
|
147
|
+
return Math.max(0, Math.round(baseMs + jitter));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Compute the retry delay for a given attempt number.
|
|
151
|
+
* Uses the delays table from config, clamping at the last entry.
|
|
152
|
+
*/
|
|
153
|
+
function computeRetryDelay(attempt, config = DEFAULT_RECONNECT_CONFIG) {
|
|
154
|
+
const { delays, jitterFraction } = config;
|
|
155
|
+
const base = delays[Math.min(attempt, delays.length - 1)];
|
|
156
|
+
return applyJitter(base, jitterFraction);
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
330
159
|
//#region src/items.ts
|
|
331
160
|
const BINARY_CONTENT_TYPE = "application/octet-stream";
|
|
332
161
|
function encodeItems(items) {
|
|
@@ -748,6 +577,23 @@ function sleep(ms, signal) {
|
|
|
748
577
|
}, { once: true });
|
|
749
578
|
});
|
|
750
579
|
}
|
|
580
|
+
function mergeAbortSignals(...signals) {
|
|
581
|
+
const active = signals.filter((signal) => signal != null);
|
|
582
|
+
if (active.length === 0) return;
|
|
583
|
+
if (active.length === 1) return active[0];
|
|
584
|
+
if (typeof AbortSignal.any === "function") return AbortSignal.any(active);
|
|
585
|
+
const controller = new AbortController();
|
|
586
|
+
for (const signal of active) {
|
|
587
|
+
if (signal.aborted) {
|
|
588
|
+
controller.abort(signal.reason);
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
signal.addEventListener("abort", () => {
|
|
592
|
+
controller.abort(signal.reason);
|
|
593
|
+
}, { once: true });
|
|
594
|
+
}
|
|
595
|
+
return controller.signal;
|
|
596
|
+
}
|
|
751
597
|
function classifyFetchError(err) {
|
|
752
598
|
const message = err instanceof Error ? err.message : String(err);
|
|
753
599
|
const status = err instanceof HttpError ? err.status : void 0;
|
|
@@ -959,7 +805,10 @@ var StreamsTransportClient = class {
|
|
|
959
805
|
streamUrl;
|
|
960
806
|
adapter;
|
|
961
807
|
client;
|
|
808
|
+
fetchImpl;
|
|
809
|
+
authProvider;
|
|
962
810
|
reconnectConfig;
|
|
811
|
+
authRefreshInFlight;
|
|
963
812
|
constructor(options) {
|
|
964
813
|
this.streamUrl = options.streamUrl;
|
|
965
814
|
this.adapter = options.adapter;
|
|
@@ -973,6 +822,8 @@ var StreamsTransportClient = class {
|
|
|
973
822
|
pollTimeoutMs: options.reconnectConfig.pollTimeoutMs
|
|
974
823
|
}
|
|
975
824
|
});
|
|
825
|
+
this.fetchImpl = options.fetchImpl;
|
|
826
|
+
this.authProvider = options.authProvider;
|
|
976
827
|
this.reconnectConfig = options.reconnectConfig;
|
|
977
828
|
}
|
|
978
829
|
async createStreamRequest() {
|
|
@@ -1031,171 +882,836 @@ var StreamsTransportClient = class {
|
|
|
1031
882
|
const snapshotOffset = result.result.snapshotOffset;
|
|
1032
883
|
return snapshotOffset == null || snapshotOffset.length === 0 ? null : snapshotOffset;
|
|
1033
884
|
}
|
|
1034
|
-
async putSnapshotAt(snapshotOffset, snapshot) {
|
|
1035
|
-
const result = await this.client.putSnapshot({
|
|
1036
|
-
offset: snapshotOffset,
|
|
1037
|
-
part: {
|
|
1038
|
-
contentType: "application/octet-stream",
|
|
1039
|
-
body: snapshot
|
|
1040
|
-
}
|
|
885
|
+
async putSnapshotAt(snapshotOffset, snapshot) {
|
|
886
|
+
const result = await this.client.putSnapshot({
|
|
887
|
+
offset: snapshotOffset,
|
|
888
|
+
part: {
|
|
889
|
+
contentType: "application/octet-stream",
|
|
890
|
+
body: snapshot
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
if (!result.ok) throw this.toTransportFailure(result.result);
|
|
894
|
+
}
|
|
895
|
+
async appendUpdates(updates, producerSession) {
|
|
896
|
+
const result = await this.client.append({
|
|
897
|
+
part: {
|
|
898
|
+
contentType: BINARY_CONTENT_TYPE,
|
|
899
|
+
body: encodeItems(updates)
|
|
900
|
+
},
|
|
901
|
+
producer: {
|
|
902
|
+
producerId: producerSession.producerId,
|
|
903
|
+
epoch: producerSession.producerEpoch,
|
|
904
|
+
seq: producerSession.nextProducerSeq
|
|
905
|
+
}
|
|
906
|
+
});
|
|
907
|
+
if (result.ok) return {
|
|
908
|
+
kind: "ok",
|
|
909
|
+
ackEpoch: result.result.producer?.epoch ?? producerSession.producerEpoch,
|
|
910
|
+
ackSeq: result.result.producer?.seq ?? producerSession.nextProducerSeq,
|
|
911
|
+
nextOffset: result.result.nextOffset
|
|
912
|
+
};
|
|
913
|
+
const error = result.result;
|
|
914
|
+
if (error.code === "conflict" && error.detail?.kind === "producer_seq_gap") return {
|
|
915
|
+
kind: "seq-gap",
|
|
916
|
+
expectedSeq: error.detail.expectedSeq
|
|
917
|
+
};
|
|
918
|
+
if (error.code === "forbidden" && error.detail?.kind === "stale_producer_epoch") return {
|
|
919
|
+
kind: "stale-epoch",
|
|
920
|
+
serverEpoch: error.detail.serverEpoch
|
|
921
|
+
};
|
|
922
|
+
throw this.toTransportFailure(error);
|
|
923
|
+
}
|
|
924
|
+
async appendItems(updates) {
|
|
925
|
+
const result = await this.client.append({ part: {
|
|
926
|
+
contentType: BINARY_CONTENT_TYPE,
|
|
927
|
+
body: encodeItems(updates)
|
|
928
|
+
} });
|
|
929
|
+
if (result.ok) return result.result.nextOffset;
|
|
930
|
+
throw this.toTransportFailure(result.result);
|
|
931
|
+
}
|
|
932
|
+
async appendEphemeralUpdate(update, signal) {
|
|
933
|
+
if (update.byteLength === 0) return;
|
|
934
|
+
const response = await this.fetchAuthorized(this.streamUrl, {
|
|
935
|
+
method: "POST",
|
|
936
|
+
headers: { "Content-Type": BINARY_CONTENT_TYPE },
|
|
937
|
+
body: update,
|
|
938
|
+
signal
|
|
939
|
+
}, this.reconnectConfig.connectTimeoutMs, "connect");
|
|
940
|
+
if (response.status === 200 || response.status === 204) return;
|
|
941
|
+
throw await this.responseToEphemeralFailure(response, "ephemeral append");
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Opens one bootstrap-first ephemeral SSE session.
|
|
945
|
+
*
|
|
946
|
+
* Ephemeral channels intentionally do not expose offsets or control events:
|
|
947
|
+
* the first event must be `bootstrap`, and all following events must be live
|
|
948
|
+
* `data` updates.
|
|
949
|
+
*/
|
|
950
|
+
async readEphemeralSse(onBootstrap, onData, signal) {
|
|
951
|
+
const requestUrl = this.ephemeralReadEndpoint();
|
|
952
|
+
const response = await this.fetchAuthorized(requestUrl, {
|
|
953
|
+
method: "GET",
|
|
954
|
+
headers: { Accept: "text/event-stream" },
|
|
955
|
+
signal
|
|
956
|
+
}, this.reconnectConfig.connectTimeoutMs, "connect");
|
|
957
|
+
if (response.status === 404) return { kind: "not-found" };
|
|
958
|
+
if (!response.ok) {
|
|
959
|
+
if (response.status === 400) {
|
|
960
|
+
const message = await response.text();
|
|
961
|
+
if (isSseUnsupportedErrorText(message)) return { kind: "unsupported" };
|
|
962
|
+
throw new HttpError(message.length > 0 ? `ephemeral live read failed: ${message}` : "ephemeral live read failed", response.status);
|
|
963
|
+
}
|
|
964
|
+
throw await this.responseToEphemeralFailure(response, "ephemeral live read");
|
|
965
|
+
}
|
|
966
|
+
if (response.body == null) throw new ProtocolError("ephemeral sse response missing body");
|
|
967
|
+
if (!(response.headers.get("Content-Type") ?? "").toLowerCase().includes("text/event-stream")) throw new ProtocolError("ephemeral sse response must use text/event-stream content type");
|
|
968
|
+
const dataEncoding = response.headers.get("Stream-SSE-Data-Encoding") ?? void 0;
|
|
969
|
+
const consume = async () => {
|
|
970
|
+
let sawBootstrap = false;
|
|
971
|
+
for await (const event of readSseEvents(response.body)) {
|
|
972
|
+
if (!sawBootstrap) {
|
|
973
|
+
if (event.event !== "bootstrap") throw new ProtocolError(`ephemeral sse must start with bootstrap, got '${event.event}'`);
|
|
974
|
+
await onBootstrap(decodeSsePayload(event.data, dataEncoding));
|
|
975
|
+
sawBootstrap = true;
|
|
976
|
+
continue;
|
|
977
|
+
}
|
|
978
|
+
if (event.event === "data") {
|
|
979
|
+
await onData(decodeSsePayload(event.data, dataEncoding));
|
|
980
|
+
continue;
|
|
981
|
+
}
|
|
982
|
+
throw new ProtocolError(`unexpected ephemeral sse event '${event.event}'`);
|
|
983
|
+
}
|
|
984
|
+
if (!sawBootstrap) throw new ProtocolError("ephemeral sse ended before bootstrap");
|
|
985
|
+
return { kind: "ok" };
|
|
986
|
+
};
|
|
987
|
+
let abortHandler;
|
|
988
|
+
const abortRace = signal != null && !signal.aborted ? new Promise((_, reject) => {
|
|
989
|
+
abortHandler = () => {
|
|
990
|
+
response.body?.cancel().catch(() => {});
|
|
991
|
+
reject(signal.reason ?? new DOMException("The operation was aborted.", "AbortError"));
|
|
992
|
+
};
|
|
993
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
994
|
+
}) : null;
|
|
995
|
+
try {
|
|
996
|
+
if (signal?.aborted) {
|
|
997
|
+
await response.body.cancel().catch(() => {});
|
|
998
|
+
throw signal.reason ?? new DOMException("The operation was aborted.", "AbortError");
|
|
999
|
+
}
|
|
1000
|
+
return abortRace == null ? await consume() : await Promise.race([consume(), abortRace]);
|
|
1001
|
+
} finally {
|
|
1002
|
+
if (abortHandler != null && signal != null) signal.removeEventListener("abort", abortHandler);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
async fetchAuthorized(input, init, timeoutMs, phase) {
|
|
1006
|
+
const firstToken = await this.resolveToken({ reason: "request" });
|
|
1007
|
+
const firstResponse = await this.fetchWithTimeout(input, {
|
|
1008
|
+
...init,
|
|
1009
|
+
headers: this.attachAuthorization(init.headers, firstToken)
|
|
1010
|
+
}, timeoutMs, phase);
|
|
1011
|
+
if (this.authProvider == null || firstResponse.status !== 401 && firstResponse.status !== 403) return firstResponse;
|
|
1012
|
+
await firstResponse.body?.cancel().catch(() => {});
|
|
1013
|
+
const refreshedToken = await this.refreshTokenAfterFailure(firstResponse.status, firstToken);
|
|
1014
|
+
return this.fetchWithTimeout(input, {
|
|
1015
|
+
...init,
|
|
1016
|
+
headers: this.attachAuthorization(init.headers, refreshedToken)
|
|
1017
|
+
}, timeoutMs, phase);
|
|
1018
|
+
}
|
|
1019
|
+
async fetchWithTimeout(input, init, timeoutMs, phase) {
|
|
1020
|
+
const controller = new AbortController();
|
|
1021
|
+
const signal = mergeAbortSignals(init.signal, controller.signal);
|
|
1022
|
+
const timer = setTimeout(() => {
|
|
1023
|
+
controller.abort(new TimeoutError(phase, timeoutMs));
|
|
1024
|
+
}, timeoutMs);
|
|
1025
|
+
try {
|
|
1026
|
+
return await this.fetchImpl(input, {
|
|
1027
|
+
...init,
|
|
1028
|
+
signal
|
|
1029
|
+
});
|
|
1030
|
+
} catch (error) {
|
|
1031
|
+
if (controller.signal.aborted && controller.signal.reason instanceof TimeoutError) throw controller.signal.reason;
|
|
1032
|
+
throw error;
|
|
1033
|
+
} finally {
|
|
1034
|
+
clearTimeout(timer);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
async resolveToken(context) {
|
|
1038
|
+
return await this.authProvider?.(context);
|
|
1039
|
+
}
|
|
1040
|
+
async refreshTokenAfterFailure(status, previousToken) {
|
|
1041
|
+
if (this.authProvider == null) return;
|
|
1042
|
+
if (this.authRefreshInFlight == null) this.authRefreshInFlight = this.resolveToken({
|
|
1043
|
+
reason: "unauthorized",
|
|
1044
|
+
status,
|
|
1045
|
+
previousToken
|
|
1046
|
+
}).finally(() => {
|
|
1047
|
+
this.authRefreshInFlight = void 0;
|
|
1048
|
+
});
|
|
1049
|
+
return await this.authRefreshInFlight;
|
|
1050
|
+
}
|
|
1051
|
+
attachAuthorization(base, token) {
|
|
1052
|
+
const headers = new Headers(base);
|
|
1053
|
+
if (token == null || token.length === 0) {
|
|
1054
|
+
headers.delete("Authorization");
|
|
1055
|
+
return headers;
|
|
1056
|
+
}
|
|
1057
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
1058
|
+
return headers;
|
|
1059
|
+
}
|
|
1060
|
+
async responseToEphemeralFailure(response, context) {
|
|
1061
|
+
const message = await response.text().catch(() => "");
|
|
1062
|
+
return new HttpError(message.length > 0 ? `${context} failed: ${message}` : `${context} failed with status ${response.status}`, response.status);
|
|
1063
|
+
}
|
|
1064
|
+
ephemeralReadEndpoint() {
|
|
1065
|
+
const target = new URL(this.streamUrl);
|
|
1066
|
+
target.searchParams.set("live", "sse");
|
|
1067
|
+
return target.toString();
|
|
1068
|
+
}
|
|
1069
|
+
toTransportFailure(error) {
|
|
1070
|
+
switch (error.code) {
|
|
1071
|
+
case "bad_request":
|
|
1072
|
+
case "unauthorized":
|
|
1073
|
+
case "forbidden":
|
|
1074
|
+
case "not_found":
|
|
1075
|
+
case "conflict":
|
|
1076
|
+
case "gone":
|
|
1077
|
+
case "rate_limited": return new HttpError(error.message, error.status);
|
|
1078
|
+
case "timeout": return new TimeoutError(error.phase, error.phase === "poll" ? this.reconnectConfig.pollTimeoutMs : this.reconnectConfig.connectTimeoutMs);
|
|
1079
|
+
case "network_error": return error.cause instanceof TypeError ? error.cause : new TypeError(error.message);
|
|
1080
|
+
case "protocol_error": return new ProtocolError(error.message, error.status);
|
|
1081
|
+
case "unknown":
|
|
1082
|
+
if (typeof error.status === "number") return new HttpError(error.message, error.status);
|
|
1083
|
+
if (error.cause instanceof TypeError) return error.cause;
|
|
1084
|
+
if (error.cause instanceof Error) return error.cause;
|
|
1085
|
+
return new Error(error.message);
|
|
1086
|
+
default: {
|
|
1087
|
+
const exhaustive = error;
|
|
1088
|
+
return new Error(String(exhaustive));
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
async readItemsSse(initialOffset, cursor, onControl, onOpen, signal, onActivity) {
|
|
1093
|
+
const opened = await this.openItemsSseSession(initialOffset, cursor, onOpen, signal, onActivity);
|
|
1094
|
+
if (opened.kind !== "ok") return opened;
|
|
1095
|
+
let abortHandler;
|
|
1096
|
+
const abortRace = signal != null && !signal.aborted ? new Promise((_, reject) => {
|
|
1097
|
+
abortHandler = () => {
|
|
1098
|
+
opened.session.cancel();
|
|
1099
|
+
reject(signal.reason ?? new DOMException("The operation was aborted.", "AbortError"));
|
|
1100
|
+
};
|
|
1101
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
1102
|
+
}) : null;
|
|
1103
|
+
try {
|
|
1104
|
+
if (signal?.aborted) {
|
|
1105
|
+
await opened.session.cancel();
|
|
1106
|
+
throw signal.reason ?? new DOMException("The operation was aborted.", "AbortError");
|
|
1107
|
+
}
|
|
1108
|
+
const result = abortRace != null ? await Promise.race([opened.session.continue(onControl), abortRace]) : await opened.session.continue(onControl);
|
|
1109
|
+
if (result.paused) await opened.session.cancel();
|
|
1110
|
+
return {
|
|
1111
|
+
kind: "ok",
|
|
1112
|
+
latestOffset: result.latestOffset,
|
|
1113
|
+
latestCursor: result.latestCursor
|
|
1114
|
+
};
|
|
1115
|
+
} finally {
|
|
1116
|
+
if (abortHandler != null && signal != null) signal.removeEventListener("abort", abortHandler);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
async openItemsSseSession(initialOffset, cursor, onOpen, signal, onActivity) {
|
|
1120
|
+
const result = await this.client.openSseSession({
|
|
1121
|
+
offset: initialOffset,
|
|
1122
|
+
cursor,
|
|
1123
|
+
signal,
|
|
1124
|
+
onActivity
|
|
1125
|
+
});
|
|
1126
|
+
if (!result.ok) {
|
|
1127
|
+
if (result.result.code === "gone") return { kind: "gone" };
|
|
1128
|
+
if (result.result.code === "not_found") return { kind: "not-found" };
|
|
1129
|
+
if (result.result.code === "bad_request" && isSseUnsupportedErrorText(result.result.message)) return { kind: "unsupported" };
|
|
1130
|
+
throw this.toTransportFailure(result.result);
|
|
1131
|
+
}
|
|
1132
|
+
const session = result.result;
|
|
1133
|
+
onOpen?.();
|
|
1134
|
+
let pendingDataEvents = [];
|
|
1135
|
+
let latestOffset = session.nextOffset ?? initialOffset;
|
|
1136
|
+
let latestCursor = session.cursor;
|
|
1137
|
+
let exhausted = false;
|
|
1138
|
+
return {
|
|
1139
|
+
kind: "ok",
|
|
1140
|
+
session: {
|
|
1141
|
+
continue: async (nextOnControl) => {
|
|
1142
|
+
if (exhausted) return {
|
|
1143
|
+
kind: "ok",
|
|
1144
|
+
latestOffset,
|
|
1145
|
+
latestCursor,
|
|
1146
|
+
paused: false
|
|
1147
|
+
};
|
|
1148
|
+
for await (const event of session.events) {
|
|
1149
|
+
if (event.event === "data") {
|
|
1150
|
+
pendingDataEvents.push(event.data);
|
|
1151
|
+
continue;
|
|
1152
|
+
}
|
|
1153
|
+
if (event.event !== "control") continue;
|
|
1154
|
+
const control = parseSseControlData(event.data);
|
|
1155
|
+
const updates = [];
|
|
1156
|
+
for (const eventData of pendingDataEvents) updates.push(...decodeSseItemPayloads(eventData, session.dataEncoding));
|
|
1157
|
+
pendingDataEvents = [];
|
|
1158
|
+
const handlerResult = await nextOnControl(updates, control.nextOffset, control.upToDate, control.streamCursor);
|
|
1159
|
+
latestOffset = control.nextOffset;
|
|
1160
|
+
if (control.streamCursor != null) latestCursor = control.streamCursor;
|
|
1161
|
+
if (handlerResult === "stop") {
|
|
1162
|
+
exhausted = true;
|
|
1163
|
+
await session.cancel();
|
|
1164
|
+
return {
|
|
1165
|
+
kind: "ok",
|
|
1166
|
+
latestOffset,
|
|
1167
|
+
latestCursor,
|
|
1168
|
+
paused: false
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
if (handlerResult === "pause") return {
|
|
1172
|
+
kind: "ok",
|
|
1173
|
+
latestOffset,
|
|
1174
|
+
latestCursor,
|
|
1175
|
+
paused: true
|
|
1176
|
+
};
|
|
1177
|
+
}
|
|
1178
|
+
exhausted = true;
|
|
1179
|
+
return {
|
|
1180
|
+
kind: "ok",
|
|
1181
|
+
latestOffset,
|
|
1182
|
+
latestCursor,
|
|
1183
|
+
paused: false
|
|
1184
|
+
};
|
|
1185
|
+
},
|
|
1186
|
+
cancel: async () => {
|
|
1187
|
+
if (exhausted) return;
|
|
1188
|
+
exhausted = true;
|
|
1189
|
+
await session.cancel();
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
};
|
|
1193
|
+
}
|
|
1194
|
+
};
|
|
1195
|
+
//#endregion
|
|
1196
|
+
//#region src/ephemeral-runtime.ts
|
|
1197
|
+
function createNoopAdapter() {
|
|
1198
|
+
return {
|
|
1199
|
+
emptyVersion: () => ({}),
|
|
1200
|
+
mergeVersions: (base) => base,
|
|
1201
|
+
exportSnapshot: () => new Uint8Array(),
|
|
1202
|
+
applySnapshot: () => ({}),
|
|
1203
|
+
exportUpdates: () => void 0,
|
|
1204
|
+
applyRemoteUpdates: () => ({}),
|
|
1205
|
+
subscribeLocalUpdates: () => () => void 0
|
|
1206
|
+
};
|
|
1207
|
+
}
|
|
1208
|
+
function normalizeEphemeralUpdate(update) {
|
|
1209
|
+
if (update == null || update.byteLength === 0) return null;
|
|
1210
|
+
return update;
|
|
1211
|
+
}
|
|
1212
|
+
function toError(error) {
|
|
1213
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
1214
|
+
}
|
|
1215
|
+
function closedSubscriptionError() {
|
|
1216
|
+
return /* @__PURE__ */ new Error("subscription closed");
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* Realtime transport for one ephemeral CRDT stream.
|
|
1220
|
+
*
|
|
1221
|
+
* This is intentionally separate from `StreamsCrdt`: it bootstraps through
|
|
1222
|
+
* the first SSE event, then tails live deltas, and never stores a replay
|
|
1223
|
+
* offset, snapshot checkpoint, or remote cursor.
|
|
1224
|
+
*/
|
|
1225
|
+
var EphemeralStreamCrdt = class {
|
|
1226
|
+
/** Full ephemeral stream URL, including `?ephemeral=` or `?awareness=`. */
|
|
1227
|
+
streamUrl;
|
|
1228
|
+
/** Store-specific adapter used to encode local state and apply remote bytes. */
|
|
1229
|
+
adaptor;
|
|
1230
|
+
/** Low-level HTTP/SSE transport. It is reused only for auth, fetch, and parsing helpers. */
|
|
1231
|
+
client;
|
|
1232
|
+
/** Retry policy shared by the read loop and local write queue. */
|
|
1233
|
+
reconnectConfig;
|
|
1234
|
+
/** Enables diagnostic console errors for failed read/write loops. */
|
|
1235
|
+
debug;
|
|
1236
|
+
/** Current shared subscription state, if the stream is joined. */
|
|
1237
|
+
joinState;
|
|
1238
|
+
constructor(options) {
|
|
1239
|
+
this.streamUrl = options.streamUrl;
|
|
1240
|
+
this.adaptor = options.adaptor;
|
|
1241
|
+
this.reconnectConfig = {
|
|
1242
|
+
...DEFAULT_RECONNECT_CONFIG,
|
|
1243
|
+
...options.reconnectConfig
|
|
1244
|
+
};
|
|
1245
|
+
this.debug = options.debug ?? false;
|
|
1246
|
+
this.client = new StreamsTransportClient({
|
|
1247
|
+
streamUrl: options.streamUrl,
|
|
1248
|
+
adapter: createNoopAdapter(),
|
|
1249
|
+
authProvider: normalizeAuthProvider(options.auth),
|
|
1250
|
+
fetchImpl: options.fetch ?? globalThis.fetch.bind(globalThis),
|
|
1251
|
+
reconnectConfig: this.reconnectConfig
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
async join(params) {
|
|
1255
|
+
const existing = this.joinState;
|
|
1256
|
+
if (existing != null) try {
|
|
1257
|
+
await existing.started;
|
|
1258
|
+
return {
|
|
1259
|
+
ok: true,
|
|
1260
|
+
value: this.createSubscription(existing, params)
|
|
1261
|
+
};
|
|
1262
|
+
} catch (error) {
|
|
1263
|
+
return {
|
|
1264
|
+
ok: false,
|
|
1265
|
+
error: toTransportError(error)
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
const state = this.createJoinState();
|
|
1269
|
+
state.started = this.startJoin(state);
|
|
1270
|
+
this.joinState = state;
|
|
1271
|
+
try {
|
|
1272
|
+
await state.started;
|
|
1273
|
+
return {
|
|
1274
|
+
ok: true,
|
|
1275
|
+
value: this.createSubscription(state, params)
|
|
1276
|
+
};
|
|
1277
|
+
} catch (error) {
|
|
1278
|
+
await this.disposeJoinState(state);
|
|
1279
|
+
return {
|
|
1280
|
+
ok: false,
|
|
1281
|
+
error: toTransportError(error)
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
rejoin() {
|
|
1286
|
+
const state = this.joinState;
|
|
1287
|
+
if (state == null || state.closed) return;
|
|
1288
|
+
state.retryAttempt = 0;
|
|
1289
|
+
state.retrySleepController.abort();
|
|
1290
|
+
state.retrySleepController = new AbortController();
|
|
1291
|
+
const previousController = state.requestController;
|
|
1292
|
+
state.requestController = new AbortController();
|
|
1293
|
+
previousController?.abort();
|
|
1294
|
+
if (state.readStatus === "disconnected" || state.readStatus === "error") {
|
|
1295
|
+
this.setReadStatus(state, "reconnecting");
|
|
1296
|
+
this.runReadLoop(state);
|
|
1297
|
+
}
|
|
1298
|
+
if (state.writeStatus === "error") {
|
|
1299
|
+
this.setWriteStatus(state, "reconnecting");
|
|
1300
|
+
this.flushPendingLocal(state);
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
async close() {
|
|
1304
|
+
const state = this.joinState;
|
|
1305
|
+
if (state != null) await this.disposeJoinState(state);
|
|
1306
|
+
}
|
|
1307
|
+
createJoinState() {
|
|
1308
|
+
return {
|
|
1309
|
+
refCount: 0,
|
|
1310
|
+
closed: false,
|
|
1311
|
+
status: "connecting",
|
|
1312
|
+
readStatus: "connecting",
|
|
1313
|
+
writeStatus: "connecting",
|
|
1314
|
+
statusListeners: /* @__PURE__ */ new Set(),
|
|
1315
|
+
pendingLocal: new Deque(),
|
|
1316
|
+
syncWaiters: [],
|
|
1317
|
+
flushingLocal: false,
|
|
1318
|
+
retryAttempt: 0,
|
|
1319
|
+
retrySleepController: new AbortController(),
|
|
1320
|
+
firstOpenSettled: false,
|
|
1321
|
+
started: Promise.resolve()
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
1324
|
+
async startJoin(state) {
|
|
1325
|
+
state.unsubscribeLocal = this.adaptor.subscribeLocalUpdates((update) => {
|
|
1326
|
+
this.enqueueLocal(state, normalizeEphemeralUpdate(update));
|
|
1327
|
+
});
|
|
1328
|
+
state.requestController = new AbortController();
|
|
1329
|
+
const firstOpen = this.waitForFirstOpen(state);
|
|
1330
|
+
this.runReadLoop(state);
|
|
1331
|
+
await firstOpen;
|
|
1332
|
+
if (state.closed) return;
|
|
1333
|
+
this.setWriteStatus(state, "ok");
|
|
1334
|
+
await this.flushPendingLocal(state);
|
|
1335
|
+
if (state.status === "error" || state.status === "disconnected") throw new Error(`ephemeral stream failed to join: ${state.status}`);
|
|
1336
|
+
}
|
|
1337
|
+
waitForFirstOpen(state) {
|
|
1338
|
+
return new Promise((resolve, reject) => {
|
|
1339
|
+
state.firstOpenResolve = resolve;
|
|
1340
|
+
state.firstOpenReject = reject;
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
createSubscription(state, params) {
|
|
1344
|
+
state.refCount += 1;
|
|
1345
|
+
const statusListener = params?.onStatusChange;
|
|
1346
|
+
if (statusListener != null) {
|
|
1347
|
+
state.statusListeners.add(statusListener);
|
|
1348
|
+
statusListener(state.status);
|
|
1349
|
+
}
|
|
1350
|
+
let unsubscribed = false;
|
|
1351
|
+
return {
|
|
1352
|
+
unsubscribe: () => {
|
|
1353
|
+
if (unsubscribed) return;
|
|
1354
|
+
unsubscribed = true;
|
|
1355
|
+
if (statusListener != null) state.statusListeners.delete(statusListener);
|
|
1356
|
+
state.refCount = Math.max(0, state.refCount - 1);
|
|
1357
|
+
if (state.refCount === 0 && !state.closed) this.disposeJoinState(state);
|
|
1358
|
+
},
|
|
1359
|
+
get connected() {
|
|
1360
|
+
return state.status === "joined";
|
|
1361
|
+
},
|
|
1362
|
+
get status() {
|
|
1363
|
+
return state.status;
|
|
1364
|
+
},
|
|
1365
|
+
onStatusChange: (listener) => this.onStatusChange(state, listener),
|
|
1366
|
+
waitUntilSynced: async () => {
|
|
1367
|
+
if (state.closed) throw closedSubscriptionError();
|
|
1368
|
+
const remaining = state.pendingLocal.length;
|
|
1369
|
+
if (remaining === 0) return;
|
|
1370
|
+
return new Promise((resolve, reject) => {
|
|
1371
|
+
state.syncWaiters.push({
|
|
1372
|
+
remaining,
|
|
1373
|
+
resolve,
|
|
1374
|
+
reject
|
|
1375
|
+
});
|
|
1376
|
+
});
|
|
1377
|
+
}
|
|
1378
|
+
};
|
|
1379
|
+
}
|
|
1380
|
+
onStatusChange(state, listener) {
|
|
1381
|
+
state.statusListeners.add(listener);
|
|
1382
|
+
listener(state.status);
|
|
1383
|
+
return () => {
|
|
1384
|
+
state.statusListeners.delete(listener);
|
|
1385
|
+
};
|
|
1386
|
+
}
|
|
1387
|
+
async disposeJoinState(state) {
|
|
1388
|
+
if (state.closed) return;
|
|
1389
|
+
state.closed = true;
|
|
1390
|
+
state.unsubscribeLocal?.();
|
|
1391
|
+
state.requestController?.abort();
|
|
1392
|
+
state.retrySleepController.abort();
|
|
1393
|
+
this.rejectFirstOpen(state, closedSubscriptionError());
|
|
1394
|
+
this.rejectSyncWaiters(state, closedSubscriptionError());
|
|
1395
|
+
if (this.joinState === state) this.joinState = void 0;
|
|
1396
|
+
}
|
|
1397
|
+
resolveFirstOpen(state) {
|
|
1398
|
+
if (state.firstOpenSettled) return;
|
|
1399
|
+
state.firstOpenSettled = true;
|
|
1400
|
+
state.firstOpenResolve?.();
|
|
1401
|
+
}
|
|
1402
|
+
rejectFirstOpen(state, error) {
|
|
1403
|
+
if (state.firstOpenSettled) return;
|
|
1404
|
+
state.firstOpenSettled = true;
|
|
1405
|
+
state.firstOpenReject?.(error);
|
|
1406
|
+
}
|
|
1407
|
+
async runReadLoop(state) {
|
|
1408
|
+
while (!state.closed) {
|
|
1409
|
+
state.requestController ??= new AbortController();
|
|
1410
|
+
const capturedController = state.requestController;
|
|
1411
|
+
try {
|
|
1412
|
+
const result = await this.client.readEphemeralSse(async (bootstrap) => {
|
|
1413
|
+
if (bootstrap.byteLength > 0) await this.adaptor.applyRemoteUpdates([bootstrap]);
|
|
1414
|
+
this.setReadStatus(state, "ok");
|
|
1415
|
+
this.resolveFirstOpen(state);
|
|
1416
|
+
}, async (update) => {
|
|
1417
|
+
if (update.byteLength > 0) await this.adaptor.applyRemoteUpdates([update]);
|
|
1418
|
+
}, capturedController.signal);
|
|
1419
|
+
if (state.closed) return;
|
|
1420
|
+
if (result.kind === "ok") {
|
|
1421
|
+
state.retryAttempt = 0;
|
|
1422
|
+
this.setReadStatus(state, "reconnecting");
|
|
1423
|
+
await sleep(0);
|
|
1424
|
+
continue;
|
|
1425
|
+
}
|
|
1426
|
+
if (result.kind === "unsupported") throw new Error("ephemeral stream requires SSE support");
|
|
1427
|
+
if (result.kind === "not-found") throw new HttpError("ephemeral stream not found", 404);
|
|
1428
|
+
} catch (error) {
|
|
1429
|
+
if (state.closed) return;
|
|
1430
|
+
if (capturedController.signal.aborted) {
|
|
1431
|
+
state.requestController = new AbortController();
|
|
1432
|
+
this.setReadStatus(state, "reconnecting");
|
|
1433
|
+
continue;
|
|
1434
|
+
}
|
|
1435
|
+
const classified = classifyFetchError(error);
|
|
1436
|
+
if (!classified.retriable || isAuthOrProtocolError(error)) {
|
|
1437
|
+
this.logError("ephemeral live read failed", error, {
|
|
1438
|
+
streamUrl: this.streamUrl,
|
|
1439
|
+
code: classified.code
|
|
1440
|
+
});
|
|
1441
|
+
this.setReadStatus(state, "error");
|
|
1442
|
+
this.rejectFirstOpen(state, toError(error));
|
|
1443
|
+
return;
|
|
1444
|
+
}
|
|
1445
|
+
this.setReadStatus(state, "reconnecting");
|
|
1446
|
+
if (state.retryAttempt >= this.reconnectConfig.maxAttempts) {
|
|
1447
|
+
this.logError("ephemeral live reconnect exhausted", error, {
|
|
1448
|
+
streamUrl: this.streamUrl,
|
|
1449
|
+
attempts: state.retryAttempt
|
|
1450
|
+
});
|
|
1451
|
+
this.setReadStatus(state, "disconnected");
|
|
1452
|
+
this.rejectFirstOpen(state, toError(error));
|
|
1453
|
+
return;
|
|
1454
|
+
}
|
|
1455
|
+
const delay = computeRetryDelay(state.retryAttempt, this.reconnectConfig);
|
|
1456
|
+
state.retryAttempt += 1;
|
|
1457
|
+
await sleep(delay, state.retrySleepController.signal);
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
enqueueLocal(state, update) {
|
|
1462
|
+
if (state.closed || update == null || update.byteLength === 0) return;
|
|
1463
|
+
state.pendingLocal.pushBack(update);
|
|
1464
|
+
if (state.writeStatus !== "connecting") this.flushPendingLocal(state);
|
|
1465
|
+
}
|
|
1466
|
+
async flushPendingLocal(state) {
|
|
1467
|
+
if (state.flushingLocal || state.closed) return;
|
|
1468
|
+
state.flushingLocal = true;
|
|
1469
|
+
try {
|
|
1470
|
+
while (!state.closed) {
|
|
1471
|
+
const update = state.pendingLocal.peekFront();
|
|
1472
|
+
if (update == null) return;
|
|
1473
|
+
let attempt = 0;
|
|
1474
|
+
while (!state.closed) try {
|
|
1475
|
+
await this.client.appendEphemeralUpdate(update);
|
|
1476
|
+
state.pendingLocal.popFront();
|
|
1477
|
+
this.notifySyncWaiters(state, 1);
|
|
1478
|
+
this.setWriteStatus(state, "ok");
|
|
1479
|
+
break;
|
|
1480
|
+
} catch (error) {
|
|
1481
|
+
const classified = classifyFetchError(error);
|
|
1482
|
+
if (!classified.retriable || isAuthOrProtocolError(error)) {
|
|
1483
|
+
this.rejectSyncWaiters(state, /* @__PURE__ */ new Error(`ephemeral append failed: ${classified.code}`));
|
|
1484
|
+
this.setWriteStatus(state, "error");
|
|
1485
|
+
return;
|
|
1486
|
+
}
|
|
1487
|
+
if (attempt >= this.reconnectConfig.maxAttempts) {
|
|
1488
|
+
this.rejectSyncWaiters(state, /* @__PURE__ */ new Error("ephemeral append retry budget exhausted"));
|
|
1489
|
+
this.setWriteStatus(state, "error");
|
|
1490
|
+
return;
|
|
1491
|
+
}
|
|
1492
|
+
this.setWriteStatus(state, "reconnecting");
|
|
1493
|
+
const delay = computeRetryDelay(attempt, this.reconnectConfig);
|
|
1494
|
+
attempt += 1;
|
|
1495
|
+
await sleep(delay, state.retrySleepController.signal);
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
} finally {
|
|
1499
|
+
state.flushingLocal = false;
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
notifySyncWaiters(state, flushedCount) {
|
|
1503
|
+
let i = 0;
|
|
1504
|
+
while (i < state.syncWaiters.length) {
|
|
1505
|
+
const waiter = state.syncWaiters[i];
|
|
1506
|
+
waiter.remaining -= flushedCount;
|
|
1507
|
+
if (waiter.remaining <= 0) {
|
|
1508
|
+
state.syncWaiters.splice(i, 1);
|
|
1509
|
+
waiter.resolve();
|
|
1510
|
+
} else i += 1;
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
rejectSyncWaiters(state, error) {
|
|
1514
|
+
for (const waiter of state.syncWaiters.splice(0)) waiter.reject(error);
|
|
1515
|
+
}
|
|
1516
|
+
setReadStatus(state, readStatus) {
|
|
1517
|
+
if (state.readStatus === readStatus) return;
|
|
1518
|
+
state.readStatus = readStatus;
|
|
1519
|
+
this.recomputeStatus(state);
|
|
1520
|
+
}
|
|
1521
|
+
setWriteStatus(state, writeStatus) {
|
|
1522
|
+
if (state.writeStatus === writeStatus) return;
|
|
1523
|
+
state.writeStatus = writeStatus;
|
|
1524
|
+
this.recomputeStatus(state);
|
|
1525
|
+
}
|
|
1526
|
+
recomputeStatus(state) {
|
|
1527
|
+
const status = this.computeStatus(state);
|
|
1528
|
+
if (state.status === status) return;
|
|
1529
|
+
state.status = status;
|
|
1530
|
+
for (const listener of state.statusListeners) listener(status);
|
|
1531
|
+
}
|
|
1532
|
+
computeStatus(state) {
|
|
1533
|
+
if (state.readStatus === "error" || state.writeStatus === "error") return "error";
|
|
1534
|
+
if (state.readStatus === "disconnected") return "disconnected";
|
|
1535
|
+
if (state.readStatus === "connecting" || state.writeStatus === "connecting") return "connecting";
|
|
1536
|
+
if (state.readStatus === "reconnecting" || state.writeStatus === "reconnecting") return "reconnecting";
|
|
1537
|
+
return "joined";
|
|
1538
|
+
}
|
|
1539
|
+
logError(message, error, detail) {
|
|
1540
|
+
if (!this.debug) return;
|
|
1541
|
+
console.error(`[streams-crdt] ${message}`, error, detail);
|
|
1542
|
+
}
|
|
1543
|
+
};
|
|
1544
|
+
//#endregion
|
|
1545
|
+
//#region src/checkpoint.ts
|
|
1546
|
+
function composeBeforeRemoteCursorSaveHooks(...hooks) {
|
|
1547
|
+
const active = hooks.filter((hook) => hook != null);
|
|
1548
|
+
if (active.length === 0) return;
|
|
1549
|
+
return async (context) => {
|
|
1550
|
+
for (const hook of active) await hook(context);
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
async function persistRemoteCursor(input) {
|
|
1554
|
+
const next = {
|
|
1555
|
+
...input.cursor,
|
|
1556
|
+
updatedAtMs: (input.now ?? Date.now)()
|
|
1557
|
+
};
|
|
1558
|
+
if (input.beforeRemoteCursorSave != null) await input.beforeRemoteCursorSave({
|
|
1559
|
+
cursor: next,
|
|
1560
|
+
source: input.source
|
|
1561
|
+
});
|
|
1562
|
+
await input.remoteCursorStore.save(next);
|
|
1563
|
+
return next;
|
|
1564
|
+
}
|
|
1565
|
+
function key(streamUrl) {
|
|
1566
|
+
return streamUrl;
|
|
1567
|
+
}
|
|
1568
|
+
function isJsonObject(value) {
|
|
1569
|
+
return typeof value === "object" && value != null && !Array.isArray(value);
|
|
1570
|
+
}
|
|
1571
|
+
function cloneJsonValue(value) {
|
|
1572
|
+
return structuredClone(value);
|
|
1573
|
+
}
|
|
1574
|
+
function cloneRemoteCursor(cursor) {
|
|
1575
|
+
return {
|
|
1576
|
+
...cursor,
|
|
1577
|
+
serverLowerBoundVersion: cloneJsonValue(cursor.serverLowerBoundVersion)
|
|
1578
|
+
};
|
|
1579
|
+
}
|
|
1580
|
+
function fromStoredRemoteCursor(record) {
|
|
1581
|
+
if (!isJsonObject(record.serverLowerBoundVersion)) throw new Error("invalid remote cursor record: serverLowerBoundVersion is not a JSON object");
|
|
1582
|
+
return {
|
|
1583
|
+
streamUrl: record.streamUrl,
|
|
1584
|
+
nextOffset: record.nextOffset,
|
|
1585
|
+
serverLowerBoundVersion: cloneJsonValue(record.serverLowerBoundVersion),
|
|
1586
|
+
updatedAtMs: record.updatedAtMs
|
|
1587
|
+
};
|
|
1588
|
+
}
|
|
1589
|
+
function toStoredRemoteCursor(cursor) {
|
|
1590
|
+
return {
|
|
1591
|
+
streamUrl: cursor.streamUrl,
|
|
1592
|
+
nextOffset: cursor.nextOffset,
|
|
1593
|
+
serverLowerBoundVersion: cloneJsonValue(cursor.serverLowerBoundVersion),
|
|
1594
|
+
updatedAtMs: cursor.updatedAtMs
|
|
1595
|
+
};
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
* In-memory cursor store useful for tests or ephemeral runtimes.
|
|
1599
|
+
*/
|
|
1600
|
+
var InMemoryRemoteCursorStore = class {
|
|
1601
|
+
data = /* @__PURE__ */ new Map();
|
|
1602
|
+
async load(streamUrl) {
|
|
1603
|
+
const value = this.data.get(key(streamUrl));
|
|
1604
|
+
return value == null ? null : cloneRemoteCursor(value);
|
|
1605
|
+
}
|
|
1606
|
+
async save(cursor) {
|
|
1607
|
+
this.data.set(key(cursor.streamUrl), cloneRemoteCursor(cursor));
|
|
1608
|
+
}
|
|
1609
|
+
async delete(streamUrl) {
|
|
1610
|
+
this.data.delete(key(streamUrl));
|
|
1611
|
+
}
|
|
1612
|
+
};
|
|
1613
|
+
const DEFAULT_DB_NAME = "streams-crdt";
|
|
1614
|
+
const DEFAULT_STORE_NAME = "remote-cursors";
|
|
1615
|
+
const DEFAULT_DB_VERSION = 1;
|
|
1616
|
+
/**
|
|
1617
|
+
* IndexedDB-backed cursor store for browser clients that need replay progress persistence.
|
|
1618
|
+
*
|
|
1619
|
+
* This store only persists `RemoteCursor` metadata. It does not persist CRDT
|
|
1620
|
+
* payloads. Use it only when the application also persists the corresponding
|
|
1621
|
+
* local document state and restores that state before calling `sync()` or
|
|
1622
|
+
* `join()` after a reload.
|
|
1623
|
+
*/
|
|
1624
|
+
var IndexedDbRemoteCursorStore = class {
|
|
1625
|
+
dbName;
|
|
1626
|
+
storeName;
|
|
1627
|
+
dbVersion;
|
|
1628
|
+
indexedDb;
|
|
1629
|
+
db = null;
|
|
1630
|
+
initPromise = null;
|
|
1631
|
+
constructor(options) {
|
|
1632
|
+
this.dbName = options?.dbName ?? DEFAULT_DB_NAME;
|
|
1633
|
+
this.storeName = options?.storeName ?? DEFAULT_STORE_NAME;
|
|
1634
|
+
this.dbVersion = options?.dbVersion ?? DEFAULT_DB_VERSION;
|
|
1635
|
+
this.indexedDb = options?.indexedDb ?? globalThis.indexedDB;
|
|
1636
|
+
}
|
|
1637
|
+
async load(streamUrl) {
|
|
1638
|
+
const db = await this.ensureDb();
|
|
1639
|
+
const record = await new Promise((resolve, reject) => {
|
|
1640
|
+
const request = db.transaction([this.storeName], "readonly").objectStore(this.storeName).get(key(streamUrl));
|
|
1641
|
+
request.onsuccess = () => {
|
|
1642
|
+
resolve(request.result ?? null);
|
|
1643
|
+
};
|
|
1644
|
+
request.onerror = () => {
|
|
1645
|
+
reject(request.error ?? /* @__PURE__ */ new Error("indexeddb load failed"));
|
|
1646
|
+
};
|
|
1041
1647
|
});
|
|
1042
|
-
|
|
1648
|
+
return record == null ? null : fromStoredRemoteCursor(record);
|
|
1043
1649
|
}
|
|
1044
|
-
async
|
|
1045
|
-
const
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
}
|
|
1650
|
+
async save(cursor) {
|
|
1651
|
+
const db = await this.ensureDb();
|
|
1652
|
+
const record = toStoredRemoteCursor(cursor);
|
|
1653
|
+
await new Promise((resolve, reject) => {
|
|
1654
|
+
const tx = db.transaction([this.storeName], "readwrite");
|
|
1655
|
+
const request = tx.objectStore(this.storeName).put(record, key(cursor.streamUrl));
|
|
1656
|
+
tx.oncomplete = () => resolve();
|
|
1657
|
+
tx.onerror = () => reject(tx.error ?? request.error ?? /* @__PURE__ */ new Error("indexeddb save failed"));
|
|
1658
|
+
tx.onabort = () => reject(tx.error ?? request.error ?? /* @__PURE__ */ new Error("indexeddb save transaction aborted"));
|
|
1659
|
+
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("indexeddb save failed"));
|
|
1055
1660
|
});
|
|
1056
|
-
if (result.ok) return {
|
|
1057
|
-
kind: "ok",
|
|
1058
|
-
ackEpoch: result.result.producer?.epoch ?? producerSession.producerEpoch,
|
|
1059
|
-
ackSeq: result.result.producer?.seq ?? producerSession.nextProducerSeq,
|
|
1060
|
-
nextOffset: result.result.nextOffset
|
|
1061
|
-
};
|
|
1062
|
-
const error = result.result;
|
|
1063
|
-
if (error.code === "conflict" && error.detail?.kind === "producer_seq_gap") return {
|
|
1064
|
-
kind: "seq-gap",
|
|
1065
|
-
expectedSeq: error.detail.expectedSeq
|
|
1066
|
-
};
|
|
1067
|
-
if (error.code === "forbidden" && error.detail?.kind === "stale_producer_epoch") return {
|
|
1068
|
-
kind: "stale-epoch",
|
|
1069
|
-
serverEpoch: error.detail.serverEpoch
|
|
1070
|
-
};
|
|
1071
|
-
throw this.toTransportFailure(error);
|
|
1072
1661
|
}
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
case "network_error": return error.cause instanceof TypeError ? error.cause : new TypeError(error.message);
|
|
1084
|
-
case "protocol_error": return new ProtocolError(error.message, error.status);
|
|
1085
|
-
case "unknown":
|
|
1086
|
-
if (typeof error.status === "number") return new HttpError(error.message, error.status);
|
|
1087
|
-
if (error.cause instanceof TypeError) return error.cause;
|
|
1088
|
-
if (error.cause instanceof Error) return error.cause;
|
|
1089
|
-
return new Error(error.message);
|
|
1090
|
-
default: {
|
|
1091
|
-
const exhaustive = error;
|
|
1092
|
-
return new Error(String(exhaustive));
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1662
|
+
async delete(streamUrl) {
|
|
1663
|
+
const db = await this.ensureDb();
|
|
1664
|
+
await new Promise((resolve, reject) => {
|
|
1665
|
+
const tx = db.transaction([this.storeName], "readwrite");
|
|
1666
|
+
const request = tx.objectStore(this.storeName).delete(key(streamUrl));
|
|
1667
|
+
tx.oncomplete = () => resolve();
|
|
1668
|
+
tx.onerror = () => reject(tx.error ?? request.error ?? /* @__PURE__ */ new Error("indexeddb delete failed"));
|
|
1669
|
+
tx.onabort = () => reject(tx.error ?? request.error ?? /* @__PURE__ */ new Error("indexeddb delete transaction aborted"));
|
|
1670
|
+
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("indexeddb delete failed"));
|
|
1671
|
+
});
|
|
1095
1672
|
}
|
|
1096
|
-
async
|
|
1097
|
-
|
|
1098
|
-
if (
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1673
|
+
async ensureDb() {
|
|
1674
|
+
if (this.db != null) return this.db;
|
|
1675
|
+
if (this.initPromise != null) {
|
|
1676
|
+
await this.initPromise;
|
|
1677
|
+
if (this.db == null) throw new Error("indexeddb is not available");
|
|
1678
|
+
return this.db;
|
|
1679
|
+
}
|
|
1680
|
+
if (this.indexedDb == null) throw new Error("indexeddb is not available in this environment");
|
|
1681
|
+
this.initPromise = new Promise((resolve, reject) => {
|
|
1682
|
+
const request = this.indexedDb.open(this.dbName, this.dbVersion);
|
|
1683
|
+
request.onupgradeneeded = () => {
|
|
1684
|
+
const db = request.result;
|
|
1685
|
+
if (!db.objectStoreNames.contains(this.storeName)) db.createObjectStore(this.storeName);
|
|
1104
1686
|
};
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
if (result.paused) await opened.session.cancel();
|
|
1114
|
-
return {
|
|
1115
|
-
kind: "ok",
|
|
1116
|
-
latestOffset: result.latestOffset,
|
|
1117
|
-
latestCursor: result.latestCursor
|
|
1687
|
+
request.onerror = () => reject(request.error ?? /* @__PURE__ */ new Error("indexeddb open failed"));
|
|
1688
|
+
request.onsuccess = () => {
|
|
1689
|
+
this.db = request.result;
|
|
1690
|
+
this.db.onversionchange = () => {
|
|
1691
|
+
this.db?.close();
|
|
1692
|
+
this.db = null;
|
|
1693
|
+
};
|
|
1694
|
+
resolve();
|
|
1118
1695
|
};
|
|
1119
|
-
}
|
|
1120
|
-
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
async openItemsSseSession(initialOffset, cursor, onOpen, signal, onActivity) {
|
|
1124
|
-
const result = await this.client.openSseSession({
|
|
1125
|
-
offset: initialOffset,
|
|
1126
|
-
cursor,
|
|
1127
|
-
signal,
|
|
1128
|
-
onActivity
|
|
1696
|
+
}).finally(() => {
|
|
1697
|
+
this.initPromise = null;
|
|
1129
1698
|
});
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
if (result.result.code === "bad_request" && isSseUnsupportedErrorText(result.result.message)) return { kind: "unsupported" };
|
|
1134
|
-
throw this.toTransportFailure(result.result);
|
|
1135
|
-
}
|
|
1136
|
-
const session = result.result;
|
|
1137
|
-
onOpen?.();
|
|
1138
|
-
let pendingDataEvents = [];
|
|
1139
|
-
let latestOffset = session.nextOffset ?? initialOffset;
|
|
1140
|
-
let latestCursor = session.cursor;
|
|
1141
|
-
let exhausted = false;
|
|
1142
|
-
return {
|
|
1143
|
-
kind: "ok",
|
|
1144
|
-
session: {
|
|
1145
|
-
continue: async (nextOnControl) => {
|
|
1146
|
-
if (exhausted) return {
|
|
1147
|
-
kind: "ok",
|
|
1148
|
-
latestOffset,
|
|
1149
|
-
latestCursor,
|
|
1150
|
-
paused: false
|
|
1151
|
-
};
|
|
1152
|
-
for await (const event of session.events) {
|
|
1153
|
-
if (event.event === "data") {
|
|
1154
|
-
pendingDataEvents.push(event.data);
|
|
1155
|
-
continue;
|
|
1156
|
-
}
|
|
1157
|
-
if (event.event !== "control") continue;
|
|
1158
|
-
const control = parseSseControlData(event.data);
|
|
1159
|
-
const updates = [];
|
|
1160
|
-
for (const eventData of pendingDataEvents) updates.push(...decodeSseItemPayloads(eventData, session.dataEncoding));
|
|
1161
|
-
pendingDataEvents = [];
|
|
1162
|
-
const handlerResult = await nextOnControl(updates, control.nextOffset, control.upToDate, control.streamCursor);
|
|
1163
|
-
latestOffset = control.nextOffset;
|
|
1164
|
-
if (control.streamCursor != null) latestCursor = control.streamCursor;
|
|
1165
|
-
if (handlerResult === "stop") {
|
|
1166
|
-
exhausted = true;
|
|
1167
|
-
await session.cancel();
|
|
1168
|
-
return {
|
|
1169
|
-
kind: "ok",
|
|
1170
|
-
latestOffset,
|
|
1171
|
-
latestCursor,
|
|
1172
|
-
paused: false
|
|
1173
|
-
};
|
|
1174
|
-
}
|
|
1175
|
-
if (handlerResult === "pause") return {
|
|
1176
|
-
kind: "ok",
|
|
1177
|
-
latestOffset,
|
|
1178
|
-
latestCursor,
|
|
1179
|
-
paused: true
|
|
1180
|
-
};
|
|
1181
|
-
}
|
|
1182
|
-
exhausted = true;
|
|
1183
|
-
return {
|
|
1184
|
-
kind: "ok",
|
|
1185
|
-
latestOffset,
|
|
1186
|
-
latestCursor,
|
|
1187
|
-
paused: false
|
|
1188
|
-
};
|
|
1189
|
-
},
|
|
1190
|
-
cancel: async () => {
|
|
1191
|
-
if (exhausted) return;
|
|
1192
|
-
exhausted = true;
|
|
1193
|
-
await session.cancel();
|
|
1194
|
-
}
|
|
1195
|
-
}
|
|
1196
|
-
};
|
|
1699
|
+
await this.initPromise;
|
|
1700
|
+
if (this.db == null) throw new Error("indexeddb open succeeded without a database handle");
|
|
1701
|
+
return this.db;
|
|
1197
1702
|
}
|
|
1198
1703
|
};
|
|
1704
|
+
/**
|
|
1705
|
+
* Creates a cursor value suitable for seeding a `RemoteCursorStore`.
|
|
1706
|
+
*/
|
|
1707
|
+
function createInitialRemoteCursor(input) {
|
|
1708
|
+
return {
|
|
1709
|
+
streamUrl: input.streamUrl,
|
|
1710
|
+
nextOffset: input.nextOffset,
|
|
1711
|
+
serverLowerBoundVersion: cloneJsonValue(input.serverLowerBoundVersion),
|
|
1712
|
+
updatedAtMs: Date.now()
|
|
1713
|
+
};
|
|
1714
|
+
}
|
|
1199
1715
|
//#endregion
|
|
1200
1716
|
//#region src/transport-cursor.ts
|
|
1201
1717
|
var TransportCursorManager = class {
|
|
@@ -1462,6 +1978,8 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
1462
1978
|
producerId = createProducerId();
|
|
1463
1979
|
producerEpoch = 0;
|
|
1464
1980
|
nextProducerSeq = 0;
|
|
1981
|
+
frozenLocalAppend;
|
|
1982
|
+
committedPendingCount;
|
|
1465
1983
|
pendingWriteOnly = new Deque();
|
|
1466
1984
|
writeOnlyCursor;
|
|
1467
1985
|
unsubscribeWriteOnlyLocal;
|
|
@@ -1671,6 +2189,8 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
1671
2189
|
await this.remoteCursorStore.delete?.(this.streamUrl);
|
|
1672
2190
|
this.producerEpoch = 0;
|
|
1673
2191
|
this.nextProducerSeq = 0;
|
|
2192
|
+
this.frozenLocalAppend = void 0;
|
|
2193
|
+
this.committedPendingCount = void 0;
|
|
1674
2194
|
return deleted;
|
|
1675
2195
|
}) }
|
|
1676
2196
|
};
|
|
@@ -1718,15 +2238,17 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
1718
2238
|
localVersion: this.writeOnlyCursor.serverLowerBoundVersion
|
|
1719
2239
|
}
|
|
1720
2240
|
};
|
|
1721
|
-
const
|
|
1722
|
-
|
|
1723
|
-
|
|
2241
|
+
const appended = await this.appendLocalBatchRemote(this.writeOnlyCursor, drained.batch, drained.count, "write-only");
|
|
2242
|
+
const committedCount = appended.pendingCount ?? drained.count;
|
|
2243
|
+
for (let i = 0; i < committedCount; i += 1) this.pendingWriteOnly.popFront();
|
|
2244
|
+
this.writeOnlyCursor = appended.cursor;
|
|
2245
|
+
this.commitProducerAck(appended.producerAck);
|
|
1724
2246
|
return {
|
|
1725
2247
|
ok: true,
|
|
1726
2248
|
value: {
|
|
1727
2249
|
appended: true,
|
|
1728
|
-
nextOffset:
|
|
1729
|
-
localVersion:
|
|
2250
|
+
nextOffset: appended.cursor.nextOffset,
|
|
2251
|
+
localVersion: appended.cursor.serverLowerBoundVersion
|
|
1730
2252
|
}
|
|
1731
2253
|
};
|
|
1732
2254
|
} catch (error) {
|
|
@@ -1902,7 +2424,7 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
1902
2424
|
}
|
|
1903
2425
|
}
|
|
1904
2426
|
async syncOnce() {
|
|
1905
|
-
let cursor = (await this.resolveInitialRemoteState()).cursor;
|
|
2427
|
+
let cursor = (await this.finalizeDirectLocalAppendIfNeeded(await this.resolveInitialRemoteState())).cursor;
|
|
1906
2428
|
const localBatch = this.exportUpdates(cursor.serverLowerBoundVersion);
|
|
1907
2429
|
if (localBatch.batch != null) {
|
|
1908
2430
|
cursor = await this.appendLocalBatch(cursor, localBatch.batch);
|
|
@@ -1911,7 +2433,7 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
1911
2433
|
return cursor;
|
|
1912
2434
|
}
|
|
1913
2435
|
async performInitialJoinSync(preferredLiveMode) {
|
|
1914
|
-
const initial = await this.resolveInitialRemoteState();
|
|
2436
|
+
const initial = await this.finalizeDirectLocalAppendIfNeeded(await this.resolveInitialRemoteState());
|
|
1915
2437
|
let cursor = initial.cursor;
|
|
1916
2438
|
let localExportRefVersion = cursor.serverLowerBoundVersion;
|
|
1917
2439
|
let streamCursor = initial.streamCursor;
|
|
@@ -2007,17 +2529,52 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
2007
2529
|
};
|
|
2008
2530
|
}
|
|
2009
2531
|
}
|
|
2010
|
-
async appendLocalBatch(cursor, batch,
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2532
|
+
async appendLocalBatch(cursor, batch, pendingCount) {
|
|
2533
|
+
const source = pendingCount == null ? "direct" : "pending";
|
|
2534
|
+
const committed = await this.appendLocalBatchDurably(cursor, batch, pendingCount, source);
|
|
2535
|
+
this.committedPendingCount = committed.pendingCount;
|
|
2536
|
+
return committed.cursor;
|
|
2537
|
+
}
|
|
2538
|
+
async appendLocalBatchDurably(cursor, batch, pendingCount, source = pendingCount == null ? "direct" : "pending") {
|
|
2539
|
+
const frozen = await this.getOrCreateFrozenLocalAppend(cursor, batch, pendingCount, source);
|
|
2540
|
+
const appended = await this.appendFrozenLocalBatchRemote(frozen);
|
|
2541
|
+
const saved = await this.saveRemoteCursor(appended.cursor, "local");
|
|
2542
|
+
this.commitProducerAck(appended.producerAck);
|
|
2543
|
+
return {
|
|
2544
|
+
cursor: saved,
|
|
2545
|
+
pendingCount: frozen.pendingCount
|
|
2546
|
+
};
|
|
2547
|
+
}
|
|
2548
|
+
async appendLocalBatchRemote(cursor, batch, pendingCount, source = pendingCount == null ? "direct" : "pending") {
|
|
2549
|
+
const frozen = await this.getOrCreateFrozenLocalAppend(cursor, batch, pendingCount, source);
|
|
2550
|
+
return {
|
|
2551
|
+
...await this.appendFrozenLocalBatchRemote(frozen),
|
|
2552
|
+
pendingCount: frozen.pendingCount
|
|
2553
|
+
};
|
|
2554
|
+
}
|
|
2555
|
+
async getOrCreateFrozenLocalAppend(cursor, batch, pendingCount, source = pendingCount == null ? "direct" : "pending") {
|
|
2556
|
+
const existing = this.frozenLocalAppend;
|
|
2557
|
+
if (existing != null) {
|
|
2558
|
+
if (existing.source !== source) throw new Error(`${existing.source} local append must be finalized before ${source} append`);
|
|
2559
|
+
return existing;
|
|
2560
|
+
}
|
|
2561
|
+
const frozen = {
|
|
2562
|
+
source,
|
|
2563
|
+
baseCursor: cursor,
|
|
2564
|
+
batch,
|
|
2565
|
+
pendingCount,
|
|
2566
|
+
wireUpdates: this.encodeUpdateItems == null ? batch.updates : await this.encodeUpdateItems(batch.updates),
|
|
2567
|
+
producerSession: this.createCurrentProducerSession()
|
|
2015
2568
|
};
|
|
2569
|
+
this.frozenLocalAppend = frozen;
|
|
2570
|
+
return frozen;
|
|
2571
|
+
}
|
|
2572
|
+
async appendFrozenLocalBatchRemote(frozen) {
|
|
2573
|
+
if (frozen.acknowledged != null) return frozen.acknowledged;
|
|
2016
2574
|
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
2017
2575
|
let result;
|
|
2018
2576
|
try {
|
|
2019
|
-
|
|
2020
|
-
result = await this.client.appendUpdates(wireUpdates, producerSession);
|
|
2577
|
+
result = await this.client.appendUpdates(frozen.wireUpdates, frozen.producerSession);
|
|
2021
2578
|
} catch (error) {
|
|
2022
2579
|
if (isStreamNotFoundError(error) && this.createStreamIfMissing) {
|
|
2023
2580
|
await this.ensureStreamExists();
|
|
@@ -2026,30 +2583,66 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
2026
2583
|
throw error;
|
|
2027
2584
|
}
|
|
2028
2585
|
if (result.kind === "ok") {
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2586
|
+
const appended = {
|
|
2587
|
+
cursor: {
|
|
2588
|
+
...frozen.baseCursor,
|
|
2589
|
+
nextOffset: result.nextOffset,
|
|
2590
|
+
serverLowerBoundVersion: this.adapter.mergeVersions(frozen.baseCursor.serverLowerBoundVersion, frozen.batch.version)
|
|
2591
|
+
},
|
|
2592
|
+
producerAck: {
|
|
2593
|
+
epoch: result.ackEpoch,
|
|
2594
|
+
seq: result.ackSeq
|
|
2595
|
+
}
|
|
2035
2596
|
};
|
|
2036
|
-
|
|
2597
|
+
frozen.acknowledged = appended;
|
|
2598
|
+
return appended;
|
|
2037
2599
|
}
|
|
2038
2600
|
if (result.kind === "seq-gap") {
|
|
2039
|
-
producerSession = {
|
|
2040
|
-
...producerSession,
|
|
2601
|
+
frozen.producerSession = {
|
|
2602
|
+
...frozen.producerSession,
|
|
2041
2603
|
nextProducerSeq: result.expectedSeq
|
|
2042
2604
|
};
|
|
2043
2605
|
continue;
|
|
2044
2606
|
}
|
|
2045
|
-
producerSession = {
|
|
2046
|
-
...producerSession,
|
|
2607
|
+
frozen.producerSession = {
|
|
2608
|
+
...frozen.producerSession,
|
|
2047
2609
|
producerEpoch: result.serverEpoch + 1,
|
|
2048
2610
|
nextProducerSeq: 0
|
|
2049
2611
|
};
|
|
2050
2612
|
}
|
|
2051
2613
|
throw new Error("append retry budget exhausted");
|
|
2052
2614
|
}
|
|
2615
|
+
async finalizeDirectLocalAppendIfNeeded(state) {
|
|
2616
|
+
const frozen = this.frozenLocalAppend;
|
|
2617
|
+
if (frozen == null || frozen.pendingCount != null) return state;
|
|
2618
|
+
const appended = await this.appendFrozenLocalBatchRemote(frozen);
|
|
2619
|
+
const saved = await this.saveRemoteCursor(appended.cursor, "local");
|
|
2620
|
+
this.commitProducerAck(appended.producerAck);
|
|
2621
|
+
return await this.catchup(saved, void 0, state.streamCursor);
|
|
2622
|
+
}
|
|
2623
|
+
createCurrentProducerSession() {
|
|
2624
|
+
return {
|
|
2625
|
+
producerId: this.producerId,
|
|
2626
|
+
producerEpoch: this.producerEpoch,
|
|
2627
|
+
nextProducerSeq: this.nextProducerSeq
|
|
2628
|
+
};
|
|
2629
|
+
}
|
|
2630
|
+
commitProducerAck(ack) {
|
|
2631
|
+
this.producerEpoch = ack.epoch;
|
|
2632
|
+
this.nextProducerSeq = ack.seq + 1;
|
|
2633
|
+
this.frozenLocalAppend = void 0;
|
|
2634
|
+
}
|
|
2635
|
+
abandonFrozenLocalAppend(frozen) {
|
|
2636
|
+
if (this.frozenLocalAppend !== frozen) return;
|
|
2637
|
+
this.frozenLocalAppend = void 0;
|
|
2638
|
+
this.producerEpoch = Math.max(this.producerEpoch, frozen.producerSession.producerEpoch + 1);
|
|
2639
|
+
this.nextProducerSeq = 0;
|
|
2640
|
+
}
|
|
2641
|
+
consumeCommittedPendingCount() {
|
|
2642
|
+
const count = this.committedPendingCount;
|
|
2643
|
+
this.committedPendingCount = void 0;
|
|
2644
|
+
return count;
|
|
2645
|
+
}
|
|
2053
2646
|
/**
|
|
2054
2647
|
* Ensures the target stream exists, creating it if necessary.
|
|
2055
2648
|
* Tolerates 409 Conflict (stream already created concurrently).
|
|
@@ -2097,9 +2690,17 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
2097
2690
|
*/
|
|
2098
2691
|
async inferRemoteAndAppendLocal(bootstrapCursor, cursorManager) {
|
|
2099
2692
|
const inferredRemote = await this.bootstrapState(bootstrapCursor, { cursorManager });
|
|
2100
|
-
|
|
2101
|
-
if (
|
|
2102
|
-
|
|
2693
|
+
let pendingAppend;
|
|
2694
|
+
if (this.frozenLocalAppend != null) {
|
|
2695
|
+
if (this.frozenLocalAppend.pendingCount != null) throw new Error("pending local append must be finalized by pending flush");
|
|
2696
|
+
pendingAppend = await this.appendFrozenLocalBatchRemote(this.frozenLocalAppend);
|
|
2697
|
+
} else {
|
|
2698
|
+
const localBatch = this.exportUpdates(inferredRemote.cursor.serverLowerBoundVersion);
|
|
2699
|
+
if (localBatch.batch != null) pendingAppend = await this.appendLocalBatchRemote(inferredRemote.cursor, localBatch.batch);
|
|
2700
|
+
}
|
|
2701
|
+
const result = await this.bootstrapState(bootstrapCursor);
|
|
2702
|
+
if (pendingAppend != null) this.commitProducerAck(pendingAppend.producerAck);
|
|
2703
|
+
return result;
|
|
2103
2704
|
}
|
|
2104
2705
|
async recoverFromGoneWithLocalBootstrap(cursor) {
|
|
2105
2706
|
const isolatedCursorManager = await this.createIsolatedCursorManager();
|
|
@@ -2115,9 +2716,17 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
2115
2716
|
const inferredRemote = await this.bootstrapState(bootstrapCursor, { cursorManager: this.volatileCursorManager });
|
|
2116
2717
|
await this.adapter.applySnapshot(preservedLocalSnapshot);
|
|
2117
2718
|
restoredLocalSnapshot = true;
|
|
2118
|
-
|
|
2119
|
-
if (
|
|
2120
|
-
|
|
2719
|
+
let pendingAppend;
|
|
2720
|
+
if (this.frozenLocalAppend != null) {
|
|
2721
|
+
if (this.frozenLocalAppend.pendingCount != null) throw new Error("pending local append must be finalized by pending flush");
|
|
2722
|
+
pendingAppend = await this.appendFrozenLocalBatchRemote(this.frozenLocalAppend);
|
|
2723
|
+
} else {
|
|
2724
|
+
const localBatch = this.exportUpdates(inferredRemote.cursor.serverLowerBoundVersion);
|
|
2725
|
+
if (localBatch.batch != null) pendingAppend = await this.appendLocalBatchRemote(inferredRemote.cursor, localBatch.batch);
|
|
2726
|
+
}
|
|
2727
|
+
const result = await this.bootstrapState(bootstrapCursor);
|
|
2728
|
+
if (pendingAppend != null) this.commitProducerAck(pendingAppend.producerAck);
|
|
2729
|
+
return result;
|
|
2121
2730
|
} catch (error) {
|
|
2122
2731
|
if (!restoredLocalSnapshot) await this.adapter.applySnapshot(preservedLocalSnapshot);
|
|
2123
2732
|
throw error;
|
|
@@ -2332,7 +2941,6 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
2332
2941
|
let mergedVersion = first.version;
|
|
2333
2942
|
const allUpdates = Array.from(first.updates);
|
|
2334
2943
|
let count = 1;
|
|
2335
|
-
await this.measureAppendBodyByteLength(allUpdates);
|
|
2336
2944
|
while (count < pendingLocal.length) {
|
|
2337
2945
|
const next = pendingLocal.at(count);
|
|
2338
2946
|
const candidateUpdates = [...allUpdates, ...next.updates];
|
|
@@ -2368,9 +2976,10 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
2368
2976
|
if (drained == null) return;
|
|
2369
2977
|
let attempts = 0;
|
|
2370
2978
|
while (!state.closed) try {
|
|
2371
|
-
state.cursor = await this.enqueueExclusive(async () => this.appendLocalBatch(state.cursor, drained.batch));
|
|
2372
|
-
|
|
2373
|
-
|
|
2979
|
+
state.cursor = await this.enqueueExclusive(async () => this.appendLocalBatch(state.cursor, drained.batch, drained.count));
|
|
2980
|
+
const committedCount = this.consumeCommittedPendingCount() ?? drained.count;
|
|
2981
|
+
for (let i = 0; i < committedCount; i += 1) state.pendingLocal.popFront();
|
|
2982
|
+
this.notifySyncWaiters(state, committedCount);
|
|
2374
2983
|
this.setWriteSubStatus(state, "ok");
|
|
2375
2984
|
this.snapshotManager.schedule(state);
|
|
2376
2985
|
break;
|
|
@@ -2465,6 +3074,8 @@ var StreamsCrdt = class StreamsCrdt {
|
|
|
2465
3074
|
return updates.reduce((sum, update) => sum + 4 + update.byteLength, 0);
|
|
2466
3075
|
}
|
|
2467
3076
|
disableWriteOnlyMode(reason) {
|
|
3077
|
+
const frozen = this.frozenLocalAppend;
|
|
3078
|
+
if (frozen?.source === "write-only") this.abandonFrozenLocalAppend(frozen);
|
|
2468
3079
|
if (this.writeOnlyDisabledReason != null) return;
|
|
2469
3080
|
this.writeOnlyDisabledReason = reason;
|
|
2470
3081
|
this.pendingWriteOnly.clear();
|
|
@@ -2522,6 +3133,6 @@ function createStreamUrl(input) {
|
|
|
2522
3133
|
return `${baseUrl}/ds/${encodeURIComponent(input.bucketId)}/${encodeURIComponent(streamId)}`;
|
|
2523
3134
|
}
|
|
2524
3135
|
//#endregion
|
|
2525
|
-
export {
|
|
3136
|
+
export { InMemoryRemoteCursorStore as a, EphemeralStreamCrdt as c, StreamsCrdt as i, PayloadProtectionError as l, isValidBucketId as n, IndexedDbRemoteCursorStore as o, isValidRillId as r, createInitialRemoteCursor as s, createStreamUrl as t };
|
|
2526
3137
|
|
|
2527
|
-
//# sourceMappingURL=stream-id-
|
|
3138
|
+
//# sourceMappingURL=stream-id-CCPEBb6G.js.map
|