@sentry/replay 7.50.0 → 7.51.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
@@ -3375,6 +3375,14 @@ function t(t){let e=t.length;for(;--e>=0;)t[e]=0}const e=new Uint8Array([0,0,0,0
3375
3375
 
3376
3376
  function e(){const e=new Blob([r]);return URL.createObjectURL(e)}
3377
3377
 
3378
+ /**
3379
+ * Converts a timestamp to ms, if it was in s, or keeps it as ms.
3380
+ */
3381
+ function timestampToMs(timestamp) {
3382
+ const isMs = timestamp > 9999999999;
3383
+ return isMs ? timestamp : timestamp * 1000;
3384
+ }
3385
+
3378
3386
  /**
3379
3387
  * A basic event buffer that does not do any compression.
3380
3388
  * Used as fallback if the compression worker cannot be loaded or is disabled.
@@ -3397,14 +3405,8 @@ class EventBufferArray {
3397
3405
  }
3398
3406
 
3399
3407
  /** @inheritdoc */
3400
- async addEvent(event, isCheckout) {
3401
- if (isCheckout) {
3402
- this.events = [event];
3403
- return;
3404
- }
3405
-
3408
+ async addEvent(event) {
3406
3409
  this.events.push(event);
3407
- return;
3408
3410
  }
3409
3411
 
3410
3412
  /** @inheritdoc */
@@ -3418,6 +3420,22 @@ class EventBufferArray {
3418
3420
  resolve(JSON.stringify(eventsRet));
3419
3421
  });
3420
3422
  }
3423
+
3424
+ /** @inheritdoc */
3425
+ clear() {
3426
+ this.events = [];
3427
+ }
3428
+
3429
+ /** @inheritdoc */
3430
+ getEarliestTimestamp() {
3431
+ const timestamp = this.events.map(event => event.timestamp).sort()[0];
3432
+
3433
+ if (!timestamp) {
3434
+ return null;
3435
+ }
3436
+
3437
+ return timestampToMs(timestamp);
3438
+ }
3421
3439
  }
3422
3440
 
3423
3441
  /**
@@ -3525,11 +3543,15 @@ class WorkerHandler {
3525
3543
  * Exported only for testing.
3526
3544
  */
3527
3545
  class EventBufferCompressionWorker {
3528
- /** @inheritdoc */
3529
3546
 
3530
3547
  constructor(worker) {
3531
3548
  this._worker = new WorkerHandler(worker);
3532
- this.hasEvents = false;
3549
+ this._earliestTimestamp = null;
3550
+ }
3551
+
3552
+ /** @inheritdoc */
3553
+ get hasEvents() {
3554
+ return !!this._earliestTimestamp;
3533
3555
  }
3534
3556
 
3535
3557
  /**
@@ -3552,13 +3574,10 @@ class EventBufferCompressionWorker {
3552
3574
  *
3553
3575
  * Returns true if event was successfuly received and processed by worker.
3554
3576
  */
3555
- async addEvent(event, isCheckout) {
3556
- this.hasEvents = true;
3557
-
3558
- if (isCheckout) {
3559
- // This event is a checkout, make sure worker buffer is cleared before
3560
- // proceeding.
3561
- await this._clear();
3577
+ addEvent(event) {
3578
+ const timestamp = timestampToMs(event.timestamp);
3579
+ if (!this._earliestTimestamp || timestamp < this._earliestTimestamp) {
3580
+ this._earliestTimestamp = timestamp;
3562
3581
  }
3563
3582
 
3564
3583
  return this._sendEventToWorker(event);
@@ -3571,6 +3590,18 @@ class EventBufferCompressionWorker {
3571
3590
  return this._finishRequest();
3572
3591
  }
3573
3592
 
3593
+ /** @inheritdoc */
3594
+ clear() {
3595
+ this._earliestTimestamp = null;
3596
+ // We do not wait on this, as we assume the order of messages is consistent for the worker
3597
+ void this._worker.postMessage('clear');
3598
+ }
3599
+
3600
+ /** @inheritdoc */
3601
+ getEarliestTimestamp() {
3602
+ return this._earliestTimestamp;
3603
+ }
3604
+
3574
3605
  /**
3575
3606
  * Send the event to the worker.
3576
3607
  */
@@ -3584,15 +3615,10 @@ class EventBufferCompressionWorker {
3584
3615
  async _finishRequest() {
3585
3616
  const response = await this._worker.postMessage('finish');
3586
3617
 
3587
- this.hasEvents = false;
3618
+ this._earliestTimestamp = null;
3588
3619
 
3589
3620
  return response;
3590
3621
  }
3591
-
3592
- /** Clear any pending events from the worker. */
3593
- _clear() {
3594
- return this._worker.postMessage('clear');
3595
- }
3596
3622
  }
3597
3623
 
3598
3624
  /**
@@ -3621,13 +3647,23 @@ class EventBufferProxy {
3621
3647
  this._compression.destroy();
3622
3648
  }
3623
3649
 
3650
+ /** @inheritdoc */
3651
+ clear() {
3652
+ return this._used.clear();
3653
+ }
3654
+
3655
+ /** @inheritdoc */
3656
+ getEarliestTimestamp() {
3657
+ return this._used.getEarliestTimestamp();
3658
+ }
3659
+
3624
3660
  /**
3625
3661
  * Add an event to the event buffer.
3626
3662
  *
3627
3663
  * Returns true if event was successfully added.
3628
3664
  */
3629
- addEvent(event, isCheckout) {
3630
- return this._used.addEvent(event, isCheckout);
3665
+ addEvent(event) {
3666
+ return this._used.addEvent(event);
3631
3667
  }
3632
3668
 
3633
3669
  /** @inheritDoc */
@@ -3928,10 +3964,7 @@ async function addEvent(
3928
3964
  return null;
3929
3965
  }
3930
3966
 
3931
- // TODO: sadness -- we will want to normalize timestamps to be in ms -
3932
- // requires coordination with frontend
3933
- const isMs = event.timestamp > 9999999999;
3934
- const timestampInMs = isMs ? event.timestamp : event.timestamp * 1000;
3967
+ const timestampInMs = timestampToMs(event.timestamp);
3935
3968
 
3936
3969
  // Throw out events that happen more than 5 minutes ago. This can happen if
3937
3970
  // page has been left open and idle for a long period of time and user
@@ -3941,15 +3974,12 @@ async function addEvent(
3941
3974
  return null;
3942
3975
  }
3943
3976
 
3944
- // Only record earliest event if a new session was created, otherwise it
3945
- // shouldn't be relevant
3946
- const earliestEvent = replay.getContext().earliestEvent;
3947
- if (replay.session && replay.session.segmentId === 0 && (!earliestEvent || timestampInMs < earliestEvent)) {
3948
- replay.getContext().earliestEvent = timestampInMs;
3949
- }
3950
-
3951
3977
  try {
3952
- return await replay.eventBuffer.addEvent(event, isCheckout);
3978
+ if (isCheckout) {
3979
+ replay.eventBuffer.clear();
3980
+ }
3981
+
3982
+ return await replay.eventBuffer.addEvent(event);
3953
3983
  } catch (error) {
3954
3984
  (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error(error);
3955
3985
  await replay.stop('addEvent');
@@ -5647,7 +5677,8 @@ function addGlobalListeners(replay) {
5647
5677
  client.on('afterSendEvent', handleAfterSendEvent(replay));
5648
5678
  client.on('createDsc', (dsc) => {
5649
5679
  const replayId = replay.getSessionId();
5650
- if (replayId && replay.isEnabled()) {
5680
+ // We do not want to set the DSC when in buffer mode, as that means the replay has not been sent (yet)
5681
+ if (replayId && replay.isEnabled() && replay.recordingMode === 'session') {
5651
5682
  dsc.replay_id = replayId;
5652
5683
  }
5653
5684
  });
@@ -5979,10 +6010,10 @@ function getHandleRecordingEmit(replay) {
5979
6010
  return true;
5980
6011
  }
5981
6012
 
5982
- // See note above re: session start needs to reflect the most recent
5983
- // checkout.
5984
- if (replay.recordingMode === 'buffer' && replay.session) {
5985
- const { earliestEvent } = replay.getContext();
6013
+ // When in buffer mode, make sure we adjust the session started date to the current earliest event of the buffer
6014
+ // this should usually be the timestamp of the checkout event, but to be safe...
6015
+ if (replay.recordingMode === 'buffer' && replay.session && replay.eventBuffer) {
6016
+ const earliestEvent = replay.eventBuffer.getEarliestTimestamp();
5986
6017
  if (earliestEvent) {
5987
6018
  replay.session.started = earliestEvent;
5988
6019
 
@@ -6388,7 +6419,6 @@ class ReplayContainer {
6388
6419
  errorIds: new Set(),
6389
6420
  traceIds: new Set(),
6390
6421
  urls: [],
6391
- earliestEvent: null,
6392
6422
  initialTimestamp: Date.now(),
6393
6423
  initialUrl: '',
6394
6424
  };}
@@ -7088,22 +7118,35 @@ class ReplayContainer {
7088
7118
  this._context.errorIds.clear();
7089
7119
  this._context.traceIds.clear();
7090
7120
  this._context.urls = [];
7091
- this._context.earliestEvent = null;
7121
+ }
7122
+
7123
+ /** Update the initial timestamp based on the buffer content. */
7124
+ _updateInitialTimestampFromEventBuffer() {
7125
+ const { session, eventBuffer } = this;
7126
+ if (!session || !eventBuffer) {
7127
+ return;
7128
+ }
7129
+
7130
+ // we only ever update this on the initial segment
7131
+ if (session.segmentId) {
7132
+ return;
7133
+ }
7134
+
7135
+ const earliestEvent = eventBuffer.getEarliestTimestamp();
7136
+ if (earliestEvent && earliestEvent < this._context.initialTimestamp) {
7137
+ this._context.initialTimestamp = earliestEvent;
7138
+ }
7092
7139
  }
7093
7140
 
7094
7141
  /**
7095
7142
  * Return and clear _context
7096
7143
  */
7097
7144
  _popEventContext() {
7098
- if (this._context.earliestEvent && this._context.earliestEvent < this._context.initialTimestamp) {
7099
- this._context.initialTimestamp = this._context.earliestEvent;
7100
- }
7101
-
7102
7145
  const _context = {
7103
7146
  initialTimestamp: this._context.initialTimestamp,
7104
7147
  initialUrl: this._context.initialUrl,
7105
- errorIds: Array.from(this._context.errorIds).filter(Boolean),
7106
- traceIds: Array.from(this._context.traceIds).filter(Boolean),
7148
+ errorIds: Array.from(this._context.errorIds),
7149
+ traceIds: Array.from(this._context.traceIds),
7107
7150
  urls: this._context.urls,
7108
7151
  };
7109
7152
 
@@ -7142,6 +7185,9 @@ class ReplayContainer {
7142
7185
  }
7143
7186
 
7144
7187
  try {
7188
+ // This uses the data from the eventBuffer, so we need to call this before `finish()
7189
+ this._updateInitialTimestampFromEventBuffer();
7190
+
7145
7191
  // Note this empties the event buffer regardless of outcome of sending replay
7146
7192
  const recordingData = await this.eventBuffer.finish();
7147
7193