@sentry/replay 0.5.9 → 0.5.12

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
@@ -267,6 +267,22 @@ function getSpanHandler(type) {
267
267
  return handleHistory;
268
268
  }
269
269
 
270
+ /**
271
+ * Deletes a session from storage
272
+ */
273
+ function deleteSession() {
274
+ var hasSessionStorage = 'sessionStorage' in window;
275
+ if (!hasSessionStorage) {
276
+ return;
277
+ }
278
+ try {
279
+ window.sessionStorage.removeItem(REPLAY_SESSION_KEY);
280
+ }
281
+ catch (_a) {
282
+ // Ignore potential SecurityError exceptions
283
+ }
284
+ }
285
+
270
286
  /**
271
287
  * Given an initial timestamp and an expiry duration, checks to see if current
272
288
  * time should be considered as expired.
@@ -455,7 +471,8 @@ function fetchSession() {
455
471
  */
456
472
  function getSession(_a) {
457
473
  var expiry = _a.expiry, currentSession = _a.currentSession, stickySession = _a.stickySession, samplingRate = _a.samplingRate;
458
- var session = stickySession ? fetchSession() : currentSession;
474
+ // If session exists and is passed, use it instead of always hitting session storage
475
+ var session = currentSession || (stickySession && fetchSession());
459
476
  if (session) {
460
477
  // If there is a session, check if it is valid (e.g. "last activity" time should be within the "session idle time")
461
478
  // TODO: We should probably set a max age on this as well
@@ -791,17 +808,11 @@ var SentryReplay = /** @class */ (function () {
791
808
  function SentryReplay(_a) {
792
809
  if (_a === void 0) { _a = {}; }
793
810
  var _this = this;
794
- 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
795
- _f = _a.useCompression, // TBD: Making this opt-in for now
796
- useCompression = _f === void 0 ? true : _f, _g = _a.captureOnlyOnError, captureOnlyOnError = _g === void 0 ? false : _g, _h = _a.replaysSamplingRate, replaysSamplingRate = _h === void 0 ? 1.0 : _h, _j = _a.recordingConfig, _k = _j === void 0 ? {} : _j, _l = _k.maskAllInputs, maskAllInputs = _l === void 0 ? true : _l, _m = _k.blockClass, blockClass = _m === void 0 ? 'sr-block' : _m, _o = _k.ignoreClass, ignoreClass = _o === void 0 ? 'sr-ignore' : _o, _p = _k.maskTextClass, maskTextClass = _p === void 0 ? 'sr-mask' : _p, recordingOptions = __rest(_k, ["maskAllInputs", "blockClass", "ignoreClass", "maskTextClass"]);
811
+ 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 ? true : _e, _f = _a.useCompression, useCompression = _f === void 0 ? true : _f, _g = _a.captureOnlyOnError, captureOnlyOnError = _g === void 0 ? false : _g, _h = _a.replaysSamplingRate, replaysSamplingRate = _h === void 0 ? 1.0 : _h, _j = _a.recordingConfig, _k = _j === void 0 ? {} : _j, _l = _k.maskAllInputs, maskAllInputs = _l === void 0 ? true : _l, _m = _k.blockClass, blockClass = _m === void 0 ? 'sr-block' : _m, _o = _k.ignoreClass, ignoreClass = _o === void 0 ? 'sr-ignore' : _o, _p = _k.maskTextClass, maskTextClass = _p === void 0 ? 'sr-mask' : _p, recordingOptions = __rest(_k, ["maskAllInputs", "blockClass", "ignoreClass", "maskTextClass"]);
797
812
  /**
798
813
  * @inheritDoc
799
814
  */
800
815
  this.name = SentryReplay.id;
801
- /**
802
- * Buffer of breadcrumbs to be uploaded
803
- */
804
- this.breadcrumbs = [];
805
816
  /**
806
817
  * List of PerformanceEntry from PerformanceObserver
807
818
  */
@@ -822,6 +833,19 @@ var SentryReplay = /** @class */ (function () {
822
833
  * immediately before sending recording)
823
834
  */
824
835
  this.needsCaptureReplay = false;
836
+ /**
837
+ * Is the integration currently active?
838
+ */
839
+ this.isEnabled = false;
840
+ /**
841
+ * Have we attached listeners to the core SDK?
842
+ * Note we have to track this as there is no way to remove instrumentation handlers.
843
+ */
844
+ this.hasInitializedCoreListeners = false;
845
+ /**
846
+ * Function to stop recording
847
+ */
848
+ this.stopRecording = null;
825
849
  this.context = {
826
850
  errorIds: new Set(),
827
851
  traceIds: new Set(),
@@ -897,8 +921,7 @@ var SentryReplay = /** @class */ (function () {
897
921
  // a previous session ID. In this case, we want to buffer events
898
922
  // for a set amount of time before flushing. This can help avoid
899
923
  // capturing replays of users that immediately close the window.
900
- var now = new Date().getTime();
901
- setTimeout(function () { return _this.conditionalFlush(now); }, _this.options.initialFlushDelay);
924
+ setTimeout(function () { return _this.conditionalFlush(); }, _this.options.initialFlushDelay);
902
925
  return true;
903
926
  });
904
927
  };
@@ -941,6 +964,9 @@ var SentryReplay = /** @class */ (function () {
941
964
  *
942
965
  */
943
966
  this.handleCoreSpanListener = function (type) { return function (handlerData) {
967
+ if (!_this.isEnabled) {
968
+ return;
969
+ }
944
970
  var handler = getSpanHandler(type);
945
971
  var result = handler(handlerData);
946
972
  if (result === null) {
@@ -964,6 +990,9 @@ var SentryReplay = /** @class */ (function () {
964
990
  * These events will create breadcrumb-like objects in the recording.
965
991
  */
966
992
  this.handleCoreBreadcrumbListener = function (type) { return function (handlerData) {
993
+ if (!_this.isEnabled) {
994
+ return;
995
+ }
967
996
  var handler = getBreadcrumbHandler(type);
968
997
  var result = handler(handlerData);
969
998
  if (result === null) {
@@ -1046,7 +1075,8 @@ var SentryReplay = /** @class */ (function () {
1046
1075
  // Tag all (non replay) events that get sent to Sentry with the current
1047
1076
  // replay ID so that we can reference them later in the UI
1048
1077
  core.addGlobalEventProcessor(this.handleGlobalEvent);
1049
- rrweb.record(__assign(__assign({}, this.recordingOptions), { emit: this.handleRecordingEmit }));
1078
+ this.stopRecording = rrweb.record(__assign(__assign({}, this.recordingOptions), { emit: this.handleRecordingEmit }));
1079
+ this.isEnabled = true;
1050
1080
  };
1051
1081
  /**
1052
1082
  * We want to batch uploads of replay events. Save events only if
@@ -1094,17 +1124,23 @@ var SentryReplay = /** @class */ (function () {
1094
1124
  // elapses.
1095
1125
  this.timeout = window.setTimeout(function () {
1096
1126
  logger.log('replay timeout exceeded, finishing replay event');
1097
- _this.flushUpdate(now);
1127
+ _this.flushUpdate();
1098
1128
  }, this.options.flushMinDelay);
1099
1129
  };
1100
1130
  /**
1101
1131
  * Currently, this needs to be manually called (e.g. for tests). Sentry SDK does not support a teardown
1102
1132
  */
1103
1133
  SentryReplay.prototype.destroy = function () {
1134
+ var _a, _b;
1104
1135
  logger.log('Destroying instance');
1136
+ this.isEnabled = false;
1105
1137
  this.removeListeners();
1138
+ (_a = this.stopRecording) === null || _a === void 0 ? void 0 : _a.call(this);
1139
+ (_b = this.eventBuffer) === null || _b === void 0 ? void 0 : _b.destroy();
1140
+ this.eventBuffer = null;
1106
1141
  };
1107
1142
  SentryReplay.prototype.clearSession = function () {
1143
+ deleteSession();
1108
1144
  this.session = undefined;
1109
1145
  };
1110
1146
  /**
@@ -1139,6 +1175,7 @@ var SentryReplay = /** @class */ (function () {
1139
1175
  SentryReplay.prototype.setInitialState = function () {
1140
1176
  var urlPath = "".concat(window.location.pathname).concat(window.location.hash).concat(window.location.search);
1141
1177
  var url = "".concat(window.location.origin).concat(urlPath);
1178
+ this.performanceEvents = [];
1142
1179
  // Reset context as well
1143
1180
  this.popEventContext();
1144
1181
  this.initialState = {
@@ -1155,13 +1192,17 @@ var SentryReplay = /** @class */ (function () {
1155
1192
  document.addEventListener('visibilitychange', this.handleVisibilityChange);
1156
1193
  window.addEventListener('blur', this.handleWindowBlur);
1157
1194
  window.addEventListener('focus', this.handleWindowFocus);
1158
- // Listeners from core SDK //
1159
- var scope = core.getCurrentHub().getScope();
1160
- scope === null || scope === void 0 ? void 0 : scope.addScopeListener(this.handleCoreBreadcrumbListener('scope'));
1161
- utils.addInstrumentationHandler('dom', this.handleCoreBreadcrumbListener('dom'));
1162
- utils.addInstrumentationHandler('fetch', this.handleCoreSpanListener('fetch'));
1163
- utils.addInstrumentationHandler('xhr', this.handleCoreSpanListener('xhr'));
1164
- utils.addInstrumentationHandler('history', this.handleCoreSpanListener('history'));
1195
+ // There is no way to remove these listeners, so ensure they are only added once
1196
+ if (!this.hasInitializedCoreListeners) {
1197
+ // Listeners from core SDK //
1198
+ var scope = core.getCurrentHub().getScope();
1199
+ scope === null || scope === void 0 ? void 0 : scope.addScopeListener(this.handleCoreBreadcrumbListener('scope'));
1200
+ utils.addInstrumentationHandler('dom', this.handleCoreBreadcrumbListener('dom'));
1201
+ utils.addInstrumentationHandler('fetch', this.handleCoreSpanListener('fetch'));
1202
+ utils.addInstrumentationHandler('xhr', this.handleCoreSpanListener('xhr'));
1203
+ utils.addInstrumentationHandler('history', this.handleCoreSpanListener('history'));
1204
+ this.hasInitializedCoreListeners = true;
1205
+ }
1165
1206
  // PerformanceObserver //
1166
1207
  if (!('PerformanceObserver' in window)) {
1167
1208
  return;
@@ -1180,10 +1221,16 @@ var SentryReplay = /** @class */ (function () {
1180
1221
  'resource',
1181
1222
  ].forEach(function (type) {
1182
1223
  var _a;
1183
- return (_a = _this.performanceObserver) === null || _a === void 0 ? void 0 : _a.observe({
1184
- type: type,
1185
- buffered: true,
1186
- });
1224
+ try {
1225
+ (_a = _this.performanceObserver) === null || _a === void 0 ? void 0 : _a.observe({
1226
+ type: type,
1227
+ buffered: true,
1228
+ });
1229
+ }
1230
+ catch (_b) {
1231
+ // This can throw if an entry type is not supported in the browser.
1232
+ // Ignore these errors.
1233
+ }
1187
1234
  });
1188
1235
  };
1189
1236
  /**
@@ -1252,7 +1299,14 @@ var SentryReplay = /** @class */ (function () {
1252
1299
  * Add an event to the event buffer
1253
1300
  */
1254
1301
  SentryReplay.prototype.addEvent = function (event, isCheckout) {
1255
- var timestampInMs = event.timestamp * 1000;
1302
+ if (!this.eventBuffer) {
1303
+ // This implies that `isEnabled` is false
1304
+ return;
1305
+ }
1306
+ // TODO: sadness -- we will want to normalize timestamps to be in ms -
1307
+ // requires coordination with frontend
1308
+ var isMs = event.timestamp > 9999999999;
1309
+ var timestampInMs = isMs ? event.timestamp : event.timestamp * 1000;
1256
1310
  if (!this.context.earliestEvent ||
1257
1311
  timestampInMs < this.context.earliestEvent) {
1258
1312
  this.context.earliestEvent = timestampInMs;
@@ -1359,11 +1413,11 @@ var SentryReplay = /** @class */ (function () {
1359
1413
  /**
1360
1414
  * Only flush if `captureOnlyOnError` is false.
1361
1415
  */
1362
- SentryReplay.prototype.conditionalFlush = function (lastActivity) {
1416
+ SentryReplay.prototype.conditionalFlush = function () {
1363
1417
  if (this.options.captureOnlyOnError) {
1364
1418
  return;
1365
1419
  }
1366
- return this.flushUpdate(lastActivity);
1420
+ return this.flushUpdate();
1367
1421
  };
1368
1422
  /**
1369
1423
  * Return and clear context
@@ -1397,13 +1451,18 @@ var SentryReplay = /** @class */ (function () {
1397
1451
  * Performance events are only added right before flushing - this is probably
1398
1452
  * due to the buffered performance observer events.
1399
1453
  */
1400
- SentryReplay.prototype.flushUpdate = function (lastActivity) {
1401
- var _a;
1454
+ SentryReplay.prototype.flushUpdate = function () {
1455
+ var _a, _b;
1402
1456
  return __awaiter(this, void 0, void 0, function () {
1403
1457
  var timestamp, recordingData, err_1;
1404
- return __generator(this, function (_b) {
1405
- switch (_b.label) {
1458
+ return __generator(this, function (_c) {
1459
+ switch (_c.label) {
1406
1460
  case 0:
1461
+ if (!this.isEnabled) {
1462
+ // This is just a precaution, there should be no listeners that would
1463
+ // cause a flush.
1464
+ return [2 /*return*/];
1465
+ }
1407
1466
  if (!this.checkAndHandleExpiredSession()) {
1408
1467
  logger.error(new Error('Attempting to finish replay event after session expired.'));
1409
1468
  return [2 /*return*/];
@@ -1416,16 +1475,16 @@ var SentryReplay = /** @class */ (function () {
1416
1475
  clearTimeout(this.timeout);
1417
1476
  return [4 /*yield*/, this.addPerformanceEntries()];
1418
1477
  case 1:
1419
- _b.sent();
1420
- if (!this.eventBuffer.length) {
1478
+ _c.sent();
1479
+ if (!((_b = this.eventBuffer) === null || _b === void 0 ? void 0 : _b.length)) {
1421
1480
  return [2 /*return*/];
1422
1481
  }
1423
1482
  // Only attach memory event if eventBuffer is not empty
1424
1483
  return [4 /*yield*/, this.addMemoryEntry()];
1425
1484
  case 2:
1426
1485
  // Only attach memory event if eventBuffer is not empty
1427
- _b.sent();
1428
- timestamp = lastActivity !== null && lastActivity !== void 0 ? lastActivity : new Date().getTime();
1486
+ _c.sent();
1487
+ timestamp = new Date().getTime();
1429
1488
  // Only want to create replay event if session is new
1430
1489
  if (this.needsCaptureReplay) {
1431
1490
  // This event needs to exist before calling `sendReplay`
@@ -1435,15 +1494,15 @@ var SentryReplay = /** @class */ (function () {
1435
1494
  // Reset this to null regardless of `sendReplay` result so that future
1436
1495
  // events will get flushed properly
1437
1496
  this.initialEventTimestampSinceFlush = null;
1438
- _b.label = 3;
1497
+ _c.label = 3;
1439
1498
  case 3:
1440
- _b.trys.push([3, 6, , 7]);
1499
+ _c.trys.push([3, 6, , 7]);
1441
1500
  return [4 /*yield*/, this.eventBuffer.finish()];
1442
1501
  case 4:
1443
- recordingData = _b.sent();
1502
+ recordingData = _c.sent();
1444
1503
  return [4 /*yield*/, this.sendReplay(this.session.id, recordingData)];
1445
1504
  case 5:
1446
- _b.sent();
1505
+ _c.sent();
1447
1506
  // The below will only happen after successfully sending replay //
1448
1507
  // TBD: Alternatively we could update this after every rrweb event
1449
1508
  // `timestamp` should reflect when the event happens. e.g. the timestamp
@@ -1453,7 +1512,7 @@ var SentryReplay = /** @class */ (function () {
1453
1512
  captureReplayUpdate(this.popEventContext({ timestamp: timestamp }));
1454
1513
  return [3 /*break*/, 7];
1455
1514
  case 6:
1456
- err_1 = _b.sent();
1515
+ err_1 = _c.sent();
1457
1516
  core.captureException(err_1);
1458
1517
  console.error(err_1);
1459
1518
  return [3 /*break*/, 7];