|
| 1 | +// Fetch polyfill from https://github.com/developit/unfetch |
| 2 | +// License: |
| 3 | +//============================================================================== |
| 4 | +// Copyright (c) 2017 Jason Miller |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +//============================================================================== |
| 24 | + |
| 25 | +#if !POLYFILL |
| 26 | +#error "this file should never be included unless POLYFILL is set" |
| 27 | +#endif |
| 28 | + |
| 29 | +if (typeof globalThis.fetch == 'undefined') { |
| 30 | + globalThis.fetch = function (url, options) { |
| 31 | + options = options || {}; |
| 32 | + return new Promise((resolve, reject) => { |
| 33 | + const request = new XMLHttpRequest(); |
| 34 | + const keys = []; |
| 35 | + const headers = {}; |
| 36 | + |
| 37 | + request.responseType = 'arraybuffer'; |
| 38 | + |
| 39 | + const response = () => ({ |
| 40 | + ok: ((request.status / 100) | 0) == 2, // 200-299 |
| 41 | + statusText: request.statusText, |
| 42 | + status: request.status, |
| 43 | + url: request.responseURL, |
| 44 | + text: () => Promise.resolve(request.responseText), |
| 45 | + json: () => Promise.resolve(request.responseText).then(JSON.parse), |
| 46 | + blob: () => Promise.resolve(new Blob([request.response])), |
| 47 | + arrayBuffer: () => Promise.resolve(request.response), |
| 48 | + clone: response, |
| 49 | + headers: { |
| 50 | + keys: () => keys, |
| 51 | + entries: () => keys.map((n) => [n, request.getResponseHeader(n)]), |
| 52 | + get: (n) => request.getResponseHeader(n), |
| 53 | + has: (n) => request.getResponseHeader(n) != null, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + request.open(options.method || "get", url, true); |
| 58 | + |
| 59 | + request.onload = () => { |
| 60 | + request |
| 61 | + .getAllResponseHeaders() |
| 62 | + .toLowerCase() |
| 63 | + .replace(/^(.+?):/gm, (m, key) => { |
| 64 | + headers[key] || keys.push((headers[key] = key)); |
| 65 | + }); |
| 66 | + resolve(response()); |
| 67 | + }; |
| 68 | + |
| 69 | + request.onerror = reject; |
| 70 | + |
| 71 | + request.withCredentials = options.credentials == "include"; |
| 72 | + |
| 73 | + for (const i in options.headers) { |
| 74 | + request.setRequestHeader(i, options.headers[i]); |
| 75 | + } |
| 76 | + |
| 77 | + request.send(options.body || null); |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
0 commit comments