ext-localize 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -0
- package/LICENSE +21 -0
- package/README.md +586 -2
- package/assets/fill-core.js +398 -0
- package/dist/batch.js +25 -0
- package/dist/check.js +392 -0
- package/dist/cli.js +722 -0
- package/dist/config.js +114 -0
- package/dist/glossary.js +71 -0
- package/dist/import.js +89 -0
- package/dist/listing.js +318 -0
- package/dist/locales.js +86 -0
- package/dist/messages.js +123 -0
- package/dist/paths.js +30 -0
- package/dist/placeholders.js +171 -0
- package/dist/plurals.js +63 -0
- package/dist/providers/anthropic.js +60 -0
- package/dist/providers/deepl.js +269 -0
- package/dist/providers/index.js +40 -0
- package/dist/providers/ollama.js +48 -0
- package/dist/providers/openai.js +50 -0
- package/dist/providers/prompt.js +100 -0
- package/dist/providers/types.js +18 -0
- package/dist/retry.js +66 -0
- package/dist/stale.js +152 -0
- package/dist/state.js +237 -0
- package/dist/stores/chrome-auto.js +205 -0
- package/dist/stores/chrome.js +194 -0
- package/dist/stores/edge.js +43 -0
- package/dist/stores/firefox.js +235 -0
- package/dist/translate.js +309 -0
- package/dist/usage.js +3 -0
- package/dist/zip.js +88 -0
- package/package.json +55 -4
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { codePoints } from "../listing.js";
|
|
7
|
+
import { localizerDir } from "../paths.js";
|
|
8
|
+
/**
|
|
9
|
+
* Chrome Web Store listing delivery.
|
|
10
|
+
*
|
|
11
|
+
* Chrome has no API for listing text. Name and summary come from the manifest
|
|
12
|
+
* (_locales). Only the "Detailed description" is typed per language in the
|
|
13
|
+
* dashboard. Default delivery: one script the developer pastes into DevTools on
|
|
14
|
+
* the Store listing tab; it fills every language, and the developer clicks
|
|
15
|
+
* "Save draft". The script never clicks Submit or Publish.
|
|
16
|
+
*/
|
|
17
|
+
/** Dashboard textarea maxlength (seen on the real dashboard; not in Google docs). */
|
|
18
|
+
export const CHROME_DESCRIPTION_MAX = 16000;
|
|
19
|
+
export const DASHBOARD_URL = "https://chrome.google.com/webstore/devconsole/";
|
|
20
|
+
/** fill-core.js ships in assets/ next to src/ and dist/. */
|
|
21
|
+
export function fillCorePath() {
|
|
22
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
23
|
+
return join(here, "..", "..", "assets", "fill-core.js");
|
|
24
|
+
}
|
|
25
|
+
export function readFillCore() {
|
|
26
|
+
return readFileSync(fillCorePath(), "utf8");
|
|
27
|
+
}
|
|
28
|
+
/** First 12 hex digits of SHA-256 over the UTF-8 text (same as fill-core's shortHash). */
|
|
29
|
+
export function shortHash(text) {
|
|
30
|
+
return createHash("sha256").update(text, "utf8").digest("hex").slice(0, 12);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* JSON with every non-ASCII character written as a \uXXXX escape (astral
|
|
34
|
+
* characters as surrogate pairs). The result is pure ASCII, so no clipboard or
|
|
35
|
+
* encoding step between the file and the console can change it.
|
|
36
|
+
*/
|
|
37
|
+
export function asciiJson(value, space) {
|
|
38
|
+
return JSON.stringify(value, null, space).replace(/[\u0080-\uffff]/g, (c) => "\\u" + c.charCodeAt(0).toString(16).padStart(4, "0"));
|
|
39
|
+
}
|
|
40
|
+
export function isAscii(text) {
|
|
41
|
+
return /^[\x00-\x7f]*$/.test(text);
|
|
42
|
+
}
|
|
43
|
+
/** Pick the descriptions to fill; report the ones that cannot be used. */
|
|
44
|
+
export function chromeDescriptions(listings) {
|
|
45
|
+
const descriptions = {};
|
|
46
|
+
const problems = [];
|
|
47
|
+
for (const locale of Object.keys(listings).sort()) {
|
|
48
|
+
const d = listings[locale]?.description;
|
|
49
|
+
if (d === undefined || d.trim() === "") {
|
|
50
|
+
problems.push(`${locale}: no description`);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (codePoints(d) > CHROME_DESCRIPTION_MAX) {
|
|
54
|
+
problems.push(`${locale}: description is ${codePoints(d)} characters; the dashboard limit is ${CHROME_DESCRIPTION_MAX}`);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
// The page normalizes line endings the same way before comparing.
|
|
58
|
+
const text = d.replace(/\r\n/g, "\n");
|
|
59
|
+
descriptions[locale] = { description: text, length: codePoints(text), sha: shortHash(text) };
|
|
60
|
+
}
|
|
61
|
+
return { descriptions, problems };
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Build the paste script. Plain concatenation (no templates over fill-core,
|
|
65
|
+
* which contains "$" and backticks of its own).
|
|
66
|
+
*/
|
|
67
|
+
export function buildPasteScript(descriptions, opts = {}) {
|
|
68
|
+
const script = buildPasteScriptRaw(descriptions, opts);
|
|
69
|
+
if (!isAscii(script))
|
|
70
|
+
throw new Error("internal error: the paste script is not pure ASCII");
|
|
71
|
+
return script;
|
|
72
|
+
}
|
|
73
|
+
function buildPasteScriptRaw(descriptions, opts) {
|
|
74
|
+
const mode = opts.mode ?? "fill-all";
|
|
75
|
+
const save = mode === "save-each" ? "wait" : "none";
|
|
76
|
+
const core = opts.core ?? readFillCore();
|
|
77
|
+
const locales = Object.keys(descriptions);
|
|
78
|
+
return [
|
|
79
|
+
"// ext-localize: paste into the DevTools console on the Chrome Web Store \"Store listing\" tab.",
|
|
80
|
+
`// Fills the detailed description for ${locales.length} language(s): ${locales.join(", ")}.`,
|
|
81
|
+
"// It never clicks Submit for review or Publish. Check the text, then click \"Save draft\" yourself.",
|
|
82
|
+
`// mode=${mode}`,
|
|
83
|
+
"(async () => {",
|
|
84
|
+
core,
|
|
85
|
+
"const LISTINGS = " + asciiJson(descriptions, 2) + ";",
|
|
86
|
+
"window.__listingFillReport = await globalThis.ListingFill.run(LISTINGS, { mode: " +
|
|
87
|
+
JSON.stringify(mode) +
|
|
88
|
+
", save: " +
|
|
89
|
+
JSON.stringify(save) +
|
|
90
|
+
" });",
|
|
91
|
+
"})();",
|
|
92
|
+
"",
|
|
93
|
+
].join("\n");
|
|
94
|
+
}
|
|
95
|
+
/** Read-only script: finds the elements and lists the dashboard languages. Changes nothing. */
|
|
96
|
+
export function buildDiagnoseScript(locales, opts = {}) {
|
|
97
|
+
const core = opts.core ?? readFillCore();
|
|
98
|
+
const script = [
|
|
99
|
+
"// ext-localize: DIAGNOSE (read-only). Paste on the Store listing tab. Changes nothing, saves nothing.",
|
|
100
|
+
"(async () => {",
|
|
101
|
+
core,
|
|
102
|
+
"const L = globalThis.ListingFill, out = { url: location.href, version: L.VERSION };",
|
|
103
|
+
"const tryIt = (k, f) => { try { out[k] = f(); } catch (e) { out[k] = 'ERROR: ' + e.message; } };",
|
|
104
|
+
"tryIt('textarea', () => { const t = L.findTextarea(); return { foundBy: t.how, maxlength: t.el.getAttribute('maxlength'), length: t.el.value.length }; });",
|
|
105
|
+
"try { out.languages = await L.listDashboardLanguages(); } catch (e) { out.languages = 'ERROR: ' + e.message; }",
|
|
106
|
+
"out.localeFiles = " + asciiJson(locales) + ".map((l) => ({ locale: l, dashboardCode: L.toDashboardCode(l) }));",
|
|
107
|
+
"console.log('[listing-fill] DIAGNOSE', JSON.stringify(out, null, 2));",
|
|
108
|
+
"window.__listingFillDiagnose = out;",
|
|
109
|
+
"})();",
|
|
110
|
+
"",
|
|
111
|
+
].join("\n");
|
|
112
|
+
if (!isAscii(script))
|
|
113
|
+
throw new Error("internal error: the diagnose script is not pure ASCII");
|
|
114
|
+
return script;
|
|
115
|
+
}
|
|
116
|
+
/** Output folder for generated files. It gets its own .gitignore. */
|
|
117
|
+
export function outDir(root) {
|
|
118
|
+
const dir = join(localizerDir(root), "out");
|
|
119
|
+
mkdirSync(dir, { recursive: true });
|
|
120
|
+
const gi = join(dir, ".gitignore");
|
|
121
|
+
if (!existsSync(gi))
|
|
122
|
+
writeFileSync(gi, "# Generated by ext-localize. Not meant for git.\n*\n");
|
|
123
|
+
return dir;
|
|
124
|
+
}
|
|
125
|
+
// maxBuffer: scripts with non-Latin text can pass 1 MB (the spawnSync default limit).
|
|
126
|
+
export const defaultRun = (cmd, opts) => spawnSync(cmd, { input: opts.input, env: opts.env, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
|
|
127
|
+
/**
|
|
128
|
+
* Copy text to the macOS clipboard, then read it back and compare.
|
|
129
|
+
* pbcopy decodes its input with the current locale; with no LANG/LC_* set it
|
|
130
|
+
* reads UTF-8 as MacRoman. So both commands run with LC_ALL=en_US.UTF-8.
|
|
131
|
+
* Returns an error message, or null when the clipboard holds exactly `text`.
|
|
132
|
+
*/
|
|
133
|
+
export function copyToClipboard(text, run = defaultRun, platform = process.platform) {
|
|
134
|
+
if (platform !== "darwin")
|
|
135
|
+
return "copying is only supported on macOS (pbcopy); open the file instead";
|
|
136
|
+
const env = { ...process.env, LC_ALL: "en_US.UTF-8", LANG: "en_US.UTF-8" };
|
|
137
|
+
const w = run("pbcopy", { input: text, env });
|
|
138
|
+
if (w.error)
|
|
139
|
+
return `pbcopy failed: ${w.error.message}`;
|
|
140
|
+
if (w.status !== 0)
|
|
141
|
+
return `pbcopy exited with ${w.status}`;
|
|
142
|
+
const r = run("pbpaste", { env });
|
|
143
|
+
if (r.error)
|
|
144
|
+
return `pbpaste failed: ${r.error.message}`;
|
|
145
|
+
if (r.status !== 0)
|
|
146
|
+
return `pbpaste exited with ${r.status}`;
|
|
147
|
+
const back = String(r.stdout ?? "");
|
|
148
|
+
if (back !== text) {
|
|
149
|
+
return `the clipboard does not hold the script (read back ${back.length} characters, expected ${text.length}). Do not paste it; open the file and copy it by hand`;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
// Guards shared with --auto
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
const DASHBOARD_HOSTS = new Set(["chrome.google.com", "chromewebstore.google.com"]);
|
|
157
|
+
const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
158
|
+
/**
|
|
159
|
+
* Item id of a dashboard URL, or null. Only the dashboard hosts count (and
|
|
160
|
+
* local pages, used by the tests with a mock dashboard).
|
|
161
|
+
* Example: https://chrome.google.com/webstore/devconsole/<publisher>/<item>/edit
|
|
162
|
+
*/
|
|
163
|
+
export function itemFromUrl(url) {
|
|
164
|
+
let u;
|
|
165
|
+
try {
|
|
166
|
+
u = new URL(url);
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
if (!DASHBOARD_HOSTS.has(u.hostname) && !LOCAL_HOSTS.has(u.hostname))
|
|
172
|
+
return null;
|
|
173
|
+
const m = /\/devconsole\/[^/]+\/([a-p]{32})(?:\/|$)/.exec(u.pathname);
|
|
174
|
+
return m ? m[1] : null;
|
|
175
|
+
}
|
|
176
|
+
export function isItemId(id) {
|
|
177
|
+
return /^[a-p]{32}$/.test(id);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Decide whether --auto may click "Save draft".
|
|
181
|
+
* - --item is always required, and the open tab must be that item.
|
|
182
|
+
* - Saving is allowed on items listed in chrome.testItems (config). Any other
|
|
183
|
+
* item may be live, so it also needs --allow-live.
|
|
184
|
+
*/
|
|
185
|
+
export function checkSaveAllowed(tabItem, a) {
|
|
186
|
+
if (!a.item)
|
|
187
|
+
throw new Error("Refusing: pass --item <itemId> so only the item you named is touched.");
|
|
188
|
+
if (tabItem !== a.item)
|
|
189
|
+
throw new Error(`Refusing: the open tab is item ${tabItem ?? "(none)"}, not --item ${a.item}.`);
|
|
190
|
+
if (a.save && !(a.testItems ?? []).includes(a.item) && !a.allowLive) {
|
|
191
|
+
throw new Error(`Refusing to save: ${a.item} is not in chrome.testItems in .localizer/config.json, so it may be a live item. ` +
|
|
192
|
+
`Use a test item, or pass --allow-live after you decide to.`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { EDGE_DESCRIPTION_MIN, STORE_LIMITS, codePoints } from "../listing.js";
|
|
4
|
+
/**
|
|
5
|
+
* Microsoft Edge Add-ons: there is no API for listing text. The Edge Add-ons
|
|
6
|
+
* REST API only uploads and publishes packages; the docs say metadata such as
|
|
7
|
+
* the description must be edited in Partner Center.
|
|
8
|
+
*
|
|
9
|
+
* In Partner Center the extension name and short description are read from the
|
|
10
|
+
* package manifest (_locales). Only the Description (250 to 10,000 characters)
|
|
11
|
+
* and optional search terms are typed per language. So this command writes one
|
|
12
|
+
* text file per locale with the description, ready to copy.
|
|
13
|
+
* Source: https://learn.microsoft.com/en-us/microsoft-edge/extensions/publish/publish-extension
|
|
14
|
+
*/
|
|
15
|
+
export const EDGE_DESCRIPTION_MAX = STORE_LIMITS.edge.description;
|
|
16
|
+
/** Write <dir>/<locale>.txt per listing; return what was written and what has problems. */
|
|
17
|
+
export function writeEdgeFiles(listings, dir) {
|
|
18
|
+
mkdirSync(dir, { recursive: true });
|
|
19
|
+
const out = [];
|
|
20
|
+
for (const locale of Object.keys(listings).sort()) {
|
|
21
|
+
const d = listings[locale]?.description ?? "";
|
|
22
|
+
const length = codePoints(d);
|
|
23
|
+
const entry = { locale, length };
|
|
24
|
+
if (d.trim() === "")
|
|
25
|
+
entry.problem = "no description";
|
|
26
|
+
else if (length < EDGE_DESCRIPTION_MIN)
|
|
27
|
+
entry.problem = `too short: Partner Center needs at least ${EDGE_DESCRIPTION_MIN} characters`;
|
|
28
|
+
else if (length > EDGE_DESCRIPTION_MAX)
|
|
29
|
+
entry.problem = `too long: Partner Center allows at most ${EDGE_DESCRIPTION_MAX} characters`;
|
|
30
|
+
if (d.trim() !== "") {
|
|
31
|
+
const file = join(dir, `${locale}.txt`);
|
|
32
|
+
writeFileSync(file, d.endsWith("\n") ? d : d + "\n");
|
|
33
|
+
entry.file = file;
|
|
34
|
+
}
|
|
35
|
+
out.push(entry);
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/** Copy-ready block for one locale. */
|
|
40
|
+
export function edgeBlock(locale, listing) {
|
|
41
|
+
const line = "=".repeat(60);
|
|
42
|
+
return `${line}\n${locale} - Description (${codePoints(listing.description ?? "")} characters)\n${line}\n${listing.description ?? ""}\n`;
|
|
43
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// Firefox Add-ons (AMO): push the translated listing with one PATCH.
|
|
2
|
+
// Ported from spikes/amo-push (locales.ts, payload.ts, jwt.ts); see its REPORT.md for sources.
|
|
3
|
+
//
|
|
4
|
+
// - PATCH /api/v5/addons/addon/<id|slug|guid>/ with {name, summary, description} as locale -> text maps.
|
|
5
|
+
// Locales not sent are left as they are; null deletes a locale.
|
|
6
|
+
// - Auth: "Authorization: JWT <token>", HS256, iss = API key, exp at most 5 minutes after iat.
|
|
7
|
+
// https://addons-server.readthedocs.io/en/latest/topics/api/auth.html
|
|
8
|
+
import { createHmac, randomUUID } from 'node:crypto';
|
|
9
|
+
import { LISTING_FIELDS } from '../listing.js';
|
|
10
|
+
// Chrome -> AMO locale mapping.
|
|
11
|
+
//
|
|
12
|
+
// Sources (checked 2026-11-16):
|
|
13
|
+
// - Chrome Web Store locales: https://developer.chrome.com/docs/extensions/reference/api/i18n#locales
|
|
14
|
+
// - AMO locales: mozilla/addons-server src/olympia/core/languages.py (PROD_LANGUAGES),
|
|
15
|
+
// used as AMO_LANGUAGES by src/olympia/conf/prod/settings.py.
|
|
16
|
+
// Commit e9274a547adb34545aee8fc8fa53aa27394d8a40 (2026-11-13).
|
|
17
|
+
// A PATCH with a locale key not in this list is rejected by
|
|
18
|
+
// TranslationSerializerField ("The language code ... is invalid.").
|
|
19
|
+
// Mozilla changes this list when translation completeness changes,
|
|
20
|
+
// so callers can pass their own list.
|
|
21
|
+
/** Locales AMO production accepts in translated fields (PROD_LANGUAGES, 44 codes). */
|
|
22
|
+
export const AMO_PROD_LOCALES = [
|
|
23
|
+
'cs', 'de', 'dsb', 'el', 'en-CA', 'en-GB', 'en-US', 'es-AR', 'es-CL', 'es-ES',
|
|
24
|
+
'es-MX', 'fa', 'fi', 'fr', 'fur', 'fy-NL', 'he', 'hr', 'hsb', 'hu', 'ia', 'it',
|
|
25
|
+
'ja', 'ka', 'kab', 'ko', 'nb-NO', 'nl', 'nn-NO', 'pl', 'pt-BR', 'pt-PT', 'ro',
|
|
26
|
+
'ru', 'sk', 'sl', 'sq', 'sr', 'sv-SE', 'tr', 'uk', 'vi', 'zh-CN', 'zh-TW',
|
|
27
|
+
];
|
|
28
|
+
/**
|
|
29
|
+
* Codes that cannot be mapped by replacing "_" with "-".
|
|
30
|
+
* "addons-server" = SHORTER_LANGUAGES in src/olympia/lib/settings_base.py.
|
|
31
|
+
* "our choice" = a decision of this project, not from Mozilla.
|
|
32
|
+
*/
|
|
33
|
+
const ALIASES = {
|
|
34
|
+
en: { amo: ['en-US'], rule: 'alias', source: 'addons-server' },
|
|
35
|
+
es: { amo: ['es-ES'], rule: 'alias', source: 'addons-server' },
|
|
36
|
+
ga: { amo: ['ga-IE'], rule: 'alias', source: 'addons-server' },
|
|
37
|
+
pt: { amo: ['pt-PT'], rule: 'alias', source: 'addons-server' },
|
|
38
|
+
sv: { amo: ['sv-SE'], rule: 'alias', source: 'addons-server' },
|
|
39
|
+
zh: { amo: ['zh-CN'], rule: 'alias', source: 'addons-server' },
|
|
40
|
+
// Chrome "no" is Norwegian Bokmal in practice.
|
|
41
|
+
no: { amo: ['nb-NO'], rule: 'alias', source: 'our choice' },
|
|
42
|
+
// AMO falls back to the add-on default locale, not to another Spanish,
|
|
43
|
+
// so we copy Latin American Spanish to every Latin American locale AMO has.
|
|
44
|
+
es_419: { amo: ['es-AR', 'es-CL', 'es-MX'], rule: 'fan-out', source: 'our choice' },
|
|
45
|
+
};
|
|
46
|
+
/** Why some Chrome codes are skipped. Shown to the user as a warning. */
|
|
47
|
+
const SKIP_NOTES = {
|
|
48
|
+
fil: 'AMO knows "tl" (Tagalog) but it is not enabled on production. Not folded into another locale.',
|
|
49
|
+
en_AU: 'AMO knows "en-AU" but it is not enabled on production. Not folded into en-US/en-GB.',
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Map one Chrome locale code (e.g. "pt_BR") to AMO locale codes (e.g. ["pt-BR"]).
|
|
53
|
+
* `supported` is the list of AMO locales the target server accepts.
|
|
54
|
+
*/
|
|
55
|
+
export function mapChromeLocale(chrome, supported = AMO_PROD_LOCALES) {
|
|
56
|
+
const byLower = new Map(supported.map((l) => [l.toLowerCase(), l]));
|
|
57
|
+
const alias = ALIASES[chrome];
|
|
58
|
+
if (alias) {
|
|
59
|
+
// Partial fan-out is fine: send to the targets the server accepts.
|
|
60
|
+
const amo = alias.amo
|
|
61
|
+
.map((l) => byLower.get(l.toLowerCase()))
|
|
62
|
+
.filter((l) => l !== undefined);
|
|
63
|
+
if (amo.length === 0) {
|
|
64
|
+
return { ok: false, chrome, reason: `maps to ${alias.amo.join(', ')}, which AMO does not accept` };
|
|
65
|
+
}
|
|
66
|
+
return { ok: true, chrome, amo, rule: alias.rule, source: alias.source, priority: 1 };
|
|
67
|
+
}
|
|
68
|
+
const candidate = chrome.replace(/_/g, '-').toLowerCase();
|
|
69
|
+
const canonical = byLower.get(candidate);
|
|
70
|
+
if (canonical) {
|
|
71
|
+
return { ok: true, chrome, amo: [canonical], rule: 'direct', source: 'same code', priority: 2 };
|
|
72
|
+
}
|
|
73
|
+
const reason = SKIP_NOTES[chrome] ?? `AMO does not accept "${chrome.replace(/_/g, '-')}"`;
|
|
74
|
+
return { ok: false, chrome, reason };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Resolve a locale given on the command line (for --remove).
|
|
78
|
+
* Accepts an AMO code ("pt-BR") or a Chrome code ("pt_BR").
|
|
79
|
+
*/
|
|
80
|
+
export function resolveLocaleArg(arg, supported = AMO_PROD_LOCALES) {
|
|
81
|
+
const exact = supported.find((l) => l.toLowerCase() === arg.toLowerCase());
|
|
82
|
+
if (exact)
|
|
83
|
+
return [exact];
|
|
84
|
+
const mapped = mapChromeLocale(arg, supported);
|
|
85
|
+
return mapped.ok ? mapped.amo : [];
|
|
86
|
+
}
|
|
87
|
+
// Build the JSON body for PATCH /api/v5/addons/addon/<id>/
|
|
88
|
+
//
|
|
89
|
+
// Translated field format (addons-server docs, "Translated Fields"):
|
|
90
|
+
// { "name": { "en-US": "...", "de": "..." } }
|
|
91
|
+
// Locales not in the object are left as they are. A null value deletes that locale.
|
|
92
|
+
// The add-on default locale value cannot be deleted while other locales exist.
|
|
93
|
+
const FIELDS = LISTING_FIELDS;
|
|
94
|
+
/**
|
|
95
|
+
* Max lengths from the Addon model in addons-server (src/olympia/addons/models.py):
|
|
96
|
+
* name = TranslatedField(max_length=50)
|
|
97
|
+
* summary = NoURLsField(max_length=250)
|
|
98
|
+
* description = PurifiedMarkdownField(max_length=15000)
|
|
99
|
+
* The API returns 400 above these (addons-server tests test_fields_max_length,
|
|
100
|
+
* test_set_data_too_long_other_textfields). We check them here too, so a bad file
|
|
101
|
+
* fails before any request.
|
|
102
|
+
*/
|
|
103
|
+
export const AMO_LIMITS = { name: 50, summary: 250, description: 15000 };
|
|
104
|
+
/** Length in Unicode code points. Python (the AMO server) counts this way, not in UTF-16 units. */
|
|
105
|
+
export function codePointLength(s) {
|
|
106
|
+
return [...s].length;
|
|
107
|
+
}
|
|
108
|
+
export function buildPatchBody(listings, options = {}) {
|
|
109
|
+
const supported = options.supported ?? AMO_PROD_LOCALES;
|
|
110
|
+
const defaultLocale = options.defaultLocale ?? 'en-US';
|
|
111
|
+
const errors = [];
|
|
112
|
+
const warnings = [];
|
|
113
|
+
// 1. Pick the Chrome file for each AMO locale. A more specific file wins
|
|
114
|
+
// (e.g. en_US beats en for en-US). Equal priority is an error.
|
|
115
|
+
const chosen = new Map();
|
|
116
|
+
for (const chrome of Object.keys(listings).sort()) {
|
|
117
|
+
const m = mapChromeLocale(chrome, supported);
|
|
118
|
+
if (!m.ok) {
|
|
119
|
+
warnings.push(`skip ${chrome}: ${m.reason}`);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
for (const amo of m.amo) {
|
|
123
|
+
const prev = chosen.get(amo);
|
|
124
|
+
if (!prev || m.priority > prev.priority) {
|
|
125
|
+
if (prev)
|
|
126
|
+
warnings.push(`${amo}: using ${chrome}, not the less specific ${prev.chrome}`);
|
|
127
|
+
chosen.set(amo, { chrome, priority: m.priority });
|
|
128
|
+
}
|
|
129
|
+
else if (m.priority === prev.priority) {
|
|
130
|
+
errors.push(`${chrome} and ${prev.chrome} both map to ${amo}; keep only one`);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
warnings.push(`${amo}: using ${prev.chrome}, not the less specific ${chrome}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// 2. Check each used file once, then fill the body.
|
|
138
|
+
const valid = new Map();
|
|
139
|
+
for (const chrome of new Set([...chosen.values()].map((c) => c.chrome))) {
|
|
140
|
+
const listing = listings[chrome] ?? {};
|
|
141
|
+
const ok = {};
|
|
142
|
+
for (const field of FIELDS) {
|
|
143
|
+
const value = listing[field];
|
|
144
|
+
if (value === undefined)
|
|
145
|
+
continue;
|
|
146
|
+
if (typeof value !== 'string') {
|
|
147
|
+
errors.push(`${chrome}.${field}: must be a string`);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (value.trim() === '') {
|
|
151
|
+
errors.push(`${chrome}.${field}: empty; remove the key to leave it unchanged`);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const len = codePointLength(value);
|
|
155
|
+
if (len > AMO_LIMITS[field]) {
|
|
156
|
+
errors.push(`${chrome}.${field}: ${len} characters, limit is ${AMO_LIMITS[field]}`);
|
|
157
|
+
}
|
|
158
|
+
ok[field] = value;
|
|
159
|
+
}
|
|
160
|
+
valid.set(chrome, ok);
|
|
161
|
+
}
|
|
162
|
+
const body = {};
|
|
163
|
+
const sources = {};
|
|
164
|
+
for (const [amo, { chrome }] of [...chosen].sort(([a], [b]) => a.localeCompare(b))) {
|
|
165
|
+
sources[amo] = chrome;
|
|
166
|
+
const listing = valid.get(chrome);
|
|
167
|
+
for (const field of FIELDS) {
|
|
168
|
+
const value = listing[field];
|
|
169
|
+
if (value !== undefined)
|
|
170
|
+
(body[field] ??= {})[amo] = value;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// 3. Deletions.
|
|
174
|
+
for (const arg of options.remove ?? []) {
|
|
175
|
+
const locales = resolveLocaleArg(arg, supported);
|
|
176
|
+
if (locales.length === 0) {
|
|
177
|
+
errors.push(`--remove ${arg}: not a locale AMO accepts (the API rejects unknown locale keys, even with null)`);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
for (const amo of locales) {
|
|
181
|
+
if (amo.toLowerCase() === defaultLocale.toLowerCase()) {
|
|
182
|
+
errors.push(`--remove ${arg}: ${amo} is the default locale; AMO does not allow deleting it`);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (chosen.has(amo)) {
|
|
186
|
+
errors.push(`--remove ${arg}: ${amo} also has a listing file (${chosen.get(amo).chrome})`);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
for (const field of FIELDS)
|
|
190
|
+
(body[field] ??= {})[amo] = null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (body.name || body.summary) {
|
|
194
|
+
warnings.push('name/summary changes start an AMO content review (addons-server AddonSerializer.update)');
|
|
195
|
+
}
|
|
196
|
+
if (Object.keys(body).length === 0) {
|
|
197
|
+
errors.push('nothing to send');
|
|
198
|
+
}
|
|
199
|
+
return { body, errors, warnings, sources };
|
|
200
|
+
}
|
|
201
|
+
// ---- JWT ----
|
|
202
|
+
function b64url(input) {
|
|
203
|
+
return Buffer.from(input).toString('base64url');
|
|
204
|
+
}
|
|
205
|
+
export function signAmoJwt(issuer, secret, opts = {}) {
|
|
206
|
+
const iat = Math.floor((opts.now ?? Date.now()) / 1000);
|
|
207
|
+
const ttl = Math.min(opts.ttlSeconds ?? 60, 300);
|
|
208
|
+
const header = { alg: 'HS256', typ: 'JWT' };
|
|
209
|
+
const payload = { iss: issuer, jti: opts.jti ?? randomUUID(), iat, exp: iat + ttl };
|
|
210
|
+
const unsigned = `${b64url(JSON.stringify(header))}.${b64url(JSON.stringify(payload))}`;
|
|
211
|
+
const sig = createHmac('sha256', secret).update(unsigned).digest('base64url');
|
|
212
|
+
return `${unsigned}.${sig}`;
|
|
213
|
+
}
|
|
214
|
+
export const AMO_HOST = 'https://addons.mozilla.org';
|
|
215
|
+
export function addonUrl(host, addon) {
|
|
216
|
+
return `${host.replace(/\/+$/, '')}/api/v5/addons/addon/${encodeURIComponent(addon)}/`;
|
|
217
|
+
}
|
|
218
|
+
/** AMO code for the add-on default locale, from the Chrome default_locale (en -> en-US). */
|
|
219
|
+
export function amoDefaultLocale(chromeDefault, supported = AMO_PROD_LOCALES) {
|
|
220
|
+
const m = mapChromeLocale(chromeDefault, supported);
|
|
221
|
+
return m.ok ? (m.amo[0] ?? null) : null;
|
|
222
|
+
}
|
|
223
|
+
/** Send the PATCH. Only called with an explicit --send and both env vars set. */
|
|
224
|
+
export async function sendPatch(url, body, creds, fetchImpl = fetch) {
|
|
225
|
+
const res = await fetchImpl(url, {
|
|
226
|
+
method: 'PATCH',
|
|
227
|
+
headers: {
|
|
228
|
+
Authorization: `JWT ${signAmoJwt(creds.issuer, creds.secret)}`,
|
|
229
|
+
'Content-Type': 'application/json',
|
|
230
|
+
Accept: 'application/json',
|
|
231
|
+
},
|
|
232
|
+
body: JSON.stringify(body),
|
|
233
|
+
});
|
|
234
|
+
return { status: res.status, ok: res.ok, text: await res.text() };
|
|
235
|
+
}
|