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