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.
@@ -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); }
@@ -3424,7 +3434,6 @@ var ClientState = /*#__PURE__*/function () {
3424
3434
  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; }
3425
3435
 
3426
3436
  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(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; }
3427
-
3428
3437
  var InsightMetrics = function InsightMetrics() {
3429
3438
  _classCallCheck(this, InsightMetrics);
3430
3439
 
@@ -3434,18 +3443,15 @@ var InsightMetrics = function InsightMetrics() {
3434
3443
 
3435
3444
  _defineProperty(this, "wsTotalFailures", void 0);
3436
3445
 
3446
+ _defineProperty(this, "instanceClientId", void 0);
3447
+
3437
3448
  this.connectionStartTimestamp = null;
3438
3449
  this.wsTotalFailures = 0;
3439
3450
  this.wsConsecutiveFailures = 0;
3451
+ this.instanceClientId = randomId();
3440
3452
  };
3441
3453
  function buildWsFatalInsight(connection, event) {
3442
- return _objectSpread$2({
3443
- err: {
3444
- wasClean: event.wasClean,
3445
- code: event.code,
3446
- reason: event.reason
3447
- }
3448
- }, buildWsBaseInsight(connection));
3454
+ return _objectSpread$2(_objectSpread$2({}, event), buildWsBaseInsight(connection));
3449
3455
  }
3450
3456
 
3451
3457
  function buildWsBaseInsight(connection) {
@@ -3468,7 +3474,8 @@ function buildWsBaseInsight(connection) {
3468
3474
  ws_total_failures: connection.insightMetrics.wsTotalFailures,
3469
3475
  request_id: connection.requestID,
3470
3476
  online: typeof navigator !== 'undefined' ? (_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.onLine : null,
3471
- user_agent: typeof navigator !== 'undefined' ? (_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : _navigator2.userAgent : null
3477
+ user_agent: typeof navigator !== 'undefined' ? (_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : _navigator2.userAgent : null,
3478
+ instance_client_id: connection.insightMetrics.instanceClientId
3472
3479
  };
3473
3480
  }
3474
3481
 
@@ -3597,7 +3604,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3597
3604
  user_token: _this.tokenManager.getToken(),
3598
3605
  server_determines_connection_id: true,
3599
3606
  device: _this.device,
3600
- request_id: reqID
3607
+ client_request_id: reqID
3601
3608
  };
3602
3609
  var qs = encodeURIComponent(JSON.stringify(params));
3603
3610
 
@@ -3682,13 +3689,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3682
3689
  });
3683
3690
 
3684
3691
  _defineProperty(this, "onclose", function (wsID, event) {
3685
- if (event.code !== chatCodes.WS_CLOSED_SUCCESS) {
3686
- var _this$postInsights;
3687
-
3688
- _this.insightMetrics.wsConsecutiveFailures++;
3689
- _this.insightMetrics.wsTotalFailures++;
3690
- (_this$postInsights = _this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(_this, 'ws_fatal', buildWsFatalInsight(_this, event));
3691
- }
3692
+ if (_this.wsID !== wsID) return;
3692
3693
 
3693
3694
  _this.logger('info', 'connection:onclose() - onclose callback - ' + event.code, {
3694
3695
  tags: ['connection'],
@@ -3696,8 +3697,6 @@ var StableWSConnection = /*#__PURE__*/function () {
3696
3697
  wsID: wsID
3697
3698
  });
3698
3699
 
3699
- if (_this.wsID !== wsID) return;
3700
-
3701
3700
  if (event.code === chatCodes.WS_CLOSED_SUCCESS) {
3702
3701
  var _this$rejectPromise2;
3703
3702
 
@@ -3705,6 +3704,9 @@ var StableWSConnection = /*#__PURE__*/function () {
3705
3704
  // usually caused by invalid auth details
3706
3705
  var error = new Error("WS connection reject with error ".concat(event.reason));
3707
3706
  error.reason = event.reason;
3707
+ error.code = event.code;
3708
+ error.wasClean = event.wasClean;
3709
+ error.target = event.target;
3708
3710
  (_this$rejectPromise2 = _this.rejectPromise) === null || _this$rejectPromise2 === void 0 ? void 0 : _this$rejectPromise2.call(_this, error);
3709
3711
 
3710
3712
  _this.logger('info', "connection:onclose() - WS connection reject with error ".concat(event.reason), {
@@ -4235,7 +4237,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4235
4237
  key: "_connect",
4236
4238
  value: function () {
4237
4239
  var _connect3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
4238
- var wsURL, response, _this$postInsights2;
4240
+ var wsURL, response, _this$postInsights, insights;
4239
4241
 
4240
4242
  return _regeneratorRuntime.wrap(function _callee5$(_context5) {
4241
4243
  while (1) {
@@ -4280,24 +4282,33 @@ var StableWSConnection = /*#__PURE__*/function () {
4280
4282
 
4281
4283
  this.connectionID = response.connection_id;
4282
4284
 
4283
- if (this.insightMetrics.wsConsecutiveFailures > 0) {
4284
- (_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));
4285
4287
  this.insightMetrics.wsConsecutiveFailures = 0;
4286
4288
  }
4287
4289
 
4288
4290
  return _context5.abrupt("return", response);
4289
4291
 
4290
4292
  case 23:
4291
- _context5.next = 29;
4293
+ _context5.next = 30;
4292
4294
  break;
4293
4295
 
4294
4296
  case 25:
4295
4297
  _context5.prev = 25;
4296
4298
  _context5.t0 = _context5["catch"](5);
4297
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
+
4298
4309
  throw _context5.t0;
4299
4310
 
4300
- case 29:
4311
+ case 30:
4301
4312
  case "end":
4302
4313
  return _context5.stop();
4303
4314
  }
@@ -5098,7 +5109,9 @@ var StreamChat = /*#__PURE__*/function () {
5098
5109
  var options,
5099
5110
  requestConfig,
5100
5111
  response,
5112
+ _requestConfig$header,
5101
5113
  _args4 = arguments;
5114
+
5102
5115
  return _regeneratorRuntime.wrap(function _callee4$(_context4) {
5103
5116
  while (1) {
5104
5117
  switch (_context4.prev = _context4.next) {
@@ -5177,45 +5190,46 @@ var StreamChat = /*#__PURE__*/function () {
5177
5190
  case 39:
5178
5191
  _context4.prev = 39;
5179
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'];
5180
5194
 
5181
5195
  _this._logApiError(type, url, _context4.t1);
5182
5196
 
5183
5197
  _this.consecutiveFailures += 1;
5184
5198
 
5185
5199
  if (!_context4.t1.response) {
5186
- _context4.next = 55;
5200
+ _context4.next = 56;
5187
5201
  break;
5188
5202
  }
5189
5203
 
5190
5204
  if (!(_context4.t1.response.data.code === chatCodes.TOKEN_EXPIRED && !_this.tokenManager.isStatic())) {
5191
- _context4.next = 52;
5205
+ _context4.next = 53;
5192
5206
  break;
5193
5207
  }
5194
5208
 
5195
5209
  if (!(_this.consecutiveFailures > 1)) {
5196
- _context4.next = 48;
5210
+ _context4.next = 49;
5197
5211
  break;
5198
5212
  }
5199
5213
 
5200
- _context4.next = 48;
5214
+ _context4.next = 49;
5201
5215
  return sleep(retryInterval(_this.consecutiveFailures));
5202
5216
 
5203
- case 48:
5217
+ case 49:
5204
5218
  _this.tokenManager.loadToken();
5205
5219
 
5206
- _context4.next = 51;
5220
+ _context4.next = 52;
5207
5221
  return _this.doAxiosRequest(type, url, data, options);
5208
5222
 
5209
- case 51:
5223
+ case 52:
5210
5224
  return _context4.abrupt("return", _context4.sent);
5211
5225
 
5212
- case 52:
5226
+ case 53:
5213
5227
  return _context4.abrupt("return", _this.handleResponse(_context4.t1.response));
5214
5228
 
5215
- case 55:
5229
+ case 56:
5216
5230
  throw _context4.t1;
5217
5231
 
5218
- case 56:
5232
+ case 57:
5219
5233
  case "end":
5220
5234
  return _context4.stop();
5221
5235
  }
@@ -5534,7 +5548,7 @@ var StreamChat = /*#__PURE__*/function () {
5534
5548
 
5535
5549
  _context6.prev = 3;
5536
5550
  _context6.next = 6;
5537
- 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);
5538
5552
 
5539
5553
  case 6:
5540
5554
  _context6.next = 14;
@@ -5609,7 +5623,7 @@ var StreamChat = /*#__PURE__*/function () {
5609
5623
  }
5610
5624
 
5611
5625
  this.axiosInstance = axios.create(this.options);
5612
- 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');
5613
5627
 
5614
5628
  if (typeof process !== 'undefined' && process.env.STREAM_LOCAL_TEST_RUN) {
5615
5629
  this.setBaseURL('http://localhost:3030');
@@ -6443,7 +6457,7 @@ var StreamChat = /*#__PURE__*/function () {
6443
6457
  */
6444
6458
  function () {
6445
6459
  var _connect = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee15() {
6446
- var client, warmUpPromise, handshake;
6460
+ var client;
6447
6461
  return _regeneratorRuntime.wrap(function _callee15$(_context15) {
6448
6462
  while (1) {
6449
6463
  switch (_context15.prev = _context15.next) {
@@ -6475,7 +6489,11 @@ var StreamChat = /*#__PURE__*/function () {
6475
6489
  throw Error('clientID is not set');
6476
6490
 
6477
6491
  case 8:
6478
- // The StableWSConnection handles all the reconnection logic.
6492
+ if (!this.wsConnection && (this.options.warmUp || this.options.enableInsights)) {
6493
+ this.sendBeacon();
6494
+ } // The StableWSConnection handles all the reconnection logic.
6495
+
6496
+
6479
6497
  this.wsConnection = new StableWSConnection({
6480
6498
  wsBaseURL: client.wsBaseURL,
6481
6499
  clientID: client.clientID,
@@ -6493,40 +6511,18 @@ var StreamChat = /*#__PURE__*/function () {
6493
6511
  postInsights: this.options.enableInsights ? this.postInsights : undefined,
6494
6512
  insightMetrics: this.insightMetrics
6495
6513
  });
6496
-
6497
- if (this.options.warmUp) {
6498
- warmUpPromise = this.doAxiosRequest('options', this.baseURL + '/connect');
6499
- }
6500
-
6501
6514
  _context15.next = 12;
6502
6515
  return this.wsConnection.connect();
6503
6516
 
6504
6517
  case 12:
6505
- handshake = _context15.sent;
6506
- _context15.prev = 13;
6507
- _context15.next = 16;
6508
- return warmUpPromise;
6509
-
6510
- case 16:
6511
- _context15.next = 21;
6512
- break;
6513
-
6514
- case 18:
6515
- _context15.prev = 18;
6516
- _context15.t0 = _context15["catch"](13);
6517
- this.logger('error', 'Warmup request failed', {
6518
- error: _context15.t0
6519
- });
6520
-
6521
- case 21:
6522
- return _context15.abrupt("return", handshake);
6518
+ return _context15.abrupt("return", _context15.sent);
6523
6519
 
6524
- case 22:
6520
+ case 13:
6525
6521
  case "end":
6526
6522
  return _context15.stop();
6527
6523
  }
6528
6524
  }
6529
- }, _callee15, this, [[13, 18]]);
6525
+ }, _callee15, this);
6530
6526
  }));
6531
6527
 
6532
6528
  function connect() {
@@ -6535,6 +6531,25 @@ var StreamChat = /*#__PURE__*/function () {
6535
6531
 
6536
6532
  return connect;
6537
6533
  }()
6534
+ }, {
6535
+ key: "sendBeacon",
6536
+ value: function sendBeacon() {
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 + '/beacon', null, opts).catch(function (e) {
6546
+ _this4.postInsights('http_beacon_failed', {
6547
+ api_key: _this4.key,
6548
+ err: e,
6549
+ client_request_id: client_request_id
6550
+ });
6551
+ });
6552
+ }
6538
6553
  /**
6539
6554
  * queryUsers - Query users and watch user presence
6540
6555
  *
@@ -8346,7 +8361,7 @@ var StreamChat = /*#__PURE__*/function () {
8346
8361
  }, {
8347
8362
  key: "getUserAgent",
8348
8363
  value: function getUserAgent() {
8349
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.1");
8364
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.3-dev.2");
8350
8365
  }
8351
8366
  }, {
8352
8367
  key: "setUserAgent",
@@ -8360,7 +8375,7 @@ var StreamChat = /*#__PURE__*/function () {
8360
8375
  }, {
8361
8376
  key: "_enrichAxiosOptions",
8362
8377
  value: function _enrichAxiosOptions() {
8363
- var _this$wsConnection4;
8378
+ var _options$headers, _this$wsConnection4;
8364
8379
 
8365
8380
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
8366
8381
  params: {},
@@ -8370,6 +8385,12 @@ var StreamChat = /*#__PURE__*/function () {
8370
8385
 
8371
8386
  var token = this._getToken();
8372
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
+
8373
8394
  return _objectSpread({
8374
8395
  params: _objectSpread(_objectSpread({
8375
8396
  user_id: this.userID