Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/date-now-guard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb': patch
---

Guard against presence of older 3rd party javascript libraries which redefine Date.now()
5 changes: 5 additions & 0 deletions .changeset/little-radios-thank.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
hasShadowRoot,
isSerializedIframe,
isSerializedStylesheet,
nowTimestamp,
} from '../utils';
import type { recordOptions } from '../types';
import {
Expand Down Expand Up @@ -42,7 +43,7 @@ import {
function wrapEvent(e: event): eventWithTime {
return {
...e,
timestamp: Date.now(),
timestamp: nowTimestamp(),
};
}

Expand Down
5 changes: 3 additions & 2 deletions packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
legacy_isTouchEvent,
patch,
StyleSheetMirror,
nowTimestamp,
} from '../utils';
import type { observerParam, MutationBufferParam } from '../types';
import {
Expand Down Expand Up @@ -180,13 +181,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'
Expand Down
9 changes: 9 additions & 0 deletions packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down