@pmate/chat-sdk 0.13.0-profile.8 → 0.13.0-profile.9
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 +16 -0
- package/lib/profile.d.ts +1 -1
- package/lib/profile.js +1 -1
- package/lib/runtime/profileCommitQueue.js +3 -3
- package/lib/runtime/profileCoordinator.d.ts +19 -20
- package/lib/runtime/profileCoordinator.js +123 -128
- package/lib/runtime/profileDatabase.d.ts +6 -9
- package/lib/runtime/profileDatabase.js +8 -38
- package/lib/runtime/profileGenerationBuilder.d.ts +2 -3
- package/lib/runtime/profileGenerationBuilder.js +6 -10
- package/lib/runtime/profileMemoryStore.d.ts +1 -1
- package/lib/runtime/profileMemoryStore.js +1 -1
- package/lib/runtime/profileOwner.d.ts +0 -7
- package/lib/runtime/profileOwner.js +9 -41
- package/lib/runtime/profileStoreTypes.d.ts +1 -10
- package/lib/runtime/{profileReplica.d.ts → profileWorkerClient.d.ts} +9 -26
- package/lib/runtime/{profileReplica.js → profileWorkerClient.js} +45 -97
- package/lib/runtime/profileWorkerHost.d.ts +3 -6
- package/lib/runtime/profileWorkerHost.js +23 -30
- 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, 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pmate/chat-sdk",
|
|
3
|
-
"version": "0.13.0-profile.
|
|
3
|
+
"version": "0.13.0-profile.9",
|
|
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"
|