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.
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); }
@@ -3444,7 +3454,6 @@ var ClientState = /*#__PURE__*/function () {
3444
3454
  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; }
3445
3455
 
3446
3456
  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__default['default'](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; }
3447
-
3448
3457
  var InsightMetrics = function InsightMetrics() {
3449
3458
  _classCallCheck__default['default'](this, InsightMetrics);
3450
3459
 
@@ -3454,18 +3463,15 @@ var InsightMetrics = function InsightMetrics() {
3454
3463
 
3455
3464
  _defineProperty__default['default'](this, "wsTotalFailures", void 0);
3456
3465
 
3466
+ _defineProperty__default['default'](this, "instanceClientId", void 0);
3467
+
3457
3468
  this.connectionStartTimestamp = null;
3458
3469
  this.wsTotalFailures = 0;
3459
3470
  this.wsConsecutiveFailures = 0;
3471
+ this.instanceClientId = randomId();
3460
3472
  };
3461
3473
  function buildWsFatalInsight(connection, event) {
3462
- return _objectSpread$2({
3463
- err: {
3464
- wasClean: event.wasClean,
3465
- code: event.code,
3466
- reason: event.reason
3467
- }
3468
- }, buildWsBaseInsight(connection));
3474
+ return _objectSpread$2(_objectSpread$2({}, event), buildWsBaseInsight(connection));
3469
3475
  }
3470
3476
 
3471
3477
  function buildWsBaseInsight(connection) {
@@ -3488,7 +3494,8 @@ function buildWsBaseInsight(connection) {
3488
3494
  ws_total_failures: connection.insightMetrics.wsTotalFailures,
3489
3495
  request_id: connection.requestID,
3490
3496
  online: typeof navigator !== 'undefined' ? (_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.onLine : null,
3491
- user_agent: typeof navigator !== 'undefined' ? (_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : _navigator2.userAgent : null
3497
+ user_agent: typeof navigator !== 'undefined' ? (_navigator2 = navigator) === null || _navigator2 === void 0 ? void 0 : _navigator2.userAgent : null,
3498
+ instance_client_id: connection.insightMetrics.instanceClientId
3492
3499
  };
3493
3500
  }
3494
3501
 
@@ -3617,7 +3624,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3617
3624
  user_token: _this.tokenManager.getToken(),
3618
3625
  server_determines_connection_id: true,
3619
3626
  device: _this.device,
3620
- request_id: reqID
3627
+ client_request_id: reqID
3621
3628
  };
3622
3629
  var qs = encodeURIComponent(JSON.stringify(params));
3623
3630
 
@@ -3702,13 +3709,7 @@ var StableWSConnection = /*#__PURE__*/function () {
3702
3709
  });
3703
3710
 
3704
3711
  _defineProperty__default['default'](this, "onclose", function (wsID, event) {
3705
- if (event.code !== chatCodes.WS_CLOSED_SUCCESS) {
3706
- var _this$postInsights;
3707
-
3708
- _this.insightMetrics.wsConsecutiveFailures++;
3709
- _this.insightMetrics.wsTotalFailures++;
3710
- (_this$postInsights = _this.postInsights) === null || _this$postInsights === void 0 ? void 0 : _this$postInsights.call(_this, 'ws_fatal', buildWsFatalInsight(_this, event));
3711
- }
3712
+ if (_this.wsID !== wsID) return;
3712
3713
 
3713
3714
  _this.logger('info', 'connection:onclose() - onclose callback - ' + event.code, {
3714
3715
  tags: ['connection'],
@@ -3716,8 +3717,6 @@ var StableWSConnection = /*#__PURE__*/function () {
3716
3717
  wsID: wsID
3717
3718
  });
3718
3719
 
3719
- if (_this.wsID !== wsID) return;
3720
-
3721
3720
  if (event.code === chatCodes.WS_CLOSED_SUCCESS) {
3722
3721
  var _this$rejectPromise2;
3723
3722
 
@@ -3725,6 +3724,9 @@ var StableWSConnection = /*#__PURE__*/function () {
3725
3724
  // usually caused by invalid auth details
3726
3725
  var error = new Error("WS connection reject with error ".concat(event.reason));
3727
3726
  error.reason = event.reason;
3727
+ error.code = event.code;
3728
+ error.wasClean = event.wasClean;
3729
+ error.target = event.target;
3728
3730
  (_this$rejectPromise2 = _this.rejectPromise) === null || _this$rejectPromise2 === void 0 ? void 0 : _this$rejectPromise2.call(_this, error);
3729
3731
 
3730
3732
  _this.logger('info', "connection:onclose() - WS connection reject with error ".concat(event.reason), {
@@ -4255,7 +4257,7 @@ var StableWSConnection = /*#__PURE__*/function () {
4255
4257
  key: "_connect",
4256
4258
  value: function () {
4257
4259
  var _connect3 = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee5() {
4258
- var wsURL, response, _this$postInsights2;
4260
+ var wsURL, response, _this$postInsights, insights;
4259
4261
 
4260
4262
  return _regeneratorRuntime__default['default'].wrap(function _callee5$(_context5) {
4261
4263
  while (1) {
@@ -4300,24 +4302,33 @@ var StableWSConnection = /*#__PURE__*/function () {
4300
4302
 
4301
4303
  this.connectionID = response.connection_id;
4302
4304
 
4303
- if (this.insightMetrics.wsConsecutiveFailures > 0) {
4304
- (_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));
4305
4307
  this.insightMetrics.wsConsecutiveFailures = 0;
4306
4308
  }
4307
4309
 
4308
4310
  return _context5.abrupt("return", response);
4309
4311
 
4310
4312
  case 23:
4311
- _context5.next = 29;
4313
+ _context5.next = 30;
4312
4314
  break;
4313
4315
 
4314
4316
  case 25:
4315
4317
  _context5.prev = 25;
4316
4318
  _context5.t0 = _context5["catch"](5);
4317
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
+
4318
4329
  throw _context5.t0;
4319
4330
 
4320
- case 29:
4331
+ case 30:
4321
4332
  case "end":
4322
4333
  return _context5.stop();
4323
4334
  }
@@ -5118,7 +5129,9 @@ var StreamChat = /*#__PURE__*/function () {
5118
5129
  var options,
5119
5130
  requestConfig,
5120
5131
  response,
5132
+ _requestConfig$header,
5121
5133
  _args4 = arguments;
5134
+
5122
5135
  return _regeneratorRuntime__default['default'].wrap(function _callee4$(_context4) {
5123
5136
  while (1) {
5124
5137
  switch (_context4.prev = _context4.next) {
@@ -5197,45 +5210,46 @@ var StreamChat = /*#__PURE__*/function () {
5197
5210
  case 39:
5198
5211
  _context4.prev = 39;
5199
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'];
5200
5214
 
5201
5215
  _this._logApiError(type, url, _context4.t1);
5202
5216
 
5203
5217
  _this.consecutiveFailures += 1;
5204
5218
 
5205
5219
  if (!_context4.t1.response) {
5206
- _context4.next = 55;
5220
+ _context4.next = 56;
5207
5221
  break;
5208
5222
  }
5209
5223
 
5210
5224
  if (!(_context4.t1.response.data.code === chatCodes.TOKEN_EXPIRED && !_this.tokenManager.isStatic())) {
5211
- _context4.next = 52;
5225
+ _context4.next = 53;
5212
5226
  break;
5213
5227
  }
5214
5228
 
5215
5229
  if (!(_this.consecutiveFailures > 1)) {
5216
- _context4.next = 48;
5230
+ _context4.next = 49;
5217
5231
  break;
5218
5232
  }
5219
5233
 
5220
- _context4.next = 48;
5234
+ _context4.next = 49;
5221
5235
  return sleep(retryInterval(_this.consecutiveFailures));
5222
5236
 
5223
- case 48:
5237
+ case 49:
5224
5238
  _this.tokenManager.loadToken();
5225
5239
 
5226
- _context4.next = 51;
5240
+ _context4.next = 52;
5227
5241
  return _this.doAxiosRequest(type, url, data, options);
5228
5242
 
5229
- case 51:
5243
+ case 52:
5230
5244
  return _context4.abrupt("return", _context4.sent);
5231
5245
 
5232
- case 52:
5246
+ case 53:
5233
5247
  return _context4.abrupt("return", _this.handleResponse(_context4.t1.response));
5234
5248
 
5235
- case 55:
5249
+ case 56:
5236
5250
  throw _context4.t1;
5237
5251
 
5238
- case 56:
5252
+ case 57:
5239
5253
  case "end":
5240
5254
  return _context4.stop();
5241
5255
  }
@@ -5554,7 +5568,7 @@ var StreamChat = /*#__PURE__*/function () {
5554
5568
 
5555
5569
  _context6.prev = 3;
5556
5570
  _context6.next = 6;
5557
- 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);
5558
5572
 
5559
5573
  case 6:
5560
5574
  _context6.next = 14;
@@ -5629,7 +5643,7 @@ var StreamChat = /*#__PURE__*/function () {
5629
5643
  }
5630
5644
 
5631
5645
  this.axiosInstance = axios__default['default'].create(this.options);
5632
- 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');
5633
5647
 
5634
5648
  if (typeof process !== 'undefined' && process.env.STREAM_LOCAL_TEST_RUN) {
5635
5649
  this.setBaseURL('http://localhost:3030');
@@ -6463,7 +6477,7 @@ var StreamChat = /*#__PURE__*/function () {
6463
6477
  */
6464
6478
  function () {
6465
6479
  var _connect = _asyncToGenerator__default['default']( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee15() {
6466
- var client, warmUpPromise, handshake;
6480
+ var client;
6467
6481
  return _regeneratorRuntime__default['default'].wrap(function _callee15$(_context15) {
6468
6482
  while (1) {
6469
6483
  switch (_context15.prev = _context15.next) {
@@ -6495,7 +6509,11 @@ var StreamChat = /*#__PURE__*/function () {
6495
6509
  throw Error('clientID is not set');
6496
6510
 
6497
6511
  case 8:
6498
- // The StableWSConnection handles all the reconnection logic.
6512
+ if (!this.wsConnection && (this.options.warmUp || this.options.enableInsights)) {
6513
+ this.sendBeacon();
6514
+ } // The StableWSConnection handles all the reconnection logic.
6515
+
6516
+
6499
6517
  this.wsConnection = new StableWSConnection({
6500
6518
  wsBaseURL: client.wsBaseURL,
6501
6519
  clientID: client.clientID,
@@ -6513,40 +6531,18 @@ var StreamChat = /*#__PURE__*/function () {
6513
6531
  postInsights: this.options.enableInsights ? this.postInsights : undefined,
6514
6532
  insightMetrics: this.insightMetrics
6515
6533
  });
6516
-
6517
- if (this.options.warmUp) {
6518
- warmUpPromise = this.doAxiosRequest('options', this.baseURL + '/connect');
6519
- }
6520
-
6521
6534
  _context15.next = 12;
6522
6535
  return this.wsConnection.connect();
6523
6536
 
6524
6537
  case 12:
6525
- handshake = _context15.sent;
6526
- _context15.prev = 13;
6527
- _context15.next = 16;
6528
- return warmUpPromise;
6529
-
6530
- case 16:
6531
- _context15.next = 21;
6532
- break;
6533
-
6534
- case 18:
6535
- _context15.prev = 18;
6536
- _context15.t0 = _context15["catch"](13);
6537
- this.logger('error', 'Warmup request failed', {
6538
- error: _context15.t0
6539
- });
6540
-
6541
- case 21:
6542
- return _context15.abrupt("return", handshake);
6538
+ return _context15.abrupt("return", _context15.sent);
6543
6539
 
6544
- case 22:
6540
+ case 13:
6545
6541
  case "end":
6546
6542
  return _context15.stop();
6547
6543
  }
6548
6544
  }
6549
- }, _callee15, this, [[13, 18]]);
6545
+ }, _callee15, this);
6550
6546
  }));
6551
6547
 
6552
6548
  function connect() {
@@ -6555,6 +6551,25 @@ var StreamChat = /*#__PURE__*/function () {
6555
6551
 
6556
6552
  return connect;
6557
6553
  }()
6554
+ }, {
6555
+ key: "sendBeacon",
6556
+ value: function sendBeacon() {
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 + '/beacon', null, opts).catch(function (e) {
6566
+ _this4.postInsights('http_beacon_failed', {
6567
+ api_key: _this4.key,
6568
+ err: e,
6569
+ client_request_id: client_request_id
6570
+ });
6571
+ });
6572
+ }
6558
6573
  /**
6559
6574
  * queryUsers - Query users and watch user presence
6560
6575
  *
@@ -8366,7 +8381,7 @@ var StreamChat = /*#__PURE__*/function () {
8366
8381
  }, {
8367
8382
  key: "getUserAgent",
8368
8383
  value: function getUserAgent() {
8369
- return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.1");
8384
+ return this.userAgent || "stream-chat-javascript-client-".concat(this.node ? 'node' : 'browser', "-", "4.4.3-dev.2");
8370
8385
  }
8371
8386
  }, {
8372
8387
  key: "setUserAgent",
@@ -8380,7 +8395,7 @@ var StreamChat = /*#__PURE__*/function () {
8380
8395
  }, {
8381
8396
  key: "_enrichAxiosOptions",
8382
8397
  value: function _enrichAxiosOptions() {
8383
- var _this$wsConnection4;
8398
+ var _options$headers, _this$wsConnection4;
8384
8399
 
8385
8400
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
8386
8401
  params: {},
@@ -8390,6 +8405,12 @@ var StreamChat = /*#__PURE__*/function () {
8390
8405
 
8391
8406
  var token = this._getToken();
8392
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
+
8393
8414
  return _objectSpread({
8394
8415
  params: _objectSpread(_objectSpread({
8395
8416
  user_id: this.userID