|
| 1 | +import _ from 'lodash'; |
| 2 | +import path from 'path'; |
| 3 | +import {logger} from '@appium/support'; |
| 4 | +import semver from 'semver'; |
| 5 | +import {ARCH, CPU} from '../constants'; |
| 6 | + |
| 7 | +const log = logger.getLogger('ChromedriverChromelabsStorageClient'); |
| 8 | + |
| 9 | +/** |
| 10 | + * Parses The output of the corresponding JSON API |
| 11 | + * that retrieves Chromedriver versions. See |
| 12 | + * https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints |
| 13 | + * for more details. |
| 14 | + * |
| 15 | + * @param {string} jsonStr |
| 16 | + * @returns {ChromedriverDetailsMapping} |
| 17 | + */ |
| 18 | +export function parseKnownGoodVersionsWithDownloadsJson(jsonStr) { |
| 19 | + let json; |
| 20 | + try { |
| 21 | + json = JSON.parse(jsonStr); |
| 22 | + } catch (e) { |
| 23 | + const err = /** @type {Error} */ (e); |
| 24 | + throw new Error(`Storage JSON cannot be parsed. Original error: ${err.message}`); |
| 25 | + } |
| 26 | + /** |
| 27 | + * Example output: |
| 28 | + * { |
| 29 | + * "timestamp":"2023-07-28T13:09:17.042Z", |
| 30 | + * "versions":[ |
| 31 | + * { |
| 32 | + * "version":"113.0.5672.0", |
| 33 | + * "revision":"1121455", |
| 34 | + * "downloads":{ |
| 35 | + * "chromedriver":[ |
| 36 | + * { |
| 37 | + * "platform":"linux64", |
| 38 | + * "url":"https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/113.0.5672.0/linux64/chrome-linux64.zip" |
| 39 | + * }, |
| 40 | + * { |
| 41 | + * "platform":"mac-arm64", |
| 42 | + * "url":"https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/113.0.5672.0/mac-arm64/chrome-mac-arm64.zip" |
| 43 | + * }, |
| 44 | + * { |
| 45 | + * "platform":"mac-x64", |
| 46 | + * "url":"https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/113.0.5672.0/mac-x64/chrome-mac-x64.zip" |
| 47 | + * }, |
| 48 | + * { |
| 49 | + * "platform":"win32", |
| 50 | + * "url":"https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/113.0.5672.0/win32/chrome-win32.zip" |
| 51 | + * }, |
| 52 | + * { |
| 53 | + * "platform":"win64", |
| 54 | + * "url":"https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/113.0.5672.0/win64/chrome-win64.zip" |
| 55 | + * } |
| 56 | + * ] |
| 57 | + * } |
| 58 | + * }, |
| 59 | + * { |
| 60 | + * "version":"113.0.5672.35", |
| 61 | + * ... |
| 62 | + */ |
| 63 | + /** @type {ChromedriverDetailsMapping} */ |
| 64 | + const mapping = {}; |
| 65 | + if (!_.isArray(json?.versions)) { |
| 66 | + log.debug(jsonStr); |
| 67 | + throw new Error('The format of the storage JSON is not supported'); |
| 68 | + } |
| 69 | + for (const {version, downloads} of json.versions) { |
| 70 | + if (!_.isArray(downloads?.chromedriver)) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + const versionObj = semver.parse(version, {loose: true}); |
| 74 | + if (!versionObj) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + for (const downloadEntry of downloads.chromedriver) { |
| 78 | + if (!downloadEntry?.url || !downloadEntry?.platform) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + const osNameMatch = /^[a-z]+/i.exec(downloadEntry.platform); |
| 82 | + if (!osNameMatch) { |
| 83 | + log.debug(`The entry '${downloadEntry.url}' does not contain valid platform name. Skipping it`); |
| 84 | + continue; |
| 85 | + } |
| 86 | + const key = `${path.basename(path.dirname(path.dirname(downloadEntry.url)))}/` + |
| 87 | + `${path.basename(downloadEntry.url)}`; |
| 88 | + mapping[key] = { |
| 89 | + url: downloadEntry.url, |
| 90 | + etag: null, |
| 91 | + version, |
| 92 | + minBrowserVersion: `${versionObj.major}`, |
| 93 | + os: { |
| 94 | + name: osNameMatch[0], |
| 95 | + arch: downloadEntry.platform.includes(ARCH.X64) ? ARCH.X64 : ARCH.X86, |
| 96 | + cpu: downloadEntry.platform.includes(CPU.ARM) ? CPU.ARM : CPU.INTEL, |
| 97 | + } |
| 98 | + }; |
| 99 | + } |
| 100 | + } |
| 101 | + log.info(`The total count of entries in the mapping: ${_.size(mapping)}`); |
| 102 | + return mapping; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Parses The output of the corresponding JSON API |
| 107 | + * that retrieves the most recent stable Chromedriver version. See |
| 108 | + * https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints |
| 109 | + * for more details. |
| 110 | + * |
| 111 | + * @param {string} jsonStr |
| 112 | + * @returns {string} The most recent available chromedriver version |
| 113 | + */ |
| 114 | +export function parseLatestKnownGoodVersionsJson(jsonStr) { |
| 115 | + let json; |
| 116 | + try { |
| 117 | + json = JSON.parse(jsonStr); |
| 118 | + } catch (e) { |
| 119 | + const err = /** @type {Error} */ (e); |
| 120 | + throw new Error(`Storage JSON cannot be parsed. Original error: ${err.message}`); |
| 121 | + } |
| 122 | + /** |
| 123 | + * Example output: |
| 124 | + * "timestamp":"2023-07-28T13:09:17.036Z", |
| 125 | + * "channels":{ |
| 126 | + * "Stable":{ |
| 127 | + * "channel":"Stable", |
| 128 | + * "version":"115.0.5790.102", |
| 129 | + * "revision":"1148114" |
| 130 | + * ... |
| 131 | + */ |
| 132 | + if (!json?.channels?.Stable?.version) { |
| 133 | + log.debug(jsonStr); |
| 134 | + throw new Error('The format of the storage JSON is not supported'); |
| 135 | + } |
| 136 | + return json.channels.Stable.version; |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * @typedef {import('../types').ChromedriverDetailsMapping} ChromedriverDetailsMapping |
| 141 | + */ |
0 commit comments