UserAgentData.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. */
  8. /**
  9. * Usage note:
  10. * This module makes a best effort to export the same data we would internally.
  11. * At Facebook we use a server-generated module that does the parsing and
  12. * exports the data for the client to use. We can't rely on a server-side
  13. * implementation in open source so instead we make use of an open source
  14. * library to do the heavy lifting and then make some adjustments as necessary.
  15. * It's likely there will be some differences. Some we can smooth over.
  16. * Others are going to be harder.
  17. */
  18. 'use strict';
  19. var UAParser = require('ua-parser-js');
  20. var UNKNOWN = 'Unknown';
  21. var PLATFORM_MAP = {
  22. 'Mac OS': 'Mac OS X'
  23. };
  24. /**
  25. * Convert from UAParser platform name to what we expect.
  26. */
  27. function convertPlatformName(name) {
  28. return PLATFORM_MAP[name] || name;
  29. }
  30. /**
  31. * Get the version number in parts. This is very naive. We actually get major
  32. * version as a part of UAParser already, which is generally good enough, but
  33. * let's get the minor just in case.
  34. */
  35. function getBrowserVersion(version) {
  36. if (!version) {
  37. return {
  38. major: '',
  39. minor: ''
  40. };
  41. }
  42. var parts = version.split('.');
  43. return {
  44. major: parts[0],
  45. minor: parts[1]
  46. };
  47. }
  48. /**
  49. * Get the UA data fom UAParser and then convert it to the format we're
  50. * expecting for our APIS.
  51. */
  52. var parser = new UAParser();
  53. var results = parser.getResult();
  54. // Do some conversion first.
  55. var browserVersionData = getBrowserVersion(results.browser.version);
  56. var uaData = {
  57. browserArchitecture: results.cpu.architecture || UNKNOWN,
  58. browserFullVersion: results.browser.version || UNKNOWN,
  59. browserMinorVersion: browserVersionData.minor || UNKNOWN,
  60. browserName: results.browser.name || UNKNOWN,
  61. browserVersion: results.browser.major || UNKNOWN,
  62. deviceName: results.device.model || UNKNOWN,
  63. engineName: results.engine.name || UNKNOWN,
  64. engineVersion: results.engine.version || UNKNOWN,
  65. platformArchitecture: results.cpu.architecture || UNKNOWN,
  66. platformName: convertPlatformName(results.os.name) || UNKNOWN,
  67. platformVersion: results.os.version || UNKNOWN,
  68. platformFullVersion: results.os.version || UNKNOWN
  69. };
  70. module.exports = uaData;