Skip to content

Commit

Permalink
Merge pull request #585 from lambdalisue/hotfix-584
Browse files Browse the repository at this point in the history
Patch native fetch on Safari to fix #584
  • Loading branch information
wheresrhys committed Aug 1, 2020
2 parents 55fa469 + e3f2edb commit 54b0aeb
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/lib/fetch-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,50 @@ class AbortError extends Error {
}
}

// Patch native fetch to avoid "NotSupportedError:ReadableStream uploading is not supported" in Safari.
// See also https://github.com/wheresrhys/fetch-mock/issues/584
// See also https://stackoverflow.com/a/50952018/1273406
const patchNativeFetchForSafari = (nativeFetch) => {
// Try to patch fetch only on Safari
if (
!navigator ||
!navigator.vendor ||
navigator.vendor !== 'Apple Computer, Inc.'
) {
return nativeFetch;
}
// It seems the code is working on Safari thus patch native fetch to avoid the error.
return async (request) => {
const { method } = request;
if (!['POST', 'PUT', 'PATCH'].includes(method)) {
// No patch is required in this case
return nativeFetch(request);
}
const body = await request.clone().text();
const {
cache,
credentials,
headers,
integrity,
mode,
redirect,
referrer,
} = request;
const init = {
body,
cache,
credentials,
headers,
integrity,
mode,
redirect,
referrer,
method,
};
return nativeFetch(request.url, init);
};
};

const resolve = async (
{ response, responseIsFetch = false },
url,
Expand Down Expand Up @@ -252,7 +296,7 @@ FetchMock.getNativeFetch = function () {
'fetch-mock: Falling back to network only available on global fetch-mock, or by setting config.fetch on sandboxed fetch-mock'
);
}
return func;
return patchNativeFetchForSafari(func);
};

FetchMock.recordCall = function (obj) {
Expand Down

0 comments on commit 54b0aeb

Please sign in to comment.