Skip to content

Commit 490b3e2

Browse files
Guard against redefinition of Date.now (#1196)
* Guard against presence of likely older third party libraries which (re)define Date.now, e.g. datejs/Datejs#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 <[email protected]> * Create little-radios-thank.md * Apply formatting changes * Update .changeset/little-radios-thank.md * Apply formatting changes --------- Co-authored-by: eoghanmurray <[email protected]> Co-authored-by: Justin Halsall <[email protected]>
1 parent 325a9f0 commit 490b3e2

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

.changeset/date-now-guard.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'rrweb': patch
3+
---
4+
5+
Guard against presence of older 3rd party javascript libraries which redefine Date.now()

.changeset/little-radios-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'rrweb': patch
3+
---
4+
5+
Guard against redefinition of Date.now by third party libraries which are also present on a page alongside rrweb

packages/rrweb/src/record/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
hasShadowRoot,
1515
isSerializedIframe,
1616
isSerializedStylesheet,
17+
nowTimestamp,
1718
} from '../utils';
1819
import type { recordOptions } from '../types';
1920
import {
@@ -42,7 +43,7 @@ import {
4243
function wrapEvent(e: event): eventWithTime {
4344
return {
4445
...e,
45-
timestamp: Date.now(),
46+
timestamp: nowTimestamp(),
4647
};
4748
}
4849

packages/rrweb/src/record/observer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
legacy_isTouchEvent,
1818
patch,
1919
StyleSheetMirror,
20+
nowTimestamp,
2021
} from '../utils';
2122
import type { observerParam, MutationBufferParam } from '../types';
2223
import {
@@ -181,13 +182,13 @@ function initMoveObserver({
181182
? evt.changedTouches[0]
182183
: evt;
183184
if (!timeBaseline) {
184-
timeBaseline = Date.now();
185+
timeBaseline = nowTimestamp();
185186
}
186187
positions.push({
187188
x: clientX,
188189
y: clientY,
189190
id: mirror.getId(target as Node),
190-
timeOffset: Date.now() - timeBaseline,
191+
timeOffset: nowTimestamp() - timeBaseline,
191192
});
192193
// it is possible DragEvent is undefined even on devices
193194
// that support event 'drag'

packages/rrweb/src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ export function patch(
168168
}
169169
}
170170

171+
// guard against old third party libraries which redefine Date.now
172+
let nowTimestamp = Date.now;
173+
174+
if (!(/*@__PURE__*/ /[1-9][0-9]{12}/.test(Date.now().toString()))) {
175+
// they have already redefined it! use a fallback
176+
nowTimestamp = () => new Date().getTime();
177+
}
178+
export { nowTimestamp };
179+
171180
export function getWindowScroll(win: Window) {
172181
const doc = win.document;
173182
return {

0 commit comments

Comments
 (0)