@sentry/replay 0.6.8 → 0.6.10-0

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
@@ -473,8 +473,9 @@ function getSession(_a) {
473
473
  // If session exists and is passed, use it instead of always hitting session storage
474
474
  var session = currentSession || (stickySession && fetchSession());
475
475
  if (session) {
476
- // If there is a session, check if it is valid (e.g. "last activity" time should be within the "session idle time")
477
- // TODO: We should probably set a max age on this as well
476
+ // If there is a session, check if it is valid (e.g. "last activity" time
477
+ // should be within the "session idle time", and "session started" time is
478
+ // within "max session time").
478
479
  var isExpired = isSessionExpired(session, expiry);
479
480
  if (!isExpired) {
480
481
  return { type: 'saved', session: session };
@@ -927,10 +928,21 @@ var Replay = /** @class */ (function () {
927
928
  */
928
929
  this.newSessionCreated = false;
929
930
  this.flushLock = null;
931
+ /**
932
+ * Timestamp of the last user activity. This lives across sessions.
933
+ */
934
+ this.lastActivity = new Date().getTime();
930
935
  /**
931
936
  * Is the integration currently active?
932
937
  */
933
938
  this.isEnabled = false;
939
+ /**
940
+ *
941
+ * Paused is a state where:
942
+ * - DOM Recording is not listening at all
943
+ * - Nothing will be added to event buffer (e.g. core SDK events)
944
+ */
945
+ this.isPaused = false;
934
946
  /**
935
947
  * Have we attached listeners to the core SDK?
936
948
  * Note we have to track this as there is no way to remove instrumentation handlers.
@@ -981,7 +993,7 @@ var Replay = /** @class */ (function () {
981
993
  ) {
982
994
  // TODO: Do we continue to record after?
983
995
  // TODO: What happens if another error happens? Do we record in the same session?
984
- setTimeout(function () { return _this.flush(); });
996
+ setTimeout(function () { return _this.flushImmediate(); });
985
997
  }
986
998
  return event;
987
999
  };
@@ -1086,7 +1098,8 @@ var Replay = /** @class */ (function () {
1086
1098
  if (type === 'history') {
1087
1099
  // Need to collect visited URLs
1088
1100
  _this.context.urls.push(result.name);
1089
- _this.updateLastActivity();
1101
+ _this.updateUserActivity();
1102
+ _this.triggerUserActivity();
1090
1103
  }
1091
1104
  _this.addUpdate(function () {
1092
1105
  _this.createPerformanceSpans([result]);
@@ -1114,7 +1127,10 @@ var Replay = /** @class */ (function () {
1114
1127
  return;
1115
1128
  }
1116
1129
  if (result.category === 'ui.click') {
1117
- _this.updateLastActivity();
1130
+ _this.triggerUserActivity();
1131
+ }
1132
+ else {
1133
+ _this.checkAndHandleExpiredSession();
1118
1134
  }
1119
1135
  _this.addUpdate(function () {
1120
1136
  _this.addEvent({
@@ -1142,7 +1158,7 @@ var Replay = /** @class */ (function () {
1142
1158
  };
1143
1159
  /**
1144
1160
  * Flush recording data to Sentry. Creates a lock so that only a single flush
1145
- * can be active at a time.
1161
+ * can be active at a time. Do not call this directly.
1146
1162
  */
1147
1163
  this.flush = function () { return __awaiter(_this, void 0, void 0, function () {
1148
1164
  var err_1;
@@ -1267,7 +1283,7 @@ var Replay = /** @class */ (function () {
1267
1283
  }
1268
1284
  // setup() is generally called on page load or manually - in both cases we
1269
1285
  // should treat it as an activity
1270
- this.updateLastActivity();
1286
+ this.updateSessionActivity();
1271
1287
  this.eventBuffer = createEventBuffer({
1272
1288
  useCompression: Boolean(this.options.useCompression),
1273
1289
  });
@@ -1275,33 +1291,16 @@ var Replay = /** @class */ (function () {
1275
1291
  // Tag all (non replay) events that get sent to Sentry with the current
1276
1292
  // replay ID so that we can reference them later in the UI
1277
1293
  core.addGlobalEventProcessor(this.handleGlobalEvent);
1278
- this.stopRecording = rrweb.record(__assign(__assign({}, this.recordingOptions), { emit: this.handleRecordingEmit }));
1294
+ this.startRecording();
1279
1295
  this.isEnabled = true;
1280
1296
  };
1281
1297
  /**
1282
- * We want to batch uploads of replay events. Save events only if
1283
- * `<flushMinDelay>` milliseconds have elapsed since the last event
1284
- * *OR* if `<flushMaxDelay>` milliseconds have elapsed.
1298
+ * Start recording.
1285
1299
  *
1286
- * Accepts a callback to perform side-effects and returns true to stop batch
1287
- * processing and hand back control to caller.
1300
+ * Note that this will cause a new DOM checkout
1288
1301
  */
1289
- Replay.prototype.addUpdate = function (cb) {
1290
- // We need to always run `cb` (e.g. in the case of captureOnlyOnError == true)
1291
- var cbResult = cb === null || cb === void 0 ? void 0 : cb();
1292
- // If this option is turned on then we will only want to call `flush`
1293
- // explicitly
1294
- if (this.options.captureOnlyOnError) {
1295
- return;
1296
- }
1297
- // If callback is true, we do not want to continue with flushing -- the
1298
- // caller will need to handle it.
1299
- if (cbResult === true) {
1300
- return;
1301
- }
1302
- // addUpdate is called quite frequently - use debouncedFlush so that it
1303
- // respects the flush delays and does not flush immediately
1304
- this.debouncedFlush();
1302
+ Replay.prototype.startRecording = function () {
1303
+ this.stopRecording = rrweb.record(__assign(__assign({}, this.recordingOptions), { emit: this.handleRecordingEmit }));
1305
1304
  };
1306
1305
  /**
1307
1306
  * Currently, this needs to be manually called (e.g. for tests). Sentry SDK does not support a teardown
@@ -1318,6 +1317,27 @@ var Replay = /** @class */ (function () {
1318
1317
  (_b = this.eventBuffer) === null || _b === void 0 ? void 0 : _b.destroy();
1319
1318
  this.eventBuffer = null;
1320
1319
  };
1320
+ /**
1321
+ * Pause some replay functionality. See comments for `isPaused`.
1322
+ * This differs from stop as this only stops DOM recording, it is
1323
+ * not as thorough of a shutdown as `stop()`. */
1324
+ Replay.prototype.pause = function () {
1325
+ this.isPaused = true;
1326
+ if (this.stopRecording) {
1327
+ this.stopRecording();
1328
+ this.stopRecording = undefined;
1329
+ }
1330
+ };
1331
+ /**
1332
+ * Resumes recording, see notes for `pause().
1333
+ *
1334
+ * Note that calling `startRecording()` here will cause a
1335
+ * new DOM checkout.`
1336
+ */
1337
+ Replay.prototype.resume = function () {
1338
+ this.isPaused = false;
1339
+ this.startRecording();
1340
+ };
1321
1341
  Replay.prototype.clearSession = function () {
1322
1342
  deleteSession();
1323
1343
  this.session = undefined;
@@ -1424,6 +1444,31 @@ var Replay = /** @class */ (function () {
1424
1444
  this.performanceObserver = null;
1425
1445
  }
1426
1446
  };
1447
+ /**
1448
+ * We want to batch uploads of replay events. Save events only if
1449
+ * `<flushMinDelay>` milliseconds have elapsed since the last event
1450
+ * *OR* if `<flushMaxDelay>` milliseconds have elapsed.
1451
+ *
1452
+ * Accepts a callback to perform side-effects and returns true to stop batch
1453
+ * processing and hand back control to caller.
1454
+ */
1455
+ Replay.prototype.addUpdate = function (cb) {
1456
+ // We need to always run `cb` (e.g. in the case of captureOnlyOnError == true)
1457
+ var cbResult = cb === null || cb === void 0 ? void 0 : cb();
1458
+ // If this option is turned on then we will only want to call `flush`
1459
+ // explicitly
1460
+ if (this.options.captureOnlyOnError) {
1461
+ return;
1462
+ }
1463
+ // If callback is true, we do not want to continue with flushing -- the
1464
+ // caller will need to handle it.
1465
+ if (cbResult === true) {
1466
+ return;
1467
+ }
1468
+ // addUpdate is called quite frequently - use debouncedFlush so that it
1469
+ // respects the flush delays and does not flush immediately
1470
+ this.debouncedFlush();
1471
+ };
1427
1472
  /**
1428
1473
  * Tasks to run when we consider a page to be hidden (via blurring and/or visibility)
1429
1474
  */
@@ -1431,8 +1476,8 @@ var Replay = /** @class */ (function () {
1431
1476
  if (!this.session) {
1432
1477
  return;
1433
1478
  }
1434
- var isExpired = isSessionExpired(this.session, VISIBILITY_CHANGE_TIMEOUT);
1435
- if (breadcrumb && !isExpired) {
1479
+ var expired = isSessionExpired(this.session, VISIBILITY_CHANGE_TIMEOUT);
1480
+ if (breadcrumb && !expired) {
1436
1481
  this.createCustomBreadcrumb(breadcrumb);
1437
1482
  }
1438
1483
  // Send replay when the page/tab becomes hidden. There is no reason to send
@@ -1447,24 +1492,19 @@ var Replay = /** @class */ (function () {
1447
1492
  if (!this.session) {
1448
1493
  return;
1449
1494
  }
1450
- var isExpired = isSessionExpired(this.session, VISIBILITY_CHANGE_TIMEOUT);
1451
- if (isExpired) {
1495
+ var isSessionActive = this.checkAndHandleExpiredSession({
1496
+ expiry: VISIBILITY_CHANGE_TIMEOUT,
1497
+ });
1498
+ if (!isSessionActive) {
1452
1499
  // If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
1453
1500
  // ms, we will re-use the existing session, otherwise create a new
1454
1501
  // session
1455
1502
  logger.log('Document has become active, but session has expired');
1456
- this.loadSession({ expiry: VISIBILITY_CHANGE_TIMEOUT });
1457
- this.triggerFullSnapshot();
1458
1503
  return;
1459
1504
  }
1460
1505
  if (breadcrumb) {
1461
1506
  this.createCustomBreadcrumb(breadcrumb);
1462
1507
  }
1463
- // Otherwise if session is not expired...
1464
- // Update with current timestamp as the last session activity
1465
- // Only updating session on visibility change to be conservative about
1466
- // writing to session storage. This could be changed in the future.
1467
- this.updateLastActivity();
1468
1508
  };
1469
1509
  /**
1470
1510
  * Trigger rrweb to take a full snapshot which will cause this plugin to
@@ -1482,6 +1522,10 @@ var Replay = /** @class */ (function () {
1482
1522
  // This implies that `isEnabled` is false
1483
1523
  return;
1484
1524
  }
1525
+ if (this.isPaused) {
1526
+ // Do not add to event buffer when recording is paused
1527
+ return;
1528
+ }
1485
1529
  // TODO: sadness -- we will want to normalize timestamps to be in ms -
1486
1530
  // requires coordination with frontend
1487
1531
  var isMs = event.timestamp > 9999999999;
@@ -1502,15 +1546,46 @@ var Replay = /** @class */ (function () {
1502
1546
  }
1503
1547
  this.eventBuffer.addEvent(event, isCheckout);
1504
1548
  };
1549
+ /**
1550
+ * Update user activity (across session lifespans)
1551
+ */
1552
+ Replay.prototype.updateUserActivity = function (lastActivity) {
1553
+ if (lastActivity === void 0) { lastActivity = new Date().getTime(); }
1554
+ this.lastActivity = lastActivity;
1555
+ };
1505
1556
  /**
1506
1557
  * Updates the session's last activity timestamp
1507
1558
  */
1508
- Replay.prototype.updateLastActivity = function (lastActivity) {
1559
+ Replay.prototype.updateSessionActivity = function (lastActivity) {
1509
1560
  if (lastActivity === void 0) { lastActivity = new Date().getTime(); }
1510
1561
  if (this.session) {
1511
1562
  this.session.lastActivity = lastActivity;
1512
1563
  }
1513
1564
  };
1565
+ /**
1566
+ * Updates the user activity timestamp and resumes recording. This should be called in an event handler for a user action that we consider as the user being "active" (e.g. a mouse click).
1567
+ *
1568
+ */
1569
+ Replay.prototype.triggerUserActivity = function () {
1570
+ return __awaiter(this, void 0, void 0, function () {
1571
+ return __generator(this, function (_a) {
1572
+ this.updateUserActivity();
1573
+ // This case means that recording was once stopped due to inactivity.
1574
+ // Ensure that recording is resumed.
1575
+ if (!this.stopRecording) {
1576
+ // Create a new session, otherwise when the user action is flushed, it will get rejected due to an expired session.
1577
+ this.loadSession({ expiry: SESSION_IDLE_DURATION });
1578
+ // Note: This will cause a new DOM checkout
1579
+ this.resume();
1580
+ return [2 /*return*/];
1581
+ }
1582
+ // Otherwise... recording was never suspended, continue as normalish
1583
+ this.checkAndHandleExpiredSession();
1584
+ this.updateSessionActivity();
1585
+ return [2 /*return*/];
1586
+ });
1587
+ });
1588
+ };
1514
1589
  /**
1515
1590
  * Helper to create (and buffer) a replay breadcrumb from a core SDK breadcrumb
1516
1591
  */
@@ -1576,19 +1651,31 @@ var Replay = /** @class */ (function () {
1576
1651
  ]);
1577
1652
  };
1578
1653
  /**
1579
- *
1654
+ * Checks if recording should be stopped due to user inactivity. Otherwise
1655
+ * check if session is expired and create a new session if so. Triggers a new
1656
+ * full snapshot on new session.
1580
1657
  *
1581
1658
  * Returns true if session is not expired, false otherwise.
1582
1659
  */
1583
- Replay.prototype.checkAndHandleExpiredSession = function (expiry) {
1584
- var _a, _b;
1585
- if (expiry === void 0) { expiry = SESSION_IDLE_DURATION; }
1586
- var oldSessionId = (_a = this.session) === null || _a === void 0 ? void 0 : _a.id;
1660
+ Replay.prototype.checkAndHandleExpiredSession = function (_a) {
1661
+ var _b, _c;
1662
+ var _d = _a === void 0 ? {} : _a, _e = _d.expiry, expiry = _e === void 0 ? SESSION_IDLE_DURATION : _e;
1663
+ var oldSessionId = (_b = this.session) === null || _b === void 0 ? void 0 : _b.id;
1664
+ // Prevent starting a new session if the last user activity is older than
1665
+ // MAX_SESSION_LIFE. Otherwise non-user activity can trigger a new
1666
+ // session+recording. This creates noisy replays that do not have much
1667
+ // content in them.
1668
+ if (this.lastActivity && isExpired(this.lastActivity, MAX_SESSION_LIFE)) {
1669
+ // Pause recording
1670
+ this.pause();
1671
+ return;
1672
+ }
1673
+ // --- There is recent user activity --- //
1587
1674
  // This will create a new session if expired, based on expiry length
1588
1675
  this.loadSession({ expiry: expiry });
1589
1676
  // Session was expired if session ids do not match
1590
- var isExpired = oldSessionId !== ((_b = this.session) === null || _b === void 0 ? void 0 : _b.id);
1591
- if (!isExpired) {
1677
+ var expired = oldSessionId !== ((_c = this.session) === null || _c === void 0 ? void 0 : _c.id);
1678
+ if (!expired) {
1592
1679
  return true;
1593
1680
  }
1594
1681
  // TODO: We could potentially figure out a way to save the last session,
@@ -1606,7 +1693,7 @@ var Replay = /** @class */ (function () {
1606
1693
  if (this.options.captureOnlyOnError) {
1607
1694
  return;
1608
1695
  }
1609
- return this.flush();
1696
+ this.flushImmediate();
1610
1697
  };
1611
1698
  /**
1612
1699
  * Clear context
@@ -1695,6 +1782,17 @@ var Replay = /** @class */ (function () {
1695
1782
  });
1696
1783
  });
1697
1784
  };
1785
+ /**
1786
+ *
1787
+ * Always flush via `debouncedFlush` so that we do not have flushes triggered
1788
+ * from calling both `flush` and `debouncedFlush`. Otherwise, there could be
1789
+ * cases of mulitple flushes happening closely together.
1790
+ */
1791
+ Replay.prototype.flushImmediate = function () {
1792
+ this.debouncedFlush();
1793
+ // `.flush` is provided by lodash.debounce
1794
+ this.debouncedFlush.flush();
1795
+ };
1698
1796
  /**
1699
1797
  * Send replay attachment using `fetch()`
1700
1798
  */
@@ -1717,7 +1815,7 @@ var Replay = /** @class */ (function () {
1717
1815
  timestamp = new Date().getTime();
1718
1816
  sdkInfo = {
1719
1817
  name: 'sentry.javascript.integration.replay',
1720
- version: "0.6.8",
1818
+ version: "0.6.10-0",
1721
1819
  };
1722
1820
  return [4 /*yield*/, new Promise(function (resolve) {
1723
1821
  var _a;