From 96017dedebec6672c00441bfffc899d38864ce39 Mon Sep 17 00:00:00 2001 From: mercpls Date: Mon, 28 Aug 2023 18:24:31 -0400 Subject: [PATCH] Fix - File fetching broken since commit 0c1d2b9 (#1375) --- fetch.js | 10 ++++++++-- test/test.js | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/fetch.js b/fetch.js index 001ffeff..99360494 100644 --- a/fetch.js +++ b/fetch.js @@ -538,10 +538,16 @@ export function fetch(input, init) { xhr.onload = function() { var options = { - status: xhr.status, statusText: xhr.statusText, headers: parseHeaders(xhr.getAllResponseHeaders() || '') } + // This check if specifically for when a user fetches a file locally from the file system + // Only if the status is out of a normal range + if (request.url.startsWith('file://') && (xhr.status < 200 || xhr.status > 599)) { + options.status = 200; + } else { + options.status = xhr.status; + } options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL') var body = 'response' in xhr ? xhr.response : xhr.responseText setTimeout(function() { @@ -632,4 +638,4 @@ if (!g.fetch) { g.Headers = Headers g.Request = Request g.Response = Response -} +} \ No newline at end of file diff --git a/test/test.js b/test/test.js index e19a9841..bd3b739b 100644 --- a/test/test.js +++ b/test/test.js @@ -647,7 +647,15 @@ exercise.forEach(function(exerciseMode) { new Response('', {status: i}) }) } + // A fetch with the url of a `file://` scheme may have a status 0 or + // similar in some operating systems. In the event that a status is found outside + // the standard range of 200-599, and the url start with `file://` + // the status should return 200 + assert.doesNotThrow(function() { + new Request('', {status: 0, request: {url: 'file://path/to/local/file'}}) + }) }) + test('creates Headers object from raw headers', function() { var r = new Response('{"foo":"bar"}', {headers: {'content-type': 'application/json'}}) assert.equal(r.headers instanceof Headers, true)