@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.
package/dist/record.cjs CHANGED
@@ -365,12 +365,8 @@ function toLowerCase(str) {
365
365
  const ORIGINAL_ATTRIBUTE_NAME = "__rrweb_original__";
366
366
  const COPY_CANVAS_ON_VOLUME_GREATER_THAN = 25e4;
367
367
  function getReadableCanvasContext(canvas) {
368
- if (canvas.width === 0 || canvas.height === 0) return canvas.getContext("2d");
369
368
  if (canvas.width * canvas.height < COPY_CANVAS_ON_VOLUME_GREATER_THAN)
370
369
  return canvas.getContext("2d");
371
- console.log(
372
- "detected large canvas, copying to offscreen canvas for willReadFrequently"
373
- );
374
370
  const offscreen = document.createElement("canvas");
375
371
  offscreen.width = canvas.width;
376
372
  offscreen.height = canvas.height;
@@ -381,24 +377,23 @@ function getReadableCanvasContext(canvas) {
381
377
  return offscreen.getContext("2d", { willReadFrequently: true });
382
378
  }
383
379
  function is2DCanvasBlank(canvas) {
380
+ if (canvas.width === 0 || canvas.height === 0) return true;
384
381
  const ctx = getReadableCanvasContext(canvas);
385
382
  if (!ctx) return true;
386
- const chunkSize = 50;
383
+ const chunkSize = canvas.width > 512 || canvas.height > 512 ? 100 : 50;
384
+ const getImageData = ctx.getImageData;
385
+ const originalGetImageData = ORIGINAL_ATTRIBUTE_NAME in getImageData ? getImageData[ORIGINAL_ATTRIBUTE_NAME] : getImageData;
387
386
  for (let x2 = 0; x2 < canvas.width; x2 += chunkSize) {
387
+ const w = Math.min(chunkSize, canvas.width - x2);
388
388
  for (let y = 0; y < canvas.height; y += chunkSize) {
389
- const getImageData = ctx.getImageData;
390
- const originalGetImageData = ORIGINAL_ATTRIBUTE_NAME in getImageData ? getImageData[ORIGINAL_ATTRIBUTE_NAME] : getImageData;
389
+ const h = Math.min(chunkSize, canvas.height - y);
391
390
  const pixelBuffer = new Uint32Array(
392
391
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
393
- originalGetImageData.call(
394
- ctx,
395
- x2,
396
- y,
397
- Math.min(chunkSize, canvas.width - x2),
398
- Math.min(chunkSize, canvas.height - y)
399
- ).data.buffer
392
+ originalGetImageData.call(ctx, x2, y, w, h).data.buffer
400
393
  );
401
- if (pixelBuffer.some((pixel) => pixel !== 0)) return false;
394
+ for (let i2 = 0; i2 < pixelBuffer.length; i2++) {
395
+ if (pixelBuffer[i2] !== 0) return false;
396
+ }
402
397
  }
403
398
  }
404
399
  return true;
@@ -9399,7 +9394,9 @@ class MutationBuffer {
9399
9394
  __publicField(this, "processedNodeManager");
9400
9395
  __publicField(this, "unattachedDoc");
9401
9396
  __publicField(this, "processMutations", (mutations) => {
9402
- mutations.forEach(this.processMutation);
9397
+ for (const mut of mutations) {
9398
+ this.processMutation(mut);
9399
+ }
9403
9400
  this.emit();
9404
9401
  });
9405
9402
  __publicField(this, "emit", () => {
@@ -9550,34 +9547,91 @@ class MutationBuffer {
9550
9547
  addList.removeNode(node2.value);
9551
9548
  pushAdd(node2.value);
9552
9549
  }
9553
- const payload = {
9554
- texts: this.texts.map((text) => {
9555
- const n2 = text.node;
9556
- const parent = index.parentNode(n2);
9557
- if (parent && parent.tagName === "TEXTAREA") {
9558
- this.genTextAreaValueMutation(parent);
9559
- }
9560
- return {
9561
- id: this.mirror.getId(n2),
9550
+ const payloadTexts = [];
9551
+ for (const text of this.texts) {
9552
+ const n2 = text.node;
9553
+ const parent = index.parentNode(n2);
9554
+ if (parent && parent.tagName === "TEXTAREA") {
9555
+ this.genTextAreaValueMutation(parent);
9556
+ }
9557
+ const id = this.mirror.getId(n2);
9558
+ if (!addedIds.has(id) && this.mirror.has(id)) {
9559
+ payloadTexts.push({
9560
+ id,
9562
9561
  value: text.value
9563
- };
9564
- }).filter((text) => !addedIds.has(text.id)).filter((text) => this.mirror.has(text.id)),
9565
- attributes: this.attributes.map((attribute) => {
9566
- const { attributes } = attribute;
9567
- if (typeof attributes.style === "string") {
9568
- const diffAsStr = JSON.stringify(attribute.styleDiff);
9569
- const unchangedAsStr = JSON.stringify(attribute._unchangedStyles);
9570
- if (diffAsStr.length < attributes.style.length) {
9571
- if ((diffAsStr + unchangedAsStr).split("var(").length === attributes.style.split("var(").length) {
9572
- attributes.style = attribute.styleDiff;
9573
- }
9562
+ });
9563
+ }
9564
+ }
9565
+ const payloadAttributes = [];
9566
+ for (const attribute of this.attributes) {
9567
+ const { attributes } = attribute;
9568
+ if (typeof attributes.style === "string") {
9569
+ const diffAsStr = JSON.stringify(attribute.styleDiff);
9570
+ const unchangedAsStr = JSON.stringify(attribute._unchangedStyles);
9571
+ if (diffAsStr.length < attributes.style.length) {
9572
+ if ((diffAsStr + unchangedAsStr).split("var(").length === attributes.style.split("var(").length) {
9573
+ attributes.style = attribute.styleDiff;
9574
9574
  }
9575
9575
  }
9576
- return {
9577
- id: this.mirror.getId(attribute.node),
9576
+ }
9577
+ const id = this.mirror.getId(attribute.node);
9578
+ if (!addedIds.has(id) && this.mirror.has(id)) {
9579
+ payloadAttributes.push({
9580
+ id,
9578
9581
  attributes
9579
- };
9580
- }).filter((attribute) => !addedIds.has(attribute.id)).filter((attribute) => this.mirror.has(attribute.id)),
9582
+ });
9583
+ }
9584
+ }
9585
+ const payload = {
9586
+ texts: payloadTexts,
9587
+ attributes: payloadAttributes,
9588
+ //original implementation instead of "payloadTexts"
9589
+ // this.texts
9590
+ // .map((text) => {
9591
+ // const n = text.node;
9592
+ // const parent = dom.parentNode(n);
9593
+ // if (parent && (parent as Element).tagName === 'TEXTAREA') {
9594
+ // // the node is being ignored as it isn't in the mirror, so shift mutation to attributes on parent textarea
9595
+ // this.genTextAreaValueMutation(parent as HTMLTextAreaElement);
9596
+ // }
9597
+ // return {
9598
+ // id: this.mirror.getId(n),
9599
+ // value: text.value,
9600
+ // };
9601
+ // })
9602
+ // // no need to include them on added elements, as they have just been serialized with up to date attribubtes
9603
+ // .filter((text) => !addedIds.has(text.id))
9604
+ // // text mutation's id was not in the mirror map means the target node has been removed
9605
+ // .filter((text) => this.mirror.has(text.id)),
9606
+ //original implementation instead of "payloadAttributes"
9607
+ // this.attributes
9608
+ // .map((attribute) => {
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
+ // // check if the style diff is actually shorter than the regular string based mutation
9614
+ // // (which was the whole point of #464 'compact style mutation').
9615
+ // if (diffAsStr.length < attributes.style.length) {
9616
+ // // also: CSSOM fails badly when var() is present on shorthand properties, so only proceed with
9617
+ // // the compact style mutation if these have all been accounted for
9618
+ // if (
9619
+ // (diffAsStr + unchangedAsStr).split('var(').length ===
9620
+ // attributes.style.split('var(').length
9621
+ // ) {
9622
+ // attributes.style = attribute.styleDiff;
9623
+ // }
9624
+ // }
9625
+ // }
9626
+ // return {
9627
+ // id: this.mirror.getId(attribute.node),
9628
+ // attributes: attributes,
9629
+ // };
9630
+ // })
9631
+ // // no need to include them on added elements, as they have just been serialized with up to date attribubtes
9632
+ // .filter((attribute) => !addedIds.has(attribute.id))
9633
+ // // attribute mutation's id was not in the mirror map means the target node has been removed
9634
+ // .filter((attribute) => this.mirror.has(attribute.id)),
9581
9635
  removes: this.removes,
9582
9636
  adds
9583
9637
  };
@@ -9734,8 +9788,11 @@ class MutationBuffer {
9734
9788
  this.genTextAreaValueMutation(m.target);
9735
9789
  return;
9736
9790
  }
9737
- m.addedNodes.forEach((n2) => this.genAdds(n2, m.target));
9738
- m.removedNodes.forEach((n2) => {
9791
+ for (let i2 = 0; i2 < m.addedNodes.length; i2++) {
9792
+ this.genAdds(m.addedNodes[i2], m.target);
9793
+ }
9794
+ for (let i2 = 0; i2 < m.removedNodes.length; i2++) {
9795
+ const n2 = m.removedNodes[i2];
9739
9796
  const nodeId = this.mirror.getId(n2);
9740
9797
  const parentId = isShadowRoot(m.target) ? this.mirror.getId(index.host(m.target)) : this.mirror.getId(m.target);
9741
9798
  if (isBlocked(m.target, this.blockClass, this.blockSelector, false) || isIgnored(n2, this.mirror, this.slimDOMOptions) || !isSerialized(n2, this.mirror)) {
@@ -9757,7 +9814,7 @@ class MutationBuffer {
9757
9814
  processRemoves(n2, this.removesSubTreeCache);
9758
9815
  }
9759
9816
  this.mapRemoves.push(n2);
9760
- });
9817
+ }
9761
9818
  break;
9762
9819
  }
9763
9820
  }
@@ -9782,12 +9839,17 @@ class MutationBuffer {
9782
9839
  this.droppedSet.delete(n2);
9783
9840
  }
9784
9841
  if (!isBlocked(n2, this.blockClass, this.blockSelector, false)) {
9785
- index.childNodes(n2).forEach((childN) => this.genAdds(childN));
9842
+ const childNodes2 = index.childNodes(n2);
9843
+ for (let i2 = 0; i2 < childNodes2.length; i2++) {
9844
+ this.genAdds(childNodes2[i2]);
9845
+ }
9786
9846
  if (hasShadowRoot(n2)) {
9787
- index.childNodes(index.shadowRoot(n2)).forEach((childN) => {
9847
+ const shadowRootChildNodes = index.childNodes(index.shadowRoot(n2));
9848
+ for (let i2 = 0; i2 < shadowRootChildNodes.length; i2++) {
9849
+ const childN = shadowRootChildNodes[i2];
9788
9850
  this.processedNodeManager.add(childN, this);
9789
9851
  this.genAdds(childN, n2);
9790
- });
9852
+ }
9791
9853
  }
9792
9854
  }
9793
9855
  });
@@ -9847,7 +9909,10 @@ class MutationBuffer {
9847
9909
  }
9848
9910
  function deepDelete(addsSet, n2) {
9849
9911
  addsSet.delete(n2);
9850
- index.childNodes(n2).forEach((childN) => deepDelete(addsSet, childN));
9912
+ const childNodes2 = index.childNodes(n2);
9913
+ for (let i2 = 0; i2 < childNodes2.length; i2++) {
9914
+ deepDelete(addsSet, childNodes2[i2]);
9915
+ }
9851
9916
  }
9852
9917
  function processRemoves(n2, cache) {
9853
9918
  const queue = [n2];
@@ -9855,7 +9920,10 @@ function processRemoves(n2, cache) {
9855
9920
  const next = queue.pop();
9856
9921
  if (cache.has(next)) continue;
9857
9922
  cache.add(next);
9858
- index.childNodes(next).forEach((n22) => queue.push(n22));
9923
+ const childNodes2 = index.childNodes(next);
9924
+ for (let i2 = 0; i2 < childNodes2.length; i2++) {
9925
+ queue.push(childNodes2[i2]);
9926
+ }
9859
9927
  }
9860
9928
  return;
9861
9929
  }