|
1 | 1 | /* eslint-disable no-console */ |
2 | | -const request = require('request'); |
3 | 2 | const fs = require('fs'); |
4 | 3 | const path = require('path'); |
5 | 4 | const Chrome = require('../src/data/Chrome'); |
6 | 5 | console.log('Updating chrome versions...'); |
7 | 6 | const filepath = path.join(__dirname, '../data/browsers-chrome.js'); |
8 | | -const fileStream = fs.createWriteStream(filepath); |
9 | | -const stable = { |
10 | | - desktop: [], |
11 | | - mobile: [], |
12 | | -}; |
13 | | -function comparator(a, b) { |
14 | | - return parseInt(a, 10) - parseInt(b, 10); |
15 | | -} |
16 | 7 |
|
17 | | -request('http://omahaproxy.appspot.com/history', (err, response = {}) => { |
18 | | - if (err) { |
19 | | - return; |
20 | | - } |
21 | | - const omaha = response.body.split('\n'); |
22 | | - omaha.forEach((line) => { |
23 | | - const [os, stability, version] = line.split(','); |
24 | | - if (os === 'mac' && stability === 'stable') { |
25 | | - stable.desktop.push(version.split('.').slice(0, 3).join('.')); |
26 | | - } |
27 | | - if (os === 'android' && stability === 'stable') { |
28 | | - stable.mobile.push(version.split('.').slice(0, 3).join('.')); |
29 | | - } |
30 | | - }); |
31 | | - |
32 | | - stable.desktop.sort(comparator); |
33 | | - stable.mobile.sort(comparator); |
| 8 | +/* OmahaProxy (the previous source for this data) has been shut down; the Chromium Dash |
| 9 | + * release API is the maintained replacement. `num` is capped server-side at 1000, which |
| 10 | + * is enough to cover the full Stable release history for both platforms. */ |
| 11 | +const ENDPOINT = 'https://chromiumdash.appspot.com/fetch_releases?channel=Stable&num=1000&platform='; |
34 | 12 |
|
35 | | - let countNewDesktop = 0; |
36 | | - stable.desktop.forEach((version) => { |
37 | | - if (!Chrome.DESKTOP[version]) { |
38 | | - Chrome.DESKTOP[version] = 'stable'; |
39 | | - countNewDesktop++; |
| 13 | +function compareVersions(a, b) { |
| 14 | + const partsA = a.split('.').map(Number); |
| 15 | + const partsB = b.split('.').map(Number); |
| 16 | + for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) { |
| 17 | + const diff = (partsA[i] || 0) - (partsB[i] || 0); |
| 18 | + if (diff !== 0) { |
| 19 | + return diff; |
40 | 20 | } |
41 | | - }); |
| 21 | + } |
| 22 | + return 0; |
| 23 | +} |
42 | 24 |
|
43 | | - let countNewMobile = 0; |
44 | | - stable.mobile.forEach((version) => { |
45 | | - if (!Chrome.MOBILE[version]) { |
46 | | - Chrome.MOBILE[version] = 'stable'; |
47 | | - countNewMobile++; |
48 | | - } |
49 | | - }); |
| 25 | +function fetchPlatform(platform, callback) { |
| 26 | + const url = ENDPOINT + platform; |
| 27 | + fetch(url) |
| 28 | + .then((response) => { |
| 29 | + if (!response.ok) { |
| 30 | + throw new Error(`${url} responded with HTTP ${response.status}`); |
| 31 | + } |
| 32 | + return response.json(); |
| 33 | + }) |
| 34 | + .then((body) => { |
| 35 | + if (!Array.isArray(body)) { |
| 36 | + throw new Error(`${url} returned an unexpected payload`); |
| 37 | + } |
| 38 | + callback(body.map((release) => release.version.split('.').slice(0, 3).join('.'))); |
| 39 | + }) |
| 40 | + .catch((err) => { |
| 41 | + console.error(`Failed to fetch ${platform} releases: ${err.message}`); |
| 42 | + process.exitCode = 1; |
| 43 | + callback([]); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +fetchPlatform('Mac', (desktopVersions) => { |
| 48 | + fetchPlatform('Android', (mobileVersions) => { |
| 49 | + let countNewDesktop = 0; |
| 50 | + desktopVersions.forEach((version) => { |
| 51 | + if (!Chrome.DESKTOP[version]) { |
| 52 | + Chrome.DESKTOP[version] = 'stable'; |
| 53 | + countNewDesktop++; |
| 54 | + } |
| 55 | + }); |
50 | 56 |
|
51 | | - fileStream.write('module.exports = {\n'); |
52 | | - fileStream.write(' DESKTOP: {\n'); |
53 | | - Object.keys(Chrome.DESKTOP).forEach((version) => |
54 | | - fileStream.write(` '${version}': '${Chrome.DESKTOP[version]}',\n`) |
55 | | - ); |
56 | | - fileStream.write(' },\n'); |
57 | | - fileStream.write(' MOBILE: {\n'); |
58 | | - Object.keys(Chrome.MOBILE).forEach((version) => |
59 | | - fileStream.write(` '${version}': '${Chrome.MOBILE[version]}',\n`) |
60 | | - ); |
61 | | - fileStream.write(' },\n'); |
62 | | - fileStream.write('};\n'); |
| 57 | + let countNewMobile = 0; |
| 58 | + mobileVersions.forEach((version) => { |
| 59 | + if (!Chrome.MOBILE[version]) { |
| 60 | + Chrome.MOBILE[version] = 'stable'; |
| 61 | + countNewMobile++; |
| 62 | + } |
| 63 | + }); |
63 | 64 |
|
64 | | - fileStream.end(); |
| 65 | + const fileStream = fs.createWriteStream(filepath); |
| 66 | + fileStream.write('module.exports = {\n'); |
| 67 | + fileStream.write(' DESKTOP: {\n'); |
| 68 | + Object.keys(Chrome.DESKTOP) |
| 69 | + .sort(compareVersions) |
| 70 | + .forEach((version) => fileStream.write(` '${version}': '${Chrome.DESKTOP[version]}',\n`)); |
| 71 | + fileStream.write(' },\n'); |
| 72 | + fileStream.write(' MOBILE: {\n'); |
| 73 | + Object.keys(Chrome.MOBILE) |
| 74 | + .sort(compareVersions) |
| 75 | + .forEach((version) => fileStream.write(` '${version}': '${Chrome.MOBILE[version]}',\n`)); |
| 76 | + fileStream.write(' },\n'); |
| 77 | + fileStream.write('};\n'); |
| 78 | + fileStream.end(); |
65 | 79 |
|
66 | | - console.log(`Updated chrome with ${countNewDesktop} new stable version for DEKSTOP`); |
67 | | - console.log(`Updated chrome with ${countNewMobile} new stable version for MOBILE`); |
| 80 | + console.log(`Updated chrome with ${countNewDesktop} new stable version for DEKSTOP`); |
| 81 | + console.log(`Updated chrome with ${countNewMobile} new stable version for MOBILE`); |
| 82 | + }); |
68 | 83 | }); |
0 commit comments