userlens-session-recorder 2.0.0 → 2.0.2

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.
@@ -20,6 +20,7 @@ declare class SessionRecorder {
20
20
  private maskingOptions;
21
21
  private sessionUuid;
22
22
  private sessionEvents;
23
+ private bufferBytes;
23
24
  private rrwebStop;
24
25
  private debug;
25
26
  private recordCrossOriginIframes;
@@ -20675,21 +20675,41 @@
20675
20675
  return "ok";
20676
20676
  };
20677
20677
 
20678
- var _SessionRecorder_instances, _SessionRecorder_trackEventsThrottled, _SessionRecorder_uploading, _SessionRecorder_log, _SessionRecorder_initRecorder, _SessionRecorder_isUserInteraction, _SessionRecorder_handleEvent, _SessionRecorder_resetSession, _SessionRecorder_createSession, _SessionRecorder_handlePageHide, _SessionRecorder_initListeners, _SessionRecorder_throttle, _SessionRecorder_trackEvents, _SessionRecorder_clearEvents;
20678
+ var _SessionRecorder_instances, _SessionRecorder_uploading, _SessionRecorder_uploadingMaxTs, _SessionRecorder_bufferTimer, _SessionRecorder_log, _SessionRecorder_initRecorder, _SessionRecorder_isUserInteraction, _SessionRecorder_handleEvent, _SessionRecorder_armBufferTimer, _SessionRecorder_flushBuffer, _SessionRecorder_resetSession, _SessionRecorder_createSession, _SessionRecorder_handlePageHide, _SessionRecorder_initListeners, _SessionRecorder_trackEvents, _SessionRecorder_clearEvents;
20679
+ const BUFFER_TIMEOUT_MS = 2000;
20680
+ const MAX_BUFFER_BYTES = 900 * 1024;
20681
+ function estimateEventSize(event) {
20682
+ try {
20683
+ return JSON.stringify(event).length;
20684
+ }
20685
+ catch {
20686
+ return 0;
20687
+ }
20688
+ }
20679
20689
  class SessionRecorder {
20680
20690
  constructor(config) {
20681
20691
  var _a, _b, _c;
20682
20692
  _SessionRecorder_instances.add(this);
20683
20693
  this.sessionEvents = [];
20694
+ this.bufferBytes = 0;
20684
20695
  this.rrwebStop = null;
20685
20696
  this.debug = false;
20686
- _SessionRecorder_trackEventsThrottled.set(this, void 0);
20687
20697
  _SessionRecorder_uploading.set(this, false);
20698
+ _SessionRecorder_uploadingMaxTs.set(this, 0);
20699
+ _SessionRecorder_bufferTimer.set(this, null);
20688
20700
  _SessionRecorder_handlePageHide.set(this, () => {
20689
20701
  try {
20690
20702
  if (this.sessionEvents.length === 0)
20691
20703
  return;
20692
- const events = [...this.sessionEvents];
20704
+ let events;
20705
+ if (__classPrivateFieldGet(this, _SessionRecorder_uploading, "f") && __classPrivateFieldGet(this, _SessionRecorder_uploadingMaxTs, "f") > 0) {
20706
+ events = this.sessionEvents.filter((e) => e.timestamp > __classPrivateFieldGet(this, _SessionRecorder_uploadingMaxTs, "f"));
20707
+ }
20708
+ else {
20709
+ events = [...this.sessionEvents];
20710
+ }
20711
+ if (events.length === 0)
20712
+ return;
20693
20713
  const state = readSessionState();
20694
20714
  if (!state)
20695
20715
  return;
@@ -20708,6 +20728,7 @@
20708
20728
  keepalive: true,
20709
20729
  }).catch(() => { });
20710
20730
  this.sessionEvents = [];
20731
+ this.bufferBytes = 0;
20711
20732
  }
20712
20733
  catch (err) {
20713
20734
  __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_log).call(this, "Page hide handling failed", err);
@@ -20740,15 +20761,12 @@
20740
20761
  saveWriteCode(config.WRITE_CODE);
20741
20762
  this.userId = config.userId;
20742
20763
  const { recordingOptions = {} } = config;
20743
- const { TIMEOUT = 30 * 60 * 1000, BUFFER_SIZE = 10, maskingOptions = ["passwords"], recordCrossOriginIframes = false, } = recordingOptions;
20764
+ const { TIMEOUT = 30 * 60 * 1000, BUFFER_SIZE = 30, maskingOptions = ["passwords"], recordCrossOriginIframes = false, } = recordingOptions;
20744
20765
  this.TIMEOUT = TIMEOUT;
20745
20766
  this.BUFFER_SIZE = BUFFER_SIZE;
20746
20767
  this.maskingOptions = maskingOptions;
20747
20768
  this.recordCrossOriginIframes = recordCrossOriginIframes;
20748
20769
  this.sessionEvents = [];
20749
- __classPrivateFieldSet(this, _SessionRecorder_trackEventsThrottled, __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_throttle).call(this, () => {
20750
- __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_trackEvents).call(this);
20751
- }, 5000), "f");
20752
20770
  __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_initRecorder).call(this);
20753
20771
  }
20754
20772
  catch (err) {
@@ -20775,7 +20793,7 @@
20775
20793
  }
20776
20794
  }
20777
20795
  }
20778
- _SessionRecorder_trackEventsThrottled = new WeakMap(), _SessionRecorder_uploading = new WeakMap(), _SessionRecorder_handlePageHide = new WeakMap(), _SessionRecorder_instances = new WeakSet(), _SessionRecorder_log = function _SessionRecorder_log(message, error) {
20796
+ _SessionRecorder_uploading = new WeakMap(), _SessionRecorder_uploadingMaxTs = new WeakMap(), _SessionRecorder_bufferTimer = new WeakMap(), _SessionRecorder_handlePageHide = new WeakMap(), _SessionRecorder_instances = new WeakSet(), _SessionRecorder_log = function _SessionRecorder_log(message, error) {
20779
20797
  if (!this.debug)
20780
20798
  return;
20781
20799
  if (error) {
@@ -20809,7 +20827,6 @@
20809
20827
  }
20810
20828
  return false;
20811
20829
  }, _SessionRecorder_handleEvent = function _SessionRecorder_handleEvent(event, _isCheckout) {
20812
- var _a;
20813
20830
  try {
20814
20831
  const now = Date.now();
20815
20832
  const state = readSessionState();
@@ -20824,13 +20841,29 @@
20824
20841
  }
20825
20842
  }
20826
20843
  this.sessionEvents.push(event);
20827
- if (this.sessionEvents.length >= this.BUFFER_SIZE) {
20828
- (_a = __classPrivateFieldGet(this, _SessionRecorder_trackEventsThrottled, "f")) === null || _a === void 0 ? void 0 : _a.call(this);
20844
+ this.bufferBytes += estimateEventSize(event);
20845
+ __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_armBufferTimer).call(this);
20846
+ if (this.bufferBytes >= MAX_BUFFER_BYTES) {
20847
+ __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_flushBuffer).call(this);
20829
20848
  }
20830
20849
  }
20831
20850
  catch (err) {
20832
20851
  __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_log).call(this, "Event handling failed", err);
20833
20852
  }
20853
+ }, _SessionRecorder_armBufferTimer = function _SessionRecorder_armBufferTimer() {
20854
+ if (__classPrivateFieldGet(this, _SessionRecorder_bufferTimer, "f") !== null) {
20855
+ clearTimeout(__classPrivateFieldGet(this, _SessionRecorder_bufferTimer, "f"));
20856
+ }
20857
+ __classPrivateFieldSet(this, _SessionRecorder_bufferTimer, setTimeout(() => {
20858
+ __classPrivateFieldSet(this, _SessionRecorder_bufferTimer, null, "f");
20859
+ __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_flushBuffer).call(this);
20860
+ }, BUFFER_TIMEOUT_MS), "f");
20861
+ }, _SessionRecorder_flushBuffer = function _SessionRecorder_flushBuffer() {
20862
+ if (__classPrivateFieldGet(this, _SessionRecorder_bufferTimer, "f") !== null) {
20863
+ clearTimeout(__classPrivateFieldGet(this, _SessionRecorder_bufferTimer, "f"));
20864
+ __classPrivateFieldSet(this, _SessionRecorder_bufferTimer, null, "f");
20865
+ }
20866
+ void __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_trackEvents).call(this);
20834
20867
  }, _SessionRecorder_resetSession = function _SessionRecorder_resetSession() {
20835
20868
  clearSessionState();
20836
20869
  __classPrivateFieldGet(this, _SessionRecorder_instances, "m", _SessionRecorder_clearEvents).call(this);
@@ -20855,15 +20888,6 @@
20855
20888
  }
20856
20889
  }, _SessionRecorder_initListeners = function _SessionRecorder_initListeners() {
20857
20890
  window.addEventListener("pagehide", __classPrivateFieldGet(this, _SessionRecorder_handlePageHide, "f"));
20858
- }, _SessionRecorder_throttle = function _SessionRecorder_throttle(func, delay) {
20859
- let lastCall = 0;
20860
- return (...args) => {
20861
- const now = Date.now();
20862
- if (now - lastCall >= delay) {
20863
- lastCall = now;
20864
- func.apply(this, args);
20865
- }
20866
- };
20867
20891
  }, _SessionRecorder_trackEvents = async function _SessionRecorder_trackEvents() {
20868
20892
  if (__classPrivateFieldGet(this, _SessionRecorder_uploading, "f"))
20869
20893
  return;
@@ -20873,6 +20897,7 @@
20873
20897
  try {
20874
20898
  const events = [...this.sessionEvents];
20875
20899
  const snapshot_count = events.length;
20900
+ __classPrivateFieldSet(this, _SessionRecorder_uploadingMaxTs, events[events.length - 1].timestamp, "f");
20876
20901
  const start_ts_ms = events[0].timestamp;
20877
20902
  const end_ts_ms = events[events.length - 1].timestamp;
20878
20903
  const state = readSessionState();
@@ -20890,10 +20915,14 @@
20890
20915
  initial_url: chunk_seq === 0 ? state.initial_url : undefined,
20891
20916
  events,
20892
20917
  });
20918
+ const removedBytes = events.reduce((sum, e) => sum + estimateEventSize(e), 0);
20893
20919
  this.sessionEvents = this.sessionEvents.slice(snapshot_count);
20920
+ this.bufferBytes = Math.max(0, this.bufferBytes - removedBytes);
20894
20921
  const after = readSessionState();
20895
- if (after && after.session_uuid === state.session_uuid) {
20896
- writeSessionState({ ...after, chunk_seq: after.chunk_seq + 1 });
20922
+ if (after &&
20923
+ after.session_uuid === state.session_uuid &&
20924
+ after.chunk_seq === chunk_seq) {
20925
+ writeSessionState({ ...after, chunk_seq: chunk_seq + 1 });
20897
20926
  }
20898
20927
  }
20899
20928
  catch (err) {
@@ -20901,9 +20930,15 @@
20901
20930
  }
20902
20931
  finally {
20903
20932
  __classPrivateFieldSet(this, _SessionRecorder_uploading, false, "f");
20933
+ __classPrivateFieldSet(this, _SessionRecorder_uploadingMaxTs, 0, "f");
20904
20934
  }
20905
20935
  }, _SessionRecorder_clearEvents = function _SessionRecorder_clearEvents() {
20906
20936
  this.sessionEvents = [];
20937
+ this.bufferBytes = 0;
20938
+ if (__classPrivateFieldGet(this, _SessionRecorder_bufferTimer, "f") !== null) {
20939
+ clearTimeout(__classPrivateFieldGet(this, _SessionRecorder_bufferTimer, "f"));
20940
+ __classPrivateFieldSet(this, _SessionRecorder_bufferTimer, null, "f");
20941
+ }
20907
20942
  };
20908
20943
 
20909
20944
  exports.default = SessionRecorder;