@sentry/replay 0.5.14 → 0.5.16

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
@@ -4,8 +4,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var core = require('@sentry/core');
6
6
  var utils = require('@sentry/utils');
7
+ var throttle = require('lodash.throttle');
7
8
  var rrweb = require('rrweb');
8
9
 
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ var throttle__default = /*#__PURE__*/_interopDefaultLegacy(throttle);
13
+
9
14
  /******************************************************************************
10
15
  Copyright (c) Microsoft Corporation.
11
16
 
@@ -95,17 +100,17 @@ function __spreadArray(to, from, pack) {
95
100
  var REPLAY_SESSION_KEY = 'sentryReplaySession';
96
101
  var REPLAY_EVENT_NAME = 'replay_event';
97
102
  // The idle limit for a session
98
- var SESSION_IDLE_DURATION = 900000; // 15 minutes
103
+ var SESSION_IDLE_DURATION = 300000; // 5 minutes in ms
99
104
  // Grace period to keep a session when a user changes tabs or hides window
100
105
  var VISIBILITY_CHANGE_TIMEOUT = SESSION_IDLE_DURATION;
101
106
 
102
107
  function captureReplayEvent(_a) {
103
108
  var initialState = _a.initialState, includeReplayStartTimestamp = _a.includeReplayStartTimestamp, errorIds = _a.errorIds, replay_id = _a.replayId, segment_id = _a.segmentId, timestamp = _a.timestamp, traceIds = _a.traceIds, urls = _a.urls;
104
- core.captureEvent(__assign(__assign(__assign({
109
+ core.captureEvent(__assign(__assign({
105
110
  // @ts-expect-error replay_event is a new event type
106
111
  type: REPLAY_EVENT_NAME }, (includeReplayStartTimestamp
107
112
  ? { replay_start_timestamp: initialState.timestamp / 1000 }
108
- : {})), (timestamp ? { timestamp: timestamp / 1000 } : {})), { error_ids: errorIds, trace_ids: traceIds, urls: urls, replay_id: replay_id, segment_id: segment_id }), { event_id: replay_id });
113
+ : {})), { timestamp: timestamp / 1000, error_ids: errorIds, trace_ids: traceIds, urls: urls, replay_id: replay_id, segment_id: segment_id }), { event_id: replay_id });
109
114
  }
110
115
 
111
116
  function createBreadcrumb(breadcrumb) {
@@ -525,7 +530,11 @@ function createPaintEntry(entry) {
525
530
  }
526
531
  function createNavigationEntry(entry) {
527
532
  // TODO: There looks to be some more interesting bits in here (domComplete, domContentLoaded)
528
- var entryType = entry.entryType, name = entry.name, domComplete = entry.domComplete, startTime = entry.startTime, transferSize = entry.transferSize, type = entry.type;
533
+ var entryType = entry.entryType, name = entry.name, duration = entry.duration, domComplete = entry.domComplete, startTime = entry.startTime, transferSize = entry.transferSize, type = entry.type;
534
+ // Ignore entries with no duration, they do not seem to be useful and cause dupes
535
+ if (duration === 0) {
536
+ return null;
537
+ }
529
538
  return {
530
539
  type: "".concat(entryType, ".").concat(type),
531
540
  start: getAbsoluteTime(startTime),
@@ -533,11 +542,12 @@ function createNavigationEntry(entry) {
533
542
  name: name,
534
543
  data: {
535
544
  size: transferSize,
545
+ duration: duration,
536
546
  },
537
547
  };
538
548
  }
539
549
  function createResourceEntry(entry) {
540
- var entryType = entry.entryType, initiatorType = entry.initiatorType, name = entry.name, responseEnd = entry.responseEnd, startTime = entry.startTime, transferSize = entry.transferSize;
550
+ var entryType = entry.entryType, initiatorType = entry.initiatorType, name = entry.name, responseEnd = entry.responseEnd, startTime = entry.startTime, encodedBodySize = entry.encodedBodySize, transferSize = entry.transferSize;
541
551
  // Do not capture fetches to Sentry ingestion endpoint
542
552
  if (isIngestHost(name)) {
543
553
  return null;
@@ -553,6 +563,7 @@ function createResourceEntry(entry) {
553
563
  name: name,
554
564
  data: {
555
565
  size: transferSize,
566
+ encodedBodySize: encodedBodySize,
556
567
  },
557
568
  };
558
569
  }
@@ -565,6 +576,7 @@ function createLargestContentfulPaint(entry) {
565
576
  start: start,
566
577
  end: start + duration,
567
578
  data: {
579
+ duration: duration,
568
580
  size: size,
569
581
  // Not sure why this errors, Node should be correct (Argument of type 'Node' is not assignable to parameter of type 'INode')
570
582
  nodeId: rrweb.record.mirror.getId(entry.element),
@@ -663,7 +675,6 @@ var EventBufferCompressionWorker = /** @class */ (function () {
663
675
  method: 'addEvent',
664
676
  args: [event],
665
677
  });
666
- logger.log('Message posted to worker');
667
678
  // XXX: See note in `get length()`
668
679
  _this.eventBufferItemLength++;
669
680
  return promise;
@@ -812,6 +823,7 @@ var SentryReplay = /** @class */ (function () {
812
823
  * slightly differently in this case: `replay_start_timestamp` is included.
813
824
  */
814
825
  this.newSessionCreated = false;
826
+ this.flushLock = null;
815
827
  /**
816
828
  * Is the integration currently active?
817
829
  */
@@ -925,6 +937,8 @@ var SentryReplay = /** @class */ (function () {
925
937
  var breadcrumb = createBreadcrumb({
926
938
  category: 'ui.blur',
927
939
  });
940
+ // Do not count blur as a user action -- it's part of the process of them
941
+ // leaving the page
928
942
  _this.doChangeToBackgroundTasks(breadcrumb);
929
943
  };
930
944
  /**
@@ -934,6 +948,8 @@ var SentryReplay = /** @class */ (function () {
934
948
  var breadcrumb = createBreadcrumb({
935
949
  category: 'ui.focus',
936
950
  });
951
+ // Do not count focus as a user action -- instead wait until they focus and
952
+ // interactive with page
937
953
  _this.doChangeToForegroundTasks(breadcrumb);
938
954
  };
939
955
  /**
@@ -954,6 +970,7 @@ var SentryReplay = /** @class */ (function () {
954
970
  if (type === 'history') {
955
971
  // Need to collect visited URLs
956
972
  _this.context.urls.push(result.name);
973
+ _this.updateLastActivity();
957
974
  }
958
975
  _this.addUpdate(function () {
959
976
  _this.createPerformanceSpans([result]);
@@ -980,6 +997,9 @@ var SentryReplay = /** @class */ (function () {
980
997
  if (result.category === 'sentry.transaction') {
981
998
  return;
982
999
  }
1000
+ if (result.category === 'ui.click') {
1001
+ _this.updateLastActivity();
1002
+ }
983
1003
  _this.addUpdate(function () {
984
1004
  _this.addEvent({
985
1005
  type: rrweb.EventType.Custom,
@@ -1002,6 +1022,63 @@ var SentryReplay = /** @class */ (function () {
1002
1022
  var newEntries = new Set(list.getEntries());
1003
1023
  _this.performanceEvents = __spreadArray(__spreadArray([], _this.performanceEvents, true), Array.from(newEntries), true);
1004
1024
  };
1025
+ /**
1026
+ * Flushes replay event buffer to Sentry.
1027
+ *
1028
+ * Performance events are only added right before flushing - this is probably
1029
+ * due to the buffered performance observer events.
1030
+ */
1031
+ this.flushUpdate = function () { return __awaiter(_this, void 0, void 0, function () {
1032
+ var err_1;
1033
+ var _a;
1034
+ return __generator(this, function (_b) {
1035
+ switch (_b.label) {
1036
+ case 0:
1037
+ if (!this.isEnabled) {
1038
+ // This is just a precaution, there should be no listeners that would
1039
+ // cause a flush.
1040
+ return [2 /*return*/];
1041
+ }
1042
+ if (!this.checkAndHandleExpiredSession()) {
1043
+ logger.error(new Error('Attempting to finish replay event after session expired.'));
1044
+ return [2 /*return*/];
1045
+ }
1046
+ if (!((_a = this.session) === null || _a === void 0 ? void 0 : _a.id)) {
1047
+ console.error(new Error('[Sentry]: No transaction, no replay'));
1048
+ return [2 /*return*/];
1049
+ }
1050
+ // Since flushing, ensure other queued flushes are cancelled
1051
+ // We do this regardless of having a queued flush since the event updates
1052
+ // will be handled by queued flush
1053
+ clearTimeout(this.timeout);
1054
+ if (!!this.flushLock) return [3 /*break*/, 2];
1055
+ this.flushLock = this.runFlush();
1056
+ return [4 /*yield*/, this.flushLock];
1057
+ case 1:
1058
+ _b.sent();
1059
+ this.flushLock = null;
1060
+ return [2 /*return*/];
1061
+ case 2:
1062
+ _b.trys.push([2, 4, 5, 6]);
1063
+ return [4 /*yield*/, this.flushLock];
1064
+ case 3:
1065
+ _b.sent();
1066
+ return [3 /*break*/, 6];
1067
+ case 4:
1068
+ err_1 = _b.sent();
1069
+ console.error(err_1);
1070
+ return [3 /*break*/, 6];
1071
+ case 5:
1072
+ this.throttledFlushUpdate();
1073
+ return [7 /*endfinally*/];
1074
+ case 6: return [2 /*return*/];
1075
+ }
1076
+ });
1077
+ }); };
1078
+ this.throttledFlushUpdate = throttle__default["default"](this.flushUpdate, 1000, {
1079
+ leading: false,
1080
+ trailing: true,
1081
+ });
1005
1082
  this.recordingOptions = __assign({ maskAllInputs: maskAllInputs, blockClass: blockClass, ignoreClass: ignoreClass, maskTextClass: maskTextClass }, recordingOptions);
1006
1083
  this.options = {
1007
1084
  flushMinDelay: flushMinDelay,
@@ -1047,6 +1124,9 @@ var SentryReplay = /** @class */ (function () {
1047
1124
  // If session was not sampled, then we do not initialize the integration at all.
1048
1125
  return;
1049
1126
  }
1127
+ // setup() is generally called on page load or manually - in both cases we
1128
+ // should treat it as an activity
1129
+ this.updateLastActivity();
1050
1130
  this.eventBuffer = createEventBuffer({
1051
1131
  useCompression: Boolean(this.options.useCompression),
1052
1132
  });
@@ -1155,8 +1235,9 @@ var SentryReplay = /** @class */ (function () {
1155
1235
  var urlPath = "".concat(window.location.pathname).concat(window.location.hash).concat(window.location.search);
1156
1236
  var url = "".concat(window.location.origin).concat(urlPath);
1157
1237
  this.performanceEvents = [];
1238
+ this.initialEventTimestampSinceFlush = null;
1158
1239
  // Reset context as well
1159
- this.popEventContext();
1240
+ this.clearContext();
1160
1241
  this.initialState = {
1161
1242
  timestamp: new Date().getTime(),
1162
1243
  url: url,
@@ -1286,8 +1367,11 @@ var SentryReplay = /** @class */ (function () {
1286
1367
  // requires coordination with frontend
1287
1368
  var isMs = event.timestamp > 9999999999;
1288
1369
  var timestampInMs = isMs ? event.timestamp : event.timestamp * 1000;
1289
- if (!this.context.earliestEvent ||
1290
- timestampInMs < this.context.earliestEvent) {
1370
+ // Only record earliest event if a new session was created, otherwise it
1371
+ // shouldn't be relevant
1372
+ if (this.newSessionCreated &&
1373
+ (!this.context.earliestEvent ||
1374
+ timestampInMs < this.context.earliestEvent)) {
1291
1375
  this.context.earliestEvent = timestampInMs;
1292
1376
  }
1293
1377
  this.eventBuffer.addEvent(event, isCheckout);
@@ -1398,11 +1482,20 @@ var SentryReplay = /** @class */ (function () {
1398
1482
  }
1399
1483
  return this.flushUpdate();
1400
1484
  };
1485
+ /**
1486
+ * Clear context
1487
+ */
1488
+ SentryReplay.prototype.clearContext = function () {
1489
+ this.context.errorIds.clear();
1490
+ this.context.traceIds.clear();
1491
+ this.context.urls = [];
1492
+ this.context.earliestEvent = null;
1493
+ };
1401
1494
  /**
1402
1495
  * Return and clear context
1403
1496
  */
1404
1497
  SentryReplay.prototype.popEventContext = function (_a) {
1405
- var _b = _a === void 0 ? {} : _a, timestamp = _b.timestamp;
1498
+ var timestamp = _a.timestamp;
1406
1499
  var initialState = this.initialState;
1407
1500
  if (this.initialState &&
1408
1501
  this.context.earliestEvent &&
@@ -1416,52 +1509,31 @@ var SentryReplay = /** @class */ (function () {
1416
1509
  traceIds: Array.from(this.context.traceIds).filter(Boolean),
1417
1510
  urls: this.context.urls,
1418
1511
  };
1419
- this.context.errorIds.clear();
1420
- this.context.traceIds.clear();
1421
- this.context.urls = [];
1422
- this.context.earliestEvent = null;
1423
- // @ts-expect-error: Type 'undefined' is not assignable to type 'Session'.ts(2322)
1512
+ this.clearContext();
1424
1513
  return context;
1425
1514
  };
1426
- /**
1427
- * Flushes replay event buffer to Sentry.
1428
- *
1429
- * Performance events are only added right before flushing - this is probably
1430
- * due to the buffered performance observer events.
1431
- */
1432
- SentryReplay.prototype.flushUpdate = function () {
1433
- var _a, _b;
1515
+ SentryReplay.prototype.runFlush = function () {
1516
+ var _a;
1434
1517
  return __awaiter(this, void 0, void 0, function () {
1435
- var timestamp, replayId, newSessionCreated, segmentId, recordingData, err_1;
1436
- return __generator(this, function (_c) {
1437
- switch (_c.label) {
1518
+ var timestamp, replayId, newSessionCreated, segmentId, recordingData, err_2;
1519
+ return __generator(this, function (_b) {
1520
+ switch (_b.label) {
1438
1521
  case 0:
1439
- if (!this.isEnabled) {
1440
- // This is just a precaution, there should be no listeners that would
1441
- // cause a flush.
1442
- return [2 /*return*/];
1443
- }
1444
- if (!this.checkAndHandleExpiredSession()) {
1445
- logger.error(new Error('Attempting to finish replay event after session expired.'));
1446
- return [2 /*return*/];
1447
- }
1448
- if (!((_a = this.session) === null || _a === void 0 ? void 0 : _a.id)) {
1522
+ if (!this.session) {
1449
1523
  console.error(new Error('[Sentry]: No transaction, no replay'));
1450
1524
  return [2 /*return*/];
1451
1525
  }
1452
- // Since already flushing, ensure other queued flushes are cancelled
1453
- clearTimeout(this.timeout);
1454
1526
  return [4 /*yield*/, this.addPerformanceEntries()];
1455
1527
  case 1:
1456
- _c.sent();
1457
- if (!((_b = this.eventBuffer) === null || _b === void 0 ? void 0 : _b.length)) {
1528
+ _b.sent();
1529
+ if (!((_a = this.eventBuffer) === null || _a === void 0 ? void 0 : _a.length)) {
1458
1530
  return [2 /*return*/];
1459
1531
  }
1460
1532
  // Only attach memory event if eventBuffer is not empty
1461
1533
  return [4 /*yield*/, this.addMemoryEntry()];
1462
1534
  case 2:
1463
1535
  // Only attach memory event if eventBuffer is not empty
1464
- _c.sent();
1536
+ _b.sent();
1465
1537
  timestamp = new Date().getTime();
1466
1538
  replayId = this.session.id;
1467
1539
  newSessionCreated = this.newSessionCreated;
@@ -1469,28 +1541,23 @@ var SentryReplay = /** @class */ (function () {
1469
1541
  // Reset this to null regardless of `sendReplay` result so that future
1470
1542
  // events will get flushed properly
1471
1543
  this.initialEventTimestampSinceFlush = null;
1472
- _c.label = 3;
1544
+ _b.label = 3;
1473
1545
  case 3:
1474
- _c.trys.push([3, 6, , 7]);
1546
+ _b.trys.push([3, 6, , 7]);
1475
1547
  return [4 /*yield*/, this.eventBuffer.finish()];
1476
1548
  case 4:
1477
- recordingData = _c.sent();
1549
+ recordingData = _b.sent();
1478
1550
  return [4 /*yield*/, this.sendReplay(replayId, recordingData, segmentId)];
1479
1551
  case 5:
1480
- _c.sent();
1552
+ _b.sent();
1481
1553
  // The below will only happen after successfully sending replay //
1482
- // TBD: Alternatively we could update this after every rrweb event
1483
- // `timestamp` should reflect when the event happens. e.g. the timestamp
1484
- // of the event is passed as an argument in the case where a timeout
1485
- // occurs.
1486
- this.updateLastActivity(timestamp);
1487
1554
  captureReplayEvent(__assign(__assign({}, this.popEventContext({ timestamp: timestamp })), { replayId: replayId, segmentId: segmentId, includeReplayStartTimestamp: newSessionCreated }));
1488
1555
  this.newSessionCreated = false;
1489
1556
  return [3 /*break*/, 7];
1490
1557
  case 6:
1491
- err_1 = _c.sent();
1492
- core.captureException(err_1);
1493
- console.error(err_1);
1558
+ err_2 = _b.sent();
1559
+ core.captureException(err_2);
1560
+ console.error(err_2);
1494
1561
  return [3 /*break*/, 7];
1495
1562
  case 7: return [2 /*return*/];
1496
1563
  }