From 2b407990e22765e76c7d1de0a2808ec31f0691ea Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 22 Feb 2024 13:24:12 +0100 Subject: [PATCH] Use the Fetch API to download the l10n files Given that the Fetch API is supported since Node.js 18 we should be able to use it when downloading l10n files, which allows us to simplify the code and to make it fully `async`. --- external/importL10n/locales.mjs | 88 +++++++++++---------------------- 1 file changed, 30 insertions(+), 58 deletions(-) diff --git a/external/importL10n/locales.mjs b/external/importL10n/locales.mjs index 7883e0977..62264d3ff 100644 --- a/external/importL10n/locales.mjs +++ b/external/importL10n/locales.mjs @@ -14,7 +14,6 @@ */ import fs from "fs"; -import https from "https"; import path from "path"; // Fetches all languages that have an *active* translation in mozilla-central. @@ -22,47 +21,34 @@ import path from "path"; const DEFAULT_LOCALE = "en-US"; -const EXCLUDE_LANG_CODES = ["ca-valencia", "ja-JP-mac"]; +const EXCLUDE_LANG_CODES = new Set(["ca-valencia", "ja-JP-mac"]); function normalizeText(s) { return s.replaceAll(/\r\n?/g, "\n").replaceAll("\uFEFF", ""); } -function downloadLanguageCodes() { +async function downloadLanguageCodes() { console.log("Downloading language codes...\n"); const ALL_LOCALES = "https://hg.mozilla.org/mozilla-central/raw-file/tip/browser/locales/all-locales"; - return new Promise(function (resolve) { - https.get(ALL_LOCALES, function (response) { - if (response.statusCode === 200) { - let content = ""; - response.setEncoding("utf8"); - response.on("data", function (chunk) { - content += chunk; - }); - response.on("end", function () { - content = content.trim(); // Remove any leading/trailing white-space. - const langCodes = normalizeText(content).split("\n"); - // Remove all locales that we don't want to download below. - for (const langCode of [DEFAULT_LOCALE, ...EXCLUDE_LANG_CODES]) { - const i = langCodes.indexOf(langCode); - if (i > -1) { - langCodes.splice(i, 1); - } - } - resolve(langCodes); - }); - } else { - resolve([]); - } - }); - }); + const response = await fetch(ALL_LOCALES); + if (!response.ok) { + throw new Error(response.statusText); + } + const content = await response.text(); + + // Remove any leading/trailing white-space. + const langCodes = normalizeText(content.trim()).split("\n"); + // Remove all locales that we don't want to download below. + return langCodes.filter( + langCode => langCode !== DEFAULT_LOCALE && !EXCLUDE_LANG_CODES.has(langCode) + ); } -function downloadLanguageFiles(root, langCode) { - console.log("Downloading " + langCode + "..."); +async function downloadLanguageFiles(root, langCode) { + console.log(`Downloading ${langCode}...`); // Constants for constructing the URLs. Translations are taken from the // Nightly channel as those are the most recent ones. @@ -71,41 +57,27 @@ function downloadLanguageFiles(root, langCode) { // Defines which files to download for each language. const files = ["viewer.ftl"]; - let downloadsLeft = files.length; const outputDir = path.join(root, langCode); if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir); } - return new Promise(function (resolve) { - // Download the necessary files for this language. - files.forEach(function (fileName) { - const outputPath = path.join(outputDir, fileName); - const url = - MOZ_CENTRAL_ROOT + langCode + MOZ_CENTRAL_PDFJS_DIR + fileName; + // Download the necessary files for this language. + for (const fileName of files) { + const outputPath = path.join(outputDir, fileName); + const url = MOZ_CENTRAL_ROOT + langCode + MOZ_CENTRAL_PDFJS_DIR + fileName; - https.get(url, function (response) { - // Not all files exist for each language. Files without translations - // have been removed (https://bugzilla.mozilla.org/show_bug.cgi?id=1443175). - if (response.statusCode === 200) { - let content = ""; - response.setEncoding("utf8"); - response.on("data", function (chunk) { - content += chunk; - }); - response.on("end", function () { - fs.writeFileSync(outputPath, normalizeText(content), "utf8"); - if (--downloadsLeft === 0) { - resolve(); - } - }); - } else if (--downloadsLeft === 0) { - resolve(); - } - }); - }); - }); + const response = await fetch(url); + if (!response.ok) { + // Not all files exist for each language. Files without translations + // have been removed (https://bugzilla.mozilla.org/show_bug.cgi?id=1443175). + continue; + } + const content = await response.text(); + + fs.writeFileSync(outputPath, normalizeText(content), "utf8"); + } } async function downloadL10n(root) {