@sentry/replay 7.50.0 → 7.51.1

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 (32) hide show
  1. package/cjs/index.js +329 -136
  2. package/cjs/index.js.map +1 -1
  3. package/esm/index.js +329 -136
  4. package/esm/index.js.map +1 -1
  5. package/package.json +7 -7
  6. package/types/coreHandlers/handleAfterSendEvent.d.ts.map +1 -1
  7. package/types/coreHandlers/handleDom.d.ts +7 -2
  8. package/types/coreHandlers/handleDom.d.ts.map +1 -1
  9. package/types/coreHandlers/handleGlobalEvent.d.ts.map +1 -1
  10. package/types/coreHandlers/util/shouldSampleForBufferEvent.d.ts +9 -0
  11. package/types/coreHandlers/util/shouldSampleForBufferEvent.d.ts.map +1 -0
  12. package/types/eventBuffer/EventBufferArray.d.ts +8 -2
  13. package/types/eventBuffer/EventBufferArray.d.ts.map +1 -1
  14. package/types/eventBuffer/EventBufferCompressionWorker.d.ts +11 -6
  15. package/types/eventBuffer/EventBufferCompressionWorker.d.ts.map +1 -1
  16. package/types/eventBuffer/EventBufferProxy.d.ts +8 -2
  17. package/types/eventBuffer/EventBufferProxy.d.ts.map +1 -1
  18. package/types/integration.d.ts.map +1 -1
  19. package/types/replay.d.ts +4 -0
  20. package/types/replay.d.ts.map +1 -1
  21. package/types/types/rrweb.d.ts +1 -1
  22. package/types/types/rrweb.d.ts.map +1 -1
  23. package/types/types.d.ts +25 -9
  24. package/types/types.d.ts.map +1 -1
  25. package/types/util/addEvent.d.ts.map +1 -1
  26. package/types/util/addGlobalListeners.d.ts.map +1 -1
  27. package/types/util/handleRecordingEmit.d.ts +4 -0
  28. package/types/util/handleRecordingEmit.d.ts.map +1 -1
  29. package/types/util/sendReplayRequest.d.ts +1 -1
  30. package/types/util/sendReplayRequest.d.ts.map +1 -1
  31. package/types/util/timestampToMs.d.ts +5 -0
  32. package/types/util/timestampToMs.d.ts.map +1 -0
package/cjs/index.js CHANGED
@@ -75,7 +75,7 @@ function maskInputValue({ input, maskInputSelector, unmaskInputSelector, maskInp
75
75
  if (unmaskInputSelector && input.matches(unmaskInputSelector)) {
76
76
  return text;
77
77
  }
78
- if (input.hasAttribute('rr_is_password')) {
78
+ if (input.hasAttribute('data-rr-is-password')) {
79
79
  type = 'password';
80
80
  }
81
81
  if (isInputTypeMasked({ maskInputOptions, tagName, type }) ||
@@ -108,6 +108,21 @@ function is2DCanvasBlank(canvas) {
108
108
  }
109
109
  return true;
110
110
  }
111
+ function getInputType(element) {
112
+ const type = element.type;
113
+ return element.hasAttribute('data-rr-is-password')
114
+ ? 'password'
115
+ : type
116
+ ? type.toLowerCase()
117
+ : null;
118
+ }
119
+ function getInputValue(el, tagName, type) {
120
+ typeof type === 'string' ? type.toLowerCase() : '';
121
+ if (tagName === 'INPUT' && (type === 'radio' || type === 'checkbox')) {
122
+ return el.getAttribute('value') || '';
123
+ }
124
+ return el.value;
125
+ }
111
126
 
112
127
  let _id = 1;
113
128
  const tagNameRegex = new RegExp('[^a-z0-9-_:]');
@@ -146,6 +161,13 @@ function getCssRuleString(rule) {
146
161
  catch (_a) {
147
162
  }
148
163
  }
164
+ return validateStringifiedCssRule(cssStringified);
165
+ }
166
+ function validateStringifiedCssRule(cssStringified) {
167
+ if (cssStringified.indexOf(':') > -1) {
168
+ const regex = /(\[(?:[\w-]+)[^\\])(:(?:[\w-]+)\])/gm;
169
+ return cssStringified.replace(regex, '$1\\$2');
170
+ }
149
171
  return cssStringified;
150
172
  }
151
173
  function isCSSImportRule(rule) {
@@ -154,7 +176,7 @@ function isCSSImportRule(rule) {
154
176
  function stringifyStyleSheet(sheet) {
155
177
  return sheet.cssRules
156
178
  ? Array.from(sheet.cssRules)
157
- .map((rule) => rule.cssText || '')
179
+ .map((rule) => rule.cssText ? validateStringifiedCssRule(rule.cssText) : '')
158
180
  .join('')
159
181
  : '';
160
182
  }
@@ -489,14 +511,15 @@ function serializeNode(n, options) {
489
511
  tagName === 'select' ||
490
512
  tagName === 'option') {
491
513
  const el = n;
492
- const value = getInputValue(tagName, el, attributes);
514
+ const type = getInputType(el);
515
+ const value = getInputValue(el, tagName.toUpperCase(), type);
493
516
  const checked = n.checked;
494
- if (attributes.type !== 'submit' &&
495
- attributes.type !== 'button' &&
517
+ if (type !== 'submit' &&
518
+ type !== 'button' &&
496
519
  value) {
497
520
  attributes.value = maskInputValue({
498
521
  input: el,
499
- type: attributes.type,
522
+ type,
500
523
  tagName,
501
524
  value,
502
525
  maskInputSelector,
@@ -969,15 +992,8 @@ function snapshot(n, options) {
969
992
  function skipAttribute(tagName, attributeName, value) {
970
993
  return ((tagName === 'video' || tagName === 'audio') && attributeName === 'autoplay');
971
994
  }
972
- function getInputValue(tagName, el, attributes) {
973
- if (tagName === 'input' &&
974
- (attributes.type === 'radio' || attributes.type === 'checkbox')) {
975
- return el.getAttribute('value') || '';
976
- }
977
- return el.value;
978
- }
979
995
 
980
- var EventType;
996
+ var EventType$1;
981
997
  (function (EventType) {
982
998
  EventType[EventType["DomContentLoaded"] = 0] = "DomContentLoaded";
983
999
  EventType[EventType["Load"] = 1] = "Load";
@@ -986,7 +1002,7 @@ var EventType;
986
1002
  EventType[EventType["Meta"] = 4] = "Meta";
987
1003
  EventType[EventType["Custom"] = 5] = "Custom";
988
1004
  EventType[EventType["Plugin"] = 6] = "Plugin";
989
- })(EventType || (EventType = {}));
1005
+ })(EventType$1 || (EventType$1 = {}));
990
1006
  var IncrementalSource;
991
1007
  (function (IncrementalSource) {
992
1008
  IncrementalSource[IncrementalSource["Mutation"] = 0] = "Mutation";
@@ -1601,9 +1617,9 @@ class MutationBuffer {
1601
1617
  this.attributes.push(item);
1602
1618
  }
1603
1619
  if (m.attributeName === 'type' &&
1604
- m.target.tagName === 'INPUT' &&
1620
+ target.tagName === 'INPUT' &&
1605
1621
  (m.oldValue || '').toLowerCase() === 'password') {
1606
- m.target.setAttribute('rr_is_password', 'true');
1622
+ target.setAttribute('data-rr-is-password', 'true');
1607
1623
  }
1608
1624
  if (m.attributeName === 'style') {
1609
1625
  const old = this.doc.createElement('span');
@@ -2000,27 +2016,25 @@ function initInputObserver({ inputCb, doc, mirror, blockClass, blockSelector, un
2000
2016
  isBlocked(target, blockClass, blockSelector, unblockSelector)) {
2001
2017
  return;
2002
2018
  }
2003
- let type = target.type;
2004
- if (target.classList.contains(ignoreClass) ||
2005
- (ignoreSelector && target.matches(ignoreSelector))) {
2019
+ const el = target;
2020
+ const type = getInputType(el);
2021
+ if (el.classList.contains(ignoreClass) ||
2022
+ (ignoreSelector && el.matches(ignoreSelector))) {
2006
2023
  return;
2007
2024
  }
2008
- let text = target.value;
2025
+ let text = getInputValue(el, tagName, type);
2009
2026
  let isChecked = false;
2010
- if (target.hasAttribute('rr_is_password')) {
2011
- type = 'password';
2012
- }
2013
2027
  if (type === 'radio' || type === 'checkbox') {
2014
2028
  isChecked = target.checked;
2015
2029
  }
2016
- else if (hasInputMaskOptions({
2030
+ if (hasInputMaskOptions({
2017
2031
  maskInputOptions,
2018
2032
  maskInputSelector,
2019
2033
  tagName,
2020
2034
  type,
2021
2035
  })) {
2022
2036
  text = maskInputValue({
2023
- input: target,
2037
+ input: el,
2024
2038
  maskInputOptions,
2025
2039
  maskInputSelector,
2026
2040
  unmaskInputSelector,
@@ -2037,8 +2051,18 @@ function initInputObserver({ inputCb, doc, mirror, blockClass, blockSelector, un
2037
2051
  .querySelectorAll(`input[type="radio"][name="${name}"]`)
2038
2052
  .forEach((el) => {
2039
2053
  if (el !== target) {
2054
+ const text = maskInputValue({
2055
+ input: el,
2056
+ maskInputOptions,
2057
+ maskInputSelector,
2058
+ unmaskInputSelector,
2059
+ tagName,
2060
+ type,
2061
+ value: getInputValue(el, tagName, type),
2062
+ maskInputFn,
2063
+ });
2040
2064
  cbWithDedup(el, callbackWrapper(wrapEventWithUserTriggeredFlag)({
2041
- text: el.value,
2065
+ text,
2042
2066
  isChecked: !isChecked,
2043
2067
  userTriggered: false,
2044
2068
  }, userTriggeredOnInput));
@@ -2951,17 +2975,17 @@ function record(options = {}) {
2951
2975
  wrappedEmit = (e, isCheckout) => {
2952
2976
  var _a;
2953
2977
  if (((_a = mutationBuffers[0]) === null || _a === void 0 ? void 0 : _a.isFrozen()) &&
2954
- e.type !== EventType.FullSnapshot &&
2955
- !(e.type === EventType.IncrementalSnapshot &&
2978
+ e.type !== EventType$1.FullSnapshot &&
2979
+ !(e.type === EventType$1.IncrementalSnapshot &&
2956
2980
  e.data.source === IncrementalSource.Mutation)) {
2957
2981
  mutationBuffers.forEach((buf) => buf.unfreeze());
2958
2982
  }
2959
2983
  emit(eventProcessor(e), isCheckout);
2960
- if (e.type === EventType.FullSnapshot) {
2984
+ if (e.type === EventType$1.FullSnapshot) {
2961
2985
  lastFullSnapshotEvent = e;
2962
2986
  incrementalSnapshotCount = 0;
2963
2987
  }
2964
- else if (e.type === EventType.IncrementalSnapshot) {
2988
+ else if (e.type === EventType$1.IncrementalSnapshot) {
2965
2989
  if (e.data.source === IncrementalSource.Mutation &&
2966
2990
  e.data.isAttachIframe) {
2967
2991
  return;
@@ -2977,16 +3001,16 @@ function record(options = {}) {
2977
3001
  };
2978
3002
  const wrappedMutationEmit = (m) => {
2979
3003
  wrappedEmit(wrapEvent({
2980
- type: EventType.IncrementalSnapshot,
3004
+ type: EventType$1.IncrementalSnapshot,
2981
3005
  data: Object.assign({ source: IncrementalSource.Mutation }, m),
2982
3006
  }));
2983
3007
  };
2984
3008
  const wrappedScrollEmit = (p) => wrappedEmit(wrapEvent({
2985
- type: EventType.IncrementalSnapshot,
3009
+ type: EventType$1.IncrementalSnapshot,
2986
3010
  data: Object.assign({ source: IncrementalSource.Scroll }, p),
2987
3011
  }));
2988
3012
  const wrappedCanvasMutationEmit = (p) => wrappedEmit(wrapEvent({
2989
- type: EventType.IncrementalSnapshot,
3013
+ type: EventType$1.IncrementalSnapshot,
2990
3014
  data: Object.assign({ source: IncrementalSource.CanvasMutation }, p),
2991
3015
  }));
2992
3016
  const iframeManager = new IframeManager({
@@ -3031,7 +3055,7 @@ function record(options = {}) {
3031
3055
  takeFullSnapshot = (isCheckout = false) => {
3032
3056
  var _a, _b, _c, _d;
3033
3057
  wrappedEmit(wrapEvent({
3034
- type: EventType.Meta,
3058
+ type: EventType$1.Meta,
3035
3059
  data: {
3036
3060
  href: window.location.href,
3037
3061
  width: getWindowWidth(),
@@ -3074,7 +3098,7 @@ function record(options = {}) {
3074
3098
  }
3075
3099
  mirror.map = idNodeMap;
3076
3100
  wrappedEmit(wrapEvent({
3077
- type: EventType.FullSnapshot,
3101
+ type: EventType$1.FullSnapshot,
3078
3102
  data: {
3079
3103
  node,
3080
3104
  initialOffset: {
@@ -3099,7 +3123,7 @@ function record(options = {}) {
3099
3123
  const handlers = [];
3100
3124
  handlers.push(on('DOMContentLoaded', () => {
3101
3125
  wrappedEmit(wrapEvent({
3102
- type: EventType.DomContentLoaded,
3126
+ type: EventType$1.DomContentLoaded,
3103
3127
  data: {},
3104
3128
  }));
3105
3129
  }));
@@ -3109,40 +3133,40 @@ function record(options = {}) {
3109
3133
  onMutation,
3110
3134
  mutationCb: wrappedMutationEmit,
3111
3135
  mousemoveCb: (positions, source) => wrappedEmit(wrapEvent({
3112
- type: EventType.IncrementalSnapshot,
3136
+ type: EventType$1.IncrementalSnapshot,
3113
3137
  data: {
3114
3138
  source,
3115
3139
  positions,
3116
3140
  },
3117
3141
  })),
3118
3142
  mouseInteractionCb: (d) => wrappedEmit(wrapEvent({
3119
- type: EventType.IncrementalSnapshot,
3143
+ type: EventType$1.IncrementalSnapshot,
3120
3144
  data: Object.assign({ source: IncrementalSource.MouseInteraction }, d),
3121
3145
  })),
3122
3146
  scrollCb: wrappedScrollEmit,
3123
3147
  viewportResizeCb: (d) => wrappedEmit(wrapEvent({
3124
- type: EventType.IncrementalSnapshot,
3148
+ type: EventType$1.IncrementalSnapshot,
3125
3149
  data: Object.assign({ source: IncrementalSource.ViewportResize }, d),
3126
3150
  })),
3127
3151
  inputCb: (v) => wrappedEmit(wrapEvent({
3128
- type: EventType.IncrementalSnapshot,
3152
+ type: EventType$1.IncrementalSnapshot,
3129
3153
  data: Object.assign({ source: IncrementalSource.Input }, v),
3130
3154
  })),
3131
3155
  mediaInteractionCb: (p) => wrappedEmit(wrapEvent({
3132
- type: EventType.IncrementalSnapshot,
3156
+ type: EventType$1.IncrementalSnapshot,
3133
3157
  data: Object.assign({ source: IncrementalSource.MediaInteraction }, p),
3134
3158
  })),
3135
3159
  styleSheetRuleCb: (r) => wrappedEmit(wrapEvent({
3136
- type: EventType.IncrementalSnapshot,
3160
+ type: EventType$1.IncrementalSnapshot,
3137
3161
  data: Object.assign({ source: IncrementalSource.StyleSheetRule }, r),
3138
3162
  })),
3139
3163
  styleDeclarationCb: (r) => wrappedEmit(wrapEvent({
3140
- type: EventType.IncrementalSnapshot,
3164
+ type: EventType$1.IncrementalSnapshot,
3141
3165
  data: Object.assign({ source: IncrementalSource.StyleDeclaration }, r),
3142
3166
  })),
3143
3167
  canvasMutationCb: wrappedCanvasMutationEmit,
3144
3168
  fontCb: (p) => wrappedEmit(wrapEvent({
3145
- type: EventType.IncrementalSnapshot,
3169
+ type: EventType$1.IncrementalSnapshot,
3146
3170
  data: Object.assign({ source: IncrementalSource.Font }, p),
3147
3171
  })),
3148
3172
  blockClass,
@@ -3175,7 +3199,7 @@ function record(options = {}) {
3175
3199
  observer: p.observer,
3176
3200
  options: p.options,
3177
3201
  callback: (payload) => wrappedEmit(wrapEvent({
3178
- type: EventType.Plugin,
3202
+ type: EventType$1.Plugin,
3179
3203
  data: {
3180
3204
  plugin: p.name,
3181
3205
  payload,
@@ -3203,7 +3227,7 @@ function record(options = {}) {
3203
3227
  else {
3204
3228
  handlers.push(on('load', () => {
3205
3229
  wrappedEmit(wrapEvent({
3206
- type: EventType.Load,
3230
+ type: EventType$1.Load,
3207
3231
  data: {},
3208
3232
  }));
3209
3233
  init();
@@ -3222,7 +3246,7 @@ record.addCustomEvent = (tag, payload) => {
3222
3246
  throw new Error('please add custom event after start recording');
3223
3247
  }
3224
3248
  wrappedEmit(wrapEvent({
3225
- type: EventType.Custom,
3249
+ type: EventType$1.Custom,
3226
3250
  data: {
3227
3251
  tag,
3228
3252
  payload,
@@ -3377,6 +3401,14 @@ function t(t){let e=t.length;for(;--e>=0;)t[e]=0}const e=new Uint8Array([0,0,0,0
3377
3401
 
3378
3402
  function e(){const e=new Blob([r]);return URL.createObjectURL(e)}
3379
3403
 
3404
+ /**
3405
+ * Converts a timestamp to ms, if it was in s, or keeps it as ms.
3406
+ */
3407
+ function timestampToMs(timestamp) {
3408
+ const isMs = timestamp > 9999999999;
3409
+ return isMs ? timestamp : timestamp * 1000;
3410
+ }
3411
+
3380
3412
  /**
3381
3413
  * A basic event buffer that does not do any compression.
3382
3414
  * Used as fallback if the compression worker cannot be loaded or is disabled.
@@ -3393,20 +3425,19 @@ class EventBufferArray {
3393
3425
  return this.events.length > 0;
3394
3426
  }
3395
3427
 
3428
+ /** @inheritdoc */
3429
+ get type() {
3430
+ return 'sync';
3431
+ }
3432
+
3396
3433
  /** @inheritdoc */
3397
3434
  destroy() {
3398
3435
  this.events = [];
3399
3436
  }
3400
3437
 
3401
3438
  /** @inheritdoc */
3402
- async addEvent(event, isCheckout) {
3403
- if (isCheckout) {
3404
- this.events = [event];
3405
- return;
3406
- }
3407
-
3439
+ async addEvent(event) {
3408
3440
  this.events.push(event);
3409
- return;
3410
3441
  }
3411
3442
 
3412
3443
  /** @inheritdoc */
@@ -3420,6 +3451,22 @@ class EventBufferArray {
3420
3451
  resolve(JSON.stringify(eventsRet));
3421
3452
  });
3422
3453
  }
3454
+
3455
+ /** @inheritdoc */
3456
+ clear() {
3457
+ this.events = [];
3458
+ }
3459
+
3460
+ /** @inheritdoc */
3461
+ getEarliestTimestamp() {
3462
+ const timestamp = this.events.map(event => event.timestamp).sort()[0];
3463
+
3464
+ if (!timestamp) {
3465
+ return null;
3466
+ }
3467
+
3468
+ return timestampToMs(timestamp);
3469
+ }
3423
3470
  }
3424
3471
 
3425
3472
  /**
@@ -3527,11 +3574,20 @@ class WorkerHandler {
3527
3574
  * Exported only for testing.
3528
3575
  */
3529
3576
  class EventBufferCompressionWorker {
3530
- /** @inheritdoc */
3531
3577
 
3532
3578
  constructor(worker) {
3533
3579
  this._worker = new WorkerHandler(worker);
3534
- this.hasEvents = false;
3580
+ this._earliestTimestamp = null;
3581
+ }
3582
+
3583
+ /** @inheritdoc */
3584
+ get hasEvents() {
3585
+ return !!this._earliestTimestamp;
3586
+ }
3587
+
3588
+ /** @inheritdoc */
3589
+ get type() {
3590
+ return 'worker';
3535
3591
  }
3536
3592
 
3537
3593
  /**
@@ -3554,13 +3610,10 @@ class EventBufferCompressionWorker {
3554
3610
  *
3555
3611
  * Returns true if event was successfuly received and processed by worker.
3556
3612
  */
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();
3613
+ addEvent(event) {
3614
+ const timestamp = timestampToMs(event.timestamp);
3615
+ if (!this._earliestTimestamp || timestamp < this._earliestTimestamp) {
3616
+ this._earliestTimestamp = timestamp;
3564
3617
  }
3565
3618
 
3566
3619
  return this._sendEventToWorker(event);
@@ -3573,6 +3626,18 @@ class EventBufferCompressionWorker {
3573
3626
  return this._finishRequest();
3574
3627
  }
3575
3628
 
3629
+ /** @inheritdoc */
3630
+ clear() {
3631
+ this._earliestTimestamp = null;
3632
+ // We do not wait on this, as we assume the order of messages is consistent for the worker
3633
+ void this._worker.postMessage('clear');
3634
+ }
3635
+
3636
+ /** @inheritdoc */
3637
+ getEarliestTimestamp() {
3638
+ return this._earliestTimestamp;
3639
+ }
3640
+
3576
3641
  /**
3577
3642
  * Send the event to the worker.
3578
3643
  */
@@ -3586,15 +3651,10 @@ class EventBufferCompressionWorker {
3586
3651
  async _finishRequest() {
3587
3652
  const response = await this._worker.postMessage('finish');
3588
3653
 
3589
- this.hasEvents = false;
3654
+ this._earliestTimestamp = null;
3590
3655
 
3591
3656
  return response;
3592
3657
  }
3593
-
3594
- /** Clear any pending events from the worker. */
3595
- _clear() {
3596
- return this._worker.postMessage('clear');
3597
- }
3598
3658
  }
3599
3659
 
3600
3660
  /**
@@ -3612,6 +3672,11 @@ class EventBufferProxy {
3612
3672
  this._ensureWorkerIsLoadedPromise = this._ensureWorkerIsLoaded();
3613
3673
  }
3614
3674
 
3675
+ /** @inheritdoc */
3676
+ get type() {
3677
+ return this._used.type;
3678
+ }
3679
+
3615
3680
  /** @inheritDoc */
3616
3681
  get hasEvents() {
3617
3682
  return this._used.hasEvents;
@@ -3623,13 +3688,23 @@ class EventBufferProxy {
3623
3688
  this._compression.destroy();
3624
3689
  }
3625
3690
 
3691
+ /** @inheritdoc */
3692
+ clear() {
3693
+ return this._used.clear();
3694
+ }
3695
+
3696
+ /** @inheritdoc */
3697
+ getEarliestTimestamp() {
3698
+ return this._used.getEarliestTimestamp();
3699
+ }
3700
+
3626
3701
  /**
3627
3702
  * Add an event to the event buffer.
3628
3703
  *
3629
3704
  * Returns true if event was successfully added.
3630
3705
  */
3631
- addEvent(event, isCheckout) {
3632
- return this._used.addEvent(event, isCheckout);
3706
+ addEvent(event) {
3707
+ return this._used.addEvent(event);
3633
3708
  }
3634
3709
 
3635
3710
  /** @inheritDoc */
@@ -3930,10 +4005,7 @@ async function addEvent(
3930
4005
  return null;
3931
4006
  }
3932
4007
 
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;
4008
+ const timestampInMs = timestampToMs(event.timestamp);
3937
4009
 
3938
4010
  // Throw out events that happen more than 5 minutes ago. This can happen if
3939
4011
  // page has been left open and idle for a long period of time and user
@@ -3943,15 +4015,12 @@ async function addEvent(
3943
4015
  return null;
3944
4016
  }
3945
4017
 
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
4018
  try {
3954
- return await replay.eventBuffer.addEvent(event, isCheckout);
4019
+ if (isCheckout) {
4020
+ replay.eventBuffer.clear();
4021
+ }
4022
+
4023
+ return await replay.eventBuffer.addEvent(event);
3955
4024
  } catch (error) {
3956
4025
  (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.error(error);
3957
4026
  await replay.stop('addEvent');
@@ -4013,22 +4082,18 @@ function handleAfterSendEvent(replay) {
4013
4082
  return;
4014
4083
  }
4015
4084
 
4016
- // Add error to list of errorIds of replay
4085
+ // Add error to list of errorIds of replay. This is ok to do even if not
4086
+ // sampled because context will get reset at next checkout.
4087
+ // XXX: There is also a race condition where it's possible to capture an
4088
+ // error to Sentry before Replay SDK has loaded, but response returns after
4089
+ // it was loaded, and this gets called.
4017
4090
  if (event.event_id) {
4018
4091
  replay.getContext().errorIds.add(event.event_id);
4019
4092
  }
4020
4093
 
4021
- // Trigger error recording
4094
+ // If error event is tagged with replay id it means it was sampled (when in buffer mode)
4022
4095
  // Need to be very careful that this does not cause an infinite loop
4023
- if (
4024
- replay.recordingMode === 'buffer' &&
4025
- event.exception &&
4026
- event.message !== UNABLE_TO_SEND_REPLAY // ignore this error because otherwise we could loop indefinitely with trying to capture replay and failing
4027
- ) {
4028
- if (!isSampled(replay.getOptions().errorSampleRate)) {
4029
- return;
4030
- }
4031
-
4096
+ if (replay.recordingMode === 'buffer' && event.tags && event.tags.replayId) {
4032
4097
  setTimeout(() => {
4033
4098
  // Capture current event buffer as new replay
4034
4099
  void replay.sendBufferedReplayOrFlush();
@@ -4092,7 +4157,7 @@ function addBreadcrumbEvent(replay, breadcrumb) {
4092
4157
 
4093
4158
  replay.addUpdate(() => {
4094
4159
  void addEvent(replay, {
4095
- type: EventType.Custom,
4160
+ type: EventType$1.Custom,
4096
4161
  // TODO: We were converting from ms to seconds for breadcrumbs, spans,
4097
4162
  // but maybe we should just keep them as milliseconds
4098
4163
  timestamp: (breadcrumb.timestamp || 0) * 1000,
@@ -4160,15 +4225,18 @@ const handleDomListener =
4160
4225
 
4161
4226
  /**
4162
4227
  * An event handler to react to DOM events.
4228
+ * Exported for tests only.
4163
4229
  */
4164
4230
  function handleDom(handlerData) {
4165
4231
  let target;
4166
4232
  let targetNode;
4167
4233
 
4234
+ const isClick = handlerData.name === 'click';
4235
+
4168
4236
  // Accessing event.target can throw (see getsentry/raven-js#838, #768)
4169
4237
  try {
4170
- targetNode = getTargetNode(handlerData);
4171
- target = utils.htmlTreeAsString(targetNode);
4238
+ targetNode = isClick ? getClickTargetNode(handlerData.event) : getTargetNode(handlerData.event);
4239
+ target = utils.htmlTreeAsString(targetNode, { maxStringLength: 200 });
4172
4240
  } catch (e) {
4173
4241
  target = '<unknown>';
4174
4242
  }
@@ -4202,12 +4270,29 @@ function handleDom(handlerData) {
4202
4270
  });
4203
4271
  }
4204
4272
 
4205
- function getTargetNode(handlerData) {
4206
- if (isEventWithTarget(handlerData.event)) {
4207
- return handlerData.event.target;
4273
+ function getTargetNode(event) {
4274
+ if (isEventWithTarget(event)) {
4275
+ return event.target;
4208
4276
  }
4209
4277
 
4210
- return handlerData.event;
4278
+ return event;
4279
+ }
4280
+
4281
+ const INTERACTIVE_SELECTOR = 'button,a';
4282
+
4283
+ // For clicks, we check if the target is inside of a button or link
4284
+ // If so, we use this as the target instead
4285
+ // This is useful because if you click on the image in <button><img></button>,
4286
+ // The target will be the image, not the button, which we don't want here
4287
+ function getClickTargetNode(event) {
4288
+ const target = getTargetNode(event);
4289
+
4290
+ if (!target || !(target instanceof Element)) {
4291
+ return target;
4292
+ }
4293
+
4294
+ const closestInteractive = target.closest(INTERACTIVE_SELECTOR);
4295
+ return closestInteractive || target;
4211
4296
  }
4212
4297
 
4213
4298
  function isEventWithTarget(event) {
@@ -4237,6 +4322,30 @@ function isRrwebError(event, hint) {
4237
4322
  });
4238
4323
  }
4239
4324
 
4325
+ /**
4326
+ * Determine if event should be sampled (only applies in buffer mode).
4327
+ * When an event is captured by `hanldleGlobalEvent`, when in buffer mode
4328
+ * we determine if we want to sample the error or not.
4329
+ */
4330
+ function shouldSampleForBufferEvent(replay, event) {
4331
+ if (replay.recordingMode !== 'buffer') {
4332
+ return false;
4333
+ }
4334
+
4335
+ // ignore this error because otherwise we could loop indefinitely with
4336
+ // trying to capture replay and failing
4337
+ if (event.message === UNABLE_TO_SEND_REPLAY) {
4338
+ return false;
4339
+ }
4340
+
4341
+ // Require the event to be an error event & to have an exception
4342
+ if (!event.exception || event.type) {
4343
+ return false;
4344
+ }
4345
+
4346
+ return isSampled(replay.getOptions().errorSampleRate);
4347
+ }
4348
+
4240
4349
  /**
4241
4350
  * Returns a listener to be added to `addGlobalEventProcessor(listener)`.
4242
4351
  */
@@ -4266,8 +4375,16 @@ function handleGlobalEventListener(
4266
4375
  return null;
4267
4376
  }
4268
4377
 
4269
- // Only tag transactions with replayId if not waiting for an error
4270
- if (isErrorEvent(event) || (isTransactionEvent(event) && replay.recordingMode === 'session')) {
4378
+ // When in buffer mode, we decide to sample here.
4379
+ // Later, in `handleAfterSendEvent`, if the replayId is set, we know that we sampled
4380
+ // And convert the buffer session to a full session
4381
+ const isErrorEventSampled = shouldSampleForBufferEvent(replay, event);
4382
+
4383
+ // Tag errors if it has been sampled in buffer mode, or if it is session mode
4384
+ // Only tag transactions if in session mode
4385
+ const shouldTagReplayId = isErrorEventSampled || replay.recordingMode === 'session';
4386
+
4387
+ if (shouldTagReplayId) {
4271
4388
  event.tags = { ...event.tags, replayId: replay.getSessionId() };
4272
4389
  }
4273
4390
 
@@ -4317,7 +4434,7 @@ function createPerformanceSpans(
4317
4434
  ) {
4318
4435
  return entries.map(({ type, start, end, name, data }) =>
4319
4436
  addEvent(replay, {
4320
- type: EventType.Custom,
4437
+ type: EventType$1.Custom,
4321
4438
  timestamp: start,
4322
4439
  data: {
4323
4440
  tag: 'performanceSpan',
@@ -5649,7 +5766,8 @@ function addGlobalListeners(replay) {
5649
5766
  client.on('afterSendEvent', handleAfterSendEvent(replay));
5650
5767
  client.on('createDsc', (dsc) => {
5651
5768
  const replayId = replay.getSessionId();
5652
- if (replayId && replay.isEnabled()) {
5769
+ // We do not want to set the DSC when in buffer mode, as that means the replay has not been sent (yet)
5770
+ if (replayId && replay.isEnabled() && replay.recordingMode === 'session') {
5653
5771
  dsc.replay_id = replayId;
5654
5772
  }
5655
5773
  });
@@ -5929,6 +6047,23 @@ function debounce(func, wait, options) {
5929
6047
  return debounced;
5930
6048
  }
5931
6049
 
6050
+ /* eslint-disable @typescript-eslint/naming-convention */
6051
+
6052
+ var EventType; (function (EventType) {
6053
+ const DomContentLoaded = 0; EventType[EventType["DomContentLoaded"] = DomContentLoaded] = "DomContentLoaded";
6054
+ const Load = 1; EventType[EventType["Load"] = Load] = "Load";
6055
+ const FullSnapshot = 2; EventType[EventType["FullSnapshot"] = FullSnapshot] = "FullSnapshot";
6056
+ const IncrementalSnapshot = 3; EventType[EventType["IncrementalSnapshot"] = IncrementalSnapshot] = "IncrementalSnapshot";
6057
+ const Meta = 4; EventType[EventType["Meta"] = Meta] = "Meta";
6058
+ const Custom = 5; EventType[EventType["Custom"] = Custom] = "Custom";
6059
+ const Plugin = 6; EventType[EventType["Plugin"] = Plugin] = "Plugin";
6060
+ })(EventType || (EventType = {}));
6061
+
6062
+ /**
6063
+ * This is a partial copy of rrweb's eventWithTime type which only contains the properties
6064
+ * we specifcally need in the SDK.
6065
+ */
6066
+
5932
6067
  /**
5933
6068
  * Handler for recording events.
5934
6069
  *
@@ -5971,6 +6106,14 @@ function getHandleRecordingEmit(replay) {
5971
6106
  return false;
5972
6107
  }
5973
6108
 
6109
+ // Additionally, create a meta event that will capture certain SDK settings.
6110
+ // In order to handle buffer mode, this needs to either be done when we
6111
+ // receive checkout events or at flush time.
6112
+ //
6113
+ // `isCheckout` is always true, but want to be explicit that it should
6114
+ // only be added for checkouts
6115
+ void addSettingsEvent(replay, isCheckout);
6116
+
5974
6117
  // If there is a previousSessionId after a full snapshot occurs, then
5975
6118
  // the replay session was started due to session expiration. The new session
5976
6119
  // is started before triggering a new checkout and contains the id
@@ -5981,10 +6124,10 @@ function getHandleRecordingEmit(replay) {
5981
6124
  return true;
5982
6125
  }
5983
6126
 
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();
6127
+ // When in buffer mode, make sure we adjust the session started date to the current earliest event of the buffer
6128
+ // this should usually be the timestamp of the checkout event, but to be safe...
6129
+ if (replay.recordingMode === 'buffer' && replay.session && replay.eventBuffer) {
6130
+ const earliestEvent = replay.eventBuffer.getEarliestTimestamp();
5988
6131
  if (earliestEvent) {
5989
6132
  replay.session.started = earliestEvent;
5990
6133
 
@@ -6008,6 +6151,46 @@ function getHandleRecordingEmit(replay) {
6008
6151
  };
6009
6152
  }
6010
6153
 
6154
+ /**
6155
+ * Exported for tests
6156
+ */
6157
+ function createOptionsEvent(replay) {
6158
+ const options = replay.getOptions();
6159
+ return {
6160
+ type: EventType.Custom,
6161
+ timestamp: Date.now(),
6162
+ data: {
6163
+ tag: 'options',
6164
+ payload: {
6165
+ sessionSampleRate: options.sessionSampleRate,
6166
+ errorSampleRate: options.errorSampleRate,
6167
+ useCompressionOption: options.useCompression,
6168
+ blockAllMedia: options.blockAllMedia,
6169
+ maskAllText: options.maskAllText,
6170
+ maskAllInputs: options.maskAllInputs,
6171
+ useCompression: replay.eventBuffer ? replay.eventBuffer.type === 'worker' : false,
6172
+ networkDetailHasUrls: options.networkDetailAllowUrls.length > 0,
6173
+ networkCaptureBodies: options.networkCaptureBodies,
6174
+ networkRequestHasHeaders: options.networkRequestHeaders.length > 0,
6175
+ networkResponseHasHeaders: options.networkResponseHeaders.length > 0,
6176
+ },
6177
+ },
6178
+ };
6179
+ }
6180
+
6181
+ /**
6182
+ * Add a "meta" event that contains a simplified view on current configuration
6183
+ * options. This should only be included on the first segment of a recording.
6184
+ */
6185
+ function addSettingsEvent(replay, isCheckout) {
6186
+ // Only need to add this event when sending the first segment
6187
+ if (!isCheckout || !replay.session || replay.session.segmentId !== 0) {
6188
+ return Promise.resolve(null);
6189
+ }
6190
+
6191
+ return addEvent(replay, createOptionsEvent(replay), false);
6192
+ }
6193
+
6011
6194
  /**
6012
6195
  * Create a replay envelope ready to be sent.
6013
6196
  * This includes both the replay event, as well as the recording data.
@@ -6122,7 +6305,6 @@ async function sendReplayRequest({
6122
6305
  eventContext,
6123
6306
  timestamp,
6124
6307
  session,
6125
- options,
6126
6308
  }) {
6127
6309
  const preparedRecordingData = prepareRecordingData({
6128
6310
  recordingData,
@@ -6164,15 +6346,6 @@ async function sendReplayRequest({
6164
6346
  return;
6165
6347
  }
6166
6348
 
6167
- replayEvent.contexts = {
6168
- ...replayEvent.contexts,
6169
- replay: {
6170
- ...(replayEvent.contexts && replayEvent.contexts.replay),
6171
- session_sample_rate: options.sessionSampleRate,
6172
- error_sample_rate: options.errorSampleRate,
6173
- },
6174
- };
6175
-
6176
6349
  /*
6177
6350
  For reference, the fully built event looks something like this:
6178
6351
  {
@@ -6203,10 +6376,6 @@ async function sendReplayRequest({
6203
6376
  },
6204
6377
  "sdkProcessingMetadata": {},
6205
6378
  "contexts": {
6206
- "replay": {
6207
- "session_sample_rate": 1,
6208
- "error_sample_rate": 0,
6209
- },
6210
6379
  },
6211
6380
  }
6212
6381
  */
@@ -6390,7 +6559,6 @@ class ReplayContainer {
6390
6559
  errorIds: new Set(),
6391
6560
  traceIds: new Set(),
6392
6561
  urls: [],
6393
- earliestEvent: null,
6394
6562
  initialTimestamp: Date.now(),
6395
6563
  initialUrl: '',
6396
6564
  };}
@@ -6400,7 +6568,7 @@ class ReplayContainer {
6400
6568
  recordingOptions,
6401
6569
  }
6402
6570
 
6403
- ) {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);
6571
+ ) {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);ReplayContainer.prototype.__init18.call(this);
6404
6572
  this._recordingOptions = recordingOptions;
6405
6573
  this._options = options;
6406
6574
 
@@ -6892,6 +7060,7 @@ class ReplayContainer {
6892
7060
  WINDOW.document.addEventListener('visibilitychange', this._handleVisibilityChange);
6893
7061
  WINDOW.addEventListener('blur', this._handleWindowBlur);
6894
7062
  WINDOW.addEventListener('focus', this._handleWindowFocus);
7063
+ WINDOW.addEventListener('keydown', this._handleKeyboardEvent);
6895
7064
 
6896
7065
  // There is no way to remove these listeners, so ensure they are only added once
6897
7066
  if (!this._hasInitializedCoreListeners) {
@@ -6920,6 +7089,7 @@ class ReplayContainer {
6920
7089
 
6921
7090
  WINDOW.removeEventListener('blur', this._handleWindowBlur);
6922
7091
  WINDOW.removeEventListener('focus', this._handleWindowFocus);
7092
+ WINDOW.removeEventListener('keydown', this._handleKeyboardEvent);
6923
7093
 
6924
7094
  if (this._performanceObserver) {
6925
7095
  this._performanceObserver.disconnect();
@@ -6970,6 +7140,11 @@ class ReplayContainer {
6970
7140
  this._doChangeToForegroundTasks(breadcrumb);
6971
7141
  };}
6972
7142
 
7143
+ /** Ensure page remains active when a key is pressed. */
7144
+ __init16() {this._handleKeyboardEvent = () => {
7145
+ this.triggerUserActivity();
7146
+ };}
7147
+
6973
7148
  /**
6974
7149
  * Tasks to run when we consider a page to be hidden (via blurring and/or visibility)
6975
7150
  */
@@ -7049,7 +7224,7 @@ class ReplayContainer {
7049
7224
  _createCustomBreadcrumb(breadcrumb) {
7050
7225
  this.addUpdate(() => {
7051
7226
  void addEvent(this, {
7052
- type: EventType.Custom,
7227
+ type: EventType$1.Custom,
7053
7228
  timestamp: breadcrumb.timestamp || 0,
7054
7229
  data: {
7055
7230
  tag: 'breadcrumb',
@@ -7090,22 +7265,35 @@ class ReplayContainer {
7090
7265
  this._context.errorIds.clear();
7091
7266
  this._context.traceIds.clear();
7092
7267
  this._context.urls = [];
7093
- this._context.earliestEvent = null;
7268
+ }
7269
+
7270
+ /** Update the initial timestamp based on the buffer content. */
7271
+ _updateInitialTimestampFromEventBuffer() {
7272
+ const { session, eventBuffer } = this;
7273
+ if (!session || !eventBuffer) {
7274
+ return;
7275
+ }
7276
+
7277
+ // we only ever update this on the initial segment
7278
+ if (session.segmentId) {
7279
+ return;
7280
+ }
7281
+
7282
+ const earliestEvent = eventBuffer.getEarliestTimestamp();
7283
+ if (earliestEvent && earliestEvent < this._context.initialTimestamp) {
7284
+ this._context.initialTimestamp = earliestEvent;
7285
+ }
7094
7286
  }
7095
7287
 
7096
7288
  /**
7097
7289
  * Return and clear _context
7098
7290
  */
7099
7291
  _popEventContext() {
7100
- if (this._context.earliestEvent && this._context.earliestEvent < this._context.initialTimestamp) {
7101
- this._context.initialTimestamp = this._context.earliestEvent;
7102
- }
7103
-
7104
7292
  const _context = {
7105
7293
  initialTimestamp: this._context.initialTimestamp,
7106
7294
  initialUrl: this._context.initialUrl,
7107
- errorIds: Array.from(this._context.errorIds).filter(Boolean),
7108
- traceIds: Array.from(this._context.traceIds).filter(Boolean),
7295
+ errorIds: Array.from(this._context.errorIds),
7296
+ traceIds: Array.from(this._context.traceIds),
7109
7297
  urls: this._context.urls,
7110
7298
  };
7111
7299
 
@@ -7144,6 +7332,9 @@ class ReplayContainer {
7144
7332
  }
7145
7333
 
7146
7334
  try {
7335
+ // This uses the data from the eventBuffer, so we need to call this before `finish()
7336
+ this._updateInitialTimestampFromEventBuffer();
7337
+
7147
7338
  // Note this empties the event buffer regardless of outcome of sending replay
7148
7339
  const recordingData = await this.eventBuffer.finish();
7149
7340
 
@@ -7184,7 +7375,7 @@ class ReplayContainer {
7184
7375
  * Flush recording data to Sentry. Creates a lock so that only a single flush
7185
7376
  * can be active at a time. Do not call this directly.
7186
7377
  */
7187
- __init16() {this._flush = async ({
7378
+ __init17() {this._flush = async ({
7188
7379
  force = false,
7189
7380
  }
7190
7381
 
@@ -7239,7 +7430,7 @@ class ReplayContainer {
7239
7430
  }
7240
7431
 
7241
7432
  /** Handler for rrweb.record.onMutation */
7242
- __init17() {this._onMutationHandler = (mutations) => {
7433
+ __init18() {this._onMutationHandler = (mutations) => {
7243
7434
  const count = mutations.length;
7244
7435
 
7245
7436
  const mutationLimit = this._options._experiments.mutationLimit || 0;
@@ -7478,6 +7669,8 @@ class Replay {
7478
7669
  errorSampleRate,
7479
7670
  useCompression,
7480
7671
  blockAllMedia,
7672
+ maskAllInputs,
7673
+ maskAllText,
7481
7674
  networkDetailAllowUrls,
7482
7675
  networkCaptureBodies,
7483
7676
  networkRequestHeaders: _getMergedNetworkHeaders(networkRequestHeaders),