capability.js 1.9 KB

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