@sentry/replay 7.45.0 → 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 (36) hide show
  1. package/cjs/index.js +442 -209
  2. package/cjs/index.js.map +1 -1
  3. package/esm/index.js +443 -210
  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 +5 -11
  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/addMemoryEntry.d.ts.map +1 -1
  30. package/types/util/createPerformanceEntries.d.ts +2 -2
  31. package/types/util/createPerformanceEntries.d.ts.map +1 -1
  32. package/types/util/createPerformanceSpans.d.ts +2 -2
  33. package/types/util/createPerformanceSpans.d.ts.map +1 -1
  34. package/types/coreHandlers/addBreadcrumbEvent.d.ts.map +0 -1
  35. package/types/coreHandlers/addNetworkBreadcrumb.d.ts +0 -4
  36. 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,131 +4474,202 @@ 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) => beforeAddNetworkBreadcrumb(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 beforeAddNetworkBreadcrumb(
4513
- options,
4514
- breadcrumb,
4515
- hint,
4516
+ /** Convert a Content-Length header to number/undefined. */
4517
+ function parseContentLengthHeader(header) {
4518
+ if (!header) {
4519
+ return undefined;
4520
+ }
4521
+
4522
+ const size = parseInt(header, 10);
4523
+ return isNaN(size) ? undefined : size;
4524
+ }
4525
+
4526
+ /** Get the string representation of a body. */
4527
+ function getBodyString(body) {
4528
+ if (typeof body === 'string') {
4529
+ return body;
4530
+ }
4531
+
4532
+ if (body instanceof URLSearchParams) {
4533
+ return body.toString();
4534
+ }
4535
+
4536
+ if (body instanceof FormData) {
4537
+ return _serializeFormData(body);
4538
+ }
4539
+
4540
+ return undefined;
4541
+ }
4542
+
4543
+ /** Convert ReplayNetworkRequestData to a PerformanceEntry. */
4544
+ function makeNetworkReplayBreadcrumb(
4545
+ type,
4546
+ data,
4516
4547
  ) {
4517
- if (!breadcrumb.data) {
4518
- return;
4548
+ if (!data) {
4549
+ return null;
4519
4550
  }
4520
4551
 
4521
- try {
4522
- if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) {
4523
- _handleXhrBreadcrumb(breadcrumb, hint, options);
4524
- }
4552
+ const { startTimestamp, endTimestamp, url, method, statusCode, request, response } = data;
4525
4553
 
4526
- if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) {
4527
- // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick
4528
- // Because the hook runs synchronously, and the breadcrumb is afterwards passed on
4529
- // So any async mutations to it will not be reflected in the final breadcrumb
4530
- _enrichFetchBreadcrumb(breadcrumb, hint, options);
4554
+ const result = {
4555
+ type,
4556
+ start: startTimestamp / 1000,
4557
+ end: endTimestamp / 1000,
4558
+ name: url,
4559
+ data: dropUndefinedKeys({
4560
+ method,
4561
+ statusCode,
4562
+ request,
4563
+ response,
4564
+ }),
4565
+ };
4531
4566
 
4532
- void _handleFetchBreadcrumb(breadcrumb, hint, options);
4533
- }
4534
- } catch (e) {
4535
- (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Error when enriching network breadcrumb');
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;
4536
4574
  }
4575
+
4576
+ try {
4577
+ return JSON.parse(bodyText);
4578
+ } catch (e2) {
4579
+ // return text
4580
+ }
4581
+
4582
+ return bodyText;
4537
4583
  }
4538
4584
 
4539
- function _handleXhrBreadcrumb(
4540
- breadcrumb,
4541
- hint,
4542
- options,
4585
+ /** Build the request or response part of a replay network breadcrumb. */
4586
+ function buildNetworkRequestOrResponse(
4587
+ bodySize,
4588
+ body,
4543
4589
  ) {
4544
- // Enriches the breadcrumb overall
4545
- _enrichXhrBreadcrumb(breadcrumb, hint, options);
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
+ }
4546
4611
 
4547
- // Create a replay performance entry from this breadcrumb
4548
- const result = _makeNetworkReplayBreadcrumb('resource.xhr', breadcrumb, hint);
4549
- addNetworkBreadcrumb(options.replay, result);
4612
+ return info;
4550
4613
  }
4551
4614
 
4552
- async function _handleFetchBreadcrumb(
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();
4620
+ }
4621
+
4622
+ /**
4623
+ * Capture a fetch breadcrumb to a replay.
4624
+ * This adds additional data (where approriate).
4625
+ */
4626
+ async function captureFetchBreadcrumbToReplay(
4553
4627
  breadcrumb,
4554
4628
  hint,
4555
4629
  options,
4556
4630
  ) {
4557
- const fullBreadcrumb = await _parseFetchResponse(breadcrumb, hint, options);
4631
+ try {
4632
+ const data = await _prepareFetchData(breadcrumb, hint, options);
4558
4633
 
4559
- // Create a replay performance entry from this breadcrumb
4560
- const result = _makeNetworkReplayBreadcrumb('resource.fetch', fullBreadcrumb, hint);
4561
- addNetworkBreadcrumb(options.replay, result);
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);
4639
+ }
4562
4640
  }
4563
4641
 
4564
- // This does async operations on the breadcrumb for replay
4565
- async function _parseFetchResponse(
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(
4566
4648
  breadcrumb,
4567
4649
  hint,
4568
4650
  options,
4569
4651
  ) {
4570
- if (breadcrumb.data.response_body_size || !hint.response) {
4571
- return breadcrumb;
4572
- }
4652
+ const { input, response } = hint;
4573
4653
 
4574
- // If no Content-Length header exists, we try to get the size from the response body
4575
- try {
4576
- // We have to clone this, as the body can only be read once
4577
- const response = (hint.response ).clone();
4578
- const body = await response.text();
4654
+ const body = _getFetchRequestArgBody(input);
4655
+ const reqSize = getBodySize(body, options.textEncoder);
4656
+ const resSize = response ? parseContentLengthHeader(response.headers.get('content-length')) : undefined;
4579
4657
 
4580
- if (body.length) {
4581
- return {
4582
- ...breadcrumb,
4583
- data: { ...breadcrumb.data, response_body_size: getBodySize(body, options.textEncoder) },
4584
- };
4585
- }
4586
- } catch (e3) {
4587
- // just ignore if something fails here
4658
+ if (reqSize !== undefined) {
4659
+ breadcrumb.data.request_body_size = reqSize;
4660
+ }
4661
+ if (resSize !== undefined) {
4662
+ breadcrumb.data.response_body_size = resSize;
4588
4663
  }
4589
-
4590
- return breadcrumb;
4591
4664
  }
4592
4665
 
4593
- function _makeNetworkReplayBreadcrumb(
4594
- type,
4666
+ async function _prepareFetchData(
4595
4667
  breadcrumb,
4596
4668
  hint,
4669
+ options,
4597
4670
  ) {
4598
4671
  const { startTimestamp, endTimestamp } = hint;
4599
4672
 
4600
- if (!endTimestamp) {
4601
- return null;
4602
- }
4603
-
4604
4673
  const {
4605
4674
  url,
4606
4675
  method,
@@ -4609,32 +4678,122 @@ function _makeNetworkReplayBreadcrumb(
4609
4678
  response_body_size: responseBodySize,
4610
4679
  } = breadcrumb.data;
4611
4680
 
4612
- if (url === undefined) {
4613
- return null;
4614
- }
4681
+ const request = _getRequestInfo(options, hint.input, requestBodySize);
4682
+ const response = await _getResponseInfo(options, hint.response, responseBodySize);
4615
4683
 
4616
- const result = {
4617
- type,
4618
- start: startTimestamp / 1000,
4619
- end: endTimestamp / 1000,
4620
- name: url,
4621
- data: {
4622
- method,
4623
- statusCode,
4624
- },
4684
+ return {
4685
+ startTimestamp,
4686
+ endTimestamp,
4687
+ url,
4688
+ method,
4689
+ statusCode: statusCode || 0,
4690
+ request,
4691
+ response,
4625
4692
  };
4693
+ }
4694
+
4695
+ function _getRequestInfo(
4696
+ { captureBodies },
4697
+ input,
4698
+ requestBodySize,
4699
+ ) {
4700
+ if (!captureBodies) {
4701
+ return buildNetworkRequestOrResponse(requestBodySize, undefined);
4702
+ }
4703
+
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
+ }
4626
4709
 
4627
- if (requestBodySize) {
4628
- result.data.requestBodySize = requestBodySize;
4710
+ async function _getResponseInfo(
4711
+ { captureBodies, textEncoder },
4712
+ response,
4713
+ responseBodySize,
4714
+ ) {
4715
+ if (!captureBodies && responseBodySize !== undefined) {
4716
+ return buildNetworkRequestOrResponse(responseBodySize, undefined);
4629
4717
  }
4630
- if (responseBodySize) {
4631
- result.data.responseBodySize = responseBodySize;
4718
+
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);
4724
+
4725
+ const size =
4726
+ bodyText && bodyText.length && responseBodySize === undefined
4727
+ ? getBodySize(bodyText, textEncoder)
4728
+ : responseBodySize;
4729
+
4730
+ if (captureBodies) {
4731
+ return buildNetworkRequestOrResponse(size, body);
4732
+ }
4733
+
4734
+ return buildNetworkRequestOrResponse(size, undefined);
4735
+ } catch (e) {
4736
+ // fallback
4737
+ return buildNetworkRequestOrResponse(responseBodySize, undefined);
4632
4738
  }
4739
+ }
4633
4740
 
4634
- return result;
4741
+ async function _parseFetchBody(
4742
+ response,
4743
+ ) {
4744
+ let bodyText;
4745
+
4746
+ try {
4747
+ bodyText = await response.text();
4748
+ } catch (e2) {
4749
+ return {};
4750
+ }
4751
+
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 };
4635
4760
  }
4636
4761
 
4637
- function _enrichXhrBreadcrumb(
4762
+ function _getFetchRequestArgBody(fetchArgs = []) {
4763
+ // We only support getting the body from the fetch options
4764
+ if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== 'object') {
4765
+ return undefined;
4766
+ }
4767
+
4768
+ return (fetchArgs[1] ).body;
4769
+ }
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(
4638
4797
  breadcrumb,
4639
4798
  hint,
4640
4799
  options,
@@ -4643,7 +4802,7 @@ function _enrichXhrBreadcrumb(
4643
4802
 
4644
4803
  const reqSize = getBodySize(input, options.textEncoder);
4645
4804
  const resSize = xhr.getResponseHeader('content-length')
4646
- ? parseContentSizeHeader(xhr.getResponseHeader('content-length'))
4805
+ ? parseContentLengthHeader(xhr.getResponseHeader('content-length'))
4647
4806
  : getBodySize(xhr.response, options.textEncoder);
4648
4807
 
4649
4808
  if (reqSize !== undefined) {
@@ -4654,84 +4813,106 @@ function _enrichXhrBreadcrumb(
4654
4813
  }
4655
4814
  }
4656
4815
 
4657
- function _enrichFetchBreadcrumb(
4816
+ function _prepareXhrData(
4658
4817
  breadcrumb,
4659
4818
  hint,
4660
4819
  options,
4661
4820
  ) {
4662
- const { input, response } = hint;
4821
+ const { startTimestamp, endTimestamp, input } = hint;
4663
4822
 
4664
- const body = getFetchBody(input);
4665
- const reqSize = getBodySize(body, options.textEncoder);
4666
- const resSize = response ? parseContentSizeHeader(response.headers.get('content-length')) : undefined;
4823
+ const {
4824
+ url,
4825
+ method,
4826
+ status_code: statusCode,
4827
+ request_body_size: requestBodySize,
4828
+ response_body_size: responseBodySize,
4829
+ } = breadcrumb.data;
4667
4830
 
4668
- if (reqSize !== undefined) {
4669
- breadcrumb.data.request_body_size = reqSize;
4670
- }
4671
- if (resSize !== undefined) {
4672
- breadcrumb.data.response_body_size = resSize;
4831
+ if (!url) {
4832
+ return null;
4673
4833
  }
4674
- }
4675
4834
 
4676
- /** only exported for tests */
4677
- function getBodySize(
4678
- body,
4679
- textEncoder,
4680
- ) {
4681
- if (!body) {
4682
- return undefined;
4683
- }
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
+ );
4684
4843
 
4685
- try {
4686
- if (typeof body === 'string') {
4687
- return textEncoder.encode(body).length;
4688
- }
4844
+ return {
4845
+ startTimestamp,
4846
+ endTimestamp,
4847
+ url,
4848
+ method,
4849
+ statusCode: statusCode || 0,
4850
+ request,
4851
+ response,
4852
+ };
4853
+ }
4689
4854
 
4690
- if (body instanceof URLSearchParams) {
4691
- return textEncoder.encode(body.toString()).length;
4692
- }
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();
4693
4863
 
4694
- if (body instanceof FormData) {
4695
- // This is a bit simplified, but gives us a decent estimate
4696
- // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13'
4697
- // @ts-ignore passing FormData to URLSearchParams actually works
4698
- const formDataStr = new URLSearchParams(body).toString();
4699
- return textEncoder.encode(formDataStr).length;
4700
- }
4864
+ try {
4865
+ const textEncoder = new TextEncoder();
4701
4866
 
4702
- if (body instanceof Blob) {
4703
- return body.size;
4704
- }
4867
+ const options = {
4868
+ replay,
4869
+ textEncoder,
4870
+ captureBodies: replay.getOptions()._experiments.captureNetworkBodies || false,
4871
+ };
4705
4872
 
4706
- if (body instanceof ArrayBuffer) {
4707
- return body.byteLength;
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));
4708
4879
  }
4709
-
4710
- // Currently unhandled types: ArrayBufferView, ReadableStream
4711
- } catch (e4) {
4712
- // just return undefined
4880
+ } catch (e2) {
4881
+ // Do nothing
4713
4882
  }
4714
-
4715
- return undefined;
4716
4883
  }
4717
4884
 
4718
- /** only exported for tests */
4719
- function parseContentSizeHeader(header) {
4720
- if (!header) {
4721
- return undefined;
4885
+ /** just exported for tests */
4886
+ function beforeAddNetworkBreadcrumb(
4887
+ options,
4888
+ breadcrumb,
4889
+ hint,
4890
+ ) {
4891
+ if (!breadcrumb.data) {
4892
+ return;
4722
4893
  }
4723
4894
 
4724
- const size = parseInt(header, 10);
4725
- return isNaN(size) ? undefined : size;
4726
- }
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);
4727
4901
 
4728
- function getFetchBody(fetchArgs = []) {
4729
- // We only support getting the body from the fetch options
4730
- if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== 'object') {
4731
- return undefined;
4732
- }
4902
+ void captureXhrBreadcrumbToReplay(breadcrumb, hint, options);
4903
+ }
4733
4904
 
4734
- return (fetchArgs[1] ).body;
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
+ }
4735
4916
  }
4736
4917
 
4737
4918
  function _isXhrBreadcrumb(breadcrumb) {
@@ -4874,7 +5055,9 @@ function createMemoryEntry(memoryEntry) {
4874
5055
 
4875
5056
  // Map entryType -> function to normalize data for event
4876
5057
  // @ts-ignore TODO: entry type does not fit the create* functions entry type
4877
- const ENTRY_TYPES = {
5058
+ const ENTRY_TYPES
5059
+
5060
+ = {
4878
5061
  // @ts-ignore TODO: entry type does not fit the create* functions entry type
4879
5062
  resource: createResourceEntry,
4880
5063
  paint: createPaintEntry,
@@ -4887,7 +5070,9 @@ const ENTRY_TYPES = {
4887
5070
  /**
4888
5071
  * Create replay performance entries from the browser performance entries.
4889
5072
  */
4890
- function createPerformanceEntries(entries) {
5073
+ function createPerformanceEntries(
5074
+ entries,
5075
+ ) {
4891
5076
  return entries.map(createPerformanceEntry).filter(Boolean) ;
4892
5077
  }
4893
5078
 
@@ -4905,8 +5090,6 @@ function getAbsoluteTime(time) {
4905
5090
  return ((browserPerformanceTimeOrigin || WINDOW.performance.timeOrigin) + time) / 1000;
4906
5091
  }
4907
5092
 
4908
- // TODO: type definition!
4909
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4910
5093
  function createPaintEntry(entry) {
4911
5094
  const { duration, entryType, name, startTime } = entry;
4912
5095
 
@@ -4916,14 +5099,28 @@ function createPaintEntry(entry) {
4916
5099
  name,
4917
5100
  start,
4918
5101
  end: start + duration,
5102
+ data: undefined,
4919
5103
  };
4920
5104
  }
4921
5105
 
4922
- // TODO: type definition!
4923
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4924
5106
  function createNavigationEntry(entry) {
4925
- // TODO: There looks to be some more interesting bits in here (domComplete, domContentLoaded)
4926
- 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;
4927
5124
 
4928
5125
  // Ignore entries with no duration, they do not seem to be useful and cause dupes
4929
5126
  if (duration === 0) {
@@ -4937,15 +5134,34 @@ function createNavigationEntry(entry) {
4937
5134
  name,
4938
5135
  data: {
4939
5136
  size: transferSize,
5137
+ decodedBodySize,
5138
+ encodedBodySize,
4940
5139
  duration,
5140
+ domInteractive,
5141
+ domContentLoadedEventStart,
5142
+ domContentLoadedEventEnd,
5143
+ loadEventStart,
5144
+ loadEventEnd,
5145
+ domComplete,
5146
+ redirectCount,
4941
5147
  },
4942
5148
  };
4943
5149
  }
4944
5150
 
4945
- // TODO: type definition!
4946
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4947
- function createResourceEntry(entry) {
4948
- 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;
4949
5165
 
4950
5166
  // Core SDK handles these
4951
5167
  if (['fetch', 'xmlhttprequest'].includes(initiatorType)) {
@@ -4959,14 +5175,16 @@ function createResourceEntry(entry) {
4959
5175
  name,
4960
5176
  data: {
4961
5177
  size: transferSize,
5178
+ statusCode: responseStatus,
5179
+ decodedBodySize,
4962
5180
  encodedBodySize,
4963
5181
  },
4964
5182
  };
4965
5183
  }
4966
5184
 
4967
- // TODO: type definition!
4968
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4969
- function createLargestContentfulPaint(entry) {
5185
+ function createLargestContentfulPaint(
5186
+ entry,
5187
+ ) {
4970
5188
  const { entryType, startTime, size } = entry;
4971
5189
 
4972
5190
  let startTimeOrNavigationActivation = 0;
@@ -5534,7 +5752,7 @@ class ReplayContainer {
5534
5752
  recordingOptions,
5535
5753
  }
5536
5754
 
5537
- ) {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);
5538
5756
  this._recordingOptions = recordingOptions;
5539
5757
  this._options = options;
5540
5758
 
@@ -5623,23 +5841,7 @@ class ReplayContainer {
5623
5841
  // instead, we'll always keep the last 60 seconds of replay before an error happened
5624
5842
  ...(this.recordingMode === 'error' && { checkoutEveryNms: ERROR_CHECKOUT_TIME }),
5625
5843
  emit: getHandleRecordingEmit(this),
5626
- onMutation: (mutations) => {
5627
- if (this._options._experiments.captureMutationSize) {
5628
- const count = mutations.length;
5629
-
5630
- if (count > 500) {
5631
- const breadcrumb = createBreadcrumb({
5632
- category: 'replay.mutations',
5633
- data: {
5634
- count,
5635
- },
5636
- });
5637
- this._createCustomBreadcrumb(breadcrumb);
5638
- }
5639
- }
5640
- // `true` means we use the regular mutation handling by rrweb
5641
- return true;
5642
- },
5844
+ onMutation: this._onMutationHandler,
5643
5845
  });
5644
5846
  } catch (err) {
5645
5847
  this._handleException(err);
@@ -6038,10 +6240,10 @@ class ReplayContainer {
6038
6240
  * Trigger rrweb to take a full snapshot which will cause this plugin to
6039
6241
  * create a new Replay event.
6040
6242
  */
6041
- _triggerFullSnapshot() {
6243
+ _triggerFullSnapshot(checkout = true) {
6042
6244
  try {
6043
6245
  (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Taking full rrweb snapshot');
6044
- record.takeFullSnapshot(true);
6246
+ record.takeFullSnapshot(checkout);
6045
6247
  } catch (err) {
6046
6248
  this._handleException(err);
6047
6249
  }
@@ -6255,6 +6457,37 @@ class ReplayContainer {
6255
6457
  saveSession(this.session);
6256
6458
  }
6257
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
+ };}
6258
6491
  }
6259
6492
 
6260
6493
  function getOption(