@sentry/replay 0.2.0-10 → 0.2.0-14

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/dist/index.js CHANGED
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var Sentry = require('@sentry/browser');
6
- var rrweb = require('rrweb');
7
6
  var utils = require('@sentry/utils');
7
+ var rrweb = require('rrweb');
8
8
 
9
9
  function _interopNamespace(e) {
10
10
  if (e && e.__esModule) return e;
@@ -178,7 +178,7 @@ function createLargestContentfulPaint(entry) {
178
178
  end: start + duration,
179
179
  data: {
180
180
  size: size,
181
- // @ts-expect-error Not sure why this errors, Node should be correct (Argument of type 'Node' is not assignable to parameter of type 'INode')
181
+ // Not sure why this errors, Node should be correct (Argument of type 'Node' is not assignable to parameter of type 'INode')
182
182
  nodeId: rrweb.record.mirror.getId(entry.element),
183
183
  },
184
184
  };
@@ -270,25 +270,18 @@ function saveSession(session) {
270
270
  function createSession(_a) {
271
271
  var _b = _a.stickySession, stickySession = _b === void 0 ? false : _b;
272
272
  var currentDate = new Date().getTime();
273
- // Create root replay event, this is where attachments will be saved
274
- var transaction = Sentry__namespace.getCurrentHub().startTransaction({
275
- name: ROOT_REPLAY_NAME,
276
- tags: {
277
- isReplayRoot: 'yes',
278
- },
279
- });
280
- // We have to finish the transaction to get an event ID to be able to
281
- // upload an attachment for that event
282
- // @ts-expect-error This returns an eventId (string), but is not typed as such
283
- var id = transaction.finish();
284
- logger.log("Creating new session: ".concat(id));
273
+ var hub = Sentry.getCurrentHub();
285
274
  var session = {
286
- id: id,
287
- spanId: transaction.spanId,
288
- traceId: transaction.traceId,
275
+ id: utils.uuid4(),
289
276
  started: currentDate,
290
277
  lastActivity: currentDate,
278
+ sequenceId: 0,
291
279
  };
280
+ hub.captureEvent({
281
+ message: ROOT_REPLAY_NAME,
282
+ tags: { sequenceId: session.sequenceId },
283
+ }, { event_id: session.id });
284
+ logger.log("Creating new session: ".concat(session.id));
292
285
  if (stickySession) {
293
286
  saveSession(session);
294
287
  }
@@ -312,8 +305,8 @@ function fetchSession() {
312
305
  * Get or create a session
313
306
  */
314
307
  function getSession(_a) {
315
- var expiry = _a.expiry, stickySession = _a.stickySession;
316
- var session = stickySession && fetchSession();
308
+ var expiry = _a.expiry, currentSession = _a.currentSession, stickySession = _a.stickySession;
309
+ var session = stickySession ? fetchSession() : currentSession;
317
310
  if (session) {
318
311
  // If there is a session, check if it is valid (e.g. "last activity" time should be within the "session idle time")
319
312
  // TODO: We should probably set a max age on this as well
@@ -349,6 +342,74 @@ function updateSessionActivity(_a) {
349
342
  return newSession;
350
343
  }
351
344
 
345
+ function handleDom(handlerData) {
346
+ // Taken from https://github.com/getsentry/sentry-javascript/blob/master/packages/browser/src/integrations/breadcrumbs.ts#L112
347
+ var target;
348
+ var targetNode;
349
+ // Accessing event.target can throw (see getsentry/raven-js#838, #768)
350
+ try {
351
+ targetNode =
352
+ handlerData.event.target ||
353
+ handlerData.event;
354
+ target = utils.htmlTreeAsString(targetNode);
355
+ }
356
+ catch (e) {
357
+ target = '<unknown>';
358
+ }
359
+ if (target.length === 0) {
360
+ return null;
361
+ }
362
+ return {
363
+ timestamp: new Date().getTime() / 1000,
364
+ type: 'default',
365
+ category: "ui.".concat(handlerData.name),
366
+ message: target,
367
+ data: __assign({}, (targetNode ? { nodeId: rrweb.record.mirror.getId(targetNode) } : {})),
368
+ };
369
+ }
370
+
371
+ function handleFetch(handlerData) {
372
+ // TODO: add status code into data, etc.
373
+ if (!handlerData.endTimestamp) {
374
+ return null;
375
+ }
376
+ return {
377
+ description: handlerData.args[1],
378
+ op: handlerData.args[0],
379
+ startTimestamp: handlerData.startTimestamp / 1000,
380
+ endTimestamp: handlerData.endTimestamp / 1000,
381
+ };
382
+ }
383
+
384
+ function handleScope(scope) {
385
+ //@ts-expect-error using private val
386
+ var newBreadcrumb = scope._breadcrumbs[scope._breadcrumbs.length - 1];
387
+ if (['fetch', 'xhr', 'sentry.event'].includes(newBreadcrumb.category) ||
388
+ newBreadcrumb.category.startsWith('ui.')) {
389
+ return null;
390
+ }
391
+ return __assign({ type: 'default' }, newBreadcrumb);
392
+ }
393
+
394
+ function handleXhr(handlerData) {
395
+ // TODO: add status code into data, etc.
396
+ if (handlerData.startTimestamp) {
397
+ handlerData.xhr.__sentry_xhr__.startTimestamp = handlerData.startTimestamp;
398
+ }
399
+ if (!handlerData.endTimestamp) {
400
+ return null;
401
+ }
402
+ return {
403
+ description: handlerData.args[1],
404
+ op: handlerData.args[0],
405
+ startTimestamp: handlerData.xhr.__sentry_xhr__.startTimestamp / 1000 ||
406
+ handlerData.endTimestamp / 1000.0,
407
+ endTimestamp: handlerData.endTimestamp / 1000.0,
408
+ };
409
+ }
410
+
411
+ var BASE_RETRY_INTERVAL = 5000;
412
+ var MAX_RETRY_COUNT = 5;
352
413
  var SentryReplay = /** @class */ (function () {
353
414
  function SentryReplay(_a) {
354
415
  if (_a === void 0) { _a = {}; }
@@ -364,6 +425,17 @@ var SentryReplay = /** @class */ (function () {
364
425
  * Buffer of rrweb events that will be serialized as JSON and saved as an attachment to a Sentry event
365
426
  */
366
427
  this.events = [];
428
+ /**
429
+ * Buffer of breadcrumbs to be uploaded
430
+ */
431
+ this.breadcrumbs = [];
432
+ /**
433
+ * Buffer of replay spans to be uploaded
434
+ */
435
+ this.replaySpans = [];
436
+ /**
437
+ * List of PerformanceEntry from PerformanceObserver
438
+ */
367
439
  this.performanceEvents = [];
368
440
  /**
369
441
  * The timestamp of the first event since the last flush.
@@ -371,14 +443,8 @@ var SentryReplay = /** @class */ (function () {
371
443
  */
372
444
  this.initialEventTimestampSinceFlush = null;
373
445
  this.performanceObserver = null;
374
- /**
375
- * Keep a list of performance entries that will be sent with a replay
376
- */
377
- this.handlePerformanceObserver = function (list
378
- // observer: PerformanceObserver
379
- ) {
380
- _this.performanceEvents = __spreadArray(__spreadArray([], _this.performanceEvents, true), list.getEntries(), true);
381
- };
446
+ this.retryCount = 0;
447
+ this.retryInterval = BASE_RETRY_INTERVAL;
382
448
  /**
383
449
  * Handle when visibility of the page changes. (e.g. new tab is opened)
384
450
  */
@@ -405,25 +471,51 @@ var SentryReplay = /** @class */ (function () {
405
471
  // Update with current timestamp as the last session activity
406
472
  // Only updating session on visibility change to be conservative about
407
473
  // writing to session storage. This could be changed in the future.
408
- updateSessionActivity({
474
+ _this.updateSession(updateSessionActivity({
409
475
  stickySession: _this.options.stickySession,
410
- });
476
+ }));
411
477
  // Send replay when the page/tab becomes hidden. There is no reason to send
412
478
  // replay if it becomes visible, since no actions we care about were done
413
479
  // while it was hidden
414
480
  if (document.visibilityState !== 'visible') {
415
- _this.finishReplayEvent();
481
+ _this.flushUpdate();
416
482
  }
417
483
  };
484
+ this.handleCoreListener = function (type) { return function (handlerData) {
485
+ var handlerMap = {
486
+ scope: [handleScope, _this.breadcrumbs],
487
+ dom: [handleDom, _this.breadcrumbs],
488
+ fetch: [handleFetch, _this.replaySpans],
489
+ xhr: [handleXhr, _this.replaySpans],
490
+ };
491
+ if (!(type in handlerMap)) {
492
+ throw new Error("No handler defined for type: ".concat(type));
493
+ }
494
+ var _a = handlerMap[type], handlerFn = _a[0], handlerStore = _a[1];
495
+ var result = handlerFn(handlerData);
496
+ if (result === null) {
497
+ return;
498
+ }
499
+ handlerStore.push(result);
500
+ _this.addUpdate();
501
+ }; };
502
+ /**
503
+ * Keep a list of performance entries that will be sent with a replay
504
+ */
505
+ this.handlePerformanceObserver = function (list
506
+ // observer: PerformanceObserver
507
+ ) {
508
+ _this.performanceEvents = __spreadArray(__spreadArray([], _this.performanceEvents, true), list.getEntries(), true);
509
+ };
418
510
  this.rrwebRecordOptions = __assign({ maskAllInputs: maskAllInputs, blockClass: blockClass, ignoreClass: ignoreClass, maskTextClass: maskTextClass }, rrwebRecordOptions);
419
511
  this.options = { uploadMinDelay: uploadMinDelay, uploadMaxDelay: uploadMaxDelay, stickySession: stickySession };
420
512
  this.events = [];
421
513
  }
422
514
  SentryReplay.attachmentUrlFromDsn = function (dsn, eventId) {
423
- var host = dsn.host, projectId = dsn.projectId, protocol = dsn.protocol, user = dsn.user;
515
+ var host = dsn.host, projectId = dsn.projectId, protocol = dsn.protocol, publicKey = dsn.publicKey;
424
516
  var port = dsn.port !== '' ? ":".concat(dsn.port) : '';
425
517
  var path = dsn.path !== '' ? "/".concat(dsn.path) : '';
426
- return "".concat(protocol, "://").concat(host).concat(port).concat(path, "/api/").concat(projectId, "/events/").concat(eventId, "/attachments/?sentry_key=").concat(user, "&sentry_version=7&sentry_client=replay");
518
+ return "".concat(protocol, "://").concat(host).concat(port).concat(path, "/api/").concat(projectId, "/events/").concat(eventId, "/attachments/?sentry_key=").concat(publicKey, "&sentry_version=7&sentry_client=replay");
427
519
  };
428
520
  SentryReplay.prototype.setupOnce = function () {
429
521
  var _this = this;
@@ -444,71 +536,83 @@ var SentryReplay = /** @class */ (function () {
444
536
  if (!this.session) {
445
537
  throw new Error('Invalid session');
446
538
  }
539
+ this.addListeners();
447
540
  // Tag all (non replay) events that get sent to Sentry with the current
448
541
  // replay ID so that we can reference them later in the UI
449
542
  Sentry__namespace.addGlobalEventProcessor(function (event) {
450
- // Do not apply replayId to the root transaction
451
- if (event.transaction === ROOT_REPLAY_NAME) {
543
+ // Do not apply replayId to the root event
544
+ if (event.message === ROOT_REPLAY_NAME) {
452
545
  return event;
453
546
  }
454
547
  event.tags = __assign(__assign({}, event.tags), { replayId: _this.session.id });
455
548
  return event;
456
549
  });
457
- // not fully initialized and the event will not get properly sent to Sentry
458
- this.createReplayEvent();
459
550
  rrweb.record(__assign(__assign({}, this.rrwebRecordOptions), { emit: function (event, isCheckout) {
460
- // We want to batch uploads of replay events. Save events only if
461
- // `<uploadMinDelay>` milliseconds have elapsed since the last event
462
- // *OR* if `<uploadMaxDelay>` milliseconds have elapsed.
463
- var now = new Date().getTime();
464
- // Timestamp of the first replay event since the last flush, this gets
465
- // reset when we finish the replay event
466
- if (!_this.initialEventTimestampSinceFlush) {
467
- _this.initialEventTimestampSinceFlush = now;
468
- }
469
- // Do not finish the replay event if we receive a new replay event
470
- if (_this.timeout) {
471
- window.clearTimeout(_this.timeout);
472
- }
473
551
  // If this is false, it means session is expired, create and a new session and wait for checkout
474
552
  if (!_this.checkAndHandleExpiredSession()) {
475
553
  logger.error(new Error('Received replay event after session expired.'));
476
554
  return;
477
555
  }
478
- // We need to clear existing events on a checkout, otherwise they are
479
- // incremental event updates and should be appended
480
- if (isCheckout) {
481
- _this.events = [event];
482
- }
483
- else {
484
- _this.events.push(event);
485
- }
486
- // This event type is a fullsnapshot, we should save immediately when this occurs
487
- // See https://github.com/rrweb-io/rrweb/blob/d8f9290ca496712aa1e7d472549480c4e7876594/packages/rrweb/src/types.ts#L16
488
- if (event.type === 2) {
489
- // A fullsnapshot happens on initial load and if we need to start a
490
- // new replay due to idle timeout. In the latter case, a new session *should* have been started
491
- // before triggering a new checkout
492
- _this.finishReplayEvent();
493
- return;
494
- }
495
- var uploadMaxDelayExceeded = isExpired(_this.initialEventTimestampSinceFlush, _this.options.uploadMaxDelay, now);
496
- // If `uploadMaxDelayExceeded` is true, then we should finish the replay event immediately,
497
- // Otherwise schedule it to be finished in `this.options.uploadMinDelay`
498
- if (uploadMaxDelayExceeded) {
499
- logger.log('replay max delay exceeded, finishing replay event');
500
- _this.finishReplayEvent();
501
- return;
502
- }
503
- // Set timer to finish replay event and send replay attachment to
504
- // Sentry. Will be cancelled if an event happens before `uploadMinDelay`
505
- // elapses.
506
- _this.timeout = window.setTimeout(function () {
507
- logger.log('replay timeout exceeded, finishing replay event');
508
- _this.finishReplayEvent();
509
- }, _this.options.uploadMinDelay);
556
+ _this.addUpdate(function () {
557
+ // We need to clear existing events on a checkout, otherwise they are
558
+ // incremental event updates and should be appended
559
+ if (isCheckout) {
560
+ _this.events = [event];
561
+ }
562
+ else {
563
+ _this.events.push(event);
564
+ }
565
+ // This event type is a fullsnapshot, we should save immediately when this occurs
566
+ // See https://github.com/rrweb-io/rrweb/blob/d8f9290ca496712aa1e7d472549480c4e7876594/packages/rrweb/src/types.ts#L16
567
+ if (event.type === 2) {
568
+ // A fullsnapshot happens on initial load and if we need to start a
569
+ // new replay due to idle timeout. In the latter case, a new session *should* have been started
570
+ // before triggering a new checkout
571
+ return true;
572
+ }
573
+ return false;
574
+ });
510
575
  } }));
511
- this.addListeners();
576
+ };
577
+ /**
578
+ * We want to batch uploads of replay events. Save events only if
579
+ * `<uploadMinDelay>` milliseconds have elapsed since the last event
580
+ * *OR* if `<uploadMaxDelay>` milliseconds have elapsed.
581
+ *
582
+ * Accepts a callback to perform side-effects and returns a boolean value if we
583
+ * should flush events immediately
584
+ */
585
+ SentryReplay.prototype.addUpdate = function (cb) {
586
+ var _this = this;
587
+ var now = new Date().getTime();
588
+ // Timestamp of the first replay event since the last flush, this gets
589
+ // reset when we finish the replay event
590
+ if (!this.initialEventTimestampSinceFlush) {
591
+ this.initialEventTimestampSinceFlush = now;
592
+ }
593
+ // Do not finish the replay event if we receive a new replay event
594
+ if (this.timeout) {
595
+ window.clearTimeout(this.timeout);
596
+ }
597
+ if ((cb === null || cb === void 0 ? void 0 : cb()) === true) {
598
+ this.flushUpdate();
599
+ return;
600
+ }
601
+ var uploadMaxDelayExceeded = isExpired(this.initialEventTimestampSinceFlush, this.options.uploadMaxDelay, now);
602
+ // If `uploadMaxDelayExceeded` is true, then we should finish the replay event immediately,
603
+ // Otherwise schedule it to be finished in `this.options.uploadMinDelay`
604
+ if (uploadMaxDelayExceeded) {
605
+ logger.log('replay max delay exceeded, finishing replay event');
606
+ this.flushUpdate();
607
+ return;
608
+ }
609
+ // Set timer to finish replay event and send replay attachment to
610
+ // Sentry. Will be cancelled if an event happens before `uploadMinDelay`
611
+ // elapses.
612
+ this.timeout = window.setTimeout(function () {
613
+ logger.log('replay timeout exceeded, finishing replay event');
614
+ _this.flushUpdate();
615
+ }, this.options.uploadMinDelay);
512
616
  };
513
617
  /**
514
618
  * Currently, this needs to be manually called (e.g. for tests). Sentry SDK does not support a teardown
@@ -516,6 +620,9 @@ var SentryReplay = /** @class */ (function () {
516
620
  SentryReplay.prototype.teardown = function () {
517
621
  this.removeListeners();
518
622
  };
623
+ SentryReplay.prototype.clearSession = function () {
624
+ this.session = null;
625
+ };
519
626
  /**
520
627
  * Loads a session from storage, or creates a new one
521
628
  */
@@ -524,11 +631,20 @@ var SentryReplay = /** @class */ (function () {
524
631
  this.session = getSession({
525
632
  expiry: expiry,
526
633
  stickySession: this.options.stickySession,
634
+ currentSession: this.session,
527
635
  });
528
636
  };
637
+ SentryReplay.prototype.updateSession = function (session) {
638
+ this.session = __assign(__assign({}, this.session), session);
639
+ };
529
640
  SentryReplay.prototype.addListeners = function () {
530
641
  var _this = this;
531
642
  document.addEventListener('visibilitychange', this.handleVisibilityChange);
643
+ // Listeners from core SDK //
644
+ var scope = Sentry__namespace.getCurrentHub().getScope();
645
+ scope.addScopeListener(this.handleCoreListener('scope'));
646
+ utils.addInstrumentationHandler('dom', this.handleCoreListener('dom'));
647
+ // PerformanceObserver //
532
648
  if (!('PerformanceObserver' in window)) {
533
649
  return;
534
650
  }
@@ -566,40 +682,20 @@ var SentryReplay = /** @class */ (function () {
566
682
  logger.log('Taking full rrweb snapshot');
567
683
  rrweb.record.takeFullSnapshot(true);
568
684
  };
569
- /**
570
- * This is our pseudo replay event disguised as a transaction. It will be
571
- * used to store performance entries and breadcrumbs for every incremental
572
- * replay event.
573
- **/
574
- SentryReplay.prototype.createReplayEvent = function () {
575
- var _this = this;
576
- logger.log('CreateReplayEvent rootReplayId', this.session.id);
577
- this.replayEvent = Sentry__namespace.getCurrentHub().startTransaction({
578
- name: REPLAY_EVENT_NAME,
579
- parentSpanId: this.session.spanId,
580
- traceId: this.session.traceId,
581
- tags: {
582
- replayId: this.session.id,
583
- },
584
- });
585
- Sentry__namespace.configureScope(function (scope) { return scope.setSpan(_this.replayEvent); });
586
- return this.replayEvent;
587
- };
588
685
  /**
589
686
  * Create a span for each performance entry. The parent transaction is `this.replayEvent`.
590
687
  */
591
688
  SentryReplay.prototype.createPerformanceSpans = function (entries) {
592
689
  var _this = this;
593
690
  entries.forEach(function (_a) {
594
- var _b;
595
691
  var type = _a.type, start = _a.start, end = _a.end, name = _a.name, data = _a.data;
596
- var span = (_b = _this.replayEvent) === null || _b === void 0 ? void 0 : _b.startChild({
692
+ _this.replaySpans.push({
597
693
  op: type,
598
694
  description: name,
599
695
  startTimestamp: start,
696
+ endTimestamp: end,
600
697
  data: data,
601
698
  });
602
- span.finish(end);
603
699
  });
604
700
  };
605
701
  /**
@@ -618,7 +714,6 @@ var SentryReplay = /** @class */ (function () {
618
714
  // @ts-expect-error memory doesn't exist on type Performance as the API is non-standard
619
715
  entryEvents.push(createMemoryEntry(window.performance.memory));
620
716
  }
621
- // This current implementation is to create spans on the transaction referenced in `this.replayEvent`
622
717
  this.createPerformanceSpans(entryEvents);
623
718
  };
624
719
  /**
@@ -644,8 +739,7 @@ var SentryReplay = /** @class */ (function () {
644
739
  this.triggerFullSnapshot();
645
740
  return false;
646
741
  };
647
- SentryReplay.prototype.finishReplayEvent = function () {
648
- var _a;
742
+ SentryReplay.prototype.flushUpdate = function () {
649
743
  if (!this.checkAndHandleExpiredSession()) {
650
744
  logger.error(new Error('Attempting to finish replay event after session expired.'));
651
745
  }
@@ -653,15 +747,19 @@ var SentryReplay = /** @class */ (function () {
653
747
  console.error(new Error('[Sentry]: No transaction, no replay'));
654
748
  return;
655
749
  }
750
+ // TEMP: keep sending a replay event just for the duration
751
+ Sentry.captureEvent({
752
+ message: "".concat(REPLAY_EVENT_NAME, "-").concat(utils.uuid4().substring(16)),
753
+ tags: {
754
+ replayId: this.session.id,
755
+ sequenceId: this.session.sequenceId++,
756
+ },
757
+ });
758
+ this.addPerformanceEntries();
656
759
  this.sendReplay(this.session.id);
657
760
  this.initialEventTimestampSinceFlush = null;
658
761
  // TBD: Alternatively we could update this after every rrweb event
659
762
  this.session.lastActivity = new Date().getTime();
660
- // include performance entries
661
- this.addPerformanceEntries();
662
- // Close out existing replay event and create a new one
663
- (_a = this.replayEvent) === null || _a === void 0 ? void 0 : _a.setStatus('ok').finish();
664
- this.createReplayEvent();
665
763
  };
666
764
  /**
667
765
  * Determine if there is browser support for `navigator.sendBeacon`
@@ -672,13 +770,18 @@ var SentryReplay = /** @class */ (function () {
672
770
  /**
673
771
  * Send replay attachment using either `sendBeacon()` or `fetch()`
674
772
  */
675
- SentryReplay.prototype.sendReplayRequest = function (endpoint, events) {
773
+ SentryReplay.prototype.sendReplayRequest = function (_a) {
774
+ var endpoint = _a.endpoint, events = _a.events, replaySpans = _a.replaySpans, breadcrumbs = _a.breadcrumbs;
676
775
  return __awaiter(this, void 0, void 0, function () {
677
776
  var stringifiedPayload, formData;
678
- return __generator(this, function (_a) {
679
- switch (_a.label) {
777
+ return __generator(this, function (_b) {
778
+ switch (_b.label) {
680
779
  case 0:
681
- stringifiedPayload = JSON.stringify({ events: events });
780
+ stringifiedPayload = JSON.stringify({
781
+ recording: events,
782
+ replaySpans: replaySpans,
783
+ breadcrumbs: breadcrumbs,
784
+ });
682
785
  formData = new FormData();
683
786
  formData.append('rrweb', new Blob([stringifiedPayload], {
684
787
  type: 'application/json',
@@ -696,34 +799,52 @@ var SentryReplay = /** @class */ (function () {
696
799
  body: formData,
697
800
  })];
698
801
  case 1:
699
- _a.sent();
802
+ _b.sent();
700
803
  return [2 /*return*/];
701
804
  }
702
805
  });
703
806
  });
704
807
  };
808
+ SentryReplay.prototype.resetRetries = function () {
809
+ this.retryCount = 0;
810
+ this.retryInterval = BASE_RETRY_INTERVAL;
811
+ };
705
812
  /**
706
813
  * Finalize and send the current replay event to Sentry
707
814
  */
708
815
  SentryReplay.prototype.sendReplay = function (eventId) {
709
816
  return __awaiter(this, void 0, void 0, function () {
710
- var events, client, endpoint, ex_1;
817
+ var events, replaySpans, breadcrumbs, client, endpoint, ex_1;
818
+ var _this = this;
711
819
  return __generator(this, function (_a) {
712
820
  switch (_a.label) {
713
821
  case 0:
714
- // short circuit if theres no events to replay
715
- if (!this.events.length)
822
+ // short circuit if theres no events to upload
823
+ if (!this.events.length &&
824
+ !this.replaySpans.length &&
825
+ !this.breadcrumbs.length) {
716
826
  return [2 /*return*/];
827
+ }
717
828
  events = this.events;
829
+ replaySpans = this.replaySpans;
830
+ breadcrumbs = this.breadcrumbs;
831
+ this.replaySpans = [];
832
+ this.breadcrumbs = [];
718
833
  this.events = [];
719
834
  client = Sentry__namespace.getCurrentHub().getClient();
720
835
  endpoint = SentryReplay.attachmentUrlFromDsn(client.getDsn(), eventId);
721
836
  _a.label = 1;
722
837
  case 1:
723
838
  _a.trys.push([1, 3, , 4]);
724
- return [4 /*yield*/, this.sendReplayRequest(endpoint, events)];
839
+ return [4 /*yield*/, this.sendReplayRequest({
840
+ endpoint: endpoint,
841
+ events: events,
842
+ replaySpans: replaySpans,
843
+ breadcrumbs: breadcrumbs,
844
+ })];
725
845
  case 2:
726
846
  _a.sent();
847
+ this.resetRetries();
727
848
  return [2 /*return*/, true];
728
849
  case 3:
729
850
  ex_1 = _a.sent();
@@ -731,6 +852,17 @@ var SentryReplay = /** @class */ (function () {
731
852
  console.error(ex_1);
732
853
  // If an error happened here, it's likely that uploading the attachment failed, we'll want to restore the events that failed to upload
733
854
  this.events = __spreadArray(__spreadArray([], events, true), this.events, true);
855
+ this.replaySpans = __spreadArray(__spreadArray([], replaySpans, true), this.replaySpans, true);
856
+ this.breadcrumbs = __spreadArray(__spreadArray([], replaySpans, true), this.breadcrumbs, true);
857
+ if (this.retryCount >= MAX_RETRY_COUNT) {
858
+ this.resetRetries();
859
+ }
860
+ else {
861
+ this.retryCount = this.retryCount + 1;
862
+ // will retry in intervals of 5, 10, 15, 20, 25 seconds
863
+ this.retryInterval = this.retryCount * this.retryInterval;
864
+ setTimeout(function () { return _this.sendReplay(eventId); }, this.retryInterval);
865
+ }
734
866
  return [2 /*return*/, false];
735
867
  case 4: return [2 /*return*/];
736
868
  }