@sentry/replay 7.44.2 → 7.46.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 (37) hide show
  1. package/cjs/index.js +471 -181
  2. package/cjs/index.js.map +1 -1
  3. package/esm/index.js +472 -182
  4. package/esm/index.js.map +1 -1
  5. package/package.json +5 -5
  6. package/types/constants.d.ts +1 -0
  7. package/types/constants.d.ts.map +1 -1
  8. package/types/coreHandlers/handleFetch.d.ts +2 -2
  9. package/types/coreHandlers/handleFetch.d.ts.map +1 -1
  10. package/types/coreHandlers/handleHistory.d.ts.map +1 -1
  11. package/types/coreHandlers/handleNetworkBreadcrumbs.d.ts +6 -12
  12. package/types/coreHandlers/handleNetworkBreadcrumbs.d.ts.map +1 -1
  13. package/types/coreHandlers/handleXhr.d.ts +2 -2
  14. package/types/coreHandlers/handleXhr.d.ts.map +1 -1
  15. package/types/coreHandlers/{addBreadcrumbEvent.d.ts → util/addBreadcrumbEvent.d.ts} +1 -1
  16. package/types/coreHandlers/util/addBreadcrumbEvent.d.ts.map +1 -0
  17. package/types/coreHandlers/util/addNetworkBreadcrumb.d.ts +4 -0
  18. package/types/coreHandlers/util/addNetworkBreadcrumb.d.ts.map +1 -0
  19. package/types/coreHandlers/util/fetchUtils.d.ts +24 -0
  20. package/types/coreHandlers/util/fetchUtils.d.ts.map +1 -0
  21. package/types/coreHandlers/util/networkUtils.d.ts +15 -0
  22. package/types/coreHandlers/util/networkUtils.d.ts.map +1 -0
  23. package/types/coreHandlers/util/xhrUtils.d.ts +23 -0
  24. package/types/coreHandlers/util/xhrUtils.d.ts.map +1 -0
  25. package/types/replay.d.ts +2 -0
  26. package/types/replay.d.ts.map +1 -1
  27. package/types/types.d.ts +126 -5
  28. package/types/types.d.ts.map +1 -1
  29. package/types/util/addGlobalListeners.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/coreHandlers/addBreadcrumbEvent.d.ts.map +0 -1
  36. package/types/coreHandlers/addNetworkBreadcrumb.d.ts +0 -4
  37. package/types/coreHandlers/addNetworkBreadcrumb.d.ts.map +0 -1
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, addInstrumentationHandler, browserPerformanceTimeOrigin, createEnvelope, createEventEnvelopeHeaders, getSdkMetadataForEnvelopeHeader, isNodeEnv, dropUndefinedKeys } from '@sentry/utils';
2
+ import { GLOBAL_OBJ, logger, uuid4, htmlTreeAsString, 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
@@ -29,6 +29,9 @@ const ERROR_CHECKOUT_TIME = 60000;
29
29
  const RETRY_BASE_INTERVAL = 5000;
30
30
  const RETRY_MAX_COUNT = 3;
31
31
 
32
+ /* The max (uncompressed) size in bytes of a network body. Any body larger than this will be dropped. */
33
+ const NETWORK_BODY_MAX_SIZE = 300000;
34
+
32
35
  var NodeType$1;
33
36
  (function (NodeType) {
34
37
  NodeType[NodeType["Document"] = 0] = "Document";
@@ -4366,7 +4369,10 @@ function _isSentryRequest(url) {
4366
4369
  }
4367
4370
 
4368
4371
  /** Add a performance entry breadcrumb */
4369
- function addNetworkBreadcrumb(replay, result) {
4372
+ function addNetworkBreadcrumb(
4373
+ replay,
4374
+ result,
4375
+ ) {
4370
4376
  if (!replay.isEnabled()) {
4371
4377
  return;
4372
4378
  }
@@ -4396,18 +4402,17 @@ function handleFetch(handlerData) {
4396
4402
  return null;
4397
4403
  }
4398
4404
 
4399
- const { method, request_body_size: requestBodySize, response_body_size: responseBodySize } = fetchData;
4405
+ // This is only used as a fallback, so we know the body sizes are never set here
4406
+ const { method, url } = fetchData;
4400
4407
 
4401
4408
  return {
4402
4409
  type: 'resource.fetch',
4403
4410
  start: startTimestamp / 1000,
4404
4411
  end: endTimestamp / 1000,
4405
- name: fetchData.url,
4412
+ name: url,
4406
4413
  data: {
4407
4414
  method,
4408
4415
  statusCode: response && (response ).status,
4409
- requestBodySize,
4410
- responseBodySize,
4411
4416
  },
4412
4417
  };
4413
4418
  }
@@ -4435,13 +4440,8 @@ function handleXhr(handlerData) {
4435
4440
  return null;
4436
4441
  }
4437
4442
 
4438
- const {
4439
- method,
4440
- url,
4441
- status_code: statusCode,
4442
- request_body_size: requestBodySize,
4443
- response_body_size: responseBodySize,
4444
- } = xhr.__sentry_xhr__;
4443
+ // 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__;
4445
4445
 
4446
4446
  if (url === undefined) {
4447
4447
  return null;
@@ -4455,8 +4455,6 @@ function handleXhr(handlerData) {
4455
4455
  data: {
4456
4456
  method,
4457
4457
  statusCode,
4458
- requestBodySize,
4459
- responseBodySize,
4460
4458
  },
4461
4459
  };
4462
4460
  }
@@ -4476,145 +4474,186 @@ function handleXhrSpanListener(replay) {
4476
4474
  };
4477
4475
  }
4478
4476
 
4479
- /**
4480
- * This will enrich the xhr/fetch breadcrumbs with additional information.
4481
- *
4482
- * This adds:
4483
- * * request_body_size
4484
- * * response_body_size
4485
- *
4486
- * to the breadcrumb data.
4487
- */
4488
- function handleNetworkBreadcrumbs(replay) {
4489
- const client = getCurrentHub().getClient();
4477
+ /** Get the size of a body. */
4478
+ function getBodySize(
4479
+ body,
4480
+ textEncoder,
4481
+ ) {
4482
+ if (!body) {
4483
+ return undefined;
4484
+ }
4490
4485
 
4491
4486
  try {
4492
- const textEncoder = new TextEncoder();
4487
+ if (typeof body === 'string') {
4488
+ return textEncoder.encode(body).length;
4489
+ }
4493
4490
 
4494
- const options = {
4495
- replay,
4496
- textEncoder,
4497
- };
4491
+ if (body instanceof URLSearchParams) {
4492
+ return textEncoder.encode(body.toString()).length;
4493
+ }
4498
4494
 
4499
- if (client && client.on) {
4500
- client.on('beforeAddBreadcrumb', (breadcrumb, hint) => handleNetworkBreadcrumb(options, breadcrumb, hint));
4501
- } else {
4502
- // Fallback behavior
4503
- addInstrumentationHandler('fetch', handleFetchSpanListener(replay));
4504
- addInstrumentationHandler('xhr', handleXhrSpanListener(replay));
4495
+ if (body instanceof FormData) {
4496
+ const formDataStr = _serializeFormData(body);
4497
+ return textEncoder.encode(formDataStr).length;
4505
4498
  }
4506
- } catch (e2) {
4507
- // Do nothing
4499
+
4500
+ if (body instanceof Blob) {
4501
+ return body.size;
4502
+ }
4503
+
4504
+ if (body instanceof ArrayBuffer) {
4505
+ return body.byteLength;
4506
+ }
4507
+
4508
+ // Currently unhandled types: ArrayBufferView, ReadableStream
4509
+ } catch (e) {
4510
+ // just return undefined
4508
4511
  }
4512
+
4513
+ return undefined;
4509
4514
  }
4510
4515
 
4511
- /** just exported for tests */
4512
- function handleNetworkBreadcrumb(
4513
- options,
4514
- breadcrumb,
4515
- hint,
4516
- ) {
4517
- if (!breadcrumb.data) {
4518
- return;
4516
+ /** Convert a Content-Length header to number/undefined. */
4517
+ function parseContentLengthHeader(header) {
4518
+ if (!header) {
4519
+ return undefined;
4519
4520
  }
4520
4521
 
4521
- try {
4522
- if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) {
4523
- // Enriches the breadcrumb overall
4524
- _enrichXhrBreadcrumb(breadcrumb, hint, options);
4522
+ const size = parseInt(header, 10);
4523
+ return isNaN(size) ? undefined : size;
4524
+ }
4525
4525
 
4526
- // Create a replay performance entry from this breadcrumb
4527
- const result = _makeNetworkReplayBreadcrumb('resource.xhr', breadcrumb, hint);
4528
- addNetworkBreadcrumb(options.replay, result);
4529
- }
4526
+ /** Get the string representation of a body. */
4527
+ function getBodyString(body) {
4528
+ if (typeof body === 'string') {
4529
+ return body;
4530
+ }
4530
4531
 
4531
- if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) {
4532
- // Enriches the breadcrumb overall
4533
- _enrichFetchBreadcrumb(breadcrumb, hint, options);
4532
+ if (body instanceof URLSearchParams) {
4533
+ return body.toString();
4534
+ }
4534
4535
 
4535
- // Create a replay performance entry from this breadcrumb
4536
- const result = _makeNetworkReplayBreadcrumb('resource.fetch', breadcrumb, hint);
4537
- addNetworkBreadcrumb(options.replay, result);
4538
- }
4539
- } catch (e) {
4540
- (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Error when enriching network breadcrumb');
4536
+ if (body instanceof FormData) {
4537
+ return _serializeFormData(body);
4541
4538
  }
4539
+
4540
+ return undefined;
4542
4541
  }
4543
4542
 
4544
- function _makeNetworkReplayBreadcrumb(
4543
+ /** Convert ReplayNetworkRequestData to a PerformanceEntry. */
4544
+ function makeNetworkReplayBreadcrumb(
4545
4545
  type,
4546
- breadcrumb,
4547
- hint,
4546
+ data,
4548
4547
  ) {
4549
- const { startTimestamp, endTimestamp } = hint;
4550
-
4551
- if (!endTimestamp) {
4548
+ if (!data) {
4552
4549
  return null;
4553
4550
  }
4554
4551
 
4555
- const {
4556
- url,
4557
- method,
4558
- status_code: statusCode,
4559
- request_body_size: requestBodySize,
4560
- response_body_size: responseBodySize,
4561
- } = breadcrumb.data;
4562
-
4563
- if (url === undefined) {
4564
- return null;
4565
- }
4552
+ const { startTimestamp, endTimestamp, url, method, statusCode, request, response } = data;
4566
4553
 
4567
4554
  const result = {
4568
4555
  type,
4569
4556
  start: startTimestamp / 1000,
4570
4557
  end: endTimestamp / 1000,
4571
4558
  name: url,
4572
- data: {
4559
+ data: dropUndefinedKeys({
4573
4560
  method,
4574
4561
  statusCode,
4575
- },
4562
+ request,
4563
+ response,
4564
+ }),
4576
4565
  };
4577
4566
 
4578
- if (requestBodySize) {
4579
- result.data.requestBodySize = requestBodySize;
4567
+ return result;
4568
+ }
4569
+
4570
+ /** Get either a JSON network body, or a text representation. */
4571
+ function getNetworkBody(bodyText) {
4572
+ if (!bodyText) {
4573
+ return;
4580
4574
  }
4581
- if (responseBodySize) {
4582
- result.data.responseBodySize = responseBodySize;
4575
+
4576
+ try {
4577
+ return JSON.parse(bodyText);
4578
+ } catch (e2) {
4579
+ // return text
4583
4580
  }
4584
4581
 
4585
- return result;
4582
+ return bodyText;
4583
+ }
4584
+
4585
+ /** Build the request or response part of a replay network breadcrumb. */
4586
+ function buildNetworkRequestOrResponse(
4587
+ bodySize,
4588
+ body,
4589
+ ) {
4590
+ if (!bodySize) {
4591
+ return undefined;
4592
+ }
4593
+
4594
+ if (!body) {
4595
+ return {
4596
+ size: bodySize,
4597
+ };
4598
+ }
4599
+
4600
+ const info = {
4601
+ size: bodySize,
4602
+ };
4603
+
4604
+ if (bodySize < NETWORK_BODY_MAX_SIZE) {
4605
+ info.body = body;
4606
+ } else {
4607
+ info._meta = {
4608
+ errors: ['MAX_BODY_SIZE_EXCEEDED'],
4609
+ };
4610
+ }
4611
+
4612
+ return info;
4613
+ }
4614
+
4615
+ function _serializeFormData(formData) {
4616
+ // This is a bit simplified, but gives us a decent estimate
4617
+ // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'
4618
+ // @ts-ignore passing FormData to URLSearchParams actually works
4619
+ return new URLSearchParams(formData).toString();
4586
4620
  }
4587
4621
 
4588
- function _enrichXhrBreadcrumb(
4622
+ /**
4623
+ * Capture a fetch breadcrumb to a replay.
4624
+ * This adds additional data (where approriate).
4625
+ */
4626
+ async function captureFetchBreadcrumbToReplay(
4589
4627
  breadcrumb,
4590
4628
  hint,
4591
4629
  options,
4592
4630
  ) {
4593
- const { xhr, input } = hint;
4594
-
4595
- const reqSize = getBodySize(input, options.textEncoder);
4596
- const resSize = xhr.getResponseHeader('content-length')
4597
- ? parseContentSizeHeader(xhr.getResponseHeader('content-length'))
4598
- : getBodySize(xhr.response, options.textEncoder);
4631
+ try {
4632
+ const data = await _prepareFetchData(breadcrumb, hint, options);
4599
4633
 
4600
- if (reqSize !== undefined) {
4601
- breadcrumb.data.request_body_size = reqSize;
4602
- }
4603
- if (resSize !== undefined) {
4604
- breadcrumb.data.response_body_size = resSize;
4634
+ // Create a replay performance entry from this breadcrumb
4635
+ const result = makeNetworkReplayBreadcrumb('resource.fetch', data);
4636
+ addNetworkBreadcrumb(options.replay, result);
4637
+ } catch (error) {
4638
+ (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('[Replay] Failed to capture fetch breadcrumb', error);
4605
4639
  }
4606
4640
  }
4607
4641
 
4608
- function _enrichFetchBreadcrumb(
4642
+ /**
4643
+ * Enrich a breadcrumb with additional data.
4644
+ * This has to be sync & mutate the given breadcrumb,
4645
+ * as the breadcrumb is afterwards consumed by other handlers.
4646
+ */
4647
+ function enrichFetchBreadcrumb(
4609
4648
  breadcrumb,
4610
4649
  hint,
4611
4650
  options,
4612
4651
  ) {
4613
4652
  const { input, response } = hint;
4614
4653
 
4615
- const body = getFetchBody(input);
4654
+ const body = _getFetchRequestArgBody(input);
4616
4655
  const reqSize = getBodySize(body, options.textEncoder);
4617
- const resSize = response ? parseContentSizeHeader(response.headers.get('content-length')) : undefined;
4656
+ const resSize = response ? parseContentLengthHeader(response.headers.get('content-length')) : undefined;
4618
4657
 
4619
4658
  if (reqSize !== undefined) {
4620
4659
  breadcrumb.data.request_body_size = reqSize;
@@ -4624,59 +4663,103 @@ function _enrichFetchBreadcrumb(
4624
4663
  }
4625
4664
  }
4626
4665
 
4627
- /** only exported for tests */
4628
- function getBodySize(
4629
- body,
4630
- textEncoder,
4666
+ async function _prepareFetchData(
4667
+ breadcrumb,
4668
+ hint,
4669
+ options,
4631
4670
  ) {
4632
- if (!body) {
4633
- return undefined;
4671
+ const { startTimestamp, endTimestamp } = hint;
4672
+
4673
+ const {
4674
+ url,
4675
+ method,
4676
+ status_code: statusCode,
4677
+ request_body_size: requestBodySize,
4678
+ response_body_size: responseBodySize,
4679
+ } = breadcrumb.data;
4680
+
4681
+ const request = _getRequestInfo(options, hint.input, requestBodySize);
4682
+ const response = await _getResponseInfo(options, hint.response, responseBodySize);
4683
+
4684
+ return {
4685
+ startTimestamp,
4686
+ endTimestamp,
4687
+ url,
4688
+ method,
4689
+ statusCode: statusCode || 0,
4690
+ request,
4691
+ response,
4692
+ };
4693
+ }
4694
+
4695
+ function _getRequestInfo(
4696
+ { captureBodies },
4697
+ input,
4698
+ requestBodySize,
4699
+ ) {
4700
+ if (!captureBodies) {
4701
+ return buildNetworkRequestOrResponse(requestBodySize, undefined);
4634
4702
  }
4635
4703
 
4636
- try {
4637
- if (typeof body === 'string') {
4638
- return textEncoder.encode(body).length;
4639
- }
4704
+ // We only want to transmit string or string-like bodies
4705
+ const requestBody = _getFetchRequestArgBody(input);
4706
+ const body = getNetworkBody(getBodyString(requestBody));
4707
+ return buildNetworkRequestOrResponse(requestBodySize, body);
4708
+ }
4640
4709
 
4641
- if (body instanceof URLSearchParams) {
4642
- return textEncoder.encode(body.toString()).length;
4643
- }
4710
+ async function _getResponseInfo(
4711
+ { captureBodies, textEncoder },
4712
+ response,
4713
+ responseBodySize,
4714
+ ) {
4715
+ if (!captureBodies && responseBodySize !== undefined) {
4716
+ return buildNetworkRequestOrResponse(responseBodySize, undefined);
4717
+ }
4644
4718
 
4645
- if (body instanceof FormData) {
4646
- // This is a bit simplified, but gives us a decent estimate
4647
- // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'
4648
- // @ts-ignore passing FormData to URLSearchParams actually works
4649
- const formDataStr = new URLSearchParams(body).toString();
4650
- return textEncoder.encode(formDataStr).length;
4651
- }
4719
+ // Only clone the response if we need to
4720
+ try {
4721
+ // We have to clone this, as the body can only be read once
4722
+ const res = response.clone();
4723
+ const { body, bodyText } = await _parseFetchBody(res);
4652
4724
 
4653
- if (body instanceof Blob) {
4654
- return body.size;
4655
- }
4725
+ const size =
4726
+ bodyText && bodyText.length && responseBodySize === undefined
4727
+ ? getBodySize(bodyText, textEncoder)
4728
+ : responseBodySize;
4656
4729
 
4657
- if (body instanceof ArrayBuffer) {
4658
- return body.byteLength;
4730
+ if (captureBodies) {
4731
+ return buildNetworkRequestOrResponse(size, body);
4659
4732
  }
4660
4733
 
4661
- // Currently unhandled types: ArrayBufferView, ReadableStream
4662
- } catch (e3) {
4663
- // just return undefined
4734
+ return buildNetworkRequestOrResponse(size, undefined);
4735
+ } catch (e) {
4736
+ // fallback
4737
+ return buildNetworkRequestOrResponse(responseBodySize, undefined);
4664
4738
  }
4665
-
4666
- return undefined;
4667
4739
  }
4668
4740
 
4669
- /** only exported for tests */
4670
- function parseContentSizeHeader(header) {
4671
- if (!header) {
4672
- return undefined;
4741
+ async function _parseFetchBody(
4742
+ response,
4743
+ ) {
4744
+ let bodyText;
4745
+
4746
+ try {
4747
+ bodyText = await response.text();
4748
+ } catch (e2) {
4749
+ return {};
4673
4750
  }
4674
4751
 
4675
- const size = parseInt(header, 10);
4676
- return isNaN(size) ? undefined : size;
4752
+ try {
4753
+ const body = JSON.parse(bodyText);
4754
+ return { body, bodyText };
4755
+ } catch (e3) {
4756
+ // just send bodyText
4757
+ }
4758
+
4759
+ return { bodyText, body: bodyText };
4677
4760
  }
4678
4761
 
4679
- function getFetchBody(fetchArgs = []) {
4762
+ function _getFetchRequestArgBody(fetchArgs = []) {
4680
4763
  // We only support getting the body from the fetch options
4681
4764
  if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== 'object') {
4682
4765
  return undefined;
@@ -4685,6 +4768,153 @@ function getFetchBody(fetchArgs = []) {
4685
4768
  return (fetchArgs[1] ).body;
4686
4769
  }
4687
4770
 
4771
+ /**
4772
+ * Capture an XHR breadcrumb to a replay.
4773
+ * This adds additional data (where approriate).
4774
+ */
4775
+ async function captureXhrBreadcrumbToReplay(
4776
+ breadcrumb,
4777
+ hint,
4778
+ options,
4779
+ ) {
4780
+ try {
4781
+ const data = _prepareXhrData(breadcrumb, hint, options);
4782
+
4783
+ // Create a replay performance entry from this breadcrumb
4784
+ const result = makeNetworkReplayBreadcrumb('resource.xhr', data);
4785
+ addNetworkBreadcrumb(options.replay, result);
4786
+ } catch (error) {
4787
+ (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('[Replay] Failed to capture fetch breadcrumb', error);
4788
+ }
4789
+ }
4790
+
4791
+ /**
4792
+ * Enrich a breadcrumb with additional data.
4793
+ * This has to be sync & mutate the given breadcrumb,
4794
+ * as the breadcrumb is afterwards consumed by other handlers.
4795
+ */
4796
+ function enrichXhrBreadcrumb(
4797
+ breadcrumb,
4798
+ hint,
4799
+ options,
4800
+ ) {
4801
+ const { xhr, input } = hint;
4802
+
4803
+ const reqSize = getBodySize(input, options.textEncoder);
4804
+ const resSize = xhr.getResponseHeader('content-length')
4805
+ ? parseContentLengthHeader(xhr.getResponseHeader('content-length'))
4806
+ : getBodySize(xhr.response, options.textEncoder);
4807
+
4808
+ if (reqSize !== undefined) {
4809
+ breadcrumb.data.request_body_size = reqSize;
4810
+ }
4811
+ if (resSize !== undefined) {
4812
+ breadcrumb.data.response_body_size = resSize;
4813
+ }
4814
+ }
4815
+
4816
+ function _prepareXhrData(
4817
+ breadcrumb,
4818
+ hint,
4819
+ options,
4820
+ ) {
4821
+ const { startTimestamp, endTimestamp, input } = hint;
4822
+
4823
+ const {
4824
+ url,
4825
+ method,
4826
+ status_code: statusCode,
4827
+ request_body_size: requestBodySize,
4828
+ response_body_size: responseBodySize,
4829
+ } = breadcrumb.data;
4830
+
4831
+ if (!url) {
4832
+ return null;
4833
+ }
4834
+
4835
+ const request = buildNetworkRequestOrResponse(
4836
+ requestBodySize,
4837
+ options.captureBodies ? getNetworkBody(getBodyString(input)) : undefined,
4838
+ );
4839
+ const response = buildNetworkRequestOrResponse(
4840
+ responseBodySize,
4841
+ options.captureBodies ? getNetworkBody(hint.xhr.responseText) : undefined,
4842
+ );
4843
+
4844
+ return {
4845
+ startTimestamp,
4846
+ endTimestamp,
4847
+ url,
4848
+ method,
4849
+ statusCode: statusCode || 0,
4850
+ request,
4851
+ response,
4852
+ };
4853
+ }
4854
+
4855
+ /**
4856
+ * This method does two things:
4857
+ * - It enriches the regular XHR/fetch breadcrumbs with request/response size data
4858
+ * - It captures the XHR/fetch breadcrumbs to the replay
4859
+ * (enriching it with further data that is _not_ added to the regular breadcrumbs)
4860
+ */
4861
+ function handleNetworkBreadcrumbs(replay) {
4862
+ const client = getCurrentHub().getClient();
4863
+
4864
+ try {
4865
+ const textEncoder = new TextEncoder();
4866
+
4867
+ const options = {
4868
+ replay,
4869
+ textEncoder,
4870
+ captureBodies: replay.getOptions()._experiments.captureNetworkBodies || false,
4871
+ };
4872
+
4873
+ if (client && client.on) {
4874
+ client.on('beforeAddBreadcrumb', (breadcrumb, hint) => beforeAddNetworkBreadcrumb(options, breadcrumb, hint));
4875
+ } else {
4876
+ // Fallback behavior
4877
+ addInstrumentationHandler('fetch', handleFetchSpanListener(replay));
4878
+ addInstrumentationHandler('xhr', handleXhrSpanListener(replay));
4879
+ }
4880
+ } catch (e2) {
4881
+ // Do nothing
4882
+ }
4883
+ }
4884
+
4885
+ /** just exported for tests */
4886
+ function beforeAddNetworkBreadcrumb(
4887
+ options,
4888
+ breadcrumb,
4889
+ hint,
4890
+ ) {
4891
+ if (!breadcrumb.data) {
4892
+ return;
4893
+ }
4894
+
4895
+ try {
4896
+ if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) {
4897
+ // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick
4898
+ // Because the hook runs synchronously, and the breadcrumb is afterwards passed on
4899
+ // So any async mutations to it will not be reflected in the final breadcrumb
4900
+ enrichXhrBreadcrumb(breadcrumb, hint, options);
4901
+
4902
+ void captureXhrBreadcrumbToReplay(breadcrumb, hint, options);
4903
+ }
4904
+
4905
+ if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) {
4906
+ // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick
4907
+ // Because the hook runs synchronously, and the breadcrumb is afterwards passed on
4908
+ // So any async mutations to it will not be reflected in the final breadcrumb
4909
+ enrichFetchBreadcrumb(breadcrumb, hint, options);
4910
+
4911
+ void captureFetchBreadcrumbToReplay(breadcrumb, hint, options);
4912
+ }
4913
+ } catch (e) {
4914
+ (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Error when enriching network breadcrumb');
4915
+ }
4916
+ }
4917
+
4688
4918
  function _isXhrBreadcrumb(breadcrumb) {
4689
4919
  return breadcrumb.category === 'xhr';
4690
4920
  }
@@ -4763,19 +4993,27 @@ function addGlobalListeners(replay) {
4763
4993
  addInstrumentationHandler('history', handleHistorySpanListener(replay));
4764
4994
  handleNetworkBreadcrumbs(replay);
4765
4995
 
4766
- // If a custom client has no hooks yet, we continue to use the "old" implementation
4767
- const hasHooks = !!(client && client.on);
4768
-
4769
4996
  // Tag all (non replay) events that get sent to Sentry with the current
4770
4997
  // replay ID so that we can reference them later in the UI
4771
- addGlobalEventProcessor(handleGlobalEventListener(replay, !hasHooks));
4998
+ addGlobalEventProcessor(handleGlobalEventListener(replay, !hasHooks(client)));
4772
4999
 
4773
- if (hasHooks) {
4774
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4775
- (client ).on('afterSendEvent', handleAfterSendEvent(replay));
5000
+ // If a custom client has no hooks yet, we continue to use the "old" implementation
5001
+ if (hasHooks(client)) {
5002
+ client.on('afterSendEvent', handleAfterSendEvent(replay));
5003
+ client.on('createDsc', (dsc) => {
5004
+ const replayId = replay.getSessionId();
5005
+ if (replayId) {
5006
+ dsc.replay_id = replayId;
5007
+ }
5008
+ });
4776
5009
  }
4777
5010
  }
4778
5011
 
5012
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5013
+ function hasHooks(client) {
5014
+ return !!(client && client.on);
5015
+ }
5016
+
4779
5017
  /**
4780
5018
  * Create a "span" for the total amount of memory being used by JS objects
4781
5019
  * (including v8 internal objects).
@@ -4817,7 +5055,9 @@ function createMemoryEntry(memoryEntry) {
4817
5055
 
4818
5056
  // Map entryType -> function to normalize data for event
4819
5057
  // @ts-ignore TODO: entry type does not fit the create* functions entry type
4820
- const ENTRY_TYPES = {
5058
+ const ENTRY_TYPES
5059
+
5060
+ = {
4821
5061
  // @ts-ignore TODO: entry type does not fit the create* functions entry type
4822
5062
  resource: createResourceEntry,
4823
5063
  paint: createPaintEntry,
@@ -4830,7 +5070,9 @@ const ENTRY_TYPES = {
4830
5070
  /**
4831
5071
  * Create replay performance entries from the browser performance entries.
4832
5072
  */
4833
- function createPerformanceEntries(entries) {
5073
+ function createPerformanceEntries(
5074
+ entries,
5075
+ ) {
4834
5076
  return entries.map(createPerformanceEntry).filter(Boolean) ;
4835
5077
  }
4836
5078
 
@@ -4848,8 +5090,6 @@ function getAbsoluteTime(time) {
4848
5090
  return ((browserPerformanceTimeOrigin || WINDOW.performance.timeOrigin) + time) / 1000;
4849
5091
  }
4850
5092
 
4851
- // TODO: type definition!
4852
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4853
5093
  function createPaintEntry(entry) {
4854
5094
  const { duration, entryType, name, startTime } = entry;
4855
5095
 
@@ -4859,14 +5099,28 @@ function createPaintEntry(entry) {
4859
5099
  name,
4860
5100
  start,
4861
5101
  end: start + duration,
5102
+ data: undefined,
4862
5103
  };
4863
5104
  }
4864
5105
 
4865
- // TODO: type definition!
4866
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4867
5106
  function createNavigationEntry(entry) {
4868
- // TODO: There looks to be some more interesting bits in here (domComplete, domContentLoaded)
4869
- const { entryType, name, duration, domComplete, startTime, transferSize, type } = entry;
5107
+ const {
5108
+ entryType,
5109
+ name,
5110
+ decodedBodySize,
5111
+ duration,
5112
+ domComplete,
5113
+ encodedBodySize,
5114
+ domContentLoadedEventStart,
5115
+ domContentLoadedEventEnd,
5116
+ domInteractive,
5117
+ loadEventStart,
5118
+ loadEventEnd,
5119
+ redirectCount,
5120
+ startTime,
5121
+ transferSize,
5122
+ type,
5123
+ } = entry;
4870
5124
 
4871
5125
  // Ignore entries with no duration, they do not seem to be useful and cause dupes
4872
5126
  if (duration === 0) {
@@ -4880,15 +5134,34 @@ function createNavigationEntry(entry) {
4880
5134
  name,
4881
5135
  data: {
4882
5136
  size: transferSize,
5137
+ decodedBodySize,
5138
+ encodedBodySize,
4883
5139
  duration,
5140
+ domInteractive,
5141
+ domContentLoadedEventStart,
5142
+ domContentLoadedEventEnd,
5143
+ loadEventStart,
5144
+ loadEventEnd,
5145
+ domComplete,
5146
+ redirectCount,
4884
5147
  },
4885
5148
  };
4886
5149
  }
4887
5150
 
4888
- // TODO: type definition!
4889
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4890
- function createResourceEntry(entry) {
4891
- const { entryType, initiatorType, name, responseEnd, startTime, encodedBodySize, transferSize } = entry;
5151
+ function createResourceEntry(
5152
+ entry,
5153
+ ) {
5154
+ const {
5155
+ entryType,
5156
+ initiatorType,
5157
+ name,
5158
+ responseEnd,
5159
+ startTime,
5160
+ decodedBodySize,
5161
+ encodedBodySize,
5162
+ responseStatus,
5163
+ transferSize,
5164
+ } = entry;
4892
5165
 
4893
5166
  // Core SDK handles these
4894
5167
  if (['fetch', 'xmlhttprequest'].includes(initiatorType)) {
@@ -4902,14 +5175,16 @@ function createResourceEntry(entry) {
4902
5175
  name,
4903
5176
  data: {
4904
5177
  size: transferSize,
5178
+ statusCode: responseStatus,
5179
+ decodedBodySize,
4905
5180
  encodedBodySize,
4906
5181
  },
4907
5182
  };
4908
5183
  }
4909
5184
 
4910
- // TODO: type definition!
4911
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4912
- function createLargestContentfulPaint(entry) {
5185
+ function createLargestContentfulPaint(
5186
+ entry,
5187
+ ) {
4913
5188
  const { entryType, startTime, size } = entry;
4914
5189
 
4915
5190
  let startTimeOrNavigationActivation = 0;
@@ -5477,7 +5752,7 @@ class ReplayContainer {
5477
5752
  recordingOptions,
5478
5753
  }
5479
5754
 
5480
- ) {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);
5755
+ ) {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);
5481
5756
  this._recordingOptions = recordingOptions;
5482
5757
  this._options = options;
5483
5758
 
@@ -5566,23 +5841,7 @@ class ReplayContainer {
5566
5841
  // instead, we'll always keep the last 60 seconds of replay before an error happened
5567
5842
  ...(this.recordingMode === 'error' && { checkoutEveryNms: ERROR_CHECKOUT_TIME }),
5568
5843
  emit: getHandleRecordingEmit(this),
5569
- onMutation: (mutations) => {
5570
- if (this._options._experiments.captureMutationSize) {
5571
- const count = mutations.length;
5572
-
5573
- if (count > 500) {
5574
- const breadcrumb = createBreadcrumb({
5575
- category: 'replay.mutations',
5576
- data: {
5577
- count,
5578
- },
5579
- });
5580
- this._createCustomBreadcrumb(breadcrumb);
5581
- }
5582
- }
5583
- // `true` means we use the regular mutation handling by rrweb
5584
- return true;
5585
- },
5844
+ onMutation: this._onMutationHandler,
5586
5845
  });
5587
5846
  } catch (err) {
5588
5847
  this._handleException(err);
@@ -5981,10 +6240,10 @@ class ReplayContainer {
5981
6240
  * Trigger rrweb to take a full snapshot which will cause this plugin to
5982
6241
  * create a new Replay event.
5983
6242
  */
5984
- _triggerFullSnapshot() {
6243
+ _triggerFullSnapshot(checkout = true) {
5985
6244
  try {
5986
6245
  (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Taking full rrweb snapshot');
5987
- record.takeFullSnapshot(true);
6246
+ record.takeFullSnapshot(checkout);
5988
6247
  } catch (err) {
5989
6248
  this._handleException(err);
5990
6249
  }
@@ -6198,6 +6457,37 @@ class ReplayContainer {
6198
6457
  saveSession(this.session);
6199
6458
  }
6200
6459
  }
6460
+
6461
+ /** Handler for rrweb.record.onMutation */
6462
+ __init17() {this._onMutationHandler = (mutations) => {
6463
+ const count = mutations.length;
6464
+
6465
+ const mutationLimit = this._options._experiments.mutationLimit || 0;
6466
+ const mutationBreadcrumbLimit = this._options._experiments.mutationBreadcrumbLimit || 1000;
6467
+ const overMutationLimit = mutationLimit && count > mutationLimit;
6468
+
6469
+ // Create a breadcrumb if a lot of mutations happen at the same time
6470
+ // We can show this in the UI as an information with potential performance improvements
6471
+ if (count > mutationBreadcrumbLimit || overMutationLimit) {
6472
+ const breadcrumb = createBreadcrumb({
6473
+ category: 'replay.mutations',
6474
+ data: {
6475
+ count,
6476
+ },
6477
+ });
6478
+ this._createCustomBreadcrumb(breadcrumb);
6479
+ }
6480
+
6481
+ if (overMutationLimit) {
6482
+ // We want to skip doing an incremental snapshot if there are too many mutations
6483
+ // Instead, we do a full snapshot
6484
+ this._triggerFullSnapshot(false);
6485
+ return false;
6486
+ }
6487
+
6488
+ // `true` means we use the regular mutation handling by rrweb
6489
+ return true;
6490
+ };}
6201
6491
  }
6202
6492
 
6203
6493
  function getOption(