@puzzle-ui/i18n 0.0.0-stage → 0.16.0
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/README.md +94 -2
- package/ai/instructions.md +141 -0
- package/ai/manifest.json +10 -0
- package/dist/LICENSE +21 -0
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/adapters/next.cjs +11 -0
- package/dist/adapters/next.cjs.map +1 -0
- package/dist/adapters/next.d.cts +7 -0
- package/dist/adapters/next.d.ts +7 -0
- package/dist/adapters/next.js +10 -0
- package/dist/adapters/next.js.map +1 -0
- package/dist/adapters/react-router.cjs +13 -0
- package/dist/adapters/react-router.cjs.map +1 -0
- package/dist/adapters/react-router.d.cts +10 -0
- package/dist/adapters/react-router.d.ts +10 -0
- package/dist/adapters/react-router.js +12 -0
- package/dist/adapters/react-router.js.map +1 -0
- package/dist/adapters/request.cjs +67 -0
- package/dist/adapters/request.cjs.map +1 -0
- package/dist/adapters/request.d.cts +16 -0
- package/dist/adapters/request.d.ts +16 -0
- package/dist/adapters/request.js +65 -0
- package/dist/adapters/request.js.map +1 -0
- package/dist/adapters/tanstack.cjs +13 -0
- package/dist/adapters/tanstack.cjs.map +1 -0
- package/dist/adapters/tanstack.d.cts +10 -0
- package/dist/adapters/tanstack.d.ts +10 -0
- package/dist/adapters/tanstack.js +12 -0
- package/dist/adapters/tanstack.js.map +1 -0
- package/dist/ai/index.cjs +205 -0
- package/dist/ai/index.cjs.map +1 -0
- package/dist/ai/index.d.cts +75 -0
- package/dist/ai/index.d.ts +75 -0
- package/dist/ai/index.js +195 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/ai/review.cjs +319 -0
- package/dist/ai/review.cjs.map +1 -0
- package/dist/ai/review.d.cts +91 -0
- package/dist/ai/review.d.ts +91 -0
- package/dist/ai/review.js +315 -0
- package/dist/ai/review.js.map +1 -0
- package/dist/cli/bin.cjs +10 -0
- package/dist/cli/bin.cjs.map +1 -0
- package/dist/cli/bin.d.cts +1 -0
- package/dist/cli/bin.d.ts +1 -0
- package/dist/cli/bin.js +12 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/index.cjs +310 -0
- package/dist/cli/index.cjs.map +1 -0
- package/dist/cli/index.d.cts +7 -0
- package/dist/cli/index.d.ts +7 -0
- package/dist/cli/index.js +309 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/compiler/index.cjs +218 -0
- package/dist/compiler/index.cjs.map +1 -0
- package/dist/compiler/index.d.cts +18 -0
- package/dist/compiler/index.d.ts +18 -0
- package/dist/compiler/index.js +210 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/compiler/typescript.cjs +251 -0
- package/dist/compiler/typescript.cjs.map +1 -0
- package/dist/compiler/typescript.d.cts +27 -0
- package/dist/compiler/typescript.d.ts +27 -0
- package/dist/compiler/typescript.js +247 -0
- package/dist/compiler/typescript.js.map +1 -0
- package/dist/compiler/vite.cjs +94 -0
- package/dist/compiler/vite.cjs.map +1 -0
- package/dist/compiler/vite.d.cts +13 -0
- package/dist/compiler/vite.d.ts +13 -0
- package/dist/compiler/vite.js +92 -0
- package/dist/compiler/vite.js.map +1 -0
- package/dist/content/index.cjs +156 -0
- package/dist/content/index.cjs.map +1 -0
- package/dist/content/index.d.cts +75 -0
- package/dist/content/index.d.ts +75 -0
- package/dist/content/index.js +151 -0
- package/dist/content/index.js.map +1 -0
- package/dist/content/sanity.cjs +74 -0
- package/dist/content/sanity.cjs.map +1 -0
- package/dist/content/sanity.d.cts +19 -0
- package/dist/content/sanity.d.ts +19 -0
- package/dist/content/sanity.js +73 -0
- package/dist/content/sanity.js.map +1 -0
- package/dist/devtools/index.cjs +77 -0
- package/dist/devtools/index.cjs.map +1 -0
- package/dist/devtools/index.d.cts +29 -0
- package/dist/devtools/index.d.ts +29 -0
- package/dist/devtools/index.js +75 -0
- package/dist/devtools/index.js.map +1 -0
- package/dist/format.cjs +17 -0
- package/dist/format.cjs.map +1 -0
- package/dist/format.d.cts +9 -0
- package/dist/format.d.ts +9 -0
- package/dist/format.js +13 -0
- package/dist/format.js.map +1 -0
- package/dist/index.cjs +20 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/locale.cjs +158 -0
- package/dist/locale.cjs.map +1 -0
- package/dist/locale.d.cts +42 -0
- package/dist/locale.d.ts +42 -0
- package/dist/locale.js +155 -0
- package/dist/locale.js.map +1 -0
- package/dist/react/index.cjs +239 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +105 -0
- package/dist/react/index.d.ts +105 -0
- package/dist/react/index.js +238 -0
- package/dist/react/index.js.map +1 -0
- package/dist/routing/index.cjs +381 -0
- package/dist/routing/index.cjs.map +1 -0
- package/dist/routing/index.d.cts +44 -0
- package/dist/routing/index.d.ts +44 -0
- package/dist/routing/index.js +380 -0
- package/dist/routing/index.js.map +1 -0
- package/dist/runtime.cjs +261 -0
- package/dist/runtime.cjs.map +1 -0
- package/dist/runtime.d.cts +74 -0
- package/dist/runtime.d.ts +74 -0
- package/dist/runtime.js +257 -0
- package/dist/runtime.js.map +1 -0
- package/dist/seo/index.cjs +212 -0
- package/dist/seo/index.cjs.map +1 -0
- package/dist/seo/index.d.cts +52 -0
- package/dist/seo/index.d.ts +52 -0
- package/dist/seo/index.js +209 -0
- package/dist/seo/index.js.map +1 -0
- package/dist/seo/origin.cjs +27 -0
- package/dist/seo/origin.cjs.map +1 -0
- package/dist/seo/origin.js +26 -0
- package/dist/seo/origin.js.map +1 -0
- package/dist/seo/sitemap.cjs +74 -0
- package/dist/seo/sitemap.cjs.map +1 -0
- package/dist/seo/sitemap.d.cts +16 -0
- package/dist/seo/sitemap.d.ts +16 -0
- package/dist/seo/sitemap.js +72 -0
- package/dist/seo/sitemap.js.map +1 -0
- package/dist/sources/index.cjs +227 -0
- package/dist/sources/index.cjs.map +1 -0
- package/dist/sources/index.d.cts +41 -0
- package/dist/sources/index.d.ts +41 -0
- package/dist/sources/index.js +222 -0
- package/dist/sources/index.js.map +1 -0
- package/dist/sources/node.cjs +50 -0
- package/dist/sources/node.cjs.map +1 -0
- package/dist/sources/node.d.cts +5 -0
- package/dist/sources/node.d.ts +5 -0
- package/dist/sources/node.js +49 -0
- package/dist/sources/node.js.map +1 -0
- package/package.json +256 -4
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
// src/sources/index.ts
|
|
3
|
+
var CatalogSourceError = class extends Error {
|
|
4
|
+
constructor(message, options) {
|
|
5
|
+
super(message, options);
|
|
6
|
+
this.name = "CatalogSourceError";
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
const isCatalogObject = (value) => {
|
|
10
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
11
|
+
const prototype = Object.getPrototypeOf(value);
|
|
12
|
+
return prototype === Object.prototype || prototype === null;
|
|
13
|
+
};
|
|
14
|
+
const canonicalize = (locale) => {
|
|
15
|
+
try {
|
|
16
|
+
const [canonical] = Intl.getCanonicalLocales(locale);
|
|
17
|
+
if (!canonical) throw new Error();
|
|
18
|
+
return canonical;
|
|
19
|
+
} catch {
|
|
20
|
+
throw new CatalogSourceError(`Invalid BCP 47 catalog locale "${locale}".`);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const copyCatalog = (value, locale, path = "", ancestors = /* @__PURE__ */ new WeakSet()) => {
|
|
24
|
+
if (!isCatalogObject(value)) throw new CatalogSourceError(`Catalog for "${locale}" at "${path || "<root>"}" must be a plain object.`);
|
|
25
|
+
if (ancestors.has(value)) throw new CatalogSourceError(`Catalog for "${locale}" contains a circular reference at "${path || "<root>"}".`);
|
|
26
|
+
ancestors.add(value);
|
|
27
|
+
const entries = Object.entries(value).map(([key, entry]) => {
|
|
28
|
+
const keyPath = path ? `${path}.${key}` : key;
|
|
29
|
+
if (!key || key.includes(".")) throw new CatalogSourceError(`Catalog for "${locale}" has an invalid key "${key}" at "${path || "<root>"}".`);
|
|
30
|
+
if (typeof entry === "string") return [key, entry];
|
|
31
|
+
return [key, copyCatalog(entry, locale, keyPath, ancestors)];
|
|
32
|
+
});
|
|
33
|
+
ancestors.delete(value);
|
|
34
|
+
return Object.freeze(Object.fromEntries(entries));
|
|
35
|
+
};
|
|
36
|
+
const selectNamespace = (catalog, namespace, locale) => {
|
|
37
|
+
if (!namespace) return catalog;
|
|
38
|
+
let value = catalog;
|
|
39
|
+
for (const part of namespace.split(".")) {
|
|
40
|
+
if (!part || !isCatalogObject(value) || !Object.hasOwn(value, part)) throw new CatalogSourceError(`Catalog namespace "${namespace}" was not found for "${locale}".`);
|
|
41
|
+
value = value[part];
|
|
42
|
+
}
|
|
43
|
+
if (!isCatalogObject(value)) throw new CatalogSourceError(`Catalog namespace "${namespace}" for "${locale}" is not an object.`);
|
|
44
|
+
return value;
|
|
45
|
+
};
|
|
46
|
+
const parseJsonCatalog = (json, locale) => {
|
|
47
|
+
const canonicalLocale = canonicalize(locale);
|
|
48
|
+
let parsed;
|
|
49
|
+
try {
|
|
50
|
+
parsed = JSON.parse(json);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
throw new CatalogSourceError(`Catalog JSON for "${canonicalLocale}" is invalid.`, { cause: error });
|
|
53
|
+
}
|
|
54
|
+
return copyCatalog(parsed, canonicalLocale);
|
|
55
|
+
};
|
|
56
|
+
const createMemoryCatalogSource = (options) => {
|
|
57
|
+
const catalogs = /* @__PURE__ */ new Map();
|
|
58
|
+
for (const [inputLocale, catalog] of Object.entries(options.catalogs)) {
|
|
59
|
+
const locale = canonicalize(inputLocale);
|
|
60
|
+
if (catalogs.has(locale)) throw new CatalogSourceError(`Duplicate catalog for locale "${locale}".`);
|
|
61
|
+
catalogs.set(locale, copyCatalog(catalog, locale));
|
|
62
|
+
}
|
|
63
|
+
return Object.freeze({ async load(locale, namespace, signal) {
|
|
64
|
+
if (signal?.aborted) throw signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
65
|
+
const canonicalLocale = canonicalize(locale);
|
|
66
|
+
const catalog = catalogs.get(canonicalLocale);
|
|
67
|
+
if (!catalog) throw new CatalogSourceError(`No in-memory catalog exists for "${canonicalLocale}".`);
|
|
68
|
+
return selectNamespace(catalog, namespace, canonicalLocale);
|
|
69
|
+
} });
|
|
70
|
+
};
|
|
71
|
+
const createTypeScriptCatalogSource = (options) => {
|
|
72
|
+
const catalogs = /* @__PURE__ */ new Map();
|
|
73
|
+
for (const [inputLocale, inputCatalog] of Object.entries(options.catalogs)) {
|
|
74
|
+
const locale = canonicalize(inputLocale);
|
|
75
|
+
if (catalogs.has(locale)) throw new CatalogSourceError(`Duplicate catalog for locale "${locale}".`);
|
|
76
|
+
const catalog = copyCatalog(inputCatalog, locale);
|
|
77
|
+
options.validate?.(catalog, { locale });
|
|
78
|
+
catalogs.set(locale, catalog);
|
|
79
|
+
}
|
|
80
|
+
return Object.freeze({ async load(locale, namespace, signal) {
|
|
81
|
+
if (signal?.aborted) throw signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
82
|
+
const canonicalLocale = canonicalize(locale);
|
|
83
|
+
const catalog = catalogs.get(canonicalLocale);
|
|
84
|
+
if (!catalog) throw new CatalogSourceError(`No TypeScript catalog exists for "${canonicalLocale}".`);
|
|
85
|
+
return selectNamespace(catalog, namespace, canonicalLocale);
|
|
86
|
+
} });
|
|
87
|
+
};
|
|
88
|
+
const validateRemoteUrl = (value) => {
|
|
89
|
+
let url;
|
|
90
|
+
try {
|
|
91
|
+
url = new URL(value);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
throw new CatalogSourceError("Remote catalog URL must be absolute.", { cause: error });
|
|
94
|
+
}
|
|
95
|
+
if (url.protocol !== "https:" && url.protocol !== "http:" || url.username || url.password || url.hash) throw new CatalogSourceError("Remote catalog URLs must use HTTP(S) and must not contain credentials or a hash.");
|
|
96
|
+
return url;
|
|
97
|
+
};
|
|
98
|
+
const wait = (milliseconds, signal) => new Promise((resolve, reject) => {
|
|
99
|
+
if (signal?.aborted) {
|
|
100
|
+
reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const timer = setTimeout(() => {
|
|
104
|
+
signal?.removeEventListener("abort", onAbort);
|
|
105
|
+
resolve();
|
|
106
|
+
}, milliseconds);
|
|
107
|
+
const onAbort = () => {
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
signal?.removeEventListener("abort", onAbort);
|
|
110
|
+
reject(signal?.reason ?? new DOMException("Aborted", "AbortError"));
|
|
111
|
+
};
|
|
112
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
113
|
+
});
|
|
114
|
+
const createRemoteCatalogSource = (options) => {
|
|
115
|
+
const timeoutMs = options.timeoutMs ?? 5e3;
|
|
116
|
+
const retries = options.retries ?? 1;
|
|
117
|
+
const cacheTtlMs = options.cacheTtlMs ?? 3e4;
|
|
118
|
+
const maxResponseBytes = options.maxResponseBytes ?? 1048576;
|
|
119
|
+
const fetcher = options.fetcher ?? globalThis.fetch;
|
|
120
|
+
const headers = new Headers(options.headers);
|
|
121
|
+
const cache = /* @__PURE__ */ new Map();
|
|
122
|
+
const encoder = new TextEncoder();
|
|
123
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 6e4) throw new RangeError("Remote catalog timeoutMs must be between 1 and 60000.");
|
|
124
|
+
if (!Number.isInteger(retries) || retries < 0 || retries > 5) throw new RangeError("Remote catalog retries must be between 0 and 5.");
|
|
125
|
+
if (!Number.isInteger(cacheTtlMs) || cacheTtlMs < 0) throw new RangeError("Remote catalog cacheTtlMs must be a non-negative integer.");
|
|
126
|
+
if (!Number.isInteger(maxResponseBytes) || maxResponseBytes < 1 || maxResponseBytes > 10485760) throw new RangeError("Remote catalog maxResponseBytes must be between 1 and 10485760.");
|
|
127
|
+
if (typeof fetcher !== "function") throw new CatalogSourceError("A Fetch-compatible implementation is required.");
|
|
128
|
+
const load = async (inputLocale, namespace, signal) => {
|
|
129
|
+
const locale = canonicalize(inputLocale);
|
|
130
|
+
if (signal?.aborted) throw signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
131
|
+
let url;
|
|
132
|
+
try {
|
|
133
|
+
url = validateRemoteUrl(options.url(locale, namespace));
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error instanceof CatalogSourceError) throw error;
|
|
136
|
+
throw new CatalogSourceError(`Could not resolve a remote catalog URL for "${locale}".`, { cause: error });
|
|
137
|
+
}
|
|
138
|
+
const cacheKey = `${locale}\u0000${namespace ?? ""}\u0000${url.href}`;
|
|
139
|
+
const cached = cache.get(cacheKey);
|
|
140
|
+
if (cached && cached.expiresAt > Date.now()) return cached.catalog;
|
|
141
|
+
for (let attempt = 0; attempt <= retries; attempt += 1) {
|
|
142
|
+
const controller = new AbortController();
|
|
143
|
+
let timedOut = false;
|
|
144
|
+
const timeout = setTimeout(() => {
|
|
145
|
+
timedOut = true;
|
|
146
|
+
controller.abort(/* @__PURE__ */ new Error("Remote catalog request timed out."));
|
|
147
|
+
}, timeoutMs);
|
|
148
|
+
const abortFromCaller = () => controller.abort(signal?.reason ?? new DOMException("Aborted", "AbortError"));
|
|
149
|
+
signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
150
|
+
let retryStatus;
|
|
151
|
+
let retryNetworkFailure = false;
|
|
152
|
+
let payload;
|
|
153
|
+
try {
|
|
154
|
+
const response = await fetcher(url, {
|
|
155
|
+
headers: new Headers(headers),
|
|
156
|
+
signal: controller.signal,
|
|
157
|
+
redirect: "error"
|
|
158
|
+
});
|
|
159
|
+
const isRedirectStatus = response.status >= 300 && response.status < 400;
|
|
160
|
+
if (response.redirected || isRedirectStatus) throw new CatalogSourceError(`Remote catalog redirects are not allowed${isRedirectStatus ? ` (HTTP ${response.status})` : ""}.`);
|
|
161
|
+
if (!response.ok) {
|
|
162
|
+
if ((response.status === 408 || response.status === 429 || response.status >= 500) && attempt < retries) retryStatus = response.status;
|
|
163
|
+
else throw new CatalogSourceError(`Remote catalog request for "${locale}" returned HTTP ${response.status}.`);
|
|
164
|
+
} else {
|
|
165
|
+
const declaredLength = Number(response.headers.get("content-length"));
|
|
166
|
+
if (Number.isFinite(declaredLength) && declaredLength > maxResponseBytes) throw new CatalogSourceError(`Remote catalog response for "${locale}" exceeds the configured size limit.`);
|
|
167
|
+
try {
|
|
168
|
+
const body = await response.text();
|
|
169
|
+
if (encoder.encode(body).byteLength > maxResponseBytes) throw new CatalogSourceError(`Remote catalog response for "${locale}" exceeds the configured size limit.`);
|
|
170
|
+
payload = JSON.parse(body);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (error instanceof CatalogSourceError) throw error;
|
|
173
|
+
if (signal?.aborted) throw signal.reason ?? error;
|
|
174
|
+
if (timedOut) throw new CatalogSourceError(`Remote catalog request for "${locale}" timed out.`, { cause: error });
|
|
175
|
+
throw new CatalogSourceError(`Remote catalog response for "${locale}" was not valid JSON.`, { cause: error });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (error instanceof CatalogSourceError) throw error;
|
|
180
|
+
if (signal?.aborted) throw signal.reason ?? error;
|
|
181
|
+
if (timedOut) throw new CatalogSourceError(`Remote catalog request for "${locale}" timed out.`, { cause: error });
|
|
182
|
+
if (attempt < retries) retryNetworkFailure = true;
|
|
183
|
+
else throw new CatalogSourceError(`Remote catalog request for "${locale}" failed.`, { cause: error });
|
|
184
|
+
} finally {
|
|
185
|
+
clearTimeout(timeout);
|
|
186
|
+
signal?.removeEventListener("abort", abortFromCaller);
|
|
187
|
+
}
|
|
188
|
+
if (retryStatus !== void 0 || retryNetworkFailure) {
|
|
189
|
+
await wait(100 * 2 ** attempt, signal);
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (signal?.aborted) throw signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
193
|
+
const catalog = copyCatalog(payload, locale);
|
|
194
|
+
const selectedCatalog = selectNamespace(catalog, namespace, locale);
|
|
195
|
+
if (cacheTtlMs > 0) cache.set(cacheKey, {
|
|
196
|
+
catalog: selectedCatalog,
|
|
197
|
+
expiresAt: Date.now() + cacheTtlMs
|
|
198
|
+
});
|
|
199
|
+
return selectedCatalog;
|
|
200
|
+
}
|
|
201
|
+
throw new CatalogSourceError(`Remote catalog request for "${locale}" exhausted its retries.`);
|
|
202
|
+
};
|
|
203
|
+
const invalidate = (inputLocale, namespace) => {
|
|
204
|
+
if (!inputLocale) {
|
|
205
|
+
cache.clear();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const locale = canonicalize(inputLocale);
|
|
209
|
+
if (namespace !== void 0) {
|
|
210
|
+
const namespacePrefix = `${locale}\u0000${namespace}\u0000`;
|
|
211
|
+
for (const key of cache.keys()) if (key.startsWith(namespacePrefix)) cache.delete(key);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
for (const key of cache.keys()) if (key.startsWith(`${locale}\u0000`)) cache.delete(key);
|
|
215
|
+
};
|
|
216
|
+
return Object.freeze({
|
|
217
|
+
load,
|
|
218
|
+
invalidate
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
exports.CatalogSourceError = CatalogSourceError;
|
|
222
|
+
exports.createMemoryCatalogSource = createMemoryCatalogSource;
|
|
223
|
+
exports.createRemoteCatalogSource = createRemoteCatalogSource;
|
|
224
|
+
exports.createTypeScriptCatalogSource = createTypeScriptCatalogSource;
|
|
225
|
+
exports.parseJsonCatalog = parseJsonCatalog;
|
|
226
|
+
|
|
227
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../../src/sources/index.ts"],"sourcesContent":["import type { MessageCatalog } from \"../runtime\";\n\nexport interface CatalogSource {\n load(\n locale: string,\n namespace?: string,\n signal?: AbortSignal,\n ): Promise<MessageCatalog>;\n invalidate?(locale?: string, namespace?: string): void;\n}\n\nexport interface RemoteCatalogSource extends CatalogSource {\n invalidate(locale?: string, namespace?: string): void;\n}\n\nexport class CatalogSourceError extends Error {\n constructor(\n message: string,\n options?: { cause?: unknown },\n ) {\n super(message, options);\n this.name = \"CatalogSourceError\";\n }\n}\n\nconst isCatalogObject = (\n value: unknown,\n): value is Record<string, unknown> => {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n};\n\nconst canonicalize = (locale: string): string => {\n try {\n const [canonical] = Intl.getCanonicalLocales(locale);\n\n if (!canonical) {\n throw new Error();\n }\n\n return canonical;\n } catch {\n throw new CatalogSourceError(`Invalid BCP 47 catalog locale \"${locale}\".`);\n }\n};\n\nconst copyCatalog = (\n value: unknown,\n locale: string,\n path = \"\",\n ancestors = new WeakSet<object>(),\n): MessageCatalog => {\n if (!isCatalogObject(value)) {\n throw new CatalogSourceError(\n `Catalog for \"${locale}\" at \"${path || \"<root>\"}\" must be a plain object.`,\n );\n }\n\n if (ancestors.has(value)) {\n throw new CatalogSourceError(\n `Catalog for \"${locale}\" contains a circular reference at \"${path || \"<root>\"}\".`,\n );\n }\n\n ancestors.add(value);\n const entries = Object.entries(value).map(([key, entry]) => {\n const keyPath = path ? `${path}.${key}` : key;\n\n if (!key || key.includes(\".\")) {\n throw new CatalogSourceError(\n `Catalog for \"${locale}\" has an invalid key \"${key}\" at \"${path || \"<root>\"}\".`,\n );\n }\n\n if (typeof entry === \"string\") {\n return [key, entry] as const;\n }\n\n return [key, copyCatalog(entry, locale, keyPath, ancestors)] as const;\n });\n\n ancestors.delete(value);\n return Object.freeze(Object.fromEntries(entries));\n};\n\nconst selectNamespace = (\n catalog: MessageCatalog,\n namespace: string | undefined,\n locale: string,\n): MessageCatalog => {\n if (!namespace) {\n return catalog;\n }\n\n let value: unknown = catalog;\n\n for (const part of namespace.split(\".\")) {\n if (!part || !isCatalogObject(value) || !Object.hasOwn(value, part)) {\n throw new CatalogSourceError(\n `Catalog namespace \"${namespace}\" was not found for \"${locale}\".`,\n );\n }\n\n value = value[part];\n }\n\n if (!isCatalogObject(value)) {\n throw new CatalogSourceError(\n `Catalog namespace \"${namespace}\" for \"${locale}\" is not an object.`,\n );\n }\n\n return value as MessageCatalog;\n};\n\nexport const parseJsonCatalog = (\n json: string,\n locale: string,\n): MessageCatalog => {\n const canonicalLocale = canonicalize(locale);\n let parsed: unknown;\n\n try {\n parsed = JSON.parse(json);\n } catch (error) {\n throw new CatalogSourceError(\n `Catalog JSON for \"${canonicalLocale}\" is invalid.`,\n { cause: error },\n );\n }\n\n return copyCatalog(parsed, canonicalLocale);\n};\n\nexport interface MemoryCatalogSourceOptions {\n catalogs: Readonly<Record<string, MessageCatalog>>;\n}\n\nexport const createMemoryCatalogSource = (\n options: MemoryCatalogSourceOptions,\n): CatalogSource => {\n const catalogs = new Map<string, MessageCatalog>();\n\n for (const [inputLocale, catalog] of Object.entries(options.catalogs)) {\n const locale = canonicalize(inputLocale);\n\n if (catalogs.has(locale)) {\n throw new CatalogSourceError(`Duplicate catalog for locale \"${locale}\".`);\n }\n\n catalogs.set(locale, copyCatalog(catalog, locale));\n }\n\n return Object.freeze({\n async load(locale: string, namespace?: string, signal?: AbortSignal) {\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"Aborted\", \"AbortError\");\n }\n\n const canonicalLocale = canonicalize(locale);\n const catalog = catalogs.get(canonicalLocale);\n\n if (!catalog) {\n throw new CatalogSourceError(\n `No in-memory catalog exists for \"${canonicalLocale}\".`,\n );\n }\n\n return selectNamespace(\n catalog,\n namespace,\n canonicalLocale,\n );\n },\n });\n};\n\nexport interface CatalogValidationContext {\n readonly locale: string;\n}\n\nexport type CatalogSchemaValidator = (\n catalog: unknown,\n context: CatalogValidationContext,\n) => void;\n\nexport interface TypeScriptCatalogSourceOptions<\n Catalogs extends Readonly<Record<string, MessageCatalog>>,\n> {\n catalogs: Catalogs;\n validate?: CatalogSchemaValidator;\n}\n\nexport interface TypeScriptCatalogSource<\n Catalogs extends Readonly<Record<string, MessageCatalog>>,\n> {\n load(\n locale: string,\n namespace?: undefined,\n signal?: AbortSignal,\n ): Promise<Catalogs[keyof Catalogs]>;\n load(\n locale: string,\n namespace: string,\n signal?: AbortSignal,\n ): Promise<MessageCatalog>;\n}\n\nexport const createTypeScriptCatalogSource = <\n Catalogs extends Readonly<Record<string, MessageCatalog>>,\n>(\n options: TypeScriptCatalogSourceOptions<Catalogs>,\n): TypeScriptCatalogSource<Catalogs> => {\n const catalogs = new Map<string, MessageCatalog>();\n\n for (const [inputLocale, inputCatalog] of Object.entries(options.catalogs)) {\n const locale = canonicalize(inputLocale);\n\n if (catalogs.has(locale)) {\n throw new CatalogSourceError(`Duplicate catalog for locale \"${locale}\".`);\n }\n\n const catalog = copyCatalog(inputCatalog, locale);\n options.validate?.(catalog, { locale });\n catalogs.set(locale, catalog);\n }\n\n return Object.freeze({\n async load(locale: string, namespace?: string, signal?: AbortSignal) {\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"Aborted\", \"AbortError\");\n }\n\n const canonicalLocale = canonicalize(locale);\n const catalog = catalogs.get(canonicalLocale);\n\n if (!catalog) {\n throw new CatalogSourceError(\n `No TypeScript catalog exists for \"${canonicalLocale}\".`,\n );\n }\n\n return selectNamespace(catalog, namespace, canonicalLocale);\n },\n }) as TypeScriptCatalogSource<Catalogs>;\n};\n\nexport interface RemoteCatalogSourceOptions {\n url(locale: string, namespace?: string): string | URL;\n headers?: HeadersInit;\n fetcher?: typeof fetch;\n timeoutMs?: number;\n retries?: number;\n cacheTtlMs?: number;\n maxResponseBytes?: number;\n}\n\ninterface CachedCatalog {\n catalog: MessageCatalog;\n expiresAt: number;\n}\n\nconst validateRemoteUrl = (value: string | URL): URL => {\n let url: URL;\n\n try {\n url = new URL(value);\n } catch (error) {\n throw new CatalogSourceError(\"Remote catalog URL must be absolute.\", {\n cause: error,\n });\n }\n\n if (\n (url.protocol !== \"https:\" && url.protocol !== \"http:\") ||\n url.username ||\n url.password ||\n url.hash\n ) {\n throw new CatalogSourceError(\n \"Remote catalog URLs must use HTTP(S) and must not contain credentials or a hash.\",\n );\n }\n\n return url;\n};\n\nconst wait = (milliseconds: number, signal?: AbortSignal): Promise<void> =>\n new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(signal.reason ?? new DOMException(\"Aborted\", \"AbortError\"));\n return;\n }\n\n const timer = setTimeout(() => {\n signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, milliseconds);\n const onAbort = () => {\n clearTimeout(timer);\n signal?.removeEventListener(\"abort\", onAbort);\n reject(signal?.reason ?? new DOMException(\"Aborted\", \"AbortError\"));\n };\n\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n\nexport const createRemoteCatalogSource = (\n options: RemoteCatalogSourceOptions,\n): RemoteCatalogSource => {\n const timeoutMs = options.timeoutMs ?? 5_000;\n const retries = options.retries ?? 1;\n const cacheTtlMs = options.cacheTtlMs ?? 30_000;\n const maxResponseBytes = options.maxResponseBytes ?? 1_048_576;\n const fetcher = options.fetcher ?? globalThis.fetch;\n const headers = new Headers(options.headers);\n const cache = new Map<string, CachedCatalog>();\n const encoder = new TextEncoder();\n\n if (!Number.isInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 60_000) {\n throw new RangeError(\"Remote catalog timeoutMs must be between 1 and 60000.\");\n }\n\n if (!Number.isInteger(retries) || retries < 0 || retries > 5) {\n throw new RangeError(\"Remote catalog retries must be between 0 and 5.\");\n }\n\n if (!Number.isInteger(cacheTtlMs) || cacheTtlMs < 0) {\n throw new RangeError(\"Remote catalog cacheTtlMs must be a non-negative integer.\");\n }\n\n if (\n !Number.isInteger(maxResponseBytes) ||\n maxResponseBytes < 1 ||\n maxResponseBytes > 10_485_760\n ) {\n throw new RangeError(\n \"Remote catalog maxResponseBytes must be between 1 and 10485760.\",\n );\n }\n\n if (typeof fetcher !== \"function\") {\n throw new CatalogSourceError(\"A Fetch-compatible implementation is required.\");\n }\n\n const load = async (\n inputLocale: string,\n namespace?: string,\n signal?: AbortSignal,\n ): Promise<MessageCatalog> => {\n const locale = canonicalize(inputLocale);\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"Aborted\", \"AbortError\");\n }\n\n let url: URL;\n\n try {\n url = validateRemoteUrl(options.url(locale, namespace));\n } catch (error) {\n if (error instanceof CatalogSourceError) {\n throw error;\n }\n\n throw new CatalogSourceError(\n `Could not resolve a remote catalog URL for \"${locale}\".`,\n { cause: error },\n );\n }\n\n const cacheKey = `${locale}\\u0000${namespace ?? \"\"}\\u0000${url.href}`;\n const cached = cache.get(cacheKey);\n\n if (cached && cached.expiresAt > Date.now()) {\n return cached.catalog;\n }\n\n for (let attempt = 0; attempt <= retries; attempt += 1) {\n const controller = new AbortController();\n let timedOut = false;\n const timeout = setTimeout(\n () => {\n timedOut = true;\n controller.abort(new Error(\"Remote catalog request timed out.\"));\n },\n timeoutMs,\n );\n const abortFromCaller = () =>\n controller.abort(\n signal?.reason ?? new DOMException(\"Aborted\", \"AbortError\"),\n );\n signal?.addEventListener(\"abort\", abortFromCaller, { once: true });\n\n let retryStatus: number | undefined;\n let retryNetworkFailure = false;\n let payload: unknown;\n\n try {\n const response = await fetcher(url, {\n headers: new Headers(headers),\n signal: controller.signal,\n redirect: \"error\",\n });\n\n const isRedirectStatus =\n response.status >= 300 && response.status < 400;\n\n if (response.redirected || isRedirectStatus) {\n const statusDetail = isRedirectStatus\n ? ` (HTTP ${response.status})`\n : \"\";\n\n throw new CatalogSourceError(\n `Remote catalog redirects are not allowed${statusDetail}.`,\n );\n }\n\n if (!response.ok) {\n const retryable =\n response.status === 408 ||\n response.status === 429 ||\n response.status >= 500;\n\n if (retryable && attempt < retries) {\n retryStatus = response.status;\n } else {\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" returned HTTP ${response.status}.`,\n );\n }\n } else {\n const declaredLength = Number(response.headers.get(\"content-length\"));\n\n if (Number.isFinite(declaredLength) && declaredLength > maxResponseBytes) {\n throw new CatalogSourceError(\n `Remote catalog response for \"${locale}\" exceeds the configured size limit.`,\n );\n }\n\n try {\n const body = await response.text();\n\n if (encoder.encode(body).byteLength > maxResponseBytes) {\n throw new CatalogSourceError(\n `Remote catalog response for \"${locale}\" exceeds the configured size limit.`,\n );\n }\n\n payload = JSON.parse(body);\n } catch (error) {\n if (error instanceof CatalogSourceError) {\n throw error;\n }\n\n if (signal?.aborted) {\n throw signal.reason ?? error;\n }\n\n if (timedOut) {\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" timed out.`,\n { cause: error },\n );\n }\n\n throw new CatalogSourceError(\n `Remote catalog response for \"${locale}\" was not valid JSON.`,\n { cause: error },\n );\n }\n }\n } catch (error) {\n if (error instanceof CatalogSourceError) {\n throw error;\n }\n\n if (signal?.aborted) {\n throw signal.reason ?? error;\n }\n\n if (timedOut) {\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" timed out.`,\n { cause: error },\n );\n }\n\n if (attempt < retries) {\n retryNetworkFailure = true;\n } else {\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" failed.`,\n { cause: error },\n );\n }\n } finally {\n clearTimeout(timeout);\n signal?.removeEventListener(\"abort\", abortFromCaller);\n }\n\n if (retryStatus !== undefined || retryNetworkFailure) {\n await wait(100 * 2 ** attempt, signal);\n continue;\n }\n\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"Aborted\", \"AbortError\");\n }\n\n const catalog = copyCatalog(payload, locale);\n const selectedCatalog = selectNamespace(catalog, namespace, locale);\n\n if (cacheTtlMs > 0) {\n cache.set(cacheKey, {\n catalog: selectedCatalog,\n expiresAt: Date.now() + cacheTtlMs,\n });\n }\n\n return selectedCatalog;\n }\n\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" exhausted its retries.`,\n );\n };\n\n const invalidate = (inputLocale?: string, namespace?: string): void => {\n if (!inputLocale) {\n cache.clear();\n return;\n }\n\n const locale = canonicalize(inputLocale);\n\n if (namespace !== undefined) {\n const namespacePrefix = `${locale}\\u0000${namespace}\\u0000`;\n\n for (const key of cache.keys()) {\n if (key.startsWith(namespacePrefix)) {\n cache.delete(key);\n }\n }\n\n return;\n }\n\n for (const key of cache.keys()) {\n if (key.startsWith(`${locale}\\u0000`)) {\n cache.delete(key);\n }\n }\n };\n\n return Object.freeze({ load, invalidate });\n};\n"],"mappings":";;AAeA,IAAa,qBAAb,cAAwC,MAAM;CAC5C,YACE,SACA,SACA;EACA,MAAM,SAAS,OAAO;EACtB,KAAK,OAAO;CACd;AACF;AAEA,MAAM,mBACJ,UACqC;CACrC,IAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,MAAM,QAAQ,KAAK,GACpE,OAAO;CAGT,MAAM,YAAY,OAAO,eAAe,KAAK;CAC7C,OAAO,cAAc,OAAO,aAAa,cAAc;AACzD;AAEA,MAAM,gBAAgB,WAA2B;CAC/C,IAAI;EACF,MAAM,CAAC,aAAa,KAAK,oBAAoB,MAAM;EAEnD,IAAI,CAAC,WACH,MAAM,IAAI,MAAM;EAGlB,OAAO;CACT,QAAQ;EACN,MAAM,IAAI,mBAAmB,kCAAkC,OAAO,GAAG;CAC3E;AACF;AAEA,MAAM,eACJ,OACA,QACA,OAAO,IACP,4BAAY,IAAI,QAAgB,MACb;CACnB,IAAI,CAAC,gBAAgB,KAAK,GACxB,MAAM,IAAI,mBACR,gBAAgB,OAAO,QAAQ,QAAQ,SAAS,0BAClD;CAGF,IAAI,UAAU,IAAI,KAAK,GACrB,MAAM,IAAI,mBACR,gBAAgB,OAAO,sCAAsC,QAAQ,SAAS,GAChF;CAGF,UAAU,IAAI,KAAK;CACnB,MAAM,UAAU,OAAO,QAAQ,KAAK,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW;EAC1D,MAAM,UAAU,OAAO,GAAG,KAAK,GAAG,QAAQ;EAE1C,IAAI,CAAC,OAAO,IAAI,SAAS,GAAG,GAC1B,MAAM,IAAI,mBACR,gBAAgB,OAAO,wBAAwB,IAA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AAkB,gBAAgB,SAAS,WAAW,MAAM;GAElE,IAAI,aAAa,GACf,MAAM,IAAI,UAAU;IAClB,SAAS;IACT,WAAW,KAAK,IAAI,IAAI;GAC1B,CAAC;GAGH,OAAO;EACT;EAEA,MAAM,IAAI,mBACR,+BAA+B,OAAO,yBACxC;CACF;CAEA,MAAM,cAAc,aAAsB,cAA6B;EACrE,IAAI,CAAC,aAAa;GAChB,MAAM,MAAM;GACZ;EACF;EAEA,MAAM,SAAS,aAAa,WAAW;EAEvC,IAAI,cAAc,KAAA,GAAW;GAC3B,MAAM,kBAAkB,GAAG,OAAO,QAAQ,UAAU;GAEpD,KAAK,MAAM,OAAO,MAAM,KAAK,GAC3B,IAAI,IAAI,WAAW,eAAe,GAChC,MAAM,OAAO,GAAG;GAIpB;EACF;EAEA,KAAK,MAAM,OAAO,MAAM,KAAK,GAC3B,IAAI,IAAI,WAAW,GAAG,OAAO,OAAO,GAClC,MAAM,OAAO,GAAG;CAGtB;CAEA,OAAO,OAAO,OAAO;EAAE;EAAM;CAAW,CAAC;AAC3C"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { MessageCatalog } from "../runtime.cjs";
|
|
2
|
+
export interface CatalogSource {
|
|
3
|
+
load(locale: string, namespace?: string, signal?: AbortSignal): Promise<MessageCatalog>;
|
|
4
|
+
invalidate?(locale?: string, namespace?: string): void;
|
|
5
|
+
}
|
|
6
|
+
export interface RemoteCatalogSource extends CatalogSource {
|
|
7
|
+
invalidate(locale?: string, namespace?: string): void;
|
|
8
|
+
}
|
|
9
|
+
export declare class CatalogSourceError extends Error {
|
|
10
|
+
constructor(message: string, options?: {
|
|
11
|
+
cause?: unknown;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export declare const parseJsonCatalog: (json: string, locale: string) => MessageCatalog;
|
|
15
|
+
export interface MemoryCatalogSourceOptions {
|
|
16
|
+
catalogs: Readonly<Record<string, MessageCatalog>>;
|
|
17
|
+
}
|
|
18
|
+
export declare const createMemoryCatalogSource: (options: MemoryCatalogSourceOptions) => CatalogSource;
|
|
19
|
+
export interface CatalogValidationContext {
|
|
20
|
+
readonly locale: string;
|
|
21
|
+
}
|
|
22
|
+
export type CatalogSchemaValidator = (catalog: unknown, context: CatalogValidationContext) => void;
|
|
23
|
+
export interface TypeScriptCatalogSourceOptions<Catalogs extends Readonly<Record<string, MessageCatalog>>> {
|
|
24
|
+
catalogs: Catalogs;
|
|
25
|
+
validate?: CatalogSchemaValidator;
|
|
26
|
+
}
|
|
27
|
+
export interface TypeScriptCatalogSource<Catalogs extends Readonly<Record<string, MessageCatalog>>> {
|
|
28
|
+
load(locale: string, namespace?: undefined, signal?: AbortSignal): Promise<Catalogs[keyof Catalogs]>;
|
|
29
|
+
load(locale: string, namespace: string, signal?: AbortSignal): Promise<MessageCatalog>;
|
|
30
|
+
}
|
|
31
|
+
export declare const createTypeScriptCatalogSource: <Catalogs extends Readonly<Record<string, MessageCatalog>>>(options: TypeScriptCatalogSourceOptions<Catalogs>) => TypeScriptCatalogSource<Catalogs>;
|
|
32
|
+
export interface RemoteCatalogSourceOptions {
|
|
33
|
+
url(locale: string, namespace?: string): string | URL;
|
|
34
|
+
headers?: HeadersInit;
|
|
35
|
+
fetcher?: typeof fetch;
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
retries?: number;
|
|
38
|
+
cacheTtlMs?: number;
|
|
39
|
+
maxResponseBytes?: number;
|
|
40
|
+
}
|
|
41
|
+
export declare const createRemoteCatalogSource: (options: RemoteCatalogSourceOptions) => RemoteCatalogSource;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { MessageCatalog } from "../runtime.js";
|
|
2
|
+
export interface CatalogSource {
|
|
3
|
+
load(locale: string, namespace?: string, signal?: AbortSignal): Promise<MessageCatalog>;
|
|
4
|
+
invalidate?(locale?: string, namespace?: string): void;
|
|
5
|
+
}
|
|
6
|
+
export interface RemoteCatalogSource extends CatalogSource {
|
|
7
|
+
invalidate(locale?: string, namespace?: string): void;
|
|
8
|
+
}
|
|
9
|
+
export declare class CatalogSourceError extends Error {
|
|
10
|
+
constructor(message: string, options?: {
|
|
11
|
+
cause?: unknown;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export declare const parseJsonCatalog: (json: string, locale: string) => MessageCatalog;
|
|
15
|
+
export interface MemoryCatalogSourceOptions {
|
|
16
|
+
catalogs: Readonly<Record<string, MessageCatalog>>;
|
|
17
|
+
}
|
|
18
|
+
export declare const createMemoryCatalogSource: (options: MemoryCatalogSourceOptions) => CatalogSource;
|
|
19
|
+
export interface CatalogValidationContext {
|
|
20
|
+
readonly locale: string;
|
|
21
|
+
}
|
|
22
|
+
export type CatalogSchemaValidator = (catalog: unknown, context: CatalogValidationContext) => void;
|
|
23
|
+
export interface TypeScriptCatalogSourceOptions<Catalogs extends Readonly<Record<string, MessageCatalog>>> {
|
|
24
|
+
catalogs: Catalogs;
|
|
25
|
+
validate?: CatalogSchemaValidator;
|
|
26
|
+
}
|
|
27
|
+
export interface TypeScriptCatalogSource<Catalogs extends Readonly<Record<string, MessageCatalog>>> {
|
|
28
|
+
load(locale: string, namespace?: undefined, signal?: AbortSignal): Promise<Catalogs[keyof Catalogs]>;
|
|
29
|
+
load(locale: string, namespace: string, signal?: AbortSignal): Promise<MessageCatalog>;
|
|
30
|
+
}
|
|
31
|
+
export declare const createTypeScriptCatalogSource: <Catalogs extends Readonly<Record<string, MessageCatalog>>>(options: TypeScriptCatalogSourceOptions<Catalogs>) => TypeScriptCatalogSource<Catalogs>;
|
|
32
|
+
export interface RemoteCatalogSourceOptions {
|
|
33
|
+
url(locale: string, namespace?: string): string | URL;
|
|
34
|
+
headers?: HeadersInit;
|
|
35
|
+
fetcher?: typeof fetch;
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
retries?: number;
|
|
38
|
+
cacheTtlMs?: number;
|
|
39
|
+
maxResponseBytes?: number;
|
|
40
|
+
}
|
|
41
|
+
export declare const createRemoteCatalogSource: (options: RemoteCatalogSourceOptions) => RemoteCatalogSource;
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// src/sources/index.ts
|
|
2
|
+
var CatalogSourceError = class extends Error {
|
|
3
|
+
constructor(message, options) {
|
|
4
|
+
super(message, options);
|
|
5
|
+
this.name = "CatalogSourceError";
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
const isCatalogObject = (value) => {
|
|
9
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
10
|
+
const prototype = Object.getPrototypeOf(value);
|
|
11
|
+
return prototype === Object.prototype || prototype === null;
|
|
12
|
+
};
|
|
13
|
+
const canonicalize = (locale) => {
|
|
14
|
+
try {
|
|
15
|
+
const [canonical] = Intl.getCanonicalLocales(locale);
|
|
16
|
+
if (!canonical) throw new Error();
|
|
17
|
+
return canonical;
|
|
18
|
+
} catch {
|
|
19
|
+
throw new CatalogSourceError(`Invalid BCP 47 catalog locale "${locale}".`);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const copyCatalog = (value, locale, path = "", ancestors = /* @__PURE__ */ new WeakSet()) => {
|
|
23
|
+
if (!isCatalogObject(value)) throw new CatalogSourceError(`Catalog for "${locale}" at "${path || "<root>"}" must be a plain object.`);
|
|
24
|
+
if (ancestors.has(value)) throw new CatalogSourceError(`Catalog for "${locale}" contains a circular reference at "${path || "<root>"}".`);
|
|
25
|
+
ancestors.add(value);
|
|
26
|
+
const entries = Object.entries(value).map(([key, entry]) => {
|
|
27
|
+
const keyPath = path ? `${path}.${key}` : key;
|
|
28
|
+
if (!key || key.includes(".")) throw new CatalogSourceError(`Catalog for "${locale}" has an invalid key "${key}" at "${path || "<root>"}".`);
|
|
29
|
+
if (typeof entry === "string") return [key, entry];
|
|
30
|
+
return [key, copyCatalog(entry, locale, keyPath, ancestors)];
|
|
31
|
+
});
|
|
32
|
+
ancestors.delete(value);
|
|
33
|
+
return Object.freeze(Object.fromEntries(entries));
|
|
34
|
+
};
|
|
35
|
+
const selectNamespace = (catalog, namespace, locale) => {
|
|
36
|
+
if (!namespace) return catalog;
|
|
37
|
+
let value = catalog;
|
|
38
|
+
for (const part of namespace.split(".")) {
|
|
39
|
+
if (!part || !isCatalogObject(value) || !Object.hasOwn(value, part)) throw new CatalogSourceError(`Catalog namespace "${namespace}" was not found for "${locale}".`);
|
|
40
|
+
value = value[part];
|
|
41
|
+
}
|
|
42
|
+
if (!isCatalogObject(value)) throw new CatalogSourceError(`Catalog namespace "${namespace}" for "${locale}" is not an object.`);
|
|
43
|
+
return value;
|
|
44
|
+
};
|
|
45
|
+
const parseJsonCatalog = (json, locale) => {
|
|
46
|
+
const canonicalLocale = canonicalize(locale);
|
|
47
|
+
let parsed;
|
|
48
|
+
try {
|
|
49
|
+
parsed = JSON.parse(json);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
throw new CatalogSourceError(`Catalog JSON for "${canonicalLocale}" is invalid.`, { cause: error });
|
|
52
|
+
}
|
|
53
|
+
return copyCatalog(parsed, canonicalLocale);
|
|
54
|
+
};
|
|
55
|
+
const createMemoryCatalogSource = (options) => {
|
|
56
|
+
const catalogs = /* @__PURE__ */ new Map();
|
|
57
|
+
for (const [inputLocale, catalog] of Object.entries(options.catalogs)) {
|
|
58
|
+
const locale = canonicalize(inputLocale);
|
|
59
|
+
if (catalogs.has(locale)) throw new CatalogSourceError(`Duplicate catalog for locale "${locale}".`);
|
|
60
|
+
catalogs.set(locale, copyCatalog(catalog, locale));
|
|
61
|
+
}
|
|
62
|
+
return Object.freeze({ async load(locale, namespace, signal) {
|
|
63
|
+
if (signal?.aborted) throw signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
64
|
+
const canonicalLocale = canonicalize(locale);
|
|
65
|
+
const catalog = catalogs.get(canonicalLocale);
|
|
66
|
+
if (!catalog) throw new CatalogSourceError(`No in-memory catalog exists for "${canonicalLocale}".`);
|
|
67
|
+
return selectNamespace(catalog, namespace, canonicalLocale);
|
|
68
|
+
} });
|
|
69
|
+
};
|
|
70
|
+
const createTypeScriptCatalogSource = (options) => {
|
|
71
|
+
const catalogs = /* @__PURE__ */ new Map();
|
|
72
|
+
for (const [inputLocale, inputCatalog] of Object.entries(options.catalogs)) {
|
|
73
|
+
const locale = canonicalize(inputLocale);
|
|
74
|
+
if (catalogs.has(locale)) throw new CatalogSourceError(`Duplicate catalog for locale "${locale}".`);
|
|
75
|
+
const catalog = copyCatalog(inputCatalog, locale);
|
|
76
|
+
options.validate?.(catalog, { locale });
|
|
77
|
+
catalogs.set(locale, catalog);
|
|
78
|
+
}
|
|
79
|
+
return Object.freeze({ async load(locale, namespace, signal) {
|
|
80
|
+
if (signal?.aborted) throw signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
81
|
+
const canonicalLocale = canonicalize(locale);
|
|
82
|
+
const catalog = catalogs.get(canonicalLocale);
|
|
83
|
+
if (!catalog) throw new CatalogSourceError(`No TypeScript catalog exists for "${canonicalLocale}".`);
|
|
84
|
+
return selectNamespace(catalog, namespace, canonicalLocale);
|
|
85
|
+
} });
|
|
86
|
+
};
|
|
87
|
+
const validateRemoteUrl = (value) => {
|
|
88
|
+
let url;
|
|
89
|
+
try {
|
|
90
|
+
url = new URL(value);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
throw new CatalogSourceError("Remote catalog URL must be absolute.", { cause: error });
|
|
93
|
+
}
|
|
94
|
+
if (url.protocol !== "https:" && url.protocol !== "http:" || url.username || url.password || url.hash) throw new CatalogSourceError("Remote catalog URLs must use HTTP(S) and must not contain credentials or a hash.");
|
|
95
|
+
return url;
|
|
96
|
+
};
|
|
97
|
+
const wait = (milliseconds, signal) => new Promise((resolve, reject) => {
|
|
98
|
+
if (signal?.aborted) {
|
|
99
|
+
reject(signal.reason ?? new DOMException("Aborted", "AbortError"));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const timer = setTimeout(() => {
|
|
103
|
+
signal?.removeEventListener("abort", onAbort);
|
|
104
|
+
resolve();
|
|
105
|
+
}, milliseconds);
|
|
106
|
+
const onAbort = () => {
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
signal?.removeEventListener("abort", onAbort);
|
|
109
|
+
reject(signal?.reason ?? new DOMException("Aborted", "AbortError"));
|
|
110
|
+
};
|
|
111
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
112
|
+
});
|
|
113
|
+
const createRemoteCatalogSource = (options) => {
|
|
114
|
+
const timeoutMs = options.timeoutMs ?? 5e3;
|
|
115
|
+
const retries = options.retries ?? 1;
|
|
116
|
+
const cacheTtlMs = options.cacheTtlMs ?? 3e4;
|
|
117
|
+
const maxResponseBytes = options.maxResponseBytes ?? 1048576;
|
|
118
|
+
const fetcher = options.fetcher ?? globalThis.fetch;
|
|
119
|
+
const headers = new Headers(options.headers);
|
|
120
|
+
const cache = /* @__PURE__ */ new Map();
|
|
121
|
+
const encoder = new TextEncoder();
|
|
122
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 6e4) throw new RangeError("Remote catalog timeoutMs must be between 1 and 60000.");
|
|
123
|
+
if (!Number.isInteger(retries) || retries < 0 || retries > 5) throw new RangeError("Remote catalog retries must be between 0 and 5.");
|
|
124
|
+
if (!Number.isInteger(cacheTtlMs) || cacheTtlMs < 0) throw new RangeError("Remote catalog cacheTtlMs must be a non-negative integer.");
|
|
125
|
+
if (!Number.isInteger(maxResponseBytes) || maxResponseBytes < 1 || maxResponseBytes > 10485760) throw new RangeError("Remote catalog maxResponseBytes must be between 1 and 10485760.");
|
|
126
|
+
if (typeof fetcher !== "function") throw new CatalogSourceError("A Fetch-compatible implementation is required.");
|
|
127
|
+
const load = async (inputLocale, namespace, signal) => {
|
|
128
|
+
const locale = canonicalize(inputLocale);
|
|
129
|
+
if (signal?.aborted) throw signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
130
|
+
let url;
|
|
131
|
+
try {
|
|
132
|
+
url = validateRemoteUrl(options.url(locale, namespace));
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (error instanceof CatalogSourceError) throw error;
|
|
135
|
+
throw new CatalogSourceError(`Could not resolve a remote catalog URL for "${locale}".`, { cause: error });
|
|
136
|
+
}
|
|
137
|
+
const cacheKey = `${locale}\u0000${namespace ?? ""}\u0000${url.href}`;
|
|
138
|
+
const cached = cache.get(cacheKey);
|
|
139
|
+
if (cached && cached.expiresAt > Date.now()) return cached.catalog;
|
|
140
|
+
for (let attempt = 0; attempt <= retries; attempt += 1) {
|
|
141
|
+
const controller = new AbortController();
|
|
142
|
+
let timedOut = false;
|
|
143
|
+
const timeout = setTimeout(() => {
|
|
144
|
+
timedOut = true;
|
|
145
|
+
controller.abort(/* @__PURE__ */ new Error("Remote catalog request timed out."));
|
|
146
|
+
}, timeoutMs);
|
|
147
|
+
const abortFromCaller = () => controller.abort(signal?.reason ?? new DOMException("Aborted", "AbortError"));
|
|
148
|
+
signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
149
|
+
let retryStatus;
|
|
150
|
+
let retryNetworkFailure = false;
|
|
151
|
+
let payload;
|
|
152
|
+
try {
|
|
153
|
+
const response = await fetcher(url, {
|
|
154
|
+
headers: new Headers(headers),
|
|
155
|
+
signal: controller.signal,
|
|
156
|
+
redirect: "error"
|
|
157
|
+
});
|
|
158
|
+
const isRedirectStatus = response.status >= 300 && response.status < 400;
|
|
159
|
+
if (response.redirected || isRedirectStatus) throw new CatalogSourceError(`Remote catalog redirects are not allowed${isRedirectStatus ? ` (HTTP ${response.status})` : ""}.`);
|
|
160
|
+
if (!response.ok) {
|
|
161
|
+
if ((response.status === 408 || response.status === 429 || response.status >= 500) && attempt < retries) retryStatus = response.status;
|
|
162
|
+
else throw new CatalogSourceError(`Remote catalog request for "${locale}" returned HTTP ${response.status}.`);
|
|
163
|
+
} else {
|
|
164
|
+
const declaredLength = Number(response.headers.get("content-length"));
|
|
165
|
+
if (Number.isFinite(declaredLength) && declaredLength > maxResponseBytes) throw new CatalogSourceError(`Remote catalog response for "${locale}" exceeds the configured size limit.`);
|
|
166
|
+
try {
|
|
167
|
+
const body = await response.text();
|
|
168
|
+
if (encoder.encode(body).byteLength > maxResponseBytes) throw new CatalogSourceError(`Remote catalog response for "${locale}" exceeds the configured size limit.`);
|
|
169
|
+
payload = JSON.parse(body);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (error instanceof CatalogSourceError) throw error;
|
|
172
|
+
if (signal?.aborted) throw signal.reason ?? error;
|
|
173
|
+
if (timedOut) throw new CatalogSourceError(`Remote catalog request for "${locale}" timed out.`, { cause: error });
|
|
174
|
+
throw new CatalogSourceError(`Remote catalog response for "${locale}" was not valid JSON.`, { cause: error });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (error instanceof CatalogSourceError) throw error;
|
|
179
|
+
if (signal?.aborted) throw signal.reason ?? error;
|
|
180
|
+
if (timedOut) throw new CatalogSourceError(`Remote catalog request for "${locale}" timed out.`, { cause: error });
|
|
181
|
+
if (attempt < retries) retryNetworkFailure = true;
|
|
182
|
+
else throw new CatalogSourceError(`Remote catalog request for "${locale}" failed.`, { cause: error });
|
|
183
|
+
} finally {
|
|
184
|
+
clearTimeout(timeout);
|
|
185
|
+
signal?.removeEventListener("abort", abortFromCaller);
|
|
186
|
+
}
|
|
187
|
+
if (retryStatus !== void 0 || retryNetworkFailure) {
|
|
188
|
+
await wait(100 * 2 ** attempt, signal);
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (signal?.aborted) throw signal.reason ?? new DOMException("Aborted", "AbortError");
|
|
192
|
+
const catalog = copyCatalog(payload, locale);
|
|
193
|
+
const selectedCatalog = selectNamespace(catalog, namespace, locale);
|
|
194
|
+
if (cacheTtlMs > 0) cache.set(cacheKey, {
|
|
195
|
+
catalog: selectedCatalog,
|
|
196
|
+
expiresAt: Date.now() + cacheTtlMs
|
|
197
|
+
});
|
|
198
|
+
return selectedCatalog;
|
|
199
|
+
}
|
|
200
|
+
throw new CatalogSourceError(`Remote catalog request for "${locale}" exhausted its retries.`);
|
|
201
|
+
};
|
|
202
|
+
const invalidate = (inputLocale, namespace) => {
|
|
203
|
+
if (!inputLocale) {
|
|
204
|
+
cache.clear();
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const locale = canonicalize(inputLocale);
|
|
208
|
+
if (namespace !== void 0) {
|
|
209
|
+
const namespacePrefix = `${locale}\u0000${namespace}\u0000`;
|
|
210
|
+
for (const key of cache.keys()) if (key.startsWith(namespacePrefix)) cache.delete(key);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
for (const key of cache.keys()) if (key.startsWith(`${locale}\u0000`)) cache.delete(key);
|
|
214
|
+
};
|
|
215
|
+
return Object.freeze({
|
|
216
|
+
load,
|
|
217
|
+
invalidate
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
export { CatalogSourceError, createMemoryCatalogSource, createRemoteCatalogSource, createTypeScriptCatalogSource, parseJsonCatalog };
|
|
221
|
+
|
|
222
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/sources/index.ts"],"sourcesContent":["import type { MessageCatalog } from \"../runtime\";\n\nexport interface CatalogSource {\n load(\n locale: string,\n namespace?: string,\n signal?: AbortSignal,\n ): Promise<MessageCatalog>;\n invalidate?(locale?: string, namespace?: string): void;\n}\n\nexport interface RemoteCatalogSource extends CatalogSource {\n invalidate(locale?: string, namespace?: string): void;\n}\n\nexport class CatalogSourceError extends Error {\n constructor(\n message: string,\n options?: { cause?: unknown },\n ) {\n super(message, options);\n this.name = \"CatalogSourceError\";\n }\n}\n\nconst isCatalogObject = (\n value: unknown,\n): value is Record<string, unknown> => {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n};\n\nconst canonicalize = (locale: string): string => {\n try {\n const [canonical] = Intl.getCanonicalLocales(locale);\n\n if (!canonical) {\n throw new Error();\n }\n\n return canonical;\n } catch {\n throw new CatalogSourceError(`Invalid BCP 47 catalog locale \"${locale}\".`);\n }\n};\n\nconst copyCatalog = (\n value: unknown,\n locale: string,\n path = \"\",\n ancestors = new WeakSet<object>(),\n): MessageCatalog => {\n if (!isCatalogObject(value)) {\n throw new CatalogSourceError(\n `Catalog for \"${locale}\" at \"${path || \"<root>\"}\" must be a plain object.`,\n );\n }\n\n if (ancestors.has(value)) {\n throw new CatalogSourceError(\n `Catalog for \"${locale}\" contains a circular reference at \"${path || \"<root>\"}\".`,\n );\n }\n\n ancestors.add(value);\n const entries = Object.entries(value).map(([key, entry]) => {\n const keyPath = path ? `${path}.${key}` : key;\n\n if (!key || key.includes(\".\")) {\n throw new CatalogSourceError(\n `Catalog for \"${locale}\" has an invalid key \"${key}\" at \"${path || \"<root>\"}\".`,\n );\n }\n\n if (typeof entry === \"string\") {\n return [key, entry] as const;\n }\n\n return [key, copyCatalog(entry, locale, keyPath, ancestors)] as const;\n });\n\n ancestors.delete(value);\n return Object.freeze(Object.fromEntries(entries));\n};\n\nconst selectNamespace = (\n catalog: MessageCatalog,\n namespace: string | undefined,\n locale: string,\n): MessageCatalog => {\n if (!namespace) {\n return catalog;\n }\n\n let value: unknown = catalog;\n\n for (const part of namespace.split(\".\")) {\n if (!part || !isCatalogObject(value) || !Object.hasOwn(value, part)) {\n throw new CatalogSourceError(\n `Catalog namespace \"${namespace}\" was not found for \"${locale}\".`,\n );\n }\n\n value = value[part];\n }\n\n if (!isCatalogObject(value)) {\n throw new CatalogSourceError(\n `Catalog namespace \"${namespace}\" for \"${locale}\" is not an object.`,\n );\n }\n\n return value as MessageCatalog;\n};\n\nexport const parseJsonCatalog = (\n json: string,\n locale: string,\n): MessageCatalog => {\n const canonicalLocale = canonicalize(locale);\n let parsed: unknown;\n\n try {\n parsed = JSON.parse(json);\n } catch (error) {\n throw new CatalogSourceError(\n `Catalog JSON for \"${canonicalLocale}\" is invalid.`,\n { cause: error },\n );\n }\n\n return copyCatalog(parsed, canonicalLocale);\n};\n\nexport interface MemoryCatalogSourceOptions {\n catalogs: Readonly<Record<string, MessageCatalog>>;\n}\n\nexport const createMemoryCatalogSource = (\n options: MemoryCatalogSourceOptions,\n): CatalogSource => {\n const catalogs = new Map<string, MessageCatalog>();\n\n for (const [inputLocale, catalog] of Object.entries(options.catalogs)) {\n const locale = canonicalize(inputLocale);\n\n if (catalogs.has(locale)) {\n throw new CatalogSourceError(`Duplicate catalog for locale \"${locale}\".`);\n }\n\n catalogs.set(locale, copyCatalog(catalog, locale));\n }\n\n return Object.freeze({\n async load(locale: string, namespace?: string, signal?: AbortSignal) {\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"Aborted\", \"AbortError\");\n }\n\n const canonicalLocale = canonicalize(locale);\n const catalog = catalogs.get(canonicalLocale);\n\n if (!catalog) {\n throw new CatalogSourceError(\n `No in-memory catalog exists for \"${canonicalLocale}\".`,\n );\n }\n\n return selectNamespace(\n catalog,\n namespace,\n canonicalLocale,\n );\n },\n });\n};\n\nexport interface CatalogValidationContext {\n readonly locale: string;\n}\n\nexport type CatalogSchemaValidator = (\n catalog: unknown,\n context: CatalogValidationContext,\n) => void;\n\nexport interface TypeScriptCatalogSourceOptions<\n Catalogs extends Readonly<Record<string, MessageCatalog>>,\n> {\n catalogs: Catalogs;\n validate?: CatalogSchemaValidator;\n}\n\nexport interface TypeScriptCatalogSource<\n Catalogs extends Readonly<Record<string, MessageCatalog>>,\n> {\n load(\n locale: string,\n namespace?: undefined,\n signal?: AbortSignal,\n ): Promise<Catalogs[keyof Catalogs]>;\n load(\n locale: string,\n namespace: string,\n signal?: AbortSignal,\n ): Promise<MessageCatalog>;\n}\n\nexport const createTypeScriptCatalogSource = <\n Catalogs extends Readonly<Record<string, MessageCatalog>>,\n>(\n options: TypeScriptCatalogSourceOptions<Catalogs>,\n): TypeScriptCatalogSource<Catalogs> => {\n const catalogs = new Map<string, MessageCatalog>();\n\n for (const [inputLocale, inputCatalog] of Object.entries(options.catalogs)) {\n const locale = canonicalize(inputLocale);\n\n if (catalogs.has(locale)) {\n throw new CatalogSourceError(`Duplicate catalog for locale \"${locale}\".`);\n }\n\n const catalog = copyCatalog(inputCatalog, locale);\n options.validate?.(catalog, { locale });\n catalogs.set(locale, catalog);\n }\n\n return Object.freeze({\n async load(locale: string, namespace?: string, signal?: AbortSignal) {\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"Aborted\", \"AbortError\");\n }\n\n const canonicalLocale = canonicalize(locale);\n const catalog = catalogs.get(canonicalLocale);\n\n if (!catalog) {\n throw new CatalogSourceError(\n `No TypeScript catalog exists for \"${canonicalLocale}\".`,\n );\n }\n\n return selectNamespace(catalog, namespace, canonicalLocale);\n },\n }) as TypeScriptCatalogSource<Catalogs>;\n};\n\nexport interface RemoteCatalogSourceOptions {\n url(locale: string, namespace?: string): string | URL;\n headers?: HeadersInit;\n fetcher?: typeof fetch;\n timeoutMs?: number;\n retries?: number;\n cacheTtlMs?: number;\n maxResponseBytes?: number;\n}\n\ninterface CachedCatalog {\n catalog: MessageCatalog;\n expiresAt: number;\n}\n\nconst validateRemoteUrl = (value: string | URL): URL => {\n let url: URL;\n\n try {\n url = new URL(value);\n } catch (error) {\n throw new CatalogSourceError(\"Remote catalog URL must be absolute.\", {\n cause: error,\n });\n }\n\n if (\n (url.protocol !== \"https:\" && url.protocol !== \"http:\") ||\n url.username ||\n url.password ||\n url.hash\n ) {\n throw new CatalogSourceError(\n \"Remote catalog URLs must use HTTP(S) and must not contain credentials or a hash.\",\n );\n }\n\n return url;\n};\n\nconst wait = (milliseconds: number, signal?: AbortSignal): Promise<void> =>\n new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(signal.reason ?? new DOMException(\"Aborted\", \"AbortError\"));\n return;\n }\n\n const timer = setTimeout(() => {\n signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, milliseconds);\n const onAbort = () => {\n clearTimeout(timer);\n signal?.removeEventListener(\"abort\", onAbort);\n reject(signal?.reason ?? new DOMException(\"Aborted\", \"AbortError\"));\n };\n\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n\nexport const createRemoteCatalogSource = (\n options: RemoteCatalogSourceOptions,\n): RemoteCatalogSource => {\n const timeoutMs = options.timeoutMs ?? 5_000;\n const retries = options.retries ?? 1;\n const cacheTtlMs = options.cacheTtlMs ?? 30_000;\n const maxResponseBytes = options.maxResponseBytes ?? 1_048_576;\n const fetcher = options.fetcher ?? globalThis.fetch;\n const headers = new Headers(options.headers);\n const cache = new Map<string, CachedCatalog>();\n const encoder = new TextEncoder();\n\n if (!Number.isInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 60_000) {\n throw new RangeError(\"Remote catalog timeoutMs must be between 1 and 60000.\");\n }\n\n if (!Number.isInteger(retries) || retries < 0 || retries > 5) {\n throw new RangeError(\"Remote catalog retries must be between 0 and 5.\");\n }\n\n if (!Number.isInteger(cacheTtlMs) || cacheTtlMs < 0) {\n throw new RangeError(\"Remote catalog cacheTtlMs must be a non-negative integer.\");\n }\n\n if (\n !Number.isInteger(maxResponseBytes) ||\n maxResponseBytes < 1 ||\n maxResponseBytes > 10_485_760\n ) {\n throw new RangeError(\n \"Remote catalog maxResponseBytes must be between 1 and 10485760.\",\n );\n }\n\n if (typeof fetcher !== \"function\") {\n throw new CatalogSourceError(\"A Fetch-compatible implementation is required.\");\n }\n\n const load = async (\n inputLocale: string,\n namespace?: string,\n signal?: AbortSignal,\n ): Promise<MessageCatalog> => {\n const locale = canonicalize(inputLocale);\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"Aborted\", \"AbortError\");\n }\n\n let url: URL;\n\n try {\n url = validateRemoteUrl(options.url(locale, namespace));\n } catch (error) {\n if (error instanceof CatalogSourceError) {\n throw error;\n }\n\n throw new CatalogSourceError(\n `Could not resolve a remote catalog URL for \"${locale}\".`,\n { cause: error },\n );\n }\n\n const cacheKey = `${locale}\\u0000${namespace ?? \"\"}\\u0000${url.href}`;\n const cached = cache.get(cacheKey);\n\n if (cached && cached.expiresAt > Date.now()) {\n return cached.catalog;\n }\n\n for (let attempt = 0; attempt <= retries; attempt += 1) {\n const controller = new AbortController();\n let timedOut = false;\n const timeout = setTimeout(\n () => {\n timedOut = true;\n controller.abort(new Error(\"Remote catalog request timed out.\"));\n },\n timeoutMs,\n );\n const abortFromCaller = () =>\n controller.abort(\n signal?.reason ?? new DOMException(\"Aborted\", \"AbortError\"),\n );\n signal?.addEventListener(\"abort\", abortFromCaller, { once: true });\n\n let retryStatus: number | undefined;\n let retryNetworkFailure = false;\n let payload: unknown;\n\n try {\n const response = await fetcher(url, {\n headers: new Headers(headers),\n signal: controller.signal,\n redirect: \"error\",\n });\n\n const isRedirectStatus =\n response.status >= 300 && response.status < 400;\n\n if (response.redirected || isRedirectStatus) {\n const statusDetail = isRedirectStatus\n ? ` (HTTP ${response.status})`\n : \"\";\n\n throw new CatalogSourceError(\n `Remote catalog redirects are not allowed${statusDetail}.`,\n );\n }\n\n if (!response.ok) {\n const retryable =\n response.status === 408 ||\n response.status === 429 ||\n response.status >= 500;\n\n if (retryable && attempt < retries) {\n retryStatus = response.status;\n } else {\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" returned HTTP ${response.status}.`,\n );\n }\n } else {\n const declaredLength = Number(response.headers.get(\"content-length\"));\n\n if (Number.isFinite(declaredLength) && declaredLength > maxResponseBytes) {\n throw new CatalogSourceError(\n `Remote catalog response for \"${locale}\" exceeds the configured size limit.`,\n );\n }\n\n try {\n const body = await response.text();\n\n if (encoder.encode(body).byteLength > maxResponseBytes) {\n throw new CatalogSourceError(\n `Remote catalog response for \"${locale}\" exceeds the configured size limit.`,\n );\n }\n\n payload = JSON.parse(body);\n } catch (error) {\n if (error instanceof CatalogSourceError) {\n throw error;\n }\n\n if (signal?.aborted) {\n throw signal.reason ?? error;\n }\n\n if (timedOut) {\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" timed out.`,\n { cause: error },\n );\n }\n\n throw new CatalogSourceError(\n `Remote catalog response for \"${locale}\" was not valid JSON.`,\n { cause: error },\n );\n }\n }\n } catch (error) {\n if (error instanceof CatalogSourceError) {\n throw error;\n }\n\n if (signal?.aborted) {\n throw signal.reason ?? error;\n }\n\n if (timedOut) {\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" timed out.`,\n { cause: error },\n );\n }\n\n if (attempt < retries) {\n retryNetworkFailure = true;\n } else {\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" failed.`,\n { cause: error },\n );\n }\n } finally {\n clearTimeout(timeout);\n signal?.removeEventListener(\"abort\", abortFromCaller);\n }\n\n if (retryStatus !== undefined || retryNetworkFailure) {\n await wait(100 * 2 ** attempt, signal);\n continue;\n }\n\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"Aborted\", \"AbortError\");\n }\n\n const catalog = copyCatalog(payload, locale);\n const selectedCatalog = selectNamespace(catalog, namespace, locale);\n\n if (cacheTtlMs > 0) {\n cache.set(cacheKey, {\n catalog: selectedCatalog,\n expiresAt: Date.now() + cacheTtlMs,\n });\n }\n\n return selectedCatalog;\n }\n\n throw new CatalogSourceError(\n `Remote catalog request for \"${locale}\" exhausted its retries.`,\n );\n };\n\n const invalidate = (inputLocale?: string, namespace?: string): void => {\n if (!inputLocale) {\n cache.clear();\n return;\n }\n\n const locale = canonicalize(inputLocale);\n\n if (namespace !== undefined) {\n const namespacePrefix = `${locale}\\u0000${namespace}\\u0000`;\n\n for (const key of cache.keys()) {\n if (key.startsWith(namespacePrefix)) {\n cache.delete(key);\n }\n }\n\n return;\n }\n\n for (const key of cache.keys()) {\n if (key.startsWith(`${locale}\\u0000`)) {\n cache.delete(key);\n }\n }\n };\n\n return Object.freeze({ load, invalidate });\n};\n"],"mappings":";AAeA,IAAa,qBAAb,cAAwC,MAAM;CAC5C,YACE,SACA,SACA;EACA,MAAM,SAAS,OAAO;EACtB,KAAK,OAAO;CACd;AACF;AAEA,MAAM,mBACJ,UACqC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