@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.
@@ -36,10 +36,21 @@ export declare class Replay implements Integration {
36
36
  */
37
37
  private newSessionCreated;
38
38
  private flushLock;
39
+ /**
40
+ * Timestamp of the last user activity. This lives across sessions.
41
+ */
42
+ private lastActivity;
39
43
  /**
40
44
  * Is the integration currently active?
41
45
  */
42
46
  private isEnabled;
47
+ /**
48
+ *
49
+ * Paused is a state where:
50
+ * - DOM Recording is not listening at all
51
+ * - Nothing will be added to event buffer (e.g. core SDK events)
52
+ */
53
+ private isPaused;
43
54
  /**
44
55
  * Have we attached listeners to the core SDK?
45
56
  * Note we have to track this as there is no way to remove instrumentation handlers.
@@ -73,18 +84,27 @@ export declare class Replay implements Integration {
73
84
  */
74
85
  start(): void;
75
86
  /**
76
- * We want to batch uploads of replay events. Save events only if
77
- * `<flushMinDelay>` milliseconds have elapsed since the last event
78
- * *OR* if `<flushMaxDelay>` milliseconds have elapsed.
87
+ * Start recording.
79
88
  *
80
- * Accepts a callback to perform side-effects and returns true to stop batch
81
- * processing and hand back control to caller.
89
+ * Note that this will cause a new DOM checkout
82
90
  */
83
- addUpdate(cb?: AddUpdateCallback): void;
91
+ startRecording(): void;
84
92
  /**
85
93
  * Currently, this needs to be manually called (e.g. for tests). Sentry SDK does not support a teardown
86
94
  */
87
95
  stop(): void;
96
+ /**
97
+ * Pause some replay functionality. See comments for `isPaused`.
98
+ * This differs from stop as this only stops DOM recording, it is
99
+ * not as thorough of a shutdown as `stop()`. */
100
+ pause(): void;
101
+ /**
102
+ * Resumes recording, see notes for `pause().
103
+ *
104
+ * Note that calling `startRecording()` here will cause a
105
+ * new DOM checkout.`
106
+ */
107
+ resume(): void;
88
108
  clearSession(): void;
89
109
  /**
90
110
  * Loads a session from storage, or creates a new one if it does not exist or
@@ -107,6 +127,15 @@ export declare class Replay implements Integration {
107
127
  * Cleans up listeners that were created in `addListeners`
108
128
  */
109
129
  removeListeners(): void;
130
+ /**
131
+ * We want to batch uploads of replay events. Save events only if
132
+ * `<flushMinDelay>` milliseconds have elapsed since the last event
133
+ * *OR* if `<flushMaxDelay>` milliseconds have elapsed.
134
+ *
135
+ * Accepts a callback to perform side-effects and returns true to stop batch
136
+ * processing and hand back control to caller.
137
+ */
138
+ addUpdate(cb?: AddUpdateCallback): void;
110
139
  /**
111
140
  * Core Sentry SDK global event handler. Attaches `replayId` to all [non-replay]
112
141
  * events as a tag. Also handles the case where we only want to capture a reply
@@ -168,10 +197,19 @@ export declare class Replay implements Integration {
168
197
  * Add an event to the event buffer
169
198
  */
170
199
  addEvent(event: RecordingEvent, isCheckout?: boolean): void;
200
+ /**
201
+ * Update user activity (across session lifespans)
202
+ */
203
+ updateUserActivity(lastActivity?: number): void;
171
204
  /**
172
205
  * Updates the session's last activity timestamp
173
206
  */
174
- updateLastActivity(lastActivity?: number): void;
207
+ updateSessionActivity(lastActivity?: number): void;
208
+ /**
209
+ * 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).
210
+ *
211
+ */
212
+ triggerUserActivity(): Promise<void>;
175
213
  /**
176
214
  * Helper to create (and buffer) a replay breadcrumb from a core SDK breadcrumb
177
215
  */
@@ -191,15 +229,19 @@ export declare class Replay implements Integration {
191
229
  */
192
230
  addMemoryEntry(): Promise<void[]> | undefined;
193
231
  /**
194
- *
232
+ * Checks if recording should be stopped due to user inactivity. Otherwise
233
+ * check if session is expired and create a new session if so. Triggers a new
234
+ * full snapshot on new session.
195
235
  *
196
236
  * Returns true if session is not expired, false otherwise.
197
237
  */
198
- checkAndHandleExpiredSession(expiry?: number): boolean;
238
+ checkAndHandleExpiredSession({ expiry, }?: {
239
+ expiry?: number;
240
+ }): boolean | undefined;
199
241
  /**
200
242
  * Only flush if `captureOnlyOnError` is false.
201
243
  */
202
- conditionalFlush(): Promise<void> | undefined;
244
+ conditionalFlush(): void;
203
245
  /**
204
246
  * Clear context
205
247
  */
@@ -224,9 +266,16 @@ export declare class Replay implements Integration {
224
266
  runFlush(): Promise<void>;
225
267
  /**
226
268
  * Flush recording data to Sentry. Creates a lock so that only a single flush
227
- * can be active at a time.
269
+ * can be active at a time. Do not call this directly.
228
270
  */
229
271
  flush: () => Promise<void>;
272
+ /**
273
+ *
274
+ * Always flush via `debouncedFlush` so that we do not have flushes triggered
275
+ * from calling both `flush` and `debouncedFlush`. Otherwise, there could be
276
+ * cases of mulitple flushes happening closely together.
277
+ */
278
+ flushImmediate(): void;
230
279
  /**
231
280
  * Send replay attachment using `fetch()`
232
281
  */
@@ -1,4 +1,4 @@
1
1
  import type { Breadcrumb } from '@sentry/types';
2
2
  declare type RequiredProperties = 'category' | 'message';
3
- export default function createBreadcrumb(breadcrumb: Pick<Breadcrumb, RequiredProperties> & Partial<Omit<Breadcrumb, RequiredProperties>>): Breadcrumb;
3
+ export declare function createBreadcrumb(breadcrumb: Pick<Breadcrumb, RequiredProperties> & Partial<Omit<Breadcrumb, RequiredProperties>>): Breadcrumb;
4
4
  export {};
package/dist/index.es.js CHANGED
@@ -465,8 +465,9 @@ function getSession(_a) {
465
465
  // If session exists and is passed, use it instead of always hitting session storage
466
466
  var session = currentSession || (stickySession && fetchSession());
467
467
  if (session) {
468
- // If there is a session, check if it is valid (e.g. "last activity" time should be within the "session idle time")
469
- // TODO: We should probably set a max age on this as well
468
+ // If there is a session, check if it is valid (e.g. "last activity" time
469
+ // should be within the "session idle time", and "session started" time is
470
+ // within "max session time").
470
471
  var isExpired = isSessionExpired(session, expiry);
471
472
  if (!isExpired) {
472
473
  return { type: 'saved', session: session };
@@ -919,10 +920,21 @@ var Replay = /** @class */ (function () {
919
920
  */
920
921
  this.newSessionCreated = false;
921
922
  this.flushLock = null;
923
+ /**
924
+ * Timestamp of the last user activity. This lives across sessions.
925
+ */
926
+ this.lastActivity = new Date().getTime();
922
927
  /**
923
928
  * Is the integration currently active?
924
929
  */
925
930
  this.isEnabled = false;
931
+ /**
932
+ *
933
+ * Paused is a state where:
934
+ * - DOM Recording is not listening at all
935
+ * - Nothing will be added to event buffer (e.g. core SDK events)
936
+ */
937
+ this.isPaused = false;
926
938
  /**
927
939
  * Have we attached listeners to the core SDK?
928
940
  * Note we have to track this as there is no way to remove instrumentation handlers.
@@ -973,7 +985,7 @@ var Replay = /** @class */ (function () {
973
985
  ) {
974
986
  // TODO: Do we continue to record after?
975
987
  // TODO: What happens if another error happens? Do we record in the same session?
976
- setTimeout(function () { return _this.flush(); });
988
+ setTimeout(function () { return _this.flushImmediate(); });
977
989
  }
978
990
  return event;
979
991
  };
@@ -1078,7 +1090,8 @@ var Replay = /** @class */ (function () {
1078
1090
  if (type === 'history') {
1079
1091
  // Need to collect visited URLs
1080
1092
  _this.context.urls.push(result.name);
1081
- _this.updateLastActivity();
1093
+ _this.updateUserActivity();
1094
+ _this.triggerUserActivity();
1082
1095
  }
1083
1096
  _this.addUpdate(function () {
1084
1097
  _this.createPerformanceSpans([result]);
@@ -1106,7 +1119,10 @@ var Replay = /** @class */ (function () {
1106
1119
  return;
1107
1120
  }
1108
1121
  if (result.category === 'ui.click') {
1109
- _this.updateLastActivity();
1122
+ _this.triggerUserActivity();
1123
+ }
1124
+ else {
1125
+ _this.checkAndHandleExpiredSession();
1110
1126
  }
1111
1127
  _this.addUpdate(function () {
1112
1128
  _this.addEvent({
@@ -1134,7 +1150,7 @@ var Replay = /** @class */ (function () {
1134
1150
  };
1135
1151
  /**
1136
1152
  * Flush recording data to Sentry. Creates a lock so that only a single flush
1137
- * can be active at a time.
1153
+ * can be active at a time. Do not call this directly.
1138
1154
  */
1139
1155
  this.flush = function () { return __awaiter(_this, void 0, void 0, function () {
1140
1156
  var err_1;
@@ -1259,7 +1275,7 @@ var Replay = /** @class */ (function () {
1259
1275
  }
1260
1276
  // setup() is generally called on page load or manually - in both cases we
1261
1277
  // should treat it as an activity
1262
- this.updateLastActivity();
1278
+ this.updateSessionActivity();
1263
1279
  this.eventBuffer = createEventBuffer({
1264
1280
  useCompression: Boolean(this.options.useCompression),
1265
1281
  });
@@ -1267,33 +1283,16 @@ var Replay = /** @class */ (function () {
1267
1283
  // Tag all (non replay) events that get sent to Sentry with the current
1268
1284
  // replay ID so that we can reference them later in the UI
1269
1285
  addGlobalEventProcessor(this.handleGlobalEvent);
1270
- this.stopRecording = record(__assign(__assign({}, this.recordingOptions), { emit: this.handleRecordingEmit }));
1286
+ this.startRecording();
1271
1287
  this.isEnabled = true;
1272
1288
  };
1273
1289
  /**
1274
- * We want to batch uploads of replay events. Save events only if
1275
- * `<flushMinDelay>` milliseconds have elapsed since the last event
1276
- * *OR* if `<flushMaxDelay>` milliseconds have elapsed.
1290
+ * Start recording.
1277
1291
  *
1278
- * Accepts a callback to perform side-effects and returns true to stop batch
1279
- * processing and hand back control to caller.
1292
+ * Note that this will cause a new DOM checkout
1280
1293
  */
1281
- Replay.prototype.addUpdate = function (cb) {
1282
- // We need to always run `cb` (e.g. in the case of captureOnlyOnError == true)
1283
- var cbResult = cb === null || cb === void 0 ? void 0 : cb();
1284
- // If this option is turned on then we will only want to call `flush`
1285
- // explicitly
1286
- if (this.options.captureOnlyOnError) {
1287
- return;
1288
- }
1289
- // If callback is true, we do not want to continue with flushing -- the
1290
- // caller will need to handle it.
1291
- if (cbResult === true) {
1292
- return;
1293
- }
1294
- // addUpdate is called quite frequently - use debouncedFlush so that it
1295
- // respects the flush delays and does not flush immediately
1296
- this.debouncedFlush();
1294
+ Replay.prototype.startRecording = function () {
1295
+ this.stopRecording = record(__assign(__assign({}, this.recordingOptions), { emit: this.handleRecordingEmit }));
1297
1296
  };
1298
1297
  /**
1299
1298
  * Currently, this needs to be manually called (e.g. for tests). Sentry SDK does not support a teardown
@@ -1310,6 +1309,27 @@ var Replay = /** @class */ (function () {
1310
1309
  (_b = this.eventBuffer) === null || _b === void 0 ? void 0 : _b.destroy();
1311
1310
  this.eventBuffer = null;
1312
1311
  };
1312
+ /**
1313
+ * Pause some replay functionality. See comments for `isPaused`.
1314
+ * This differs from stop as this only stops DOM recording, it is
1315
+ * not as thorough of a shutdown as `stop()`. */
1316
+ Replay.prototype.pause = function () {
1317
+ this.isPaused = true;
1318
+ if (this.stopRecording) {
1319
+ this.stopRecording();
1320
+ this.stopRecording = undefined;
1321
+ }
1322
+ };
1323
+ /**
1324
+ * Resumes recording, see notes for `pause().
1325
+ *
1326
+ * Note that calling `startRecording()` here will cause a
1327
+ * new DOM checkout.`
1328
+ */
1329
+ Replay.prototype.resume = function () {
1330
+ this.isPaused = false;
1331
+ this.startRecording();
1332
+ };
1313
1333
  Replay.prototype.clearSession = function () {
1314
1334
  deleteSession();
1315
1335
  this.session = undefined;
@@ -1416,6 +1436,31 @@ var Replay = /** @class */ (function () {
1416
1436
  this.performanceObserver = null;
1417
1437
  }
1418
1438
  };
1439
+ /**
1440
+ * We want to batch uploads of replay events. Save events only if
1441
+ * `<flushMinDelay>` milliseconds have elapsed since the last event
1442
+ * *OR* if `<flushMaxDelay>` milliseconds have elapsed.
1443
+ *
1444
+ * Accepts a callback to perform side-effects and returns true to stop batch
1445
+ * processing and hand back control to caller.
1446
+ */
1447
+ Replay.prototype.addUpdate = function (cb) {
1448
+ // We need to always run `cb` (e.g. in the case of captureOnlyOnError == true)
1449
+ var cbResult = cb === null || cb === void 0 ? void 0 : cb();
1450
+ // If this option is turned on then we will only want to call `flush`
1451
+ // explicitly
1452
+ if (this.options.captureOnlyOnError) {
1453
+ return;
1454
+ }
1455
+ // If callback is true, we do not want to continue with flushing -- the
1456
+ // caller will need to handle it.
1457
+ if (cbResult === true) {
1458
+ return;
1459
+ }
1460
+ // addUpdate is called quite frequently - use debouncedFlush so that it
1461
+ // respects the flush delays and does not flush immediately
1462
+ this.debouncedFlush();
1463
+ };
1419
1464
  /**
1420
1465
  * Tasks to run when we consider a page to be hidden (via blurring and/or visibility)
1421
1466
  */
@@ -1423,8 +1468,8 @@ var Replay = /** @class */ (function () {
1423
1468
  if (!this.session) {
1424
1469
  return;
1425
1470
  }
1426
- var isExpired = isSessionExpired(this.session, VISIBILITY_CHANGE_TIMEOUT);
1427
- if (breadcrumb && !isExpired) {
1471
+ var expired = isSessionExpired(this.session, VISIBILITY_CHANGE_TIMEOUT);
1472
+ if (breadcrumb && !expired) {
1428
1473
  this.createCustomBreadcrumb(breadcrumb);
1429
1474
  }
1430
1475
  // Send replay when the page/tab becomes hidden. There is no reason to send
@@ -1439,24 +1484,19 @@ var Replay = /** @class */ (function () {
1439
1484
  if (!this.session) {
1440
1485
  return;
1441
1486
  }
1442
- var isExpired = isSessionExpired(this.session, VISIBILITY_CHANGE_TIMEOUT);
1443
- if (isExpired) {
1487
+ var isSessionActive = this.checkAndHandleExpiredSession({
1488
+ expiry: VISIBILITY_CHANGE_TIMEOUT,
1489
+ });
1490
+ if (!isSessionActive) {
1444
1491
  // If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
1445
1492
  // ms, we will re-use the existing session, otherwise create a new
1446
1493
  // session
1447
1494
  logger.log('Document has become active, but session has expired');
1448
- this.loadSession({ expiry: VISIBILITY_CHANGE_TIMEOUT });
1449
- this.triggerFullSnapshot();
1450
1495
  return;
1451
1496
  }
1452
1497
  if (breadcrumb) {
1453
1498
  this.createCustomBreadcrumb(breadcrumb);
1454
1499
  }
1455
- // Otherwise if session is not expired...
1456
- // Update with current timestamp as the last session activity
1457
- // Only updating session on visibility change to be conservative about
1458
- // writing to session storage. This could be changed in the future.
1459
- this.updateLastActivity();
1460
1500
  };
1461
1501
  /**
1462
1502
  * Trigger rrweb to take a full snapshot which will cause this plugin to
@@ -1474,6 +1514,10 @@ var Replay = /** @class */ (function () {
1474
1514
  // This implies that `isEnabled` is false
1475
1515
  return;
1476
1516
  }
1517
+ if (this.isPaused) {
1518
+ // Do not add to event buffer when recording is paused
1519
+ return;
1520
+ }
1477
1521
  // TODO: sadness -- we will want to normalize timestamps to be in ms -
1478
1522
  // requires coordination with frontend
1479
1523
  var isMs = event.timestamp > 9999999999;
@@ -1494,15 +1538,46 @@ var Replay = /** @class */ (function () {
1494
1538
  }
1495
1539
  this.eventBuffer.addEvent(event, isCheckout);
1496
1540
  };
1541
+ /**
1542
+ * Update user activity (across session lifespans)
1543
+ */
1544
+ Replay.prototype.updateUserActivity = function (lastActivity) {
1545
+ if (lastActivity === void 0) { lastActivity = new Date().getTime(); }
1546
+ this.lastActivity = lastActivity;
1547
+ };
1497
1548
  /**
1498
1549
  * Updates the session's last activity timestamp
1499
1550
  */
1500
- Replay.prototype.updateLastActivity = function (lastActivity) {
1551
+ Replay.prototype.updateSessionActivity = function (lastActivity) {
1501
1552
  if (lastActivity === void 0) { lastActivity = new Date().getTime(); }
1502
1553
  if (this.session) {
1503
1554
  this.session.lastActivity = lastActivity;
1504
1555
  }
1505
1556
  };
1557
+ /**
1558
+ * 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).
1559
+ *
1560
+ */
1561
+ Replay.prototype.triggerUserActivity = function () {
1562
+ return __awaiter(this, void 0, void 0, function () {
1563
+ return __generator(this, function (_a) {
1564
+ this.updateUserActivity();
1565
+ // This case means that recording was once stopped due to inactivity.
1566
+ // Ensure that recording is resumed.
1567
+ if (!this.stopRecording) {
1568
+ // Create a new session, otherwise when the user action is flushed, it will get rejected due to an expired session.
1569
+ this.loadSession({ expiry: SESSION_IDLE_DURATION });
1570
+ // Note: This will cause a new DOM checkout
1571
+ this.resume();
1572
+ return [2 /*return*/];
1573
+ }
1574
+ // Otherwise... recording was never suspended, continue as normalish
1575
+ this.checkAndHandleExpiredSession();
1576
+ this.updateSessionActivity();
1577
+ return [2 /*return*/];
1578
+ });
1579
+ });
1580
+ };
1506
1581
  /**
1507
1582
  * Helper to create (and buffer) a replay breadcrumb from a core SDK breadcrumb
1508
1583
  */
@@ -1568,19 +1643,31 @@ var Replay = /** @class */ (function () {
1568
1643
  ]);
1569
1644
  };
1570
1645
  /**
1571
- *
1646
+ * Checks if recording should be stopped due to user inactivity. Otherwise
1647
+ * check if session is expired and create a new session if so. Triggers a new
1648
+ * full snapshot on new session.
1572
1649
  *
1573
1650
  * Returns true if session is not expired, false otherwise.
1574
1651
  */
1575
- Replay.prototype.checkAndHandleExpiredSession = function (expiry) {
1576
- var _a, _b;
1577
- if (expiry === void 0) { expiry = SESSION_IDLE_DURATION; }
1578
- var oldSessionId = (_a = this.session) === null || _a === void 0 ? void 0 : _a.id;
1652
+ Replay.prototype.checkAndHandleExpiredSession = function (_a) {
1653
+ var _b, _c;
1654
+ var _d = _a === void 0 ? {} : _a, _e = _d.expiry, expiry = _e === void 0 ? SESSION_IDLE_DURATION : _e;
1655
+ var oldSessionId = (_b = this.session) === null || _b === void 0 ? void 0 : _b.id;
1656
+ // Prevent starting a new session if the last user activity is older than
1657
+ // MAX_SESSION_LIFE. Otherwise non-user activity can trigger a new
1658
+ // session+recording. This creates noisy replays that do not have much
1659
+ // content in them.
1660
+ if (this.lastActivity && isExpired(this.lastActivity, MAX_SESSION_LIFE)) {
1661
+ // Pause recording
1662
+ this.pause();
1663
+ return;
1664
+ }
1665
+ // --- There is recent user activity --- //
1579
1666
  // This will create a new session if expired, based on expiry length
1580
1667
  this.loadSession({ expiry: expiry });
1581
1668
  // Session was expired if session ids do not match
1582
- var isExpired = oldSessionId !== ((_b = this.session) === null || _b === void 0 ? void 0 : _b.id);
1583
- if (!isExpired) {
1669
+ var expired = oldSessionId !== ((_c = this.session) === null || _c === void 0 ? void 0 : _c.id);
1670
+ if (!expired) {
1584
1671
  return true;
1585
1672
  }
1586
1673
  // TODO: We could potentially figure out a way to save the last session,
@@ -1598,7 +1685,7 @@ var Replay = /** @class */ (function () {
1598
1685
  if (this.options.captureOnlyOnError) {
1599
1686
  return;
1600
1687
  }
1601
- return this.flush();
1688
+ this.flushImmediate();
1602
1689
  };
1603
1690
  /**
1604
1691
  * Clear context
@@ -1687,6 +1774,17 @@ var Replay = /** @class */ (function () {
1687
1774
  });
1688
1775
  });
1689
1776
  };
1777
+ /**
1778
+ *
1779
+ * Always flush via `debouncedFlush` so that we do not have flushes triggered
1780
+ * from calling both `flush` and `debouncedFlush`. Otherwise, there could be
1781
+ * cases of mulitple flushes happening closely together.
1782
+ */
1783
+ Replay.prototype.flushImmediate = function () {
1784
+ this.debouncedFlush();
1785
+ // `.flush` is provided by lodash.debounce
1786
+ this.debouncedFlush.flush();
1787
+ };
1690
1788
  /**
1691
1789
  * Send replay attachment using `fetch()`
1692
1790
  */
@@ -1709,7 +1807,7 @@ var Replay = /** @class */ (function () {
1709
1807
  timestamp = new Date().getTime();
1710
1808
  sdkInfo = {
1711
1809
  name: 'sentry.javascript.integration.replay',
1712
- version: "0.6.8",
1810
+ version: "0.6.10-0",
1713
1811
  };
1714
1812
  return [4 /*yield*/, new Promise(function (resolve) {
1715
1813
  var _a;