@pmate/chat-sdk 0.12.0-next.20 → 0.12.0-next.22
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 +8 -0
- package/lib/runtime/batchedReadIndex.d.ts +11 -2
- package/lib/runtime/batchedReadIndex.js +21 -1
- package/lib/runtime/chatSession.d.ts +10 -0
- package/lib/runtime/chatSession.js +129 -21
- package/lib/runtime/durableSender.d.ts +28 -4
- package/lib/runtime/durableSender.js +154 -34
- package/lib/runtime/messageClient.d.ts +17 -1
- package/lib/runtime/messageClient.js +164 -39
- package/lib/runtime/messageReaders.d.ts +4 -0
- package/lib/runtime/messageReaders.js +12 -4
- package/lib/runtime/messageStore.d.ts +16 -3
- package/lib/runtime/messageStore.js +74 -14
- package/lib/runtime/readCounts.d.ts +3 -0
- package/lib/runtime/readCounts.js +23 -5
- package/lib/runtime/readIndex.d.ts +21 -2
- package/lib/runtime/readIndex.js +47 -2
- package/lib/runtime/readSync.js +12 -4
- package/lib/runtime/readingClient.d.ts +11 -0
- package/lib/runtime/readingClient.js +78 -11
- package/lib/runtime/responseCache.d.ts +15 -0
- package/lib/runtime/responseCache.js +64 -2
- package/package.json +1 -1
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
import { Dexie } from "dexie";
|
|
2
2
|
import { WindowedTasks } from "./coalescer.js";
|
|
3
|
+
export function isResponseCacheAuthorityError(error) {
|
|
4
|
+
return /RESPONSE_CACHE_(ACCESS_REVOKED|SCOPE_CHANGED|INVALIDATED)/.test(String(error));
|
|
5
|
+
}
|
|
6
|
+
export async function assertResponseCacheAccess(access, expectedVersion) {
|
|
7
|
+
if (!access)
|
|
8
|
+
return undefined;
|
|
9
|
+
const current = await access();
|
|
10
|
+
if (!current.allowed)
|
|
11
|
+
throw new Error("RESPONSE_CACHE_ACCESS_REVOKED");
|
|
12
|
+
if (expectedVersion !== undefined && current.version !== expectedVersion)
|
|
13
|
+
throw new Error("RESPONSE_CACHE_SCOPE_CHANGED");
|
|
14
|
+
return current.version;
|
|
15
|
+
}
|
|
3
16
|
/** Small response windows, isolated by identity. Epochs fence late writes after
|
|
4
17
|
* access withdrawal. No localStorage or synchronous writes on the read path. */
|
|
5
18
|
export class IndexedDbResponseCache {
|
|
6
19
|
scope;
|
|
20
|
+
options;
|
|
7
21
|
db;
|
|
8
22
|
rows;
|
|
9
23
|
epochs;
|
|
@@ -15,14 +29,43 @@ export class IndexedDbResponseCache {
|
|
|
15
29
|
closing;
|
|
16
30
|
constructor(scope, options = {}) {
|
|
17
31
|
this.scope = scope;
|
|
32
|
+
this.options = options;
|
|
18
33
|
this.db = new Dexie(options.name ?? "pmate-chat-response-windows", { indexedDB: options.indexedDB ?? globalThis.indexedDB, IDBKeyRange: globalThis.IDBKeyRange });
|
|
19
34
|
this.db.version(1).stores({ windows: "[scope+key],scope", epochs: "scope" });
|
|
20
35
|
this.rows = this.db.table("windows");
|
|
21
36
|
this.epochs = this.db.table("epochs");
|
|
22
37
|
}
|
|
38
|
+
/** Resolve shared message-scope authority before opening our transaction.
|
|
39
|
+
* A newer authority version retires response windows exactly once; delayed
|
|
40
|
+
* callers can never roll the authority backwards or delete recovered data. */
|
|
41
|
+
async synchronizeAccess() {
|
|
42
|
+
if (!this.options.access)
|
|
43
|
+
return;
|
|
44
|
+
const access = await this.options.access();
|
|
45
|
+
const stored = await this.epochs.get(this.scope);
|
|
46
|
+
const storedVersion = stored?.accessVersion ?? 0;
|
|
47
|
+
if (access.version < storedVersion)
|
|
48
|
+
throw new Error("RESPONSE_CACHE_SCOPE_CHANGED");
|
|
49
|
+
// Ordinary cache reads must not take an IndexedDB write lock. Only an
|
|
50
|
+
// authority transition needs the atomic epoch bump and range deletion.
|
|
51
|
+
if (access.version > storedVersion)
|
|
52
|
+
await this.db.transaction("rw", this.rows, this.epochs, async () => {
|
|
53
|
+
const current = await this.epochs.get(this.scope);
|
|
54
|
+
const version = current?.accessVersion ?? 0;
|
|
55
|
+
if (access.version < version)
|
|
56
|
+
throw new Error("RESPONSE_CACHE_SCOPE_CHANGED");
|
|
57
|
+
if (access.version > version) {
|
|
58
|
+
await this.epochs.put({ scope: this.scope, epoch: (current?.epoch ?? 0) + 1, accessVersion: access.version });
|
|
59
|
+
await this.rows.where("scope").equals(this.scope).delete();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
if (!access.allowed)
|
|
63
|
+
throw new Error("RESPONSE_CACHE_ACCESS_REVOKED");
|
|
64
|
+
}
|
|
23
65
|
async read(key) {
|
|
24
66
|
if (this.closed)
|
|
25
67
|
throw new Error("RESPONSE_CACHE_CLOSED");
|
|
68
|
+
await this.synchronizeAccess();
|
|
26
69
|
return this.db.transaction("r", this.rows, this.epochs, async () => {
|
|
27
70
|
const epoch = (await this.epochs.get(this.scope))?.epoch ?? 0;
|
|
28
71
|
const row = await this.rows.get([this.scope, key]);
|
|
@@ -41,13 +84,31 @@ export class IndexedDbResponseCache {
|
|
|
41
84
|
this.tasks.schedule("write", () => this.flush());
|
|
42
85
|
return promise;
|
|
43
86
|
}
|
|
87
|
+
/** Idempotent shared-scope retirement: an old cleanup cannot erase windows
|
|
88
|
+
* created after the same authority version was already recovered. */
|
|
89
|
+
retire(version) {
|
|
90
|
+
if (this.closed)
|
|
91
|
+
return Promise.reject(new Error("RESPONSE_CACHE_CLOSED"));
|
|
92
|
+
if (!Number.isSafeInteger(version) || version < 1)
|
|
93
|
+
return Promise.reject(new Error("INVALID_RESPONSE_SCOPE_VERSION"));
|
|
94
|
+
const task = this.db.transaction("rw", this.rows, this.epochs, async () => {
|
|
95
|
+
const current = await this.epochs.get(this.scope);
|
|
96
|
+
if ((current?.accessVersion ?? 0) >= version)
|
|
97
|
+
return;
|
|
98
|
+
await this.epochs.put({ scope: this.scope, epoch: (current?.epoch ?? 0) + 1, accessVersion: version });
|
|
99
|
+
await this.rows.where("scope").equals(this.scope).delete();
|
|
100
|
+
});
|
|
101
|
+
this.invalidations.add(task);
|
|
102
|
+
return task.finally(() => this.invalidations.delete(task));
|
|
103
|
+
}
|
|
44
104
|
invalidate() {
|
|
45
105
|
if (this.closed)
|
|
46
106
|
return Promise.reject(new Error("RESPONSE_CACHE_CLOSED"));
|
|
47
107
|
const task = (async () => {
|
|
48
108
|
await this.db.transaction("rw", this.rows, this.epochs, async () => {
|
|
49
|
-
const
|
|
50
|
-
|
|
109
|
+
const current = await this.epochs.get(this.scope);
|
|
110
|
+
const epoch = (current?.epoch ?? 0) + 1;
|
|
111
|
+
await this.epochs.put({ scope: this.scope, epoch, accessVersion: current?.accessVersion });
|
|
51
112
|
await this.rows.where("scope").equals(this.scope).delete();
|
|
52
113
|
});
|
|
53
114
|
})();
|
|
@@ -66,6 +127,7 @@ export class IndexedDbResponseCache {
|
|
|
66
127
|
return;
|
|
67
128
|
const task = (async () => {
|
|
68
129
|
try {
|
|
130
|
+
await this.synchronizeAccess();
|
|
69
131
|
const accepted = await this.db.transaction("rw", this.rows, this.epochs, async () => {
|
|
70
132
|
const epoch = (await this.epochs.get(this.scope))?.epoch ?? 0;
|
|
71
133
|
const valid = batch.filter(write => write.row.epoch === epoch);
|
package/package.json
CHANGED