@menapo/vi18n 0.1.1 → 0.2.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 +19 -0
- package/dist/{apply-Bre-lY9J.js → apply-Bg-3rB2m.js} +69 -89
- package/dist/{captured-Bj8bO6vd.js → captured-DfLk50Ji.js} +196 -217
- package/dist/{check-BIKEuu6B.js → check-CunekuYs.js} +76 -85
- package/dist/cli.js +2 -3
- package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
- package/dist/collisions-CDSdtLQm.js +215 -0
- package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
- package/dist/compile-hva0qjhZ.js +240 -0
- 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-LgIWyeSp.js} +28 -27
- package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
- package/dist/{duplicates-yuRljQ6h.js → duplicates-8cudcHNj.js} +10 -27
- package/dist/engine-ecRD8-IE.js +556 -0
- package/dist/engine.d.ts +1 -5
- package/dist/engine.js +297 -168
- package/dist/exchange-DjwlrTQ9.js +41 -0
- package/dist/{exclude-Du-Zd_qI.js → exclude-BFETmvde.js} +2 -3
- package/dist/{export-C9Sy6UWv.js → export-D2tsqWIr.js} +17 -38
- package/dist/{extract-C3S8btx0.js → extract-BxYchVmY.js} +114 -82
- package/dist/{formats-9unqj_fZ.js → formats-BaryZ2N7.js} +352 -276
- package/dist/fs-ezCMZeDV.js +52 -0
- package/dist/i18next-COO6o0tX.js +1907 -0
- package/dist/i18next-Dr7DCQlQ.js +1 -0
- package/dist/{import-nANvQutw.js → import-vSC29Cn4.js} +81 -75
- package/dist/index.d.ts +1 -13
- package/dist/index.js +14 -16
- package/dist/{init-C2ScuW6a.js → init-Cc7A-8VX.js} +466 -501
- package/dist/{library-D5CWvTQs.js → library-tp_xqzSY.js} +975 -451
- package/dist/lingui-BnnlqGXT.js +902 -0
- package/dist/lint.d.ts +0 -4
- package/dist/lint.js +70 -82
- package/dist/{load-gW-oJt1Q.js → load-BSIeGJ5v.js} +275 -148
- package/dist/{load-CQRz4PpM.js → load-SSrAeJVB.js} +1214 -1116
- package/dist/{locales-DX4MOBFE.js → locales-D0U-yVRm.js} +271 -237
- package/dist/{lsp-RMMMc88P.js → lsp-DvC11pBZ.js} +4 -5
- package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
- package/dist/{mcp-CHVwBoRP.js → mcp-D2zzOTBy.js} +4 -5
- package/dist/{merge-driver-BTX87aVm.js → merge-driver-BLI8OtAc.js} +138 -122
- package/dist/migrate-1r43OBfe.js +1 -0
- package/dist/migrate-ChDGhlC-.js +2169 -0
- package/dist/{openai-CTaMDcl-.js → openai-CLn4puY8.js} +128 -87
- package/dist/{output-DWE3SIUA.js → output-D5tAt8q4.js} +15 -9
- package/dist/parse.d.ts +1 -3
- package/dist/parse.js +7 -8
- package/dist/{pipeline-C9Dbotkh.js → pipeline-BJSkkRYC.js} +387 -359
- package/dist/{pipeline-DbX6rlo5.js → pipeline-BeCc441z.js} +1 -1
- package/dist/project-B5KIO0q-.js +544 -0
- package/dist/{pseudo-DRv30iN5.js → pseudo-B2DUpoLu.js} +126 -3
- package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
- package/dist/react-intl-C8JehBBL.js +634 -0
- package/dist/rename-ZVGq9ZG1.js +577 -0
- package/dist/repeated-uuI1tUGg.js +85 -0
- package/dist/restore-xlBIrtRt.js +199 -0
- package/dist/rewrite-BwzXoBiA.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-CQ9pDLSa.js +674 -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-Da1Ot9IN.js → server-Br8tAuG2.js} +312 -386
- package/dist/{server-deBOq2vq.js → server-DJRpUQVZ.js} +1059 -333
- package/dist/server.d.ts +1 -17
- package/dist/server.js +419 -379
- package/dist/{sitemap-DJXzKoza.js → sitemap-CSAkm6oi.js} +21 -33
- package/dist/{sources-D19czDSA.js → sources-DNVbYsgs.js} +713 -373
- package/dist/{status-zr4jY59Y.js → status-q6M3rBrh.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-BNIAwMhR.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 +126 -56
- package/dist/translate-BP4iVhOR.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-B4wNX6PA.d.ts} +6 -8
- package/dist/{types-CgUCkFU1.d.ts → types-_4UdFhR9.d.ts} +24 -21
- package/dist/types-xR2exI3L.js +42 -0
- package/dist/{units-CQ5Q99RC.js → units-DPPfCN7j.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-BxpupdAf.js → validate-CgzaDdOY.js} +690 -377
- package/dist/{validate-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
- package/dist/{version-Byqi2guX.js → version-DC-J5WvV.js} +3 -5
- package/dist/{vite-scan-CT_vsp4g.js → vite-scan-kM8Ta3rO.js} +87 -92
- package/dist/vite.d.ts +2 -6
- package/dist/vite.js +6500 -6032
- package/dist/vue-i18n-_uIXlFVh.js +806 -0
- package/dist/{which-BOemZ5Q8.js → which-DFWk_KwN.js} +44 -37
- package/dist/{worker-84U6uYB3.js → worker-zlhNj4y3.js} +454 -608
- package/package.json +5 -5
- package/dist/check-HsmmpJZg.js +0 -1
- package/dist/codec-DuvU2VQy.js +0 -207
- package/dist/collisions-CfpZU8e4.js +0 -217
- package/dist/compile-BaDh0cHG.js +0 -245
- package/dist/concurrency-C9UUWuDC.js +0 -96
- package/dist/concurrency-DONL7t4V.js +0 -1
- package/dist/engine-C2ENBRJe.js +0 -531
- package/dist/i18next-9HAoxwUI.js +0 -1
- package/dist/i18next-BEJgv_S8.js +0 -1596
- package/dist/lingui-zUiCl9Um.js +0 -833
- package/dist/messages-BfjIExyB.js +0 -166
- package/dist/migrate-IpLDMXmq.js +0 -1484
- package/dist/migrate-rCQuOGsf.js +0 -1
- package/dist/project-Bw9UPIdx.js +0 -1046
- package/dist/project-C4GpaN-n.js +0 -381
- package/dist/react-intl-BP0u-D5m.js +0 -519
- package/dist/restore-CiYv-jxS.js +0 -175
- package/dist/rewrite-EsIw_lsq.js +0 -803
- package/dist/run-BEcnBbu-.js +0 -694
- package/dist/runtime-DTMmliT1.js +0 -64
- package/dist/sync-B9V2KFwb.js +0 -658
- package/dist/translate-CQnJfLjO.js +0 -299
- package/dist/types-wSh9fCBY.js +0 -11
- package/dist/vue-i18n-g0iGy6CZ.js +0 -651
package/dist/server.js
CHANGED
|
@@ -1,42 +1,85 @@
|
|
|
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, s as page, 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
18
|
/**
|
|
25
|
-
*
|
|
26
|
-
* the
|
|
27
|
-
* (§6 D134).
|
|
19
|
+
* Not a production build the plugin made: dev, Vitest, or code bundled
|
|
20
|
+
* without the plugin. Builds fold what only these need away.
|
|
28
21
|
*/
|
|
22
|
+
const UNBUILT = typeof __VI18N_DEV__ === "undefined" || !!__VI18N_DEV__;
|
|
23
|
+
/** `ROUTES` of the URL codec: localized paths and domains are compiled in. */
|
|
29
24
|
const ROUTES = typeof __VI18N_ROUTES__ === "undefined" || !!__VI18N_ROUTES__;
|
|
30
|
-
const own = Object.hasOwn;
|
|
31
|
-
const noop = () => {};
|
|
32
25
|
const error = (text) => /* @__PURE__ */ new Error("vi18n: " + text);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
// src/server/als.ts
|
|
27
|
+
/**
|
|
28
|
+
* Request isolation (PLAN §5.13 "Isolation"). Scopes travel in an
|
|
29
|
+
* AsyncLocalStorage; there is deliberately no synchronous-slot fallback,
|
|
30
|
+
* because a slot still readable after an `await` would serve one request's
|
|
31
|
+
* locale to another.
|
|
32
|
+
*/
|
|
33
|
+
const als = S.serverScopes ??= new AsyncLocalStorage();
|
|
34
|
+
/** Framework request stores (SvelteKit `getRequestEvent()`) supply the scope; null removes it. */
|
|
35
|
+
function setScopeResolver(resolve) {
|
|
36
|
+
als.resolver = resolve;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The current scope. A `withLocale`/`createTranslator` scope entered inside
|
|
40
|
+
* a request outranks the framework's request store: the store would still
|
|
41
|
+
* name the request, and an e-mail rendered for another user inside it must
|
|
42
|
+
* use that user's locale. Otherwise the resolver wins over the ALS, as
|
|
43
|
+
* frameworks may run render code outside the async context `handle` opened.
|
|
44
|
+
*/
|
|
45
|
+
function getScope() {
|
|
46
|
+
const s = als.getStore();
|
|
47
|
+
const user = als.resolver;
|
|
48
|
+
if (s?.kind === "translator" || !user) return s;
|
|
49
|
+
let u;
|
|
50
|
+
try {
|
|
51
|
+
u = user();
|
|
52
|
+
} catch {}
|
|
53
|
+
return u ?? s;
|
|
39
54
|
}
|
|
55
|
+
S.serverResolver = getScope;
|
|
56
|
+
/**
|
|
57
|
+
* Some ALS implementations resolve but do not propagate (polyfills that hold
|
|
58
|
+
* the store only synchronously): reading another request's locale later
|
|
59
|
+
* would be silent, so the first scope opener checks that a store survives
|
|
60
|
+
* an `await`, and fails loudly otherwise.
|
|
61
|
+
*/
|
|
62
|
+
async function assertPropagation(store) {
|
|
63
|
+
const k = {};
|
|
64
|
+
if (!await store.run(k, async () => {
|
|
65
|
+
await Promise.resolve();
|
|
66
|
+
return store.getStore() === k;
|
|
67
|
+
})) throw error("AsyncLocalStorage loses its store across await; enable nodejs_als or nodejs_compat (compatibility date 2026-08-04+)");
|
|
68
|
+
}
|
|
69
|
+
let probed;
|
|
70
|
+
let ok = false;
|
|
71
|
+
/** Runs the probe once per process; a failure is remembered, so every request reports it. */
|
|
72
|
+
function propagation() {
|
|
73
|
+
if (ok) return void 0;
|
|
74
|
+
return probed ??= assertPropagation(als).then(() => {
|
|
75
|
+
ok = true;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
// src/server/validate.ts
|
|
79
|
+
/**
|
|
80
|
+
* Checks of untyped input (INTERNALS §3.2): `config.json` from `vi18n compile
|
|
81
|
+
* --out`, catalog and `sources` modules, and time zones from requests.
|
|
82
|
+
*/
|
|
40
83
|
const zones = /* @__PURE__ */ new Set();
|
|
41
84
|
/**
|
|
42
85
|
* True for an IANA zone name this engine's Intl accepts (INTERNALS §3.2).
|
|
@@ -56,6 +99,88 @@ function isTimeZone(zone) {
|
|
|
56
99
|
if (zones.size < 1024) zones.add(zone);
|
|
57
100
|
return true;
|
|
58
101
|
}
|
|
102
|
+
const isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
103
|
+
/** A module namespace's default export, or the object itself (both are accepted everywhere). */
|
|
104
|
+
function unwrap(m) {
|
|
105
|
+
return isObject(m) && own(m, "default") && isObject(m.default) ? m.default : m;
|
|
106
|
+
}
|
|
107
|
+
const STRATEGIES = [
|
|
108
|
+
"url",
|
|
109
|
+
"cookie",
|
|
110
|
+
"header",
|
|
111
|
+
"localStorage",
|
|
112
|
+
"default"
|
|
113
|
+
];
|
|
114
|
+
const RULES = [
|
|
115
|
+
["locales", []],
|
|
116
|
+
["pseudoLocales", []],
|
|
117
|
+
["fallbacks", { values: [] }],
|
|
118
|
+
["routing", {}],
|
|
119
|
+
["routing.strategy", [STRATEGIES]],
|
|
120
|
+
["routing.routeStrategies", { values: [STRATEGIES] }],
|
|
121
|
+
["routing.url", [
|
|
122
|
+
"prefix_except_default",
|
|
123
|
+
"prefix",
|
|
124
|
+
"prefix_and_default",
|
|
125
|
+
"no_prefix"
|
|
126
|
+
]],
|
|
127
|
+
["routing.redirectRoot", "boolean"],
|
|
128
|
+
["routing.propagate", ["header", false]],
|
|
129
|
+
["routing.switch", ["in-place", "navigate"]],
|
|
130
|
+
["routing.cookie", {}],
|
|
131
|
+
["routing.cookie.name", "string"],
|
|
132
|
+
["routing.cookie.maxAge", "number"],
|
|
133
|
+
["routing.base", "string"],
|
|
134
|
+
["ssr", {}],
|
|
135
|
+
["ssr.timeZone", "string"],
|
|
136
|
+
["ssr.timeZoneFrom", { values: ["config", "request"] }],
|
|
137
|
+
["ssr.normalizeSpaces", "boolean"]
|
|
138
|
+
];
|
|
139
|
+
function matches(v, rule) {
|
|
140
|
+
if (typeof rule === "string") return typeof v === rule;
|
|
141
|
+
if (Array.isArray(rule)) {
|
|
142
|
+
if (!rule.length || Array.isArray(rule[0])) {
|
|
143
|
+
const list = rule[0];
|
|
144
|
+
return Array.isArray(v) && v.every((x) => list ? list.includes(x) : typeof x === "string");
|
|
145
|
+
}
|
|
146
|
+
return rule.includes(v);
|
|
147
|
+
}
|
|
148
|
+
return isObject(v) && (!rule.values || Object.values(v).every((x) => matches(x, rule.values)));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* `config.json` from `vi18n compile --out` arrives as untyped JSON: each
|
|
152
|
+
* field the server reads is checked, so a stale or hand-edited file fails at
|
|
153
|
+
* startup with the field's name instead of mid-request.
|
|
154
|
+
*/
|
|
155
|
+
function validateConfig(input) {
|
|
156
|
+
const c = unwrap(input);
|
|
157
|
+
const fail = (path) => {
|
|
158
|
+
throw new TypeError("vi18n: registerCatalogs: invalid config: " + path);
|
|
159
|
+
};
|
|
160
|
+
if (!isObject(c)) fail("not an object");
|
|
161
|
+
for (const [path, rule] of RULES) if (!matches(path.split(".").reduce((o, k) => o[k], c), rule)) fail(path);
|
|
162
|
+
if (!c.locales.length) fail("locales");
|
|
163
|
+
if (!c.locales.includes(c.sourceLocale)) fail("sourceLocale");
|
|
164
|
+
if (ROUTES) {
|
|
165
|
+
const { paths, domains } = c.routing;
|
|
166
|
+
if (paths !== void 0 && !matches(paths, { values: { values: "string" } })) fail("routing.paths");
|
|
167
|
+
if (domains !== void 0 && !matches(domains, { values: "string" })) fail("routing.domains");
|
|
168
|
+
}
|
|
169
|
+
if (!isTimeZone(c.ssr.timeZone)) fail("ssr.timeZone");
|
|
170
|
+
return c;
|
|
171
|
+
}
|
|
172
|
+
// src/server/catalogs.ts
|
|
173
|
+
/**
|
|
174
|
+
* Server registration and catalogs (INTERNALS §3.2 "Server registration").
|
|
175
|
+
* The boot module (or `registerCatalogs`) registers once; from then on this
|
|
176
|
+
* module owns what `/server` loads for it: the per-locale catalogs, the
|
|
177
|
+
* `sources` map and the client plan. The source catalog and `sources` start
|
|
178
|
+
* loading at registration, so `State.serverFallback` exists before the first
|
|
179
|
+
* request and untransformed code resolves source literals from the start.
|
|
180
|
+
*
|
|
181
|
+
* Module-level variables rather than one state object: the minifier renames
|
|
182
|
+
* them, which keeps `/server` inside its PLAN §6 budget.
|
|
183
|
+
*/
|
|
59
184
|
/** The registration served; replaced when the state registers again. */
|
|
60
185
|
let reg = null;
|
|
61
186
|
let config;
|
|
@@ -117,11 +242,6 @@ function ready() {
|
|
|
117
242
|
throw e;
|
|
118
243
|
});
|
|
119
244
|
}
|
|
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
245
|
async function loadSources(r) {
|
|
126
246
|
if (!r.sources) return void 0;
|
|
127
247
|
const data = unwrap(await r.sources());
|
|
@@ -147,7 +267,7 @@ function catalog(locale) {
|
|
|
147
267
|
const into = loaded;
|
|
148
268
|
let c = into.get(locale);
|
|
149
269
|
if (!c) {
|
|
150
|
-
if (!loaders || !own(loaders, locale)) return Promise.reject(error("no server catalog for locale " + JSON.stringify(locale)));
|
|
270
|
+
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
271
|
if (DEV && dev) return loaders[locale]().then((m) => {
|
|
152
272
|
if (!isObject(m)) return toCatalog(locale, m);
|
|
153
273
|
let d = devMaps.get(m);
|
|
@@ -181,76 +301,11 @@ function loadPlan() {
|
|
|
181
301
|
throw e;
|
|
182
302
|
});
|
|
183
303
|
}
|
|
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
304
|
/**
|
|
228
305
|
* The last registration `registerCatalogs()` made: a server built without
|
|
229
306
|
* the Vite plugin, so no client plan exists and no client reads a payload.
|
|
230
307
|
*/
|
|
231
308
|
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
309
|
function registerCatalogs(loaders, options) {
|
|
255
310
|
const config = validateConfig(options.config);
|
|
256
311
|
if (!isObject(loaders)) throw new TypeError("vi18n: registerCatalogs: loaders must be an object");
|
|
@@ -269,61 +324,7 @@ S.register = (r) => {
|
|
|
269
324
|
if (r.consumer === "server" && S.registration === r) current();
|
|
270
325
|
return done;
|
|
271
326
|
};
|
|
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
|
|
327
|
+
// src/server/inject.ts
|
|
327
328
|
/**
|
|
328
329
|
* The streaming HTML transform of `handle()` (PLAN §5.13 step 5): `lang` and
|
|
329
330
|
* `dir` on `<html>` unless the app set them, the payload before `</head>`,
|
|
@@ -444,9 +445,8 @@ function injector(hooks) {
|
|
|
444
445
|
emit(lt);
|
|
445
446
|
insert(hooks.head());
|
|
446
447
|
phase = 2;
|
|
447
|
-
} else if (t === 2)
|
|
448
|
-
|
|
449
|
-
} else if (t === 3) {
|
|
448
|
+
} else if (t === 2) phase = 2;
|
|
449
|
+
else if (t === 3) {
|
|
450
450
|
emit(lt);
|
|
451
451
|
insert(hooks.tail());
|
|
452
452
|
phase = 3;
|
|
@@ -459,14 +459,7 @@ function injector(hooks) {
|
|
|
459
459
|
}
|
|
460
460
|
};
|
|
461
461
|
}
|
|
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
|
|
462
|
+
// src/server/negotiate.ts
|
|
470
463
|
/**
|
|
471
464
|
* Server locale resolution (INTERNALS §2.11.9, PLAN §5.14). The SPA boot
|
|
472
465
|
* script and the client runtime implement the same algorithm, and
|
|
@@ -474,21 +467,6 @@ const TIME_ZONE_COOKIE = "vi18n_tz";
|
|
|
474
467
|
* (URL segment, header, cookie, Referer) is matched against `locales` and
|
|
475
468
|
* nothing else, so a request can only ever select a configured locale.
|
|
476
469
|
*/
|
|
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
470
|
const keyCache = /* @__PURE__ */ new WeakMap();
|
|
493
471
|
function keysOf(o) {
|
|
494
472
|
let k = keyCache.get(o);
|
|
@@ -496,15 +474,6 @@ function keysOf(o) {
|
|
|
496
474
|
return k;
|
|
497
475
|
}
|
|
498
476
|
/**
|
|
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
477
|
* The canonical URL of a page the URL names, where localized paths or
|
|
509
478
|
* domains are configured (§6 D133), or null when `pathname` is it: the
|
|
510
479
|
* locale's own path for the route (`/de/about` → `/de/ueber-uns`), without
|
|
@@ -515,10 +484,9 @@ function urlStep(config, pathname) {
|
|
|
515
484
|
*/
|
|
516
485
|
function canonical(pathname, config) {
|
|
517
486
|
const url = config.routing.url;
|
|
518
|
-
if (url === "no_prefix") return null;
|
|
519
487
|
const split = splitLocalePath(pathname, config);
|
|
520
|
-
const l = split.locale ?? (
|
|
521
|
-
if (l ===
|
|
488
|
+
const l = split.locale ?? unprefixedLocale(config);
|
|
489
|
+
if (l === void 0) return null;
|
|
522
490
|
const away = awayHref(split.path, l, config);
|
|
523
491
|
if (away !== null) return away;
|
|
524
492
|
const c = url === "prefix_and_default" && split.locale !== null ? {
|
|
@@ -531,18 +499,6 @@ function canonical(pathname, config) {
|
|
|
531
499
|
const target = localizePath(split.path, l, c);
|
|
532
500
|
return target !== pathname && splitLocalePath(target, c).path === split.path ? target : null;
|
|
533
501
|
}
|
|
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
502
|
const MAX_HEADER = 1024;
|
|
547
503
|
const MAX_ENTRIES = 20;
|
|
548
504
|
const QVALUE = /^\s*q\s*=\s*(0(?:\.\d{0,3})?|1(?:\.0{0,3})?)\s*$/i;
|
|
@@ -579,92 +535,112 @@ function parseAcceptLanguage(header) {
|
|
|
579
535
|
return entries.map((e) => e.tag);
|
|
580
536
|
}
|
|
581
537
|
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"))) {
|
|
538
|
+
/**
|
|
539
|
+
* Step 1, before the chain: the tab's locale on fetches, the page's on
|
|
540
|
+
* islands. A URL that names its locale outranks the tab's (§6 D108):
|
|
541
|
+
* routers fetch a link's data before they push its URL, while the tab is
|
|
542
|
+
* still in the old locale. Routes that follow the tab (APIs, server
|
|
543
|
+
* functions) list a chain without `url` in `routeStrategies`.
|
|
544
|
+
*/
|
|
545
|
+
function beforeChain(headers, pathname, config, chain) {
|
|
546
|
+
if (headers.get("sec-fetch-mode") !== "navigate" && !(urlLocale(config, pathname) && chain.includes("url"))) {
|
|
598
547
|
const x = headers.get(LOCALE_HEADER);
|
|
599
|
-
if (x !== null && locales.includes(x)) {
|
|
600
|
-
locale
|
|
601
|
-
source
|
|
602
|
-
vary
|
|
603
|
-
}
|
|
548
|
+
if (x !== null && config.locales.includes(x)) return {
|
|
549
|
+
locale: x,
|
|
550
|
+
source: "header-x",
|
|
551
|
+
vary: "x-vi18n-locale"
|
|
552
|
+
};
|
|
604
553
|
}
|
|
605
|
-
if (
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
554
|
+
if (!routePath(pathname, config).startsWith(ISLANDS)) return null;
|
|
555
|
+
let ref = null;
|
|
556
|
+
try {
|
|
557
|
+
ref = new URL(headers.get("referer")).pathname;
|
|
558
|
+
} catch {}
|
|
559
|
+
const l = ref === null || ref === "/" ? null : urlLocale(config, ref);
|
|
560
|
+
return l === null ? null : {
|
|
561
|
+
locale: l,
|
|
562
|
+
source: "referer",
|
|
563
|
+
vary: "Referer"
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Step 2, the chain, first answer wins; no answer is the source locale, as
|
|
568
|
+
* from a final `default`. `redirecting` skips the URL step (the root
|
|
569
|
+
* redirect runs the chain "without url"). Once a cookie or Accept-Language
|
|
570
|
+
* step was read, the answer depends on them, even when a later step gives
|
|
571
|
+
* it. `localStorage` is client-only: the server has nothing to read.
|
|
572
|
+
*/
|
|
573
|
+
function chainLocale(headers, pathname, config, chain, redirecting) {
|
|
574
|
+
let asked = false;
|
|
575
|
+
let n = {
|
|
576
|
+
locale: config.sourceLocale,
|
|
577
|
+
source: "default"
|
|
578
|
+
};
|
|
579
|
+
for (const step of chain) {
|
|
580
|
+
let l = null;
|
|
581
|
+
if (step === "url") {
|
|
582
|
+
if (!redirecting) l = urlLocale(config, pathname);
|
|
583
|
+
} else if (step === "cookie") {
|
|
584
|
+
asked = true;
|
|
585
|
+
l = readCookie(headers.get("cookie") ?? "", config.routing.cookie.name);
|
|
586
|
+
} else if (step === "header") {
|
|
587
|
+
asked = true;
|
|
588
|
+
l = matchLocale(parseAcceptLanguage(headers.get("accept-language")), config);
|
|
589
|
+
} else if (step === "default") l = config.sourceLocale;
|
|
590
|
+
if (l !== null && config.locales.includes(l)) {
|
|
591
|
+
n = {
|
|
592
|
+
locale: l,
|
|
593
|
+
source: step
|
|
594
|
+
};
|
|
595
|
+
break;
|
|
615
596
|
}
|
|
616
597
|
}
|
|
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";
|
|
598
|
+
if (asked) n.vary = "Accept-Language";
|
|
599
|
+
return n;
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Steps 2 and 3: the chain, and the root redirect, where preferences decide
|
|
603
|
+
* but only at the root, which runs the chain "without url".
|
|
604
|
+
*/
|
|
605
|
+
function negotiated(headers, url, config, chain) {
|
|
606
|
+
const pathname = url.pathname;
|
|
607
|
+
const base = baseOf(config);
|
|
608
|
+
const atRoot = pathname === base || pathname + "/" === base;
|
|
609
|
+
const redirecting = atRoot && config.routing.redirectRoot;
|
|
610
|
+
const n = chainLocale(headers, pathname, config, chain, redirecting);
|
|
611
|
+
const locale = n.locale;
|
|
612
|
+
if (atRoot && n.source !== "url") n.vary = "Accept-Language";
|
|
613
|
+
if (redirecting && locale !== urlLocale(config, pathname)) {
|
|
641
614
|
const target = ROUTES && awayHref(base, locale, config) || localizePath(base, locale, config);
|
|
642
|
-
if (
|
|
643
|
-
redirect = new URL(target + url.search, url);
|
|
644
|
-
status = 307;
|
|
615
|
+
if (target !== pathname) {
|
|
616
|
+
n.redirect = new URL(target + url.search, url);
|
|
617
|
+
n.status = 307;
|
|
645
618
|
}
|
|
646
619
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
620
|
+
return n;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* §2.11.9 for one request, with the canonical and root redirects. Only the
|
|
624
|
+
* fields that apply are set, as `negotiate()` documents them.
|
|
625
|
+
*/
|
|
626
|
+
function negotiateFor(request, url, config) {
|
|
627
|
+
if (ROUTES) config = atHost(config, url.host);
|
|
628
|
+
const pathname = url.pathname;
|
|
629
|
+
const { routeStrategies, strategy } = config.routing;
|
|
630
|
+
const keys = keysOf(routeStrategies);
|
|
631
|
+
const route = keys.length ? matchRoute(keys, routePath(pathname, config)) : null;
|
|
632
|
+
const chain = route === null ? strategy : routeStrategies[route];
|
|
633
|
+
const n = beforeChain(request.headers, pathname, config, chain) ?? negotiated(request.headers, url, config, chain);
|
|
634
|
+
if (n.redirect || request.method !== "GET" && request.method !== "HEAD") return n;
|
|
635
|
+
let target = null;
|
|
636
|
+
if (ROUTES && (config.routing.paths || config.routing.domains)) target = canonical(pathname, config);
|
|
637
|
+
else if (config.routing.url === "prefix_except_default") {
|
|
654
638
|
const split = splitLocalePath(pathname, config);
|
|
655
|
-
if (split.locale === sourceLocale)
|
|
656
|
-
redirect = new URL(split.path + url.search, url);
|
|
657
|
-
status = 301;
|
|
658
|
-
}
|
|
639
|
+
if (split.locale === config.sourceLocale) target = split.path;
|
|
659
640
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
};
|
|
664
|
-
if (vary) n.vary = vary;
|
|
665
|
-
if (redirect) {
|
|
666
|
-
n.redirect = redirect;
|
|
667
|
-
n.status = status;
|
|
641
|
+
if (target !== null) {
|
|
642
|
+
n.redirect = new URL(target + url.search, url);
|
|
643
|
+
n.status = 301;
|
|
668
644
|
}
|
|
669
645
|
return n;
|
|
670
646
|
}
|
|
@@ -675,8 +651,7 @@ function negotiateFor(request, url, config) {
|
|
|
675
651
|
function negotiate(request) {
|
|
676
652
|
return negotiateFor(request, new URL(request.url), current());
|
|
677
653
|
}
|
|
678
|
-
|
|
679
|
-
//#region src/server/payload.ts
|
|
654
|
+
// src/server/payload.ts
|
|
680
655
|
/**
|
|
681
656
|
* The SSR payload (INTERNALS §2.10, PLAN §5.13 "Payload contents"): what a
|
|
682
657
|
* page inlines so hydration renders with zero catalog requests. Ids alone
|
|
@@ -773,6 +748,62 @@ function select(plan, fresh, st) {
|
|
|
773
748
|
return out.sort();
|
|
774
749
|
}
|
|
775
750
|
/**
|
|
751
|
+
* `ssr.payload: "preload"`: a preload link per selected bundle, complete in
|
|
752
|
+
* the server catalog or not, since the client fetches the file, never this
|
|
753
|
+
* server's copy of it; the payload names their keys. "" when a late payload
|
|
754
|
+
* would name none.
|
|
755
|
+
*/
|
|
756
|
+
function preloaded(scope, st, plan, keys, nonce) {
|
|
757
|
+
const locale = scope.locale;
|
|
758
|
+
const hashes = own(plan.hashes, locale) && plan.hashes[locale] || {};
|
|
759
|
+
const named = keys.filter((k) => own(hashes, k));
|
|
760
|
+
if (st.head && !named.length) return "";
|
|
761
|
+
let links = "";
|
|
762
|
+
for (const k of named) links += "<link rel=\"preload\" as=\"fetch\" crossorigin href=\"" + attr(plan.assetsUrl + locale + "/" + k + "." + hashes[k] + ".json") + "\">";
|
|
763
|
+
return links + script(scope, st, plan, "},\"preload\":" + escapeJson(JSON.stringify(named)), nonce);
|
|
764
|
+
}
|
|
765
|
+
/** The selected bundles inlined, each one the server catalog holds completely. */
|
|
766
|
+
function inlined(plan, keys, messages) {
|
|
767
|
+
let bundles = "";
|
|
768
|
+
for (const k of keys) {
|
|
769
|
+
const json = bundleJson(plan, messages, k);
|
|
770
|
+
if (json !== null) bundles += (bundles ? "," : "") + JSON.stringify(k) + ":" + json;
|
|
771
|
+
}
|
|
772
|
+
return bundles;
|
|
773
|
+
}
|
|
774
|
+
/** Fallback mode: no bundles to name, so the rendered messages themselves. */
|
|
775
|
+
function rendered(fresh, messages) {
|
|
776
|
+
fresh.sort();
|
|
777
|
+
let m = "";
|
|
778
|
+
for (const id of fresh) {
|
|
779
|
+
const msg = messages.get(id);
|
|
780
|
+
if (msg !== void 0) m += (m ? "," : "") + JSON.stringify(id) + ":" + JSON.stringify(msg);
|
|
781
|
+
}
|
|
782
|
+
return m;
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* The payload element: its head fields, then `body`, the JSON after
|
|
786
|
+
* `"bundles":{` (the inlined bundles, then any further fields). The first
|
|
787
|
+
* payload of a scope is the page's; later ones are late payloads.
|
|
788
|
+
*/
|
|
789
|
+
function script(scope, st, plan, body, nonce) {
|
|
790
|
+
const late = st.head;
|
|
791
|
+
const locale = scope.locale;
|
|
792
|
+
const v = plan ? plan.v : reg.mode === "dev" && "dev" || null;
|
|
793
|
+
st.head = true;
|
|
794
|
+
const head = late ? {
|
|
795
|
+
v,
|
|
796
|
+
locale
|
|
797
|
+
} : {
|
|
798
|
+
v,
|
|
799
|
+
locale,
|
|
800
|
+
timeZone: scope.timeZone ?? current().ssr.timeZone,
|
|
801
|
+
now: scope.now ?? Date.now(),
|
|
802
|
+
manifest: plan && own(plan.hashes, locale) && plan.hashes[locale] || null
|
|
803
|
+
};
|
|
804
|
+
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>";
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
776
807
|
* The `<script>` to insert for `scope` now: the payload when none was
|
|
777
808
|
* emitted yet, otherwise a late payload of what is new, after the preload
|
|
778
809
|
* links of its bundles under `ssr.payload: "preload"`. "" when there is
|
|
@@ -793,57 +824,19 @@ function scriptFor(scope, nonce) {
|
|
|
793
824
|
fresh.push(id);
|
|
794
825
|
}
|
|
795
826
|
if (!fresh.length) return "";
|
|
796
|
-
const late = st.head;
|
|
797
827
|
const config = current();
|
|
798
828
|
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 + "}");
|
|
829
|
+
if (!plan$1) {
|
|
830
|
+
const m = rendered(fresh, scope.messages);
|
|
831
|
+
if (st.head && !m) return "";
|
|
832
|
+
return script(scope, st, plan$1, "},\"messages\":" + escapeJson("{" + m + "}"), nonce);
|
|
833
833
|
}
|
|
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>";
|
|
834
|
+
const keys = select(plan$1, fresh, st);
|
|
835
|
+
if (!keys) return "";
|
|
836
|
+
if (PRELOAD && config.ssr.payload === "preload") return preloaded(scope, st, plan$1, keys, nonce);
|
|
837
|
+
const bundles = inlined(plan$1, keys, scope.messages);
|
|
838
|
+
if (st.head && !bundles) return "";
|
|
839
|
+
return script(scope, st, plan$1, bundles + "}", nonce);
|
|
847
840
|
}
|
|
848
841
|
/**
|
|
849
842
|
* The payload element for HTML-string hooks (Nuxt `render:html`), with its
|
|
@@ -863,8 +856,7 @@ function htmlAttributes() {
|
|
|
863
856
|
dir: textDirection(locale)
|
|
864
857
|
};
|
|
865
858
|
}
|
|
866
|
-
|
|
867
|
-
//#region src/server/translator.ts
|
|
859
|
+
// src/server/translator.ts
|
|
868
860
|
/**
|
|
869
861
|
* Scope opening shared by every `/server` entry point, plus `withLocale` and
|
|
870
862
|
* `createTranslator` (PLAN §5.13 "E-mail, jobs and webhooks"). Every scope
|
|
@@ -892,7 +884,7 @@ function pickLocale(config, input) {
|
|
|
892
884
|
}
|
|
893
885
|
/** An app-given zone: wrong zones are programming errors, unlike a visitor's cookie. */
|
|
894
886
|
function explicitZone(zone) {
|
|
895
|
-
if (!isTimeZone(zone)) throw new RangeError("vi18n: invalid time zone " + JSON.stringify(zone));
|
|
887
|
+
if (!isTimeZone(zone)) throw new RangeError("vi18n: invalid time zone " + JSON.stringify(zone) + (DEV ? "; use an IANA name such as Europe/Berlin" : ""));
|
|
896
888
|
return zone;
|
|
897
889
|
}
|
|
898
890
|
/**
|
|
@@ -914,7 +906,7 @@ async function prepare(locale) {
|
|
|
914
906
|
]))[3];
|
|
915
907
|
}
|
|
916
908
|
/** 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) {
|
|
909
|
+
async function openScope(kind, locale, [timeZone, timeZoneSource], now, trackUsed) {
|
|
918
910
|
const cat = await prepare(locale);
|
|
919
911
|
const scope = createScope(kind, locale, cat.messages, {
|
|
920
912
|
timeZone,
|
|
@@ -924,12 +916,13 @@ async function openScope(kind, locale, timeZone, timeZoneSource, now, trackUsed)
|
|
|
924
916
|
sources
|
|
925
917
|
});
|
|
926
918
|
if (DEV && cat.labels) scope.labels = cat.labels;
|
|
919
|
+
if (UNBUILT) scope.test = page.test;
|
|
927
920
|
return scope;
|
|
928
921
|
}
|
|
929
922
|
function translatorScope(locale, options) {
|
|
930
923
|
const config = current();
|
|
931
924
|
const zone = options?.timeZone;
|
|
932
|
-
return openScope("translator", pickLocale(config, locale), zone === void 0 ? config.ssr.timeZone : explicitZone(zone),
|
|
925
|
+
return openScope("translator", pickLocale(config, locale), zone === void 0 ? [config.ssr.timeZone, "config"] : [explicitZone(zone), "explicit"], options?.now, false);
|
|
933
926
|
}
|
|
934
927
|
/**
|
|
935
928
|
* Runs `fn` with `t` and `fmt` bound to `locale` (and the zone in `options`),
|
|
@@ -943,8 +936,7 @@ async function withLocale(locale, fn, options) {
|
|
|
943
936
|
async function createTranslator(locale, options) {
|
|
944
937
|
return createScopedApi(await translatorScope(locale, options));
|
|
945
938
|
}
|
|
946
|
-
|
|
947
|
-
//#region src/server/handle.ts
|
|
939
|
+
// src/server/handle.ts
|
|
948
940
|
/**
|
|
949
941
|
* Request scopes (PLAN §5.13 `handle(request, next)` steps 1–5, INTERNALS
|
|
950
942
|
* §3.2): negotiate, maybe redirect, open the scope with its zone and `now`,
|
|
@@ -960,20 +952,29 @@ function zoneFor(request, url, config, explicit) {
|
|
|
960
952
|
if (explicit !== void 0) return [explicitZone(explicit), "explicit"];
|
|
961
953
|
const from = ssr.timeZoneFrom;
|
|
962
954
|
const keys = keysOf(from);
|
|
963
|
-
const p = keys.length ? matchRoute(keys,
|
|
955
|
+
const p = keys.length ? matchRoute(keys, routePath(url.pathname, config)) : null;
|
|
964
956
|
if (p === null || from[p] !== "config") {
|
|
965
|
-
const c = readCookie(request.headers.get("cookie"), TIME_ZONE_COOKIE);
|
|
957
|
+
const c = readCookie(request.headers.get("cookie") ?? "", TIME_ZONE_COOKIE);
|
|
966
958
|
if (c !== null && isTimeZone(c)) return [c, "cookie"];
|
|
967
959
|
const cf = request.cf?.timezone;
|
|
968
960
|
if (isTimeZone(cf)) return [cf, "cf"];
|
|
969
961
|
}
|
|
970
962
|
return [ssr.timeZone, "config"];
|
|
971
963
|
}
|
|
972
|
-
/**
|
|
964
|
+
/**
|
|
965
|
+
* Opens the request scope once its catalogs are loaded. On a listed host
|
|
966
|
+
* the scope knows its locale, so links rendered here never name the
|
|
967
|
+
* request's `Host`.
|
|
968
|
+
*/
|
|
973
969
|
function open(config, request, url, locale, options) {
|
|
974
|
-
const
|
|
970
|
+
const zone = zoneFor(request, url, config, options?.timeZone);
|
|
975
971
|
const now = options?.now;
|
|
976
|
-
|
|
972
|
+
const scope = openScope("request", locale, zone, Number.isFinite(now) ? now : Date.now(), true);
|
|
973
|
+
if (!ROUTES) return scope;
|
|
974
|
+
return scope.then((s) => {
|
|
975
|
+
s.home = hostLocale(url.host, config);
|
|
976
|
+
return s;
|
|
977
|
+
});
|
|
977
978
|
}
|
|
978
979
|
const PUBLIC = /(?:^|[\s,])(?:public|s-maxage\s*=)/i;
|
|
979
980
|
const HTML = /^\s*text\/html\s*(?:;|$)/i;
|
|
@@ -989,55 +990,98 @@ const HTML = /^\s*text\/html\s*(?:;|$)/i;
|
|
|
989
990
|
function inScope(body, scope, inject) {
|
|
990
991
|
const reader = body.getReader();
|
|
991
992
|
return new ReadableStream({
|
|
992
|
-
pull: (c) => als.run(scope, () => reader.read()).then((r) =>
|
|
993
|
+
pull: (c) => als.run(scope, () => reader.read()).then((r) => {
|
|
994
|
+
if (r.done) {
|
|
995
|
+
inject?.flush(c);
|
|
996
|
+
c.close();
|
|
997
|
+
} else if (inject) inject.transform(r.value, c);
|
|
998
|
+
else c.enqueue(r.value);
|
|
999
|
+
}),
|
|
993
1000
|
cancel: (reason) => als.run(scope, () => reader.cancel(reason))
|
|
994
1001
|
});
|
|
995
1002
|
}
|
|
1003
|
+
/** Adds `vary` to `Vary` unless it lists it or `*`; `append` joins with ", ", as a server would. */
|
|
1004
|
+
function addVary(headers, vary) {
|
|
1005
|
+
const lower = vary.toLowerCase();
|
|
1006
|
+
if (!(headers.get("vary") ?? "").split(",").some((token) => {
|
|
1007
|
+
const t = token.trim().toLowerCase();
|
|
1008
|
+
return t === "*" || t === lower;
|
|
1009
|
+
})) headers.append("vary", vary);
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Dev: a page that formats times in the visitor's zone (`cookie`, `cf`)
|
|
1013
|
+
* must not be publicly cacheable.
|
|
1014
|
+
*/
|
|
1015
|
+
function warnIfPublic(headers, scope, request) {
|
|
1016
|
+
const cc = headers.get("cache-control");
|
|
1017
|
+
const zoned = scope.timeZoneSource === "cookie" || scope.timeZoneSource === "cf";
|
|
1018
|
+
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`);
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* `Vary`, and `Cache-Control: private` where no shared cache may serve the
|
|
1022
|
+
* response to another visitor. Rendered in this visitor's zone (`perUser`),
|
|
1023
|
+
* the app's own Cache-Control wins. A locale the visitor's cookie or
|
|
1024
|
+
* languages chose (`Vary: Accept-Language`) may come from the cookie, which
|
|
1025
|
+
* no shared cache varies on: none may store the response, whatever the app
|
|
1026
|
+
* allowed (`private` overrides `public` and `s-maxage`). PLAN §5.14.
|
|
1027
|
+
*/
|
|
1028
|
+
function cacheHeaders(headers, vary, perUser) {
|
|
1029
|
+
if (vary) addVary(headers, vary);
|
|
1030
|
+
const cc = headers.get("cache-control");
|
|
1031
|
+
if (vary === "Accept-Language" ? !cc || !/private|no-store/i.test(cc) : perUser && !cc) headers.set("cache-control", cc ? "private, " + cc : "private");
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* An HTML copy's headers: `Content-Language`, and where the payload is
|
|
1035
|
+
* injected (`plain`) no `Content-Length` and a weak ETag.
|
|
1036
|
+
*/
|
|
1037
|
+
function htmlHeaders(headers, scope, plain) {
|
|
1038
|
+
if (!headers.has("content-language")) headers.set("content-language", scope.locale);
|
|
1039
|
+
if (!plain) return;
|
|
1040
|
+
headers.delete("content-length");
|
|
1041
|
+
const etag = headers.get("etag");
|
|
1042
|
+
if (etag && !etag.startsWith("W/")) headers.set("etag", "W/" + etag);
|
|
1043
|
+
}
|
|
1044
|
+
/** Dev: why an HTML response gets no payload, or one its charset would garble. */
|
|
1045
|
+
function warnHtml(request, headers, plain) {
|
|
1046
|
+
const path = new URL(request.url).pathname;
|
|
1047
|
+
if (!plain) {
|
|
1048
|
+
warnOnce(`${path}: ${headers.get("content-encoding")}-encoded HTML is not injected; let the server compress after handle()`);
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
const type = headers.get("content-type");
|
|
1052
|
+
if (/charset=(?!"?utf-?8)/i.test(type)) warnOnce(`${path}: the payload is UTF-8; serve HTML as UTF-8 (${type})`);
|
|
1053
|
+
}
|
|
996
1054
|
/**
|
|
997
1055
|
* A copy of `response` with the scope's headers and its body read inside
|
|
998
1056
|
* the scope: injected for HTML, unchanged otherwise.
|
|
999
1057
|
*/
|
|
1000
|
-
function finish(request, response, scope,
|
|
1058
|
+
function finish(request, response, scope, vary, options) {
|
|
1001
1059
|
if (response.status < 200) return response;
|
|
1002
1060
|
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;
|
|
1061
|
+
const html = type !== null && HTML.test(type);
|
|
1062
|
+
const perUser = (scope.timeZoneSource === "cookie" || scope.timeZoneSource === "cf") && (scope.formattedTime === true || html);
|
|
1007
1063
|
const body = response.body;
|
|
1008
1064
|
if (!html && !vary && !perUser && !body) return response;
|
|
1009
1065
|
const encoding = response.headers.get("content-encoding");
|
|
1010
1066
|
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
1067
|
const script = () => options?.payload ?? reg !== bare ? scriptFor(scope, options?.nonce) : "";
|
|
1016
|
-
const
|
|
1068
|
+
const tail = () => {
|
|
1069
|
+
warnIfPublic(response.headers, scope, request);
|
|
1070
|
+
return script();
|
|
1071
|
+
};
|
|
1072
|
+
const inject = plain && request.method !== "HEAD" ? injector({
|
|
1017
1073
|
attributes: () => ({
|
|
1018
1074
|
lang: scope.locale,
|
|
1019
1075
|
dir: textDirection(scope.locale)
|
|
1020
1076
|
}),
|
|
1021
1077
|
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
|
-
}
|
|
1078
|
+
tail: DEV ? tail : script
|
|
1079
|
+
}) : void 0;
|
|
1080
|
+
const res = new Response(body && inScope(body, scope, inject), response);
|
|
1081
|
+
cacheHeaders(res.headers, vary, perUser);
|
|
1082
|
+
if (DEV) warnIfPublic(response.headers, scope, request);
|
|
1083
|
+
if (html) htmlHeaders(res.headers, scope, plain);
|
|
1084
|
+
if (DEV && html) warnHtml(request, res.headers, plain);
|
|
1041
1085
|
return res;
|
|
1042
1086
|
}
|
|
1043
1087
|
/**
|
|
@@ -1062,10 +1106,9 @@ async function handle(request, next, options) {
|
|
|
1062
1106
|
});
|
|
1063
1107
|
}
|
|
1064
1108
|
const scope = await open(config, request, url, f ?? n.locale, options);
|
|
1065
|
-
if (ROUTES) scope.home = hostLocale(url.host, config);
|
|
1066
1109
|
const response = await als.run(scope, () => next(request));
|
|
1067
1110
|
if (DEV && !(response instanceof Response)) throw error("handle(): next() must return a Response");
|
|
1068
|
-
return finish(request, response, scope, n,
|
|
1111
|
+
return finish(request, response, scope, n?.vary, options);
|
|
1069
1112
|
}
|
|
1070
1113
|
/**
|
|
1071
1114
|
* Runs `fn` in the request's scope, for servers that build the response
|
|
@@ -1077,7 +1120,6 @@ async function withRequest(request, fn, options) {
|
|
|
1077
1120
|
const config = current();
|
|
1078
1121
|
const url = new URL(request.url);
|
|
1079
1122
|
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
1123
|
const value = await als.run(scope, fn);
|
|
1082
1124
|
return value instanceof Response && value.body ? new Response(inScope(value.body, scope), value) : value;
|
|
1083
1125
|
}
|
|
@@ -1096,8 +1138,7 @@ async function scopeLocale(locale) {
|
|
|
1096
1138
|
scope.messages = cat.messages;
|
|
1097
1139
|
scope.labels = cat.labels;
|
|
1098
1140
|
}
|
|
1099
|
-
|
|
1100
|
-
//#region src/server/seo.ts
|
|
1141
|
+
// src/server/seo.ts
|
|
1101
1142
|
/**
|
|
1102
1143
|
* Prerender and SEO helpers (PLAN §5.13, §5.14): every locale's URL of a page
|
|
1103
1144
|
* for crawlers (`alternates`), and the paths or params a prerenderer walks
|
|
@@ -1230,5 +1271,4 @@ function localizedParams(key = "lang") {
|
|
|
1230
1271
|
}
|
|
1231
1272
|
return out;
|
|
1232
1273
|
}
|
|
1233
|
-
//#endregion
|
|
1234
1274
|
export { alternates, createTranslator, getScope, handle, htmlAttributes, localizedParams, localizedPaths, negotiate, payload, registerCatalogs, scopeLocale, setScopeResolver, withLocale, withRequest };
|