chrome_xml.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const semver = require("semver");
  4. const config_1 = require("../config");
  5. const http_utils_1 = require("../http_utils");
  6. const config_source_1 = require("./config_source");
  7. class ChromeXml extends config_source_1.XmlConfigSource {
  8. constructor() {
  9. super('chrome', config_1.Config.cdnUrls()['chrome']);
  10. this.maxVersion = config_1.Config.binaryVersions().maxChrome;
  11. }
  12. getUrl(version) {
  13. if (version === 'latest') {
  14. return this.getLatestChromeDriverVersion();
  15. }
  16. else {
  17. return this.getSpecificChromeDriverVersion(version);
  18. }
  19. }
  20. /**
  21. * Get a list of chrome drivers paths available for the configuration OS type and architecture.
  22. */
  23. getVersionList() {
  24. return this.getXml().then(xml => {
  25. let versionPaths = [];
  26. let osType = this.getOsTypeName();
  27. for (let content of xml.ListBucketResult.Contents) {
  28. let contentKey = content.Key[0];
  29. if (
  30. // Filter for 32-bit devices, make sure x64 is not an option
  31. (this.osarch.includes('64') || !contentKey.includes('64')) &&
  32. // Filter for x86 macs, make sure m1 is not an option
  33. ((this.ostype === 'Darwin' && this.osarch === 'arm64') || !contentKey.includes('m1'))) {
  34. // Filter for only the osType
  35. if (contentKey.includes(osType)) {
  36. versionPaths.push(contentKey);
  37. }
  38. }
  39. }
  40. return versionPaths;
  41. });
  42. }
  43. /**
  44. * Helper method, gets the ostype and gets the name used by the XML
  45. */
  46. getOsTypeName() {
  47. // Get the os type name.
  48. if (this.ostype === 'Darwin') {
  49. return 'mac';
  50. }
  51. else if (this.ostype === 'Windows_NT') {
  52. return 'win';
  53. }
  54. else {
  55. return 'linux';
  56. }
  57. }
  58. /**
  59. * Gets the latest item from the XML.
  60. */
  61. getLatestChromeDriverVersion() {
  62. const latestReleaseUrl = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE';
  63. return http_utils_1.requestBody(latestReleaseUrl).then(latestVersion => {
  64. return this.getSpecificChromeDriverVersion(latestVersion);
  65. });
  66. }
  67. /**
  68. * Gets a specific item from the XML.
  69. */
  70. getSpecificChromeDriverVersion(inputVersion) {
  71. return this.getVersionList().then(list => {
  72. const specificVersion = getValidSemver(inputVersion);
  73. if (specificVersion === '') {
  74. throw new Error(`version ${inputVersion} ChromeDriver does not exist`);
  75. }
  76. let itemFound = '';
  77. for (let item of list) {
  78. // Get a semantic version.
  79. let version = item.split('/')[0];
  80. if (semver.valid(version) == null) {
  81. const lookUpVersion = getValidSemver(version);
  82. if (semver.valid(lookUpVersion)) {
  83. // Check to see if the specified version matches.
  84. if (lookUpVersion === specificVersion) {
  85. // When item found is null, check the os arch
  86. // 64-bit version works OR not 64-bit version and the path does not have '64'
  87. if (itemFound == '') {
  88. if (this.osarch === 'x64' ||
  89. (this.osarch !== 'x64' && !item.includes(this.getOsTypeName() + '64'))) {
  90. itemFound = item;
  91. }
  92. if (this.osarch === 'arm64' && this.ostype === 'Darwin' && item.includes('m1')) {
  93. itemFound = item;
  94. }
  95. }
  96. else if (this.osarch === 'x64') {
  97. // No win64 version exists, so even on x64 we need to look for win32
  98. const osTypeNameAndArch = this.getOsTypeName() + (this.getOsTypeName() === 'win' ? '32' : '64');
  99. if (item.includes(osTypeNameAndArch)) {
  100. itemFound = item;
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. if (itemFound == '') {
  108. return { url: '', version: inputVersion };
  109. }
  110. else {
  111. return { url: config_1.Config.cdnUrls().chrome + itemFound, version: inputVersion };
  112. }
  113. });
  114. }
  115. }
  116. exports.ChromeXml = ChromeXml;
  117. /**
  118. * Chromedriver is the only binary that does not conform to semantic versioning
  119. * and either has too little number of digits or too many. To get this to be in
  120. * semver, we will either add a '.0' at the end or chop off the last set of
  121. * digits. This is so we can compare to find the latest and greatest.
  122. *
  123. * Example:
  124. * 2.46 -> 2.46.0
  125. * 75.0.3770.8 -> 75.0.3770
  126. *
  127. * @param version
  128. */
  129. function getValidSemver(version) {
  130. let lookUpVersion = '';
  131. // This supports downloading 2.46
  132. try {
  133. const oldRegex = /(\d+.\d+)/g;
  134. const exec = oldRegex.exec(version);
  135. if (exec) {
  136. lookUpVersion = exec[1] + '.0';
  137. }
  138. }
  139. catch (_) {
  140. // no-op: is this is not valid, do not throw here.
  141. }
  142. // This supports downloading 74.0.3729.6
  143. try {
  144. const newRegex = /(\d+.\d+.\d+).\d+/g;
  145. const exec = newRegex.exec(version);
  146. if (exec) {
  147. lookUpVersion = exec[1];
  148. }
  149. }
  150. catch (_) {
  151. // no-op: if this does not work, use the other regex pattern.
  152. }
  153. return lookUpVersion;
  154. }
  155. exports.getValidSemver = getValidSemver;
  156. //# sourceMappingURL=chrome_xml.js.map