@glimt/record 0.0.79 → 0.0.81

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,154 +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, "mutationOptions");
10339
- __publicField(this, "emitter", null);
10340
- __publicField(this, "isSnapshottingShadowRoots", false);
10341
- __publicField(this, "isSnapshottingDocs", false);
10328
+ __publicField(this, "docObservers", /* @__PURE__ */ new WeakMap());
10342
10329
  if (_ObserveManager2.instance) {
10343
10330
  return _ObserveManager2.instance;
10344
10331
  }
10345
10332
  _ObserveManager2.instance = this;
10346
10333
  }
10347
- setEmitter(emitter) {
10348
- this.emitter = emitter;
10349
- }
10350
- setMutationOptions(options) {
10351
- this.mutationOptions = options;
10352
- }
10353
- get usable() {
10354
- const result2 = this.emitter != null;
10355
- if (!result2) debugLog("observerManager: emitter is null");
10356
- return result2;
10357
- }
10358
- serializeDoc(doc) {
10359
- var _a2, _b2, _c;
10360
- if (!((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return null;
10361
- const serialized = serializeNodeWithId(doc, __spreadProps(__spreadValues({}, this.mutationOptions), {
10362
- doc,
10363
- skipChild: false,
10364
- maskTextFn: (_b2 = this.mutationOptions) == null ? void 0 : _b2.maskTextFn,
10365
- maskInputFn: (_c = this.mutationOptions) == null ? void 0 : _c.maskInputFn
10366
- }));
10367
- if (!serialized) {
10368
- debugLog("snapshotDoc: no serialized node");
10369
- 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();
10370
10342
  }
10371
- return serialized;
10372
- }
10373
- emitDoc(serialized) {
10374
- if (!this.emitter || !serialized) return;
10375
- this.emitter({
10376
- type: EventType.FullSnapshot,
10377
- data: {
10378
- node: serialized,
10379
- initialOffset: {
10380
- left: 0,
10381
- top: 0
10382
- }
10383
- }
10384
- });
10385
- }
10386
- emitShadowRoot(serialized, shadowRoot2) {
10387
- var _a2;
10388
- if (!this.emitter || !serialized || !((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return;
10389
- const hostId = this.mutationOptions.mirror.getId(shadowRoot2.host);
10390
- const shadowId = this.mutationOptions.mirror.getId(shadowRoot2);
10391
- this.emitter({
10392
- type: EventType.IncrementalSnapshot,
10393
- data: {
10394
- source: IncrementalSource.Mutation,
10395
- adds: [
10396
- {
10397
- parentId: hostId,
10398
- nextId: null,
10399
- node: serialized
10400
- }
10401
- ],
10402
- removes: [
10403
- {
10404
- id: shadowId,
10405
- parentId: hostId
10406
- }
10407
- ],
10408
- attributes: [],
10409
- texts: []
10410
- }
10343
+ const id = makeid();
10344
+ init(id);
10345
+ this.docObservers.set(doc, () => {
10346
+ cleanup(id);
10411
10347
  });
10412
10348
  }
10413
- serializeAndEmitDoc(doc) {
10414
- const serialized = this.serializeDoc(doc);
10415
- this.emitDoc(serialized);
10416
- }
10417
- serializeAndEmitShadowRoot(shadowRoot2) {
10418
- const serialized = this.serializeDoc(shadowRoot2.ownerDocument);
10419
- this.emitShadowRoot(serialized, shadowRoot2);
10420
- }
10421
- onDocObserver(doc) {
10422
- if (this.isSnapshottingDocs) return false;
10423
- if (!this.usable) return false;
10349
+ canRegisterDocObserver(doc) {
10424
10350
  if (!this.docsObservers.has(doc)) {
10425
10351
  this.docsObservers.add(doc);
10426
10352
  return true;
10427
10353
  }
10428
- debugLog(
10429
- "onDocObserver: doc already observed, emitting full snapshot for doc",
10430
- doc
10431
- );
10432
- this.isSnapshottingDocs = true;
10433
- this.serializeAndEmitDoc(doc);
10434
- this.isSnapshottingDocs = false;
10435
10354
  return false;
10436
10355
  }
10437
- onShadowRootObserver(shadowRoot2) {
10438
- if (this.isSnapshottingShadowRoots) return false;
10439
- if (!this.usable) return false;
10356
+ canRegisterShadowRootObserver(shadowRoot2) {
10440
10357
  if (!this.shadowRootsObservers.has(shadowRoot2)) {
10441
10358
  this.shadowRootsObservers.add(shadowRoot2);
10442
10359
  return true;
10443
10360
  }
10444
- debugLog(
10445
- "onShadowRootObserver: shadowRoot already observed, emitting full snapshot for shadowRoot",
10446
- shadowRoot2
10447
- );
10448
- this.isSnapshottingShadowRoots = true;
10449
- this.serializeAndEmitShadowRoot(shadowRoot2);
10450
- this.isSnapshottingShadowRoots = false;
10451
10361
  return false;
10452
10362
  }
10453
- // canObserveDoc(doc: Document) {
10454
- // if (!this.usable) return false;
10455
- // const hasObserver = this.docsObservers.has(doc);
10456
- // if (!hasObserver) return true;
10457
- // return false;
10458
- // }
10459
- // canObserveShadowRoot(shadowRoot: ShadowRoot) {
10460
- // if (!this.usable) return false;
10461
- // return !this.shadowRootsObserved.has(shadowRoot);
10462
- // }
10463
- // observerAttached(doc: Document, onCleanup: VoidFunction) {
10464
- // if (!this.usable) return;
10465
- // debugLog('[doc] attaching observer to doc', doc);
10466
- // if (this.docsObservers.has(doc)) {
10467
- // debugLog('[doc] detected existing observer, cleaning up old observer');
10468
- // const cleanupFn = this.docsObservers.get(doc);
10469
- // cleanupFn?.();
10470
- // }
10471
- // this.docsObservers.set(doc, onCleanup);
10472
- // }
10473
- // observerAttachedToShadow(shadowRoot: ShadowRoot, onCleanup: VoidFunction) {
10474
- // if (!this.usable) return;
10475
- // debugLog('[shadow] attaching observer to shadowRoot', shadowRoot);
10476
- // if (this.shadowRootsObserved.has(shadowRoot)) {
10477
- // debugLog('[shadow] detected existing observer, cleaning up old observer');
10478
- // const cleanupFn = this.shadowRootsObserved.get(shadowRoot);
10479
- // cleanupFn?.();
10480
- // }
10481
- // this.shadowRootsObserved.set(shadowRoot, onCleanup);
10482
- // }
10483
10363
  destroy() {
10484
10364
  this.docsObservers = /* @__PURE__ */ new WeakSet();
10485
10365
  this.shadowRootsObservers = /* @__PURE__ */ new WeakSet();
10366
+ this.docObservers = /* @__PURE__ */ new WeakMap();
10486
10367
  }
10487
10368
  };
10488
10369
  __publicField(_ObserveManager, "instance");
@@ -10494,7 +10375,7 @@ const _MutationRateLimiter = class _MutationRateLimiter2 {
10494
10375
  __publicField(this, "exitMutTracker");
10495
10376
  __publicField(this, "interval", 50);
10496
10377
  __publicField(this, "exitInterval", 100);
10497
- __publicField(this, "mutThreshold", 100);
10378
+ __publicField(this, "mutThreshold", 250);
10498
10379
  __publicField(this, "inGlobalStorm", false);
10499
10380
  __publicField(this, "currentStormStartedAt", -1);
10500
10381
  __publicField(this, "stormTimeLimit", 5e3);
@@ -13024,7 +12905,6 @@ class ShadowDomManager {
13024
12905
  __publicField(this, "bypassOptions");
13025
12906
  __publicField(this, "mirror");
13026
12907
  __publicField(this, "restoreHandlers", []);
13027
- __publicField(this, "mappedRestoreHandlers", {});
13028
12908
  this.mutationCb = options.mutationCb;
13029
12909
  this.scrollCb = options.scrollCb;
13030
12910
  this.bypassOptions = options.bypassOptions;
@@ -13037,10 +12917,15 @@ class ShadowDomManager {
13037
12917
  }
13038
12918
  addShadowRoot(shadowRoot2, doc) {
13039
12919
  if (!isNativeShadowDom(shadowRoot2)) return;
13040
- if (!observeManager.onShadowRootObserver(shadowRoot2)) return;
12920
+ if (!observeManager.canRegisterShadowRootObserver(shadowRoot2)) return;
13041
12921
  if (this.shadowDoms.has(shadowRoot2)) return;
13042
12922
  this.shadowDoms.add(shadowRoot2);
13043
- 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
+ );
13044
12929
  const observer = initMutationObserver(
13045
12930
  __spreadProps(__spreadValues({}, this.bypassOptions), {
13046
12931
  doc,
@@ -13115,15 +13000,6 @@ class ShadowDomManager {
13115
13000
  } catch (e2) {
13116
13001
  }
13117
13002
  });
13118
- Object.values(this.mappedRestoreHandlers).forEach((handlers) => {
13119
- for (const handler of handlers) {
13120
- try {
13121
- handler();
13122
- } catch (e2) {
13123
- }
13124
- }
13125
- });
13126
- this.mappedRestoreHandlers = {};
13127
13003
  this.restoreHandlers = [];
13128
13004
  this.shadowDoms = /* @__PURE__ */ new WeakSet();
13129
13005
  }
@@ -13309,10 +13185,11 @@ function record(options = {}) {
13309
13185
  }
13310
13186
  return e2;
13311
13187
  };
13312
- wrappedEmit = (r2, isCheckout) => {
13188
+ wrappedEmit = (r2, isCheckout, customOpts) => {
13189
+ var _a3;
13313
13190
  var _a2;
13314
13191
  const e2 = r2;
13315
- e2.timestamp = nowTimestamp();
13192
+ e2.timestamp = (_a3 = customOpts == null ? void 0 : customOpts.overrideTimestamp) != null ? _a3 : nowTimestamp();
13316
13193
  if (((_a2 = mutationBuffers[0]) == null ? void 0 : _a2.isFrozen()) && e2.type !== EventType.FullSnapshot && !(e2.type === EventType.IncrementalSnapshot && e2.data.source === IncrementalSource.Mutation)) {
13317
13194
  mutationBuffers.forEach((buf) => buf.unfreeze());
13318
13195
  }
@@ -13423,29 +13300,6 @@ function record(options = {}) {
13423
13300
  },
13424
13301
  mirror
13425
13302
  });
13426
- observeManager.setEmitter(wrappedEmit);
13427
- observeManager.setMutationOptions({
13428
- blockClass,
13429
- blockSelector,
13430
- maskTextClass,
13431
- maskTextSelector,
13432
- inlineStylesheet,
13433
- maskInputOptions,
13434
- maskTextFn,
13435
- maskInputFn,
13436
- slimDOMOptions,
13437
- dataURLOptions,
13438
- canvasManager,
13439
- stylesheetManager,
13440
- shadowDomManager,
13441
- recordCanvas,
13442
- inlineImages,
13443
- mirror,
13444
- iframeManager,
13445
- keepIframeSrcFn,
13446
- mutationCb: wrappedMutationEmit,
13447
- processedNodeManager
13448
- });
13449
13303
  takeFullSnapshot$1 = (isCheckout = false) => {
13450
13304
  if (!recordDOM) {
13451
13305
  return;
@@ -13521,7 +13375,6 @@ function record(options = {}) {
13521
13375
  };
13522
13376
  try {
13523
13377
  const handlers = [];
13524
- const registeredHandlers = {};
13525
13378
  const observe = (doc) => {
13526
13379
  var _a2;
13527
13380
  return callbackWrapper(initObservers)(
@@ -13636,11 +13489,31 @@ function record(options = {}) {
13636
13489
  hooks
13637
13490
  );
13638
13491
  };
13492
+ const registeredHandlers = {};
13639
13493
  iframeManager.addLoadListener((iframeEl) => {
13640
13494
  try {
13641
- if (!observeManager.onDocObserver(iframeEl.contentDocument)) return;
13642
- debugLog("Adding mutation observer for iframe", iframeEl);
13643
- 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
+ );
13644
13517
  } catch (error) {
13645
13518
  if (isDebug()) {
13646
13519
  console.warn("internal error");
@@ -13701,6 +13574,7 @@ function record(options = {}) {
13701
13574
  handlers.forEach((h) => h());
13702
13575
  Object.values(registeredHandlers).forEach((h) => h());
13703
13576
  processedNodeManager.destroy();
13577
+ iframeManager.destroy();
13704
13578
  observeManager.destroy();
13705
13579
  recording = false;
13706
13580
  unregisterErrorHandler();