Skip to content

Commit

Permalink
Fix - File fetching broken since commit 0c1d2b9 (#1375)
Browse files Browse the repository at this point in the history
  • Loading branch information
mercpls authored Aug 28, 2023
1 parent 1dc07c6 commit 96017de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -632,4 +638,4 @@ if (!g.fetch) {
g.Headers = Headers
g.Request = Request
g.Response = Response
}
}
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 96017de

Please sign in to comment.