@sentry/replay 7.45.0 → 7.47.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.
Files changed (38) hide show
  1. package/README.md +16 -4
  2. package/cjs/index.js +443 -211
  3. package/cjs/index.js.map +1 -1
  4. package/esm/index.js +444 -212
  5. package/esm/index.js.map +1 -1
  6. package/package.json +5 -5
  7. package/types/constants.d.ts +1 -0
  8. package/types/constants.d.ts.map +1 -1
  9. package/types/coreHandlers/handleFetch.d.ts +2 -2
  10. package/types/coreHandlers/handleFetch.d.ts.map +1 -1
  11. package/types/coreHandlers/handleHistory.d.ts.map +1 -1
  12. package/types/coreHandlers/handleNetworkBreadcrumbs.d.ts +5 -11
  13. package/types/coreHandlers/handleNetworkBreadcrumbs.d.ts.map +1 -1
  14. package/types/coreHandlers/handleXhr.d.ts +2 -2
  15. package/types/coreHandlers/handleXhr.d.ts.map +1 -1
  16. package/types/coreHandlers/{addBreadcrumbEvent.d.ts → util/addBreadcrumbEvent.d.ts} +1 -1
  17. package/types/coreHandlers/util/addBreadcrumbEvent.d.ts.map +1 -0
  18. package/types/coreHandlers/util/addNetworkBreadcrumb.d.ts +4 -0
  19. package/types/coreHandlers/util/addNetworkBreadcrumb.d.ts.map +1 -0
  20. package/types/coreHandlers/util/fetchUtils.d.ts +24 -0
  21. package/types/coreHandlers/util/fetchUtils.d.ts.map +1 -0
  22. package/types/coreHandlers/util/networkUtils.d.ts +15 -0
  23. package/types/coreHandlers/util/networkUtils.d.ts.map +1 -0
  24. package/types/coreHandlers/util/xhrUtils.d.ts +23 -0
  25. package/types/coreHandlers/util/xhrUtils.d.ts.map +1 -0
  26. package/types/replay.d.ts +2 -0
  27. package/types/replay.d.ts.map +1 -1
  28. package/types/types.d.ts +126 -5
  29. package/types/types.d.ts.map +1 -1
  30. package/types/util/addMemoryEntry.d.ts.map +1 -1
  31. package/types/util/createPerformanceEntries.d.ts +2 -2
  32. package/types/util/createPerformanceEntries.d.ts.map +1 -1
  33. package/types/util/createPerformanceSpans.d.ts +2 -2
  34. package/types/util/createPerformanceSpans.d.ts.map +1 -1
  35. package/types/util/sendReplayRequest.d.ts.map +1 -1
  36. package/types/coreHandlers/addBreadcrumbEvent.d.ts.map +0 -1
  37. package/types/coreHandlers/addNetworkBreadcrumb.d.ts +0 -4
  38. package/types/coreHandlers/addNetworkBreadcrumb.d.ts.map +0 -1
package/cjs/index.js CHANGED
@@ -31,6 +31,9 @@ const ERROR_CHECKOUT_TIME = 60000;
31
31
  const RETRY_BASE_INTERVAL = 5000;
32
32
  const RETRY_MAX_COUNT = 3;
33
33
 
34
+ /* The max (uncompressed) size in bytes of a network body. Any body larger than this will be dropped. */
35
+ const NETWORK_BODY_MAX_SIZE = 300000;
36
+
34
37
  var NodeType$1;
35
38
  (function (NodeType) {
36
39
  NodeType[NodeType["Document"] = 0] = "Document";
@@ -4068,7 +4071,7 @@ function addBreadcrumbEvent(replay, breadcrumb) {
4068
4071
  timestamp: (breadcrumb.timestamp || 0) * 1000,
4069
4072
  data: {
4070
4073
  tag: 'breadcrumb',
4071
- payload: breadcrumb,
4074
+ payload: utils.normalize(breadcrumb),
4072
4075
  },
4073
4076
  });
4074
4077
 
@@ -4368,7 +4371,10 @@ function _isSentryRequest(url) {
4368
4371
  }
4369
4372
 
4370
4373
  /** Add a performance entry breadcrumb */
4371
- function addNetworkBreadcrumb(replay, result) {
4374
+ function addNetworkBreadcrumb(
4375
+ replay,
4376
+ result,
4377
+ ) {
4372
4378
  if (!replay.isEnabled()) {
4373
4379
  return;
4374
4380
  }
@@ -4398,18 +4404,17 @@ function handleFetch(handlerData) {
4398
4404
  return null;
4399
4405
  }
4400
4406
 
4401
- const { method, request_body_size: requestBodySize, response_body_size: responseBodySize } = fetchData;
4407
+ // This is only used as a fallback, so we know the body sizes are never set here
4408
+ const { method, url } = fetchData;
4402
4409
 
4403
4410
  return {
4404
4411
  type: 'resource.fetch',
4405
4412
  start: startTimestamp / 1000,
4406
4413
  end: endTimestamp / 1000,
4407
- name: fetchData.url,
4414
+ name: url,
4408
4415
  data: {
4409
4416
  method,
4410
4417
  statusCode: response && (response ).status,
4411
- requestBodySize,
4412
- responseBodySize,
4413
4418
  },
4414
4419
  };
4415
4420
  }
@@ -4437,13 +4442,8 @@ function handleXhr(handlerData) {
4437
4442
  return null;
4438
4443
  }
4439
4444
 
4440
- const {
4441
- method,
4442
- url,
4443
- status_code: statusCode,
4444
- request_body_size: requestBodySize,
4445
- response_body_size: responseBodySize,
4446
- } = xhr.__sentry_xhr__;
4445
+ // 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__;
4447
4447
 
4448
4448
  if (url === undefined) {
4449
4449
  return null;
@@ -4457,8 +4457,6 @@ function handleXhr(handlerData) {
4457
4457
  data: {
4458
4458
  method,
4459
4459
  statusCode,
4460
- requestBodySize,
4461
- responseBodySize,
4462
4460
  },
4463
4461
  };
4464
4462
  }
@@ -4478,131 +4476,202 @@ function handleXhrSpanListener(replay) {
4478
4476
  };
4479
4477
  }
4480
4478
 
4481
- /**
4482
- * This will enrich the xhr/fetch breadcrumbs with additional information.
4483
- *
4484
- * This adds:
4485
- * * request_body_size
4486
- * * response_body_size
4487
- *
4488
- * to the breadcrumb data.
4489
- */
4490
- function handleNetworkBreadcrumbs(replay) {
4491
- const client = core.getCurrentHub().getClient();
4479
+ /** Get the size of a body. */
4480
+ function getBodySize(
4481
+ body,
4482
+ textEncoder,
4483
+ ) {
4484
+ if (!body) {
4485
+ return undefined;
4486
+ }
4492
4487
 
4493
4488
  try {
4494
- const textEncoder = new TextEncoder();
4489
+ if (typeof body === 'string') {
4490
+ return textEncoder.encode(body).length;
4491
+ }
4495
4492
 
4496
- const options = {
4497
- replay,
4498
- textEncoder,
4499
- };
4493
+ if (body instanceof URLSearchParams) {
4494
+ return textEncoder.encode(body.toString()).length;
4495
+ }
4500
4496
 
4501
- if (client && client.on) {
4502
- client.on('beforeAddBreadcrumb', (breadcrumb, hint) => beforeAddNetworkBreadcrumb(options, breadcrumb, hint));
4503
- } else {
4504
- // Fallback behavior
4505
- utils.addInstrumentationHandler('fetch', handleFetchSpanListener(replay));
4506
- utils.addInstrumentationHandler('xhr', handleXhrSpanListener(replay));
4497
+ if (body instanceof FormData) {
4498
+ const formDataStr = _serializeFormData(body);
4499
+ return textEncoder.encode(formDataStr).length;
4507
4500
  }
4508
- } catch (e2) {
4509
- // Do nothing
4501
+
4502
+ if (body instanceof Blob) {
4503
+ return body.size;
4504
+ }
4505
+
4506
+ if (body instanceof ArrayBuffer) {
4507
+ return body.byteLength;
4508
+ }
4509
+
4510
+ // Currently unhandled types: ArrayBufferView, ReadableStream
4511
+ } catch (e) {
4512
+ // just return undefined
4510
4513
  }
4514
+
4515
+ return undefined;
4511
4516
  }
4512
4517
 
4513
- /** just exported for tests */
4514
- function beforeAddNetworkBreadcrumb(
4515
- options,
4516
- breadcrumb,
4517
- hint,
4518
+ /** Convert a Content-Length header to number/undefined. */
4519
+ function parseContentLengthHeader(header) {
4520
+ if (!header) {
4521
+ return undefined;
4522
+ }
4523
+
4524
+ const size = parseInt(header, 10);
4525
+ return isNaN(size) ? undefined : size;
4526
+ }
4527
+
4528
+ /** Get the string representation of a body. */
4529
+ function getBodyString(body) {
4530
+ if (typeof body === 'string') {
4531
+ return body;
4532
+ }
4533
+
4534
+ if (body instanceof URLSearchParams) {
4535
+ return body.toString();
4536
+ }
4537
+
4538
+ if (body instanceof FormData) {
4539
+ return _serializeFormData(body);
4540
+ }
4541
+
4542
+ return undefined;
4543
+ }
4544
+
4545
+ /** Convert ReplayNetworkRequestData to a PerformanceEntry. */
4546
+ function makeNetworkReplayBreadcrumb(
4547
+ type,
4548
+ data,
4518
4549
  ) {
4519
- if (!breadcrumb.data) {
4520
- return;
4550
+ if (!data) {
4551
+ return null;
4521
4552
  }
4522
4553
 
4523
- try {
4524
- if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) {
4525
- _handleXhrBreadcrumb(breadcrumb, hint, options);
4526
- }
4554
+ const { startTimestamp, endTimestamp, url, method, statusCode, request, response } = data;
4527
4555
 
4528
- if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) {
4529
- // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick
4530
- // Because the hook runs synchronously, and the breadcrumb is afterwards passed on
4531
- // So any async mutations to it will not be reflected in the final breadcrumb
4532
- _enrichFetchBreadcrumb(breadcrumb, hint, options);
4556
+ const result = {
4557
+ type,
4558
+ start: startTimestamp / 1000,
4559
+ end: endTimestamp / 1000,
4560
+ name: url,
4561
+ data: utils.dropUndefinedKeys({
4562
+ method,
4563
+ statusCode,
4564
+ request,
4565
+ response,
4566
+ }),
4567
+ };
4533
4568
 
4534
- void _handleFetchBreadcrumb(breadcrumb, hint, options);
4535
- }
4536
- } catch (e) {
4537
- (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('Error when enriching network breadcrumb');
4569
+ return result;
4570
+ }
4571
+
4572
+ /** Get either a JSON network body, or a text representation. */
4573
+ function getNetworkBody(bodyText) {
4574
+ if (!bodyText) {
4575
+ return;
4538
4576
  }
4577
+
4578
+ try {
4579
+ return JSON.parse(bodyText);
4580
+ } catch (e2) {
4581
+ // return text
4582
+ }
4583
+
4584
+ return bodyText;
4539
4585
  }
4540
4586
 
4541
- function _handleXhrBreadcrumb(
4542
- breadcrumb,
4543
- hint,
4544
- options,
4587
+ /** Build the request or response part of a replay network breadcrumb. */
4588
+ function buildNetworkRequestOrResponse(
4589
+ bodySize,
4590
+ body,
4545
4591
  ) {
4546
- // Enriches the breadcrumb overall
4547
- _enrichXhrBreadcrumb(breadcrumb, hint, options);
4592
+ if (!bodySize) {
4593
+ return undefined;
4594
+ }
4595
+
4596
+ if (!body) {
4597
+ return {
4598
+ size: bodySize,
4599
+ };
4600
+ }
4601
+
4602
+ const info = {
4603
+ size: bodySize,
4604
+ };
4605
+
4606
+ if (bodySize < NETWORK_BODY_MAX_SIZE) {
4607
+ info.body = body;
4608
+ } else {
4609
+ info._meta = {
4610
+ errors: ['MAX_BODY_SIZE_EXCEEDED'],
4611
+ };
4612
+ }
4548
4613
 
4549
- // Create a replay performance entry from this breadcrumb
4550
- const result = _makeNetworkReplayBreadcrumb('resource.xhr', breadcrumb, hint);
4551
- addNetworkBreadcrumb(options.replay, result);
4614
+ return info;
4552
4615
  }
4553
4616
 
4554
- async function _handleFetchBreadcrumb(
4617
+ function _serializeFormData(formData) {
4618
+ // This is a bit simplified, but gives us a decent estimate
4619
+ // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'
4620
+ // @ts-ignore passing FormData to URLSearchParams actually works
4621
+ return new URLSearchParams(formData).toString();
4622
+ }
4623
+
4624
+ /**
4625
+ * Capture a fetch breadcrumb to a replay.
4626
+ * This adds additional data (where approriate).
4627
+ */
4628
+ async function captureFetchBreadcrumbToReplay(
4555
4629
  breadcrumb,
4556
4630
  hint,
4557
4631
  options,
4558
4632
  ) {
4559
- const fullBreadcrumb = await _parseFetchResponse(breadcrumb, hint, options);
4633
+ try {
4634
+ const data = await _prepareFetchData(breadcrumb, hint, options);
4560
4635
 
4561
- // Create a replay performance entry from this breadcrumb
4562
- const result = _makeNetworkReplayBreadcrumb('resource.fetch', fullBreadcrumb, hint);
4563
- addNetworkBreadcrumb(options.replay, result);
4636
+ // Create a replay performance entry from this breadcrumb
4637
+ const result = makeNetworkReplayBreadcrumb('resource.fetch', data);
4638
+ addNetworkBreadcrumb(options.replay, result);
4639
+ } catch (error) {
4640
+ (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.error('[Replay] Failed to capture fetch breadcrumb', error);
4641
+ }
4564
4642
  }
4565
4643
 
4566
- // This does async operations on the breadcrumb for replay
4567
- async function _parseFetchResponse(
4644
+ /**
4645
+ * Enrich a breadcrumb with additional data.
4646
+ * This has to be sync & mutate the given breadcrumb,
4647
+ * as the breadcrumb is afterwards consumed by other handlers.
4648
+ */
4649
+ function enrichFetchBreadcrumb(
4568
4650
  breadcrumb,
4569
4651
  hint,
4570
4652
  options,
4571
4653
  ) {
4572
- if (breadcrumb.data.response_body_size || !hint.response) {
4573
- return breadcrumb;
4574
- }
4654
+ const { input, response } = hint;
4575
4655
 
4576
- // If no Content-Length header exists, we try to get the size from the response body
4577
- try {
4578
- // We have to clone this, as the body can only be read once
4579
- const response = (hint.response ).clone();
4580
- const body = await response.text();
4656
+ const body = _getFetchRequestArgBody(input);
4657
+ const reqSize = getBodySize(body, options.textEncoder);
4658
+ const resSize = response ? parseContentLengthHeader(response.headers.get('content-length')) : undefined;
4581
4659
 
4582
- if (body.length) {
4583
- return {
4584
- ...breadcrumb,
4585
- data: { ...breadcrumb.data, response_body_size: getBodySize(body, options.textEncoder) },
4586
- };
4587
- }
4588
- } catch (e3) {
4589
- // just ignore if something fails here
4660
+ if (reqSize !== undefined) {
4661
+ breadcrumb.data.request_body_size = reqSize;
4662
+ }
4663
+ if (resSize !== undefined) {
4664
+ breadcrumb.data.response_body_size = resSize;
4590
4665
  }
4591
-
4592
- return breadcrumb;
4593
4666
  }
4594
4667
 
4595
- function _makeNetworkReplayBreadcrumb(
4596
- type,
4668
+ async function _prepareFetchData(
4597
4669
  breadcrumb,
4598
4670
  hint,
4671
+ options,
4599
4672
  ) {
4600
4673
  const { startTimestamp, endTimestamp } = hint;
4601
4674
 
4602
- if (!endTimestamp) {
4603
- return null;
4604
- }
4605
-
4606
4675
  const {
4607
4676
  url,
4608
4677
  method,
@@ -4611,32 +4680,122 @@ function _makeNetworkReplayBreadcrumb(
4611
4680
  response_body_size: responseBodySize,
4612
4681
  } = breadcrumb.data;
4613
4682
 
4614
- if (url === undefined) {
4615
- return null;
4616
- }
4683
+ const request = _getRequestInfo(options, hint.input, requestBodySize);
4684
+ const response = await _getResponseInfo(options, hint.response, responseBodySize);
4617
4685
 
4618
- const result = {
4619
- type,
4620
- start: startTimestamp / 1000,
4621
- end: endTimestamp / 1000,
4622
- name: url,
4623
- data: {
4624
- method,
4625
- statusCode,
4626
- },
4686
+ return {
4687
+ startTimestamp,
4688
+ endTimestamp,
4689
+ url,
4690
+ method,
4691
+ statusCode: statusCode || 0,
4692
+ request,
4693
+ response,
4627
4694
  };
4695
+ }
4696
+
4697
+ function _getRequestInfo(
4698
+ { captureBodies },
4699
+ input,
4700
+ requestBodySize,
4701
+ ) {
4702
+ if (!captureBodies) {
4703
+ return buildNetworkRequestOrResponse(requestBodySize, undefined);
4704
+ }
4705
+
4706
+ // We only want to transmit string or string-like bodies
4707
+ const requestBody = _getFetchRequestArgBody(input);
4708
+ const body = getNetworkBody(getBodyString(requestBody));
4709
+ return buildNetworkRequestOrResponse(requestBodySize, body);
4710
+ }
4628
4711
 
4629
- if (requestBodySize) {
4630
- result.data.requestBodySize = requestBodySize;
4712
+ async function _getResponseInfo(
4713
+ { captureBodies, textEncoder },
4714
+ response,
4715
+ responseBodySize,
4716
+ ) {
4717
+ if (!captureBodies && responseBodySize !== undefined) {
4718
+ return buildNetworkRequestOrResponse(responseBodySize, undefined);
4631
4719
  }
4632
- if (responseBodySize) {
4633
- result.data.responseBodySize = responseBodySize;
4720
+
4721
+ // Only clone the response if we need to
4722
+ try {
4723
+ // We have to clone this, as the body can only be read once
4724
+ const res = response.clone();
4725
+ const { body, bodyText } = await _parseFetchBody(res);
4726
+
4727
+ const size =
4728
+ bodyText && bodyText.length && responseBodySize === undefined
4729
+ ? getBodySize(bodyText, textEncoder)
4730
+ : responseBodySize;
4731
+
4732
+ if (captureBodies) {
4733
+ return buildNetworkRequestOrResponse(size, body);
4734
+ }
4735
+
4736
+ return buildNetworkRequestOrResponse(size, undefined);
4737
+ } catch (e) {
4738
+ // fallback
4739
+ return buildNetworkRequestOrResponse(responseBodySize, undefined);
4634
4740
  }
4741
+ }
4635
4742
 
4636
- return result;
4743
+ async function _parseFetchBody(
4744
+ response,
4745
+ ) {
4746
+ let bodyText;
4747
+
4748
+ try {
4749
+ bodyText = await response.text();
4750
+ } catch (e2) {
4751
+ return {};
4752
+ }
4753
+
4754
+ try {
4755
+ const body = JSON.parse(bodyText);
4756
+ return { body, bodyText };
4757
+ } catch (e3) {
4758
+ // just send bodyText
4759
+ }
4760
+
4761
+ return { bodyText, body: bodyText };
4637
4762
  }
4638
4763
 
4639
- function _enrichXhrBreadcrumb(
4764
+ function _getFetchRequestArgBody(fetchArgs = []) {
4765
+ // We only support getting the body from the fetch options
4766
+ if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== 'object') {
4767
+ return undefined;
4768
+ }
4769
+
4770
+ return (fetchArgs[1] ).body;
4771
+ }
4772
+
4773
+ /**
4774
+ * Capture an XHR breadcrumb to a replay.
4775
+ * This adds additional data (where approriate).
4776
+ */
4777
+ async function captureXhrBreadcrumbToReplay(
4778
+ breadcrumb,
4779
+ hint,
4780
+ options,
4781
+ ) {
4782
+ try {
4783
+ const data = _prepareXhrData(breadcrumb, hint, options);
4784
+
4785
+ // Create a replay performance entry from this breadcrumb
4786
+ const result = makeNetworkReplayBreadcrumb('resource.xhr', data);
4787
+ addNetworkBreadcrumb(options.replay, result);
4788
+ } catch (error) {
4789
+ (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.error('[Replay] Failed to capture fetch breadcrumb', error);
4790
+ }
4791
+ }
4792
+
4793
+ /**
4794
+ * Enrich a breadcrumb with additional data.
4795
+ * This has to be sync & mutate the given breadcrumb,
4796
+ * as the breadcrumb is afterwards consumed by other handlers.
4797
+ */
4798
+ function enrichXhrBreadcrumb(
4640
4799
  breadcrumb,
4641
4800
  hint,
4642
4801
  options,
@@ -4645,7 +4804,7 @@ function _enrichXhrBreadcrumb(
4645
4804
 
4646
4805
  const reqSize = getBodySize(input, options.textEncoder);
4647
4806
  const resSize = xhr.getResponseHeader('content-length')
4648
- ? parseContentSizeHeader(xhr.getResponseHeader('content-length'))
4807
+ ? parseContentLengthHeader(xhr.getResponseHeader('content-length'))
4649
4808
  : getBodySize(xhr.response, options.textEncoder);
4650
4809
 
4651
4810
  if (reqSize !== undefined) {
@@ -4656,84 +4815,106 @@ function _enrichXhrBreadcrumb(
4656
4815
  }
4657
4816
  }
4658
4817
 
4659
- function _enrichFetchBreadcrumb(
4818
+ function _prepareXhrData(
4660
4819
  breadcrumb,
4661
4820
  hint,
4662
4821
  options,
4663
4822
  ) {
4664
- const { input, response } = hint;
4823
+ const { startTimestamp, endTimestamp, input } = hint;
4665
4824
 
4666
- const body = getFetchBody(input);
4667
- const reqSize = getBodySize(body, options.textEncoder);
4668
- const resSize = response ? parseContentSizeHeader(response.headers.get('content-length')) : undefined;
4825
+ const {
4826
+ url,
4827
+ method,
4828
+ status_code: statusCode,
4829
+ request_body_size: requestBodySize,
4830
+ response_body_size: responseBodySize,
4831
+ } = breadcrumb.data;
4669
4832
 
4670
- if (reqSize !== undefined) {
4671
- breadcrumb.data.request_body_size = reqSize;
4672
- }
4673
- if (resSize !== undefined) {
4674
- breadcrumb.data.response_body_size = resSize;
4833
+ if (!url) {
4834
+ return null;
4675
4835
  }
4676
- }
4677
4836
 
4678
- /** only exported for tests */
4679
- function getBodySize(
4680
- body,
4681
- textEncoder,
4682
- ) {
4683
- if (!body) {
4684
- return undefined;
4685
- }
4837
+ const request = buildNetworkRequestOrResponse(
4838
+ requestBodySize,
4839
+ options.captureBodies ? getNetworkBody(getBodyString(input)) : undefined,
4840
+ );
4841
+ const response = buildNetworkRequestOrResponse(
4842
+ responseBodySize,
4843
+ options.captureBodies ? getNetworkBody(hint.xhr.responseText) : undefined,
4844
+ );
4686
4845
 
4687
- try {
4688
- if (typeof body === 'string') {
4689
- return textEncoder.encode(body).length;
4690
- }
4846
+ return {
4847
+ startTimestamp,
4848
+ endTimestamp,
4849
+ url,
4850
+ method,
4851
+ statusCode: statusCode || 0,
4852
+ request,
4853
+ response,
4854
+ };
4855
+ }
4691
4856
 
4692
- if (body instanceof URLSearchParams) {
4693
- return textEncoder.encode(body.toString()).length;
4694
- }
4857
+ /**
4858
+ * This method does two things:
4859
+ * - It enriches the regular XHR/fetch breadcrumbs with request/response size data
4860
+ * - It captures the XHR/fetch breadcrumbs to the replay
4861
+ * (enriching it with further data that is _not_ added to the regular breadcrumbs)
4862
+ */
4863
+ function handleNetworkBreadcrumbs(replay) {
4864
+ const client = core.getCurrentHub().getClient();
4695
4865
 
4696
- if (body instanceof FormData) {
4697
- // This is a bit simplified, but gives us a decent estimate
4698
- // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'
4699
- // @ts-ignore passing FormData to URLSearchParams actually works
4700
- const formDataStr = new URLSearchParams(body).toString();
4701
- return textEncoder.encode(formDataStr).length;
4702
- }
4866
+ try {
4867
+ const textEncoder = new TextEncoder();
4703
4868
 
4704
- if (body instanceof Blob) {
4705
- return body.size;
4706
- }
4869
+ const options = {
4870
+ replay,
4871
+ textEncoder,
4872
+ captureBodies: replay.getOptions()._experiments.captureNetworkBodies || false,
4873
+ };
4707
4874
 
4708
- if (body instanceof ArrayBuffer) {
4709
- return body.byteLength;
4875
+ if (client && client.on) {
4876
+ client.on('beforeAddBreadcrumb', (breadcrumb, hint) => beforeAddNetworkBreadcrumb(options, breadcrumb, hint));
4877
+ } else {
4878
+ // Fallback behavior
4879
+ utils.addInstrumentationHandler('fetch', handleFetchSpanListener(replay));
4880
+ utils.addInstrumentationHandler('xhr', handleXhrSpanListener(replay));
4710
4881
  }
4711
-
4712
- // Currently unhandled types: ArrayBufferView, ReadableStream
4713
- } catch (e4) {
4714
- // just return undefined
4882
+ } catch (e2) {
4883
+ // Do nothing
4715
4884
  }
4716
-
4717
- return undefined;
4718
4885
  }
4719
4886
 
4720
- /** only exported for tests */
4721
- function parseContentSizeHeader(header) {
4722
- if (!header) {
4723
- return undefined;
4887
+ /** just exported for tests */
4888
+ function beforeAddNetworkBreadcrumb(
4889
+ options,
4890
+ breadcrumb,
4891
+ hint,
4892
+ ) {
4893
+ if (!breadcrumb.data) {
4894
+ return;
4724
4895
  }
4725
4896
 
4726
- const size = parseInt(header, 10);
4727
- return isNaN(size) ? undefined : size;
4728
- }
4897
+ try {
4898
+ if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) {
4899
+ // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick
4900
+ // Because the hook runs synchronously, and the breadcrumb is afterwards passed on
4901
+ // So any async mutations to it will not be reflected in the final breadcrumb
4902
+ enrichXhrBreadcrumb(breadcrumb, hint, options);
4729
4903
 
4730
- function getFetchBody(fetchArgs = []) {
4731
- // We only support getting the body from the fetch options
4732
- if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== 'object') {
4733
- return undefined;
4734
- }
4904
+ void captureXhrBreadcrumbToReplay(breadcrumb, hint, options);
4905
+ }
4735
4906
 
4736
- return (fetchArgs[1] ).body;
4907
+ if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) {
4908
+ // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick
4909
+ // Because the hook runs synchronously, and the breadcrumb is afterwards passed on
4910
+ // So any async mutations to it will not be reflected in the final breadcrumb
4911
+ enrichFetchBreadcrumb(breadcrumb, hint, options);
4912
+
4913
+ void captureFetchBreadcrumbToReplay(breadcrumb, hint, options);
4914
+ }
4915
+ } catch (e) {
4916
+ (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('Error when enriching network breadcrumb');
4917
+ }
4737
4918
  }
4738
4919
 
4739
4920
  function _isXhrBreadcrumb(breadcrumb) {
@@ -4876,7 +5057,9 @@ function createMemoryEntry(memoryEntry) {
4876
5057
 
4877
5058
  // Map entryType -> function to normalize data for event
4878
5059
  // @ts-ignore TODO: entry type does not fit the create* functions entry type
4879
- const ENTRY_TYPES = {
5060
+ const ENTRY_TYPES
5061
+
5062
+ = {
4880
5063
  // @ts-ignore TODO: entry type does not fit the create* functions entry type
4881
5064
  resource: createResourceEntry,
4882
5065
  paint: createPaintEntry,
@@ -4889,7 +5072,9 @@ const ENTRY_TYPES = {
4889
5072
  /**
4890
5073
  * Create replay performance entries from the browser performance entries.
4891
5074
  */
4892
- function createPerformanceEntries(entries) {
5075
+ function createPerformanceEntries(
5076
+ entries,
5077
+ ) {
4893
5078
  return entries.map(createPerformanceEntry).filter(Boolean) ;
4894
5079
  }
4895
5080
 
@@ -4907,8 +5092,6 @@ function getAbsoluteTime(time) {
4907
5092
  return ((utils.browserPerformanceTimeOrigin || WINDOW.performance.timeOrigin) + time) / 1000;
4908
5093
  }
4909
5094
 
4910
- // TODO: type definition!
4911
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4912
5095
  function createPaintEntry(entry) {
4913
5096
  const { duration, entryType, name, startTime } = entry;
4914
5097
 
@@ -4918,14 +5101,28 @@ function createPaintEntry(entry) {
4918
5101
  name,
4919
5102
  start,
4920
5103
  end: start + duration,
5104
+ data: undefined,
4921
5105
  };
4922
5106
  }
4923
5107
 
4924
- // TODO: type definition!
4925
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4926
5108
  function createNavigationEntry(entry) {
4927
- // TODO: There looks to be some more interesting bits in here (domComplete, domContentLoaded)
4928
- const { entryType, name, duration, domComplete, startTime, transferSize, type } = entry;
5109
+ const {
5110
+ entryType,
5111
+ name,
5112
+ decodedBodySize,
5113
+ duration,
5114
+ domComplete,
5115
+ encodedBodySize,
5116
+ domContentLoadedEventStart,
5117
+ domContentLoadedEventEnd,
5118
+ domInteractive,
5119
+ loadEventStart,
5120
+ loadEventEnd,
5121
+ redirectCount,
5122
+ startTime,
5123
+ transferSize,
5124
+ type,
5125
+ } = entry;
4929
5126
 
4930
5127
  // Ignore entries with no duration, they do not seem to be useful and cause dupes
4931
5128
  if (duration === 0) {
@@ -4939,15 +5136,34 @@ function createNavigationEntry(entry) {
4939
5136
  name,
4940
5137
  data: {
4941
5138
  size: transferSize,
5139
+ decodedBodySize,
5140
+ encodedBodySize,
4942
5141
  duration,
5142
+ domInteractive,
5143
+ domContentLoadedEventStart,
5144
+ domContentLoadedEventEnd,
5145
+ loadEventStart,
5146
+ loadEventEnd,
5147
+ domComplete,
5148
+ redirectCount,
4943
5149
  },
4944
5150
  };
4945
5151
  }
4946
5152
 
4947
- // TODO: type definition!
4948
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4949
- function createResourceEntry(entry) {
4950
- const { entryType, initiatorType, name, responseEnd, startTime, encodedBodySize, transferSize } = entry;
5153
+ function createResourceEntry(
5154
+ entry,
5155
+ ) {
5156
+ const {
5157
+ entryType,
5158
+ initiatorType,
5159
+ name,
5160
+ responseEnd,
5161
+ startTime,
5162
+ decodedBodySize,
5163
+ encodedBodySize,
5164
+ responseStatus,
5165
+ transferSize,
5166
+ } = entry;
4951
5167
 
4952
5168
  // Core SDK handles these
4953
5169
  if (['fetch', 'xmlhttprequest'].includes(initiatorType)) {
@@ -4961,14 +5177,16 @@ function createResourceEntry(entry) {
4961
5177
  name,
4962
5178
  data: {
4963
5179
  size: transferSize,
5180
+ statusCode: responseStatus,
5181
+ decodedBodySize,
4964
5182
  encodedBodySize,
4965
5183
  },
4966
5184
  };
4967
5185
  }
4968
5186
 
4969
- // TODO: type definition!
4970
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4971
- function createLargestContentfulPaint(entry) {
5187
+ function createLargestContentfulPaint(
5188
+ entry,
5189
+ ) {
4972
5190
  const { entryType, startTime, size } = entry;
4973
5191
 
4974
5192
  let startTimeOrNavigationActivation = 0;
@@ -5282,7 +5500,6 @@ async function sendReplayRequest({
5282
5500
  }
5283
5501
 
5284
5502
  const baseEvent = {
5285
- // @ts-ignore private api
5286
5503
  type: REPLAY_EVENT_NAME,
5287
5504
  ...(includeReplayStartTimestamp ? { replay_start_timestamp: initialTimestamp / 1000 } : {}),
5288
5505
  timestamp: timestamp / 1000,
@@ -5536,7 +5753,7 @@ class ReplayContainer {
5536
5753
  recordingOptions,
5537
5754
  }
5538
5755
 
5539
- ) {ReplayContainer.prototype.__init.call(this);ReplayContainer.prototype.__init2.call(this);ReplayContainer.prototype.__init3.call(this);ReplayContainer.prototype.__init4.call(this);ReplayContainer.prototype.__init5.call(this);ReplayContainer.prototype.__init6.call(this);ReplayContainer.prototype.__init7.call(this);ReplayContainer.prototype.__init8.call(this);ReplayContainer.prototype.__init9.call(this);ReplayContainer.prototype.__init10.call(this);ReplayContainer.prototype.__init11.call(this);ReplayContainer.prototype.__init12.call(this);ReplayContainer.prototype.__init13.call(this);ReplayContainer.prototype.__init14.call(this);ReplayContainer.prototype.__init15.call(this);ReplayContainer.prototype.__init16.call(this);
5756
+ ) {ReplayContainer.prototype.__init.call(this);ReplayContainer.prototype.__init2.call(this);ReplayContainer.prototype.__init3.call(this);ReplayContainer.prototype.__init4.call(this);ReplayContainer.prototype.__init5.call(this);ReplayContainer.prototype.__init6.call(this);ReplayContainer.prototype.__init7.call(this);ReplayContainer.prototype.__init8.call(this);ReplayContainer.prototype.__init9.call(this);ReplayContainer.prototype.__init10.call(this);ReplayContainer.prototype.__init11.call(this);ReplayContainer.prototype.__init12.call(this);ReplayContainer.prototype.__init13.call(this);ReplayContainer.prototype.__init14.call(this);ReplayContainer.prototype.__init15.call(this);ReplayContainer.prototype.__init16.call(this);ReplayContainer.prototype.__init17.call(this);
5540
5757
  this._recordingOptions = recordingOptions;
5541
5758
  this._options = options;
5542
5759
 
@@ -5625,23 +5842,7 @@ class ReplayContainer {
5625
5842
  // instead, we'll always keep the last 60 seconds of replay before an error happened
5626
5843
  ...(this.recordingMode === 'error' && { checkoutEveryNms: ERROR_CHECKOUT_TIME }),
5627
5844
  emit: getHandleRecordingEmit(this),
5628
- onMutation: (mutations) => {
5629
- if (this._options._experiments.captureMutationSize) {
5630
- const count = mutations.length;
5631
-
5632
- if (count > 500) {
5633
- const breadcrumb = createBreadcrumb({
5634
- category: 'replay.mutations',
5635
- data: {
5636
- count,
5637
- },
5638
- });
5639
- this._createCustomBreadcrumb(breadcrumb);
5640
- }
5641
- }
5642
- // `true` means we use the regular mutation handling by rrweb
5643
- return true;
5644
- },
5845
+ onMutation: this._onMutationHandler,
5645
5846
  });
5646
5847
  } catch (err) {
5647
5848
  this._handleException(err);
@@ -6040,10 +6241,10 @@ class ReplayContainer {
6040
6241
  * Trigger rrweb to take a full snapshot which will cause this plugin to
6041
6242
  * create a new Replay event.
6042
6243
  */
6043
- _triggerFullSnapshot() {
6244
+ _triggerFullSnapshot(checkout = true) {
6044
6245
  try {
6045
6246
  (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.log('[Replay] Taking full rrweb snapshot');
6046
- record.takeFullSnapshot(true);
6247
+ record.takeFullSnapshot(checkout);
6047
6248
  } catch (err) {
6048
6249
  this._handleException(err);
6049
6250
  }
@@ -6257,6 +6458,37 @@ class ReplayContainer {
6257
6458
  saveSession(this.session);
6258
6459
  }
6259
6460
  }
6461
+
6462
+ /** Handler for rrweb.record.onMutation */
6463
+ __init17() {this._onMutationHandler = (mutations) => {
6464
+ const count = mutations.length;
6465
+
6466
+ const mutationLimit = this._options._experiments.mutationLimit || 0;
6467
+ const mutationBreadcrumbLimit = this._options._experiments.mutationBreadcrumbLimit || 1000;
6468
+ const overMutationLimit = mutationLimit && count > mutationLimit;
6469
+
6470
+ // Create a breadcrumb if a lot of mutations happen at the same time
6471
+ // We can show this in the UI as an information with potential performance improvements
6472
+ if (count > mutationBreadcrumbLimit || overMutationLimit) {
6473
+ const breadcrumb = createBreadcrumb({
6474
+ category: 'replay.mutations',
6475
+ data: {
6476
+ count,
6477
+ },
6478
+ });
6479
+ this._createCustomBreadcrumb(breadcrumb);
6480
+ }
6481
+
6482
+ if (overMutationLimit) {
6483
+ // We want to skip doing an incremental snapshot if there are too many mutations
6484
+ // Instead, we do a full snapshot
6485
+ this._triggerFullSnapshot(false);
6486
+ return false;
6487
+ }
6488
+
6489
+ // `true` means we use the regular mutation handling by rrweb
6490
+ return true;
6491
+ };}
6260
6492
  }
6261
6493
 
6262
6494
  function getOption(