@pmate/chat-sdk 0.13.0-profile.8 → 0.14.0-seq.0
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 +57 -0
- package/lib/profile.d.ts +1 -1
- package/lib/profile.js +1 -1
- package/lib/runtime/profileChat.d.ts +7 -0
- package/lib/runtime/profileChat.js +105 -20
- package/lib/runtime/profileCommitQueue.js +3 -3
- package/lib/runtime/profileCoordinator.d.ts +25 -20
- package/lib/runtime/profileCoordinator.js +146 -128
- package/lib/runtime/profileDatabase.d.ts +6 -9
- package/lib/runtime/profileDatabase.js +10 -39
- package/lib/runtime/profileGenerationBuilder.d.ts +2 -3
- package/lib/runtime/profileGenerationBuilder.js +23 -23
- package/lib/runtime/profileInboxFeed.js +3 -2
- package/lib/runtime/profileLegacyInbox.js +2 -1
- package/lib/runtime/profileMemoryStore.d.ts +9 -3
- package/lib/runtime/profileMemoryStore.js +50 -10
- package/lib/runtime/profileOutboxDispatcher.js +4 -1
- package/lib/runtime/profileOwner.d.ts +2 -7
- package/lib/runtime/profileOwner.js +18 -46
- package/lib/runtime/profileProjection.d.ts +19 -1
- package/lib/runtime/profileProjection.js +139 -18
- package/lib/runtime/profileStoreTypes.d.ts +4 -11
- package/lib/runtime/profileWindows.d.ts +2 -0
- package/lib/runtime/profileWindows.js +13 -3
- package/lib/runtime/{profileReplica.d.ts → profileWorkerClient.d.ts} +9 -26
- package/lib/runtime/{profileReplica.js → profileWorkerClient.js} +46 -97
- package/lib/runtime/profileWorkerHost.d.ts +3 -6
- package/lib/runtime/profileWorkerHost.js +23 -30
- package/lib/runtime/readSync.d.ts +1 -1
- package/lib/runtime/readSync.js +10 -6
- package/lib/runtime/sourceCursor.d.ts +6 -0
- package/lib/runtime/sourceCursor.js +7 -0
- package/package.json +2 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ProfileDatabase } from "./profileDatabase.js";
|
|
2
2
|
import { ProfileOwner } from "./profileOwner.js";
|
|
3
|
-
import {
|
|
3
|
+
import { ProfileWorkerClient, serveProfileWorker } from "./profileWorkerClient.js";
|
|
4
4
|
import { PROFILE_PROTOCOL_VERSION } from "./profileMemoryStore.js";
|
|
5
5
|
import { ProfileInboxFeed } from "./profileInboxFeed.js";
|
|
6
6
|
import { ProfileOutboxDispatcher } from "./profileOutboxDispatcher.js";
|
|
@@ -13,7 +13,6 @@ export const profileChannelName = (identity) => `pmate-profile:${JSON.stringify(
|
|
|
13
13
|
* Identity cannot change within a host; a Profile switch creates another host. */
|
|
14
14
|
export function installProfileWorker(port, options = {}) {
|
|
15
15
|
let owner, database;
|
|
16
|
-
let channel;
|
|
17
16
|
let scope, opening;
|
|
18
17
|
let releaseWriterLock;
|
|
19
18
|
let disposers = [];
|
|
@@ -38,8 +37,6 @@ export function installProfileWorker(port, options = {}) {
|
|
|
38
37
|
off();
|
|
39
38
|
owner = undefined;
|
|
40
39
|
opening = undefined;
|
|
41
|
-
channel?.close();
|
|
42
|
-
channel = undefined;
|
|
43
40
|
database?.close();
|
|
44
41
|
database = undefined;
|
|
45
42
|
reply(request.requestId, null);
|
|
@@ -58,19 +55,18 @@ export function installProfileWorker(port, options = {}) {
|
|
|
58
55
|
network = config.network;
|
|
59
56
|
if (!opening) {
|
|
60
57
|
opening = (async () => {
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
// lockless owners retain the normal expiry/fencing rules.
|
|
58
|
+
// Lifetime browser admission only: no persisted lease, expiry,
|
|
59
|
+
// renewal, election or handoff. Also excludes older lock-based clients.
|
|
64
60
|
const locks = options.locks === undefined ? globalThis.navigator?.locks : options.locks;
|
|
65
|
-
const writerLock = `${nextScope}:writer`;
|
|
66
61
|
if (locks)
|
|
67
|
-
releaseWriterLock = await holdProfileWriterLock(locks,
|
|
62
|
+
releaseWriterLock = await holdProfileWriterLock(locks, `${nextScope}:writer`);
|
|
63
|
+
else if (options.locks !== null)
|
|
64
|
+
throw Error("PROFILE_LOCKS_UNAVAILABLE");
|
|
68
65
|
database = options.database?.(config.identity) ?? new ProfileDatabase(config.identity);
|
|
69
66
|
const stored = await database.inboxState.get("current");
|
|
70
|
-
owner = await ProfileOwner.open({ database,
|
|
67
|
+
owner = await ProfileOwner.open({ database, preparePresentation: false, epoch: stored?.epoch ?? config.epoch ?? "uninitialized", windowStart: config.windowStart,
|
|
71
68
|
onError: error => port.postMessage({ profileControl: "error", error: error instanceof Error ? error.message : "PROFILE_OWNER_FAILED" }) });
|
|
72
|
-
|
|
73
|
-
disposers.push(serveProfileOwner(owner, port, nextScope), serveProfileOwner(owner, channel, nextScope));
|
|
69
|
+
disposers.push(serveProfileWorker(owner, port, nextScope));
|
|
74
70
|
if (network) {
|
|
75
71
|
const report = (error) => port.postMessage({ profileControl: "error", error: error instanceof Error ? error.message : "PROFILE_NETWORK_FAILED" });
|
|
76
72
|
const endpoint = network.hubHttpUrl.replace(/\/$/, "").replace(/^ws/, "http");
|
|
@@ -79,7 +75,6 @@ export function installProfileWorker(port, options = {}) {
|
|
|
79
75
|
network = { ...network, token: value };
|
|
80
76
|
const message = { profileControl: "credentials", token: value };
|
|
81
77
|
port.postMessage(message);
|
|
82
|
-
channel?.postMessage(message);
|
|
83
78
|
});
|
|
84
79
|
connection = new ChatClient({ appId: config.identity.appId, profileId: config.identity.profileId,
|
|
85
80
|
hubUrl: endpoint.replace(/^http/, "ws"), fetchImpl: options.fetch, getToken: async () => token(),
|
|
@@ -89,7 +84,7 @@ export function installProfileWorker(port, options = {}) {
|
|
|
89
84
|
await migrateLegacyOutbox(owner);
|
|
90
85
|
await migrateLegacyReading(owner);
|
|
91
86
|
},
|
|
92
|
-
onProgress: progress => { const message = { profileControl: "progress", progress }; port.postMessage(message);
|
|
87
|
+
onProgress: progress => { const message = { profileControl: "progress", progress }; port.postMessage(message); } });
|
|
93
88
|
owner.setSynchronizer(() => feed.synchronize());
|
|
94
89
|
dispatcher = new ProfileOutboxDispatcher({ owner, hubHttpUrl: endpoint, getToken: token, fetch: options.fetch,
|
|
95
90
|
uploadEndpoint: network.uploadEndpoint,
|
|
@@ -101,8 +96,6 @@ export function installProfileWorker(port, options = {}) {
|
|
|
101
96
|
await stopNetwork();
|
|
102
97
|
for (const off of disposers.splice(0))
|
|
103
98
|
off();
|
|
104
|
-
channel?.close();
|
|
105
|
-
channel = undefined;
|
|
106
99
|
await owner?.close();
|
|
107
100
|
owner = undefined;
|
|
108
101
|
await releaseWriterLock?.();
|
|
@@ -128,23 +121,24 @@ export function installProfileWorker(port, options = {}) {
|
|
|
128
121
|
port.removeEventListener("message", listener);
|
|
129
122
|
for (const off of disposers.splice(0))
|
|
130
123
|
off();
|
|
131
|
-
channel?.close();
|
|
132
124
|
database?.close();
|
|
133
125
|
};
|
|
134
126
|
}
|
|
135
127
|
/** The application passes its bundler-created Worker. Worker code and main-page
|
|
136
128
|
* code ship together, so protocol negotiation precedes any snapshot handoff. */
|
|
137
|
-
export async function openProfileWorker(worker, configuration, timeoutMs = 30000) {
|
|
129
|
+
export async function openProfileWorker(worker, configuration, timeoutMs = 30000, signal) {
|
|
138
130
|
async function control(action, config = configuration) {
|
|
131
|
+
signal?.throwIfAborted();
|
|
139
132
|
const requestId = crypto.randomUUID();
|
|
140
133
|
return new Promise((resolve, reject) => {
|
|
141
|
-
const
|
|
134
|
+
const cleanup = () => { clearTimeout(timer); worker.removeEventListener("message", receive); signal?.removeEventListener("abort", abort); };
|
|
135
|
+
const abort = () => { cleanup(); reject(signal.reason); };
|
|
136
|
+
const timer = setTimeout(() => { cleanup(); reject(Error("PROFILE_WORKER_TIMEOUT")); }, timeoutMs);
|
|
142
137
|
const receive = (event) => {
|
|
143
138
|
const response = event.data;
|
|
144
139
|
if (response?.profileControl !== "result" || response.requestId !== requestId)
|
|
145
140
|
return;
|
|
146
|
-
|
|
147
|
-
worker.removeEventListener("message", receive);
|
|
141
|
+
cleanup();
|
|
148
142
|
if (response.protocolVersion !== PROFILE_PROTOCOL_VERSION)
|
|
149
143
|
reject(Error("PROFILE_PROTOCOL_INCOMPATIBLE"));
|
|
150
144
|
else if (response.error)
|
|
@@ -153,13 +147,13 @@ export async function openProfileWorker(worker, configuration, timeoutMs = 30000
|
|
|
153
147
|
resolve();
|
|
154
148
|
};
|
|
155
149
|
worker.addEventListener("message", receive);
|
|
150
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
156
151
|
worker.start?.();
|
|
157
152
|
try {
|
|
158
153
|
worker.postMessage({ profileControl: action, requestId, configuration: config, protocolVersion: PROFILE_PROTOCOL_VERSION });
|
|
159
154
|
}
|
|
160
155
|
catch (error) {
|
|
161
|
-
|
|
162
|
-
worker.removeEventListener("message", receive);
|
|
156
|
+
cleanup();
|
|
163
157
|
reject(error);
|
|
164
158
|
}
|
|
165
159
|
});
|
|
@@ -170,26 +164,25 @@ export async function openProfileWorker(worker, configuration, timeoutMs = 30000
|
|
|
170
164
|
export async function connectProfileWorker(worker, configuration, timeoutMs = 30000) {
|
|
171
165
|
const scope = profileChannelName(configuration.identity);
|
|
172
166
|
const host = await openProfileWorker(worker, configuration, timeoutMs);
|
|
173
|
-
const
|
|
167
|
+
const client = new ProfileWorkerClient(worker, scope, undefined, timeoutMs, configuration.identity.profileId);
|
|
174
168
|
try {
|
|
175
|
-
await
|
|
169
|
+
await client.start();
|
|
176
170
|
}
|
|
177
171
|
catch (error) {
|
|
178
|
-
|
|
172
|
+
client.close();
|
|
179
173
|
await host.close();
|
|
180
174
|
throw error;
|
|
181
175
|
}
|
|
182
|
-
return {
|
|
176
|
+
return { client, close: async () => { client.close(); await host.close(); } };
|
|
183
177
|
}
|
|
184
|
-
/**
|
|
185
|
-
* browser lock. It never authorizes bypassing an unexpired database lease. */
|
|
178
|
+
/** Refuse a second document instead of creating a distributed replica. */
|
|
186
179
|
async function holdProfileWriterLock(locks, name) {
|
|
187
180
|
return new Promise((resolve, reject) => {
|
|
188
181
|
let release;
|
|
189
182
|
const held = new Promise(done => { release = done; });
|
|
190
183
|
const request = locks.request(name, { mode: "exclusive", ifAvailable: true }, lock => {
|
|
191
184
|
if (!lock) {
|
|
192
|
-
|
|
185
|
+
reject(Error("PROFILE_ALREADY_OPEN"));
|
|
193
186
|
return;
|
|
194
187
|
}
|
|
195
188
|
resolve(async () => { release(); await request; });
|
package/lib/runtime/readSync.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { sourceCursor } from "./sourceCursor.js";
|
|
1
2
|
import { readingMetadata, resolveObservedReads, assertReadIndexVersions, applyReadChange, ReadSyncGap, createReadIndex, installReadSnapshot } from "./readIndex.js";
|
|
2
3
|
/** Keeps local operations until an authoritative projection covers them. */
|
|
3
4
|
export class ReadSyncClient {
|
|
4
5
|
options;
|
|
5
6
|
controller = new AbortController();
|
|
6
7
|
inFlight;
|
|
7
|
-
readAfter
|
|
8
|
+
readAfter;
|
|
8
9
|
inboxApplying;
|
|
9
10
|
inboxDirty = false;
|
|
10
11
|
constructor(options) {
|
|
@@ -161,10 +162,13 @@ export class ReadSyncClient {
|
|
|
161
162
|
return this.run(true);
|
|
162
163
|
throw error;
|
|
163
164
|
}
|
|
164
|
-
if (receipt.operationId !== operation.operationId || receipt.accepted !== true
|
|
165
|
-
|| !Number.isSafeInteger(receipt.acceptedAt) || receipt.acceptedAt <= 0)
|
|
165
|
+
if (receipt.operationId !== operation.operationId || receipt.accepted !== true)
|
|
166
166
|
throw new Error("INVALID_READING_RECEIPT");
|
|
167
|
-
|
|
167
|
+
const cursor = sourceCursor(receipt);
|
|
168
|
+
if (cursor.seq === "0" || this.readAfter && this.readAfter.streamId !== cursor.streamId)
|
|
169
|
+
throw new Error("INVALID_READING_RECEIPT");
|
|
170
|
+
if (!this.readAfter || BigInt(cursor.seq) > BigInt(this.readAfter.seq))
|
|
171
|
+
this.readAfter = cursor;
|
|
168
172
|
}
|
|
169
173
|
if (this.options.inbox) {
|
|
170
174
|
await this.options.inbox.synchronize();
|
|
@@ -200,7 +204,7 @@ export class ReadSyncClient {
|
|
|
200
204
|
}
|
|
201
205
|
}
|
|
202
206
|
const snapshot = await this.request(`/api/social/reading/snapshot?${query}`, {
|
|
203
|
-
headers: this.readAfter ? { "x-pmate-read-after-
|
|
207
|
+
headers: this.readAfter ? { "x-pmate-read-after-seq": this.readAfter.seq, "x-pmate-stream-id": this.readAfter.streamId } : {},
|
|
204
208
|
});
|
|
205
209
|
if (this.controller.signal.aborted)
|
|
206
210
|
throw new Error("READ_SYNC_STOPPED");
|
|
@@ -210,7 +214,7 @@ export class ReadSyncClient {
|
|
|
210
214
|
return installReadSnapshot(state, this.options.metadataOnly ? readingMetadata(snapshot) : snapshot);
|
|
211
215
|
});
|
|
212
216
|
if (!next.pending.length)
|
|
213
|
-
this.readAfter =
|
|
217
|
+
this.readAfter = undefined;
|
|
214
218
|
if (!this.controller.signal.aborted)
|
|
215
219
|
this.options.onChange?.(next);
|
|
216
220
|
return next;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export function sourceCursor(value) {
|
|
2
|
+
const cursor = value;
|
|
3
|
+
if (!cursor || typeof cursor.streamId !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(cursor.streamId)
|
|
4
|
+
|| typeof cursor.seq !== "string" || cursor.seq.length > 20 || !/^(0|[1-9][0-9]*)$/.test(cursor.seq) || BigInt(cursor.seq) > 18446744073709551615n)
|
|
5
|
+
throw Error("INVALID_SOURCE_CURSOR");
|
|
6
|
+
return { streamId: cursor.streamId, seq: cursor.seq };
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pmate/chat-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0-seq.0",
|
|
4
4
|
"description": "Local-first chat SDK, message subscriptions, reading state, and shared chat UI primitives",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
"scripts": {
|
|
99
99
|
"clean": "rimraf lib",
|
|
100
100
|
"build": "pnpm run clean && tsc -p tsconfig.build.json && cp src/voice/recorder-processor.js lib/voice/recorder-processor.js",
|
|
101
|
-
"npm:publish": "pnpm
|
|
101
|
+
"npm:publish": "pnpm publish --access public --registry https://registry.npmjs.org/",
|
|
102
102
|
"test": "vitest run",
|
|
103
103
|
"storybook": "storybook dev -p 6009",
|
|
104
104
|
"build-storybook": "storybook build --output-dir dist"
|