capability.js 2.4KB

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