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.
@@ -930,6 +930,16 @@ function getRandomBytes(length) {
930
930
  return bytes;
931
931
  }
932
932
 
933
+ function convertErrorToJson(err) {
934
+ var jsonObj = {};
935
+ if (!err) return jsonObj;
936
+ Object.getOwnPropertyNames(err).forEach(function (key) {
937
+ // @ts-ignore
938
+ jsonObj[key] = err[key];
939
+ });
940
+ return jsonObj;
941
+ }
942
+
933
943
  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; } } }; }
934
944
 
935
945
  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); }
@@ -3441,13 +3451,7 @@ var InsightMetrics = function InsightMetrics() {
3441
3451
  this.instanceClientId = randomId();
3442
3452
  };
3443
3453
  function buildWsFatalInsight(connection, event) {
3444
- return _objectSpread$2({
3445
- err: {
3446
- wasClean: event.wasClean,
3447
- code: event.code,
3448
- reason: event.reason
3449
- }
3450
- }, buildWsBaseInsight(connection));
3454
+ return _objectSpread$2(_objectSpread$2({}, event), buildWsBaseInsight(connection));
3451
3455
  }
3452
3456
 
3453
3457
  function buildWsBaseInsight(connection) {
@@ -3600,7 +3604,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3600
3604
  user_token: _this.tokenManager.getToken(),
3601
3605
  server_determines_connection_id: true,
3602
3606
  device: _this.device,
3603
- request_id: reqID
3607
+ client_request_id: reqID
3604
3608
  };
3605
3609
  var qs = encodeURIComponent(JSON.stringify(params));
3606
3610
 
@@ -3685,13 +3689,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3685
3689
  });
3686
3690
 
3687
3691
  _defineProperty(this, "onclose", function (wsID, event) {
3688
- if (event.code !== chatCodes.WS_CLOSED_SUCCESS) {
3689
- var _this$postInsights;
3690
-
3691
- _this.insightMetrics.wsConsecutiveFailures++;
3692
- _this.insightMetrics.wsTotalFailures++;
3693
- (_this$postInsights = _this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(_this, 'ws_fatal', buildWsFatalInsight(_this, event));
3694
- }
3692
+ if (_this.wsID !== wsID) return;
3695
3693
 
3696
3694
  _this.logger('info', 'connection:onclose() - onclose callback - ' + event.code, {
3697
3695
  tags: ['connection'],
@@ -3699,8 +3697,6 @@ var StableWSConnection = /*#__PURE__*/function () {
3699
3697
  wsID: wsID
3700
3698
  });
3701
3699
 
3702
- if (_this.wsID !== wsID) return;
3703
-
3704
3700
  if (event.code === chatCodes.WS_CLOSED_SUCCESS) {
3705
3701
  var _this$rejectPromise2;
3706
3702
 
@@ -3708,6 +3704,9 @@ var StableWSConnection = /*#__PURE__*/function () {
3708
3704
  // usually caused by invalid auth details
3709
3705
  var error = new Error("WS connection reject with error ".concat(event.reason));
3710
3706
  error.reason = event.reason;
3707
+ error.code = event.code;
3708
+ error.wasClean = event.wasClean;
3709
+ error.target = event.target;
3711
3710
  (_this$rejectPromise2 = _this.rejectPromise) === null || _this$rejectPromise2 === void 0 ? void 0 : _this$rejectPromise2.call(_this, error);
3712
3711
 
3713
3712
  _this.logger('info', "connection:onclose() - WS connection reject with error ".concat(event.reason), {
@@ -4238,7 +4237,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4238
4237
  key: "_connect",
4239
4238
  value: function () {
4240
4239
  var _connect3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
4241
- var wsURL, response, _this$postInsights2;
4240
+ var wsURL, response, _this$postInsights, insights;
4242
4241
 
4243
4242
  return _regeneratorRuntime.wrap(function _callee5$(_context5) {
4244
4243
  while (1) {
@@ -4283,24 +4282,33 @@ var StableWSConnection = /*#__PURE__*/function () {
4283
4282
 
4284
4283
  this.connectionID = response.connection_id;
4285
4284
 
4286
- if (this.insightMetrics.wsConsecutiveFailures > 0) {
4287
- (_this$postInsights2 = this.postInsights) === null || _this$postInsights2 === void 0 ? void 0 : _this$postInsights2.call(this, 'ws_success_after_failure', buildWsSuccessAfterFailureInsight(this));
4285
+ if (this.insightMetrics.wsConsecutiveFailures > 0 && this.postInsights) {
4286
+ this.postInsights('ws_success_after_failure', buildWsSuccessAfterFailureInsight(this));
4288
4287
  this.insightMetrics.wsConsecutiveFailures = 0;
4289
4288
  }
4290
4289
 
4291
4290
  return _context5.abrupt("return", response);
4292
4291
 
4293
4292
  case 23:
4294
- _context5.next = 29;
4293
+ _context5.next = 30;
4295
4294
  break;
4296
4295
 
4297
4296
  case 25:
4298
4297
  _context5.prev = 25;
4299
4298
  _context5.t0 = _context5["catch"](5);
4300
4299
  this.isConnecting = false;
4300
+
4301
+ if (this.postInsights) {
4302
+ this.insightMetrics.wsConsecutiveFailures++;
4303
+ this.insightMetrics.wsTotalFailures++; // @ts-ignore
4304
+
4305
+ insights = buildWsFatalInsight(this, convertErrorToJson(_context5.t0));
4306
+ (_this$postInsights = this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(this, 'ws_fatal', insights);
4307
+ }
4308
+
4301
4309
  throw _context5.t0;
4302
4310
 
4303
- case 29:
4311
+ case 30:
4304
4312
  case "end":
4305
4313
  return _context5.stop();
4306
4314
  }
@@ -5101,7 +5109,9 @@ var StreamChat = /*#__PURE__*/function () {
5101
5109
  var options,
5102
5110
  requestConfig,
5103
5111
  response,
5112
+ _requestConfig$header,
5104
5113
  _args4 = arguments;
5114
+
5105
5115
  return _regeneratorRuntime.wrap(function _callee4$(_context4) {
5106
5116
  while (1) {
5107
5117
  switch (_context4.prev = _context4.next) {
@@ -5180,45 +5190,46 @@ var StreamChat = /*#__PURE__*/function () {
5180
5190
  case 39:
5181
5191
  _context4.prev = 39;
5182
5192
  _context4.t1 = _context4["catch"](4);
5193
+ _context4.t1.client_request_id = (_requestConfig$header = requestConfig.headers) === null || _requestConfig$header === void 0 ? void 0 : _requestConfig$header['x-client-request-id'];
5183
5194
 
5184
5195
  _this._logApiError(type, url, _context4.t1);
5185
5196
 
5186
5197
  _this.consecutiveFailures += 1;
5187
5198
 
5188
5199
  if (!_context4.t1.response) {
5189
- _context4.next = 55;
5200
+ _context4.next = 56;
5190
5201
  break;
5191
5202
  }
5192
5203
 
5193
5204
  if (!(_context4.t1.response.data.code === chatCodes.TOKEN_EXPIRED && !_this.tokenManager.isStatic())) {
5194
- _context4.next = 52;
5205
+ _context4.next = 53;
5195
5206
  break;
5196
5207
  }
5197
5208
 
5198
5209
  if (!(_this.consecutiveFailures > 1)) {
5199
- _context4.next = 48;
5210
+ _context4.next = 49;
5200
5211
  break;
5201
5212
  }
5202
5213
 
5203
- _context4.next = 48;
5214
+ _context4.next = 49;
5204
5215
  return sleep(retryInterval(_this.consecutiveFailures));
5205
5216
 
5206
- case 48:
5217
+ case 49:
5207
5218
  _this.tokenManager.loadToken();
5208
5219
 
5209
- _context4.next = 51;
5220
+ _context4.next = 52;
5210
5221
  return _this.doAxiosRequest(type, url, data, options);
5211
5222
 
5212
- case 51:
5223
+ case 52:
5213
5224
  return _context4.abrupt("return", _context4.sent);
5214
5225
 
5215
- case 52:
5226
+ case 53:
5216
5227
  return _context4.abrupt("return", _this.handleResponse(_context4.t1.response));
5217
5228
 
5218
- case 55:
5229
+ case 56:
5219
5230
  throw _context4.t1;
5220
5231
 
5221
- case 56:
5232
+ case 57:
5222
5233
  case "end":
5223
5234
  return _context4.stop();
5224
5235
  }
@@ -5537,7 +5548,7 @@ var StreamChat = /*#__PURE__*/function () {
5537
5548
 
5538
5549
  _context6.prev = 3;
5539
5550
  _context6.next = 6;
5540
- return _this.axiosInstance.post("https://insights.stream-io-api.com/insights/".concat(insightType), insights);
5551
+ return _this.axiosInstance.post("https://chat-insights.getstream.io/insights/".concat(insightType), insights);
5541
5552
 
5542
5553
  case 6:
5543
5554
  _context6.next = 14;
@@ -5612,7 +5623,7 @@ var StreamChat = /*#__PURE__*/function () {
5612
5623
  }
5613
5624
 
5614
5625
  this.axiosInstance = axios.create(this.options);
5615
- this.setBaseURL(this.options.baseURL || 'https://chat-us-east-1.stream-io-api.com');
5626
+ this.setBaseURL(this.options.baseURL || 'https://chat.stream-io-api.com');
5616
5627
 
5617
5628
  if (typeof process !== 'undefined' && process.env.STREAM_LOCAL_TEST_RUN) {
5618
5629
  this.setBaseURL('http://localhost:3030');
@@ -6446,7 +6457,7 @@ var StreamChat = /*#__PURE__*/function () {
6446
6457
  */
6447
6458
  function () {
6448
6459
  var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee15() {
6449
- var client, warmUpPromise, handshake;
6460
+ var client;
6450
6461
  return _regeneratorRuntime.wrap(function _callee15$(_context15) {
6451
6462
  while (1) {
6452
6463
  switch (_context15.prev = _context15.next) {
@@ -6478,7 +6489,11 @@ var StreamChat = /*#__PURE__*/function () {
6478
6489
  throw Error('clientID is not set');
6479
6490
 
6480
6491
  case 8:
6481
- // The StableWSConnection handles all the reconnection logic.
6492
+ if (!this.wsConnection && (this.options.warmUp || this.options.enableInsights)) {
6493
+ this.sayHi();
6494
+ } // The StableWSConnection handles all the reconnection logic.
6495
+
6496
+
6482
6497
  this.wsConnection = new StableWSConnection({
6483
6498
  wsBaseURL: client.wsBaseURL,
6484
6499
  clientID: client.clientID,
@@ -6496,40 +6511,18 @@ var StreamChat = /*#__PURE__*/function () {
6496
6511
  postInsights: this.options.enableInsights ? this.postInsights : undefined,
6497
6512
  insightMetrics: this.insightMetrics
6498
6513
  });
6499
-
6500
- if (this.options.warmUp) {
6501
- warmUpPromise = this.doAxiosRequest('options', this.baseURL + '/connect');
6502
- }
6503
-
6504
6514
  _context15.next = 12;
6505
6515
  return this.wsConnection.connect();
6506
6516
 
6507
6517
  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);
6518
+ return _context15.abrupt("return", _context15.sent);
6526
6519
 
6527
- case 22:
6520
+ case 13:
6528
6521
  case "end":
6529
6522
  return _context15.stop();
6530
6523
  }
6531
6524
  }
6532
- }, _callee15, this, [[13, 18]]);
6525
+ }, _callee15, this);
6533
6526
  }));
6534
6527
 
6535
6528
  function connect() {
@@ -6538,6 +6531,25 @@ var StreamChat = /*#__PURE__*/function () {
6538
6531
 
6539
6532
  return connect;
6540
6533
  }()
6534
+ }, {
6535
+ key: "sayHi",
6536
+ value: function sayHi() {
6537
+ var _this4 = this;
6538
+
6539
+ var client_request_id = randomId();
6540
+ var opts = {
6541
+ headers: {
6542
+ 'x-client-request-id': client_request_id
6543
+ }
6544
+ };
6545
+ this.doAxiosRequest('get', this.baseURL + '/hi', null, opts).catch(function (e) {
6546
+ _this4.postInsights('http_hi_failed', {
6547
+ api_key: _this4.key,
6548
+ err: e,
6549
+ client_request_id: client_request_id
6550
+ });
6551
+ });
6552
+ }
6541
6553
  /**
6542
6554
  * queryUsers - Query users and watch user presence
6543
6555
  *
@@ -8349,7 +8361,7 @@ var StreamChat = /*#__PURE__*/function () {
8349
8361
  }, {
8350
8362
  key: "getUserAgent",
8351
8363
  value: function getUserAgent() {
8352
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.2");
8364
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.3-dev.3");
8353
8365
  }
8354
8366
  }, {
8355
8367
  key: "setUserAgent",
@@ -8363,7 +8375,7 @@ var StreamChat = /*#__PURE__*/function () {
8363
8375
  }, {
8364
8376
  key: "_enrichAxiosOptions",
8365
8377
  value: function _enrichAxiosOptions() {
8366
- var _this$wsConnection4;
8378
+ var _options$headers, _this$wsConnection4;
8367
8379
 
8368
8380
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
8369
8381
  params: {},
@@ -8373,6 +8385,12 @@ var StreamChat = /*#__PURE__*/function () {
8373
8385
 
8374
8386
  var token = this._getToken();
8375
8387
 
8388
+ if (!((_options$headers = options.headers) !== null && _options$headers !== void 0 && _options$headers['x-client-request-id'])) {
8389
+ options.headers = {
8390
+ 'x-client-request-id': randomId()
8391
+ };
8392
+ }
8393
+
8376
8394
  return _objectSpread({
8377
8395
  params: _objectSpread(_objectSpread({
8378
8396
  user_id: this.userID