capability.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 && checkTypeSupport('moz-chunked-arraybuffer');
  52. // If fetch is supported, then overrideMimeType will be supported too. Skip calling
  53. // getXHR().
  54. exports.overrideMimeType = exports.fetch || (getXHR() ? isFunction(getXHR().overrideMimeType) : false);
  55. exports.vbArray = isFunction(global.VBArray);
  56. function isFunction(value) {
  57. return typeof value === 'function';
  58. }
  59. xhr = null; // Help gc