@sentry/replay 7.46.0 → 7.48.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/README.md CHANGED
@@ -60,6 +60,9 @@ If you do not want to start Replay immediately (e.g. if you want to lazy-load it
60
60
  you can also use `addIntegration` to load it later:
61
61
 
62
62
  ```js
63
+ import * as Sentry from "@sentry/react";
64
+ import { BrowserClient } from "@sentry/browser";
65
+
63
66
  Sentry.init({
64
67
  // Do not load it initially
65
68
  integrations: []
@@ -67,7 +70,10 @@ Sentry.init({
67
70
 
68
71
  // Sometime later
69
72
  const { Replay } = await import('@sentry/browser');
70
- getCurrentHub().getClient().addIntegration(new Replay());
73
+ const client = Sentry.getCurrentHub().getClient<BrowserClient>();
74
+
75
+ // Client can be undefined
76
+ client?.addIntegration(new Replay());
71
77
  ```
72
78
 
73
79
  ### Identifying Users
@@ -76,6 +82,7 @@ If you have only followed the above instructions to setup session replays, you w
76
82
 
77
83
  ```javascript
78
84
  import * as Sentry from "@sentry/browser";
85
+
79
86
  Sentry.setUser({ email: "jane.doe@example.com" });
80
87
  ```
81
88
 
@@ -84,17 +91,22 @@ Sentry.setUser({ email: "jane.doe@example.com" });
84
91
  Replay recording only starts when it is included in the `integrations` array when calling `Sentry.init` or calling `addIntegration` from the a Sentry client instance. To stop recording you can call the `stop()`.
85
92
 
86
93
  ```js
94
+ import * as Sentry from "@sentry/react";
95
+ import { BrowserClient } from "@sentry/browser";
96
+
87
97
  const replay = new Replay();
98
+
88
99
  Sentry.init({
89
100
  integrations: [replay]
90
101
  });
91
102
 
92
- const client = getClient();
103
+ const client = Sentry.getCurrentHub().getClient<BrowserClient>();
93
104
 
94
105
  // Add replay integration, will start recoring
95
- client.addIntegration(replay);
106
+ client?.addIntegration(replay);
96
107
 
97
- replay.stop(); // Stop recording
108
+ // Stop recording
109
+ replay.stop();
98
110
  ```
99
111
 
100
112
  ## Loading Replay as a CDN Bundle
@@ -126,6 +138,11 @@ A session starts when the Session Replay SDK is first loaded and initialized. Th
126
138
 
127
139
  [^1]: An 'interaction' refers to either a mouse click or a browser navigation event.
128
140
 
141
+ ### Accessing the Replay Session ID
142
+
143
+ You can get the ID of the currently running session via `replay.getReplayId()`.
144
+ This will return `undefined` if no session is ongoing.
145
+
129
146
  ### Replay Captures Only on Errors
130
147
 
131
148
  Alternatively, rather than recording an entire session, you can capture a replay only when an error occurs. In this case, the integration will buffer up to one minute worth of events prior to the error being thrown. It will continue to record the session following the rules above regarding session life and activity. Read the [sampling](#Sampling) section for configuration options.
package/cjs/index.js CHANGED
@@ -4071,7 +4071,7 @@ function addBreadcrumbEvent(replay, breadcrumb) {
4071
4071
  timestamp: (breadcrumb.timestamp || 0) * 1000,
4072
4072
  data: {
4073
4073
  tag: 'breadcrumb',
4074
- payload: breadcrumb,
4074
+ payload: utils.normalize(breadcrumb),
4075
4075
  },
4076
4076
  });
4077
4077
 
@@ -4438,12 +4438,14 @@ function handleFetchSpanListener(replay) {
4438
4438
  function handleXhr(handlerData) {
4439
4439
  const { startTimestamp, endTimestamp, xhr } = handlerData;
4440
4440
 
4441
- if (!startTimestamp || !endTimestamp || !xhr.__sentry_xhr__) {
4441
+ const sentryXhrData = xhr[utils.SENTRY_XHR_DATA_KEY];
4442
+
4443
+ if (!startTimestamp || !endTimestamp || !sentryXhrData) {
4442
4444
  return null;
4443
4445
  }
4444
4446
 
4445
4447
  // This is only used as a fallback, so we know the body sizes are never set here
4446
- const { method, url, status_code: statusCode } = xhr.__sentry_xhr__;
4448
+ const { method, url, status_code: statusCode } = sentryXhrData;
4447
4449
 
4448
4450
  if (url === undefined) {
4449
4451
  return null;
@@ -4586,20 +4588,29 @@ function getNetworkBody(bodyText) {
4586
4588
 
4587
4589
  /** Build the request or response part of a replay network breadcrumb. */
4588
4590
  function buildNetworkRequestOrResponse(
4591
+ headers,
4589
4592
  bodySize,
4590
4593
  body,
4591
4594
  ) {
4592
- if (!bodySize) {
4595
+ if (!bodySize && Object.keys(headers).length === 0) {
4593
4596
  return undefined;
4594
4597
  }
4595
4598
 
4599
+ if (!bodySize) {
4600
+ return {
4601
+ headers,
4602
+ };
4603
+ }
4604
+
4596
4605
  if (!body) {
4597
4606
  return {
4607
+ headers,
4598
4608
  size: bodySize,
4599
4609
  };
4600
4610
  }
4601
4611
 
4602
4612
  const info = {
4613
+ headers,
4603
4614
  size: bodySize,
4604
4615
  };
4605
4616
 
@@ -4614,6 +4625,18 @@ function buildNetworkRequestOrResponse(
4614
4625
  return info;
4615
4626
  }
4616
4627
 
4628
+ /** Filter a set of headers */
4629
+ function getAllowedHeaders(headers, allowedHeaders) {
4630
+ return Object.keys(headers).reduce((filteredHeaders, key) => {
4631
+ const normalizedKey = key.toLowerCase();
4632
+ // Avoid putting empty strings into the headers
4633
+ if (allowedHeaders.includes(normalizedKey) && headers[key]) {
4634
+ filteredHeaders[normalizedKey] = headers[key];
4635
+ }
4636
+ return filteredHeaders;
4637
+ }, {});
4638
+ }
4639
+
4617
4640
  function _serializeFormData(formData) {
4618
4641
  // This is a bit simplified, but gives us a decent estimate
4619
4642
  // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'
@@ -4628,7 +4651,9 @@ function _serializeFormData(formData) {
4628
4651
  async function captureFetchBreadcrumbToReplay(
4629
4652
  breadcrumb,
4630
4653
  hint,
4631
- options,
4654
+ options
4655
+
4656
+ ,
4632
4657
  ) {
4633
4658
  try {
4634
4659
  const data = await _prepareFetchData(breadcrumb, hint, options);
@@ -4655,6 +4680,7 @@ function enrichFetchBreadcrumb(
4655
4680
 
4656
4681
  const body = _getFetchRequestArgBody(input);
4657
4682
  const reqSize = getBodySize(body, options.textEncoder);
4683
+
4658
4684
  const resSize = response ? parseContentLengthHeader(response.headers.get('content-length')) : undefined;
4659
4685
 
4660
4686
  if (reqSize !== undefined) {
@@ -4668,7 +4694,9 @@ function enrichFetchBreadcrumb(
4668
4694
  async function _prepareFetchData(
4669
4695
  breadcrumb,
4670
4696
  hint,
4671
- options,
4697
+ options
4698
+
4699
+ ,
4672
4700
  ) {
4673
4701
  const { startTimestamp, endTimestamp } = hint;
4674
4702
 
@@ -4695,27 +4723,37 @@ async function _prepareFetchData(
4695
4723
  }
4696
4724
 
4697
4725
  function _getRequestInfo(
4698
- { captureBodies },
4726
+ { captureBodies, requestHeaders },
4699
4727
  input,
4700
4728
  requestBodySize,
4701
4729
  ) {
4730
+ const headers = getRequestHeaders(input, requestHeaders);
4731
+
4702
4732
  if (!captureBodies) {
4703
- return buildNetworkRequestOrResponse(requestBodySize, undefined);
4733
+ return buildNetworkRequestOrResponse(headers, requestBodySize, undefined);
4704
4734
  }
4705
4735
 
4706
4736
  // We only want to transmit string or string-like bodies
4707
4737
  const requestBody = _getFetchRequestArgBody(input);
4708
4738
  const body = getNetworkBody(getBodyString(requestBody));
4709
- return buildNetworkRequestOrResponse(requestBodySize, body);
4739
+ return buildNetworkRequestOrResponse(headers, requestBodySize, body);
4710
4740
  }
4711
4741
 
4712
4742
  async function _getResponseInfo(
4713
- { captureBodies, textEncoder },
4743
+ {
4744
+ captureBodies,
4745
+ textEncoder,
4746
+ responseHeaders,
4747
+ }
4748
+
4749
+ ,
4714
4750
  response,
4715
4751
  responseBodySize,
4716
4752
  ) {
4753
+ const headers = getAllHeaders(response.headers, responseHeaders);
4754
+
4717
4755
  if (!captureBodies && responseBodySize !== undefined) {
4718
- return buildNetworkRequestOrResponse(responseBodySize, undefined);
4756
+ return buildNetworkRequestOrResponse(headers, responseBodySize, undefined);
4719
4757
  }
4720
4758
 
4721
4759
  // Only clone the response if we need to
@@ -4730,13 +4768,13 @@ async function _getResponseInfo(
4730
4768
  : responseBodySize;
4731
4769
 
4732
4770
  if (captureBodies) {
4733
- return buildNetworkRequestOrResponse(size, body);
4771
+ return buildNetworkRequestOrResponse(headers, size, body);
4734
4772
  }
4735
4773
 
4736
- return buildNetworkRequestOrResponse(size, undefined);
4774
+ return buildNetworkRequestOrResponse(headers, size, undefined);
4737
4775
  } catch (e) {
4738
4776
  // fallback
4739
- return buildNetworkRequestOrResponse(responseBodySize, undefined);
4777
+ return buildNetworkRequestOrResponse(headers, responseBodySize, undefined);
4740
4778
  }
4741
4779
  }
4742
4780
 
@@ -4770,6 +4808,56 @@ function _getFetchRequestArgBody(fetchArgs = []) {
4770
4808
  return (fetchArgs[1] ).body;
4771
4809
  }
4772
4810
 
4811
+ function getAllHeaders(headers, allowedHeaders) {
4812
+ const allHeaders = {};
4813
+
4814
+ allowedHeaders.forEach(header => {
4815
+ if (headers.get(header)) {
4816
+ allHeaders[header] = headers.get(header) ;
4817
+ }
4818
+ });
4819
+
4820
+ return allHeaders;
4821
+ }
4822
+
4823
+ function getRequestHeaders(fetchArgs, allowedHeaders) {
4824
+ if (fetchArgs.length === 1 && typeof fetchArgs[0] !== 'string') {
4825
+ return getHeadersFromOptions(fetchArgs[0] , allowedHeaders);
4826
+ }
4827
+
4828
+ if (fetchArgs.length === 2) {
4829
+ return getHeadersFromOptions(fetchArgs[1] , allowedHeaders);
4830
+ }
4831
+
4832
+ return {};
4833
+ }
4834
+
4835
+ function getHeadersFromOptions(
4836
+ input,
4837
+ allowedHeaders,
4838
+ ) {
4839
+ if (!input) {
4840
+ return {};
4841
+ }
4842
+
4843
+ const headers = input.headers;
4844
+
4845
+ if (!headers) {
4846
+ return {};
4847
+ }
4848
+
4849
+ if (headers instanceof Headers) {
4850
+ return getAllHeaders(headers, allowedHeaders);
4851
+ }
4852
+
4853
+ // We do not support this, as it is not really documented (anymore?)
4854
+ if (Array.isArray(headers)) {
4855
+ return {};
4856
+ }
4857
+
4858
+ return getAllowedHeaders(headers, allowedHeaders);
4859
+ }
4860
+
4773
4861
  /**
4774
4862
  * Capture an XHR breadcrumb to a replay.
4775
4863
  * This adds additional data (where approriate).
@@ -4820,7 +4908,7 @@ function _prepareXhrData(
4820
4908
  hint,
4821
4909
  options,
4822
4910
  ) {
4823
- const { startTimestamp, endTimestamp, input } = hint;
4911
+ const { startTimestamp, endTimestamp, input, xhr } = hint;
4824
4912
 
4825
4913
  const {
4826
4914
  url,
@@ -4830,15 +4918,21 @@ function _prepareXhrData(
4830
4918
  response_body_size: responseBodySize,
4831
4919
  } = breadcrumb.data;
4832
4920
 
4921
+ const xhrInfo = xhr[utils.SENTRY_XHR_DATA_KEY];
4922
+ const requestHeaders = xhrInfo ? getAllowedHeaders(xhrInfo.request_headers, options.requestHeaders) : {};
4923
+ const responseHeaders = getAllowedHeaders(getResponseHeaders(xhr), options.responseHeaders);
4924
+
4833
4925
  if (!url) {
4834
4926
  return null;
4835
4927
  }
4836
4928
 
4837
4929
  const request = buildNetworkRequestOrResponse(
4930
+ requestHeaders,
4838
4931
  requestBodySize,
4839
4932
  options.captureBodies ? getNetworkBody(getBodyString(input)) : undefined,
4840
4933
  );
4841
4934
  const response = buildNetworkRequestOrResponse(
4935
+ responseHeaders,
4842
4936
  responseBodySize,
4843
4937
  options.captureBodies ? getNetworkBody(hint.xhr.responseText) : undefined,
4844
4938
  );
@@ -4854,6 +4948,20 @@ function _prepareXhrData(
4854
4948
  };
4855
4949
  }
4856
4950
 
4951
+ function getResponseHeaders(xhr) {
4952
+ const headers = xhr.getAllResponseHeaders();
4953
+
4954
+ if (!headers) {
4955
+ return {};
4956
+ }
4957
+
4958
+ return headers.split('\r\n').reduce((acc, line) => {
4959
+ const [key, value] = line.split(': ');
4960
+ acc[key.toLowerCase()] = value;
4961
+ return acc;
4962
+ }, {});
4963
+ }
4964
+
4857
4965
  /**
4858
4966
  * This method does two things:
4859
4967
  * - It enriches the regular XHR/fetch breadcrumbs with request/response size data
@@ -4869,7 +4977,7 @@ function handleNetworkBreadcrumbs(replay) {
4869
4977
  const options = {
4870
4978
  replay,
4871
4979
  textEncoder,
4872
- captureBodies: replay.getOptions()._experiments.captureNetworkBodies || false,
4980
+ ...replay.getExperimentalOptions().network,
4873
4981
  };
4874
4982
 
4875
4983
  if (client && client.on) {
@@ -5474,7 +5582,6 @@ async function sendReplayRequest({
5474
5582
  recordingData,
5475
5583
  replayId,
5476
5584
  segmentId: segment_id,
5477
- includeReplayStartTimestamp,
5478
5585
  eventContext,
5479
5586
  timestamp,
5480
5587
  session,
@@ -5500,9 +5607,8 @@ async function sendReplayRequest({
5500
5607
  }
5501
5608
 
5502
5609
  const baseEvent = {
5503
- // @ts-ignore private api
5504
5610
  type: REPLAY_EVENT_NAME,
5505
- ...(includeReplayStartTimestamp ? { replay_start_timestamp: initialTimestamp / 1000 } : {}),
5611
+ replay_start_timestamp: initialTimestamp / 1000,
5506
5612
  timestamp: timestamp / 1000,
5507
5613
  error_ids: errorIds,
5508
5614
  trace_ids: traceIds,
@@ -5761,6 +5867,8 @@ class ReplayContainer {
5761
5867
  this._debouncedFlush = debounce(() => this._flush(), this._options.flushMinDelay, {
5762
5868
  maxWait: this._options.flushMaxDelay,
5763
5869
  });
5870
+
5871
+ this._experimentalOptions = _getExperimentalOptions(options);
5764
5872
  }
5765
5873
 
5766
5874
  /** Get the event context. */
@@ -5783,6 +5891,15 @@ class ReplayContainer {
5783
5891
  return this._options;
5784
5892
  }
5785
5893
 
5894
+ /**
5895
+ * Get the experimental options.
5896
+ * THIS IS INTERNAL AND SUBJECT TO CHANGE!
5897
+ * @hidden
5898
+ */
5899
+ getExperimentalOptions() {
5900
+ return this._experimentalOptions;
5901
+ }
5902
+
5786
5903
  /**
5787
5904
  * Initializes the plugin.
5788
5905
  *
@@ -6384,7 +6501,6 @@ class ReplayContainer {
6384
6501
  replayId,
6385
6502
  recordingData,
6386
6503
  segmentId,
6387
- includeReplayStartTimestamp: segmentId === 0,
6388
6504
  eventContext,
6389
6505
  session: this.session,
6390
6506
  options: this.getOptions(),
@@ -6492,6 +6608,23 @@ class ReplayContainer {
6492
6608
  };}
6493
6609
  }
6494
6610
 
6611
+ function _getExperimentalOptions(options) {
6612
+ const requestHeaders = options._experiments.captureRequestHeaders || [];
6613
+ const responseHeaders = options._experiments.captureResponseHeaders || [];
6614
+ const captureBodies = options._experiments.captureNetworkBodies || false;
6615
+
6616
+ // Add defaults
6617
+ const defaultHeaders = ['content-length', 'content-type', 'accept'];
6618
+
6619
+ return {
6620
+ network: {
6621
+ captureBodies,
6622
+ requestHeaders: [...defaultHeaders, ...requestHeaders.map(header => header.toLowerCase())],
6623
+ responseHeaders: [...defaultHeaders, ...responseHeaders.map(header => header.toLowerCase())],
6624
+ },
6625
+ };
6626
+ }
6627
+
6495
6628
  function getOption(
6496
6629
  selectors,
6497
6630
  defaultSelectors,
@@ -6803,6 +6936,17 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`,
6803
6936
  return this._replay.flushImmediate();
6804
6937
  }
6805
6938
 
6939
+ /**
6940
+ * Get the current session ID.
6941
+ */
6942
+ getReplayId() {
6943
+ if (!this._replay || !this._replay.isEnabled()) {
6944
+ return;
6945
+ }
6946
+
6947
+ return this._replay.getSessionId();
6948
+ }
6949
+
6806
6950
  /** Setup the integration. */
6807
6951
  _setup() {
6808
6952
  // Client is not available in constructor, so we need to wait until setupOnce