@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/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { getCurrentHub, addBreadcrumb, addGlobalEventProcessor, prepareEvent, setContext, captureException } from '@sentry/core';
2
- import { GLOBAL_OBJ, logger, uuid4, htmlTreeAsString, dropUndefinedKeys, addInstrumentationHandler, browserPerformanceTimeOrigin, createEnvelope, createEventEnvelopeHeaders, getSdkMetadataForEnvelopeHeader, isNodeEnv } from '@sentry/utils';
2
+ import { GLOBAL_OBJ, logger, uuid4, normalize, htmlTreeAsString, SENTRY_XHR_DATA_KEY, dropUndefinedKeys, addInstrumentationHandler, browserPerformanceTimeOrigin, createEnvelope, createEventEnvelopeHeaders, getSdkMetadataForEnvelopeHeader, isNodeEnv } from '@sentry/utils';
3
3
 
4
4
  // exporting a separate copy of `WINDOW` rather than exporting the one from `@sentry/browser`
5
5
  // prevents the browser package from being bundled in the CDN bundle, and avoids a
@@ -4069,7 +4069,7 @@ function addBreadcrumbEvent(replay, breadcrumb) {
4069
4069
  timestamp: (breadcrumb.timestamp || 0) * 1000,
4070
4070
  data: {
4071
4071
  tag: 'breadcrumb',
4072
- payload: breadcrumb,
4072
+ payload: normalize(breadcrumb),
4073
4073
  },
4074
4074
  });
4075
4075
 
@@ -4436,12 +4436,14 @@ function handleFetchSpanListener(replay) {
4436
4436
  function handleXhr(handlerData) {
4437
4437
  const { startTimestamp, endTimestamp, xhr } = handlerData;
4438
4438
 
4439
- if (!startTimestamp || !endTimestamp || !xhr.__sentry_xhr__) {
4439
+ const sentryXhrData = xhr[SENTRY_XHR_DATA_KEY];
4440
+
4441
+ if (!startTimestamp || !endTimestamp || !sentryXhrData) {
4440
4442
  return null;
4441
4443
  }
4442
4444
 
4443
4445
  // This is only used as a fallback, so we know the body sizes are never set here
4444
- const { method, url, status_code: statusCode } = xhr.__sentry_xhr__;
4446
+ const { method, url, status_code: statusCode } = sentryXhrData;
4445
4447
 
4446
4448
  if (url === undefined) {
4447
4449
  return null;
@@ -4584,20 +4586,29 @@ function getNetworkBody(bodyText) {
4584
4586
 
4585
4587
  /** Build the request or response part of a replay network breadcrumb. */
4586
4588
  function buildNetworkRequestOrResponse(
4589
+ headers,
4587
4590
  bodySize,
4588
4591
  body,
4589
4592
  ) {
4590
- if (!bodySize) {
4593
+ if (!bodySize && Object.keys(headers).length === 0) {
4591
4594
  return undefined;
4592
4595
  }
4593
4596
 
4597
+ if (!bodySize) {
4598
+ return {
4599
+ headers,
4600
+ };
4601
+ }
4602
+
4594
4603
  if (!body) {
4595
4604
  return {
4605
+ headers,
4596
4606
  size: bodySize,
4597
4607
  };
4598
4608
  }
4599
4609
 
4600
4610
  const info = {
4611
+ headers,
4601
4612
  size: bodySize,
4602
4613
  };
4603
4614
 
@@ -4612,6 +4623,18 @@ function buildNetworkRequestOrResponse(
4612
4623
  return info;
4613
4624
  }
4614
4625
 
4626
+ /** Filter a set of headers */
4627
+ function getAllowedHeaders(headers, allowedHeaders) {
4628
+ return Object.keys(headers).reduce((filteredHeaders, key) => {
4629
+ const normalizedKey = key.toLowerCase();
4630
+ // Avoid putting empty strings into the headers
4631
+ if (allowedHeaders.includes(normalizedKey) && headers[key]) {
4632
+ filteredHeaders[normalizedKey] = headers[key];
4633
+ }
4634
+ return filteredHeaders;
4635
+ }, {});
4636
+ }
4637
+
4615
4638
  function _serializeFormData(formData) {
4616
4639
  // This is a bit simplified, but gives us a decent estimate
4617
4640
  // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'
@@ -4626,7 +4649,9 @@ function _serializeFormData(formData) {
4626
4649
  async function captureFetchBreadcrumbToReplay(
4627
4650
  breadcrumb,
4628
4651
  hint,
4629
- options,
4652
+ options
4653
+
4654
+ ,
4630
4655
  ) {
4631
4656
  try {
4632
4657
  const data = await _prepareFetchData(breadcrumb, hint, options);
@@ -4653,6 +4678,7 @@ function enrichFetchBreadcrumb(
4653
4678
 
4654
4679
  const body = _getFetchRequestArgBody(input);
4655
4680
  const reqSize = getBodySize(body, options.textEncoder);
4681
+
4656
4682
  const resSize = response ? parseContentLengthHeader(response.headers.get('content-length')) : undefined;
4657
4683
 
4658
4684
  if (reqSize !== undefined) {
@@ -4666,7 +4692,9 @@ function enrichFetchBreadcrumb(
4666
4692
  async function _prepareFetchData(
4667
4693
  breadcrumb,
4668
4694
  hint,
4669
- options,
4695
+ options
4696
+
4697
+ ,
4670
4698
  ) {
4671
4699
  const { startTimestamp, endTimestamp } = hint;
4672
4700
 
@@ -4693,27 +4721,37 @@ async function _prepareFetchData(
4693
4721
  }
4694
4722
 
4695
4723
  function _getRequestInfo(
4696
- { captureBodies },
4724
+ { captureBodies, requestHeaders },
4697
4725
  input,
4698
4726
  requestBodySize,
4699
4727
  ) {
4728
+ const headers = getRequestHeaders(input, requestHeaders);
4729
+
4700
4730
  if (!captureBodies) {
4701
- return buildNetworkRequestOrResponse(requestBodySize, undefined);
4731
+ return buildNetworkRequestOrResponse(headers, requestBodySize, undefined);
4702
4732
  }
4703
4733
 
4704
4734
  // We only want to transmit string or string-like bodies
4705
4735
  const requestBody = _getFetchRequestArgBody(input);
4706
4736
  const body = getNetworkBody(getBodyString(requestBody));
4707
- return buildNetworkRequestOrResponse(requestBodySize, body);
4737
+ return buildNetworkRequestOrResponse(headers, requestBodySize, body);
4708
4738
  }
4709
4739
 
4710
4740
  async function _getResponseInfo(
4711
- { captureBodies, textEncoder },
4741
+ {
4742
+ captureBodies,
4743
+ textEncoder,
4744
+ responseHeaders,
4745
+ }
4746
+
4747
+ ,
4712
4748
  response,
4713
4749
  responseBodySize,
4714
4750
  ) {
4751
+ const headers = getAllHeaders(response.headers, responseHeaders);
4752
+
4715
4753
  if (!captureBodies && responseBodySize !== undefined) {
4716
- return buildNetworkRequestOrResponse(responseBodySize, undefined);
4754
+ return buildNetworkRequestOrResponse(headers, responseBodySize, undefined);
4717
4755
  }
4718
4756
 
4719
4757
  // Only clone the response if we need to
@@ -4728,13 +4766,13 @@ async function _getResponseInfo(
4728
4766
  : responseBodySize;
4729
4767
 
4730
4768
  if (captureBodies) {
4731
- return buildNetworkRequestOrResponse(size, body);
4769
+ return buildNetworkRequestOrResponse(headers, size, body);
4732
4770
  }
4733
4771
 
4734
- return buildNetworkRequestOrResponse(size, undefined);
4772
+ return buildNetworkRequestOrResponse(headers, size, undefined);
4735
4773
  } catch (e) {
4736
4774
  // fallback
4737
- return buildNetworkRequestOrResponse(responseBodySize, undefined);
4775
+ return buildNetworkRequestOrResponse(headers, responseBodySize, undefined);
4738
4776
  }
4739
4777
  }
4740
4778
 
@@ -4768,6 +4806,56 @@ function _getFetchRequestArgBody(fetchArgs = []) {
4768
4806
  return (fetchArgs[1] ).body;
4769
4807
  }
4770
4808
 
4809
+ function getAllHeaders(headers, allowedHeaders) {
4810
+ const allHeaders = {};
4811
+
4812
+ allowedHeaders.forEach(header => {
4813
+ if (headers.get(header)) {
4814
+ allHeaders[header] = headers.get(header) ;
4815
+ }
4816
+ });
4817
+
4818
+ return allHeaders;
4819
+ }
4820
+
4821
+ function getRequestHeaders(fetchArgs, allowedHeaders) {
4822
+ if (fetchArgs.length === 1 && typeof fetchArgs[0] !== 'string') {
4823
+ return getHeadersFromOptions(fetchArgs[0] , allowedHeaders);
4824
+ }
4825
+
4826
+ if (fetchArgs.length === 2) {
4827
+ return getHeadersFromOptions(fetchArgs[1] , allowedHeaders);
4828
+ }
4829
+
4830
+ return {};
4831
+ }
4832
+
4833
+ function getHeadersFromOptions(
4834
+ input,
4835
+ allowedHeaders,
4836
+ ) {
4837
+ if (!input) {
4838
+ return {};
4839
+ }
4840
+
4841
+ const headers = input.headers;
4842
+
4843
+ if (!headers) {
4844
+ return {};
4845
+ }
4846
+
4847
+ if (headers instanceof Headers) {
4848
+ return getAllHeaders(headers, allowedHeaders);
4849
+ }
4850
+
4851
+ // We do not support this, as it is not really documented (anymore?)
4852
+ if (Array.isArray(headers)) {
4853
+ return {};
4854
+ }
4855
+
4856
+ return getAllowedHeaders(headers, allowedHeaders);
4857
+ }
4858
+
4771
4859
  /**
4772
4860
  * Capture an XHR breadcrumb to a replay.
4773
4861
  * This adds additional data (where approriate).
@@ -4818,7 +4906,7 @@ function _prepareXhrData(
4818
4906
  hint,
4819
4907
  options,
4820
4908
  ) {
4821
- const { startTimestamp, endTimestamp, input } = hint;
4909
+ const { startTimestamp, endTimestamp, input, xhr } = hint;
4822
4910
 
4823
4911
  const {
4824
4912
  url,
@@ -4828,15 +4916,21 @@ function _prepareXhrData(
4828
4916
  response_body_size: responseBodySize,
4829
4917
  } = breadcrumb.data;
4830
4918
 
4919
+ const xhrInfo = xhr[SENTRY_XHR_DATA_KEY];
4920
+ const requestHeaders = xhrInfo ? getAllowedHeaders(xhrInfo.request_headers, options.requestHeaders) : {};
4921
+ const responseHeaders = getAllowedHeaders(getResponseHeaders(xhr), options.responseHeaders);
4922
+
4831
4923
  if (!url) {
4832
4924
  return null;
4833
4925
  }
4834
4926
 
4835
4927
  const request = buildNetworkRequestOrResponse(
4928
+ requestHeaders,
4836
4929
  requestBodySize,
4837
4930
  options.captureBodies ? getNetworkBody(getBodyString(input)) : undefined,
4838
4931
  );
4839
4932
  const response = buildNetworkRequestOrResponse(
4933
+ responseHeaders,
4840
4934
  responseBodySize,
4841
4935
  options.captureBodies ? getNetworkBody(hint.xhr.responseText) : undefined,
4842
4936
  );
@@ -4852,6 +4946,20 @@ function _prepareXhrData(
4852
4946
  };
4853
4947
  }
4854
4948
 
4949
+ function getResponseHeaders(xhr) {
4950
+ const headers = xhr.getAllResponseHeaders();
4951
+
4952
+ if (!headers) {
4953
+ return {};
4954
+ }
4955
+
4956
+ return headers.split('\r\n').reduce((acc, line) => {
4957
+ const [key, value] = line.split(': ');
4958
+ acc[key.toLowerCase()] = value;
4959
+ return acc;
4960
+ }, {});
4961
+ }
4962
+
4855
4963
  /**
4856
4964
  * This method does two things:
4857
4965
  * - It enriches the regular XHR/fetch breadcrumbs with request/response size data
@@ -4867,7 +4975,7 @@ function handleNetworkBreadcrumbs(replay) {
4867
4975
  const options = {
4868
4976
  replay,
4869
4977
  textEncoder,
4870
- captureBodies: replay.getOptions()._experiments.captureNetworkBodies || false,
4978
+ ...replay.getExperimentalOptions().network,
4871
4979
  };
4872
4980
 
4873
4981
  if (client && client.on) {
@@ -5472,7 +5580,6 @@ async function sendReplayRequest({
5472
5580
  recordingData,
5473
5581
  replayId,
5474
5582
  segmentId: segment_id,
5475
- includeReplayStartTimestamp,
5476
5583
  eventContext,
5477
5584
  timestamp,
5478
5585
  session,
@@ -5498,9 +5605,8 @@ async function sendReplayRequest({
5498
5605
  }
5499
5606
 
5500
5607
  const baseEvent = {
5501
- // @ts-ignore private api
5502
5608
  type: REPLAY_EVENT_NAME,
5503
- ...(includeReplayStartTimestamp ? { replay_start_timestamp: initialTimestamp / 1000 } : {}),
5609
+ replay_start_timestamp: initialTimestamp / 1000,
5504
5610
  timestamp: timestamp / 1000,
5505
5611
  error_ids: errorIds,
5506
5612
  trace_ids: traceIds,
@@ -5759,6 +5865,8 @@ class ReplayContainer {
5759
5865
  this._debouncedFlush = debounce(() => this._flush(), this._options.flushMinDelay, {
5760
5866
  maxWait: this._options.flushMaxDelay,
5761
5867
  });
5868
+
5869
+ this._experimentalOptions = _getExperimentalOptions(options);
5762
5870
  }
5763
5871
 
5764
5872
  /** Get the event context. */
@@ -5781,6 +5889,15 @@ class ReplayContainer {
5781
5889
  return this._options;
5782
5890
  }
5783
5891
 
5892
+ /**
5893
+ * Get the experimental options.
5894
+ * THIS IS INTERNAL AND SUBJECT TO CHANGE!
5895
+ * @hidden
5896
+ */
5897
+ getExperimentalOptions() {
5898
+ return this._experimentalOptions;
5899
+ }
5900
+
5784
5901
  /**
5785
5902
  * Initializes the plugin.
5786
5903
  *
@@ -6382,7 +6499,6 @@ class ReplayContainer {
6382
6499
  replayId,
6383
6500
  recordingData,
6384
6501
  segmentId,
6385
- includeReplayStartTimestamp: segmentId === 0,
6386
6502
  eventContext,
6387
6503
  session: this.session,
6388
6504
  options: this.getOptions(),
@@ -6490,6 +6606,23 @@ class ReplayContainer {
6490
6606
  };}
6491
6607
  }
6492
6608
 
6609
+ function _getExperimentalOptions(options) {
6610
+ const requestHeaders = options._experiments.captureRequestHeaders || [];
6611
+ const responseHeaders = options._experiments.captureResponseHeaders || [];
6612
+ const captureBodies = options._experiments.captureNetworkBodies || false;
6613
+
6614
+ // Add defaults
6615
+ const defaultHeaders = ['content-length', 'content-type', 'accept'];
6616
+
6617
+ return {
6618
+ network: {
6619
+ captureBodies,
6620
+ requestHeaders: [...defaultHeaders, ...requestHeaders.map(header => header.toLowerCase())],
6621
+ responseHeaders: [...defaultHeaders, ...responseHeaders.map(header => header.toLowerCase())],
6622
+ },
6623
+ };
6624
+ }
6625
+
6493
6626
  function getOption(
6494
6627
  selectors,
6495
6628
  defaultSelectors,
@@ -6801,6 +6934,17 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`,
6801
6934
  return this._replay.flushImmediate();
6802
6935
  }
6803
6936
 
6937
+ /**
6938
+ * Get the current session ID.
6939
+ */
6940
+ getReplayId() {
6941
+ if (!this._replay || !this._replay.isEnabled()) {
6942
+ return;
6943
+ }
6944
+
6945
+ return this._replay.getSessionId();
6946
+ }
6947
+
6804
6948
  /** Setup the integration. */
6805
6949
  _setup() {
6806
6950
  // Client is not available in constructor, so we need to wait until setupOnce