@koi-design/callkit 1.0.24 → 1.0.25

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/index.mjs CHANGED
@@ -49,6 +49,10 @@ var Api = class {
49
49
  data: params
50
50
  });
51
51
  }
52
+ async measureRTT(config) {
53
+ this.callKit.logger.debug(`Request ${config.url}:`, config.data);
54
+ return axios_default(config);
55
+ }
52
56
  async post(config) {
53
57
  this.callKit.logger.debug(`Request ${config.url}:`, config.data);
54
58
  const { userInfo, host } = this.callKit.config.getConfig();
@@ -80,6 +84,12 @@ var Api = class {
80
84
  }
81
85
  throw new Error(message ?? "Request failed");
82
86
  }
87
+ async get(config) {
88
+ return this.post({
89
+ ...config,
90
+ method: "get"
91
+ });
92
+ }
83
93
  };
84
94
 
85
95
  // package/const.ts
@@ -212,7 +222,14 @@ var KitEvent = {
212
222
  * User status change
213
223
  */
214
224
  USER_STATUS_CHANGE: "userStatusChange",
215
- CONNECT_EVENT: "CONNECT_EVENT"
225
+ /**
226
+ * SOCKET CONNECT EVENT
227
+ */
228
+ CONNECT_EVENT: "CONNECT_EVENT",
229
+ /**
230
+ * NETWORK EVENT
231
+ */
232
+ NETWORK_EVENT: "NETWORK_EVENT"
216
233
  };
217
234
  var ErrorCode = {
218
235
  /**
@@ -434,7 +451,13 @@ var ConnectEvent = {
434
451
  INCALL_RECONNECT_ERROR: "INCALL_RECONNECT_ERROR",
435
452
  INCALL_RECONNECT_SUCCESS: "INCALL_RECONNECT_SUCCESS",
436
453
  OPTIONS_HEARTBEAT_EXPIRED: "OPTIONS_HEARTBEAT_EXPIRED",
437
- USER_AGENT_START_ERROR: "USER_AGENT_START_ERROR"
454
+ USER_AGENT_START_ERROR: "USER_AGENT_START_ERROR",
455
+ CONNECT_CLOSING: "CONNECT_CLOSING",
456
+ CONNECT_CLOSING_END: "CONNECT_CLOSING_END"
457
+ };
458
+ var NetWorkEvent = {
459
+ RTT_MEASURE_SUCCESS: "RTT_MEASURE_SUCCESS",
460
+ RTT_MEASURE_FAILED: "RTT_MEASURE_FAILED"
438
461
  };
439
462
 
440
463
  // package/call.ts
@@ -1350,6 +1373,59 @@ var Connect = class {
1350
1373
  }
1351
1374
  };
1352
1375
 
1376
+ // package/monitor.ts
1377
+ import axios2 from "axios";
1378
+ var defaultConfig = {
1379
+ interval: 5e3,
1380
+ apiEndpoint: "/livez"
1381
+ };
1382
+ var Monitor = class {
1383
+ callKit;
1384
+ interval = defaultConfig.interval;
1385
+ apiEndpoint = defaultConfig.apiEndpoint;
1386
+ monitorIntervalHandler = null;
1387
+ constructor(callKit) {
1388
+ this.callKit = callKit;
1389
+ const { monitor } = this.callKit.config.getConfig();
1390
+ this.interval = monitor?.interval ?? defaultConfig.interval;
1391
+ this.apiEndpoint = monitor?.apiEndpoint ?? defaultConfig.apiEndpoint;
1392
+ this.monitorIntervalHandler = null;
1393
+ }
1394
+ getEndPointPath = () => {
1395
+ const config = this.callKit.config.getConfig();
1396
+ const defaultPath = `${config.host}${this.apiEndpoint}`;
1397
+ const endPointPath = config.monitor?.apiEndpoint ?? defaultPath;
1398
+ return endPointPath;
1399
+ };
1400
+ measureRTT = async () => {
1401
+ try {
1402
+ const endPoint = this.getEndPointPath();
1403
+ const startTime = performance.now();
1404
+ await axios2.get(endPoint);
1405
+ const endTime = performance.now();
1406
+ const rtt = Math.round(endTime - startTime);
1407
+ this.callKit.trigger(KitEvent.NETWORK_EVENT, {
1408
+ event: NetWorkEvent.RTT_MEASURE_SUCCESS,
1409
+ rtt
1410
+ });
1411
+ } catch (error) {
1412
+ this.callKit.trigger(KitEvent.NETWORK_EVENT, {
1413
+ event: NetWorkEvent.RTT_MEASURE_FAILED,
1414
+ rtt: 0
1415
+ });
1416
+ }
1417
+ };
1418
+ stopRTTMonitoring() {
1419
+ if (this.monitorIntervalHandler) {
1420
+ clearInterval(this.monitorIntervalHandler);
1421
+ }
1422
+ }
1423
+ startRTTMonitoring() {
1424
+ this.stopRTTMonitoring();
1425
+ this.monitorIntervalHandler = setInterval(this.measureRTT, this.interval);
1426
+ }
1427
+ };
1428
+
1353
1429
  // package/socket.ts
1354
1430
  var RECONNECT_CONFIG = {
1355
1431
  enabled: true,
@@ -1373,6 +1449,7 @@ var Socket = class {
1373
1449
  reconnectTimer;
1374
1450
  isReconnecting = false;
1375
1451
  reconnectAttempts = 0;
1452
+ closing = false;
1376
1453
  constructor(callKit) {
1377
1454
  this.callKit = callKit;
1378
1455
  const { reconnect } = this.callKit.config.getConfig();
@@ -1386,6 +1463,16 @@ var Socket = class {
1386
1463
  this.callKit.logger.debug(`socket init: ${socket}`);
1387
1464
  this.connect(socket);
1388
1465
  }
1466
+ close(code, reason) {
1467
+ this.ws?.close(code, reason);
1468
+ }
1469
+ gracefulClose() {
1470
+ this.closing = true;
1471
+ this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1472
+ event: ConnectEvent.CONNECT_CLOSING
1473
+ });
1474
+ this.close(1e3, "normal");
1475
+ }
1389
1476
  reconnect(ev) {
1390
1477
  this.callKit.logger.debug("socket reconnect", ev);
1391
1478
  this.isConnected = false;
@@ -1451,6 +1538,10 @@ var Socket = class {
1451
1538
  }
1452
1539
  this.callKit.connect.hangup();
1453
1540
  this.reset();
1541
+ this.closing = false;
1542
+ this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1543
+ event: ConnectEvent.CONNECT_CLOSING_END
1544
+ });
1454
1545
  }
1455
1546
  }
1456
1547
  onError(ev) {
@@ -1752,6 +1843,7 @@ var CallKit = class {
1752
1843
  connect;
1753
1844
  socket;
1754
1845
  user;
1846
+ monitor;
1755
1847
  listener = [];
1756
1848
  constructor(options) {
1757
1849
  this.config = new Config(this);
@@ -1769,6 +1861,8 @@ var CallKit = class {
1769
1861
  );
1770
1862
  this.config.setConfig("reconnect", options.reconnect);
1771
1863
  this.logger = new Logger(this, options.log);
1864
+ this.config.setConfig("monitor", options.monitor);
1865
+ this.monitor = new Monitor(this);
1772
1866
  this.logger.debug("callKit init", options);
1773
1867
  this.api = new Api(this);
1774
1868
  this.connect = new Connect(this);
@@ -1838,6 +1932,9 @@ var CallKit = class {
1838
1932
  await this.user.setUserStatus(UserStatus.offline);
1839
1933
  if (this.config.isLogin()) {
1840
1934
  const { sessionId } = this.config.getConfig().userInfo;
1935
+ this.unregister();
1936
+ this.stop();
1937
+ this.socket.gracefulClose();
1841
1938
  this.api.loginOut({ sessionId }).catch((err) => {
1842
1939
  this.logger.error(err, { errCode: ErrorCode.API_USER_LOGOUT_ERROR });
1843
1940
  });
@@ -1945,6 +2042,12 @@ var CallKit = class {
1945
2042
  await this.user.setUserStatus(UserStatus.offline);
1946
2043
  }
1947
2044
  }
2045
+ startRTTMonitoring() {
2046
+ this.monitor.startRTTMonitoring();
2047
+ }
2048
+ stopRTTMonitoring() {
2049
+ this.monitor.stopRTTMonitoring();
2050
+ }
1948
2051
  on(event, callback) {
1949
2052
  this.logger.debug(`on ${event}`);
1950
2053
  this.listener.push({