@glimt/record 0.0.74 → 0.0.76

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/record.cjs CHANGED
@@ -10291,45 +10291,143 @@ class IframeManager {
10291
10291
  }
10292
10292
  const _ObserveManager = class _ObserveManager2 {
10293
10293
  constructor() {
10294
- __publicField(this, "docsObservers", /* @__PURE__ */ new WeakMap());
10295
- __publicField(this, "shadowRootsObserved", /* @__PURE__ */ new WeakMap());
10294
+ __publicField(this, "docsObservers", /* @__PURE__ */ new WeakSet());
10295
+ __publicField(this, "shadowRootsObservers", /* @__PURE__ */ new WeakMap());
10296
+ __publicField(this, "mutationOptions");
10297
+ __publicField(this, "emitter", null);
10296
10298
  if (_ObserveManager2.instance) {
10297
10299
  return _ObserveManager2.instance;
10298
10300
  }
10299
10301
  _ObserveManager2.instance = this;
10300
10302
  }
10301
- observerAttached(doc, onCleanup) {
10302
- debugLog("[doc] attaching observer to doc", doc);
10303
- if (this.docsObservers.has(doc)) {
10304
- debugLog("[doc] detected existing observer, cleaning up old observer");
10305
- const cleanupFn = this.docsObservers.get(doc);
10306
- cleanupFn == null ? void 0 : cleanupFn();
10307
- }
10308
- this.docsObservers.set(doc, onCleanup);
10303
+ setEmitter(emitter) {
10304
+ this.emitter = emitter;
10305
+ }
10306
+ setMutationOptions(options) {
10307
+ this.mutationOptions = options;
10308
+ }
10309
+ get usable() {
10310
+ const result2 = this.emitter != null;
10311
+ if (!result2) debugLog("observerManager: emitter is null");
10312
+ return result2;
10309
10313
  }
10310
- observerAttachedToShadow(shadowRoot2, onCleanup) {
10311
- debugLog("[shadow] attaching observer to shadowRoot", shadowRoot2);
10312
- if (this.shadowRootsObserved.has(shadowRoot2)) {
10313
- debugLog("[shadow] detected existing observer, cleaning up old observer");
10314
- const cleanupFn = this.shadowRootsObserved.get(shadowRoot2);
10315
- cleanupFn == null ? void 0 : cleanupFn();
10314
+ serializeDoc(doc) {
10315
+ var _a2, _b2, _c;
10316
+ if (!((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return null;
10317
+ const serialized = serializeNodeWithId(doc, {
10318
+ ...this.mutationOptions,
10319
+ doc,
10320
+ skipChild: false,
10321
+ maskTextFn: (_b2 = this.mutationOptions) == null ? void 0 : _b2.maskTextFn,
10322
+ maskInputFn: (_c = this.mutationOptions) == null ? void 0 : _c.maskInputFn
10323
+ });
10324
+ if (!serialized) {
10325
+ debugLog("snapshotDoc: no serialized node");
10326
+ return null;
10316
10327
  }
10317
- this.shadowRootsObserved.set(shadowRoot2, onCleanup);
10328
+ return serialized;
10329
+ }
10330
+ emitDoc(serialized) {
10331
+ if (!this.emitter || !serialized) return;
10332
+ this.emitter({
10333
+ type: EventType.FullSnapshot,
10334
+ data: {
10335
+ node: serialized,
10336
+ initialOffset: {
10337
+ left: 0,
10338
+ top: 0
10339
+ }
10340
+ }
10341
+ });
10342
+ }
10343
+ emitShadowRoot(serialized, shadowRoot2) {
10344
+ var _a2;
10345
+ if (!this.emitter || !serialized || !((_a2 = this.mutationOptions) == null ? void 0 : _a2.mirror)) return;
10346
+ const hostId = this.mutationOptions.mirror.getId(shadowRoot2.host);
10347
+ const shadowId = this.mutationOptions.mirror.getId(shadowRoot2);
10348
+ this.emitter({
10349
+ type: EventType.IncrementalSnapshot,
10350
+ data: {
10351
+ source: IncrementalSource.Mutation,
10352
+ adds: [
10353
+ {
10354
+ parentId: hostId,
10355
+ nextId: null,
10356
+ node: serialized
10357
+ }
10358
+ ],
10359
+ removes: [
10360
+ {
10361
+ id: shadowId,
10362
+ parentId: hostId
10363
+ }
10364
+ ],
10365
+ attributes: [],
10366
+ texts: []
10367
+ }
10368
+ });
10369
+ }
10370
+ serializeAndEmitDoc(doc) {
10371
+ const serialized = this.serializeDoc(doc);
10372
+ this.emitDoc(serialized);
10373
+ }
10374
+ serializeAndEmitShadowRoot(shadowRoot2) {
10375
+ const serialized = this.serializeDoc(shadowRoot2.ownerDocument);
10376
+ this.emitShadowRoot(serialized, shadowRoot2);
10377
+ }
10378
+ onDocObserver(doc) {
10379
+ if (!this.usable) return false;
10380
+ if (!this.docsObservers.has(doc)) return true;
10381
+ debugLog(
10382
+ "onDocObserver: doc already observed, emitting full snapshot for doc",
10383
+ doc
10384
+ );
10385
+ this.serializeAndEmitDoc(doc);
10386
+ return false;
10387
+ }
10388
+ onShadowRootObserver(shadowRoot2) {
10389
+ if (!this.usable) return false;
10390
+ if (!this.shadowRootsObservers.has(shadowRoot2)) return true;
10391
+ debugLog(
10392
+ "onShadowRootObserver: shadowRoot already observed, emitting full snapshot for shadowRoot",
10393
+ shadowRoot2
10394
+ );
10395
+ this.serializeAndEmitShadowRoot(shadowRoot2);
10396
+ return false;
10318
10397
  }
10319
- // attachObserverToDoc(doc: Document, observerMethod: (doc: Document)) {}
10320
10398
  // canObserveDoc(doc: Document) {
10321
- // if (this.docsObservers.has(doc)) return false;
10322
- // this.docsObservers.set(doc);
10323
- // return true;
10399
+ // if (!this.usable) return false;
10400
+ // const hasObserver = this.docsObservers.has(doc);
10401
+ // if (!hasObserver) return true;
10402
+ // return false;
10403
+ // }
10404
+ // canObserveShadowRoot(shadowRoot: ShadowRoot) {
10405
+ // if (!this.usable) return false;
10406
+ // return !this.shadowRootsObserved.has(shadowRoot);
10324
10407
  // }
10325
- // canObserveShadow(shadowRoot: ShadowRoot) {
10326
- // if (this.shadowRootsObserved.has(shadowRoot)) return false;
10327
- // this.shadowRootsObserved.add(shadowRoot);
10328
- // return true;
10408
+ // observerAttached(doc: Document, onCleanup: VoidFunction) {
10409
+ // if (!this.usable) return;
10410
+ // debugLog('[doc] attaching observer to doc', doc);
10411
+ // if (this.docsObservers.has(doc)) {
10412
+ // debugLog('[doc] detected existing observer, cleaning up old observer');
10413
+ // const cleanupFn = this.docsObservers.get(doc);
10414
+ // cleanupFn?.();
10415
+ // }
10416
+ // this.docsObservers.set(doc, onCleanup);
10417
+ // }
10418
+ // observerAttachedToShadow(shadowRoot: ShadowRoot, onCleanup: VoidFunction) {
10419
+ // if (!this.usable) return;
10420
+ // debugLog('[shadow] attaching observer to shadowRoot', shadowRoot);
10421
+ // if (this.shadowRootsObserved.has(shadowRoot)) {
10422
+ // debugLog('[shadow] detected existing observer, cleaning up old observer');
10423
+ // const cleanupFn = this.shadowRootsObserved.get(shadowRoot);
10424
+ // cleanupFn?.();
10425
+ // }
10426
+ // this.shadowRootsObserved.set(shadowRoot, onCleanup);
10329
10427
  // }
10330
10428
  destroy() {
10331
- this.docsObservers = /* @__PURE__ */ new WeakMap();
10332
- this.shadowRootsObserved = /* @__PURE__ */ new WeakMap();
10429
+ this.docsObservers = /* @__PURE__ */ new WeakSet();
10430
+ this.shadowRootsObservers = /* @__PURE__ */ new WeakMap();
10333
10431
  }
10334
10432
  };
10335
10433
  __publicField(_ObserveManager, "instance");
@@ -12886,10 +12984,10 @@ class ShadowDomManager {
12886
12984
  }
12887
12985
  addShadowRoot(shadowRoot2, doc) {
12888
12986
  if (!isNativeShadowDom(shadowRoot2)) return;
12987
+ if (!observeManager.onShadowRootObserver(shadowRoot2)) return;
12889
12988
  if (this.shadowDoms.has(shadowRoot2)) return;
12890
12989
  this.shadowDoms.add(shadowRoot2);
12891
12990
  debugLog(`Adding mutation observer for shadowRoot ${shadowRoot2.host}`);
12892
- const observeId = makeid();
12893
12991
  const observer = initMutationObserver(
12894
12992
  {
12895
12993
  ...this.bypassOptions,
@@ -12900,8 +12998,8 @@ class ShadowDomManager {
12900
12998
  },
12901
12999
  shadowRoot2
12902
13000
  );
12903
- this.mappedRestoreHandlers[observeId] = [
12904
- () => observer.disconnect(),
13001
+ this.restoreHandlers.push(() => observer.disconnect());
13002
+ this.restoreHandlers.push(
12905
13003
  initScrollObserver({
12906
13004
  ...this.bypassOptions,
12907
13005
  scrollCb: this.scrollCb,
@@ -12910,14 +13008,14 @@ class ShadowDomManager {
12910
13008
  doc: shadowRoot2,
12911
13009
  mirror: this.mirror
12912
13010
  })
12913
- ];
13011
+ );
12914
13012
  setTimeout(() => {
12915
13013
  if (shadowRoot2.adoptedStyleSheets && shadowRoot2.adoptedStyleSheets.length > 0)
12916
13014
  this.bypassOptions.stylesheetManager.adoptStyleSheets(
12917
13015
  shadowRoot2.adoptedStyleSheets,
12918
13016
  this.mirror.getId(index$1.host(shadowRoot2))
12919
13017
  );
12920
- this.mappedRestoreHandlers[observeId].push(
13018
+ this.restoreHandlers.push(
12921
13019
  initAdoptedStyleSheetObserver(
12922
13020
  {
12923
13021
  mirror: this.mirror,
@@ -12927,18 +13025,6 @@ class ShadowDomManager {
12927
13025
  )
12928
13026
  );
12929
13027
  }, 0);
12930
- observeManager.observerAttachedToShadow(shadowRoot2, () => {
12931
- const handlers = this.mappedRestoreHandlers[observeId];
12932
- if (handlers) {
12933
- for (const handler of handlers) {
12934
- try {
12935
- handler();
12936
- } catch (e2) {
12937
- }
12938
- }
12939
- }
12940
- delete this.mappedRestoreHandlers[observeId];
12941
- });
12942
13028
  }
12943
13029
  /**
12944
13030
  * Monkey patch 'attachShadow' of an IFrameElement to observe newly added shadow doms.
@@ -13290,6 +13376,29 @@ function record(options = {}) {
13290
13376
  },
13291
13377
  mirror
13292
13378
  });
13379
+ observeManager.setEmitter(wrappedEmit);
13380
+ observeManager.setMutationOptions({
13381
+ blockClass,
13382
+ blockSelector,
13383
+ maskTextClass,
13384
+ maskTextSelector,
13385
+ inlineStylesheet,
13386
+ maskInputOptions,
13387
+ maskTextFn,
13388
+ maskInputFn,
13389
+ slimDOMOptions,
13390
+ dataURLOptions,
13391
+ canvasManager,
13392
+ stylesheetManager,
13393
+ shadowDomManager,
13394
+ recordCanvas,
13395
+ inlineImages,
13396
+ mirror,
13397
+ iframeManager,
13398
+ keepIframeSrcFn,
13399
+ mutationCb: wrappedMutationEmit,
13400
+ processedNodeManager
13401
+ });
13293
13402
  takeFullSnapshot$1 = (isCheckout = false) => {
13294
13403
  if (!recordDOM) {
13295
13404
  return;
@@ -13491,13 +13600,8 @@ function record(options = {}) {
13491
13600
  };
13492
13601
  iframeManager.addLoadListener((iframeEl) => {
13493
13602
  try {
13494
- const stopObserve = observe(iframeEl.contentDocument);
13495
- const id = makeid();
13496
- registeredHandlers[id] = stopObserve;
13497
- observeManager.observerAttached(iframeEl.contentDocument, () => {
13498
- stopObserve();
13499
- delete registeredHandlers[id];
13500
- });
13603
+ if (!observeManager.onDocObserver(iframeEl.contentDocument)) return;
13604
+ handlers.push(observe(iframeEl.contentDocument));
13501
13605
  } catch (error) {
13502
13606
  if (isDebug()) {
13503
13607
  console.warn("internal error");