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/browser.js CHANGED
@@ -950,6 +950,16 @@ function getRandomBytes(length) {
950
950
  return bytes;
951
951
  }
952
952
 
953
+ function convertErrorToJson(err) {
954
+ var jsonObj = {};
955
+ if (!err) return jsonObj;
956
+ Object.getOwnPropertyNames(err).forEach(function (key) {
957
+ // @ts-ignore
958
+ jsonObj[key] = err[key];
959
+ });
960
+ return jsonObj;
961
+ }
962
+
953
963
  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; } } }; }
954
964
 
955
965
  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); }
@@ -3461,13 +3471,7 @@ var InsightMetrics = function InsightMetrics() {
3461
3471
  this.instanceClientId = randomId();
3462
3472
  };
3463
3473
  function buildWsFatalInsight(connection, event) {
3464
- return _objectSpread$2({
3465
- err: {
3466
- wasClean: event.wasClean,
3467
- code: event.code,
3468
- reason: event.reason
3469
- }
3470
- }, buildWsBaseInsight(connection));
3474
+ return _objectSpread$2(_objectSpread$2({}, event), buildWsBaseInsight(connection));
3471
3475
  }
3472
3476
 
3473
3477
  function buildWsBaseInsight(connection) {
@@ -3620,7 +3624,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3620
3624
  user_token: _this.tokenManager.getToken(),
3621
3625
  server_determines_connection_id: true,
3622
3626
  device: _this.device,
3623
- request_id: reqID
3627
+ client_request_id: reqID
3624
3628
  };
3625
3629
  var qs = encodeURIComponent(JSON.stringify(params));
3626
3630
 
@@ -3705,13 +3709,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3705
3709
  });
3706
3710
 
3707
3711
  _defineProperty__default['default'](this, "onclose", function (wsID, event) {
3708
- if (event.code !== chatCodes.WS_CLOSED_SUCCESS) {
3709
- var _this$postInsights;
3710
-
3711
- _this.insightMetrics.wsConsecutiveFailures++;
3712
- _this.insightMetrics.wsTotalFailures++;
3713
- (_this$postInsights = _this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(_this, 'ws_fatal', buildWsFatalInsight(_this, event));
3714
- }
3712
+ if (_this.wsID !== wsID) return;
3715
3713
 
3716
3714
  _this.logger('info', 'connection:onclose() - onclose callback - ' + event.code, {
3717
3715
  tags: ['connection'],
@@ -3719,8 +3717,6 @@ var StableWSConnection = /*#__PURE__*/function () {
3719
3717
  wsID: wsID
3720
3718
  });
3721
3719
 
3722
- if (_this.wsID !== wsID) return;
3723
-
3724
3720
  if (event.code === chatCodes.WS_CLOSED_SUCCESS) {
3725
3721
  var _this$rejectPromise2;
3726
3722
 
@@ -3728,6 +3724,9 @@ var StableWSConnection = /*#__PURE__*/function () {
3728
3724
  // usually caused by invalid auth details
3729
3725
  var error = new Error("WS connection reject with error ".concat(event.reason));
3730
3726
  error.reason = event.reason;
3727
+ error.code = event.code;
3728
+ error.wasClean = event.wasClean;
3729
+ error.target = event.target;
3731
3730
  (_this$rejectPromise2 = _this.rejectPromise) === null || _this$rejectPromise2 === void 0 ? void 0 : _this$rejectPromise2.call(_this, error);
3732
3731
 
3733
3732
  _this.logger('info', "connection:onclose() - WS connection reject with error ".concat(event.reason), {
@@ -4258,7 +4257,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4258
4257
  key: "_connect",
4259
4258
  value: function () {
4260
4259
  var _connect3 = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee5() {
4261
- var wsURL, response, _this$postInsights2;
4260
+ var wsURL, response, _this$postInsights, insights;
4262
4261
 
4263
4262
  return _regeneratorRuntime__default['default'].wrap(function _callee5$(_context5) {
4264
4263
  while (1) {
@@ -4303,24 +4302,33 @@ var StableWSConnection = /*#__PURE__*/function () {
4303
4302
 
4304
4303
  this.connectionID = response.connection_id;
4305
4304
 
4306
- if (this.insightMetrics.wsConsecutiveFailures > 0) {
4307
- (_this$postInsights2 = this.postInsights) === null || _this$postInsights2 === void 0 ? void 0 : _this$postInsights2.call(this, 'ws_success_after_failure', buildWsSuccessAfterFailureInsight(this));
4305
+ if (this.insightMetrics.wsConsecutiveFailures > 0 && this.postInsights) {
4306
+ this.postInsights('ws_success_after_failure', buildWsSuccessAfterFailureInsight(this));
4308
4307
  this.insightMetrics.wsConsecutiveFailures = 0;
4309
4308
  }
4310
4309
 
4311
4310
  return _context5.abrupt("return", response);
4312
4311
 
4313
4312
  case 23:
4314
- _context5.next = 29;
4313
+ _context5.next = 30;
4315
4314
  break;
4316
4315
 
4317
4316
  case 25:
4318
4317
  _context5.prev = 25;
4319
4318
  _context5.t0 = _context5["catch"](5);
4320
4319
  this.isConnecting = false;
4320
+
4321
+ if (this.postInsights) {
4322
+ this.insightMetrics.wsConsecutiveFailures++;
4323
+ this.insightMetrics.wsTotalFailures++; // @ts-ignore
4324
+
4325
+ insights = buildWsFatalInsight(this, convertErrorToJson(_context5.t0));
4326
+ (_this$postInsights = this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(this, 'ws_fatal', insights);
4327
+ }
4328
+
4321
4329
  throw _context5.t0;
4322
4330
 
4323
- case 29:
4331
+ case 30:
4324
4332
  case "end":
4325
4333
  return _context5.stop();
4326
4334
  }
@@ -5121,7 +5129,9 @@ var StreamChat = /*#__PURE__*/function () {
5121
5129
  var options,
5122
5130
  requestConfig,
5123
5131
  response,
5132
+ _requestConfig$header,
5124
5133
  _args4 = arguments;
5134
+
5125
5135
  return _regeneratorRuntime__default['default'].wrap(function _callee4$(_context4) {
5126
5136
  while (1) {
5127
5137
  switch (_context4.prev = _context4.next) {
@@ -5200,45 +5210,46 @@ var StreamChat = /*#__PURE__*/function () {
5200
5210
  case 39:
5201
5211
  _context4.prev = 39;
5202
5212
  _context4.t1 = _context4["catch"](4);
5213
+ _context4.t1.client_request_id = (_requestConfig$header = requestConfig.headers) === null || _requestConfig$header === void 0 ? void 0 : _requestConfig$header['x-client-request-id'];
5203
5214
 
5204
5215
  _this._logApiError(type, url, _context4.t1);
5205
5216
 
5206
5217
  _this.consecutiveFailures += 1;
5207
5218
 
5208
5219
  if (!_context4.t1.response) {
5209
- _context4.next = 55;
5220
+ _context4.next = 56;
5210
5221
  break;
5211
5222
  }
5212
5223
 
5213
5224
  if (!(_context4.t1.response.data.code === chatCodes.TOKEN_EXPIRED && !_this.tokenManager.isStatic())) {
5214
- _context4.next = 52;
5225
+ _context4.next = 53;
5215
5226
  break;
5216
5227
  }
5217
5228
 
5218
5229
  if (!(_this.consecutiveFailures > 1)) {
5219
- _context4.next = 48;
5230
+ _context4.next = 49;
5220
5231
  break;
5221
5232
  }
5222
5233
 
5223
- _context4.next = 48;
5234
+ _context4.next = 49;
5224
5235
  return sleep(retryInterval(_this.consecutiveFailures));
5225
5236
 
5226
- case 48:
5237
+ case 49:
5227
5238
  _this.tokenManager.loadToken();
5228
5239
 
5229
- _context4.next = 51;
5240
+ _context4.next = 52;
5230
5241
  return _this.doAxiosRequest(type, url, data, options);
5231
5242
 
5232
- case 51:
5243
+ case 52:
5233
5244
  return _context4.abrupt("return", _context4.sent);
5234
5245
 
5235
- case 52:
5246
+ case 53:
5236
5247
  return _context4.abrupt("return", _this.handleResponse(_context4.t1.response));
5237
5248
 
5238
- case 55:
5249
+ case 56:
5239
5250
  throw _context4.t1;
5240
5251
 
5241
- case 56:
5252
+ case 57:
5242
5253
  case "end":
5243
5254
  return _context4.stop();
5244
5255
  }
@@ -5557,7 +5568,7 @@ var StreamChat = /*#__PURE__*/function () {
5557
5568
 
5558
5569
  _context6.prev = 3;
5559
5570
  _context6.next = 6;
5560
- return _this.axiosInstance.post("https://insights.stream-io-api.com/insights/".concat(insightType), insights);
5571
+ return _this.axiosInstance.post("https://chat-insights.getstream.io/insights/".concat(insightType), insights);
5561
5572
 
5562
5573
  case 6:
5563
5574
  _context6.next = 14;
@@ -5632,7 +5643,7 @@ var StreamChat = /*#__PURE__*/function () {
5632
5643
  }
5633
5644
 
5634
5645
  this.axiosInstance = axios__default['default'].create(this.options);
5635
- this.setBaseURL(this.options.baseURL || 'https://chat-us-east-1.stream-io-api.com');
5646
+ this.setBaseURL(this.options.baseURL || 'https://chat.stream-io-api.com');
5636
5647
 
5637
5648
  if (typeof process !== 'undefined' && process.env.STREAM_LOCAL_TEST_RUN) {
5638
5649
  this.setBaseURL('http://localhost:3030');
@@ -6466,7 +6477,7 @@ var StreamChat = /*#__PURE__*/function () {
6466
6477
  */
6467
6478
  function () {
6468
6479
  var _connect = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee15() {
6469
- var client, warmUpPromise, handshake;
6480
+ var client;
6470
6481
  return _regeneratorRuntime__default['default'].wrap(function _callee15$(_context15) {
6471
6482
  while (1) {
6472
6483
  switch (_context15.prev = _context15.next) {
@@ -6498,7 +6509,11 @@ var StreamChat = /*#__PURE__*/function () {
6498
6509
  throw Error('clientID is not set');
6499
6510
 
6500
6511
  case 8:
6501
- // The StableWSConnection handles all the reconnection logic.
6512
+ if (!this.wsConnection && (this.options.warmUp || this.options.enableInsights)) {
6513
+ this.sayHi();
6514
+ } // The StableWSConnection handles all the reconnection logic.
6515
+
6516
+
6502
6517
  this.wsConnection = new StableWSConnection({
6503
6518
  wsBaseURL: client.wsBaseURL,
6504
6519
  clientID: client.clientID,
@@ -6516,40 +6531,18 @@ var StreamChat = /*#__PURE__*/function () {
6516
6531
  postInsights: this.options.enableInsights ? this.postInsights : undefined,
6517
6532
  insightMetrics: this.insightMetrics
6518
6533
  });
6519
-
6520
- if (this.options.warmUp) {
6521
- warmUpPromise = this.doAxiosRequest('options', this.baseURL + '/connect');
6522
- }
6523
-
6524
6534
  _context15.next = 12;
6525
6535
  return this.wsConnection.connect();
6526
6536
 
6527
6537
  case 12:
6528
- handshake = _context15.sent;
6529
- _context15.prev = 13;
6530
- _context15.next = 16;
6531
- return warmUpPromise;
6532
-
6533
- case 16:
6534
- _context15.next = 21;
6535
- break;
6536
-
6537
- case 18:
6538
- _context15.prev = 18;
6539
- _context15.t0 = _context15["catch"](13);
6540
- this.logger('error', 'Warmup request failed', {
6541
- error: _context15.t0
6542
- });
6543
-
6544
- case 21:
6545
- return _context15.abrupt("return", handshake);
6538
+ return _context15.abrupt("return", _context15.sent);
6546
6539
 
6547
- case 22:
6540
+ case 13:
6548
6541
  case "end":
6549
6542
  return _context15.stop();
6550
6543
  }
6551
6544
  }
6552
- }, _callee15, this, [[13, 18]]);
6545
+ }, _callee15, this);
6553
6546
  }));
6554
6547
 
6555
6548
  function connect() {
@@ -6558,6 +6551,25 @@ var StreamChat = /*#__PURE__*/function () {
6558
6551
 
6559
6552
  return connect;
6560
6553
  }()
6554
+ }, {
6555
+ key: "sayHi",
6556
+ value: function sayHi() {
6557
+ var _this4 = this;
6558
+
6559
+ var client_request_id = randomId();
6560
+ var opts = {
6561
+ headers: {
6562
+ 'x-client-request-id': client_request_id
6563
+ }
6564
+ };
6565
+ this.doAxiosRequest('get', this.baseURL + '/hi', null, opts).catch(function (e) {
6566
+ _this4.postInsights('http_hi_failed', {
6567
+ api_key: _this4.key,
6568
+ err: e,
6569
+ client_request_id: client_request_id
6570
+ });
6571
+ });
6572
+ }
6561
6573
  /**
6562
6574
  * queryUsers - Query users and watch user presence
6563
6575
  *
@@ -8369,7 +8381,7 @@ var StreamChat = /*#__PURE__*/function () {
8369
8381
  }, {
8370
8382
  key: "getUserAgent",
8371
8383
  value: function getUserAgent() {
8372
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.2");
8384
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.3-dev.3");
8373
8385
  }
8374
8386
  }, {
8375
8387
  key: "setUserAgent",
@@ -8383,7 +8395,7 @@ var StreamChat = /*#__PURE__*/function () {
8383
8395
  }, {
8384
8396
  key: "_enrichAxiosOptions",
8385
8397
  value: function _enrichAxiosOptions() {
8386
- var _this$wsConnection4;
8398
+ var _options$headers, _this$wsConnection4;
8387
8399
 
8388
8400
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
8389
8401
  params: {},
@@ -8393,6 +8405,12 @@ var StreamChat = /*#__PURE__*/function () {
8393
8405
 
8394
8406
  var token = this._getToken();
8395
8407
 
8408
+ if (!((_options$headers = options.headers) !== null && _options$headers !== void 0 && _options$headers['x-client-request-id'])) {
8409
+ options.headers = {
8410
+ 'x-client-request-id': randomId()
8411
+ };
8412
+ }
8413
+
8396
8414
  return _objectSpread({
8397
8415
  params: _objectSpread(_objectSpread({
8398
8416
  user_id: this.userID