@kalayanasundaram123/rrweb 2.0.3 → 2.0.4

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/rrweb.cjs CHANGED
@@ -13950,6 +13950,26 @@ function fnv1aHash(buffer) {
13950
13950
  function canSnapshotCanvas() {
13951
13951
  return typeof OffscreenCanvas !== "undefined";
13952
13952
  }
13953
+ const FPS_WORKER_SOURCE = `
13954
+ var _C='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
13955
+ function _enc(ab){var b=new Uint8Array(ab),i,n=b.length,o='';for(i=0;i<n;i+=3){o+=_C[b[i]>>2];o+=_C[(b[i]&3)<<4|b[i+1]>>4];o+=_C[(b[i+1]&15)<<2|b[i+2]>>6];o+=_C[b[i+2]&63];}if(n%3===2){o=o.substring(0,o.length-1)+'=';}else if(n%3===1){o=o.substring(0,o.length-2)+'==';}return o;}
13956
+ function _fp(ab){var v=new Uint8Array(ab),h=0x811c9dc5;for(var i=0;i<v.length;i++){h^=v[i];h=(h*0x01000193)|0;}return (h>>>0).toString(16);}
13957
+ var _last=new Map(),_cv=null,_ctx=null;
13958
+ self.onmessage=async function(e){
13959
+ var d=e.data,id=d.id,bm=d.bitmap,w=d.width,h=d.height,opt=d.dataURLOptions;
13960
+ try{
13961
+ if(!('OffscreenCanvas' in self)){bm.close();return self.postMessage({id:id});}
13962
+ if(!_cv||_cv.width!==w||_cv.height!==h){_cv=new OffscreenCanvas(w,h);_ctx=_cv.getContext('2d');}
13963
+ _ctx.clearRect(0,0,w,h);_ctx.drawImage(bm,0,0);bm.close();
13964
+ var blob=await _cv.convertToBlob(opt);
13965
+ var buf=await blob.arrayBuffer();
13966
+ var fp=_fp(buf);
13967
+ if(_last.get(id)===fp)return self.postMessage({id:id});
13968
+ _last.set(id,fp);
13969
+ self.postMessage({id:id,type:blob.type,base64:_enc(buf),width:w,height:h});
13970
+ }catch(err){try{bm.close();}catch(_){}self.postMessage({id:id});}
13971
+ };
13972
+ `;
13953
13973
  class CanvasManager {
13954
13974
  constructor(options) {
13955
13975
  __publicField(this, "pendingCanvasMutations", /* @__PURE__ */ new Map());
@@ -13972,6 +13992,9 @@ class CanvasManager {
13972
13992
  __publicField(this, "lastFingerprintMap", /* @__PURE__ */ new Map());
13973
13993
  __publicField(this, "snapshotCanvas", null);
13974
13994
  __publicField(this, "snapshotCtx", null);
13995
+ // Encode worker (created from a `data:` URL). null => encode on the main
13996
+ // thread instead (worker unavailable or blocked by CSP).
13997
+ __publicField(this, "worker", null);
13975
13998
  __publicField(this, "lastSnapshotTime", 0);
13976
13999
  __publicField(this, "processMutation", (target, mutation) => {
13977
14000
  const newFrame = this.rafStamps.invokeId && this.rafStamps.latestId !== this.rafStamps.invokeId;
@@ -13993,10 +14016,12 @@ class CanvasManager {
13993
14016
  this.startPendingCanvasMutationFlusher();
13994
14017
  }
13995
14018
  if (recordCanvas && typeof sampling === "number" && canSnapshotCanvas()) {
14019
+ this.worker = this.initFPSWorker();
13996
14020
  this.initCanvasFPSObserver();
13997
14021
  }
13998
14022
  }
13999
14023
  reset() {
14024
+ var _a2;
14000
14025
  this.pendingCanvasMutations.clear();
14001
14026
  this.restoreHandlers.forEach((handler) => {
14002
14027
  try {
@@ -14012,6 +14037,8 @@ class CanvasManager {
14012
14037
  this.lastFingerprintMap = /* @__PURE__ */ new Map();
14013
14038
  this.snapshotCanvas = null;
14014
14039
  this.snapshotCtx = null;
14040
+ (_a2 = this.worker) == null ? void 0 : _a2.terminate();
14041
+ this.worker = null;
14015
14042
  }
14016
14043
  freeze() {
14017
14044
  this.frozen = true;
@@ -14025,6 +14052,29 @@ class CanvasManager {
14025
14052
  unlock() {
14026
14053
  this.locked = false;
14027
14054
  }
14055
+ initFPSWorker() {
14056
+ try {
14057
+ const worker = new Worker(
14058
+ "data:application/javascript;charset=utf-8," + encodeURIComponent(FPS_WORKER_SOURCE)
14059
+ );
14060
+ worker.onmessage = (e2) => {
14061
+ const data = e2.data;
14062
+ const { id } = data;
14063
+ this.snapshotInProgressMap.set(id, false);
14064
+ if (data.base64 === void 0) return;
14065
+ this.emitCanvasSnapshot(
14066
+ id,
14067
+ data.base64,
14068
+ data.type || "image/png",
14069
+ data.width || 0,
14070
+ data.height || 0
14071
+ );
14072
+ };
14073
+ return worker;
14074
+ } catch {
14075
+ return null;
14076
+ }
14077
+ }
14028
14078
  /**
14029
14079
  * Begin observing canvas activity inside `win` — the top window, or the
14030
14080
  * `contentWindow` of a same-origin iframe. De-duplicated, so it is safe to
@@ -14231,9 +14281,22 @@ class CanvasManager {
14231
14281
  }
14232
14282
  const width = canvas.width;
14233
14283
  const height = canvas.height;
14234
- createImageBitmap(canvas).then(
14235
- (bitmap) => this.snapshotImageBitmap(id, bitmap, width, height, dataURLOptions)
14236
- ).catch(() => {
14284
+ createImageBitmap(canvas).then((bitmap) => {
14285
+ if (this.worker) {
14286
+ this.worker.postMessage(
14287
+ { id, bitmap, width, height, dataURLOptions },
14288
+ [bitmap]
14289
+ );
14290
+ } else {
14291
+ void this.snapshotImageBitmap(
14292
+ id,
14293
+ bitmap,
14294
+ width,
14295
+ height,
14296
+ dataURLOptions
14297
+ );
14298
+ }
14299
+ }).catch(() => {
14237
14300
  this.snapshotInProgressMap.set(id, false);
14238
14301
  });
14239
14302
  });