@sentry/replay 0.4.0 → 0.4.1

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