2014-03-25 05:52:11 +09:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-07-08 19:44:37 +09:00
|
|
|
import crypto from "crypto";
|
|
|
|
import fs from "fs";
|
|
|
|
import http from "http";
|
|
|
|
import https from "https";
|
|
|
|
import { resolve as urlResolve } from "url";
|
2014-03-25 05:52:11 +09:00
|
|
|
|
2017-10-01 00:14:41 +09:00
|
|
|
function rewriteWebArchiveUrl(url) {
|
|
|
|
// Web Archive URLs need to be transformed to add `if_` after the ID.
|
|
|
|
// Without this, an HTML page containing an iframe with the PDF file
|
|
|
|
// will be served instead (issue 8920).
|
2023-12-18 04:37:58 +09:00
|
|
|
const webArchiveRegex =
|
2021-05-16 17:58:34 +09:00
|
|
|
/(^https?:\/\/web\.archive\.org\/web\/)(\d+)(\/https?:\/\/.+)/g;
|
2023-12-18 04:37:58 +09:00
|
|
|
const urlParts = webArchiveRegex.exec(url);
|
2017-10-01 00:14:41 +09:00
|
|
|
if (urlParts) {
|
2023-12-18 04:37:58 +09:00
|
|
|
return `${urlParts[1]}${urlParts[2]}if_${urlParts[3]}`;
|
2017-10-01 00:14:41 +09:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2023-12-18 04:11:23 +09:00
|
|
|
function downloadFile(file, url, redirects = 0) {
|
2017-10-01 00:14:41 +09:00
|
|
|
url = rewriteWebArchiveUrl(url);
|
2023-12-18 04:11:23 +09:00
|
|
|
const protocol = /^https:\/\//.test(url) ? https : http;
|
2017-10-01 00:14:41 +09:00
|
|
|
|
2023-12-18 04:11:23 +09:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
protocol
|
|
|
|
.get(url, async function (response) {
|
|
|
|
if ([301, 302, 307, 308].includes(response.statusCode)) {
|
|
|
|
if (redirects > 10) {
|
|
|
|
reject(new Error("Too many redirects"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const redirectTo = urlResolve(url, response.headers.location);
|
|
|
|
try {
|
|
|
|
await downloadFile(file, redirectTo, ++redirects);
|
|
|
|
resolve();
|
|
|
|
} catch (ex) {
|
|
|
|
reject(ex);
|
|
|
|
}
|
|
|
|
return;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
}
|
2014-03-26 02:24:46 +09:00
|
|
|
|
2023-12-18 04:11:23 +09:00
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
reject(new Error(`HTTP ${response.statusCode}`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stream = fs.createWriteStream(file);
|
|
|
|
stream.on("error", error => reject(error));
|
|
|
|
stream.on("finish", () => {
|
|
|
|
stream.end();
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
response.pipe(stream);
|
|
|
|
})
|
|
|
|
.on("error", error => reject(error));
|
|
|
|
});
|
2014-03-25 05:52:11 +09:00
|
|
|
}
|
|
|
|
|
2023-12-18 04:26:53 +09:00
|
|
|
async function downloadManifestFiles(manifest) {
|
|
|
|
const links = manifest
|
|
|
|
.filter(item => item.link && !fs.existsSync(item.file))
|
|
|
|
.map(item => {
|
|
|
|
let url = fs.readFileSync(`${item.file}.link`).toString();
|
|
|
|
url = url.replace(/\s+$/, "");
|
|
|
|
return { file: item.file, url };
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const { file, url } of links) {
|
|
|
|
console.log(`Downloading ${url} to ${file}...`);
|
2023-12-18 04:11:23 +09:00
|
|
|
try {
|
|
|
|
await downloadFile(file, url);
|
|
|
|
} catch (ex) {
|
|
|
|
console.error(`Error during downloading of ${url}: ${ex}`);
|
|
|
|
fs.writeFileSync(file, ""); // making it empty file
|
|
|
|
fs.writeFileSync(`${file}.error`, ex);
|
|
|
|
}
|
2014-03-25 05:52:11 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 00:22:47 +09:00
|
|
|
function calculateMD5(file) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const hash = crypto.createHash("md5");
|
|
|
|
const stream = fs.createReadStream(file);
|
|
|
|
stream.on("data", data => hash.update(data));
|
|
|
|
stream.on("error", error => reject(error));
|
|
|
|
stream.on("end", () => resolve(hash.digest("hex")));
|
2014-03-25 05:52:11 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-04 00:31:45 +09:00
|
|
|
async function verifyManifestFiles(manifest) {
|
|
|
|
let error = false;
|
|
|
|
|
|
|
|
for (const item of manifest) {
|
|
|
|
if (fs.existsSync(`${item.file}.error`)) {
|
2014-03-25 05:52:11 +09:00
|
|
|
console.error(
|
2023-12-04 00:31:45 +09:00
|
|
|
`WARNING: "${item.file}" was not downloaded; see "${item.file}.error" file.`
|
2014-03-25 05:52:11 +09:00
|
|
|
);
|
|
|
|
error = true;
|
2023-12-04 00:31:45 +09:00
|
|
|
continue;
|
2014-03-25 05:52:11 +09:00
|
|
|
}
|
2023-12-04 00:31:45 +09:00
|
|
|
|
|
|
|
if (item.link && !fs.existsSync(`${item.file}.link`)) {
|
2021-09-09 23:39:57 +09:00
|
|
|
console.error(
|
|
|
|
`WARNING: Unneeded \`"link": true\`-entry for the "${item.id}" test.`
|
|
|
|
);
|
|
|
|
error = true;
|
2023-12-04 00:31:45 +09:00
|
|
|
continue;
|
2021-09-09 23:39:57 +09:00
|
|
|
}
|
2023-12-04 00:22:47 +09:00
|
|
|
|
|
|
|
try {
|
|
|
|
const md5 = await calculateMD5(item.file);
|
|
|
|
if (!item.md5) {
|
2014-03-25 05:52:11 +09:00
|
|
|
console.error(
|
2023-12-04 00:22:47 +09:00
|
|
|
`WARNING: MD5 hash missing for "${item.file}" (computed "${md5}").`
|
2014-03-25 05:52:11 +09:00
|
|
|
);
|
|
|
|
error = true;
|
|
|
|
} else if (md5 !== item.md5) {
|
|
|
|
console.error(
|
2023-12-04 00:22:47 +09:00
|
|
|
`WARNING: MD5 hash mismatch for "${item.file}" (expected "${item.md5}", computed "${md5}").`
|
2014-03-25 05:52:11 +09:00
|
|
|
);
|
|
|
|
error = true;
|
|
|
|
}
|
2023-12-04 00:22:47 +09:00
|
|
|
} catch (ex) {
|
|
|
|
console.log(
|
|
|
|
`WARNING: MD5 hash calculation failed for "${item.file}" ("${ex}").`
|
|
|
|
);
|
|
|
|
error = true;
|
|
|
|
}
|
2023-12-04 00:31:45 +09:00
|
|
|
}
|
2023-12-04 00:22:47 +09:00
|
|
|
|
2023-12-04 00:31:45 +09:00
|
|
|
if (error) {
|
|
|
|
throw new Error("Manifest validation failed");
|
2014-03-25 05:52:11 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 19:44:37 +09:00
|
|
|
export { downloadManifestFiles, verifyManifestFiles };
|