|
| 1 | +import { forget, getGlobalObject, isNativeFetch, logger, supportsFetch } from '@sentry/utils'; |
| 2 | + |
| 3 | +const global = getGlobalObject<Window>(); |
| 4 | +let cachedFetchImpl: FetchImpl; |
| 5 | + |
| 6 | +export type FetchImpl = typeof fetch; |
| 7 | + |
| 8 | +/** |
| 9 | + * A special usecase for incorrectly wrapped Fetch APIs in conjunction with ad-blockers. |
| 10 | + * Whenever someone wraps the Fetch API and returns the wrong promise chain, |
| 11 | + * this chain becomes orphaned and there is no possible way to capture it's rejections |
| 12 | + * other than allowing it bubble up to this very handler. eg. |
| 13 | + * |
| 14 | + * const f = window.fetch; |
| 15 | + * window.fetch = function () { |
| 16 | + * const p = f.apply(this, arguments); |
| 17 | + * |
| 18 | + * p.then(function() { |
| 19 | + * console.log('hi.'); |
| 20 | + * }); |
| 21 | + * |
| 22 | + * return p; |
| 23 | + * } |
| 24 | + * |
| 25 | + * `p.then(function () { ... })` is producing a completely separate promise chain, |
| 26 | + * however, what's returned is `p` - the result of original `fetch` call. |
| 27 | + * |
| 28 | + * This mean, that whenever we use the Fetch API to send our own requests, _and_ |
| 29 | + * some ad-blocker blocks it, this orphaned chain will _always_ reject, |
| 30 | + * effectively causing another event to be captured. |
| 31 | + * This makes a whole process become an infinite loop, which we need to somehow |
| 32 | + * deal with, and break it in one way or another. |
| 33 | + * |
| 34 | + * To deal with this issue, we are making sure that we _always_ use the real |
| 35 | + * browser Fetch API, instead of relying on what `window.fetch` exposes. |
| 36 | + * The only downside to this would be missing our own requests as breadcrumbs, |
| 37 | + * but because we are already not doing this, it should be just fine. |
| 38 | + * |
| 39 | + * Possible failed fetch error messages per-browser: |
| 40 | + * |
| 41 | + * Chrome: Failed to fetch |
| 42 | + * Edge: Failed to Fetch |
| 43 | + * Firefox: NetworkError when attempting to fetch resource |
| 44 | + * Safari: resource blocked by content blocker |
| 45 | + */ |
| 46 | +export function getNativeFetchImplementation(): FetchImpl { |
| 47 | + if (cachedFetchImpl) { |
| 48 | + return cachedFetchImpl; |
| 49 | + } |
| 50 | + |
| 51 | + /* eslint-disable @typescript-eslint/unbound-method */ |
| 52 | + |
| 53 | + // Fast path to avoid DOM I/O |
| 54 | + if (isNativeFetch(global.fetch)) { |
| 55 | + return (cachedFetchImpl = global.fetch.bind(global)); |
| 56 | + } |
| 57 | + |
| 58 | + const document = global.document; |
| 59 | + let fetchImpl = global.fetch; |
| 60 | + // eslint-disable-next-line deprecation/deprecation |
| 61 | + if (typeof document?.createElement === `function`) { |
| 62 | + try { |
| 63 | + const sandbox = document.createElement('iframe'); |
| 64 | + sandbox.hidden = true; |
| 65 | + document.head.appendChild(sandbox); |
| 66 | + if (sandbox.contentWindow?.fetch) { |
| 67 | + fetchImpl = sandbox.contentWindow.fetch; |
| 68 | + } |
| 69 | + document.head.removeChild(sandbox); |
| 70 | + } catch (e) { |
| 71 | + logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return (cachedFetchImpl = fetchImpl.bind(global)); |
| 76 | + /* eslint-enable @typescript-eslint/unbound-method */ |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Sends sdk client report using sendBeacon or fetch as a fallback if available |
| 81 | + * |
| 82 | + * @param url report endpoint |
| 83 | + * @param body report payload |
| 84 | + */ |
| 85 | +export function sendReport(url: string, body: string): void { |
| 86 | + const isRealNavigator = Object.prototype.toString.call(global && global.navigator) === '[object Navigator]'; |
| 87 | + const hasSendBeacon = isRealNavigator && typeof global.navigator.sendBeacon === 'function'; |
| 88 | + |
| 89 | + if (hasSendBeacon) { |
| 90 | + // Prevent illegal invocations - https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch |
| 91 | + const sendBeacon = global.navigator.sendBeacon.bind(global.navigator); |
| 92 | + return sendBeacon(url, body); |
| 93 | + } |
| 94 | + |
| 95 | + if (supportsFetch()) { |
| 96 | + const fetch = getNativeFetchImplementation(); |
| 97 | + return forget( |
| 98 | + fetch(url, { |
| 99 | + body, |
| 100 | + method: 'POST', |
| 101 | + credentials: 'omit', |
| 102 | + keepalive: true, |
| 103 | + }), |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments