From 490b3e2b62b62d61e6f6f5391d5b879194c9a221 Mon Sep 17 00:00:00 2001 From: Eoghan Murray Date: Fri, 2 Jun 2023 11:20:14 +0100 Subject: [PATCH] Guard against redefinition of Date.now (#1196) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Guard against presence of likely older third party libraries which (re)define Date.now, e.g. https://github.com/datejs/Datejs/issues/92 * Apply formatting changes * (remove nowTimestamp import where Date.now() is not used) * Add a PURE marker so an empty `ìf` statement doesn't show up in the rrweb-replay output * Update packages/rrweb/src/utils.ts Fix typing issue with regex against `Date.now()` Co-authored-by: Justin Halsall * Create little-radios-thank.md * Apply formatting changes * Update .changeset/little-radios-thank.md * Apply formatting changes --------- Co-authored-by: eoghanmurray Co-authored-by: Justin Halsall --- .changeset/date-now-guard.md | 5 +++++ .changeset/little-radios-thank.md | 5 +++++ packages/rrweb/src/record/index.ts | 3 ++- packages/rrweb/src/record/observer.ts | 5 +++-- packages/rrweb/src/utils.ts | 9 +++++++++ 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 .changeset/date-now-guard.md create mode 100644 .changeset/little-radios-thank.md diff --git a/.changeset/date-now-guard.md b/.changeset/date-now-guard.md new file mode 100644 index 00000000..2e0ac5e7 --- /dev/null +++ b/.changeset/date-now-guard.md @@ -0,0 +1,5 @@ +--- +'rrweb': patch +--- + +Guard against presence of older 3rd party javascript libraries which redefine Date.now() diff --git a/.changeset/little-radios-thank.md b/.changeset/little-radios-thank.md new file mode 100644 index 00000000..ea9c3f60 --- /dev/null +++ b/.changeset/little-radios-thank.md @@ -0,0 +1,5 @@ +--- +'rrweb': patch +--- + +Guard against redefinition of Date.now by third party libraries which are also present on a page alongside rrweb diff --git a/packages/rrweb/src/record/index.ts b/packages/rrweb/src/record/index.ts index c0e69a02..56394231 100644 --- a/packages/rrweb/src/record/index.ts +++ b/packages/rrweb/src/record/index.ts @@ -14,6 +14,7 @@ import { hasShadowRoot, isSerializedIframe, isSerializedStylesheet, + nowTimestamp, } from '../utils'; import type { recordOptions } from '../types'; import { @@ -42,7 +43,7 @@ import { function wrapEvent(e: event): eventWithTime { return { ...e, - timestamp: Date.now(), + timestamp: nowTimestamp(), }; } diff --git a/packages/rrweb/src/record/observer.ts b/packages/rrweb/src/record/observer.ts index 754f9240..78690578 100644 --- a/packages/rrweb/src/record/observer.ts +++ b/packages/rrweb/src/record/observer.ts @@ -17,6 +17,7 @@ import { legacy_isTouchEvent, patch, StyleSheetMirror, + nowTimestamp, } from '../utils'; import type { observerParam, MutationBufferParam } from '../types'; import { @@ -181,13 +182,13 @@ function initMoveObserver({ ? evt.changedTouches[0] : evt; if (!timeBaseline) { - timeBaseline = Date.now(); + timeBaseline = nowTimestamp(); } positions.push({ x: clientX, y: clientY, id: mirror.getId(target as Node), - timeOffset: Date.now() - timeBaseline, + timeOffset: nowTimestamp() - timeBaseline, }); // it is possible DragEvent is undefined even on devices // that support event 'drag' diff --git a/packages/rrweb/src/utils.ts b/packages/rrweb/src/utils.ts index 21977962..604c8810 100644 --- a/packages/rrweb/src/utils.ts +++ b/packages/rrweb/src/utils.ts @@ -168,6 +168,15 @@ export function patch( } } +// guard against old third party libraries which redefine Date.now +let nowTimestamp = Date.now; + +if (!(/*@__PURE__*/ /[1-9][0-9]{12}/.test(Date.now().toString()))) { + // they have already redefined it! use a fallback + nowTimestamp = () => new Date().getTime(); +} +export { nowTimestamp }; + export function getWindowScroll(win: Window) { const doc = win.document; return {