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