@pmate/chat-sdk 0.12.0-next.67 → 0.12.0-next.68

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.
@@ -4,7 +4,7 @@ function media(value) {
4
4
  if (typeof value !== "string" || !value)
5
5
  return undefined;
6
6
  try {
7
- const url = new URL(value);
7
+ const url = new URL(value, "https://chat.invalid");
8
8
  return ["https:", "http:"].includes(url.protocol) && !url.username && !url.password ? value : undefined;
9
9
  }
10
10
  catch {
@@ -415,7 +415,19 @@ export class ChatSession {
415
415
  return (delivered ? this.sender.send(threadId, wire) : this.sender.enqueue(threadId, wire)).then(next => { if (this.withdrawn)
416
416
  throw new Error("CHAT_ACCESS_REVOKED"); return next; });
417
417
  }
418
- getConversation(target) { this.assertOpen(); return this.conversations.create(target); }
418
+ async getConversation(target) {
419
+ this.assertOpen();
420
+ try {
421
+ return await this.conversations.create(target);
422
+ }
423
+ catch (error) {
424
+ if (error?.message !== "CHAT_ACCESS_REVOKED")
425
+ throw error;
426
+ await this.messages.authorizeScope();
427
+ this.assertOpen();
428
+ return this.conversations.create(target);
429
+ }
430
+ }
419
431
  async sendContent(target, input, delivered) {
420
432
  let thread = typeof target === "string" ? await this.resolveConversation(target) : target;
421
433
  validateConversation(this.options.messages.scope, thread);
@@ -34,7 +34,7 @@ export function createChat(options) {
34
34
  },
35
35
  loadGroup: (groupId, signal) => reader.group({ groupId, teamId }, signal),
36
36
  activeThreadId: options.threadId,
37
- messages: { scope: { appId, accountId, profileId, teamId }, transport: { load: (thread, signal) => page(thread, signal), changes: (thread, after, signal) => page(thread, signal, { after }), older: (thread, before, signal) => page(thread, signal, { before }) }, onError },
37
+ messages: { scope: { appId, accountId, profileId, teamId }, transport: { authorizeScope: async (signal) => { await reader.contacts(teamId, signal); }, load: (thread, signal) => page(thread, signal), changes: (thread, after, signal) => page(thread, signal, { after }), older: (thread, before, signal) => page(thread, signal, { before }) }, onError },
38
38
  reading: { appId, profileId, sessionToken: token, hubHttpUrl: http, fetch: options.fetch, onError },
39
39
  connection: { appId, profileId, teamId, hubUrl: ws, clientInstanceId: options.clientInstanceId, fetchImpl: options.fetch,
40
40
  session: { token, identity: { accountId, app: appId, profileId } }, onNotification: options.onNotification, onReach: options.onReach },
@@ -7,6 +7,8 @@ export type MessagePage<T> = {
7
7
  resetRequired?: boolean;
8
8
  };
9
9
  export type MessageTransport<T> = {
10
+ /** Optional team-scoped authorization for fresh instances recovering a revoked cache. */
11
+ authorizeScope?: (signal: AbortSignal) => Promise<void>;
10
12
  /** Authoritative history. Must include control state needed to merge old rows. */
11
13
  load: (threadId: string, signal: AbortSignal) => Promise<MessagePage<T>>;
12
14
  changes?: (threadId: string, after: string, signal: AbortSignal) => Promise<MessagePage<T>>;
@@ -72,6 +74,9 @@ export declare class MessageClient<T extends StoredMessage> {
72
74
  version: number;
73
75
  allowed: boolean;
74
76
  }>;
77
+ private authorizingScope?;
78
+ /** Recovery belongs to the SDK; withdrawn instances never regain authority. */
79
+ authorizeScope(): Promise<void>;
75
80
  getSnapshot(thread: string): MessageSnapshot<T> | undefined;
76
81
  /** Exact authoritative row only; migration-only rows never prove delivery. */
77
82
  cachedMessage(thread: string, id: string): Promise<T>;
@@ -100,6 +100,38 @@ export class MessageClient {
100
100
  const current = await this.storage.scopeAccess();
101
101
  return { version: current.version, allowed: !this.scopeDenied && !current.revoked && current.version === this.scopeVersion };
102
102
  }
103
+ authorizingScope;
104
+ /** Recovery belongs to the SDK; withdrawn instances never regain authority. */
105
+ authorizeScope() {
106
+ return this.authorizingScope ??= (async () => {
107
+ await this.ensureScope();
108
+ if (this.closed || this.closing || this.scopeDenied)
109
+ throw accessRevoked();
110
+ const access = await this.scopeAccess();
111
+ if (access.allowed)
112
+ return;
113
+ if (access.version !== this.scopeVersion) {
114
+ this.denyScope();
115
+ throw accessRevoked();
116
+ }
117
+ if (!this.options.transport.authorizeScope)
118
+ throw accessRevoked();
119
+ try {
120
+ await this.options.transport.authorizeScope(this.abort.signal);
121
+ if (this.closed || this.closing || this.scopeDenied)
122
+ throw accessRevoked();
123
+ await this.storage.authorizeScope(this.scopeVersion);
124
+ if (this.closed || this.closing || this.scopeDenied)
125
+ throw accessRevoked();
126
+ this.scopeAllowed = true;
127
+ }
128
+ catch (error) {
129
+ if ([401, 403].includes(error?.status ?? 0) || String(error).includes("MESSAGE_SCOPE_CHANGED"))
130
+ this.denyScope();
131
+ throw error;
132
+ }
133
+ })().finally(() => { this.authorizingScope = undefined; });
134
+ }
103
135
  getSnapshot(thread) { return this.watches.get(thread)?.snapshot; }
104
136
  /** Exact authoritative row only; migration-only rows never prove delivery. */
105
137
  async cachedMessage(thread, id) {
@@ -82,6 +82,8 @@ export declare class IndexedDbMessageStore<T extends StoredMessage> {
82
82
  private finishPage;
83
83
  windows(): Promise<CoverageRow[]>;
84
84
  scopeAccess(): Promise<MessageScopeAccess>;
85
+ /** Only fresh, server-confirmed authorization may lift this durable tombstone. */
86
+ authorizeScope(expectedVersion: number): Promise<void>;
85
87
  private scopedCoverage;
86
88
  private clearScopeRows;
87
89
  /** Persist the denial first; failed cleanup must never make cached rows readable. */
@@ -230,6 +230,18 @@ export class IndexedDbMessageStore {
230
230
  throw new Error("INVALID_MESSAGE_SCOPE_ACCESS");
231
231
  return access;
232
232
  }
233
+ /** Only fresh, server-confirmed authorization may lift this durable tombstone. */
234
+ async authorizeScope(expectedVersion) {
235
+ await this.db.transaction("rw", [this.rows, this.legacy, this.coverage, this.accessRows, this.controls], async () => {
236
+ const access = await this.scopeAccess();
237
+ if (access.version !== expectedVersion)
238
+ throw new Error("MESSAGE_SCOPE_CHANGED");
239
+ if (!access.revoked)
240
+ return;
241
+ await this.clearScopeRows();
242
+ await this.accessRows.put({ ...access, revoked: false });
243
+ });
244
+ }
233
245
  scopedCoverage(coverage, access) {
234
246
  return access.version ? { ...coverage, complete: coverage?.complete ?? false, scopeVersion: access.version } : coverage;
235
247
  }
@@ -46,7 +46,7 @@ export declare class SocialReadClient {
46
46
  private readonly options;
47
47
  constructor(options: SocialReadClientOptions);
48
48
  /** Without teamId includes current Teams and unassigned contact metadata for migration. */
49
- contacts(teamId?: string): Promise<ChatReaderContact[]>;
49
+ contacts(teamId?: string, signal?: AbortSignal): Promise<ChatReaderContact[]>;
50
50
  unread(teamId?: string): Promise<ChatReaderUnread[]>;
51
51
  messages(input: {
52
52
  threadId: string;
@@ -14,8 +14,8 @@ export class SocialReadClient {
14
14
  options;
15
15
  constructor(options) { this.options = { ...options }; }
16
16
  /** Without teamId includes current Teams and unassigned contact metadata for migration. */
17
- contacts(teamId) {
18
- return this.list("contacts", this.teamQuery(teamId));
17
+ contacts(teamId, signal) {
18
+ return this.list("contacts", this.teamQuery(teamId), signal);
19
19
  }
20
20
  unread(teamId) {
21
21
  return this.list("unread", this.teamQuery(teamId));
@@ -58,8 +58,8 @@ export class SocialReadClient {
58
58
  if (this.options.appId === "pmate:merry" && !teamId)
59
59
  throw new SocialReadError(400, "TEAM_ID_REQUIRED");
60
60
  }
61
- async list(action, query) {
62
- const value = await this.request(action, query);
61
+ async list(action, query, signal) {
62
+ const value = await this.request(action, query, signal);
63
63
  if (!Array.isArray(value))
64
64
  throw new SocialReadError(502, "INVALID_SOCIAL_READ_RESPONSE");
65
65
  return value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.12.0-next.67",
3
+ "version": "0.12.0-next.68",
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",