Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - File fetching broken since commit 0c1d2b9 #1375

Merged
merged 11 commits into from Aug 28, 2023
10 changes: 8 additions & 2 deletions fetch.js
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
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