UserAgent.js.flow 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule UserAgent
  8. */
  9. 'use strict';
  10. const UserAgentData = require("./UserAgentData");
  11. const VersionRange = require("./VersionRange");
  12. const mapObject = require("./mapObject");
  13. const memoizeStringOnly = require("./memoizeStringOnly");
  14. /**
  15. * Checks to see whether `name` and `version` satisfy `query`.
  16. *
  17. * @param {string} name Name of the browser, device, engine or platform
  18. * @param {?string} version Version of the browser, engine or platform
  19. * @param {string} query Query of form "Name [range expression]"
  20. * @param {?function} normalizer Optional pre-processor for range expression
  21. * @return {boolean}
  22. */
  23. function compare(name, version, query, normalizer) {
  24. // check for exact match with no version
  25. if (name === query) {
  26. return true;
  27. } // check for non-matching names
  28. if (!query.startsWith(name)) {
  29. return false;
  30. } // full comparison with version
  31. let range = query.slice(name.length);
  32. if (version) {
  33. range = normalizer ? normalizer(range) : range;
  34. return VersionRange.contains(range, version);
  35. }
  36. return false;
  37. }
  38. /**
  39. * Normalizes `version` by stripping any "NT" prefix, but only on the Windows
  40. * platform.
  41. *
  42. * Mimics the stripping performed by the `UserAgentWindowsPlatform` PHP class.
  43. *
  44. * @param {string} version
  45. * @return {string}
  46. */
  47. function normalizePlatformVersion(version) {
  48. if (UserAgentData.platformName === 'Windows') {
  49. return version.replace(/^\s*NT/, '');
  50. }
  51. return version;
  52. }
  53. /**
  54. * Provides client-side access to the authoritative PHP-generated User Agent
  55. * information supplied by the server.
  56. */
  57. const UserAgent = {
  58. /**
  59. * Check if the User Agent browser matches `query`.
  60. *
  61. * `query` should be a string like "Chrome" or "Chrome > 33".
  62. *
  63. * Valid browser names include:
  64. *
  65. * - ACCESS NetFront
  66. * - AOL
  67. * - Amazon Silk
  68. * - Android
  69. * - BlackBerry
  70. * - BlackBerry PlayBook
  71. * - Chrome
  72. * - Chrome for iOS
  73. * - Chrome frame
  74. * - Facebook PHP SDK
  75. * - Facebook for iOS
  76. * - Firefox
  77. * - IE
  78. * - IE Mobile
  79. * - Mobile Safari
  80. * - Motorola Internet Browser
  81. * - Nokia
  82. * - Openwave Mobile Browser
  83. * - Opera
  84. * - Opera Mini
  85. * - Opera Mobile
  86. * - Safari
  87. * - UIWebView
  88. * - Unknown
  89. * - webOS
  90. * - etc...
  91. *
  92. * An authoritative list can be found in the PHP `BrowserDetector` class and
  93. * related classes in the same file (see calls to `new UserAgentBrowser` here:
  94. * https://fburl.com/50728104).
  95. *
  96. * @note Function results are memoized
  97. *
  98. * @param {string} query Query of the form "Name [range expression]"
  99. * @return {boolean}
  100. */
  101. isBrowser(query) {
  102. return compare(UserAgentData.browserName, UserAgentData.browserFullVersion, query);
  103. },
  104. /**
  105. * Check if the User Agent browser uses a 32 or 64 bit architecture.
  106. *
  107. * @note Function results are memoized
  108. *
  109. * @param {string} query Query of the form "32" or "64".
  110. * @return {boolean}
  111. */
  112. isBrowserArchitecture(query) {
  113. return compare(UserAgentData.browserArchitecture, null, query);
  114. },
  115. /**
  116. * Check if the User Agent device matches `query`.
  117. *
  118. * `query` should be a string like "iPhone" or "iPad".
  119. *
  120. * Valid device names include:
  121. *
  122. * - Kindle
  123. * - Kindle Fire
  124. * - Unknown
  125. * - iPad
  126. * - iPhone
  127. * - iPod
  128. * - etc...
  129. *
  130. * An authoritative list can be found in the PHP `DeviceDetector` class and
  131. * related classes in the same file (see calls to `new UserAgentDevice` here:
  132. * https://fburl.com/50728332).
  133. *
  134. * @note Function results are memoized
  135. *
  136. * @param {string} query Query of the form "Name"
  137. * @return {boolean}
  138. */
  139. isDevice(query) {
  140. return compare(UserAgentData.deviceName, null, query);
  141. },
  142. /**
  143. * Check if the User Agent rendering engine matches `query`.
  144. *
  145. * `query` should be a string like "WebKit" or "WebKit >= 537".
  146. *
  147. * Valid engine names include:
  148. *
  149. * - Gecko
  150. * - Presto
  151. * - Trident
  152. * - WebKit
  153. * - etc...
  154. *
  155. * An authoritative list can be found in the PHP `RenderingEngineDetector`
  156. * class related classes in the same file (see calls to `new
  157. * UserAgentRenderingEngine` here: https://fburl.com/50728617).
  158. *
  159. * @note Function results are memoized
  160. *
  161. * @param {string} query Query of the form "Name [range expression]"
  162. * @return {boolean}
  163. */
  164. isEngine(query) {
  165. return compare(UserAgentData.engineName, UserAgentData.engineVersion, query);
  166. },
  167. /**
  168. * Check if the User Agent platform matches `query`.
  169. *
  170. * `query` should be a string like "Windows" or "iOS 5 - 6".
  171. *
  172. * Valid platform names include:
  173. *
  174. * - Android
  175. * - BlackBerry OS
  176. * - Java ME
  177. * - Linux
  178. * - Mac OS X
  179. * - Mac OS X Calendar
  180. * - Mac OS X Internet Account
  181. * - Symbian
  182. * - SymbianOS
  183. * - Windows
  184. * - Windows Mobile
  185. * - Windows Phone
  186. * - iOS
  187. * - iOS Facebook Integration Account
  188. * - iOS Facebook Social Sharing UI
  189. * - webOS
  190. * - Chrome OS
  191. * - etc...
  192. *
  193. * An authoritative list can be found in the PHP `PlatformDetector` class and
  194. * related classes in the same file (see calls to `new UserAgentPlatform`
  195. * here: https://fburl.com/50729226).
  196. *
  197. * @note Function results are memoized
  198. *
  199. * @param {string} query Query of the form "Name [range expression]"
  200. * @return {boolean}
  201. */
  202. isPlatform(query) {
  203. return compare(UserAgentData.platformName, UserAgentData.platformFullVersion, query, normalizePlatformVersion);
  204. },
  205. /**
  206. * Check if the User Agent platform is a 32 or 64 bit architecture.
  207. *
  208. * @note Function results are memoized
  209. *
  210. * @param {string} query Query of the form "32" or "64".
  211. * @return {boolean}
  212. */
  213. isPlatformArchitecture(query) {
  214. return compare(UserAgentData.platformArchitecture, null, query);
  215. }
  216. };
  217. module.exports = mapObject(UserAgent, memoizeStringOnly);