@pmate/chat-sdk 0.12.0-next.2 → 0.12.0-next.21

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.
@@ -77,20 +77,43 @@ export declare function unreadInThread(state: ReadIndexState, threadId: string):
77
77
  unreadMentionCount: number;
78
78
  badgeCount: number;
79
79
  };
80
+ export type ReadIndexVersions = Readonly<Record<string, number>>;
81
+ export type ReadIndexTransform = (current: ReadIndexState, versions?: ReadIndexVersions) => ReadIndexState;
82
+ export declare function assertReadIndexVersions(expected: ReadIndexVersions, current?: ReadIndexVersions): void;
83
+ /** Remove only the withdrawn team's original sources, logical conversations and
84
+ * queued observations. Other teams' pending operations must survive. */
85
+ export declare function removeReadTeam(state: ReadIndexState, teamId: string, knownThreadIds?: readonly string[]): ReadIndexState;
80
86
  export interface ReadIndexPersistence {
81
87
  load(appId: string, profileId: string): Promise<ReadIndexState | undefined>;
88
+ loadVersioned?(appId: string, profileId: string): Promise<{
89
+ state: ReadIndexState | undefined;
90
+ versions: ReadIndexVersions;
91
+ }>;
92
+ loadVersions?(appId: string, profileId: string): Promise<ReadIndexVersions>;
93
+ invalidateTeam?(appId: string, profileId: string, teamId: string, version: number, knownThreadIds?: readonly string[]): Promise<ReadIndexState>;
82
94
  /** Transform and persist atomically; UI publishes only the committed result. */
83
- update(appId: string, profileId: string, transform: (current: ReadIndexState) => ReadIndexState): Promise<ReadIndexState>;
95
+ update(appId: string, profileId: string, transform: ReadIndexTransform): Promise<ReadIndexState>;
84
96
  }
85
97
  /** Dexie preserves the legacy database/store and outbound [appId, profileId] keys. */
86
98
  export declare class IndexedDbReadIndex implements ReadIndexPersistence {
99
+ private storageScope?;
100
+ private migrateLegacy;
87
101
  private database;
88
102
  private profiles;
103
+ private versions;
89
104
  private closed;
90
- constructor(factory?: IDBFactory, name?: string);
105
+ constructor(factory?: IDBFactory, name?: string, storageScope?: string | undefined, migrateLegacy?: boolean);
91
106
  private transaction;
107
+ private key;
108
+ private loadCurrent;
92
109
  load(appId: string, profileId: string): Promise<ReadIndexState | undefined>;
93
- update(appId: string, profileId: string, transform: (current: ReadIndexState) => ReadIndexState): Promise<ReadIndexState>;
110
+ loadVersioned(appId: string, profileId: string): Promise<{
111
+ state: ReadIndexState | undefined;
112
+ versions: Record<string, number>;
113
+ }>;
114
+ loadVersions(appId: string, profileId: string): Promise<ReadIndexVersions>;
115
+ invalidateTeam(appId: string, profileId: string, teamId: string, version: number, knownThreadIds?: readonly string[]): Promise<ReadIndexState>;
116
+ update(appId: string, profileId: string, transform: ReadIndexTransform): Promise<ReadIndexState>;
94
117
  close(): Promise<void>;
95
118
  }
96
119
  export declare function sourcesInConversation(state: ReadIndexState, conversationId: string): IndexedReadThread[];
@@ -164,18 +164,45 @@ export function unreadInThread(state, threadId) {
164
164
  const unreadMentionCount = messages.filter(message => message.mentions.includes(state.profileId)).length;
165
165
  return { unreadMessageCount, unreadMentionCount, badgeCount: thread.kind === "group" ? unreadMentionCount : unreadMessageCount };
166
166
  }
167
+ function readTeamVersion(versions, team) { return Object.hasOwn(versions, team) ? versions[team] : 0; }
168
+ export function assertReadIndexVersions(expected, current = {}) {
169
+ const teams = new Set([...Object.keys(expected), ...Object.keys(current)]);
170
+ if ([...teams].some(team => readTeamVersion(expected, team) !== readTeamVersion(current, team)))
171
+ throw new Error("READ_SCOPE_CHANGED");
172
+ }
173
+ /** Remove only the withdrawn team's original sources, logical conversations and
174
+ * queued observations. Other teams' pending operations must survive. */
175
+ export function removeReadTeam(state, teamId, knownThreadIds = []) {
176
+ const removed = new Set(knownThreadIds);
177
+ for (const thread of state.threads)
178
+ if (thread.teamId === teamId) {
179
+ removed.add(thread.id);
180
+ if (thread.conversationId)
181
+ removed.add(thread.conversationId);
182
+ }
183
+ return { ...state, syncCursor: "0", threads: state.threads.filter(thread => thread.teamId !== teamId && !removed.has(thread.id)),
184
+ pending: state.pending.filter(operation => !removed.has(operation.threadId)),
185
+ observedReadIntents: state.observedReadIntents?.filter(intent => !removed.has(intent.threadId)) };
186
+ }
167
187
  /** Dexie preserves the legacy database/store and outbound [appId, profileId] keys. */
168
188
  export class IndexedDbReadIndex {
189
+ storageScope;
190
+ migrateLegacy;
169
191
  database;
170
192
  profiles;
193
+ versions;
171
194
  closed = false;
172
- constructor(factory = globalThis.indexedDB, name = "pmate-social-read-index") {
195
+ constructor(factory = globalThis.indexedDB, name = "pmate-social-read-index", storageScope, migrateLegacy = false) {
196
+ this.storageScope = storageScope;
197
+ this.migrateLegacy = migrateLegacy;
173
198
  if (!factory)
174
199
  throw new Error("READ_STORAGE_UNAVAILABLE");
175
200
  this.database = new Dexie(name, { indexedDB: factory, IDBKeyRange: globalThis.IDBKeyRange });
176
201
  // The empty schema retains the legacy out-of-line primary key and records.
177
202
  this.database.version(1).stores({ profiles: "" });
203
+ this.database.version(2).stores({ profiles: "", versions: "" });
178
204
  this.profiles = this.database.table("profiles");
205
+ this.versions = this.database.table("versions");
179
206
  this.database.on("versionchange", () => { this.database.close({ disableAutoOpen: false }); return false; });
180
207
  }
181
208
  async transaction(mode, execute) {
@@ -184,7 +211,7 @@ export class IndexedDbReadIndex {
184
211
  throw new Error("READ_STORAGE_CLOSED");
185
212
  let started = false;
186
213
  try {
187
- return await this.database.transaction(mode, this.profiles, async () => {
214
+ return await this.database.transaction(mode, this.profiles, this.versions, async () => {
188
215
  if (this.closed)
189
216
  throw new Error("READ_STORAGE_CLOSED");
190
217
  started = true;
@@ -202,17 +229,59 @@ export class IndexedDbReadIndex {
202
229
  }
203
230
  throw new Error("READ_STORAGE_UNAVAILABLE");
204
231
  }
232
+ key(appId, profileId) { return this.storageScope === undefined ? [appId, profileId] : [appId, profileId, this.storageScope]; }
233
+ async loadCurrent(appId, profileId) {
234
+ const current = await this.profiles.get(this.key(appId, profileId));
235
+ if (current || !this.migrateLegacy || this.storageScope === undefined)
236
+ return current;
237
+ const markerKey = [appId, profileId, "legacy-owner-v1"];
238
+ const marker = await this.profiles.get(markerKey);
239
+ if (marker && marker.legacyOwner !== this.storageScope)
240
+ return undefined;
241
+ const legacy = await this.profiles.get([appId, profileId]);
242
+ if (!legacy)
243
+ return undefined;
244
+ if (legacy.appId !== appId || legacy.profileId !== profileId || legacy.schemaVersion !== 1)
245
+ throw new Error("READ_IDENTITY_MISMATCH");
246
+ const ownership = { ...createReadIndex(appId, profileId), legacyOwner: this.storageScope };
247
+ await this.profiles.put(ownership, markerKey);
248
+ await this.profiles.put(legacy, this.key(appId, profileId));
249
+ await this.profiles.delete([appId, profileId]);
250
+ return legacy;
251
+ }
205
252
  async load(appId, profileId) {
206
- return this.transaction("r", () => this.profiles.get([appId, profileId]));
253
+ return this.transaction(this.migrateLegacy ? "rw" : "r", () => this.loadCurrent(appId, profileId));
254
+ }
255
+ async loadVersioned(appId, profileId) {
256
+ return this.transaction(this.migrateLegacy ? "rw" : "r", async () => ({
257
+ state: await this.loadCurrent(appId, profileId), versions: (await this.versions.get(this.key(appId, profileId)))?.teams ?? {},
258
+ }));
259
+ }
260
+ async loadVersions(appId, profileId) {
261
+ return this.transaction("r", async () => (await this.versions.get(this.key(appId, profileId)))?.teams ?? {});
262
+ }
263
+ async invalidateTeam(appId, profileId, teamId, version, knownThreadIds = []) {
264
+ if (!isValidChatTeamId(teamId) || !Number.isSafeInteger(version) || version < 1)
265
+ throw new Error("INVALID_READ_SCOPE_VERSION");
266
+ return this.transaction("rw", async () => {
267
+ const state = await this.loadCurrent(appId, profileId) ?? createReadIndex(appId, profileId);
268
+ const teams = (await this.versions.get(this.key(appId, profileId)))?.teams ?? {};
269
+ if (readTeamVersion(teams, teamId) >= version)
270
+ return state;
271
+ const next = removeReadTeam(state, teamId, knownThreadIds);
272
+ await this.versions.put({ teams: { ...teams, [teamId]: version } }, this.key(appId, profileId));
273
+ await this.profiles.put(next, this.key(appId, profileId));
274
+ return next;
275
+ });
207
276
  }
208
277
  async update(appId, profileId, transform) {
209
278
  return this.transaction("rw", async () => {
210
- const previous = await this.profiles.get([appId, profileId]);
211
- const next = transform(previous ?? createReadIndex(appId, profileId));
279
+ const previous = await this.loadCurrent(appId, profileId);
280
+ const next = transform(previous ?? createReadIndex(appId, profileId), (await this.versions.get(this.key(appId, profileId)))?.teams ?? {});
212
281
  if (next.appId !== appId || next.profileId !== profileId || next.schemaVersion !== 1)
213
282
  throw new Error("READ_IDENTITY_MISMATCH");
214
283
  if (JSON.stringify(next) !== JSON.stringify(previous))
215
- await this.profiles.put(next, [appId, profileId]);
284
+ await this.profiles.put(next, this.key(appId, profileId));
216
285
  return next;
217
286
  });
218
287
  }
@@ -1,4 +1,4 @@
1
- import { applyReadChange, ReadSyncGap, createReadIndex, installReadSnapshot } from "./readIndex.js";
1
+ import { assertReadIndexVersions, applyReadChange, ReadSyncGap, createReadIndex, installReadSnapshot } from "./readIndex.js";
2
2
  /** Keeps local operations until an authoritative projection covers them. */
3
3
  export class ReadSyncClient {
4
4
  options;
@@ -46,7 +46,9 @@ export class ReadSyncClient {
46
46
  }
47
47
  async run() {
48
48
  const { appId, profileId, persistence } = this.options;
49
- const current = await this.loadLocal();
49
+ const loaded = persistence.loadVersioned ? await persistence.loadVersioned(appId, profileId) : { state: await persistence.load(appId, profileId), versions: {} };
50
+ const current = loaded.state ?? createReadIndex(appId, profileId);
51
+ const versions = loaded.versions;
50
52
  // Ordinary reads are monotonic: only the furthest position per thread needs
51
53
  // sending. Keep every original operation in storage until the projection
52
54
  // confirms it, and never rewrite an operation ID's payload during retries.
@@ -60,6 +62,8 @@ export class ReadSyncClient {
60
62
  }
61
63
  const outgoing = current.pending.filter(operation => operation.explicitReadMessageIds.length || operation.readThroughSeq <= 0 || latest.get(operation.threadId) === operation);
62
64
  for (const operation of outgoing) {
65
+ if (persistence.loadVersions)
66
+ assertReadIndexVersions(versions, await persistence.loadVersions(appId, profileId));
63
67
  const receipt = await this.request("/api/social/reading", {
64
68
  method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ ...operation, appId, profileId }),
65
69
  });
@@ -78,7 +82,8 @@ export class ReadSyncClient {
78
82
  try {
79
83
  if (this.controller.signal.aborted)
80
84
  throw new Error("READ_SYNC_STOPPED");
81
- const next = await persistence.update(appId, profileId, state => {
85
+ const next = await persistence.update(appId, profileId, (state, currentVersions) => {
86
+ assertReadIndexVersions(versions, currentVersions);
82
87
  const applied = delta.changes.reduce(applyReadChange, state);
83
88
  if (applied.syncCursor !== delta.syncCursor)
84
89
  throw new ReadSyncGap(applied.syncCursor);
@@ -100,7 +105,10 @@ export class ReadSyncClient {
100
105
  if (this.controller.signal.aborted)
101
106
  throw new Error("READ_SYNC_STOPPED");
102
107
  // Merge into the latest transaction, including reads made while HTTP was pending.
103
- const next = await persistence.update(appId, profileId, state => installReadSnapshot(state, snapshot));
108
+ const next = await persistence.update(appId, profileId, (state, currentVersions) => {
109
+ assertReadIndexVersions(versions, currentVersions);
110
+ return installReadSnapshot(state, snapshot);
111
+ });
104
112
  if (!next.pending.length)
105
113
  this.readAfter = 0;
106
114
  if (!this.controller.signal.aborted)
@@ -1,3 +1,4 @@
1
+ import { type ConversationReadMessage } from "./conversationRead.js";
1
2
  import { type ReadIndexPersistence, type ReadIndexState } from "./readIndex.js";
2
3
  export type ReadingClientOptions = {
3
4
  appId: string;
@@ -5,6 +6,8 @@ export type ReadingClientOptions = {
5
6
  sessionToken: string;
6
7
  hubHttpUrl: string;
7
8
  persistence?: ReadIndexPersistence;
9
+ storageScope?: string;
10
+ migrateLegacy?: boolean;
8
11
  fetch?: typeof fetch;
9
12
  onError?: (error: unknown) => void;
10
13
  onRecovered?: () => void;
@@ -14,6 +17,11 @@ export type ReadingClientOptions = {
14
17
  export declare class ReadingClient {
15
18
  private options;
16
19
  private storage;
20
+ private backing;
21
+ private closing?;
22
+ private revoking?;
23
+ private versions?;
24
+ private accepted;
17
25
  private persistence;
18
26
  private sync;
19
27
  private tasks;
@@ -33,8 +41,15 @@ export declare class ReadingClient {
33
41
  refresh: () => void;
34
42
  synchronize(targetCursor?: string, profileId?: string): Promise<ReadIndexState>;
35
43
  update(transform: (state: ReadIndexState) => ReadIndexState): Promise<ReadIndexState>;
44
+ prepareTeam(teamId: string, version: number): Promise<void>;
45
+ markRead(threadId: string, messages?: readonly ConversationReadMessage[]): Promise<ReadIndexState>;
36
46
  recordVisible(threadId: string, messageIds: string[]): Promise<ReadIndexState>;
37
47
  recordExplicit(threadId: string, messageIds: string[]): Promise<ReadIndexState>;
38
48
  private record;
49
+ withdraw(): void;
50
+ private stop;
51
+ /** This client is retired permanently. Drain accepted local observations
52
+ * before deleting one team's profile-wide projection; sibling teams survive. */
53
+ revokeTeam(teamId: string, version: number, knownThreadIds?: readonly string[]): Promise<void>;
39
54
  close(): Promise<void>;
40
55
  }
@@ -1,12 +1,18 @@
1
+ import { recordConversationRead } from "./conversationRead.js";
1
2
  import { BatchedReadIndex } from "./batchedReadIndex.js";
2
3
  import { WindowedTasks } from "./coalescer.js";
3
- import { IndexedDbReadIndex, recordReadIntent } from "./readIndex.js";
4
+ import { IndexedDbReadIndex, recordReadIntent, assertReadIndexVersions } from "./readIndex.js";
4
5
  import { ReadSyncClient } from "./readSync.js";
5
6
  /** Owns durable local reads, network coalescing, retries and state notifications.
6
7
  * Hosts report actual visibility; loading or subscribing never marks messages read. */
7
8
  export class ReadingClient {
8
9
  options;
9
10
  storage;
11
+ backing;
12
+ closing;
13
+ revoking;
14
+ versions;
15
+ accepted = new Set();
10
16
  persistence;
11
17
  sync;
12
18
  tasks;
@@ -19,7 +25,8 @@ export class ReadingClient {
19
25
  generation = 0;
20
26
  constructor(options) {
21
27
  this.options = options;
22
- const storage = options.persistence ?? (this.storage = new IndexedDbReadIndex());
28
+ const storage = options.persistence ?? (this.storage = new IndexedDbReadIndex(globalThis.indexedDB, "pmate-social-read-index", options.storageScope, options.migrateLegacy));
29
+ this.backing = storage;
23
30
  this.persistence = new BatchedReadIndex(storage);
24
31
  this.tasks = new WindowedTasks(1000, error => this.failed(error));
25
32
  this.sync = new ReadSyncClient({ ...options, persistence: this.persistence, onChange: state => this.publish(state) });
@@ -84,16 +91,40 @@ export class ReadingClient {
84
91
  }
85
92
  throw new Error("Reading projection has not reached the announced version");
86
93
  }
87
- async update(transform) {
94
+ update(transform) {
95
+ if (this.closed)
96
+ return Promise.reject(new Error("READING_CLIENT_CLOSED"));
97
+ // One shared metadata read per local authority generation, rather than one
98
+ // IndexedDB lookup for every viewport observation.
99
+ const versions = this.versions ??= this.persistence.loadVersions(this.options.appId, this.options.profileId);
100
+ const task = (async () => {
101
+ const expected = await versions;
102
+ const next = await this.persistence.update(this.options.appId, this.options.profileId, (state, currentVersions) => {
103
+ assertReadIndexVersions(expected, currentVersions);
104
+ return transform(state);
105
+ });
106
+ if (!this.closed) {
107
+ this.publish(next);
108
+ if (next.pending.length || next.observedReadIntents?.length)
109
+ this.refresh();
110
+ }
111
+ return next;
112
+ })().catch(error => { if (String(error).includes("READ_SCOPE_CHANGED"))
113
+ this.versions = undefined; throw error; });
114
+ this.accepted.add(task);
115
+ void task.finally(() => this.accepted.delete(task)).catch(() => { });
116
+ return task;
117
+ }
118
+ async prepareTeam(teamId, version) {
88
119
  if (this.closed)
89
120
  throw new Error("READING_CLIENT_CLOSED");
90
- const next = await this.persistence.update(this.options.appId, this.options.profileId, transform);
91
- if (!this.closed) {
92
- this.publish(next);
93
- if (next.pending.length || next.observedReadIntents?.length)
94
- this.refresh();
121
+ if (version > 0) {
122
+ await this.persistence.invalidateTeam(this.options.appId, this.options.profileId, teamId, version);
123
+ this.versions = undefined;
95
124
  }
96
- return next;
125
+ }
126
+ markRead(threadId, messages = []) {
127
+ return this.update(state => recordConversationRead(state, threadId, messages));
97
128
  }
98
129
  recordVisible(threadId, messageIds) { return this.record(threadId, messageIds, "visible"); }
99
130
  recordExplicit(threadId, messageIds) { return this.record(threadId, messageIds, "explicit"); }
@@ -101,15 +132,55 @@ export class ReadingClient {
101
132
  const messageIds = [...new Set(ids)];
102
133
  return this.update(state => messageIds.reduce((next, messageId) => recordReadIntent(next, { threadId, messageId, readPolicy, operationId: crypto.randomUUID() }), state));
103
134
  }
104
- async close() {
135
+ withdraw() { this.stop(); }
136
+ stop() {
137
+ if (this.closed)
138
+ return;
105
139
  this.closed = true;
106
140
  this.generation++;
107
141
  this.offWake?.();
108
142
  this.tasks.close();
109
143
  clearTimeout(this.retry);
110
144
  this.sync.stop();
145
+ this.state = undefined;
146
+ this.listeners.forEach(listener => listener());
111
147
  this.listeners.clear();
112
- await this.persistence.close();
113
- await this.storage?.close();
148
+ }
149
+ /** This client is retired permanently. Drain accepted local observations
150
+ * before deleting one team's profile-wide projection; sibling teams survive. */
151
+ revokeTeam(teamId, version, knownThreadIds = []) {
152
+ if (this.revoking)
153
+ return this.revoking;
154
+ if (this.closing)
155
+ return Promise.reject(new Error("READING_CLIENT_CLOSED"));
156
+ this.stop();
157
+ return this.revoking = (async () => {
158
+ try {
159
+ await Promise.allSettled([...this.accepted]);
160
+ await this.persistence.close();
161
+ if (!this.backing.invalidateTeam)
162
+ throw new Error("READ_SCOPE_INVALIDATION_UNAVAILABLE");
163
+ await this.backing.invalidateTeam(this.options.appId, this.options.profileId, teamId, version, knownThreadIds);
164
+ }
165
+ finally {
166
+ await this.storage?.close();
167
+ }
168
+ })();
169
+ }
170
+ close() {
171
+ if (this.revoking)
172
+ return this.revoking;
173
+ if (this.closing)
174
+ return this.closing;
175
+ this.stop();
176
+ return this.closing = (async () => {
177
+ try {
178
+ await Promise.allSettled([...this.accepted]);
179
+ await this.persistence.close();
180
+ }
181
+ finally {
182
+ await this.storage?.close();
183
+ }
184
+ })();
114
185
  }
115
186
  }
@@ -5,6 +5,8 @@ export type RecordCacheOptions<T> = {
5
5
  indexedDB?: IDBFactory;
6
6
  name?: string;
7
7
  onError?: (error: unknown) => void;
8
+ transactionBytes?: number;
9
+ queueBytes?: number;
8
10
  };
9
11
  /** In-memory control-event index backed by bounded asynchronous IndexedDB batches.
10
12
  * Legacy storage is read only during open; the hot path never uses localStorage. */
@@ -23,6 +25,11 @@ export declare class IndexedDbRecordCache<T> {
23
25
  private closing;
24
26
  private initialized;
25
27
  private running?;
28
+ private activeBytes;
29
+ private pendingBytes;
30
+ private activeRecords;
31
+ private transactionBytes;
32
+ private queueBytes;
26
33
  private channel?;
27
34
  private listeners;
28
35
  constructor(options: RecordCacheOptions<T>);
@@ -34,7 +41,9 @@ export declare class IndexedDbRecordCache<T> {
34
41
  subscribe(listener: () => void): () => void;
35
42
  private reload;
36
43
  upsert(incoming: T[]): Promise<void>;
44
+ private bytes;
37
45
  private write;
46
+ private writeBatch;
38
47
  flush(): Promise<void>;
39
48
  private runFlush;
40
49
  close(): Promise<void>;
@@ -17,17 +17,26 @@ export class IndexedDbRecordCache {
17
17
  closing = false;
18
18
  initialized = false;
19
19
  running;
20
+ activeBytes = 0;
21
+ pendingBytes = 0;
22
+ activeRecords = 0;
23
+ transactionBytes;
24
+ queueBytes;
20
25
  channel;
21
26
  listeners = new Set();
22
27
  constructor(options) {
23
28
  this.options = options;
29
+ this.transactionBytes = options.transactionBytes ?? 1_048_576;
30
+ this.queueBytes = options.queueBytes ?? 8_388_608;
31
+ if (!Number.isSafeInteger(this.transactionBytes) || this.transactionBytes < 1 || !Number.isSafeInteger(this.queueBytes) || this.queueBytes < this.transactionBytes)
32
+ throw new Error("INVALID_RECORD_CACHE_BYTE_LIMIT");
24
33
  this.db = new Dexie(options.name ?? "pmate-chat-records", { indexedDB: options.indexedDB ?? globalThis.indexedDB, IDBKeyRange: globalThis.IDBKeyRange });
25
34
  this.db.version(1).stores({ records: "[scope+id],scope", metadata: "scope" });
26
35
  this.records = this.db.table("records");
27
36
  this.metadata = this.db.table("metadata");
28
37
  this.tasks = new WindowedTasks(16, error => options.onError?.(error));
29
38
  if (typeof BroadcastChannel !== "undefined") {
30
- this.channel = new BroadcastChannel(`pmate-chat-records:${options.scope}`);
39
+ this.channel = new BroadcastChannel(`pmate-chat-records:${options.scope}${this.db.name === "pmate-chat-records" ? "" : `:${this.db.name}`}`);
31
40
  this.channel.onmessage = () => this.tasks.schedule("reload", () => this.reload());
32
41
  }
33
42
  }
@@ -83,30 +92,70 @@ export class IndexedDbRecordCache {
83
92
  await this.ready;
84
93
  if (this.closed || this.closing)
85
94
  throw new Error("RECORD_CACHE_CLOSED");
86
- if (incoming.length + this.pending.size > 4096)
95
+ if (incoming.length > 4096)
87
96
  throw new Error("RECORD_CACHE_BACKPRESSURE");
88
- let changed = false;
97
+ // Plan and validate the whole operation before publishing any in-memory state.
98
+ const staged = new Map();
89
99
  for (const value of incoming) {
90
- const key = this.options.key(value), old = this.values.get(key), next = this.options.merge(old, value);
100
+ const key = this.options.key(value), old = staged.get(key) ?? this.values.get(key);
101
+ const next = this.options.merge(old, value);
91
102
  if (JSON.stringify(old) === JSON.stringify(next))
92
103
  continue;
104
+ if (this.bytes(next) > this.transactionBytes)
105
+ throw new Error("RECORD_CACHE_RECORD_TOO_LARGE");
106
+ staged.set(key, structuredClone(next));
107
+ }
108
+ let nextBytes = this.pendingBytes, nextRecords = this.pending.size;
109
+ for (const [key, value] of staged) {
110
+ const previous = this.pending.get(key);
111
+ nextBytes += this.bytes(value) - (previous === undefined ? 0 : this.bytes(previous));
112
+ if (!this.pending.has(key))
113
+ nextRecords++;
114
+ }
115
+ if (nextRecords + this.activeRecords > 4096 || nextBytes + this.activeBytes > this.queueBytes)
116
+ throw new Error("RECORD_CACHE_BACKPRESSURE");
117
+ let changed = false;
118
+ for (const [key, next] of staged) {
93
119
  this.values.set(key, next);
94
120
  this.pending.set(key, next);
95
121
  changed = true;
96
122
  }
123
+ this.pendingBytes = nextBytes;
97
124
  if (changed)
98
125
  this.snapshotValue = [...this.values.values()];
99
- if (!changed && !this.pending.size)
126
+ if (!changed && !this.pending.size) {
127
+ await this.running;
100
128
  return;
129
+ }
101
130
  const committed = new Promise((resolve, reject) => this.waiters.push({ resolve, reject }));
102
131
  this.tasks.schedule("write", () => this.flush());
103
132
  return committed;
104
133
  }
134
+ bytes(value) { return new TextEncoder().encode(JSON.stringify({ scope: this.options.scope, id: this.options.key(value), value })).byteLength; }
105
135
  async write(values) {
136
+ let batch = [], bytes = 0;
137
+ for (const value of values) {
138
+ const size = this.bytes(value);
139
+ if (size > this.transactionBytes)
140
+ throw new Error("RECORD_CACHE_RECORD_TOO_LARGE");
141
+ if (batch.length && (batch.length === 128 || bytes + size > this.transactionBytes)) {
142
+ await this.writeBatch(batch);
143
+ batch = [];
144
+ bytes = 0;
145
+ }
146
+ batch.push(value);
147
+ bytes += size;
148
+ }
149
+ if (batch.length)
150
+ await this.writeBatch(batch);
151
+ }
152
+ async writeBatch(values) {
106
153
  await this.db.transaction("rw", this.records, async () => {
107
154
  const previous = await this.records.bulkGet(values.map(value => [this.options.scope, this.options.key(value)]));
108
155
  const records = values.map((value, index) => ({ scope: this.options.scope, id: this.options.key(value), value: this.options.merge(previous[index]?.value, value) }));
109
156
  const changed = records.filter((row, index) => JSON.stringify(previous[index]?.value) !== JSON.stringify(row.value));
157
+ if (changed.reduce((sum, row) => sum + this.bytes(row.value), 0) > this.transactionBytes)
158
+ throw new Error("RECORD_CACHE_MERGE_TOO_LARGE");
110
159
  if (changed.length)
111
160
  await this.records.bulkPut(changed);
112
161
  });
@@ -126,8 +175,11 @@ export class IndexedDbRecordCache {
126
175
  async runFlush() {
127
176
  const values = [...this.pending.values()], waiters = this.waiters.splice(0);
128
177
  this.pending.clear();
178
+ this.pendingBytes = 0;
129
179
  if (!values.length)
130
180
  return;
181
+ this.activeBytes = values.reduce((sum, value) => sum + this.bytes(value), 0);
182
+ this.activeRecords = values.length;
131
183
  try {
132
184
  for (let offset = 0; offset < values.length; offset += 128)
133
185
  await this.write(values.slice(offset, offset + 128));
@@ -142,9 +194,14 @@ export class IndexedDbRecordCache {
142
194
  const key = this.options.key(value);
143
195
  this.pending.set(key, this.options.merge(value, this.pending.get(key) ?? value));
144
196
  }
197
+ this.pendingBytes = [...this.pending.values()].reduce((sum, value) => sum + this.bytes(value), 0);
145
198
  waiters.forEach(waiter => waiter.reject(error));
146
199
  throw error;
147
200
  }
201
+ finally {
202
+ this.activeBytes = 0;
203
+ this.activeRecords = 0;
204
+ }
148
205
  }
149
206
  async close() {
150
207
  this.closing = true;
@@ -0,0 +1,44 @@
1
+ export type ResponseCacheAccess = () => Promise<{
2
+ version: number;
3
+ allowed: boolean;
4
+ }>;
5
+ export declare function isResponseCacheAuthorityError(error: unknown): boolean;
6
+ export declare function assertResponseCacheAccess(access?: ResponseCacheAccess, expectedVersion?: number): Promise<number | undefined>;
7
+ /** Small response windows, isolated by identity. Epochs fence late writes after
8
+ * access withdrawal. No localStorage or synchronous writes on the read path. */
9
+ export declare class IndexedDbResponseCache<T> {
10
+ private scope;
11
+ private options;
12
+ private db;
13
+ private rows;
14
+ private epochs;
15
+ private pending;
16
+ private tasks;
17
+ private running?;
18
+ private invalidations;
19
+ private closed;
20
+ private closing?;
21
+ constructor(scope: string, options?: {
22
+ indexedDB?: IDBFactory;
23
+ name?: string;
24
+ access?: ResponseCacheAccess;
25
+ });
26
+ /** Resolve shared message-scope authority before opening our transaction.
27
+ * A newer authority version retires response windows exactly once; delayed
28
+ * callers can never roll the authority backwards or delete recovered data. */
29
+ private synchronizeAccess;
30
+ read(key: string): Promise<{
31
+ epoch: number;
32
+ cached: {
33
+ value: T;
34
+ time: number;
35
+ } | undefined;
36
+ }>;
37
+ write(key: string, value: T, epoch: number): Promise<void>;
38
+ /** Idempotent shared-scope retirement: an old cleanup cannot erase windows
39
+ * created after the same authority version was already recovered. */
40
+ retire(version: number): Promise<void>;
41
+ invalidate(): Promise<void>;
42
+ flush(): Promise<void>;
43
+ close(): Promise<void>;
44
+ }