forked from dtolstyi/node-chromium
-
Notifications
You must be signed in to change notification settings - Fork 4
/
write-revisions.js
159 lines (143 loc) · 5.14 KB
/
write-revisions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
const fs = require('fs');
const pathUtils = require('path');
const got = require('got');
const packageJson = require('./package.json');
const config = require('./config');
const utils = require('./utils');
const operationSystems = [
{platform: 'linux', architecture: 'x64'},
{platform: 'win32', architecture: 'x32'},
{platform: 'win32', architecture: 'x64'},
{platform: 'darwin'}
];
const revisionSearchUrl = 'https://omahaproxy.appspot.com/deps.json';
const operationSystemRevisionsPath = pathUtils.join(
__dirname, 'operationSystemRevisions.json'
);
// Parses chromium version from package version, e.g.:
// 72.0.3586-2 -> 72.0.3586.2
// 72.0.3586-2-2 -> 72.0.3586.2
function parseChromiumVersion(packageVersion) {
return packageVersion.split(/\.|-/, 4).join('.');
}
function checkArchiveExists(url) {
return new Promise((resolve, reject) => {
got.stream(url)
.on('response', () => resolve(true))
.on('error', error => {
if (error.statusCode === 404) {
resolve(false);
} else {
reject(error);
}
});
});
}
function getBaseRevision(version) {
return new Promise((resolve, reject) => {
got(
revisionSearchUrl,
{
query: {
version
},
json: true
}
)
.then(response => {
const baseRevision = response.body.chromium_base_position;
console.log(`Base revision: ${baseRevision}`);
resolve(baseRevision);
})
.catch(err => {
console.log('An error occured while trying to get base revision by chromium version', err);
reject(err);
});
});
}
function detectOperationSystemRevision(params) {
const operationSystem = params.operationSystem;
const platform = operationSystem.platform;
const architecture = operationSystem.architecture;
const revision = params.revision;
const attemptNumber = params.attemptNumber;
const logPrefix = `${platform} ${architecture || ''}`;
console.log(`[${logPrefix}] Detect revision, attempt #${attemptNumber}`);
return new Promise((resolve, reject) => {
const urls = utils.getOsCdnUrls({
revision,
platform: operationSystem.platform,
architecture: operationSystem.architecture
});
Promise.all(
urls.map(url => checkArchiveExists(url))
).then(checkArchiveExistsResults => {
const archiveExists = checkArchiveExistsResults.some(result => result);
if (archiveExists) {
console.log(`[${logPrefix}] found archive for revision #${revision}`);
resolve(revision);
} else if (attemptNumber < config.ARCHIVE_DOWNLOAD_ATTEMPTS_COUNT) {
console.log(`[${logPrefix}] archive not found, will try again`);
detectOperationSystemRevision({
operationSystem,
revision: Number(revision) - 1,
attemptNumber: attemptNumber + 1
})
.then(resolve)
.catch(reject);
} else {
throw new Error(`[${logPrefix}] archive cannot be found`);
}
})
.catch(err => reject(err));
});
}
function getOperationSystemRevisions(baseRevision) {
return new Promise((resolve, reject) => {
Promise.all(
operationSystems.map(operationSystem => {
return detectOperationSystemRevision({
operationSystem,
revision: baseRevision,
attemptNumber: 0
});
})
).then(revisions => {
const operationSystemRevisions = operationSystems.map(
(operationSystem, index) => {
return {
platform: operationSystem.platform,
architecture: operationSystem.architecture,
revision: String(revisions[index])
};
}
);
resolve(operationSystemRevisions);
})
.catch(err => reject(err));
});
}
function writeOperationSystemRevisions(operationSystemRevisions) {
return new Promise((resolve, reject) => {
fs.writeFile(
operationSystemRevisionsPath,
JSON.stringify(operationSystemRevisions, null, 4),
error => {
if (error) {
reject(error);
} else {
resolve();
}
}
);
});
}
module.exports = Promise.resolve(parseChromiumVersion(packageJson.version))
.then(chromiumVersion => getBaseRevision(chromiumVersion))
.then(baseRevision => getOperationSystemRevisions(baseRevision))
.then(operationSystemRevisions => {
return writeOperationSystemRevisions(operationSystemRevisions);
})
.then(() => console.log(`Revisions for operation systems wrote to ${operationSystemRevisionsPath}`))
.catch(err => console.log(err.stack));