@menapo/vi18n 0.1.0 → 0.1.2
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 +14 -14
- package/dist/{apply-Bre-lY9J.js → apply-FAHl1R6T.js} +69 -89
- package/dist/{captured-Bj8bO6vd.js → captured-CTjjDF5o.js} +196 -217
- package/dist/{check-DyrAbv73.js → check-DwFwOB1d.js} +66 -84
- package/dist/cli.js +2 -3
- package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
- package/dist/collisions-uMTuq36M.js +215 -0
- package/dist/compile-CEvH-6sY.js +240 -0
- package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
- package/dist/concurrency-BCY_4zcr.js +1 -0
- package/dist/concurrency-Dy2ny50V.js +107 -0
- package/dist/config.d.ts +1 -5
- package/dist/config.js +4 -5
- package/dist/{types-D3QGM8XO.js → diagnostics-1onDF6Ua.js} +26 -26
- package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
- package/dist/{duplicates-yuRljQ6h.js → duplicates-CsUXRX2J.js} +10 -27
- package/dist/engine-Bf7nVigL.js +556 -0
- package/dist/engine.d.ts +1 -5
- package/dist/engine.js +297 -168
- package/dist/exchange-DYG9RZoT.js +41 -0
- package/dist/{exclude-Du-Zd_qI.js → exclude-vMnm3lqU.js} +2 -3
- package/dist/{export-DGsMbcTZ.js → export-BYq0gfLd.js} +17 -38
- package/dist/{extract-Dujlsgnk.js → extract-CYtuEo8u.js} +114 -82
- package/dist/{formats-KPIg-08V.js → formats-ChV1LVH_.js} +352 -276
- package/dist/fs-ezCMZeDV.js +52 -0
- package/dist/i18next-1waHziMy.js +1 -0
- package/dist/i18next-D66Icz3L.js +1907 -0
- package/dist/{import-Cwdhcywt.js → import-BApbQlmm.js} +81 -75
- package/dist/index.d.ts +1 -13
- package/dist/index.js +14 -16
- package/dist/{init-BecCTvKi.js → init-J7AwwA5e.js} +466 -501
- package/dist/{library-D5CWvTQs.js → library-BfSOBeBd.js} +975 -451
- package/dist/lingui-Ee2farVo.js +902 -0
- package/dist/lint.d.ts +0 -4
- package/dist/lint.js +69 -82
- package/dist/{load-gW-oJt1Q.js → load-BFLLqY8x.js} +272 -147
- package/dist/{load-CQRz4PpM.js → load-CMwhukZ4.js} +1211 -1115
- package/dist/{locales-DX4MOBFE.js → locales-DxLDrx-h.js} +271 -237
- package/dist/{lsp-Cq1Nuu-Q.js → lsp-BC7xDGON.js} +4 -5
- package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
- package/dist/{mcp-Cf0T7qPW.js → mcp-Cka8ywDF.js} +4 -5
- package/dist/{merge-driver-CpiiSVIc.js → merge-driver-B04Apm29.js} +138 -122
- package/dist/migrate-D7TNl6SD.js +2169 -0
- package/dist/migrate-khCY8TE0.js +1 -0
- package/dist/{openai-CTaMDcl-.js → openai-RWw0YlAx.js} +128 -87
- package/dist/{output-DsaHKHEt.js → output-CF9uvk_-.js} +15 -9
- package/dist/parse.d.ts +1 -3
- package/dist/parse.js +7 -8
- package/dist/{pipeline-wxS0SApG.js → pipeline-B30irGMD.js} +1 -1
- package/dist/{pipeline-C7g11THL.js → pipeline-sgl_q_op.js} +387 -359
- package/dist/project-DSzWKU3C.js +544 -0
- package/dist/{pseudo-DRv30iN5.js → pseudo-DNbNNVGP.js} +125 -3
- package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
- package/dist/react-intl-tzGIk6G3.js +634 -0
- package/dist/restore-BG_zMpSj.js +199 -0
- package/dist/rewrite-hAcRJwiZ.js +415 -0
- package/dist/{rich-DCUKl5HM.js → rich-ApY1Dm4S.js} +35 -28
- package/dist/rolldown-runtime-C1I3wEg2.js +43 -0
- package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
- package/dist/run-CFzR3V93.js +647 -0
- package/dist/scan-worker.d.ts +2 -4
- package/dist/scan-worker.js +1 -1
- package/dist/{scoped-BMYpSthj.js → scoped-h3KE8XWW.js} +3 -4
- package/dist/{server-BDkykMjb.js → server-WlxZH0oX.js} +275 -330
- package/dist/{server-qv3mz70d.js → server-ceuzzkal.js} +292 -386
- package/dist/server.d.ts +1 -17
- package/dist/server.js +415 -381
- package/dist/{sitemap-DV3o8QD7.js → sitemap-796jJKUT.js} +21 -33
- package/dist/{sources-D19czDSA.js → sources-DuOdM02f.js} +712 -373
- package/dist/{status-DZArW5aE.js → status-CTH3qI-0.js} +30 -30
- package/dist/{stdio-FPOKQAgu.js → stdio-D9kTzyqB.js} +2 -4
- package/dist/{stdio-CVaJfxA7.js → stdio-FWIVU3P4.js} +1 -2
- package/dist/sync-CpL0Qsgw.js +1352 -0
- package/dist/{t-DC-c9zE5.js → t-gvEXx25s.js} +76 -53
- package/dist/test.d.ts +5 -16
- package/dist/test.js +77 -48
- package/dist/translate-BrfVRkjl.js +336 -0
- package/dist/{ts-BaU5yOf0.js → ts-BX6kwig5.js} +25 -14
- package/dist/{types-DtvQ7UdD.js → types-3kwyaCAm.js} +1 -2
- package/dist/{types-DsdWBG0b.d.ts → types-BaG1LboH.d.ts} +2 -6
- package/dist/{types-CgUCkFU1.d.ts → types-Cy2bPdSg.d.ts} +14 -20
- package/dist/types-Ds_vebja.js +41 -0
- package/dist/{units-CQ5Q99RC.js → units-VVLgQ7x7.js} +7 -16
- package/dist/{url-CRfCOhUI.js → url-Bpc6wLea.js} +24 -11
- package/dist/{url-CsBkRaJy.js → url-D2_VlEeH.js} +25 -12
- package/dist/{validate-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
- package/dist/{validate-BxpupdAf.js → validate-zv4-IZyZ.js} +690 -377
- package/dist/{version-BlyLGlOa.js → version-BFs370Ce.js} +3 -5
- package/dist/{vite-scan-B_YW4X_a.js → vite-scan-DFKSGVE9.js} +87 -92
- package/dist/vite.d.ts +2 -6
- package/dist/vite.js +6482 -6018
- package/dist/vue-i18n-CBmZqHbG.js +806 -0
- package/dist/{which-C6PFI1Dm.js → which-BpyJyBkX.js} +44 -37
- package/dist/{worker-84U6uYB3.js → worker-BjzVqJVO.js} +454 -608
- package/package.json +8 -8
- package/dist/check-D34P-S--.js +0 -1
- package/dist/codec-DuvU2VQy.js +0 -207
- package/dist/collisions-CfpZU8e4.js +0 -217
- package/dist/compile-B5Mz6WJm.js +0 -245
- package/dist/concurrency-C9UUWuDC.js +0 -96
- package/dist/concurrency-DONL7t4V.js +0 -1
- package/dist/engine-B7p40M__.js +0 -531
- package/dist/i18next-CrUnKFfh.js +0 -1596
- package/dist/i18next-Rgo-v9l0.js +0 -1
- package/dist/lingui--PcOOYry.js +0 -833
- package/dist/messages-Cxa-5tR0.js +0 -166
- package/dist/migrate-CntqvrW4.js +0 -1
- package/dist/migrate-dPgq9uzy.js +0 -1484
- package/dist/project-Dwx7iPm6.js +0 -381
- package/dist/project-pXipT8iG.js +0 -1046
- package/dist/react-intl-CaBVwJ9u.js +0 -519
- package/dist/restore-Btq66Tid.js +0 -175
- package/dist/rewrite-CiTRAICl.js +0 -803
- package/dist/run-DYD_VXKy.js +0 -694
- package/dist/runtime-DTMmliT1.js +0 -64
- package/dist/sync-CcVzger_.js +0 -658
- package/dist/translate-DZR3MkSA.js +0 -299
- package/dist/types-wSh9fCBY.js +0 -11
- package/dist/vue-i18n-CEMnpv7k.js +0 -651
package/dist/server.js
CHANGED
|
@@ -1,42 +1,80 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as
|
|
3
|
-
import { r as
|
|
4
|
-
import { t as createScopedApi } from "./scoped-
|
|
1
|
+
import { b as warnOnce, m as createScope, n as S, o as noop, t as textDirection, v as own } from "./dir-TGLYuOZb.js";
|
|
2
|
+
import { a as matchLocale, c as urlLocale, o as readCookie, r as TIME_ZONE_COOKIE, t as LOCALE_HEADER } from "./match-BLDcj610.js";
|
|
3
|
+
import { a as baseOf, c as localeOrigin, d as matchRoute, f as routePath, i as awayHref, l as localizePath, m as unprefixedLocale, n as atHome, p as splitLocalePath, r as atHost, s as hostLocale, u as localizedPathsFor } from "./url-D2_VlEeH.js";
|
|
4
|
+
import { t as createScopedApi } from "./scoped-h3KE8XWW.js";
|
|
5
5
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
|
-
|
|
6
|
+
// src/server/env.ts
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* loading at registration, so `State.serverFallback` exists before the first
|
|
13
|
-
* request and untransformed code resolves source literals from the start.
|
|
14
|
-
*
|
|
15
|
-
* Module-level variables rather than one state object: the minifier renames
|
|
16
|
-
* them, which keeps `/server` inside its PLAN §6 budget.
|
|
8
|
+
* The plugin's defines as `/server` reads them. The packed `/server` imports
|
|
9
|
+
* the runtime chunk, where a constant cannot fold this entry's branches out
|
|
10
|
+
* (§6 D134), so it reads them here again rather than importing the
|
|
11
|
+
* runtime's.
|
|
17
12
|
*/
|
|
18
|
-
const S = getState();
|
|
19
13
|
/**
|
|
20
|
-
* Dev mode
|
|
21
|
-
*
|
|
14
|
+
* Dev mode, read the way the runtime reads it: code bundled without the
|
|
15
|
+
* plugin behaves as production.
|
|
22
16
|
*/
|
|
23
17
|
const DEV = typeof __VI18N_DEV__ !== "undefined" && !!__VI18N_DEV__;
|
|
24
|
-
/**
|
|
25
|
-
* `ROUTES` of the URL codec, read here again: the packed `/server` imports
|
|
26
|
-
* the runtime chunk, where a constant cannot fold this entry's branches out
|
|
27
|
-
* (§6 D134).
|
|
28
|
-
*/
|
|
18
|
+
/** `ROUTES` of the URL codec: localized paths and domains are compiled in. */
|
|
29
19
|
const ROUTES = typeof __VI18N_ROUTES__ === "undefined" || !!__VI18N_ROUTES__;
|
|
30
|
-
const own = Object.hasOwn;
|
|
31
|
-
const noop = () => {};
|
|
32
20
|
const error = (text) => /* @__PURE__ */ new Error("vi18n: " + text);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
21
|
+
// src/server/als.ts
|
|
22
|
+
/**
|
|
23
|
+
* Request isolation (PLAN §5.13 "Isolation"). Scopes travel in an
|
|
24
|
+
* AsyncLocalStorage; there is deliberately no synchronous-slot fallback,
|
|
25
|
+
* because a slot still readable after an `await` would serve one request's
|
|
26
|
+
* locale to another.
|
|
27
|
+
*/
|
|
28
|
+
const als = S.serverScopes ??= new AsyncLocalStorage();
|
|
29
|
+
/** Framework request stores (SvelteKit `getRequestEvent()`) supply the scope; null removes it. */
|
|
30
|
+
function setScopeResolver(resolve) {
|
|
31
|
+
als.resolver = resolve;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The current scope. A `withLocale`/`createTranslator` scope entered inside
|
|
35
|
+
* a request outranks the framework's request store: the store would still
|
|
36
|
+
* name the request, and an e-mail rendered for another user inside it must
|
|
37
|
+
* use that user's locale. Otherwise the resolver wins over the ALS, as
|
|
38
|
+
* frameworks may run render code outside the async context `handle` opened.
|
|
39
|
+
*/
|
|
40
|
+
function getScope() {
|
|
41
|
+
const s = als.getStore();
|
|
42
|
+
const user = als.resolver;
|
|
43
|
+
if (s?.kind === "translator" || !user) return s;
|
|
44
|
+
let u;
|
|
45
|
+
try {
|
|
46
|
+
u = user();
|
|
47
|
+
} catch {}
|
|
48
|
+
return u ?? s;
|
|
39
49
|
}
|
|
50
|
+
S.serverResolver = getScope;
|
|
51
|
+
/**
|
|
52
|
+
* Some ALS implementations resolve but do not propagate (polyfills that hold
|
|
53
|
+
* the store only synchronously): reading another request's locale later
|
|
54
|
+
* would be silent, so the first scope opener checks that a store survives
|
|
55
|
+
* an `await`, and fails loudly otherwise.
|
|
56
|
+
*/
|
|
57
|
+
async function assertPropagation(store) {
|
|
58
|
+
const k = {};
|
|
59
|
+
if (!await store.run(k, async () => {
|
|
60
|
+
await Promise.resolve();
|
|
61
|
+
return store.getStore() === k;
|
|
62
|
+
})) throw error("AsyncLocalStorage loses its store across await; enable nodejs_als or nodejs_compat (compatibility date 2026-08-04+)");
|
|
63
|
+
}
|
|
64
|
+
let probed;
|
|
65
|
+
let ok = false;
|
|
66
|
+
/** Runs the probe once per process; a failure is remembered, so every request reports it. */
|
|
67
|
+
function propagation() {
|
|
68
|
+
if (ok) return void 0;
|
|
69
|
+
return probed ??= assertPropagation(als).then(() => {
|
|
70
|
+
ok = true;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
// src/server/validate.ts
|
|
74
|
+
/**
|
|
75
|
+
* Checks of untyped input (INTERNALS §3.2): `config.json` from `vi18n compile
|
|
76
|
+
* --out`, catalog and `sources` modules, and time zones from requests.
|
|
77
|
+
*/
|
|
40
78
|
const zones = /* @__PURE__ */ new Set();
|
|
41
79
|
/**
|
|
42
80
|
* True for an IANA zone name this engine's Intl accepts (INTERNALS §3.2).
|
|
@@ -56,6 +94,88 @@ function isTimeZone(zone) {
|
|
|
56
94
|
if (zones.size < 1024) zones.add(zone);
|
|
57
95
|
return true;
|
|
58
96
|
}
|
|
97
|
+
const isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
98
|
+
/** A module namespace's default export, or the object itself (both are accepted everywhere). */
|
|
99
|
+
function unwrap(m) {
|
|
100
|
+
return isObject(m) && own(m, "default") && isObject(m.default) ? m.default : m;
|
|
101
|
+
}
|
|
102
|
+
const STRATEGIES = [
|
|
103
|
+
"url",
|
|
104
|
+
"cookie",
|
|
105
|
+
"header",
|
|
106
|
+
"localStorage",
|
|
107
|
+
"default"
|
|
108
|
+
];
|
|
109
|
+
const RULES = [
|
|
110
|
+
["locales", []],
|
|
111
|
+
["pseudoLocales", []],
|
|
112
|
+
["fallbacks", { values: [] }],
|
|
113
|
+
["routing", {}],
|
|
114
|
+
["routing.strategy", [STRATEGIES]],
|
|
115
|
+
["routing.routeStrategies", { values: [STRATEGIES] }],
|
|
116
|
+
["routing.url", [
|
|
117
|
+
"prefix_except_default",
|
|
118
|
+
"prefix",
|
|
119
|
+
"prefix_and_default",
|
|
120
|
+
"no_prefix"
|
|
121
|
+
]],
|
|
122
|
+
["routing.redirectRoot", "boolean"],
|
|
123
|
+
["routing.propagate", ["header", false]],
|
|
124
|
+
["routing.switch", ["in-place", "navigate"]],
|
|
125
|
+
["routing.cookie", {}],
|
|
126
|
+
["routing.cookie.name", "string"],
|
|
127
|
+
["routing.cookie.maxAge", "number"],
|
|
128
|
+
["routing.base", "string"],
|
|
129
|
+
["ssr", {}],
|
|
130
|
+
["ssr.timeZone", "string"],
|
|
131
|
+
["ssr.timeZoneFrom", { values: ["config", "request"] }],
|
|
132
|
+
["ssr.normalizeSpaces", "boolean"]
|
|
133
|
+
];
|
|
134
|
+
function matches(v, rule) {
|
|
135
|
+
if (typeof rule === "string") return typeof v === rule;
|
|
136
|
+
if (Array.isArray(rule)) {
|
|
137
|
+
if (!rule.length || Array.isArray(rule[0])) {
|
|
138
|
+
const list = rule[0];
|
|
139
|
+
return Array.isArray(v) && v.every((x) => list ? list.includes(x) : typeof x === "string");
|
|
140
|
+
}
|
|
141
|
+
return rule.includes(v);
|
|
142
|
+
}
|
|
143
|
+
return isObject(v) && (!rule.values || Object.values(v).every((x) => matches(x, rule.values)));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* `config.json` from `vi18n compile --out` arrives as untyped JSON: each
|
|
147
|
+
* field the server reads is checked, so a stale or hand-edited file fails at
|
|
148
|
+
* startup with the field's name instead of mid-request.
|
|
149
|
+
*/
|
|
150
|
+
function validateConfig(input) {
|
|
151
|
+
const c = unwrap(input);
|
|
152
|
+
const fail = (path) => {
|
|
153
|
+
throw new TypeError("vi18n: registerCatalogs: invalid config: " + path);
|
|
154
|
+
};
|
|
155
|
+
if (!isObject(c)) fail("not an object");
|
|
156
|
+
for (const [path, rule] of RULES) if (!matches(path.split(".").reduce((o, k) => o[k], c), rule)) fail(path);
|
|
157
|
+
if (!c.locales.length) fail("locales");
|
|
158
|
+
if (!c.locales.includes(c.sourceLocale)) fail("sourceLocale");
|
|
159
|
+
if (ROUTES) {
|
|
160
|
+
const { paths, domains } = c.routing;
|
|
161
|
+
if (paths !== void 0 && !matches(paths, { values: { values: "string" } })) fail("routing.paths");
|
|
162
|
+
if (domains !== void 0 && !matches(domains, { values: "string" })) fail("routing.domains");
|
|
163
|
+
}
|
|
164
|
+
if (!isTimeZone(c.ssr.timeZone)) fail("ssr.timeZone");
|
|
165
|
+
return c;
|
|
166
|
+
}
|
|
167
|
+
// src/server/catalogs.ts
|
|
168
|
+
/**
|
|
169
|
+
* Server registration and catalogs (INTERNALS §3.2 "Server registration").
|
|
170
|
+
* The boot module (or `registerCatalogs`) registers once; from then on this
|
|
171
|
+
* module owns what `/server` loads for it: the per-locale catalogs, the
|
|
172
|
+
* `sources` map and the client plan. The source catalog and `sources` start
|
|
173
|
+
* loading at registration, so `State.serverFallback` exists before the first
|
|
174
|
+
* request and untransformed code resolves source literals from the start.
|
|
175
|
+
*
|
|
176
|
+
* Module-level variables rather than one state object: the minifier renames
|
|
177
|
+
* them, which keeps `/server` inside its PLAN §6 budget.
|
|
178
|
+
*/
|
|
59
179
|
/** The registration served; replaced when the state registers again. */
|
|
60
180
|
let reg = null;
|
|
61
181
|
let config;
|
|
@@ -117,11 +237,6 @@ function ready() {
|
|
|
117
237
|
throw e;
|
|
118
238
|
});
|
|
119
239
|
}
|
|
120
|
-
const isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
121
|
-
/** A module namespace's default export, or the object itself (both are accepted everywhere). */
|
|
122
|
-
function unwrap(m) {
|
|
123
|
-
return isObject(m) && own(m, "default") && isObject(m.default) ? m.default : m;
|
|
124
|
-
}
|
|
125
240
|
async function loadSources(r) {
|
|
126
241
|
if (!r.sources) return void 0;
|
|
127
242
|
const data = unwrap(await r.sources());
|
|
@@ -147,7 +262,7 @@ function catalog(locale) {
|
|
|
147
262
|
const into = loaded;
|
|
148
263
|
let c = into.get(locale);
|
|
149
264
|
if (!c) {
|
|
150
|
-
if (!loaders || !own(loaders, locale)) return Promise.reject(error("no server catalog for locale " + JSON.stringify(locale)));
|
|
265
|
+
if (!loaders || !own(loaders, locale)) return Promise.reject(error("no server catalog for locale " + JSON.stringify(locale) + (DEV ? "; run vi18n compile, or add it to the loaders registerCatalogs() gets" : "")));
|
|
151
266
|
if (DEV && dev) return loaders[locale]().then((m) => {
|
|
152
267
|
if (!isObject(m)) return toCatalog(locale, m);
|
|
153
268
|
let d = devMaps.get(m);
|
|
@@ -181,76 +296,11 @@ function loadPlan() {
|
|
|
181
296
|
throw e;
|
|
182
297
|
});
|
|
183
298
|
}
|
|
184
|
-
const STRATEGIES = [
|
|
185
|
-
"url",
|
|
186
|
-
"cookie",
|
|
187
|
-
"header",
|
|
188
|
-
"localStorage",
|
|
189
|
-
"default"
|
|
190
|
-
];
|
|
191
|
-
const RULES = [
|
|
192
|
-
["locales", []],
|
|
193
|
-
["pseudoLocales", []],
|
|
194
|
-
["fallbacks", { values: [] }],
|
|
195
|
-
["routing", {}],
|
|
196
|
-
["routing.strategy", [STRATEGIES]],
|
|
197
|
-
["routing.routeStrategies", { values: [STRATEGIES] }],
|
|
198
|
-
["routing.url", [
|
|
199
|
-
"prefix_except_default",
|
|
200
|
-
"prefix",
|
|
201
|
-
"prefix_and_default",
|
|
202
|
-
"no_prefix"
|
|
203
|
-
]],
|
|
204
|
-
["routing.redirectRoot", "boolean"],
|
|
205
|
-
["routing.propagate", ["header", false]],
|
|
206
|
-
["routing.switch", ["in-place", "navigate"]],
|
|
207
|
-
["routing.cookie", {}],
|
|
208
|
-
["routing.cookie.name", "string"],
|
|
209
|
-
["routing.cookie.maxAge", "number"],
|
|
210
|
-
["routing.base", "string"],
|
|
211
|
-
["ssr", {}],
|
|
212
|
-
["ssr.timeZone", "string"],
|
|
213
|
-
["ssr.timeZoneFrom", { values: ["config", "request"] }],
|
|
214
|
-
["ssr.normalizeSpaces", "boolean"]
|
|
215
|
-
];
|
|
216
|
-
function matches(v, rule) {
|
|
217
|
-
if (typeof rule === "string") return typeof v === rule;
|
|
218
|
-
if (Array.isArray(rule)) {
|
|
219
|
-
if (!rule.length || Array.isArray(rule[0])) {
|
|
220
|
-
const list = rule[0];
|
|
221
|
-
return Array.isArray(v) && v.every((x) => list ? list.includes(x) : typeof x === "string");
|
|
222
|
-
}
|
|
223
|
-
return rule.includes(v);
|
|
224
|
-
}
|
|
225
|
-
return isObject(v) && (!rule.values || Object.values(v).every((x) => matches(x, rule.values)));
|
|
226
|
-
}
|
|
227
299
|
/**
|
|
228
300
|
* The last registration `registerCatalogs()` made: a server built without
|
|
229
301
|
* the Vite plugin, so no client plan exists and no client reads a payload.
|
|
230
302
|
*/
|
|
231
303
|
let bare;
|
|
232
|
-
/**
|
|
233
|
-
* `config.json` from `vi18n compile --out` arrives as untyped JSON: each
|
|
234
|
-
* field the server reads is checked, so a stale or hand-edited file fails at
|
|
235
|
-
* startup with the field's name instead of mid-request.
|
|
236
|
-
*/
|
|
237
|
-
function validateConfig(input) {
|
|
238
|
-
const c = unwrap(input);
|
|
239
|
-
const fail = (path) => {
|
|
240
|
-
throw new TypeError("vi18n: registerCatalogs: invalid config: " + path);
|
|
241
|
-
};
|
|
242
|
-
if (!isObject(c)) fail("not an object");
|
|
243
|
-
for (const [path, rule] of RULES) if (!matches(path.split(".").reduce((o, k) => o[k], c), rule)) fail(path);
|
|
244
|
-
if (!c.locales.length) fail("locales");
|
|
245
|
-
if (!c.locales.includes(c.sourceLocale)) fail("sourceLocale");
|
|
246
|
-
if (ROUTES) {
|
|
247
|
-
const { paths, domains } = c.routing;
|
|
248
|
-
if (paths !== void 0 && !matches(paths, { values: { values: "string" } })) fail("routing.paths");
|
|
249
|
-
if (domains !== void 0 && !matches(domains, { values: "string" })) fail("routing.domains");
|
|
250
|
-
}
|
|
251
|
-
if (!isTimeZone(c.ssr.timeZone)) fail("ssr.timeZone");
|
|
252
|
-
return c;
|
|
253
|
-
}
|
|
254
304
|
function registerCatalogs(loaders, options) {
|
|
255
305
|
const config = validateConfig(options.config);
|
|
256
306
|
if (!isObject(loaders)) throw new TypeError("vi18n: registerCatalogs: loaders must be an object");
|
|
@@ -269,61 +319,7 @@ S.register = (r) => {
|
|
|
269
319
|
if (r.consumer === "server" && S.registration === r) current();
|
|
270
320
|
return done;
|
|
271
321
|
};
|
|
272
|
-
|
|
273
|
-
//#region src/server/als.ts
|
|
274
|
-
/**
|
|
275
|
-
* Request isolation (PLAN §5.13 "Isolation"). Scopes travel in an
|
|
276
|
-
* AsyncLocalStorage; there is deliberately no synchronous-slot fallback,
|
|
277
|
-
* because a slot still readable after an `await` would serve one request's
|
|
278
|
-
* locale to another.
|
|
279
|
-
*/
|
|
280
|
-
const als = S.serverScopes ??= new AsyncLocalStorage();
|
|
281
|
-
/** Framework request stores (SvelteKit `getRequestEvent()`) supply the scope; null removes it. */
|
|
282
|
-
function setScopeResolver(resolve) {
|
|
283
|
-
als.resolver = resolve;
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* The current scope. A `withLocale`/`createTranslator` scope entered inside
|
|
287
|
-
* a request outranks the framework's request store: the store would still
|
|
288
|
-
* name the request, and an e-mail rendered for another user inside it must
|
|
289
|
-
* use that user's locale. Otherwise the resolver wins over the ALS, as
|
|
290
|
-
* frameworks may run render code outside the async context `handle` opened.
|
|
291
|
-
*/
|
|
292
|
-
function getScope() {
|
|
293
|
-
const s = als.getStore();
|
|
294
|
-
const user = als.resolver;
|
|
295
|
-
if (s?.kind === "translator" || !user) return s;
|
|
296
|
-
let u;
|
|
297
|
-
try {
|
|
298
|
-
u = user();
|
|
299
|
-
} catch {}
|
|
300
|
-
return u ?? s;
|
|
301
|
-
}
|
|
302
|
-
S.serverResolver = getScope;
|
|
303
|
-
/**
|
|
304
|
-
* Some ALS implementations resolve but do not propagate (polyfills that hold
|
|
305
|
-
* the store only synchronously): reading another request's locale later
|
|
306
|
-
* would be silent, so the first scope opener checks that a store survives
|
|
307
|
-
* an `await`, and fails loudly otherwise.
|
|
308
|
-
*/
|
|
309
|
-
async function assertPropagation(store) {
|
|
310
|
-
const k = {};
|
|
311
|
-
if (!await store.run(k, async () => {
|
|
312
|
-
await Promise.resolve();
|
|
313
|
-
return store.getStore() === k;
|
|
314
|
-
})) throw error("AsyncLocalStorage loses its store across await; enable nodejs_als or nodejs_compat (compatibility date 2026-08-04+)");
|
|
315
|
-
}
|
|
316
|
-
let probed;
|
|
317
|
-
let ok = false;
|
|
318
|
-
/** Runs the probe once per process; a failure is remembered, so every request reports it. */
|
|
319
|
-
function propagation() {
|
|
320
|
-
if (ok) return void 0;
|
|
321
|
-
return probed ??= assertPropagation(als).then(() => {
|
|
322
|
-
ok = true;
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
//#endregion
|
|
326
|
-
//#region src/server/inject.ts
|
|
322
|
+
// src/server/inject.ts
|
|
327
323
|
/**
|
|
328
324
|
* The streaming HTML transform of `handle()` (PLAN §5.13 step 5): `lang` and
|
|
329
325
|
* `dir` on `<html>` unless the app set them, the payload before `</head>`,
|
|
@@ -444,9 +440,8 @@ function injector(hooks) {
|
|
|
444
440
|
emit(lt);
|
|
445
441
|
insert(hooks.head());
|
|
446
442
|
phase = 2;
|
|
447
|
-
} else if (t === 2)
|
|
448
|
-
|
|
449
|
-
} else if (t === 3) {
|
|
443
|
+
} else if (t === 2) phase = 2;
|
|
444
|
+
else if (t === 3) {
|
|
450
445
|
emit(lt);
|
|
451
446
|
insert(hooks.tail());
|
|
452
447
|
phase = 3;
|
|
@@ -459,14 +454,7 @@ function injector(hooks) {
|
|
|
459
454
|
}
|
|
460
455
|
};
|
|
461
456
|
}
|
|
462
|
-
|
|
463
|
-
//#region src/server/types.ts
|
|
464
|
-
/** Header the client runtime adds to same-origin `fetch` calls (`routing.propagate`). */
|
|
465
|
-
const LOCALE_HEADER = "x-vi18n-locale";
|
|
466
|
-
/** Cookie the client runtime writes with the browser's IANA zone. */
|
|
467
|
-
const TIME_ZONE_COOKIE = "vi18n_tz";
|
|
468
|
-
//#endregion
|
|
469
|
-
//#region src/server/negotiate.ts
|
|
457
|
+
// src/server/negotiate.ts
|
|
470
458
|
/**
|
|
471
459
|
* Server locale resolution (INTERNALS §2.11.9, PLAN §5.14). The SPA boot
|
|
472
460
|
* script and the client runtime implement the same algorithm, and
|
|
@@ -474,21 +462,6 @@ const TIME_ZONE_COOKIE = "vi18n_tz";
|
|
|
474
462
|
* (URL segment, header, cookie, Referer) is matched against `locales` and
|
|
475
463
|
* nothing else, so a request can only ever select a configured locale.
|
|
476
464
|
*/
|
|
477
|
-
/** The base as a path starting and ending with `/`, as the URL codec reads it. */
|
|
478
|
-
function baseOf(config) {
|
|
479
|
-
const b = config.routing.base;
|
|
480
|
-
return b && b[0] === "/" ? b.endsWith("/") ? b : b + "/" : "/";
|
|
481
|
-
}
|
|
482
|
-
/**
|
|
483
|
-
* The pathname as route globs see it (INTERNALS §2.7.3): delocalized,
|
|
484
|
-
* without the base and without a trailing slash; the root is `/`.
|
|
485
|
-
*/
|
|
486
|
-
function routeOf(pathname, config) {
|
|
487
|
-
const p = splitLocalePath(pathname, config).path;
|
|
488
|
-
const base = baseOf(config);
|
|
489
|
-
const s = p.startsWith(base) ? "/" + p.slice(base.length) : p + "/" === base ? "/" : p;
|
|
490
|
-
return s.length > 1 && s.endsWith("/") ? s.slice(0, -1) : s;
|
|
491
|
-
}
|
|
492
465
|
const keyCache = /* @__PURE__ */ new WeakMap();
|
|
493
466
|
function keysOf(o) {
|
|
494
467
|
let k = keyCache.get(o);
|
|
@@ -496,15 +469,6 @@ function keysOf(o) {
|
|
|
496
469
|
return k;
|
|
497
470
|
}
|
|
498
471
|
/**
|
|
499
|
-
* The `url` step: an unprefixed path is the source locale's canonical page
|
|
500
|
-
* (on a listed host, the host's locale's) unless every locale is prefixed.
|
|
501
|
-
*/
|
|
502
|
-
function urlStep(config, pathname) {
|
|
503
|
-
const url = config.routing.url;
|
|
504
|
-
if (url === "no_prefix") return null;
|
|
505
|
-
return splitLocalePath(pathname, config).locale ?? (url === "prefix" ? null : ROUTES && config.routing.home || config.sourceLocale);
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
472
|
* The canonical URL of a page the URL names, where localized paths or
|
|
509
473
|
* domains are configured (§6 D133), or null when `pathname` is it: the
|
|
510
474
|
* locale's own path for the route (`/de/about` → `/de/ueber-uns`), without
|
|
@@ -515,10 +479,9 @@ function urlStep(config, pathname) {
|
|
|
515
479
|
*/
|
|
516
480
|
function canonical(pathname, config) {
|
|
517
481
|
const url = config.routing.url;
|
|
518
|
-
if (url === "no_prefix") return null;
|
|
519
482
|
const split = splitLocalePath(pathname, config);
|
|
520
|
-
const l = split.locale ?? (
|
|
521
|
-
if (l ===
|
|
483
|
+
const l = split.locale ?? unprefixedLocale(config);
|
|
484
|
+
if (l === void 0) return null;
|
|
522
485
|
const away = awayHref(split.path, l, config);
|
|
523
486
|
if (away !== null) return away;
|
|
524
487
|
const c = url === "prefix_and_default" && split.locale !== null ? {
|
|
@@ -531,18 +494,6 @@ function canonical(pathname, config) {
|
|
|
531
494
|
const target = localizePath(split.path, l, c);
|
|
532
495
|
return target !== pathname && splitLocalePath(target, c).path === split.path ? target : null;
|
|
533
496
|
}
|
|
534
|
-
/** One cookie's decoded value from a `Cookie` header, or null. */
|
|
535
|
-
function readCookie(header, name) {
|
|
536
|
-
for (const part of header ? header.split(";") : []) {
|
|
537
|
-
const eq = part.indexOf("=");
|
|
538
|
-
if (eq > 0 && part.slice(0, eq).trim() === name) try {
|
|
539
|
-
return decodeURIComponent(part.slice(eq + 1).trim());
|
|
540
|
-
} catch {
|
|
541
|
-
return null;
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
return null;
|
|
545
|
-
}
|
|
546
497
|
const MAX_HEADER = 1024;
|
|
547
498
|
const MAX_ENTRIES = 20;
|
|
548
499
|
const QVALUE = /^\s*q\s*=\s*(0(?:\.\d{0,3})?|1(?:\.0{0,3})?)\s*$/i;
|
|
@@ -579,92 +530,112 @@ function parseAcceptLanguage(header) {
|
|
|
579
530
|
return entries.map((e) => e.tag);
|
|
580
531
|
}
|
|
581
532
|
const ISLANDS = "/_server-islands/";
|
|
582
|
-
/**
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
let locale = null;
|
|
592
|
-
let source = "default";
|
|
593
|
-
let vary;
|
|
594
|
-
let redirect;
|
|
595
|
-
let status;
|
|
596
|
-
const headers = request.headers;
|
|
597
|
-
if (headers.get("sec-fetch-mode") !== "navigate" && !(urlStep(config, pathname) && chain.includes("url"))) {
|
|
533
|
+
/**
|
|
534
|
+
* Step 1, before the chain: the tab's locale on fetches, the page's on
|
|
535
|
+
* islands. A URL that names its locale outranks the tab's (§6 D108):
|
|
536
|
+
* routers fetch a link's data before they push its URL, while the tab is
|
|
537
|
+
* still in the old locale. Routes that follow the tab (APIs, server
|
|
538
|
+
* functions) list a chain without `url` in `routeStrategies`.
|
|
539
|
+
*/
|
|
540
|
+
function beforeChain(headers, pathname, config, chain) {
|
|
541
|
+
if (headers.get("sec-fetch-mode") !== "navigate" && !(urlLocale(config, pathname) && chain.includes("url"))) {
|
|
598
542
|
const x = headers.get(LOCALE_HEADER);
|
|
599
|
-
if (x !== null && locales.includes(x)) {
|
|
600
|
-
locale
|
|
601
|
-
source
|
|
602
|
-
vary
|
|
603
|
-
}
|
|
543
|
+
if (x !== null && config.locales.includes(x)) return {
|
|
544
|
+
locale: x,
|
|
545
|
+
source: "header-x",
|
|
546
|
+
vary: "x-vi18n-locale"
|
|
547
|
+
};
|
|
604
548
|
}
|
|
605
|
-
if (
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
549
|
+
if (!routePath(pathname, config).startsWith(ISLANDS)) return null;
|
|
550
|
+
let ref = null;
|
|
551
|
+
try {
|
|
552
|
+
ref = new URL(headers.get("referer")).pathname;
|
|
553
|
+
} catch {}
|
|
554
|
+
const l = ref === null || ref === "/" ? null : urlLocale(config, ref);
|
|
555
|
+
return l === null ? null : {
|
|
556
|
+
locale: l,
|
|
557
|
+
source: "referer",
|
|
558
|
+
vary: "Referer"
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Step 2, the chain, first answer wins; no answer is the source locale, as
|
|
563
|
+
* from a final `default`. `redirecting` skips the URL step (the root
|
|
564
|
+
* redirect runs the chain "without url"). Once a cookie or Accept-Language
|
|
565
|
+
* step was read, the answer depends on them, even when a later step gives
|
|
566
|
+
* it. `localStorage` is client-only: the server has nothing to read.
|
|
567
|
+
*/
|
|
568
|
+
function chainLocale(headers, pathname, config, chain, redirecting) {
|
|
569
|
+
let asked = false;
|
|
570
|
+
let n = {
|
|
571
|
+
locale: config.sourceLocale,
|
|
572
|
+
source: "default"
|
|
573
|
+
};
|
|
574
|
+
for (const step of chain) {
|
|
575
|
+
let l = null;
|
|
576
|
+
if (step === "url") {
|
|
577
|
+
if (!redirecting) l = urlLocale(config, pathname);
|
|
578
|
+
} else if (step === "cookie") {
|
|
579
|
+
asked = true;
|
|
580
|
+
l = readCookie(headers.get("cookie") ?? "", config.routing.cookie.name);
|
|
581
|
+
} else if (step === "header") {
|
|
582
|
+
asked = true;
|
|
583
|
+
l = matchLocale(parseAcceptLanguage(headers.get("accept-language")), config);
|
|
584
|
+
} else if (step === "default") l = config.sourceLocale;
|
|
585
|
+
if (l !== null && config.locales.includes(l)) {
|
|
586
|
+
n = {
|
|
587
|
+
locale: l,
|
|
588
|
+
source: step
|
|
589
|
+
};
|
|
590
|
+
break;
|
|
615
591
|
}
|
|
616
592
|
}
|
|
617
|
-
if (
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
if (l !== null && locales.includes(l)) {
|
|
634
|
-
locale = l;
|
|
635
|
-
source = step;
|
|
636
|
-
break;
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
locale ??= sourceLocale;
|
|
640
|
-
if (asked || atRoot && source !== "url") vary = "Accept-Language";
|
|
593
|
+
if (asked) n.vary = "Accept-Language";
|
|
594
|
+
return n;
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Steps 2 and 3: the chain, and the root redirect, where preferences decide
|
|
598
|
+
* but only at the root, which runs the chain "without url".
|
|
599
|
+
*/
|
|
600
|
+
function negotiated(headers, url, config, chain) {
|
|
601
|
+
const pathname = url.pathname;
|
|
602
|
+
const base = baseOf(config);
|
|
603
|
+
const atRoot = pathname === base || pathname + "/" === base;
|
|
604
|
+
const redirecting = atRoot && config.routing.redirectRoot;
|
|
605
|
+
const n = chainLocale(headers, pathname, config, chain, redirecting);
|
|
606
|
+
const locale = n.locale;
|
|
607
|
+
if (atRoot && n.source !== "url") n.vary = "Accept-Language";
|
|
608
|
+
if (redirecting && locale !== urlLocale(config, pathname)) {
|
|
641
609
|
const target = ROUTES && awayHref(base, locale, config) || localizePath(base, locale, config);
|
|
642
|
-
if (
|
|
643
|
-
redirect = new URL(target + url.search, url);
|
|
644
|
-
status = 307;
|
|
610
|
+
if (target !== pathname) {
|
|
611
|
+
n.redirect = new URL(target + url.search, url);
|
|
612
|
+
n.status = 307;
|
|
645
613
|
}
|
|
646
614
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
615
|
+
return n;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* §2.11.9 for one request, with the canonical and root redirects. Only the
|
|
619
|
+
* fields that apply are set, as `negotiate()` documents them.
|
|
620
|
+
*/
|
|
621
|
+
function negotiateFor(request, url, config) {
|
|
622
|
+
if (ROUTES) config = atHost(config, url.host);
|
|
623
|
+
const pathname = url.pathname;
|
|
624
|
+
const { routeStrategies, strategy } = config.routing;
|
|
625
|
+
const keys = keysOf(routeStrategies);
|
|
626
|
+
const route = keys.length ? matchRoute(keys, routePath(pathname, config)) : null;
|
|
627
|
+
const chain = route === null ? strategy : routeStrategies[route];
|
|
628
|
+
const n = beforeChain(request.headers, pathname, config, chain) ?? negotiated(request.headers, url, config, chain);
|
|
629
|
+
if (n.redirect || request.method !== "GET" && request.method !== "HEAD") return n;
|
|
630
|
+
let target = null;
|
|
631
|
+
if (ROUTES && (config.routing.paths || config.routing.domains)) target = canonical(pathname, config);
|
|
632
|
+
else if (config.routing.url === "prefix_except_default") {
|
|
654
633
|
const split = splitLocalePath(pathname, config);
|
|
655
|
-
if (split.locale === sourceLocale)
|
|
656
|
-
redirect = new URL(split.path + url.search, url);
|
|
657
|
-
status = 301;
|
|
658
|
-
}
|
|
634
|
+
if (split.locale === config.sourceLocale) target = split.path;
|
|
659
635
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
};
|
|
664
|
-
if (vary) n.vary = vary;
|
|
665
|
-
if (redirect) {
|
|
666
|
-
n.redirect = redirect;
|
|
667
|
-
n.status = status;
|
|
636
|
+
if (target !== null) {
|
|
637
|
+
n.redirect = new URL(target + url.search, url);
|
|
638
|
+
n.status = 301;
|
|
668
639
|
}
|
|
669
640
|
return n;
|
|
670
641
|
}
|
|
@@ -675,8 +646,7 @@ function negotiateFor(request, url, config) {
|
|
|
675
646
|
function negotiate(request) {
|
|
676
647
|
return negotiateFor(request, new URL(request.url), current());
|
|
677
648
|
}
|
|
678
|
-
|
|
679
|
-
//#region src/server/payload.ts
|
|
649
|
+
// src/server/payload.ts
|
|
680
650
|
/**
|
|
681
651
|
* The SSR payload (INTERNALS §2.10, PLAN §5.13 "Payload contents"): what a
|
|
682
652
|
* page inlines so hydration renders with zero catalog requests. Ids alone
|
|
@@ -773,6 +743,62 @@ function select(plan, fresh, st) {
|
|
|
773
743
|
return out.sort();
|
|
774
744
|
}
|
|
775
745
|
/**
|
|
746
|
+
* `ssr.payload: "preload"`: a preload link per selected bundle, complete in
|
|
747
|
+
* the server catalog or not, since the client fetches the file, never this
|
|
748
|
+
* server's copy of it; the payload names their keys. "" when a late payload
|
|
749
|
+
* would name none.
|
|
750
|
+
*/
|
|
751
|
+
function preloaded(scope, st, plan, keys, nonce) {
|
|
752
|
+
const locale = scope.locale;
|
|
753
|
+
const hashes = own(plan.hashes, locale) && plan.hashes[locale] || {};
|
|
754
|
+
const named = keys.filter((k) => own(hashes, k));
|
|
755
|
+
if (st.head && !named.length) return "";
|
|
756
|
+
let links = "";
|
|
757
|
+
for (const k of named) links += "<link rel=\"preload\" as=\"fetch\" crossorigin href=\"" + attr(plan.assetsUrl + locale + "/" + k + "." + hashes[k] + ".json") + "\">";
|
|
758
|
+
return links + script(scope, st, plan, "},\"preload\":" + escapeJson(JSON.stringify(named)), nonce);
|
|
759
|
+
}
|
|
760
|
+
/** The selected bundles inlined, each one the server catalog holds completely. */
|
|
761
|
+
function inlined(plan, keys, messages) {
|
|
762
|
+
let bundles = "";
|
|
763
|
+
for (const k of keys) {
|
|
764
|
+
const json = bundleJson(plan, messages, k);
|
|
765
|
+
if (json !== null) bundles += (bundles ? "," : "") + JSON.stringify(k) + ":" + json;
|
|
766
|
+
}
|
|
767
|
+
return bundles;
|
|
768
|
+
}
|
|
769
|
+
/** Fallback mode: no bundles to name, so the rendered messages themselves. */
|
|
770
|
+
function rendered(fresh, messages) {
|
|
771
|
+
fresh.sort();
|
|
772
|
+
let m = "";
|
|
773
|
+
for (const id of fresh) {
|
|
774
|
+
const msg = messages.get(id);
|
|
775
|
+
if (msg !== void 0) m += (m ? "," : "") + JSON.stringify(id) + ":" + JSON.stringify(msg);
|
|
776
|
+
}
|
|
777
|
+
return m;
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* The payload element: its head fields, then `body`, the JSON after
|
|
781
|
+
* `"bundles":{` (the inlined bundles, then any further fields). The first
|
|
782
|
+
* payload of a scope is the page's; later ones are late payloads.
|
|
783
|
+
*/
|
|
784
|
+
function script(scope, st, plan, body, nonce) {
|
|
785
|
+
const late = st.head;
|
|
786
|
+
const locale = scope.locale;
|
|
787
|
+
const v = plan ? plan.v : reg.mode === "dev" && "dev" || null;
|
|
788
|
+
st.head = true;
|
|
789
|
+
const head = late ? {
|
|
790
|
+
v,
|
|
791
|
+
locale
|
|
792
|
+
} : {
|
|
793
|
+
v,
|
|
794
|
+
locale,
|
|
795
|
+
timeZone: scope.timeZone ?? current().ssr.timeZone,
|
|
796
|
+
now: scope.now ?? Date.now(),
|
|
797
|
+
manifest: plan && own(plan.hashes, locale) && plan.hashes[locale] || null
|
|
798
|
+
};
|
|
799
|
+
return "<script type=\"application/json\" " + (late ? "data-vi18n-late" : "id=\"__vi18n\"") + (nonce ? " nonce=\"" + attr(nonce) + "\"" : "") + ">" + escapeJson(JSON.stringify(head).slice(0, -1)) + ",\"bundles\":{" + body + "}<\/script>";
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
776
802
|
* The `<script>` to insert for `scope` now: the payload when none was
|
|
777
803
|
* emitted yet, otherwise a late payload of what is new, after the preload
|
|
778
804
|
* links of its bundles under `ssr.payload: "preload"`. "" when there is
|
|
@@ -793,57 +819,19 @@ function scriptFor(scope, nonce) {
|
|
|
793
819
|
fresh.push(id);
|
|
794
820
|
}
|
|
795
821
|
if (!fresh.length) return "";
|
|
796
|
-
const late = st.head;
|
|
797
822
|
const config = current();
|
|
798
823
|
const plan$1 = plan ?? null;
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
let links = "";
|
|
804
|
-
if (plan$1) {
|
|
805
|
-
const keys = select(plan$1, fresh, st);
|
|
806
|
-
if (!keys) return "";
|
|
807
|
-
if (PRELOAD && config.ssr.payload === "preload") {
|
|
808
|
-
const hashes = own(plan$1.hashes, locale) && plan$1.hashes[locale] || {};
|
|
809
|
-
const named = [];
|
|
810
|
-
for (const k of keys) {
|
|
811
|
-
if (!own(hashes, k)) continue;
|
|
812
|
-
named.push(k);
|
|
813
|
-
links += "<link rel=\"preload\" as=\"fetch\" crossorigin href=\"" + attr(plan$1.assetsUrl + locale + "/" + k + "." + hashes[k] + ".json") + "\">";
|
|
814
|
-
}
|
|
815
|
-
if (late && !named.length) return "";
|
|
816
|
-
extra = ",\"preload\":" + escapeJson(JSON.stringify(named));
|
|
817
|
-
} else {
|
|
818
|
-
for (const k of keys) {
|
|
819
|
-
const json = bundleJson(plan$1, messages, k);
|
|
820
|
-
if (json !== null) bundles += (bundles ? "," : "") + JSON.stringify(k) + ":" + json;
|
|
821
|
-
}
|
|
822
|
-
if (late && !bundles) return "";
|
|
823
|
-
}
|
|
824
|
-
} else {
|
|
825
|
-
fresh.sort();
|
|
826
|
-
let m = "";
|
|
827
|
-
for (const id of fresh) {
|
|
828
|
-
const msg = messages.get(id);
|
|
829
|
-
if (msg !== void 0) m += (m ? "," : "") + JSON.stringify(id) + ":" + JSON.stringify(msg);
|
|
830
|
-
}
|
|
831
|
-
if (late && !m) return "";
|
|
832
|
-
extra = ",\"messages\":" + escapeJson("{" + m + "}");
|
|
824
|
+
if (!plan$1) {
|
|
825
|
+
const m = rendered(fresh, scope.messages);
|
|
826
|
+
if (st.head && !m) return "";
|
|
827
|
+
return script(scope, st, plan$1, "},\"messages\":" + escapeJson("{" + m + "}"), nonce);
|
|
833
828
|
}
|
|
834
|
-
const
|
|
835
|
-
if (!
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
}
|
|
840
|
-
v,
|
|
841
|
-
locale,
|
|
842
|
-
timeZone: scope.timeZone ?? config.ssr.timeZone,
|
|
843
|
-
now: scope.now ?? Date.now(),
|
|
844
|
-
manifest: plan$1 && own(plan$1.hashes, locale) && plan$1.hashes[locale] || null
|
|
845
|
-
}).slice(0, -1)) + ",\"bundles\":{" + bundles + "}" + extra + "}";
|
|
846
|
-
return links + "<script type=\"application/json\" " + (late ? "data-vi18n-late" : "id=\"__vi18n\"") + (nonce ? " nonce=\"" + attr(nonce) + "\"" : "") + ">" + json + "<\/script>";
|
|
829
|
+
const keys = select(plan$1, fresh, st);
|
|
830
|
+
if (!keys) return "";
|
|
831
|
+
if (PRELOAD && config.ssr.payload === "preload") return preloaded(scope, st, plan$1, keys, nonce);
|
|
832
|
+
const bundles = inlined(plan$1, keys, scope.messages);
|
|
833
|
+
if (st.head && !bundles) return "";
|
|
834
|
+
return script(scope, st, plan$1, bundles + "}", nonce);
|
|
847
835
|
}
|
|
848
836
|
/**
|
|
849
837
|
* The payload element for HTML-string hooks (Nuxt `render:html`), with its
|
|
@@ -863,8 +851,7 @@ function htmlAttributes() {
|
|
|
863
851
|
dir: textDirection(locale)
|
|
864
852
|
};
|
|
865
853
|
}
|
|
866
|
-
|
|
867
|
-
//#region src/server/translator.ts
|
|
854
|
+
// src/server/translator.ts
|
|
868
855
|
/**
|
|
869
856
|
* Scope opening shared by every `/server` entry point, plus `withLocale` and
|
|
870
857
|
* `createTranslator` (PLAN §5.13 "E-mail, jobs and webhooks"). Every scope
|
|
@@ -892,7 +879,7 @@ function pickLocale(config, input) {
|
|
|
892
879
|
}
|
|
893
880
|
/** An app-given zone: wrong zones are programming errors, unlike a visitor's cookie. */
|
|
894
881
|
function explicitZone(zone) {
|
|
895
|
-
if (!isTimeZone(zone)) throw new RangeError("vi18n: invalid time zone " + JSON.stringify(zone));
|
|
882
|
+
if (!isTimeZone(zone)) throw new RangeError("vi18n: invalid time zone " + JSON.stringify(zone) + (DEV ? "; use an IANA name such as Europe/Berlin" : ""));
|
|
896
883
|
return zone;
|
|
897
884
|
}
|
|
898
885
|
/**
|
|
@@ -914,7 +901,7 @@ async function prepare(locale) {
|
|
|
914
901
|
]))[3];
|
|
915
902
|
}
|
|
916
903
|
/** A scope once its catalogs are loaded, carrying the registration's `sources` and dev labels. */
|
|
917
|
-
async function openScope(kind, locale, timeZone, timeZoneSource, now, trackUsed) {
|
|
904
|
+
async function openScope(kind, locale, [timeZone, timeZoneSource], now, trackUsed) {
|
|
918
905
|
const cat = await prepare(locale);
|
|
919
906
|
const scope = createScope(kind, locale, cat.messages, {
|
|
920
907
|
timeZone,
|
|
@@ -929,7 +916,7 @@ async function openScope(kind, locale, timeZone, timeZoneSource, now, trackUsed)
|
|
|
929
916
|
function translatorScope(locale, options) {
|
|
930
917
|
const config = current();
|
|
931
918
|
const zone = options?.timeZone;
|
|
932
|
-
return openScope("translator", pickLocale(config, locale), zone === void 0 ? config.ssr.timeZone : explicitZone(zone),
|
|
919
|
+
return openScope("translator", pickLocale(config, locale), zone === void 0 ? [config.ssr.timeZone, "config"] : [explicitZone(zone), "explicit"], options?.now, false);
|
|
933
920
|
}
|
|
934
921
|
/**
|
|
935
922
|
* Runs `fn` with `t` and `fmt` bound to `locale` (and the zone in `options`),
|
|
@@ -943,8 +930,7 @@ async function withLocale(locale, fn, options) {
|
|
|
943
930
|
async function createTranslator(locale, options) {
|
|
944
931
|
return createScopedApi(await translatorScope(locale, options));
|
|
945
932
|
}
|
|
946
|
-
|
|
947
|
-
//#region src/server/handle.ts
|
|
933
|
+
// src/server/handle.ts
|
|
948
934
|
/**
|
|
949
935
|
* Request scopes (PLAN §5.13 `handle(request, next)` steps 1–5, INTERNALS
|
|
950
936
|
* §3.2): negotiate, maybe redirect, open the scope with its zone and `now`,
|
|
@@ -960,20 +946,29 @@ function zoneFor(request, url, config, explicit) {
|
|
|
960
946
|
if (explicit !== void 0) return [explicitZone(explicit), "explicit"];
|
|
961
947
|
const from = ssr.timeZoneFrom;
|
|
962
948
|
const keys = keysOf(from);
|
|
963
|
-
const p = keys.length ? matchRoute(keys,
|
|
949
|
+
const p = keys.length ? matchRoute(keys, routePath(url.pathname, config)) : null;
|
|
964
950
|
if (p === null || from[p] !== "config") {
|
|
965
|
-
const c = readCookie(request.headers.get("cookie"), TIME_ZONE_COOKIE);
|
|
951
|
+
const c = readCookie(request.headers.get("cookie") ?? "", TIME_ZONE_COOKIE);
|
|
966
952
|
if (c !== null && isTimeZone(c)) return [c, "cookie"];
|
|
967
953
|
const cf = request.cf?.timezone;
|
|
968
954
|
if (isTimeZone(cf)) return [cf, "cf"];
|
|
969
955
|
}
|
|
970
956
|
return [ssr.timeZone, "config"];
|
|
971
957
|
}
|
|
972
|
-
/**
|
|
958
|
+
/**
|
|
959
|
+
* Opens the request scope once its catalogs are loaded. On a listed host
|
|
960
|
+
* the scope knows its locale, so links rendered here never name the
|
|
961
|
+
* request's `Host`.
|
|
962
|
+
*/
|
|
973
963
|
function open(config, request, url, locale, options) {
|
|
974
|
-
const
|
|
964
|
+
const zone = zoneFor(request, url, config, options?.timeZone);
|
|
975
965
|
const now = options?.now;
|
|
976
|
-
|
|
966
|
+
const scope = openScope("request", locale, zone, Number.isFinite(now) ? now : Date.now(), true);
|
|
967
|
+
if (!ROUTES) return scope;
|
|
968
|
+
return scope.then((s) => {
|
|
969
|
+
s.home = hostLocale(url.host, config);
|
|
970
|
+
return s;
|
|
971
|
+
});
|
|
977
972
|
}
|
|
978
973
|
const PUBLIC = /(?:^|[\s,])(?:public|s-maxage\s*=)/i;
|
|
979
974
|
const HTML = /^\s*text\/html\s*(?:;|$)/i;
|
|
@@ -989,55 +984,98 @@ const HTML = /^\s*text\/html\s*(?:;|$)/i;
|
|
|
989
984
|
function inScope(body, scope, inject) {
|
|
990
985
|
const reader = body.getReader();
|
|
991
986
|
return new ReadableStream({
|
|
992
|
-
pull: (c) => als.run(scope, () => reader.read()).then((r) =>
|
|
987
|
+
pull: (c) => als.run(scope, () => reader.read()).then((r) => {
|
|
988
|
+
if (r.done) {
|
|
989
|
+
inject?.flush(c);
|
|
990
|
+
c.close();
|
|
991
|
+
} else if (inject) inject.transform(r.value, c);
|
|
992
|
+
else c.enqueue(r.value);
|
|
993
|
+
}),
|
|
993
994
|
cancel: (reason) => als.run(scope, () => reader.cancel(reason))
|
|
994
995
|
});
|
|
995
996
|
}
|
|
997
|
+
/** Adds `vary` to `Vary` unless it lists it or `*`; `append` joins with ", ", as a server would. */
|
|
998
|
+
function addVary(headers, vary) {
|
|
999
|
+
const lower = vary.toLowerCase();
|
|
1000
|
+
if (!(headers.get("vary") ?? "").split(",").some((token) => {
|
|
1001
|
+
const t = token.trim().toLowerCase();
|
|
1002
|
+
return t === "*" || t === lower;
|
|
1003
|
+
})) headers.append("vary", vary);
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Dev: a page that formats times in the visitor's zone (`cookie`, `cf`)
|
|
1007
|
+
* must not be publicly cacheable.
|
|
1008
|
+
*/
|
|
1009
|
+
function warnIfPublic(headers, scope, request) {
|
|
1010
|
+
const cc = headers.get("cache-control");
|
|
1011
|
+
const zoned = scope.timeZoneSource === "cookie" || scope.timeZoneSource === "cf";
|
|
1012
|
+
if (scope.formattedTime && zoned && cc && PUBLIC.test(cc)) warnOnce(`${routePath(new URL(request.url).pathname, current())} formats times in a per-user zone but is publicly cacheable; set ssr.timeZoneFrom: config`);
|
|
1013
|
+
}
|
|
1014
|
+
/**
|
|
1015
|
+
* `Vary`, and `Cache-Control: private` where no shared cache may serve the
|
|
1016
|
+
* response to another visitor. Rendered in this visitor's zone (`perUser`),
|
|
1017
|
+
* the app's own Cache-Control wins. A locale the visitor's cookie or
|
|
1018
|
+
* languages chose (`Vary: Accept-Language`) may come from the cookie, which
|
|
1019
|
+
* no shared cache varies on: none may store the response, whatever the app
|
|
1020
|
+
* allowed (`private` overrides `public` and `s-maxage`). PLAN §5.14.
|
|
1021
|
+
*/
|
|
1022
|
+
function cacheHeaders(headers, vary, perUser) {
|
|
1023
|
+
if (vary) addVary(headers, vary);
|
|
1024
|
+
const cc = headers.get("cache-control");
|
|
1025
|
+
if (vary === "Accept-Language" ? !cc || !/private|no-store/i.test(cc) : perUser && !cc) headers.set("cache-control", cc ? "private, " + cc : "private");
|
|
1026
|
+
}
|
|
1027
|
+
/**
|
|
1028
|
+
* An HTML copy's headers: `Content-Language`, and where the payload is
|
|
1029
|
+
* injected (`plain`) no `Content-Length` and a weak ETag.
|
|
1030
|
+
*/
|
|
1031
|
+
function htmlHeaders(headers, scope, plain) {
|
|
1032
|
+
if (!headers.has("content-language")) headers.set("content-language", scope.locale);
|
|
1033
|
+
if (!plain) return;
|
|
1034
|
+
headers.delete("content-length");
|
|
1035
|
+
const etag = headers.get("etag");
|
|
1036
|
+
if (etag && !etag.startsWith("W/")) headers.set("etag", "W/" + etag);
|
|
1037
|
+
}
|
|
1038
|
+
/** Dev: why an HTML response gets no payload, or one its charset would garble. */
|
|
1039
|
+
function warnHtml(request, headers, plain) {
|
|
1040
|
+
const path = new URL(request.url).pathname;
|
|
1041
|
+
if (!plain) {
|
|
1042
|
+
warnOnce(`${path}: ${headers.get("content-encoding")}-encoded HTML is not injected; let the server compress after handle()`);
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
const type = headers.get("content-type");
|
|
1046
|
+
if (/charset=(?!"?utf-?8)/i.test(type)) warnOnce(`${path}: the payload is UTF-8; serve HTML as UTF-8 (${type})`);
|
|
1047
|
+
}
|
|
996
1048
|
/**
|
|
997
1049
|
* A copy of `response` with the scope's headers and its body read inside
|
|
998
1050
|
* the scope: injected for HTML, unchanged otherwise.
|
|
999
1051
|
*/
|
|
1000
|
-
function finish(request, response, scope,
|
|
1052
|
+
function finish(request, response, scope, vary, options) {
|
|
1001
1053
|
if (response.status < 200) return response;
|
|
1002
1054
|
const type = response.headers.get("content-type");
|
|
1003
|
-
const html = HTML.test(type);
|
|
1004
|
-
const
|
|
1005
|
-
const perUser = zoned && (scope.formattedTime === true || html);
|
|
1006
|
-
const vary = n?.vary;
|
|
1055
|
+
const html = type !== null && HTML.test(type);
|
|
1056
|
+
const perUser = (scope.timeZoneSource === "cookie" || scope.timeZoneSource === "cf") && (scope.formattedTime === true || html);
|
|
1007
1057
|
const body = response.body;
|
|
1008
1058
|
if (!html && !vary && !perUser && !body) return response;
|
|
1009
1059
|
const encoding = response.headers.get("content-encoding");
|
|
1010
1060
|
const plain = html && !(encoding && encoding.toLowerCase() !== "identity");
|
|
1011
|
-
const publicly = () => {
|
|
1012
|
-
const cc = headers.get("cache-control");
|
|
1013
|
-
if (DEV && scope.formattedTime && zoned && cc && PUBLIC.test(cc)) warnOnce(`${routeOf(url.pathname, config)} formats times in a per-user zone but is publicly cacheable; set ssr.timeZoneFrom: config`);
|
|
1014
|
-
};
|
|
1015
1061
|
const script = () => options?.payload ?? reg !== bare ? scriptFor(scope, options?.nonce) : "";
|
|
1016
|
-
const
|
|
1062
|
+
const tail = () => {
|
|
1063
|
+
warnIfPublic(response.headers, scope, request);
|
|
1064
|
+
return script();
|
|
1065
|
+
};
|
|
1066
|
+
const inject = plain && request.method !== "HEAD" ? injector({
|
|
1017
1067
|
attributes: () => ({
|
|
1018
1068
|
lang: scope.locale,
|
|
1019
1069
|
dir: textDirection(scope.locale)
|
|
1020
1070
|
}),
|
|
1021
1071
|
head: script,
|
|
1022
|
-
tail: DEV ?
|
|
1023
|
-
}) : void 0
|
|
1024
|
-
const
|
|
1025
|
-
|
|
1026
|
-
if (
|
|
1027
|
-
|
|
1028
|
-
if (
|
|
1029
|
-
if (DEV) publicly();
|
|
1030
|
-
if (html) {
|
|
1031
|
-
if (!headers.has("content-language")) headers.set("content-language", scope.locale);
|
|
1032
|
-
if (!plain) {
|
|
1033
|
-
if (DEV) warnOnce(`${url.pathname}: ${encoding}-encoded HTML is not injected; let the server compress after handle()`);
|
|
1034
|
-
} else {
|
|
1035
|
-
headers.delete("content-length");
|
|
1036
|
-
const etag = headers.get("etag");
|
|
1037
|
-
if (etag && !etag.startsWith("W/")) headers.set("etag", "W/" + etag);
|
|
1038
|
-
if (DEV && /charset=(?!"?utf-?8)/i.test(type)) warnOnce(`${url.pathname}: the payload is UTF-8; serve HTML as UTF-8 (${type})`);
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1072
|
+
tail: DEV ? tail : script
|
|
1073
|
+
}) : void 0;
|
|
1074
|
+
const res = new Response(body && inScope(body, scope, inject), response);
|
|
1075
|
+
cacheHeaders(res.headers, vary, perUser);
|
|
1076
|
+
if (DEV) warnIfPublic(response.headers, scope, request);
|
|
1077
|
+
if (html) htmlHeaders(res.headers, scope, plain);
|
|
1078
|
+
if (DEV && html) warnHtml(request, res.headers, plain);
|
|
1041
1079
|
return res;
|
|
1042
1080
|
}
|
|
1043
1081
|
/**
|
|
@@ -1062,10 +1100,9 @@ async function handle(request, next, options) {
|
|
|
1062
1100
|
});
|
|
1063
1101
|
}
|
|
1064
1102
|
const scope = await open(config, request, url, f ?? n.locale, options);
|
|
1065
|
-
if (ROUTES) scope.home = hostLocale(url.host, config);
|
|
1066
1103
|
const response = await als.run(scope, () => next(request));
|
|
1067
1104
|
if (DEV && !(response instanceof Response)) throw error("handle(): next() must return a Response");
|
|
1068
|
-
return finish(request, response, scope, n,
|
|
1105
|
+
return finish(request, response, scope, n?.vary, options);
|
|
1069
1106
|
}
|
|
1070
1107
|
/**
|
|
1071
1108
|
* Runs `fn` in the request's scope, for servers that build the response
|
|
@@ -1077,7 +1114,6 @@ async function withRequest(request, fn, options) {
|
|
|
1077
1114
|
const config = current();
|
|
1078
1115
|
const url = new URL(request.url);
|
|
1079
1116
|
const scope = await open(config, request, url, matched(config, options?.locale) ?? negotiateFor(request, url, config).locale, options);
|
|
1080
|
-
if (ROUTES) scope.home = hostLocale(url.host, config);
|
|
1081
1117
|
const value = await als.run(scope, fn);
|
|
1082
1118
|
return value instanceof Response && value.body ? new Response(inScope(value.body, scope), value) : value;
|
|
1083
1119
|
}
|
|
@@ -1096,8 +1132,7 @@ async function scopeLocale(locale) {
|
|
|
1096
1132
|
scope.messages = cat.messages;
|
|
1097
1133
|
scope.labels = cat.labels;
|
|
1098
1134
|
}
|
|
1099
|
-
|
|
1100
|
-
//#region src/server/seo.ts
|
|
1135
|
+
// src/server/seo.ts
|
|
1101
1136
|
/**
|
|
1102
1137
|
* Prerender and SEO helpers (PLAN §5.13, §5.14): every locale's URL of a page
|
|
1103
1138
|
* for crawlers (`alternates`), and the paths or params a prerenderer walks
|
|
@@ -1230,5 +1265,4 @@ function localizedParams(key = "lang") {
|
|
|
1230
1265
|
}
|
|
1231
1266
|
return out;
|
|
1232
1267
|
}
|
|
1233
|
-
//#endregion
|
|
1234
1268
|
export { alternates, createTranslator, getScope, handle, htmlAttributes, localizedParams, localizedPaths, negotiate, payload, registerCatalogs, scopeLocale, setScopeResolver, withLocale, withRequest };
|