@msssystems/mss-link-sdk 0.1.0 → 0.1.2
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/api-base-url.d.ts +1 -0
- package/dist/client/api-base-url.js +10 -0
- package/dist/client/client-options.d.ts +11 -0
- package/dist/client/client-options.js +1 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.js +5 -0
- package/dist/client/message.contracts.d.ts +20 -0
- package/dist/client/message.contracts.js +1 -0
- package/dist/client/message.mapper.d.ts +2 -0
- package/dist/client/message.mapper.js +14 -0
- package/dist/client/mss-link-browser-client.d.ts +31 -0
- package/dist/client/mss-link-browser-client.js +84 -0
- package/dist/client/wasm-call-queue.d.ts +4 -0
- package/dist/client/wasm-call-queue.js +8 -0
- package/dist/client/wasm-client.types.d.ts +6 -0
- package/dist/client/wasm-client.types.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +10 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function normalizeProductApiBaseUrl(apiBaseUrl: string): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function normalizeProductApiBaseUrl(apiBaseUrl) {
|
|
2
|
+
const normalizedApiUrl = apiBaseUrl.replace(/\/+$/, '');
|
|
3
|
+
if (!normalizedApiUrl) {
|
|
4
|
+
return '/link/v1';
|
|
5
|
+
}
|
|
6
|
+
if (normalizedApiUrl.endsWith('/link/v1')) {
|
|
7
|
+
return normalizedApiUrl;
|
|
8
|
+
}
|
|
9
|
+
return `${normalizedApiUrl}/link/v1`;
|
|
10
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface MssLinkBrowserClientOptions {
|
|
2
|
+
userId: string;
|
|
3
|
+
apiBaseUrl: string;
|
|
4
|
+
accessToken?: string | null;
|
|
5
|
+
deviceId?: number;
|
|
6
|
+
registrationId?: number;
|
|
7
|
+
signedPrekeyId?: number;
|
|
8
|
+
initialOneTimePrekeyCount?: number;
|
|
9
|
+
minOneTimePrekeys?: number;
|
|
10
|
+
replenishOneTimePrekeys?: number;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface LocalSdkMessage {
|
|
2
|
+
messageId: string;
|
|
3
|
+
chatId: string;
|
|
4
|
+
senderId: string;
|
|
5
|
+
plaintext: string;
|
|
6
|
+
kind: string;
|
|
7
|
+
deliveryState: string;
|
|
8
|
+
createdAt?: string;
|
|
9
|
+
updatedAt?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface DecryptedMessage {
|
|
12
|
+
id: string;
|
|
13
|
+
chatId: string;
|
|
14
|
+
senderId: string;
|
|
15
|
+
text: string;
|
|
16
|
+
kind: string;
|
|
17
|
+
status: string;
|
|
18
|
+
createdAt: string;
|
|
19
|
+
updatedAt: string;
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function mapLocalSdkMessage(message, now = () => new Date().toISOString()) {
|
|
2
|
+
const createdAt = message.createdAt ?? now();
|
|
3
|
+
const updatedAt = message.updatedAt ?? createdAt;
|
|
4
|
+
return {
|
|
5
|
+
id: message.messageId,
|
|
6
|
+
chatId: message.chatId,
|
|
7
|
+
senderId: message.senderId,
|
|
8
|
+
text: message.plaintext,
|
|
9
|
+
kind: message.kind,
|
|
10
|
+
status: message.deliveryState,
|
|
11
|
+
createdAt,
|
|
12
|
+
updatedAt,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { MssLinkBrowserClientOptions } from './client-options';
|
|
2
|
+
import type { DecryptedMessage } from './message.contracts';
|
|
3
|
+
export declare class MssLinkBrowserClient {
|
|
4
|
+
private readonly userId;
|
|
5
|
+
private readonly apiBaseUrl;
|
|
6
|
+
private readonly deviceId;
|
|
7
|
+
private readonly registrationId;
|
|
8
|
+
private readonly signedPrekeyId;
|
|
9
|
+
private readonly initialOneTimePrekeyCount;
|
|
10
|
+
private readonly minOneTimePrekeys;
|
|
11
|
+
private readonly replenishOneTimePrekeys;
|
|
12
|
+
private readonly queue;
|
|
13
|
+
private wasmClient;
|
|
14
|
+
private wasmModulePromise;
|
|
15
|
+
private accessToken;
|
|
16
|
+
constructor(options: MssLinkBrowserClientOptions);
|
|
17
|
+
setAccessToken(accessToken: string | null): void;
|
|
18
|
+
ensureReady(): Promise<void>;
|
|
19
|
+
sendMessage(params: {
|
|
20
|
+
chatId: string;
|
|
21
|
+
targetUserId: string;
|
|
22
|
+
text: string;
|
|
23
|
+
kind?: string;
|
|
24
|
+
}): Promise<string>;
|
|
25
|
+
syncMessages(): Promise<void>;
|
|
26
|
+
getLocalMessages(chatId: string): Promise<DecryptedMessage[]>;
|
|
27
|
+
reset(): void;
|
|
28
|
+
private runWithClient;
|
|
29
|
+
private getClient;
|
|
30
|
+
private loadWasmModule;
|
|
31
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { normalizeProductApiBaseUrl } from './api-base-url';
|
|
2
|
+
import { mapLocalSdkMessage } from './message.mapper';
|
|
3
|
+
import { WasmCallQueue } from './wasm-call-queue';
|
|
4
|
+
const DEFAULT_DEVICE_ID = 1;
|
|
5
|
+
const DEFAULT_REGISTRATION_ID = 1;
|
|
6
|
+
const DEFAULT_SIGNED_PREKEY_ID = 1;
|
|
7
|
+
const DEFAULT_ONE_TIME_PREKEY_COUNT = 10;
|
|
8
|
+
const DEFAULT_MIN_ONE_TIME_PREKEYS = 5;
|
|
9
|
+
const DEFAULT_REPLENISH_ONE_TIME_PREKEYS = 10;
|
|
10
|
+
export class MssLinkBrowserClient {
|
|
11
|
+
userId;
|
|
12
|
+
apiBaseUrl;
|
|
13
|
+
deviceId;
|
|
14
|
+
registrationId;
|
|
15
|
+
signedPrekeyId;
|
|
16
|
+
initialOneTimePrekeyCount;
|
|
17
|
+
minOneTimePrekeys;
|
|
18
|
+
replenishOneTimePrekeys;
|
|
19
|
+
queue = new WasmCallQueue();
|
|
20
|
+
wasmClient = null;
|
|
21
|
+
wasmModulePromise = null;
|
|
22
|
+
accessToken;
|
|
23
|
+
constructor(options) {
|
|
24
|
+
this.userId = options.userId;
|
|
25
|
+
this.apiBaseUrl = normalizeProductApiBaseUrl(options.apiBaseUrl);
|
|
26
|
+
this.accessToken = options.accessToken ?? null;
|
|
27
|
+
this.deviceId = options.deviceId ?? DEFAULT_DEVICE_ID;
|
|
28
|
+
this.registrationId =
|
|
29
|
+
options.registrationId ?? DEFAULT_REGISTRATION_ID;
|
|
30
|
+
this.signedPrekeyId =
|
|
31
|
+
options.signedPrekeyId ?? DEFAULT_SIGNED_PREKEY_ID;
|
|
32
|
+
this.initialOneTimePrekeyCount =
|
|
33
|
+
options.initialOneTimePrekeyCount ??
|
|
34
|
+
DEFAULT_ONE_TIME_PREKEY_COUNT;
|
|
35
|
+
this.minOneTimePrekeys =
|
|
36
|
+
options.minOneTimePrekeys ?? DEFAULT_MIN_ONE_TIME_PREKEYS;
|
|
37
|
+
this.replenishOneTimePrekeys =
|
|
38
|
+
options.replenishOneTimePrekeys ??
|
|
39
|
+
DEFAULT_REPLENISH_ONE_TIME_PREKEYS;
|
|
40
|
+
}
|
|
41
|
+
setAccessToken(accessToken) {
|
|
42
|
+
this.accessToken = accessToken;
|
|
43
|
+
this.wasmClient?.setAccessToken(accessToken);
|
|
44
|
+
}
|
|
45
|
+
async ensureReady() {
|
|
46
|
+
await this.runWithClient(async (client) => {
|
|
47
|
+
await client.ensureDeviceRegistered(this.registrationId, this.signedPrekeyId, this.initialOneTimePrekeyCount);
|
|
48
|
+
await client.replenishOneTimePreKeys(this.minOneTimePrekeys, this.replenishOneTimePrekeys);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async sendMessage(params) {
|
|
52
|
+
return this.runWithClient((client) => client.sendMessage(params.chatId, params.targetUserId, params.text, params.kind ?? 'TEXT'));
|
|
53
|
+
}
|
|
54
|
+
async syncMessages() {
|
|
55
|
+
await this.runWithClient((client) => client.syncMessages());
|
|
56
|
+
}
|
|
57
|
+
async getLocalMessages(chatId) {
|
|
58
|
+
const rawMessages = await this.runWithClient((client) => client.getLocalMessages(chatId));
|
|
59
|
+
return rawMessages.map((message) => mapLocalSdkMessage(message));
|
|
60
|
+
}
|
|
61
|
+
reset() {
|
|
62
|
+
this.wasmClient?.free();
|
|
63
|
+
this.wasmClient = null;
|
|
64
|
+
this.wasmModulePromise = null;
|
|
65
|
+
}
|
|
66
|
+
async runWithClient(callback) {
|
|
67
|
+
return this.queue.run(async () => callback(await this.getClient()));
|
|
68
|
+
}
|
|
69
|
+
async getClient() {
|
|
70
|
+
if (typeof window === 'undefined') {
|
|
71
|
+
throw new Error('MSS Link browser client is available only in browser context.');
|
|
72
|
+
}
|
|
73
|
+
if (!this.wasmClient) {
|
|
74
|
+
const sdkModule = await this.loadWasmModule();
|
|
75
|
+
this.wasmClient = new sdkModule.WasmMssClient(this.userId, this.deviceId, this.apiBaseUrl);
|
|
76
|
+
this.wasmClient.setAccessToken(this.accessToken);
|
|
77
|
+
}
|
|
78
|
+
return this.wasmClient;
|
|
79
|
+
}
|
|
80
|
+
loadWasmModule() {
|
|
81
|
+
this.wasmModulePromise ??= import('@msssystems/mss-crypto-wasm');
|
|
82
|
+
return this.wasmModulePromise;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { WasmMssClient } from '@msssystems/mss-crypto-wasm';
|
|
2
|
+
export type WasmMssClientConstructor = new (userId: string, deviceId: number, apiBaseUrl: string) => WasmMssClient;
|
|
3
|
+
export interface MssCryptoWasmModule {
|
|
4
|
+
WasmMssClient: WasmMssClientConstructor;
|
|
5
|
+
}
|
|
6
|
+
export type WasmMssClientInstance = WasmMssClient;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@msssystems/mss-link-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Official managed application SDK for MSS ecosystem",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
12
|
},
|
|
13
|
+
"./client": {
|
|
14
|
+
"types": "./dist/client/index.d.ts",
|
|
15
|
+
"import": "./dist/client/index.js"
|
|
16
|
+
},
|
|
13
17
|
"./control": {
|
|
14
18
|
"types": "./dist/control/index.d.ts",
|
|
15
19
|
"import": "./dist/control/index.js"
|
|
@@ -49,12 +53,16 @@
|
|
|
49
53
|
"registry": "https://registry.npmjs.org/"
|
|
50
54
|
},
|
|
51
55
|
"peerDependencies": {
|
|
56
|
+
"@msssystems/mss-crypto-wasm": "^0.1.16",
|
|
52
57
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
53
58
|
"@nestjs/core": "^10.0.0 || ^11.0.0",
|
|
54
59
|
"express": "^4.0.0 || ^5.0.0",
|
|
55
60
|
"rxjs": "^7.0.0"
|
|
56
61
|
},
|
|
57
62
|
"peerDependenciesMeta": {
|
|
63
|
+
"@msssystems/mss-crypto-wasm": {
|
|
64
|
+
"optional": true
|
|
65
|
+
},
|
|
58
66
|
"@nestjs/common": {
|
|
59
67
|
"optional": true
|
|
60
68
|
},
|
|
@@ -69,6 +77,7 @@
|
|
|
69
77
|
}
|
|
70
78
|
},
|
|
71
79
|
"devDependencies": {
|
|
80
|
+
"@msssystems/mss-crypto-wasm": "^0.1.16",
|
|
72
81
|
"@nestjs/common": "^11.0.1",
|
|
73
82
|
"@nestjs/core": "^11.0.1",
|
|
74
83
|
"@types/express": "^5.0.0",
|