@posthog/rrweb-snapshot 0.0.29 → 0.0.30

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/index.d.cts CHANGED
@@ -30,6 +30,8 @@ export declare type cdataNode = {
30
30
  textContent: '';
31
31
  };
32
32
 
33
+ export declare function checkDataURLSize(dataURL: string, maxLength: number | undefined): string;
34
+
33
35
  export declare function classMatchesRegex(node: Node | null, regex: RegExp, checkAncestors: boolean): boolean;
34
36
 
35
37
  export declare function cleanupSnapshot(): void;
@@ -188,6 +190,8 @@ export declare function rebuild(n: serializedNodeWithId, options: {
188
190
  mirror: Mirror;
189
191
  }): Node | null;
190
192
 
193
+ export declare function recompressBase64Image(img: HTMLImageElement, dataURL: string, type?: string, quality?: number): string;
194
+
191
195
  export declare type serializedElementNodeWithId = Extract<serializedNodeWithId, Record<'type', NodeType.Element>>;
192
196
 
193
197
  export declare type serializedNode = (documentNode | documentTypeNode | elementNode | textNode | cdataNode | commentNode) & {
@@ -276,7 +280,7 @@ export declare type textNode = {
276
280
 
277
281
  export declare function toLowerCase<T extends string>(str: T): Lowercase<T>;
278
282
 
279
- export declare function transformAttribute(doc: Document, tagName: Lowercase<string>, name: Lowercase<string>, value: string | null): string | null;
283
+ export declare function transformAttribute(doc: Document, tagName: Lowercase<string>, name: Lowercase<string>, value: string | null, element?: HTMLElement, dataURLOptions?: DataURLOptions): string | null;
280
284
 
281
285
  export declare function visitSnapshot(node: serializedNodeWithId, onVisit: (node: serializedNodeWithId) => unknown): void;
282
286
 
package/dist/index.d.ts CHANGED
@@ -30,6 +30,8 @@ export declare type cdataNode = {
30
30
  textContent: '';
31
31
  };
32
32
 
33
+ export declare function checkDataURLSize(dataURL: string, maxLength: number | undefined): string;
34
+
33
35
  export declare function classMatchesRegex(node: Node | null, regex: RegExp, checkAncestors: boolean): boolean;
34
36
 
35
37
  export declare function cleanupSnapshot(): void;
@@ -188,6 +190,8 @@ export declare function rebuild(n: serializedNodeWithId, options: {
188
190
  mirror: Mirror;
189
191
  }): Node | null;
190
192
 
193
+ export declare function recompressBase64Image(img: HTMLImageElement, dataURL: string, type?: string, quality?: number): string;
194
+
191
195
  export declare type serializedElementNodeWithId = Extract<serializedNodeWithId, Record<'type', NodeType.Element>>;
192
196
 
193
197
  export declare type serializedNode = (documentNode | documentTypeNode | elementNode | textNode | cdataNode | commentNode) & {
@@ -276,7 +280,7 @@ export declare type textNode = {
276
280
 
277
281
  export declare function toLowerCase<T extends string>(str: T): Lowercase<T>;
278
282
 
279
- export declare function transformAttribute(doc: Document, tagName: Lowercase<string>, name: Lowercase<string>, value: string | null): string | null;
283
+ export declare function transformAttribute(doc: Document, tagName: Lowercase<string>, name: Lowercase<string>, value: string | null, element?: HTMLElement, dataURLOptions?: DataURLOptions): string | null;
280
284
 
281
285
  export declare function visitSnapshot(node: serializedNodeWithId, onVisit: (node: serializedNodeWithId) => unknown): void;
282
286
 
@@ -470,6 +470,36 @@ function absolutifyURLs(cssText, href) {
470
470
  }
471
471
  );
472
472
  }
473
+ const STRIPED_PLACEHOLDER_SVG = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxkZWZzPgogICAgPHBhdHRlcm4gaWQ9InN0cmlwZXMiIHBhdHRlcm5Vbml0cz0idXNlclNwYWNlT25Vc2UiIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiI+CiAgICAgIDxyZWN0IHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgZmlsbD0iYmxhY2siLz4KICAgICAgPHBhdGggZD0iTTggMEgxNkwwIDE2VjhMOCAwWiIgZmlsbD0iIzJEMkQyRCIvPgogICAgICA8cGF0aCBkPSJNMTYgOFYxNkg4TDE2IDhaIiBmaWxsPSIjMkQyRDJEIi8+CiAgICA8L3BhdHRlcm4+CiAgPC9kZWZzPgogIDxyZWN0IHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjc3RyaXBlcykiLz4KPC9zdmc+Cg==";
474
+ const MAX_IMAGE_DIMENSION_FOR_RECOMPRESSION = 4096;
475
+ function recompressBase64Image(img, dataURL, type, quality) {
476
+ if (!img.complete || img.naturalWidth === 0) {
477
+ return dataURL;
478
+ }
479
+ if (img.naturalWidth > MAX_IMAGE_DIMENSION_FOR_RECOMPRESSION || img.naturalHeight > MAX_IMAGE_DIMENSION_FOR_RECOMPRESSION) {
480
+ return dataURL;
481
+ }
482
+ try {
483
+ const canvas = document.createElement("canvas");
484
+ canvas.width = img.naturalWidth;
485
+ canvas.height = img.naturalHeight;
486
+ const ctx = canvas.getContext("2d");
487
+ if (!ctx) {
488
+ return dataURL;
489
+ }
490
+ ctx.drawImage(img, 0, 0);
491
+ const recompressed = canvas.toDataURL(type || "image/webp", quality ?? 0.4);
492
+ return recompressed;
493
+ } catch (err) {
494
+ return dataURL;
495
+ }
496
+ }
497
+ function checkDataURLSize(dataURL, maxLength) {
498
+ if (!maxLength || dataURL.length <= maxLength) {
499
+ return dataURL;
500
+ }
501
+ return STRIPED_PLACEHOLDER_SVG;
502
+ }
473
503
  let _id = 1;
474
504
  const tagNameRegex = new RegExp("[^a-z0-9-_:]");
475
505
  const IGNORED_NODE = -2;
@@ -568,12 +598,32 @@ function getHref(doc, customHref) {
568
598
  a.setAttribute("href", customHref);
569
599
  return a.href;
570
600
  }
571
- function transformAttribute(doc, tagName, name, value) {
601
+ function transformAttribute(doc, tagName, name, value, element, dataURLOptions) {
572
602
  if (!value) {
573
603
  return value;
574
604
  }
575
605
  if (name === "src" || name === "href" && !(tagName === "use" && value[0] === "#")) {
576
- return absoluteToDoc(doc, value);
606
+ const transformedValue = absoluteToDoc(doc, value);
607
+ if (tagName === "img" && transformedValue.startsWith("data:") && element) {
608
+ const img = element;
609
+ let processedDataURL = transformedValue;
610
+ if ((dataURLOptions == null ? void 0 : dataURLOptions.type) || (dataURLOptions == null ? void 0 : dataURLOptions.quality) !== void 0) {
611
+ processedDataURL = recompressBase64Image(
612
+ img,
613
+ transformedValue,
614
+ dataURLOptions.type,
615
+ dataURLOptions.quality
616
+ );
617
+ }
618
+ if (dataURLOptions == null ? void 0 : dataURLOptions.maxBase64ImageLength) {
619
+ processedDataURL = checkDataURLSize(
620
+ processedDataURL,
621
+ dataURLOptions.maxBase64ImageLength
622
+ );
623
+ }
624
+ return processedDataURL;
625
+ }
626
+ return transformedValue;
577
627
  } else if (name === "xlink:href" && value[0] !== "#") {
578
628
  return absoluteToDoc(doc, value);
579
629
  } else if (name === "background" && (tagName === "table" || tagName === "td" || tagName === "th")) {
@@ -864,7 +914,9 @@ function serializeElementNode(n, options) {
864
914
  doc,
865
915
  tagName,
866
916
  toLowerCase(attr.name),
867
- attr.value
917
+ attr.value,
918
+ n,
919
+ dataURLOptions
868
920
  );
869
921
  }
870
922
  }
@@ -5735,6 +5787,7 @@ exports.NodeType = NodeType;
5735
5787
  exports.absolutifyURLs = absolutifyURLs;
5736
5788
  exports.adaptCssForReplay = adaptCssForReplay;
5737
5789
  exports.buildNodeWithSN = buildNodeWithSN;
5790
+ exports.checkDataURLSize = checkDataURLSize;
5738
5791
  exports.classMatchesRegex = classMatchesRegex;
5739
5792
  exports.cleanupSnapshot = cleanupSnapshot;
5740
5793
  exports.createCache = createCache;
@@ -5755,6 +5808,7 @@ exports.isShadowRoot = isShadowRoot;
5755
5808
  exports.maskInputValue = maskInputValue;
5756
5809
  exports.needMaskingText = needMaskingText;
5757
5810
  exports.rebuild = rebuild;
5811
+ exports.recompressBase64Image = recompressBase64Image;
5758
5812
  exports.serializeNodeWithId = serializeNodeWithId;
5759
5813
  exports.snapshot = snapshot;
5760
5814
  exports.stringifyRule = stringifyRule;