|
| 1 | +import { logger } from '@sentry/utils'; |
| 2 | +import { DEBUG_BUILD } from './debug-build'; |
| 3 | +import { WINDOW } from './types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * We generally want to use window.fetch / window.setTimeout. |
| 7 | + * However, in some cases this may be wrapped (e.g. by Zone.js for Angular), |
| 8 | + * so we try to get an unpatched version of this from a sandboxed iframe. |
| 9 | + */ |
| 10 | + |
| 11 | +interface CacheableImplementations { |
| 12 | + setTimeout: typeof WINDOW.setTimeout; |
| 13 | + fetch: typeof WINDOW.fetch; |
| 14 | +} |
| 15 | + |
| 16 | +const cachedImplementations: Partial<CacheableImplementations> = {}; |
| 17 | + |
| 18 | +/** |
| 19 | + * isNative checks if the given function is a native implementation |
| 20 | + */ |
| 21 | +// eslint-disable-next-line @typescript-eslint/ban-types |
| 22 | +function isNative(func: Function): boolean { |
| 23 | + return func && /^function\s+\w+\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString()); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Get the native implementation of a browser function. |
| 28 | + * |
| 29 | + * This can be used to ensure we get an unwrapped version of a function, in cases where a wrapped function can lead to problems. |
| 30 | + * |
| 31 | + * The following methods can be retrieved: |
| 32 | + * - `setTimeout`: This can be wrapped by e.g. Angular, causing change detection to be triggered. |
| 33 | + * - `fetch`: This can be wrapped by e.g. ad-blockers, causing an infinite loop when a request is blocked. |
| 34 | + */ |
| 35 | +export function getNativeImplementation<T extends keyof CacheableImplementations>( |
| 36 | + name: T, |
| 37 | +): CacheableImplementations[T] { |
| 38 | + const cached = cachedImplementations[name]; |
| 39 | + if (cached) { |
| 40 | + return cached; |
| 41 | + } |
| 42 | + |
| 43 | + let impl = WINDOW[name] as CacheableImplementations[T]; |
| 44 | + |
| 45 | + // Fast path to avoid DOM I/O |
| 46 | + if (isNative(impl)) { |
| 47 | + return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]); |
| 48 | + } |
| 49 | + |
| 50 | + const document = WINDOW.document; |
| 51 | + // eslint-disable-next-line deprecation/deprecation |
| 52 | + if (document && typeof document.createElement === 'function') { |
| 53 | + try { |
| 54 | + const sandbox = document.createElement('iframe'); |
| 55 | + sandbox.hidden = true; |
| 56 | + document.head.appendChild(sandbox); |
| 57 | + const contentWindow = sandbox.contentWindow; |
| 58 | + if (contentWindow && contentWindow[name]) { |
| 59 | + impl = contentWindow[name] as CacheableImplementations[T]; |
| 60 | + } |
| 61 | + document.head.removeChild(sandbox); |
| 62 | + } catch (e) { |
| 63 | + // Could not create sandbox iframe, just use window.xxx |
| 64 | + DEBUG_BUILD && logger.warn(`Could not create sandbox iframe for ${name} check, bailing to window.${name}: `, e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Sanity check: This _should_ not happen, but if it does, we just skip caching... |
| 69 | + // This can happen e.g. in tests where fetch may not be available in the env, or similar. |
| 70 | + if (!impl) { |
| 71 | + return impl; |
| 72 | + } |
| 73 | + |
| 74 | + return (cachedImplementations[name] = impl.bind(WINDOW) as CacheableImplementations[T]); |
| 75 | +} |
| 76 | + |
| 77 | +/** Clear a cached implementation. */ |
| 78 | +export function clearCachedImplementation(name: keyof CacheableImplementations): void { |
| 79 | + cachedImplementations[name] = undefined; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * A special usecase for incorrectly wrapped Fetch APIs in conjunction with ad-blockers. |
| 84 | + * Whenever someone wraps the Fetch API and returns the wrong promise chain, |
| 85 | + * this chain becomes orphaned and there is no possible way to capture it's rejections |
| 86 | + * other than allowing it bubble up to this very handler. eg. |
| 87 | + * |
| 88 | + * const f = window.fetch; |
| 89 | + * window.fetch = function () { |
| 90 | + * const p = f.apply(this, arguments); |
| 91 | + * |
| 92 | + * p.then(function() { |
| 93 | + * console.log('hi.'); |
| 94 | + * }); |
| 95 | + * |
| 96 | + * return p; |
| 97 | + * } |
| 98 | + * |
| 99 | + * `p.then(function () { ... })` is producing a completely separate promise chain, |
| 100 | + * however, what's returned is `p` - the result of original `fetch` call. |
| 101 | + * |
| 102 | + * This mean, that whenever we use the Fetch API to send our own requests, _and_ |
| 103 | + * some ad-blocker blocks it, this orphaned chain will _always_ reject, |
| 104 | + * effectively causing another event to be captured. |
| 105 | + * This makes a whole process become an infinite loop, which we need to somehow |
| 106 | + * deal with, and break it in one way or another. |
| 107 | + * |
| 108 | + * To deal with this issue, we are making sure that we _always_ use the real |
| 109 | + * browser Fetch API, instead of relying on what `window.fetch` exposes. |
| 110 | + * The only downside to this would be missing our own requests as breadcrumbs, |
| 111 | + * but because we are already not doing this, it should be just fine. |
| 112 | + * |
| 113 | + * Possible failed fetch error messages per-browser: |
| 114 | + * |
| 115 | + * Chrome: Failed to fetch |
| 116 | + * Edge: Failed to Fetch |
| 117 | + * Firefox: NetworkError when attempting to fetch resource |
| 118 | + * Safari: resource blocked by content blocker |
| 119 | + */ |
| 120 | +export function fetch(...rest: Parameters<typeof WINDOW.fetch>): ReturnType<typeof WINDOW.fetch> { |
| 121 | + return getNativeImplementation('fetch')(...rest); |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Get an unwrapped `setTimeout` method. |
| 126 | + * This ensures that even if e.g. Angular wraps `setTimeout`, we get the native implementation, |
| 127 | + * avoiding triggering change detection. |
| 128 | + */ |
| 129 | +export function setTimeout(...rest: Parameters<typeof WINDOW.setTimeout>): ReturnType<typeof WINDOW.setTimeout> { |
| 130 | + return getNativeImplementation('setTimeout')(...rest); |
| 131 | +} |
0 commit comments