sync-fetch
Advanced tools
Comparing version 0.4.3 to 0.4.4
@@ -15,7 +15,12 @@ /* eslint-env browser */ | ||
let useBinaryEncoding = false | ||
try { | ||
// Only allowed in Worker scope, not available in older browsers | ||
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType#Synchronous_XHR_restrictions | ||
xhr.responseType = 'arraybuffer' | ||
} catch (e) { | ||
// not in Worker scope | ||
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType#Synchronous_XHR_restrictions | ||
// Not in Worker scope; instead, attempt this alternative method | ||
// https://web.archive.org/web/20071103070418/http://mgran.blogspot.com/2006/08/downloading-binary-streams-with.html | ||
xhr.overrideMimeType('text/plain; charset=x-binary') | ||
useBinaryEncoding = true | ||
} | ||
@@ -33,3 +38,12 @@ | ||
const response = new syncFetch.Response(xhr.response, { | ||
let body = xhr.response | ||
if (useBinaryEncoding) { | ||
const buffer = Buffer.alloc(body.length) | ||
for (let i = 0; i < body.length; i++) { | ||
buffer[i] = body.charCodeAt(i) & 0xff | ||
} | ||
body = buffer | ||
} | ||
const response = new syncFetch.Response(body, { | ||
headers, | ||
@@ -36,0 +50,0 @@ status: xhr.status, |
{ | ||
"name": "sync-fetch", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "Synchronous version of the Fetch API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -39,3 +39,2 @@ # sync-fetch | ||
- (Non-spec) `timeout` | ||
- Does not support [binary responses in the main thread](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType#Synchronous_XHR_restrictions) | ||
- CORS limitations apply, of course (note they may be stricter for synchronous requests) |
Sorry, the diff of this file is too big to display
136743
4277
40