@sentry/replay 0.5.11 → 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/README.md CHANGED
@@ -46,6 +46,18 @@ Sentry.init({
46
46
  });
47
47
  ```
48
48
 
49
+ ### Stopping Replays
50
+
51
+ To stop replays you can call the `destroy()` method on the integration instance. It's possible to resume recording by calling `setup()`.
52
+
53
+ ```javascript
54
+
55
+ const replay = new SentryReplay(); // This will begin recording replays
56
+
57
+ replay.destroy(); // Stop recording
58
+
59
+ replay.setup(); // Start recording again
60
+ ```
49
61
 
50
62
  ## Configuration
51
63
 
@@ -18,11 +18,7 @@ export declare class SentryReplay implements Integration {
18
18
  * @inheritDoc
19
19
  */
20
20
  name: string;
21
- eventBuffer: IEventBuffer;
22
- /**
23
- * Buffer of breadcrumbs to be uploaded
24
- */
25
- breadcrumbs: Breadcrumb[];
21
+ eventBuffer: IEventBuffer | null;
26
22
  /**
27
23
  * List of PerformanceEntry from PerformanceObserver
28
24
  */
@@ -52,6 +48,19 @@ export declare class SentryReplay implements Integration {
52
48
  * immediately before sending recording)
53
49
  */
54
50
  private needsCaptureReplay;
51
+ /**
52
+ * Is the integration currently active?
53
+ */
54
+ private isEnabled;
55
+ /**
56
+ * Have we attached listeners to the core SDK?
57
+ * Note we have to track this as there is no way to remove instrumentation handlers.
58
+ */
59
+ private hasInitializedCoreListeners;
60
+ /**
61
+ * Function to stop recording
62
+ */
63
+ private stopRecording;
55
64
  /**
56
65
  * Captured state when integration is first initialized
57
66
  */
package/dist/index.es.js CHANGED
@@ -809,10 +809,6 @@ var SentryReplay = /** @class */ (function () {
809
809
  * @inheritDoc
810
810
  */
811
811
  this.name = SentryReplay.id;
812
- /**
813
- * Buffer of breadcrumbs to be uploaded
814
- */
815
- this.breadcrumbs = [];
816
812
  /**
817
813
  * List of PerformanceEntry from PerformanceObserver
818
814
  */
@@ -833,6 +829,19 @@ var SentryReplay = /** @class */ (function () {
833
829
  * immediately before sending recording)
834
830
  */
835
831
  this.needsCaptureReplay = false;
832
+ /**
833
+ * Is the integration currently active?
834
+ */
835
+ this.isEnabled = false;
836
+ /**
837
+ * Have we attached listeners to the core SDK?
838
+ * Note we have to track this as there is no way to remove instrumentation handlers.
839
+ */
840
+ this.hasInitializedCoreListeners = false;
841
+ /**
842
+ * Function to stop recording
843
+ */
844
+ this.stopRecording = null;
836
845
  this.context = {
837
846
  errorIds: new Set(),
838
847
  traceIds: new Set(),
@@ -951,6 +960,9 @@ var SentryReplay = /** @class */ (function () {
951
960
  *
952
961
  */
953
962
  this.handleCoreSpanListener = function (type) { return function (handlerData) {
963
+ if (!_this.isEnabled) {
964
+ return;
965
+ }
954
966
  var handler = getSpanHandler(type);
955
967
  var result = handler(handlerData);
956
968
  if (result === null) {
@@ -974,6 +986,9 @@ var SentryReplay = /** @class */ (function () {
974
986
  * These events will create breadcrumb-like objects in the recording.
975
987
  */
976
988
  this.handleCoreBreadcrumbListener = function (type) { return function (handlerData) {
989
+ if (!_this.isEnabled) {
990
+ return;
991
+ }
977
992
  var handler = getBreadcrumbHandler(type);
978
993
  var result = handler(handlerData);
979
994
  if (result === null) {
@@ -1056,7 +1071,8 @@ var SentryReplay = /** @class */ (function () {
1056
1071
  // Tag all (non replay) events that get sent to Sentry with the current
1057
1072
  // replay ID so that we can reference them later in the UI
1058
1073
  addGlobalEventProcessor(this.handleGlobalEvent);
1059
- record(__assign(__assign({}, this.recordingOptions), { emit: this.handleRecordingEmit }));
1074
+ this.stopRecording = record(__assign(__assign({}, this.recordingOptions), { emit: this.handleRecordingEmit }));
1075
+ this.isEnabled = true;
1060
1076
  };
1061
1077
  /**
1062
1078
  * We want to batch uploads of replay events. Save events only if
@@ -1111,8 +1127,13 @@ var SentryReplay = /** @class */ (function () {
1111
1127
  * Currently, this needs to be manually called (e.g. for tests). Sentry SDK does not support a teardown
1112
1128
  */
1113
1129
  SentryReplay.prototype.destroy = function () {
1130
+ var _a, _b;
1114
1131
  logger.log('Destroying instance');
1132
+ this.isEnabled = false;
1115
1133
  this.removeListeners();
1134
+ (_a = this.stopRecording) === null || _a === void 0 ? void 0 : _a.call(this);
1135
+ (_b = this.eventBuffer) === null || _b === void 0 ? void 0 : _b.destroy();
1136
+ this.eventBuffer = null;
1116
1137
  };
1117
1138
  SentryReplay.prototype.clearSession = function () {
1118
1139
  deleteSession();
@@ -1150,6 +1171,7 @@ var SentryReplay = /** @class */ (function () {
1150
1171
  SentryReplay.prototype.setInitialState = function () {
1151
1172
  var urlPath = "".concat(window.location.pathname).concat(window.location.hash).concat(window.location.search);
1152
1173
  var url = "".concat(window.location.origin).concat(urlPath);
1174
+ this.performanceEvents = [];
1153
1175
  // Reset context as well
1154
1176
  this.popEventContext();
1155
1177
  this.initialState = {
@@ -1166,13 +1188,17 @@ var SentryReplay = /** @class */ (function () {
1166
1188
  document.addEventListener('visibilitychange', this.handleVisibilityChange);
1167
1189
  window.addEventListener('blur', this.handleWindowBlur);
1168
1190
  window.addEventListener('focus', this.handleWindowFocus);
1169
- // Listeners from core SDK //
1170
- var scope = getCurrentHub().getScope();
1171
- scope === null || scope === void 0 ? void 0 : scope.addScopeListener(this.handleCoreBreadcrumbListener('scope'));
1172
- addInstrumentationHandler('dom', this.handleCoreBreadcrumbListener('dom'));
1173
- addInstrumentationHandler('fetch', this.handleCoreSpanListener('fetch'));
1174
- addInstrumentationHandler('xhr', this.handleCoreSpanListener('xhr'));
1175
- addInstrumentationHandler('history', this.handleCoreSpanListener('history'));
1191
+ // There is no way to remove these listeners, so ensure they are only added once
1192
+ if (!this.hasInitializedCoreListeners) {
1193
+ // Listeners from core SDK //
1194
+ var scope = getCurrentHub().getScope();
1195
+ scope === null || scope === void 0 ? void 0 : scope.addScopeListener(this.handleCoreBreadcrumbListener('scope'));
1196
+ addInstrumentationHandler('dom', this.handleCoreBreadcrumbListener('dom'));
1197
+ addInstrumentationHandler('fetch', this.handleCoreSpanListener('fetch'));
1198
+ addInstrumentationHandler('xhr', this.handleCoreSpanListener('xhr'));
1199
+ addInstrumentationHandler('history', this.handleCoreSpanListener('history'));
1200
+ this.hasInitializedCoreListeners = true;
1201
+ }
1176
1202
  // PerformanceObserver //
1177
1203
  if (!('PerformanceObserver' in window)) {
1178
1204
  return;
@@ -1269,7 +1295,14 @@ var SentryReplay = /** @class */ (function () {
1269
1295
  * Add an event to the event buffer
1270
1296
  */
1271
1297
  SentryReplay.prototype.addEvent = function (event, isCheckout) {
1272
- var timestampInMs = event.timestamp * 1000;
1298
+ if (!this.eventBuffer) {
1299
+ // This implies that `isEnabled` is false
1300
+ return;
1301
+ }
1302
+ // TODO: sadness -- we will want to normalize timestamps to be in ms -
1303
+ // requires coordination with frontend
1304
+ var isMs = event.timestamp > 9999999999;
1305
+ var timestampInMs = isMs ? event.timestamp : event.timestamp * 1000;
1273
1306
  if (!this.context.earliestEvent ||
1274
1307
  timestampInMs < this.context.earliestEvent) {
1275
1308
  this.context.earliestEvent = timestampInMs;
@@ -1415,12 +1448,17 @@ var SentryReplay = /** @class */ (function () {
1415
1448
  * due to the buffered performance observer events.
1416
1449
  */
1417
1450
  SentryReplay.prototype.flushUpdate = function () {
1418
- var _a;
1451
+ var _a, _b;
1419
1452
  return __awaiter(this, void 0, void 0, function () {
1420
1453
  var timestamp, recordingData, err_1;
1421
- return __generator(this, function (_b) {
1422
- switch (_b.label) {
1454
+ return __generator(this, function (_c) {
1455
+ switch (_c.label) {
1423
1456
  case 0:
1457
+ if (!this.isEnabled) {
1458
+ // This is just a precaution, there should be no listeners that would
1459
+ // cause a flush.
1460
+ return [2 /*return*/];
1461
+ }
1424
1462
  if (!this.checkAndHandleExpiredSession()) {
1425
1463
  logger.error(new Error('Attempting to finish replay event after session expired.'));
1426
1464
  return [2 /*return*/];
@@ -1433,15 +1471,15 @@ var SentryReplay = /** @class */ (function () {
1433
1471
  clearTimeout(this.timeout);
1434
1472
  return [4 /*yield*/, this.addPerformanceEntries()];
1435
1473
  case 1:
1436
- _b.sent();
1437
- if (!this.eventBuffer.length) {
1474
+ _c.sent();
1475
+ if (!((_b = this.eventBuffer) === null || _b === void 0 ? void 0 : _b.length)) {
1438
1476
  return [2 /*return*/];
1439
1477
  }
1440
1478
  // Only attach memory event if eventBuffer is not empty
1441
1479
  return [4 /*yield*/, this.addMemoryEntry()];
1442
1480
  case 2:
1443
1481
  // Only attach memory event if eventBuffer is not empty
1444
- _b.sent();
1482
+ _c.sent();
1445
1483
  timestamp = new Date().getTime();
1446
1484
  // Only want to create replay event if session is new
1447
1485
  if (this.needsCaptureReplay) {
@@ -1452,15 +1490,15 @@ var SentryReplay = /** @class */ (function () {
1452
1490
  // Reset this to null regardless of `sendReplay` result so that future
1453
1491
  // events will get flushed properly
1454
1492
  this.initialEventTimestampSinceFlush = null;
1455
- _b.label = 3;
1493
+ _c.label = 3;
1456
1494
  case 3:
1457
- _b.trys.push([3, 6, , 7]);
1495
+ _c.trys.push([3, 6, , 7]);
1458
1496
  return [4 /*yield*/, this.eventBuffer.finish()];
1459
1497
  case 4:
1460
- recordingData = _b.sent();
1498
+ recordingData = _c.sent();
1461
1499
  return [4 /*yield*/, this.sendReplay(this.session.id, recordingData)];
1462
1500
  case 5:
1463
- _b.sent();
1501
+ _c.sent();
1464
1502
  // The below will only happen after successfully sending replay //
1465
1503
  // TBD: Alternatively we could update this after every rrweb event
1466
1504
  // `timestamp` should reflect when the event happens. e.g. the timestamp
@@ -1470,7 +1508,7 @@ var SentryReplay = /** @class */ (function () {
1470
1508
  captureReplayUpdate(this.popEventContext({ timestamp: timestamp }));
1471
1509
  return [3 /*break*/, 7];
1472
1510
  case 6:
1473
- err_1 = _b.sent();
1511
+ err_1 = _c.sent();
1474
1512
  captureException(err_1);
1475
1513
  console.error(err_1);
1476
1514
  return [3 /*break*/, 7];
package/dist/index.js CHANGED
@@ -813,10 +813,6 @@ var SentryReplay = /** @class */ (function () {
813
813
  * @inheritDoc
814
814
  */
815
815
  this.name = SentryReplay.id;
816
- /**
817
- * Buffer of breadcrumbs to be uploaded
818
- */
819
- this.breadcrumbs = [];
820
816
  /**
821
817
  * List of PerformanceEntry from PerformanceObserver
822
818
  */
@@ -837,6 +833,19 @@ var SentryReplay = /** @class */ (function () {
837
833
  * immediately before sending recording)
838
834
  */
839
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;
840
849
  this.context = {
841
850
  errorIds: new Set(),
842
851
  traceIds: new Set(),
@@ -955,6 +964,9 @@ var SentryReplay = /** @class */ (function () {
955
964
  *
956
965
  */
957
966
  this.handleCoreSpanListener = function (type) { return function (handlerData) {
967
+ if (!_this.isEnabled) {
968
+ return;
969
+ }
958
970
  var handler = getSpanHandler(type);
959
971
  var result = handler(handlerData);
960
972
  if (result === null) {
@@ -978,6 +990,9 @@ var SentryReplay = /** @class */ (function () {
978
990
  * These events will create breadcrumb-like objects in the recording.
979
991
  */
980
992
  this.handleCoreBreadcrumbListener = function (type) { return function (handlerData) {
993
+ if (!_this.isEnabled) {
994
+ return;
995
+ }
981
996
  var handler = getBreadcrumbHandler(type);
982
997
  var result = handler(handlerData);
983
998
  if (result === null) {
@@ -1060,7 +1075,8 @@ var SentryReplay = /** @class */ (function () {
1060
1075
  // Tag all (non replay) events that get sent to Sentry with the current
1061
1076
  // replay ID so that we can reference them later in the UI
1062
1077
  core.addGlobalEventProcessor(this.handleGlobalEvent);
1063
- 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;
1064
1080
  };
1065
1081
  /**
1066
1082
  * We want to batch uploads of replay events. Save events only if
@@ -1115,8 +1131,13 @@ var SentryReplay = /** @class */ (function () {
1115
1131
  * Currently, this needs to be manually called (e.g. for tests). Sentry SDK does not support a teardown
1116
1132
  */
1117
1133
  SentryReplay.prototype.destroy = function () {
1134
+ var _a, _b;
1118
1135
  logger.log('Destroying instance');
1136
+ this.isEnabled = false;
1119
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;
1120
1141
  };
1121
1142
  SentryReplay.prototype.clearSession = function () {
1122
1143
  deleteSession();
@@ -1154,6 +1175,7 @@ var SentryReplay = /** @class */ (function () {
1154
1175
  SentryReplay.prototype.setInitialState = function () {
1155
1176
  var urlPath = "".concat(window.location.pathname).concat(window.location.hash).concat(window.location.search);
1156
1177
  var url = "".concat(window.location.origin).concat(urlPath);
1178
+ this.performanceEvents = [];
1157
1179
  // Reset context as well
1158
1180
  this.popEventContext();
1159
1181
  this.initialState = {
@@ -1170,13 +1192,17 @@ var SentryReplay = /** @class */ (function () {
1170
1192
  document.addEventListener('visibilitychange', this.handleVisibilityChange);
1171
1193
  window.addEventListener('blur', this.handleWindowBlur);
1172
1194
  window.addEventListener('focus', this.handleWindowFocus);
1173
- // Listeners from core SDK //
1174
- var scope = core.getCurrentHub().getScope();
1175
- scope === null || scope === void 0 ? void 0 : scope.addScopeListener(this.handleCoreBreadcrumbListener('scope'));
1176
- utils.addInstrumentationHandler('dom', this.handleCoreBreadcrumbListener('dom'));
1177
- utils.addInstrumentationHandler('fetch', this.handleCoreSpanListener('fetch'));
1178
- utils.addInstrumentationHandler('xhr', this.handleCoreSpanListener('xhr'));
1179
- 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
+ }
1180
1206
  // PerformanceObserver //
1181
1207
  if (!('PerformanceObserver' in window)) {
1182
1208
  return;
@@ -1273,7 +1299,14 @@ var SentryReplay = /** @class */ (function () {
1273
1299
  * Add an event to the event buffer
1274
1300
  */
1275
1301
  SentryReplay.prototype.addEvent = function (event, isCheckout) {
1276
- 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;
1277
1310
  if (!this.context.earliestEvent ||
1278
1311
  timestampInMs < this.context.earliestEvent) {
1279
1312
  this.context.earliestEvent = timestampInMs;
@@ -1419,12 +1452,17 @@ var SentryReplay = /** @class */ (function () {
1419
1452
  * due to the buffered performance observer events.
1420
1453
  */
1421
1454
  SentryReplay.prototype.flushUpdate = function () {
1422
- var _a;
1455
+ var _a, _b;
1423
1456
  return __awaiter(this, void 0, void 0, function () {
1424
1457
  var timestamp, recordingData, err_1;
1425
- return __generator(this, function (_b) {
1426
- switch (_b.label) {
1458
+ return __generator(this, function (_c) {
1459
+ switch (_c.label) {
1427
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
+ }
1428
1466
  if (!this.checkAndHandleExpiredSession()) {
1429
1467
  logger.error(new Error('Attempting to finish replay event after session expired.'));
1430
1468
  return [2 /*return*/];
@@ -1437,15 +1475,15 @@ var SentryReplay = /** @class */ (function () {
1437
1475
  clearTimeout(this.timeout);
1438
1476
  return [4 /*yield*/, this.addPerformanceEntries()];
1439
1477
  case 1:
1440
- _b.sent();
1441
- if (!this.eventBuffer.length) {
1478
+ _c.sent();
1479
+ if (!((_b = this.eventBuffer) === null || _b === void 0 ? void 0 : _b.length)) {
1442
1480
  return [2 /*return*/];
1443
1481
  }
1444
1482
  // Only attach memory event if eventBuffer is not empty
1445
1483
  return [4 /*yield*/, this.addMemoryEntry()];
1446
1484
  case 2:
1447
1485
  // Only attach memory event if eventBuffer is not empty
1448
- _b.sent();
1486
+ _c.sent();
1449
1487
  timestamp = new Date().getTime();
1450
1488
  // Only want to create replay event if session is new
1451
1489
  if (this.needsCaptureReplay) {
@@ -1456,15 +1494,15 @@ var SentryReplay = /** @class */ (function () {
1456
1494
  // Reset this to null regardless of `sendReplay` result so that future
1457
1495
  // events will get flushed properly
1458
1496
  this.initialEventTimestampSinceFlush = null;
1459
- _b.label = 3;
1497
+ _c.label = 3;
1460
1498
  case 3:
1461
- _b.trys.push([3, 6, , 7]);
1499
+ _c.trys.push([3, 6, , 7]);
1462
1500
  return [4 /*yield*/, this.eventBuffer.finish()];
1463
1501
  case 4:
1464
- recordingData = _b.sent();
1502
+ recordingData = _c.sent();
1465
1503
  return [4 /*yield*/, this.sendReplay(this.session.id, recordingData)];
1466
1504
  case 5:
1467
- _b.sent();
1505
+ _c.sent();
1468
1506
  // The below will only happen after successfully sending replay //
1469
1507
  // TBD: Alternatively we could update this after every rrweb event
1470
1508
  // `timestamp` should reflect when the event happens. e.g. the timestamp
@@ -1474,7 +1512,7 @@ var SentryReplay = /** @class */ (function () {
1474
1512
  captureReplayUpdate(this.popEventContext({ timestamp: timestamp }));
1475
1513
  return [3 /*break*/, 7];
1476
1514
  case 6:
1477
- err_1 = _b.sent();
1515
+ err_1 = _c.sent();
1478
1516
  core.captureException(err_1);
1479
1517
  console.error(err_1);
1480
1518
  return [3 /*break*/, 7];