chrome.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @license
  3. * Copyright 2023 Google Inc.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. import path from 'path';
  7. import semver from 'semver';
  8. import { getJSON } from '../httpUtil.js';
  9. import { BrowserPlatform, ChromeReleaseChannel } from './types.js';
  10. function folder(platform) {
  11. switch (platform) {
  12. case BrowserPlatform.LINUX:
  13. return 'linux64';
  14. case BrowserPlatform.MAC_ARM:
  15. return 'mac-arm64';
  16. case BrowserPlatform.MAC:
  17. return 'mac-x64';
  18. case BrowserPlatform.WIN32:
  19. return 'win32';
  20. case BrowserPlatform.WIN64:
  21. return 'win64';
  22. }
  23. }
  24. export function resolveDownloadUrl(platform, buildId, baseUrl = 'https://storage.googleapis.com/chrome-for-testing-public') {
  25. return `${baseUrl}/${resolveDownloadPath(platform, buildId).join('/')}`;
  26. }
  27. export function resolveDownloadPath(platform, buildId) {
  28. return [buildId, folder(platform), `chrome-${folder(platform)}.zip`];
  29. }
  30. export function relativeExecutablePath(platform, _buildId) {
  31. switch (platform) {
  32. case BrowserPlatform.MAC:
  33. case BrowserPlatform.MAC_ARM:
  34. return path.join('chrome-' + folder(platform), 'Google Chrome for Testing.app', 'Contents', 'MacOS', 'Google Chrome for Testing');
  35. case BrowserPlatform.LINUX:
  36. return path.join('chrome-linux64', 'chrome');
  37. case BrowserPlatform.WIN32:
  38. case BrowserPlatform.WIN64:
  39. return path.join('chrome-' + folder(platform), 'chrome.exe');
  40. }
  41. }
  42. export async function getLastKnownGoodReleaseForChannel(channel) {
  43. const data = (await getJSON(new URL('https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json')));
  44. for (const channel of Object.keys(data.channels)) {
  45. data.channels[channel.toLowerCase()] = data.channels[channel];
  46. delete data.channels[channel];
  47. }
  48. return data.channels[channel];
  49. }
  50. export async function getLastKnownGoodReleaseForMilestone(milestone) {
  51. const data = (await getJSON(new URL('https://googlechromelabs.github.io/chrome-for-testing/latest-versions-per-milestone.json')));
  52. return data.milestones[milestone];
  53. }
  54. export async function getLastKnownGoodReleaseForBuild(
  55. /**
  56. * @example `112.0.23`,
  57. */
  58. buildPrefix) {
  59. const data = (await getJSON(new URL('https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json')));
  60. return data.builds[buildPrefix];
  61. }
  62. export async function resolveBuildId(channel) {
  63. if (Object.values(ChromeReleaseChannel).includes(channel)) {
  64. return (await getLastKnownGoodReleaseForChannel(channel)).version;
  65. }
  66. if (channel.match(/^\d+$/)) {
  67. // Potentially a milestone.
  68. return (await getLastKnownGoodReleaseForMilestone(channel))?.version;
  69. }
  70. if (channel.match(/^\d+\.\d+\.\d+$/)) {
  71. // Potentially a build prefix without the patch version.
  72. return (await getLastKnownGoodReleaseForBuild(channel))?.version;
  73. }
  74. return;
  75. }
  76. export function resolveSystemExecutablePath(platform, channel) {
  77. switch (platform) {
  78. case BrowserPlatform.WIN64:
  79. case BrowserPlatform.WIN32:
  80. switch (channel) {
  81. case ChromeReleaseChannel.STABLE:
  82. return `${process.env['PROGRAMFILES']}\\Google\\Chrome\\Application\\chrome.exe`;
  83. case ChromeReleaseChannel.BETA:
  84. return `${process.env['PROGRAMFILES']}\\Google\\Chrome Beta\\Application\\chrome.exe`;
  85. case ChromeReleaseChannel.CANARY:
  86. return `${process.env['PROGRAMFILES']}\\Google\\Chrome SxS\\Application\\chrome.exe`;
  87. case ChromeReleaseChannel.DEV:
  88. return `${process.env['PROGRAMFILES']}\\Google\\Chrome Dev\\Application\\chrome.exe`;
  89. }
  90. case BrowserPlatform.MAC_ARM:
  91. case BrowserPlatform.MAC:
  92. switch (channel) {
  93. case ChromeReleaseChannel.STABLE:
  94. return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
  95. case ChromeReleaseChannel.BETA:
  96. return '/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta';
  97. case ChromeReleaseChannel.CANARY:
  98. return '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary';
  99. case ChromeReleaseChannel.DEV:
  100. return '/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome Dev';
  101. }
  102. case BrowserPlatform.LINUX:
  103. switch (channel) {
  104. case ChromeReleaseChannel.STABLE:
  105. return '/opt/google/chrome/chrome';
  106. case ChromeReleaseChannel.BETA:
  107. return '/opt/google/chrome-beta/chrome';
  108. case ChromeReleaseChannel.DEV:
  109. return '/opt/google/chrome-unstable/chrome';
  110. }
  111. }
  112. throw new Error(`Unable to detect browser executable path for '${channel}' on ${platform}.`);
  113. }
  114. export function compareVersions(a, b) {
  115. if (!semver.valid(a)) {
  116. throw new Error(`Version ${a} is not a valid semver version`);
  117. }
  118. if (!semver.valid(b)) {
  119. throw new Error(`Version ${b} is not a valid semver version`);
  120. }
  121. if (semver.gt(a, b)) {
  122. return 1;
  123. }
  124. else if (semver.lt(a, b)) {
  125. return -1;
  126. }
  127. else {
  128. return 0;
  129. }
  130. }
  131. //# sourceMappingURL=chrome.js.map