stream-chat 4.4.2 → 4.4.3-dev.3

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); }
@@ -3465,13 +3475,7 @@ var InsightMetrics = function InsightMetrics() {
3465
3475
  this.instanceClientId = randomId();
3466
3476
  };
3467
3477
  function buildWsFatalInsight(connection, event) {
3468
- return _objectSpread$2({
3469
- err: {
3470
- wasClean: event.wasClean,
3471
- code: event.code,
3472
- reason: event.reason
3473
- }
3474
- }, buildWsBaseInsight(connection));
3478
+ return _objectSpread$2(_objectSpread$2({}, event), buildWsBaseInsight(connection));
3475
3479
  }
3476
3480
 
3477
3481
  function buildWsBaseInsight(connection) {
@@ -3624,7 +3628,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3624
3628
  user_token: _this.tokenManager.getToken(),
3625
3629
  server_determines_connection_id: true,
3626
3630
  device: _this.device,
3627
- request_id: reqID
3631
+ client_request_id: reqID
3628
3632
  };
3629
3633
  var qs = encodeURIComponent(JSON.stringify(params));
3630
3634
 
@@ -3709,13 +3713,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3709
3713
  });
3710
3714
 
3711
3715
  _defineProperty__default['default'](this, "onclose", function (wsID, event) {
3712
- if (event.code !== chatCodes.WS_CLOSED_SUCCESS) {
3713
- var _this$postInsights;
3714
-
3715
- _this.insightMetrics.wsConsecutiveFailures++;
3716
- _this.insightMetrics.wsTotalFailures++;
3717
- (_this$postInsights = _this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(_this, 'ws_fatal', buildWsFatalInsight(_this, event));
3718
- }
3716
+ if (_this.wsID !== wsID) return;
3719
3717
 
3720
3718
  _this.logger('info', 'connection:onclose() - onclose callback - ' + event.code, {
3721
3719
  tags: ['connection'],
@@ -3723,8 +3721,6 @@ var StableWSConnection = /*#__PURE__*/function () {
3723
3721
  wsID: wsID
3724
3722
  });
3725
3723
 
3726
- if (_this.wsID !== wsID) return;
3727
-
3728
3724
  if (event.code === chatCodes.WS_CLOSED_SUCCESS) {
3729
3725
  var _this$rejectPromise2;
3730
3726
 
@@ -3732,6 +3728,9 @@ var StableWSConnection = /*#__PURE__*/function () {
3732
3728
  // usually caused by invalid auth details
3733
3729
  var error = new Error("WS connection reject with error ".concat(event.reason));
3734
3730
  error.reason = event.reason;
3731
+ error.code = event.code;
3732
+ error.wasClean = event.wasClean;
3733
+ error.target = event.target;
3735
3734
  (_this$rejectPromise2 = _this.rejectPromise) === null || _this$rejectPromise2 === void 0 ? void 0 : _this$rejectPromise2.call(_this, error);
3736
3735
 
3737
3736
  _this.logger('info', "connection:onclose() - WS connection reject with error ".concat(event.reason), {
@@ -4262,7 +4261,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4262
4261
  key: "_connect",
4263
4262
  value: function () {
4264
4263
  var _connect3 = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee5() {
4265
- var wsURL, response, _this$postInsights2;
4264
+ var wsURL, response, _this$postInsights, insights;
4266
4265
 
4267
4266
  return _regeneratorRuntime__default['default'].wrap(function _callee5$(_context5) {
4268
4267
  while (1) {
@@ -4307,24 +4306,33 @@ var StableWSConnection = /*#__PURE__*/function () {
4307
4306
 
4308
4307
  this.connectionID = response.connection_id;
4309
4308
 
4310
- if (this.insightMetrics.wsConsecutiveFailures > 0) {
4311
- (_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));
4312
4311
  this.insightMetrics.wsConsecutiveFailures = 0;
4313
4312
  }
4314
4313
 
4315
4314
  return _context5.abrupt("return", response);
4316
4315
 
4317
4316
  case 23:
4318
- _context5.next = 29;
4317
+ _context5.next = 30;
4319
4318
  break;
4320
4319
 
4321
4320
  case 25:
4322
4321
  _context5.prev = 25;
4323
4322
  _context5.t0 = _context5["catch"](5);
4324
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
+
4325
4333
  throw _context5.t0;
4326
4334
 
4327
- case 29:
4335
+ case 30:
4328
4336
  case "end":
4329
4337
  return _context5.stop();
4330
4338
  }
@@ -5127,7 +5135,9 @@ var StreamChat = /*#__PURE__*/function () {
5127
5135
  var options,
5128
5136
  requestConfig,
5129
5137
  response,
5138
+ _requestConfig$header,
5130
5139
  _args4 = arguments;
5140
+
5131
5141
  return _regeneratorRuntime__default['default'].wrap(function _callee4$(_context4) {
5132
5142
  while (1) {
5133
5143
  switch (_context4.prev = _context4.next) {
@@ -5206,45 +5216,46 @@ var StreamChat = /*#__PURE__*/function () {
5206
5216
  case 39:
5207
5217
  _context4.prev = 39;
5208
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'];
5209
5220
 
5210
5221
  _this._logApiError(type, url, _context4.t1);
5211
5222
 
5212
5223
  _this.consecutiveFailures += 1;
5213
5224
 
5214
5225
  if (!_context4.t1.response) {
5215
- _context4.next = 55;
5226
+ _context4.next = 56;
5216
5227
  break;
5217
5228
  }
5218
5229
 
5219
5230
  if (!(_context4.t1.response.data.code === chatCodes.TOKEN_EXPIRED && !_this.tokenManager.isStatic())) {
5220
- _context4.next = 52;
5231
+ _context4.next = 53;
5221
5232
  break;
5222
5233
  }
5223
5234
 
5224
5235
  if (!(_this.consecutiveFailures > 1)) {
5225
- _context4.next = 48;
5236
+ _context4.next = 49;
5226
5237
  break;
5227
5238
  }
5228
5239
 
5229
- _context4.next = 48;
5240
+ _context4.next = 49;
5230
5241
  return sleep(retryInterval(_this.consecutiveFailures));
5231
5242
 
5232
- case 48:
5243
+ case 49:
5233
5244
  _this.tokenManager.loadToken();
5234
5245
 
5235
- _context4.next = 51;
5246
+ _context4.next = 52;
5236
5247
  return _this.doAxiosRequest(type, url, data, options);
5237
5248
 
5238
- case 51:
5249
+ case 52:
5239
5250
  return _context4.abrupt("return", _context4.sent);
5240
5251
 
5241
- case 52:
5252
+ case 53:
5242
5253
  return _context4.abrupt("return", _this.handleResponse(_context4.t1.response));
5243
5254
 
5244
- case 55:
5255
+ case 56:
5245
5256
  throw _context4.t1;
5246
5257
 
5247
- case 56:
5258
+ case 57:
5248
5259
  case "end":
5249
5260
  return _context4.stop();
5250
5261
  }
@@ -5563,7 +5574,7 @@ var StreamChat = /*#__PURE__*/function () {
5563
5574
 
5564
5575
  _context6.prev = 3;
5565
5576
  _context6.next = 6;
5566
- 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);
5567
5578
 
5568
5579
  case 6:
5569
5580
  _context6.next = 14;
@@ -5638,7 +5649,7 @@ var StreamChat = /*#__PURE__*/function () {
5638
5649
  }
5639
5650
 
5640
5651
  this.axiosInstance = axios__default['default'].create(this.options);
5641
- 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');
5642
5653
 
5643
5654
  if (typeof process !== 'undefined' && process.env.STREAM_LOCAL_TEST_RUN) {
5644
5655
  this.setBaseURL('http://localhost:3030');
@@ -6472,7 +6483,7 @@ var StreamChat = /*#__PURE__*/function () {
6472
6483
  */
6473
6484
  function () {
6474
6485
  var _connect = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee15() {
6475
- var client, warmUpPromise, handshake;
6486
+ var client;
6476
6487
  return _regeneratorRuntime__default['default'].wrap(function _callee15$(_context15) {
6477
6488
  while (1) {
6478
6489
  switch (_context15.prev = _context15.next) {
@@ -6504,7 +6515,11 @@ var StreamChat = /*#__PURE__*/function () {
6504
6515
  throw Error('clientID is not set');
6505
6516
 
6506
6517
  case 8:
6507
- // The StableWSConnection handles all the reconnection logic.
6518
+ if (!this.wsConnection && (this.options.warmUp || this.options.enableInsights)) {
6519
+ this.sayHi();
6520
+ } // The StableWSConnection handles all the reconnection logic.
6521
+
6522
+
6508
6523
  this.wsConnection = new StableWSConnection({
6509
6524
  wsBaseURL: client.wsBaseURL,
6510
6525
  clientID: client.clientID,
@@ -6522,40 +6537,18 @@ var StreamChat = /*#__PURE__*/function () {
6522
6537
  postInsights: this.options.enableInsights ? this.postInsights : undefined,
6523
6538
  insightMetrics: this.insightMetrics
6524
6539
  });
6525
-
6526
- if (this.options.warmUp) {
6527
- warmUpPromise = this.doAxiosRequest('options', this.baseURL + '/connect');
6528
- }
6529
-
6530
6540
  _context15.next = 12;
6531
6541
  return this.wsConnection.connect();
6532
6542
 
6533
6543
  case 12:
6534
- handshake = _context15.sent;
6535
- _context15.prev = 13;
6536
- _context15.next = 16;
6537
- return warmUpPromise;
6538
-
6539
- case 16:
6540
- _context15.next = 21;
6541
- break;
6542
-
6543
- case 18:
6544
- _context15.prev = 18;
6545
- _context15.t0 = _context15["catch"](13);
6546
- this.logger('error', 'Warmup request failed', {
6547
- error: _context15.t0
6548
- });
6549
-
6550
- case 21:
6551
- return _context15.abrupt("return", handshake);
6544
+ return _context15.abrupt("return", _context15.sent);
6552
6545
 
6553
- case 22:
6546
+ case 13:
6554
6547
  case "end":
6555
6548
  return _context15.stop();
6556
6549
  }
6557
6550
  }
6558
- }, _callee15, this, [[13, 18]]);
6551
+ }, _callee15, this);
6559
6552
  }));
6560
6553
 
6561
6554
  function connect() {
@@ -6564,6 +6557,25 @@ var StreamChat = /*#__PURE__*/function () {
6564
6557
 
6565
6558
  return connect;
6566
6559
  }()
6560
+ }, {
6561
+ key: "sayHi",
6562
+ value: function sayHi() {
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 + '/hi', null, opts).catch(function (e) {
6572
+ _this4.postInsights('http_hi_failed', {
6573
+ api_key: _this4.key,
6574
+ err: e,
6575
+ client_request_id: client_request_id
6576
+ });
6577
+ });
6578
+ }
6567
6579
  /**
6568
6580
  * queryUsers - Query users and watch user presence
6569
6581
  *
@@ -8375,7 +8387,7 @@ var StreamChat = /*#__PURE__*/function () {
8375
8387
  }, {
8376
8388
  key: "getUserAgent",
8377
8389
  value: function getUserAgent() {
8378
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.2");
8390
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.3-dev.3");
8379
8391
  }
8380
8392
  }, {
8381
8393
  key: "setUserAgent",
@@ -8389,7 +8401,7 @@ var StreamChat = /*#__PURE__*/function () {
8389
8401
  }, {
8390
8402
  key: "_enrichAxiosOptions",
8391
8403
  value: function _enrichAxiosOptions() {
8392
- var _this$wsConnection4;
8404
+ var _options$headers, _this$wsConnection4;
8393
8405
 
8394
8406
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
8395
8407
  params: {},
@@ -8399,6 +8411,12 @@ var StreamChat = /*#__PURE__*/function () {
8399
8411
 
8400
8412
  var token = this._getToken();
8401
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
+
8402
8420
  return _objectSpread({
8403
8421
  params: _objectSpread(_objectSpread({
8404
8422
  user_id: this.userID