@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/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";
@@ -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,145 +4476,186 @@ 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) => handleNetworkBreadcrumb(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 handleNetworkBreadcrumb(
4515
- options,
4516
- breadcrumb,
4517
- hint,
4518
- ) {
4519
- if (!breadcrumb.data) {
4520
- return;
4518
+ /** Convert a Content-Length header to number/undefined. */
4519
+ function parseContentLengthHeader(header) {
4520
+ if (!header) {
4521
+ return undefined;
4521
4522
  }
4522
4523
 
4523
- try {
4524
- if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) {
4525
- // Enriches the breadcrumb overall
4526
- _enrichXhrBreadcrumb(breadcrumb, hint, options);
4524
+ const size = parseInt(header, 10);
4525
+ return isNaN(size) ? undefined : size;
4526
+ }
4527
4527
 
4528
- // Create a replay performance entry from this breadcrumb
4529
- const result = _makeNetworkReplayBreadcrumb('resource.xhr', breadcrumb, hint);
4530
- addNetworkBreadcrumb(options.replay, result);
4531
- }
4528
+ /** Get the string representation of a body. */
4529
+ function getBodyString(body) {
4530
+ if (typeof body === 'string') {
4531
+ return body;
4532
+ }
4532
4533
 
4533
- if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) {
4534
- // Enriches the breadcrumb overall
4535
- _enrichFetchBreadcrumb(breadcrumb, hint, options);
4534
+ if (body instanceof URLSearchParams) {
4535
+ return body.toString();
4536
+ }
4536
4537
 
4537
- // Create a replay performance entry from this breadcrumb
4538
- const result = _makeNetworkReplayBreadcrumb('resource.fetch', breadcrumb, hint);
4539
- addNetworkBreadcrumb(options.replay, result);
4540
- }
4541
- } catch (e) {
4542
- (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('Error when enriching network breadcrumb');
4538
+ if (body instanceof FormData) {
4539
+ return _serializeFormData(body);
4543
4540
  }
4541
+
4542
+ return undefined;
4544
4543
  }
4545
4544
 
4546
- function _makeNetworkReplayBreadcrumb(
4545
+ /** Convert ReplayNetworkRequestData to a PerformanceEntry. */
4546
+ function makeNetworkReplayBreadcrumb(
4547
4547
  type,
4548
- breadcrumb,
4549
- hint,
4548
+ data,
4550
4549
  ) {
4551
- const { startTimestamp, endTimestamp } = hint;
4552
-
4553
- if (!endTimestamp) {
4550
+ if (!data) {
4554
4551
  return null;
4555
4552
  }
4556
4553
 
4557
- const {
4558
- url,
4559
- method,
4560
- status_code: statusCode,
4561
- request_body_size: requestBodySize,
4562
- response_body_size: responseBodySize,
4563
- } = breadcrumb.data;
4564
-
4565
- if (url === undefined) {
4566
- return null;
4567
- }
4554
+ const { startTimestamp, endTimestamp, url, method, statusCode, request, response } = data;
4568
4555
 
4569
4556
  const result = {
4570
4557
  type,
4571
4558
  start: startTimestamp / 1000,
4572
4559
  end: endTimestamp / 1000,
4573
4560
  name: url,
4574
- data: {
4561
+ data: utils.dropUndefinedKeys({
4575
4562
  method,
4576
4563
  statusCode,
4577
- },
4564
+ request,
4565
+ response,
4566
+ }),
4578
4567
  };
4579
4568
 
4580
- if (requestBodySize) {
4581
- result.data.requestBodySize = requestBodySize;
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;
4582
4576
  }
4583
- if (responseBodySize) {
4584
- result.data.responseBodySize = responseBodySize;
4577
+
4578
+ try {
4579
+ return JSON.parse(bodyText);
4580
+ } catch (e2) {
4581
+ // return text
4585
4582
  }
4586
4583
 
4587
- return result;
4584
+ return bodyText;
4585
+ }
4586
+
4587
+ /** Build the request or response part of a replay network breadcrumb. */
4588
+ function buildNetworkRequestOrResponse(
4589
+ bodySize,
4590
+ body,
4591
+ ) {
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
+ }
4613
+
4614
+ return info;
4615
+ }
4616
+
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();
4588
4622
  }
4589
4623
 
4590
- function _enrichXhrBreadcrumb(
4624
+ /**
4625
+ * Capture a fetch breadcrumb to a replay.
4626
+ * This adds additional data (where approriate).
4627
+ */
4628
+ async function captureFetchBreadcrumbToReplay(
4591
4629
  breadcrumb,
4592
4630
  hint,
4593
4631
  options,
4594
4632
  ) {
4595
- const { xhr, input } = hint;
4596
-
4597
- const reqSize = getBodySize(input, options.textEncoder);
4598
- const resSize = xhr.getResponseHeader('content-length')
4599
- ? parseContentSizeHeader(xhr.getResponseHeader('content-length'))
4600
- : getBodySize(xhr.response, options.textEncoder);
4633
+ try {
4634
+ const data = await _prepareFetchData(breadcrumb, hint, options);
4601
4635
 
4602
- if (reqSize !== undefined) {
4603
- breadcrumb.data.request_body_size = reqSize;
4604
- }
4605
- if (resSize !== undefined) {
4606
- breadcrumb.data.response_body_size = resSize;
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);
4607
4641
  }
4608
4642
  }
4609
4643
 
4610
- function _enrichFetchBreadcrumb(
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(
4611
4650
  breadcrumb,
4612
4651
  hint,
4613
4652
  options,
4614
4653
  ) {
4615
4654
  const { input, response } = hint;
4616
4655
 
4617
- const body = getFetchBody(input);
4656
+ const body = _getFetchRequestArgBody(input);
4618
4657
  const reqSize = getBodySize(body, options.textEncoder);
4619
- const resSize = response ? parseContentSizeHeader(response.headers.get('content-length')) : undefined;
4658
+ const resSize = response ? parseContentLengthHeader(response.headers.get('content-length')) : undefined;
4620
4659
 
4621
4660
  if (reqSize !== undefined) {
4622
4661
  breadcrumb.data.request_body_size = reqSize;
@@ -4626,59 +4665,103 @@ function _enrichFetchBreadcrumb(
4626
4665
  }
4627
4666
  }
4628
4667
 
4629
- /** only exported for tests */
4630
- function getBodySize(
4631
- body,
4632
- textEncoder,
4668
+ async function _prepareFetchData(
4669
+ breadcrumb,
4670
+ hint,
4671
+ options,
4633
4672
  ) {
4634
- if (!body) {
4635
- return undefined;
4673
+ const { startTimestamp, endTimestamp } = hint;
4674
+
4675
+ const {
4676
+ url,
4677
+ method,
4678
+ status_code: statusCode,
4679
+ request_body_size: requestBodySize,
4680
+ response_body_size: responseBodySize,
4681
+ } = breadcrumb.data;
4682
+
4683
+ const request = _getRequestInfo(options, hint.input, requestBodySize);
4684
+ const response = await _getResponseInfo(options, hint.response, responseBodySize);
4685
+
4686
+ return {
4687
+ startTimestamp,
4688
+ endTimestamp,
4689
+ url,
4690
+ method,
4691
+ statusCode: statusCode || 0,
4692
+ request,
4693
+ response,
4694
+ };
4695
+ }
4696
+
4697
+ function _getRequestInfo(
4698
+ { captureBodies },
4699
+ input,
4700
+ requestBodySize,
4701
+ ) {
4702
+ if (!captureBodies) {
4703
+ return buildNetworkRequestOrResponse(requestBodySize, undefined);
4636
4704
  }
4637
4705
 
4638
- try {
4639
- if (typeof body === 'string') {
4640
- return textEncoder.encode(body).length;
4641
- }
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
+ }
4642
4711
 
4643
- if (body instanceof URLSearchParams) {
4644
- return textEncoder.encode(body.toString()).length;
4645
- }
4712
+ async function _getResponseInfo(
4713
+ { captureBodies, textEncoder },
4714
+ response,
4715
+ responseBodySize,
4716
+ ) {
4717
+ if (!captureBodies && responseBodySize !== undefined) {
4718
+ return buildNetworkRequestOrResponse(responseBodySize, undefined);
4719
+ }
4646
4720
 
4647
- if (body instanceof FormData) {
4648
- // This is a bit simplified, but gives us a decent estimate
4649
- // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'
4650
- // @ts-ignore passing FormData to URLSearchParams actually works
4651
- const formDataStr = new URLSearchParams(body).toString();
4652
- return textEncoder.encode(formDataStr).length;
4653
- }
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);
4654
4726
 
4655
- if (body instanceof Blob) {
4656
- return body.size;
4657
- }
4727
+ const size =
4728
+ bodyText && bodyText.length && responseBodySize === undefined
4729
+ ? getBodySize(bodyText, textEncoder)
4730
+ : responseBodySize;
4658
4731
 
4659
- if (body instanceof ArrayBuffer) {
4660
- return body.byteLength;
4732
+ if (captureBodies) {
4733
+ return buildNetworkRequestOrResponse(size, body);
4661
4734
  }
4662
4735
 
4663
- // Currently unhandled types: ArrayBufferView, ReadableStream
4664
- } catch (e3) {
4665
- // just return undefined
4736
+ return buildNetworkRequestOrResponse(size, undefined);
4737
+ } catch (e) {
4738
+ // fallback
4739
+ return buildNetworkRequestOrResponse(responseBodySize, undefined);
4666
4740
  }
4667
-
4668
- return undefined;
4669
4741
  }
4670
4742
 
4671
- /** only exported for tests */
4672
- function parseContentSizeHeader(header) {
4673
- if (!header) {
4674
- return undefined;
4743
+ async function _parseFetchBody(
4744
+ response,
4745
+ ) {
4746
+ let bodyText;
4747
+
4748
+ try {
4749
+ bodyText = await response.text();
4750
+ } catch (e2) {
4751
+ return {};
4675
4752
  }
4676
4753
 
4677
- const size = parseInt(header, 10);
4678
- return isNaN(size) ? undefined : size;
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 };
4679
4762
  }
4680
4763
 
4681
- function getFetchBody(fetchArgs = []) {
4764
+ function _getFetchRequestArgBody(fetchArgs = []) {
4682
4765
  // We only support getting the body from the fetch options
4683
4766
  if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== 'object') {
4684
4767
  return undefined;
@@ -4687,6 +4770,153 @@ function getFetchBody(fetchArgs = []) {
4687
4770
  return (fetchArgs[1] ).body;
4688
4771
  }
4689
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(
4799
+ breadcrumb,
4800
+ hint,
4801
+ options,
4802
+ ) {
4803
+ const { xhr, input } = hint;
4804
+
4805
+ const reqSize = getBodySize(input, options.textEncoder);
4806
+ const resSize = xhr.getResponseHeader('content-length')
4807
+ ? parseContentLengthHeader(xhr.getResponseHeader('content-length'))
4808
+ : getBodySize(xhr.response, options.textEncoder);
4809
+
4810
+ if (reqSize !== undefined) {
4811
+ breadcrumb.data.request_body_size = reqSize;
4812
+ }
4813
+ if (resSize !== undefined) {
4814
+ breadcrumb.data.response_body_size = resSize;
4815
+ }
4816
+ }
4817
+
4818
+ function _prepareXhrData(
4819
+ breadcrumb,
4820
+ hint,
4821
+ options,
4822
+ ) {
4823
+ const { startTimestamp, endTimestamp, input } = hint;
4824
+
4825
+ const {
4826
+ url,
4827
+ method,
4828
+ status_code: statusCode,
4829
+ request_body_size: requestBodySize,
4830
+ response_body_size: responseBodySize,
4831
+ } = breadcrumb.data;
4832
+
4833
+ if (!url) {
4834
+ return null;
4835
+ }
4836
+
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
+ );
4845
+
4846
+ return {
4847
+ startTimestamp,
4848
+ endTimestamp,
4849
+ url,
4850
+ method,
4851
+ statusCode: statusCode || 0,
4852
+ request,
4853
+ response,
4854
+ };
4855
+ }
4856
+
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();
4865
+
4866
+ try {
4867
+ const textEncoder = new TextEncoder();
4868
+
4869
+ const options = {
4870
+ replay,
4871
+ textEncoder,
4872
+ captureBodies: replay.getOptions()._experiments.captureNetworkBodies || false,
4873
+ };
4874
+
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));
4881
+ }
4882
+ } catch (e2) {
4883
+ // Do nothing
4884
+ }
4885
+ }
4886
+
4887
+ /** just exported for tests */
4888
+ function beforeAddNetworkBreadcrumb(
4889
+ options,
4890
+ breadcrumb,
4891
+ hint,
4892
+ ) {
4893
+ if (!breadcrumb.data) {
4894
+ return;
4895
+ }
4896
+
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);
4903
+
4904
+ void captureXhrBreadcrumbToReplay(breadcrumb, hint, options);
4905
+ }
4906
+
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
+ }
4918
+ }
4919
+
4690
4920
  function _isXhrBreadcrumb(breadcrumb) {
4691
4921
  return breadcrumb.category === 'xhr';
4692
4922
  }
@@ -4765,19 +4995,27 @@ function addGlobalListeners(replay) {
4765
4995
  utils.addInstrumentationHandler('history', handleHistorySpanListener(replay));
4766
4996
  handleNetworkBreadcrumbs(replay);
4767
4997
 
4768
- // If a custom client has no hooks yet, we continue to use the "old" implementation
4769
- const hasHooks = !!(client && client.on);
4770
-
4771
4998
  // Tag all (non replay) events that get sent to Sentry with the current
4772
4999
  // replay ID so that we can reference them later in the UI
4773
- core.addGlobalEventProcessor(handleGlobalEventListener(replay, !hasHooks));
5000
+ core.addGlobalEventProcessor(handleGlobalEventListener(replay, !hasHooks(client)));
4774
5001
 
4775
- if (hasHooks) {
4776
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4777
- (client ).on('afterSendEvent', handleAfterSendEvent(replay));
5002
+ // If a custom client has no hooks yet, we continue to use the "old" implementation
5003
+ if (hasHooks(client)) {
5004
+ client.on('afterSendEvent', handleAfterSendEvent(replay));
5005
+ client.on('createDsc', (dsc) => {
5006
+ const replayId = replay.getSessionId();
5007
+ if (replayId) {
5008
+ dsc.replay_id = replayId;
5009
+ }
5010
+ });
4778
5011
  }
4779
5012
  }
4780
5013
 
5014
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5015
+ function hasHooks(client) {
5016
+ return !!(client && client.on);
5017
+ }
5018
+
4781
5019
  /**
4782
5020
  * Create a "span" for the total amount of memory being used by JS objects
4783
5021
  * (including v8 internal objects).
@@ -4819,7 +5057,9 @@ function createMemoryEntry(memoryEntry) {
4819
5057
 
4820
5058
  // Map entryType -> function to normalize data for event
4821
5059
  // @ts-ignore TODO: entry type does not fit the create* functions entry type
4822
- const ENTRY_TYPES = {
5060
+ const ENTRY_TYPES
5061
+
5062
+ = {
4823
5063
  // @ts-ignore TODO: entry type does not fit the create* functions entry type
4824
5064
  resource: createResourceEntry,
4825
5065
  paint: createPaintEntry,
@@ -4832,7 +5072,9 @@ const ENTRY_TYPES = {
4832
5072
  /**
4833
5073
  * Create replay performance entries from the browser performance entries.
4834
5074
  */
4835
- function createPerformanceEntries(entries) {
5075
+ function createPerformanceEntries(
5076
+ entries,
5077
+ ) {
4836
5078
  return entries.map(createPerformanceEntry).filter(Boolean) ;
4837
5079
  }
4838
5080
 
@@ -4850,8 +5092,6 @@ function getAbsoluteTime(time) {
4850
5092
  return ((utils.browserPerformanceTimeOrigin || WINDOW.performance.timeOrigin) + time) / 1000;
4851
5093
  }
4852
5094
 
4853
- // TODO: type definition!
4854
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4855
5095
  function createPaintEntry(entry) {
4856
5096
  const { duration, entryType, name, startTime } = entry;
4857
5097
 
@@ -4861,14 +5101,28 @@ function createPaintEntry(entry) {
4861
5101
  name,
4862
5102
  start,
4863
5103
  end: start + duration,
5104
+ data: undefined,
4864
5105
  };
4865
5106
  }
4866
5107
 
4867
- // TODO: type definition!
4868
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4869
5108
  function createNavigationEntry(entry) {
4870
- // TODO: There looks to be some more interesting bits in here (domComplete, domContentLoaded)
4871
- 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;
4872
5126
 
4873
5127
  // Ignore entries with no duration, they do not seem to be useful and cause dupes
4874
5128
  if (duration === 0) {
@@ -4882,15 +5136,34 @@ function createNavigationEntry(entry) {
4882
5136
  name,
4883
5137
  data: {
4884
5138
  size: transferSize,
5139
+ decodedBodySize,
5140
+ encodedBodySize,
4885
5141
  duration,
5142
+ domInteractive,
5143
+ domContentLoadedEventStart,
5144
+ domContentLoadedEventEnd,
5145
+ loadEventStart,
5146
+ loadEventEnd,
5147
+ domComplete,
5148
+ redirectCount,
4886
5149
  },
4887
5150
  };
4888
5151
  }
4889
5152
 
4890
- // TODO: type definition!
4891
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4892
- function createResourceEntry(entry) {
4893
- 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;
4894
5167
 
4895
5168
  // Core SDK handles these
4896
5169
  if (['fetch', 'xmlhttprequest'].includes(initiatorType)) {
@@ -4904,14 +5177,16 @@ function createResourceEntry(entry) {
4904
5177
  name,
4905
5178
  data: {
4906
5179
  size: transferSize,
5180
+ statusCode: responseStatus,
5181
+ decodedBodySize,
4907
5182
  encodedBodySize,
4908
5183
  },
4909
5184
  };
4910
5185
  }
4911
5186
 
4912
- // TODO: type definition!
4913
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4914
- function createLargestContentfulPaint(entry) {
5187
+ function createLargestContentfulPaint(
5188
+ entry,
5189
+ ) {
4915
5190
  const { entryType, startTime, size } = entry;
4916
5191
 
4917
5192
  let startTimeOrNavigationActivation = 0;
@@ -5479,7 +5754,7 @@ class ReplayContainer {
5479
5754
  recordingOptions,
5480
5755
  }
5481
5756
 
5482
- ) {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);
5757
+ ) {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);
5483
5758
  this._recordingOptions = recordingOptions;
5484
5759
  this._options = options;
5485
5760
 
@@ -5568,23 +5843,7 @@ class ReplayContainer {
5568
5843
  // instead, we'll always keep the last 60 seconds of replay before an error happened
5569
5844
  ...(this.recordingMode === 'error' && { checkoutEveryNms: ERROR_CHECKOUT_TIME }),
5570
5845
  emit: getHandleRecordingEmit(this),
5571
- onMutation: (mutations) => {
5572
- if (this._options._experiments.captureMutationSize) {
5573
- const count = mutations.length;
5574
-
5575
- if (count > 500) {
5576
- const breadcrumb = createBreadcrumb({
5577
- category: 'replay.mutations',
5578
- data: {
5579
- count,
5580
- },
5581
- });
5582
- this._createCustomBreadcrumb(breadcrumb);
5583
- }
5584
- }
5585
- // `true` means we use the regular mutation handling by rrweb
5586
- return true;
5587
- },
5846
+ onMutation: this._onMutationHandler,
5588
5847
  });
5589
5848
  } catch (err) {
5590
5849
  this._handleException(err);
@@ -5983,10 +6242,10 @@ class ReplayContainer {
5983
6242
  * Trigger rrweb to take a full snapshot which will cause this plugin to
5984
6243
  * create a new Replay event.
5985
6244
  */
5986
- _triggerFullSnapshot() {
6245
+ _triggerFullSnapshot(checkout = true) {
5987
6246
  try {
5988
6247
  (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.log('[Replay] Taking full rrweb snapshot');
5989
- record.takeFullSnapshot(true);
6248
+ record.takeFullSnapshot(checkout);
5990
6249
  } catch (err) {
5991
6250
  this._handleException(err);
5992
6251
  }
@@ -6200,6 +6459,37 @@ class ReplayContainer {
6200
6459
  saveSession(this.session);
6201
6460
  }
6202
6461
  }
6462
+
6463
+ /** Handler for rrweb.record.onMutation */
6464
+ __init17() {this._onMutationHandler = (mutations) => {
6465
+ const count = mutations.length;
6466
+
6467
+ const mutationLimit = this._options._experiments.mutationLimit || 0;
6468
+ const mutationBreadcrumbLimit = this._options._experiments.mutationBreadcrumbLimit || 1000;
6469
+ const overMutationLimit = mutationLimit && count > mutationLimit;
6470
+
6471
+ // Create a breadcrumb if a lot of mutations happen at the same time
6472
+ // We can show this in the UI as an information with potential performance improvements
6473
+ if (count > mutationBreadcrumbLimit || overMutationLimit) {
6474
+ const breadcrumb = createBreadcrumb({
6475
+ category: 'replay.mutations',
6476
+ data: {
6477
+ count,
6478
+ },
6479
+ });
6480
+ this._createCustomBreadcrumb(breadcrumb);
6481
+ }
6482
+
6483
+ if (overMutationLimit) {
6484
+ // We want to skip doing an incremental snapshot if there are too many mutations
6485
+ // Instead, we do a full snapshot
6486
+ this._triggerFullSnapshot(false);
6487
+ return false;
6488
+ }
6489
+
6490
+ // `true` means we use the regular mutation handling by rrweb
6491
+ return true;
6492
+ };}
6203
6493
  }
6204
6494
 
6205
6495
  function getOption(