@sentry/replay 0.6.3 → 0.6.4

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
@@ -97,49 +97,6 @@ function __spreadArray(to, from, pack) {
97
97
  return to.concat(ar || Array.prototype.slice.call(from));
98
98
  }
99
99
 
100
- var REPLAY_SESSION_KEY = 'sentryReplaySession';
101
- var REPLAY_EVENT_NAME = 'replay_event';
102
- // The idle limit for a session
103
- var SESSION_IDLE_DURATION = 300000; // 5 minutes in ms
104
- // Grace period to keep a session when a user changes tabs or hides window
105
- var VISIBILITY_CHANGE_TIMEOUT = SESSION_IDLE_DURATION;
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
-
126
- function captureReplayEvent(_a) {
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;
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.6.3");
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
- });
141
- }
142
-
143
100
  function createBreadcrumb(breadcrumb) {
144
101
  return __assign({ timestamp: new Date().getTime() / 1000, type: 'default' }, breadcrumb);
145
102
  }
@@ -193,6 +150,14 @@ function getBreadcrumbHandler(type) {
193
150
  return handleDom;
194
151
  }
195
152
 
153
+ /**
154
+ * Returns true if we are currently recording an internal to Sentry replay
155
+ * (e.g. on https://sentry.io )
156
+ */
157
+ function isInternal() {
158
+ return ['sentry.io', 'dev.getsentry.net'].includes(window.location.host);
159
+ }
160
+
196
161
  /**
197
162
  * Checks is `targetHost` is a Sentry ingestion host
198
163
  */
@@ -285,6 +250,13 @@ function getSpanHandler(type) {
285
250
  return handleHistory;
286
251
  }
287
252
 
253
+ var REPLAY_SESSION_KEY = 'sentryReplaySession';
254
+ var REPLAY_EVENT_NAME = 'replay_event';
255
+ // The idle limit for a session
256
+ var SESSION_IDLE_DURATION = 300000; // 5 minutes in ms
257
+ // Grace period to keep a session when a user changes tabs or hides window
258
+ var VISIBILITY_CHANGE_TIMEOUT = SESSION_IDLE_DURATION;
259
+
288
260
  /**
289
261
  * Deletes a session from storage
290
262
  */
@@ -507,6 +479,17 @@ function getSession(_a) {
507
479
  return { type: 'new', session: newSession };
508
480
  }
509
481
 
482
+ /**
483
+ * Wrapper for core SDK's `addBreadcrumb` only when used on `sentry.io`
484
+ */
485
+ function addInternalBreadcrumb(arg) {
486
+ if (!isInternal()) {
487
+ return;
488
+ }
489
+ var category = arg.category, level = arg.level, message = arg.message, rest = __rest(arg, ["category", "level", "message"]);
490
+ core.addBreadcrumb(__assign({ category: category || 'console', level: level || 'debug', message: "[debug]: ".concat(message) }, rest));
491
+ }
492
+
510
493
  /**
511
494
  * Captures exceptions to Sentry only when it occurs on sentry.io
512
495
  */
@@ -911,6 +894,7 @@ var BASE_RETRY_INTERVAL = 5000;
911
894
  var MAX_RETRY_COUNT = 3;
912
895
  var UNABLE_TO_SEND_REPLAY = 'Unable to send Replay';
913
896
  var MEDIA_SELECTORS = 'img,image,svg,path,rect,area,video,object,picture,embed,map,audio';
897
+ var _initialized = false;
914
898
  var Replay = /** @class */ (function () {
915
899
  function Replay(_a) {
916
900
  if (_a === void 0) { _a = {}; }
@@ -1230,6 +1214,12 @@ var Replay = /** @class */ (function () {
1230
1214
  this.debouncedFlush = debounce__default["default"](function () { return _this.flush(); }, this.options.flushMinDelay, {
1231
1215
  maxWait: this.options.flushMaxDelay,
1232
1216
  });
1217
+ if (_initialized) {
1218
+ var error = new Error('Multiple Sentry Session Replay instances are not supported');
1219
+ captureInternalException(error);
1220
+ throw error;
1221
+ }
1222
+ _initialized = true;
1233
1223
  }
1234
1224
  /**
1235
1225
  * Because we create a transaction in `setupOnce`, we can potentially create a
@@ -1254,7 +1244,8 @@ var Replay = /** @class */ (function () {
1254
1244
  this.loadSession({ expiry: SESSION_IDLE_DURATION });
1255
1245
  // If there is no session, then something bad has happened - can't continue
1256
1246
  if (!this.session) {
1257
- throw new Error('Invalid session');
1247
+ captureInternalException(new Error('Invalid session'));
1248
+ return;
1258
1249
  }
1259
1250
  if (!this.session.sampled) {
1260
1251
  // If session was not sampled, then we do not initialize the integration at all.
@@ -1605,8 +1596,7 @@ var Replay = /** @class */ (function () {
1605
1596
  /**
1606
1597
  * Return and clear context
1607
1598
  */
1608
- Replay.prototype.popEventContext = function (_a) {
1609
- var timestamp = _a.timestamp;
1599
+ Replay.prototype.popEventContext = function () {
1610
1600
  var initialState = this.initialState;
1611
1601
  if (this.initialState &&
1612
1602
  this.context.earliestEvent &&
@@ -1615,7 +1605,6 @@ var Replay = /** @class */ (function () {
1615
1605
  }
1616
1606
  var context = {
1617
1607
  initialState: initialState,
1618
- timestamp: timestamp,
1619
1608
  errorIds: Array.from(this.context.errorIds).filter(Boolean),
1620
1609
  traceIds: Array.from(this.context.traceIds).filter(Boolean),
1621
1610
  urls: this.context.urls,
@@ -1666,11 +1655,14 @@ var Replay = /** @class */ (function () {
1666
1655
  return [4 /*yield*/, this.eventBuffer.finish()];
1667
1656
  case 4:
1668
1657
  recordingData = _d.sent();
1669
- return [4 /*yield*/, this.sendReplay(replayId, recordingData, segmentId)];
1658
+ return [4 /*yield*/, this.sendReplay({
1659
+ replayId: replayId,
1660
+ events: recordingData,
1661
+ segmentId: segmentId,
1662
+ includeReplayStartTimestamp: newSessionCreated,
1663
+ })];
1670
1664
  case 5:
1671
1665
  _d.sent();
1672
- // The below will only happen after successfully sending replay //
1673
- captureReplayEvent(__assign(__assign({}, this.popEventContext({ timestamp: new Date().getTime() })), { replayId: replayId, segmentId: segmentId, includeReplayStartTimestamp: newSessionCreated }));
1674
1666
  this.newSessionCreated = false;
1675
1667
  return [3 /*break*/, 7];
1676
1668
  case 6:
@@ -1687,12 +1679,13 @@ var Replay = /** @class */ (function () {
1687
1679
  * Send replay attachment using `fetch()`
1688
1680
  */
1689
1681
  Replay.prototype.sendReplayRequest = function (_a) {
1690
- var endpoint = _a.endpoint, events = _a.events, event_id = _a.replayId, segment_id = _a.segmentId;
1682
+ var endpoint = _a.endpoint, events = _a.events, event_id = _a.replayId, segment_id = _a.segmentId, includeReplayStartTimestamp = _a.includeReplayStartTimestamp;
1691
1683
  return __awaiter(this, void 0, void 0, function () {
1692
- var payloadWithSequence, envelope, response, _b, _c;
1693
- var _d;
1694
- return __generator(this, function (_e) {
1695
- switch (_e.label) {
1684
+ var payloadWithSequence, _b, urls, errorIds, traceIds, initialState, timestamp, sdkInfo, replayEvent, envelope, response, _c, _d;
1685
+ var _e;
1686
+ var _this = this;
1687
+ return __generator(this, function (_f) {
1688
+ switch (_f.label) {
1696
1689
  case 0:
1697
1690
  payloadWithSequence = createPayload({
1698
1691
  events: events,
@@ -1700,14 +1693,43 @@ var Replay = /** @class */ (function () {
1700
1693
  segment_id: segment_id,
1701
1694
  },
1702
1695
  });
1696
+ _b = this.popEventContext(), urls = _b.urls, errorIds = _b.errorIds, traceIds = _b.traceIds, initialState = _b.initialState;
1697
+ timestamp = new Date().getTime();
1698
+ sdkInfo = {
1699
+ name: 'sentry.javascript.integration.replay',
1700
+ version: "0.6.4",
1701
+ };
1702
+ return [4 /*yield*/, new Promise(function (resolve) {
1703
+ var _a;
1704
+ (_a = core.getCurrentHub()) === null || _a === void 0 ? void 0 : _a._withClient(function (client, scope) { return __awaiter(_this, void 0, void 0, function () {
1705
+ var preparedEvent, session;
1706
+ return __generator(this, function (_a) {
1707
+ switch (_a.label) {
1708
+ case 0: return [4 /*yield*/, client._prepareEvent(__assign(__assign({ type: REPLAY_EVENT_NAME }, (includeReplayStartTimestamp
1709
+ ? { replay_start_timestamp: initialState.timestamp / 1000 }
1710
+ : {})), { timestamp: timestamp / 1000, error_ids: errorIds, trace_ids: traceIds, urls: urls, replay_id: event_id, segment_id: segment_id }), { event_id: event_id }, scope)];
1711
+ case 1:
1712
+ preparedEvent = _a.sent();
1713
+ session = scope && scope.getSession();
1714
+ if (session) {
1715
+ client._updateSessionFromEvent(session, preparedEvent);
1716
+ }
1717
+ preparedEvent.sdk = __assign(__assign({}, preparedEvent.sdk), sdkInfo);
1718
+ resolve(preparedEvent);
1719
+ return [2 /*return*/];
1720
+ }
1721
+ });
1722
+ }); });
1723
+ })];
1724
+ case 1:
1725
+ replayEvent = _f.sent();
1703
1726
  envelope = utils.createEnvelope({
1704
1727
  event_id: event_id,
1705
1728
  sent_at: new Date().toISOString(),
1706
- sdk: {
1707
- name: 'sentry.javascript.integration.replay',
1708
- version: "0.6.3",
1709
- },
1729
+ sdk: sdkInfo,
1710
1730
  }, [
1731
+ // @ts-expect-error New types
1732
+ [{ type: 'replay_event' }, replayEvent],
1711
1733
  [
1712
1734
  {
1713
1735
  // @ts-expect-error setting envelope
@@ -1724,20 +1746,20 @@ var Replay = /** @class */ (function () {
1724
1746
  method: 'POST',
1725
1747
  body: utils.serializeEnvelope(envelope),
1726
1748
  })];
1727
- case 1:
1728
- response = _e.sent();
1729
- if (!(response.status !== 200)) return [3 /*break*/, 3];
1730
- _b = core.setContext;
1731
- _c = ['Send Replay Response'];
1732
- _d = {
1749
+ case 2:
1750
+ response = _f.sent();
1751
+ if (!(response.status !== 200)) return [3 /*break*/, 4];
1752
+ _c = core.setContext;
1753
+ _d = ['Send Replay Response'];
1754
+ _e = {
1733
1755
  status: response.status
1734
1756
  };
1735
1757
  return [4 /*yield*/, response.text()];
1736
- case 2:
1737
- _b.apply(void 0, _c.concat([(_d.body = _e.sent(),
1738
- _d)]));
1758
+ case 3:
1759
+ _c.apply(void 0, _d.concat([(_e.body = _f.sent(),
1760
+ _e)]));
1739
1761
  throw new Error(UNABLE_TO_SEND_REPLAY);
1740
- case 3: return [2 /*return*/];
1762
+ case 4: return [2 /*return*/];
1741
1763
  }
1742
1764
  });
1743
1765
  });
@@ -1749,12 +1771,13 @@ var Replay = /** @class */ (function () {
1749
1771
  /**
1750
1772
  * Finalize and send the current replay event to Sentry
1751
1773
  */
1752
- Replay.prototype.sendReplay = function (replayId, events, segmentId) {
1774
+ Replay.prototype.sendReplay = function (_a) {
1775
+ var replayId = _a.replayId, events = _a.events, segmentId = _a.segmentId, includeReplayStartTimestamp = _a.includeReplayStartTimestamp;
1753
1776
  return __awaiter(this, void 0, void 0, function () {
1754
1777
  var client, endpoint, ex_1;
1755
1778
  var _this = this;
1756
- return __generator(this, function (_a) {
1757
- switch (_a.label) {
1779
+ return __generator(this, function (_b) {
1780
+ switch (_b.label) {
1758
1781
  case 0:
1759
1782
  // short circuit if there's no events to upload
1760
1783
  if (!events.length) {
@@ -1762,21 +1785,22 @@ var Replay = /** @class */ (function () {
1762
1785
  }
1763
1786
  client = core.getCurrentHub().getClient();
1764
1787
  endpoint = core.getEnvelopeEndpointWithUrlEncodedAuth(client.getDsn());
1765
- _a.label = 1;
1788
+ _b.label = 1;
1766
1789
  case 1:
1767
- _a.trys.push([1, 3, , 5]);
1790
+ _b.trys.push([1, 3, , 5]);
1768
1791
  return [4 /*yield*/, this.sendReplayRequest({
1769
1792
  endpoint: endpoint,
1770
1793
  events: events,
1771
1794
  replayId: replayId,
1772
1795
  segmentId: segmentId,
1796
+ includeReplayStartTimestamp: includeReplayStartTimestamp,
1773
1797
  })];
1774
1798
  case 2:
1775
- _a.sent();
1799
+ _b.sent();
1776
1800
  this.resetRetries();
1777
1801
  return [2 /*return*/, true];
1778
1802
  case 3:
1779
- ex_1 = _a.sent();
1803
+ ex_1 = _b.sent();
1780
1804
  console.error(ex_1);
1781
1805
  // Capture error for every failed replay
1782
1806
  // TODO: Remove this before GA as this will create an error on customer's project
@@ -1799,7 +1823,12 @@ var Replay = /** @class */ (function () {
1799
1823
  switch (_a.label) {
1800
1824
  case 0:
1801
1825
  _a.trys.push([0, 2, , 3]);
1802
- return [4 /*yield*/, this.sendReplay(replayId, events, segmentId)];
1826
+ return [4 /*yield*/, this.sendReplay({
1827
+ replayId: replayId,
1828
+ events: events,
1829
+ segmentId: segmentId,
1830
+ includeReplayStartTimestamp: includeReplayStartTimestamp,
1831
+ })];
1803
1832
  case 1:
1804
1833
  _a.sent();
1805
1834
  resolve(true);
@@ -1813,7 +1842,7 @@ var Replay = /** @class */ (function () {
1813
1842
  });
1814
1843
  }); }, _this.retryInterval);
1815
1844
  })];
1816
- case 4: return [2 /*return*/, _a.sent()];
1845
+ case 4: return [2 /*return*/, _b.sent()];
1817
1846
  case 5: return [2 /*return*/];
1818
1847
  }
1819
1848
  });