@glimt/record 0.0.80 → 0.0.82

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.
@@ -1783,12 +1783,7 @@ function slimDOMExcluded(sn, slimDOMOptions) {
1783
1783
  }
1784
1784
  return false;
1785
1785
  }
1786
- const debugging = {
1787
- index: 0,
1788
- store: {}
1789
- };
1790
1786
  function serializeNodeWithId(n2, options) {
1791
- const start = Date.now();
1792
1787
  const {
1793
1788
  doc,
1794
1789
  mirror: mirror2,
@@ -2018,16 +2013,6 @@ function serializeNodeWithId(n2, options) {
2018
2013
  stylesheetLoadTimeout
2019
2014
  );
2020
2015
  }
2021
- debugging.index++;
2022
- const took = Date.now() - start;
2023
- if (!(n2.nodeName in debugging.store)) {
2024
- debugging.store[n2.nodeName] = [];
2025
- }
2026
- debugging.store[n2.nodeName].push(took);
2027
- if (debugging.index % 5e3 === 0) {
2028
- debugging.index = 0;
2029
- debugging.store = {};
2030
- }
2031
2016
  return serializedNode;
2032
2017
  }
2033
2018
  function snapshot(n2, options) {
@@ -10124,6 +10109,11 @@ class IframeManager {
10124
10109
  window.addEventListener("message", this.handleMessage.bind(this));
10125
10110
  }
10126
10111
  }
10112
+ destroy() {
10113
+ this.iframes = /* @__PURE__ */ new WeakMap();
10114
+ this.crossOriginIframeMap = /* @__PURE__ */ new WeakMap();
10115
+ this.crossOriginIframeRootIdMap = /* @__PURE__ */ new WeakMap();
10116
+ }
10127
10117
  addIframe(iframeEl) {
10128
10118
  this.iframes.set(iframeEl, true);
10129
10119
  if (iframeEl.contentWindow)
@@ -10335,187 +10325,45 @@ const _ObserveManager = class _ObserveManager2 {
10335
10325
  constructor() {
10336
10326
  __publicField(this, "docsObservers", /* @__PURE__ */ new WeakSet());
10337
10327
  __publicField(this, "shadowRootsObservers", /* @__PURE__ */ new WeakSet());
10338
- __publicField(this, "docsDebounceTimers", /* @__PURE__ */ new WeakMap());
10339
- __publicField(this, "shadowRootsDebounceTimers", /* @__PURE__ */ new WeakMap());
10340
- __publicField(this, "debounceTime", 15);
10341
- __publicField(this, "mutationOptions");
10342
- __publicField(this, "emitter", null);
10343
- __publicField(this, "isSnapshottingShadowRoots", false);
10344
- __publicField(this, "isSnapshottingDocs", false);
10328
+ __publicField(this, "docObservers", /* @__PURE__ */ new WeakMap());
10345
10329
  if (_ObserveManager2.instance) {
10346
10330
  return _ObserveManager2.instance;
10347
10331
  }
10348
10332
  _ObserveManager2.instance = this;
10349
10333
  }
10350
- setEmitter(emitter) {
10351
- this.emitter = emitter;
10352
- }
10353
- setMutationOptions(options) {
10354
- this.mutationOptions = options;
10355
- }
10356
- get usable() {
10357
- const result2 = this.emitter != null;
10358
- if (!result2) debugLog("observerManager: emitter is null");
10359
- return result2;
10360
- }
10361
- serializeDoc(doc) {
10362
- var _a2, _b2, _c;
10363
- if (!((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return null;
10364
- const serialized = serializeNodeWithId(doc, __spreadProps(__spreadValues({}, this.mutationOptions), {
10365
- doc,
10366
- skipChild: false,
10367
- maskTextFn: (_b2 = this.mutationOptions) == null ? void 0 : _b2.maskTextFn,
10368
- maskInputFn: (_c = this.mutationOptions) == null ? void 0 : _c.maskInputFn
10369
- }));
10370
- if (!serialized) {
10371
- debugLog("snapshotDoc: no serialized node");
10372
- return null;
10334
+ observeIframe(doc, init, cleanup) {
10335
+ const current = this.docObservers.get(doc);
10336
+ if (current) {
10337
+ debugLog(
10338
+ doc,
10339
+ "iframe is already observed, cleaning up and registering again"
10340
+ );
10341
+ current();
10373
10342
  }
10374
- return serialized;
10375
- }
10376
- emitDoc(serialized) {
10377
- if (!this.emitter || !serialized) return;
10378
- this.emitter({
10379
- type: EventType.FullSnapshot,
10380
- data: {
10381
- node: serialized,
10382
- initialOffset: {
10383
- left: 0,
10384
- top: 0
10385
- }
10386
- }
10343
+ const id = makeid();
10344
+ init(id);
10345
+ this.docObservers.set(doc, () => {
10346
+ cleanup(id);
10387
10347
  });
10388
10348
  }
10389
- emitShadowRoot(serialized, shadowRoot2) {
10390
- var _a2;
10391
- if (!this.emitter || !serialized || !((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return;
10392
- const hostId = this.mutationOptions.mirror.getId(shadowRoot2.host);
10393
- const shadowId = this.mutationOptions.mirror.getId(shadowRoot2);
10394
- this.emitter({
10395
- type: EventType.IncrementalSnapshot,
10396
- data: {
10397
- source: IncrementalSource.Mutation,
10398
- adds: [
10399
- {
10400
- parentId: hostId,
10401
- nextId: null,
10402
- node: serialized
10403
- }
10404
- ],
10405
- removes: [
10406
- {
10407
- id: shadowId,
10408
- parentId: hostId
10409
- }
10410
- ],
10411
- attributes: [],
10412
- texts: []
10413
- }
10414
- });
10415
- }
10416
- serializeAndEmitDoc(doc) {
10417
- debugLog(
10418
- "onDocObserver: doc already observed, emitting full snapshot for doc",
10419
- doc
10420
- );
10421
- this.isSnapshottingDocs = true;
10422
- const serialized = this.serializeDoc(doc);
10423
- this.emitDoc(serialized);
10424
- this.isSnapshottingDocs = false;
10425
- }
10426
- serializeAndEmitShadowRoot(shadowRoot2) {
10427
- debugLog(
10428
- "onShadowRootObserver: shadowRoot already observed, emitting full snapshot for shadowRoot",
10429
- shadowRoot2
10430
- );
10431
- this.isSnapshottingShadowRoots = true;
10432
- const serialized = this.serializeDoc(shadowRoot2.ownerDocument);
10433
- this.emitShadowRoot(serialized, shadowRoot2);
10434
- this.isSnapshottingShadowRoots = false;
10435
- }
10436
- debounceEmitDoc(doc) {
10437
- const existingTimer = this.docsDebounceTimers.get(doc);
10438
- if (existingTimer) clearTimeout(existingTimer);
10439
- this.docsDebounceTimers.set(
10440
- doc,
10441
- setTimeout(() => {
10442
- this.docsDebounceTimers.delete(doc);
10443
- this.serializeAndEmitDoc(doc);
10444
- }, this.debounceTime)
10445
- );
10446
- }
10447
- debounceEmitShadowRoot(shadowRoot2) {
10448
- const existingTimer = this.shadowRootsDebounceTimers.get(shadowRoot2);
10449
- if (existingTimer) clearTimeout(existingTimer);
10450
- this.shadowRootsDebounceTimers.set(
10451
- shadowRoot2,
10452
- setTimeout(() => {
10453
- this.shadowRootsDebounceTimers.delete(shadowRoot2);
10454
- this.serializeAndEmitShadowRoot(shadowRoot2);
10455
- }, this.debounceTime)
10456
- );
10457
- }
10458
- onDocObserver(doc) {
10459
- if (this.isSnapshottingDocs) return false;
10460
- if (!this.usable) return false;
10349
+ canRegisterDocObserver(doc) {
10461
10350
  if (!this.docsObservers.has(doc)) {
10462
10351
  this.docsObservers.add(doc);
10463
10352
  return true;
10464
10353
  }
10465
- debugLog(
10466
- "onDocObserver: doc already observed, debouncing full snapshot for doc",
10467
- doc
10468
- );
10469
- this.debounceEmitDoc(doc);
10470
10354
  return false;
10471
10355
  }
10472
- onShadowRootObserver(shadowRoot2) {
10473
- if (this.isSnapshottingShadowRoots) return false;
10474
- if (!this.usable) return false;
10356
+ canRegisterShadowRootObserver(shadowRoot2) {
10475
10357
  if (!this.shadowRootsObservers.has(shadowRoot2)) {
10476
10358
  this.shadowRootsObservers.add(shadowRoot2);
10477
10359
  return true;
10478
10360
  }
10479
- debugLog(
10480
- "onShadowRootObserver: shadowRoot already observed, debouncing full snapshot for shadowRoot",
10481
- shadowRoot2
10482
- );
10483
- this.debounceEmitShadowRoot(shadowRoot2);
10484
10361
  return false;
10485
10362
  }
10486
- // canObserveDoc(doc: Document) {
10487
- // if (!this.usable) return false;
10488
- // const hasObserver = this.docsObservers.has(doc);
10489
- // if (!hasObserver) return true;
10490
- // return false;
10491
- // }
10492
- // canObserveShadowRoot(shadowRoot: ShadowRoot) {
10493
- // if (!this.usable) return false;
10494
- // return !this.shadowRootsObserved.has(shadowRoot);
10495
- // }
10496
- // observerAttached(doc: Document, onCleanup: VoidFunction) {
10497
- // if (!this.usable) return;
10498
- // debugLog('[doc] attaching observer to doc', doc);
10499
- // if (this.docsObservers.has(doc)) {
10500
- // debugLog('[doc] detected existing observer, cleaning up old observer');
10501
- // const cleanupFn = this.docsObservers.get(doc);
10502
- // cleanupFn?.();
10503
- // }
10504
- // this.docsObservers.set(doc, onCleanup);
10505
- // }
10506
- // observerAttachedToShadow(shadowRoot: ShadowRoot, onCleanup: VoidFunction) {
10507
- // if (!this.usable) return;
10508
- // debugLog('[shadow] attaching observer to shadowRoot', shadowRoot);
10509
- // if (this.shadowRootsObserved.has(shadowRoot)) {
10510
- // debugLog('[shadow] detected existing observer, cleaning up old observer');
10511
- // const cleanupFn = this.shadowRootsObserved.get(shadowRoot);
10512
- // cleanupFn?.();
10513
- // }
10514
- // this.shadowRootsObserved.set(shadowRoot, onCleanup);
10515
- // }
10516
10363
  destroy() {
10517
10364
  this.docsObservers = /* @__PURE__ */ new WeakSet();
10518
10365
  this.shadowRootsObservers = /* @__PURE__ */ new WeakSet();
10366
+ this.docObservers = /* @__PURE__ */ new WeakMap();
10519
10367
  }
10520
10368
  };
10521
10369
  __publicField(_ObserveManager, "instance");
@@ -10527,7 +10375,7 @@ const _MutationRateLimiter = class _MutationRateLimiter2 {
10527
10375
  __publicField(this, "exitMutTracker");
10528
10376
  __publicField(this, "interval", 50);
10529
10377
  __publicField(this, "exitInterval", 100);
10530
- __publicField(this, "mutThreshold", 100);
10378
+ __publicField(this, "mutThreshold", 250);
10531
10379
  __publicField(this, "inGlobalStorm", false);
10532
10380
  __publicField(this, "currentStormStartedAt", -1);
10533
10381
  __publicField(this, "stormTimeLimit", 5e3);
@@ -13057,7 +12905,6 @@ class ShadowDomManager {
13057
12905
  __publicField(this, "bypassOptions");
13058
12906
  __publicField(this, "mirror");
13059
12907
  __publicField(this, "restoreHandlers", []);
13060
- __publicField(this, "mappedRestoreHandlers", {});
13061
12908
  this.mutationCb = options.mutationCb;
13062
12909
  this.scrollCb = options.scrollCb;
13063
12910
  this.bypassOptions = options.bypassOptions;
@@ -13070,10 +12917,15 @@ class ShadowDomManager {
13070
12917
  }
13071
12918
  addShadowRoot(shadowRoot2, doc) {
13072
12919
  if (!isNativeShadowDom(shadowRoot2)) return;
13073
- if (!observeManager.onShadowRootObserver(shadowRoot2)) return;
12920
+ if (!observeManager.canRegisterShadowRootObserver(shadowRoot2)) return;
13074
12921
  if (this.shadowDoms.has(shadowRoot2)) return;
13075
12922
  this.shadowDoms.add(shadowRoot2);
13076
- debugLog(`Adding mutation observer for shadowRoot ${shadowRoot2.host}`);
12923
+ debugLog(
12924
+ `Adding mutation observer for shadowRoot`,
12925
+ shadowRoot2.host,
12926
+ "restoreHandlers",
12927
+ this.restoreHandlers.length
12928
+ );
13077
12929
  const observer = initMutationObserver(
13078
12930
  __spreadProps(__spreadValues({}, this.bypassOptions), {
13079
12931
  doc,
@@ -13148,15 +13000,6 @@ class ShadowDomManager {
13148
13000
  } catch (e2) {
13149
13001
  }
13150
13002
  });
13151
- Object.values(this.mappedRestoreHandlers).forEach((handlers) => {
13152
- for (const handler of handlers) {
13153
- try {
13154
- handler();
13155
- } catch (e2) {
13156
- }
13157
- }
13158
- });
13159
- this.mappedRestoreHandlers = {};
13160
13003
  this.restoreHandlers = [];
13161
13004
  this.shadowDoms = /* @__PURE__ */ new WeakSet();
13162
13005
  }
@@ -13342,10 +13185,11 @@ function record(options = {}) {
13342
13185
  }
13343
13186
  return e2;
13344
13187
  };
13345
- wrappedEmit = (r2, isCheckout) => {
13188
+ wrappedEmit = (r2, isCheckout, customOpts) => {
13189
+ var _a3;
13346
13190
  var _a2;
13347
13191
  const e2 = r2;
13348
- e2.timestamp = nowTimestamp();
13192
+ e2.timestamp = (_a3 = customOpts == null ? void 0 : customOpts.overrideTimestamp) != null ? _a3 : nowTimestamp();
13349
13193
  if (((_a2 = mutationBuffers[0]) == null ? void 0 : _a2.isFrozen()) && e2.type !== EventType.FullSnapshot && !(e2.type === EventType.IncrementalSnapshot && e2.data.source === IncrementalSource.Mutation)) {
13350
13194
  mutationBuffers.forEach((buf) => buf.unfreeze());
13351
13195
  }
@@ -13456,29 +13300,6 @@ function record(options = {}) {
13456
13300
  },
13457
13301
  mirror
13458
13302
  });
13459
- observeManager.setEmitter(wrappedEmit);
13460
- observeManager.setMutationOptions({
13461
- blockClass,
13462
- blockSelector,
13463
- maskTextClass,
13464
- maskTextSelector,
13465
- inlineStylesheet,
13466
- maskInputOptions,
13467
- maskTextFn,
13468
- maskInputFn,
13469
- slimDOMOptions,
13470
- dataURLOptions,
13471
- canvasManager,
13472
- stylesheetManager,
13473
- shadowDomManager,
13474
- recordCanvas,
13475
- inlineImages,
13476
- mirror,
13477
- iframeManager,
13478
- keepIframeSrcFn,
13479
- mutationCb: wrappedMutationEmit,
13480
- processedNodeManager
13481
- });
13482
13303
  takeFullSnapshot$1 = (isCheckout = false) => {
13483
13304
  if (!recordDOM) {
13484
13305
  return;
@@ -13554,7 +13375,6 @@ function record(options = {}) {
13554
13375
  };
13555
13376
  try {
13556
13377
  const handlers = [];
13557
- const registeredHandlers = {};
13558
13378
  const observe = (doc) => {
13559
13379
  var _a2;
13560
13380
  return callbackWrapper(initObservers)(
@@ -13669,11 +13489,31 @@ function record(options = {}) {
13669
13489
  hooks
13670
13490
  );
13671
13491
  };
13492
+ const registeredHandlers = {};
13672
13493
  iframeManager.addLoadListener((iframeEl) => {
13673
13494
  try {
13674
- if (!observeManager.onDocObserver(iframeEl.contentDocument)) return;
13675
- debugLog("Adding mutation observer for iframe", iframeEl);
13676
- handlers.push(observe(iframeEl.contentDocument));
13495
+ observeManager.observeIframe(
13496
+ iframeEl.contentDocument,
13497
+ (id) => {
13498
+ const cleanup = observe(iframeEl.contentDocument);
13499
+ registeredHandlers[id] = cleanup;
13500
+ debugLog(
13501
+ iframeEl.contentDocument,
13502
+ "did register handler on iframe. current registered handlers",
13503
+ Object.keys(registeredHandlers).length
13504
+ );
13505
+ },
13506
+ (id) => {
13507
+ const handler = registeredHandlers[id];
13508
+ if (handler) handler();
13509
+ delete registeredHandlers[id];
13510
+ debugLog(
13511
+ iframeEl.contentDocument,
13512
+ "did cleanup for regisited handler on iframe. current registered handlers",
13513
+ Object.keys(registeredHandlers).length
13514
+ );
13515
+ }
13516
+ );
13677
13517
  } catch (error) {
13678
13518
  if (isDebug()) {
13679
13519
  console.warn("internal error");
@@ -13734,6 +13574,7 @@ function record(options = {}) {
13734
13574
  handlers.forEach((h) => h());
13735
13575
  Object.values(registeredHandlers).forEach((h) => h());
13736
13576
  processedNodeManager.destroy();
13577
+ iframeManager.destroy();
13737
13578
  observeManager.destroy();
13738
13579
  recording = false;
13739
13580
  unregisterErrorHandler();