@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/cjs/index.js CHANGED
@@ -4499,7 +4499,7 @@ function handleNetworkBreadcrumbs(replay) {
4499
4499
  };
4500
4500
 
4501
4501
  if (client && client.on) {
4502
- client.on('beforeAddBreadcrumb', (breadcrumb, hint) => handleNetworkBreadcrumb(options, breadcrumb, hint));
4502
+ client.on('beforeAddBreadcrumb', (breadcrumb, hint) => beforeAddNetworkBreadcrumb(options, breadcrumb, hint));
4503
4503
  } else {
4504
4504
  // Fallback behavior
4505
4505
  utils.addInstrumentationHandler('fetch', handleFetchSpanListener(replay));
@@ -4511,7 +4511,7 @@ function handleNetworkBreadcrumbs(replay) {
4511
4511
  }
4512
4512
 
4513
4513
  /** just exported for tests */
4514
- function handleNetworkBreadcrumb(
4514
+ function beforeAddNetworkBreadcrumb(
4515
4515
  options,
4516
4516
  breadcrumb,
4517
4517
  hint,
@@ -4522,27 +4522,76 @@ function handleNetworkBreadcrumb(
4522
4522
 
4523
4523
  try {
4524
4524
  if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) {
4525
- // Enriches the breadcrumb overall
4526
- _enrichXhrBreadcrumb(breadcrumb, hint, options);
4527
-
4528
- // Create a replay performance entry from this breadcrumb
4529
- const result = _makeNetworkReplayBreadcrumb('resource.xhr', breadcrumb, hint);
4530
- addNetworkBreadcrumb(options.replay, result);
4525
+ _handleXhrBreadcrumb(breadcrumb, hint, options);
4531
4526
  }
4532
4527
 
4533
4528
  if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) {
4534
- // Enriches the breadcrumb overall
4529
+ // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick
4530
+ // Because the hook runs synchronously, and the breadcrumb is afterwards passed on
4531
+ // So any async mutations to it will not be reflected in the final breadcrumb
4535
4532
  _enrichFetchBreadcrumb(breadcrumb, hint, options);
4536
4533
 
4537
- // Create a replay performance entry from this breadcrumb
4538
- const result = _makeNetworkReplayBreadcrumb('resource.fetch', breadcrumb, hint);
4539
- addNetworkBreadcrumb(options.replay, result);
4534
+ void _handleFetchBreadcrumb(breadcrumb, hint, options);
4540
4535
  }
4541
4536
  } catch (e) {
4542
4537
  (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('Error when enriching network breadcrumb');
4543
4538
  }
4544
4539
  }
4545
4540
 
4541
+ function _handleXhrBreadcrumb(
4542
+ breadcrumb,
4543
+ hint,
4544
+ options,
4545
+ ) {
4546
+ // Enriches the breadcrumb overall
4547
+ _enrichXhrBreadcrumb(breadcrumb, hint, options);
4548
+
4549
+ // Create a replay performance entry from this breadcrumb
4550
+ const result = _makeNetworkReplayBreadcrumb('resource.xhr', breadcrumb, hint);
4551
+ addNetworkBreadcrumb(options.replay, result);
4552
+ }
4553
+
4554
+ async function _handleFetchBreadcrumb(
4555
+ breadcrumb,
4556
+ hint,
4557
+ options,
4558
+ ) {
4559
+ const fullBreadcrumb = await _parseFetchResponse(breadcrumb, hint, options);
4560
+
4561
+ // Create a replay performance entry from this breadcrumb
4562
+ const result = _makeNetworkReplayBreadcrumb('resource.fetch', fullBreadcrumb, hint);
4563
+ addNetworkBreadcrumb(options.replay, result);
4564
+ }
4565
+
4566
+ // This does async operations on the breadcrumb for replay
4567
+ async function _parseFetchResponse(
4568
+ breadcrumb,
4569
+ hint,
4570
+ options,
4571
+ ) {
4572
+ if (breadcrumb.data.response_body_size || !hint.response) {
4573
+ return breadcrumb;
4574
+ }
4575
+
4576
+ // If no Content-Length header exists, we try to get the size from the response body
4577
+ try {
4578
+ // We have to clone this, as the body can only be read once
4579
+ const response = (hint.response ).clone();
4580
+ const body = await response.text();
4581
+
4582
+ if (body.length) {
4583
+ return {
4584
+ ...breadcrumb,
4585
+ data: { ...breadcrumb.data, response_body_size: getBodySize(body, options.textEncoder) },
4586
+ };
4587
+ }
4588
+ } catch (e3) {
4589
+ // just ignore if something fails here
4590
+ }
4591
+
4592
+ return breadcrumb;
4593
+ }
4594
+
4546
4595
  function _makeNetworkReplayBreadcrumb(
4547
4596
  type,
4548
4597
  breadcrumb,
@@ -4661,7 +4710,7 @@ function getBodySize(
4661
4710
  }
4662
4711
 
4663
4712
  // Currently unhandled types: ArrayBufferView, ReadableStream
4664
- } catch (e3) {
4713
+ } catch (e4) {
4665
4714
  // just return undefined
4666
4715
  }
4667
4716
 
@@ -4765,19 +4814,27 @@ function addGlobalListeners(replay) {
4765
4814
  utils.addInstrumentationHandler('history', handleHistorySpanListener(replay));
4766
4815
  handleNetworkBreadcrumbs(replay);
4767
4816
 
4768
- // If a custom client has no hooks yet, we continue to use the "old" implementation
4769
- const hasHooks = !!(client && client.on);
4770
-
4771
4817
  // Tag all (non replay) events that get sent to Sentry with the current
4772
4818
  // replay ID so that we can reference them later in the UI
4773
- core.addGlobalEventProcessor(handleGlobalEventListener(replay, !hasHooks));
4819
+ core.addGlobalEventProcessor(handleGlobalEventListener(replay, !hasHooks(client)));
4774
4820
 
4775
- if (hasHooks) {
4776
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4777
- (client ).on('afterSendEvent', handleAfterSendEvent(replay));
4821
+ // If a custom client has no hooks yet, we continue to use the "old" implementation
4822
+ if (hasHooks(client)) {
4823
+ client.on('afterSendEvent', handleAfterSendEvent(replay));
4824
+ client.on('createDsc', (dsc) => {
4825
+ const replayId = replay.getSessionId();
4826
+ if (replayId) {
4827
+ dsc.replay_id = replayId;
4828
+ }
4829
+ });
4778
4830
  }
4779
4831
  }
4780
4832
 
4833
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4834
+ function hasHooks(client) {
4835
+ return !!(client && client.on);
4836
+ }
4837
+
4781
4838
  /**
4782
4839
  * Create a "span" for the total amount of memory being used by JS objects
4783
4840
  * (including v8 internal objects).