@glimt/record 0.0.75 → 0.0.77

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.
@@ -10333,40 +10333,144 @@ class IframeManager {
10333
10333
  }
10334
10334
  const _ObserveManager = class _ObserveManager2 {
10335
10335
  constructor() {
10336
- __publicField(this, "docsObservers", /* @__PURE__ */ new WeakMap());
10337
- __publicField(this, "shadowRootsObserved", /* @__PURE__ */ new WeakMap());
10336
+ __publicField(this, "docsObservers", /* @__PURE__ */ new WeakSet());
10337
+ __publicField(this, "shadowRootsObservers", /* @__PURE__ */ new WeakSet());
10338
+ __publicField(this, "mutationOptions");
10339
+ __publicField(this, "emitter", null);
10338
10340
  if (_ObserveManager2.instance) {
10339
10341
  return _ObserveManager2.instance;
10340
10342
  }
10341
10343
  _ObserveManager2.instance = this;
10342
10344
  }
10343
- canObserveDoc(doc) {
10344
- return !this.docsObservers.has(doc);
10345
+ setEmitter(emitter) {
10346
+ this.emitter = emitter;
10345
10347
  }
10346
- canObserveShadowRoot(shadowRoot2) {
10347
- return !this.shadowRootsObserved.has(shadowRoot2);
10348
+ setMutationOptions(options) {
10349
+ this.mutationOptions = options;
10348
10350
  }
10349
- observerAttached(doc, onCleanup) {
10350
- debugLog("[doc] attaching observer to doc", doc);
10351
- if (this.docsObservers.has(doc)) {
10352
- debugLog("[doc] detected existing observer, cleaning up old observer");
10353
- const cleanupFn = this.docsObservers.get(doc);
10354
- cleanupFn == null ? void 0 : cleanupFn();
10355
- }
10356
- this.docsObservers.set(doc, onCleanup);
10351
+ get usable() {
10352
+ const result2 = this.emitter != null;
10353
+ if (!result2) debugLog("observerManager: emitter is null");
10354
+ return result2;
10357
10355
  }
10358
- observerAttachedToShadow(shadowRoot2, onCleanup) {
10359
- debugLog("[shadow] attaching observer to shadowRoot", shadowRoot2);
10360
- if (this.shadowRootsObserved.has(shadowRoot2)) {
10361
- debugLog("[shadow] detected existing observer, cleaning up old observer");
10362
- const cleanupFn = this.shadowRootsObserved.get(shadowRoot2);
10363
- cleanupFn == null ? void 0 : cleanupFn();
10356
+ serializeDoc(doc) {
10357
+ var _a2, _b2, _c;
10358
+ if (!((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return null;
10359
+ const serialized = serializeNodeWithId(doc, __spreadProps(__spreadValues({}, this.mutationOptions), {
10360
+ doc,
10361
+ skipChild: false,
10362
+ maskTextFn: (_b2 = this.mutationOptions) == null ? void 0 : _b2.maskTextFn,
10363
+ maskInputFn: (_c = this.mutationOptions) == null ? void 0 : _c.maskInputFn
10364
+ }));
10365
+ if (!serialized) {
10366
+ debugLog("snapshotDoc: no serialized node");
10367
+ return null;
10364
10368
  }
10365
- this.shadowRootsObserved.set(shadowRoot2, onCleanup);
10369
+ return serialized;
10370
+ }
10371
+ emitDoc(serialized) {
10372
+ if (!this.emitter || !serialized) return;
10373
+ this.emitter({
10374
+ type: EventType.FullSnapshot,
10375
+ data: {
10376
+ node: serialized,
10377
+ initialOffset: {
10378
+ left: 0,
10379
+ top: 0
10380
+ }
10381
+ }
10382
+ });
10383
+ }
10384
+ emitShadowRoot(serialized, shadowRoot2) {
10385
+ var _a2;
10386
+ if (!this.emitter || !serialized || !((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return;
10387
+ const hostId = this.mutationOptions.mirror.getId(shadowRoot2.host);
10388
+ const shadowId = this.mutationOptions.mirror.getId(shadowRoot2);
10389
+ this.emitter({
10390
+ type: EventType.IncrementalSnapshot,
10391
+ data: {
10392
+ source: IncrementalSource.Mutation,
10393
+ adds: [
10394
+ {
10395
+ parentId: hostId,
10396
+ nextId: null,
10397
+ node: serialized
10398
+ }
10399
+ ],
10400
+ removes: [
10401
+ {
10402
+ id: shadowId,
10403
+ parentId: hostId
10404
+ }
10405
+ ],
10406
+ attributes: [],
10407
+ texts: []
10408
+ }
10409
+ });
10410
+ }
10411
+ serializeAndEmitDoc(doc) {
10412
+ const serialized = this.serializeDoc(doc);
10413
+ this.emitDoc(serialized);
10414
+ }
10415
+ serializeAndEmitShadowRoot(shadowRoot2) {
10416
+ const serialized = this.serializeDoc(shadowRoot2.ownerDocument);
10417
+ this.emitShadowRoot(serialized, shadowRoot2);
10418
+ }
10419
+ onDocObserver(doc) {
10420
+ if (!this.usable) return false;
10421
+ if (!this.docsObservers.has(doc)) return true;
10422
+ this.docsObservers.add(doc);
10423
+ debugLog(
10424
+ "onDocObserver: doc already observed, emitting full snapshot for doc",
10425
+ doc
10426
+ );
10427
+ this.serializeAndEmitDoc(doc);
10428
+ return false;
10366
10429
  }
10430
+ onShadowRootObserver(shadowRoot2) {
10431
+ if (!this.usable) return false;
10432
+ if (!this.shadowRootsObservers.has(shadowRoot2)) return true;
10433
+ this.shadowRootsObservers.add(shadowRoot2);
10434
+ debugLog(
10435
+ "onShadowRootObserver: shadowRoot already observed, emitting full snapshot for shadowRoot",
10436
+ shadowRoot2
10437
+ );
10438
+ this.serializeAndEmitShadowRoot(shadowRoot2);
10439
+ return false;
10440
+ }
10441
+ // canObserveDoc(doc: Document) {
10442
+ // if (!this.usable) return false;
10443
+ // const hasObserver = this.docsObservers.has(doc);
10444
+ // if (!hasObserver) return true;
10445
+ // return false;
10446
+ // }
10447
+ // canObserveShadowRoot(shadowRoot: ShadowRoot) {
10448
+ // if (!this.usable) return false;
10449
+ // return !this.shadowRootsObserved.has(shadowRoot);
10450
+ // }
10451
+ // observerAttached(doc: Document, onCleanup: VoidFunction) {
10452
+ // if (!this.usable) return;
10453
+ // debugLog('[doc] attaching observer to doc', doc);
10454
+ // if (this.docsObservers.has(doc)) {
10455
+ // debugLog('[doc] detected existing observer, cleaning up old observer');
10456
+ // const cleanupFn = this.docsObservers.get(doc);
10457
+ // cleanupFn?.();
10458
+ // }
10459
+ // this.docsObservers.set(doc, onCleanup);
10460
+ // }
10461
+ // observerAttachedToShadow(shadowRoot: ShadowRoot, onCleanup: VoidFunction) {
10462
+ // if (!this.usable) return;
10463
+ // debugLog('[shadow] attaching observer to shadowRoot', shadowRoot);
10464
+ // if (this.shadowRootsObserved.has(shadowRoot)) {
10465
+ // debugLog('[shadow] detected existing observer, cleaning up old observer');
10466
+ // const cleanupFn = this.shadowRootsObserved.get(shadowRoot);
10467
+ // cleanupFn?.();
10468
+ // }
10469
+ // this.shadowRootsObserved.set(shadowRoot, onCleanup);
10470
+ // }
10367
10471
  destroy() {
10368
- this.docsObservers = /* @__PURE__ */ new WeakMap();
10369
- this.shadowRootsObserved = /* @__PURE__ */ new WeakMap();
10472
+ this.docsObservers = /* @__PURE__ */ new WeakSet();
10473
+ this.shadowRootsObservers = /* @__PURE__ */ new WeakSet();
10370
10474
  }
10371
10475
  };
10372
10476
  __publicField(_ObserveManager, "instance");
@@ -12921,11 +13025,10 @@ class ShadowDomManager {
12921
13025
  }
12922
13026
  addShadowRoot(shadowRoot2, doc) {
12923
13027
  if (!isNativeShadowDom(shadowRoot2)) return;
12924
- if (!observeManager.canObserveShadowRoot(shadowRoot2)) return;
13028
+ if (!observeManager.onShadowRootObserver(shadowRoot2)) return;
12925
13029
  if (this.shadowDoms.has(shadowRoot2)) return;
12926
13030
  this.shadowDoms.add(shadowRoot2);
12927
13031
  debugLog(`Adding mutation observer for shadowRoot ${shadowRoot2.host}`);
12928
- const observeId = makeid();
12929
13032
  const observer = initMutationObserver(
12930
13033
  __spreadProps(__spreadValues({}, this.bypassOptions), {
12931
13034
  doc,
@@ -12935,8 +13038,8 @@ class ShadowDomManager {
12935
13038
  }),
12936
13039
  shadowRoot2
12937
13040
  );
12938
- this.mappedRestoreHandlers[observeId] = [
12939
- () => observer.disconnect(),
13041
+ this.restoreHandlers.push(() => observer.disconnect());
13042
+ this.restoreHandlers.push(
12940
13043
  initScrollObserver(__spreadProps(__spreadValues({}, this.bypassOptions), {
12941
13044
  scrollCb: this.scrollCb,
12942
13045
  // https://gist.github.com/praveenpuglia/0832da687ed5a5d7a0907046c9ef1813
@@ -12944,14 +13047,14 @@ class ShadowDomManager {
12944
13047
  doc: shadowRoot2,
12945
13048
  mirror: this.mirror
12946
13049
  }))
12947
- ];
13050
+ );
12948
13051
  setTimeout(() => {
12949
13052
  if (shadowRoot2.adoptedStyleSheets && shadowRoot2.adoptedStyleSheets.length > 0)
12950
13053
  this.bypassOptions.stylesheetManager.adoptStyleSheets(
12951
13054
  shadowRoot2.adoptedStyleSheets,
12952
13055
  this.mirror.getId(index$1.host(shadowRoot2))
12953
13056
  );
12954
- this.mappedRestoreHandlers[observeId].push(
13057
+ this.restoreHandlers.push(
12955
13058
  initAdoptedStyleSheetObserver(
12956
13059
  {
12957
13060
  mirror: this.mirror,
@@ -12961,18 +13064,6 @@ class ShadowDomManager {
12961
13064
  )
12962
13065
  );
12963
13066
  }, 0);
12964
- observeManager.observerAttachedToShadow(shadowRoot2, () => {
12965
- const handlers = this.mappedRestoreHandlers[observeId];
12966
- if (handlers) {
12967
- for (const handler of handlers) {
12968
- try {
12969
- handler();
12970
- } catch (e2) {
12971
- }
12972
- }
12973
- }
12974
- delete this.mappedRestoreHandlers[observeId];
12975
- });
12976
13067
  }
12977
13068
  /**
12978
13069
  * Monkey patch 'attachShadow' of an IFrameElement to observe newly added shadow doms.
@@ -13320,6 +13411,29 @@ function record(options = {}) {
13320
13411
  },
13321
13412
  mirror
13322
13413
  });
13414
+ observeManager.setEmitter(wrappedEmit);
13415
+ observeManager.setMutationOptions({
13416
+ blockClass,
13417
+ blockSelector,
13418
+ maskTextClass,
13419
+ maskTextSelector,
13420
+ inlineStylesheet,
13421
+ maskInputOptions,
13422
+ maskTextFn,
13423
+ maskInputFn,
13424
+ slimDOMOptions,
13425
+ dataURLOptions,
13426
+ canvasManager,
13427
+ stylesheetManager,
13428
+ shadowDomManager,
13429
+ recordCanvas,
13430
+ inlineImages,
13431
+ mirror,
13432
+ iframeManager,
13433
+ keepIframeSrcFn,
13434
+ mutationCb: wrappedMutationEmit,
13435
+ processedNodeManager
13436
+ });
13323
13437
  takeFullSnapshot$1 = (isCheckout = false) => {
13324
13438
  if (!recordDOM) {
13325
13439
  return;
@@ -13512,14 +13626,8 @@ function record(options = {}) {
13512
13626
  };
13513
13627
  iframeManager.addLoadListener((iframeEl) => {
13514
13628
  try {
13515
- if (!observeManager.canObserveDoc(iframeEl.contentDocument)) return;
13516
- const stopObserve = observe(iframeEl.contentDocument);
13517
- const id = makeid();
13518
- registeredHandlers[id] = stopObserve;
13519
- observeManager.observerAttached(iframeEl.contentDocument, () => {
13520
- stopObserve();
13521
- delete registeredHandlers[id];
13522
- });
13629
+ if (!observeManager.onDocObserver(iframeEl.contentDocument)) return;
13630
+ handlers.push(observe(iframeEl.contentDocument));
13523
13631
  } catch (error) {
13524
13632
  if (isDebug()) {
13525
13633
  console.warn("internal error");