Skip to content
Open
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
25 changes: 22 additions & 3 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,15 @@ interface CacheableImplementations {

const cachedImplementations: Partial<CacheableImplementations> = {};

function isNativeFunction(func: unknown): boolean {
return (
typeof func === 'function' &&
/^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(
func.toString(),
)
);
}

function getImplementation<T extends keyof CacheableImplementations>(
name: T,
): CacheableImplementations[T] {
Expand All @@ -450,16 +459,26 @@ function getImplementation<T extends keyof CacheableImplementations>(
return cached;
}

const impl = window[name] as CacheableImplementations[T];

// Fast path to avoid DOM I/O — matches the approach used in
// @sentry-internal/browser-utils getNativeImplementation.
if (isNativeFunction(impl)) {
return (cachedImplementations[name] = impl.bind(
window,
) as CacheableImplementations[T]);
}

let sandboxImpl = impl;
const document = window.document;
let impl = window[name] as CacheableImplementations[T];
if (document && typeof document.createElement === 'function') {
try {
const sandbox = document.createElement('iframe');
sandbox.hidden = true;
document.head.appendChild(sandbox);
const contentWindow = sandbox.contentWindow;
if (contentWindow && contentWindow[name]) {
impl =
sandboxImpl =
// eslint-disable-next-line @typescript-eslint/unbound-method
contentWindow[name] as CacheableImplementations[T];
}
Expand All @@ -469,7 +488,7 @@ function getImplementation<T extends keyof CacheableImplementations>(
}
}

return (cachedImplementations[name] = impl.bind(
return (cachedImplementations[name] = sandboxImpl.bind(
window,
) as CacheableImplementations[T]);
}
Expand Down
25 changes: 22 additions & 3 deletions packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,15 @@ interface CacheableImplementations {

const cachedImplementations: Partial<CacheableImplementations> = {};

function isNativeFunction(func: unknown): boolean {
return (
typeof func === 'function' &&
/^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(
func.toString(),
)
);
}

function getImplementation<T extends keyof CacheableImplementations>(
name: T,
): CacheableImplementations[T] {
Expand All @@ -635,16 +644,26 @@ function getImplementation<T extends keyof CacheableImplementations>(
return cached;
}

const impl = window[name] as CacheableImplementations[T];

// Fast path to avoid DOM I/O — matches the approach used in
// @sentry-internal/browser-utils getNativeImplementation.
if (isNativeFunction(impl)) {
return (cachedImplementations[name] = impl.bind(
window,
) as CacheableImplementations[T]);
}

let sandboxImpl = impl;
const document = window.document;
let impl = window[name] as CacheableImplementations[T];
if (document && typeof document.createElement === 'function') {
try {
const sandbox = document.createElement('iframe');
sandbox.hidden = true;
document.head.appendChild(sandbox);
const contentWindow = sandbox.contentWindow;
if (contentWindow && contentWindow[name]) {
impl =
sandboxImpl =
// eslint-disable-next-line @typescript-eslint/unbound-method
contentWindow[name] as CacheableImplementations[T];
}
Expand All @@ -654,7 +673,7 @@ function getImplementation<T extends keyof CacheableImplementations>(
}
}

return (cachedImplementations[name] = impl.bind(
return (cachedImplementations[name] = sandboxImpl.bind(
window,
) as CacheableImplementations[T]);
}
Expand Down