@bleedingdev/modern-js-runtime-extensions 3.9.0-ultramodern.4 → 3.9.0-ultramodern.5
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/contextExtensions.js +74 -0
- package/dist/cjs/headRuntime.js +91 -0
- package/dist/cjs/helmetContext.js +54 -0
- package/dist/cjs/index.js +18 -4
- package/dist/cjs/prefetchPolicy.js +52 -0
- package/dist/cjs/request-policy/browser.js +139 -0
- package/dist/cjs/request-policy/client.js +234 -0
- package/dist/cjs/request-policy/node.js +149 -0
- package/dist/cjs/request-policy/policyCore.js +266 -0
- package/dist/cjs/request-policy/requestContext.js +137 -0
- package/dist/cjs/request-policy/traceparent.js +56 -0
- package/dist/cjs/request-policy/transport.js +234 -0
- package/dist/cjs/request-policy/types.js +57 -0
- package/dist/cjs/routeHydration.js +127 -0
- package/dist/cjs/routerPrefetchPolicy.js +153 -0
- package/dist/cjs/routerProvider.js +170 -0
- package/dist/cjs/routerState.js +167 -0
- package/dist/cjs/routerStatePlugin.js +86 -0
- package/dist/cjs/routerStateTypes.js +18 -0
- package/dist/cjs/runtimeContextProjection.js +112 -0
- package/dist/cjs/server-config/bffRuntime.js +18 -0
- package/dist/cjs/server-config/index.js +18 -0
- package/dist/cjs/server-config/serverTelemetry.js +18 -0
- package/dist/cjs/shellStream.js +34 -2
- package/dist/cjs/workspaceEvents.js +159 -0
- package/dist/esm/contextExtensions.mjs +33 -0
- package/dist/esm/headRuntime.mjs +43 -0
- package/dist/esm/helmetContext.mjs +13 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/prefetchPolicy.mjs +11 -0
- package/dist/esm/request-policy/browser.mjs +11 -0
- package/dist/esm/request-policy/client.mjs +195 -0
- package/dist/esm/request-policy/policyCore.mjs +171 -0
- package/dist/esm/request-policy/requestContext.mjs +75 -0
- package/dist/esm/request-policy/traceparent.mjs +18 -0
- package/dist/esm/request-policy/transport.mjs +196 -0
- package/dist/esm/request-policy/types.mjs +10 -0
- package/dist/esm/routeHydration.mjs +77 -0
- package/dist/esm/routerPrefetchPolicy.mjs +115 -0
- package/dist/esm/routerProvider.mjs +117 -0
- package/dist/esm/routerState.mjs +105 -0
- package/dist/esm/routerStatePlugin.mjs +48 -0
- package/dist/esm/routerStateTypes.mjs +0 -0
- package/dist/esm/runtimeContextProjection.mjs +74 -0
- package/dist/esm/server-config/bffRuntime.mjs +0 -0
- package/dist/esm/server-config/index.mjs +0 -0
- package/dist/esm/server-config/serverTelemetry.mjs +0 -0
- package/dist/esm/shellStream.mjs +34 -2
- package/dist/esm/workspaceEvents.mjs +66 -0
- package/dist/esm-node/contextExtensions.mjs +34 -0
- package/dist/esm-node/headRuntime.mjs +44 -0
- package/dist/esm-node/helmetContext.mjs +14 -0
- package/dist/esm-node/index.mjs +2 -0
- package/dist/esm-node/prefetchPolicy.mjs +12 -0
- package/dist/esm-node/request-policy/browser.mjs +12 -0
- package/dist/esm-node/request-policy/client.mjs +196 -0
- package/dist/esm-node/request-policy/node.mjs +22 -0
- package/dist/esm-node/request-policy/policyCore.mjs +172 -0
- package/dist/esm-node/request-policy/requestContext.mjs +76 -0
- package/dist/esm-node/request-policy/traceparent.mjs +19 -0
- package/dist/esm-node/request-policy/transport.mjs +197 -0
- package/dist/esm-node/request-policy/types.mjs +11 -0
- package/dist/esm-node/routeHydration.mjs +78 -0
- package/dist/esm-node/routerPrefetchPolicy.mjs +116 -0
- package/dist/esm-node/routerProvider.mjs +118 -0
- package/dist/esm-node/routerState.mjs +106 -0
- package/dist/esm-node/routerStatePlugin.mjs +49 -0
- package/dist/esm-node/routerStateTypes.mjs +1 -0
- package/dist/esm-node/runtimeContextProjection.mjs +75 -0
- package/dist/esm-node/server-config/bffRuntime.mjs +1 -0
- package/dist/esm-node/server-config/index.mjs +1 -0
- package/dist/esm-node/server-config/serverTelemetry.mjs +1 -0
- package/dist/esm-node/shellStream.mjs +34 -2
- package/dist/esm-node/workspaceEvents.mjs +67 -0
- package/dist/types/contextExtensions.d.ts +34 -0
- package/dist/types/headRuntime.d.ts +12 -0
- package/dist/types/helmetContext.d.ts +10 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/prefetchPolicy.d.ts +4 -0
- package/dist/types/request-policy/browser.d.ts +10 -0
- package/dist/types/request-policy/client.d.ts +14 -0
- package/dist/types/request-policy/node.d.ts +10 -0
- package/dist/types/request-policy/policyCore.d.ts +107 -0
- package/dist/types/request-policy/requestContext.d.ts +22 -0
- package/dist/types/request-policy/traceparent.d.ts +10 -0
- package/dist/types/request-policy/transport.d.ts +12 -0
- package/dist/types/request-policy/types.d.ts +144 -0
- package/dist/types/routeHydration.d.ts +24 -0
- package/dist/types/routerPrefetchPolicy.d.ts +28 -0
- package/dist/types/routerProvider.d.ts +77 -0
- package/dist/types/routerState.d.ts +12 -0
- package/dist/types/routerStatePlugin.d.ts +19 -0
- package/dist/types/routerStateTypes.d.ts +40 -0
- package/dist/types/runtimeContextProjection.d.ts +30 -0
- package/dist/types/server-config/bffRuntime.d.ts +122 -0
- package/dist/types/server-config/index.d.ts +2 -0
- package/dist/types/server-config/serverTelemetry.d.ts +322 -0
- package/dist/types/workspaceEvents.d.ts +62 -0
- package/package.json +198 -13
- package/rslib.config.mts +17 -2
- package/rstest.config.mts +33 -2
- package/src/contextExtensions.ts +81 -0
- package/src/headRuntime.tsx +77 -0
- package/src/helmetContext.ts +31 -0
- package/src/index.ts +2 -0
- package/src/prefetchPolicy.ts +33 -0
- package/src/request-policy/browser.ts +22 -0
- package/src/request-policy/client.ts +404 -0
- package/src/request-policy/node.ts +41 -0
- package/src/request-policy/policyCore.ts +424 -0
- package/src/request-policy/requestContext.ts +149 -0
- package/src/request-policy/traceparent.ts +43 -0
- package/src/request-policy/transport.ts +349 -0
- package/src/request-policy/types.ts +229 -0
- package/src/routeHydration.ts +208 -0
- package/src/routerPrefetchPolicy.ts +195 -0
- package/src/routerProvider.ts +340 -0
- package/src/routerState.ts +208 -0
- package/src/routerStatePlugin.ts +86 -0
- package/src/routerStateTypes.ts +46 -0
- package/src/runtimeContextProjection.ts +105 -0
- package/src/server-config/bffRuntime.ts +136 -0
- package/src/server-config/index.ts +2 -0
- package/src/server-config/serverTelemetry.ts +337 -0
- package/src/shellStream.ts +55 -25
- package/src/workspaceEvents.ts +324 -0
- package/tests/buildIdentity.test.ts +2 -4
- package/tests/contextExtensions.test.ts +71 -0
- package/tests/headRuntime.test.tsx +369 -0
- package/tests/nativeSafeFailureCompatibility.test.ts +117 -0
- package/tests/prefetchPolicy.test.ts +51 -0
- package/tests/rendererHead.test.ts +1 -72
- package/tests/request-policy/browser-policy.test.ts +608 -0
- package/tests/request-policy/client-isolation.test.ts +149 -0
- package/tests/request-policy/identityBinding.test.ts +75 -0
- package/tests/request-policy/node-policy.test.ts +741 -0
- package/tests/request-policy/policyCore.test.ts +250 -0
- package/tests/request-policy/requestContext.test.ts +80 -0
- package/tests/request-policy/requestFactory.outbound-contract.test.ts +691 -0
- package/tests/request-policy/traceparent.test.ts +55 -0
- package/tests/request-policy/transport-retry.test.ts +370 -0
- package/tests/routeHydration.test.ts +180 -0
- package/tests/routerPrefetchPolicy.test.ts +102 -0
- package/tests/routerProvider.test.ts +38 -0
- package/tests/routerState.test.ts +157 -0
- package/tests/routerStatePlugin.test.ts +108 -0
- package/tests/runtimeContextProjection.test.ts +135 -0
- package/tests/shellStream.test.ts +37 -0
- package/tests/workspaceEvents.test.ts +391 -0
- package/dist/cjs/rscHtmlStream.js +0 -128
- package/dist/esm/rscHtmlStream.mjs +0 -86
- package/dist/esm-node/rscHtmlStream.mjs +0 -87
- package/dist/types/rscHtmlStream.d.ts +0 -4
- package/src/rscHtmlStream.ts +0 -142
- package/tests/rscHtmlStream.test.ts +0 -153
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { escapeHtmlAttribute } from "./html.mjs";
|
|
2
|
+
function getScriptTags(template) {
|
|
3
|
+
const scriptRegExp = /<script\b[^>]*\bsrc=(["'])(.*?)\1[^>]*><\/script>/g;
|
|
4
|
+
return Array.from(template.matchAll(scriptRegExp)).map((match)=>({
|
|
5
|
+
index: match.index ?? 0,
|
|
6
|
+
tag: match[0],
|
|
7
|
+
src: match[2]
|
|
8
|
+
}));
|
|
9
|
+
}
|
|
10
|
+
function getAssetBasename(src) {
|
|
11
|
+
const withoutQuery = src.split(/[?#]/)[0];
|
|
12
|
+
return withoutQuery.split('/').pop() || withoutQuery;
|
|
13
|
+
}
|
|
14
|
+
function isEntryScript(src, entryName, asyncEntry) {
|
|
15
|
+
const basename = getAssetBasename(src);
|
|
16
|
+
const prefix = asyncEntry ? `async-${entryName}` : entryName;
|
|
17
|
+
return basename === `${prefix}.js` || basename.startsWith(`${prefix}.`) || basename.startsWith(`${prefix}-`);
|
|
18
|
+
}
|
|
19
|
+
const dedupeByUrl = (chunks)=>{
|
|
20
|
+
const seen = new Set();
|
|
21
|
+
return chunks.filter((chunk)=>{
|
|
22
|
+
if (void 0 === chunk.url || '' === chunk.url || seen.has(chunk.url)) return false;
|
|
23
|
+
seen.add(chunk.url);
|
|
24
|
+
return true;
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
const isAsyncEntryScriptChunk = (chunk, entryName)=>{
|
|
28
|
+
if (void 0 === chunk.url || !chunk.url.endsWith('.js')) return false;
|
|
29
|
+
const asyncEntryName = `async-${entryName}`;
|
|
30
|
+
const filename = chunk.filename ?? chunk.url;
|
|
31
|
+
const basename = filename.split('/').pop() ?? filename;
|
|
32
|
+
return basename === `${asyncEntryName}.js` || basename.startsWith(`${asyncEntryName}.`) || basename.startsWith(`${asyncEntryName}-`);
|
|
33
|
+
};
|
|
34
|
+
function getMatchedRouteChunks(routeManifest, matchedRouteIds, routeAssetToChunk) {
|
|
35
|
+
const routeAssets = routeManifest?.routeAssets;
|
|
36
|
+
if (void 0 === routeAssets) return [];
|
|
37
|
+
return matchedRouteIds.flatMap((routeId)=>routeAssets[routeId]?.assets ?? []).map(routeAssetToChunk);
|
|
38
|
+
}
|
|
39
|
+
const orderHydrationScriptChunks = ({ asyncEntryChunks, collectedChunks, matchedRouteChunks, entryName })=>{
|
|
40
|
+
const asyncEntryScriptChunks = [];
|
|
41
|
+
const asyncEntryDependencyChunks = [];
|
|
42
|
+
for (const chunk of asyncEntryChunks)if (isAsyncEntryScriptChunk(chunk, entryName)) asyncEntryScriptChunks.push(chunk);
|
|
43
|
+
else asyncEntryDependencyChunks.push(chunk);
|
|
44
|
+
return dedupeByUrl([
|
|
45
|
+
...asyncEntryDependencyChunks,
|
|
46
|
+
...collectedChunks,
|
|
47
|
+
...matchedRouteChunks,
|
|
48
|
+
...asyncEntryScriptChunks
|
|
49
|
+
]);
|
|
50
|
+
};
|
|
51
|
+
function injectBeforeHydrationEntryScript(template, scripts, entryName = 'index') {
|
|
52
|
+
if ('' === scripts) return template;
|
|
53
|
+
const scriptTags = getScriptTags(template);
|
|
54
|
+
const target = scriptTags.find((match)=>isEntryScript(match.src, entryName, false)) ?? scriptTags.find((match)=>isEntryScript(match.src, entryName, true));
|
|
55
|
+
if (void 0 === target) return template;
|
|
56
|
+
return `${template.slice(0, target.index)}${scripts}${template.slice(target.index)}`;
|
|
57
|
+
}
|
|
58
|
+
function replaceChunkJsPlaceholder(template, scripts, entryName, placeholder) {
|
|
59
|
+
if (!template.includes(placeholder)) return template;
|
|
60
|
+
if ('' === scripts) return template.replace(placeholder, ()=>'');
|
|
61
|
+
const withoutPlaceholder = template.replace(placeholder, ()=>'');
|
|
62
|
+
const withEarlyScripts = injectBeforeHydrationEntryScript(withoutPlaceholder, scripts, entryName);
|
|
63
|
+
if (withEarlyScripts !== withoutPlaceholder) return withEarlyScripts;
|
|
64
|
+
return template.replace(placeholder, ()=>scripts);
|
|
65
|
+
}
|
|
66
|
+
function createRouteHydrationScriptTags(routeManifest, matchedRouteIds, entryName, options = {}) {
|
|
67
|
+
const { nonce, template } = options;
|
|
68
|
+
const routeAssets = routeManifest?.routeAssets;
|
|
69
|
+
if (void 0 === routeAssets) return '';
|
|
70
|
+
const existingScriptSrcs = void 0 === template ? new Set() : new Set(getScriptTags(template).map(({ src })=>src));
|
|
71
|
+
const jsAssets = Array.from(new Set([
|
|
72
|
+
...getMatchedRouteChunks(routeManifest, matchedRouteIds, (asset)=>asset),
|
|
73
|
+
...routeAssets[`async-${entryName}`]?.assets ?? []
|
|
74
|
+
].filter((asset)=>asset.endsWith('.js'))));
|
|
75
|
+
return jsAssets.filter((asset)=>!existingScriptSrcs.has(asset)).map((asset)=>`<script src="${escapeHtmlAttribute(asset)}"${nonce ? ` nonce="${escapeHtmlAttribute(nonce)}"` : ''}></script>`).join(' ');
|
|
76
|
+
}
|
|
77
|
+
export { createRouteHydrationScriptTags, getMatchedRouteChunks, injectBeforeHydrationEntryScript, orderHydrationScriptChunks, replaceChunkJsPlaceholder };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { getNavigationWarmupCacheKey } from "./navigationWarmup.mjs";
|
|
2
|
+
import { normalizePreloadBehavior } from "./prefetchPolicy.mjs";
|
|
3
|
+
const INTENT_DELAY = 100;
|
|
4
|
+
const VIEWPORT_ROOT_MARGIN = '200px';
|
|
5
|
+
const MAX_CONCURRENT_WARMUPS = 4;
|
|
6
|
+
const WARMUP_TTL = 30000;
|
|
7
|
+
function createRouterPrefetchPolicy() {
|
|
8
|
+
const cache = new Map();
|
|
9
|
+
const queue = [];
|
|
10
|
+
let activeWarmups = 0;
|
|
11
|
+
const canWarmup = ()=>{
|
|
12
|
+
const navigator = globalThis.navigator;
|
|
13
|
+
const connection = navigator?.connection || navigator?.mozConnection || navigator?.webkitConnection;
|
|
14
|
+
return !connection?.saveData && connection?.effectiveType !== 'slow-2g' && connection?.effectiveType !== '2g';
|
|
15
|
+
};
|
|
16
|
+
const runNext = ()=>{
|
|
17
|
+
while(activeWarmups < MAX_CONCURRENT_WARMUPS && queue.length > 0){
|
|
18
|
+
const task = queue.shift();
|
|
19
|
+
if (task.cancelled) continue;
|
|
20
|
+
activeWarmups += 1;
|
|
21
|
+
task.run().catch(()=>{
|
|
22
|
+
cache.delete(task.key);
|
|
23
|
+
}).finally(()=>{
|
|
24
|
+
activeWarmups -= 1;
|
|
25
|
+
runNext();
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
canWarmup,
|
|
31
|
+
allowData (route) {
|
|
32
|
+
return route?.handle?.navigationWarmup?.data !== false;
|
|
33
|
+
},
|
|
34
|
+
observe ({ element, prefetch: inputPrefetch, preload: inputPreload, notify }) {
|
|
35
|
+
const prefetch = inputPrefetch ?? 'render';
|
|
36
|
+
const preload = normalizePreloadBehavior(inputPreload, prefetch);
|
|
37
|
+
let state = {
|
|
38
|
+
code: 'render' === prefetch || 'render' === preload,
|
|
39
|
+
data: 'render' === prefetch
|
|
40
|
+
};
|
|
41
|
+
let timer;
|
|
42
|
+
let observer;
|
|
43
|
+
const update = (next)=>{
|
|
44
|
+
state = next;
|
|
45
|
+
notify(state);
|
|
46
|
+
};
|
|
47
|
+
const cancelTimer = ()=>{
|
|
48
|
+
if (void 0 !== timer) {
|
|
49
|
+
clearTimeout(timer);
|
|
50
|
+
timer = void 0;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
notify(state);
|
|
54
|
+
if (element && ('viewport' === prefetch || 'viewport' === preload) && "u" > typeof IntersectionObserver) {
|
|
55
|
+
observer = new IntersectionObserver((entries)=>{
|
|
56
|
+
if (!entries.some((entry)=>entry.isIntersecting)) return;
|
|
57
|
+
update({
|
|
58
|
+
code: true,
|
|
59
|
+
data: state.data || 'viewport' === prefetch
|
|
60
|
+
});
|
|
61
|
+
observer?.disconnect();
|
|
62
|
+
}, {
|
|
63
|
+
rootMargin: VIEWPORT_ROOT_MARGIN
|
|
64
|
+
});
|
|
65
|
+
observer.observe(element);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
onIntent () {
|
|
69
|
+
if ('intent' !== prefetch && 'intent' !== preload) return;
|
|
70
|
+
if (void 0 !== timer) return;
|
|
71
|
+
timer = setTimeout(()=>{
|
|
72
|
+
timer = void 0;
|
|
73
|
+
update({
|
|
74
|
+
code: true,
|
|
75
|
+
data: state.data || 'intent' === prefetch
|
|
76
|
+
});
|
|
77
|
+
}, INTENT_DELAY);
|
|
78
|
+
},
|
|
79
|
+
onCancel () {
|
|
80
|
+
if ('intent' === prefetch || 'intent' === preload) {
|
|
81
|
+
cancelTimer();
|
|
82
|
+
update({
|
|
83
|
+
code: false,
|
|
84
|
+
data: false
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
dispose () {
|
|
89
|
+
cancelTimer();
|
|
90
|
+
observer?.disconnect();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
schedule ({ runtimeContext, chunkLoader, publicPath, key: operationKey, run }) {
|
|
95
|
+
if (!canWarmup()) return ()=>{};
|
|
96
|
+
const now = performance.now();
|
|
97
|
+
for (const [key, timestamp] of cache)if (now - timestamp > WARMUP_TTL) cache.delete(key);
|
|
98
|
+
const key = getNavigationWarmupCacheKey(runtimeContext, chunkLoader, publicPath, operationKey);
|
|
99
|
+
if (cache.has(key)) return ()=>{};
|
|
100
|
+
cache.set(key, now);
|
|
101
|
+
const task = {
|
|
102
|
+
key,
|
|
103
|
+
run,
|
|
104
|
+
cancelled: false
|
|
105
|
+
};
|
|
106
|
+
queue.push(task);
|
|
107
|
+
runNext();
|
|
108
|
+
return ()=>{
|
|
109
|
+
task.cancelled = true;
|
|
110
|
+
if (queue.includes(task)) cache.delete(key);
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
export { createRouterPrefetchPolicy };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { merge } from "@modern-js/runtime-utils/merge";
|
|
2
|
+
function reportUnsupportedProviderRegistryHooks(providerPlugin, routerProviderRegistryHooks) {
|
|
3
|
+
const unsupportedHookNames = Object.keys(providerPlugin.registryHooks ?? {}).filter((hookName)=>!(hookName in routerProviderRegistryHooks));
|
|
4
|
+
if (unsupportedHookNames.length > 0) console.warn(`[@modern-js/runtime] The router provider "${providerPlugin.name}" declares registry hooks outside the router hook contract: ${unsupportedHookNames.join(', ')}. These hooks are not registered when the provider is resolved through \`runtime.router.framework\` — declare them on a separate runtime plugin instead.`);
|
|
5
|
+
return unsupportedHookNames;
|
|
6
|
+
}
|
|
7
|
+
function createRouterProviderRealm(registrations) {
|
|
8
|
+
const providers = new Map();
|
|
9
|
+
let defaultProvider;
|
|
10
|
+
for (const registration of registrations){
|
|
11
|
+
if (0 === registration.name.length) throw new Error('[@modern-js/runtime] A router provider realm cannot contain an unnamed provider.');
|
|
12
|
+
if (providers.has(registration.name)) throw new Error(`[@modern-js/runtime] Router provider "${registration.name}" is declared more than once in the same runtime realm.`);
|
|
13
|
+
if (true === registration.isDefault && void 0 !== defaultProvider && defaultProvider !== registration.name) throw new Error(`[@modern-js/runtime] Router provider realm declares both "${defaultProvider}" and "${registration.name}" as defaults.`);
|
|
14
|
+
providers.set(registration.name, registration.factory);
|
|
15
|
+
if (true === registration.isDefault) defaultProvider = registration.name;
|
|
16
|
+
}
|
|
17
|
+
const providerNames = Object.freeze([
|
|
18
|
+
...providers.keys()
|
|
19
|
+
]);
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
...void 0 !== defaultProvider ? {
|
|
22
|
+
defaultProvider
|
|
23
|
+
} : {},
|
|
24
|
+
get: (name)=>providers.get(name),
|
|
25
|
+
names: ()=>providerNames
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const REGISTRY_SLOT = Symbol.for('@modern-js/runtime:router-providers:v3');
|
|
29
|
+
const LEGACY_V2_REGISTRY_SLOT = Symbol.for('@modern-js/runtime:router-providers:v2');
|
|
30
|
+
function getRegistry() {
|
|
31
|
+
const host = globalThis;
|
|
32
|
+
host[REGISTRY_SLOT] ??= {
|
|
33
|
+
providers: new Map(),
|
|
34
|
+
duplicateProviders: new Set(),
|
|
35
|
+
warnedDuplicates: new Set()
|
|
36
|
+
};
|
|
37
|
+
host[REGISTRY_SLOT].duplicateProviders ??= new Set();
|
|
38
|
+
host[REGISTRY_SLOT].warnedDuplicates ??= new Set();
|
|
39
|
+
return host[REGISTRY_SLOT];
|
|
40
|
+
}
|
|
41
|
+
function getLegacyV2Registry() {
|
|
42
|
+
const host = globalThis;
|
|
43
|
+
return host[LEGACY_V2_REGISTRY_SLOT];
|
|
44
|
+
}
|
|
45
|
+
function registerRouterProvider(name, factory, options = {}) {
|
|
46
|
+
const registry = getRegistry();
|
|
47
|
+
const existing = registry.providers.get(name);
|
|
48
|
+
if (void 0 !== existing) {
|
|
49
|
+
if (existing !== factory) registry.duplicateProviders.add(name);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (true === options.isDefault) registry.defaultProvider = name;
|
|
53
|
+
registry.providers.set(name, factory);
|
|
54
|
+
}
|
|
55
|
+
function resolveRouterProvider(framework, options = {}) {
|
|
56
|
+
if (void 0 !== options.realm) {
|
|
57
|
+
const name = framework || options.realm.defaultProvider;
|
|
58
|
+
const realmProviderNames = options.realm.names();
|
|
59
|
+
if (void 0 === name) throw new Error(`[@modern-js/runtime] The app-owned router provider realm does not declare a default provider. Available realm providers: ${realmProviderNames.join(', ') || '(none)'}.`);
|
|
60
|
+
const factory = options.realm.get(name);
|
|
61
|
+
if (void 0 === factory) throw new Error(`[@modern-js/runtime] Router provider "${name}" is not registered in the app-owned router provider realm. Available realm providers: ${realmProviderNames.join(', ') || '(none)'}. Compatibility registry fallback is disabled for app-owned realms.`);
|
|
62
|
+
return factory;
|
|
63
|
+
}
|
|
64
|
+
const registry = getRegistry();
|
|
65
|
+
const legacyV2Registry = getLegacyV2Registry();
|
|
66
|
+
const name = framework || options.localDefault?.name || registry.defaultProvider || legacyV2Registry?.defaultProvider;
|
|
67
|
+
if (void 0 === name) throw new Error('[@modern-js/runtime] No default router provider is registered. This is a bug in the runtime setup.');
|
|
68
|
+
if (void 0 !== options.localDefault && name === options.localDefault.name) return options.localDefault.factory;
|
|
69
|
+
const factory = registry.providers.get(name);
|
|
70
|
+
if (void 0 !== factory) {
|
|
71
|
+
if (registry.duplicateProviders.has(name) && !registry.warnedDuplicates.has(name)) {
|
|
72
|
+
registry.warnedDuplicates.add(name);
|
|
73
|
+
console.warn(`[@modern-js/runtime] The router provider "${name}" was registered more than once with different module instances, and a router wrapper without an app-owned provider realm is resolving the mixed-version compatibility fallback. The fallback is keeping the first registration. Modern router wrappers isolate app-owned realms automatically. For an older wrapper, share the provider runtime between Module Federation host and remotes or upgrade every app to a realm-aware runtime.`);
|
|
74
|
+
}
|
|
75
|
+
return factory;
|
|
76
|
+
}
|
|
77
|
+
const legacyV2Factory = legacyV2Registry?.providers.get(name);
|
|
78
|
+
if (void 0 !== legacyV2Factory) return legacyV2Factory;
|
|
79
|
+
if ('tanstack' === name) throw new Error("[@modern-js/runtime] `runtime.router.framework` is set to \"tanstack\", but no TanStack router provider is registered. Install @modern-js/plugin-tanstack, add `tanstackRouterPlugin()` to the `plugins` array in modern.config.ts, and make sure '@modern-js/plugin-tanstack/runtime' is imported (e.g. in modern.runtime.ts).");
|
|
80
|
+
throw new Error(`[@modern-js/runtime] Unknown router framework "${name}". Registered providers: ${[
|
|
81
|
+
...new Set([
|
|
82
|
+
...registry.providers.keys(),
|
|
83
|
+
...legacyV2Registry?.providers.keys() ?? []
|
|
84
|
+
])
|
|
85
|
+
].join(', ') || '(none)'}. Install and register the plugin that provides this router framework.`);
|
|
86
|
+
}
|
|
87
|
+
function unsafe_resetRouterProvidersForTesting() {
|
|
88
|
+
const host = globalThis;
|
|
89
|
+
delete host[REGISTRY_SLOT];
|
|
90
|
+
delete host[LEGACY_V2_REGISTRY_SLOT];
|
|
91
|
+
}
|
|
92
|
+
function createRouterPlugin({ defaultProvider, registryHooks, localProviders = [] }) {
|
|
93
|
+
registerRouterProvider(defaultProvider.name, defaultProvider.factory, {
|
|
94
|
+
isDefault: true
|
|
95
|
+
});
|
|
96
|
+
const realm = createRouterProviderRealm([
|
|
97
|
+
{
|
|
98
|
+
...defaultProvider,
|
|
99
|
+
isDefault: true
|
|
100
|
+
},
|
|
101
|
+
...localProviders
|
|
102
|
+
]);
|
|
103
|
+
return (userConfig = {})=>({
|
|
104
|
+
name: '@modern-js/plugin-router',
|
|
105
|
+
registryHooks,
|
|
106
|
+
setup (api) {
|
|
107
|
+
const mergedConfig = merge(api.getRuntimeConfig().router || {}, userConfig);
|
|
108
|
+
const factory = resolveRouterProvider(mergedConfig.framework, {
|
|
109
|
+
realm
|
|
110
|
+
});
|
|
111
|
+
const provider = factory(userConfig);
|
|
112
|
+
reportUnsupportedProviderRegistryHooks(provider, registryHooks);
|
|
113
|
+
provider.setup?.(api);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
export { createRouterPlugin, createRouterProviderRealm, registerRouterProvider, reportUnsupportedProviderRegistryHooks, resolveRouterProvider, unsafe_resetRouterProvidersForTesting };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { createRuntimeContextExtension } from "./contextExtensions.mjs";
|
|
2
|
+
const routerRuntimeStateExtension = createRuntimeContextExtension('@modern-js/runtime:router-runtime-state');
|
|
3
|
+
const routerServerSnapshotExtension = createRuntimeContextExtension('@modern-js/runtime:router-server-snapshot');
|
|
4
|
+
function getRouterRuntimeState(runtimeContext) {
|
|
5
|
+
return routerRuntimeStateExtension.get(runtimeContext);
|
|
6
|
+
}
|
|
7
|
+
function getRouterServerSnapshot(runtimeContext) {
|
|
8
|
+
return routerServerSnapshotExtension.get(runtimeContext);
|
|
9
|
+
}
|
|
10
|
+
function toHydrationScripts(state) {
|
|
11
|
+
if (state.hydrationScripts?.length) return state.hydrationScripts;
|
|
12
|
+
return state.hydrationScript ? [
|
|
13
|
+
state.hydrationScript
|
|
14
|
+
] : void 0;
|
|
15
|
+
}
|
|
16
|
+
function getMatchedRouteIdsFromMatches(matches) {
|
|
17
|
+
const routeIds = matches?.map((match)=>match.assetRouteId ?? match.routeId).filter((routeId)=>'string' == typeof routeId);
|
|
18
|
+
return routeIds?.length ? routeIds : void 0;
|
|
19
|
+
}
|
|
20
|
+
function createRouterServerSnapshot(state) {
|
|
21
|
+
const hydrationScripts = toHydrationScripts(state);
|
|
22
|
+
const matchedRouteIds = state.matchedRouteIds ?? getMatchedRouteIdsFromMatches(state.matches);
|
|
23
|
+
return {
|
|
24
|
+
...state,
|
|
25
|
+
...hydrationScripts?.length ? {
|
|
26
|
+
hydrationScript: state.hydrationScript ?? hydrationScripts[0],
|
|
27
|
+
hydrationScripts
|
|
28
|
+
} : {},
|
|
29
|
+
...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
|
|
62
|
+
} : {},
|
|
63
|
+
...matchedRouteIds ? {
|
|
64
|
+
matchedRouteIds
|
|
65
|
+
} : {},
|
|
66
|
+
...serverSnapshot ? {
|
|
67
|
+
serverSnapshot
|
|
68
|
+
} : {}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function applyRouterRuntimeState(runtimeContext, state) {
|
|
72
|
+
const normalized = createRouterRuntimeState(state);
|
|
73
|
+
routerRuntimeStateExtension.set(runtimeContext, normalized);
|
|
74
|
+
if (normalized.serverSnapshot) routerServerSnapshotExtension.set(runtimeContext, normalized.serverSnapshot);
|
|
75
|
+
return runtimeContext;
|
|
76
|
+
}
|
|
77
|
+
function applyRouterServerPrepareResult(runtimeContext, result) {
|
|
78
|
+
const state = createRouterRuntimeState({
|
|
79
|
+
...result.state,
|
|
80
|
+
cleanup: result.cleanup ?? result.state.cleanup,
|
|
81
|
+
serverSnapshot: result.snapshot ?? result.state.serverSnapshot
|
|
82
|
+
});
|
|
83
|
+
applyRouterRuntimeState(runtimeContext, state);
|
|
84
|
+
return runtimeContext;
|
|
85
|
+
}
|
|
86
|
+
function getRouterHydrationScripts(runtimeContext) {
|
|
87
|
+
const serverSnapshot = getRouterServerSnapshot(runtimeContext);
|
|
88
|
+
const runtimeState = getRouterRuntimeState(runtimeContext);
|
|
89
|
+
return serverSnapshot?.hydrationScripts ?? toHydrationScripts({
|
|
90
|
+
hydrationScript: serverSnapshot?.hydrationScript
|
|
91
|
+
}) ?? runtimeState?.hydrationScripts ?? toHydrationScripts({
|
|
92
|
+
hydrationScript: runtimeState?.hydrationScript
|
|
93
|
+
}) ?? [];
|
|
94
|
+
}
|
|
95
|
+
function getRouterMatchedRouteIds(runtimeContext) {
|
|
96
|
+
const serverSnapshot = getRouterServerSnapshot(runtimeContext);
|
|
97
|
+
const runtimeState = getRouterRuntimeState(runtimeContext);
|
|
98
|
+
return serverSnapshot?.matchedRouteIds ?? getMatchedRouteIdsFromMatches(serverSnapshot?.matches) ?? runtimeState?.matchedRouteIds ?? getMatchedRouteIdsFromMatches(runtimeState?.matches);
|
|
99
|
+
}
|
|
100
|
+
async function cleanupRouterRuntimeState(runtimeContext) {
|
|
101
|
+
try {
|
|
102
|
+
await getRouterRuntimeState(runtimeContext)?.cleanup?.();
|
|
103
|
+
} catch {}
|
|
104
|
+
}
|
|
105
|
+
export { applyRouterRuntimeState, applyRouterServerPrepareResult, cleanupRouterRuntimeState, createRouterRuntimeState, createRouterServerSnapshot, getRouterHydrationScripts, getRouterMatchedRouteIds, getRouterRuntimeState, getRouterServerSnapshot };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createRouterPrefetchPolicy } from "./routerPrefetchPolicy.mjs";
|
|
2
|
+
import { applyRouterRuntimeState, createRouterServerSnapshot } from "./routerState.mjs";
|
|
3
|
+
function createRouterStatePlugin({ registryHooks }) {
|
|
4
|
+
return {
|
|
5
|
+
name: '@modern-js/router-runtime-policy',
|
|
6
|
+
registryHooks,
|
|
7
|
+
setup (api) {
|
|
8
|
+
api.onBeforeRender((context)=>{
|
|
9
|
+
const runtimeContext = context;
|
|
10
|
+
runtimeContext.linkPrefetchPolicy ??= createRouterPrefetchPolicy();
|
|
11
|
+
});
|
|
12
|
+
api.onAfterCreateRouter((event)=>{
|
|
13
|
+
if ('react-router' !== event.framework) return;
|
|
14
|
+
if ('ssr-prepare' === event.phase) {
|
|
15
|
+
const { routerContext } = event.runtimeContext;
|
|
16
|
+
if (!routerContext) return;
|
|
17
|
+
const serverSnapshot = createRouterServerSnapshot({
|
|
18
|
+
framework: event.framework,
|
|
19
|
+
basename: event.basename,
|
|
20
|
+
statusCode: routerContext.statusCode,
|
|
21
|
+
errors: routerContext.errors ?? void 0,
|
|
22
|
+
routerData: {
|
|
23
|
+
loaderData: routerContext.loaderData,
|
|
24
|
+
errors: routerContext.errors ?? void 0
|
|
25
|
+
},
|
|
26
|
+
matches: routerContext.matches.flatMap((match)=>'string' == typeof match.route.id ? [
|
|
27
|
+
{
|
|
28
|
+
routeId: match.route.id
|
|
29
|
+
}
|
|
30
|
+
] : [])
|
|
31
|
+
});
|
|
32
|
+
applyRouterRuntimeState(event.runtimeContext, {
|
|
33
|
+
framework: event.framework,
|
|
34
|
+
basename: event.basename,
|
|
35
|
+
instance: event.router,
|
|
36
|
+
matchedRouteIds: serverSnapshot.matchedRouteIds,
|
|
37
|
+
serverSnapshot
|
|
38
|
+
});
|
|
39
|
+
} else if ('client-create' === event.phase) applyRouterRuntimeState(event.runtimeContext, {
|
|
40
|
+
framework: event.framework,
|
|
41
|
+
basename: event.basename,
|
|
42
|
+
instance: event.router
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export { createRouterStatePlugin };
|
|
File without changes
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { stripRuntimeContextExtensions } from "./contextExtensions.mjs";
|
|
2
|
+
function createRscSafeRequestContext(ssrContext) {
|
|
3
|
+
const requestContext = {
|
|
4
|
+
request: {
|
|
5
|
+
params: {},
|
|
6
|
+
pathname: '',
|
|
7
|
+
query: {},
|
|
8
|
+
headers: {},
|
|
9
|
+
host: '',
|
|
10
|
+
url: ''
|
|
11
|
+
},
|
|
12
|
+
response: {
|
|
13
|
+
setHeader () {},
|
|
14
|
+
status () {},
|
|
15
|
+
locals: {}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(requestContext.response, 'setHeader', {
|
|
19
|
+
enumerable: false
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(requestContext.response, 'status', {
|
|
22
|
+
enumerable: false
|
|
23
|
+
});
|
|
24
|
+
if (void 0 === ssrContext) return requestContext;
|
|
25
|
+
const { request, response } = ssrContext;
|
|
26
|
+
return {
|
|
27
|
+
request: {
|
|
28
|
+
url: request.url,
|
|
29
|
+
userAgent: request.userAgent,
|
|
30
|
+
cookie: request.cookie,
|
|
31
|
+
pathname: request.pathname,
|
|
32
|
+
query: {
|
|
33
|
+
...request.query
|
|
34
|
+
},
|
|
35
|
+
params: {
|
|
36
|
+
...request.params
|
|
37
|
+
},
|
|
38
|
+
headers: {
|
|
39
|
+
...request.headers
|
|
40
|
+
},
|
|
41
|
+
host: request.host,
|
|
42
|
+
referer: request.referer
|
|
43
|
+
},
|
|
44
|
+
response: Object.assign(requestContext.response, {
|
|
45
|
+
locals: response.locals ?? {}
|
|
46
|
+
})
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function projectRuntimeContext(projection, options) {
|
|
50
|
+
let internalContext = projection.internalContext;
|
|
51
|
+
let publicContext = {
|
|
52
|
+
...projection.publicContext
|
|
53
|
+
};
|
|
54
|
+
stripRuntimeContextExtensions(publicContext);
|
|
55
|
+
if (options.isRsc && false === options.context.isBrowser) {
|
|
56
|
+
const requestContext = createRscSafeRequestContext(options.context.ssrContext);
|
|
57
|
+
internalContext = {
|
|
58
|
+
...internalContext,
|
|
59
|
+
context: requestContext,
|
|
60
|
+
requestContext
|
|
61
|
+
};
|
|
62
|
+
delete internalContext.ssrContext;
|
|
63
|
+
publicContext = {
|
|
64
|
+
...publicContext,
|
|
65
|
+
context: requestContext,
|
|
66
|
+
requestContext
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
internalContext,
|
|
71
|
+
publicContext
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export { projectRuntimeContext };
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/dist/esm/shellStream.mjs
CHANGED
|
@@ -12,7 +12,7 @@ const createConservingWebShellStream = (source, context, marker, onMissingMarker
|
|
|
12
12
|
markerFound = candidate.includes(marker);
|
|
13
13
|
markerCarry = markerFound ? '' : candidate.slice(-(marker.length - 1));
|
|
14
14
|
};
|
|
15
|
-
|
|
15
|
+
const transform = new TransformStream({
|
|
16
16
|
transform (chunk, controller) {
|
|
17
17
|
const decoded = decoder.decode(chunk, {
|
|
18
18
|
stream: true
|
|
@@ -33,6 +33,38 @@ const createConservingWebShellStream = (source, context, marker, onMissingMarker
|
|
|
33
33
|
const output = headProcessor.finish(decoded);
|
|
34
34
|
if (output) controller.enqueue(encoder.encode(output));
|
|
35
35
|
}
|
|
36
|
-
})
|
|
36
|
+
});
|
|
37
|
+
const piping = source.pipeTo(transform.writable);
|
|
38
|
+
piping.catch(()=>{});
|
|
39
|
+
const reader = transform.readable.getReader();
|
|
40
|
+
return new ReadableStream({
|
|
41
|
+
async pull (controller) {
|
|
42
|
+
try {
|
|
43
|
+
const result = await reader.read();
|
|
44
|
+
if (result.done) {
|
|
45
|
+
await piping;
|
|
46
|
+
controller.close();
|
|
47
|
+
reader.releaseLock();
|
|
48
|
+
} else controller.enqueue(result.value);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
await piping.catch(()=>{});
|
|
51
|
+
controller.error(error);
|
|
52
|
+
reader.releaseLock();
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
async cancel (reason) {
|
|
56
|
+
try {
|
|
57
|
+
await reader.cancel(reason);
|
|
58
|
+
} finally{
|
|
59
|
+
try {
|
|
60
|
+
await piping.catch((error)=>{
|
|
61
|
+
if (error !== reason) throw error;
|
|
62
|
+
});
|
|
63
|
+
} finally{
|
|
64
|
+
reader.releaseLock();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
37
69
|
};
|
|
38
70
|
export { createConservingWebShellStream };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const ultramodernWorkspaceEventNames = {
|
|
2
|
+
navigate: 'ultramodern:navigate',
|
|
3
|
+
performanceSignal: 'ultramodern:performance-signal',
|
|
4
|
+
remoteReady: 'ultramodern:remote-ready',
|
|
5
|
+
routeSettled: 'ultramodern:route-settled'
|
|
6
|
+
};
|
|
7
|
+
class UltramodernWorkspaceEventValidationError {
|
|
8
|
+
constructor(eventName, payload){
|
|
9
|
+
this.name = 'UltramodernWorkspaceEventValidationError';
|
|
10
|
+
this.message = `Invalid payload for UltraModern workspace event "${eventName}"`;
|
|
11
|
+
this.eventName = eventName;
|
|
12
|
+
this.payload = payload;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
|
|
16
|
+
const isNonEmptyString = (value)=>'string' == typeof value && value.trim().length > 0;
|
|
17
|
+
const isNonNegativeNumber = (value)=>'number' == typeof value && Number.isFinite(value) && value >= 0;
|
|
18
|
+
const isUltramodernWorkspaceLocale = (value)=>'en' === value || 'cs' === value;
|
|
19
|
+
const isPerformanceReadinessSignalId = (value)=>'bfcache' === value || 'core-web-vitals-rum' === value || 'duplicate-prefetch-warmup' === value || 'cache-policy-sanity' === value || 'save-data-behavior' === value || 'cloudflare-ssr-cache-hints' === value;
|
|
20
|
+
const isPerformanceReadinessSignalStatus = (value)=>'pass' === value || 'warn' === value || 'fail' === value;
|
|
21
|
+
const hasOptionalString = (value, key)=>void 0 === value[key] || isNonEmptyString(value[key]);
|
|
22
|
+
const hasOptionalBoolean = (value, key)=>void 0 === value[key] || 'boolean' == typeof value[key];
|
|
23
|
+
const hasOptionalRecord = (value, key)=>void 0 === value[key] || isRecord(value[key]);
|
|
24
|
+
const hasOptionalNonNegativeNumber = (value, key)=>void 0 === value[key] || isNonNegativeNumber(value[key]);
|
|
25
|
+
const hasOptionalLocale = (value, key)=>void 0 === value[key] || isUltramodernWorkspaceLocale(value[key]);
|
|
26
|
+
const isUltramodernNavigatePayload = (payload)=>isRecord(payload) && isNonEmptyString(payload.to) && hasOptionalBoolean(payload, 'replace') && hasOptionalRecord(payload, 'state');
|
|
27
|
+
const isUltramodernRouteSettledPayload = (payload)=>isRecord(payload) && isNonEmptyString(payload.pathname) && hasOptionalLocale(payload, 'locale') && hasOptionalString(payload, 'title');
|
|
28
|
+
const isUltramodernRemoteReadyPayload = (payload)=>isRecord(payload) && isNonEmptyString(payload.appId) && hasOptionalString(payload, 'build') && hasOptionalString(payload, 'surface') && hasOptionalString(payload, 'version');
|
|
29
|
+
const isUltramodernPerformanceSignalPayload = (payload)=>isRecord(payload) && isPerformanceReadinessSignalId(payload.signalId) && isPerformanceReadinessSignalStatus(payload.status) && hasOptionalNonNegativeNumber(payload, 'durationMs') && hasOptionalRecord(payload, 'detail');
|
|
30
|
+
const ultramodernWorkspaceEventValidators = {
|
|
31
|
+
[ultramodernWorkspaceEventNames.navigate]: isUltramodernNavigatePayload,
|
|
32
|
+
[ultramodernWorkspaceEventNames.performanceSignal]: isUltramodernPerformanceSignalPayload,
|
|
33
|
+
[ultramodernWorkspaceEventNames.remoteReady]: isUltramodernRemoteReadyPayload,
|
|
34
|
+
[ultramodernWorkspaceEventNames.routeSettled]: isUltramodernRouteSettledPayload
|
|
35
|
+
};
|
|
36
|
+
const isUltramodernWorkspaceEventPayload = (eventName, payload)=>ultramodernWorkspaceEventValidators[eventName](payload);
|
|
37
|
+
const assertUltramodernWorkspaceEventPayload = (eventName, payload)=>{
|
|
38
|
+
if (!isUltramodernWorkspaceEventPayload(eventName, payload)) throw new UltramodernWorkspaceEventValidationError(eventName, payload);
|
|
39
|
+
return payload;
|
|
40
|
+
};
|
|
41
|
+
const createUltramodernWorkspaceEvent = (eventName, payload)=>new CustomEvent(eventName, {
|
|
42
|
+
bubbles: true,
|
|
43
|
+
composed: true,
|
|
44
|
+
detail: assertUltramodernWorkspaceEventPayload(eventName, payload)
|
|
45
|
+
});
|
|
46
|
+
const dispatchUltramodernWorkspaceEvent = (target, eventName, payload)=>target.dispatchEvent(createUltramodernWorkspaceEvent(eventName, payload));
|
|
47
|
+
const onUltramodernWorkspaceEvent = (target, eventName, handler)=>{
|
|
48
|
+
const listener = (event)=>{
|
|
49
|
+
if (!('detail' in event)) throw new UltramodernWorkspaceEventValidationError(eventName, void 0);
|
|
50
|
+
const customEvent = event;
|
|
51
|
+
handler(assertUltramodernWorkspaceEventPayload(eventName, customEvent.detail), customEvent);
|
|
52
|
+
};
|
|
53
|
+
target.addEventListener(eventName, listener);
|
|
54
|
+
return ()=>{
|
|
55
|
+
target.removeEventListener(eventName, listener);
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
const dispatchUltramodernNavigate = (target, payload)=>dispatchUltramodernWorkspaceEvent(target, ultramodernWorkspaceEventNames.navigate, payload);
|
|
59
|
+
const dispatchUltramodernRouteSettled = (target, payload)=>dispatchUltramodernWorkspaceEvent(target, ultramodernWorkspaceEventNames.routeSettled, payload);
|
|
60
|
+
const dispatchUltramodernRemoteReady = (target, payload)=>dispatchUltramodernWorkspaceEvent(target, ultramodernWorkspaceEventNames.remoteReady, payload);
|
|
61
|
+
const dispatchUltramodernPerformanceSignal = (target, payload)=>dispatchUltramodernWorkspaceEvent(target, ultramodernWorkspaceEventNames.performanceSignal, payload);
|
|
62
|
+
const onUltramodernNavigate = (target, handler)=>onUltramodernWorkspaceEvent(target, ultramodernWorkspaceEventNames.navigate, handler);
|
|
63
|
+
const onUltramodernRouteSettled = (target, handler)=>onUltramodernWorkspaceEvent(target, ultramodernWorkspaceEventNames.routeSettled, handler);
|
|
64
|
+
const onUltramodernRemoteReady = (target, handler)=>onUltramodernWorkspaceEvent(target, ultramodernWorkspaceEventNames.remoteReady, handler);
|
|
65
|
+
const onUltramodernPerformanceSignal = (target, handler)=>onUltramodernWorkspaceEvent(target, ultramodernWorkspaceEventNames.performanceSignal, handler);
|
|
66
|
+
export { UltramodernWorkspaceEventValidationError, assertUltramodernWorkspaceEventPayload, createUltramodernWorkspaceEvent, dispatchUltramodernNavigate, dispatchUltramodernPerformanceSignal, dispatchUltramodernRemoteReady, dispatchUltramodernRouteSettled, dispatchUltramodernWorkspaceEvent, isUltramodernNavigatePayload, isUltramodernPerformanceSignalPayload, isUltramodernRemoteReadyPayload, isUltramodernRouteSettledPayload, isUltramodernWorkspaceEventPayload, onUltramodernNavigate, onUltramodernPerformanceSignal, onUltramodernRemoteReady, onUltramodernRouteSettled, onUltramodernWorkspaceEvent, ultramodernWorkspaceEventNames };
|