@bleedingdev/modern-js-plugin-i18n 3.8.2-ultramodern.9 → 3.8.3-ultramodern.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/dist/cjs/cli/index.js +3 -3
- package/dist/cjs/runtime/context.js +19 -6
- package/dist/cjs/runtime/hooks.js +8 -11
- package/dist/cjs/runtime/localizedPaths.js +2 -2
- package/dist/cjs/runtime/providerComposition.js +3 -1
- package/dist/cjs/runtime/utils.js +3 -3
- package/dist/cjs/server/index.js +2 -2
- package/dist/cjs/server/redirectPolicy.js +6 -6
- package/dist/esm/cli/index.mjs +1 -1
- package/dist/esm/runtime/context.mjs +19 -6
- package/dist/esm/runtime/hooks.mjs +8 -11
- package/dist/esm/runtime/localizedPaths.mjs +1 -1
- package/dist/esm/runtime/providerComposition.mjs +3 -1
- package/dist/esm/runtime/utils.mjs +1 -1
- package/dist/esm/server/index.mjs +1 -1
- package/dist/esm/server/redirectPolicy.mjs +1 -1
- package/dist/esm-node/cli/index.mjs +1 -1
- package/dist/esm-node/runtime/context.mjs +19 -6
- package/dist/esm-node/runtime/hooks.mjs +8 -11
- package/dist/esm-node/runtime/localizedPaths.mjs +1 -1
- package/dist/esm-node/runtime/providerComposition.mjs +3 -1
- package/dist/esm-node/runtime/utils.mjs +1 -1
- package/dist/esm-node/server/index.mjs +1 -1
- package/dist/esm-node/server/redirectPolicy.mjs +1 -1
- package/dist/types/runtime/context.d.ts +3 -2
- package/dist/types/runtime/contextHelpers.d.ts +1 -1
- package/dist/types/runtime/hooks.d.ts +1 -1
- package/dist/types/runtime/localizedPaths.d.ts +1 -1
- package/dist/types/runtime/utils.d.ts +1 -1
- package/dist/types/shared/type.d.ts +1 -1
- package/package.json +32 -11
- package/src/cli/index.ts +4 -4
- package/src/runtime/context.tsx +38 -14
- package/src/runtime/contextHelpers.ts +1 -1
- package/src/runtime/hooks.ts +9 -10
- package/src/runtime/localizedPaths.ts +4 -2
- package/src/runtime/providerComposition.tsx +5 -2
- package/src/runtime/utils.ts +4 -4
- package/src/server/index.ts +1 -1
- package/src/server/redirectPolicy.ts +1 -1
- package/src/shared/type.ts +1 -1
- package/tests/federatedI18nBoundary.client.test.tsx +21 -4
- package/tests/federatedI18nBoundary.test.tsx +15 -3
- package/tests/linkTypes.test.ts +20 -0
- package/tests/localisedUrlRewriteMatrix.fork.test.ts +1 -1
- package/tests/localisedUrls.test.ts +0 -351
- package/tests/routerAdapter.test.tsx +5 -12
- package/dist/cjs/shared/localisedUrls/config.js +0 -49
- package/dist/cjs/shared/localisedUrls/index.js +0 -91
- package/dist/cjs/shared/localisedUrls/normalise.js +0 -87
- package/dist/cjs/shared/localisedUrls/pathname.js +0 -68
- package/dist/cjs/shared/localisedUrls/patterns.js +0 -130
- package/dist/cjs/shared/localisedUrls/redirect.js +0 -103
- package/dist/cjs/shared/localisedUrls/resolve.js +0 -88
- package/dist/cjs/shared/localisedUrls/routes.js +0 -130
- package/dist/cjs/shared/localisedUrls/types.js +0 -18
- package/dist/cjs/shared/localisedUrls.js +0 -85
- package/dist/esm/shared/localisedUrls/config.mjs +0 -11
- package/dist/esm/shared/localisedUrls/index.mjs +0 -7
- package/dist/esm/shared/localisedUrls/normalise.mjs +0 -37
- package/dist/esm/shared/localisedUrls/pathname.mjs +0 -24
- package/dist/esm/shared/localisedUrls/patterns.mjs +0 -86
- package/dist/esm/shared/localisedUrls/redirect.mjs +0 -56
- package/dist/esm/shared/localisedUrls/resolve.mjs +0 -47
- package/dist/esm/shared/localisedUrls/routes.mjs +0 -89
- package/dist/esm/shared/localisedUrls/types.mjs +0 -0
- package/dist/esm/shared/localisedUrls.mjs +0 -1
- package/dist/esm-node/shared/localisedUrls/config.mjs +0 -12
- package/dist/esm-node/shared/localisedUrls/index.mjs +0 -8
- package/dist/esm-node/shared/localisedUrls/normalise.mjs +0 -38
- package/dist/esm-node/shared/localisedUrls/pathname.mjs +0 -25
- package/dist/esm-node/shared/localisedUrls/patterns.mjs +0 -87
- package/dist/esm-node/shared/localisedUrls/redirect.mjs +0 -57
- package/dist/esm-node/shared/localisedUrls/resolve.mjs +0 -48
- package/dist/esm-node/shared/localisedUrls/routes.mjs +0 -90
- package/dist/esm-node/shared/localisedUrls/types.mjs +0 -1
- package/dist/esm-node/shared/localisedUrls.mjs +0 -2
- package/dist/types/shared/localisedUrls/config.d.ts +0 -9
- package/dist/types/shared/localisedUrls/index.d.ts +0 -9
- package/dist/types/shared/localisedUrls/normalise.d.ts +0 -10
- package/dist/types/shared/localisedUrls/pathname.d.ts +0 -4
- package/dist/types/shared/localisedUrls/patterns.d.ts +0 -5
- package/dist/types/shared/localisedUrls/redirect.d.ts +0 -11
- package/dist/types/shared/localisedUrls/resolve.d.ts +0 -7
- package/dist/types/shared/localisedUrls/routes.d.ts +0 -3
- package/dist/types/shared/localisedUrls/types.d.ts +0 -14
- package/dist/types/shared/localisedUrls.d.ts +0 -2
- package/src/shared/localisedUrls/config.ts +0 -18
- package/src/shared/localisedUrls/index.ts +0 -24
- package/src/shared/localisedUrls/normalise.ts +0 -67
- package/src/shared/localisedUrls/pathname.ts +0 -60
- package/src/shared/localisedUrls/patterns.ts +0 -160
- package/src/shared/localisedUrls/redirect.ts +0 -93
- package/src/shared/localisedUrls/resolve.ts +0 -135
- package/src/shared/localisedUrls/routes.ts +0 -234
- package/src/shared/localisedUrls/types.ts +0 -18
- package/src/shared/localisedUrls.ts +0 -26
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import { getLeadingLocaleParam, normalisePathPattern, normaliseRoutePath, stripLeadingLocaleParam } from "./normalise.mjs";
|
|
3
|
-
const isLocalisableRoutePath = (path)=>{
|
|
4
|
-
const pathWithoutLocale = stripLeadingLocaleParam(path);
|
|
5
|
-
if (!pathWithoutLocale || '/' === pathWithoutLocale || '*' === pathWithoutLocale) return false;
|
|
6
|
-
return true;
|
|
7
|
-
};
|
|
8
|
-
const joinPath = (parentPath, routePath)=>{
|
|
9
|
-
if (!isLocalisableRoutePath(routePath)) return parentPath;
|
|
10
|
-
const segment = normaliseRoutePath(stripLeadingLocaleParam(routePath) || '');
|
|
11
|
-
return normalisePathPattern(`${parentPath}/${segment}`);
|
|
12
|
-
};
|
|
13
|
-
const isFrameworkInternalRoutePath = (canonicalPath)=>'/_mf' === canonicalPath || canonicalPath.startsWith('/_mf/');
|
|
14
|
-
const ensureLocalisedUrlsForPath = (canonicalPath, languages, localisedUrls)=>{
|
|
15
|
-
const entry = localisedUrls[canonicalPath];
|
|
16
|
-
if (!entry) throw new Error(`localisedUrls is enabled, but route "${canonicalPath}" does not define localised URLs for languages: ${languages.join(', ')}. Add localisedUrls["${canonicalPath}"] or set localeDetection.localisedUrls to false.`);
|
|
17
|
-
const missingLanguages = languages.filter((language)=>!entry[language]);
|
|
18
|
-
if (missingLanguages.length > 0) throw new Error(`localisedUrls["${canonicalPath}"] is missing languages: ${missingLanguages.join(', ')}. Every configured language must have a localised URL.`);
|
|
19
|
-
return entry;
|
|
20
|
-
};
|
|
21
|
-
const validateLocalisedUrls = (routes, languages, localisedUrls)=>{
|
|
22
|
-
const visit = (route, parentPath)=>{
|
|
23
|
-
const canonicalPath = joinPath(parentPath, route.path);
|
|
24
|
-
if (isLocalisableRoutePath(route.path) && !isFrameworkInternalRoutePath(canonicalPath)) ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls);
|
|
25
|
-
if ('children' in route && route.children) route.children.forEach((child)=>visit(child, canonicalPath));
|
|
26
|
-
};
|
|
27
|
-
routes.forEach((route)=>visit(route, ''));
|
|
28
|
-
};
|
|
29
|
-
const getLocalisedRoutePaths = (canonicalPath, parentLocalisedPaths, languages, entry)=>{
|
|
30
|
-
const paths = languages.map((language)=>{
|
|
31
|
-
const fullPath = normalisePathPattern(entry[language]);
|
|
32
|
-
const parentPath = normalisePathPattern(parentLocalisedPaths[language] || '/');
|
|
33
|
-
if ('/' === parentPath) return normaliseRoutePath(fullPath) || void 0;
|
|
34
|
-
const parentPrefix = `${parentPath}/`;
|
|
35
|
-
if (!fullPath.startsWith(parentPrefix)) throw new Error(`localisedUrls["${canonicalPath}"].${language} must be nested under "${parentPath}" because its parent route is localised there.`);
|
|
36
|
-
return normaliseRoutePath(fullPath.slice(parentPath.length));
|
|
37
|
-
});
|
|
38
|
-
return Array.from(new Set(paths.filter(Boolean)));
|
|
39
|
-
};
|
|
40
|
-
const transformLocalisedRoute = (route, parentCanonicalPath, parentLocalisedPaths, languages, localisedUrls)=>{
|
|
41
|
-
const canonicalPath = joinPath(parentCanonicalPath, route.path);
|
|
42
|
-
const localisedUrlEntry = isLocalisableRoutePath(route.path) && !isFrameworkInternalRoutePath(canonicalPath) ? ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls) : void 0;
|
|
43
|
-
const routeLocalisedPaths = localisedUrlEntry ? languages.reduce((acc, language)=>{
|
|
44
|
-
acc[language] = normalisePathPattern(localisedUrlEntry[language]);
|
|
45
|
-
return acc;
|
|
46
|
-
}, {}) : parentLocalisedPaths;
|
|
47
|
-
const children = 'children' in route && route.children ? route.children.flatMap((child)=>transformLocalisedRoute(child, canonicalPath, routeLocalisedPaths, languages, localisedUrls)) : void 0;
|
|
48
|
-
const baseRoute = {
|
|
49
|
-
...route,
|
|
50
|
-
...children ? {
|
|
51
|
-
children
|
|
52
|
-
} : {}
|
|
53
|
-
};
|
|
54
|
-
if (!localisedUrlEntry) return [
|
|
55
|
-
baseRoute
|
|
56
|
-
];
|
|
57
|
-
return getLocalisedRoutePaths(canonicalPath, parentLocalisedPaths, languages, localisedUrlEntry).map((localisedPath, index)=>cloneRouteWithLocalisedPath(baseRoute, localisedPath, index, canonicalPath));
|
|
58
|
-
};
|
|
59
|
-
const legalRouteIdPart = (value)=>value.replace(/[^a-zA-Z0-9_$-]+/g, '_').replace(/^_+|_+$/g, '') || 'index';
|
|
60
|
-
const suffixRouteIds = (route, suffix)=>{
|
|
61
|
-
const children = 'children' in route && route.children ? route.children.map((child)=>suffixRouteIds(child, suffix)) : void 0;
|
|
62
|
-
return {
|
|
63
|
-
...route,
|
|
64
|
-
...route.id ? {
|
|
65
|
-
id: `${route.id}__localised_${suffix}`
|
|
66
|
-
} : {},
|
|
67
|
-
...children ? {
|
|
68
|
-
children
|
|
69
|
-
} : {}
|
|
70
|
-
};
|
|
71
|
-
};
|
|
72
|
-
const cloneRouteWithLocalisedPath = (route, path, index, canonicalPath)=>{
|
|
73
|
-
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
74
|
-
const localisedPath = leadingLocaleParam ? normaliseRoutePath(`${leadingLocaleParam}/${path}`) : path;
|
|
75
|
-
const routeWithPath = {
|
|
76
|
-
...route,
|
|
77
|
-
path: localisedPath
|
|
78
|
-
};
|
|
79
|
-
routeWithPath.modernCanonicalPath = canonicalPath;
|
|
80
|
-
return 0 === index ? routeWithPath : suffixRouteIds(routeWithPath, legalRouteIdPart(localisedPath));
|
|
81
|
-
};
|
|
82
|
-
const applyLocalisedUrlsToRoutes = (routes, languages, localisedUrls)=>{
|
|
83
|
-
const rootLocalisedPaths = languages.reduce((acc, language)=>{
|
|
84
|
-
acc[language] = '/';
|
|
85
|
-
return acc;
|
|
86
|
-
}, {});
|
|
87
|
-
validateLocalisedUrls(routes, languages, localisedUrls);
|
|
88
|
-
return routes.flatMap((route)=>transformLocalisedRoute(route, '', rootLocalisedPaths, languages, localisedUrls));
|
|
89
|
-
};
|
|
90
|
-
export { applyLocalisedUrlsToRoutes, validateLocalisedUrls };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
export { DEFAULT_LOCALE_REDIRECT_SKIP_RULES, applyLocalisedUrlsToRoutes, buildPathFromPattern, canonicalTargetPathname, isDefaultLocaleRedirectSkipPath, localiseTargetPathname, matchPathPattern, matchesPathPrefix, normalisePathPattern, normalisePathname, resolveCanonicalLocalisedPath, resolveLocalisedPath, resolveLocalisedUrlsConfig, shouldSkipLocaleRedirect, stripLanguagePrefix, validateLocalisedUrls } from "./localisedUrls/index.mjs";
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { LocalisedUrlsOption, ResolvedLocalisedUrlsConfig } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Localised URLs are strictly opt-in: only an explicit, non-empty map enables
|
|
4
|
-
* route expansion and validation. `true`, `false`, an empty map and absence
|
|
5
|
-
* all resolve to disabled, so upstream-style configs (`localePathRedirect` +
|
|
6
|
-
* `languages` without a map) keep plain locale-prefix behavior instead of
|
|
7
|
-
* failing the build for every route missing from a map they never wrote.
|
|
8
|
-
*/
|
|
9
|
-
export declare const resolveLocalisedUrlsConfig: (option: LocalisedUrlsOption | undefined) => ResolvedLocalisedUrlsConfig;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export { resolveLocalisedUrlsConfig } from './config';
|
|
2
|
-
export { normalisePathname, normalisePathPattern } from './normalise';
|
|
3
|
-
export { canonicalTargetPathname, localiseTargetPathname, stripLanguagePrefix, } from './pathname';
|
|
4
|
-
export { buildPathFromPattern, matchPathPattern } from './patterns';
|
|
5
|
-
export type { LocaleRedirectSkipRule } from './redirect';
|
|
6
|
-
export { DEFAULT_LOCALE_REDIRECT_SKIP_RULES, isDefaultLocaleRedirectSkipPath, matchesPathPrefix, shouldSkipLocaleRedirect, } from './redirect';
|
|
7
|
-
export { resolveCanonicalLocalisedPath, resolveLocalisedPath } from './resolve';
|
|
8
|
-
export { applyLocalisedUrlsToRoutes, validateLocalisedUrls } from './routes';
|
|
9
|
-
export type { LocalisedRoute, LocalisedUrlPathMap, LocalisedUrlsMap, LocalisedUrlsOption, ResolvedLocalisedUrlsConfig, } from './types';
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export declare const normalisePathPattern: (path: string) => string;
|
|
2
|
-
/**
|
|
3
|
-
* Normalise a concrete request pathname: slash cleanup only. Unlike
|
|
4
|
-
* {@link normalisePathPattern} it must not rewrite literal `[x]` segments to
|
|
5
|
-
* `:x` params — pathnames are values, not patterns.
|
|
6
|
-
*/
|
|
7
|
-
export declare const normalisePathname: (pathname: string) => string;
|
|
8
|
-
export declare const normaliseRoutePath: (path: string) => string;
|
|
9
|
-
export declare const stripLeadingLocaleParam: (path?: string) => string | undefined;
|
|
10
|
-
export declare const getLeadingLocaleParam: (path?: string) => string | null;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { LocalisedUrlsOption } from './types';
|
|
2
|
-
export declare const stripLanguagePrefix: (pathname: string, languages: string[]) => string;
|
|
3
|
-
export declare const localiseTargetPathname: (pathname: string, language: string, languages: string[], localisedUrls?: LocalisedUrlsOption) => string;
|
|
4
|
-
export declare const canonicalTargetPathname: (pathname: string, languages: string[], localisedUrls?: LocalisedUrlsOption) => string;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare const sortPatternsBySpecificity: <T extends {
|
|
2
|
-
pattern: string;
|
|
3
|
-
}>(patterns: T[]) => T[];
|
|
4
|
-
export declare const matchPathPattern: (pathname: string, pattern: string) => Record<string, string> | null;
|
|
5
|
-
export declare const buildPathFromPattern: (pattern: string, params: Record<string, string>) => string;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export type LocaleRedirectSkipRule = {
|
|
2
|
-
type: 'exact';
|
|
3
|
-
path: string;
|
|
4
|
-
} | {
|
|
5
|
-
type: 'prefix';
|
|
6
|
-
path: string;
|
|
7
|
-
};
|
|
8
|
-
export declare const DEFAULT_LOCALE_REDIRECT_SKIP_RULES: readonly LocaleRedirectSkipRule[];
|
|
9
|
-
export declare const matchesPathPrefix: (pathname: string, prefix: string) => boolean;
|
|
10
|
-
export declare const isDefaultLocaleRedirectSkipPath: (pathname: string, languages?: string[]) => boolean;
|
|
11
|
-
export declare const shouldSkipLocaleRedirect: (pathname: string, languages?: string[], ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean)) => boolean;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { LocalisedUrlsMap } from './types';
|
|
2
|
-
export declare const resolveLocalisedPath: (pathname: string, targetLanguage: string, languages: string[], localisedUrls: LocalisedUrlsMap) => string;
|
|
3
|
-
/**
|
|
4
|
-
* Reverse-map language-specific pathnames back to language-agnostic
|
|
5
|
-
* LocalisedUrlsMap keys.
|
|
6
|
-
*/
|
|
7
|
-
export declare const resolveCanonicalLocalisedPath: (pathname: string, languages: string[], localisedUrls: LocalisedUrlsMap) => string;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { LocalisedRoute, LocalisedUrlsMap } from './types';
|
|
2
|
-
export declare const validateLocalisedUrls: (routes: LocalisedRoute[], languages: string[], localisedUrls: LocalisedUrlsMap) => void;
|
|
3
|
-
export declare const applyLocalisedUrlsToRoutes: (routes: LocalisedRoute[], languages: string[], localisedUrls: LocalisedUrlsMap) => LocalisedRoute[];
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export type LocalisedRoute = {
|
|
2
|
-
type: 'nested' | 'page';
|
|
3
|
-
path?: string;
|
|
4
|
-
id?: string;
|
|
5
|
-
children?: LocalisedRoute[];
|
|
6
|
-
[key: string]: any;
|
|
7
|
-
};
|
|
8
|
-
export type LocalisedUrlPathMap = Record<string, string>;
|
|
9
|
-
export type LocalisedUrlsMap = Record<string, LocalisedUrlPathMap>;
|
|
10
|
-
export type LocalisedUrlsOption = boolean | LocalisedUrlsMap;
|
|
11
|
-
export interface ResolvedLocalisedUrlsConfig {
|
|
12
|
-
enabled: boolean;
|
|
13
|
-
map: LocalisedUrlsMap;
|
|
14
|
-
}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export type { LocaleRedirectSkipRule, LocalisedRoute, LocalisedUrlPathMap, LocalisedUrlsMap, LocalisedUrlsOption, ResolvedLocalisedUrlsConfig, } from './localisedUrls/index';
|
|
2
|
-
export { applyLocalisedUrlsToRoutes, buildPathFromPattern, canonicalTargetPathname, DEFAULT_LOCALE_REDIRECT_SKIP_RULES, isDefaultLocaleRedirectSkipPath, localiseTargetPathname, matchesPathPrefix, matchPathPattern, normalisePathname, normalisePathPattern, resolveCanonicalLocalisedPath, resolveLocalisedPath, resolveLocalisedUrlsConfig, shouldSkipLocaleRedirect, stripLanguagePrefix, validateLocalisedUrls, } from './localisedUrls/index';
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { LocalisedUrlsOption, ResolvedLocalisedUrlsConfig } from './types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Localised URLs are strictly opt-in: only an explicit, non-empty map enables
|
|
5
|
-
* route expansion and validation. `true`, `false`, an empty map and absence
|
|
6
|
-
* all resolve to disabled, so upstream-style configs (`localePathRedirect` +
|
|
7
|
-
* `languages` without a map) keep plain locale-prefix behavior instead of
|
|
8
|
-
* failing the build for every route missing from a map they never wrote.
|
|
9
|
-
*/
|
|
10
|
-
export const resolveLocalisedUrlsConfig = (
|
|
11
|
-
option: LocalisedUrlsOption | undefined,
|
|
12
|
-
): ResolvedLocalisedUrlsConfig => {
|
|
13
|
-
if (option && typeof option === 'object' && Object.keys(option).length > 0) {
|
|
14
|
-
return { enabled: true, map: option };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return { enabled: false, map: {} };
|
|
18
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export { resolveLocalisedUrlsConfig } from './config';
|
|
2
|
-
export { normalisePathname, normalisePathPattern } from './normalise';
|
|
3
|
-
export {
|
|
4
|
-
canonicalTargetPathname,
|
|
5
|
-
localiseTargetPathname,
|
|
6
|
-
stripLanguagePrefix,
|
|
7
|
-
} from './pathname';
|
|
8
|
-
export { buildPathFromPattern, matchPathPattern } from './patterns';
|
|
9
|
-
export type { LocaleRedirectSkipRule } from './redirect';
|
|
10
|
-
export {
|
|
11
|
-
DEFAULT_LOCALE_REDIRECT_SKIP_RULES,
|
|
12
|
-
isDefaultLocaleRedirectSkipPath,
|
|
13
|
-
matchesPathPrefix,
|
|
14
|
-
shouldSkipLocaleRedirect,
|
|
15
|
-
} from './redirect';
|
|
16
|
-
export { resolveCanonicalLocalisedPath, resolveLocalisedPath } from './resolve';
|
|
17
|
-
export { applyLocalisedUrlsToRoutes, validateLocalisedUrls } from './routes';
|
|
18
|
-
export type {
|
|
19
|
-
LocalisedRoute,
|
|
20
|
-
LocalisedUrlPathMap,
|
|
21
|
-
LocalisedUrlsMap,
|
|
22
|
-
LocalisedUrlsOption,
|
|
23
|
-
ResolvedLocalisedUrlsConfig,
|
|
24
|
-
} from './types';
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
const LOCALE_PARAM_NAMES = new Set(['lang', 'locale', 'language']);
|
|
2
|
-
|
|
3
|
-
const normaliseSlashes = (path: string): string => {
|
|
4
|
-
const withoutDuplicateSlashes = path.replace(/\/+/g, '/');
|
|
5
|
-
const withLeadingSlash = withoutDuplicateSlashes.startsWith('/')
|
|
6
|
-
? withoutDuplicateSlashes
|
|
7
|
-
: `/${withoutDuplicateSlashes}`;
|
|
8
|
-
|
|
9
|
-
return withLeadingSlash.length > 1
|
|
10
|
-
? withLeadingSlash.replace(/\/+$/, '')
|
|
11
|
-
: withLeadingSlash;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export const normalisePathPattern = (path: string): string =>
|
|
15
|
-
normaliseSlashes(path).replace(/\[(.+?)\]/g, ':$1');
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Normalise a concrete request pathname: slash cleanup only. Unlike
|
|
19
|
-
* {@link normalisePathPattern} it must not rewrite literal `[x]` segments to
|
|
20
|
-
* `:x` params — pathnames are values, not patterns.
|
|
21
|
-
*/
|
|
22
|
-
export const normalisePathname = (pathname: string): string =>
|
|
23
|
-
normaliseSlashes(pathname);
|
|
24
|
-
|
|
25
|
-
export const normaliseRoutePath = (path: string): string => {
|
|
26
|
-
const normalized = normalisePathPattern(path);
|
|
27
|
-
return normalized === '/' ? '' : normalized.slice(1);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const getLocaleParamSegment = (segment: string): string | null => {
|
|
31
|
-
if (!segment.startsWith(':')) {
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const paramName = segment.slice(1).replace(/\?$/, '');
|
|
36
|
-
return LOCALE_PARAM_NAMES.has(paramName) ? segment : null;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const splitPathSegments = (path?: string): string[] => {
|
|
40
|
-
if (!path) {
|
|
41
|
-
return [];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return normalisePathPattern(path).split('/').filter(Boolean);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export const stripLeadingLocaleParam = (path?: string): string | undefined => {
|
|
48
|
-
const segments = splitPathSegments(path);
|
|
49
|
-
const leadingLocaleParam = getLocaleParamSegment(segments[0] || '');
|
|
50
|
-
|
|
51
|
-
if (!leadingLocaleParam) {
|
|
52
|
-
return path;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const remainingPath = segments.slice(1).join('/');
|
|
56
|
-
return remainingPath ? `/${remainingPath}` : undefined;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export const getLeadingLocaleParam = (path?: string): string | null => {
|
|
60
|
-
const segments = splitPathSegments(path);
|
|
61
|
-
return getLocaleParamSegment(segments[0] || '');
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const isLocaleParamPath = (path?: string): boolean => {
|
|
65
|
-
const segments = splitPathSegments(path);
|
|
66
|
-
return segments.length === 1 && Boolean(getLocaleParamSegment(segments[0]));
|
|
67
|
-
};
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { resolveLocalisedUrlsConfig } from './config';
|
|
2
|
-
|
|
3
|
-
import { resolveCanonicalLocalisedPath, resolveLocalisedPath } from './resolve';
|
|
4
|
-
|
|
5
|
-
import type { LocalisedUrlsOption } from './types';
|
|
6
|
-
|
|
7
|
-
export const stripLanguagePrefix = (
|
|
8
|
-
pathname: string,
|
|
9
|
-
languages: string[],
|
|
10
|
-
): string => {
|
|
11
|
-
const segments = pathname.split('/').filter(Boolean);
|
|
12
|
-
const firstSegment = segments[0]?.toLowerCase();
|
|
13
|
-
|
|
14
|
-
if (
|
|
15
|
-
firstSegment &&
|
|
16
|
-
languages.some(language => language.toLowerCase() === firstSegment)
|
|
17
|
-
) {
|
|
18
|
-
return `/${segments.slice(1).join('/')}`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return pathname || '/';
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export const localiseTargetPathname = (
|
|
25
|
-
pathname: string,
|
|
26
|
-
language: string,
|
|
27
|
-
languages: string[],
|
|
28
|
-
localisedUrls?: LocalisedUrlsOption,
|
|
29
|
-
): string => {
|
|
30
|
-
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
31
|
-
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
32
|
-
const resolvedPath = localisedUrlsConfig.enabled
|
|
33
|
-
? resolveLocalisedPath(
|
|
34
|
-
pathWithoutLanguage,
|
|
35
|
-
language,
|
|
36
|
-
languages,
|
|
37
|
-
localisedUrlsConfig.map,
|
|
38
|
-
)
|
|
39
|
-
: pathWithoutLanguage;
|
|
40
|
-
const resolvedSegments = resolvedPath.split('/').filter(Boolean);
|
|
41
|
-
|
|
42
|
-
return `/${[language, ...resolvedSegments].join('/')}`;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export const canonicalTargetPathname = (
|
|
46
|
-
pathname: string,
|
|
47
|
-
languages: string[],
|
|
48
|
-
localisedUrls?: LocalisedUrlsOption,
|
|
49
|
-
): string => {
|
|
50
|
-
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
51
|
-
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
52
|
-
|
|
53
|
-
return localisedUrlsConfig.enabled
|
|
54
|
-
? resolveCanonicalLocalisedPath(
|
|
55
|
-
pathWithoutLanguage,
|
|
56
|
-
languages,
|
|
57
|
-
localisedUrlsConfig.map,
|
|
58
|
-
)
|
|
59
|
-
: pathWithoutLanguage;
|
|
60
|
-
};
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { normalisePathname, normalisePathPattern } from './normalise';
|
|
2
|
-
|
|
3
|
-
const escapeRegExp = (value: string): string =>
|
|
4
|
-
value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
5
|
-
|
|
6
|
-
const getParamName = (segment: string): string =>
|
|
7
|
-
segment.slice(1).replace(/\?$/, '');
|
|
8
|
-
|
|
9
|
-
interface CompiledPathPattern {
|
|
10
|
-
names: string[];
|
|
11
|
-
regexp: RegExp;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const compiledPathPatternCache = new Map<string, CompiledPathPattern>();
|
|
15
|
-
|
|
16
|
-
const compilePathPattern = (pattern: string): CompiledPathPattern => {
|
|
17
|
-
const normalizedPattern = normalisePathPattern(pattern);
|
|
18
|
-
const cached = compiledPathPatternCache.get(normalizedPattern);
|
|
19
|
-
if (cached) {
|
|
20
|
-
return cached;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const names: string[] = [];
|
|
24
|
-
const segments = normalizedPattern.split('/').filter(Boolean);
|
|
25
|
-
const source = segments
|
|
26
|
-
.map(segment => {
|
|
27
|
-
if (segment.startsWith(':')) {
|
|
28
|
-
names.push(getParamName(segment));
|
|
29
|
-
const paramPattern = '([^/]+)';
|
|
30
|
-
return segment.endsWith('?')
|
|
31
|
-
? `(?:/${paramPattern})?`
|
|
32
|
-
: `/${paramPattern}`;
|
|
33
|
-
}
|
|
34
|
-
if (segment === '*') {
|
|
35
|
-
names.push('*');
|
|
36
|
-
return '/(.*)';
|
|
37
|
-
}
|
|
38
|
-
return `/${escapeRegExp(segment)}`;
|
|
39
|
-
})
|
|
40
|
-
.join('');
|
|
41
|
-
|
|
42
|
-
const compiled = {
|
|
43
|
-
names,
|
|
44
|
-
regexp: new RegExp(`^${source || '/'}$`),
|
|
45
|
-
};
|
|
46
|
-
compiledPathPatternCache.set(normalizedPattern, compiled);
|
|
47
|
-
|
|
48
|
-
return compiled;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const getPatternSpecificity = (pattern: string) => {
|
|
52
|
-
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
53
|
-
let staticSegments = 0;
|
|
54
|
-
let dynamicSegments = 0;
|
|
55
|
-
let splatSegments = 0;
|
|
56
|
-
|
|
57
|
-
for (const segment of segments) {
|
|
58
|
-
if (segment === '*') {
|
|
59
|
-
splatSegments++;
|
|
60
|
-
} else if (segment.startsWith(':')) {
|
|
61
|
-
dynamicSegments++;
|
|
62
|
-
} else {
|
|
63
|
-
staticSegments++;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
staticSegments,
|
|
69
|
-
dynamicSegments,
|
|
70
|
-
splatSegments,
|
|
71
|
-
totalSegments: segments.length,
|
|
72
|
-
};
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const comparePatternSpecificity = (left: string, right: string): number => {
|
|
76
|
-
const a = getPatternSpecificity(left);
|
|
77
|
-
const b = getPatternSpecificity(right);
|
|
78
|
-
|
|
79
|
-
return (
|
|
80
|
-
b.staticSegments - a.staticSegments ||
|
|
81
|
-
b.totalSegments - a.totalSegments ||
|
|
82
|
-
a.splatSegments - b.splatSegments ||
|
|
83
|
-
a.dynamicSegments - b.dynamicSegments
|
|
84
|
-
);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export const sortPatternsBySpecificity = <T extends { pattern: string }>(
|
|
88
|
-
patterns: T[],
|
|
89
|
-
): T[] =>
|
|
90
|
-
patterns
|
|
91
|
-
.map((pattern, index) => ({ pattern, index }))
|
|
92
|
-
.sort(
|
|
93
|
-
(left, right) =>
|
|
94
|
-
comparePatternSpecificity(
|
|
95
|
-
left.pattern.pattern,
|
|
96
|
-
right.pattern.pattern,
|
|
97
|
-
) || left.index - right.index,
|
|
98
|
-
)
|
|
99
|
-
.map(({ pattern }) => pattern);
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* `decodeURIComponent` throws `URIError` on malformed percent-encoding
|
|
103
|
-
* (e.g. `%E0%A4%A`), which attacker-controlled request URLs can carry.
|
|
104
|
-
* Treat such segments as undecodable instead of throwing.
|
|
105
|
-
*/
|
|
106
|
-
const decodePathParam = (value: string): string | null => {
|
|
107
|
-
try {
|
|
108
|
-
return decodeURIComponent(value);
|
|
109
|
-
} catch {
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const encodeSplatParam = (value: string): string =>
|
|
115
|
-
value.split('/').map(encodeURIComponent).join('/');
|
|
116
|
-
|
|
117
|
-
export const matchPathPattern = (
|
|
118
|
-
pathname: string,
|
|
119
|
-
pattern: string,
|
|
120
|
-
): Record<string, string> | null => {
|
|
121
|
-
const { names, regexp } = compilePathPattern(pattern);
|
|
122
|
-
const match = regexp.exec(normalisePathname(pathname));
|
|
123
|
-
if (!match) {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const params: Record<string, string> = {};
|
|
128
|
-
for (let index = 0; index < names.length; index++) {
|
|
129
|
-
const decoded = decodePathParam(match[index + 1] || '');
|
|
130
|
-
if (decoded === null) {
|
|
131
|
-
// Malformed encoding cannot identify a localised route: no match.
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
params[names[index]] = decoded;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return params;
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
export const buildPathFromPattern = (
|
|
141
|
-
pattern: string,
|
|
142
|
-
params: Record<string, string>,
|
|
143
|
-
): string => {
|
|
144
|
-
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
145
|
-
const path = segments
|
|
146
|
-
.map(segment => {
|
|
147
|
-
if (segment.startsWith(':')) {
|
|
148
|
-
const param = params[getParamName(segment)];
|
|
149
|
-
return param ? encodeURIComponent(param) : '';
|
|
150
|
-
}
|
|
151
|
-
if (segment === '*') {
|
|
152
|
-
return encodeSplatParam(params['*'] || '');
|
|
153
|
-
}
|
|
154
|
-
return segment;
|
|
155
|
-
})
|
|
156
|
-
.filter(Boolean)
|
|
157
|
-
.join('/');
|
|
158
|
-
|
|
159
|
-
return `/${path}`;
|
|
160
|
-
};
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { normalisePathname } from './normalise';
|
|
2
|
-
import { stripLanguagePrefix } from './pathname';
|
|
3
|
-
|
|
4
|
-
export type LocaleRedirectSkipRule =
|
|
5
|
-
| {
|
|
6
|
-
type: 'exact';
|
|
7
|
-
path: string;
|
|
8
|
-
}
|
|
9
|
-
| {
|
|
10
|
-
type: 'prefix';
|
|
11
|
-
path: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export const DEFAULT_LOCALE_REDIRECT_SKIP_RULES: readonly LocaleRedirectSkipRule[] =
|
|
15
|
-
[
|
|
16
|
-
{ type: 'exact', path: '/backend-mf-manifest.json' },
|
|
17
|
-
{ type: 'exact', path: '/backendRemoteEntry.cjs' },
|
|
18
|
-
{ type: 'exact', path: '/mf-manifest.json' },
|
|
19
|
-
{ type: 'exact', path: '/mf-stats.json' },
|
|
20
|
-
{ type: 'exact', path: '/remoteEntry.js' },
|
|
21
|
-
{ type: 'prefix', path: '/static/' },
|
|
22
|
-
{ type: 'prefix', path: '/upload/' },
|
|
23
|
-
];
|
|
24
|
-
|
|
25
|
-
const stripTrailingSlash = (pathname: string): string =>
|
|
26
|
-
pathname.length > 1 ? pathname.replace(/\/+$/, '') : pathname;
|
|
27
|
-
|
|
28
|
-
export const matchesPathPrefix = (
|
|
29
|
-
pathname: string,
|
|
30
|
-
prefix: string,
|
|
31
|
-
): boolean => {
|
|
32
|
-
const normalizedPathname = normalisePathname(pathname);
|
|
33
|
-
const normalizedPrefix = stripTrailingSlash(normalisePathname(prefix));
|
|
34
|
-
|
|
35
|
-
return (
|
|
36
|
-
normalizedPathname === normalizedPrefix ||
|
|
37
|
-
normalizedPathname.startsWith(`${normalizedPrefix}/`)
|
|
38
|
-
);
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const matchesSkipRule = (
|
|
42
|
-
pathname: string,
|
|
43
|
-
rule: LocaleRedirectSkipRule,
|
|
44
|
-
): boolean => {
|
|
45
|
-
if (rule.type === 'exact') {
|
|
46
|
-
return normalisePathname(pathname) === normalisePathname(rule.path);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return matchesPathPrefix(pathname, rule.path);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export const isDefaultLocaleRedirectSkipPath = (
|
|
53
|
-
pathname: string,
|
|
54
|
-
languages: string[] = [],
|
|
55
|
-
): boolean => {
|
|
56
|
-
const normalizedPathname = normalisePathname(pathname);
|
|
57
|
-
const pathWithoutLanguage = stripLanguagePrefix(
|
|
58
|
-
normalizedPathname,
|
|
59
|
-
languages,
|
|
60
|
-
);
|
|
61
|
-
|
|
62
|
-
return DEFAULT_LOCALE_REDIRECT_SKIP_RULES.some(rule =>
|
|
63
|
-
matchesSkipRule(pathWithoutLanguage, rule),
|
|
64
|
-
);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const shouldSkipLocaleRedirect = (
|
|
68
|
-
pathname: string,
|
|
69
|
-
languages: string[] = [],
|
|
70
|
-
ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean),
|
|
71
|
-
): boolean => {
|
|
72
|
-
const normalizedPathname = normalisePathname(pathname);
|
|
73
|
-
const pathWithoutLanguage = stripLanguagePrefix(
|
|
74
|
-
normalizedPathname,
|
|
75
|
-
languages,
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
if (isDefaultLocaleRedirectSkipPath(pathWithoutLanguage)) {
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (!ignoreRedirectRoutes) {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (typeof ignoreRedirectRoutes === 'function') {
|
|
87
|
-
return ignoreRedirectRoutes(pathWithoutLanguage);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return ignoreRedirectRoutes.some(pattern =>
|
|
91
|
-
matchesPathPrefix(pathWithoutLanguage, pattern),
|
|
92
|
-
);
|
|
93
|
-
};
|