@module-federation/runtime-core 2.0.0 → 2.1.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/README.md +1 -1
- package/dist/_virtual/_rolldown/runtime.cjs +19 -0
- package/dist/_virtual/_rolldown/runtime.js +18 -0
- package/dist/constant.cjs +9 -0
- package/dist/constant.cjs.map +1 -0
- package/dist/constant.js +7 -0
- package/dist/constant.js.map +1 -0
- package/dist/core.cjs +153 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.ts +132 -0
- package/dist/core.js +153 -0
- package/dist/core.js.map +1 -0
- package/dist/global.cjs +160 -0
- package/dist/global.cjs.map +1 -0
- package/dist/global.d.ts +46 -0
- package/dist/global.js +142 -0
- package/dist/global.js.map +1 -0
- package/dist/helpers.cjs +44 -0
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.ts +32 -0
- package/dist/helpers.js +44 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.cjs +59 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +30 -1
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/module/index.cjs +128 -0
- package/dist/module/index.cjs.map +1 -0
- package/dist/module/index.d.ts +32 -0
- package/dist/module/index.js +128 -0
- package/dist/module/index.js.map +1 -0
- package/dist/plugins/generate-preload-assets.cjs +182 -0
- package/dist/plugins/generate-preload-assets.cjs.map +1 -0
- package/dist/plugins/generate-preload-assets.js +182 -0
- package/dist/plugins/generate-preload-assets.js.map +1 -0
- package/dist/plugins/snapshot/SnapshotHandler.cjs +184 -0
- package/dist/plugins/snapshot/SnapshotHandler.cjs.map +1 -0
- package/dist/plugins/snapshot/SnapshotHandler.d.ts +68 -0
- package/dist/plugins/snapshot/SnapshotHandler.js +183 -0
- package/dist/plugins/snapshot/SnapshotHandler.js.map +1 -0
- package/dist/plugins/snapshot/index.cjs +62 -0
- package/dist/plugins/snapshot/index.cjs.map +1 -0
- package/dist/plugins/snapshot/index.js +61 -0
- package/dist/plugins/snapshot/index.js.map +1 -0
- package/dist/remote/index.cjs +303 -0
- package/dist/remote/index.cjs.map +1 -0
- package/dist/remote/index.d.ts +121 -0
- package/dist/remote/index.js +303 -0
- package/dist/remote/index.js.map +1 -0
- package/dist/shared/index.cjs +304 -0
- package/dist/shared/index.cjs.map +1 -0
- package/dist/shared/index.d.ts +85 -0
- package/dist/shared/index.js +304 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/type/config.d.ts +130 -0
- package/dist/type/index.cjs +13 -0
- package/dist/type/index.cjs.map +1 -0
- package/dist/type/index.d.ts +11 -0
- package/dist/type/index.js +8 -0
- package/dist/type/index.js.map +1 -0
- package/dist/type/plugin.d.ts +27 -0
- package/dist/type/preload.d.ts +31 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +5 -1
- package/dist/types.js +1 -0
- package/dist/utils/env.cjs +10 -0
- package/dist/utils/env.cjs.map +1 -0
- package/dist/utils/env.d.ts +1 -0
- package/dist/utils/env.js +10 -0
- package/dist/utils/env.js.map +1 -0
- package/dist/utils/hooks/asyncHook.cjs +23 -0
- package/dist/utils/hooks/asyncHook.cjs.map +1 -0
- package/dist/utils/hooks/asyncHook.d.ts +10 -0
- package/dist/utils/hooks/asyncHook.js +23 -0
- package/dist/utils/hooks/asyncHook.js.map +1 -0
- package/dist/utils/hooks/asyncWaterfallHooks.cjs +42 -0
- package/dist/utils/hooks/asyncWaterfallHooks.cjs.map +1 -0
- package/dist/utils/hooks/asyncWaterfallHooks.d.ts +12 -0
- package/dist/utils/hooks/asyncWaterfallHooks.js +42 -0
- package/dist/utils/hooks/asyncWaterfallHooks.js.map +1 -0
- package/dist/utils/hooks/index.cjs +5 -0
- package/dist/utils/hooks/index.d.ts +5 -0
- package/dist/utils/hooks/index.js +7 -0
- package/dist/utils/hooks/pluginSystem.cjs +37 -0
- package/dist/utils/hooks/pluginSystem.cjs.map +1 -0
- package/dist/utils/hooks/pluginSystem.d.ts +19 -0
- package/dist/utils/hooks/pluginSystem.js +37 -0
- package/dist/utils/hooks/pluginSystem.js.map +1 -0
- package/dist/utils/hooks/syncHook.cjs +36 -0
- package/dist/utils/hooks/syncHook.cjs.map +1 -0
- package/dist/utils/hooks/syncHook.d.ts +16 -0
- package/dist/utils/hooks/syncHook.js +35 -0
- package/dist/utils/hooks/syncHook.js.map +1 -0
- package/dist/utils/hooks/syncWaterfallHook.cjs +39 -0
- package/dist/utils/hooks/syncWaterfallHook.cjs.map +1 -0
- package/dist/utils/hooks/syncWaterfallHook.d.ts +11 -0
- package/dist/utils/hooks/syncWaterfallHook.js +38 -0
- package/dist/utils/hooks/syncWaterfallHook.js.map +1 -0
- package/dist/utils/index.cjs +7 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/load.cjs +177 -0
- package/dist/utils/load.cjs.map +1 -0
- package/dist/utils/load.d.ts +16 -0
- package/dist/utils/load.js +175 -0
- package/dist/utils/load.js.map +1 -0
- package/dist/utils/logger.cjs +28 -0
- package/dist/utils/logger.cjs.map +1 -0
- package/dist/utils/logger.d.ts +7 -0
- package/dist/utils/logger.js +25 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/manifest.cjs +51 -0
- package/dist/utils/manifest.cjs.map +1 -0
- package/dist/utils/manifest.d.ts +12 -0
- package/dist/utils/manifest.js +49 -0
- package/dist/utils/manifest.js.map +1 -0
- package/dist/utils/plugin.cjs +27 -0
- package/dist/utils/plugin.cjs.map +1 -0
- package/dist/utils/plugin.d.ts +2 -0
- package/dist/utils/plugin.js +27 -0
- package/dist/utils/plugin.js.map +1 -0
- package/dist/utils/preload.cjs +146 -0
- package/dist/utils/preload.cjs.map +1 -0
- package/dist/utils/preload.d.ts +10 -0
- package/dist/utils/preload.js +143 -0
- package/dist/utils/preload.js.map +1 -0
- package/dist/utils/semver/compare.cjs +48 -0
- package/dist/utils/semver/compare.cjs.map +1 -0
- package/dist/utils/semver/compare.js +47 -0
- package/dist/utils/semver/compare.js.map +1 -0
- package/dist/utils/semver/constants.cjs +41 -0
- package/dist/utils/semver/constants.cjs.map +1 -0
- package/dist/utils/semver/constants.js +31 -0
- package/dist/utils/semver/constants.js.map +1 -0
- package/dist/utils/semver/index.cjs +68 -0
- package/dist/utils/semver/index.cjs.map +1 -0
- package/dist/utils/semver/index.d.ts +5 -0
- package/dist/utils/semver/index.js +68 -0
- package/dist/utils/semver/index.js.map +1 -0
- package/dist/utils/semver/parser.cjs +104 -0
- package/dist/utils/semver/parser.cjs.map +1 -0
- package/dist/utils/semver/parser.js +96 -0
- package/dist/utils/semver/parser.js.map +1 -0
- package/dist/utils/semver/utils.cjs +28 -0
- package/dist/utils/semver/utils.cjs.map +1 -0
- package/dist/utils/semver/utils.js +24 -0
- package/dist/utils/semver/utils.js.map +1 -0
- package/dist/utils/share.cjs +284 -0
- package/dist/utils/share.cjs.map +1 -0
- package/dist/utils/share.d.ts +26 -0
- package/dist/utils/share.js +278 -0
- package/dist/utils/share.js.map +1 -0
- package/dist/utils/tool.cjs +82 -0
- package/dist/utils/tool.cjs.map +1 -0
- package/dist/utils/tool.d.ts +9 -0
- package/dist/utils/tool.js +71 -0
- package/dist/utils/tool.js.map +1 -0
- package/package.json +9 -9
- package/dist/index.cjs.cjs +0 -3428
- package/dist/index.cjs.cjs.map +0 -1
- package/dist/index.cjs.d.ts +0 -1
- package/dist/index.esm.js +0 -3404
- package/dist/index.esm.js.map +0 -1
- package/dist/src/constant.d.ts +0 -2
- package/dist/src/core.d.ts +0 -119
- package/dist/src/global.d.ts +0 -42
- package/dist/src/helpers.d.ts +0 -38
- package/dist/src/index.d.ts +0 -14
- package/dist/src/module/index.d.ts +0 -24
- package/dist/src/plugins/generate-preload-assets.d.ts +0 -8
- package/dist/src/plugins/snapshot/SnapshotHandler.d.ts +0 -60
- package/dist/src/plugins/snapshot/index.d.ts +0 -5
- package/dist/src/remote/index.d.ts +0 -109
- package/dist/src/shared/index.d.ts +0 -75
- package/dist/src/type/config.d.ts +0 -128
- package/dist/src/type/index.d.ts +0 -3
- package/dist/src/type/plugin.d.ts +0 -35
- package/dist/src/type/preload.d.ts +0 -26
- package/dist/src/types.d.ts +0 -1
- package/dist/src/utils/env.d.ts +0 -3
- package/dist/src/utils/hooks/asyncHook.d.ts +0 -6
- package/dist/src/utils/hooks/asyncWaterfallHooks.d.ts +0 -10
- package/dist/src/utils/hooks/index.d.ts +0 -6
- package/dist/src/utils/hooks/pluginSystem.d.ts +0 -16
- package/dist/src/utils/hooks/syncHook.d.ts +0 -12
- package/dist/src/utils/hooks/syncWaterfallHook.d.ts +0 -9
- package/dist/src/utils/index.d.ts +0 -6
- package/dist/src/utils/load.d.ts +0 -11
- package/dist/src/utils/logger.d.ts +0 -6
- package/dist/src/utils/manifest.d.ts +0 -7
- package/dist/src/utils/plugin.d.ts +0 -3
- package/dist/src/utils/preload.d.ts +0 -6
- package/dist/src/utils/semver/compare.d.ts +0 -9
- package/dist/src/utils/semver/constants.d.ts +0 -10
- package/dist/src/utils/semver/index.d.ts +0 -2
- package/dist/src/utils/semver/parser.d.ts +0 -9
- package/dist/src/utils/semver/utils.d.ts +0 -11
- package/dist/src/utils/share.d.ts +0 -45
- package/dist/src/utils/tool.d.ts +0 -18
- package/dist/types.cjs.cjs +0 -3
- package/dist/types.cjs.cjs.map +0 -1
- package/dist/types.cjs.d.ts +0 -1
- package/dist/types.esm.js +0 -2
- package/dist/types.esm.js.map +0 -1
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { assert, error } from "../../utils/logger.js";
|
|
2
|
+
import { isRemoteInfoWithEntry } from "../../utils/tool.js";
|
|
3
|
+
import { Global, addGlobalSnapshot, getGlobalSnapshot, getGlobalSnapshotInfoByModuleInfo, getInfoWithoutType, setGlobalSnapshotInfoByModuleInfo } from "../../global.js";
|
|
4
|
+
import "../../utils/index.js";
|
|
5
|
+
import { AsyncHook } from "../../utils/hooks/asyncHook.js";
|
|
6
|
+
import { AsyncWaterfallHook } from "../../utils/hooks/asyncWaterfallHooks.js";
|
|
7
|
+
import { PluginSystem } from "../../utils/hooks/pluginSystem.js";
|
|
8
|
+
import "../../utils/hooks/index.js";
|
|
9
|
+
import { generateSnapshotFromManifest, isBrowserEnv, isManifestProvider } from "@module-federation/sdk";
|
|
10
|
+
import { RUNTIME_003, RUNTIME_007, getShortErrorMsg, runtimeDescMap } from "@module-federation/error-codes";
|
|
11
|
+
|
|
12
|
+
//#region src/plugins/snapshot/SnapshotHandler.ts
|
|
13
|
+
function getGlobalRemoteInfo(moduleInfo, origin) {
|
|
14
|
+
const hostGlobalSnapshot = getGlobalSnapshotInfoByModuleInfo({
|
|
15
|
+
name: origin.name,
|
|
16
|
+
version: origin.options.version
|
|
17
|
+
});
|
|
18
|
+
const globalRemoteInfo = hostGlobalSnapshot && "remotesInfo" in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;
|
|
19
|
+
if (globalRemoteInfo && globalRemoteInfo.matchedVersion) return {
|
|
20
|
+
hostGlobalSnapshot,
|
|
21
|
+
globalSnapshot: getGlobalSnapshot(),
|
|
22
|
+
remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
|
|
23
|
+
name: moduleInfo.name,
|
|
24
|
+
version: globalRemoteInfo.matchedVersion
|
|
25
|
+
})
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
hostGlobalSnapshot: void 0,
|
|
29
|
+
globalSnapshot: getGlobalSnapshot(),
|
|
30
|
+
remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({
|
|
31
|
+
name: moduleInfo.name,
|
|
32
|
+
version: "version" in moduleInfo ? moduleInfo.version : void 0
|
|
33
|
+
})
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
var SnapshotHandler = class {
|
|
37
|
+
constructor(HostInstance) {
|
|
38
|
+
this.loadingHostSnapshot = null;
|
|
39
|
+
this.manifestCache = /* @__PURE__ */ new Map();
|
|
40
|
+
this.hooks = new PluginSystem({
|
|
41
|
+
beforeLoadRemoteSnapshot: new AsyncHook("beforeLoadRemoteSnapshot"),
|
|
42
|
+
loadSnapshot: new AsyncWaterfallHook("loadGlobalSnapshot"),
|
|
43
|
+
loadRemoteSnapshot: new AsyncWaterfallHook("loadRemoteSnapshot"),
|
|
44
|
+
afterLoadSnapshot: new AsyncWaterfallHook("afterLoadSnapshot")
|
|
45
|
+
});
|
|
46
|
+
this.manifestLoading = Global.__FEDERATION__.__MANIFEST_LOADING__;
|
|
47
|
+
this.HostInstance = HostInstance;
|
|
48
|
+
this.loaderHook = HostInstance.loaderHook;
|
|
49
|
+
}
|
|
50
|
+
async loadRemoteSnapshotInfo({ moduleInfo, id, expose }) {
|
|
51
|
+
const { options } = this.HostInstance;
|
|
52
|
+
await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({
|
|
53
|
+
options,
|
|
54
|
+
moduleInfo
|
|
55
|
+
});
|
|
56
|
+
let hostSnapshot = getGlobalSnapshotInfoByModuleInfo({
|
|
57
|
+
name: this.HostInstance.options.name,
|
|
58
|
+
version: this.HostInstance.options.version
|
|
59
|
+
});
|
|
60
|
+
if (!hostSnapshot) {
|
|
61
|
+
hostSnapshot = {
|
|
62
|
+
version: this.HostInstance.options.version || "",
|
|
63
|
+
remoteEntry: "",
|
|
64
|
+
remotesInfo: {}
|
|
65
|
+
};
|
|
66
|
+
addGlobalSnapshot({ [this.HostInstance.options.name]: hostSnapshot });
|
|
67
|
+
}
|
|
68
|
+
if (hostSnapshot && "remotesInfo" in hostSnapshot && !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value) {
|
|
69
|
+
if ("version" in moduleInfo || "entry" in moduleInfo) hostSnapshot.remotesInfo = {
|
|
70
|
+
...hostSnapshot?.remotesInfo,
|
|
71
|
+
[moduleInfo.name]: { matchedVersion: "version" in moduleInfo ? moduleInfo.version : moduleInfo.entry }
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } = this.getGlobalRemoteInfo(moduleInfo);
|
|
75
|
+
const { remoteSnapshot: globalRemoteSnapshot, globalSnapshot: globalSnapshotRes } = await this.hooks.lifecycle.loadSnapshot.emit({
|
|
76
|
+
options,
|
|
77
|
+
moduleInfo,
|
|
78
|
+
hostGlobalSnapshot,
|
|
79
|
+
remoteSnapshot,
|
|
80
|
+
globalSnapshot
|
|
81
|
+
});
|
|
82
|
+
let mSnapshot;
|
|
83
|
+
let gSnapshot;
|
|
84
|
+
if (globalRemoteSnapshot) if (isManifestProvider(globalRemoteSnapshot)) {
|
|
85
|
+
const remoteEntry = isBrowserEnv() ? globalRemoteSnapshot.remoteEntry : globalRemoteSnapshot.ssrRemoteEntry || globalRemoteSnapshot.remoteEntry || "";
|
|
86
|
+
const moduleSnapshot = await this.getManifestJson(remoteEntry, moduleInfo, {});
|
|
87
|
+
const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo({
|
|
88
|
+
...moduleInfo,
|
|
89
|
+
entry: remoteEntry
|
|
90
|
+
}, moduleSnapshot);
|
|
91
|
+
mSnapshot = moduleSnapshot;
|
|
92
|
+
gSnapshot = globalSnapshotRes;
|
|
93
|
+
} else {
|
|
94
|
+
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
95
|
+
options: this.HostInstance.options,
|
|
96
|
+
moduleInfo,
|
|
97
|
+
remoteSnapshot: globalRemoteSnapshot,
|
|
98
|
+
from: "global"
|
|
99
|
+
});
|
|
100
|
+
mSnapshot = remoteSnapshotRes;
|
|
101
|
+
gSnapshot = globalSnapshotRes;
|
|
102
|
+
}
|
|
103
|
+
else if (isRemoteInfoWithEntry(moduleInfo)) {
|
|
104
|
+
const moduleSnapshot = await this.getManifestJson(moduleInfo.entry, moduleInfo, {});
|
|
105
|
+
const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(moduleInfo, moduleSnapshot);
|
|
106
|
+
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
107
|
+
options: this.HostInstance.options,
|
|
108
|
+
moduleInfo,
|
|
109
|
+
remoteSnapshot: moduleSnapshot,
|
|
110
|
+
from: "global"
|
|
111
|
+
});
|
|
112
|
+
mSnapshot = remoteSnapshotRes;
|
|
113
|
+
gSnapshot = globalSnapshotRes;
|
|
114
|
+
} else error(getShortErrorMsg(RUNTIME_007, runtimeDescMap, {
|
|
115
|
+
hostName: moduleInfo.name,
|
|
116
|
+
hostVersion: moduleInfo.version,
|
|
117
|
+
globalSnapshot: JSON.stringify(globalSnapshotRes)
|
|
118
|
+
}));
|
|
119
|
+
await this.hooks.lifecycle.afterLoadSnapshot.emit({
|
|
120
|
+
id,
|
|
121
|
+
host: this.HostInstance,
|
|
122
|
+
options,
|
|
123
|
+
moduleInfo,
|
|
124
|
+
remoteSnapshot: mSnapshot
|
|
125
|
+
});
|
|
126
|
+
return {
|
|
127
|
+
remoteSnapshot: mSnapshot,
|
|
128
|
+
globalSnapshot: gSnapshot
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
getGlobalRemoteInfo(moduleInfo) {
|
|
132
|
+
return getGlobalRemoteInfo(moduleInfo, this.HostInstance);
|
|
133
|
+
}
|
|
134
|
+
async getManifestJson(manifestUrl, moduleInfo, extraOptions) {
|
|
135
|
+
const getManifest = async () => {
|
|
136
|
+
let manifestJson = this.manifestCache.get(manifestUrl);
|
|
137
|
+
if (manifestJson) return manifestJson;
|
|
138
|
+
try {
|
|
139
|
+
let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {});
|
|
140
|
+
if (!res || !(res instanceof Response)) res = await fetch(manifestUrl, {});
|
|
141
|
+
manifestJson = await res.json();
|
|
142
|
+
} catch (err) {
|
|
143
|
+
manifestJson = await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit({
|
|
144
|
+
id: manifestUrl,
|
|
145
|
+
error: err,
|
|
146
|
+
from: "runtime",
|
|
147
|
+
lifecycle: "afterResolve",
|
|
148
|
+
origin: this.HostInstance
|
|
149
|
+
});
|
|
150
|
+
if (!manifestJson) {
|
|
151
|
+
delete this.manifestLoading[manifestUrl];
|
|
152
|
+
error(getShortErrorMsg(RUNTIME_003, runtimeDescMap, {
|
|
153
|
+
manifestUrl,
|
|
154
|
+
moduleName: moduleInfo.name,
|
|
155
|
+
hostName: this.HostInstance.options.name
|
|
156
|
+
}, `${err}`));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
assert(manifestJson.metaData && manifestJson.exposes && manifestJson.shared, `${manifestUrl} is not a federation manifest`);
|
|
160
|
+
this.manifestCache.set(manifestUrl, manifestJson);
|
|
161
|
+
return manifestJson;
|
|
162
|
+
};
|
|
163
|
+
const asyncLoadProcess = async () => {
|
|
164
|
+
const manifestJson = await getManifest();
|
|
165
|
+
const remoteSnapshot = generateSnapshotFromManifest(manifestJson, { version: manifestUrl });
|
|
166
|
+
const { remoteSnapshot: remoteSnapshotRes } = await this.hooks.lifecycle.loadRemoteSnapshot.emit({
|
|
167
|
+
options: this.HostInstance.options,
|
|
168
|
+
moduleInfo,
|
|
169
|
+
manifestJson,
|
|
170
|
+
remoteSnapshot,
|
|
171
|
+
manifestUrl,
|
|
172
|
+
from: "manifest"
|
|
173
|
+
});
|
|
174
|
+
return remoteSnapshotRes;
|
|
175
|
+
};
|
|
176
|
+
if (!this.manifestLoading[manifestUrl]) this.manifestLoading[manifestUrl] = asyncLoadProcess().then((res) => res);
|
|
177
|
+
return this.manifestLoading[manifestUrl];
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
export { SnapshotHandler, getGlobalRemoteInfo };
|
|
183
|
+
//# sourceMappingURL=SnapshotHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SnapshotHandler.js","names":[],"sources":["../../../src/plugins/snapshot/SnapshotHandler.ts"],"sourcesContent":["import {\n GlobalModuleInfo,\n Manifest,\n ModuleInfo,\n generateSnapshotFromManifest,\n isManifestProvider,\n isBrowserEnv,\n} from '@module-federation/sdk';\nimport {\n getShortErrorMsg,\n RUNTIME_003,\n RUNTIME_007,\n runtimeDescMap,\n} from '@module-federation/error-codes';\nimport { Options, Remote } from '../../type';\nimport { isRemoteInfoWithEntry, error } from '../../utils';\nimport {\n getGlobalSnapshot,\n setGlobalSnapshotInfoByModuleInfo,\n Global,\n addGlobalSnapshot,\n getGlobalSnapshotInfoByModuleInfo,\n getInfoWithoutType,\n} from '../../global';\nimport { PluginSystem, AsyncHook, AsyncWaterfallHook } from '../../utils/hooks';\nimport { ModuleFederation } from '../../core';\nimport { assert } from '../../utils/logger';\n\nexport function getGlobalRemoteInfo(\n moduleInfo: Remote,\n origin: ModuleFederation,\n): {\n hostGlobalSnapshot: ModuleInfo | undefined;\n globalSnapshot: ReturnType<typeof getGlobalSnapshot>;\n remoteSnapshot: GlobalModuleInfo[string] | undefined;\n} {\n const hostGlobalSnapshot = getGlobalSnapshotInfoByModuleInfo({\n name: origin.name,\n version: origin.options.version,\n });\n\n // get remote detail info from global\n const globalRemoteInfo =\n hostGlobalSnapshot &&\n 'remotesInfo' in hostGlobalSnapshot &&\n hostGlobalSnapshot.remotesInfo &&\n getInfoWithoutType(hostGlobalSnapshot.remotesInfo, moduleInfo.name).value;\n\n if (globalRemoteInfo && globalRemoteInfo.matchedVersion) {\n return {\n hostGlobalSnapshot,\n globalSnapshot: getGlobalSnapshot(),\n remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({\n name: moduleInfo.name,\n version: globalRemoteInfo.matchedVersion,\n }),\n };\n }\n\n return {\n hostGlobalSnapshot: undefined,\n globalSnapshot: getGlobalSnapshot(),\n remoteSnapshot: getGlobalSnapshotInfoByModuleInfo({\n name: moduleInfo.name,\n version: 'version' in moduleInfo ? moduleInfo.version : undefined,\n }),\n };\n}\n\nexport class SnapshotHandler {\n loadingHostSnapshot: Promise<GlobalModuleInfo | void> | null = null;\n HostInstance: ModuleFederation;\n manifestCache: Map<string, Manifest> = new Map();\n hooks = new PluginSystem({\n beforeLoadRemoteSnapshot: new AsyncHook<\n [\n {\n options: Options;\n moduleInfo: Remote;\n },\n ],\n void\n >('beforeLoadRemoteSnapshot'),\n loadSnapshot: new AsyncWaterfallHook<{\n options: Options;\n moduleInfo: Remote;\n hostGlobalSnapshot: GlobalModuleInfo[string] | undefined;\n globalSnapshot: ReturnType<typeof getGlobalSnapshot>;\n remoteSnapshot?: GlobalModuleInfo[string] | undefined;\n }>('loadGlobalSnapshot'),\n loadRemoteSnapshot: new AsyncWaterfallHook<{\n options: Options;\n moduleInfo: Remote;\n manifestJson?: Manifest;\n manifestUrl?: string;\n remoteSnapshot: ModuleInfo;\n from: 'global' | 'manifest';\n }>('loadRemoteSnapshot'),\n afterLoadSnapshot: new AsyncWaterfallHook<{\n id?: string;\n host: ModuleFederation;\n options: Options;\n moduleInfo: Remote;\n remoteSnapshot: ModuleInfo;\n }>('afterLoadSnapshot'),\n });\n loaderHook: ModuleFederation['loaderHook'];\n manifestLoading: Record<string, Promise<ModuleInfo>> =\n Global.__FEDERATION__.__MANIFEST_LOADING__;\n\n constructor(HostInstance: ModuleFederation) {\n this.HostInstance = HostInstance;\n this.loaderHook = HostInstance.loaderHook;\n }\n\n // eslint-disable-next-line max-lines-per-function\n async loadRemoteSnapshotInfo({\n moduleInfo,\n id,\n expose,\n }: {\n moduleInfo: Remote;\n id?: string;\n expose?: string;\n }):\n | Promise<{\n remoteSnapshot: ModuleInfo;\n globalSnapshot: GlobalModuleInfo;\n }>\n | never {\n const { options } = this.HostInstance;\n\n await this.hooks.lifecycle.beforeLoadRemoteSnapshot.emit({\n options,\n moduleInfo,\n });\n\n let hostSnapshot = getGlobalSnapshotInfoByModuleInfo({\n name: this.HostInstance.options.name,\n version: this.HostInstance.options.version,\n });\n\n if (!hostSnapshot) {\n hostSnapshot = {\n version: this.HostInstance.options.version || '',\n remoteEntry: '',\n remotesInfo: {},\n };\n addGlobalSnapshot({\n [this.HostInstance.options.name]: hostSnapshot,\n });\n }\n\n // In dynamic loadRemote scenarios, incomplete remotesInfo delivery may occur. In such cases, the remotesInfo in the host needs to be completed in the snapshot at runtime.\n // This ensures the snapshot's integrity and helps the chrome plugin correctly identify all producer modules, ensuring that proxyable producer modules will not be missing.\n if (\n hostSnapshot &&\n 'remotesInfo' in hostSnapshot &&\n !getInfoWithoutType(hostSnapshot.remotesInfo, moduleInfo.name).value\n ) {\n if ('version' in moduleInfo || 'entry' in moduleInfo) {\n hostSnapshot.remotesInfo = {\n ...hostSnapshot?.remotesInfo,\n [moduleInfo.name]: {\n matchedVersion:\n 'version' in moduleInfo ? moduleInfo.version : moduleInfo.entry,\n },\n };\n }\n }\n\n const { hostGlobalSnapshot, remoteSnapshot, globalSnapshot } =\n this.getGlobalRemoteInfo(moduleInfo);\n const {\n remoteSnapshot: globalRemoteSnapshot,\n globalSnapshot: globalSnapshotRes,\n } = await this.hooks.lifecycle.loadSnapshot.emit({\n options,\n moduleInfo,\n hostGlobalSnapshot,\n remoteSnapshot,\n globalSnapshot,\n });\n\n let mSnapshot;\n let gSnapshot;\n // global snapshot includes manifest or module info includes manifest\n if (globalRemoteSnapshot) {\n if (isManifestProvider(globalRemoteSnapshot)) {\n const remoteEntry = isBrowserEnv()\n ? globalRemoteSnapshot.remoteEntry\n : globalRemoteSnapshot.ssrRemoteEntry ||\n globalRemoteSnapshot.remoteEntry ||\n '';\n const moduleSnapshot = await this.getManifestJson(\n remoteEntry,\n moduleInfo,\n {},\n );\n // eslint-disable-next-line @typescript-eslint/no-shadow\n const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(\n {\n ...moduleInfo,\n // The global remote may be overridden\n // Therefore, set the snapshot key to the global address of the actual request\n entry: remoteEntry,\n },\n moduleSnapshot,\n );\n mSnapshot = moduleSnapshot;\n gSnapshot = globalSnapshotRes;\n } else {\n const { remoteSnapshot: remoteSnapshotRes } =\n await this.hooks.lifecycle.loadRemoteSnapshot.emit({\n options: this.HostInstance.options,\n moduleInfo,\n remoteSnapshot: globalRemoteSnapshot,\n from: 'global',\n });\n mSnapshot = remoteSnapshotRes;\n gSnapshot = globalSnapshotRes;\n }\n } else {\n if (isRemoteInfoWithEntry(moduleInfo)) {\n // get from manifest.json and merge remote info from remote server\n const moduleSnapshot = await this.getManifestJson(\n moduleInfo.entry,\n moduleInfo,\n {},\n );\n // eslint-disable-next-line @typescript-eslint/no-shadow\n const globalSnapshotRes = setGlobalSnapshotInfoByModuleInfo(\n moduleInfo,\n moduleSnapshot,\n );\n const { remoteSnapshot: remoteSnapshotRes } =\n await this.hooks.lifecycle.loadRemoteSnapshot.emit({\n options: this.HostInstance.options,\n moduleInfo,\n remoteSnapshot: moduleSnapshot,\n from: 'global',\n });\n\n mSnapshot = remoteSnapshotRes;\n gSnapshot = globalSnapshotRes;\n } else {\n error(\n getShortErrorMsg(RUNTIME_007, runtimeDescMap, {\n hostName: moduleInfo.name,\n hostVersion: moduleInfo.version,\n globalSnapshot: JSON.stringify(globalSnapshotRes),\n }),\n );\n }\n }\n\n await this.hooks.lifecycle.afterLoadSnapshot.emit({\n id,\n host: this.HostInstance,\n options,\n moduleInfo,\n remoteSnapshot: mSnapshot,\n });\n\n return {\n remoteSnapshot: mSnapshot,\n globalSnapshot: gSnapshot,\n };\n }\n\n getGlobalRemoteInfo(moduleInfo: Remote): {\n hostGlobalSnapshot: ModuleInfo | undefined;\n globalSnapshot: ReturnType<typeof getGlobalSnapshot>;\n remoteSnapshot: GlobalModuleInfo[string] | undefined;\n } {\n return getGlobalRemoteInfo(moduleInfo, this.HostInstance);\n }\n\n private async getManifestJson(\n manifestUrl: string,\n moduleInfo: Remote,\n extraOptions: Record<string, any>,\n ): Promise<ModuleInfo> {\n const getManifest = async (): Promise<Manifest> => {\n let manifestJson: Manifest | undefined =\n this.manifestCache.get(manifestUrl);\n if (manifestJson) {\n return manifestJson;\n }\n try {\n let res = await this.loaderHook.lifecycle.fetch.emit(manifestUrl, {});\n if (!res || !(res instanceof Response)) {\n res = await fetch(manifestUrl, {});\n }\n manifestJson = (await res.json()) as Manifest;\n } catch (err) {\n manifestJson =\n (await this.HostInstance.remoteHandler.hooks.lifecycle.errorLoadRemote.emit(\n {\n id: manifestUrl,\n error: err,\n from: 'runtime',\n lifecycle: 'afterResolve',\n origin: this.HostInstance,\n },\n )) as Manifest | undefined;\n\n if (!manifestJson) {\n delete this.manifestLoading[manifestUrl];\n error(\n getShortErrorMsg(\n RUNTIME_003,\n runtimeDescMap,\n {\n manifestUrl,\n moduleName: moduleInfo.name,\n hostName: this.HostInstance.options.name,\n },\n `${err}`,\n ),\n );\n }\n }\n\n assert(\n manifestJson.metaData && manifestJson.exposes && manifestJson.shared,\n `${manifestUrl} is not a federation manifest`,\n );\n this.manifestCache.set(manifestUrl, manifestJson);\n return manifestJson;\n };\n\n const asyncLoadProcess = async () => {\n const manifestJson = await getManifest();\n const remoteSnapshot = generateSnapshotFromManifest(manifestJson, {\n version: manifestUrl,\n });\n\n const { remoteSnapshot: remoteSnapshotRes } =\n await this.hooks.lifecycle.loadRemoteSnapshot.emit({\n options: this.HostInstance.options,\n moduleInfo,\n manifestJson,\n remoteSnapshot,\n manifestUrl,\n from: 'manifest',\n });\n return remoteSnapshotRes;\n };\n\n if (!this.manifestLoading[manifestUrl]) {\n this.manifestLoading[manifestUrl] = asyncLoadProcess().then((res) => res);\n }\n return this.manifestLoading[manifestUrl];\n }\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const require_logger = require('../../utils/logger.cjs');
|
|
2
|
+
const require_tool = require('../../utils/tool.cjs');
|
|
3
|
+
require('../../utils/index.cjs');
|
|
4
|
+
const require_preload = require('../../utils/preload.cjs');
|
|
5
|
+
let _module_federation_sdk = require("@module-federation/sdk");
|
|
6
|
+
|
|
7
|
+
//#region src/plugins/snapshot/index.ts
|
|
8
|
+
function assignRemoteInfo(remoteInfo, remoteSnapshot) {
|
|
9
|
+
const remoteEntryInfo = require_tool.getRemoteEntryInfoFromSnapshot(remoteSnapshot);
|
|
10
|
+
if (!remoteEntryInfo.url) require_logger.error(`The attribute remoteEntry of ${remoteInfo.name} must not be undefined.`);
|
|
11
|
+
let entryUrl = (0, _module_federation_sdk.getResourceUrl)(remoteSnapshot, remoteEntryInfo.url);
|
|
12
|
+
if (!(0, _module_federation_sdk.isBrowserEnv)() && !entryUrl.startsWith("http")) entryUrl = `https:${entryUrl}`;
|
|
13
|
+
remoteInfo.type = remoteEntryInfo.type;
|
|
14
|
+
remoteInfo.entryGlobalName = remoteEntryInfo.globalName;
|
|
15
|
+
remoteInfo.entry = entryUrl;
|
|
16
|
+
remoteInfo.version = remoteSnapshot.version;
|
|
17
|
+
remoteInfo.buildVersion = remoteSnapshot.buildVersion;
|
|
18
|
+
}
|
|
19
|
+
function snapshotPlugin() {
|
|
20
|
+
return {
|
|
21
|
+
name: "snapshot-plugin",
|
|
22
|
+
async afterResolve(args) {
|
|
23
|
+
const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;
|
|
24
|
+
if (!require_tool.isRemoteInfoWithEntry(remote) || !require_tool.isPureRemoteEntry(remote)) {
|
|
25
|
+
const { remoteSnapshot, globalSnapshot } = await origin.snapshotHandler.loadRemoteSnapshotInfo({
|
|
26
|
+
moduleInfo: remote,
|
|
27
|
+
id
|
|
28
|
+
});
|
|
29
|
+
assignRemoteInfo(remoteInfo, remoteSnapshot);
|
|
30
|
+
const preloadOptions = {
|
|
31
|
+
remote,
|
|
32
|
+
preloadConfig: {
|
|
33
|
+
nameOrAlias: pkgNameOrAlias,
|
|
34
|
+
exposes: [expose],
|
|
35
|
+
resourceCategory: "sync",
|
|
36
|
+
share: false,
|
|
37
|
+
depsRemote: false
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const assets = await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit({
|
|
41
|
+
origin,
|
|
42
|
+
preloadOptions,
|
|
43
|
+
remoteInfo,
|
|
44
|
+
remote,
|
|
45
|
+
remoteSnapshot,
|
|
46
|
+
globalSnapshot
|
|
47
|
+
});
|
|
48
|
+
if (assets) require_preload.preloadAssets(remoteInfo, origin, assets, false);
|
|
49
|
+
return {
|
|
50
|
+
...args,
|
|
51
|
+
remoteSnapshot
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return args;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
exports.assignRemoteInfo = assignRemoteInfo;
|
|
61
|
+
exports.snapshotPlugin = snapshotPlugin;
|
|
62
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["getRemoteEntryInfoFromSnapshot","isRemoteInfoWithEntry","isPureRemoteEntry"],"sources":["../../../src/plugins/snapshot/index.ts"],"sourcesContent":["import {\n ModuleInfo,\n getResourceUrl,\n isBrowserEnv,\n} from '@module-federation/sdk';\nimport { ModuleFederationRuntimePlugin } from '../../type/plugin';\nimport {\n error,\n isPureRemoteEntry,\n isRemoteInfoWithEntry,\n getRemoteEntryInfoFromSnapshot,\n} from '../../utils';\nimport { PreloadOptions, RemoteInfo } from '../../type';\nimport { preloadAssets } from '../../utils/preload';\n\nexport function assignRemoteInfo(\n remoteInfo: RemoteInfo,\n remoteSnapshot: ModuleInfo,\n): void {\n const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);\n if (!remoteEntryInfo.url) {\n error(\n `The attribute remoteEntry of ${remoteInfo.name} must not be undefined.`,\n );\n }\n\n let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);\n\n if (!isBrowserEnv() && !entryUrl.startsWith('http')) {\n entryUrl = `https:${entryUrl}`;\n }\n\n remoteInfo.type = remoteEntryInfo.type;\n remoteInfo.entryGlobalName = remoteEntryInfo.globalName;\n remoteInfo.entry = entryUrl;\n remoteInfo.version = remoteSnapshot.version;\n remoteInfo.buildVersion = remoteSnapshot.buildVersion;\n}\n\nexport function snapshotPlugin(): ModuleFederationRuntimePlugin {\n return {\n name: 'snapshot-plugin',\n async afterResolve(args) {\n const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;\n\n if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {\n const { remoteSnapshot, globalSnapshot } =\n await origin.snapshotHandler.loadRemoteSnapshotInfo({\n moduleInfo: remote,\n id,\n });\n\n assignRemoteInfo(remoteInfo, remoteSnapshot);\n // preloading assets\n const preloadOptions: PreloadOptions[0] = {\n remote,\n preloadConfig: {\n nameOrAlias: pkgNameOrAlias,\n exposes: [expose],\n resourceCategory: 'sync',\n share: false,\n depsRemote: false,\n },\n };\n\n const assets =\n await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit(\n {\n origin,\n preloadOptions,\n remoteInfo,\n remote,\n remoteSnapshot,\n globalSnapshot,\n },\n );\n\n if (assets) {\n preloadAssets(remoteInfo, origin, assets, false);\n }\n\n return {\n ...args,\n remoteSnapshot,\n };\n }\n\n return args;\n },\n };\n}\n"],"mappings":";;;;;;;AAeA,SAAgB,iBACd,YACA,gBACM;CACN,MAAM,kBAAkBA,4CAA+B,eAAe;AACtE,KAAI,CAAC,gBAAgB,IACnB,sBACE,gCAAgC,WAAW,KAAK,yBACjD;CAGH,IAAI,sDAA0B,gBAAgB,gBAAgB,IAAI;AAElE,KAAI,2CAAe,IAAI,CAAC,SAAS,WAAW,OAAO,CACjD,YAAW,SAAS;AAGtB,YAAW,OAAO,gBAAgB;AAClC,YAAW,kBAAkB,gBAAgB;AAC7C,YAAW,QAAQ;AACnB,YAAW,UAAU,eAAe;AACpC,YAAW,eAAe,eAAe;;AAG3C,SAAgB,iBAAgD;AAC9D,QAAO;EACL,MAAM;EACN,MAAM,aAAa,MAAM;GACvB,MAAM,EAAE,QAAQ,gBAAgB,QAAQ,QAAQ,YAAY,OAAO;AAEnE,OAAI,CAACC,mCAAsB,OAAO,IAAI,CAACC,+BAAkB,OAAO,EAAE;IAChE,MAAM,EAAE,gBAAgB,mBACtB,MAAM,OAAO,gBAAgB,uBAAuB;KAClD,YAAY;KACZ;KACD,CAAC;AAEJ,qBAAiB,YAAY,eAAe;IAE5C,MAAM,iBAAoC;KACxC;KACA,eAAe;MACb,aAAa;MACb,SAAS,CAAC,OAAO;MACjB,kBAAkB;MAClB,OAAO;MACP,YAAY;MACb;KACF;IAED,MAAM,SACJ,MAAM,OAAO,cAAc,MAAM,UAAU,sBAAsB,KAC/D;KACE;KACA;KACA;KACA;KACA;KACA;KACD,CACF;AAEH,QAAI,OACF,+BAAc,YAAY,QAAQ,QAAQ,MAAM;AAGlD,WAAO;KACL,GAAG;KACH;KACD;;AAGH,UAAO;;EAEV"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { error } from "../../utils/logger.js";
|
|
2
|
+
import { getRemoteEntryInfoFromSnapshot, isPureRemoteEntry, isRemoteInfoWithEntry } from "../../utils/tool.js";
|
|
3
|
+
import "../../utils/index.js";
|
|
4
|
+
import { preloadAssets } from "../../utils/preload.js";
|
|
5
|
+
import { getResourceUrl, isBrowserEnv } from "@module-federation/sdk";
|
|
6
|
+
|
|
7
|
+
//#region src/plugins/snapshot/index.ts
|
|
8
|
+
function assignRemoteInfo(remoteInfo, remoteSnapshot) {
|
|
9
|
+
const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);
|
|
10
|
+
if (!remoteEntryInfo.url) error(`The attribute remoteEntry of ${remoteInfo.name} must not be undefined.`);
|
|
11
|
+
let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);
|
|
12
|
+
if (!isBrowserEnv() && !entryUrl.startsWith("http")) entryUrl = `https:${entryUrl}`;
|
|
13
|
+
remoteInfo.type = remoteEntryInfo.type;
|
|
14
|
+
remoteInfo.entryGlobalName = remoteEntryInfo.globalName;
|
|
15
|
+
remoteInfo.entry = entryUrl;
|
|
16
|
+
remoteInfo.version = remoteSnapshot.version;
|
|
17
|
+
remoteInfo.buildVersion = remoteSnapshot.buildVersion;
|
|
18
|
+
}
|
|
19
|
+
function snapshotPlugin() {
|
|
20
|
+
return {
|
|
21
|
+
name: "snapshot-plugin",
|
|
22
|
+
async afterResolve(args) {
|
|
23
|
+
const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;
|
|
24
|
+
if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {
|
|
25
|
+
const { remoteSnapshot, globalSnapshot } = await origin.snapshotHandler.loadRemoteSnapshotInfo({
|
|
26
|
+
moduleInfo: remote,
|
|
27
|
+
id
|
|
28
|
+
});
|
|
29
|
+
assignRemoteInfo(remoteInfo, remoteSnapshot);
|
|
30
|
+
const preloadOptions = {
|
|
31
|
+
remote,
|
|
32
|
+
preloadConfig: {
|
|
33
|
+
nameOrAlias: pkgNameOrAlias,
|
|
34
|
+
exposes: [expose],
|
|
35
|
+
resourceCategory: "sync",
|
|
36
|
+
share: false,
|
|
37
|
+
depsRemote: false
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const assets = await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit({
|
|
41
|
+
origin,
|
|
42
|
+
preloadOptions,
|
|
43
|
+
remoteInfo,
|
|
44
|
+
remote,
|
|
45
|
+
remoteSnapshot,
|
|
46
|
+
globalSnapshot
|
|
47
|
+
});
|
|
48
|
+
if (assets) preloadAssets(remoteInfo, origin, assets, false);
|
|
49
|
+
return {
|
|
50
|
+
...args,
|
|
51
|
+
remoteSnapshot
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return args;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
export { assignRemoteInfo, snapshotPlugin };
|
|
61
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/plugins/snapshot/index.ts"],"sourcesContent":["import {\n ModuleInfo,\n getResourceUrl,\n isBrowserEnv,\n} from '@module-federation/sdk';\nimport { ModuleFederationRuntimePlugin } from '../../type/plugin';\nimport {\n error,\n isPureRemoteEntry,\n isRemoteInfoWithEntry,\n getRemoteEntryInfoFromSnapshot,\n} from '../../utils';\nimport { PreloadOptions, RemoteInfo } from '../../type';\nimport { preloadAssets } from '../../utils/preload';\n\nexport function assignRemoteInfo(\n remoteInfo: RemoteInfo,\n remoteSnapshot: ModuleInfo,\n): void {\n const remoteEntryInfo = getRemoteEntryInfoFromSnapshot(remoteSnapshot);\n if (!remoteEntryInfo.url) {\n error(\n `The attribute remoteEntry of ${remoteInfo.name} must not be undefined.`,\n );\n }\n\n let entryUrl = getResourceUrl(remoteSnapshot, remoteEntryInfo.url);\n\n if (!isBrowserEnv() && !entryUrl.startsWith('http')) {\n entryUrl = `https:${entryUrl}`;\n }\n\n remoteInfo.type = remoteEntryInfo.type;\n remoteInfo.entryGlobalName = remoteEntryInfo.globalName;\n remoteInfo.entry = entryUrl;\n remoteInfo.version = remoteSnapshot.version;\n remoteInfo.buildVersion = remoteSnapshot.buildVersion;\n}\n\nexport function snapshotPlugin(): ModuleFederationRuntimePlugin {\n return {\n name: 'snapshot-plugin',\n async afterResolve(args) {\n const { remote, pkgNameOrAlias, expose, origin, remoteInfo, id } = args;\n\n if (!isRemoteInfoWithEntry(remote) || !isPureRemoteEntry(remote)) {\n const { remoteSnapshot, globalSnapshot } =\n await origin.snapshotHandler.loadRemoteSnapshotInfo({\n moduleInfo: remote,\n id,\n });\n\n assignRemoteInfo(remoteInfo, remoteSnapshot);\n // preloading assets\n const preloadOptions: PreloadOptions[0] = {\n remote,\n preloadConfig: {\n nameOrAlias: pkgNameOrAlias,\n exposes: [expose],\n resourceCategory: 'sync',\n share: false,\n depsRemote: false,\n },\n };\n\n const assets =\n await origin.remoteHandler.hooks.lifecycle.generatePreloadAssets.emit(\n {\n origin,\n preloadOptions,\n remoteInfo,\n remote,\n remoteSnapshot,\n globalSnapshot,\n },\n );\n\n if (assets) {\n preloadAssets(remoteInfo, origin, assets, false);\n }\n\n return {\n ...args,\n remoteSnapshot,\n };\n }\n\n return args;\n },\n };\n}\n"],"mappings":";;;;;;;AAeA,SAAgB,iBACd,YACA,gBACM;CACN,MAAM,kBAAkB,+BAA+B,eAAe;AACtE,KAAI,CAAC,gBAAgB,IACnB,OACE,gCAAgC,WAAW,KAAK,yBACjD;CAGH,IAAI,WAAW,eAAe,gBAAgB,gBAAgB,IAAI;AAElE,KAAI,CAAC,cAAc,IAAI,CAAC,SAAS,WAAW,OAAO,CACjD,YAAW,SAAS;AAGtB,YAAW,OAAO,gBAAgB;AAClC,YAAW,kBAAkB,gBAAgB;AAC7C,YAAW,QAAQ;AACnB,YAAW,UAAU,eAAe;AACpC,YAAW,eAAe,eAAe;;AAG3C,SAAgB,iBAAgD;AAC9D,QAAO;EACL,MAAM;EACN,MAAM,aAAa,MAAM;GACvB,MAAM,EAAE,QAAQ,gBAAgB,QAAQ,QAAQ,YAAY,OAAO;AAEnE,OAAI,CAAC,sBAAsB,OAAO,IAAI,CAAC,kBAAkB,OAAO,EAAE;IAChE,MAAM,EAAE,gBAAgB,mBACtB,MAAM,OAAO,gBAAgB,uBAAuB;KAClD,YAAY;KACZ;KACD,CAAC;AAEJ,qBAAiB,YAAY,eAAe;IAE5C,MAAM,iBAAoC;KACxC;KACA,eAAe;MACb,aAAa;MACb,SAAS,CAAC,OAAO;MACjB,kBAAkB;MAClB,OAAO;MACP,YAAY;MACb;KACF;IAED,MAAM,SACJ,MAAM,OAAO,cAAc,MAAM,UAAU,sBAAsB,KAC/D;KACE;KACA;KACA;KACA;KACA;KACA;KACD,CACF;AAEH,QAAI,OACF,eAAc,YAAY,QAAQ,QAAQ,MAAM;AAGlD,WAAO;KACL,GAAG;KACH;KACD;;AAGH,UAAO;;EAEV"}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
const require_logger = require('../utils/logger.cjs');
|
|
2
|
+
const require_global = require('../global.cjs');
|
|
3
|
+
const require_constant = require('../constant.cjs');
|
|
4
|
+
const require_share = require('../utils/share.cjs');
|
|
5
|
+
const require_manifest = require('../utils/manifest.cjs');
|
|
6
|
+
const require_load = require('../utils/load.cjs');
|
|
7
|
+
require('../utils/index.cjs');
|
|
8
|
+
const require_preload = require('../utils/preload.cjs');
|
|
9
|
+
const require_index$1 = require('../module/index.cjs');
|
|
10
|
+
const require_syncHook = require('../utils/hooks/syncHook.cjs');
|
|
11
|
+
const require_asyncHook = require('../utils/hooks/asyncHook.cjs');
|
|
12
|
+
const require_syncWaterfallHook = require('../utils/hooks/syncWaterfallHook.cjs');
|
|
13
|
+
const require_asyncWaterfallHooks = require('../utils/hooks/asyncWaterfallHooks.cjs');
|
|
14
|
+
const require_pluginSystem = require('../utils/hooks/pluginSystem.cjs');
|
|
15
|
+
require('../utils/hooks/index.cjs');
|
|
16
|
+
const require_SnapshotHandler = require('../plugins/snapshot/SnapshotHandler.cjs');
|
|
17
|
+
let _module_federation_sdk = require("@module-federation/sdk");
|
|
18
|
+
let _module_federation_error_codes = require("@module-federation/error-codes");
|
|
19
|
+
|
|
20
|
+
//#region src/remote/index.ts
|
|
21
|
+
var RemoteHandler = class {
|
|
22
|
+
constructor(host) {
|
|
23
|
+
this.hooks = new require_pluginSystem.PluginSystem({
|
|
24
|
+
beforeRegisterRemote: new require_syncWaterfallHook.SyncWaterfallHook("beforeRegisterRemote"),
|
|
25
|
+
registerRemote: new require_syncWaterfallHook.SyncWaterfallHook("registerRemote"),
|
|
26
|
+
beforeRequest: new require_asyncWaterfallHooks.AsyncWaterfallHook("beforeRequest"),
|
|
27
|
+
onLoad: new require_asyncHook.AsyncHook("onLoad"),
|
|
28
|
+
handlePreloadModule: new require_syncHook.SyncHook("handlePreloadModule"),
|
|
29
|
+
errorLoadRemote: new require_asyncHook.AsyncHook("errorLoadRemote"),
|
|
30
|
+
beforePreloadRemote: new require_asyncHook.AsyncHook("beforePreloadRemote"),
|
|
31
|
+
generatePreloadAssets: new require_asyncHook.AsyncHook("generatePreloadAssets"),
|
|
32
|
+
afterPreloadRemote: new require_asyncHook.AsyncHook(),
|
|
33
|
+
loadEntry: new require_asyncHook.AsyncHook()
|
|
34
|
+
});
|
|
35
|
+
this.host = host;
|
|
36
|
+
this.idToRemoteMap = {};
|
|
37
|
+
}
|
|
38
|
+
formatAndRegisterRemote(globalOptions, userOptions) {
|
|
39
|
+
return (userOptions.remotes || []).reduce((res, remote) => {
|
|
40
|
+
this.registerRemote(remote, res, { force: false });
|
|
41
|
+
return res;
|
|
42
|
+
}, globalOptions.remotes);
|
|
43
|
+
}
|
|
44
|
+
setIdToRemoteMap(id, remoteMatchInfo) {
|
|
45
|
+
const { remote, expose } = remoteMatchInfo;
|
|
46
|
+
const { name, alias } = remote;
|
|
47
|
+
this.idToRemoteMap[id] = {
|
|
48
|
+
name: remote.name,
|
|
49
|
+
expose
|
|
50
|
+
};
|
|
51
|
+
if (alias && id.startsWith(name)) {
|
|
52
|
+
const idWithAlias = id.replace(name, alias);
|
|
53
|
+
this.idToRemoteMap[idWithAlias] = {
|
|
54
|
+
name: remote.name,
|
|
55
|
+
expose
|
|
56
|
+
};
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (alias && id.startsWith(alias)) {
|
|
60
|
+
const idWithName = id.replace(alias, name);
|
|
61
|
+
this.idToRemoteMap[idWithName] = {
|
|
62
|
+
name: remote.name,
|
|
63
|
+
expose
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async loadRemote(id, options) {
|
|
68
|
+
const { host } = this;
|
|
69
|
+
try {
|
|
70
|
+
const { loadFactory = true } = options || { loadFactory: true };
|
|
71
|
+
const { module, moduleOptions, remoteMatchInfo } = await this.getRemoteModuleAndOptions({ id });
|
|
72
|
+
const { pkgNameOrAlias, remote, expose, id: idRes, remoteSnapshot } = remoteMatchInfo;
|
|
73
|
+
const moduleOrFactory = await module.get(idRes, expose, options, remoteSnapshot);
|
|
74
|
+
const moduleWrapper = await this.hooks.lifecycle.onLoad.emit({
|
|
75
|
+
id: idRes,
|
|
76
|
+
pkgNameOrAlias,
|
|
77
|
+
expose,
|
|
78
|
+
exposeModule: loadFactory ? moduleOrFactory : void 0,
|
|
79
|
+
exposeModuleFactory: loadFactory ? void 0 : moduleOrFactory,
|
|
80
|
+
remote,
|
|
81
|
+
options: moduleOptions,
|
|
82
|
+
moduleInstance: module,
|
|
83
|
+
origin: host
|
|
84
|
+
});
|
|
85
|
+
this.setIdToRemoteMap(id, remoteMatchInfo);
|
|
86
|
+
if (typeof moduleWrapper === "function") return moduleWrapper;
|
|
87
|
+
return moduleOrFactory;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
const { from = "runtime" } = options || { from: "runtime" };
|
|
90
|
+
const failOver = await this.hooks.lifecycle.errorLoadRemote.emit({
|
|
91
|
+
id,
|
|
92
|
+
error,
|
|
93
|
+
from,
|
|
94
|
+
lifecycle: "onLoad",
|
|
95
|
+
origin: host
|
|
96
|
+
});
|
|
97
|
+
if (!failOver) throw error;
|
|
98
|
+
return failOver;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async preloadRemote(preloadOptions) {
|
|
102
|
+
const { host } = this;
|
|
103
|
+
await this.hooks.lifecycle.beforePreloadRemote.emit({
|
|
104
|
+
preloadOps: preloadOptions,
|
|
105
|
+
options: host.options,
|
|
106
|
+
origin: host
|
|
107
|
+
});
|
|
108
|
+
const preloadOps = require_preload.formatPreloadArgs(host.options.remotes, preloadOptions);
|
|
109
|
+
await Promise.all(preloadOps.map(async (ops) => {
|
|
110
|
+
const { remote } = ops;
|
|
111
|
+
const remoteInfo = require_load.getRemoteInfo(remote);
|
|
112
|
+
const { globalSnapshot, remoteSnapshot } = await host.snapshotHandler.loadRemoteSnapshotInfo({ moduleInfo: remote });
|
|
113
|
+
const assets = await this.hooks.lifecycle.generatePreloadAssets.emit({
|
|
114
|
+
origin: host,
|
|
115
|
+
preloadOptions: ops,
|
|
116
|
+
remote,
|
|
117
|
+
remoteInfo,
|
|
118
|
+
globalSnapshot,
|
|
119
|
+
remoteSnapshot
|
|
120
|
+
});
|
|
121
|
+
if (!assets) return;
|
|
122
|
+
require_preload.preloadAssets(remoteInfo, host, assets);
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
registerRemotes(remotes, options) {
|
|
126
|
+
const { host } = this;
|
|
127
|
+
remotes.forEach((remote) => {
|
|
128
|
+
this.registerRemote(remote, host.options.remotes, { force: options?.force });
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
async getRemoteModuleAndOptions(options) {
|
|
132
|
+
const { host } = this;
|
|
133
|
+
const { id } = options;
|
|
134
|
+
let loadRemoteArgs;
|
|
135
|
+
try {
|
|
136
|
+
loadRemoteArgs = await this.hooks.lifecycle.beforeRequest.emit({
|
|
137
|
+
id,
|
|
138
|
+
options: host.options,
|
|
139
|
+
origin: host
|
|
140
|
+
});
|
|
141
|
+
} catch (error) {
|
|
142
|
+
loadRemoteArgs = await this.hooks.lifecycle.errorLoadRemote.emit({
|
|
143
|
+
id,
|
|
144
|
+
options: host.options,
|
|
145
|
+
origin: host,
|
|
146
|
+
from: "runtime",
|
|
147
|
+
error,
|
|
148
|
+
lifecycle: "beforeRequest"
|
|
149
|
+
});
|
|
150
|
+
if (!loadRemoteArgs) throw error;
|
|
151
|
+
}
|
|
152
|
+
const { id: idRes } = loadRemoteArgs;
|
|
153
|
+
const remoteSplitInfo = require_manifest.matchRemoteWithNameAndExpose(host.options.remotes, idRes);
|
|
154
|
+
require_logger.assert(remoteSplitInfo, (0, _module_federation_error_codes.getShortErrorMsg)(_module_federation_error_codes.RUNTIME_004, _module_federation_error_codes.runtimeDescMap, {
|
|
155
|
+
hostName: host.options.name,
|
|
156
|
+
requestId: idRes
|
|
157
|
+
}));
|
|
158
|
+
const { remote: rawRemote } = remoteSplitInfo;
|
|
159
|
+
const remoteInfo = require_load.getRemoteInfo(rawRemote);
|
|
160
|
+
const matchInfo = await host.sharedHandler.hooks.lifecycle.afterResolve.emit({
|
|
161
|
+
id: idRes,
|
|
162
|
+
...remoteSplitInfo,
|
|
163
|
+
options: host.options,
|
|
164
|
+
origin: host,
|
|
165
|
+
remoteInfo
|
|
166
|
+
});
|
|
167
|
+
const { remote, expose } = matchInfo;
|
|
168
|
+
require_logger.assert(remote && expose, `The 'beforeRequest' hook was executed, but it failed to return the correct 'remote' and 'expose' values while loading ${idRes}.`);
|
|
169
|
+
let module = host.moduleCache.get(remote.name);
|
|
170
|
+
const moduleOptions = {
|
|
171
|
+
host,
|
|
172
|
+
remoteInfo
|
|
173
|
+
};
|
|
174
|
+
if (!module) {
|
|
175
|
+
module = new require_index$1.Module(moduleOptions);
|
|
176
|
+
host.moduleCache.set(remote.name, module);
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
module,
|
|
180
|
+
moduleOptions,
|
|
181
|
+
remoteMatchInfo: matchInfo
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
registerRemote(remote, targetRemotes, options) {
|
|
185
|
+
const { host } = this;
|
|
186
|
+
const normalizeRemote = () => {
|
|
187
|
+
if (remote.alias) {
|
|
188
|
+
const findEqual = targetRemotes.find((item) => remote.alias && (item.name.startsWith(remote.alias) || item.alias?.startsWith(remote.alias)));
|
|
189
|
+
require_logger.assert(!findEqual, `The alias ${remote.alias} of remote ${remote.name} is not allowed to be the prefix of ${findEqual && findEqual.name} name or alias`);
|
|
190
|
+
}
|
|
191
|
+
if ("entry" in remote) {
|
|
192
|
+
if ((0, _module_federation_sdk.isBrowserEnv)() && !remote.entry.startsWith("http")) remote.entry = new URL(remote.entry, window.location.origin).href;
|
|
193
|
+
}
|
|
194
|
+
if (!remote.shareScope) remote.shareScope = require_constant.DEFAULT_SCOPE;
|
|
195
|
+
if (!remote.type) remote.type = require_constant.DEFAULT_REMOTE_TYPE;
|
|
196
|
+
};
|
|
197
|
+
this.hooks.lifecycle.beforeRegisterRemote.emit({
|
|
198
|
+
remote,
|
|
199
|
+
origin: host
|
|
200
|
+
});
|
|
201
|
+
const registeredRemote = targetRemotes.find((item) => item.name === remote.name);
|
|
202
|
+
if (!registeredRemote) {
|
|
203
|
+
normalizeRemote();
|
|
204
|
+
targetRemotes.push(remote);
|
|
205
|
+
this.hooks.lifecycle.registerRemote.emit({
|
|
206
|
+
remote,
|
|
207
|
+
origin: host
|
|
208
|
+
});
|
|
209
|
+
} else {
|
|
210
|
+
const messages = [`The remote "${remote.name}" is already registered.`, "Please note that overriding it may cause unexpected errors."];
|
|
211
|
+
if (options?.force) {
|
|
212
|
+
this.removeRemote(registeredRemote);
|
|
213
|
+
normalizeRemote();
|
|
214
|
+
targetRemotes.push(remote);
|
|
215
|
+
this.hooks.lifecycle.registerRemote.emit({
|
|
216
|
+
remote,
|
|
217
|
+
origin: host
|
|
218
|
+
});
|
|
219
|
+
(0, _module_federation_sdk.warn)(messages.join(" "));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
removeRemote(remote) {
|
|
224
|
+
try {
|
|
225
|
+
const { host } = this;
|
|
226
|
+
const { name } = remote;
|
|
227
|
+
const remoteIndex = host.options.remotes.findIndex((item) => item.name === name);
|
|
228
|
+
if (remoteIndex !== -1) host.options.remotes.splice(remoteIndex, 1);
|
|
229
|
+
const loadedModule = host.moduleCache.get(remote.name);
|
|
230
|
+
if (loadedModule) {
|
|
231
|
+
const remoteInfo = loadedModule.remoteInfo;
|
|
232
|
+
const key = remoteInfo.entryGlobalName;
|
|
233
|
+
if (require_global.CurrentGlobal[key]) if (Object.getOwnPropertyDescriptor(require_global.CurrentGlobal, key)?.configurable) delete require_global.CurrentGlobal[key];
|
|
234
|
+
else require_global.CurrentGlobal[key] = void 0;
|
|
235
|
+
const remoteEntryUniqueKey = require_load.getRemoteEntryUniqueKey(loadedModule.remoteInfo);
|
|
236
|
+
if (require_global.globalLoading[remoteEntryUniqueKey]) delete require_global.globalLoading[remoteEntryUniqueKey];
|
|
237
|
+
host.snapshotHandler.manifestCache.delete(remoteInfo.entry);
|
|
238
|
+
let remoteInsId = remoteInfo.buildVersion ? (0, _module_federation_sdk.composeKeyWithSeparator)(remoteInfo.name, remoteInfo.buildVersion) : remoteInfo.name;
|
|
239
|
+
const remoteInsIndex = require_global.CurrentGlobal.__FEDERATION__.__INSTANCES__.findIndex((ins) => {
|
|
240
|
+
if (remoteInfo.buildVersion) return ins.options.id === remoteInsId;
|
|
241
|
+
else return ins.name === remoteInsId;
|
|
242
|
+
});
|
|
243
|
+
if (remoteInsIndex !== -1) {
|
|
244
|
+
const remoteIns = require_global.CurrentGlobal.__FEDERATION__.__INSTANCES__[remoteInsIndex];
|
|
245
|
+
remoteInsId = remoteIns.options.id || remoteInsId;
|
|
246
|
+
const globalShareScopeMap = require_share.getGlobalShareScope();
|
|
247
|
+
let isAllSharedNotUsed = true;
|
|
248
|
+
const needDeleteKeys = [];
|
|
249
|
+
Object.keys(globalShareScopeMap).forEach((instId) => {
|
|
250
|
+
const shareScopeMap = globalShareScopeMap[instId];
|
|
251
|
+
shareScopeMap && Object.keys(shareScopeMap).forEach((shareScope) => {
|
|
252
|
+
const shareScopeVal = shareScopeMap[shareScope];
|
|
253
|
+
shareScopeVal && Object.keys(shareScopeVal).forEach((shareName) => {
|
|
254
|
+
const sharedPkgs = shareScopeVal[shareName];
|
|
255
|
+
sharedPkgs && Object.keys(sharedPkgs).forEach((shareVersion) => {
|
|
256
|
+
const shared = sharedPkgs[shareVersion];
|
|
257
|
+
if (shared && typeof shared === "object" && shared.from === remoteInfo.name) if (shared.loaded || shared.loading) {
|
|
258
|
+
shared.useIn = shared.useIn.filter((usedHostName) => usedHostName !== remoteInfo.name);
|
|
259
|
+
if (shared.useIn.length) isAllSharedNotUsed = false;
|
|
260
|
+
else needDeleteKeys.push([
|
|
261
|
+
instId,
|
|
262
|
+
shareScope,
|
|
263
|
+
shareName,
|
|
264
|
+
shareVersion
|
|
265
|
+
]);
|
|
266
|
+
} else needDeleteKeys.push([
|
|
267
|
+
instId,
|
|
268
|
+
shareScope,
|
|
269
|
+
shareName,
|
|
270
|
+
shareVersion
|
|
271
|
+
]);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
if (isAllSharedNotUsed) {
|
|
277
|
+
remoteIns.shareScopeMap = {};
|
|
278
|
+
delete globalShareScopeMap[remoteInsId];
|
|
279
|
+
}
|
|
280
|
+
needDeleteKeys.forEach(([insId, shareScope, shareName, shareVersion]) => {
|
|
281
|
+
delete globalShareScopeMap[insId]?.[shareScope]?.[shareName]?.[shareVersion];
|
|
282
|
+
});
|
|
283
|
+
require_global.CurrentGlobal.__FEDERATION__.__INSTANCES__.splice(remoteInsIndex, 1);
|
|
284
|
+
}
|
|
285
|
+
const { hostGlobalSnapshot } = require_SnapshotHandler.getGlobalRemoteInfo(remote, host);
|
|
286
|
+
if (hostGlobalSnapshot) {
|
|
287
|
+
const remoteKey = hostGlobalSnapshot && "remotesInfo" in hostGlobalSnapshot && hostGlobalSnapshot.remotesInfo && require_global.getInfoWithoutType(hostGlobalSnapshot.remotesInfo, remote.name).key;
|
|
288
|
+
if (remoteKey) {
|
|
289
|
+
delete hostGlobalSnapshot.remotesInfo[remoteKey];
|
|
290
|
+
if (Boolean(require_global.Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey])) delete require_global.Global.__FEDERATION__.__MANIFEST_LOADING__[remoteKey];
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
host.moduleCache.delete(remote.name);
|
|
294
|
+
}
|
|
295
|
+
} catch (err) {
|
|
296
|
+
require_logger.logger.log("removeRemote fail: ", err);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
//#endregion
|
|
302
|
+
exports.RemoteHandler = RemoteHandler;
|
|
303
|
+
//# sourceMappingURL=index.cjs.map
|