@pmate/chat-sdk 0.12.0-next.6 → 0.12.0-next.7
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 +5 -0
- package/lib/runtime/recordCache.d.ts +9 -0
- package/lib/runtime/recordCache.js +62 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -201,6 +201,11 @@ legacy cleanup happens after durable migration. Normal reads use memory and writ
|
|
|
201
201
|
are merged into bounded IndexedDB transactions. Cache errors must be surfaced by
|
|
202
202
|
the host, and pending operations are not reported as saved before their promise
|
|
203
203
|
resolves.
|
|
204
|
+
Control-event batches are bounded by both 128 records and 1 MiB per transaction;
|
|
205
|
+
the queue includes in-flight writes and is limited to 4096 records / 8 MiB.
|
|
206
|
+
`transactionBytes` and `queueBytes` can lower these limits. Oversized operations
|
|
207
|
+
fail before changing the memory snapshot. Repeating an in-flight write waits for
|
|
208
|
+
that physical write, including its failure, rather than acknowledging it early.
|
|
204
209
|
|
|
205
210
|
`DurableChatSender` saves the exact message created by `ChatClient.prepareChat`
|
|
206
211
|
before transmitting it. `retry(hash)` uses `sendPrepared` with the original hash
|
|
@@ -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
|
|
95
|
+
if (incoming.length > 4096)
|
|
87
96
|
throw new Error("RECORD_CACHE_BACKPRESSURE");
|
|
88
|
-
|
|
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 =
|
|
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;
|
package/package.json
CHANGED