features.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const Features = {
  2. /**
  3. * Check whether WebGL is available. Optionally, specify a particular version of WebGL to check for.
  4. *
  5. * @param {number=} [majorVersion=1] The major WebGL version to check for.
  6. * @returns {boolean} If the given major version of WebGL is available.
  7. * @function Engine.isWebGLAvailable
  8. */
  9. isWebGLAvailable: function (majorVersion = 1) {
  10. try {
  11. return !!document.createElement('canvas').getContext(['webgl', 'webgl2'][majorVersion - 1]);
  12. } catch (e) { /* Not available */ }
  13. return false;
  14. },
  15. /**
  16. * Check whether the Fetch API available and supports streaming responses.
  17. *
  18. * @returns {boolean} If the Fetch API is available and supports streaming responses.
  19. * @function Engine.isFetchAvailable
  20. */
  21. isFetchAvailable: function () {
  22. return 'fetch' in window && 'Response' in window && 'body' in window.Response.prototype;
  23. },
  24. /**
  25. * Check whether the engine is running in a Secure Context.
  26. *
  27. * @returns {boolean} If the engine is running in a Secure Context.
  28. * @function Engine.isSecureContext
  29. */
  30. isSecureContext: function () {
  31. return window['isSecureContext'] === true;
  32. },
  33. /**
  34. * Check whether the engine is cross origin isolated.
  35. * This value is dependent on Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy headers sent by the server.
  36. *
  37. * @returns {boolean} If the engine is running in a Secure Context.
  38. * @function Engine.isSecureContext
  39. */
  40. isCrossOriginIsolated: function () {
  41. return window['crossOriginIsolated'] === true;
  42. },
  43. /**
  44. * Check whether SharedBufferArray is available.
  45. *
  46. * Most browsers require the page to be running in a secure context, and the
  47. * the server to provide specific CORS headers for SharedArrayBuffer to be available.
  48. *
  49. * @returns {boolean} If SharedArrayBuffer is available.
  50. * @function Engine.isSharedArrayBufferAvailable
  51. */
  52. isSharedArrayBufferAvailable: function () {
  53. return 'SharedArrayBuffer' in window;
  54. },
  55. /**
  56. * Check whether the AudioContext supports AudioWorkletNodes.
  57. *
  58. * @returns {boolean} If AudioWorkletNode is available.
  59. * @function Engine.isAudioWorkletAvailable
  60. */
  61. isAudioWorkletAvailable: function () {
  62. return 'AudioContext' in window && 'audioWorklet' in AudioContext.prototype;
  63. },
  64. /**
  65. * Return an array of missing required features (as string).
  66. *
  67. * @returns {Array<string>} A list of human-readable missing features.
  68. * @function Engine.getMissingFeatures
  69. * @param {{threads: (boolean|undefined)}} supportedFeatures
  70. */
  71. getMissingFeatures: function (supportedFeatures = {}) {
  72. const {
  73. // Quotes are needed for the Closure compiler.
  74. 'threads': supportsThreads = true,
  75. } = supportedFeatures;
  76. const missing = [];
  77. if (!Features.isWebGLAvailable(2)) {
  78. missing.push('WebGL2 - Check web browser configuration and hardware support');
  79. }
  80. if (!Features.isFetchAvailable()) {
  81. missing.push('Fetch - Check web browser version');
  82. }
  83. if (!Features.isSecureContext()) {
  84. missing.push('Secure Context - Check web server configuration (use HTTPS)');
  85. }
  86. if (supportsThreads) {
  87. if (!Features.isCrossOriginIsolated()) {
  88. missing.push('Cross-Origin Isolation - Check that the web server configuration sends the correct headers.');
  89. }
  90. if (!Features.isSharedArrayBufferAvailable()) {
  91. missing.push('SharedArrayBuffer - Check that the web server configuration sends the correct headers.');
  92. }
  93. }
  94. // Audio is normally optional since we have a dummy fallback.
  95. return missing;
  96. },
  97. };