Fix not building in some cases and wrongly following download redirects

This commit is contained in:
Christian Koop 2020-11-24 14:53:41 +01:00
parent 69c7fb8c2f
commit c06cf8b5d3
No known key found for this signature in database
GPG Key ID: DECCA4CEE0E46D6D
4 changed files with 46 additions and 39 deletions

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -48,7 +48,7 @@ async function run(): Promise<{ code: number, msg?: string }> {
console.log('Installed Java-Version:'); console.log('Installed Java-Version:');
await runCmd('java', ['-version'], workingDir.base, appLogFile); await runCmd('java', ['-version'], workingDir.base, appLogFile);
console.log(`Downloading '${buildTool.url}'...`); console.log(`\nDownloading '${buildTool.url}'...`);
await downloadFile(buildTool.url, joinPath(workingDir.cache, 'BuildTools.jar')); await downloadFile(buildTool.url, joinPath(workingDir.cache, 'BuildTools.jar'));
const gotTemplateDirectory = versions.length != 1; const gotTemplateDirectory = versions.length != 1;
@ -90,8 +90,8 @@ async function run(): Promise<{ code: number, msg?: string }> {
const tasks = []; const tasks = [];
for (const ver of versions) { for (const ver of versions) {
tasks.push((callback: (err?: Error, result?: unknown) => void) => { tasks.push(async (): Promise<void> => {
try { return new Promise(async (resolveTask, rejectTask): Promise<void> => {
const start = Date.now(); const start = Date.now();
const logFile = joinPath(workingDir.logs, `${ver}.log`); const logFile = joinPath(workingDir.logs, `${ver}.log`);
@ -102,20 +102,22 @@ async function run(): Promise<{ code: number, msg?: string }> {
const versionDir = gotTemplateDirectory ? joinPath(workingDir.base, `${ver}`) : workingDir.cache; const versionDir = gotTemplateDirectory ? joinPath(workingDir.base, `${ver}`) : workingDir.cache;
if (gotTemplateDirectory) { if (gotTemplateDirectory) {
copy(workingDir.cache, versionDir) await copy(workingDir.cache, versionDir);
.then(() => { }
runCmd('java', [...buildToolsArgs, '--rev', ver],
versionDir, logFile, true) // set to silent because multiple builds can run at once try {
.then(() => { // set to silent because multiple builds can run at once
await runCmd('java', [...buildToolsArgs, '--rev', ver], versionDir, logFile, true);
if (gotTemplateDirectory) {
rmdirSync(versionDir, {recursive: true}); // delete our task dir rmdirSync(versionDir, {recursive: true}); // delete our task dir
}
const end = Date.now(); const end = Date.now();
console.log(`Finished building '${ver}' in ${((end - start) / 60_000)} minutes`); console.log(`Finished '${ver}' in ${((end - start) / 60_000).toFixed(2)} minutes`);
callback(); resolveTask();
}); } catch (err) {
})
.catch((err) => {
console.log(`An error occurred while building '${ver}'`); console.log(`An error occurred while building '${ver}'`);
console.error(err); console.error(err);
@ -127,20 +129,17 @@ async function run(): Promise<{ code: number, msg?: string }> {
} }
}) })
.catch(console.error) .catch(console.error)
.finally(() => callback(err)); .finally(() => rejectTask(err));
}
}); });
}
} catch (err) {
callback(err);
}
}); });
} }
parallelLimit(tasks, threadCount, ((err, results) => { parallelLimit(tasks, threadCount, (err) => {
if (err) return reject(err); if (err) return reject(err);
resolve({code: 0}); resolve({code: 0});
})); });
} catch (err) { } catch (err) {
reject(err); reject(err);
} }

View File

@ -83,11 +83,19 @@ export async function downloadFile(url: string, dest: string, currRedirectDepth:
} }
}, (httpRes) => { }, (httpRes) => {
if (httpRes.statusCode != 200) { if (httpRes.statusCode != 200) {
const locHeader = httpRes.headers.location;
// Follow redirect // Follow redirect
if (currRedirectDepth < 12 && if (currRedirectDepth < 12 && locHeader &&
(httpRes.statusCode == 301 || httpRes.statusCode == 302 || httpRes.statusCode == 303 || (httpRes.statusCode == 301 || httpRes.statusCode == 302 || httpRes.statusCode == 303 ||
httpRes.statusCode == 307 || httpRes.statusCode == 308)) { httpRes.statusCode == 307 || httpRes.statusCode == 308)) {
return downloadFile(url, dest, ++currRedirectDepth) done(false);
if (!/https?:\/\//g.test(locHeader)) {
return reject(new Error(`Server responded with ${httpRes.statusCode} and a relative Location-Header value (${locHeader})`));
}
return downloadFile(locHeader, dest, ++currRedirectDepth)
.then(resolve) .then(resolve)
.catch(reject); .catch(reject);
} else { } else {