(input, init)
| 524 | } |
| 525 | |
| 526 | export function fetch(input, init) { |
| 527 | return new Promise(function(resolve, reject) { |
| 528 | var request = new Request(input, init) |
| 529 | |
| 530 | if (request.signal && request.signal.aborted) { |
| 531 | return reject(new DOMException('Aborted', 'AbortError')) |
| 532 | } |
| 533 | |
| 534 | var xhr = new XMLHttpRequest() |
| 535 | |
| 536 | function abortXhr() { |
| 537 | xhr.abort() |
| 538 | } |
| 539 | |
| 540 | xhr.onload = function() { |
| 541 | var options = { |
| 542 | statusText: xhr.statusText, |
| 543 | headers: parseHeaders(xhr.getAllResponseHeaders() || '') |
| 544 | } |
| 545 | // This check if specifically for when a user fetches a file locally from the file system |
| 546 | // Only if the status is out of a normal range |
| 547 | if (request.url.indexOf('file://') === 0 && (xhr.status < 200 || xhr.status > 599)) { |
| 548 | options.status = 200; |
| 549 | } else { |
| 550 | options.status = xhr.status; |
| 551 | } |
| 552 | options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL') |
| 553 | var body = 'response' in xhr ? xhr.response : xhr.responseText |
| 554 | setTimeout(function() { |
| 555 | resolve(new Response(body, options)) |
| 556 | }, 0) |
| 557 | } |
| 558 | |
| 559 | xhr.onerror = function() { |
| 560 | setTimeout(function() { |
| 561 | reject(new TypeError('Network request failed')) |
| 562 | }, 0) |
| 563 | } |
| 564 | |
| 565 | xhr.ontimeout = function() { |
| 566 | setTimeout(function() { |
| 567 | reject(new TypeError('Network request timed out')) |
| 568 | }, 0) |
| 569 | } |
| 570 | |
| 571 | xhr.onabort = function() { |
| 572 | setTimeout(function() { |
| 573 | reject(new DOMException('Aborted', 'AbortError')) |
| 574 | }, 0) |
| 575 | } |
| 576 | |
| 577 | function fixUrl(url) { |
| 578 | try { |
| 579 | return url === '' && g.location.href ? g.location.href : url |
| 580 | } catch (e) { |
| 581 | return url |
| 582 | } |
| 583 | } |
no test coverage detected
searching dependent graphs…