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