@sentry/replay 0.3.0 → 0.4.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/index.js CHANGED
@@ -436,6 +436,16 @@ var Session = /** @class */ (function () {
436
436
  enumerable: false,
437
437
  configurable: true
438
438
  });
439
+ Object.defineProperty(Session.prototype, "previousSessionId", {
440
+ get: function () {
441
+ return this._previousSessionId;
442
+ },
443
+ set: function (id) {
444
+ this._previousSessionId = id;
445
+ },
446
+ enumerable: false,
447
+ configurable: true
448
+ });
439
449
  Session.prototype.toJSON = function () {
440
450
  return {
441
451
  id: this.id,
@@ -555,7 +565,7 @@ function handleScope(scope) {
555
565
  var newBreadcrumb = scope._breadcrumbs[scope._breadcrumbs.length - 1];
556
566
  // Listener can be called when breadcrumbs have not changed, so we store the
557
567
  // reference to the last crumb and only return a crumb if it has changed
558
- if (_LAST_BREADCRUMB === newBreadcrumb) {
568
+ if (_LAST_BREADCRUMB === newBreadcrumb || !newBreadcrumb) {
559
569
  return null;
560
570
  }
561
571
  _LAST_BREADCRUMB = newBreadcrumb;
@@ -600,9 +610,9 @@ var SentryReplay = /** @class */ (function () {
600
610
  function SentryReplay(_a) {
601
611
  if (_a === void 0) { _a = {}; }
602
612
  var _this = this;
603
- var _b = _a.uploadMinDelay, uploadMinDelay = _b === void 0 ? 5000 : _b, _c = _a.uploadMaxDelay, uploadMaxDelay = _c === void 0 ? 15000 : _c, _d = _a.stickySession, stickySession = _d === void 0 ? false : _d, // TBD: Making this opt-in for now
604
- _e = _a.useCompression, // TBD: Making this opt-in for now
605
- useCompression = _e === void 0 ? true : _e, _f = _a.rrwebConfig, _g = _f === void 0 ? {} : _f, _h = _g.maskAllInputs, maskAllInputs = _h === void 0 ? true : _h, _j = _g.blockClass, blockClass = _j === void 0 ? 'sr-block' : _j, _k = _g.ignoreClass, ignoreClass = _k === void 0 ? 'sr-ignore' : _k, _l = _g.maskTextClass, maskTextClass = _l === void 0 ? 'sr-mask' : _l, rrwebRecordOptions = __rest(_g, ["maskAllInputs", "blockClass", "ignoreClass", "maskTextClass"]);
613
+ var _b = _a.flushMinDelay, flushMinDelay = _b === void 0 ? 5000 : _b, _c = _a.flushMaxDelay, flushMaxDelay = _c === void 0 ? 15000 : _c, _d = _a.initialFlushDelay, initialFlushDelay = _d === void 0 ? 5000 : _d, _e = _a.stickySession, stickySession = _e === void 0 ? false : _e, // TBD: Making this opt-in for now
614
+ _f = _a.useCompression, // TBD: Making this opt-in for now
615
+ useCompression = _f === void 0 ? true : _f, _g = _a.rrwebConfig, _h = _g === void 0 ? {} : _g, _j = _h.maskAllInputs, maskAllInputs = _j === void 0 ? true : _j, _k = _h.blockClass, blockClass = _k === void 0 ? 'sr-block' : _k, _l = _h.ignoreClass, ignoreClass = _l === void 0 ? 'sr-ignore' : _l, _m = _h.maskTextClass, maskTextClass = _m === void 0 ? 'sr-mask' : _m, rrwebRecordOptions = __rest(_h, ["maskAllInputs", "blockClass", "ignoreClass", "maskTextClass"]);
606
616
  /**
607
617
  * @inheritDoc
608
618
  */
@@ -620,8 +630,9 @@ var SentryReplay = /** @class */ (function () {
620
630
  */
621
631
  this.performanceEvents = [];
622
632
  /**
623
- * The timestamp of the first event since the last flush.
624
- * This is used to determine if the maximum allowed time has passed before we should flush events again.
633
+ * The timestamp of the first event since the last flush. This is used to
634
+ * determine if the maximum allowed time has passed before events should be
635
+ * flushed again.
625
636
  */
626
637
  this.initialEventTimestampSinceFlush = null;
627
638
  this.performanceObserver = null;
@@ -711,8 +722,14 @@ var SentryReplay = /** @class */ (function () {
711
722
  ) {
712
723
  _this.performanceEvents = __spreadArray(__spreadArray([], _this.performanceEvents, true), list.getEntries(), true);
713
724
  };
725
+ console.log('main2');
714
726
  this.rrwebRecordOptions = __assign({ maskAllInputs: maskAllInputs, blockClass: blockClass, ignoreClass: ignoreClass, maskTextClass: maskTextClass }, rrwebRecordOptions);
715
- this.options = { uploadMinDelay: uploadMinDelay, uploadMaxDelay: uploadMaxDelay, stickySession: stickySession };
727
+ this.options = {
728
+ flushMinDelay: flushMinDelay,
729
+ flushMaxDelay: flushMaxDelay,
730
+ stickySession: stickySession,
731
+ initialFlushDelay: initialFlushDelay,
732
+ };
716
733
  this.eventBuffer = createEventBuffer({ useCompression: useCompression });
717
734
  }
718
735
  SentryReplay.attachmentUrlFromDsn = function (dsn, eventId) {
@@ -761,25 +778,36 @@ var SentryReplay = /** @class */ (function () {
761
778
  // We need to clear existing events on a checkout, otherwise they are
762
779
  // incremental event updates and should be appended
763
780
  _this.eventBuffer.addEvent(event, isCheckout);
764
- // This event type is a fullsnapshot, we should save immediately when this occurs
781
+ // This event type is a fullsnapshot
765
782
  // See https://github.com/rrweb-io/rrweb/blob/d8f9290ca496712aa1e7d472549480c4e7876594/packages/rrweb/src/types.ts#L16
766
783
  if (event.type === 2) {
767
- // A fullsnapshot happens on initial load and if we need to start a
768
- // new replay due to idle timeout. In the latter case, a new session *should* have been started
769
- // before triggering a new checkout
784
+ // If the full snapshot is due to an initial load, we will not have
785
+ // a previous session ID. In this case, we want to buffer events
786
+ // for a set amount of time before flushing. This can help avoid
787
+ // capturing replays of users that immediately close the window.
788
+ if (!_this.session.previousSessionId) {
789
+ var now_1 = new Date().getTime();
790
+ setTimeout(function () { return _this.flushUpdate(now_1); }, _this.options.initialFlushDelay);
791
+ return true;
792
+ }
793
+ // The other case where a full snapshot occurs is when a new replay
794
+ // needs to be started due to session expiration. The new session
795
+ // is started before triggering a new checkout and contains the id
796
+ // of the previous session. Do not immediately flush in this case
797
+ // to avoid capturing only the checkout and instead the replay will
798
+ // be captured if they perform any follow-up actions.
770
799
  return true;
771
800
  }
772
- return false;
773
801
  });
774
802
  } }));
775
803
  };
776
804
  /**
777
805
  * We want to batch uploads of replay events. Save events only if
778
- * `<uploadMinDelay>` milliseconds have elapsed since the last event
779
- * *OR* if `<uploadMaxDelay>` milliseconds have elapsed.
806
+ * `<flushMinDelay>` milliseconds have elapsed since the last event
807
+ * *OR* if `<flushMaxDelay>` milliseconds have elapsed.
780
808
  *
781
- * Accepts a callback to perform side-effects and returns a boolean value if we
782
- * should flush events immediately
809
+ * Accepts a callback to perform side-effects and returns true to stop batch
810
+ * processing and hand back control to caller.
783
811
  */
784
812
  SentryReplay.prototype.addUpdate = function (cb) {
785
813
  var _this = this;
@@ -793,25 +821,26 @@ var SentryReplay = /** @class */ (function () {
793
821
  if (this.timeout) {
794
822
  window.clearTimeout(this.timeout);
795
823
  }
824
+ // If callback is true, we do not want to continue with flushing -- the
825
+ // caller will need to handle it.
796
826
  if ((cb === null || cb === void 0 ? void 0 : cb()) === true) {
797
- this.flushUpdate();
798
827
  return;
799
828
  }
800
- var uploadMaxDelayExceeded = isExpired(this.initialEventTimestampSinceFlush, this.options.uploadMaxDelay, now);
801
- // If `uploadMaxDelayExceeded` is true, then we should finish the replay event immediately,
802
- // Otherwise schedule it to be finished in `this.options.uploadMinDelay`
803
- if (uploadMaxDelayExceeded) {
829
+ var flushMaxDelayExceeded = isExpired(this.initialEventTimestampSinceFlush, this.options.flushMaxDelay, now);
830
+ // If `flushMaxDelayExceeded` is true, then we should finish the replay event immediately,
831
+ // Otherwise schedule it to be finished in `this.options.flushMinDelay`
832
+ if (flushMaxDelayExceeded) {
804
833
  logger.log('replay max delay exceeded, finishing replay event');
805
834
  this.flushUpdate();
806
835
  return;
807
836
  }
808
837
  // Set timer to finish replay event and send replay attachment to
809
- // Sentry. Will be cancelled if an event happens before `uploadMinDelay`
838
+ // Sentry. Will be cancelled if an event happens before `flushMinDelay`
810
839
  // elapses.
811
840
  this.timeout = window.setTimeout(function () {
812
841
  logger.log('replay timeout exceeded, finishing replay event');
813
842
  _this.flushUpdate(now);
814
- }, this.options.uploadMinDelay);
843
+ }, this.options.flushMinDelay);
815
844
  };
816
845
  /**
817
846
  * Currently, this needs to be manually called (e.g. for tests). Sentry SDK does not support a teardown
@@ -828,17 +857,21 @@ var SentryReplay = /** @class */ (function () {
828
857
  * is expired.
829
858
  */
830
859
  SentryReplay.prototype.loadSession = function (_a) {
860
+ var _b, _c;
831
861
  var expiry = _a.expiry;
832
- var _b = getSession({
862
+ var _d = getSession({
833
863
  expiry: expiry,
834
864
  stickySession: this.options.stickySession,
835
865
  currentSession: this.session,
836
- }), type = _b.type, session = _b.session;
866
+ }), type = _d.type, session = _d.session;
837
867
  // If session was newly created (i.e. was not loaded from storage), then
838
868
  // enable flag to create the root replay
839
869
  if (type === 'new') {
840
870
  this.needsCaptureReplay = true;
841
871
  }
872
+ if (session.id !== ((_b = this.session) === null || _b === void 0 ? void 0 : _b.id)) {
873
+ session.previousSessionId = (_c = this.session) === null || _c === void 0 ? void 0 : _c.id;
874
+ }
842
875
  this.session = session;
843
876
  };
844
877
  SentryReplay.prototype.addListeners = function () {
@@ -1015,12 +1048,13 @@ var SentryReplay = /** @class */ (function () {
1015
1048
  };
1016
1049
  SentryReplay.prototype.flushUpdate = function (lastActivity) {
1017
1050
  return __awaiter(this, void 0, void 0, function () {
1018
- var recordingData;
1051
+ var recordingData, err_1;
1019
1052
  return __generator(this, function (_a) {
1020
1053
  switch (_a.label) {
1021
1054
  case 0:
1022
1055
  if (!this.checkAndHandleExpiredSession()) {
1023
1056
  logger.error(new Error('Attempting to finish replay event after session expired.'));
1057
+ return [2 /*return*/];
1024
1058
  }
1025
1059
  if (!this.session.id) {
1026
1060
  console.error(new Error('[Sentry]: No transaction, no replay'));
@@ -1030,18 +1064,27 @@ var SentryReplay = /** @class */ (function () {
1030
1064
  if (!this.eventBuffer.length) {
1031
1065
  return [2 /*return*/];
1032
1066
  }
1033
- return [4 /*yield*/, this.eventBuffer.finish()];
1034
- case 1:
1035
- recordingData = _a.sent();
1036
- this.sendReplay(this.session.id, recordingData);
1037
- this.initialEventTimestampSinceFlush = null;
1038
- // TBD: Alternatively we could update this after every rrweb event
1039
- this.updateLastActivity(lastActivity);
1040
1067
  // Only want to create replay event if session is new
1041
1068
  if (this.needsCaptureReplay) {
1069
+ // This event needs to exist before calling `sendReplay`
1042
1070
  captureReplay(this.session);
1043
1071
  this.needsCaptureReplay = false;
1044
1072
  }
1073
+ // Reset this to null regardless of `sendReplay` result so that future
1074
+ // events will get flushed properly
1075
+ this.initialEventTimestampSinceFlush = null;
1076
+ _a.label = 1;
1077
+ case 1:
1078
+ _a.trys.push([1, 4, , 5]);
1079
+ return [4 /*yield*/, this.eventBuffer.finish()];
1080
+ case 2:
1081
+ recordingData = _a.sent();
1082
+ return [4 /*yield*/, this.sendReplay(this.session.id, recordingData)];
1083
+ case 3:
1084
+ _a.sent();
1085
+ // The below will only happen after successfully sending replay //
1086
+ // TBD: Alternatively we could update this after every rrweb event
1087
+ this.updateLastActivity(lastActivity);
1045
1088
  core.captureEvent({
1046
1089
  message: "".concat(REPLAY_EVENT_NAME, "-").concat(utils.uuid4().substring(16)),
1047
1090
  tags: {
@@ -1049,7 +1092,12 @@ var SentryReplay = /** @class */ (function () {
1049
1092
  sequenceId: this.session.sequenceId++,
1050
1093
  },
1051
1094
  });
1052
- return [2 /*return*/];
1095
+ return [3 /*break*/, 5];
1096
+ case 4:
1097
+ err_1 = _a.sent();
1098
+ console.error(err_1);
1099
+ return [3 /*break*/, 5];
1100
+ case 5: return [2 /*return*/];
1053
1101
  }
1054
1102
  });
1055
1103
  });
@@ -1106,8 +1154,8 @@ var SentryReplay = /** @class */ (function () {
1106
1154
  return __awaiter(this, void 0, void 0, function () {
1107
1155
  var client, endpoint, ex_1;
1108
1156
  var _this = this;
1109
- return __generator(this, function (_a) {
1110
- switch (_a.label) {
1157
+ return __generator(this, function (_b) {
1158
+ switch (_b.label) {
1111
1159
  case 0:
1112
1160
  // short circuit if there's no events to upload
1113
1161
  if (!events.length) {
@@ -1115,34 +1163,60 @@ var SentryReplay = /** @class */ (function () {
1115
1163
  }
1116
1164
  client = core.getCurrentHub().getClient();
1117
1165
  endpoint = SentryReplay.attachmentUrlFromDsn(client.getDsn(), eventId);
1118
- _a.label = 1;
1166
+ _b.label = 1;
1119
1167
  case 1:
1120
- _a.trys.push([1, 3, , 4]);
1168
+ _b.trys.push([1, 3, , 8]);
1121
1169
  return [4 /*yield*/, this.sendReplayRequest({
1122
1170
  endpoint: endpoint,
1123
1171
  events: events,
1124
1172
  })];
1125
1173
  case 2:
1126
- _a.sent();
1174
+ _b.sent();
1127
1175
  this.resetRetries();
1128
1176
  return [2 /*return*/, true];
1129
1177
  case 3:
1130
- ex_1 = _a.sent();
1178
+ ex_1 = _b.sent();
1131
1179
  // we have to catch this otherwise it throws an infinite loop in Sentry
1132
1180
  console.error(ex_1);
1133
1181
  // If an error happened here, it's likely that uploading the attachment
1134
1182
  // failed, we'll can retry with the same events payload
1135
1183
  if (this.retryCount >= MAX_RETRY_COUNT) {
1136
1184
  this.resetRetries();
1185
+ return [2 /*return*/, false];
1137
1186
  }
1138
- else {
1139
- this.retryCount = this.retryCount + 1;
1140
- // will retry in intervals of 5, 10, 15, 20, 25 seconds
1141
- this.retryInterval = this.retryCount * this.retryInterval;
1142
- setTimeout(function () { return _this.sendReplay(eventId, events); }, this.retryInterval);
1143
- }
1187
+ this.retryCount = this.retryCount + 1;
1188
+ // will retry in intervals of 5, 10, 15, 20, 25 seconds
1189
+ this.retryInterval = this.retryCount * this.retryInterval;
1190
+ _b.label = 4;
1191
+ case 4:
1192
+ _b.trys.push([4, 6, , 7]);
1193
+ return [4 /*yield*/, new Promise(function (resolve, reject) {
1194
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
1195
+ var result;
1196
+ return __generator(this, function (_a) {
1197
+ switch (_a.label) {
1198
+ case 0: return [4 /*yield*/, this.sendReplay(eventId, events)];
1199
+ case 1:
1200
+ result = _a.sent();
1201
+ if (result) {
1202
+ resolve(true);
1203
+ }
1204
+ else {
1205
+ reject(new Error('Could not send replay'));
1206
+ }
1207
+ return [2 /*return*/];
1208
+ }
1209
+ });
1210
+ }); }, _this.retryInterval);
1211
+ })];
1212
+ case 5:
1213
+ _b.sent();
1214
+ return [2 /*return*/, true];
1215
+ case 6:
1216
+ _b.sent();
1144
1217
  return [2 /*return*/, false];
1145
- case 4: return [2 /*return*/];
1218
+ case 7: return [3 /*break*/, 8];
1219
+ case 8: return [2 /*return*/];
1146
1220
  }
1147
1221
  });
1148
1222
  });