@bleedingdev/modern-js-federation-runtime 3.9.0-ultramodern.8 → 3.9.3-ultramodern.1
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/module-federation/consume-surface.js +6 -16
- package/dist/cjs/module-federation/distributed-ssr.js +6 -1
- package/dist/cjs/module-federation/index.js +13 -239
- package/dist/cjs/module-federation/manifest-recovery-runtime-plugin.js +1 -1
- package/dist/cjs/module-federation/renderer-contract.js +159 -0
- package/dist/cjs/module-federation/renderer-runtime-plugin.js +265 -0
- package/dist/cjs/module-federation/telemetry.js +214 -0
- package/dist/esm/module-federation/consume-surface.mjs +3 -13
- package/dist/esm/module-federation/distributed-ssr.mjs +6 -1
- package/dist/esm/module-federation/index.mjs +1 -111
- package/dist/esm/module-federation/manifest-recovery-runtime-plugin.mjs +1 -1
- package/dist/esm/module-federation/renderer-contract.mjs +103 -0
- package/dist/esm/module-federation/renderer-runtime-plugin.mjs +222 -0
- package/dist/esm/module-federation/telemetry.mjs +142 -0
- package/dist/esm-node/module-federation/consume-surface.mjs +3 -13
- package/dist/esm-node/module-federation/distributed-ssr.mjs +6 -1
- package/dist/esm-node/module-federation/index.mjs +1 -111
- package/dist/esm-node/module-federation/manifest-recovery-runtime-plugin.mjs +1 -1
- package/dist/esm-node/module-federation/renderer-contract.mjs +104 -0
- package/dist/esm-node/module-federation/renderer-runtime-plugin.mjs +223 -0
- package/dist/esm-node/module-federation/telemetry.mjs +143 -0
- package/dist/types/module-federation/consume-surface.d.ts +5 -5
- package/dist/types/module-federation/distributed-ssr.d.ts +18 -1
- package/dist/types/module-federation/index.d.ts +1 -67
- package/dist/types/module-federation/renderer-contract.d.ts +20 -0
- package/dist/types/module-federation/renderer-runtime-plugin.d.ts +98 -0
- package/dist/types/module-federation/telemetry.d.ts +71 -0
- package/package.json +54 -18
- package/src/module-federation/consume-surface.ts +18 -33
- package/src/module-federation/distributed-ssr.tsx +27 -2
- package/src/module-federation/index.ts +1 -267
- package/src/module-federation/manifest-recovery-runtime-plugin.ts +5 -1
- package/src/module-federation/renderer-contract.ts +176 -0
- package/src/module-federation/renderer-runtime-plugin.ts +396 -0
- package/src/module-federation/telemetry.ts +297 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { assertRendererProfileCompatibility, formatBackendFederationValidationErrors, validateRendererIdentity, validateRendererProfile } from "@modern-js/backend-federation-contracts";
|
|
2
|
+
const RENDERER_FEDERATION_METADATA_KEY = 'ultramodernRenderer';
|
|
3
|
+
const RENDERER_FEDERATION_SCHEMA = 'ultramodern.renderer-federation';
|
|
4
|
+
const RENDERER_FEDERATION_SCHEMA_VERSION = 1;
|
|
5
|
+
const rendererFederationError = (message)=>new Error(`Renderer federation manifest contract: ${message}`);
|
|
6
|
+
const record = (value)=>null !== value && 'object' == typeof value && !Array.isArray(value);
|
|
7
|
+
const exactFields = (value, fields, label)=>{
|
|
8
|
+
const unknown = Object.keys(value).filter((key)=>!fields.includes(key));
|
|
9
|
+
const missing = fields.filter((key)=>!Object.hasOwn(value, key));
|
|
10
|
+
if (unknown.length || missing.length) throw rendererFederationError(`${label} fields differ (unknown=${unknown.join(',')}; missing=${missing.join(',')}).`);
|
|
11
|
+
};
|
|
12
|
+
const assertProfile = (value)=>{
|
|
13
|
+
const result = validateRendererProfile(value, 'rendererProfile');
|
|
14
|
+
if (!result.ok) throw rendererFederationError(formatBackendFederationValidationErrors(result.errors));
|
|
15
|
+
const profile = value;
|
|
16
|
+
return Object.freeze({
|
|
17
|
+
...profile,
|
|
18
|
+
compiler: Object.freeze({
|
|
19
|
+
...profile.compiler
|
|
20
|
+
}),
|
|
21
|
+
hydration: Object.freeze({
|
|
22
|
+
...profile.hydration
|
|
23
|
+
}),
|
|
24
|
+
router: Object.freeze({
|
|
25
|
+
...profile.router
|
|
26
|
+
})
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const assertPackage = (value, profile, label)=>{
|
|
30
|
+
const result = validateRendererProfile({
|
|
31
|
+
...profile,
|
|
32
|
+
compiler: value
|
|
33
|
+
}, label);
|
|
34
|
+
if (!result.ok) throw rendererFederationError(formatBackendFederationValidationErrors(result.errors));
|
|
35
|
+
return Object.freeze({
|
|
36
|
+
...value
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
function readRendererFederationCompatibility(value) {
|
|
40
|
+
if (!record(value)) throw rendererFederationError('the consuming renderer tuple is absent.');
|
|
41
|
+
exactFields(value, [
|
|
42
|
+
'profile',
|
|
43
|
+
'runtime',
|
|
44
|
+
'bootstrap'
|
|
45
|
+
], 'compatibility');
|
|
46
|
+
const profile = assertProfile(value.profile);
|
|
47
|
+
return Object.freeze({
|
|
48
|
+
profile,
|
|
49
|
+
runtime: assertPackage(value.runtime, profile, 'runtime'),
|
|
50
|
+
bootstrap: assertPackage(value.bootstrap, profile, 'bootstrap')
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function readRendererFederationContract(value) {
|
|
54
|
+
if (!record(value)) throw rendererFederationError('renderer publication metadata is absent.');
|
|
55
|
+
exactFields(value, [
|
|
56
|
+
'schema',
|
|
57
|
+
'schemaVersion',
|
|
58
|
+
'profile',
|
|
59
|
+
'runtime',
|
|
60
|
+
'bootstrap',
|
|
61
|
+
'identities'
|
|
62
|
+
], 'publication');
|
|
63
|
+
if (value.schema !== RENDERER_FEDERATION_SCHEMA || value.schemaVersion !== RENDERER_FEDERATION_SCHEMA_VERSION) throw rendererFederationError('unsupported renderer publication schema.');
|
|
64
|
+
const compatibility = readRendererFederationCompatibility({
|
|
65
|
+
profile: value.profile,
|
|
66
|
+
runtime: value.runtime,
|
|
67
|
+
bootstrap: value.bootstrap
|
|
68
|
+
});
|
|
69
|
+
if (!record(value.identities) || !Object.keys(value.identities).length) throw rendererFederationError('finalized application entry identities are absent.');
|
|
70
|
+
const identities = Object.create(null);
|
|
71
|
+
let appId;
|
|
72
|
+
for (const [entryName, valueIdentity] of Object.entries(value.identities)){
|
|
73
|
+
const result = validateRendererIdentity(valueIdentity, `identities.${entryName}`);
|
|
74
|
+
if (!result.ok) throw rendererFederationError(formatBackendFederationValidationErrors(result.errors));
|
|
75
|
+
const identity = valueIdentity;
|
|
76
|
+
if (identity.entryName !== entryName || identity.renderer !== compatibility.profile.renderer || identity.protocolVersion !== compatibility.profile.protocolVersion || void 0 !== appId && identity.appId !== appId) throw rendererFederationError(`incoherent publication identity for ${entryName}.`);
|
|
77
|
+
appId = identity.appId;
|
|
78
|
+
identities[entryName] = Object.freeze({
|
|
79
|
+
...identity
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return Object.freeze({
|
|
83
|
+
schema: RENDERER_FEDERATION_SCHEMA,
|
|
84
|
+
schemaVersion: RENDERER_FEDERATION_SCHEMA_VERSION,
|
|
85
|
+
...compatibility,
|
|
86
|
+
identities: Object.freeze(identities)
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function assertRendererFederationCompatibility(expected, actual) {
|
|
90
|
+
try {
|
|
91
|
+
assertRendererProfileCompatibility(expected.profile, actual.profile);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
throw rendererFederationError(error instanceof Error ? error.message : String(error));
|
|
94
|
+
}
|
|
95
|
+
for (const field of [
|
|
96
|
+
'runtime',
|
|
97
|
+
'bootstrap'
|
|
98
|
+
])for (const key of [
|
|
99
|
+
'name',
|
|
100
|
+
'version'
|
|
101
|
+
])if (expected[field][key] !== actual[field][key]) throw rendererFederationError(`${field}.${key} must match the consuming renderer tuple.`);
|
|
102
|
+
}
|
|
103
|
+
export { RENDERER_FEDERATION_METADATA_KEY, RENDERER_FEDERATION_SCHEMA, RENDERER_FEDERATION_SCHEMA_VERSION, assertRendererFederationCompatibility, readRendererFederationCompatibility, readRendererFederationContract, rendererFederationError };
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { getResourceUrl, isBrowserEnvValue, isReactNativeEnv } from "@module-federation/sdk";
|
|
2
|
+
import { RENDERER_FEDERATION_METADATA_KEY, assertRendererFederationCompatibility, readRendererFederationCompatibility, readRendererFederationContract, rendererFederationError } from "./renderer-contract.mjs";
|
|
3
|
+
const ATTESTATION = Symbol.for('ultramodern.renderer-federation.attestation.v1');
|
|
4
|
+
const record = (value)=>null !== value && 'object' == typeof value && !Array.isArray(value);
|
|
5
|
+
const coordinates = (snapshot)=>JSON.stringify([
|
|
6
|
+
'globalName',
|
|
7
|
+
'buildVersion',
|
|
8
|
+
'remoteEntry',
|
|
9
|
+
'remoteEntryType',
|
|
10
|
+
'ssrRemoteEntry',
|
|
11
|
+
'ssrRemoteEntryType',
|
|
12
|
+
'publicPath',
|
|
13
|
+
'ssrPublicPath',
|
|
14
|
+
'getPublicPath'
|
|
15
|
+
].map((key)=>[
|
|
16
|
+
key,
|
|
17
|
+
snapshot[key]
|
|
18
|
+
]));
|
|
19
|
+
function attestation(snapshot) {
|
|
20
|
+
const descriptor = Object.getOwnPropertyDescriptor(snapshot, ATTESTATION);
|
|
21
|
+
const value = descriptor?.value;
|
|
22
|
+
if (!descriptor || descriptor.writable || descriptor.configurable || !record(value) || 'string' != typeof value.coordinates || value.coordinates !== coordinates(snapshot)) throw rendererFederationError('cached remote snapshot is missing or has changed its attestation.');
|
|
23
|
+
return {
|
|
24
|
+
contract: readRendererFederationContract(value.contract),
|
|
25
|
+
coordinates: value.coordinates
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const assertManifestRemote = (remote)=>{
|
|
29
|
+
if (!record(remote)) throw rendererFederationError('remote registration is absent.');
|
|
30
|
+
if ('entry' in remote && ('string' != typeof remote.entry || !remote.entry.includes('.json'))) throw rendererFederationError('renderer components require a native JSON manifest remote.');
|
|
31
|
+
};
|
|
32
|
+
function sameFactory(left, right) {
|
|
33
|
+
if (left === right) return true;
|
|
34
|
+
if ('function' == typeof left.lib && 'function' == typeof right.lib) return left.lib === right.lib;
|
|
35
|
+
return 'function' == typeof left.get && left.get === right.get;
|
|
36
|
+
}
|
|
37
|
+
const containerCoordinates = (info)=>JSON.stringify([
|
|
38
|
+
'name',
|
|
39
|
+
'entry',
|
|
40
|
+
'type',
|
|
41
|
+
'entryGlobalName',
|
|
42
|
+
'buildVersion'
|
|
43
|
+
].map((key)=>[
|
|
44
|
+
key,
|
|
45
|
+
info[key]
|
|
46
|
+
]));
|
|
47
|
+
const entryCoordinates = (info)=>JSON.stringify([
|
|
48
|
+
'name',
|
|
49
|
+
'entry',
|
|
50
|
+
'type',
|
|
51
|
+
'entryGlobalName'
|
|
52
|
+
].map((key)=>[
|
|
53
|
+
key,
|
|
54
|
+
info[key]
|
|
55
|
+
]));
|
|
56
|
+
function rendererShareVersions(expected) {
|
|
57
|
+
const { profile, runtime, bootstrap } = expected;
|
|
58
|
+
if ('react' === profile.renderer) return new Map([
|
|
59
|
+
[
|
|
60
|
+
'react',
|
|
61
|
+
runtime.version
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
'react-dom',
|
|
65
|
+
profile.hydration.version
|
|
66
|
+
],
|
|
67
|
+
[
|
|
68
|
+
'react-dom/client',
|
|
69
|
+
profile.hydration.version
|
|
70
|
+
],
|
|
71
|
+
[
|
|
72
|
+
'@modern-js/runtime',
|
|
73
|
+
bootstrap.version
|
|
74
|
+
]
|
|
75
|
+
]);
|
|
76
|
+
const versions = new Map();
|
|
77
|
+
for (const [name, version] of [
|
|
78
|
+
[
|
|
79
|
+
runtime.name,
|
|
80
|
+
runtime.version
|
|
81
|
+
],
|
|
82
|
+
[
|
|
83
|
+
profile.hydration.name,
|
|
84
|
+
profile.hydration.version
|
|
85
|
+
],
|
|
86
|
+
[
|
|
87
|
+
bootstrap.name,
|
|
88
|
+
bootstrap.version
|
|
89
|
+
],
|
|
90
|
+
[
|
|
91
|
+
profile.router.name,
|
|
92
|
+
profile.router.version
|
|
93
|
+
],
|
|
94
|
+
[
|
|
95
|
+
profile.router.coreName,
|
|
96
|
+
profile.router.coreVersion
|
|
97
|
+
]
|
|
98
|
+
]){
|
|
99
|
+
const prior = versions.get(name);
|
|
100
|
+
if (void 0 !== prior && prior !== version) throw rendererFederationError(`renderer tuple names ${name} with conflicting versions.`);
|
|
101
|
+
versions.set(name, version);
|
|
102
|
+
}
|
|
103
|
+
return versions;
|
|
104
|
+
}
|
|
105
|
+
function createRendererFederationRuntimePlugin(options) {
|
|
106
|
+
const expected = readRendererFederationCompatibility(options);
|
|
107
|
+
const selected = new Map();
|
|
108
|
+
const approvedRemoteInfos = new WeakMap();
|
|
109
|
+
const observedEntries = new WeakMap();
|
|
110
|
+
const acceptedEntries = new Set();
|
|
111
|
+
const versions = rendererShareVersions(expected);
|
|
112
|
+
return {
|
|
113
|
+
name: 'ultramodern-renderer-federation-contract',
|
|
114
|
+
beforeRegisterRemote (args) {
|
|
115
|
+
assertManifestRemote(args.remote);
|
|
116
|
+
return args;
|
|
117
|
+
},
|
|
118
|
+
async afterMatchRemote (args) {
|
|
119
|
+
if (!args.error) assertManifestRemote(args.remote);
|
|
120
|
+
return args;
|
|
121
|
+
},
|
|
122
|
+
async loadRemoteSnapshot (args) {
|
|
123
|
+
const snapshot = args.remoteSnapshot;
|
|
124
|
+
if ('manifest' === args.from) {
|
|
125
|
+
const manifest = args.manifestJson;
|
|
126
|
+
const metadata = record(manifest) && record(manifest.metaData) ? manifest.metaData[RENDERER_FEDERATION_METADATA_KEY] : void 0;
|
|
127
|
+
const contract = readRendererFederationContract(metadata);
|
|
128
|
+
if (!record(snapshot) || 'string' != typeof snapshot.remoteEntry) throw rendererFederationError('native remote entry coordinates are absent.');
|
|
129
|
+
const existing = Object.getOwnPropertyDescriptor(snapshot, ATTESTATION);
|
|
130
|
+
if (existing) {
|
|
131
|
+
const prior = attestation(snapshot);
|
|
132
|
+
if (JSON.stringify(prior.contract) !== JSON.stringify(contract)) throw rendererFederationError('remote snapshot has conflicting publication ownership.');
|
|
133
|
+
} else Object.defineProperty(snapshot, ATTESTATION, {
|
|
134
|
+
value: Object.freeze({
|
|
135
|
+
contract,
|
|
136
|
+
coordinates: coordinates(snapshot)
|
|
137
|
+
})
|
|
138
|
+
});
|
|
139
|
+
} else assertRendererFederationCompatibility(expected, attestation(snapshot).contract);
|
|
140
|
+
return args;
|
|
141
|
+
},
|
|
142
|
+
async afterLoadSnapshot (args) {
|
|
143
|
+
assertRendererFederationCompatibility(expected, attestation(args.remoteSnapshot).contract);
|
|
144
|
+
const snapshot = args.remoteSnapshot;
|
|
145
|
+
const browser = isBrowserEnvValue || isReactNativeEnv() || !('ssrRemoteEntry' in snapshot);
|
|
146
|
+
const entry = browser ? snapshot.remoteEntry : snapshot.ssrRemoteEntry;
|
|
147
|
+
if ('string' != typeof entry || !entry) throw rendererFederationError('native projected remote entry is absent.');
|
|
148
|
+
let entryUrl = getResourceUrl(snapshot, entry);
|
|
149
|
+
if (!isBrowserEnvValue && !entryUrl.startsWith('http')) entryUrl = `https:${entryUrl}`;
|
|
150
|
+
acceptedEntries.add(entryCoordinates({
|
|
151
|
+
name: args.moduleInfo?.name ?? snapshot.globalName,
|
|
152
|
+
entry: entryUrl,
|
|
153
|
+
type: browser ? snapshot.remoteEntryType : snapshot.ssrRemoteEntryType || 'global',
|
|
154
|
+
entryGlobalName: snapshot.globalName
|
|
155
|
+
}));
|
|
156
|
+
return args;
|
|
157
|
+
},
|
|
158
|
+
async afterResolve (args) {
|
|
159
|
+
if (!args.remoteSnapshot) throw rendererFederationError('native remote resolution has no attested snapshot.');
|
|
160
|
+
assertRendererFederationCompatibility(expected, attestation(args.remoteSnapshot).contract);
|
|
161
|
+
const cached = args.origin.moduleCache.get(args.remote.name);
|
|
162
|
+
if (cached && (approvedRemoteInfos.get(cached.remoteInfo) !== containerCoordinates(cached.remoteInfo) || containerCoordinates(cached.remoteInfo) !== containerCoordinates(args.remoteInfo))) throw rendererFederationError('cached remote container has unattested or conflicting ownership.');
|
|
163
|
+
const priorCoordinates = approvedRemoteInfos.get(args.remoteInfo);
|
|
164
|
+
if (void 0 !== priorCoordinates && priorCoordinates !== containerCoordinates(args.remoteInfo)) throw rendererFederationError('native remote container coordinates changed after attestation.');
|
|
165
|
+
approvedRemoteInfos.set(args.remoteInfo, containerCoordinates(args.remoteInfo));
|
|
166
|
+
return args;
|
|
167
|
+
},
|
|
168
|
+
loadEntry (args) {
|
|
169
|
+
if (!acceptedEntries.has(entryCoordinates(args.remoteInfo))) throw rendererFederationError('remote entry coordinates differ from the attested native snapshot.');
|
|
170
|
+
},
|
|
171
|
+
async afterLoadEntry (args) {
|
|
172
|
+
if (args.error && !args.recovered) return args;
|
|
173
|
+
const binding = containerCoordinates(args.remoteInfo);
|
|
174
|
+
const entryBinding = entryCoordinates(args.remoteInfo);
|
|
175
|
+
if (args.cached && approvedRemoteInfos.get(args.remoteInfo) !== binding) throw rendererFederationError('remote entry has no attested native container resolution.');
|
|
176
|
+
if (!acceptedEntries.has(entryBinding)) throw rendererFederationError('remote entry coordinates differ from the attested native snapshot.');
|
|
177
|
+
const entry = args.remoteEntryExports;
|
|
178
|
+
if (('object' != typeof entry || null === entry) && 'function' != typeof entry) throw rendererFederationError('native container exports are absent.');
|
|
179
|
+
if (args.cached && observedEntries.get(entry) !== entryBinding) throw rendererFederationError('cached native container exports have unattested ownership.');
|
|
180
|
+
approvedRemoteInfos.set(args.remoteInfo, binding);
|
|
181
|
+
observedEntries.set(entry, entryBinding);
|
|
182
|
+
return args;
|
|
183
|
+
},
|
|
184
|
+
resolveShare (args) {
|
|
185
|
+
const version = versions.get(args.pkgName);
|
|
186
|
+
if (void 0 === version) return args;
|
|
187
|
+
const resolve = args.resolver;
|
|
188
|
+
return {
|
|
189
|
+
...args,
|
|
190
|
+
resolver () {
|
|
191
|
+
const result = resolve();
|
|
192
|
+
if (!result) return result;
|
|
193
|
+
const shared = result.useTreesShaking && record(result.shared.treeShaking) ? result.shared.treeShaking : result.shared;
|
|
194
|
+
if (result.shared.version !== version) throw rendererFederationError(`shared ${args.pkgName} version must be ${version}.`);
|
|
195
|
+
const prior = selected.get(args.pkgName);
|
|
196
|
+
if (prior && !sameFactory(prior, shared)) throw rendererFederationError(`shared ${args.pkgName} has conflicting runtime ownership.`);
|
|
197
|
+
for (const scope of Object.values(args.shareScopeMap))for (const candidate of Object.values(scope[args.pkgName] ?? {})){
|
|
198
|
+
const active = record(candidate.treeShaking) && candidate.treeShaking.loaded ? candidate.treeShaking : candidate;
|
|
199
|
+
if ((active.loaded || active.lib) && !sameFactory(active, shared)) throw rendererFederationError(`shared ${args.pkgName} has duplicate loaded runtime identities.`);
|
|
200
|
+
}
|
|
201
|
+
selected.set(args.pkgName, Object.freeze({
|
|
202
|
+
lib: shared.lib,
|
|
203
|
+
get: shared.get
|
|
204
|
+
}));
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
},
|
|
209
|
+
afterLoadShare (args) {
|
|
210
|
+
if (versions.has(args.pkgName) && args.selectedShared) {
|
|
211
|
+
const shared = record(args.selectedShared.treeShaking) && args.selectedShared.treeShaking.loaded ? args.selectedShared.treeShaking : args.selectedShared;
|
|
212
|
+
selected.set(args.pkgName, Object.freeze({
|
|
213
|
+
lib: shared.lib,
|
|
214
|
+
get: shared.get
|
|
215
|
+
}));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
const renderer_runtime_plugin = createRendererFederationRuntimePlugin;
|
|
221
|
+
export default renderer_runtime_plugin;
|
|
222
|
+
export { createRendererFederationRuntimePlugin, rendererShareVersions };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const MODULE_FEDERATION_FALLBACK_SIGNAL_EVENT = 'modernjs:mf-runtime-fallback';
|
|
2
|
+
const MODULE_FEDERATION_RECOVERY_SIGNAL_EVENT = 'modernjs:mf-runtime-recovery';
|
|
3
|
+
const DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT = '/_modern/contract-gates/runtime-fallback';
|
|
4
|
+
const DEFAULT_RUNTIME_FALLBACK_SIGNAL_AUTH_HEADER = 'x-modernjs-runtime-signal-token';
|
|
5
|
+
class ModuleFederationRemoteLoadTimeoutError extends Error {
|
|
6
|
+
constructor(remote, timeoutMs){
|
|
7
|
+
super(`Loading remote "${remote}" timed out after ${timeoutMs}ms`);
|
|
8
|
+
this.name = 'ModuleFederationRemoteLoadTimeoutError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
class ModuleFederationRemoteLoadError extends Error {
|
|
12
|
+
constructor(remote, attempts, causeError){
|
|
13
|
+
super(`Unable to load remote "${remote}" after ${attempts} attempt${attempts > 1 ? 's' : ''}: ${causeError.message}`);
|
|
14
|
+
this.name = 'ModuleFederationRemoteLoadError';
|
|
15
|
+
this.remote = remote;
|
|
16
|
+
this.attempts = attempts;
|
|
17
|
+
this.causeError = causeError;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
class ModuleFederationRemoteComponentContractError extends Error {
|
|
21
|
+
constructor(remote, exportName){
|
|
22
|
+
super(`Remote "${remote}" export "${exportName}" is not a valid React component`);
|
|
23
|
+
this.name = 'ModuleFederationRemoteComponentContractError';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function toError(error) {
|
|
27
|
+
if (error instanceof Error) return error;
|
|
28
|
+
return new Error('string' == typeof error ? error : 'Unknown remote load error');
|
|
29
|
+
}
|
|
30
|
+
function classifyModuleFederationFallback(error) {
|
|
31
|
+
const normalizedError = toError(error);
|
|
32
|
+
if (normalizedError instanceof ModuleFederationRemoteLoadError) return classifyModuleFederationFallback(normalizedError.causeError);
|
|
33
|
+
if (normalizedError instanceof ModuleFederationRemoteLoadTimeoutError) return 'timeout';
|
|
34
|
+
if (normalizedError instanceof ModuleFederationRemoteComponentContractError) return 'contract';
|
|
35
|
+
const message = normalizedError.message;
|
|
36
|
+
if (/version|requiredVersion|singleton|share scope|shared module/i.test(message)) return 'version-skew';
|
|
37
|
+
if (/network|fetch|script|timeout|chunk|loading/i.test(message)) return 'network';
|
|
38
|
+
return 'remote-unavailable';
|
|
39
|
+
}
|
|
40
|
+
function createModuleFederationFallbackTelemetry(input) {
|
|
41
|
+
const error = void 0 !== input.error ? toError(input.error) : void 0;
|
|
42
|
+
const status = input.status ?? 'degraded';
|
|
43
|
+
const eventName = input.eventName ?? ('recovered' === status ? MODULE_FEDERATION_RECOVERY_SIGNAL_EVENT : MODULE_FEDERATION_FALLBACK_SIGNAL_EVENT);
|
|
44
|
+
const metadata = {
|
|
45
|
+
...input.metadata ?? {},
|
|
46
|
+
classification: input.classification,
|
|
47
|
+
remote: input.remote,
|
|
48
|
+
status
|
|
49
|
+
};
|
|
50
|
+
if (void 0 !== input.exportName) metadata.exportName = input.exportName;
|
|
51
|
+
if (void 0 !== input.runtimeDigest) metadata.runtimeDigest = input.runtimeDigest;
|
|
52
|
+
if (void 0 !== error) {
|
|
53
|
+
metadata.errorName = error.name;
|
|
54
|
+
metadata.errorMessage = error.message;
|
|
55
|
+
}
|
|
56
|
+
const payload = {
|
|
57
|
+
appName: input.appName,
|
|
58
|
+
eventName,
|
|
59
|
+
metadata,
|
|
60
|
+
phase: input.phase,
|
|
61
|
+
reason: input.classification,
|
|
62
|
+
schemaVersion: 1
|
|
63
|
+
};
|
|
64
|
+
if (void 0 !== input.entry) payload.entry = input.entry;
|
|
65
|
+
if (void 0 !== input.runtimeDigest) payload.runtimeDigest = input.runtimeDigest;
|
|
66
|
+
return payload;
|
|
67
|
+
}
|
|
68
|
+
function toModuleFederationFallbackAttributes(payload) {
|
|
69
|
+
return {
|
|
70
|
+
'data-mf-fallback-app': payload.appName,
|
|
71
|
+
'data-mf-fallback-classification': payload.reason,
|
|
72
|
+
'data-mf-fallback-phase': payload.phase,
|
|
73
|
+
'data-mf-fallback-remote': String(payload.metadata.remote),
|
|
74
|
+
'data-mf-fallback-status': String(payload.metadata.status),
|
|
75
|
+
'data-mf-telemetry-event': payload.eventName
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function emitModuleFederationFallbackTelemetry(input, options = {}) {
|
|
79
|
+
return emitModuleFederationFallbackPayload(createModuleFederationFallbackTelemetry(input), options);
|
|
80
|
+
}
|
|
81
|
+
function emitModuleFederationFallbackPayload(payload, options = {}) {
|
|
82
|
+
const delivery = deliver(payload, options);
|
|
83
|
+
try {
|
|
84
|
+
options.observe?.(payload, delivery);
|
|
85
|
+
} catch {}
|
|
86
|
+
return delivery;
|
|
87
|
+
}
|
|
88
|
+
async function deliver(payload, options) {
|
|
89
|
+
let dispatched = false;
|
|
90
|
+
let timer;
|
|
91
|
+
const controller = new AbortController();
|
|
92
|
+
try {
|
|
93
|
+
if ("u" > typeof window && 'function' == typeof window.dispatchEvent && "u" > typeof CustomEvent) {
|
|
94
|
+
window.dispatchEvent(new CustomEvent(payload.eventName, {
|
|
95
|
+
detail: payload
|
|
96
|
+
}));
|
|
97
|
+
dispatched = true;
|
|
98
|
+
}
|
|
99
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
100
|
+
if (!(true === options.postSignal || options.endpoint) || 'function' != typeof fetchImpl) return {
|
|
101
|
+
dispatched,
|
|
102
|
+
posted: false
|
|
103
|
+
};
|
|
104
|
+
const headers = new Headers({
|
|
105
|
+
'content-type': 'application/json'
|
|
106
|
+
});
|
|
107
|
+
if (options.authToken) headers.set(options.authHeaderName ?? DEFAULT_RUNTIME_FALLBACK_SIGNAL_AUTH_HEADER, options.authToken);
|
|
108
|
+
const timeoutMs = Number.isSafeInteger(options.timeoutMs) && (options.timeoutMs ?? 0) > 0 ? options.timeoutMs : 1000;
|
|
109
|
+
const expired = new Promise((resolve)=>{
|
|
110
|
+
timer = setTimeout(()=>{
|
|
111
|
+
controller.abort();
|
|
112
|
+
resolve(void 0);
|
|
113
|
+
}, timeoutMs);
|
|
114
|
+
});
|
|
115
|
+
const response = await Promise.race([
|
|
116
|
+
fetchImpl(options.endpoint ?? DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, {
|
|
117
|
+
body: JSON.stringify(payload),
|
|
118
|
+
headers,
|
|
119
|
+
method: 'POST',
|
|
120
|
+
keepalive: true,
|
|
121
|
+
signal: controller.signal
|
|
122
|
+
}),
|
|
123
|
+
expired
|
|
124
|
+
]);
|
|
125
|
+
return response ? {
|
|
126
|
+
dispatched,
|
|
127
|
+
posted: true,
|
|
128
|
+
postStatus: response.status
|
|
129
|
+
} : {
|
|
130
|
+
dispatched,
|
|
131
|
+
posted: false
|
|
132
|
+
};
|
|
133
|
+
} catch {
|
|
134
|
+
return {
|
|
135
|
+
dispatched,
|
|
136
|
+
posted: false
|
|
137
|
+
};
|
|
138
|
+
} finally{
|
|
139
|
+
if (void 0 !== timer) clearTimeout(timer);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export { DEFAULT_RUNTIME_FALLBACK_SIGNAL_AUTH_HEADER, DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, MODULE_FEDERATION_FALLBACK_SIGNAL_EVENT, MODULE_FEDERATION_RECOVERY_SIGNAL_EVENT, ModuleFederationRemoteComponentContractError, ModuleFederationRemoteLoadError, ModuleFederationRemoteLoadTimeoutError, classifyModuleFederationFallback, createModuleFederationFallbackTelemetry, emitModuleFederationFallbackPayload, emitModuleFederationFallbackTelemetry, toModuleFederationFallbackAttributes };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "node:module";
|
|
2
|
-
import { classifyModuleFederationFallback, createModuleFederationFallbackTelemetry, emitModuleFederationFallbackTelemetry } from "./index.mjs";
|
|
3
2
|
import { createDiscoveryError, formatSurfaceRef, parseSurfaceRef, validateSurfaceRef } from "./surface-resolution-types.mjs";
|
|
3
|
+
import { classifyModuleFederationFallback, createModuleFederationFallbackTelemetry, emitModuleFederationFallbackPayload, emitModuleFederationFallbackTelemetry } from "./telemetry.mjs";
|
|
4
4
|
function classifyDiscoveryError(code) {
|
|
5
5
|
switch(code){
|
|
6
6
|
case 'major-not-published':
|
|
@@ -39,17 +39,7 @@ async function reportFailure(options, ref, base) {
|
|
|
39
39
|
remote,
|
|
40
40
|
status: 'degraded'
|
|
41
41
|
});
|
|
42
|
-
|
|
43
|
-
await emitModuleFederationFallbackTelemetry({
|
|
44
|
-
appName: options.appName,
|
|
45
|
-
classification: base.classification,
|
|
46
|
-
error: base.error ?? base.discoveryError,
|
|
47
|
-
metadata: telemetry.metadata,
|
|
48
|
-
phase: base.phase,
|
|
49
|
-
remote,
|
|
50
|
-
status: 'degraded'
|
|
51
|
-
}, options.telemetry ?? {});
|
|
52
|
-
} catch {}
|
|
42
|
+
emitModuleFederationFallbackPayload(telemetry, options.telemetry);
|
|
53
43
|
const failure = {
|
|
54
44
|
ref,
|
|
55
45
|
env: options.env,
|
|
@@ -69,7 +59,7 @@ async function reportFailure(options, ref, base) {
|
|
|
69
59
|
degradedValue = await options.degraded(failure);
|
|
70
60
|
} catch (handlerError) {
|
|
71
61
|
try {
|
|
72
|
-
|
|
62
|
+
emitModuleFederationFallbackTelemetry({
|
|
73
63
|
appName: options.appName,
|
|
74
64
|
classification: base.classification,
|
|
75
65
|
error: handlerError,
|
|
@@ -63,7 +63,12 @@ function DistributedSsrBoundary({ children, expose, fallback, fragmentProps, rem
|
|
|
63
63
|
function createDistributedSsrComponent({ createComponent, expose, fallback, remote }) {
|
|
64
64
|
let RemoteComponent;
|
|
65
65
|
function DeferredRemoteComponent(props) {
|
|
66
|
-
RemoteComponent ??= createComponent(
|
|
66
|
+
RemoteComponent ??= createComponent({
|
|
67
|
+
export: 'default',
|
|
68
|
+
fallback,
|
|
69
|
+
injectLink: false,
|
|
70
|
+
loading: null
|
|
71
|
+
});
|
|
67
72
|
return /*#__PURE__*/ jsx(RemoteComponent, {
|
|
68
73
|
...props
|
|
69
74
|
});
|
|
@@ -1,116 +1,6 @@
|
|
|
1
1
|
import "node:module";
|
|
2
2
|
export * from "./consume-surface.mjs";
|
|
3
|
-
export * from "./distributed-ssr.mjs";
|
|
4
3
|
export * from "./last-known-good.mjs";
|
|
5
4
|
export * from "./manifest-recovery-runtime-plugin.mjs";
|
|
6
5
|
export * from "./surface-resolution-types.mjs";
|
|
7
|
-
|
|
8
|
-
const MODULE_FEDERATION_RECOVERY_SIGNAL_EVENT = 'modernjs:mf-runtime-recovery';
|
|
9
|
-
const DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT = '/_modern/contract-gates/runtime-fallback';
|
|
10
|
-
const DEFAULT_RUNTIME_FALLBACK_SIGNAL_AUTH_HEADER = 'x-modernjs-runtime-signal-token';
|
|
11
|
-
class ModuleFederationRemoteLoadTimeoutError extends Error {
|
|
12
|
-
constructor(remote, timeoutMs){
|
|
13
|
-
super(`Loading remote "${remote}" timed out after ${timeoutMs}ms`);
|
|
14
|
-
this.name = 'ModuleFederationRemoteLoadTimeoutError';
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
class ModuleFederationRemoteLoadError extends Error {
|
|
18
|
-
constructor(remote, attempts, causeError){
|
|
19
|
-
super(`Unable to load remote "${remote}" after ${attempts} attempt${attempts > 1 ? 's' : ''}: ${causeError.message}`);
|
|
20
|
-
this.name = 'ModuleFederationRemoteLoadError';
|
|
21
|
-
this.remote = remote;
|
|
22
|
-
this.attempts = attempts;
|
|
23
|
-
this.causeError = causeError;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
class ModuleFederationRemoteComponentContractError extends Error {
|
|
27
|
-
constructor(remote, exportName){
|
|
28
|
-
super(`Remote "${remote}" export "${exportName}" is not a valid React component`);
|
|
29
|
-
this.name = 'ModuleFederationRemoteComponentContractError';
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function toError(error) {
|
|
33
|
-
if (error instanceof Error) return error;
|
|
34
|
-
return new Error('string' == typeof error ? error : 'Unknown remote load error');
|
|
35
|
-
}
|
|
36
|
-
function classifyModuleFederationFallback(error) {
|
|
37
|
-
const normalizedError = toError(error);
|
|
38
|
-
if (normalizedError instanceof ModuleFederationRemoteLoadError) return classifyModuleFederationFallback(normalizedError.causeError);
|
|
39
|
-
if (normalizedError instanceof ModuleFederationRemoteLoadTimeoutError) return 'timeout';
|
|
40
|
-
if (normalizedError instanceof ModuleFederationRemoteComponentContractError) return 'contract';
|
|
41
|
-
const message = normalizedError.message;
|
|
42
|
-
if (/version|requiredVersion|singleton|share scope|shared module/i.test(message)) return 'version-skew';
|
|
43
|
-
if (/network|fetch|script|timeout|chunk|loading/i.test(message)) return 'network';
|
|
44
|
-
return 'remote-unavailable';
|
|
45
|
-
}
|
|
46
|
-
function createModuleFederationFallbackTelemetry(input) {
|
|
47
|
-
const error = void 0 !== input.error ? toError(input.error) : void 0;
|
|
48
|
-
const status = input.status ?? 'degraded';
|
|
49
|
-
const eventName = input.eventName ?? ('recovered' === status ? MODULE_FEDERATION_RECOVERY_SIGNAL_EVENT : MODULE_FEDERATION_FALLBACK_SIGNAL_EVENT);
|
|
50
|
-
const metadata = {
|
|
51
|
-
...input.metadata ?? {},
|
|
52
|
-
classification: input.classification,
|
|
53
|
-
remote: input.remote,
|
|
54
|
-
status
|
|
55
|
-
};
|
|
56
|
-
if (void 0 !== input.exportName) metadata.exportName = input.exportName;
|
|
57
|
-
if (void 0 !== input.runtimeDigest) metadata.runtimeDigest = input.runtimeDigest;
|
|
58
|
-
if (void 0 !== error) {
|
|
59
|
-
metadata.errorName = error.name;
|
|
60
|
-
metadata.errorMessage = error.message;
|
|
61
|
-
}
|
|
62
|
-
const payload = {
|
|
63
|
-
appName: input.appName,
|
|
64
|
-
eventName,
|
|
65
|
-
metadata,
|
|
66
|
-
phase: input.phase,
|
|
67
|
-
reason: input.classification,
|
|
68
|
-
schemaVersion: 1
|
|
69
|
-
};
|
|
70
|
-
if (void 0 !== input.entry) payload.entry = input.entry;
|
|
71
|
-
if (void 0 !== input.runtimeDigest) payload.runtimeDigest = input.runtimeDigest;
|
|
72
|
-
return payload;
|
|
73
|
-
}
|
|
74
|
-
function toModuleFederationFallbackAttributes(payload) {
|
|
75
|
-
return {
|
|
76
|
-
'data-mf-fallback-app': payload.appName,
|
|
77
|
-
'data-mf-fallback-classification': payload.reason,
|
|
78
|
-
'data-mf-fallback-phase': payload.phase,
|
|
79
|
-
'data-mf-fallback-remote': String(payload.metadata.remote),
|
|
80
|
-
'data-mf-fallback-status': String(payload.metadata.status),
|
|
81
|
-
'data-mf-telemetry-event': payload.eventName
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
async function emitModuleFederationFallbackTelemetry(input, options = {}) {
|
|
85
|
-
const payload = createModuleFederationFallbackTelemetry(input);
|
|
86
|
-
if ("u" > typeof window && 'function' == typeof window.dispatchEvent && "u" > typeof CustomEvent) window.dispatchEvent(new CustomEvent(payload.eventName, {
|
|
87
|
-
detail: payload
|
|
88
|
-
}));
|
|
89
|
-
const shouldPost = true === options.postSignal || Boolean(options.endpoint);
|
|
90
|
-
if (!shouldPost) return {
|
|
91
|
-
dispatched: true,
|
|
92
|
-
posted: false
|
|
93
|
-
};
|
|
94
|
-
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
95
|
-
if ('function' != typeof fetchImpl) return {
|
|
96
|
-
dispatched: true,
|
|
97
|
-
posted: false
|
|
98
|
-
};
|
|
99
|
-
const headers = new Headers({
|
|
100
|
-
'content-type': 'application/json'
|
|
101
|
-
});
|
|
102
|
-
if (void 0 !== options.authToken && options.authToken.length > 0) headers.set(options.authHeaderName ?? DEFAULT_RUNTIME_FALLBACK_SIGNAL_AUTH_HEADER, options.authToken);
|
|
103
|
-
const response = await fetchImpl(options.endpoint ?? DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, {
|
|
104
|
-
body: JSON.stringify(payload),
|
|
105
|
-
headers,
|
|
106
|
-
method: 'POST',
|
|
107
|
-
keepalive: true
|
|
108
|
-
});
|
|
109
|
-
return {
|
|
110
|
-
dispatched: true,
|
|
111
|
-
posted: true,
|
|
112
|
-
postStatus: response.status
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
export { DISTRIBUTED_SSR_FRAGMENT_REQUEST_LOCALS_KEY, useDistributedSsrFragmentProps } from "./distributed-ssr.mjs";
|
|
116
|
-
export { DEFAULT_RUNTIME_FALLBACK_SIGNAL_AUTH_HEADER, DEFAULT_RUNTIME_FALLBACK_SIGNAL_ENDPOINT, MODULE_FEDERATION_FALLBACK_SIGNAL_EVENT, MODULE_FEDERATION_RECOVERY_SIGNAL_EVENT, ModuleFederationRemoteComponentContractError, ModuleFederationRemoteLoadError, ModuleFederationRemoteLoadTimeoutError, classifyModuleFederationFallback, createModuleFederationFallbackTelemetry, emitModuleFederationFallbackTelemetry, toModuleFederationFallbackAttributes };
|
|
6
|
+
export * from "./telemetry.mjs";
|
|
@@ -32,7 +32,7 @@ const getErrorStatus = (error)=>{
|
|
|
32
32
|
if ('object' == typeof error && null !== error && 'status' in error && 'number' == typeof error.status) return error.status;
|
|
33
33
|
};
|
|
34
34
|
const isRetryableError = (error)=>{
|
|
35
|
-
if (error instanceof SyntaxError) return
|
|
35
|
+
if (error instanceof SyntaxError) return true;
|
|
36
36
|
const message = getErrorMessage(error);
|
|
37
37
|
if (NON_RETRYABLE_ERROR_PATTERNS.some((pattern)=>pattern.test(message))) return false;
|
|
38
38
|
const status = getErrorStatus(error);
|