@bleedingdev/modern-js-federation-runtime 3.9.3-ultramodern.8 → 3.9.3-ultramodern.9
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/native-browser-dev-client.js +320 -0
- package/dist/cjs/module-federation/native-browser-dev-refresh.js +226 -0
- package/dist/cjs/module-federation/native-browser-dev-runtime-plugin.js +233 -0
- package/dist/cjs/module-federation/native-dev-refresh-runtime-plugin.js +267 -0
- package/dist/cjs/module-federation/renderer-runtime-plugin.js +5 -2
- package/dist/esm/module-federation/native-browser-dev-client.mjs +276 -0
- package/dist/esm/module-federation/native-browser-dev-refresh.mjs +182 -0
- package/dist/esm/module-federation/native-browser-dev-runtime-plugin.mjs +183 -0
- package/dist/esm/module-federation/native-dev-refresh-runtime-plugin.mjs +229 -0
- package/dist/esm/module-federation/renderer-runtime-plugin.mjs +5 -2
- package/dist/esm-node/module-federation/native-browser-dev-client.mjs +277 -0
- package/dist/esm-node/module-federation/native-browser-dev-refresh.mjs +183 -0
- package/dist/esm-node/module-federation/native-browser-dev-runtime-plugin.mjs +184 -0
- package/dist/esm-node/module-federation/native-dev-refresh-runtime-plugin.mjs +230 -0
- package/dist/esm-node/module-federation/renderer-runtime-plugin.mjs +5 -2
- package/dist/types/module-federation/native-browser-dev-client.d.ts +41 -0
- package/dist/types/module-federation/native-browser-dev-refresh.d.ts +86 -0
- package/dist/types/module-federation/native-browser-dev-runtime-plugin.d.ts +40 -0
- package/dist/types/module-federation/native-dev-refresh-runtime-plugin.d.ts +90 -0
- package/package.json +54 -7
- package/src/module-federation/native-browser-dev-client.ts +424 -0
- package/src/module-federation/native-browser-dev-refresh.ts +373 -0
- package/src/module-federation/native-browser-dev-runtime-plugin.ts +276 -0
- package/src/module-federation/native-dev-refresh-runtime-plugin.ts +445 -0
- package/src/module-federation/renderer-runtime-plugin.ts +7 -2
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { isNativeBrowserDevCommitError } from "./native-browser-dev-refresh.mjs";
|
|
2
|
+
import { hasNativeBrowserDevPublisher, readNativeBrowserDevPublication, replaceNativeBrowserDevRemote } from "./native-browser-dev-runtime-plugin.mjs";
|
|
3
|
+
const CLIENTS = Symbol.for('ultramodern.federation.browser-dev-clients.v1');
|
|
4
|
+
const clients = ()=>{
|
|
5
|
+
const root = globalThis;
|
|
6
|
+
if (!root[CLIENTS]) Object.defineProperty(root, CLIENTS, {
|
|
7
|
+
value: new Map()
|
|
8
|
+
});
|
|
9
|
+
return root[CLIENTS];
|
|
10
|
+
};
|
|
11
|
+
function commitClient(remoteName, graph) {
|
|
12
|
+
const state = clients().get(remoteName);
|
|
13
|
+
const next = state?.candidates.get(graph.runtimeHash)?.deref();
|
|
14
|
+
if (!state || !next) throw new Error('Accepted native graph has no matching fresh HMR runtime.');
|
|
15
|
+
if (state.active === next) return;
|
|
16
|
+
state.active?.stop();
|
|
17
|
+
state.active = next;
|
|
18
|
+
next.start();
|
|
19
|
+
}
|
|
20
|
+
function prepareNativeBrowserDevClient(remoteName, manifest) {
|
|
21
|
+
const hash = /\/__ultramodern_mf__\/([a-f0-9]+)\//u.exec(new URL(manifest).pathname)?.[1];
|
|
22
|
+
const candidate = hash && clients().get(remoteName)?.candidates.get(hash)?.deref();
|
|
23
|
+
if (!candidate) throw new Error('Native graph has no loaded standby HMR runtime.');
|
|
24
|
+
return candidate.lease();
|
|
25
|
+
}
|
|
26
|
+
function releaseNativeBrowserDevClient(remoteName, manifest) {
|
|
27
|
+
const hash = /\/__ultramodern_mf__\/([a-f0-9]+)\//u.exec(new URL(manifest).pathname)?.[1];
|
|
28
|
+
if (!hash) return;
|
|
29
|
+
const state = clients().get(remoteName);
|
|
30
|
+
const candidate = state?.candidates.get(hash)?.deref();
|
|
31
|
+
if (!state || !candidate) return;
|
|
32
|
+
candidate.releaseStandby();
|
|
33
|
+
}
|
|
34
|
+
function startNativeBrowserDevClient(options) {
|
|
35
|
+
const state = clients().get(options.remoteName) ?? {
|
|
36
|
+
candidates: new Map()
|
|
37
|
+
};
|
|
38
|
+
clients().set(options.remoteName, state);
|
|
39
|
+
const initialHash = options.hash();
|
|
40
|
+
let socket;
|
|
41
|
+
let ping;
|
|
42
|
+
let reconnect;
|
|
43
|
+
let reconnectCount = 0;
|
|
44
|
+
let lastHash;
|
|
45
|
+
let running = false;
|
|
46
|
+
let applying;
|
|
47
|
+
let lifetime;
|
|
48
|
+
let removeCustom;
|
|
49
|
+
const custom = new Map();
|
|
50
|
+
const queued = [];
|
|
51
|
+
const leases = new Set();
|
|
52
|
+
const base = options.serverBase.endsWith('/') ? options.serverBase : `${options.serverBase}/`;
|
|
53
|
+
const report = (error)=>{
|
|
54
|
+
console.error('[ultramodern] Native federation development update failed:', error);
|
|
55
|
+
if (false !== options.config.overlay && ('object' != typeof options.config.overlay || false !== options.config.overlay.errors)) options.overlay()?.create('Native federation update failed', String(error));
|
|
56
|
+
};
|
|
57
|
+
const send = (message)=>{
|
|
58
|
+
if (socket?.readyState === WebSocket.OPEN) socket.send(JSON.stringify(message));
|
|
59
|
+
else queued.push(message);
|
|
60
|
+
};
|
|
61
|
+
const errorMessage = (error)=>error instanceof Error ? error.message : String(error);
|
|
62
|
+
const onError = (event)=>send({
|
|
63
|
+
type: 'client-error',
|
|
64
|
+
id: crypto.randomUUID(),
|
|
65
|
+
message: event.message,
|
|
66
|
+
name: event.error?.name,
|
|
67
|
+
stack: event.error?.stack
|
|
68
|
+
});
|
|
69
|
+
const onRejected = (event)=>send({
|
|
70
|
+
type: 'client-error',
|
|
71
|
+
id: crypto.randomUUID(),
|
|
72
|
+
message: `Uncaught (in promise) ${errorMessage(event.reason)}`,
|
|
73
|
+
name: event.reason?.name,
|
|
74
|
+
stack: event.reason?.stack
|
|
75
|
+
});
|
|
76
|
+
const fullReload = (data)=>{
|
|
77
|
+
if (!options.liveReload) return;
|
|
78
|
+
if (data?.path?.endsWith('.html')) {
|
|
79
|
+
const pathname = decodeURI(location.pathname);
|
|
80
|
+
const target = base + data.path.slice(1);
|
|
81
|
+
if (pathname !== target && pathname !== target.slice(0, -5) && !(pathname.endsWith('/') && `${pathname}index.html` === target)) return;
|
|
82
|
+
}
|
|
83
|
+
location.reload();
|
|
84
|
+
};
|
|
85
|
+
const apply = ()=>{
|
|
86
|
+
if (!running || applying || !lastHash || lastHash === options.hash()) return;
|
|
87
|
+
const signal = lifetime.signal;
|
|
88
|
+
const targetHash = lastHash;
|
|
89
|
+
applying = (async ()=>{
|
|
90
|
+
if (hasNativeBrowserDevPublisher(options.remoteName)) {
|
|
91
|
+
const graph = await readNativeBrowserDevPublication(options.remoteName, {
|
|
92
|
+
signal: AbortSignal.any([
|
|
93
|
+
signal,
|
|
94
|
+
AbortSignal.timeout(3000)
|
|
95
|
+
]),
|
|
96
|
+
revisionHeader: options.revisionHeader
|
|
97
|
+
});
|
|
98
|
+
if (signal.aborted) throw signal.reason;
|
|
99
|
+
if (graph.buildId !== options.buildId) {
|
|
100
|
+
try {
|
|
101
|
+
await replaceNativeBrowserDevRemote(graph);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (running && !signal.aborted && hasNativeBrowserDevPublisher(options.remoteName) && isNativeBrowserDevCommitError(error, graph.revision)) {
|
|
104
|
+
state.failure = {
|
|
105
|
+
runtimeHash: graph.runtimeHash,
|
|
106
|
+
error
|
|
107
|
+
};
|
|
108
|
+
report(error);
|
|
109
|
+
commitClient(options.remoteName, graph);
|
|
110
|
+
}
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
if (!running || signal.aborted || !hasNativeBrowserDevPublisher(options.remoteName)) return;
|
|
114
|
+
commitClient(options.remoteName, graph);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (!options.hot) return fullReload();
|
|
119
|
+
if ('idle' !== options.hot.status()) return;
|
|
120
|
+
try {
|
|
121
|
+
const updated = await options.hot.check(true);
|
|
122
|
+
if (!updated) fullReload();
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.error('[rsbuild] HMR update failed, performing full reload:', error);
|
|
125
|
+
fullReload();
|
|
126
|
+
}
|
|
127
|
+
})().catch((error)=>{
|
|
128
|
+
if (!signal.aborted) report(error);
|
|
129
|
+
}).finally(()=>{
|
|
130
|
+
applying = void 0;
|
|
131
|
+
if (running && !signal.aborted && lastHash !== targetHash) apply();
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
const clearOverlayForBuild = ()=>{
|
|
135
|
+
if (state.failure) {
|
|
136
|
+
if (!lastHash || lastHash === state.failure.runtimeHash) return;
|
|
137
|
+
state.failure = void 0;
|
|
138
|
+
}
|
|
139
|
+
options.overlay()?.clear();
|
|
140
|
+
};
|
|
141
|
+
const message = (event)=>{
|
|
142
|
+
let value;
|
|
143
|
+
try {
|
|
144
|
+
value = JSON.parse(event.data);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
report(error);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
switch(value.type){
|
|
150
|
+
case 'hash':
|
|
151
|
+
lastHash = value.data;
|
|
152
|
+
break;
|
|
153
|
+
case 'ok':
|
|
154
|
+
clearOverlayForBuild();
|
|
155
|
+
apply();
|
|
156
|
+
break;
|
|
157
|
+
case 'warnings':
|
|
158
|
+
for (const warning of value.data?.text ?? [])console.warn(warning);
|
|
159
|
+
clearOverlayForBuild();
|
|
160
|
+
apply();
|
|
161
|
+
break;
|
|
162
|
+
case 'errors':
|
|
163
|
+
for (const error of value.data?.text ?? [])console.error(error);
|
|
164
|
+
if (value.data?.html) options.overlay()?.create('Build failed', value.data.html);
|
|
165
|
+
break;
|
|
166
|
+
case 'full-reload':
|
|
167
|
+
fullReload(value.data);
|
|
168
|
+
break;
|
|
169
|
+
case 'static-changed':
|
|
170
|
+
fullReload();
|
|
171
|
+
break;
|
|
172
|
+
case 'custom':
|
|
173
|
+
for (const listener of custom.get(value.data?.event) ?? [])listener(value.data.data);
|
|
174
|
+
break;
|
|
175
|
+
case 'resolved-client-error':
|
|
176
|
+
options.overlay()?.create('Runtime errors', String(value.data?.message ?? ''));
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const socketUrl = (fallback)=>{
|
|
181
|
+
const url = new URL('http://localhost');
|
|
182
|
+
url.hostname = (fallback ? options.serverHost : options.config.host) || location.hostname;
|
|
183
|
+
url.port = String((fallback ? options.serverPort : options.config.port) || location.port);
|
|
184
|
+
url.protocol = options.config.protocol || ('https:' === location.protocol ? 'wss:' : 'ws:');
|
|
185
|
+
url.pathname = options.config.path;
|
|
186
|
+
url.searchParams.set('token', options.token);
|
|
187
|
+
return options.resolveWebSocketUrl?.(url.href) ?? url.href;
|
|
188
|
+
};
|
|
189
|
+
const detachSocket = ()=>{
|
|
190
|
+
clearInterval(ping);
|
|
191
|
+
ping = void 0;
|
|
192
|
+
const previous = socket;
|
|
193
|
+
socket = void 0;
|
|
194
|
+
if (!previous) return;
|
|
195
|
+
previous.onopen = previous.onclose = previous.onmessage = previous.onerror = null;
|
|
196
|
+
previous.close();
|
|
197
|
+
};
|
|
198
|
+
const connect = (fallback = false)=>{
|
|
199
|
+
if (!running) return;
|
|
200
|
+
socket = new WebSocket(socketUrl(fallback));
|
|
201
|
+
socket.onopen = ()=>{
|
|
202
|
+
reconnectCount = 0;
|
|
203
|
+
ping = setInterval(()=>send({
|
|
204
|
+
type: 'ping'
|
|
205
|
+
}), 30000);
|
|
206
|
+
for (const item of queued.splice(0))send(item);
|
|
207
|
+
};
|
|
208
|
+
socket.onmessage = message;
|
|
209
|
+
socket.onclose = ()=>{
|
|
210
|
+
detachSocket();
|
|
211
|
+
if (!running || reconnectCount >= options.config.reconnect) return;
|
|
212
|
+
reconnectCount++;
|
|
213
|
+
reconnect = setTimeout(()=>connect(), 1000 * 1.5 ** reconnectCount);
|
|
214
|
+
};
|
|
215
|
+
socket.onerror = ()=>{
|
|
216
|
+
if (options.resolveWebSocketUrl || fallback || socketUrl(false) === socketUrl(true)) return;
|
|
217
|
+
detachSocket();
|
|
218
|
+
connect(true);
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
const client = {
|
|
222
|
+
arm () {
|
|
223
|
+
removeCustom ??= options.installCustomListeners(custom);
|
|
224
|
+
},
|
|
225
|
+
lease () {
|
|
226
|
+
const lease = {};
|
|
227
|
+
leases.add(lease);
|
|
228
|
+
client.arm();
|
|
229
|
+
return ()=>{
|
|
230
|
+
if (!leases.delete(lease)) return;
|
|
231
|
+
client.releaseStandby();
|
|
232
|
+
};
|
|
233
|
+
},
|
|
234
|
+
releaseStandby () {
|
|
235
|
+
if (state.active !== client && !leases.size) client.stop();
|
|
236
|
+
},
|
|
237
|
+
start () {
|
|
238
|
+
if (running) return;
|
|
239
|
+
running = true;
|
|
240
|
+
lifetime = new AbortController();
|
|
241
|
+
client.arm();
|
|
242
|
+
if (options.browserLogs) {
|
|
243
|
+
window.addEventListener('error', onError);
|
|
244
|
+
window.addEventListener('unhandledrejection', onRejected);
|
|
245
|
+
}
|
|
246
|
+
connect();
|
|
247
|
+
},
|
|
248
|
+
stop () {
|
|
249
|
+
if (!running && !removeCustom) return;
|
|
250
|
+
running = false;
|
|
251
|
+
lifetime?.abort(new Error('Native browser HMR runtime retired.'));
|
|
252
|
+
clearTimeout(reconnect);
|
|
253
|
+
reconnect = void 0;
|
|
254
|
+
detachSocket();
|
|
255
|
+
window.removeEventListener('error', onError);
|
|
256
|
+
window.removeEventListener('unhandledrejection', onRejected);
|
|
257
|
+
removeCustom?.();
|
|
258
|
+
removeCustom = void 0;
|
|
259
|
+
custom.clear();
|
|
260
|
+
queued.length = 0;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
client.arm();
|
|
264
|
+
state.candidates.set(initialHash, new WeakRef(client));
|
|
265
|
+
if (!state.active) {
|
|
266
|
+
state.active = client;
|
|
267
|
+
client.start();
|
|
268
|
+
}
|
|
269
|
+
return ()=>{
|
|
270
|
+
client.stop();
|
|
271
|
+
if (state.active === client) state.active = void 0;
|
|
272
|
+
if (state.candidates.get(initialHash)?.deref() === client) state.candidates.delete(initialHash);
|
|
273
|
+
if (!state.active && !state.candidates.size) clients().delete(options.remoteName);
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
export { prepareNativeBrowserDevClient, releaseNativeBrowserDevClient, startNativeBrowserDevClient };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
const COMMIT_ERROR = Symbol.for('ultramodern.federation.browser-dev-commit-error.v1');
|
|
2
|
+
class NativeBrowserDevCommitError extends Error {
|
|
3
|
+
constructor(revision, cause){
|
|
4
|
+
super(`Native browser refresh failed after ownership transfer began: ${cause instanceof Error ? cause.message : String(cause)}`, {
|
|
5
|
+
cause
|
|
6
|
+
}), this[COMMIT_ERROR] = true;
|
|
7
|
+
this.name = 'NativeBrowserDevCommitError';
|
|
8
|
+
Object.defineProperty(this, 'revision', {
|
|
9
|
+
value: revision,
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function isNativeBrowserDevCommitError(error, revision) {
|
|
15
|
+
return error instanceof Error && true === Reflect.get(error, COMMIT_ERROR) && Reflect.get(error, 'revision') === revision;
|
|
16
|
+
}
|
|
17
|
+
function nativeBrowserDevCommitFailure(revision, cause) {
|
|
18
|
+
return new NativeBrowserDevCommitError(revision, cause);
|
|
19
|
+
}
|
|
20
|
+
const selectedShares = (host)=>Object.entries(host.shareScopeMap).flatMap(([scope, packages])=>Object.entries(packages).flatMap(([name, versions])=>Object.entries(versions).filter(([, provider])=>provider.loaded || provider.loading).map(([version, provider])=>({
|
|
21
|
+
scope,
|
|
22
|
+
name,
|
|
23
|
+
version,
|
|
24
|
+
provider,
|
|
25
|
+
lib: provider.lib,
|
|
26
|
+
get: provider.get
|
|
27
|
+
}))));
|
|
28
|
+
function assertSelectedShares(host, shares) {
|
|
29
|
+
for (const share of shares){
|
|
30
|
+
const provider = host.shareScopeMap[share.scope]?.[share.name]?.[share.version];
|
|
31
|
+
if (provider !== share.provider || provider.lib !== share.lib || provider.get !== share.get) throw new Error(`Native browser refresh changed the selected shared provider ${share.name}.`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function createNativeBrowserDevRefresh(options) {
|
|
35
|
+
let active;
|
|
36
|
+
let pending;
|
|
37
|
+
let disposed = false;
|
|
38
|
+
const consumed = new Set();
|
|
39
|
+
const recovery = new Set();
|
|
40
|
+
const released = new WeakSet();
|
|
41
|
+
const release = (stage)=>{
|
|
42
|
+
if (released.has(stage)) return;
|
|
43
|
+
released.add(stage);
|
|
44
|
+
try {
|
|
45
|
+
stage.release();
|
|
46
|
+
} finally{
|
|
47
|
+
stage.host.moduleCache.clear();
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
replace (graph) {
|
|
52
|
+
if (disposed) return Promise.reject(new Error('Native browser refresh is disposed.'));
|
|
53
|
+
if (pending) return Promise.reject(new Error('Native browser refresh already has an active replacement.'));
|
|
54
|
+
if (!/^[a-f0-9]{64}$/u.test(graph.revision)) return Promise.reject(new Error('Native browser graph revision is invalid.'));
|
|
55
|
+
if (consumed.has(graph.revision)) return Promise.reject(new Error('Native browser graph was already consumed; rebuild before retrying.'));
|
|
56
|
+
const commitErrors = new WeakSet();
|
|
57
|
+
const task = (async ()=>{
|
|
58
|
+
const host = options.host;
|
|
59
|
+
const shares = selectedShares(host);
|
|
60
|
+
const replacement = options.prepareReplacement(options.boundary);
|
|
61
|
+
const stage = options.createHost({
|
|
62
|
+
...host.options,
|
|
63
|
+
name: `${host.name}:native-dev:${graph.revision}`,
|
|
64
|
+
id: `${host.name}:native-dev:${graph.revision}`,
|
|
65
|
+
remotes: [
|
|
66
|
+
{
|
|
67
|
+
...graph.remote
|
|
68
|
+
}
|
|
69
|
+
],
|
|
70
|
+
shared: {}
|
|
71
|
+
});
|
|
72
|
+
if (stage.host === host || stage.host === active?.host) throw new Error('Native browser refresh requires an isolated staging host.');
|
|
73
|
+
let accepted = false;
|
|
74
|
+
let module;
|
|
75
|
+
let failed = false;
|
|
76
|
+
let failure;
|
|
77
|
+
try {
|
|
78
|
+
for (const [name, scope] of Object.entries(host.shareScopeMap)){
|
|
79
|
+
stage.host.initShareScopeMap(name, scope, {
|
|
80
|
+
hostShareScopeMap: host.shareScopeMap
|
|
81
|
+
});
|
|
82
|
+
if (stage.host.shareScopeMap[name] !== scope) throw new Error('Native browser refresh lost the actual shared scope.');
|
|
83
|
+
}
|
|
84
|
+
const factory = await stage.host.loadRemote(options.id, {
|
|
85
|
+
loadFactory: false
|
|
86
|
+
});
|
|
87
|
+
if (disposed) throw new Error('Native browser refresh was disposed while loading.');
|
|
88
|
+
if ('function' != typeof factory) throw new Error('Native browser refresh did not receive an exposed factory.');
|
|
89
|
+
assertSelectedShares(host, shares);
|
|
90
|
+
stage.prepareFactory?.();
|
|
91
|
+
consumed.add(graph.revision);
|
|
92
|
+
try {
|
|
93
|
+
module = replacement.execute(factory);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (replacement.commitStarted) {
|
|
96
|
+
if (active) recovery.add(active);
|
|
97
|
+
active = stage;
|
|
98
|
+
accepted = true;
|
|
99
|
+
}
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
const previous = active;
|
|
103
|
+
active = stage;
|
|
104
|
+
accepted = true;
|
|
105
|
+
assertSelectedShares(host, shares);
|
|
106
|
+
const retired = new Set(recovery);
|
|
107
|
+
recovery.clear();
|
|
108
|
+
if (previous) retired.add(previous);
|
|
109
|
+
const errors = [];
|
|
110
|
+
for (const retained of retired)try {
|
|
111
|
+
release(retained);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
errors.push(error);
|
|
114
|
+
}
|
|
115
|
+
if (errors.length) throw new AggregateError(errors, 'Native browser refresh retirement failed.');
|
|
116
|
+
} catch (error) {
|
|
117
|
+
failed = true;
|
|
118
|
+
failure = error;
|
|
119
|
+
}
|
|
120
|
+
const retired = new Set();
|
|
121
|
+
if (disposed && active === stage) {
|
|
122
|
+
active = void 0;
|
|
123
|
+
for (const retained of recovery)retired.add(retained);
|
|
124
|
+
recovery.clear();
|
|
125
|
+
retired.add(stage);
|
|
126
|
+
} else if (!accepted) retired.add(stage);
|
|
127
|
+
const cleanupErrors = [];
|
|
128
|
+
for (const retained of retired)try {
|
|
129
|
+
release(retained);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
cleanupErrors.push(error);
|
|
132
|
+
}
|
|
133
|
+
if (cleanupErrors.length) {
|
|
134
|
+
failure = new AggregateError(failed ? [
|
|
135
|
+
failure,
|
|
136
|
+
...cleanupErrors
|
|
137
|
+
] : cleanupErrors, 'Native browser refresh cleanup failed.');
|
|
138
|
+
failed = true;
|
|
139
|
+
}
|
|
140
|
+
if (failed) {
|
|
141
|
+
if (accepted) {
|
|
142
|
+
const committedFailure = nativeBrowserDevCommitFailure(graph.revision, failure);
|
|
143
|
+
commitErrors.add(committedFailure);
|
|
144
|
+
throw committedFailure;
|
|
145
|
+
}
|
|
146
|
+
throw failure;
|
|
147
|
+
}
|
|
148
|
+
return module;
|
|
149
|
+
})().catch((error)=>{
|
|
150
|
+
if (error instanceof Error && true === Reflect.get(error, COMMIT_ERROR) && !commitErrors.has(error)) throw new Error(`Native browser graph failed before its own ownership transfer: ${error.message}`, {
|
|
151
|
+
cause: error
|
|
152
|
+
});
|
|
153
|
+
throw error;
|
|
154
|
+
});
|
|
155
|
+
pending = task;
|
|
156
|
+
task.finally(()=>{
|
|
157
|
+
if (pending === task) pending = void 0;
|
|
158
|
+
}).catch(()=>{});
|
|
159
|
+
return task;
|
|
160
|
+
},
|
|
161
|
+
get activeHost () {
|
|
162
|
+
return active?.host ?? options.host;
|
|
163
|
+
},
|
|
164
|
+
dispose () {
|
|
165
|
+
if (disposed) return;
|
|
166
|
+
disposed = true;
|
|
167
|
+
const previous = active;
|
|
168
|
+
active = void 0;
|
|
169
|
+
const retained = new Set(recovery);
|
|
170
|
+
recovery.clear();
|
|
171
|
+
if (previous) retained.add(previous);
|
|
172
|
+
const errors = [];
|
|
173
|
+
for (const stage of retained)try {
|
|
174
|
+
release(stage);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
errors.push(error);
|
|
177
|
+
}
|
|
178
|
+
if (errors.length) throw new AggregateError(errors, 'Native browser refresh cleanup failed.');
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
export { createNativeBrowserDevRefresh, isNativeBrowserDevCommitError, nativeBrowserDevCommitFailure };
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { createNativeBrowserDevRefresh, isNativeBrowserDevCommitError, nativeBrowserDevCommitFailure } from "./native-browser-dev-refresh.mjs";
|
|
2
|
+
import { readRendererFederationContract } from "./renderer-contract.mjs";
|
|
3
|
+
const NATIVE_BROWSER_DEV_BOUNDARY = Symbol.for('ultramodern.federation.browser-dev-boundary.v1');
|
|
4
|
+
const REGISTRY = Symbol.for('ultramodern.federation.browser-dev-routing.v1');
|
|
5
|
+
function registry() {
|
|
6
|
+
const root = globalThis;
|
|
7
|
+
if (!root[REGISTRY]) Object.defineProperty(root, REGISTRY, {
|
|
8
|
+
value: {
|
|
9
|
+
sources: new WeakMap(),
|
|
10
|
+
bindings: new Set()
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
return root[REGISTRY];
|
|
14
|
+
}
|
|
15
|
+
function firstReference(binding) {
|
|
16
|
+
const reference = binding.references.values().next().value;
|
|
17
|
+
if (!reference) throw new Error('Native browser refresh has no mounted boundary owner.');
|
|
18
|
+
return reference;
|
|
19
|
+
}
|
|
20
|
+
function createNativeBrowserDevRuntimePlugin(options) {
|
|
21
|
+
return {
|
|
22
|
+
name: 'ultramodern-native-browser-dev-refresh',
|
|
23
|
+
beforeInit (args) {
|
|
24
|
+
const host = args.origin;
|
|
25
|
+
if (Object.hasOwn(host, NATIVE_BROWSER_DEV_BOUNDARY)) return args;
|
|
26
|
+
Object.defineProperty(host, NATIVE_BROWSER_DEV_BOUNDARY, {
|
|
27
|
+
value (id, source, boundary) {
|
|
28
|
+
const remote = options.remotes.find((remote)=>id.startsWith(`${remote.alias}/`));
|
|
29
|
+
if (!remote) throw new Error(`Native browser refresh has no registered remote for ${id}.`);
|
|
30
|
+
const state = registry();
|
|
31
|
+
let sources = state.sources.get(source);
|
|
32
|
+
if (!sources) state.sources.set(source, sources = new Map());
|
|
33
|
+
let binding = sources.get(id);
|
|
34
|
+
if (binding && binding.remoteName !== remote.name) throw new Error('Native browser refresh source has conflicting remote ownership.');
|
|
35
|
+
if (!binding) {
|
|
36
|
+
const references = new Set();
|
|
37
|
+
const current = ()=>firstReference(created);
|
|
38
|
+
const created = {
|
|
39
|
+
id,
|
|
40
|
+
remoteName: remote.name,
|
|
41
|
+
references,
|
|
42
|
+
coordinator: createNativeBrowserDevRefresh({
|
|
43
|
+
get host () {
|
|
44
|
+
return current().host;
|
|
45
|
+
},
|
|
46
|
+
id,
|
|
47
|
+
boundary: references,
|
|
48
|
+
prepareReplacement () {
|
|
49
|
+
const reference = current();
|
|
50
|
+
return reference.prepare(reference.boundary);
|
|
51
|
+
},
|
|
52
|
+
createHost: options.createHost
|
|
53
|
+
})
|
|
54
|
+
};
|
|
55
|
+
binding = created;
|
|
56
|
+
sources.set(id, binding);
|
|
57
|
+
state.bindings.add(binding);
|
|
58
|
+
}
|
|
59
|
+
const reference = {
|
|
60
|
+
host,
|
|
61
|
+
remote,
|
|
62
|
+
boundary,
|
|
63
|
+
prepare: options.prepareReplacement
|
|
64
|
+
};
|
|
65
|
+
binding.references.add(reference);
|
|
66
|
+
const owned = binding;
|
|
67
|
+
let released = false;
|
|
68
|
+
return ()=>{
|
|
69
|
+
if (released) return;
|
|
70
|
+
released = true;
|
|
71
|
+
owned.references.delete(reference);
|
|
72
|
+
if (owned.references.size) return;
|
|
73
|
+
sources.delete(id);
|
|
74
|
+
state.bindings.delete(owned);
|
|
75
|
+
owned.coordinator.dispose();
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return args;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
async function replaceNativeBrowserDevRemote(graph) {
|
|
84
|
+
const bindings = [
|
|
85
|
+
...registry().bindings
|
|
86
|
+
].filter((binding)=>binding.remoteName === graph.remote.name);
|
|
87
|
+
if (!bindings.length) throw new Error('Native browser refresh has no mounted publisher for this remote.');
|
|
88
|
+
let committed = 0;
|
|
89
|
+
try {
|
|
90
|
+
for (const binding of bindings){
|
|
91
|
+
await binding.coordinator.replace(graph);
|
|
92
|
+
committed++;
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (committed && !isNativeBrowserDevCommitError(error, graph.revision)) throw nativeBrowserDevCommitFailure(graph.revision, error);
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
return bindings.length;
|
|
99
|
+
}
|
|
100
|
+
const record = (value)=>null !== value && 'object' == typeof value && !Array.isArray(value);
|
|
101
|
+
async function readNativeBrowserDevPublication(remoteName, options) {
|
|
102
|
+
const binding = [
|
|
103
|
+
...registry().bindings
|
|
104
|
+
].find((binding)=>binding.remoteName === remoteName);
|
|
105
|
+
if (!binding) throw new Error('Native browser refresh has no mounted publisher for discovery.');
|
|
106
|
+
const reference = firstReference(binding);
|
|
107
|
+
const remote = reference.remote;
|
|
108
|
+
const host = reference.host;
|
|
109
|
+
const init = {
|
|
110
|
+
cache: 'no-store',
|
|
111
|
+
signal: options.signal
|
|
112
|
+
};
|
|
113
|
+
const context = {
|
|
114
|
+
initiator: 'loadRemote',
|
|
115
|
+
id: binding.id,
|
|
116
|
+
resourceType: 'manifest',
|
|
117
|
+
url: remote.entry
|
|
118
|
+
};
|
|
119
|
+
const hook = host.loaderHook?.lifecycle.fetch;
|
|
120
|
+
const request = (async ()=>{
|
|
121
|
+
const hooked = await hook?.emit(remote.entry, init, remote, context);
|
|
122
|
+
const response = hooked instanceof Response ? hooked : await globalThis.fetch(remote.entry, init);
|
|
123
|
+
if (options.signal.aborted) {
|
|
124
|
+
response.body?.cancel().catch(()=>{});
|
|
125
|
+
throw options.signal.reason;
|
|
126
|
+
}
|
|
127
|
+
if (!response.ok) {
|
|
128
|
+
response.body?.cancel().catch(()=>{});
|
|
129
|
+
throw new Error(`Native browser manifest discovery failed: ${response.status}`);
|
|
130
|
+
}
|
|
131
|
+
const manifest = await response.json();
|
|
132
|
+
const revision = response.headers.get(options.revisionHeader);
|
|
133
|
+
const links = [
|
|
134
|
+
...(response.headers.get('link') ?? '').matchAll(/<([^>]+)>\s*;([^,]*)/gu)
|
|
135
|
+
].filter(([, , parameters])=>/\brel\s*=\s*"?ultramodern-completed-graph"?(?:\s|;|$)/u.test(parameters));
|
|
136
|
+
if (!revision || !/^[a-f0-9]{64}$/u.test(revision) || 1 !== links.length) throw new Error('Native browser manifest has no unique completed graph coordinate.');
|
|
137
|
+
const entry = new URL(links[0][1], remote.entry);
|
|
138
|
+
if (![
|
|
139
|
+
'http:',
|
|
140
|
+
'https:'
|
|
141
|
+
].includes(entry.protocol) || entry.username || entry.password || entry.hash) throw new Error('Native browser completed graph URL is invalid.');
|
|
142
|
+
const namespace = /\/__ultramodern_mf__\/([a-f0-9]+)\//u.exec(entry.pathname);
|
|
143
|
+
if (!namespace) throw new Error('Native browser graph is not in the immutable runtime namespace.');
|
|
144
|
+
const metadata = record(manifest) && record(manifest.metaData) ? manifest.metaData.ultramodernRenderer : void 0;
|
|
145
|
+
const contract = readRendererFederationContract(metadata);
|
|
146
|
+
const buildIds = new Set(Object.values(contract.identities).map((identity)=>identity.buildId));
|
|
147
|
+
if (1 !== buildIds.size) throw new Error('Native browser graph has conflicting renderer build identities.');
|
|
148
|
+
return {
|
|
149
|
+
revision,
|
|
150
|
+
runtimeHash: namespace[1],
|
|
151
|
+
buildId: [
|
|
152
|
+
...buildIds
|
|
153
|
+
][0],
|
|
154
|
+
remote: {
|
|
155
|
+
...remote,
|
|
156
|
+
entry: entry.href
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
})();
|
|
160
|
+
let removeAbort;
|
|
161
|
+
const aborted = new Promise((_resolve, reject)=>{
|
|
162
|
+
const abort = ()=>reject(options.signal.reason);
|
|
163
|
+
removeAbort = ()=>options.signal.removeEventListener('abort', abort);
|
|
164
|
+
if (options.signal.aborted) abort();
|
|
165
|
+
else options.signal.addEventListener('abort', abort, {
|
|
166
|
+
once: true
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
try {
|
|
170
|
+
return await Promise.race([
|
|
171
|
+
request,
|
|
172
|
+
aborted
|
|
173
|
+
]);
|
|
174
|
+
} finally{
|
|
175
|
+
removeAbort();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function hasNativeBrowserDevPublisher(remoteName) {
|
|
179
|
+
return [
|
|
180
|
+
...registry().bindings
|
|
181
|
+
].some((binding)=>binding.remoteName === remoteName);
|
|
182
|
+
}
|
|
183
|
+
export { NATIVE_BROWSER_DEV_BOUNDARY, createNativeBrowserDevRuntimePlugin, hasNativeBrowserDevPublisher, readNativeBrowserDevPublication, replaceNativeBrowserDevRemote };
|