@droppii-org/wasm-client-sdk 0.8.0 → 0.8.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/README.md +2 -1
- package/lib/constant/index.d.ts +1 -0
- package/lib/index.es.js +69 -16
- package/lib/index.js +69 -16
- package/lib/index.umd.js +69 -16
- package/lib/sdk/index.d.ts +4 -1
- package/lib/sdk/initialize.d.ts +2 -0
- package/lib/types/entity.d.ts +3 -0
- package/lib/types/eventData.d.ts +1 -0
- package/lib/types/params.d.ts +3 -0
- package/lib/worker-legacy.js +1 -1
- package/lib/worker.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,10 +26,11 @@ Follow these steps to obtain the static resources required for WebAssembly (WASM
|
|
|
26
26
|
|
|
27
27
|
The files to be copied are:
|
|
28
28
|
|
|
29
|
-
- `openIM.wasm`
|
|
30
29
|
- `sql-wasm.wasm`
|
|
31
30
|
- `wasm_exec.js`
|
|
32
31
|
|
|
32
|
+
`openIM.wasm` is not shipped in this package. It is built from the Go core (`droppii/openimsdk-core`); serve it yourself and pass its URL as `coreWasmPath` to `getSDK` (default `/openIM.wasm`).
|
|
33
|
+
|
|
33
34
|
2. After copying the files, import the `wasm_exec.js` file in your `index.html` file using a `<script>` tag.
|
|
34
35
|
|
|
35
36
|
### Possible Issues ❗
|
package/lib/constant/index.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export declare enum CbEvents {
|
|
|
22
22
|
OnRecvMessageRevoked = "OnRecvMessageRevoked",
|
|
23
23
|
OnNewRecvMessageRevoked = "OnNewRecvMessageRevoked",
|
|
24
24
|
OnRecvMessagePinned = "OnRecvMessagePinned",
|
|
25
|
+
OnRecvMessageEdited = "OnRecvMessageEdited",
|
|
25
26
|
OnRecvC2CReadReceipt = "OnRecvC2CReadReceipt",
|
|
26
27
|
OnRecvGroupReadReceipt = "OnRecvGroupReadReceipt",
|
|
27
28
|
OnConversationChanged = "OnConversationChanged",
|
package/lib/index.es.js
CHANGED
|
@@ -405,6 +405,7 @@ var CbEvents;
|
|
|
405
405
|
CbEvents["OnRecvMessageRevoked"] = "OnRecvMessageRevoked";
|
|
406
406
|
CbEvents["OnNewRecvMessageRevoked"] = "OnNewRecvMessageRevoked";
|
|
407
407
|
CbEvents["OnRecvMessagePinned"] = "OnRecvMessagePinned";
|
|
408
|
+
CbEvents["OnRecvMessageEdited"] = "OnRecvMessageEdited";
|
|
408
409
|
CbEvents["OnRecvC2CReadReceipt"] = "OnRecvC2CReadReceipt";
|
|
409
410
|
CbEvents["OnRecvGroupReadReceipt"] = "OnRecvGroupReadReceipt";
|
|
410
411
|
CbEvents["OnConversationChanged"] = "OnConversationChanged";
|
|
@@ -938,33 +939,49 @@ let initialized = false;
|
|
|
938
939
|
let go;
|
|
939
940
|
let goExitPromise;
|
|
940
941
|
const CACHE_KEY = 'openim-wasm-cache';
|
|
942
|
+
const WASM_INIT_FAILED = 'WASM_INIT_FAILED';
|
|
941
943
|
async function initializeWasm(url) {
|
|
944
|
+
var _a;
|
|
942
945
|
if (initialized) {
|
|
943
946
|
return null;
|
|
944
947
|
}
|
|
945
948
|
if (typeof window === 'undefined') {
|
|
946
949
|
return Promise.resolve(null);
|
|
947
950
|
}
|
|
948
|
-
|
|
949
|
-
|
|
951
|
+
if (!url) {
|
|
952
|
+
throw new Error(`${WASM_INIT_FAILED}: empty wasm url`);
|
|
953
|
+
}
|
|
954
|
+
const nextGo = new Go();
|
|
950
955
|
try {
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
}
|
|
954
|
-
else {
|
|
955
|
-
const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
|
|
956
|
-
wasm = await WebAssembly.instantiate(bytes, go.importObject);
|
|
957
|
-
}
|
|
958
|
-
go.run(wasm.instance);
|
|
956
|
+
const wasm = await instantiate(url, nextGo);
|
|
957
|
+
nextGo.run(wasm.instance);
|
|
959
958
|
}
|
|
960
959
|
catch (error) {
|
|
961
960
|
console.error('Failed to initialize WASM:', error);
|
|
962
|
-
|
|
961
|
+
throw new Error(`${WASM_INIT_FAILED}: ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
963
962
|
}
|
|
963
|
+
go = nextGo;
|
|
964
964
|
await wait(100);
|
|
965
965
|
initialized = true;
|
|
966
966
|
return go;
|
|
967
967
|
}
|
|
968
|
+
async function instantiate(url, target) {
|
|
969
|
+
if (!('instantiateStreaming' in WebAssembly)) {
|
|
970
|
+
const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
|
|
971
|
+
return WebAssembly.instantiate(bytes, target.importObject);
|
|
972
|
+
}
|
|
973
|
+
try {
|
|
974
|
+
return await WebAssembly.instantiateStreaming(fetchWithCache(url), target.importObject);
|
|
975
|
+
}
|
|
976
|
+
catch (error) {
|
|
977
|
+
// A wrong Content-Type makes streaming reject; retry from raw bytes.
|
|
978
|
+
const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
|
|
979
|
+
return WebAssembly.instantiate(bytes, target.importObject);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
function reset() {
|
|
983
|
+
initialized = false;
|
|
984
|
+
}
|
|
968
985
|
function getGO() {
|
|
969
986
|
return go;
|
|
970
987
|
}
|
|
@@ -1201,10 +1218,15 @@ var ViewType;
|
|
|
1201
1218
|
ViewType[ViewType["Search"] = 1] = "Search";
|
|
1202
1219
|
})(ViewType || (ViewType = {}));
|
|
1203
1220
|
|
|
1221
|
+
const notInitializedError = () => ({
|
|
1222
|
+
errCode: 10001,
|
|
1223
|
+
errMsg: 'WASM_NOT_INITIALIZED',
|
|
1224
|
+
});
|
|
1204
1225
|
class SDK extends Emitter {
|
|
1205
1226
|
constructor(url = '/openIM.wasm', debug = true) {
|
|
1206
1227
|
super();
|
|
1207
1228
|
this.goExisted = false;
|
|
1229
|
+
this.initFailed = false;
|
|
1208
1230
|
this.tryParse = true;
|
|
1209
1231
|
this.isLogStandardOutput = true;
|
|
1210
1232
|
this.login = async (params, operationID = v4()) => {
|
|
@@ -1214,7 +1236,19 @@ class SDK extends Emitter {
|
|
|
1214
1236
|
operationID,
|
|
1215
1237
|
})}`);
|
|
1216
1238
|
await workerPromise;
|
|
1217
|
-
|
|
1239
|
+
try {
|
|
1240
|
+
await this.wasmInitializedPromise;
|
|
1241
|
+
}
|
|
1242
|
+
catch (error) {
|
|
1243
|
+
this._logWrap('SDK => wasm init failed', error);
|
|
1244
|
+
}
|
|
1245
|
+
if (!this.isGoRunning()) {
|
|
1246
|
+
throw {
|
|
1247
|
+
event: 'Login',
|
|
1248
|
+
operationID,
|
|
1249
|
+
...notInitializedError(),
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1218
1252
|
window.commonEventFunc(event => {
|
|
1219
1253
|
try {
|
|
1220
1254
|
this._logWrap(`%cSDK =>%c received event %c${event}%c `, logBoxStyleValue('#282828', '#ffffff'), '', 'color: #4f2398;', '');
|
|
@@ -1436,6 +1470,14 @@ class SDK extends Emitter {
|
|
|
1436
1470
|
data.clientMsgID,
|
|
1437
1471
|
]);
|
|
1438
1472
|
};
|
|
1473
|
+
this.editMessage = (data, operationID = v4()) => {
|
|
1474
|
+
return this._invoker('editMessage', window.editMessage, [
|
|
1475
|
+
operationID,
|
|
1476
|
+
data.conversationID,
|
|
1477
|
+
data.clientMsgID,
|
|
1478
|
+
data.content,
|
|
1479
|
+
]);
|
|
1480
|
+
};
|
|
1439
1481
|
this.pinMsg = (data, operationID = v4()) => {
|
|
1440
1482
|
return this._invoker('pinMsg', window.pinMsg, [
|
|
1441
1483
|
operationID,
|
|
@@ -2119,6 +2161,10 @@ class SDK extends Emitter {
|
|
|
2119
2161
|
initDatabaseAPI(debug);
|
|
2120
2162
|
this.isLogStandardOutput = debug;
|
|
2121
2163
|
this.wasmInitializedPromise = initializeWasm(url);
|
|
2164
|
+
// Let getSDK() rebuild the instance (e.g. with a new URL) after a failed load.
|
|
2165
|
+
this.wasmInitializedPromise.catch(() => {
|
|
2166
|
+
this.initFailed = true;
|
|
2167
|
+
});
|
|
2122
2168
|
this.goExitPromise = getGoExitPromise();
|
|
2123
2169
|
if (this.goExitPromise) {
|
|
2124
2170
|
this.goExitPromise
|
|
@@ -2133,6 +2179,10 @@ class SDK extends Emitter {
|
|
|
2133
2179
|
});
|
|
2134
2180
|
}
|
|
2135
2181
|
}
|
|
2182
|
+
isGoRunning() {
|
|
2183
|
+
const go = getGO();
|
|
2184
|
+
return !!go && !go.exited && !this.goExisted && !this.initFailed;
|
|
2185
|
+
}
|
|
2136
2186
|
_logWrap(...args) {
|
|
2137
2187
|
if (this.isLogStandardOutput) {
|
|
2138
2188
|
console.info(...args);
|
|
@@ -2147,8 +2197,8 @@ class SDK extends Emitter {
|
|
|
2147
2197
|
functionName.slice(1).toLowerCase()),
|
|
2148
2198
|
};
|
|
2149
2199
|
try {
|
|
2150
|
-
if (!
|
|
2151
|
-
throw
|
|
2200
|
+
if (!this.isGoRunning()) {
|
|
2201
|
+
throw notInitializedError();
|
|
2152
2202
|
}
|
|
2153
2203
|
let data = await func(...args);
|
|
2154
2204
|
if (processor) {
|
|
@@ -2182,13 +2232,16 @@ class SDK extends Emitter {
|
|
|
2182
2232
|
}
|
|
2183
2233
|
let instance;
|
|
2184
2234
|
function getSDK(config) {
|
|
2185
|
-
const { sqlWasmPath, coreWasmPath
|
|
2235
|
+
const { sqlWasmPath, coreWasmPath: configuredWasmPath, debug = true, } = config || {};
|
|
2236
|
+
// chat-sdk passes '' when no path is configured, which skips the default.
|
|
2237
|
+
const coreWasmPath = configuredWasmPath || '/openIM.wasm';
|
|
2186
2238
|
if (typeof window === 'undefined') {
|
|
2187
2239
|
return {};
|
|
2188
2240
|
}
|
|
2189
|
-
if (instance) {
|
|
2241
|
+
if (instance && !instance.initFailed) {
|
|
2190
2242
|
return instance;
|
|
2191
2243
|
}
|
|
2244
|
+
reset();
|
|
2192
2245
|
instance = new SDK(coreWasmPath, debug);
|
|
2193
2246
|
if (sqlWasmPath) {
|
|
2194
2247
|
window.setSqlWasmPath(sqlWasmPath);
|
package/lib/index.js
CHANGED
|
@@ -409,6 +409,7 @@ exports.CbEvents = void 0;
|
|
|
409
409
|
CbEvents["OnRecvMessageRevoked"] = "OnRecvMessageRevoked";
|
|
410
410
|
CbEvents["OnNewRecvMessageRevoked"] = "OnNewRecvMessageRevoked";
|
|
411
411
|
CbEvents["OnRecvMessagePinned"] = "OnRecvMessagePinned";
|
|
412
|
+
CbEvents["OnRecvMessageEdited"] = "OnRecvMessageEdited";
|
|
412
413
|
CbEvents["OnRecvC2CReadReceipt"] = "OnRecvC2CReadReceipt";
|
|
413
414
|
CbEvents["OnRecvGroupReadReceipt"] = "OnRecvGroupReadReceipt";
|
|
414
415
|
CbEvents["OnConversationChanged"] = "OnConversationChanged";
|
|
@@ -942,33 +943,49 @@ let initialized = false;
|
|
|
942
943
|
let go;
|
|
943
944
|
let goExitPromise;
|
|
944
945
|
const CACHE_KEY = 'openim-wasm-cache';
|
|
946
|
+
const WASM_INIT_FAILED = 'WASM_INIT_FAILED';
|
|
945
947
|
async function initializeWasm(url) {
|
|
948
|
+
var _a;
|
|
946
949
|
if (initialized) {
|
|
947
950
|
return null;
|
|
948
951
|
}
|
|
949
952
|
if (typeof window === 'undefined') {
|
|
950
953
|
return Promise.resolve(null);
|
|
951
954
|
}
|
|
952
|
-
|
|
953
|
-
|
|
955
|
+
if (!url) {
|
|
956
|
+
throw new Error(`${WASM_INIT_FAILED}: empty wasm url`);
|
|
957
|
+
}
|
|
958
|
+
const nextGo = new Go();
|
|
954
959
|
try {
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
}
|
|
958
|
-
else {
|
|
959
|
-
const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
|
|
960
|
-
wasm = await WebAssembly.instantiate(bytes, go.importObject);
|
|
961
|
-
}
|
|
962
|
-
go.run(wasm.instance);
|
|
960
|
+
const wasm = await instantiate(url, nextGo);
|
|
961
|
+
nextGo.run(wasm.instance);
|
|
963
962
|
}
|
|
964
963
|
catch (error) {
|
|
965
964
|
console.error('Failed to initialize WASM:', error);
|
|
966
|
-
|
|
965
|
+
throw new Error(`${WASM_INIT_FAILED}: ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
967
966
|
}
|
|
967
|
+
go = nextGo;
|
|
968
968
|
await wait(100);
|
|
969
969
|
initialized = true;
|
|
970
970
|
return go;
|
|
971
971
|
}
|
|
972
|
+
async function instantiate(url, target) {
|
|
973
|
+
if (!('instantiateStreaming' in WebAssembly)) {
|
|
974
|
+
const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
|
|
975
|
+
return WebAssembly.instantiate(bytes, target.importObject);
|
|
976
|
+
}
|
|
977
|
+
try {
|
|
978
|
+
return await WebAssembly.instantiateStreaming(fetchWithCache(url), target.importObject);
|
|
979
|
+
}
|
|
980
|
+
catch (error) {
|
|
981
|
+
// A wrong Content-Type makes streaming reject; retry from raw bytes.
|
|
982
|
+
const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
|
|
983
|
+
return WebAssembly.instantiate(bytes, target.importObject);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
function reset() {
|
|
987
|
+
initialized = false;
|
|
988
|
+
}
|
|
972
989
|
function getGO() {
|
|
973
990
|
return go;
|
|
974
991
|
}
|
|
@@ -1205,10 +1222,15 @@ exports.ViewType = void 0;
|
|
|
1205
1222
|
ViewType[ViewType["Search"] = 1] = "Search";
|
|
1206
1223
|
})(exports.ViewType || (exports.ViewType = {}));
|
|
1207
1224
|
|
|
1225
|
+
const notInitializedError = () => ({
|
|
1226
|
+
errCode: 10001,
|
|
1227
|
+
errMsg: 'WASM_NOT_INITIALIZED',
|
|
1228
|
+
});
|
|
1208
1229
|
class SDK extends Emitter {
|
|
1209
1230
|
constructor(url = '/openIM.wasm', debug = true) {
|
|
1210
1231
|
super();
|
|
1211
1232
|
this.goExisted = false;
|
|
1233
|
+
this.initFailed = false;
|
|
1212
1234
|
this.tryParse = true;
|
|
1213
1235
|
this.isLogStandardOutput = true;
|
|
1214
1236
|
this.login = async (params, operationID = v4()) => {
|
|
@@ -1218,7 +1240,19 @@ class SDK extends Emitter {
|
|
|
1218
1240
|
operationID,
|
|
1219
1241
|
})}`);
|
|
1220
1242
|
await workerPromise;
|
|
1221
|
-
|
|
1243
|
+
try {
|
|
1244
|
+
await this.wasmInitializedPromise;
|
|
1245
|
+
}
|
|
1246
|
+
catch (error) {
|
|
1247
|
+
this._logWrap('SDK => wasm init failed', error);
|
|
1248
|
+
}
|
|
1249
|
+
if (!this.isGoRunning()) {
|
|
1250
|
+
throw {
|
|
1251
|
+
event: 'Login',
|
|
1252
|
+
operationID,
|
|
1253
|
+
...notInitializedError(),
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1222
1256
|
window.commonEventFunc(event => {
|
|
1223
1257
|
try {
|
|
1224
1258
|
this._logWrap(`%cSDK =>%c received event %c${event}%c `, logBoxStyleValue('#282828', '#ffffff'), '', 'color: #4f2398;', '');
|
|
@@ -1440,6 +1474,14 @@ class SDK extends Emitter {
|
|
|
1440
1474
|
data.clientMsgID,
|
|
1441
1475
|
]);
|
|
1442
1476
|
};
|
|
1477
|
+
this.editMessage = (data, operationID = v4()) => {
|
|
1478
|
+
return this._invoker('editMessage', window.editMessage, [
|
|
1479
|
+
operationID,
|
|
1480
|
+
data.conversationID,
|
|
1481
|
+
data.clientMsgID,
|
|
1482
|
+
data.content,
|
|
1483
|
+
]);
|
|
1484
|
+
};
|
|
1443
1485
|
this.pinMsg = (data, operationID = v4()) => {
|
|
1444
1486
|
return this._invoker('pinMsg', window.pinMsg, [
|
|
1445
1487
|
operationID,
|
|
@@ -2123,6 +2165,10 @@ class SDK extends Emitter {
|
|
|
2123
2165
|
initDatabaseAPI(debug);
|
|
2124
2166
|
this.isLogStandardOutput = debug;
|
|
2125
2167
|
this.wasmInitializedPromise = initializeWasm(url);
|
|
2168
|
+
// Let getSDK() rebuild the instance (e.g. with a new URL) after a failed load.
|
|
2169
|
+
this.wasmInitializedPromise.catch(() => {
|
|
2170
|
+
this.initFailed = true;
|
|
2171
|
+
});
|
|
2126
2172
|
this.goExitPromise = getGoExitPromise();
|
|
2127
2173
|
if (this.goExitPromise) {
|
|
2128
2174
|
this.goExitPromise
|
|
@@ -2137,6 +2183,10 @@ class SDK extends Emitter {
|
|
|
2137
2183
|
});
|
|
2138
2184
|
}
|
|
2139
2185
|
}
|
|
2186
|
+
isGoRunning() {
|
|
2187
|
+
const go = getGO();
|
|
2188
|
+
return !!go && !go.exited && !this.goExisted && !this.initFailed;
|
|
2189
|
+
}
|
|
2140
2190
|
_logWrap(...args) {
|
|
2141
2191
|
if (this.isLogStandardOutput) {
|
|
2142
2192
|
console.info(...args);
|
|
@@ -2151,8 +2201,8 @@ class SDK extends Emitter {
|
|
|
2151
2201
|
functionName.slice(1).toLowerCase()),
|
|
2152
2202
|
};
|
|
2153
2203
|
try {
|
|
2154
|
-
if (!
|
|
2155
|
-
throw
|
|
2204
|
+
if (!this.isGoRunning()) {
|
|
2205
|
+
throw notInitializedError();
|
|
2156
2206
|
}
|
|
2157
2207
|
let data = await func(...args);
|
|
2158
2208
|
if (processor) {
|
|
@@ -2186,13 +2236,16 @@ class SDK extends Emitter {
|
|
|
2186
2236
|
}
|
|
2187
2237
|
let instance;
|
|
2188
2238
|
function getSDK(config) {
|
|
2189
|
-
const { sqlWasmPath, coreWasmPath
|
|
2239
|
+
const { sqlWasmPath, coreWasmPath: configuredWasmPath, debug = true, } = config || {};
|
|
2240
|
+
// chat-sdk passes '' when no path is configured, which skips the default.
|
|
2241
|
+
const coreWasmPath = configuredWasmPath || '/openIM.wasm';
|
|
2190
2242
|
if (typeof window === 'undefined') {
|
|
2191
2243
|
return {};
|
|
2192
2244
|
}
|
|
2193
|
-
if (instance) {
|
|
2245
|
+
if (instance && !instance.initFailed) {
|
|
2194
2246
|
return instance;
|
|
2195
2247
|
}
|
|
2248
|
+
reset();
|
|
2196
2249
|
instance = new SDK(coreWasmPath, debug);
|
|
2197
2250
|
if (sqlWasmPath) {
|
|
2198
2251
|
window.setSqlWasmPath(sqlWasmPath);
|
package/lib/index.umd.js
CHANGED
|
@@ -411,6 +411,7 @@
|
|
|
411
411
|
CbEvents["OnRecvMessageRevoked"] = "OnRecvMessageRevoked";
|
|
412
412
|
CbEvents["OnNewRecvMessageRevoked"] = "OnNewRecvMessageRevoked";
|
|
413
413
|
CbEvents["OnRecvMessagePinned"] = "OnRecvMessagePinned";
|
|
414
|
+
CbEvents["OnRecvMessageEdited"] = "OnRecvMessageEdited";
|
|
414
415
|
CbEvents["OnRecvC2CReadReceipt"] = "OnRecvC2CReadReceipt";
|
|
415
416
|
CbEvents["OnRecvGroupReadReceipt"] = "OnRecvGroupReadReceipt";
|
|
416
417
|
CbEvents["OnConversationChanged"] = "OnConversationChanged";
|
|
@@ -944,33 +945,49 @@
|
|
|
944
945
|
let go;
|
|
945
946
|
let goExitPromise;
|
|
946
947
|
const CACHE_KEY = 'openim-wasm-cache';
|
|
948
|
+
const WASM_INIT_FAILED = 'WASM_INIT_FAILED';
|
|
947
949
|
async function initializeWasm(url) {
|
|
950
|
+
var _a;
|
|
948
951
|
if (initialized) {
|
|
949
952
|
return null;
|
|
950
953
|
}
|
|
951
954
|
if (typeof window === 'undefined') {
|
|
952
955
|
return Promise.resolve(null);
|
|
953
956
|
}
|
|
954
|
-
|
|
955
|
-
|
|
957
|
+
if (!url) {
|
|
958
|
+
throw new Error(`${WASM_INIT_FAILED}: empty wasm url`);
|
|
959
|
+
}
|
|
960
|
+
const nextGo = new Go();
|
|
956
961
|
try {
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
}
|
|
960
|
-
else {
|
|
961
|
-
const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
|
|
962
|
-
wasm = await WebAssembly.instantiate(bytes, go.importObject);
|
|
963
|
-
}
|
|
964
|
-
go.run(wasm.instance);
|
|
962
|
+
const wasm = await instantiate(url, nextGo);
|
|
963
|
+
nextGo.run(wasm.instance);
|
|
965
964
|
}
|
|
966
965
|
catch (error) {
|
|
967
966
|
console.error('Failed to initialize WASM:', error);
|
|
968
|
-
|
|
967
|
+
throw new Error(`${WASM_INIT_FAILED}: ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
|
|
969
968
|
}
|
|
969
|
+
go = nextGo;
|
|
970
970
|
await wait(100);
|
|
971
971
|
initialized = true;
|
|
972
972
|
return go;
|
|
973
973
|
}
|
|
974
|
+
async function instantiate(url, target) {
|
|
975
|
+
if (!('instantiateStreaming' in WebAssembly)) {
|
|
976
|
+
const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
|
|
977
|
+
return WebAssembly.instantiate(bytes, target.importObject);
|
|
978
|
+
}
|
|
979
|
+
try {
|
|
980
|
+
return await WebAssembly.instantiateStreaming(fetchWithCache(url), target.importObject);
|
|
981
|
+
}
|
|
982
|
+
catch (error) {
|
|
983
|
+
// A wrong Content-Type makes streaming reject; retry from raw bytes.
|
|
984
|
+
const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
|
|
985
|
+
return WebAssembly.instantiate(bytes, target.importObject);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
function reset() {
|
|
989
|
+
initialized = false;
|
|
990
|
+
}
|
|
974
991
|
function getGO() {
|
|
975
992
|
return go;
|
|
976
993
|
}
|
|
@@ -1207,10 +1224,15 @@
|
|
|
1207
1224
|
ViewType[ViewType["Search"] = 1] = "Search";
|
|
1208
1225
|
})(exports.ViewType || (exports.ViewType = {}));
|
|
1209
1226
|
|
|
1227
|
+
const notInitializedError = () => ({
|
|
1228
|
+
errCode: 10001,
|
|
1229
|
+
errMsg: 'WASM_NOT_INITIALIZED',
|
|
1230
|
+
});
|
|
1210
1231
|
class SDK extends Emitter {
|
|
1211
1232
|
constructor(url = '/openIM.wasm', debug = true) {
|
|
1212
1233
|
super();
|
|
1213
1234
|
this.goExisted = false;
|
|
1235
|
+
this.initFailed = false;
|
|
1214
1236
|
this.tryParse = true;
|
|
1215
1237
|
this.isLogStandardOutput = true;
|
|
1216
1238
|
this.login = async (params, operationID = v4()) => {
|
|
@@ -1220,7 +1242,19 @@
|
|
|
1220
1242
|
operationID,
|
|
1221
1243
|
})}`);
|
|
1222
1244
|
await workerPromise;
|
|
1223
|
-
|
|
1245
|
+
try {
|
|
1246
|
+
await this.wasmInitializedPromise;
|
|
1247
|
+
}
|
|
1248
|
+
catch (error) {
|
|
1249
|
+
this._logWrap('SDK => wasm init failed', error);
|
|
1250
|
+
}
|
|
1251
|
+
if (!this.isGoRunning()) {
|
|
1252
|
+
throw {
|
|
1253
|
+
event: 'Login',
|
|
1254
|
+
operationID,
|
|
1255
|
+
...notInitializedError(),
|
|
1256
|
+
};
|
|
1257
|
+
}
|
|
1224
1258
|
window.commonEventFunc(event => {
|
|
1225
1259
|
try {
|
|
1226
1260
|
this._logWrap(`%cSDK =>%c received event %c${event}%c `, logBoxStyleValue('#282828', '#ffffff'), '', 'color: #4f2398;', '');
|
|
@@ -1442,6 +1476,14 @@
|
|
|
1442
1476
|
data.clientMsgID,
|
|
1443
1477
|
]);
|
|
1444
1478
|
};
|
|
1479
|
+
this.editMessage = (data, operationID = v4()) => {
|
|
1480
|
+
return this._invoker('editMessage', window.editMessage, [
|
|
1481
|
+
operationID,
|
|
1482
|
+
data.conversationID,
|
|
1483
|
+
data.clientMsgID,
|
|
1484
|
+
data.content,
|
|
1485
|
+
]);
|
|
1486
|
+
};
|
|
1445
1487
|
this.pinMsg = (data, operationID = v4()) => {
|
|
1446
1488
|
return this._invoker('pinMsg', window.pinMsg, [
|
|
1447
1489
|
operationID,
|
|
@@ -2125,6 +2167,10 @@
|
|
|
2125
2167
|
initDatabaseAPI(debug);
|
|
2126
2168
|
this.isLogStandardOutput = debug;
|
|
2127
2169
|
this.wasmInitializedPromise = initializeWasm(url);
|
|
2170
|
+
// Let getSDK() rebuild the instance (e.g. with a new URL) after a failed load.
|
|
2171
|
+
this.wasmInitializedPromise.catch(() => {
|
|
2172
|
+
this.initFailed = true;
|
|
2173
|
+
});
|
|
2128
2174
|
this.goExitPromise = getGoExitPromise();
|
|
2129
2175
|
if (this.goExitPromise) {
|
|
2130
2176
|
this.goExitPromise
|
|
@@ -2139,6 +2185,10 @@
|
|
|
2139
2185
|
});
|
|
2140
2186
|
}
|
|
2141
2187
|
}
|
|
2188
|
+
isGoRunning() {
|
|
2189
|
+
const go = getGO();
|
|
2190
|
+
return !!go && !go.exited && !this.goExisted && !this.initFailed;
|
|
2191
|
+
}
|
|
2142
2192
|
_logWrap(...args) {
|
|
2143
2193
|
if (this.isLogStandardOutput) {
|
|
2144
2194
|
console.info(...args);
|
|
@@ -2153,8 +2203,8 @@
|
|
|
2153
2203
|
functionName.slice(1).toLowerCase()),
|
|
2154
2204
|
};
|
|
2155
2205
|
try {
|
|
2156
|
-
if (!
|
|
2157
|
-
throw
|
|
2206
|
+
if (!this.isGoRunning()) {
|
|
2207
|
+
throw notInitializedError();
|
|
2158
2208
|
}
|
|
2159
2209
|
let data = await func(...args);
|
|
2160
2210
|
if (processor) {
|
|
@@ -2188,13 +2238,16 @@
|
|
|
2188
2238
|
}
|
|
2189
2239
|
let instance;
|
|
2190
2240
|
function getSDK(config) {
|
|
2191
|
-
const { sqlWasmPath, coreWasmPath
|
|
2241
|
+
const { sqlWasmPath, coreWasmPath: configuredWasmPath, debug = true, } = config || {};
|
|
2242
|
+
// chat-sdk passes '' when no path is configured, which skips the default.
|
|
2243
|
+
const coreWasmPath = configuredWasmPath || '/openIM.wasm';
|
|
2192
2244
|
if (typeof window === 'undefined') {
|
|
2193
2245
|
return {};
|
|
2194
2246
|
}
|
|
2195
|
-
if (instance) {
|
|
2247
|
+
if (instance && !instance.initFailed) {
|
|
2196
2248
|
return instance;
|
|
2197
2249
|
}
|
|
2250
|
+
reset();
|
|
2198
2251
|
instance = new SDK(coreWasmPath, debug);
|
|
2199
2252
|
if (sqlWasmPath) {
|
|
2200
2253
|
window.setSqlWasmPath(sqlWasmPath);
|
package/lib/sdk/index.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import Emitter from '../utils/emitter';
|
|
2
|
-
import { AccessFriendApplicationParams, AccessGroupApplicationParams, AccessMessageParams, AddFriendParams, AdvancedMsgParams, AdvancedQuoteMsgParams, AtMsgParams, ChangeGroupMemberMuteParams, ChangeGroupMuteParams, CreateGroupParams, CustomMsgParams, CustomSignalParams, FaceMessageParams, FileMsgParamsByURL, FindMessageParams, GetAdvancedHistoryMsgParams, GetGroupMemberByTimeParams, GetGroupMemberParams, GetGroupMessageReaderParams, GetHistoryMsgParams, GetOneConversationParams, ImageMsgParamsByURL, InitAndLoginConfig, InsertGroupMsgParams, InsertSingleMsgParams, AccessToGroupParams, SetConversationRecvOptParams, JoinGroupParams, LocationMsgParams, UpdateMemberInfoParams, MergerMsgParams, PartialUserItem, SetConversationPinParams, QuoteMsgParams, RemarkFriendParams, RtcActionParams, SearchFriendParams, SearchGroupMemberParams, SearchGroupParams, SearchPublicGroupsParams, SearchLocalParams, SendGroupReadReceiptParams, SendMsgParams, SetBurnDurationParams, SetConversationMsgDestructParams, SetConversationMsgDestructTimeParams, SetConversationDraftParams, SetGroupRoleParams, SetGroupRolePermissionsParams, SetGroupVerificationParams, SetMemberPermissionParams, SetMessageLocalExParams, SetConversationPrivateStateParams, SignalingInviteParams, SoundMsgParamsByURL, SplitConversationParams, SplitConversationAppParams, GetPinnedMessageListParams, TransferGroupParams, TypingUpdateParams, UploadFileParams, VideoMsgParamsByURL, SetGroupMemberNickParams, WasmPathConfig, PinFriendParams, SetFriendExParams, SetConversationExParams, AddBlackParams, OffsetParams, UpdateFriendsParams, SetConversationParams, GetSpecifiedFriendsParams, ChangeInputStatesParams, GetInputstatesParams } from '../types/params';
|
|
2
|
+
import { AccessFriendApplicationParams, AccessGroupApplicationParams, AccessMessageParams, EditMessageParams, AddFriendParams, AdvancedMsgParams, AdvancedQuoteMsgParams, AtMsgParams, ChangeGroupMemberMuteParams, ChangeGroupMuteParams, CreateGroupParams, CustomMsgParams, CustomSignalParams, FaceMessageParams, FileMsgParamsByURL, FindMessageParams, GetAdvancedHistoryMsgParams, GetGroupMemberByTimeParams, GetGroupMemberParams, GetGroupMessageReaderParams, GetHistoryMsgParams, GetOneConversationParams, ImageMsgParamsByURL, InitAndLoginConfig, InsertGroupMsgParams, InsertSingleMsgParams, AccessToGroupParams, SetConversationRecvOptParams, JoinGroupParams, LocationMsgParams, UpdateMemberInfoParams, MergerMsgParams, PartialUserItem, SetConversationPinParams, QuoteMsgParams, RemarkFriendParams, RtcActionParams, SearchFriendParams, SearchGroupMemberParams, SearchGroupParams, SearchPublicGroupsParams, SearchLocalParams, SendGroupReadReceiptParams, SendMsgParams, SetBurnDurationParams, SetConversationMsgDestructParams, SetConversationMsgDestructTimeParams, SetConversationDraftParams, SetGroupRoleParams, SetGroupRolePermissionsParams, SetGroupVerificationParams, SetMemberPermissionParams, SetMessageLocalExParams, SetConversationPrivateStateParams, SignalingInviteParams, SoundMsgParamsByURL, SplitConversationParams, SplitConversationAppParams, GetPinnedMessageListParams, TransferGroupParams, TypingUpdateParams, UploadFileParams, VideoMsgParamsByURL, SetGroupMemberNickParams, WasmPathConfig, PinFriendParams, SetFriendExParams, SetConversationExParams, AddBlackParams, OffsetParams, UpdateFriendsParams, SetConversationParams, GetSpecifiedFriendsParams, ChangeInputStatesParams, GetInputstatesParams } from '../types/params';
|
|
3
3
|
import { AdvancedGetMessageResult, BlackUserItem, CallingRoomData, CardElem, ConversationItem, FriendApplicationItem, FriendshipInfo, FriendUserItem, GroupApplicationItem, GroupItem, GroupMemberItem, GetFirstUnreadMessageResult, GetPinnedMessageListResult, MessageItem, PinnedMsgInfo, PublicUserItem, RtcInviteResults, SearchedFriendsInfo, SearchMessageResult, SearchPublicGroupsResult, SelfUserInfo, UserOnlineState, WsResponse } from '../types/entity';
|
|
4
4
|
import { LoginStatus, MessageReceiveOptType, Platform } from '../types/enum';
|
|
5
5
|
declare class SDK extends Emitter {
|
|
6
6
|
private wasmInitializedPromise;
|
|
7
7
|
private goExitPromise;
|
|
8
8
|
private goExisted;
|
|
9
|
+
initFailed: boolean;
|
|
9
10
|
private tryParse;
|
|
10
11
|
private isLogStandardOutput;
|
|
11
12
|
constructor(url?: string, debug?: boolean);
|
|
13
|
+
private isGoRunning;
|
|
12
14
|
_logWrap(...args: any[]): void;
|
|
13
15
|
_invoker<T>(functionName: string, func: (...args: any[]) => Promise<any>, args: any[], processor?: (data: string) => string): Promise<WsResponse<T>>;
|
|
14
16
|
login: (params: InitAndLoginConfig, operationID?: string) => Promise<any>;
|
|
@@ -43,6 +45,7 @@ declare class SDK extends Emitter {
|
|
|
43
45
|
exportDB(operationID?: string): Promise<WsResponse<unknown>>;
|
|
44
46
|
getHistoryMessageListReverse: (params: GetHistoryMsgParams, operationID?: string) => Promise<WsResponse<AdvancedGetMessageResult>>;
|
|
45
47
|
revokeMessage: <T>(data: AccessMessageParams, operationID?: string) => Promise<WsResponse<T>>;
|
|
48
|
+
editMessage: (data: EditMessageParams, operationID?: string) => Promise<WsResponse<MessageItem>>;
|
|
46
49
|
pinMsg: <T>(data: AccessMessageParams, operationID?: string) => Promise<WsResponse<T>>;
|
|
47
50
|
unpinMsg: <T>(data: AccessMessageParams, operationID?: string) => Promise<WsResponse<T>>;
|
|
48
51
|
getPinnedMsgs: (conversationID: string, operationID?: string) => Promise<WsResponse<PinnedMsgInfo[]>>;
|
package/lib/sdk/initialize.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/// <reference types="./src/types" />
|
|
2
|
+
export declare const WASM_INIT_FAILED = "WASM_INIT_FAILED";
|
|
2
3
|
export declare function initializeWasm(url: string): Promise<Go | null>;
|
|
3
4
|
export declare function reset(): void;
|
|
5
|
+
export declare function isInitialized(): boolean;
|
|
4
6
|
export declare function getGO(): Go;
|
|
5
7
|
export declare function getGoExitPromise(): Promise<void> | undefined;
|
package/lib/types/entity.d.ts
CHANGED
|
@@ -237,6 +237,8 @@ export declare type MessageItem = {
|
|
|
237
237
|
isPinned?: boolean;
|
|
238
238
|
pinnedByUserID?: string;
|
|
239
239
|
pinnedTime?: number;
|
|
240
|
+
isEdited?: boolean;
|
|
241
|
+
editTime?: number;
|
|
240
242
|
status: MessageStatus;
|
|
241
243
|
isReact?: boolean;
|
|
242
244
|
isExternalExtensions?: boolean;
|
|
@@ -348,6 +350,7 @@ export declare type AttachedInfoElem = {
|
|
|
348
350
|
inEncryptStatus: boolean;
|
|
349
351
|
burnDuration: number;
|
|
350
352
|
hasReadTime: number;
|
|
353
|
+
editTime?: number;
|
|
351
354
|
messageEntityList?: MessageEntity[];
|
|
352
355
|
uploadProgress?: UploadProgress;
|
|
353
356
|
};
|
package/lib/types/eventData.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export declare type EventDataMap = {
|
|
|
26
26
|
[CbEvents.OnNewConversation]: ConversationItem[];
|
|
27
27
|
[CbEvents.OnConversationUserInputStatusChanged]: ConversationInputStatus;
|
|
28
28
|
[CbEvents.OnNewRecvMessageRevoked]: RevokedInfo;
|
|
29
|
+
[CbEvents.OnRecvMessageEdited]: MessageItem;
|
|
29
30
|
[CbEvents.OnRecvC2CReadReceipt]: ReceiptInfo[];
|
|
30
31
|
[CbEvents.OnRecvGroupReadReceipt]: GroupMessageReceiptInfo;
|
|
31
32
|
[CbEvents.OnRecvNewMessage]: MessageItem;
|
package/lib/types/params.d.ts
CHANGED
|
@@ -254,6 +254,9 @@ export declare type AccessMessageParams = {
|
|
|
254
254
|
conversationID: string;
|
|
255
255
|
clientMsgID: string;
|
|
256
256
|
};
|
|
257
|
+
export declare type EditMessageParams = AccessMessageParams & {
|
|
258
|
+
content: string;
|
|
259
|
+
};
|
|
257
260
|
export declare type TypingUpdateParams = {
|
|
258
261
|
recvID: string;
|
|
259
262
|
msgTip: string;
|