@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.js CHANGED
@@ -82,6 +82,10 @@ var Api = class {
82
82
  data: params
83
83
  });
84
84
  }
85
+ async measureRTT(config) {
86
+ this.callKit.logger.debug(`Request ${config.url}:`, config.data);
87
+ return axios_default(config);
88
+ }
85
89
  async post(config) {
86
90
  this.callKit.logger.debug(`Request ${config.url}:`, config.data);
87
91
  const { userInfo, host } = this.callKit.config.getConfig();
@@ -113,6 +117,12 @@ var Api = class {
113
117
  }
114
118
  throw new Error(message ?? "Request failed");
115
119
  }
120
+ async get(config) {
121
+ return this.post({
122
+ ...config,
123
+ method: "get"
124
+ });
125
+ }
116
126
  };
117
127
 
118
128
  // package/const.ts
@@ -245,7 +255,14 @@ var KitEvent = {
245
255
  * User status change
246
256
  */
247
257
  USER_STATUS_CHANGE: "userStatusChange",
248
- CONNECT_EVENT: "CONNECT_EVENT"
258
+ /**
259
+ * SOCKET CONNECT EVENT
260
+ */
261
+ CONNECT_EVENT: "CONNECT_EVENT",
262
+ /**
263
+ * NETWORK EVENT
264
+ */
265
+ NETWORK_EVENT: "NETWORK_EVENT"
249
266
  };
250
267
  var ErrorCode = {
251
268
  /**
@@ -467,7 +484,13 @@ var ConnectEvent = {
467
484
  INCALL_RECONNECT_ERROR: "INCALL_RECONNECT_ERROR",
468
485
  INCALL_RECONNECT_SUCCESS: "INCALL_RECONNECT_SUCCESS",
469
486
  OPTIONS_HEARTBEAT_EXPIRED: "OPTIONS_HEARTBEAT_EXPIRED",
470
- USER_AGENT_START_ERROR: "USER_AGENT_START_ERROR"
487
+ USER_AGENT_START_ERROR: "USER_AGENT_START_ERROR",
488
+ CONNECT_CLOSING: "CONNECT_CLOSING",
489
+ CONNECT_CLOSING_END: "CONNECT_CLOSING_END"
490
+ };
491
+ var NetWorkEvent = {
492
+ RTT_MEASURE_SUCCESS: "RTT_MEASURE_SUCCESS",
493
+ RTT_MEASURE_FAILED: "RTT_MEASURE_FAILED"
471
494
  };
472
495
 
473
496
  // package/call.ts
@@ -1383,6 +1406,59 @@ var Connect = class {
1383
1406
  }
1384
1407
  };
1385
1408
 
1409
+ // package/monitor.ts
1410
+ var import_axios3 = __toESM(require("axios"));
1411
+ var defaultConfig = {
1412
+ interval: 5e3,
1413
+ apiEndpoint: "/livez"
1414
+ };
1415
+ var Monitor = class {
1416
+ callKit;
1417
+ interval = defaultConfig.interval;
1418
+ apiEndpoint = defaultConfig.apiEndpoint;
1419
+ monitorIntervalHandler = null;
1420
+ constructor(callKit) {
1421
+ this.callKit = callKit;
1422
+ const { monitor } = this.callKit.config.getConfig();
1423
+ this.interval = monitor?.interval ?? defaultConfig.interval;
1424
+ this.apiEndpoint = monitor?.apiEndpoint ?? defaultConfig.apiEndpoint;
1425
+ this.monitorIntervalHandler = null;
1426
+ }
1427
+ getEndPointPath = () => {
1428
+ const config = this.callKit.config.getConfig();
1429
+ const defaultPath = `${config.host}${this.apiEndpoint}`;
1430
+ const endPointPath = config.monitor?.apiEndpoint ?? defaultPath;
1431
+ return endPointPath;
1432
+ };
1433
+ measureRTT = async () => {
1434
+ try {
1435
+ const endPoint = this.getEndPointPath();
1436
+ const startTime = performance.now();
1437
+ await import_axios3.default.get(endPoint);
1438
+ const endTime = performance.now();
1439
+ const rtt = Math.round(endTime - startTime);
1440
+ this.callKit.trigger(KitEvent.NETWORK_EVENT, {
1441
+ event: NetWorkEvent.RTT_MEASURE_SUCCESS,
1442
+ rtt
1443
+ });
1444
+ } catch (error) {
1445
+ this.callKit.trigger(KitEvent.NETWORK_EVENT, {
1446
+ event: NetWorkEvent.RTT_MEASURE_FAILED,
1447
+ rtt: 0
1448
+ });
1449
+ }
1450
+ };
1451
+ stopRTTMonitoring() {
1452
+ if (this.monitorIntervalHandler) {
1453
+ clearInterval(this.monitorIntervalHandler);
1454
+ }
1455
+ }
1456
+ startRTTMonitoring() {
1457
+ this.stopRTTMonitoring();
1458
+ this.monitorIntervalHandler = setInterval(this.measureRTT, this.interval);
1459
+ }
1460
+ };
1461
+
1386
1462
  // package/socket.ts
1387
1463
  var RECONNECT_CONFIG = {
1388
1464
  enabled: true,
@@ -1406,6 +1482,7 @@ var Socket = class {
1406
1482
  reconnectTimer;
1407
1483
  isReconnecting = false;
1408
1484
  reconnectAttempts = 0;
1485
+ closing = false;
1409
1486
  constructor(callKit) {
1410
1487
  this.callKit = callKit;
1411
1488
  const { reconnect } = this.callKit.config.getConfig();
@@ -1419,6 +1496,16 @@ var Socket = class {
1419
1496
  this.callKit.logger.debug(`socket init: ${socket}`);
1420
1497
  this.connect(socket);
1421
1498
  }
1499
+ close(code, reason) {
1500
+ this.ws?.close(code, reason);
1501
+ }
1502
+ gracefulClose() {
1503
+ this.closing = true;
1504
+ this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1505
+ event: ConnectEvent.CONNECT_CLOSING
1506
+ });
1507
+ this.close(1e3, "normal");
1508
+ }
1422
1509
  reconnect(ev) {
1423
1510
  this.callKit.logger.debug("socket reconnect", ev);
1424
1511
  this.isConnected = false;
@@ -1484,6 +1571,10 @@ var Socket = class {
1484
1571
  }
1485
1572
  this.callKit.connect.hangup();
1486
1573
  this.reset();
1574
+ this.closing = false;
1575
+ this.callKit.trigger(KitEvent.CONNECT_EVENT, {
1576
+ event: ConnectEvent.CONNECT_CLOSING_END
1577
+ });
1487
1578
  }
1488
1579
  }
1489
1580
  onError(ev) {
@@ -1785,6 +1876,7 @@ var CallKit = class {
1785
1876
  connect;
1786
1877
  socket;
1787
1878
  user;
1879
+ monitor;
1788
1880
  listener = [];
1789
1881
  constructor(options) {
1790
1882
  this.config = new Config(this);
@@ -1802,6 +1894,8 @@ var CallKit = class {
1802
1894
  );
1803
1895
  this.config.setConfig("reconnect", options.reconnect);
1804
1896
  this.logger = new Logger(this, options.log);
1897
+ this.config.setConfig("monitor", options.monitor);
1898
+ this.monitor = new Monitor(this);
1805
1899
  this.logger.debug("callKit init", options);
1806
1900
  this.api = new Api(this);
1807
1901
  this.connect = new Connect(this);
@@ -1871,6 +1965,9 @@ var CallKit = class {
1871
1965
  await this.user.setUserStatus(UserStatus.offline);
1872
1966
  if (this.config.isLogin()) {
1873
1967
  const { sessionId } = this.config.getConfig().userInfo;
1968
+ this.unregister();
1969
+ this.stop();
1970
+ this.socket.gracefulClose();
1874
1971
  this.api.loginOut({ sessionId }).catch((err) => {
1875
1972
  this.logger.error(err, { errCode: ErrorCode.API_USER_LOGOUT_ERROR });
1876
1973
  });
@@ -1978,6 +2075,12 @@ var CallKit = class {
1978
2075
  await this.user.setUserStatus(UserStatus.offline);
1979
2076
  }
1980
2077
  }
2078
+ startRTTMonitoring() {
2079
+ this.monitor.startRTTMonitoring();
2080
+ }
2081
+ stopRTTMonitoring() {
2082
+ this.monitor.stopRTTMonitoring();
2083
+ }
1981
2084
  on(event, callback) {
1982
2085
  this.logger.debug(`on ${event}`);
1983
2086
  this.listener.push({