@msssystems/mss-link-sdk 0.2.3 → 0.2.5
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/dist/client/wasm-client.types.d.ts +1 -0
- package/dist/core/http-client.d.ts +7 -1
- package/dist/core/http-client.js +9 -2
- package/dist/modules/messages/messages-crypto.client.d.ts +1 -0
- package/dist/modules/messages/messages-crypto.client.js +24 -2
- package/dist/modules/messages/messages.client.js +2 -2
- package/package.json +1 -1
|
@@ -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 {
|
|
@@ -2,7 +2,7 @@ export interface HttpClientOptions {
|
|
|
2
2
|
baseUrl: string;
|
|
3
3
|
token?: string | (() => string | Promise<string>) | undefined;
|
|
4
4
|
fetch?: typeof fetch;
|
|
5
|
-
onUnauthorized?: (() => Promise<void>) | undefined;
|
|
5
|
+
onUnauthorized?: (() => Promise<string | null | void>) | undefined;
|
|
6
6
|
}
|
|
7
7
|
export declare class HttpClient {
|
|
8
8
|
private baseUrl;
|
|
@@ -12,6 +12,12 @@ export declare class HttpClient {
|
|
|
12
12
|
constructor(options: HttpClientOptions);
|
|
13
13
|
setToken(token: string): Promise<void>;
|
|
14
14
|
private getHeaders;
|
|
15
|
+
requestRaw(method: string, path: string, options?: {
|
|
16
|
+
body?: any;
|
|
17
|
+
params?: Record<string, any>;
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
responseType?: 'json' | 'blob';
|
|
20
|
+
}): Promise<Response>;
|
|
15
21
|
request<T>(method: string, path: string, options?: {
|
|
16
22
|
body?: any;
|
|
17
23
|
params?: Record<string, any>;
|
package/dist/core/http-client.js
CHANGED
|
@@ -27,7 +27,7 @@ export class HttpClient {
|
|
|
27
27
|
}
|
|
28
28
|
return headers;
|
|
29
29
|
}
|
|
30
|
-
async
|
|
30
|
+
async requestRaw(method, path, options = {}) {
|
|
31
31
|
const url = new URL(`${this.baseUrl}${path}`);
|
|
32
32
|
if (options.params) {
|
|
33
33
|
for (const [key, value] of Object.entries(options.params)) {
|
|
@@ -47,7 +47,10 @@ export class HttpClient {
|
|
|
47
47
|
let response = await this.customFetch(url.toString(), fetchOptions);
|
|
48
48
|
if (response.status === 401 && this.onUnauthorized) {
|
|
49
49
|
try {
|
|
50
|
-
await this.onUnauthorized();
|
|
50
|
+
const newToken = await this.onUnauthorized();
|
|
51
|
+
if (typeof newToken === 'string') {
|
|
52
|
+
this.setToken(newToken);
|
|
53
|
+
}
|
|
51
54
|
// Update headers with new token
|
|
52
55
|
fetchOptions.headers = await this.getHeaders(options.headers);
|
|
53
56
|
response = await this.customFetch(url.toString(), fetchOptions);
|
|
@@ -56,6 +59,10 @@ export class HttpClient {
|
|
|
56
59
|
// If refresh fails, let the original 401 error throw below
|
|
57
60
|
}
|
|
58
61
|
}
|
|
62
|
+
return response;
|
|
63
|
+
}
|
|
64
|
+
async request(method, path, options = {}) {
|
|
65
|
+
const response = await this.requestRaw(method, path, options);
|
|
59
66
|
if (!response.ok) {
|
|
60
67
|
let errorPayload;
|
|
61
68
|
try {
|
|
@@ -16,16 +16,38 @@ export class MessagesCryptoClient {
|
|
|
16
16
|
}
|
|
17
17
|
async syncMessages() {
|
|
18
18
|
try {
|
|
19
|
-
await this.engine.runWithClient((client) =>
|
|
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
|
|
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);
|
|
@@ -31,8 +31,8 @@ export class MessagesClient {
|
|
|
31
31
|
return this.http.get(`/link/v1/media/assets/${assetId}/url`);
|
|
32
32
|
}
|
|
33
33
|
async downloadMediaAsset(assetId) {
|
|
34
|
-
const res = await
|
|
35
|
-
|
|
34
|
+
const res = await this.http.requestRaw('GET', `/link/v1/media/assets/${assetId}/download`, {
|
|
35
|
+
responseType: 'blob'
|
|
36
36
|
});
|
|
37
37
|
if (!res.ok)
|
|
38
38
|
throw new Error('Download failed');
|