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.js CHANGED
@@ -954,6 +954,16 @@ function getRandomBytes(length) {
954
954
  return bytes;
955
955
  }
956
956
 
957
+ function convertErrorToJson(err) {
958
+ var jsonObj = {};
959
+ if (!err) return jsonObj;
960
+ Object.getOwnPropertyNames(err).forEach(function (key) {
961
+ // @ts-ignore
962
+ jsonObj[key] = err[key];
963
+ });
964
+ return jsonObj;
965
+ }
966
+
957
967
  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; } } }; }
958
968
 
959
969
  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); }
@@ -3448,7 +3458,6 @@ var ClientState = /*#__PURE__*/function () {
3448
3458
  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; }
3449
3459
 
3450
3460
  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__default['default'](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; }
3451
-
3452
3461
  var InsightMetrics = function InsightMetrics() {
3453
3462
  _classCallCheck__default['default'](this, InsightMetrics);
3454
3463
 
@@ -3458,18 +3467,15 @@ var InsightMetrics = function InsightMetrics() {
3458
3467
 
3459
3468
  _defineProperty__default['default'](this, "wsTotalFailures", void 0);
3460
3469
 
3470
+ _defineProperty__default['default'](this, "instanceClientId", void 0);
3471
+
3461
3472
  this.connectionStartTimestamp = null;
3462
3473
  this.wsTotalFailures = 0;
3463
3474
  this.wsConsecutiveFailures = 0;
3475
+ this.instanceClientId = randomId();
3464
3476
  };
3465
3477
  function buildWsFatalInsight(connection, event) {
3466
- return _objectSpread$2({
3467
- err: {
3468
- wasClean: event.wasClean,
3469
- code: event.code,
3470
- reason: event.reason
3471
- }
3472
- }, buildWsBaseInsight(connection));
3478
+ return _objectSpread$2(_objectSpread$2({}, event), buildWsBaseInsight(connection));
3473
3479
  }
3474
3480
 
3475
3481
  function buildWsBaseInsight(connection) {
@@ -3492,7 +3498,8 @@ function buildWsBaseInsight(connection) {
3492
3498
  ws_total_failures: connection.insightMetrics.wsTotalFailures,
3493
3499
  request_id: connection.requestID,
3494
3500
  online: typeof navigator !== 'undefined' ? (_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.onLine : null,
3495
- user_agent: typeof navigator !== 'undefined' ? (_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : _navigator2.userAgent : null
3501
+ user_agent: typeof navigator !== 'undefined' ? (_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : _navigator2.userAgent : null,
3502
+ instance_client_id: connection.insightMetrics.instanceClientId
3496
3503
  };
3497
3504
  }
3498
3505
 
@@ -3621,7 +3628,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3621
3628
  user_token: _this.tokenManager.getToken(),
3622
3629
  server_determines_connection_id: true,
3623
3630
  device: _this.device,
3624
- request_id: reqID
3631
+ client_request_id: reqID
3625
3632
  };
3626
3633
  var qs = encodeURIComponent(JSON.stringify(params));
3627
3634
 
@@ -3706,13 +3713,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3706
3713
  });
3707
3714
 
3708
3715
  _defineProperty__default['default'](this, "onclose", function (wsID, event) {
3709
- if (event.code !== chatCodes.WS_CLOSED_SUCCESS) {
3710
- var _this$postInsights;
3711
-
3712
- _this.insightMetrics.wsConsecutiveFailures++;
3713
- _this.insightMetrics.wsTotalFailures++;
3714
- (_this$postInsights = _this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(_this, 'ws_fatal', buildWsFatalInsight(_this, event));
3715
- }
3716
+ if (_this.wsID !== wsID) return;
3716
3717
 
3717
3718
  _this.logger('info', 'connection:onclose() - onclose callback - ' + event.code, {
3718
3719
  tags: ['connection'],
@@ -3720,8 +3721,6 @@ var StableWSConnection = /*#__PURE__*/function () {
3720
3721
  wsID: wsID
3721
3722
  });
3722
3723
 
3723
- if (_this.wsID !== wsID) return;
3724
-
3725
3724
  if (event.code === chatCodes.WS_CLOSED_SUCCESS) {
3726
3725
  var _this$rejectPromise2;
3727
3726
 
@@ -3729,6 +3728,9 @@ var StableWSConnection = /*#__PURE__*/function () {
3729
3728
  // usually caused by invalid auth details
3730
3729
  var error = new Error("WS connection reject with error ".concat(event.reason));
3731
3730
  error.reason = event.reason;
3731
+ error.code = event.code;
3732
+ error.wasClean = event.wasClean;
3733
+ error.target = event.target;
3732
3734
  (_this$rejectPromise2 = _this.rejectPromise) === null || _this$rejectPromise2 === void 0 ? void 0 : _this$rejectPromise2.call(_this, error);
3733
3735
 
3734
3736
  _this.logger('info', "connection:onclose() - WS connection reject with error ".concat(event.reason), {
@@ -4259,7 +4261,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4259
4261
  key: "_connect",
4260
4262
  value: function () {
4261
4263
  var _connect3 = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee5() {
4262
- var wsURL, response, _this$postInsights2;
4264
+ var wsURL, response, _this$postInsights, insights;
4263
4265
 
4264
4266
  return _regeneratorRuntime__default['default'].wrap(function _callee5$(_context5) {
4265
4267
  while (1) {
@@ -4304,24 +4306,33 @@ var StableWSConnection = /*#__PURE__*/function () {
4304
4306
 
4305
4307
  this.connectionID = response.connection_id;
4306
4308
 
4307
- if (this.insightMetrics.wsConsecutiveFailures > 0) {
4308
- (_this$postInsights2 = this.postInsights) === null || _this$postInsights2 === void 0 ? void 0 : _this$postInsights2.call(this, 'ws_success_after_failure', buildWsSuccessAfterFailureInsight(this));
4309
+ if (this.insightMetrics.wsConsecutiveFailures > 0 && this.postInsights) {
4310
+ this.postInsights('ws_success_after_failure', buildWsSuccessAfterFailureInsight(this));
4309
4311
  this.insightMetrics.wsConsecutiveFailures = 0;
4310
4312
  }
4311
4313
 
4312
4314
  return _context5.abrupt("return", response);
4313
4315
 
4314
4316
  case 23:
4315
- _context5.next = 29;
4317
+ _context5.next = 30;
4316
4318
  break;
4317
4319
 
4318
4320
  case 25:
4319
4321
  _context5.prev = 25;
4320
4322
  _context5.t0 = _context5["catch"](5);
4321
4323
  this.isConnecting = false;
4324
+
4325
+ if (this.postInsights) {
4326
+ this.insightMetrics.wsConsecutiveFailures++;
4327
+ this.insightMetrics.wsTotalFailures++; // @ts-ignore
4328
+
4329
+ insights = buildWsFatalInsight(this, convertErrorToJson(_context5.t0));
4330
+ (_this$postInsights = this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(this, 'ws_fatal', insights);
4331
+ }
4332
+
4322
4333
  throw _context5.t0;
4323
4334
 
4324
- case 29:
4335
+ case 30:
4325
4336
  case "end":
4326
4337
  return _context5.stop();
4327
4338
  }
@@ -5124,7 +5135,9 @@ var StreamChat = /*#__PURE__*/function () {
5124
5135
  var options,
5125
5136
  requestConfig,
5126
5137
  response,
5138
+ _requestConfig$header,
5127
5139
  _args4 = arguments;
5140
+
5128
5141
  return _regeneratorRuntime__default['default'].wrap(function _callee4$(_context4) {
5129
5142
  while (1) {
5130
5143
  switch (_context4.prev = _context4.next) {
@@ -5203,45 +5216,46 @@ var StreamChat = /*#__PURE__*/function () {
5203
5216
  case 39:
5204
5217
  _context4.prev = 39;
5205
5218
  _context4.t1 = _context4["catch"](4);
5219
+ _context4.t1.client_request_id = (_requestConfig$header = requestConfig.headers) === null || _requestConfig$header === void 0 ? void 0 : _requestConfig$header['x-client-request-id'];
5206
5220
 
5207
5221
  _this._logApiError(type, url, _context4.t1);
5208
5222
 
5209
5223
  _this.consecutiveFailures += 1;
5210
5224
 
5211
5225
  if (!_context4.t1.response) {
5212
- _context4.next = 55;
5226
+ _context4.next = 56;
5213
5227
  break;
5214
5228
  }
5215
5229
 
5216
5230
  if (!(_context4.t1.response.data.code === chatCodes.TOKEN_EXPIRED && !_this.tokenManager.isStatic())) {
5217
- _context4.next = 52;
5231
+ _context4.next = 53;
5218
5232
  break;
5219
5233
  }
5220
5234
 
5221
5235
  if (!(_this.consecutiveFailures > 1)) {
5222
- _context4.next = 48;
5236
+ _context4.next = 49;
5223
5237
  break;
5224
5238
  }
5225
5239
 
5226
- _context4.next = 48;
5240
+ _context4.next = 49;
5227
5241
  return sleep(retryInterval(_this.consecutiveFailures));
5228
5242
 
5229
- case 48:
5243
+ case 49:
5230
5244
  _this.tokenManager.loadToken();
5231
5245
 
5232
- _context4.next = 51;
5246
+ _context4.next = 52;
5233
5247
  return _this.doAxiosRequest(type, url, data, options);
5234
5248
 
5235
- case 51:
5249
+ case 52:
5236
5250
  return _context4.abrupt("return", _context4.sent);
5237
5251
 
5238
- case 52:
5252
+ case 53:
5239
5253
  return _context4.abrupt("return", _this.handleResponse(_context4.t1.response));
5240
5254
 
5241
- case 55:
5255
+ case 56:
5242
5256
  throw _context4.t1;
5243
5257
 
5244
- case 56:
5258
+ case 57:
5245
5259
  case "end":
5246
5260
  return _context4.stop();
5247
5261
  }
@@ -5560,7 +5574,7 @@ var StreamChat = /*#__PURE__*/function () {
5560
5574
 
5561
5575
  _context6.prev = 3;
5562
5576
  _context6.next = 6;
5563
- return _this.axiosInstance.post("https://insights.stream-io-api.com/insights/".concat(insightType), insights);
5577
+ return _this.axiosInstance.post("https://chat-insights.getstream.io/insights/".concat(insightType), insights);
5564
5578
 
5565
5579
  case 6:
5566
5580
  _context6.next = 14;
@@ -5635,7 +5649,7 @@ var StreamChat = /*#__PURE__*/function () {
5635
5649
  }
5636
5650
 
5637
5651
  this.axiosInstance = axios__default['default'].create(this.options);
5638
- this.setBaseURL(this.options.baseURL || 'https://chat-us-east-1.stream-io-api.com');
5652
+ this.setBaseURL(this.options.baseURL || 'https://chat.stream-io-api.com');
5639
5653
 
5640
5654
  if (typeof process !== 'undefined' && process.env.STREAM_LOCAL_TEST_RUN) {
5641
5655
  this.setBaseURL('http://localhost:3030');
@@ -6469,7 +6483,7 @@ var StreamChat = /*#__PURE__*/function () {
6469
6483
  */
6470
6484
  function () {
6471
6485
  var _connect = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee15() {
6472
- var client, warmUpPromise, handshake;
6486
+ var client;
6473
6487
  return _regeneratorRuntime__default['default'].wrap(function _callee15$(_context15) {
6474
6488
  while (1) {
6475
6489
  switch (_context15.prev = _context15.next) {
@@ -6501,7 +6515,11 @@ var StreamChat = /*#__PURE__*/function () {
6501
6515
  throw Error('clientID is not set');
6502
6516
 
6503
6517
  case 8:
6504
- // The StableWSConnection handles all the reconnection logic.
6518
+ if (!this.wsConnection && (this.options.warmUp || this.options.enableInsights)) {
6519
+ this.sendBeacon();
6520
+ } // The StableWSConnection handles all the reconnection logic.
6521
+
6522
+
6505
6523
  this.wsConnection = new StableWSConnection({
6506
6524
  wsBaseURL: client.wsBaseURL,
6507
6525
  clientID: client.clientID,
@@ -6519,40 +6537,18 @@ var StreamChat = /*#__PURE__*/function () {
6519
6537
  postInsights: this.options.enableInsights ? this.postInsights : undefined,
6520
6538
  insightMetrics: this.insightMetrics
6521
6539
  });
6522
-
6523
- if (this.options.warmUp) {
6524
- warmUpPromise = this.doAxiosRequest('options', this.baseURL + '/connect');
6525
- }
6526
-
6527
6540
  _context15.next = 12;
6528
6541
  return this.wsConnection.connect();
6529
6542
 
6530
6543
  case 12:
6531
- handshake = _context15.sent;
6532
- _context15.prev = 13;
6533
- _context15.next = 16;
6534
- return warmUpPromise;
6535
-
6536
- case 16:
6537
- _context15.next = 21;
6538
- break;
6539
-
6540
- case 18:
6541
- _context15.prev = 18;
6542
- _context15.t0 = _context15["catch"](13);
6543
- this.logger('error', 'Warmup request failed', {
6544
- error: _context15.t0
6545
- });
6546
-
6547
- case 21:
6548
- return _context15.abrupt("return", handshake);
6544
+ return _context15.abrupt("return", _context15.sent);
6549
6545
 
6550
- case 22:
6546
+ case 13:
6551
6547
  case "end":
6552
6548
  return _context15.stop();
6553
6549
  }
6554
6550
  }
6555
- }, _callee15, this, [[13, 18]]);
6551
+ }, _callee15, this);
6556
6552
  }));
6557
6553
 
6558
6554
  function connect() {
@@ -6561,6 +6557,25 @@ var StreamChat = /*#__PURE__*/function () {
6561
6557
 
6562
6558
  return connect;
6563
6559
  }()
6560
+ }, {
6561
+ key: "sendBeacon",
6562
+ value: function sendBeacon() {
6563
+ var _this4 = this;
6564
+
6565
+ var client_request_id = randomId();
6566
+ var opts = {
6567
+ headers: {
6568
+ 'x-client-request-id': client_request_id
6569
+ }
6570
+ };
6571
+ this.doAxiosRequest('get', this.baseURL + '/beacon', null, opts).catch(function (e) {
6572
+ _this4.postInsights('http_beacon_failed', {
6573
+ api_key: _this4.key,
6574
+ err: e,
6575
+ client_request_id: client_request_id
6576
+ });
6577
+ });
6578
+ }
6564
6579
  /**
6565
6580
  * queryUsers - Query users and watch user presence
6566
6581
  *
@@ -8372,7 +8387,7 @@ var StreamChat = /*#__PURE__*/function () {
8372
8387
  }, {
8373
8388
  key: "getUserAgent",
8374
8389
  value: function getUserAgent() {
8375
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.1");
8390
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.3-dev.2");
8376
8391
  }
8377
8392
  }, {
8378
8393
  key: "setUserAgent",
@@ -8386,7 +8401,7 @@ var StreamChat = /*#__PURE__*/function () {
8386
8401
  }, {
8387
8402
  key: "_enrichAxiosOptions",
8388
8403
  value: function _enrichAxiosOptions() {
8389
- var _this$wsConnection4;
8404
+ var _options$headers, _this$wsConnection4;
8390
8405
 
8391
8406
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
8392
8407
  params: {},
@@ -8396,6 +8411,12 @@ var StreamChat = /*#__PURE__*/function () {
8396
8411
 
8397
8412
  var token = this._getToken();
8398
8413
 
8414
+ if (!((_options$headers = options.headers) !== null && _options$headers !== void 0 && _options$headers['x-client-request-id'])) {
8415
+ options.headers = {
8416
+ 'x-client-request-id': randomId()
8417
+ };
8418
+ }
8419
+
8399
8420
  return _objectSpread({
8400
8421
  params: _objectSpread(_objectSpread({
8401
8422
  user_id: this.userID