@glimt/record 0.0.16 → 0.0.18

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.
@@ -397,12 +397,8 @@ function toLowerCase(str) {
397
397
  const ORIGINAL_ATTRIBUTE_NAME = "__rrweb_original__";
398
398
  const COPY_CANVAS_ON_VOLUME_GREATER_THAN = 25e4;
399
399
  function getReadableCanvasContext(canvas) {
400
- if (canvas.width === 0 || canvas.height === 0) return canvas.getContext("2d");
401
400
  if (canvas.width * canvas.height < COPY_CANVAS_ON_VOLUME_GREATER_THAN)
402
401
  return canvas.getContext("2d");
403
- console.log(
404
- "detected large canvas, copying to offscreen canvas for willReadFrequently"
405
- );
406
402
  const offscreen = document.createElement("canvas");
407
403
  offscreen.width = canvas.width;
408
404
  offscreen.height = canvas.height;
@@ -413,24 +409,23 @@ function getReadableCanvasContext(canvas) {
413
409
  return offscreen.getContext("2d", { willReadFrequently: true });
414
410
  }
415
411
  function is2DCanvasBlank(canvas) {
412
+ if (canvas.width === 0 || canvas.height === 0) return true;
416
413
  const ctx = getReadableCanvasContext(canvas);
417
414
  if (!ctx) return true;
418
- const chunkSize = 50;
415
+ const chunkSize = canvas.width > 512 || canvas.height > 512 ? 100 : 50;
416
+ const getImageData = ctx.getImageData;
417
+ const originalGetImageData = ORIGINAL_ATTRIBUTE_NAME in getImageData ? getImageData[ORIGINAL_ATTRIBUTE_NAME] : getImageData;
419
418
  for (let x2 = 0; x2 < canvas.width; x2 += chunkSize) {
419
+ const w = Math.min(chunkSize, canvas.width - x2);
420
420
  for (let y = 0; y < canvas.height; y += chunkSize) {
421
- const getImageData = ctx.getImageData;
422
- const originalGetImageData = ORIGINAL_ATTRIBUTE_NAME in getImageData ? getImageData[ORIGINAL_ATTRIBUTE_NAME] : getImageData;
421
+ const h = Math.min(chunkSize, canvas.height - y);
423
422
  const pixelBuffer = new Uint32Array(
424
423
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
425
- originalGetImageData.call(
426
- ctx,
427
- x2,
428
- y,
429
- Math.min(chunkSize, canvas.width - x2),
430
- Math.min(chunkSize, canvas.height - y)
431
- ).data.buffer
424
+ originalGetImageData.call(ctx, x2, y, w, h).data.buffer
432
425
  );
433
- if (pixelBuffer.some((pixel) => pixel !== 0)) return false;
426
+ for (let i2 = 0; i2 < pixelBuffer.length; i2++) {
427
+ if (pixelBuffer[i2] !== 0) return false;
428
+ }
434
429
  }
435
430
  }
436
431
  return true;
@@ -9441,7 +9436,9 @@ class MutationBuffer {
9441
9436
  __publicField(this, "processedNodeManager");
9442
9437
  __publicField(this, "unattachedDoc");
9443
9438
  __publicField(this, "processMutations", (mutations) => {
9444
- mutations.forEach(this.processMutation);
9439
+ for (const mut of mutations) {
9440
+ this.processMutation(mut);
9441
+ }
9445
9442
  this.emit();
9446
9443
  });
9447
9444
  __publicField(this, "emit", () => {
@@ -9592,34 +9589,91 @@ class MutationBuffer {
9592
9589
  addList.removeNode(node2.value);
9593
9590
  pushAdd(node2.value);
9594
9591
  }
9595
- const payload = {
9596
- texts: this.texts.map((text) => {
9597
- const n2 = text.node;
9598
- const parent = index.parentNode(n2);
9599
- if (parent && parent.tagName === "TEXTAREA") {
9600
- this.genTextAreaValueMutation(parent);
9601
- }
9602
- return {
9603
- id: this.mirror.getId(n2),
9592
+ const payloadTexts = [];
9593
+ for (const text of this.texts) {
9594
+ const n2 = text.node;
9595
+ const parent = index.parentNode(n2);
9596
+ if (parent && parent.tagName === "TEXTAREA") {
9597
+ this.genTextAreaValueMutation(parent);
9598
+ }
9599
+ const id = this.mirror.getId(n2);
9600
+ if (!addedIds.has(id) && this.mirror.has(id)) {
9601
+ payloadTexts.push({
9602
+ id,
9604
9603
  value: text.value
9605
- };
9606
- }).filter((text) => !addedIds.has(text.id)).filter((text) => this.mirror.has(text.id)),
9607
- attributes: this.attributes.map((attribute) => {
9608
- const { attributes } = attribute;
9609
- if (typeof attributes.style === "string") {
9610
- const diffAsStr = JSON.stringify(attribute.styleDiff);
9611
- const unchangedAsStr = JSON.stringify(attribute._unchangedStyles);
9612
- if (diffAsStr.length < attributes.style.length) {
9613
- if ((diffAsStr + unchangedAsStr).split("var(").length === attributes.style.split("var(").length) {
9614
- attributes.style = attribute.styleDiff;
9615
- }
9604
+ });
9605
+ }
9606
+ }
9607
+ const payloadAttributes = [];
9608
+ for (const attribute of this.attributes) {
9609
+ const { attributes } = attribute;
9610
+ if (typeof attributes.style === "string") {
9611
+ const diffAsStr = JSON.stringify(attribute.styleDiff);
9612
+ const unchangedAsStr = JSON.stringify(attribute._unchangedStyles);
9613
+ if (diffAsStr.length < attributes.style.length) {
9614
+ if ((diffAsStr + unchangedAsStr).split("var(").length === attributes.style.split("var(").length) {
9615
+ attributes.style = attribute.styleDiff;
9616
9616
  }
9617
9617
  }
9618
- return {
9619
- id: this.mirror.getId(attribute.node),
9618
+ }
9619
+ const id = this.mirror.getId(attribute.node);
9620
+ if (!addedIds.has(id) && this.mirror.has(id)) {
9621
+ payloadAttributes.push({
9622
+ id,
9620
9623
  attributes
9621
- };
9622
- }).filter((attribute) => !addedIds.has(attribute.id)).filter((attribute) => this.mirror.has(attribute.id)),
9624
+ });
9625
+ }
9626
+ }
9627
+ const payload = {
9628
+ texts: payloadTexts,
9629
+ attributes: payloadAttributes,
9630
+ //original implementation instead of "payloadTexts"
9631
+ // this.texts
9632
+ // .map((text) => {
9633
+ // const n = text.node;
9634
+ // const parent = dom.parentNode(n);
9635
+ // if (parent && (parent as Element).tagName === 'TEXTAREA') {
9636
+ // // the node is being ignored as it isn't in the mirror, so shift mutation to attributes on parent textarea
9637
+ // this.genTextAreaValueMutation(parent as HTMLTextAreaElement);
9638
+ // }
9639
+ // return {
9640
+ // id: this.mirror.getId(n),
9641
+ // value: text.value,
9642
+ // };
9643
+ // })
9644
+ // // no need to include them on added elements, as they have just been serialized with up to date attribubtes
9645
+ // .filter((text) => !addedIds.has(text.id))
9646
+ // // text mutation's id was not in the mirror map means the target node has been removed
9647
+ // .filter((text) => this.mirror.has(text.id)),
9648
+ //original implementation instead of "payloadAttributes"
9649
+ // this.attributes
9650
+ // .map((attribute) => {
9651
+ // const { attributes } = attribute;
9652
+ // if (typeof attributes.style === 'string') {
9653
+ // const diffAsStr = JSON.stringify(attribute.styleDiff);
9654
+ // const unchangedAsStr = JSON.stringify(attribute._unchangedStyles);
9655
+ // // check if the style diff is actually shorter than the regular string based mutation
9656
+ // // (which was the whole point of #464 'compact style mutation').
9657
+ // if (diffAsStr.length < attributes.style.length) {
9658
+ // // also: CSSOM fails badly when var() is present on shorthand properties, so only proceed with
9659
+ // // the compact style mutation if these have all been accounted for
9660
+ // if (
9661
+ // (diffAsStr + unchangedAsStr).split('var(').length ===
9662
+ // attributes.style.split('var(').length
9663
+ // ) {
9664
+ // attributes.style = attribute.styleDiff;
9665
+ // }
9666
+ // }
9667
+ // }
9668
+ // return {
9669
+ // id: this.mirror.getId(attribute.node),
9670
+ // attributes: attributes,
9671
+ // };
9672
+ // })
9673
+ // // no need to include them on added elements, as they have just been serialized with up to date attribubtes
9674
+ // .filter((attribute) => !addedIds.has(attribute.id))
9675
+ // // attribute mutation's id was not in the mirror map means the target node has been removed
9676
+ // .filter((attribute) => this.mirror.has(attribute.id)),
9623
9677
  removes: this.removes,
9624
9678
  adds
9625
9679
  };
@@ -9776,8 +9830,11 @@ class MutationBuffer {
9776
9830
  this.genTextAreaValueMutation(m.target);
9777
9831
  return;
9778
9832
  }
9779
- m.addedNodes.forEach((n2) => this.genAdds(n2, m.target));
9780
- m.removedNodes.forEach((n2) => {
9833
+ for (let i2 = 0; i2 < m.addedNodes.length; i2++) {
9834
+ this.genAdds(m.addedNodes[i2], m.target);
9835
+ }
9836
+ for (let i2 = 0; i2 < m.removedNodes.length; i2++) {
9837
+ const n2 = m.removedNodes[i2];
9781
9838
  const nodeId = this.mirror.getId(n2);
9782
9839
  const parentId = isShadowRoot(m.target) ? this.mirror.getId(index.host(m.target)) : this.mirror.getId(m.target);
9783
9840
  if (isBlocked(m.target, this.blockClass, this.blockSelector, false) || isIgnored(n2, this.mirror, this.slimDOMOptions) || !isSerialized(n2, this.mirror)) {
@@ -9799,7 +9856,7 @@ class MutationBuffer {
9799
9856
  processRemoves(n2, this.removesSubTreeCache);
9800
9857
  }
9801
9858
  this.mapRemoves.push(n2);
9802
- });
9859
+ }
9803
9860
  break;
9804
9861
  }
9805
9862
  }
@@ -9824,12 +9881,17 @@ class MutationBuffer {
9824
9881
  this.droppedSet.delete(n2);
9825
9882
  }
9826
9883
  if (!isBlocked(n2, this.blockClass, this.blockSelector, false)) {
9827
- index.childNodes(n2).forEach((childN) => this.genAdds(childN));
9884
+ const childNodes2 = index.childNodes(n2);
9885
+ for (let i2 = 0; i2 < childNodes2.length; i2++) {
9886
+ this.genAdds(childNodes2[i2]);
9887
+ }
9828
9888
  if (hasShadowRoot(n2)) {
9829
- index.childNodes(index.shadowRoot(n2)).forEach((childN) => {
9889
+ const shadowRootChildNodes = index.childNodes(index.shadowRoot(n2));
9890
+ for (let i2 = 0; i2 < shadowRootChildNodes.length; i2++) {
9891
+ const childN = shadowRootChildNodes[i2];
9830
9892
  this.processedNodeManager.add(childN, this);
9831
9893
  this.genAdds(childN, n2);
9832
- });
9894
+ }
9833
9895
  }
9834
9896
  }
9835
9897
  });
@@ -9889,7 +9951,10 @@ class MutationBuffer {
9889
9951
  }
9890
9952
  function deepDelete(addsSet, n2) {
9891
9953
  addsSet.delete(n2);
9892
- index.childNodes(n2).forEach((childN) => deepDelete(addsSet, childN));
9954
+ const childNodes2 = index.childNodes(n2);
9955
+ for (let i2 = 0; i2 < childNodes2.length; i2++) {
9956
+ deepDelete(addsSet, childNodes2[i2]);
9957
+ }
9893
9958
  }
9894
9959
  function processRemoves(n2, cache) {
9895
9960
  const queue = [n2];
@@ -9897,7 +9962,10 @@ function processRemoves(n2, cache) {
9897
9962
  const next = queue.pop();
9898
9963
  if (cache.has(next)) continue;
9899
9964
  cache.add(next);
9900
- index.childNodes(next).forEach((n22) => queue.push(n22));
9965
+ const childNodes2 = index.childNodes(next);
9966
+ for (let i2 = 0; i2 < childNodes2.length; i2++) {
9967
+ queue.push(childNodes2[i2]);
9968
+ }
9901
9969
  }
9902
9970
  return;
9903
9971
  }