capability.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. export var hasFetch = isFunction(global.fetch) && isFunction(global.ReadableStream)
  2. var _blobConstructor;
  3. export function blobConstructor() {
  4. if (typeof _blobConstructor !== 'undefined') {
  5. return _blobConstructor;
  6. }
  7. try {
  8. new global.Blob([new ArrayBuffer(1)])
  9. _blobConstructor = true
  10. } catch (e) {
  11. _blobConstructor = false
  12. }
  13. return _blobConstructor
  14. }
  15. var xhr;
  16. function checkTypeSupport(type) {
  17. if (!xhr) {
  18. xhr = new global.XMLHttpRequest()
  19. // If location.host is empty, e.g. if this page/worker was loaded
  20. // from a Blob, then use example.com to avoid an error
  21. xhr.open('GET', global.location.host ? '/' : 'https://example.com')
  22. }
  23. try {
  24. xhr.responseType = type
  25. return xhr.responseType === type
  26. } catch (e) {
  27. return false
  28. }
  29. }
  30. // For some strange reason, Safari 7.0 reports typeof global.ArrayBuffer === 'object'.
  31. // Safari 7.1 appears to have fixed this bug.
  32. var haveArrayBuffer = typeof global.ArrayBuffer !== 'undefined'
  33. var haveSlice = haveArrayBuffer && isFunction(global.ArrayBuffer.prototype.slice)
  34. export var arraybuffer = haveArrayBuffer && checkTypeSupport('arraybuffer')
  35. // These next two tests unavoidably show warnings in Chrome. Since fetch will always
  36. // be used if it's available, just return false for these to avoid the warnings.
  37. export var msstream = !hasFetch && haveSlice && checkTypeSupport('ms-stream')
  38. export var mozchunkedarraybuffer = !hasFetch && haveArrayBuffer &&
  39. checkTypeSupport('moz-chunked-arraybuffer')
  40. export var overrideMimeType = isFunction(xhr.overrideMimeType)
  41. export var vbArray = isFunction(global.VBArray)
  42. function isFunction(value) {
  43. return typeof value === 'function'
  44. }
  45. xhr = null // Help gc