@bleedingdev/modern-js-runtime-extensions 3.9.0-ultramodern.11 → 3.9.0-ultramodern.12
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/localisedUrls/config.js +49 -0
- package/dist/cjs/localisedUrls/index.js +95 -0
- package/dist/cjs/localisedUrls/normalise.js +87 -0
- package/dist/cjs/localisedUrls/pathname.js +68 -0
- package/dist/cjs/localisedUrls/patterns.js +214 -0
- package/dist/cjs/localisedUrls/redirect.js +100 -0
- package/dist/cjs/localisedUrls/resolve.js +78 -0
- package/dist/cjs/localisedUrls/routeIdentity.js +92 -0
- package/dist/cjs/localisedUrls/routes.js +217 -0
- package/dist/cjs/localisedUrls/types.js +18 -0
- package/dist/cjs/routerState.js +48 -77
- package/dist/cjs/routerStatePlugin.js +36 -9
- package/dist/esm/localisedUrls/config.mjs +11 -0
- package/dist/esm/localisedUrls/index.mjs +8 -0
- package/dist/esm/localisedUrls/normalise.mjs +37 -0
- package/dist/esm/localisedUrls/pathname.mjs +24 -0
- package/dist/esm/localisedUrls/patterns.mjs +152 -0
- package/dist/esm/localisedUrls/redirect.mjs +53 -0
- package/dist/esm/localisedUrls/resolve.mjs +37 -0
- package/dist/esm/localisedUrls/routeIdentity.mjs +54 -0
- package/dist/esm/localisedUrls/routes.mjs +176 -0
- package/dist/esm/localisedUrls/types.mjs +0 -0
- package/dist/esm/routerState.mjs +49 -75
- package/dist/esm/routerStatePlugin.mjs +37 -10
- package/dist/esm-node/localisedUrls/config.mjs +12 -0
- package/dist/esm-node/localisedUrls/index.mjs +9 -0
- package/dist/esm-node/localisedUrls/normalise.mjs +38 -0
- package/dist/esm-node/localisedUrls/pathname.mjs +25 -0
- package/dist/esm-node/localisedUrls/patterns.mjs +153 -0
- package/dist/esm-node/localisedUrls/redirect.mjs +54 -0
- package/dist/esm-node/localisedUrls/resolve.mjs +38 -0
- package/dist/esm-node/localisedUrls/routeIdentity.mjs +55 -0
- package/dist/esm-node/localisedUrls/routes.mjs +177 -0
- package/dist/esm-node/localisedUrls/types.mjs +1 -0
- package/dist/esm-node/routerState.mjs +49 -75
- package/dist/esm-node/routerStatePlugin.mjs +37 -10
- package/dist/types/localisedUrls/config.d.ts +9 -0
- package/dist/types/localisedUrls/index.d.ts +10 -0
- package/dist/types/localisedUrls/normalise.d.ts +10 -0
- package/dist/types/localisedUrls/pathname.d.ts +4 -0
- package/dist/types/localisedUrls/patterns.d.ts +11 -0
- package/dist/types/localisedUrls/redirect.d.ts +19 -0
- package/dist/types/localisedUrls/resolve.d.ts +7 -0
- package/dist/types/localisedUrls/routeIdentity.d.ts +22 -0
- package/dist/types/localisedUrls/routes.d.ts +3 -0
- package/dist/types/localisedUrls/types.d.ts +14 -0
- package/dist/types/routeHydration.d.ts +2 -2
- package/dist/types/routerState.d.ts +5 -6
- package/dist/types/routerStateTypes.d.ts +47 -17
- package/package.json +16 -4
- package/src/localisedUrls/config.ts +18 -0
- package/src/localisedUrls/index.ts +25 -0
- package/src/localisedUrls/normalise.ts +62 -0
- package/src/localisedUrls/pathname.ts +60 -0
- package/src/localisedUrls/patterns.ts +283 -0
- package/src/localisedUrls/redirect.ts +107 -0
- package/src/localisedUrls/resolve.ts +106 -0
- package/src/localisedUrls/routeIdentity.ts +83 -0
- package/src/localisedUrls/routes.ts +451 -0
- package/src/localisedUrls/types.ts +18 -0
- package/src/routeHydration.ts +2 -2
- package/src/routerState.ts +52 -122
- package/src/routerStatePlugin.ts +49 -10
- package/src/routerStateTypes.ts +47 -17
- package/tests/localisedUrls.test.ts +560 -0
- package/tests/routerState.test.ts +7 -8
package/dist/esm/routerState.mjs
CHANGED
|
@@ -7,66 +7,52 @@ function getRouterRuntimeState(runtimeContext) {
|
|
|
7
7
|
function getRouterServerSnapshot(runtimeContext) {
|
|
8
8
|
return routerServerSnapshotExtension.get(runtimeContext);
|
|
9
9
|
}
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
hydrationScript: state.hydrationScript ?? hydrationScripts[0],
|
|
27
|
-
hydrationScripts
|
|
10
|
+
function createRouterServerSnapshot(snapshot) {
|
|
11
|
+
const matches = snapshot.matches?.map((match)=>Object.freeze({
|
|
12
|
+
...match,
|
|
13
|
+
...match.params ? {
|
|
14
|
+
params: Object.freeze({
|
|
15
|
+
...match.params
|
|
16
|
+
})
|
|
17
|
+
} : {}
|
|
18
|
+
}));
|
|
19
|
+
const matchedRouteIds = snapshot.matchedRouteIds ?? matches?.map((match)=>match.assetRouteId ?? match.routeId);
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
...snapshot,
|
|
22
|
+
...snapshot.hydrationScripts ? {
|
|
23
|
+
hydrationScripts: Object.freeze([
|
|
24
|
+
...snapshot.hydrationScripts
|
|
25
|
+
])
|
|
28
26
|
} : {},
|
|
29
27
|
...matchedRouteIds ? {
|
|
30
|
-
matchedRouteIds
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
function createRouterRuntimeState(state) {
|
|
35
|
-
const hasSnapshotState = Boolean(state.serverSnapshot) || Boolean(state.hydrationScript) || Boolean(state.hydrationScripts?.length) || Boolean(state.matchedRouteIds?.length) || Boolean(state.matches?.length);
|
|
36
|
-
const serverSnapshot = state.serverSnapshot ? createRouterServerSnapshot({
|
|
37
|
-
...state.serverSnapshot,
|
|
38
|
-
framework: state.serverSnapshot.framework ?? state.framework,
|
|
39
|
-
basename: state.serverSnapshot.basename ?? state.basename,
|
|
40
|
-
hydrationScript: state.serverSnapshot.hydrationScript ?? state.hydrationScript,
|
|
41
|
-
hydrationScripts: state.serverSnapshot.hydrationScripts ?? state.hydrationScripts,
|
|
42
|
-
matchedRouteIds: state.serverSnapshot.matchedRouteIds ?? state.matchedRouteIds,
|
|
43
|
-
matches: state.serverSnapshot.matches ?? state.matches
|
|
44
|
-
}) : hasSnapshotState ? createRouterServerSnapshot({
|
|
45
|
-
framework: state.framework,
|
|
46
|
-
basename: state.basename,
|
|
47
|
-
hydrationScript: state.hydrationScript,
|
|
48
|
-
hydrationScripts: state.hydrationScripts,
|
|
49
|
-
matchedRouteIds: state.matchedRouteIds,
|
|
50
|
-
matches: state.matches
|
|
51
|
-
}) : void 0;
|
|
52
|
-
const hydrationScripts = toHydrationScripts({
|
|
53
|
-
hydrationScript: state.hydrationScript ?? serverSnapshot?.hydrationScript,
|
|
54
|
-
hydrationScripts: state.hydrationScripts ?? serverSnapshot?.hydrationScripts
|
|
55
|
-
});
|
|
56
|
-
const matchedRouteIds = state.matchedRouteIds ?? serverSnapshot?.matchedRouteIds ?? getMatchedRouteIdsFromMatches(state.matches);
|
|
57
|
-
return {
|
|
58
|
-
...state,
|
|
59
|
-
...hydrationScripts?.length ? {
|
|
60
|
-
hydrationScript: state.hydrationScript ?? hydrationScripts[0],
|
|
61
|
-
hydrationScripts
|
|
28
|
+
matchedRouteIds: Object.freeze([
|
|
29
|
+
...matchedRouteIds
|
|
30
|
+
])
|
|
62
31
|
} : {},
|
|
63
|
-
...
|
|
64
|
-
|
|
32
|
+
...matches ? {
|
|
33
|
+
matches: Object.freeze(matches)
|
|
34
|
+
} : {},
|
|
35
|
+
...snapshot.errors ? {
|
|
36
|
+
errors: Object.freeze({
|
|
37
|
+
...snapshot.errors
|
|
38
|
+
})
|
|
65
39
|
} : {},
|
|
66
|
-
...
|
|
67
|
-
|
|
40
|
+
...snapshot.routerData ? {
|
|
41
|
+
routerData: Object.freeze({
|
|
42
|
+
...snapshot.routerData,
|
|
43
|
+
...snapshot.routerData.loaderData ? {
|
|
44
|
+
loaderData: Object.freeze({
|
|
45
|
+
...snapshot.routerData.loaderData
|
|
46
|
+
})
|
|
47
|
+
} : {},
|
|
48
|
+
...snapshot.routerData.errors ? {
|
|
49
|
+
errors: Object.freeze({
|
|
50
|
+
...snapshot.routerData.errors
|
|
51
|
+
})
|
|
52
|
+
} : {}
|
|
53
|
+
})
|
|
68
54
|
} : {}
|
|
69
|
-
};
|
|
55
|
+
});
|
|
70
56
|
}
|
|
71
57
|
const routerRuntimeStateListeners = new WeakMap();
|
|
72
58
|
function subscribeRouterRuntimeState(runtimeContext, listener) {
|
|
@@ -102,38 +88,26 @@ function notifyRouterRuntimeState(runtimeContext) {
|
|
|
102
88
|
}
|
|
103
89
|
function applyRouterRuntimeState(runtimeContext, state) {
|
|
104
90
|
const previous = routerRuntimeStateExtension.get(runtimeContext);
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (normalized.serverSnapshot) routerServerSnapshotExtension.set(runtimeContext, normalized.serverSnapshot);
|
|
108
|
-
if (previous?.instance !== normalized.instance || previous?.framework !== normalized.framework) notifyRouterRuntimeState(runtimeContext);
|
|
91
|
+
routerRuntimeStateExtension.set(runtimeContext, state);
|
|
92
|
+
if (previous?.instance !== state.instance || previous?.framework !== state.framework || previous?.navigation !== state.navigation) notifyRouterRuntimeState(runtimeContext);
|
|
109
93
|
return runtimeContext;
|
|
110
94
|
}
|
|
111
95
|
function applyRouterServerPrepareResult(runtimeContext, result) {
|
|
112
|
-
|
|
96
|
+
if (result.snapshot) routerServerSnapshotExtension.set(runtimeContext, createRouterServerSnapshot(result.snapshot));
|
|
97
|
+
return applyRouterRuntimeState(runtimeContext, {
|
|
113
98
|
...result.state,
|
|
114
|
-
cleanup: result.cleanup ?? result.state.cleanup
|
|
115
|
-
serverSnapshot: result.snapshot ?? result.state.serverSnapshot
|
|
99
|
+
cleanup: result.cleanup ?? result.state.cleanup
|
|
116
100
|
});
|
|
117
|
-
applyRouterRuntimeState(runtimeContext, state);
|
|
118
|
-
return runtimeContext;
|
|
119
101
|
}
|
|
120
102
|
function getRouterHydrationScripts(runtimeContext) {
|
|
121
|
-
|
|
122
|
-
const runtimeState = getRouterRuntimeState(runtimeContext);
|
|
123
|
-
return serverSnapshot?.hydrationScripts ?? toHydrationScripts({
|
|
124
|
-
hydrationScript: serverSnapshot?.hydrationScript
|
|
125
|
-
}) ?? runtimeState?.hydrationScripts ?? toHydrationScripts({
|
|
126
|
-
hydrationScript: runtimeState?.hydrationScript
|
|
127
|
-
}) ?? [];
|
|
103
|
+
return getRouterServerSnapshot(runtimeContext)?.hydrationScripts ?? [];
|
|
128
104
|
}
|
|
129
105
|
function getRouterMatchedRouteIds(runtimeContext) {
|
|
130
|
-
|
|
131
|
-
const runtimeState = getRouterRuntimeState(runtimeContext);
|
|
132
|
-
return serverSnapshot?.matchedRouteIds ?? getMatchedRouteIdsFromMatches(serverSnapshot?.matches) ?? runtimeState?.matchedRouteIds ?? getMatchedRouteIdsFromMatches(runtimeState?.matches);
|
|
106
|
+
return getRouterServerSnapshot(runtimeContext)?.matchedRouteIds;
|
|
133
107
|
}
|
|
134
108
|
async function cleanupRouterRuntimeState(runtimeContext) {
|
|
135
109
|
try {
|
|
136
110
|
await getRouterRuntimeState(runtimeContext)?.cleanup?.();
|
|
137
111
|
} catch {}
|
|
138
112
|
}
|
|
139
|
-
export { applyRouterRuntimeState, applyRouterServerPrepareResult, cleanupRouterRuntimeState,
|
|
113
|
+
export { applyRouterRuntimeState, applyRouterServerPrepareResult, cleanupRouterRuntimeState, createRouterServerSnapshot, getRouterHydrationScripts, getRouterMatchedRouteIds, getRouterRuntimeState, getRouterServerSnapshot, subscribeRouterRuntimeState };
|
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
import { createRouterPrefetchPolicy } from "./routerPrefetchPolicy.mjs";
|
|
2
|
-
import { applyRouterRuntimeState,
|
|
2
|
+
import { applyRouterRuntimeState, applyRouterServerPrepareResult } from "./routerState.mjs";
|
|
3
|
+
function createReactRouterNavigation(router, Link) {
|
|
4
|
+
let state;
|
|
5
|
+
let snapshot;
|
|
6
|
+
return {
|
|
7
|
+
Link,
|
|
8
|
+
getSnapshot () {
|
|
9
|
+
if (state !== router.state) {
|
|
10
|
+
state = router.state;
|
|
11
|
+
snapshot = {
|
|
12
|
+
location: state.location,
|
|
13
|
+
params: Object.assign({}, ...state.matches.map((match)=>match.params))
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return snapshot;
|
|
17
|
+
},
|
|
18
|
+
subscribe: (listener)=>router.subscribe(listener),
|
|
19
|
+
navigate: (href, options)=>router.navigate(href, options)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
3
22
|
function createRouterStatePlugin({ registryHooks }) {
|
|
4
23
|
return {
|
|
5
24
|
name: '@modern-js/router-runtime-policy',
|
|
@@ -14,7 +33,7 @@ function createRouterStatePlugin({ registryHooks }) {
|
|
|
14
33
|
if ('ssr-prepare' === event.phase) {
|
|
15
34
|
const { routerContext } = event.runtimeContext;
|
|
16
35
|
if (!routerContext) return;
|
|
17
|
-
const
|
|
36
|
+
const snapshot = {
|
|
18
37
|
framework: event.framework,
|
|
19
38
|
basename: event.basename,
|
|
20
39
|
statusCode: routerContext.statusCode,
|
|
@@ -28,19 +47,27 @@ function createRouterStatePlugin({ registryHooks }) {
|
|
|
28
47
|
routeId: match.route.id
|
|
29
48
|
}
|
|
30
49
|
] : [])
|
|
50
|
+
};
|
|
51
|
+
applyRouterServerPrepareResult(event.runtimeContext, {
|
|
52
|
+
snapshot,
|
|
53
|
+
state: {
|
|
54
|
+
framework: event.framework,
|
|
55
|
+
basename: event.basename,
|
|
56
|
+
instance: event.router
|
|
57
|
+
}
|
|
31
58
|
});
|
|
59
|
+
} else if ('client-create' === event.phase) {
|
|
60
|
+
const router = event.router;
|
|
61
|
+
const { router: api } = event.runtimeContext;
|
|
32
62
|
applyRouterRuntimeState(event.runtimeContext, {
|
|
33
63
|
framework: event.framework,
|
|
34
64
|
basename: event.basename,
|
|
35
|
-
instance:
|
|
36
|
-
|
|
37
|
-
|
|
65
|
+
instance: router,
|
|
66
|
+
...api?.Link ? {
|
|
67
|
+
navigation: createReactRouterNavigation(router, api.Link)
|
|
68
|
+
} : {}
|
|
38
69
|
});
|
|
39
|
-
}
|
|
40
|
-
framework: event.framework,
|
|
41
|
-
basename: event.basename,
|
|
42
|
-
instance: event.router
|
|
43
|
-
});
|
|
70
|
+
}
|
|
44
71
|
});
|
|
45
72
|
}
|
|
46
73
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
const resolveLocalisedUrlsConfig = (option)=>{
|
|
3
|
+
if (option && 'object' == typeof option && Object.keys(option).length > 0) return {
|
|
4
|
+
enabled: true,
|
|
5
|
+
map: option
|
|
6
|
+
};
|
|
7
|
+
return {
|
|
8
|
+
enabled: false,
|
|
9
|
+
map: {}
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export { resolveLocalisedUrlsConfig };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
export { resolveLocalisedUrlsConfig } from "./config.mjs";
|
|
3
|
+
export { normalisePathPattern, normalisePathname } from "./normalise.mjs";
|
|
4
|
+
export { canonicalTargetPathname, localiseTargetPathname, stripLanguagePrefix } from "./pathname.mjs";
|
|
5
|
+
export { buildPathFromPattern, matchPathPattern } from "./patterns.mjs";
|
|
6
|
+
export { DEFAULT_LOCALE_REDIRECT_SKIP_RULES, isDefaultLocaleRedirectSkipPath, matchesPathPrefix, shouldSkipLocaleRedirect } from "./redirect.mjs";
|
|
7
|
+
export { resolveCanonicalLocalisedPath, resolveLocalisedPath } from "./resolve.mjs";
|
|
8
|
+
export { createLocalisedRouteRewrite } from "./routeIdentity.mjs";
|
|
9
|
+
export { applyLocalisedUrlsToRoutes, validateLocalisedUrls } from "./routes.mjs";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
const LOCALE_PARAM_NAMES = new Set([
|
|
3
|
+
'lang',
|
|
4
|
+
'locale',
|
|
5
|
+
'language'
|
|
6
|
+
]);
|
|
7
|
+
const normaliseSlashes = (path)=>{
|
|
8
|
+
const withoutDuplicateSlashes = path.replace(/\/+/g, '/');
|
|
9
|
+
const withLeadingSlash = withoutDuplicateSlashes.startsWith('/') ? withoutDuplicateSlashes : `/${withoutDuplicateSlashes}`;
|
|
10
|
+
return withLeadingSlash.length > 1 ? withLeadingSlash.replace(/\/+$/, '') : withLeadingSlash;
|
|
11
|
+
};
|
|
12
|
+
const normalisePathPattern = (path)=>normaliseSlashes(path).replace(/\[(.+?)\]/g, ':$1');
|
|
13
|
+
const normalisePathname = (pathname)=>normaliseSlashes(pathname);
|
|
14
|
+
const normaliseRoutePath = (path)=>{
|
|
15
|
+
const normalized = normalisePathPattern(path);
|
|
16
|
+
return '/' === normalized ? '' : normalized.slice(1);
|
|
17
|
+
};
|
|
18
|
+
const getLocaleParamSegment = (segment)=>{
|
|
19
|
+
if (!segment.startsWith(':')) return null;
|
|
20
|
+
const paramName = segment.slice(1).replace(/\?$/, '');
|
|
21
|
+
return LOCALE_PARAM_NAMES.has(paramName) ? segment : null;
|
|
22
|
+
};
|
|
23
|
+
const splitPathSegments = (path)=>{
|
|
24
|
+
if (!path) return [];
|
|
25
|
+
return normalisePathPattern(path).split('/').filter(Boolean);
|
|
26
|
+
};
|
|
27
|
+
const stripLeadingLocaleParam = (path)=>{
|
|
28
|
+
const segments = splitPathSegments(path);
|
|
29
|
+
const leadingLocaleParam = getLocaleParamSegment(segments[0] || '');
|
|
30
|
+
if (!leadingLocaleParam) return path;
|
|
31
|
+
const remainingPath = segments.slice(1).join('/');
|
|
32
|
+
return remainingPath ? `/${remainingPath}` : void 0;
|
|
33
|
+
};
|
|
34
|
+
const getLeadingLocaleParam = (path)=>{
|
|
35
|
+
const segments = splitPathSegments(path);
|
|
36
|
+
return getLocaleParamSegment(segments[0] || '');
|
|
37
|
+
};
|
|
38
|
+
export { getLeadingLocaleParam, normalisePathPattern, normalisePathname, normaliseRoutePath, stripLeadingLocaleParam };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { resolveLocalisedUrlsConfig } from "./config.mjs";
|
|
3
|
+
import { resolveCanonicalLocalisedPath, resolveLocalisedPath } from "./resolve.mjs";
|
|
4
|
+
const stripLanguagePrefix = (pathname, languages)=>{
|
|
5
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
6
|
+
const firstSegment = segments[0]?.toLowerCase();
|
|
7
|
+
if (firstSegment && languages.some((language)=>language.toLowerCase() === firstSegment)) return `/${segments.slice(1).join('/')}`;
|
|
8
|
+
return pathname || '/';
|
|
9
|
+
};
|
|
10
|
+
const localiseTargetPathname = (pathname, language, languages, localisedUrls)=>{
|
|
11
|
+
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
12
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
13
|
+
const resolvedPath = localisedUrlsConfig.enabled ? resolveLocalisedPath(pathWithoutLanguage, language, languages, localisedUrlsConfig.map) : pathWithoutLanguage;
|
|
14
|
+
const resolvedSegments = resolvedPath.split('/').filter(Boolean);
|
|
15
|
+
return `/${[
|
|
16
|
+
language,
|
|
17
|
+
...resolvedSegments
|
|
18
|
+
].join('/')}`;
|
|
19
|
+
};
|
|
20
|
+
const canonicalTargetPathname = (pathname, languages, localisedUrls)=>{
|
|
21
|
+
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
22
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
23
|
+
return localisedUrlsConfig.enabled ? resolveCanonicalLocalisedPath(pathWithoutLanguage, languages, localisedUrlsConfig.map) : pathWithoutLanguage;
|
|
24
|
+
};
|
|
25
|
+
export { canonicalTargetPathname, localiseTargetPathname, stripLanguagePrefix };
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { normalisePathPattern, normalisePathname } from "./normalise.mjs";
|
|
3
|
+
const escapeRegExp = (value)=>value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
4
|
+
const getParamName = (segment)=>segment.slice(1).replace(/\?$/, '');
|
|
5
|
+
const getOrderedPathParamSignature = (pattern)=>normalisePathPattern(pattern).split('/').filter(Boolean).flatMap((segment)=>{
|
|
6
|
+
if ('*' === segment) return [
|
|
7
|
+
'*'
|
|
8
|
+
];
|
|
9
|
+
if (!segment.startsWith(':')) return [];
|
|
10
|
+
return [
|
|
11
|
+
`:${getParamName(segment)}${segment.endsWith('?') ? '?' : ''}`
|
|
12
|
+
];
|
|
13
|
+
});
|
|
14
|
+
const getPathParamSignature = (pattern)=>getOrderedPathParamSignature(pattern).sort((left, right)=>left < right ? -1 : left > right ? 1 : 0);
|
|
15
|
+
const getPathPatternValidationIssue = (pattern)=>{
|
|
16
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
17
|
+
const seenParams = new Set();
|
|
18
|
+
for (const [index, segment] of segments.entries()){
|
|
19
|
+
if ('*' === segment) {
|
|
20
|
+
if (index !== segments.length - 1) return 'splat parameter "*" must be the final segment';
|
|
21
|
+
if (seenParams.has('*')) return 'duplicate splat parameter "*"';
|
|
22
|
+
seenParams.add('*');
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (!segment.startsWith(':')) continue;
|
|
26
|
+
const paramName = getParamName(segment);
|
|
27
|
+
if (seenParams.has(paramName)) return `duplicate path parameter ":${paramName}"`;
|
|
28
|
+
seenParams.add(paramName);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const getPhysicalPathPattern = (pattern)=>{
|
|
32
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
33
|
+
const physicalSegments = segments.map((segment)=>{
|
|
34
|
+
if ('*' === segment) return '*';
|
|
35
|
+
if (segment.startsWith(':')) return segment.endsWith('?') ? ':param?' : ':param';
|
|
36
|
+
return segment;
|
|
37
|
+
});
|
|
38
|
+
return `/${physicalSegments.join('/')}`;
|
|
39
|
+
};
|
|
40
|
+
const compiledPathPatternCache = new Map();
|
|
41
|
+
const compilePathPattern = (pattern)=>{
|
|
42
|
+
const normalizedPattern = normalisePathPattern(pattern);
|
|
43
|
+
const cached = compiledPathPatternCache.get(normalizedPattern);
|
|
44
|
+
if (cached) return cached;
|
|
45
|
+
const names = [];
|
|
46
|
+
const segments = normalizedPattern.split('/').filter(Boolean);
|
|
47
|
+
const source = segments.map((segment)=>{
|
|
48
|
+
if (segment.startsWith(':')) {
|
|
49
|
+
names.push(getParamName(segment));
|
|
50
|
+
const paramPattern = '([^/]+)';
|
|
51
|
+
return segment.endsWith('?') ? `(?:/${paramPattern})?` : `/${paramPattern}`;
|
|
52
|
+
}
|
|
53
|
+
if ('*' === segment) {
|
|
54
|
+
names.push('*');
|
|
55
|
+
return '(?:/(.*))?';
|
|
56
|
+
}
|
|
57
|
+
return `/${escapeRegExp(segment)}`;
|
|
58
|
+
}).join('');
|
|
59
|
+
const compiled = {
|
|
60
|
+
names,
|
|
61
|
+
regexp: new RegExp(`^${source || '/'}$`)
|
|
62
|
+
};
|
|
63
|
+
compiledPathPatternCache.set(normalizedPattern, compiled);
|
|
64
|
+
return compiled;
|
|
65
|
+
};
|
|
66
|
+
const getPatternSpecificity = (pattern)=>{
|
|
67
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
68
|
+
let staticSegments = 0;
|
|
69
|
+
let dynamicSegments = 0;
|
|
70
|
+
let optionalSegments = 0;
|
|
71
|
+
let splatSegments = 0;
|
|
72
|
+
for (const segment of segments)if ('*' === segment) splatSegments++;
|
|
73
|
+
else if (segment.startsWith(':')) {
|
|
74
|
+
dynamicSegments++;
|
|
75
|
+
if (segment.endsWith('?')) optionalSegments++;
|
|
76
|
+
} else staticSegments++;
|
|
77
|
+
return {
|
|
78
|
+
staticSegments,
|
|
79
|
+
dynamicSegments,
|
|
80
|
+
optionalSegments,
|
|
81
|
+
splatSegments,
|
|
82
|
+
totalSegments: segments.length
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
const comparePatternSpecificity = (left, right)=>{
|
|
86
|
+
const a = getPatternSpecificity(left);
|
|
87
|
+
const b = getPatternSpecificity(right);
|
|
88
|
+
return b.staticSegments - a.staticSegments || a.splatSegments - b.splatSegments || a.optionalSegments - b.optionalSegments || b.totalSegments - a.totalSegments || a.dynamicSegments - b.dynamicSegments;
|
|
89
|
+
};
|
|
90
|
+
const haveEqualPatternSpecificity = (left, right)=>0 === comparePatternSpecificity(left, right);
|
|
91
|
+
const doPathPatternsOverlap = (left, right)=>{
|
|
92
|
+
const leftSegments = normalisePathPattern(left).split('/').filter(Boolean);
|
|
93
|
+
const rightSegments = normalisePathPattern(right).split('/').filter(Boolean);
|
|
94
|
+
if (leftSegments.length !== rightSegments.length) return false;
|
|
95
|
+
return leftSegments.every((leftSegment, index)=>{
|
|
96
|
+
const rightSegment = rightSegments[index];
|
|
97
|
+
const leftIsDynamic = '*' === leftSegment || leftSegment.startsWith(':');
|
|
98
|
+
const rightIsDynamic = '*' === rightSegment || rightSegment.startsWith(':');
|
|
99
|
+
return leftIsDynamic || rightIsDynamic || leftSegment === rightSegment;
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
const comparePatternNames = (left, right)=>{
|
|
103
|
+
const normalizedLeft = normalisePathPattern(left);
|
|
104
|
+
const normalizedRight = normalisePathPattern(right);
|
|
105
|
+
return normalizedLeft < normalizedRight ? -1 : normalizedLeft > normalizedRight ? 1 : 0;
|
|
106
|
+
};
|
|
107
|
+
const sortPatternsBySpecificity = (patterns)=>patterns.map((pattern, index)=>({
|
|
108
|
+
pattern,
|
|
109
|
+
index
|
|
110
|
+
})).sort((left, right)=>comparePatternSpecificity(left.pattern.pattern, right.pattern.pattern) || comparePatternNames(left.pattern.pattern, right.pattern.pattern) || left.index - right.index).map(({ pattern })=>pattern);
|
|
111
|
+
const decodePathParam = (value)=>{
|
|
112
|
+
try {
|
|
113
|
+
return decodeURIComponent(value);
|
|
114
|
+
} catch {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const encodeSplatParam = (value)=>value.split('/').map(encodeURIComponent).join('/');
|
|
119
|
+
const matchPathPattern = (pathname, pattern)=>{
|
|
120
|
+
const { names, regexp } = compilePathPattern(pattern);
|
|
121
|
+
const match = regexp.exec(normalisePathname(pathname));
|
|
122
|
+
if (!match) return null;
|
|
123
|
+
const params = {};
|
|
124
|
+
for(let index = 0; index < names.length; index++){
|
|
125
|
+
const decoded = decodePathParam(match[index + 1] || '');
|
|
126
|
+
if (null === decoded) return null;
|
|
127
|
+
Object.defineProperty(params, names[index], {
|
|
128
|
+
configurable: true,
|
|
129
|
+
enumerable: true,
|
|
130
|
+
value: decoded,
|
|
131
|
+
writable: true
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return params;
|
|
135
|
+
};
|
|
136
|
+
const buildPathFromPattern = (pattern, params)=>{
|
|
137
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
138
|
+
const path = segments.map((segment)=>{
|
|
139
|
+
if (segment.startsWith(':')) {
|
|
140
|
+
const paramName = getParamName(segment);
|
|
141
|
+
const param = Object.hasOwn(params, paramName) ? params[paramName] : void 0;
|
|
142
|
+
if (void 0 === param || '' === param) {
|
|
143
|
+
if (segment.endsWith('?')) return '';
|
|
144
|
+
throw new Error(`Missing required path parameter "${paramName}" for pattern "${normalisePathPattern(pattern)}".`);
|
|
145
|
+
}
|
|
146
|
+
return encodeURIComponent(param);
|
|
147
|
+
}
|
|
148
|
+
if ('*' === segment) return encodeSplatParam(params['*'] || '');
|
|
149
|
+
return segment;
|
|
150
|
+
}).filter(Boolean).join('/');
|
|
151
|
+
return `/${path}`;
|
|
152
|
+
};
|
|
153
|
+
export { buildPathFromPattern, doPathPatternsOverlap, getOrderedPathParamSignature, getPathParamSignature, getPathPatternValidationIssue, getPhysicalPathPattern, haveEqualPatternSpecificity, matchPathPattern, sortPatternsBySpecificity };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { normalisePathname } from "./normalise.mjs";
|
|
3
|
+
import { stripLanguagePrefix } from "./pathname.mjs";
|
|
4
|
+
const DEFAULT_LOCALE_REDIRECT_SKIP_RULES = [
|
|
5
|
+
{
|
|
6
|
+
type: 'exact',
|
|
7
|
+
path: '/backend-mf-manifest.json'
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
type: 'exact',
|
|
11
|
+
path: '/mf-manifest.json'
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
type: 'exact',
|
|
15
|
+
path: '/mf-stats.json'
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
type: 'pattern',
|
|
19
|
+
path: /^\/(?:backendRemoteEntry(?:\.[a-zA-Z0-9_-]+)?\.cjs|remoteEntry(?:\.[a-zA-Z0-9_-]+)?\.js)$/
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
type: 'prefix',
|
|
23
|
+
path: '/static/'
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
type: 'prefix',
|
|
27
|
+
path: '/upload/'
|
|
28
|
+
}
|
|
29
|
+
];
|
|
30
|
+
const stripTrailingSlash = (pathname)=>pathname.length > 1 ? pathname.replace(/\/+$/, '') : pathname;
|
|
31
|
+
const matchesPathPrefix = (pathname, prefix)=>{
|
|
32
|
+
const normalizedPathname = normalisePathname(pathname);
|
|
33
|
+
const normalizedPrefix = stripTrailingSlash(normalisePathname(prefix));
|
|
34
|
+
return normalizedPathname === normalizedPrefix || normalizedPathname.startsWith(`${normalizedPrefix}/`);
|
|
35
|
+
};
|
|
36
|
+
const matchesSkipRule = (pathname, rule)=>{
|
|
37
|
+
if ('exact' === rule.type) return normalisePathname(pathname) === normalisePathname(rule.path);
|
|
38
|
+
if ('pattern' === rule.type) return rule.path.test(normalisePathname(pathname));
|
|
39
|
+
return matchesPathPrefix(pathname, rule.path);
|
|
40
|
+
};
|
|
41
|
+
const isDefaultLocaleRedirectSkipPath = (pathname, languages = [])=>{
|
|
42
|
+
const normalizedPathname = normalisePathname(pathname);
|
|
43
|
+
const pathWithoutLanguage = stripLanguagePrefix(normalizedPathname, languages);
|
|
44
|
+
return DEFAULT_LOCALE_REDIRECT_SKIP_RULES.some((rule)=>matchesSkipRule(pathWithoutLanguage, rule));
|
|
45
|
+
};
|
|
46
|
+
const shouldSkipLocaleRedirect = (pathname, languages = [], ignoreRedirectRoutes)=>{
|
|
47
|
+
const normalizedPathname = normalisePathname(pathname);
|
|
48
|
+
const pathWithoutLanguage = stripLanguagePrefix(normalizedPathname, languages);
|
|
49
|
+
if (isDefaultLocaleRedirectSkipPath(pathWithoutLanguage)) return true;
|
|
50
|
+
if (!ignoreRedirectRoutes) return false;
|
|
51
|
+
if ('function' == typeof ignoreRedirectRoutes) return ignoreRedirectRoutes(pathWithoutLanguage);
|
|
52
|
+
return ignoreRedirectRoutes.some((pattern)=>matchesPathPrefix(pathWithoutLanguage, pattern));
|
|
53
|
+
};
|
|
54
|
+
export { DEFAULT_LOCALE_REDIRECT_SKIP_RULES, isDefaultLocaleRedirectSkipPath, matchesPathPrefix, shouldSkipLocaleRedirect };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { normalisePathname } from "./normalise.mjs";
|
|
3
|
+
import { buildPathFromPattern, matchPathPattern, sortPatternsBySpecificity } from "./patterns.mjs";
|
|
4
|
+
const getCanonicalCandidates = (localisedUrls)=>sortPatternsBySpecificity(Object.entries(localisedUrls).map(([canonicalPattern, localisedUrlEntry])=>({
|
|
5
|
+
pattern: canonicalPattern,
|
|
6
|
+
canonicalPattern,
|
|
7
|
+
localisedUrlEntry
|
|
8
|
+
})));
|
|
9
|
+
const resolveLocalisedUrlPath = (pathname, languages, localisedUrls, options)=>{
|
|
10
|
+
const normalizedPathname = normalisePathname(pathname);
|
|
11
|
+
const canonicalCandidates = getCanonicalCandidates(localisedUrls);
|
|
12
|
+
const candidates = sortPatternsBySpecificity(canonicalCandidates.flatMap(({ canonicalPattern, localisedUrlEntry })=>{
|
|
13
|
+
const targetPattern = 'localised' === options.mode ? localisedUrlEntry[options.targetLanguage] : canonicalPattern;
|
|
14
|
+
if (!targetPattern) return [];
|
|
15
|
+
const sourcePatterns = new Set([
|
|
16
|
+
canonicalPattern,
|
|
17
|
+
...languages.map((language)=>localisedUrlEntry[language]).filter((sourcePattern)=>Boolean(sourcePattern))
|
|
18
|
+
]);
|
|
19
|
+
return Array.from(sourcePatterns, (sourcePattern)=>({
|
|
20
|
+
pattern: sourcePattern,
|
|
21
|
+
sourcePattern,
|
|
22
|
+
targetPattern
|
|
23
|
+
}));
|
|
24
|
+
}));
|
|
25
|
+
for (const { sourcePattern, targetPattern } of candidates){
|
|
26
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
27
|
+
if (params) return buildPathFromPattern(targetPattern, params);
|
|
28
|
+
}
|
|
29
|
+
return normalizedPathname;
|
|
30
|
+
};
|
|
31
|
+
const resolveLocalisedPath = (pathname, targetLanguage, languages, localisedUrls)=>resolveLocalisedUrlPath(pathname, languages, localisedUrls, {
|
|
32
|
+
mode: 'localised',
|
|
33
|
+
targetLanguage
|
|
34
|
+
});
|
|
35
|
+
const resolveCanonicalLocalisedPath = (pathname, languages, localisedUrls)=>resolveLocalisedUrlPath(pathname, languages, localisedUrls, {
|
|
36
|
+
mode: 'canonical'
|
|
37
|
+
});
|
|
38
|
+
export { resolveCanonicalLocalisedPath, resolveLocalisedPath };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { normalisePathPattern } from "./normalise.mjs";
|
|
3
|
+
import { buildPathFromPattern, matchPathPattern } from "./patterns.mjs";
|
|
4
|
+
function rewritePath(url, mappings) {
|
|
5
|
+
for (const { from, to } of mappings){
|
|
6
|
+
const params = matchPathPattern(url.pathname, from);
|
|
7
|
+
if (params) {
|
|
8
|
+
url.pathname = buildPathFromPattern(to, params);
|
|
9
|
+
break;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return url;
|
|
13
|
+
}
|
|
14
|
+
function createLocalisedRouteRewrite(routes) {
|
|
15
|
+
const input = [];
|
|
16
|
+
const output = [];
|
|
17
|
+
const visit = (nodes, parent)=>{
|
|
18
|
+
for (const route of nodes){
|
|
19
|
+
const fullPath = normalisePathPattern(`${parent}/${route.path ?? ''}`);
|
|
20
|
+
const identity = route.modernLocalisedRoute;
|
|
21
|
+
if (identity?.paths && fullPath.endsWith(identity.canonicalPath)) {
|
|
22
|
+
const prefix = fullPath.slice(0, -identity.canonicalPath.length);
|
|
23
|
+
const hasLocale = prefix.split('/').some((segment)=>/^:(?:lang|locale|language)\??$/.test(segment));
|
|
24
|
+
for (const [language, localisedPath] of Object.entries(identity.paths)){
|
|
25
|
+
const localePrefix = prefix.split('/').map((segment)=>/^:(?:lang|locale|language)\??$/.test(segment) ? language : segment).join('/');
|
|
26
|
+
const canonical = normalisePathPattern(`${localePrefix}/${identity.canonicalPath}`);
|
|
27
|
+
const localised = normalisePathPattern(`${localePrefix}/${localisedPath}`);
|
|
28
|
+
if (canonical !== localised) {
|
|
29
|
+
input.push({
|
|
30
|
+
from: localised,
|
|
31
|
+
to: canonical
|
|
32
|
+
});
|
|
33
|
+
if (hasLocale) output.push({
|
|
34
|
+
from: canonical,
|
|
35
|
+
to: localised
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (route.children) visit(route.children, fullPath);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
visit(routes, '');
|
|
44
|
+
if (0 === input.length) return;
|
|
45
|
+
const specificity = (pattern)=>pattern.split('/').filter((segment)=>segment && !segment.startsWith(':') && '*' !== segment).length;
|
|
46
|
+
for (const mappings of [
|
|
47
|
+
input,
|
|
48
|
+
output
|
|
49
|
+
])mappings.sort((a, b)=>specificity(b.from) - specificity(a.from));
|
|
50
|
+
return {
|
|
51
|
+
input: ({ url })=>rewritePath(url, input),
|
|
52
|
+
output: ({ url })=>rewritePath(url, output)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export { createLocalisedRouteRewrite };
|