@msssystems/mss-link-sdk 0.2.4 → 0.2.6

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.
@@ -13,7 +13,7 @@ export class BrowserMssLinkClient extends MssLinkClient {
13
13
  this.cryptoEngine = new WasmCryptoEngine({
14
14
  userId: options.userId,
15
15
  apiBaseUrl: options.baseUrl,
16
- accessToken: typeof options.token === 'string' ? options.token : undefined,
16
+ accessToken: options.token,
17
17
  deviceId: options.deviceId,
18
18
  registrationId: options.registrationId,
19
19
  signedPrekeyId: options.signedPrekeyId,
@@ -2,6 +2,7 @@ import type { WasmMssClient } from '@msssystems/mss-crypto-wasm';
2
2
  import type { WasmEncryptedMediaBytes } from '../modules/media/media-crypto.contracts';
3
3
  export interface MssLinkWasmClient extends WasmMssClient {
4
4
  sendMediaMessage(chatId: string, targetUserId: string, metadataJson: string, mediaAssetIds: string[]): Promise<string>;
5
+ healSession(chatId: string, targetUserId: string): Promise<string>;
5
6
  }
6
7
  export type WasmMssClientConstructor = new (userId: string, deviceId: number, apiBaseUrl: string) => MssLinkWasmClient;
7
8
  export interface MssCryptoWasmModule {
@@ -3,7 +3,7 @@ import type { MssCryptoWasmModule, WasmMssClientInstance } from '../client/wasm-
3
3
  export interface WasmCryptoEngineOptions {
4
4
  userId: string;
5
5
  apiBaseUrl: string;
6
- accessToken?: string | undefined;
6
+ accessToken?: string | (() => string | Promise<string>) | undefined;
7
7
  deviceId?: number | undefined;
8
8
  registrationId?: number | undefined;
9
9
  signedPrekeyId?: number | undefined;
@@ -25,7 +25,7 @@ export declare class WasmCryptoEngine {
25
25
  private wasmModulePromise;
26
26
  private accessToken;
27
27
  constructor(options: WasmCryptoEngineOptions);
28
- setAccessToken(accessToken: string | null): void;
28
+ setAccessToken(accessToken: string | (() => string | Promise<string>) | null): void;
29
29
  getLocalCryptoHealth(): Promise<import("./local-crypto-health.contracts").LocalCryptoHealth>;
30
30
  ensureReady(): Promise<void>;
31
31
  reset(): void;
@@ -36,7 +36,14 @@ export class WasmCryptoEngine {
36
36
  }
37
37
  setAccessToken(accessToken) {
38
38
  this.accessToken = accessToken;
39
- this.wasmClient?.setAccessToken(accessToken);
39
+ if (typeof accessToken === 'string' || accessToken === null) {
40
+ this.wasmClient?.setAccessToken(accessToken);
41
+ }
42
+ else if (this.wasmClient) {
43
+ Promise.resolve(accessToken()).then((tokenStr) => {
44
+ this.wasmClient?.setAccessToken(tokenStr);
45
+ }).catch(() => { });
46
+ }
40
47
  }
41
48
  async getLocalCryptoHealth() {
42
49
  const { checkLocalCryptoHealth } = await import('./local-crypto-health');
@@ -68,8 +75,9 @@ export class WasmCryptoEngine {
68
75
  if (!this.wasmClient) {
69
76
  const sdkModule = await this.loadWasmModule();
70
77
  this.wasmClient = new sdkModule.WasmMssClient(this.userId, this.deviceId, this.apiBaseUrl);
71
- this.wasmClient.setAccessToken(this.accessToken);
72
78
  }
79
+ const tokenStr = typeof this.accessToken === 'function' ? await this.accessToken() : this.accessToken;
80
+ this.wasmClient.setAccessToken(tokenStr);
73
81
  return this.wasmClient;
74
82
  }
75
83
  loadWasmModule() {
@@ -10,5 +10,6 @@ export declare class MessagesCryptoClient {
10
10
  kind?: string;
11
11
  }): Promise<string>;
12
12
  syncMessages(): Promise<void>;
13
+ private handleSessionHealing;
13
14
  getLocalMessages(chatId: string): Promise<DecryptedMessage[]>;
14
15
  }
@@ -16,16 +16,38 @@ export class MessagesCryptoClient {
16
16
  }
17
17
  async syncMessages() {
18
18
  try {
19
- await this.engine.runWithClient((client) => client.syncMessages());
19
+ await this.engine.runWithClient(async (client) => {
20
+ const rawMessages = await client.syncMessages();
21
+ await this.handleSessionHealing(client, rawMessages);
22
+ });
20
23
  }
21
24
  catch (error) {
22
25
  throw normalizeLocalCryptoError(error);
23
26
  }
24
27
  }
28
+ async handleSessionHealing(client, messages) {
29
+ for (const message of messages) {
30
+ if (message.deliveryState === 'FAILED' &&
31
+ (message.plaintext === '[Ошибка дешифровки]' ||
32
+ message.plaintext === '[Ошибка дешифровки]')) {
33
+ if (message.kind === 'SYSTEM')
34
+ continue;
35
+ console.warn(`[E2EE] Decryption failed for message ${message.messageId} from ${message.senderId}. Triggering auto-heal...`);
36
+ try {
37
+ await client.healSession(message.chatId, message.senderId);
38
+ }
39
+ catch (e) {
40
+ console.error('[E2EE] Failed to heal session:', e);
41
+ }
42
+ }
43
+ }
44
+ }
25
45
  async getLocalMessages(chatId) {
26
46
  try {
27
47
  const rawMessages = await this.engine.runWithClient((client) => client.getLocalMessages(chatId));
28
- return rawMessages.map((message) => mapLocalSdkMessage(message));
48
+ return rawMessages
49
+ .filter((message) => message.kind !== 'SYSTEM')
50
+ .map((message) => mapLocalSdkMessage(message));
29
51
  }
30
52
  catch (error) {
31
53
  throw normalizeLocalCryptoError(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@msssystems/mss-link-sdk",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Official managed application SDK for MSS ecosystem",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",