1 |
- {"version":3,"file":"utils.js","sources":["../../../../src/transports/utils.ts"],"sourcesContent":["import { isNativeFetch, logger } from '@sentry/utils';\n\nimport { DEBUG_BUILD } from '../debug-build';\nimport { WINDOW } from '../helpers';\n\nlet cachedFetchImpl: FetchImpl | undefined = undefined;\n\nexport type FetchImpl = typeof fetch;\n\n/**\n * A special usecase for incorrectly wrapped Fetch APIs in conjunction with ad-blockers.\n * Whenever someone wraps the Fetch API and returns the wrong promise chain,\n * this chain becomes orphaned and there is no possible way to capture it's rejections\n * other than allowing it bubble up to this very handler. eg.\n *\n * const f = window.fetch;\n * window.fetch = function () {\n * const p = f.apply(this, arguments);\n *\n * p.then(function() {\n * console.log('hi.');\n * });\n *\n * return p;\n * }\n *\n * `p.then(function () { ... })` is producing a completely separate promise chain,\n * however, what's returned is `p` - the result of original `fetch` call.\n *\n * This mean, that whenever we use the Fetch API to send our own requests, _and_\n * some ad-blocker blocks it, this orphaned chain will _always_ reject,\n * effectively causing another event to be captured.\n * This makes a whole process become an infinite loop, which we need to somehow\n * deal with, and break it in one way or another.\n *\n * To deal with this issue, we are making sure that we _always_ use the real\n * browser Fetch API, instead of relying on what `window.fetch` exposes.\n * The only downside to this would be missing our own requests as breadcrumbs,\n * but because we are already not doing this, it should be just fine.\n *\n * Possible failed fetch error messages per-browser:\n *\n * Chrome: Failed to fetch\n * Edge: Failed to Fetch\n * Firefox: NetworkError when attempting to fetch resource\n * Safari: resource blocked by content blocker\n */\nexport function getNativeFetchImplementation(): FetchImpl {\n if (cachedFetchImpl) {\n return cachedFetchImpl;\n }\n\n /* eslint-disable @typescript-eslint/unbound-method */\n\n // Fast path to avoid DOM I/O\n if (isNativeFetch(WINDOW.fetch)) {\n return (cachedFetchImpl = WINDOW.fetch.bind(WINDOW));\n }\n\n const document = WINDOW.document;\n let fetchImpl = WINDOW.fetch;\n // eslint-disable-next-line deprecation/deprecation\n if (document && typeof document.createElement === 'function') {\n try {\n const sandbox = document.createElement('iframe');\n sandbox.hidden = true;\n document.head.appendChild(sandbox);\n const contentWindow = sandbox.contentWindow;\n if (contentWindow && contentWindow.fetch) {\n fetchImpl = contentWindow.fetch;\n }\n document.head.removeChild(sandbox);\n } catch (e) {\n DEBUG_BUILD && logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e);\n }\n }\n\n return (cachedFetchImpl = fetchImpl.bind(WINDOW));\n /* eslint-enable @typescript-eslint/unbound-method */\n}\n\n/** Clears cached fetch impl */\nexport function clearCachedFetchImplementation(): void {\n cachedFetchImpl = undefined;\n}\n"],"names":[],"mappings":";;;;AAKA,IAAI,eAAe,GAA0B,SAAS,CAAA;;AAItD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,4BAA4B,GAAc;AAC1D,EAAE,IAAI,eAAe,EAAE;AACvB,IAAI,OAAO,eAAe,CAAA;AAC1B,GAAE;AACF;AACA;AACA;AACA;AACA,EAAE,IAAI,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;AACnC,IAAI,QAAQ,eAAgB,GAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAC;AACxD,GAAE;AACF;AACA,EAAE,MAAM,QAAA,GAAW,MAAM,CAAC,QAAQ,CAAA;AAClC,EAAE,IAAI,SAAA,GAAY,MAAM,CAAC,KAAK,CAAA;AAC9B;AACA,EAAE,IAAI,QAAA,IAAY,OAAO,QAAQ,CAAC,aAAA,KAAkB,UAAU,EAAE;AAChE,IAAI,IAAI;AACR,MAAM,MAAM,UAAU,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;AACtD,MAAM,OAAO,CAAC,MAAO,GAAE,IAAI,CAAA;AAC3B,MAAM,QAAQ,CAAC,IAAI,CAAC,WAAW,C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