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.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); }
@@ -3442,13 +3452,7 @@ var InsightMetrics = function InsightMetrics() {
3442
3452
  this.instanceClientId = randomId();
3443
3453
  };
3444
3454
  function buildWsFatalInsight(connection, event) {
3445
- return _objectSpread$2({
3446
- err: {
3447
- wasClean: event.wasClean,
3448
- code: event.code,
3449
- reason: event.reason
3450
- }
3451
- }, buildWsBaseInsight(connection));
3455
+ return _objectSpread$2(_objectSpread$2({}, event), buildWsBaseInsight(connection));
3452
3456
  }
3453
3457
 
3454
3458
  function buildWsBaseInsight(connection) {
@@ -3601,7 +3605,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3601
3605
  user_token: _this.tokenManager.getToken(),
3602
3606
  server_determines_connection_id: true,
3603
3607
  device: _this.device,
3604
- request_id: reqID
3608
+ client_request_id: reqID
3605
3609
  };
3606
3610
  var qs = encodeURIComponent(JSON.stringify(params));
3607
3611
 
@@ -3686,13 +3690,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3686
3690
  });
3687
3691
 
3688
3692
  _defineProperty(this, "onclose", function (wsID, event) {
3689
- if (event.code !== chatCodes.WS_CLOSED_SUCCESS) {
3690
- var _this$postInsights;
3691
-
3692
- _this.insightMetrics.wsConsecutiveFailures++;
3693
- _this.insightMetrics.wsTotalFailures++;
3694
- (_this$postInsights = _this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(_this, 'ws_fatal', buildWsFatalInsight(_this, event));
3695
- }
3693
+ if (_this.wsID !== wsID) return;
3696
3694
 
3697
3695
  _this.logger('info', 'connection:onclose() - onclose callback - ' + event.code, {
3698
3696
  tags: ['connection'],
@@ -3700,8 +3698,6 @@ var StableWSConnection = /*#__PURE__*/function () {
3700
3698
  wsID: wsID
3701
3699
  });
3702
3700
 
3703
- if (_this.wsID !== wsID) return;
3704
-
3705
3701
  if (event.code === chatCodes.WS_CLOSED_SUCCESS) {
3706
3702
  var _this$rejectPromise2;
3707
3703
 
@@ -3709,6 +3705,9 @@ var StableWSConnection = /*#__PURE__*/function () {
3709
3705
  // usually caused by invalid auth details
3710
3706
  var error = new Error("WS connection reject with error ".concat(event.reason));
3711
3707
  error.reason = event.reason;
3708
+ error.code = event.code;
3709
+ error.wasClean = event.wasClean;
3710
+ error.target = event.target;
3712
3711
  (_this$rejectPromise2 = _this.rejectPromise) === null || _this$rejectPromise2 === void 0 ? void 0 : _this$rejectPromise2.call(_this, error);
3713
3712
 
3714
3713
  _this.logger('info', "connection:onclose() - WS connection reject with error ".concat(event.reason), {
@@ -4239,7 +4238,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4239
4238
  key: "_connect",
4240
4239
  value: function () {
4241
4240
  var _connect3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
4242
- var wsURL, response, _this$postInsights2;
4241
+ var wsURL, response, _this$postInsights, insights;
4243
4242
 
4244
4243
  return _regeneratorRuntime.wrap(function _callee5$(_context5) {
4245
4244
  while (1) {
@@ -4284,24 +4283,33 @@ var StableWSConnection = /*#__PURE__*/function () {
4284
4283
 
4285
4284
  this.connectionID = response.connection_id;
4286
4285
 
4287
- if (this.insightMetrics.wsConsecutiveFailures > 0) {
4288
- (_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));
4289
4288
  this.insightMetrics.wsConsecutiveFailures = 0;
4290
4289
  }
4291
4290
 
4292
4291
  return _context5.abrupt("return", response);
4293
4292
 
4294
4293
  case 23:
4295
- _context5.next = 29;
4294
+ _context5.next = 30;
4296
4295
  break;
4297
4296
 
4298
4297
  case 25:
4299
4298
  _context5.prev = 25;
4300
4299
  _context5.t0 = _context5["catch"](5);
4301
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
+
4302
4310
  throw _context5.t0;
4303
4311
 
4304
- case 29:
4312
+ case 30:
4305
4313
  case "end":
4306
4314
  return _context5.stop();
4307
4315
  }
@@ -5104,7 +5112,9 @@ var StreamChat = /*#__PURE__*/function () {
5104
5112
  var options,
5105
5113
  requestConfig,
5106
5114
  response,
5115
+ _requestConfig$header,
5107
5116
  _args4 = arguments;
5117
+
5108
5118
  return _regeneratorRuntime.wrap(function _callee4$(_context4) {
5109
5119
  while (1) {
5110
5120
  switch (_context4.prev = _context4.next) {
@@ -5183,45 +5193,46 @@ var StreamChat = /*#__PURE__*/function () {
5183
5193
  case 39:
5184
5194
  _context4.prev = 39;
5185
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'];
5186
5197
 
5187
5198
  _this._logApiError(type, url, _context4.t1);
5188
5199
 
5189
5200
  _this.consecutiveFailures += 1;
5190
5201
 
5191
5202
  if (!_context4.t1.response) {
5192
- _context4.next = 55;
5203
+ _context4.next = 56;
5193
5204
  break;
5194
5205
  }
5195
5206
 
5196
5207
  if (!(_context4.t1.response.data.code === chatCodes.TOKEN_EXPIRED && !_this.tokenManager.isStatic())) {
5197
- _context4.next = 52;
5208
+ _context4.next = 53;
5198
5209
  break;
5199
5210
  }
5200
5211
 
5201
5212
  if (!(_this.consecutiveFailures > 1)) {
5202
- _context4.next = 48;
5213
+ _context4.next = 49;
5203
5214
  break;
5204
5215
  }
5205
5216
 
5206
- _context4.next = 48;
5217
+ _context4.next = 49;
5207
5218
  return sleep(retryInterval(_this.consecutiveFailures));
5208
5219
 
5209
- case 48:
5220
+ case 49:
5210
5221
  _this.tokenManager.loadToken();
5211
5222
 
5212
- _context4.next = 51;
5223
+ _context4.next = 52;
5213
5224
  return _this.doAxiosRequest(type, url, data, options);
5214
5225
 
5215
- case 51:
5226
+ case 52:
5216
5227
  return _context4.abrupt("return", _context4.sent);
5217
5228
 
5218
- case 52:
5229
+ case 53:
5219
5230
  return _context4.abrupt("return", _this.handleResponse(_context4.t1.response));
5220
5231
 
5221
- case 55:
5232
+ case 56:
5222
5233
  throw _context4.t1;
5223
5234
 
5224
- case 56:
5235
+ case 57:
5225
5236
  case "end":
5226
5237
  return _context4.stop();
5227
5238
  }
@@ -5540,7 +5551,7 @@ var StreamChat = /*#__PURE__*/function () {
5540
5551
 
5541
5552
  _context6.prev = 3;
5542
5553
  _context6.next = 6;
5543
- 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);
5544
5555
 
5545
5556
  case 6:
5546
5557
  _context6.next = 14;
@@ -5615,7 +5626,7 @@ var StreamChat = /*#__PURE__*/function () {
5615
5626
  }
5616
5627
 
5617
5628
  this.axiosInstance = axios.create(this.options);
5618
- 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');
5619
5630
 
5620
5631
  if (typeof process !== 'undefined' && process.env.STREAM_LOCAL_TEST_RUN) {
5621
5632
  this.setBaseURL('http://localhost:3030');
@@ -6449,7 +6460,7 @@ var StreamChat = /*#__PURE__*/function () {
6449
6460
  */
6450
6461
  function () {
6451
6462
  var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee15() {
6452
- var client, warmUpPromise, handshake;
6463
+ var client;
6453
6464
  return _regeneratorRuntime.wrap(function _callee15$(_context15) {
6454
6465
  while (1) {
6455
6466
  switch (_context15.prev = _context15.next) {
@@ -6481,7 +6492,11 @@ var StreamChat = /*#__PURE__*/function () {
6481
6492
  throw Error('clientID is not set');
6482
6493
 
6483
6494
  case 8:
6484
- // The StableWSConnection handles all the reconnection logic.
6495
+ if (!this.wsConnection && (this.options.warmUp || this.options.enableInsights)) {
6496
+ this.sayHi();
6497
+ } // The StableWSConnection handles all the reconnection logic.
6498
+
6499
+
6485
6500
  this.wsConnection = new StableWSConnection({
6486
6501
  wsBaseURL: client.wsBaseURL,
6487
6502
  clientID: client.clientID,
@@ -6499,40 +6514,18 @@ var StreamChat = /*#__PURE__*/function () {
6499
6514
  postInsights: this.options.enableInsights ? this.postInsights : undefined,
6500
6515
  insightMetrics: this.insightMetrics
6501
6516
  });
6502
-
6503
- if (this.options.warmUp) {
6504
- warmUpPromise = this.doAxiosRequest('options', this.baseURL + '/connect');
6505
- }
6506
-
6507
6517
  _context15.next = 12;
6508
6518
  return this.wsConnection.connect();
6509
6519
 
6510
6520
  case 12:
6511
- handshake = _context15.sent;
6512
- _context15.prev = 13;
6513
- _context15.next = 16;
6514
- return warmUpPromise;
6515
-
6516
- case 16:
6517
- _context15.next = 21;
6518
- break;
6519
-
6520
- case 18:
6521
- _context15.prev = 18;
6522
- _context15.t0 = _context15["catch"](13);
6523
- this.logger('error', 'Warmup request failed', {
6524
- error: _context15.t0
6525
- });
6526
-
6527
- case 21:
6528
- return _context15.abrupt("return", handshake);
6521
+ return _context15.abrupt("return", _context15.sent);
6529
6522
 
6530
- case 22:
6523
+ case 13:
6531
6524
  case "end":
6532
6525
  return _context15.stop();
6533
6526
  }
6534
6527
  }
6535
- }, _callee15, this, [[13, 18]]);
6528
+ }, _callee15, this);
6536
6529
  }));
6537
6530
 
6538
6531
  function connect() {
@@ -6541,6 +6534,25 @@ var StreamChat = /*#__PURE__*/function () {
6541
6534
 
6542
6535
  return connect;
6543
6536
  }()
6537
+ }, {
6538
+ key: "sayHi",
6539
+ value: function sayHi() {
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 + '/hi', null, opts).catch(function (e) {
6549
+ _this4.postInsights('http_hi_failed', {
6550
+ api_key: _this4.key,
6551
+ err: e,
6552
+ client_request_id: client_request_id
6553
+ });
6554
+ });
6555
+ }
6544
6556
  /**
6545
6557
  * queryUsers - Query users and watch user presence
6546
6558
  *
@@ -8352,7 +8364,7 @@ var StreamChat = /*#__PURE__*/function () {
8352
8364
  }, {
8353
8365
  key: "getUserAgent",
8354
8366
  value: function getUserAgent() {
8355
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.2");
8367
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.3-dev.3");
8356
8368
  }
8357
8369
  }, {
8358
8370
  key: "setUserAgent",
@@ -8366,7 +8378,7 @@ var StreamChat = /*#__PURE__*/function () {
8366
8378
  }, {
8367
8379
  key: "_enrichAxiosOptions",
8368
8380
  value: function _enrichAxiosOptions() {
8369
- var _this$wsConnection4;
8381
+ var _options$headers, _this$wsConnection4;
8370
8382
 
8371
8383
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
8372
8384
  params: {},
@@ -8376,6 +8388,12 @@ var StreamChat = /*#__PURE__*/function () {
8376
8388
 
8377
8389
  var token = this._getToken();
8378
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
+
8379
8397
  return _objectSpread({
8380
8398
  params: _objectSpread(_objectSpread({
8381
8399
  user_id: this.userID