@sentry/replay 0.4.8 → 0.4.9

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.
@@ -1,4 +1,4 @@
1
- import { RecordingEvent, WorkerRequest } from './types';
1
+ import { RecordingEvent, WorkerRequest, WorkerResponse } from './types';
2
2
  interface CreateEventBufferParams {
3
3
  useCompression: boolean;
4
4
  }
@@ -26,12 +26,19 @@ export declare class EventBufferCompressionWorker implements IEventBuffer {
26
26
  * Read-only incrementing counter
27
27
  */
28
28
  get id(): number;
29
- postMessage(args: WorkerRequest): void;
29
+ /**
30
+ * Post message to worker and wait for response before resolving promise.
31
+ */
32
+ postMessage({ id, method, args, }: WorkerRequest): Promise<WorkerResponse['response']>;
30
33
  init(): void;
31
34
  destroy(): void;
35
+ /**
36
+ * Note that this may not reflect what is actually in the event buffer. This
37
+ * is only a local count of the buffer size since `addEvent` is async.
38
+ */
32
39
  get length(): number;
33
- addEvent(event: RecordingEvent, isCheckout?: boolean): void;
34
- sendEventToWorker: (event: RecordingEvent) => void;
40
+ addEvent(event: RecordingEvent, isCheckout?: boolean): Promise<string | Uint8Array>;
41
+ sendEventToWorker: (event: RecordingEvent) => Promise<string | Uint8Array>;
35
42
  finishRequest: (id: number) => Promise<Uint8Array>;
36
43
  finish(): Promise<Uint8Array>;
37
44
  }
@@ -176,14 +176,19 @@ export declare class SentryReplay implements Integration {
176
176
  */
177
177
  createCustomBreadcrumb(breadcrumb: Breadcrumb): void;
178
178
  /**
179
- * Create a span for each performance entry. The parent transaction is `this.replayEvent`.
179
+ * Create a "span" for each performance entry. The parent transaction is `this.replayEvent`.
180
180
  */
181
- createPerformanceSpans(entries: ReplayPerformanceEntry[]): void;
181
+ createPerformanceSpans(entries: ReplayPerformanceEntry[]): Promise<void[]>;
182
182
  /**
183
183
  * Observed performance events are added to `this.performanceEvents`. These
184
184
  * are included in the replay event before it is finished and sent to Sentry.
185
185
  */
186
- addPerformanceEntries(): void;
186
+ addPerformanceEntries(): Promise<void[]>;
187
+ /**
188
+ * Create a "span" for the total amount of memory being used by JS objects
189
+ * (including v8 internal objects).
190
+ */
191
+ addMemoryEntry(): Promise<void[]>;
187
192
  /**
188
193
  *
189
194
  *
@@ -1,4 +1,4 @@
1
- import { RecordingEvent, WorkerRequest } from './types';
1
+ import { RecordingEvent, WorkerRequest, WorkerResponse } from './types';
2
2
  interface CreateEventBufferParams {
3
3
  useCompression: boolean;
4
4
  }
@@ -26,12 +26,19 @@ export declare class EventBufferCompressionWorker implements IEventBuffer {
26
26
  * Read-only incrementing counter
27
27
  */
28
28
  get id(): number;
29
- postMessage(args: WorkerRequest): void;
29
+ /**
30
+ * Post message to worker and wait for response before resolving promise.
31
+ */
32
+ postMessage({ id, method, args, }: WorkerRequest): Promise<WorkerResponse['response']>;
30
33
  init(): void;
31
34
  destroy(): void;
35
+ /**
36
+ * Note that this may not reflect what is actually in the event buffer. This
37
+ * is only a local count of the buffer size since `addEvent` is async.
38
+ */
32
39
  get length(): number;
33
- addEvent(event: RecordingEvent, isCheckout?: boolean): void;
34
- sendEventToWorker: (event: RecordingEvent) => void;
40
+ addEvent(event: RecordingEvent, isCheckout?: boolean): Promise<string | Uint8Array>;
41
+ sendEventToWorker: (event: RecordingEvent) => Promise<string | Uint8Array>;
35
42
  finishRequest: (id: number) => Promise<Uint8Array>;
36
43
  finish(): Promise<Uint8Array>;
37
44
  }
@@ -176,14 +176,19 @@ export declare class SentryReplay implements Integration {
176
176
  */
177
177
  createCustomBreadcrumb(breadcrumb: Breadcrumb): void;
178
178
  /**
179
- * Create a span for each performance entry. The parent transaction is `this.replayEvent`.
179
+ * Create a "span" for each performance entry. The parent transaction is `this.replayEvent`.
180
180
  */
181
- createPerformanceSpans(entries: ReplayPerformanceEntry[]): void;
181
+ createPerformanceSpans(entries: ReplayPerformanceEntry[]): Promise<void[]>;
182
182
  /**
183
183
  * Observed performance events are added to `this.performanceEvents`. These
184
184
  * are included in the replay event before it is finished and sent to Sentry.
185
185
  */
186
- addPerformanceEntries(): void;
186
+ addPerformanceEntries(): Promise<void[]>;
187
+ /**
188
+ * Create a "span" for the total amount of memory being used by JS objects
189
+ * (including v8 internal objects).
190
+ */
191
+ addMemoryEntry(): Promise<void[]>;
187
192
  /**
188
193
  *
189
194
  *
package/dist/index.es.js CHANGED
@@ -638,38 +638,25 @@ var EventBufferCompressionWorker = /** @class */ (function () {
638
638
  this.eventBufferItemLength = 0;
639
639
  this._id = 0;
640
640
  this.sendEventToWorker = function (event) {
641
- _this.postMessage({
641
+ var promise = _this.postMessage({
642
642
  id: _this.id,
643
643
  method: 'addEvent',
644
644
  args: [event],
645
645
  });
646
+ logger.log('Message posted to worker');
647
+ // XXX: See note in `get length()`
646
648
  _this.eventBufferItemLength++;
649
+ return promise;
647
650
  };
648
- this.finishRequest = function (id) {
649
- return new Promise(function (resolve, reject) {
650
- var finishListener = function (_a) {
651
- var data = _a.data;
652
- if (data.method !== 'finish') {
653
- return;
654
- }
655
- if (data.id !== id) {
656
- return;
657
- }
658
- if (!data.success) {
659
- // TODO: Do some error handling, not sure what
660
- logger.error(data.response);
661
- reject(new Error('Error in compression worker'));
662
- return;
663
- }
664
- resolve(data.response);
665
- logger.log('Worker responded with compressed payload');
666
- _this.eventBufferItemLength = 0;
667
- _this.worker.removeEventListener('message', finishListener);
668
- };
669
- _this.worker.addEventListener('message', finishListener);
670
- _this.postMessage({ id: id, method: 'finish', args: [] });
651
+ this.finishRequest = function (id) { return __awaiter(_this, void 0, void 0, function () {
652
+ var promise;
653
+ return __generator(this, function (_a) {
654
+ promise = this.postMessage({ id: id, method: 'finish', args: [] });
655
+ // XXX: See note in `get length()`
656
+ this.eventBufferItemLength = 0;
657
+ return [2 /*return*/, promise];
671
658
  });
672
- };
659
+ }); };
673
660
  this.worker = worker;
674
661
  }
675
662
  Object.defineProperty(EventBufferCompressionWorker.prototype, "id", {
@@ -682,8 +669,38 @@ var EventBufferCompressionWorker = /** @class */ (function () {
682
669
  enumerable: false,
683
670
  configurable: true
684
671
  });
685
- EventBufferCompressionWorker.prototype.postMessage = function (args) {
686
- this.worker.postMessage(args);
672
+ /**
673
+ * Post message to worker and wait for response before resolving promise.
674
+ */
675
+ EventBufferCompressionWorker.prototype.postMessage = function (_a) {
676
+ var _this = this;
677
+ var id = _a.id, method = _a.method, args = _a.args;
678
+ return new Promise(function (resolve, reject) {
679
+ var listener = function (_a) {
680
+ var data = _a.data;
681
+ if (data.method !== method) {
682
+ return;
683
+ }
684
+ // There can be multiple listeners for a single method, the id ensures
685
+ // that the response matches the caller.
686
+ if (data.id !== id) {
687
+ return;
688
+ }
689
+ // At this point, we'll always want to remove listener regardless of result status
690
+ _this.worker.removeEventListener('message', listener);
691
+ if (!data.success) {
692
+ // TODO: Do some error handling, not sure what
693
+ logger.error(data.response);
694
+ reject(new Error('Error in compression worker'));
695
+ return;
696
+ }
697
+ resolve(data.response);
698
+ };
699
+ // Note: we can't use `once` option because it's possible it needs to
700
+ // listen to multiple messages
701
+ _this.worker.addEventListener('message', listener);
702
+ _this.worker.postMessage({ id: id, method: method, args: args });
703
+ });
687
704
  };
688
705
  EventBufferCompressionWorker.prototype.init = function () {
689
706
  this.postMessage({ id: this.id, method: 'init', args: [] });
@@ -695,6 +712,10 @@ var EventBufferCompressionWorker = /** @class */ (function () {
695
712
  this.worker = null;
696
713
  };
697
714
  Object.defineProperty(EventBufferCompressionWorker.prototype, "length", {
715
+ /**
716
+ * Note that this may not reflect what is actually in the event buffer. This
717
+ * is only a local count of the buffer size since `addEvent` is async.
718
+ */
698
719
  get: function () {
699
720
  return this.eventBufferItemLength;
700
721
  },
@@ -702,39 +723,31 @@ var EventBufferCompressionWorker = /** @class */ (function () {
702
723
  configurable: true
703
724
  });
704
725
  EventBufferCompressionWorker.prototype.addEvent = function (event, isCheckout) {
705
- var _this = this;
706
- // If it is a checkout we should make sure worker buffer is cleared before proceeding
707
- if (!isCheckout) {
708
- this.sendEventToWorker(event);
709
- return;
710
- }
711
- var initListener = function (_a) {
712
- var data = _a.data;
713
- if (data.method !== 'init') {
714
- return;
715
- }
716
- if (!data.success) {
717
- // TODO: Do some error handling, not sure what
718
- logger.error(data.response);
719
- return;
720
- }
721
- // Worker has been re-initialized, can add event now
722
- _this.sendEventToWorker(event);
723
- _this.worker.removeEventListener('message', initListener);
724
- };
725
- this.worker.addEventListener('message', initListener);
726
- this.postMessage({ id: this.id, method: 'init', args: [] });
727
- };
728
- EventBufferCompressionWorker.prototype.finish = function () {
729
726
  return __awaiter(this, void 0, void 0, function () {
730
727
  return __generator(this, function (_a) {
731
728
  switch (_a.label) {
732
- case 0: return [4 /*yield*/, this.finishRequest(this.id)];
733
- case 1: return [2 /*return*/, _a.sent()];
729
+ case 0:
730
+ if (!isCheckout) return [3 /*break*/, 2];
731
+ // This event is a checkout, make sure worker buffer is cleared before
732
+ // proceeding.
733
+ return [4 /*yield*/, this.postMessage({
734
+ id: this.id,
735
+ method: 'init',
736
+ args: [],
737
+ })];
738
+ case 1:
739
+ // This event is a checkout, make sure worker buffer is cleared before
740
+ // proceeding.
741
+ _a.sent();
742
+ _a.label = 2;
743
+ case 2: return [2 /*return*/, this.sendEventToWorker(event)];
734
744
  }
735
745
  });
736
746
  });
737
747
  };
748
+ EventBufferCompressionWorker.prototype.finish = function () {
749
+ return this.finishRequest(this.id);
750
+ };
738
751
  return EventBufferCompressionWorker;
739
752
  }());
740
753
 
@@ -790,6 +803,9 @@ var SentryReplay = /** @class */ (function () {
790
803
  // Do not apply replayId to the root event
791
804
  if (event.message === ROOT_REPLAY_NAME ||
792
805
  ((_a = event.message) === null || _a === void 0 ? void 0 : _a.startsWith(REPLAY_EVENT_NAME))) {
806
+ // Replays have separate set of breadcrumbs, do not include breadcrumbs
807
+ // from core SDK
808
+ delete event.breadcrumbs;
793
809
  return event;
794
810
  }
795
811
  event.tags = __assign(__assign({}, event.tags), { replayId: _this.session.id });
@@ -1219,13 +1235,13 @@ var SentryReplay = /** @class */ (function () {
1219
1235
  });
1220
1236
  };
1221
1237
  /**
1222
- * Create a span for each performance entry. The parent transaction is `this.replayEvent`.
1238
+ * Create a "span" for each performance entry. The parent transaction is `this.replayEvent`.
1223
1239
  */
1224
1240
  SentryReplay.prototype.createPerformanceSpans = function (entries) {
1225
1241
  var _this = this;
1226
- entries.forEach(function (_a) {
1242
+ return Promise.all(entries.map(function (_a) {
1227
1243
  var type = _a.type, start = _a.start, end = _a.end, name = _a.name, data = _a.data;
1228
- _this.eventBuffer.addEvent({
1244
+ return _this.eventBuffer.addEvent({
1229
1245
  type: EventType.Custom,
1230
1246
  timestamp: start,
1231
1247
  data: {
@@ -1239,7 +1255,7 @@ var SentryReplay = /** @class */ (function () {
1239
1255
  },
1240
1256
  },
1241
1257
  });
1242
- });
1258
+ }));
1243
1259
  };
1244
1260
  /**
1245
1261
  * Observed performance events are added to `this.performanceEvents`. These
@@ -1249,15 +1265,22 @@ var SentryReplay = /** @class */ (function () {
1249
1265
  // Copy and reset entries before processing
1250
1266
  var entries = __spreadArray([], this.performanceEvents, true);
1251
1267
  this.performanceEvents = [];
1252
- // Parse the entries
1253
- var entryEvents = createPerformanceEntries(entries);
1268
+ return this.createPerformanceSpans(createPerformanceEntries(entries));
1269
+ };
1270
+ /**
1271
+ * Create a "span" for the total amount of memory being used by JS objects
1272
+ * (including v8 internal objects).
1273
+ */
1274
+ SentryReplay.prototype.addMemoryEntry = function () {
1254
1275
  // window.performance.memory is a non-standard API and doesn't work on all browsers
1255
1276
  // so we check before creating the event.
1256
- if ('memory' in window.performance) {
1257
- // @ts-expect-error memory doesn't exist on type Performance as the API is non-standard
1258
- entryEvents.push(createMemoryEntry(window.performance.memory));
1277
+ if (!('memory' in window.performance)) {
1278
+ return;
1259
1279
  }
1260
- this.createPerformanceSpans(entryEvents);
1280
+ return this.createPerformanceSpans([
1281
+ // @ts-expect-error memory doesn't exist on type Performance as the API is non-standard (we check that it exists above)
1282
+ createMemoryEntry(window.performance.memory),
1283
+ ]);
1261
1284
  };
1262
1285
  /**
1263
1286
  *
@@ -1329,10 +1352,19 @@ var SentryReplay = /** @class */ (function () {
1329
1352
  console.error(new Error('[Sentry]: No transaction, no replay'));
1330
1353
  return [2 /*return*/];
1331
1354
  }
1332
- this.addPerformanceEntries();
1355
+ // Since already flushing, ensure other queued flushes are cancelled
1356
+ clearTimeout(this.timeout);
1357
+ return [4 /*yield*/, this.addPerformanceEntries()];
1358
+ case 1:
1359
+ _a.sent();
1333
1360
  if (!this.eventBuffer.length) {
1334
1361
  return [2 /*return*/];
1335
1362
  }
1363
+ // Only attach memory event if eventBuffer is not empty
1364
+ return [4 /*yield*/, this.addMemoryEntry()];
1365
+ case 2:
1366
+ // Only attach memory event if eventBuffer is not empty
1367
+ _a.sent();
1336
1368
  timestamp = lastActivity !== null && lastActivity !== void 0 ? lastActivity : new Date().getTime();
1337
1369
  // Only want to create replay event if session is new
1338
1370
  if (this.needsCaptureReplay) {
@@ -1343,14 +1375,14 @@ var SentryReplay = /** @class */ (function () {
1343
1375
  // Reset this to null regardless of `sendReplay` result so that future
1344
1376
  // events will get flushed properly
1345
1377
  this.initialEventTimestampSinceFlush = null;
1346
- _a.label = 1;
1347
- case 1:
1348
- _a.trys.push([1, 4, , 5]);
1378
+ _a.label = 3;
1379
+ case 3:
1380
+ _a.trys.push([3, 6, , 7]);
1349
1381
  return [4 /*yield*/, this.eventBuffer.finish()];
1350
- case 2:
1382
+ case 4:
1351
1383
  recordingData = _a.sent();
1352
1384
  return [4 /*yield*/, this.sendReplay(this.session.id, recordingData)];
1353
- case 3:
1385
+ case 5:
1354
1386
  _a.sent();
1355
1387
  // The below will only happen after successfully sending replay //
1356
1388
  // TBD: Alternatively we could update this after every rrweb event
@@ -1359,12 +1391,13 @@ var SentryReplay = /** @class */ (function () {
1359
1391
  // occurs.
1360
1392
  this.updateLastActivity(timestamp);
1361
1393
  captureReplayUpdate(this.popEventContext({ timestamp: timestamp }));
1362
- return [3 /*break*/, 5];
1363
- case 4:
1394
+ return [3 /*break*/, 7];
1395
+ case 6:
1364
1396
  err_1 = _a.sent();
1397
+ captureException(err_1);
1365
1398
  console.error(err_1);
1366
- return [3 /*break*/, 5];
1367
- case 5: return [2 /*return*/];
1399
+ return [3 /*break*/, 7];
1400
+ case 7: return [2 /*return*/];
1368
1401
  }
1369
1402
  });
1370
1403
  });