@pmate/chat-sdk 0.13.0-profile.7 → 0.13.0-profile.8
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/lib/runtime/profileDatabase.d.ts +1 -1
- package/lib/runtime/profileDatabase.js +3 -3
- package/lib/runtime/profileOwner.d.ts +2 -0
- package/lib/runtime/profileOwner.js +2 -2
- package/lib/runtime/profileStoreTypes.d.ts +1 -0
- package/lib/runtime/profileWorkerHost.d.ts +1 -0
- package/lib/runtime/profileWorkerHost.js +32 -1
- package/package.json +1 -1
|
@@ -14,7 +14,7 @@ export declare class ProfileDatabase extends Dexie {
|
|
|
14
14
|
indexedDB?: IDBFactory;
|
|
15
15
|
name?: string;
|
|
16
16
|
});
|
|
17
|
-
claimWriter(ownerId: string, now?: number, ttlMs?: number): Promise<ProfileWriterLease | undefined>;
|
|
17
|
+
claimWriter(ownerId: string, now?: number, ttlMs?: number, writerLock?: string): Promise<ProfileWriterLease | undefined>;
|
|
18
18
|
private assertWriter;
|
|
19
19
|
releaseWriter(ownerId: string, fence: number): Promise<void>;
|
|
20
20
|
initialize(ownerId: string, fence: number, state: Omit<ProfileCurrentState, "key" | "commitRevision">): Promise<ProfileCurrentState>;
|
|
@@ -24,17 +24,17 @@ export class ProfileDatabase extends Dexie {
|
|
|
24
24
|
});
|
|
25
25
|
this.on("versionchange", () => { this.close(); return false; });
|
|
26
26
|
}
|
|
27
|
-
async claimWriter(ownerId, now = Date.now(), ttlMs = 30000) {
|
|
27
|
+
async claimWriter(ownerId, now = Date.now(), ttlMs = 30000, writerLock) {
|
|
28
28
|
if (!ownerId || !Number.isSafeInteger(ttlMs) || ttlMs < 1)
|
|
29
29
|
throw Error("INVALID_PROFILE_LEASE");
|
|
30
30
|
return this.transaction("rw", this.inboxState, async () => {
|
|
31
31
|
const old = await this.inboxState.get("lease");
|
|
32
|
-
if (old && old.ownerId !== ownerId && old.expiresAt > now)
|
|
32
|
+
if (old && old.ownerId !== ownerId && old.expiresAt > now && (!writerLock || old.writerLock !== writerLock))
|
|
33
33
|
return undefined;
|
|
34
34
|
const fence = old ? old.ownerId === ownerId && old.expiresAt > now ? old.fence : old.fence + 1 : 1;
|
|
35
35
|
if (!Number.isSafeInteger(fence))
|
|
36
36
|
throw Error("PROFILE_FENCE_EXHAUSTED");
|
|
37
|
-
const lease = { key: "lease", ownerId, fence, expiresAt: now + ttlMs };
|
|
37
|
+
const lease = { key: "lease", ownerId, fence, expiresAt: now + ttlMs, ...(writerLock ? { writerLock } : {}) };
|
|
38
38
|
await this.inboxState.put(lease);
|
|
39
39
|
return lease;
|
|
40
40
|
});
|
|
@@ -8,6 +8,8 @@ export type ProfileOwnerOptions = {
|
|
|
8
8
|
epoch: string;
|
|
9
9
|
windowStart: number;
|
|
10
10
|
ownerId?: string;
|
|
11
|
+
/** Set only while the Worker holds the matching exclusive browser lock. */
|
|
12
|
+
writerLock?: string;
|
|
11
13
|
onCommitted?: (delta: ProfileDelta) => void;
|
|
12
14
|
onError?: (error: unknown) => void;
|
|
13
15
|
windowMs?: number;
|
|
@@ -29,7 +29,7 @@ export class ProfileOwner {
|
|
|
29
29
|
static async open(options) {
|
|
30
30
|
const owner = new ProfileOwner(options);
|
|
31
31
|
const ownerId = options.ownerId ?? crypto.randomUUID();
|
|
32
|
-
const lease = await options.database.claimWriter(ownerId);
|
|
32
|
+
const lease = await options.database.claimWriter(ownerId, undefined, undefined, options.writerLock);
|
|
33
33
|
if (!lease)
|
|
34
34
|
throw Error("PROFILE_OWNER_UNAVAILABLE");
|
|
35
35
|
owner.lease = lease;
|
|
@@ -163,7 +163,7 @@ export class ProfileOwner {
|
|
|
163
163
|
if (this.closed || this.lost)
|
|
164
164
|
return;
|
|
165
165
|
try {
|
|
166
|
-
const next = await this.options.database.claimWriter(this.lease.ownerId);
|
|
166
|
+
const next = await this.options.database.claimWriter(this.lease.ownerId, undefined, undefined, this.lease.writerLock);
|
|
167
167
|
if (!next || next.fence !== this.lease.fence) {
|
|
168
168
|
this.lost = true;
|
|
169
169
|
this.report(Error("PROFILE_WRITER_FENCED"));
|
|
@@ -20,6 +20,7 @@ export declare function installProfileWorker(port: ProfileMessagePort, options?:
|
|
|
20
20
|
close(): void;
|
|
21
21
|
};
|
|
22
22
|
fetch?: typeof fetch;
|
|
23
|
+
locks?: Pick<LockManager, "request"> | null;
|
|
23
24
|
}): () => Promise<void>;
|
|
24
25
|
/** The application passes its bundler-created Worker. Worker code and main-page
|
|
25
26
|
* code ship together, so protocol negotiation precedes any snapshot handoff. */
|
|
@@ -15,6 +15,7 @@ export function installProfileWorker(port, options = {}) {
|
|
|
15
15
|
let owner, database;
|
|
16
16
|
let channel;
|
|
17
17
|
let scope, opening;
|
|
18
|
+
let releaseWriterLock;
|
|
18
19
|
let disposers = [];
|
|
19
20
|
let feed, dispatcher, connection;
|
|
20
21
|
let network;
|
|
@@ -31,6 +32,8 @@ export function installProfileWorker(port, options = {}) {
|
|
|
31
32
|
await opening;
|
|
32
33
|
await stopNetwork();
|
|
33
34
|
await owner?.close();
|
|
35
|
+
await releaseWriterLock?.();
|
|
36
|
+
releaseWriterLock = undefined;
|
|
34
37
|
for (const off of disposers.splice(0))
|
|
35
38
|
off();
|
|
36
39
|
owner = undefined;
|
|
@@ -55,9 +58,16 @@ export function installProfileWorker(port, options = {}) {
|
|
|
55
58
|
network = config.network;
|
|
56
59
|
if (!opening) {
|
|
57
60
|
opening = (async () => {
|
|
61
|
+
// Browser-managed locks disappear with a terminated Worker. Only a
|
|
62
|
+
// lease tagged with this same lock can bypass its old TTL; legacy or
|
|
63
|
+
// lockless owners retain the normal expiry/fencing rules.
|
|
64
|
+
const locks = options.locks === undefined ? globalThis.navigator?.locks : options.locks;
|
|
65
|
+
const writerLock = `${nextScope}:writer`;
|
|
66
|
+
if (locks)
|
|
67
|
+
releaseWriterLock = await holdProfileWriterLock(locks, writerLock);
|
|
58
68
|
database = options.database?.(config.identity) ?? new ProfileDatabase(config.identity);
|
|
59
69
|
const stored = await database.inboxState.get("current");
|
|
60
|
-
owner = await ProfileOwner.open({ database, epoch: stored?.epoch ?? config.epoch ?? "uninitialized", windowStart: config.windowStart,
|
|
70
|
+
owner = await ProfileOwner.open({ database, writerLock: releaseWriterLock ? writerLock : undefined, epoch: stored?.epoch ?? config.epoch ?? "uninitialized", windowStart: config.windowStart,
|
|
61
71
|
onError: error => port.postMessage({ profileControl: "error", error: error instanceof Error ? error.message : "PROFILE_OWNER_FAILED" }) });
|
|
62
72
|
channel = options.channel?.(nextScope) ?? new BroadcastChannel(nextScope);
|
|
63
73
|
disposers.push(serveProfileOwner(owner, port, nextScope), serveProfileOwner(owner, channel, nextScope));
|
|
@@ -95,6 +105,8 @@ export function installProfileWorker(port, options = {}) {
|
|
|
95
105
|
channel = undefined;
|
|
96
106
|
await owner?.close();
|
|
97
107
|
owner = undefined;
|
|
108
|
+
await releaseWriterLock?.();
|
|
109
|
+
releaseWriterLock = undefined;
|
|
98
110
|
database?.close();
|
|
99
111
|
database = undefined;
|
|
100
112
|
opening = undefined;
|
|
@@ -111,6 +123,8 @@ export function installProfileWorker(port, options = {}) {
|
|
|
111
123
|
await opening;
|
|
112
124
|
await stopNetwork();
|
|
113
125
|
await owner?.close();
|
|
126
|
+
await releaseWriterLock?.();
|
|
127
|
+
releaseWriterLock = undefined;
|
|
114
128
|
port.removeEventListener("message", listener);
|
|
115
129
|
for (const off of disposers.splice(0))
|
|
116
130
|
off();
|
|
@@ -167,3 +181,20 @@ export async function connectProfileWorker(worker, configuration, timeoutMs = 30
|
|
|
167
181
|
}
|
|
168
182
|
return { replica, close: async () => { replica.close(); await host.close(); } };
|
|
169
183
|
}
|
|
184
|
+
/** A null result preserves TTL takeover when a frozen context still holds the
|
|
185
|
+
* browser lock. It never authorizes bypassing an unexpired database lease. */
|
|
186
|
+
async function holdProfileWriterLock(locks, name) {
|
|
187
|
+
return new Promise((resolve, reject) => {
|
|
188
|
+
let release;
|
|
189
|
+
const held = new Promise(done => { release = done; });
|
|
190
|
+
const request = locks.request(name, { mode: "exclusive", ifAvailable: true }, lock => {
|
|
191
|
+
if (!lock) {
|
|
192
|
+
resolve(undefined);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
resolve(async () => { release(); await request; });
|
|
196
|
+
return held;
|
|
197
|
+
});
|
|
198
|
+
void request.catch(reject);
|
|
199
|
+
});
|
|
200
|
+
}
|
package/package.json
CHANGED