capability.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableStream)
  2. exports.blobConstructor = false
  3. try {
  4. new Blob([new ArrayBuffer(1)])
  5. exports.blobConstructor = true
  6. } catch (e) {}
  7. // The xhr request to example.com may violate some restrictive CSP configurations,
  8. // so if we're running in a browser that supports `fetch`, avoid calling getXHR()
  9. // and assume support for certain features below.
  10. var xhr
  11. function getXHR () {
  12. // Cache the xhr value
  13. if (xhr !== undefined) return xhr
  14. if (global.XMLHttpRequest) {
  15. xhr = new global.XMLHttpRequest()
  16. // If XDomainRequest is available (ie only, where xhr might not work
  17. // cross domain), use the page location. Otherwise use example.com
  18. // Note: this doesn't actually make an http request.
  19. try {
  20. xhr.open('GET', global.XDomainRequest ? '/' : 'https://example.com')
  21. } catch(e) {
  22. xhr = null
  23. }
  24. } else {
  25. // Service workers don't have XHR
  26. xhr = null
  27. }
  28. return xhr
  29. }
  30. function checkTypeSupport (type) {
  31. var xhr = getXHR()
  32. if (!xhr) return false
  33. try {
  34. xhr.responseType = type
  35. return xhr.responseType === type
  36. } catch (e) {}
  37. return false
  38. }
  39. // For some strange reason, Safari 7.0 reports typeof global.ArrayBuffer === 'object'.
  40. // Safari 7.1 appears to have fixed this bug.
  41. var haveArrayBuffer = typeof global.ArrayBuffer !== 'undefined'
  42. var haveSlice = haveArrayBuffer && isFunction(global.ArrayBuffer.prototype.slice)
  43. // If fetch is supported, then arraybuffer will be supported too. Skip calling
  44. // checkTypeSupport(), since that calls getXHR().
  45. exports.arraybuffer = exports.fetch || (haveArrayBuffer && checkTypeSupport('arraybuffer'))
  46. // These next two tests unavoidably show warnings in Chrome. Since fetch will always
  47. // be used if it's available, just return false for these to avoid the warnings.
  48. exports.msstream = !exports.fetch && haveSlice && checkTypeSupport('ms-stream')
  49. exports.mozchunkedarraybuffer = !exports.fetch && haveArrayBuffer &&
  50. checkTypeSupport('moz-chunked-arraybuffer')
  51. // If fetch is supported, then overrideMimeType will be supported too. Skip calling
  52. // getXHR().
  53. exports.overrideMimeType = exports.fetch || (getXHR() ? isFunction(getXHR().overrideMimeType) : false)
  54. exports.vbArray = isFunction(global.VBArray)
  55. function isFunction (value) {
  56. return typeof value === 'function'
  57. }
  58. xhr = null // Help gc