@sentry/replay 7.44.2 → 7.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.js CHANGED
@@ -4497,7 +4497,7 @@ function handleNetworkBreadcrumbs(replay) {
4497
4497
  };
4498
4498
 
4499
4499
  if (client && client.on) {
4500
- client.on('beforeAddBreadcrumb', (breadcrumb, hint) => handleNetworkBreadcrumb(options, breadcrumb, hint));
4500
+ client.on('beforeAddBreadcrumb', (breadcrumb, hint) => beforeAddNetworkBreadcrumb(options, breadcrumb, hint));
4501
4501
  } else {
4502
4502
  // Fallback behavior
4503
4503
  addInstrumentationHandler('fetch', handleFetchSpanListener(replay));
@@ -4509,7 +4509,7 @@ function handleNetworkBreadcrumbs(replay) {
4509
4509
  }
4510
4510
 
4511
4511
  /** just exported for tests */
4512
- function handleNetworkBreadcrumb(
4512
+ function beforeAddNetworkBreadcrumb(
4513
4513
  options,
4514
4514
  breadcrumb,
4515
4515
  hint,
@@ -4520,27 +4520,76 @@ function handleNetworkBreadcrumb(
4520
4520
 
4521
4521
  try {
4522
4522
  if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) {
4523
- // Enriches the breadcrumb overall
4524
- _enrichXhrBreadcrumb(breadcrumb, hint, options);
4525
-
4526
- // Create a replay performance entry from this breadcrumb
4527
- const result = _makeNetworkReplayBreadcrumb('resource.xhr', breadcrumb, hint);
4528
- addNetworkBreadcrumb(options.replay, result);
4523
+ _handleXhrBreadcrumb(breadcrumb, hint, options);
4529
4524
  }
4530
4525
 
4531
4526
  if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) {
4532
- // Enriches the breadcrumb overall
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
4533
4530
  _enrichFetchBreadcrumb(breadcrumb, hint, options);
4534
4531
 
4535
- // Create a replay performance entry from this breadcrumb
4536
- const result = _makeNetworkReplayBreadcrumb('resource.fetch', breadcrumb, hint);
4537
- addNetworkBreadcrumb(options.replay, result);
4532
+ void _handleFetchBreadcrumb(breadcrumb, hint, options);
4538
4533
  }
4539
4534
  } catch (e) {
4540
4535
  (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Error when enriching network breadcrumb');
4541
4536
  }
4542
4537
  }
4543
4538
 
4539
+ function _handleXhrBreadcrumb(
4540
+ breadcrumb,
4541
+ hint,
4542
+ options,
4543
+ ) {
4544
+ // Enriches the breadcrumb overall
4545
+ _enrichXhrBreadcrumb(breadcrumb, hint, options);
4546
+
4547
+ // Create a replay performance entry from this breadcrumb
4548
+ const result = _makeNetworkReplayBreadcrumb('resource.xhr', breadcrumb, hint);
4549
+ addNetworkBreadcrumb(options.replay, result);
4550
+ }
4551
+
4552
+ async function _handleFetchBreadcrumb(
4553
+ breadcrumb,
4554
+ hint,
4555
+ options,
4556
+ ) {
4557
+ const fullBreadcrumb = await _parseFetchResponse(breadcrumb, hint, options);
4558
+
4559
+ // Create a replay performance entry from this breadcrumb
4560
+ const result = _makeNetworkReplayBreadcrumb('resource.fetch', fullBreadcrumb, hint);
4561
+ addNetworkBreadcrumb(options.replay, result);
4562
+ }
4563
+
4564
+ // This does async operations on the breadcrumb for replay
4565
+ async function _parseFetchResponse(
4566
+ breadcrumb,
4567
+ hint,
4568
+ options,
4569
+ ) {
4570
+ if (breadcrumb.data.response_body_size || !hint.response) {
4571
+ return breadcrumb;
4572
+ }
4573
+
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();
4579
+
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
4588
+ }
4589
+
4590
+ return breadcrumb;
4591
+ }
4592
+
4544
4593
  function _makeNetworkReplayBreadcrumb(
4545
4594
  type,
4546
4595
  breadcrumb,
@@ -4659,7 +4708,7 @@ function getBodySize(
4659
4708
  }
4660
4709
 
4661
4710
  // Currently unhandled types: ArrayBufferView, ReadableStream
4662
- } catch (e3) {
4711
+ } catch (e4) {
4663
4712
  // just return undefined
4664
4713
  }
4665
4714
 
@@ -4763,19 +4812,27 @@ function addGlobalListeners(replay) {
4763
4812
  addInstrumentationHandler('history', handleHistorySpanListener(replay));
4764
4813
  handleNetworkBreadcrumbs(replay);
4765
4814
 
4766
- // If a custom client has no hooks yet, we continue to use the "old" implementation
4767
- const hasHooks = !!(client && client.on);
4768
-
4769
4815
  // Tag all (non replay) events that get sent to Sentry with the current
4770
4816
  // replay ID so that we can reference them later in the UI
4771
- addGlobalEventProcessor(handleGlobalEventListener(replay, !hasHooks));
4817
+ addGlobalEventProcessor(handleGlobalEventListener(replay, !hasHooks(client)));
4772
4818
 
4773
- if (hasHooks) {
4774
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4775
- (client ).on('afterSendEvent', handleAfterSendEvent(replay));
4819
+ // If a custom client has no hooks yet, we continue to use the "old" implementation
4820
+ if (hasHooks(client)) {
4821
+ client.on('afterSendEvent', handleAfterSendEvent(replay));
4822
+ client.on('createDsc', (dsc) => {
4823
+ const replayId = replay.getSessionId();
4824
+ if (replayId) {
4825
+ dsc.replay_id = replayId;
4826
+ }
4827
+ });
4776
4828
  }
4777
4829
  }
4778
4830
 
4831
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4832
+ function hasHooks(client) {
4833
+ return !!(client && client.on);
4834
+ }
4835
+
4779
4836
  /**
4780
4837
  * Create a "span" for the total amount of memory being used by JS objects
4781
4838
  * (including v8 internal objects).