@fulgurjs/federation 5.3.3 → 5.4.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/CHANGELOG.md +17 -0
- package/DESIGN.md +4 -0
- package/README.en.md +25 -3
- package/README.md +88 -1
- package/dist/bridge-app-react.cjs +14 -4
- package/dist/bridge-app-react.d.cts +16 -4
- package/dist/bridge-app-react.d.ts +16 -4
- package/dist/bridge-app-react.js +6 -4
- package/dist/bridge-app-vue.cjs +55 -9
- package/dist/bridge-app-vue.d.cts +25 -4
- package/dist/bridge-app-vue.d.ts +25 -4
- package/dist/bridge-app-vue.js +47 -9
- package/dist/bridge-core-ejlY9ddN.d.ts +93 -0
- package/dist/bridge-core.cjs +9 -1
- package/dist/bridge-core.d.cts +8 -1
- package/dist/bridge-core.d.ts +8 -1
- package/dist/bridge-core.js +2 -2
- package/dist/bridge-errors.cjs +56 -3
- package/dist/bridge-errors.d.cts +18 -2
- package/dist/bridge-errors.d.ts +18 -2
- package/dist/bridge-errors.js +11 -3
- package/dist/{bridge-host-react-C55RmGll.d.ts → bridge-host-react-Bua1TCVh.d.ts} +1 -1
- package/dist/bridge-host-react.cjs +333 -3
- package/dist/bridge-host-react.d.cts +2 -0
- package/dist/bridge-host-react.d.ts +2 -0
- package/dist/bridge-host-react.js +30 -4
- package/dist/{bridge-host-vue-BAxDKqGH.d.ts → bridge-host-vue-Dd1kkwDb.d.ts} +1 -1
- package/dist/bridge-host-vue.cjs +344 -3
- package/dist/bridge-host-vue.js +42 -5
- package/dist/bridge-react.d.ts +5 -3
- package/dist/bridge-router-core-BRZQYDPm.d.cts +46 -0
- package/dist/bridge-router-core-BRZQYDPm.d.ts +46 -0
- package/dist/bridge-router-react.d.ts +73 -0
- package/dist/bridge-router-react.js +247 -0
- package/dist/bridge-router-vue.d.ts +50 -0
- package/dist/bridge-router-vue.js +217 -0
- package/dist/bridge-vue.d.ts +2 -2
- package/dist/bridge.d.ts +6 -4
- package/dist/{chunk-GWJKABE7.js → chunk-43UX3UZE.js} +39 -4
- package/dist/chunk-7CQOVLSI.js +274 -0
- package/dist/{chunk-DH2QMM4Y.js → chunk-HZGQOW35.js} +51 -2
- package/dist/{chunk-ZTRJLCOF.js → chunk-KT3YARK5.js} +27 -3
- package/dist/{chunk-LE2G23E7.js → chunk-RXXBTPAY.js} +1 -1
- package/dist/{chunk-WD2ON7SN.js → chunk-UFWZPE36.js} +313 -12
- package/dist/index.cjs +30 -10
- package/dist/index.js +30 -10
- package/dist/react.d.ts +51 -4
- package/dist/runtime-entry.d.ts +60 -4
- package/dist/runtime.js +9 -9
- package/examples/bridge/react-host/package-lock.json +105 -104
- package/examples/bridge/react-remote/package-lock.json +105 -104
- package/examples/bridge/vue-host/package-lock.json +105 -104
- package/examples/bridge/vue-remote/package-lock.json +105 -104
- package/examples/react/host/package-lock.json +1 -1
- package/examples/react/remote/package-lock.json +1 -1
- package/examples/vue/host/package-lock.json +1 -1
- package/examples/vue/remote/package-lock.json +1 -1
- package/package.json +28 -4
- package/dist/bridge-core-6uLJiMVa.d.ts +0 -42
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
import {
|
|
2
|
+
RoutingChannel,
|
|
2
3
|
acquireBridgeSession,
|
|
3
4
|
assertBridgeContract,
|
|
4
5
|
assertBridgeOptions,
|
|
6
|
+
assertBridgeRoutingProtocol,
|
|
5
7
|
assertControlledSessionKey,
|
|
6
8
|
bridgeLifecycleError,
|
|
7
9
|
provideAppContext,
|
|
8
10
|
releaseBridgeSession,
|
|
9
11
|
resolveBridgeContext,
|
|
10
12
|
withBridgeTimeout
|
|
11
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-UFWZPE36.js";
|
|
12
14
|
|
|
13
15
|
// src/bridge-host-vue.ts
|
|
14
|
-
import { defineComponent, h, onBeforeUnmount, onMounted, ref, shallowRef, toRaw, watch } from "vue";
|
|
16
|
+
import { defineComponent, h, onActivated, onBeforeUnmount, onDeactivated, onMounted, ref, shallowRef, toRaw, watch } from "vue";
|
|
17
|
+
var assertBridgeRoutingProtocolSpec = (spec, contract) => assertBridgeRoutingProtocol(spec, contract);
|
|
18
|
+
function routingKey(routing) {
|
|
19
|
+
if (!routing) return void 0;
|
|
20
|
+
return routing.basePath + "|" + String(routing.navigation && typeof routing.navigation.navigate === "function");
|
|
21
|
+
}
|
|
15
22
|
var ERROR_STYLE = {
|
|
16
23
|
padding: "16px",
|
|
17
24
|
border: "1px solid #fde2e2",
|
|
@@ -89,7 +96,9 @@ function createVueBridgeAppWithLoader(loadRemote) {
|
|
|
89
96
|
props: {
|
|
90
97
|
appProps: { type: Object, required: false, default: void 0 },
|
|
91
98
|
// 桥接控制参数:undefined(不启用受控会话)/ null(登出态)/ 非空字符串(登录代次)
|
|
92
|
-
sessionKey: { type: null, required: false, default: void 0 }
|
|
99
|
+
sessionKey: { type: null, required: false, default: void 0 },
|
|
100
|
+
// URL 同步控制通道(独立于 appProps/Context;省略 = 原 memory 模式)
|
|
101
|
+
routing: { type: Object, required: false, default: void 0 }
|
|
93
102
|
},
|
|
94
103
|
setup(props) {
|
|
95
104
|
const status = ref("idle");
|
|
@@ -99,6 +108,8 @@ function createVueBridgeAppWithLoader(loadRemote) {
|
|
|
99
108
|
let contract;
|
|
100
109
|
let containerBlocked = false;
|
|
101
110
|
let acquiredSession;
|
|
111
|
+
let channel;
|
|
112
|
+
let genAbort;
|
|
102
113
|
const releaseAcquired = () => {
|
|
103
114
|
if (acquiredSession !== void 0) {
|
|
104
115
|
releaseBridgeSession(acquiredSession);
|
|
@@ -110,6 +121,10 @@ function createVueBridgeAppWithLoader(loadRemote) {
|
|
|
110
121
|
generation++;
|
|
111
122
|
const el = container.value;
|
|
112
123
|
releaseAcquired();
|
|
124
|
+
genAbort?.abort();
|
|
125
|
+
genAbort = void 0;
|
|
126
|
+
channel?.dispose();
|
|
127
|
+
channel = void 0;
|
|
113
128
|
if (contract && el) {
|
|
114
129
|
const c = contract;
|
|
115
130
|
contract = void 0;
|
|
@@ -151,7 +166,18 @@ function createVueBridgeAppWithLoader(loadRemote) {
|
|
|
151
166
|
if (myGeneration !== generation) return;
|
|
152
167
|
contract = assertBridgeContract(spec, mod);
|
|
153
168
|
const appPropsSource = props.appProps ? toRaw(props.appProps) : void 0;
|
|
154
|
-
|
|
169
|
+
genAbort = new AbortController();
|
|
170
|
+
let mountOptions = { signal: genAbort.signal };
|
|
171
|
+
if (props.routing) {
|
|
172
|
+
assertBridgeRoutingProtocolSpec(spec, contract);
|
|
173
|
+
const navigation = props.routing.navigation;
|
|
174
|
+
const latestLoc = navigation.getLocation();
|
|
175
|
+
if (myGeneration !== generation) return;
|
|
176
|
+
channel = new RoutingChannel(`bridge-vue:${spec}:${myGeneration}`, props.routing.basePath, navigation, spec);
|
|
177
|
+
mountOptions = { signal: genAbort.signal, routing: channel };
|
|
178
|
+
void latestLoc;
|
|
179
|
+
}
|
|
180
|
+
await contract.mount(el, { ...appPropsSource ?? {} }, mountOptions);
|
|
155
181
|
if (myGeneration !== generation) return;
|
|
156
182
|
status.value = "ready";
|
|
157
183
|
} catch (e) {
|
|
@@ -176,6 +202,15 @@ function createVueBridgeAppWithLoader(loadRemote) {
|
|
|
176
202
|
watch(() => props.sessionKey, () => {
|
|
177
203
|
start();
|
|
178
204
|
});
|
|
205
|
+
watch(() => routingKey(props.routing), (after, before) => {
|
|
206
|
+
if (after !== before) start();
|
|
207
|
+
});
|
|
208
|
+
onDeactivated(() => {
|
|
209
|
+
channel?.setActive(false);
|
|
210
|
+
});
|
|
211
|
+
onActivated(() => {
|
|
212
|
+
channel?.setActive(true);
|
|
213
|
+
});
|
|
179
214
|
onBeforeUnmount(() => {
|
|
180
215
|
invalidate();
|
|
181
216
|
});
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import {
|
|
2
|
+
routingConfigError,
|
|
3
|
+
routingNavigationError,
|
|
4
|
+
routingProtocolError,
|
|
5
|
+
routingSyncError
|
|
6
|
+
} from "./chunk-HZGQOW35.js";
|
|
7
|
+
|
|
8
|
+
// src/bridge-router-core.ts
|
|
9
|
+
var MAX_REDIRECT_PER_REQUEST = 5;
|
|
10
|
+
var MAX_GO_DELTA = 50;
|
|
11
|
+
function normalizeBasePath(basePath, spec) {
|
|
12
|
+
const bad = (reason) => routingConfigError(spec, reason);
|
|
13
|
+
if (typeof basePath !== "string" || basePath === "") throw bad("basePath \u4E0D\u80FD\u4E3A\u7A7A\u3002");
|
|
14
|
+
if (/^\/+$/u.test(basePath)) throw bad('basePath \u4E0D\u80FD\u662F\u6839 "/"\u2014\u2014\u6839\u8DEF\u5F84\u5C5E\u4E8E\u5BBF\u4E3B\u81EA\u8EAB\u9875\u9762\uFF0C\u8BF7\u4E3A\u540C\u6B65\u5B9E\u4F8B\u5206\u914D\u72EC\u7ACB\u524D\u7F00\u3002');
|
|
15
|
+
if (!basePath.startsWith("/")) throw bad(`basePath \u5FC5\u987B\u4EE5 "/" \u5F00\u5934\uFF1A\u5F53\u524D\u4E3A "${basePath}"\u3002`);
|
|
16
|
+
if (/[?#]/.test(basePath) || basePath.includes("://")) throw bad(`basePath \u4E0D\u80FD\u5305\u542B query/hash/\u534F\u8BAE\uFF1A\u5F53\u524D\u4E3A "${basePath}"\u3002`);
|
|
17
|
+
if (basePath.includes("*") || basePath.includes(":")) throw bad(`basePath \u4E0D\u63A5\u53D7\u901A\u914D\u7B26\u6216\u53C2\u6570\u6A21\u677F\uFF1A\u5F53\u524D\u4E3A "${basePath}"\u3002\u8BF7\u4F7F\u7528\u9759\u6001\u7EDD\u5BF9\u8DEF\u5F84\u3002`);
|
|
18
|
+
if (/\\|\/\/|%(?:2e|2f|5c)/i.test(basePath) || basePath.split("/").some((part) => part === "." || part === "..")) throw bad("basePath \u4E0D\u80FD\u5305\u542B\u7A7A\u8DEF\u5F84\u6BB5\u3001\u8F6C\u4E49\u5206\u9694\u7B26\u6216\u76F8\u5BF9\u8DEF\u5F84\u6BB5\u3002");
|
|
19
|
+
return basePath.endsWith("/") ? basePath.slice(0, -1) : basePath;
|
|
20
|
+
}
|
|
21
|
+
function matchesBasePath(hostPathname, basePath) {
|
|
22
|
+
return hostPathname === basePath || hostPathname.startsWith(basePath + "/");
|
|
23
|
+
}
|
|
24
|
+
function overlapsBasePath(a, b) {
|
|
25
|
+
return a === b || a.startsWith(b + "/") || b.startsWith(a + "/");
|
|
26
|
+
}
|
|
27
|
+
function toChildLocation(host, basePath) {
|
|
28
|
+
const rest = host.pathname === basePath ? "/" : host.pathname.slice(basePath.length);
|
|
29
|
+
return { pathname: rest || "/", search: host.search, hash: host.hash };
|
|
30
|
+
}
|
|
31
|
+
function toHostLocation(child, basePath, spec) {
|
|
32
|
+
const childPath = child.pathname === "/" ? "" : child.pathname;
|
|
33
|
+
if (childPath !== "" && !childPath.startsWith("/")) {
|
|
34
|
+
throw routingNavigationError(spec, `\u5BFC\u822A\u76EE\u6807 pathname \u5FC5\u987B\u4EE5 "/" \u5F00\u5934\uFF1A\u5F53\u524D\u4E3A "${child.pathname}"\u3002`);
|
|
35
|
+
}
|
|
36
|
+
return { pathname: basePath + childPath, search: child.search, hash: child.hash };
|
|
37
|
+
}
|
|
38
|
+
function sameLocation(a, b) {
|
|
39
|
+
return a.pathname === b.pathname && a.search === b.search && a.hash === b.hash;
|
|
40
|
+
}
|
|
41
|
+
function assertChildTargetInScope(target, basePath, spec) {
|
|
42
|
+
if (!target || typeof target.pathname !== "string" || typeof target.search !== "string" || typeof target.hash !== "string" || target.search !== "" && !target.search.startsWith("?") || target.hash !== "" && !target.hash.startsWith("#")) throw routingNavigationError(spec, "\u5BFC\u822A\u76EE\u6807\u5FC5\u987B\u63D0\u4F9B pathname\u3001search\u3001hash \u4E09\u6BB5\uFF0Csearch/hash \u5E94\u4E3A\u7A7A\u6216\u4EE5 ?/# \u5F00\u5934\u3002");
|
|
43
|
+
if (/[\x00-\x20\\?#]|%(?:2e|2f|5c)/i.test(target.pathname) || target.pathname.includes("//") || target.pathname.split("/").some((part) => part === "." || part === "..")) {
|
|
44
|
+
throw routingNavigationError(spec, `\u5BFC\u822A\u76EE\u6807\u8BD5\u56FE\u9003\u9038\u81EA\u8EAB\u524D\u7F00\uFF1Apathname "${target.pathname}"\u3002\u8DE8\u524D\u7F00\u5BFC\u822A\u8BF7\u901A\u8FC7\u5BBF\u4E3B\u83DC\u5355\u7B49\u5BBF\u4E3B\u80FD\u529B\u5B8C\u6210\u3002`);
|
|
45
|
+
}
|
|
46
|
+
const host = toHostLocation(target, basePath, spec);
|
|
47
|
+
if (!matchesBasePath(host.pathname, basePath)) {
|
|
48
|
+
throw routingNavigationError(spec, `\u5BFC\u822A\u76EE\u6807 ${host.pathname} \u4E0D\u5728\u524D\u7F00 ${basePath} \u4E0B\u3002`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
var activeRoutingPrefixes = /* @__PURE__ */ new Map();
|
|
52
|
+
function acquireRoutingPrefix(basePath, spec) {
|
|
53
|
+
for (const [active] of activeRoutingPrefixes) {
|
|
54
|
+
if ((activeRoutingPrefixes.get(active) ?? 0) > 0 && overlapsBasePath(active, basePath)) {
|
|
55
|
+
throw routingConfigError(spec, `\u540C\u9875\u5DF2\u6709\u5360\u7528\u524D\u7F00 "${active}" \u7684\u540C\u6B65\u5B9E\u4F8B\uFF0C\u65B0\u5B9E\u4F8B "${spec}" \u7684 basePath "${basePath}" \u4E0E\u5176\u91CD\u53E0\u3002`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
activeRoutingPrefixes.set(basePath, (activeRoutingPrefixes.get(basePath) ?? 0) + 1);
|
|
59
|
+
let released = false;
|
|
60
|
+
return () => {
|
|
61
|
+
if (released) return;
|
|
62
|
+
released = true;
|
|
63
|
+
activeRoutingPrefixes.set(basePath, Math.max(0, (activeRoutingPrefixes.get(basePath) ?? 1) - 1));
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function assertBridgeRoutingProtocol(spec, contract) {
|
|
67
|
+
const r = contract?.routing;
|
|
68
|
+
if (!r || r.protocol !== 1) {
|
|
69
|
+
throw routingProtocolError(
|
|
70
|
+
spec,
|
|
71
|
+
`\u8FDC\u7A0B\u5951\u7EA6\u672A\u58F0\u660E\u8DEF\u7531\u534F\u8BAE\uFF08\u671F\u671B routing: { protocol: 1 }\uFF0C\u5B9E\u9645 ${r === void 0 ? "\u7F3A\u5931" : `protocol=${String(r?.protocol)}`}\uFF09\u3002\u5F00\u542F URL \u540C\u6B65\u4E0D\u80FD\u9759\u9ED8\u9000\u56DE memory \u6A21\u5F0F\u3002`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
var RoutingChannel = class {
|
|
76
|
+
id;
|
|
77
|
+
basePath;
|
|
78
|
+
spec;
|
|
79
|
+
host;
|
|
80
|
+
listeners = /* @__PURE__ */ new Set();
|
|
81
|
+
current;
|
|
82
|
+
lastConfirmed;
|
|
83
|
+
seq = 0;
|
|
84
|
+
inflight = null;
|
|
85
|
+
queue;
|
|
86
|
+
generation = 0;
|
|
87
|
+
disposed = false;
|
|
88
|
+
active = true;
|
|
89
|
+
releasePrefix;
|
|
90
|
+
unsubHost;
|
|
91
|
+
/** 连续内部 replace 计数(有界重定向检测) */
|
|
92
|
+
redirectChain = [];
|
|
93
|
+
redirectTimer;
|
|
94
|
+
/** 诊断观测钩子(测试断言/诊断用) */
|
|
95
|
+
onEvent;
|
|
96
|
+
constructor(id, basePath, host, spec) {
|
|
97
|
+
this.id = id;
|
|
98
|
+
this.spec = spec;
|
|
99
|
+
this.basePath = normalizeBasePath(basePath, spec);
|
|
100
|
+
this.host = host;
|
|
101
|
+
const initial = host.getLocation();
|
|
102
|
+
this.current = matchesBasePath(initial.pathname, this.basePath) ? toChildLocation(initial, this.basePath) : { pathname: "/", search: "", hash: "" };
|
|
103
|
+
this.lastConfirmed = { ...this.current };
|
|
104
|
+
this.releasePrefix = acquireRoutingPrefix(this.basePath, spec);
|
|
105
|
+
this.unsubHost = host.subscribe((loc) => this.handleHostLocation(loc));
|
|
106
|
+
}
|
|
107
|
+
emit(loc) {
|
|
108
|
+
this.current = { ...loc };
|
|
109
|
+
for (const l of [...this.listeners]) {
|
|
110
|
+
try {
|
|
111
|
+
l(this.current);
|
|
112
|
+
} catch (e) {
|
|
113
|
+
console.error("[fulgurjs] \u5B50\u5E94\u7528\u8DEF\u7531\u8BA2\u9605\u8005\u5F02\u5E38\uFF08\u5DF2\u9694\u79BB\uFF0C\u4E0D\u5F71\u54CD\u5BBF\u4E3B\uFF09\uFF1A", e);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
handleHostLocation(hostLoc) {
|
|
118
|
+
if (this.disposed) return;
|
|
119
|
+
if (!matchesBasePath(hostLoc.pathname, this.basePath)) {
|
|
120
|
+
this.cancelPending();
|
|
121
|
+
this.onEvent?.({ type: "prefix-miss", pathname: hostLoc.pathname });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (!this.active) {
|
|
125
|
+
this.onEvent?.({ type: "paused-host-loc" });
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const child = toChildLocation(hostLoc, this.basePath);
|
|
129
|
+
if (this.inflight && sameLocation(child, this.inflight.target)) {
|
|
130
|
+
const { seq } = this.inflight;
|
|
131
|
+
this.lastConfirmed = child;
|
|
132
|
+
this.emit(child);
|
|
133
|
+
this.onEvent?.({ type: "request-confirmed", seq });
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (this.inflight) {
|
|
137
|
+
const seq = this.inflight.seq;
|
|
138
|
+
this.lastConfirmed = child;
|
|
139
|
+
this.cancelPending();
|
|
140
|
+
this.emit(child);
|
|
141
|
+
this.onEvent?.({ type: "request-superseded", seq });
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (!sameLocation(child, this.lastConfirmed)) this.generation++;
|
|
145
|
+
this.lastConfirmed = child;
|
|
146
|
+
this.emit(child);
|
|
147
|
+
this.onEvent?.({ type: "host-broadcast" });
|
|
148
|
+
}
|
|
149
|
+
getLocation() {
|
|
150
|
+
return { ...this.current };
|
|
151
|
+
}
|
|
152
|
+
subscribe(listener) {
|
|
153
|
+
if (this.disposed) {
|
|
154
|
+
throw routingProtocolError(this.spec, `\u901A\u9053 ${this.id} \u5DF2\u9500\u6BC1\uFF0C\u4E0D\u80FD\u518D\u8BA2\u9605\uFF08\u65E7\u5B9E\u4F8B\u7684\u8FDF\u5230\u8BA2\u9605\u5FC5\u987B\u5931\u6548\uFF1B\u91CD\u6302\u4F1A\u5F97\u5230\u65B0\u901A\u9053\uFF09\u3002`);
|
|
155
|
+
}
|
|
156
|
+
this.listeners.add(listener);
|
|
157
|
+
return () => {
|
|
158
|
+
this.listeners.delete(listener);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
cancelPending() {
|
|
162
|
+
this.generation++;
|
|
163
|
+
const pending = this.inflight;
|
|
164
|
+
this.inflight = null;
|
|
165
|
+
if (pending) {
|
|
166
|
+
pending.abort.abort();
|
|
167
|
+
pending.resolve({ status: "cancelled", location: { ...this.lastConfirmed } });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
navigate(target, action) {
|
|
171
|
+
const generation = this.generation;
|
|
172
|
+
const snapshot = { ...target };
|
|
173
|
+
const run = () => generation !== this.generation ? Promise.resolve({ status: "cancelled", location: { ...this.lastConfirmed } }) : this.performNavigation(snapshot, action);
|
|
174
|
+
const task = this.queue ? this.queue.then(run) : run();
|
|
175
|
+
const tail = task.then(() => {
|
|
176
|
+
}, () => {
|
|
177
|
+
});
|
|
178
|
+
this.queue = tail;
|
|
179
|
+
void tail.then(() => {
|
|
180
|
+
if (this.queue === tail) this.queue = void 0;
|
|
181
|
+
});
|
|
182
|
+
return task;
|
|
183
|
+
}
|
|
184
|
+
async performNavigation(target, action) {
|
|
185
|
+
if (this.disposed || !this.active || !matchesBasePath(this.host.getLocation().pathname, this.basePath)) {
|
|
186
|
+
this.onEvent?.({ type: this.disposed ? "rejected-disposed" : "rejected-inactive" });
|
|
187
|
+
return { status: "cancelled", location: { ...this.lastConfirmed } };
|
|
188
|
+
}
|
|
189
|
+
assertChildTargetInScope(target, this.basePath, this.spec);
|
|
190
|
+
if (sameLocation(target, this.lastConfirmed)) {
|
|
191
|
+
this.onEvent?.({ type: "noop" });
|
|
192
|
+
return { status: "committed", location: { ...this.lastConfirmed } };
|
|
193
|
+
}
|
|
194
|
+
if (action === "replace") {
|
|
195
|
+
this.redirectChain.push(target.pathname + target.search + target.hash);
|
|
196
|
+
} else {
|
|
197
|
+
this.redirectChain = [];
|
|
198
|
+
}
|
|
199
|
+
if (this.redirectTimer) clearTimeout(this.redirectTimer);
|
|
200
|
+
this.redirectTimer = setTimeout(() => {
|
|
201
|
+
this.redirectChain = [];
|
|
202
|
+
}, 1e3);
|
|
203
|
+
if (this.redirectChain.length > MAX_REDIRECT_PER_REQUEST) {
|
|
204
|
+
const chain = [...this.redirectChain];
|
|
205
|
+
this.redirectChain = [];
|
|
206
|
+
throw routingSyncError(this.spec, "\u540C\u4E00\u901A\u9053\u7684\u8FDE\u7EED replace \u91CD\u5B9A\u5411\u8D85\u8FC7\u4E0A\u9650\uFF08\u7591\u4F3C\u5B50\u5E94\u7528\u8DEF\u7531\u4E92\u76F8\u6210\u73AF\uFF09\u3002", chain);
|
|
207
|
+
}
|
|
208
|
+
const seq = ++this.seq;
|
|
209
|
+
this.onEvent?.({ type: "request-start", seq, target: { ...target }, action });
|
|
210
|
+
return new Promise((resolve, reject) => {
|
|
211
|
+
const abort = new AbortController();
|
|
212
|
+
this.inflight = { seq, target: { ...target }, resolve, abort };
|
|
213
|
+
Promise.resolve().then(() => abort.signal.aborted ? { status: "cancelled", location: this.host.getLocation() } : this.host.navigate(toHostLocation(target, this.basePath, this.spec), action, { signal: abort.signal })).then((result) => {
|
|
214
|
+
if (!this.inflight || this.inflight.seq !== seq) return;
|
|
215
|
+
if (this.disposed || !this.active) {
|
|
216
|
+
this.inflight = null;
|
|
217
|
+
resolve({ status: "cancelled", location: { ...this.lastConfirmed } });
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
this.inflight = null;
|
|
221
|
+
if (result.status === "committed" && matchesBasePath(result.location.pathname, this.basePath)) {
|
|
222
|
+
const child = toChildLocation(result.location, this.basePath);
|
|
223
|
+
this.lastConfirmed = child;
|
|
224
|
+
this.emit(child);
|
|
225
|
+
} else {
|
|
226
|
+
this.emit({ ...this.lastConfirmed });
|
|
227
|
+
}
|
|
228
|
+
this.onEvent?.({ type: "host-verdict", seq, status: result.status });
|
|
229
|
+
resolve(
|
|
230
|
+
result.status === "committed" && matchesBasePath(result.location.pathname, this.basePath) ? { status: "committed", location: { ...this.lastConfirmed } } : { status: "cancelled", location: { ...this.lastConfirmed } }
|
|
231
|
+
);
|
|
232
|
+
}).catch((cause) => {
|
|
233
|
+
if (this.inflight?.seq === seq) {
|
|
234
|
+
this.inflight = null;
|
|
235
|
+
this.onEvent?.({ type: "host-error", seq });
|
|
236
|
+
this.emit({ ...this.lastConfirmed });
|
|
237
|
+
reject(routingSyncError(this.spec, `\u5BBF\u4E3B\u5BFC\u822A\u6267\u884C\u5931\u8D25\uFF1A${cause instanceof Error ? cause.message : String(cause)}`, [], cause));
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
/** 委托宿主历史(有限整数);不创建第二条独立历史 */
|
|
243
|
+
go(delta) {
|
|
244
|
+
if (this.disposed || !this.active || !matchesBasePath(this.host.getLocation().pathname, this.basePath)) return;
|
|
245
|
+
if (typeof delta !== "number" || !Number.isInteger(delta) || Math.abs(delta) > MAX_GO_DELTA) {
|
|
246
|
+
throw routingNavigationError(this.spec, `go \u53C2\u6570\u5FC5\u987B\u662F\u6709\u9650\u6574\u6570\uFF08|delta| \u2264 ${MAX_GO_DELTA}\uFF09\uFF1A\u5F53\u524D\u4E3A ${String(delta)}\u3002`);
|
|
247
|
+
}
|
|
248
|
+
this.host.go(delta);
|
|
249
|
+
}
|
|
250
|
+
/** KeepAlive deactivate:暂停广播与写入;通道与订阅保留(activate 后 resync) */
|
|
251
|
+
setActive(active) {
|
|
252
|
+
if (this.disposed) return;
|
|
253
|
+
if (!active) this.cancelPending();
|
|
254
|
+
this.active = active;
|
|
255
|
+
this.onEvent?.({ type: active ? "resumed" : "paused" });
|
|
256
|
+
if (active) this.handleHostLocation(this.host.getLocation());
|
|
257
|
+
}
|
|
258
|
+
/** 挂载生命周期结束:清理订阅/登记/在飞请求;此后一切写入失效 */
|
|
259
|
+
dispose() {
|
|
260
|
+
if (this.disposed) return;
|
|
261
|
+
this.disposed = true;
|
|
262
|
+
if (this.redirectTimer) clearTimeout(this.redirectTimer);
|
|
263
|
+
this.unsubHost();
|
|
264
|
+
this.releasePrefix();
|
|
265
|
+
this.cancelPending();
|
|
266
|
+
this.listeners.clear();
|
|
267
|
+
this.onEvent?.({ type: "disposed" });
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
export {
|
|
272
|
+
assertBridgeRoutingProtocol,
|
|
273
|
+
RoutingChannel
|
|
274
|
+
};
|
|
@@ -9,7 +9,15 @@ var BridgeErrorCodes = {
|
|
|
9
9
|
/** 桥接准备或生命周期失败(details.phase = getContext | mount | unmount) */
|
|
10
10
|
BRIDGE_LIFECYCLE_FAILED: "MFU-016",
|
|
11
11
|
/** 桥接会话参数与 AppContext 不一致(受控 sessionKey 与全局会话矛盾) */
|
|
12
|
-
BRIDGE_SESSION_MISMATCH: "MFU-017"
|
|
12
|
+
BRIDGE_SESSION_MISMATCH: "MFU-017",
|
|
13
|
+
/** 路由同步配置/前缀冲突(basePath 非法、同页前缀冲突或重叠) */
|
|
14
|
+
ROUTING_CONFIG_INVALID: "MFU-030",
|
|
15
|
+
/** 路由协议缺失/通道失效(子应用未声明 routing 协议,或已销毁通道被再次使用) */
|
|
16
|
+
ROUTING_PROTOCOL_MISSING: "MFU-031",
|
|
17
|
+
/** 非法导航(目标越界前缀、非法 go 参数、失效实例的请求) */
|
|
18
|
+
ROUTING_NAVIGATION_INVALID: "MFU-032",
|
|
19
|
+
/** 路由准备/同步失败(重定向循环、子应用路由准备失败且已阻止错误页面显示) */
|
|
20
|
+
ROUTING_SYNC_FAILED: "MFU-033"
|
|
13
21
|
};
|
|
14
22
|
function invalidBridgeContractError(spec, mod) {
|
|
15
23
|
const actual = mod === null ? "null" : typeof mod;
|
|
@@ -66,11 +74,52 @@ function invalidSessionKeyError(spec, value) {
|
|
|
66
74
|
{ spec, value: String(value) }
|
|
67
75
|
);
|
|
68
76
|
}
|
|
77
|
+
function routingConfigError(spec, reason) {
|
|
78
|
+
return new FgError(
|
|
79
|
+
BridgeErrorCodes.ROUTING_CONFIG_INVALID,
|
|
80
|
+
`\u73B0\u8C61\uFF1A\u6865\u63A5\u5E94\u7528 "${spec}" \u7684 URL \u540C\u6B65\u914D\u7F6E\u65E0\u6CD5\u542F\u7528\u3002
|
|
81
|
+
\u539F\u56E0\uFF1A${reason}
|
|
82
|
+
\u4FEE\u6CD5\uFF1A\u4FEE\u6B63 routing.basePath \u4E3A\u5BBF\u4E3B\u8DEF\u7531\u89C6\u89D2\u7684\u9759\u6001\u7EDD\u5BF9\u8DEF\u5F84\uFF08\u5982 "/approval"\uFF09\uFF0C\u5E76\u4FDD\u8BC1\u540C\u9875\u5404\u540C\u6B65\u5B9E\u4F8B\u524D\u7F00\u4E92\u4E0D\u91CD\u53E0\uFF1B\u5BBF\u4E3B\u8DEF\u7531\u9700\u58F0\u660E\u5BF9\u5E94\u7684\u540E\u7F00\u5339\u914D\uFF08Vue "/approval/:pathMatch(.*)*"\u3001React "/approval/*"\uFF09\u3002`,
|
|
83
|
+
{ spec }
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
function routingProtocolError(spec, reason) {
|
|
87
|
+
return new FgError(
|
|
88
|
+
BridgeErrorCodes.ROUTING_PROTOCOL_MISSING,
|
|
89
|
+
`\u73B0\u8C61\uFF1A\u6865\u63A5\u5E94\u7528 "${spec}" \u7684 URL \u540C\u6B65\u901A\u9053\u4E0D\u53EF\u7528\u3002
|
|
90
|
+
\u539F\u56E0\uFF1A${reason}
|
|
91
|
+
\u4FEE\u6CD5\uFF1A\u5B50\u5E94\u7528\u4EE5 defineBridgeApp(\u5DE5\u5382, { routing: true }) \u58F0\u660E\u8DEF\u7531\u534F\u8BAE\u5E76\u5728\u5DE5\u5382\u7B2C\u4E8C\u53C2\u6570\u63A5\u6536 { signal, routing } \u63A5\u7EBF\u53D7\u63A7\u8DEF\u7531\uFF1B\u901A\u9053\u968F\u6302\u8F7D\u751F\u547D\u5468\u671F\u521B\u5EFA\uFF0C\u9500\u6BC1\u540E\u4E0D\u5F97\u590D\u7528\uFF08\u91CD\u6302\u4F1A\u5F97\u5230\u65B0\u901A\u9053\uFF09\u3002`,
|
|
92
|
+
{ spec }
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
function routingNavigationError(spec, reason) {
|
|
96
|
+
return new FgError(
|
|
97
|
+
BridgeErrorCodes.ROUTING_NAVIGATION_INVALID,
|
|
98
|
+
`\u73B0\u8C61\uFF1A\u6865\u63A5\u5E94\u7528 "${spec}" \u7684\u4E00\u6B21\u5BFC\u822A\u8BF7\u6C42\u88AB\u62D2\u7EDD\u3002
|
|
99
|
+
\u539F\u56E0\uFF1A${reason}
|
|
100
|
+
\u4FEE\u6CD5\uFF1A\u5B50\u5E94\u7528\u5BFC\u822A\u76EE\u6807\u5FC5\u987B\u4F4D\u4E8E\u81EA\u8EAB basePath \u524D\u7F00\u5185\uFF08\u8DE8\u524D\u7F00\u8BF7\u7528\u5BBF\u4E3B\u83DC\u5355/\u5BBF\u4E3B\u80FD\u529B\uFF09\uFF1Bgo \u53C2\u6570\u4E3A\u6709\u9650\u6574\u6570\u4E14 |delta| \u2264 50\u3002`,
|
|
101
|
+
{ spec }
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
function routingSyncError(spec, reason, chain, cause) {
|
|
105
|
+
return new FgError(
|
|
106
|
+
BridgeErrorCodes.ROUTING_SYNC_FAILED,
|
|
107
|
+
`\u73B0\u8C61\uFF1A\u6865\u63A5\u5E94\u7528 "${spec}" \u7684 URL \u540C\u6B65\u5931\u8D25\u3002
|
|
108
|
+
\u539F\u56E0\uFF1A${reason}
|
|
109
|
+
\u4FEE\u6CD5\uFF1A\u68C0\u67E5\u8DEF\u7531\u5B88\u536B\u3001\u52A0\u8F7D\u5668\u4E0E\u5BFC\u822A\u5F02\u5E38\uFF1B\u91CD\u5B9A\u5411\u94FE\u4E0D\u5E94\u6210\u73AF\u3002\u76EE\u6807\u94FE\uFF1A${chain.join(" \u2192 ")}\u3002`,
|
|
110
|
+
{ spec, chain: [...chain] },
|
|
111
|
+
{ cause }
|
|
112
|
+
);
|
|
113
|
+
}
|
|
69
114
|
|
|
70
115
|
export {
|
|
71
116
|
BridgeErrorCodes,
|
|
72
117
|
invalidBridgeContractError,
|
|
73
118
|
bridgeLifecycleError,
|
|
74
119
|
bridgeSessionMismatchError,
|
|
75
|
-
invalidSessionKeyError
|
|
120
|
+
invalidSessionKeyError,
|
|
121
|
+
routingConfigError,
|
|
122
|
+
routingProtocolError,
|
|
123
|
+
routingNavigationError,
|
|
124
|
+
routingSyncError
|
|
76
125
|
};
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
+
RoutingChannel,
|
|
2
3
|
acquireBridgeSession,
|
|
3
4
|
assertBridgeContract,
|
|
4
5
|
assertBridgeOptions,
|
|
6
|
+
assertBridgeRoutingProtocol,
|
|
5
7
|
assertControlledSessionKey,
|
|
6
8
|
bridgeLifecycleError,
|
|
7
9
|
provideAppContext,
|
|
8
10
|
releaseBridgeSession,
|
|
9
11
|
resolveBridgeContext,
|
|
10
12
|
withBridgeTimeout
|
|
11
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-UFWZPE36.js";
|
|
12
14
|
|
|
13
15
|
// src/bridge-host-react.ts
|
|
14
16
|
import {
|
|
@@ -18,6 +20,10 @@ import {
|
|
|
18
20
|
useRef,
|
|
19
21
|
useState
|
|
20
22
|
} from "react";
|
|
23
|
+
function routingKey(routing) {
|
|
24
|
+
if (!routing) return void 0;
|
|
25
|
+
return routing.basePath + "|" + String(routing.navigation && typeof routing.navigation.navigate === "function");
|
|
26
|
+
}
|
|
21
27
|
var ERROR_STYLE = {
|
|
22
28
|
padding: "16px",
|
|
23
29
|
border: "1px solid #fde2e2",
|
|
@@ -88,6 +94,8 @@ function createReactBridgeAppWithLoader(loadRemote) {
|
|
|
88
94
|
const contractRef = useRef(void 0);
|
|
89
95
|
const acquiredRef = useRef(void 0);
|
|
90
96
|
const blockedRef = useRef(false);
|
|
97
|
+
const channelRef = useRef(void 0);
|
|
98
|
+
const abortRef = useRef(void 0);
|
|
91
99
|
const latest = useRef(props);
|
|
92
100
|
latest.current = props;
|
|
93
101
|
useEffect(() => {
|
|
@@ -100,6 +108,12 @@ function createReactBridgeAppWithLoader(loadRemote) {
|
|
|
100
108
|
acquiredRef.current = void 0;
|
|
101
109
|
}
|
|
102
110
|
};
|
|
111
|
+
const disposeChannel = () => {
|
|
112
|
+
channelRef.current?.dispose();
|
|
113
|
+
channelRef.current = void 0;
|
|
114
|
+
abortRef.current?.abort();
|
|
115
|
+
abortRef.current = void 0;
|
|
116
|
+
};
|
|
103
117
|
const run = async () => {
|
|
104
118
|
if (!el) return;
|
|
105
119
|
const sk = latest.current.sessionKey;
|
|
@@ -124,7 +138,16 @@ function createReactBridgeAppWithLoader(loadRemote) {
|
|
|
124
138
|
if (generationRef.current !== myGen) return;
|
|
125
139
|
const contract = assertBridgeContract(spec, mod);
|
|
126
140
|
contractRef.current = contract;
|
|
127
|
-
|
|
141
|
+
abortRef.current = new AbortController();
|
|
142
|
+
let mountOptions = { signal: abortRef.current.signal };
|
|
143
|
+
const routing = latest.current.routing;
|
|
144
|
+
if (routing) {
|
|
145
|
+
assertBridgeRoutingProtocol(spec, contract);
|
|
146
|
+
if (generationRef.current !== myGen) return;
|
|
147
|
+
channelRef.current = new RoutingChannel(`bridge-react:${spec}:${myGen}`, routing.basePath, routing.navigation, spec);
|
|
148
|
+
mountOptions = { signal: abortRef.current.signal, routing: channelRef.current };
|
|
149
|
+
}
|
|
150
|
+
await contract.mount(el, { ...latest.current.appProps ?? {} }, mountOptions);
|
|
128
151
|
if (generationRef.current !== myGen) return;
|
|
129
152
|
setStatus("ready");
|
|
130
153
|
} catch (e) {
|
|
@@ -137,6 +160,7 @@ function createReactBridgeAppWithLoader(loadRemote) {
|
|
|
137
160
|
return () => {
|
|
138
161
|
generationRef.current++;
|
|
139
162
|
release();
|
|
163
|
+
disposeChannel();
|
|
140
164
|
const c = contractRef.current;
|
|
141
165
|
contractRef.current = void 0;
|
|
142
166
|
if (c && el) {
|
|
@@ -151,7 +175,7 @@ function createReactBridgeAppWithLoader(loadRemote) {
|
|
|
151
175
|
}
|
|
152
176
|
}
|
|
153
177
|
};
|
|
154
|
-
}, [props.sessionKey, attempt]);
|
|
178
|
+
}, [props.sessionKey, attempt, routingKey(latest.current.routing)]);
|
|
155
179
|
const retry = () => setAttempt((a) => a + 1);
|
|
156
180
|
const children = [];
|
|
157
181
|
if (status === "pending" && options.fallback !== void 0) {
|