()
| 104 | * @returns true if `window.fetch` is natively implemented, false otherwise |
| 105 | */ |
| 106 | export function supportsNativeFetch(): boolean { |
| 107 | if (typeof EdgeRuntime === 'string') { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | if (!_isFetchSupported()) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | // Fast path to avoid DOM I/O |
| 116 | // eslint-disable-next-line @typescript-eslint/unbound-method |
| 117 | if (isNativeFunction(WINDOW.fetch)) { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | // window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension) |
| 122 | // so create a "pure" iframe to see if that has native fetch |
| 123 | let result = false; |
| 124 | const doc = WINDOW.document; |
| 125 | // eslint-disable-next-line typescript/no-deprecated |
| 126 | if (doc && typeof (doc.createElement as unknown) === 'function') { |
| 127 | try { |
| 128 | const sandbox = doc.createElement('iframe'); |
| 129 | sandbox.hidden = true; |
| 130 | doc.head.appendChild(sandbox); |
| 131 | if (sandbox.contentWindow?.fetch) { |
| 132 | // eslint-disable-next-line @typescript-eslint/unbound-method |
| 133 | result = isNativeFunction(sandbox.contentWindow.fetch); |
| 134 | } |
| 135 | doc.head.removeChild(sandbox); |
| 136 | } catch (err) { |
| 137 | DEBUG_BUILD && debug.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Tells whether current environment supports ReportingObserver API |
no test coverage detected