|
1 | 1 | import { eventToSentryRequest, sessionToSentryRequest } from '@sentry/core';
|
2 |
| -import { Event, Response, SentryRequest, Session } from '@sentry/types'; |
3 |
| -import { getGlobalObject, supportsReferrerPolicy, SyncPromise } from '@sentry/utils'; |
| 2 | +import { Event, Response, SentryRequest, Session, TransportOptions } from '@sentry/types'; |
| 3 | +import { getGlobalObject, logger, supportsReferrerPolicy, SyncPromise } from '@sentry/utils'; |
4 | 4 |
|
5 | 5 | import { BaseTransport } from './base';
|
6 | 6 |
|
7 |
| -const global = getGlobalObject<Window>(); |
| 7 | +type FetchImpl = typeof fetch; |
| 8 | + |
| 9 | +/** |
| 10 | + * A special usecase for incorrectly wrapped Fetch APIs in conjunction with ad-blockers. |
| 11 | + * Whenever someone wraps the Fetch API and returns the wrong promise chain, |
| 12 | + * this chain becomes orphaned and there is no possible way to capture it's rejections |
| 13 | + * other than allowing it bubble up to this very handler. eg. |
| 14 | + * |
| 15 | + * const f = window.fetch; |
| 16 | + * window.fetch = function () { |
| 17 | + * const p = f.apply(this, arguments); |
| 18 | + * |
| 19 | + * p.then(function() { |
| 20 | + * console.log('hi.'); |
| 21 | + * }); |
| 22 | + * |
| 23 | + * return p; |
| 24 | + * } |
| 25 | + * |
| 26 | + * `p.then(function () { ... })` is producing a completely separate promise chain, |
| 27 | + * however, what's returned is `p` - the result of original `fetch` call. |
| 28 | + * |
| 29 | + * This mean, that whenever we use the Fetch API to send our own requests, _and_ |
| 30 | + * some ad-blocker blocks it, this orphaned chain will _always_ reject, |
| 31 | + * effectively causing another event to be captured. |
| 32 | + * This makes a whole process become an infinite loop, which we need to somehow |
| 33 | + * deal with, and break it in one way or another. |
| 34 | + * |
| 35 | + * To deal with this issue, we are making sure that we _always_ use the real |
| 36 | + * browser Fetch API, instead of relying on what `window.fetch` exposes. |
| 37 | + * The only downside to this would be missing our own requests as breadcrumbs, |
| 38 | + * but because we are already not doing this, it should be just fine. |
| 39 | + * |
| 40 | + * Possible failed fetch error messages per-browser: |
| 41 | + * |
| 42 | + * Chrome: Failed to fetch |
| 43 | + * Edge: Failed to Fetch |
| 44 | + * Firefox: NetworkError when attempting to fetch resource |
| 45 | + * Safari: resource blocked by content blocker |
| 46 | + */ |
| 47 | +function getNativeFetchImplementation(): FetchImpl { |
| 48 | + // Make sure that the fetch we use is always the native one. |
| 49 | + const global = getGlobalObject<Window>(); |
| 50 | + const document = global.document; |
| 51 | + // eslint-disable-next-line deprecation/deprecation |
| 52 | + if (typeof document?.createElement === `function`) { |
| 53 | + try { |
| 54 | + const sandbox = document.createElement('iframe'); |
| 55 | + sandbox.hidden = true; |
| 56 | + document.head.appendChild(sandbox); |
| 57 | + if (sandbox.contentWindow?.fetch) { |
| 58 | + return sandbox.contentWindow.fetch.bind(global); |
| 59 | + } |
| 60 | + document.head.removeChild(sandbox); |
| 61 | + } catch (e) { |
| 62 | + logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e); |
| 63 | + } |
| 64 | + } |
| 65 | + return global.fetch.bind(global); |
| 66 | +} |
8 | 67 |
|
9 | 68 | /** `fetch` based transport */
|
10 | 69 | export class FetchTransport extends BaseTransport {
|
| 70 | + /** |
| 71 | + * Fetch API reference which always points to native browser implementation. |
| 72 | + */ |
| 73 | + private _fetch: typeof fetch; |
| 74 | + |
| 75 | + constructor(options: TransportOptions, fetchImpl: FetchImpl = getNativeFetchImplementation()) { |
| 76 | + super(options); |
| 77 | + this._fetch = fetchImpl; |
| 78 | + } |
| 79 | + |
11 | 80 | /**
|
12 | 81 | * @inheritDoc
|
13 | 82 | */
|
@@ -54,8 +123,7 @@ export class FetchTransport extends BaseTransport {
|
54 | 123 |
|
55 | 124 | return this._buffer.add(
|
56 | 125 | new SyncPromise<Response>((resolve, reject) => {
|
57 |
| - global |
58 |
| - .fetch(sentryRequest.url, options) |
| 126 | + this._fetch(sentryRequest.url, options) |
59 | 127 | .then(response => {
|
60 | 128 | const headers = {
|
61 | 129 | 'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
|
|
0 commit comments