@droppii-org/wasm-client-sdk 0.7.1 → 0.8.1

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 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/index.es.js CHANGED
@@ -938,33 +938,49 @@ let initialized = false;
938
938
  let go;
939
939
  let goExitPromise;
940
940
  const CACHE_KEY = 'openim-wasm-cache';
941
+ const WASM_INIT_FAILED = 'WASM_INIT_FAILED';
941
942
  async function initializeWasm(url) {
943
+ var _a;
942
944
  if (initialized) {
943
945
  return null;
944
946
  }
945
947
  if (typeof window === 'undefined') {
946
948
  return Promise.resolve(null);
947
949
  }
948
- go = new Go();
949
- let wasm;
950
+ if (!url) {
951
+ throw new Error(`${WASM_INIT_FAILED}: empty wasm url`);
952
+ }
953
+ const nextGo = new Go();
950
954
  try {
951
- if ('instantiateStreaming' in WebAssembly) {
952
- wasm = await WebAssembly.instantiateStreaming(fetchWithCache(url), go.importObject);
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);
955
+ const wasm = await instantiate(url, nextGo);
956
+ nextGo.run(wasm.instance);
959
957
  }
960
958
  catch (error) {
961
959
  console.error('Failed to initialize WASM:', error);
962
- return null;
960
+ throw new Error(`${WASM_INIT_FAILED}: ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
963
961
  }
962
+ go = nextGo;
964
963
  await wait(100);
965
964
  initialized = true;
966
965
  return go;
967
966
  }
967
+ async function instantiate(url, target) {
968
+ if (!('instantiateStreaming' in WebAssembly)) {
969
+ const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
970
+ return WebAssembly.instantiate(bytes, target.importObject);
971
+ }
972
+ try {
973
+ return await WebAssembly.instantiateStreaming(fetchWithCache(url), target.importObject);
974
+ }
975
+ catch (error) {
976
+ // A wrong Content-Type makes streaming reject; retry from raw bytes.
977
+ const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
978
+ return WebAssembly.instantiate(bytes, target.importObject);
979
+ }
980
+ }
981
+ function reset() {
982
+ initialized = false;
983
+ }
968
984
  function getGO() {
969
985
  return go;
970
986
  }
@@ -1201,10 +1217,15 @@ var ViewType;
1201
1217
  ViewType[ViewType["Search"] = 1] = "Search";
1202
1218
  })(ViewType || (ViewType = {}));
1203
1219
 
1220
+ const notInitializedError = () => ({
1221
+ errCode: 10001,
1222
+ errMsg: 'WASM_NOT_INITIALIZED',
1223
+ });
1204
1224
  class SDK extends Emitter {
1205
1225
  constructor(url = '/openIM.wasm', debug = true) {
1206
1226
  super();
1207
1227
  this.goExisted = false;
1228
+ this.initFailed = false;
1208
1229
  this.tryParse = true;
1209
1230
  this.isLogStandardOutput = true;
1210
1231
  this.login = async (params, operationID = v4()) => {
@@ -1214,7 +1235,19 @@ class SDK extends Emitter {
1214
1235
  operationID,
1215
1236
  })}`);
1216
1237
  await workerPromise;
1217
- await this.wasmInitializedPromise;
1238
+ try {
1239
+ await this.wasmInitializedPromise;
1240
+ }
1241
+ catch (error) {
1242
+ this._logWrap('SDK => wasm init failed', error);
1243
+ }
1244
+ if (!this.isGoRunning()) {
1245
+ throw {
1246
+ event: 'Login',
1247
+ operationID,
1248
+ ...notInitializedError(),
1249
+ };
1250
+ }
1218
1251
  window.commonEventFunc(event => {
1219
1252
  try {
1220
1253
  this._logWrap(`%cSDK =>%c received event %c${event}%c `, logBoxStyleValue('#282828', '#ffffff'), '', 'color: #4f2398;', '');
@@ -2119,6 +2152,10 @@ class SDK extends Emitter {
2119
2152
  initDatabaseAPI(debug);
2120
2153
  this.isLogStandardOutput = debug;
2121
2154
  this.wasmInitializedPromise = initializeWasm(url);
2155
+ // Let getSDK() rebuild the instance (e.g. with a new URL) after a failed load.
2156
+ this.wasmInitializedPromise.catch(() => {
2157
+ this.initFailed = true;
2158
+ });
2122
2159
  this.goExitPromise = getGoExitPromise();
2123
2160
  if (this.goExitPromise) {
2124
2161
  this.goExitPromise
@@ -2133,6 +2170,10 @@ class SDK extends Emitter {
2133
2170
  });
2134
2171
  }
2135
2172
  }
2173
+ isGoRunning() {
2174
+ const go = getGO();
2175
+ return !!go && !go.exited && !this.goExisted && !this.initFailed;
2176
+ }
2136
2177
  _logWrap(...args) {
2137
2178
  if (this.isLogStandardOutput) {
2138
2179
  console.info(...args);
@@ -2147,8 +2188,8 @@ class SDK extends Emitter {
2147
2188
  functionName.slice(1).toLowerCase()),
2148
2189
  };
2149
2190
  try {
2150
- if (!getGO() || getGO().exited || this.goExisted) {
2151
- throw 'wasm exist already, fail to run';
2191
+ if (!this.isGoRunning()) {
2192
+ throw notInitializedError();
2152
2193
  }
2153
2194
  let data = await func(...args);
2154
2195
  if (processor) {
@@ -2182,13 +2223,16 @@ class SDK extends Emitter {
2182
2223
  }
2183
2224
  let instance;
2184
2225
  function getSDK(config) {
2185
- const { sqlWasmPath, coreWasmPath = '/openIM.wasm', debug = true, } = config || {};
2226
+ const { sqlWasmPath, coreWasmPath: configuredWasmPath, debug = true, } = config || {};
2227
+ // chat-sdk passes '' when no path is configured, which skips the default.
2228
+ const coreWasmPath = configuredWasmPath || '/openIM.wasm';
2186
2229
  if (typeof window === 'undefined') {
2187
2230
  return {};
2188
2231
  }
2189
- if (instance) {
2232
+ if (instance && !instance.initFailed) {
2190
2233
  return instance;
2191
2234
  }
2235
+ reset();
2192
2236
  instance = new SDK(coreWasmPath, debug);
2193
2237
  if (sqlWasmPath) {
2194
2238
  window.setSqlWasmPath(sqlWasmPath);
package/lib/index.js CHANGED
@@ -942,33 +942,49 @@ let initialized = false;
942
942
  let go;
943
943
  let goExitPromise;
944
944
  const CACHE_KEY = 'openim-wasm-cache';
945
+ const WASM_INIT_FAILED = 'WASM_INIT_FAILED';
945
946
  async function initializeWasm(url) {
947
+ var _a;
946
948
  if (initialized) {
947
949
  return null;
948
950
  }
949
951
  if (typeof window === 'undefined') {
950
952
  return Promise.resolve(null);
951
953
  }
952
- go = new Go();
953
- let wasm;
954
+ if (!url) {
955
+ throw new Error(`${WASM_INIT_FAILED}: empty wasm url`);
956
+ }
957
+ const nextGo = new Go();
954
958
  try {
955
- if ('instantiateStreaming' in WebAssembly) {
956
- wasm = await WebAssembly.instantiateStreaming(fetchWithCache(url), go.importObject);
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);
959
+ const wasm = await instantiate(url, nextGo);
960
+ nextGo.run(wasm.instance);
963
961
  }
964
962
  catch (error) {
965
963
  console.error('Failed to initialize WASM:', error);
966
- return null;
964
+ throw new Error(`${WASM_INIT_FAILED}: ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
967
965
  }
966
+ go = nextGo;
968
967
  await wait(100);
969
968
  initialized = true;
970
969
  return go;
971
970
  }
971
+ async function instantiate(url, target) {
972
+ if (!('instantiateStreaming' in WebAssembly)) {
973
+ const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
974
+ return WebAssembly.instantiate(bytes, target.importObject);
975
+ }
976
+ try {
977
+ return await WebAssembly.instantiateStreaming(fetchWithCache(url), target.importObject);
978
+ }
979
+ catch (error) {
980
+ // A wrong Content-Type makes streaming reject; retry from raw bytes.
981
+ const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
982
+ return WebAssembly.instantiate(bytes, target.importObject);
983
+ }
984
+ }
985
+ function reset() {
986
+ initialized = false;
987
+ }
972
988
  function getGO() {
973
989
  return go;
974
990
  }
@@ -1205,10 +1221,15 @@ exports.ViewType = void 0;
1205
1221
  ViewType[ViewType["Search"] = 1] = "Search";
1206
1222
  })(exports.ViewType || (exports.ViewType = {}));
1207
1223
 
1224
+ const notInitializedError = () => ({
1225
+ errCode: 10001,
1226
+ errMsg: 'WASM_NOT_INITIALIZED',
1227
+ });
1208
1228
  class SDK extends Emitter {
1209
1229
  constructor(url = '/openIM.wasm', debug = true) {
1210
1230
  super();
1211
1231
  this.goExisted = false;
1232
+ this.initFailed = false;
1212
1233
  this.tryParse = true;
1213
1234
  this.isLogStandardOutput = true;
1214
1235
  this.login = async (params, operationID = v4()) => {
@@ -1218,7 +1239,19 @@ class SDK extends Emitter {
1218
1239
  operationID,
1219
1240
  })}`);
1220
1241
  await workerPromise;
1221
- await this.wasmInitializedPromise;
1242
+ try {
1243
+ await this.wasmInitializedPromise;
1244
+ }
1245
+ catch (error) {
1246
+ this._logWrap('SDK => wasm init failed', error);
1247
+ }
1248
+ if (!this.isGoRunning()) {
1249
+ throw {
1250
+ event: 'Login',
1251
+ operationID,
1252
+ ...notInitializedError(),
1253
+ };
1254
+ }
1222
1255
  window.commonEventFunc(event => {
1223
1256
  try {
1224
1257
  this._logWrap(`%cSDK =>%c received event %c${event}%c `, logBoxStyleValue('#282828', '#ffffff'), '', 'color: #4f2398;', '');
@@ -2123,6 +2156,10 @@ class SDK extends Emitter {
2123
2156
  initDatabaseAPI(debug);
2124
2157
  this.isLogStandardOutput = debug;
2125
2158
  this.wasmInitializedPromise = initializeWasm(url);
2159
+ // Let getSDK() rebuild the instance (e.g. with a new URL) after a failed load.
2160
+ this.wasmInitializedPromise.catch(() => {
2161
+ this.initFailed = true;
2162
+ });
2126
2163
  this.goExitPromise = getGoExitPromise();
2127
2164
  if (this.goExitPromise) {
2128
2165
  this.goExitPromise
@@ -2137,6 +2174,10 @@ class SDK extends Emitter {
2137
2174
  });
2138
2175
  }
2139
2176
  }
2177
+ isGoRunning() {
2178
+ const go = getGO();
2179
+ return !!go && !go.exited && !this.goExisted && !this.initFailed;
2180
+ }
2140
2181
  _logWrap(...args) {
2141
2182
  if (this.isLogStandardOutput) {
2142
2183
  console.info(...args);
@@ -2151,8 +2192,8 @@ class SDK extends Emitter {
2151
2192
  functionName.slice(1).toLowerCase()),
2152
2193
  };
2153
2194
  try {
2154
- if (!getGO() || getGO().exited || this.goExisted) {
2155
- throw 'wasm exist already, fail to run';
2195
+ if (!this.isGoRunning()) {
2196
+ throw notInitializedError();
2156
2197
  }
2157
2198
  let data = await func(...args);
2158
2199
  if (processor) {
@@ -2186,13 +2227,16 @@ class SDK extends Emitter {
2186
2227
  }
2187
2228
  let instance;
2188
2229
  function getSDK(config) {
2189
- const { sqlWasmPath, coreWasmPath = '/openIM.wasm', debug = true, } = config || {};
2230
+ const { sqlWasmPath, coreWasmPath: configuredWasmPath, debug = true, } = config || {};
2231
+ // chat-sdk passes '' when no path is configured, which skips the default.
2232
+ const coreWasmPath = configuredWasmPath || '/openIM.wasm';
2190
2233
  if (typeof window === 'undefined') {
2191
2234
  return {};
2192
2235
  }
2193
- if (instance) {
2236
+ if (instance && !instance.initFailed) {
2194
2237
  return instance;
2195
2238
  }
2239
+ reset();
2196
2240
  instance = new SDK(coreWasmPath, debug);
2197
2241
  if (sqlWasmPath) {
2198
2242
  window.setSqlWasmPath(sqlWasmPath);
package/lib/index.umd.js CHANGED
@@ -944,33 +944,49 @@
944
944
  let go;
945
945
  let goExitPromise;
946
946
  const CACHE_KEY = 'openim-wasm-cache';
947
+ const WASM_INIT_FAILED = 'WASM_INIT_FAILED';
947
948
  async function initializeWasm(url) {
949
+ var _a;
948
950
  if (initialized) {
949
951
  return null;
950
952
  }
951
953
  if (typeof window === 'undefined') {
952
954
  return Promise.resolve(null);
953
955
  }
954
- go = new Go();
955
- let wasm;
956
+ if (!url) {
957
+ throw new Error(`${WASM_INIT_FAILED}: empty wasm url`);
958
+ }
959
+ const nextGo = new Go();
956
960
  try {
957
- if ('instantiateStreaming' in WebAssembly) {
958
- wasm = await WebAssembly.instantiateStreaming(fetchWithCache(url), go.importObject);
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);
961
+ const wasm = await instantiate(url, nextGo);
962
+ nextGo.run(wasm.instance);
965
963
  }
966
964
  catch (error) {
967
965
  console.error('Failed to initialize WASM:', error);
968
- return null;
966
+ throw new Error(`${WASM_INIT_FAILED}: ${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}`);
969
967
  }
968
+ go = nextGo;
970
969
  await wait(100);
971
970
  initialized = true;
972
971
  return go;
973
972
  }
973
+ async function instantiate(url, target) {
974
+ if (!('instantiateStreaming' in WebAssembly)) {
975
+ const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
976
+ return WebAssembly.instantiate(bytes, target.importObject);
977
+ }
978
+ try {
979
+ return await WebAssembly.instantiateStreaming(fetchWithCache(url), target.importObject);
980
+ }
981
+ catch (error) {
982
+ // A wrong Content-Type makes streaming reject; retry from raw bytes.
983
+ const bytes = await fetchWithCache(url).then(resp => resp.arrayBuffer());
984
+ return WebAssembly.instantiate(bytes, target.importObject);
985
+ }
986
+ }
987
+ function reset() {
988
+ initialized = false;
989
+ }
974
990
  function getGO() {
975
991
  return go;
976
992
  }
@@ -1207,10 +1223,15 @@
1207
1223
  ViewType[ViewType["Search"] = 1] = "Search";
1208
1224
  })(exports.ViewType || (exports.ViewType = {}));
1209
1225
 
1226
+ const notInitializedError = () => ({
1227
+ errCode: 10001,
1228
+ errMsg: 'WASM_NOT_INITIALIZED',
1229
+ });
1210
1230
  class SDK extends Emitter {
1211
1231
  constructor(url = '/openIM.wasm', debug = true) {
1212
1232
  super();
1213
1233
  this.goExisted = false;
1234
+ this.initFailed = false;
1214
1235
  this.tryParse = true;
1215
1236
  this.isLogStandardOutput = true;
1216
1237
  this.login = async (params, operationID = v4()) => {
@@ -1220,7 +1241,19 @@
1220
1241
  operationID,
1221
1242
  })}`);
1222
1243
  await workerPromise;
1223
- await this.wasmInitializedPromise;
1244
+ try {
1245
+ await this.wasmInitializedPromise;
1246
+ }
1247
+ catch (error) {
1248
+ this._logWrap('SDK => wasm init failed', error);
1249
+ }
1250
+ if (!this.isGoRunning()) {
1251
+ throw {
1252
+ event: 'Login',
1253
+ operationID,
1254
+ ...notInitializedError(),
1255
+ };
1256
+ }
1224
1257
  window.commonEventFunc(event => {
1225
1258
  try {
1226
1259
  this._logWrap(`%cSDK =>%c received event %c${event}%c `, logBoxStyleValue('#282828', '#ffffff'), '', 'color: #4f2398;', '');
@@ -2125,6 +2158,10 @@
2125
2158
  initDatabaseAPI(debug);
2126
2159
  this.isLogStandardOutput = debug;
2127
2160
  this.wasmInitializedPromise = initializeWasm(url);
2161
+ // Let getSDK() rebuild the instance (e.g. with a new URL) after a failed load.
2162
+ this.wasmInitializedPromise.catch(() => {
2163
+ this.initFailed = true;
2164
+ });
2128
2165
  this.goExitPromise = getGoExitPromise();
2129
2166
  if (this.goExitPromise) {
2130
2167
  this.goExitPromise
@@ -2139,6 +2176,10 @@
2139
2176
  });
2140
2177
  }
2141
2178
  }
2179
+ isGoRunning() {
2180
+ const go = getGO();
2181
+ return !!go && !go.exited && !this.goExisted && !this.initFailed;
2182
+ }
2142
2183
  _logWrap(...args) {
2143
2184
  if (this.isLogStandardOutput) {
2144
2185
  console.info(...args);
@@ -2153,8 +2194,8 @@
2153
2194
  functionName.slice(1).toLowerCase()),
2154
2195
  };
2155
2196
  try {
2156
- if (!getGO() || getGO().exited || this.goExisted) {
2157
- throw 'wasm exist already, fail to run';
2197
+ if (!this.isGoRunning()) {
2198
+ throw notInitializedError();
2158
2199
  }
2159
2200
  let data = await func(...args);
2160
2201
  if (processor) {
@@ -2188,13 +2229,16 @@
2188
2229
  }
2189
2230
  let instance;
2190
2231
  function getSDK(config) {
2191
- const { sqlWasmPath, coreWasmPath = '/openIM.wasm', debug = true, } = config || {};
2232
+ const { sqlWasmPath, coreWasmPath: configuredWasmPath, debug = true, } = config || {};
2233
+ // chat-sdk passes '' when no path is configured, which skips the default.
2234
+ const coreWasmPath = configuredWasmPath || '/openIM.wasm';
2192
2235
  if (typeof window === 'undefined') {
2193
2236
  return {};
2194
2237
  }
2195
- if (instance) {
2238
+ if (instance && !instance.initFailed) {
2196
2239
  return instance;
2197
2240
  }
2241
+ reset();
2198
2242
  instance = new SDK(coreWasmPath, debug);
2199
2243
  if (sqlWasmPath) {
2200
2244
  window.setSqlWasmPath(sqlWasmPath);
@@ -6,9 +6,11 @@ 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>;
@@ -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;
@@ -210,6 +210,7 @@ export declare type ConversationItem = {
210
210
  peerType?: string;
211
211
  members?: ConversationGroupMember[];
212
212
  lastOpenTime?: number;
213
+ pinTime?: number;
213
214
  };
214
215
  export declare type ConversationGroupMember = {
215
216
  userID: string;