-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed up polyfills and improved environment detection
- Loading branch information
1 parent
15b9139
commit ee78621
Showing
9 changed files
with
114 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
lib/ | ||
build/ | ||
dist/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,55 @@ | ||
import fetchRequest from './fetch'; | ||
import { makeXhrTransport } from './xhr'; | ||
|
||
// selected is used to cache the detected transport. | ||
let selected = null; | ||
|
||
// defaultTransportFactory selects the most appropriate transport based on the | ||
// capabilities of the current environment. | ||
export default function defaultTransportFactory() { | ||
if (!selected) { | ||
if (window.Response && window.Response.prototype.hasOwnProperty("body")) { | ||
selected = fetchRequest; | ||
} else { | ||
const tmpXhr = new XMLHttpRequest(); | ||
const mozChunked = 'moz-chunked-arraybuffer'; | ||
tmpXhr.responseType = mozChunked; | ||
if (tmpXhr.responseType === mozChunked) { | ||
selected = makeXhrTransport({ | ||
responseType: mozChunked, | ||
responseParserFactory: function () { | ||
return response => new Uint8Array(response); | ||
} | ||
}); | ||
} else { | ||
selected = makeXhrTransport({ | ||
responseType: 'text', | ||
responseParserFactory: function () { | ||
const encoder = new TextEncoder(); | ||
let offset = 0; | ||
return function (response) { | ||
const chunk = response.substr(offset); | ||
offset = response.length; | ||
return encoder.encode(chunk, { stream: true }); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
selected = detectTransport(); | ||
} | ||
return selected; | ||
} | ||
|
||
function detectTransport() { | ||
if (typeof Response !== 'undefined' && Response.prototype.hasOwnProperty("body")) { | ||
// fetch with ReadableStream support. | ||
return fetchRequest; | ||
} | ||
|
||
const mozChunked = 'moz-chunked-arraybuffer'; | ||
if (supportsXhrResponseType(mozChunked)) { | ||
// Firefox, ArrayBuffer support. | ||
return makeXhrTransport({ | ||
responseType: mozChunked, | ||
responseParserFactory: function () { | ||
return response => new Uint8Array(response); | ||
} | ||
}); | ||
} | ||
|
||
// Bog-standard, expensive, text concatenation with byte encoding :( | ||
return makeXhrTransport({ | ||
responseType: 'text', | ||
responseParserFactory: function () { | ||
const encoder = new TextEncoder(); | ||
let offset = 0; | ||
return function (response) { | ||
const chunk = response.substr(offset); | ||
offset = response.length; | ||
return encoder.encode(chunk, { stream: true }); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function supportsXhrResponseType(type) { | ||
try { | ||
const tmpXhr = new XMLHttpRequest(); | ||
tmpXhr.responseType = type; | ||
return tmpXhr.responseType === type; | ||
} catch (e) { /* IE throws on setting responseType to an unsupported value */ } | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters