@sentry/replay 0.2.0-16 → 0.2.0-19

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 (81) hide show
  1. package/dist/{jest.config.d.ts → config/dist/jest.config.d.ts} +0 -0
  2. package/dist/{jest.setup.d.ts → config/dist/jest.setup.d.ts} +2 -2
  3. package/dist/config/dist/src/api/captureReplay.d.ts +2 -0
  4. package/dist/config/dist/src/api/captureReplayUpdate.d.ts +2 -0
  5. package/dist/{src → config/dist/src}/coreHandlers/handleDom.d.ts +0 -0
  6. package/dist/{src → config/dist/src}/coreHandlers/handleFetch.d.ts +0 -0
  7. package/dist/{src/eventBuffer.test.d.ts → config/dist/src/coreHandlers/handleFetch.test.d.ts} +0 -0
  8. package/dist/{src/index.test.d.ts → config/dist/src/coreHandlers/handleScope-unit.test.d.ts} +0 -0
  9. package/dist/config/dist/src/coreHandlers/handleScope.d.ts +3 -0
  10. package/dist/{src/session/createSession.test.d.ts → config/dist/src/coreHandlers/handleScope.test.d.ts} +0 -0
  11. package/dist/{src → config/dist/src}/coreHandlers/handleXhr.d.ts +0 -0
  12. package/dist/{src → config/dist/src}/coreHandlers/index.d.ts +0 -0
  13. package/dist/{src → config/dist/src}/createPerformanceEntry.d.ts +0 -0
  14. package/dist/{src/session/fetchSession.test.d.ts → config/dist/src/createPerformanceEntry.test.d.ts} +0 -0
  15. package/dist/{src → config/dist/src}/eventBuffer.d.ts +1 -6
  16. package/dist/{src/session/getSession.test.d.ts → config/dist/src/eventBuffer.test.d.ts} +0 -0
  17. package/dist/{src → config/dist/src}/flags.d.ts +0 -0
  18. package/dist/{src/session/saveSession.test.d.ts → config/dist/src/index-noSticky.test.d.ts} +0 -0
  19. package/dist/{src → config/dist/src}/index.d.ts +6 -34
  20. package/dist/{src/session/updateSessionActivity.test.d.ts → config/dist/src/index.test.d.ts} +0 -0
  21. package/dist/config/dist/src/session/Session.d.ts +46 -0
  22. package/dist/{src/util/isExpired.test.d.ts → config/dist/src/session/Session.test.d.ts} +0 -0
  23. package/dist/{src → config/dist/src}/session/constants.d.ts +1 -1
  24. package/dist/{src → config/dist/src}/session/createSession.d.ts +2 -2
  25. package/dist/{src/util/isSessionExpired.test.d.ts → config/dist/src/session/createSession.test.d.ts} +0 -0
  26. package/dist/config/dist/src/session/fetchSession.d.ts +2 -0
  27. package/dist/config/dist/src/session/fetchSession.test.d.ts +1 -0
  28. package/dist/{src → config/dist/src}/session/getSession.d.ts +3 -3
  29. package/dist/config/dist/src/session/getSession.test.d.ts +1 -0
  30. package/dist/{src → config/dist/src}/session/index.d.ts +0 -1
  31. package/dist/config/dist/src/session/saveSession.d.ts +2 -0
  32. package/dist/config/dist/src/session/saveSession.test.d.ts +1 -0
  33. package/dist/{src → config/dist/src}/types.d.ts +26 -0
  34. package/dist/{src → config/dist/src}/util/createBreadcrumb.d.ts +0 -0
  35. package/dist/{src → config/dist/src}/util/isExpired.d.ts +0 -0
  36. package/dist/config/dist/src/util/isExpired.test.d.ts +1 -0
  37. package/dist/config/dist/src/util/isIngestHost.d.ts +4 -0
  38. package/dist/config/dist/src/util/isSessionExpired.d.ts +5 -0
  39. package/dist/config/dist/src/util/isSessionExpired.test.d.ts +1 -0
  40. package/dist/{src → config/dist/src}/util/logger.d.ts +1 -0
  41. package/dist/config/dist/test/index.d.ts +3 -0
  42. package/dist/config/dist/test/mocks/mockRrweb.d.ts +15 -0
  43. package/dist/config/dist/test/mocks/mockSdk.d.ts +11 -0
  44. package/dist/dist/coreHandlers/handleDom.d.ts +1 -0
  45. package/dist/dist/coreHandlers/handleFetch.d.ts +2 -0
  46. package/dist/dist/coreHandlers/handleScope.d.ts +3 -0
  47. package/dist/dist/coreHandlers/handleXhr.d.ts +2 -0
  48. package/dist/dist/coreHandlers/index.d.ts +4 -0
  49. package/dist/dist/createPerformanceEntry.d.ts +42 -0
  50. package/dist/dist/eventBuffer.d.ts +31 -0
  51. package/dist/dist/flags.d.ts +2 -0
  52. package/dist/dist/index.d.ts +147 -0
  53. package/dist/dist/session/Session.d.ts +46 -0
  54. package/dist/dist/session/constants.d.ts +5 -0
  55. package/dist/dist/session/createSession.d.ts +14 -0
  56. package/dist/dist/session/fetchSession.d.ts +2 -0
  57. package/dist/dist/session/getSession.d.ts +20 -0
  58. package/dist/dist/session/index.d.ts +1 -0
  59. package/dist/dist/session/saveSession.d.ts +2 -0
  60. package/dist/dist/types.d.ts +57 -0
  61. package/dist/dist/util/createBreadcrumb.d.ts +4 -0
  62. package/dist/dist/util/isExpired.d.ts +5 -0
  63. package/dist/dist/util/isIngestHost.d.ts +4 -0
  64. package/dist/dist/util/isSessionExpired.d.ts +5 -0
  65. package/dist/dist/util/logger.d.ts +12 -0
  66. package/dist/index.es.js +183 -81
  67. package/dist/index.js +183 -81
  68. package/dist/index.js.map +1 -1
  69. package/package.json +12 -9
  70. package/dist/rollup.config.d.ts +0 -3
  71. package/dist/src/coreHandlers/handleScope.d.ts +0 -2
  72. package/dist/src/createMemoryEntry.d.ts +0 -19
  73. package/dist/src/session/fetchSession.d.ts +0 -2
  74. package/dist/src/session/saveSession.d.ts +0 -2
  75. package/dist/src/session/types.d.ts +0 -6
  76. package/dist/src/session/updateSessionActivity.d.ts +0 -3
  77. package/dist/src/util/isSessionExpired.d.ts +0 -5
  78. package/dist/test/index.d.ts +0 -1
  79. package/dist/worker-9d2d4d71.js +0 -7
  80. package/dist/worker-9d2d4d71.js.map +0 -1
  81. package/dist/worker-c801dc28.js +0 -4
package/dist/index.js CHANGED
@@ -112,6 +112,14 @@ function __spreadArray(to, from, pack) {
112
112
  return to.concat(ar || Array.prototype.slice.call(from));
113
113
  }
114
114
 
115
+ /**
116
+ * Checks is `targetHost` is a Sentry ingestion host
117
+ */
118
+ function isIngestHost(targetHost) {
119
+ var _a = Sentry.getCurrentHub().getClient().getDsn(), protocol = _a.protocol, host = _a.host;
120
+ return targetHost.startsWith("".concat(protocol, "://").concat(host));
121
+ }
122
+
115
123
  var _a;
116
124
  // Map entryType -> function to normalize data for event
117
125
  var ENTRY_TYPES = (_a = {
@@ -157,11 +165,9 @@ function createNavigationEntry(entry) {
157
165
  };
158
166
  }
159
167
  function createResourceEntry(entry) {
160
- var _a, _b;
161
168
  var entryType = entry.entryType, initiatorType = entry.initiatorType, name = entry.name, responseEnd = entry.responseEnd, startTime = entry.startTime, transferSize = entry.transferSize;
162
169
  // Do not capture fetches to Sentry ingestion endpoint
163
- var _c = ((_b = (_a = Sentry.getCurrentHub()) === null || _a === void 0 ? void 0 : _a.getClient()) === null || _b === void 0 ? void 0 : _b.getDsn()) || {}, host = _c.host, protocol = _c.protocol;
164
- if (name.startsWith("".concat(protocol, "://").concat(host))) {
170
+ if (isIngestHost(name)) {
165
171
  return null;
166
172
  }
167
173
  return {
@@ -353,10 +359,10 @@ var EventBufferCompressionWorker = /** @class */ (function () {
353
359
  var REPLAY_SESSION_KEY = 'sentryReplaySession';
354
360
  var ROOT_REPLAY_NAME = 'sentry-replay';
355
361
  var REPLAY_EVENT_NAME = 'sentry-replay-event';
356
- // Grace period to keep a session when a user changes tabs or hides window
357
- var VISIBILITY_CHANGE_TIMEOUT = 60000; // 1 minute
358
362
  // The idle limit for a session
359
363
  var SESSION_IDLE_DURATION = 900000; // 15 minutes
364
+ // Grace period to keep a session when a user changes tabs or hides window
365
+ var VISIBILITY_CHANGE_TIMEOUT = SESSION_IDLE_DURATION;
360
366
 
361
367
  /**
362
368
  * Given an initial timestamp and an expiry duration, checks to see if current
@@ -396,6 +402,71 @@ function saveSession(session) {
396
402
  }
397
403
  }
398
404
 
405
+ var Session = /** @class */ (function () {
406
+ function Session(session, _a) {
407
+ if (session === void 0) { session = {}; }
408
+ var _b = _a === void 0 ? {} : _a, _c = _b.stickySession, stickySession = _c === void 0 ? false : _c;
409
+ var _d, _e, _f;
410
+ var now = new Date().getTime();
411
+ this._id = session.id || utils.uuid4();
412
+ this._started = (_d = session.started) !== null && _d !== void 0 ? _d : now;
413
+ this._lastActivity = (_e = session.lastActivity) !== null && _e !== void 0 ? _e : now;
414
+ this._sequenceId = (_f = session.sequenceId) !== null && _f !== void 0 ? _f : 0;
415
+ this.options = {
416
+ stickySession: stickySession,
417
+ };
418
+ }
419
+ Object.defineProperty(Session.prototype, "id", {
420
+ get: function () {
421
+ return this._id;
422
+ },
423
+ enumerable: false,
424
+ configurable: true
425
+ });
426
+ Object.defineProperty(Session.prototype, "started", {
427
+ get: function () {
428
+ return this._started;
429
+ },
430
+ enumerable: false,
431
+ configurable: true
432
+ });
433
+ Object.defineProperty(Session.prototype, "lastActivity", {
434
+ get: function () {
435
+ return this._lastActivity;
436
+ },
437
+ set: function (newDate) {
438
+ this._lastActivity = newDate;
439
+ if (this.options.stickySession) {
440
+ saveSession(this);
441
+ }
442
+ },
443
+ enumerable: false,
444
+ configurable: true
445
+ });
446
+ Object.defineProperty(Session.prototype, "sequenceId", {
447
+ get: function () {
448
+ return this._sequenceId;
449
+ },
450
+ set: function (id) {
451
+ this._sequenceId = id;
452
+ if (this.options.stickySession) {
453
+ saveSession(this);
454
+ }
455
+ },
456
+ enumerable: false,
457
+ configurable: true
458
+ });
459
+ Session.prototype.toJSON = function () {
460
+ return {
461
+ id: this.id,
462
+ started: this.started,
463
+ lastActivity: this.lastActivity,
464
+ sequenceId: this._sequenceId,
465
+ };
466
+ };
467
+ return Session;
468
+ }());
469
+
399
470
  /**
400
471
  * Create a new session, which in its current implementation is a Sentry event
401
472
  * that all replays will be saved to as attachments. Currently, we only expect
@@ -403,14 +474,8 @@ function saveSession(session) {
403
474
  */
404
475
  function createSession(_a) {
405
476
  var _b = _a.stickySession, stickySession = _b === void 0 ? false : _b;
406
- var currentDate = new Date().getTime();
407
477
  var hub = Sentry.getCurrentHub();
408
- var session = {
409
- id: utils.uuid4(),
410
- started: currentDate,
411
- lastActivity: currentDate,
412
- sequenceId: 0,
413
- };
478
+ var session = new Session(undefined, { stickySession: stickySession });
414
479
  hub.captureEvent({
415
480
  message: ROOT_REPLAY_NAME,
416
481
  tags: { sequenceId: session.sequenceId },
@@ -428,7 +493,10 @@ function fetchSession() {
428
493
  return null;
429
494
  }
430
495
  try {
431
- return JSON.parse(window.sessionStorage.getItem(REPLAY_SESSION_KEY));
496
+ return new Session(JSON.parse(window.sessionStorage.getItem(REPLAY_SESSION_KEY)),
497
+ // We are assuming that if there is a saved item, then the session is sticky,
498
+ // however this could break down if we used a different storage mechanism (e.g. localstorage)
499
+ { stickySession: true });
432
500
  }
433
501
  catch (_a) {
434
502
  return null;
@@ -458,24 +526,6 @@ function getSession(_a) {
458
526
  return newSession;
459
527
  }
460
528
 
461
- function updateSessionActivity(_a) {
462
- var stickySession = _a.stickySession;
463
- // Nothing to do if there are no sticky sessions
464
- if (!stickySession) {
465
- return;
466
- }
467
- var existingSession = fetchSession();
468
- // If user manually deleted from session storage, create a new session
469
- if (!existingSession) {
470
- // TBD: There was an issue here where sessions weren't saving and this
471
- // caused lots of transactions to be created
472
- return createSession({ stickySession: stickySession });
473
- }
474
- var newSession = __assign(__assign({}, existingSession), { lastActivity: new Date().getTime() });
475
- saveSession(newSession);
476
- return newSession;
477
- }
478
-
479
529
  function createBreadcrumb(breadcrumb) {
480
530
  return __assign({ timestamp: new Date().getTime() / 1000, type: 'default' }, breadcrumb);
481
531
  }
@@ -545,9 +595,10 @@ function handleXhr(handlerData) {
545
595
  if (!handlerData.endTimestamp) {
546
596
  return null;
547
597
  }
598
+ var _a = handlerData.args, op = _a[0], description = _a[1];
548
599
  return {
549
- description: handlerData.args[1],
550
- op: handlerData.args[0],
600
+ description: description,
601
+ op: op,
551
602
  startTimestamp: handlerData.xhr.__sentry_xhr__.startTimestamp / 1000 ||
552
603
  handlerData.endTimestamp / 1000.0,
553
604
  endTimestamp: handlerData.endTimestamp / 1000.0,
@@ -591,58 +642,51 @@ var SentryReplay = /** @class */ (function () {
591
642
  this.retryCount = 0;
592
643
  this.retryInterval = BASE_RETRY_INTERVAL;
593
644
  /**
594
- * Handle when visibility of the page changes. (e.g. new tab is opened)
645
+ * If current browser window in an active state
646
+ */
647
+ this.isActive = true;
648
+ /**
649
+ * Handle when visibility of the page content changes. Opening a new tab will
650
+ * cause the state to change to hidden because of content of current page will
651
+ * be hidden. Likewise, moving a different window to cover the contents of the
652
+ * page will also trigger a change to a hidden state.
595
653
  */
596
654
  this.handleVisibilityChange = function () {
597
- var isExpired = isSessionExpired(_this.session, VISIBILITY_CHANGE_TIMEOUT);
598
- if (isExpired) {
599
- if (document.visibilityState === 'visible') {
600
- // If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
601
- // ms, we will re-use the existing session, otherwise create a new
602
- // session
603
- logger.log('Document has become active, but session has expired');
604
- _this.loadSession({ expiry: VISIBILITY_CHANGE_TIMEOUT });
605
- _this.triggerFullSnapshot();
606
- }
607
- // We definitely want to return if visibilityState is "visible", and I
608
- // think we also want to do the same when "hidden", as there shouldn't be
609
- // any action to take if user has gone idle for a long period and then
610
- // comes back to hide the tab. We don't trigger a full snapshot because
611
- // we don't want to start a new session as they immediately have hidden
612
- // the tab.
613
- return;
655
+ var breadcrumb = createBreadcrumb({
656
+ category: 'ui.other',
657
+ data: {
658
+ label: "Page is ".concat(document.visibilityState),
659
+ },
660
+ });
661
+ if (document.visibilityState === 'visible') {
662
+ _this.doChangeToForegroundTasks(breadcrumb);
614
663
  }
615
- // Otherwise if session is not expired...
616
- // Update with current timestamp as the last session activity
617
- // Only updating session on visibility change to be conservative about
618
- // writing to session storage. This could be changed in the future.
619
- _this.updateSession(updateSessionActivity({
620
- stickySession: _this.options.stickySession,
621
- }));
622
- // Send replay when the page/tab becomes hidden. There is no reason to send
623
- // replay if it becomes visible, since no actions we care about were done
624
- // while it was hidden
625
- if (document.visibilityState !== 'visible') {
626
- _this.flushUpdate();
664
+ else {
665
+ _this.doChangeToBackgroundTasks(breadcrumb);
627
666
  }
628
667
  };
629
- this.handleWindowUnload = function () {
668
+ /**
669
+ * Handle when page is blurred
670
+ */
671
+ this.handleWindowBlur = function () {
630
672
  var breadcrumb = createBreadcrumb({
631
- category: 'ui.exit',
632
- message: '',
673
+ category: 'ui.blur',
633
674
  });
634
- _this.addUpdate(function () {
635
- _this.eventBuffer.addEvent({
636
- type: rrweb.EventType.Custom,
637
- // TODO: We were converting from ms to seconds for breadcrumbs, spans,
638
- // but maybe we should just keep them as milliseconds
639
- timestamp: breadcrumb.timestamp,
640
- data: {
641
- tag: 'breadcrumb',
642
- payload: breadcrumb,
643
- },
644
- });
675
+ _this.doChangeToBackgroundTasks(breadcrumb);
676
+ };
677
+ /**
678
+ * Handle when page is focused
679
+ */
680
+ this.handleWindowFocus = function () {
681
+ var breadcrumb = createBreadcrumb({
682
+ category: 'ui.focus',
645
683
  });
684
+ _this.doChangeToForegroundTasks(breadcrumb);
685
+ };
686
+ this.handleWindowUnload = function () {
687
+ _this.createCustomBreadcrumb(createBreadcrumb({
688
+ category: 'ui.exit',
689
+ }));
646
690
  };
647
691
  this.handleCoreListener = function (type) { return function (handlerData) {
648
692
  var handlerMap = {
@@ -807,12 +851,11 @@ var SentryReplay = /** @class */ (function () {
807
851
  currentSession: this.session,
808
852
  });
809
853
  };
810
- SentryReplay.prototype.updateSession = function (session) {
811
- this.session = __assign(__assign({}, this.session), session);
812
- };
813
854
  SentryReplay.prototype.addListeners = function () {
814
855
  var _this = this;
815
856
  document.addEventListener('visibilitychange', this.handleVisibilityChange);
857
+ window.addEventListener('blur', this.handleWindowBlur);
858
+ window.addEventListener('focus', this.handleWindowFocus);
816
859
  window.addEventListener('beforeunload', this.handleWindowUnload);
817
860
  // Listeners from core SDK //
818
861
  var scope = Sentry__namespace.getCurrentHub().getScope();
@@ -849,6 +892,48 @@ var SentryReplay = /** @class */ (function () {
849
892
  this.performanceObserver = null;
850
893
  }
851
894
  };
895
+ /**
896
+ * Tasks to run when we consider a page to be hidden (via blurring and/or visibility)
897
+ */
898
+ SentryReplay.prototype.doChangeToBackgroundTasks = function (breadcrumb) {
899
+ var isExpired = isSessionExpired(this.session, VISIBILITY_CHANGE_TIMEOUT);
900
+ // We check current state to make sure that we do nothing if the page is already inactive
901
+ if (!this.isActive) {
902
+ return;
903
+ }
904
+ this.createCustomBreadcrumb(__assign(__assign({}, breadcrumb), {
905
+ // if somehow the page went hidden while session is expired, attach to previous session
906
+ timestamp: isExpired
907
+ ? this.session.lastActivity / 1000
908
+ : breadcrumb.timestamp }));
909
+ this.isActive = false;
910
+ // Send replay when the page/tab becomes hidden. There is no reason to send
911
+ // replay if it becomes visible, since no actions we care about were done
912
+ // while it was hidden
913
+ this.flushUpdate();
914
+ };
915
+ /**
916
+ * Tasks to run when we consider a page to be visible (via focus and/or visibility)
917
+ */
918
+ SentryReplay.prototype.doChangeToForegroundTasks = function (breadcrumb) {
919
+ var isExpired = isSessionExpired(this.session, VISIBILITY_CHANGE_TIMEOUT);
920
+ this.isActive = true;
921
+ this.createCustomBreadcrumb(__assign(__assign({}, breadcrumb), { timestamp: isExpired ? new Date().getTime() / 1000 : breadcrumb.timestamp }));
922
+ if (isExpired) {
923
+ // If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
924
+ // ms, we will re-use the existing session, otherwise create a new
925
+ // session
926
+ logger.log('Document has become active, but session has expired');
927
+ this.loadSession({ expiry: VISIBILITY_CHANGE_TIMEOUT });
928
+ this.triggerFullSnapshot();
929
+ return;
930
+ }
931
+ // Otherwise if session is not expired...
932
+ // Update with current timestamp as the last session activity
933
+ // Only updating session on visibility change to be conservative about
934
+ // writing to session storage. This could be changed in the future.
935
+ this.updateLastActivity();
936
+ };
852
937
  /**
853
938
  * Trigger rrweb to take a full snapshot which will cause this plugin to
854
939
  * create a new Replay event.
@@ -857,6 +942,23 @@ var SentryReplay = /** @class */ (function () {
857
942
  logger.log('Taking full rrweb snapshot');
858
943
  rrweb.record.takeFullSnapshot(true);
859
944
  };
945
+ SentryReplay.prototype.updateLastActivity = function (lastActivity) {
946
+ if (lastActivity === void 0) { lastActivity = new Date().getTime(); }
947
+ this.session.lastActivity = lastActivity;
948
+ };
949
+ SentryReplay.prototype.createCustomBreadcrumb = function (breadcrumb) {
950
+ var _this = this;
951
+ this.addUpdate(function () {
952
+ _this.eventBuffer.addEvent({
953
+ type: rrweb.EventType.Custom,
954
+ timestamp: breadcrumb.timestamp,
955
+ data: {
956
+ tag: 'breadcrumb',
957
+ payload: breadcrumb,
958
+ },
959
+ });
960
+ });
961
+ };
860
962
  /**
861
963
  * Create a span for each performance entry. The parent transaction is `this.replayEvent`.
862
964
  */
@@ -949,7 +1051,7 @@ var SentryReplay = /** @class */ (function () {
949
1051
  this.sendReplay(this.session.id, recordingData);
950
1052
  this.initialEventTimestampSinceFlush = null;
951
1053
  // TBD: Alternatively we could update this after every rrweb event
952
- this.session.lastActivity = lastActivity || new Date().getTime();
1054
+ this.updateLastActivity(lastActivity);
953
1055
  return [2 /*return*/];
954
1056
  }
955
1057
  });