@fulgurjs/federation 5.2.5 → 5.3.0
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 +14 -0
- package/DESIGN.md +7 -0
- package/README.en.md +86 -4
- package/README.md +125 -3
- package/dist/bridge-app-react.cjs +185 -0
- package/dist/bridge-app-react.d.cts +37 -0
- package/dist/bridge-app-react.d.ts +37 -0
- package/dist/bridge-app-react.js +115 -0
- package/dist/bridge-app-vue.cjs +111 -0
- package/dist/bridge-app-vue.d.cts +34 -0
- package/dist/bridge-app-vue.d.ts +34 -0
- package/dist/bridge-app-vue.js +51 -0
- package/dist/bridge-core-6uLJiMVa.d.ts +42 -0
- package/dist/bridge-core.cjs +224 -0
- package/dist/bridge-core.d.cts +51 -0
- package/dist/bridge-core.d.ts +51 -0
- package/dist/bridge-core.js +21 -0
- package/dist/bridge-errors.cjs +114 -0
- package/dist/bridge-errors.d.cts +36 -0
- package/dist/bridge-errors.d.ts +36 -0
- package/dist/bridge-errors.js +15 -0
- package/dist/bridge-host-react-C55RmGll.d.ts +35 -0
- package/dist/bridge-host-react.cjs +392 -0
- package/dist/bridge-host-react.d.cts +45 -0
- package/dist/bridge-host-react.d.ts +45 -0
- package/dist/bridge-host-react.js +183 -0
- package/dist/bridge-host-vue-BAxDKqGH.d.ts +32 -0
- package/dist/bridge-host-vue.cjs +426 -0
- package/dist/bridge-host-vue.d.cts +39 -0
- package/dist/bridge-host-vue.d.ts +39 -0
- package/dist/bridge-host-vue.js +211 -0
- package/dist/bridge-react.d.ts +12 -0
- package/dist/bridge-react.js +3 -0
- package/dist/bridge-vue.d.ts +8 -0
- package/dist/bridge-vue.js +3 -0
- package/dist/bridge.d.ts +16 -0
- package/dist/bridge.js +5 -0
- package/dist/chunk-E55P4TIY.js +76 -0
- package/dist/chunk-EH34L46P.js +207 -0
- package/dist/chunk-FVYGSLAQ.js +179 -0
- package/dist/chunk-LWJBHUJQ.js +114 -0
- package/dist/chunk-OF2JCXTZ.js +15 -0
- package/dist/chunk-TSYLDY6H.js +64 -0
- package/dist/chunk-UJFCPQGN.js +1210 -0
- package/dist/context.js +8 -64
- package/dist/index.cjs +84 -8
- package/dist/index.js +84 -8
- package/dist/react.d.ts +44 -2
- package/dist/react.js +2 -0
- package/dist/runtime-entry.d.ts +41 -2
- package/dist/runtime-entry.js +1 -1
- package/dist/runtime.js +1 -1
- package/dist/vue.js +2 -0
- package/docs/webpack-mf-/345/257/271/347/205/247/344/270/216/347/274/272/345/217/243.md +1 -1
- package/docs//350/277/201/347/247/273/346/214/207/345/215/227.md +1 -1
- package/examples/README.en.md +20 -0
- package/examples/README.md +20 -0
- package/examples/bridge/README.md +36 -0
- package/examples/bridge/react-host/README.md +23 -0
- package/examples/bridge/react-host/fulgurjs.config.ts +20 -0
- package/examples/bridge/react-host/index.html +11 -0
- package/examples/bridge/react-host/package-lock.json +2457 -0
- package/examples/bridge/react-host/package.json +24 -0
- package/examples/bridge/react-host/src/env.d.ts +7 -0
- package/examples/bridge/react-host/src/host-session.ts +26 -0
- package/examples/bridge/react-host/src/main.tsx +62 -0
- package/examples/bridge/react-host/tsconfig.json +13 -0
- package/examples/bridge/react-host/vite.config.ts +9 -0
- package/examples/bridge/react-remote/README.md +20 -0
- package/examples/bridge/react-remote/fulgurjs.config.ts +17 -0
- package/examples/bridge/react-remote/index.html +11 -0
- package/examples/bridge/react-remote/package-lock.json +2382 -0
- package/examples/bridge/react-remote/package.json +24 -0
- package/examples/bridge/react-remote/src/bridge.tsx +66 -0
- package/examples/bridge/react-remote/src/env.d.ts +7 -0
- package/examples/bridge/react-remote/src/main.tsx +7 -0
- package/examples/bridge/react-remote/tsconfig.json +13 -0
- package/examples/bridge/react-remote/vite.config.ts +9 -0
- package/examples/bridge/vue-host/README.md +24 -0
- package/examples/bridge/vue-host/fulgurjs.config.ts +22 -0
- package/examples/bridge/vue-host/index.html +11 -0
- package/examples/bridge/vue-host/package-lock.json +1885 -0
- package/examples/bridge/vue-host/package.json +24 -0
- package/examples/bridge/vue-host/src/App.vue +70 -0
- package/examples/bridge/vue-host/src/env.d.ts +7 -0
- package/examples/bridge/vue-host/src/host-session.ts +29 -0
- package/examples/bridge/vue-host/src/main.ts +4 -0
- package/examples/bridge/vue-host/tsconfig.json +13 -0
- package/examples/bridge/vue-host/vite.config.ts +9 -0
- package/examples/bridge/vue-remote/README.md +19 -0
- package/examples/bridge/vue-remote/fulgurjs.config.ts +16 -0
- package/examples/bridge/vue-remote/index.html +11 -0
- package/examples/bridge/vue-remote/package-lock.json +1856 -0
- package/examples/bridge/vue-remote/package.json +21 -0
- package/examples/bridge/vue-remote/src/App.vue +12 -0
- package/examples/bridge/vue-remote/src/bridge.ts +57 -0
- package/examples/bridge/vue-remote/src/env.d.ts +7 -0
- package/examples/bridge/vue-remote/src/main.ts +15 -0
- package/examples/bridge/vue-remote/tsconfig.json +13 -0
- package/examples/bridge/vue-remote/vite.config.ts +9 -0
- package/package.json +43 -2
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import {
|
|
2
|
+
acquireBridgeSession,
|
|
3
|
+
assertBridgeContract,
|
|
4
|
+
assertBridgeOptions,
|
|
5
|
+
assertControlledSessionKey,
|
|
6
|
+
bridgeLifecycleError,
|
|
7
|
+
provideAppContext,
|
|
8
|
+
releaseBridgeSession,
|
|
9
|
+
resolveBridgeContext,
|
|
10
|
+
withBridgeTimeout
|
|
11
|
+
} from "./chunk-UJFCPQGN.js";
|
|
12
|
+
|
|
13
|
+
// src/bridge-host-react.ts
|
|
14
|
+
import {
|
|
15
|
+
createElement,
|
|
16
|
+
Fragment,
|
|
17
|
+
useEffect,
|
|
18
|
+
useRef,
|
|
19
|
+
useState
|
|
20
|
+
} from "react";
|
|
21
|
+
var ERROR_STYLE = {
|
|
22
|
+
padding: "16px",
|
|
23
|
+
border: "1px solid #fde2e2",
|
|
24
|
+
borderRadius: "4px",
|
|
25
|
+
background: "#fef0f0",
|
|
26
|
+
color: "#c45656",
|
|
27
|
+
fontSize: "13px",
|
|
28
|
+
lineHeight: "1.6",
|
|
29
|
+
boxSizing: "border-box"
|
|
30
|
+
};
|
|
31
|
+
var MESSAGE_STYLE = { margin: "0 0 8px", wordBreak: "break-all" };
|
|
32
|
+
var BUTTON_STYLE = {
|
|
33
|
+
padding: "4px 14px",
|
|
34
|
+
marginRight: "8px",
|
|
35
|
+
border: "1px solid #c45656",
|
|
36
|
+
borderRadius: "4px",
|
|
37
|
+
background: "#c45656",
|
|
38
|
+
color: "#fff",
|
|
39
|
+
fontSize: "13px",
|
|
40
|
+
cursor: "pointer"
|
|
41
|
+
};
|
|
42
|
+
var SECONDARY_BUTTON_STYLE = {
|
|
43
|
+
padding: "4px 14px",
|
|
44
|
+
border: "1px solid #c45656",
|
|
45
|
+
borderRadius: "4px",
|
|
46
|
+
background: "#fff",
|
|
47
|
+
color: "#c45656",
|
|
48
|
+
fontSize: "13px",
|
|
49
|
+
cursor: "pointer"
|
|
50
|
+
};
|
|
51
|
+
function DefaultBridgeErrorView({ error, retry }) {
|
|
52
|
+
const err = error;
|
|
53
|
+
const code = err?.code ?? "UNKNOWN";
|
|
54
|
+
const message = err?.message ?? String(error ?? "unknown error");
|
|
55
|
+
return createElement("div", { style: ERROR_STYLE, "data-fulgurjs-error": code }, [
|
|
56
|
+
createElement("p", { key: "t", style: { margin: "0 0 4px", fontWeight: 600 } }, `\u6865\u63A5\u5E94\u7528\u52A0\u8F7D\u5931\u8D25\uFF08\u9519\u8BEF\u7801 ${code}\uFF09`),
|
|
57
|
+
createElement("p", { key: "m", style: MESSAGE_STYLE }, message),
|
|
58
|
+
createElement(
|
|
59
|
+
"p",
|
|
60
|
+
{ key: "n", style: { margin: "0 0 10px" } },
|
|
61
|
+
"\u300C\u91CD\u8BD5\u52A0\u8F7D\u300D\u5728\u5F53\u524D\u9875\u9762\u91CD\u5EFA\u6302\u8F7D\uFF1B\u82E5\u6D4F\u89C8\u5668\u5DF2\u7F13\u5B58\u5931\u8D25\u7684\u6A21\u5757\uFF0C\u8BF7\u7528\u300C\u5237\u65B0\u9875\u9762\u91CD\u8BD5\u300D\u2014\u2014\u5B83\u4F1A\u6574\u9875\u5237\u65B0\uFF08\u4FDD\u7559\u5F53\u524D\u5730\u5740\uFF09\uFF0C\u672A\u4FDD\u5B58\u7684\u9875\u9762\u72B6\u6001\u4F1A\u4E22\u5931\u3002"
|
|
62
|
+
),
|
|
63
|
+
createElement("button", { key: "r", type: "button", style: BUTTON_STYLE, "data-fulgurjs-retry": "", onClick: retry }, "\u91CD\u8BD5\u52A0\u8F7D"),
|
|
64
|
+
createElement("button", {
|
|
65
|
+
key: "rl",
|
|
66
|
+
type: "button",
|
|
67
|
+
style: SECONDARY_BUTTON_STYLE,
|
|
68
|
+
"data-fulgurjs-reload": "",
|
|
69
|
+
onClick: () => window.location.reload()
|
|
70
|
+
}, "\u5237\u65B0\u9875\u9762\u91CD\u8BD5")
|
|
71
|
+
]);
|
|
72
|
+
}
|
|
73
|
+
function renderBridgeErrorNode(errorOpt, error, retry) {
|
|
74
|
+
if (typeof errorOpt === "function") return errorOpt(error, retry);
|
|
75
|
+
if (errorOpt !== void 0) return errorOpt;
|
|
76
|
+
return createElement(DefaultBridgeErrorView, { error, retry });
|
|
77
|
+
}
|
|
78
|
+
function createReactBridgeAppWithLoader(loadRemote) {
|
|
79
|
+
return function createReactBridgeApp(spec, options = {}) {
|
|
80
|
+
assertBridgeOptions(`createReactBridgeApp("${spec}")`, options);
|
|
81
|
+
const displayName = "FulgurjsBridge_" + spec.replace(/[^A-Za-z0-9_-]/g, "_");
|
|
82
|
+
function BridgeHost(props) {
|
|
83
|
+
const [status, setStatus] = useState("idle");
|
|
84
|
+
const [error, setError] = useState(void 0);
|
|
85
|
+
const [attempt, setAttempt] = useState(0);
|
|
86
|
+
const containerRef = useRef(null);
|
|
87
|
+
const generationRef = useRef(0);
|
|
88
|
+
const contractRef = useRef(void 0);
|
|
89
|
+
const acquiredRef = useRef(void 0);
|
|
90
|
+
const blockedRef = useRef(false);
|
|
91
|
+
const latest = useRef(props);
|
|
92
|
+
latest.current = props;
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
if (blockedRef.current) return;
|
|
95
|
+
const myGen = ++generationRef.current;
|
|
96
|
+
const el = containerRef.current;
|
|
97
|
+
const release = () => {
|
|
98
|
+
if (acquiredRef.current !== void 0) {
|
|
99
|
+
releaseBridgeSession(acquiredRef.current);
|
|
100
|
+
acquiredRef.current = void 0;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const run = async () => {
|
|
104
|
+
if (!el) return;
|
|
105
|
+
const sk = latest.current.sessionKey;
|
|
106
|
+
setStatus("pending");
|
|
107
|
+
setError(void 0);
|
|
108
|
+
if (sk === null) {
|
|
109
|
+
setStatus("idle");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
assertControlledSessionKey(spec, sk);
|
|
114
|
+
const controlled = typeof sk === "string" ? sk : void 0;
|
|
115
|
+
if (controlled !== void 0 && controlled !== acquiredRef.current) {
|
|
116
|
+
release();
|
|
117
|
+
acquireBridgeSession(spec, controlled);
|
|
118
|
+
acquiredRef.current = controlled;
|
|
119
|
+
}
|
|
120
|
+
const resolution = resolveBridgeContext(spec, controlled, options.getContext);
|
|
121
|
+
if (resolution.provide) provideAppContext(resolution.provide);
|
|
122
|
+
const load = loadRemote(spec, { retries: options.retries });
|
|
123
|
+
const mod = options.timeout !== void 0 ? await withBridgeTimeout(load, options.timeout, spec) : await load;
|
|
124
|
+
if (generationRef.current !== myGen) return;
|
|
125
|
+
const contract = assertBridgeContract(spec, mod);
|
|
126
|
+
contractRef.current = contract;
|
|
127
|
+
await contract.mount(el, { ...latest.current.appProps ?? {} });
|
|
128
|
+
if (generationRef.current !== myGen) return;
|
|
129
|
+
setStatus("ready");
|
|
130
|
+
} catch (e) {
|
|
131
|
+
if (generationRef.current !== myGen) return;
|
|
132
|
+
setError(e);
|
|
133
|
+
setStatus("error");
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
void run();
|
|
137
|
+
return () => {
|
|
138
|
+
generationRef.current++;
|
|
139
|
+
release();
|
|
140
|
+
const c = contractRef.current;
|
|
141
|
+
contractRef.current = void 0;
|
|
142
|
+
if (c && el) {
|
|
143
|
+
try {
|
|
144
|
+
c.unmount(el);
|
|
145
|
+
} catch (e) {
|
|
146
|
+
const err = bridgeLifecycleError("unmount", spec, e instanceof Error ? e : String(e));
|
|
147
|
+
console.error("[fulgurjs] \u6865\u63A5\u5E94\u7528\u5378\u8F7D\u5931\u8D25\uFF1A", err);
|
|
148
|
+
blockedRef.current = true;
|
|
149
|
+
setError(err);
|
|
150
|
+
setStatus("error");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}, [props.sessionKey, attempt]);
|
|
155
|
+
const retry = () => setAttempt((a) => a + 1);
|
|
156
|
+
const children = [];
|
|
157
|
+
if (status === "pending" && options.fallback !== void 0) {
|
|
158
|
+
children.push(createElement(Fragment, { key: "loading" }, options.fallback));
|
|
159
|
+
}
|
|
160
|
+
if (status === "error") {
|
|
161
|
+
children.push(createElement(Fragment, { key: "error" }, renderBridgeErrorNode(options.error, error, retry)));
|
|
162
|
+
}
|
|
163
|
+
children.push(createElement("div", {
|
|
164
|
+
key: "bridge-root",
|
|
165
|
+
ref: containerRef,
|
|
166
|
+
"data-fulgurjs-bridge-root": spec,
|
|
167
|
+
"data-fulgurjs-bridge-status": status
|
|
168
|
+
}));
|
|
169
|
+
return createElement(Fragment, null, children);
|
|
170
|
+
}
|
|
171
|
+
;
|
|
172
|
+
BridgeHost.displayName = displayName;
|
|
173
|
+
return BridgeHost;
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export {
|
|
178
|
+
createReactBridgeAppWithLoader
|
|
179
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bridgeLifecycleError,
|
|
3
|
+
bridgeSessionMismatchError,
|
|
4
|
+
invalidBridgeContractError,
|
|
5
|
+
invalidSessionKeyError
|
|
6
|
+
} from "./chunk-E55P4TIY.js";
|
|
7
|
+
import {
|
|
8
|
+
clearAppContext
|
|
9
|
+
} from "./chunk-TSYLDY6H.js";
|
|
10
|
+
|
|
11
|
+
// src/bridge-core.ts
|
|
12
|
+
function assertBridgeContract(spec, mod) {
|
|
13
|
+
const contract = mod?.default ?? mod;
|
|
14
|
+
if (contract === null || typeof contract !== "object" || typeof contract.mount !== "function" || typeof contract.unmount !== "function") {
|
|
15
|
+
throw invalidBridgeContractError(spec, mod);
|
|
16
|
+
}
|
|
17
|
+
return contract;
|
|
18
|
+
}
|
|
19
|
+
var activeBridgeSessions = /* @__PURE__ */ new Map();
|
|
20
|
+
function acquireBridgeSession(spec, sessionKey) {
|
|
21
|
+
for (const [active] of activeBridgeSessions) {
|
|
22
|
+
if (active !== sessionKey && (activeBridgeSessions.get(active) ?? 0) > 0) {
|
|
23
|
+
throw bridgeSessionMismatchError(spec, sessionKey, active, { reason: "active-instance" });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
activeBridgeSessions.set(sessionKey, (activeBridgeSessions.get(sessionKey) ?? 0) + 1);
|
|
27
|
+
}
|
|
28
|
+
function releaseBridgeSession(sessionKey) {
|
|
29
|
+
const n = activeBridgeSessions.get(sessionKey) ?? 0;
|
|
30
|
+
if (n <= 1) activeBridgeSessions.delete(sessionKey);
|
|
31
|
+
else activeBridgeSessions.set(sessionKey, n - 1);
|
|
32
|
+
}
|
|
33
|
+
function resolveBridgeContext(spec, controlled, getContext) {
|
|
34
|
+
if (getContext) {
|
|
35
|
+
let snapshot;
|
|
36
|
+
try {
|
|
37
|
+
snapshot = getContext();
|
|
38
|
+
} catch (e) {
|
|
39
|
+
throw bridgeLifecycleError("getContext", spec, e);
|
|
40
|
+
}
|
|
41
|
+
if (snapshot === null || typeof snapshot !== "object" || typeof snapshot.then === "function") {
|
|
42
|
+
throw bridgeLifecycleError(
|
|
43
|
+
"getContext",
|
|
44
|
+
spec,
|
|
45
|
+
`getContext \u8FD4\u56DE\u4E86${typeof snapshot.then === "function" ? " Promise/thenable" : "\u975E\u5BF9\u8C61\u503C\uFF08" + typeof snapshot + "\uFF09"}\uFF1B\u5B83\u5FC5\u987B\u662F\u540C\u6B65\u8FD4\u56DE\u5FEB\u7167\u5BF9\u8C61\u7684\u7EAF getter\u3002`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (controlled !== void 0 && snapshot.sessionKey !== controlled) {
|
|
49
|
+
throw bridgeSessionMismatchError(spec, controlled, snapshot.sessionKey, { reason: "getter-snapshot" });
|
|
50
|
+
}
|
|
51
|
+
const current = currentSessionKey();
|
|
52
|
+
if (snapshot.sessionKey !== void 0 && snapshot.sessionKey !== current) {
|
|
53
|
+
clearAppContext();
|
|
54
|
+
}
|
|
55
|
+
return { provide: snapshot };
|
|
56
|
+
}
|
|
57
|
+
if (controlled !== void 0) {
|
|
58
|
+
const current = currentSessionKey();
|
|
59
|
+
if (current !== controlled) {
|
|
60
|
+
throw bridgeSessionMismatchError(spec, controlled, current, { reason: "app-context" });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return {};
|
|
64
|
+
}
|
|
65
|
+
function currentSessionKey() {
|
|
66
|
+
const sk = (globalThis.__FULGURJS_APP_CONFIG__ ?? {}).sessionKey;
|
|
67
|
+
return typeof sk === "string" && sk !== "" ? sk : void 0;
|
|
68
|
+
}
|
|
69
|
+
function assertControlledSessionKey(spec, value) {
|
|
70
|
+
if (value === void 0 || value === null) return;
|
|
71
|
+
if (typeof value === "string" && value !== "") return;
|
|
72
|
+
throw invalidSessionKeyError(spec, value);
|
|
73
|
+
}
|
|
74
|
+
function assertBridgeOptions(source, opts) {
|
|
75
|
+
if (opts.retries !== void 0 && (typeof opts.retries !== "number" || !Number.isInteger(opts.retries) || opts.retries < 0 || opts.retries > 10)) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`[fulgurjs] ${source} \u7684 retries \u914D\u7F6E\u65E0\u6548\uFF1A\u5F53\u524D\u4E3A ${String(opts.retries)}\uFF0C\u5E94\u4E3A 0 \u5230 10 \u7684\u6574\u6570\u3002\u8BF7\u4FEE\u6B63\u9009\u9879\u3002`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
if (opts.timeout !== void 0 && (typeof opts.timeout !== "number" || !Number.isFinite(opts.timeout) || opts.timeout <= 0)) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`[fulgurjs] ${source} \u7684 timeout \u914D\u7F6E\u65E0\u6548\uFF1A\u5F53\u524D\u4E3A ${String(opts.timeout)}\uFF0C\u5E94\u4E3A\u5927\u4E8E 0 \u7684\u6709\u9650\u6BEB\u79D2\u6570\u3002\u8BF7\u4FEE\u6B63\u9009\u9879\u3002`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function withBridgeTimeout(p, ms, spec) {
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
const timer = setTimeout(() => {
|
|
89
|
+
reject(new Error(
|
|
90
|
+
`[fulgurjs] \u6865\u63A5\u5E94\u7528 "${spec}" \u52A0\u8F7D\u7B49\u5F85\u8D85\u8FC7 ${ms} \u6BEB\u79D2\uFF08\u9002\u914D\u5C42 timeout\uFF09\u3002\u8BE5\u8D85\u65F6\u53EA\u7ED3\u675F\u672C\u6B21\u7B49\u5F85\uFF0C\u4E0D\u53D6\u6D88\u5DF2\u53D1\u51FA\u7684\u5171\u4EAB\u8BF7\u6C42\uFF1B\u53EF\u70B9\u51FB\u300C\u91CD\u8BD5\u52A0\u8F7D\u300D\u5EFA\u7ACB\u65B0\u7684\u5C1D\u8BD5\u3002`
|
|
91
|
+
));
|
|
92
|
+
}, ms);
|
|
93
|
+
p.then(
|
|
94
|
+
(v) => {
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
resolve(v);
|
|
97
|
+
},
|
|
98
|
+
(e) => {
|
|
99
|
+
clearTimeout(timer);
|
|
100
|
+
reject(e);
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export {
|
|
107
|
+
assertBridgeContract,
|
|
108
|
+
acquireBridgeSession,
|
|
109
|
+
releaseBridgeSession,
|
|
110
|
+
resolveBridgeContext,
|
|
111
|
+
assertControlledSessionKey,
|
|
112
|
+
assertBridgeOptions,
|
|
113
|
+
withBridgeTimeout
|
|
114
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/runtime/errors.ts
|
|
2
|
+
var FgError = class extends Error {
|
|
3
|
+
code;
|
|
4
|
+
details;
|
|
5
|
+
constructor(code, message, details, options) {
|
|
6
|
+
super(`[fulgurjs:${code}] ${message}`, options);
|
|
7
|
+
this.name = "FgError";
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.details = details;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
FgError
|
|
15
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FgError
|
|
3
|
+
} from "./chunk-OF2JCXTZ.js";
|
|
4
|
+
|
|
5
|
+
// src/context.ts
|
|
6
|
+
var ContextErrorCodes = {
|
|
7
|
+
/** context 必需字段缺失(三段式,修法指向宿主桥) */
|
|
8
|
+
CONTEXT_MISSING_KEY: "CC-001",
|
|
9
|
+
/** 运行时单例不可用(独立直开远程页,修法=经宿主联邦加载) */
|
|
10
|
+
CONTEXT_NO_RUNTIME: "CC-002"
|
|
11
|
+
};
|
|
12
|
+
var APP_CONTEXT_STORAGE_KEY = "__FULGURJS_APP_CONFIG__";
|
|
13
|
+
function requireRuntime() {
|
|
14
|
+
const rt = globalThis.__FULGURJS_RUNTIME__;
|
|
15
|
+
if (!rt || typeof rt.loadRemote !== "function") {
|
|
16
|
+
throw new FgError(
|
|
17
|
+
ContextErrorCodes.CONTEXT_NO_RUNTIME,
|
|
18
|
+
"\u73B0\u8C61\uFF1A\u5F53\u524D\u9875\u9762\u627E\u4E0D\u5230 fulgurjs \u8FD0\u884C\u65F6\u5355\u4F8B\uFF08globalThis.__FULGURJS_RUNTIME__ \u672A\u5B9A\u4E49\uFF09\u3002\n \u6839\u56E0: \u5F53\u524D\u9875\u9762\u6CA1\u6709\u7ECF\u5BBF\u4E3B\u7684\u8054\u90A6\u8FD0\u884C\u65F6\u52A0\u8F7D\uFF08\u72EC\u7ACB\u76F4\u5F00\u8FDC\u7A0B\u9875\uFF0C\u6216\u5BBF\u4E3B\u6865\u665A\u4E8E\u672C\u8C03\u7528\u6267\u884C\uFF09\u3002\n \u4FEE\u6CD5: \u2460 \u4ECE\u5BBF\u4E3B\u5E94\u7528\u7684\u8054\u90A6\u8DEF\u7531\u6253\u5F00\u672C\u9875\u9762\uFF08\u5BBF\u4E3B\u6865\u4F1A\u5148\u52A0\u8F7D\u8FD0\u884C\u65F6\u5E76\u63D0\u4F9B context\uFF09\uFF1B\n \u2461 \u82E5\u4F60\u662F\u5BBF\u4E3B\u6865\u4F5C\u8005\uFF1A\u628A provideAppContext \u653E\u5728\u6865\u521D\u59CB\u5316\u5C3E\u90E8\uFF08\u65F6\u5E8F\u5951\u7EA6 bridge \u2192 \u8FDC\u7A0B setup \u2192 \u9875\u9762\u6A21\u5757\uFF09\u3002",
|
|
19
|
+
{ runtime: false }
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
return rt;
|
|
23
|
+
}
|
|
24
|
+
function provideAppContext(config) {
|
|
25
|
+
requireRuntime();
|
|
26
|
+
const g = globalThis;
|
|
27
|
+
g[APP_CONTEXT_STORAGE_KEY] = { ...g[APP_CONTEXT_STORAGE_KEY] ?? {}, ...config };
|
|
28
|
+
}
|
|
29
|
+
function clearAppContext() {
|
|
30
|
+
const g = globalThis;
|
|
31
|
+
delete g[APP_CONTEXT_STORAGE_KEY];
|
|
32
|
+
try {
|
|
33
|
+
g.__FULGURJS_RUNTIME__?.clearSessionState?.();
|
|
34
|
+
} catch {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function getAppContext() {
|
|
38
|
+
requireRuntime();
|
|
39
|
+
return globalThis[APP_CONTEXT_STORAGE_KEY] ?? {};
|
|
40
|
+
}
|
|
41
|
+
function requireAppContext(...keys) {
|
|
42
|
+
const ctx = getAppContext();
|
|
43
|
+
const missing = keys.filter((k) => ctx[k] === void 0);
|
|
44
|
+
if (missing.length === 0) return ctx;
|
|
45
|
+
const got = Object.keys(ctx);
|
|
46
|
+
const err = new FgError(
|
|
47
|
+
ContextErrorCodes.CONTEXT_MISSING_KEY,
|
|
48
|
+
`\u73B0\u8C61\uFF1AAppContext \u7F3A\u5C11\u5FC5\u9700\u5B57\u6BB5 ${missing.map((k) => `"${k}"`).join("\u3001")}\u3002
|
|
49
|
+
\u5F53\u524D\u5B57\u6BB5\uFF1A${got.length ? got.map((k) => `"${k}"`).join("\u3001") : "\u7A7A\uFF08\u5BBF\u4E3B\u6865\u53EF\u80FD\u5C1A\u672A\u8C03\u7528 provideAppContext\uFF09"}\u3002
|
|
50
|
+
\u9884\u671F\uFF1A\u5BBF\u4E3B\u6865\u5E94\u5728\u8FDC\u7A0B\u9875\u9762\u52A0\u8F7D\u524D\u63D0\u4F9B ${missing.map((k) => `"${k}"`).join("\u3001")}\u3002
|
|
51
|
+
\u793A\u4F8B\uFF1Ahost/src/fulgurjs/host/bridge.ts \u2192 provideAppContext({ ${keys.join(", ")}, ... })
|
|
52
|
+
\u4FEE\u6CD5: \u68C0\u67E5\u5BBF\u4E3B\u5E94\u7528\u7684 fulgurjs \u6865\u662F\u5426\u5728 loadRemote \u9875\u9762\u524D\u5B8C\u6210 context provide\uFF08\u65F6\u5E8F\u5951\u7EA6 bridge \u2192 \u8FDC\u7A0B setup \u2192 \u9875\u9762\u6A21\u5757\uFF09`,
|
|
53
|
+
{ missing, got }
|
|
54
|
+
);
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export {
|
|
59
|
+
ContextErrorCodes,
|
|
60
|
+
provideAppContext,
|
|
61
|
+
clearAppContext,
|
|
62
|
+
getAppContext,
|
|
63
|
+
requireAppContext
|
|
64
|
+
};
|