@sentry/replay 0.5.20 → 0.5.22

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.
Files changed (71) hide show
  1. package/dist/index.es.js +140 -38
  2. package/dist/index.js +139 -38
  3. package/dist/index.js.map +1 -1
  4. package/dist/{config/types/jest.config.d.ts → jest.config.d.ts} +0 -0
  5. package/dist/{config/types/jest.setup.d.ts → jest.setup.d.ts} +0 -0
  6. package/dist/{config/types/src → src}/api/captureReplayEvent.d.ts +0 -0
  7. package/dist/{config/types/src → src}/coreHandlers/getBreadcrumbHandler.d.ts +0 -0
  8. package/dist/{config/types/src → src}/coreHandlers/getSpanHandler.d.ts +0 -0
  9. package/dist/{config/types/src → src}/coreHandlers/handleDom.d.ts +0 -0
  10. package/dist/{config/types/src → src}/coreHandlers/handleFetch.d.ts +0 -0
  11. package/dist/{config/types/src → src}/coreHandlers/handleFetch.test.d.ts +0 -0
  12. package/dist/{config/types/src → src}/coreHandlers/handleHistory.d.ts +0 -0
  13. package/dist/{config/types/src → src}/coreHandlers/handleScope-unit.test.d.ts +0 -0
  14. package/dist/{config/types/src → src}/coreHandlers/handleScope.d.ts +0 -0
  15. package/dist/{config/types/src → src}/coreHandlers/handleScope.test.d.ts +0 -0
  16. package/dist/{config/types/src → src}/coreHandlers/handleXhr.d.ts +0 -0
  17. package/dist/{config/types/src → src}/createPerformanceEntry.d.ts +0 -0
  18. package/dist/{config/types/src → src}/createPerformanceEntry.test.d.ts +0 -0
  19. package/dist/{config/types/src → src}/eventBuffer.d.ts +0 -0
  20. package/dist/{config/types/src → src}/eventBuffer.test.d.ts +0 -0
  21. package/dist/{config/types/src → src}/flags.d.ts +0 -0
  22. package/dist/{config/types/src → src}/flush.test.d.ts +0 -0
  23. package/dist/{config/types/src → src}/index-captureOnlyOnError.test.d.ts +0 -0
  24. package/dist/{config/types/src → src}/index-noSticky.test.d.ts +0 -0
  25. package/dist/{config/types/src → src}/index-sampling.test.d.ts +0 -0
  26. package/dist/{config/types/src → src}/index.d.ts +2 -8
  27. package/dist/{config/types/src → src}/index.test.d.ts +0 -0
  28. package/dist/{config/types/src → src}/session/Session.d.ts +0 -0
  29. package/dist/{config/types/src → src}/session/Session.test.d.ts +0 -0
  30. package/dist/{config/types/src → src}/session/constants.d.ts +0 -0
  31. package/dist/{config/types/src → src}/session/createSession.d.ts +0 -0
  32. package/dist/{config/types/src → src}/session/createSession.test.d.ts +0 -0
  33. package/dist/{config/types/src → src}/session/deleteSession.d.ts +0 -0
  34. package/dist/{config/types/src → src}/session/deleteSession.test.d.ts +0 -0
  35. package/dist/{config/types/src → src}/session/fetchSession.d.ts +0 -0
  36. package/dist/{config/types/src → src}/session/fetchSession.test.d.ts +0 -0
  37. package/dist/{config/types/src → src}/session/getSession.d.ts +0 -0
  38. package/dist/{config/types/src → src}/session/getSession.test.d.ts +0 -0
  39. package/dist/{config/types/src → src}/session/index.d.ts +0 -0
  40. package/dist/{config/types/src → src}/session/saveSession.d.ts +0 -0
  41. package/dist/{config/types/src → src}/session/saveSession.test.d.ts +0 -0
  42. package/dist/{config/types/src → src}/stop.test.d.ts +0 -0
  43. package/dist/{config/types/src → src}/types.d.ts +3 -0
  44. package/dist/src/util/addInternalBreadcrumb.d.ts +5 -0
  45. package/dist/{config/types/src → src}/util/createBreadcrumb.d.ts +0 -0
  46. package/dist/{config/types/src → src}/util/createPayload.d.ts +0 -0
  47. package/dist/{config/types/src/util/dedupeNavigationEntries.d.ts → src/util/dedupePerformanceEntries.d.ts} +1 -1
  48. package/dist/{config/types/src/util/isExpired.test.d.ts → src/util/dedupePerformanceEntries.test.d.ts} +0 -0
  49. package/dist/{config/types/src → src}/util/isExpired.d.ts +0 -0
  50. package/dist/{config/types/src/util/isSampled.test.d.ts → src/util/isExpired.test.d.ts} +0 -0
  51. package/dist/{config/types/src → src}/util/isIngestHost.d.ts +0 -0
  52. package/dist/src/util/isInternal.d.ts +5 -0
  53. package/dist/{config/types/src → src}/util/isSampled.d.ts +0 -0
  54. package/dist/{config/types/src/util/isSessionExpired.test.d.ts → src/util/isSampled.test.d.ts} +0 -0
  55. package/dist/{config/types/src → src}/util/isSessionExpired.d.ts +0 -0
  56. package/dist/src/util/isSessionExpired.test.d.ts +1 -0
  57. package/dist/{config/types/src → src}/util/logger.d.ts +0 -0
  58. package/dist/test/fixtures/performanceEntry/lcp.d.ts +1 -0
  59. package/dist/test/fixtures/performanceEntry/navigation.d.ts +1 -0
  60. package/dist/test/fixtures/performanceEntry/resource.d.ts +1 -0
  61. package/dist/{config/types/test → test}/index.d.ts +0 -0
  62. package/dist/{config/types/test → test}/mocks/mockRrweb.d.ts +0 -0
  63. package/dist/{config/types/test → test}/mocks/mockSdk.d.ts +0 -0
  64. package/dist/types/src/index.d.ts +2 -8
  65. package/dist/types/src/types.d.ts +3 -0
  66. package/dist/types/src/util/addInternalBreadcrumb.d.ts +5 -0
  67. package/dist/types/src/util/dedupePerformanceEntries.d.ts +10 -0
  68. package/dist/types/src/util/isInternal.d.ts +5 -0
  69. package/package.json +1 -2
  70. package/dist/config/types/src/coreHandlers/handleConsole.d.ts +0 -1
  71. package/dist/types/src/coreHandlers/handleConsole.d.ts +0 -1
package/dist/index.es.js CHANGED
@@ -1,7 +1,6 @@
1
- import { captureEvent, getCurrentHub, captureException, addGlobalEventProcessor, setContext, getEnvelopeEndpointWithUrlEncodedAuth } from '@sentry/core';
1
+ import { addBreadcrumb, withScope, captureEvent, getCurrentHub, captureException, addGlobalEventProcessor, setContext, getEnvelopeEndpointWithUrlEncodedAuth } from '@sentry/core';
2
2
  import { htmlTreeAsString, logger as logger$1, uuid4, addInstrumentationHandler, createEnvelope, serializeEnvelope } from '@sentry/utils';
3
3
  import debounce from 'lodash.debounce';
4
- import throttle from 'lodash.throttle';
5
4
  import { record, EventType } from 'rrweb';
6
5
 
7
6
  /******************************************************************************
@@ -97,13 +96,40 @@ var SESSION_IDLE_DURATION = 300000; // 5 minutes in ms
97
96
  // Grace period to keep a session when a user changes tabs or hides window
98
97
  var VISIBILITY_CHANGE_TIMEOUT = SESSION_IDLE_DURATION;
99
98
 
99
+ /**
100
+ * Returns true if we are currently recording an internal to Sentry replay
101
+ * (e.g. on https://sentry.io )
102
+ */
103
+ function isInternal() {
104
+ return ['sentry.io', 'dev.getsentry.net'].includes(window.location.host);
105
+ }
106
+
107
+ /**
108
+ * Wrapper for core SDK's `addBreadcrumb` only when used on `sentry.io`
109
+ */
110
+ function addInternalBreadcrumb(arg) {
111
+ if (!isInternal()) {
112
+ return;
113
+ }
114
+ var category = arg.category, level = arg.level, message = arg.message, rest = __rest(arg, ["category", "level", "message"]);
115
+ addBreadcrumb(__assign({ category: category || 'console', level: level || 'debug', message: "[debug]: ".concat(message) }, rest));
116
+ }
117
+
100
118
  function captureReplayEvent(_a) {
101
119
  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;
102
- captureEvent(__assign(__assign({
103
- // @ts-expect-error replay_event is a new event type
104
- type: REPLAY_EVENT_NAME }, (includeReplayStartTimestamp
105
- ? { replay_start_timestamp: initialState.timestamp / 1000 }
106
- : {})), { timestamp: timestamp / 1000, error_ids: errorIds, trace_ids: traceIds, urls: urls, replay_id: replay_id, segment_id: segment_id }), { event_id: replay_id });
120
+ if (segment_id !== 0 && includeReplayStartTimestamp) {
121
+ addInternalBreadcrumb({
122
+ message: "Including replay_start_timestamp on non-initial segment (id: ".concat(segment_id, ")"),
123
+ });
124
+ }
125
+ withScope(function (scope) {
126
+ scope.setTag('replay_sdk_version', "0.5.22");
127
+ captureEvent(__assign(__assign({
128
+ // @ts-expect-error replay_event is a new event type
129
+ type: REPLAY_EVENT_NAME }, (includeReplayStartTimestamp
130
+ ? { replay_start_timestamp: initialState.timestamp / 1000 }
131
+ : {})), { timestamp: timestamp / 1000, error_ids: errorIds, trace_ids: traceIds, urls: urls, replay_id: replay_id, segment_id: segment_id }), { event_id: replay_id });
132
+ });
107
133
  }
108
134
 
109
135
  function createBreadcrumb(breadcrumb) {
@@ -167,7 +193,7 @@ function isIngestHost(targetHost) {
167
193
  var _b = ((_a = getCurrentHub().getClient()) === null || _a === void 0 ? void 0 : _a.getDsn()) || {}, protocol = _b.protocol, host = _b.host;
168
194
  // XXX: Special case when this integration is used by Sentry on `sentry.io`
169
195
  // We would like to capture network requests made to our ingest endpoints for debugging
170
- if (window.location.host === 'sentry.io') {
196
+ if (isInternal()) {
171
197
  return false;
172
198
  }
173
199
  return targetHost.startsWith("".concat(protocol, "://").concat(host));
@@ -493,6 +519,75 @@ function createPayload(_a) {
493
519
  return payloadWithSequence;
494
520
  }
495
521
 
522
+ var NAVIGATION_ENTRY_KEYS = [
523
+ 'name',
524
+ 'type',
525
+ 'startTime',
526
+ 'transferSize',
527
+ 'duration',
528
+ ];
529
+ function isNavigationEntryEqual(a) {
530
+ return function (b) {
531
+ return NAVIGATION_ENTRY_KEYS.every(function (key) { return a[key] === b[key]; });
532
+ };
533
+ }
534
+ /**
535
+ * There are some difficulties diagnosing why there are duplicate navigation
536
+ * entries. We've witnessed several intermittent results:
537
+ * - duplicate entries have duration = 0
538
+ * - duplicate entries are the same object reference
539
+ * - none of the above
540
+ *
541
+ * Compare the values of several keys to determine if the entries are duplicates or not.
542
+ */
543
+ function dedupePerformanceEntries(currentList, newList) {
544
+ // Partition `currentList` into 3 different lists based on entryType
545
+ var _a = currentList.reduce(function (acc, entry) {
546
+ if (entry.entryType === 'navigation') {
547
+ acc[0].push(entry);
548
+ }
549
+ else if (entry.entryType === 'largest-contentful-paint') {
550
+ acc[1].push(entry);
551
+ }
552
+ else {
553
+ acc[2].push(entry);
554
+ }
555
+ return acc;
556
+ }, [[], [], []]), existingNavigationEntries = _a[0], existingLcpEntries = _a[1], existingEntries = _a[2];
557
+ var newEntries = [];
558
+ var newNavigationEntries = [];
559
+ var newLcpEntry = existingLcpEntries.length
560
+ ? existingLcpEntries[existingLcpEntries.length - 1] // Take the last element as list is sorted
561
+ : undefined;
562
+ newList.forEach(function (entry) {
563
+ if (entry.entryType === 'largest-contentful-paint') {
564
+ // We want the latest LCP event only
565
+ if (!newLcpEntry || newLcpEntry.startTime < entry.startTime) {
566
+ newLcpEntry = entry;
567
+ }
568
+ return;
569
+ }
570
+ if (entry.entryType === 'navigation') {
571
+ var navigationEntry = entry;
572
+ // Check if the navigation entry is contained in currentList or newList
573
+ if (
574
+ // Ignore any navigation entries with duration 0, as they are likely duplicates
575
+ entry.duration > 0 &&
576
+ // Ensure new entry does not already exist in existing entries
577
+ !existingNavigationEntries.find(isNavigationEntryEqual(navigationEntry)) &&
578
+ // Ensure new entry does not already exist in new list of navigation entries
579
+ !newNavigationEntries.find(isNavigationEntryEqual(navigationEntry))) {
580
+ newNavigationEntries.push(navigationEntry);
581
+ }
582
+ // Otherwise this navigation entry is considered a duplicate and is thrown away
583
+ return;
584
+ }
585
+ newEntries.push(entry);
586
+ });
587
+ // Re-combine and sort by startTime
588
+ return __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], (newLcpEntry ? [newLcpEntry] : []), true), existingNavigationEntries, true), existingEntries, true), newEntries, true), newNavigationEntries, true).sort(function (a, b) { return a.startTime - b.startTime; });
589
+ }
590
+
496
591
  var _a;
497
592
  // Map entryType -> function to normalize data for event
498
593
  var ENTRY_TYPES = (_a = {
@@ -847,7 +942,7 @@ var SentryReplay = /** @class */ (function () {
847
942
  * when an error occurs.
848
943
  **/
849
944
  this.handleGlobalEvent = function (event) {
850
- var _a, _b;
945
+ var _a, _b, _c, _d;
851
946
  // Do not apply replayId to the root event
852
947
  if (
853
948
  // @ts-expect-error new event type
@@ -865,6 +960,12 @@ var SentryReplay = /** @class */ (function () {
865
960
  // XXX: Is it safe to assume that all other events are error events?
866
961
  // @ts-expect-error: Type 'undefined' is not assignable to type 'string'.ts(2345)
867
962
  _this.context.errorIds.add(event.event_id);
963
+ if (event.exception) {
964
+ var exc = (_d = (_c = event.exception) === null || _c === void 0 ? void 0 : _c.values) === null || _d === void 0 ? void 0 : _d[0];
965
+ addInternalBreadcrumb({
966
+ message: "Tagging event (".concat(event.event_id, ") - ").concat((exc === null || exc === void 0 ? void 0 : exc.type) || 'Unknown', ": ").concat((exc === null || exc === void 0 ? void 0 : exc.value) || 'n/a'),
967
+ });
968
+ }
868
969
  // Need to be very careful that this does not cause an infinite loop
869
970
  if (_this.options.captureOnlyOnError &&
870
971
  event.exception &&
@@ -1009,6 +1110,8 @@ var SentryReplay = /** @class */ (function () {
1009
1110
  payload: result,
1010
1111
  },
1011
1112
  });
1113
+ // Do not flush after console log messages
1114
+ return result.category === 'console';
1012
1115
  });
1013
1116
  }; };
1014
1117
  /**
@@ -1017,8 +1120,8 @@ var SentryReplay = /** @class */ (function () {
1017
1120
  this.handlePerformanceObserver = function (list) {
1018
1121
  // For whatever reason the observer was returning duplicate navigation
1019
1122
  // entries (the other entry types were not duplicated).
1020
- var newEntries = new Set(list.getEntries());
1021
- _this.performanceEvents = __spreadArray(__spreadArray([], _this.performanceEvents, true), Array.from(newEntries), true);
1123
+ var newPerformanceEntries = dedupePerformanceEntries(_this.performanceEvents, list.getEntries());
1124
+ _this.performanceEvents = newPerformanceEntries;
1022
1125
  };
1023
1126
  /**
1024
1127
  * Flush recording data to Sentry. Creates a lock so that only a single flush
@@ -1043,9 +1146,7 @@ var SentryReplay = /** @class */ (function () {
1043
1146
  console.error(new Error('[Sentry]: No transaction, no replay'));
1044
1147
  return [2 /*return*/];
1045
1148
  }
1046
- // Since flushing, ensure other queued flushes are cancelled
1047
- // We do this regardless of having a queued flush since the event updates
1048
- // will be handled by queued flush
1149
+ // A flush is about to happen, cancel any queued flushes
1049
1150
  (_b = this.debouncedFlush) === null || _b === void 0 ? void 0 : _b.cancel();
1050
1151
  if (!!this.flushLock) return [3 /*break*/, 2];
1051
1152
  this.flushLock = this.runFlush();
@@ -1065,22 +1166,12 @@ var SentryReplay = /** @class */ (function () {
1065
1166
  console.error(err_1);
1066
1167
  return [3 /*break*/, 6];
1067
1168
  case 5:
1068
- this.throttledFlush();
1169
+ this.debouncedFlush();
1069
1170
  return [7 /*endfinally*/];
1070
1171
  case 6: return [2 /*return*/];
1071
1172
  }
1072
1173
  });
1073
1174
  }); };
1074
- /**
1075
- * A throttled `flush()` that is called after a flush is completed and there
1076
- * are other queued flushes.
1077
- *
1078
- * Instead of calling a flush for every <n> queued flush, condense it to a single call
1079
- */
1080
- this.throttledFlush = throttle(this.flush, 1000, {
1081
- leading: false,
1082
- trailing: true,
1083
- });
1084
1175
  this.recordingOptions = __assign({ maskAllInputs: maskAllInputs, blockClass: blockClass, ignoreClass: ignoreClass, maskTextClass: maskTextClass }, recordingOptions);
1085
1176
  this.options = {
1086
1177
  flushMinDelay: flushMinDelay,
@@ -1177,6 +1268,8 @@ var SentryReplay = /** @class */ (function () {
1177
1268
  if (cbResult === true) {
1178
1269
  return;
1179
1270
  }
1271
+ // addUpdate is called quite frequently - use debouncedFlush so that it
1272
+ // respects the flush delays and does not flush immediately
1180
1273
  this.debouncedFlush();
1181
1274
  };
1182
1275
  /**
@@ -1510,50 +1603,56 @@ var SentryReplay = /** @class */ (function () {
1510
1603
  *
1511
1604
  * Performance events are only added right before flushing - this is
1512
1605
  * due to the buffered performance observer events.
1606
+ *
1607
+ * Should never be called directly, only by `flush`
1513
1608
  */
1514
1609
  SentryReplay.prototype.runFlush = function () {
1515
- var _a;
1610
+ var _a, _b, _c;
1516
1611
  return __awaiter(this, void 0, void 0, function () {
1517
- var replayId, newSessionCreated, segmentId, recordingData, err_2;
1518
- return __generator(this, function (_b) {
1519
- switch (_b.label) {
1612
+ var stack, replayId, newSessionCreated, segmentId, recordingData, err_2;
1613
+ return __generator(this, function (_d) {
1614
+ switch (_d.label) {
1520
1615
  case 0:
1616
+ stack = ((_a = new Error('trace').stack) === null || _a === void 0 ? void 0 : _a.split('\n')) || [];
1617
+ addInternalBreadcrumb({
1618
+ message: "runFlush (".concat((_b = this.session) === null || _b === void 0 ? void 0 : _b.segmentId, ")\n\n").concat(stack.slice(1).join('\n')),
1619
+ });
1521
1620
  if (!this.session) {
1522
1621
  console.error(new Error('[Sentry]: No transaction, no replay'));
1523
1622
  return [2 /*return*/];
1524
1623
  }
1525
1624
  return [4 /*yield*/, this.addPerformanceEntries()];
1526
1625
  case 1:
1527
- _b.sent();
1528
- if (!((_a = this.eventBuffer) === null || _a === void 0 ? void 0 : _a.length)) {
1626
+ _d.sent();
1627
+ if (!((_c = this.eventBuffer) === null || _c === void 0 ? void 0 : _c.length)) {
1529
1628
  return [2 /*return*/];
1530
1629
  }
1531
1630
  // Only attach memory event if eventBuffer is not empty
1532
1631
  return [4 /*yield*/, this.addMemoryEntry()];
1533
1632
  case 2:
1534
1633
  // Only attach memory event if eventBuffer is not empty
1535
- _b.sent();
1634
+ _d.sent();
1536
1635
  replayId = this.session.id;
1537
1636
  newSessionCreated = this.newSessionCreated;
1538
1637
  segmentId = this.session.segmentId++;
1539
1638
  // Reset this to null regardless of `sendReplay` result so that future
1540
1639
  // events will get flushed properly
1541
1640
  this.initialEventTimestampSinceFlush = null;
1542
- _b.label = 3;
1641
+ _d.label = 3;
1543
1642
  case 3:
1544
- _b.trys.push([3, 6, , 7]);
1643
+ _d.trys.push([3, 6, , 7]);
1545
1644
  return [4 /*yield*/, this.eventBuffer.finish()];
1546
1645
  case 4:
1547
- recordingData = _b.sent();
1646
+ recordingData = _d.sent();
1548
1647
  return [4 /*yield*/, this.sendReplay(replayId, recordingData, segmentId)];
1549
1648
  case 5:
1550
- _b.sent();
1649
+ _d.sent();
1551
1650
  // The below will only happen after successfully sending replay //
1552
1651
  captureReplayEvent(__assign(__assign({}, this.popEventContext({ timestamp: new Date().getTime() })), { replayId: replayId, segmentId: segmentId, includeReplayStartTimestamp: newSessionCreated }));
1553
1652
  this.newSessionCreated = false;
1554
1653
  return [3 /*break*/, 7];
1555
1654
  case 6:
1556
- err_2 = _b.sent();
1655
+ err_2 = _d.sent();
1557
1656
  captureException(err_2);
1558
1657
  console.error(err_2);
1559
1658
  return [3 /*break*/, 7];
@@ -1582,7 +1681,10 @@ var SentryReplay = /** @class */ (function () {
1582
1681
  envelope = createEnvelope({
1583
1682
  event_id: event_id,
1584
1683
  sent_at: new Date().toISOString(),
1585
- sdk: { name: 'sentry.javascript.integration.replay', version: '1.0.0' },
1684
+ sdk: {
1685
+ name: 'sentry.javascript.integration.replay',
1686
+ version: "0.5.22",
1687
+ },
1586
1688
  }, [
1587
1689
  [
1588
1690
  {
package/dist/index.js CHANGED
@@ -5,13 +5,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var core = require('@sentry/core');
6
6
  var utils = require('@sentry/utils');
7
7
  var debounce = require('lodash.debounce');
8
- var throttle = require('lodash.throttle');
9
8
  var rrweb = require('rrweb');
10
9
 
11
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
11
 
13
12
  var debounce__default = /*#__PURE__*/_interopDefaultLegacy(debounce);
14
- var throttle__default = /*#__PURE__*/_interopDefaultLegacy(throttle);
15
13
 
16
14
  /******************************************************************************
17
15
  Copyright (c) Microsoft Corporation.
@@ -106,13 +104,40 @@ var SESSION_IDLE_DURATION = 300000; // 5 minutes in ms
106
104
  // Grace period to keep a session when a user changes tabs or hides window
107
105
  var VISIBILITY_CHANGE_TIMEOUT = SESSION_IDLE_DURATION;
108
106
 
107
+ /**
108
+ * Returns true if we are currently recording an internal to Sentry replay
109
+ * (e.g. on https://sentry.io )
110
+ */
111
+ function isInternal() {
112
+ return ['sentry.io', 'dev.getsentry.net'].includes(window.location.host);
113
+ }
114
+
115
+ /**
116
+ * Wrapper for core SDK's `addBreadcrumb` only when used on `sentry.io`
117
+ */
118
+ function addInternalBreadcrumb(arg) {
119
+ if (!isInternal()) {
120
+ return;
121
+ }
122
+ var category = arg.category, level = arg.level, message = arg.message, rest = __rest(arg, ["category", "level", "message"]);
123
+ core.addBreadcrumb(__assign({ category: category || 'console', level: level || 'debug', message: "[debug]: ".concat(message) }, rest));
124
+ }
125
+
109
126
  function captureReplayEvent(_a) {
110
127
  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;
111
- core.captureEvent(__assign(__assign({
112
- // @ts-expect-error replay_event is a new event type
113
- type: REPLAY_EVENT_NAME }, (includeReplayStartTimestamp
114
- ? { replay_start_timestamp: initialState.timestamp / 1000 }
115
- : {})), { timestamp: timestamp / 1000, error_ids: errorIds, trace_ids: traceIds, urls: urls, replay_id: replay_id, segment_id: segment_id }), { event_id: replay_id });
128
+ if (segment_id !== 0 && includeReplayStartTimestamp) {
129
+ addInternalBreadcrumb({
130
+ message: "Including replay_start_timestamp on non-initial segment (id: ".concat(segment_id, ")"),
131
+ });
132
+ }
133
+ core.withScope(function (scope) {
134
+ scope.setTag('replay_sdk_version', "0.5.22");
135
+ core.captureEvent(__assign(__assign({
136
+ // @ts-expect-error replay_event is a new event type
137
+ type: REPLAY_EVENT_NAME }, (includeReplayStartTimestamp
138
+ ? { replay_start_timestamp: initialState.timestamp / 1000 }
139
+ : {})), { timestamp: timestamp / 1000, error_ids: errorIds, trace_ids: traceIds, urls: urls, replay_id: replay_id, segment_id: segment_id }), { event_id: replay_id });
140
+ });
116
141
  }
117
142
 
118
143
  function createBreadcrumb(breadcrumb) {
@@ -176,7 +201,7 @@ function isIngestHost(targetHost) {
176
201
  var _b = ((_a = core.getCurrentHub().getClient()) === null || _a === void 0 ? void 0 : _a.getDsn()) || {}, protocol = _b.protocol, host = _b.host;
177
202
  // XXX: Special case when this integration is used by Sentry on `sentry.io`
178
203
  // We would like to capture network requests made to our ingest endpoints for debugging
179
- if (window.location.host === 'sentry.io') {
204
+ if (isInternal()) {
180
205
  return false;
181
206
  }
182
207
  return targetHost.startsWith("".concat(protocol, "://").concat(host));
@@ -502,6 +527,75 @@ function createPayload(_a) {
502
527
  return payloadWithSequence;
503
528
  }
504
529
 
530
+ var NAVIGATION_ENTRY_KEYS = [
531
+ 'name',
532
+ 'type',
533
+ 'startTime',
534
+ 'transferSize',
535
+ 'duration',
536
+ ];
537
+ function isNavigationEntryEqual(a) {
538
+ return function (b) {
539
+ return NAVIGATION_ENTRY_KEYS.every(function (key) { return a[key] === b[key]; });
540
+ };
541
+ }
542
+ /**
543
+ * There are some difficulties diagnosing why there are duplicate navigation
544
+ * entries. We've witnessed several intermittent results:
545
+ * - duplicate entries have duration = 0
546
+ * - duplicate entries are the same object reference
547
+ * - none of the above
548
+ *
549
+ * Compare the values of several keys to determine if the entries are duplicates or not.
550
+ */
551
+ function dedupePerformanceEntries(currentList, newList) {
552
+ // Partition `currentList` into 3 different lists based on entryType
553
+ var _a = currentList.reduce(function (acc, entry) {
554
+ if (entry.entryType === 'navigation') {
555
+ acc[0].push(entry);
556
+ }
557
+ else if (entry.entryType === 'largest-contentful-paint') {
558
+ acc[1].push(entry);
559
+ }
560
+ else {
561
+ acc[2].push(entry);
562
+ }
563
+ return acc;
564
+ }, [[], [], []]), existingNavigationEntries = _a[0], existingLcpEntries = _a[1], existingEntries = _a[2];
565
+ var newEntries = [];
566
+ var newNavigationEntries = [];
567
+ var newLcpEntry = existingLcpEntries.length
568
+ ? existingLcpEntries[existingLcpEntries.length - 1] // Take the last element as list is sorted
569
+ : undefined;
570
+ newList.forEach(function (entry) {
571
+ if (entry.entryType === 'largest-contentful-paint') {
572
+ // We want the latest LCP event only
573
+ if (!newLcpEntry || newLcpEntry.startTime < entry.startTime) {
574
+ newLcpEntry = entry;
575
+ }
576
+ return;
577
+ }
578
+ if (entry.entryType === 'navigation') {
579
+ var navigationEntry = entry;
580
+ // Check if the navigation entry is contained in currentList or newList
581
+ if (
582
+ // Ignore any navigation entries with duration 0, as they are likely duplicates
583
+ entry.duration > 0 &&
584
+ // Ensure new entry does not already exist in existing entries
585
+ !existingNavigationEntries.find(isNavigationEntryEqual(navigationEntry)) &&
586
+ // Ensure new entry does not already exist in new list of navigation entries
587
+ !newNavigationEntries.find(isNavigationEntryEqual(navigationEntry))) {
588
+ newNavigationEntries.push(navigationEntry);
589
+ }
590
+ // Otherwise this navigation entry is considered a duplicate and is thrown away
591
+ return;
592
+ }
593
+ newEntries.push(entry);
594
+ });
595
+ // Re-combine and sort by startTime
596
+ return __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], (newLcpEntry ? [newLcpEntry] : []), true), existingNavigationEntries, true), existingEntries, true), newEntries, true), newNavigationEntries, true).sort(function (a, b) { return a.startTime - b.startTime; });
597
+ }
598
+
505
599
  var _a;
506
600
  // Map entryType -> function to normalize data for event
507
601
  var ENTRY_TYPES = (_a = {
@@ -856,7 +950,7 @@ var SentryReplay = /** @class */ (function () {
856
950
  * when an error occurs.
857
951
  **/
858
952
  this.handleGlobalEvent = function (event) {
859
- var _a, _b;
953
+ var _a, _b, _c, _d;
860
954
  // Do not apply replayId to the root event
861
955
  if (
862
956
  // @ts-expect-error new event type
@@ -874,6 +968,12 @@ var SentryReplay = /** @class */ (function () {
874
968
  // XXX: Is it safe to assume that all other events are error events?
875
969
  // @ts-expect-error: Type 'undefined' is not assignable to type 'string'.ts(2345)
876
970
  _this.context.errorIds.add(event.event_id);
971
+ if (event.exception) {
972
+ var exc = (_d = (_c = event.exception) === null || _c === void 0 ? void 0 : _c.values) === null || _d === void 0 ? void 0 : _d[0];
973
+ addInternalBreadcrumb({
974
+ message: "Tagging event (".concat(event.event_id, ") - ").concat((exc === null || exc === void 0 ? void 0 : exc.type) || 'Unknown', ": ").concat((exc === null || exc === void 0 ? void 0 : exc.value) || 'n/a'),
975
+ });
976
+ }
877
977
  // Need to be very careful that this does not cause an infinite loop
878
978
  if (_this.options.captureOnlyOnError &&
879
979
  event.exception &&
@@ -1018,6 +1118,8 @@ var SentryReplay = /** @class */ (function () {
1018
1118
  payload: result,
1019
1119
  },
1020
1120
  });
1121
+ // Do not flush after console log messages
1122
+ return result.category === 'console';
1021
1123
  });
1022
1124
  }; };
1023
1125
  /**
@@ -1026,8 +1128,8 @@ var SentryReplay = /** @class */ (function () {
1026
1128
  this.handlePerformanceObserver = function (list) {
1027
1129
  // For whatever reason the observer was returning duplicate navigation
1028
1130
  // entries (the other entry types were not duplicated).
1029
- var newEntries = new Set(list.getEntries());
1030
- _this.performanceEvents = __spreadArray(__spreadArray([], _this.performanceEvents, true), Array.from(newEntries), true);
1131
+ var newPerformanceEntries = dedupePerformanceEntries(_this.performanceEvents, list.getEntries());
1132
+ _this.performanceEvents = newPerformanceEntries;
1031
1133
  };
1032
1134
  /**
1033
1135
  * Flush recording data to Sentry. Creates a lock so that only a single flush
@@ -1052,9 +1154,7 @@ var SentryReplay = /** @class */ (function () {
1052
1154
  console.error(new Error('[Sentry]: No transaction, no replay'));
1053
1155
  return [2 /*return*/];
1054
1156
  }
1055
- // Since flushing, ensure other queued flushes are cancelled
1056
- // We do this regardless of having a queued flush since the event updates
1057
- // will be handled by queued flush
1157
+ // A flush is about to happen, cancel any queued flushes
1058
1158
  (_b = this.debouncedFlush) === null || _b === void 0 ? void 0 : _b.cancel();
1059
1159
  if (!!this.flushLock) return [3 /*break*/, 2];
1060
1160
  this.flushLock = this.runFlush();
@@ -1074,22 +1174,12 @@ var SentryReplay = /** @class */ (function () {
1074
1174
  console.error(err_1);
1075
1175
  return [3 /*break*/, 6];
1076
1176
  case 5:
1077
- this.throttledFlush();
1177
+ this.debouncedFlush();
1078
1178
  return [7 /*endfinally*/];
1079
1179
  case 6: return [2 /*return*/];
1080
1180
  }
1081
1181
  });
1082
1182
  }); };
1083
- /**
1084
- * A throttled `flush()` that is called after a flush is completed and there
1085
- * are other queued flushes.
1086
- *
1087
- * Instead of calling a flush for every <n> queued flush, condense it to a single call
1088
- */
1089
- this.throttledFlush = throttle__default["default"](this.flush, 1000, {
1090
- leading: false,
1091
- trailing: true,
1092
- });
1093
1183
  this.recordingOptions = __assign({ maskAllInputs: maskAllInputs, blockClass: blockClass, ignoreClass: ignoreClass, maskTextClass: maskTextClass }, recordingOptions);
1094
1184
  this.options = {
1095
1185
  flushMinDelay: flushMinDelay,
@@ -1186,6 +1276,8 @@ var SentryReplay = /** @class */ (function () {
1186
1276
  if (cbResult === true) {
1187
1277
  return;
1188
1278
  }
1279
+ // addUpdate is called quite frequently - use debouncedFlush so that it
1280
+ // respects the flush delays and does not flush immediately
1189
1281
  this.debouncedFlush();
1190
1282
  };
1191
1283
  /**
@@ -1519,50 +1611,56 @@ var SentryReplay = /** @class */ (function () {
1519
1611
  *
1520
1612
  * Performance events are only added right before flushing - this is
1521
1613
  * due to the buffered performance observer events.
1614
+ *
1615
+ * Should never be called directly, only by `flush`
1522
1616
  */
1523
1617
  SentryReplay.prototype.runFlush = function () {
1524
- var _a;
1618
+ var _a, _b, _c;
1525
1619
  return __awaiter(this, void 0, void 0, function () {
1526
- var replayId, newSessionCreated, segmentId, recordingData, err_2;
1527
- return __generator(this, function (_b) {
1528
- switch (_b.label) {
1620
+ var stack, replayId, newSessionCreated, segmentId, recordingData, err_2;
1621
+ return __generator(this, function (_d) {
1622
+ switch (_d.label) {
1529
1623
  case 0:
1624
+ stack = ((_a = new Error('trace').stack) === null || _a === void 0 ? void 0 : _a.split('\n')) || [];
1625
+ addInternalBreadcrumb({
1626
+ message: "runFlush (".concat((_b = this.session) === null || _b === void 0 ? void 0 : _b.segmentId, ")\n\n").concat(stack.slice(1).join('\n')),
1627
+ });
1530
1628
  if (!this.session) {
1531
1629
  console.error(new Error('[Sentry]: No transaction, no replay'));
1532
1630
  return [2 /*return*/];
1533
1631
  }
1534
1632
  return [4 /*yield*/, this.addPerformanceEntries()];
1535
1633
  case 1:
1536
- _b.sent();
1537
- if (!((_a = this.eventBuffer) === null || _a === void 0 ? void 0 : _a.length)) {
1634
+ _d.sent();
1635
+ if (!((_c = this.eventBuffer) === null || _c === void 0 ? void 0 : _c.length)) {
1538
1636
  return [2 /*return*/];
1539
1637
  }
1540
1638
  // Only attach memory event if eventBuffer is not empty
1541
1639
  return [4 /*yield*/, this.addMemoryEntry()];
1542
1640
  case 2:
1543
1641
  // Only attach memory event if eventBuffer is not empty
1544
- _b.sent();
1642
+ _d.sent();
1545
1643
  replayId = this.session.id;
1546
1644
  newSessionCreated = this.newSessionCreated;
1547
1645
  segmentId = this.session.segmentId++;
1548
1646
  // Reset this to null regardless of `sendReplay` result so that future
1549
1647
  // events will get flushed properly
1550
1648
  this.initialEventTimestampSinceFlush = null;
1551
- _b.label = 3;
1649
+ _d.label = 3;
1552
1650
  case 3:
1553
- _b.trys.push([3, 6, , 7]);
1651
+ _d.trys.push([3, 6, , 7]);
1554
1652
  return [4 /*yield*/, this.eventBuffer.finish()];
1555
1653
  case 4:
1556
- recordingData = _b.sent();
1654
+ recordingData = _d.sent();
1557
1655
  return [4 /*yield*/, this.sendReplay(replayId, recordingData, segmentId)];
1558
1656
  case 5:
1559
- _b.sent();
1657
+ _d.sent();
1560
1658
  // The below will only happen after successfully sending replay //
1561
1659
  captureReplayEvent(__assign(__assign({}, this.popEventContext({ timestamp: new Date().getTime() })), { replayId: replayId, segmentId: segmentId, includeReplayStartTimestamp: newSessionCreated }));
1562
1660
  this.newSessionCreated = false;
1563
1661
  return [3 /*break*/, 7];
1564
1662
  case 6:
1565
- err_2 = _b.sent();
1663
+ err_2 = _d.sent();
1566
1664
  core.captureException(err_2);
1567
1665
  console.error(err_2);
1568
1666
  return [3 /*break*/, 7];
@@ -1591,7 +1689,10 @@ var SentryReplay = /** @class */ (function () {
1591
1689
  envelope = utils.createEnvelope({
1592
1690
  event_id: event_id,
1593
1691
  sent_at: new Date().toISOString(),
1594
- sdk: { name: 'sentry.javascript.integration.replay', version: '1.0.0' },
1692
+ sdk: {
1693
+ name: 'sentry.javascript.integration.replay',
1694
+ version: "0.5.22",
1695
+ },
1595
1696
  }, [
1596
1697
  [
1597
1698
  {