UserAgent.js.flow 5.9 KB

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