stream-chat 4.4.1 → 4.4.3-dev.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/index.es.js CHANGED
@@ -931,6 +931,16 @@ function getRandomBytes(length) {
931
931
  return bytes;
932
932
  }
933
933
 
934
+ function convertErrorToJson(err) {
935
+ var jsonObj = {};
936
+ if (!err) return jsonObj;
937
+ Object.getOwnPropertyNames(err).forEach(function (key) {
938
+ // @ts-ignore
939
+ jsonObj[key] = err[key];
940
+ });
941
+ return jsonObj;
942
+ }
943
+
934
944
  function _createForOfIteratorHelper$2(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$2(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
935
945
 
936
946
  function _unsupportedIterableToArray$2(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$2(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$2(o, minLen); }
@@ -3425,7 +3435,6 @@ var ClientState = /*#__PURE__*/function () {
3425
3435
  function ownKeys$2(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
3426
3436
 
3427
3437
  function _objectSpread$2(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$2(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$2(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
3428
-
3429
3438
  var InsightMetrics = function InsightMetrics() {
3430
3439
  _classCallCheck(this, InsightMetrics);
3431
3440
 
@@ -3435,18 +3444,15 @@ var InsightMetrics = function InsightMetrics() {
3435
3444
 
3436
3445
  _defineProperty(this, "wsTotalFailures", void 0);
3437
3446
 
3447
+ _defineProperty(this, "instanceClientId", void 0);
3448
+
3438
3449
  this.connectionStartTimestamp = null;
3439
3450
  this.wsTotalFailures = 0;
3440
3451
  this.wsConsecutiveFailures = 0;
3452
+ this.instanceClientId = randomId();
3441
3453
  };
3442
3454
  function buildWsFatalInsight(connection, event) {
3443
- return _objectSpread$2({
3444
- err: {
3445
- wasClean: event.wasClean,
3446
- code: event.code,
3447
- reason: event.reason
3448
- }
3449
- }, buildWsBaseInsight(connection));
3455
+ return _objectSpread$2(_objectSpread$2({}, event), buildWsBaseInsight(connection));
3450
3456
  }
3451
3457
 
3452
3458
  function buildWsBaseInsight(connection) {
@@ -3469,7 +3475,8 @@ function buildWsBaseInsight(connection) {
3469
3475
  ws_total_failures: connection.insightMetrics.wsTotalFailures,
3470
3476
  request_id: connection.requestID,
3471
3477
  online: typeof navigator !== 'undefined' ? (_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.onLine : null,
3472
- user_agent: typeof navigator !== 'undefined' ? (_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : _navigator2.userAgent : null
3478
+ user_agent: typeof navigator !== 'undefined' ? (_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : _navigator2.userAgent : null,
3479
+ instance_client_id: connection.insightMetrics.instanceClientId
3473
3480
  };
3474
3481
  }
3475
3482
 
@@ -3598,7 +3605,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3598
3605
  user_token: _this.tokenManager.getToken(),
3599
3606
  server_determines_connection_id: true,
3600
3607
  device: _this.device,
3601
- request_id: reqID
3608
+ client_request_id: reqID
3602
3609
  };
3603
3610
  var qs = encodeURIComponent(JSON.stringify(params));
3604
3611
 
@@ -3683,13 +3690,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3683
3690
  });
3684
3691
 
3685
3692
  _defineProperty(this, "onclose", function (wsID, event) {
3686
- if (event.code !== chatCodes.WS_CLOSED_SUCCESS) {
3687
- var _this$postInsights;
3688
-
3689
- _this.insightMetrics.wsConsecutiveFailures++;
3690
- _this.insightMetrics.wsTotalFailures++;
3691
- (_this$postInsights = _this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(_this, 'ws_fatal', buildWsFatalInsight(_this, event));
3692
- }
3693
+ if (_this.wsID !== wsID) return;
3693
3694
 
3694
3695
  _this.logger('info', 'connection:onclose() - onclose callback - ' + event.code, {
3695
3696
  tags: ['connection'],
@@ -3697,8 +3698,6 @@ var StableWSConnection = /*#__PURE__*/function () {
3697
3698
  wsID: wsID
3698
3699
  });
3699
3700
 
3700
- if (_this.wsID !== wsID) return;
3701
-
3702
3701
  if (event.code === chatCodes.WS_CLOSED_SUCCESS) {
3703
3702
  var _this$rejectPromise2;
3704
3703
 
@@ -3706,6 +3705,9 @@ var StableWSConnection = /*#__PURE__*/function () {
3706
3705
  // usually caused by invalid auth details
3707
3706
  var error = new Error("WS connection reject with error ".concat(event.reason));
3708
3707
  error.reason = event.reason;
3708
+ error.code = event.code;
3709
+ error.wasClean = event.wasClean;
3710
+ error.target = event.target;
3709
3711
  (_this$rejectPromise2 = _this.rejectPromise) === null || _this$rejectPromise2 === void 0 ? void 0 : _this$rejectPromise2.call(_this, error);
3710
3712
 
3711
3713
  _this.logger('info', "connection:onclose() - WS connection reject with error ".concat(event.reason), {
@@ -4236,7 +4238,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4236
4238
  key: "_connect",
4237
4239
  value: function () {
4238
4240
  var _connect3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
4239
- var wsURL, response, _this$postInsights2;
4241
+ var wsURL, response, _this$postInsights, insights;
4240
4242
 
4241
4243
  return _regeneratorRuntime.wrap(function _callee5$(_context5) {
4242
4244
  while (1) {
@@ -4281,24 +4283,33 @@ var StableWSConnection = /*#__PURE__*/function () {
4281
4283
 
4282
4284
  this.connectionID = response.connection_id;
4283
4285
 
4284
- if (this.insightMetrics.wsConsecutiveFailures > 0) {
4285
- (_this$postInsights2 = this.postInsights) === null || _this$postInsights2 === void 0 ? void 0 : _this$postInsights2.call(this, 'ws_success_after_failure', buildWsSuccessAfterFailureInsight(this));
4286
+ if (this.insightMetrics.wsConsecutiveFailures > 0 && this.postInsights) {
4287
+ this.postInsights('ws_success_after_failure', buildWsSuccessAfterFailureInsight(this));
4286
4288
  this.insightMetrics.wsConsecutiveFailures = 0;
4287
4289
  }
4288
4290
 
4289
4291
  return _context5.abrupt("return", response);
4290
4292
 
4291
4293
  case 23:
4292
- _context5.next = 29;
4294
+ _context5.next = 30;
4293
4295
  break;
4294
4296
 
4295
4297
  case 25:
4296
4298
  _context5.prev = 25;
4297
4299
  _context5.t0 = _context5["catch"](5);
4298
4300
  this.isConnecting = false;
4301
+
4302
+ if (this.postInsights) {
4303
+ this.insightMetrics.wsConsecutiveFailures++;
4304
+ this.insightMetrics.wsTotalFailures++; // @ts-ignore
4305
+
4306
+ insights = buildWsFatalInsight(this, convertErrorToJson(_context5.t0));
4307
+ (_this$postInsights = this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(this, 'ws_fatal', insights);
4308
+ }
4309
+
4299
4310
  throw _context5.t0;
4300
4311
 
4301
- case 29:
4312
+ case 30:
4302
4313
  case "end":
4303
4314
  return _context5.stop();
4304
4315
  }
@@ -5101,7 +5112,9 @@ var StreamChat = /*#__PURE__*/function () {
5101
5112
  var options,
5102
5113
  requestConfig,
5103
5114
  response,
5115
+ _requestConfig$header,
5104
5116
  _args4 = arguments;
5117
+
5105
5118
  return _regeneratorRuntime.wrap(function _callee4$(_context4) {
5106
5119
  while (1) {
5107
5120
  switch (_context4.prev = _context4.next) {
@@ -5180,45 +5193,46 @@ var StreamChat = /*#__PURE__*/function () {
5180
5193
  case 39:
5181
5194
  _context4.prev = 39;
5182
5195
  _context4.t1 = _context4["catch"](4);
5196
+ _context4.t1.client_request_id = (_requestConfig$header = requestConfig.headers) === null || _requestConfig$header === void 0 ? void 0 : _requestConfig$header['x-client-request-id'];
5183
5197
 
5184
5198
  _this._logApiError(type, url, _context4.t1);
5185
5199
 
5186
5200
  _this.consecutiveFailures += 1;
5187
5201
 
5188
5202
  if (!_context4.t1.response) {
5189
- _context4.next = 55;
5203
+ _context4.next = 56;
5190
5204
  break;
5191
5205
  }
5192
5206
 
5193
5207
  if (!(_context4.t1.response.data.code === chatCodes.TOKEN_EXPIRED && !_this.tokenManager.isStatic())) {
5194
- _context4.next = 52;
5208
+ _context4.next = 53;
5195
5209
  break;
5196
5210
  }
5197
5211
 
5198
5212
  if (!(_this.consecutiveFailures > 1)) {
5199
- _context4.next = 48;
5213
+ _context4.next = 49;
5200
5214
  break;
5201
5215
  }
5202
5216
 
5203
- _context4.next = 48;
5217
+ _context4.next = 49;
5204
5218
  return sleep(retryInterval(_this.consecutiveFailures));
5205
5219
 
5206
- case 48:
5220
+ case 49:
5207
5221
  _this.tokenManager.loadToken();
5208
5222
 
5209
- _context4.next = 51;
5223
+ _context4.next = 52;
5210
5224
  return _this.doAxiosRequest(type, url, data, options);
5211
5225
 
5212
- case 51:
5226
+ case 52:
5213
5227
  return _context4.abrupt("return", _context4.sent);
5214
5228
 
5215
- case 52:
5229
+ case 53:
5216
5230
  return _context4.abrupt("return", _this.handleResponse(_context4.t1.response));
5217
5231
 
5218
- case 55:
5232
+ case 56:
5219
5233
  throw _context4.t1;
5220
5234
 
5221
- case 56:
5235
+ case 57:
5222
5236
  case "end":
5223
5237
  return _context4.stop();
5224
5238
  }
@@ -5537,7 +5551,7 @@ var StreamChat = /*#__PURE__*/function () {
5537
5551
 
5538
5552
  _context6.prev = 3;
5539
5553
  _context6.next = 6;
5540
- return _this.axiosInstance.post("https://insights.stream-io-api.com/insights/".concat(insightType), insights);
5554
+ return _this.axiosInstance.post("https://chat-insights.getstream.io/insights/".concat(insightType), insights);
5541
5555
 
5542
5556
  case 6:
5543
5557
  _context6.next = 14;
@@ -5612,7 +5626,7 @@ var StreamChat = /*#__PURE__*/function () {
5612
5626
  }
5613
5627
 
5614
5628
  this.axiosInstance = axios.create(this.options);
5615
- this.setBaseURL(this.options.baseURL || 'https://chat-us-east-1.stream-io-api.com');
5629
+ this.setBaseURL(this.options.baseURL || 'https://chat.stream-io-api.com');
5616
5630
 
5617
5631
  if (typeof process !== 'undefined' && process.env.STREAM_LOCAL_TEST_RUN) {
5618
5632
  this.setBaseURL('http://localhost:3030');
@@ -6446,7 +6460,7 @@ var StreamChat = /*#__PURE__*/function () {
6446
6460
  */
6447
6461
  function () {
6448
6462
  var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee15() {
6449
- var client, warmUpPromise, handshake;
6463
+ var client;
6450
6464
  return _regeneratorRuntime.wrap(function _callee15$(_context15) {
6451
6465
  while (1) {
6452
6466
  switch (_context15.prev = _context15.next) {
@@ -6478,7 +6492,11 @@ var StreamChat = /*#__PURE__*/function () {
6478
6492
  throw Error('clientID is not set');
6479
6493
 
6480
6494
  case 8:
6481
- // The StableWSConnection handles all the reconnection logic.
6495
+ if (!this.wsConnection && (this.options.warmUp || this.options.enableInsights)) {
6496
+ this.sendBeacon();
6497
+ } // The StableWSConnection handles all the reconnection logic.
6498
+
6499
+
6482
6500
  this.wsConnection = new StableWSConnection({
6483
6501
  wsBaseURL: client.wsBaseURL,
6484
6502
  clientID: client.clientID,
@@ -6496,40 +6514,18 @@ var StreamChat = /*#__PURE__*/function () {
6496
6514
  postInsights: this.options.enableInsights ? this.postInsights : undefined,
6497
6515
  insightMetrics: this.insightMetrics
6498
6516
  });
6499
-
6500
- if (this.options.warmUp) {
6501
- warmUpPromise = this.doAxiosRequest('options', this.baseURL + '/connect');
6502
- }
6503
-
6504
6517
  _context15.next = 12;
6505
6518
  return this.wsConnection.connect();
6506
6519
 
6507
6520
  case 12:
6508
- handshake = _context15.sent;
6509
- _context15.prev = 13;
6510
- _context15.next = 16;
6511
- return warmUpPromise;
6512
-
6513
- case 16:
6514
- _context15.next = 21;
6515
- break;
6516
-
6517
- case 18:
6518
- _context15.prev = 18;
6519
- _context15.t0 = _context15["catch"](13);
6520
- this.logger('error', 'Warmup request failed', {
6521
- error: _context15.t0
6522
- });
6523
-
6524
- case 21:
6525
- return _context15.abrupt("return", handshake);
6521
+ return _context15.abrupt("return", _context15.sent);
6526
6522
 
6527
- case 22:
6523
+ case 13:
6528
6524
  case "end":
6529
6525
  return _context15.stop();
6530
6526
  }
6531
6527
  }
6532
- }, _callee15, this, [[13, 18]]);
6528
+ }, _callee15, this);
6533
6529
  }));
6534
6530
 
6535
6531
  function connect() {
@@ -6538,6 +6534,25 @@ var StreamChat = /*#__PURE__*/function () {
6538
6534
 
6539
6535
  return connect;
6540
6536
  }()
6537
+ }, {
6538
+ key: "sendBeacon",
6539
+ value: function sendBeacon() {
6540
+ var _this4 = this;
6541
+
6542
+ var client_request_id = randomId();
6543
+ var opts = {
6544
+ headers: {
6545
+ 'x-client-request-id': client_request_id
6546
+ }
6547
+ };
6548
+ this.doAxiosRequest('get', this.baseURL + '/beacon', null, opts).catch(function (e) {
6549
+ _this4.postInsights('http_beacon_failed', {
6550
+ api_key: _this4.key,
6551
+ err: e,
6552
+ client_request_id: client_request_id
6553
+ });
6554
+ });
6555
+ }
6541
6556
  /**
6542
6557
  * queryUsers - Query users and watch user presence
6543
6558
  *
@@ -8349,7 +8364,7 @@ var StreamChat = /*#__PURE__*/function () {
8349
8364
  }, {
8350
8365
  key: "getUserAgent",
8351
8366
  value: function getUserAgent() {
8352
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.1");
8367
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.3-dev.2");
8353
8368
  }
8354
8369
  }, {
8355
8370
  key: "setUserAgent",
@@ -8363,7 +8378,7 @@ var StreamChat = /*#__PURE__*/function () {
8363
8378
  }, {
8364
8379
  key: "_enrichAxiosOptions",
8365
8380
  value: function _enrichAxiosOptions() {
8366
- var _this$wsConnection4;
8381
+ var _options$headers, _this$wsConnection4;
8367
8382
 
8368
8383
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
8369
8384
  params: {},
@@ -8373,6 +8388,12 @@ var StreamChat = /*#__PURE__*/function () {
8373
8388
 
8374
8389
  var token = this._getToken();
8375
8390
 
8391
+ if (!((_options$headers = options.headers) !== null && _options$headers !== void 0 && _options$headers['x-client-request-id'])) {
8392
+ options.headers = {
8393
+ 'x-client-request-id': randomId()
8394
+ };
8395
+ }
8396
+
8376
8397
  return _objectSpread({
8377
8398
  params: _objectSpread(_objectSpread({
8378
8399
  user_id: this.userID