@module-federation/bridge-react 0.0.0-next-20250707074728 → 0.0.0-next-20250708033956

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +8 -4
  2. package/__tests__/bridge.spec.tsx +7 -7
  3. package/dist/{bridge-base-P6pEjY1q.js → bridge-base-BoshEggF.mjs} +1 -1
  4. package/dist/{bridge-base-BBH982Tz.cjs → bridge-base-UGCwcMnG.js} +1 -1
  5. package/dist/data-fetch-runtime-plugin.cjs.js +73 -0
  6. package/dist/data-fetch-runtime-plugin.d.ts +6 -0
  7. package/dist/data-fetch-runtime-plugin.es.js +74 -0
  8. package/dist/data-fetch-server-middleware.cjs.js +163 -0
  9. package/dist/data-fetch-server-middleware.d.ts +6 -0
  10. package/dist/data-fetch-server-middleware.es.js +164 -0
  11. package/dist/data-fetch-utils.cjs.js +1273 -0
  12. package/dist/data-fetch-utils.d.ts +77 -0
  13. package/dist/data-fetch-utils.es.js +1275 -0
  14. package/dist/index-C0fDZB5b.js +45 -0
  15. package/dist/index-CqxytsLY.mjs +46 -0
  16. package/dist/index.cjs.js +461 -9
  17. package/dist/index.d.ts +143 -0
  18. package/dist/index.es.js +464 -11
  19. package/dist/index.esm-BCeUd-x9.mjs +418 -0
  20. package/dist/index.esm-j_1sIRzg.js +417 -0
  21. package/dist/inject-data-fetch-CAvi-gSf.js +79 -0
  22. package/dist/inject-data-fetch-errCdqBS.mjs +80 -0
  23. package/dist/lazy-utils.cjs.js +24 -0
  24. package/dist/lazy-utils.d.ts +140 -0
  25. package/dist/lazy-utils.es.js +24 -0
  26. package/dist/plugin.d.ts +4 -4
  27. package/dist/router-v5.cjs.js +1 -1
  28. package/dist/router-v5.es.js +1 -1
  29. package/dist/router-v6.cjs.js +1 -1
  30. package/dist/router-v6.es.js +1 -1
  31. package/dist/router.cjs.js +1 -1
  32. package/dist/router.es.js +1 -1
  33. package/dist/utils-Bk8hGjjF.mjs +2016 -0
  34. package/dist/utils-iEVlDmyk.js +2015 -0
  35. package/dist/v18.cjs.js +1 -1
  36. package/dist/v18.es.js +1 -1
  37. package/dist/v19.cjs.js +1 -1
  38. package/dist/v19.es.js +1 -1
  39. package/package.json +45 -5
  40. package/src/index.ts +30 -1
  41. package/src/lazy/AwaitDataFetch.tsx +215 -0
  42. package/src/lazy/constant.ts +30 -0
  43. package/src/lazy/createLazyComponent.tsx +404 -0
  44. package/src/lazy/data-fetch/cache.ts +296 -0
  45. package/src/lazy/data-fetch/call-data-fetch.ts +13 -0
  46. package/src/lazy/data-fetch/data-fetch-server-middleware.ts +196 -0
  47. package/src/lazy/data-fetch/index.ts +15 -0
  48. package/src/lazy/data-fetch/inject-data-fetch.ts +109 -0
  49. package/src/lazy/data-fetch/prefetch.ts +102 -0
  50. package/src/lazy/data-fetch/runtime-plugin.ts +116 -0
  51. package/src/lazy/index.ts +31 -0
  52. package/src/lazy/logger.ts +6 -0
  53. package/src/lazy/types.ts +75 -0
  54. package/src/lazy/utils.ts +372 -0
  55. package/src/lazy/wrapNoSSR.tsx +10 -0
  56. package/src/provider/plugin.ts +4 -4
  57. package/src/remote/component.tsx +3 -3
  58. package/src/remote/create.tsx +17 -4
  59. package/tsconfig.json +1 -1
  60. package/vite.config.ts +13 -0
  61. package/dist/index-Cv3p6r66.cjs +0 -235
  62. package/dist/index-D4yt7Udv.js +0 -236
package/CHANGELOG.md CHANGED
@@ -1,12 +1,16 @@
1
1
  # @module-federation/bridge-react
2
2
 
3
- ## 0.0.0-next-20250707074728
3
+ ## 0.0.0-next-20250708033956
4
+
5
+ ### Minor Changes
6
+
7
+ - eb0d1dc: refactor(bridge-react): rename createRemoteComponent as createRemoteAppComponent
4
8
 
5
9
  ### Patch Changes
6
10
 
7
- - f1834fa: chore: rename FederationHost to ModuleFederation
8
- - @module-federation/sdk@0.0.0-next-20250707074728
9
- - @module-federation/bridge-shared@0.0.0-next-20250707074728
11
+ - eb0d1dc: feat(bridge-react): export createLazyCompoent api
12
+ - @module-federation/sdk@0.0.0-next-20250708033956
13
+ - @module-federation/bridge-shared@0.0.0-next-20250708033956
10
14
 
11
15
  ## 0.16.0
12
16
 
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { assert, describe, it } from 'vitest';
3
- import { createBridgeComponent, createRemoteComponent } from '../src';
3
+ import { createBridgeComponent, createRemoteAppComponent } from '../src';
4
4
  import {
5
5
  act,
6
6
  fireEvent,
@@ -44,14 +44,14 @@ describe('bridge', () => {
44
44
  expect(document.querySelector('#container')!.innerHTML).toContain('');
45
45
  });
46
46
 
47
- it('createRemoteComponent', async () => {
47
+ it('createRemoteAppComponent', async () => {
48
48
  function Component({ props }: { props?: Record<string, any> }) {
49
49
  return <div>life cycle render {props?.msg}</div>;
50
50
  }
51
51
  const BridgeComponent = createBridgeComponent({
52
52
  rootComponent: Component,
53
53
  });
54
- const RemoteComponent = createRemoteComponent({
54
+ const RemoteComponent = createRemoteAppComponent({
55
55
  loader: async () => {
56
56
  return {
57
57
  default: BridgeComponent,
@@ -71,7 +71,7 @@ describe('bridge', () => {
71
71
  expect(getHtml(container)).toMatch('hello world');
72
72
  });
73
73
 
74
- it('createRemoteComponent and obtain ref property', async () => {
74
+ it('createRemoteAppComponent and obtain ref property', async () => {
75
75
  const ref = {
76
76
  current: null,
77
77
  };
@@ -82,7 +82,7 @@ describe('bridge', () => {
82
82
  const BridgeComponent = createBridgeComponent({
83
83
  rootComponent: Component,
84
84
  });
85
- const RemoteComponent = createRemoteComponent({
85
+ const RemoteComponent = createRemoteAppComponent({
86
86
  loader: async () => {
87
87
  return {
88
88
  default: BridgeComponent,
@@ -103,7 +103,7 @@ describe('bridge', () => {
103
103
  expect(ref.current).not.toBeNull();
104
104
  });
105
105
 
106
- it('createRemoteComponent with custom createRoot prop', async () => {
106
+ it('createRemoteAppComponent with custom createRoot prop', async () => {
107
107
  const renderMock = vi.fn();
108
108
 
109
109
  function Component({ props }: { props?: Record<string, any> }) {
@@ -118,7 +118,7 @@ describe('bridge', () => {
118
118
  };
119
119
  },
120
120
  });
121
- const RemoteComponent = createRemoteComponent({
121
+ const RemoteComponent = createRemoteAppComponent({
122
122
  loader: async () => {
123
123
  return {
124
124
  default: BridgeComponent,
@@ -1,6 +1,6 @@
1
1
  import * as React from "react";
2
2
  import { Component, createElement, createContext } from "react";
3
- import { L as LoggerInstance, R as RouterContext } from "./index-D4yt7Udv.js";
3
+ import { L as LoggerInstance, R as RouterContext } from "./index-CqxytsLY.mjs";
4
4
  import { federationRuntime } from "./plugin.es.js";
5
5
  const ErrorBoundaryContext = createContext(null);
6
6
  const initialState = {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  const React = require("react");
3
- const index = require("./index-Cv3p6r66.cjs");
3
+ const index = require("./index-C0fDZB5b.js");
4
4
  const plugin = require("./plugin.cjs.js");
5
5
  function _interopNamespaceDefault(e) {
6
6
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ const injectDataFetch = require("./inject-data-fetch-CAvi-gSf.js");
3
+ const lazyUtils = require("./utils-iEVlDmyk.js");
4
+ const autoFetchData = () => ({
5
+ name: "auto-fetch-data-plugin",
6
+ beforeInit(args) {
7
+ lazyUtils.initDataFetchMap();
8
+ injectDataFetch.injectDataFetch();
9
+ return args;
10
+ },
11
+ afterLoadSnapshot(args) {
12
+ const { id, moduleInfo, remoteSnapshot, host } = args;
13
+ if (typeof id === "string" && lazyUtils.isDataLoaderExpose(id)) {
14
+ return args;
15
+ }
16
+ if (!remoteSnapshot || !id || !("modules" in remoteSnapshot)) {
17
+ return args;
18
+ }
19
+ const { name, alias } = moduleInfo;
20
+ const dataFetchInfo = lazyUtils.getDataFetchInfo({
21
+ name,
22
+ alias,
23
+ id,
24
+ remoteSnapshot
25
+ });
26
+ if (!dataFetchInfo) {
27
+ return args;
28
+ }
29
+ const { dataFetchId, dataFetchName } = dataFetchInfo;
30
+ const dataFetchMapKey = lazyUtils.getDataFetchMapKey(dataFetchInfo, {
31
+ name: host.name,
32
+ version: host.options.version
33
+ });
34
+ lazyUtils.logger.debug(
35
+ "======= auto fetch plugin dataFetchMapKey: ",
36
+ dataFetchMapKey
37
+ );
38
+ if (!dataFetchMapKey) {
39
+ return args;
40
+ }
41
+ const dataFetchItem = lazyUtils.getDataFetchItem(dataFetchMapKey);
42
+ if (dataFetchItem) {
43
+ return args;
44
+ }
45
+ const dataFetchMap = lazyUtils.getDataFetchMap();
46
+ const hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
47
+ const hasDataFetchClient = Boolean(
48
+ remoteSnapshot.modules.find(
49
+ (module2) => module2.moduleName === `${dataFetchName}${lazyUtils.DATA_FETCH_CLIENT_SUFFIX}`
50
+ )
51
+ );
52
+ const downgradeType = hasDataFetchClient ? lazyUtils.MF_DATA_FETCH_TYPE.FETCH_CLIENT : hasSSRAsset ? lazyUtils.MF_DATA_FETCH_TYPE.FETCH_SERVER : lazyUtils.MF_DATA_FETCH_TYPE.FETCH_CLIENT;
53
+ let finalDataFetchId = dataFetchId;
54
+ if (!lazyUtils.isServerEnv()) {
55
+ finalDataFetchId = downgradeType === lazyUtils.MF_DATA_FETCH_TYPE.FETCH_CLIENT ? hasDataFetchClient ? `${dataFetchId}${lazyUtils.DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId : dataFetchId;
56
+ }
57
+ const getDataFetchGetter = () => lazyUtils.loadDataFetchModule(host, finalDataFetchId);
58
+ const dataFetchFnItem = [
59
+ getDataFetchGetter,
60
+ downgradeType
61
+ ];
62
+ if (typeof window === "undefined" || lazyUtils.isCSROnly()) {
63
+ dataFetchFnItem.push(getDataFetchGetter());
64
+ }
65
+ dataFetchMap[dataFetchMapKey] = [
66
+ dataFetchFnItem,
67
+ void 0,
68
+ lazyUtils.MF_DATA_FETCH_STATUS.AWAIT
69
+ ];
70
+ return args;
71
+ }
72
+ });
73
+ module.exports = autoFetchData;
@@ -0,0 +1,6 @@
1
+ import { FederationRuntimePlugin } from '@module-federation/runtime';
2
+
3
+ declare const autoFetchData: () => FederationRuntimePlugin;
4
+ export default autoFetchData;
5
+
6
+ export { }
@@ -0,0 +1,74 @@
1
+ import { i as injectDataFetch } from "./inject-data-fetch-errCdqBS.mjs";
2
+ import { i as isDataLoaderExpose, g as getDataFetchInfo, a as getDataFetchMapKey, l as logger, b as getDataFetchItem, D as DATA_FETCH_CLIENT_SUFFIX, M as MF_DATA_FETCH_TYPE, c as isServerEnv, d as isCSROnly, e as loadDataFetchModule, f as MF_DATA_FETCH_STATUS, h as getDataFetchMap, j as initDataFetchMap } from "./utils-Bk8hGjjF.mjs";
3
+ const autoFetchData = () => ({
4
+ name: "auto-fetch-data-plugin",
5
+ beforeInit(args) {
6
+ initDataFetchMap();
7
+ injectDataFetch();
8
+ return args;
9
+ },
10
+ afterLoadSnapshot(args) {
11
+ const { id, moduleInfo, remoteSnapshot, host } = args;
12
+ if (typeof id === "string" && isDataLoaderExpose(id)) {
13
+ return args;
14
+ }
15
+ if (!remoteSnapshot || !id || !("modules" in remoteSnapshot)) {
16
+ return args;
17
+ }
18
+ const { name, alias } = moduleInfo;
19
+ const dataFetchInfo = getDataFetchInfo({
20
+ name,
21
+ alias,
22
+ id,
23
+ remoteSnapshot
24
+ });
25
+ if (!dataFetchInfo) {
26
+ return args;
27
+ }
28
+ const { dataFetchId, dataFetchName } = dataFetchInfo;
29
+ const dataFetchMapKey = getDataFetchMapKey(dataFetchInfo, {
30
+ name: host.name,
31
+ version: host.options.version
32
+ });
33
+ logger.debug(
34
+ "======= auto fetch plugin dataFetchMapKey: ",
35
+ dataFetchMapKey
36
+ );
37
+ if (!dataFetchMapKey) {
38
+ return args;
39
+ }
40
+ const dataFetchItem = getDataFetchItem(dataFetchMapKey);
41
+ if (dataFetchItem) {
42
+ return args;
43
+ }
44
+ const dataFetchMap = getDataFetchMap();
45
+ const hasSSRAsset = Boolean(remoteSnapshot.ssrRemoteEntry);
46
+ const hasDataFetchClient = Boolean(
47
+ remoteSnapshot.modules.find(
48
+ (module) => module.moduleName === `${dataFetchName}${DATA_FETCH_CLIENT_SUFFIX}`
49
+ )
50
+ );
51
+ const downgradeType = hasDataFetchClient ? MF_DATA_FETCH_TYPE.FETCH_CLIENT : hasSSRAsset ? MF_DATA_FETCH_TYPE.FETCH_SERVER : MF_DATA_FETCH_TYPE.FETCH_CLIENT;
52
+ let finalDataFetchId = dataFetchId;
53
+ if (!isServerEnv()) {
54
+ finalDataFetchId = downgradeType === MF_DATA_FETCH_TYPE.FETCH_CLIENT ? hasDataFetchClient ? `${dataFetchId}${DATA_FETCH_CLIENT_SUFFIX}` : dataFetchId : dataFetchId;
55
+ }
56
+ const getDataFetchGetter = () => loadDataFetchModule(host, finalDataFetchId);
57
+ const dataFetchFnItem = [
58
+ getDataFetchGetter,
59
+ downgradeType
60
+ ];
61
+ if (typeof window === "undefined" || isCSROnly()) {
62
+ dataFetchFnItem.push(getDataFetchGetter());
63
+ }
64
+ dataFetchMap[dataFetchMapKey] = [
65
+ dataFetchFnItem,
66
+ void 0,
67
+ MF_DATA_FETCH_STATUS.AWAIT
68
+ ];
69
+ return args;
70
+ }
71
+ });
72
+ export {
73
+ autoFetchData as default
74
+ };
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ const lazyUtils = require("./utils-iEVlDmyk.js");
3
+ const index_esm = require("./index.esm-j_1sIRzg.js");
4
+ function wrapSetTimeout(targetPromise, delay = 2e4, id) {
5
+ if (targetPromise && typeof targetPromise.then === "function") {
6
+ return new Promise((resolve, reject) => {
7
+ const timeoutId = setTimeout(() => {
8
+ lazyUtils.logger.warn(`Data fetch for ID ${id} timed out after 20 seconds.`);
9
+ reject(new Error(`Data fetch for ID ${id} timed out after 20 seconds`));
10
+ }, delay);
11
+ targetPromise.then((value) => {
12
+ clearTimeout(timeoutId);
13
+ resolve(value);
14
+ }).catch((err) => {
15
+ clearTimeout(timeoutId);
16
+ reject(err);
17
+ });
18
+ });
19
+ }
20
+ }
21
+ function addProtocol(url) {
22
+ if (url.startsWith("//")) {
23
+ return "https:" + url;
24
+ }
25
+ return url;
26
+ }
27
+ const getDecodeQuery = (url, name) => {
28
+ const res = url.searchParams.get(name);
29
+ if (!res) {
30
+ return null;
31
+ }
32
+ return decodeURIComponent(res);
33
+ };
34
+ const dataFetchServerMiddleware = async (ctx, next) => {
35
+ var _a, _b, _c;
36
+ let url;
37
+ let dataFetchKey;
38
+ let params;
39
+ let remoteInfo;
40
+ try {
41
+ url = new URL(ctx.req.url);
42
+ dataFetchKey = getDecodeQuery(url, lazyUtils.DATA_FETCH_QUERY);
43
+ params = JSON.parse(getDecodeQuery(url, "params") || "{}");
44
+ const remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
45
+ remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
46
+ } catch (e) {
47
+ lazyUtils.logger.error("fetch data from server, error: ", e);
48
+ return next();
49
+ }
50
+ if (!dataFetchKey) {
51
+ return next();
52
+ }
53
+ lazyUtils.logger.log("fetch data from server, dataFetchKey: ", dataFetchKey);
54
+ lazyUtils.logger.debug(
55
+ "fetch data from server, moduleInfo: ",
56
+ (_a = globalThis.__FEDERATION__) == null ? void 0 : _a.moduleInfo
57
+ );
58
+ try {
59
+ const dataFetchMap = lazyUtils.getDataFetchMap();
60
+ if (!dataFetchMap) {
61
+ lazyUtils.initDataFetchMap();
62
+ }
63
+ const fetchDataPromise = (_b = dataFetchMap[dataFetchKey]) == null ? void 0 : _b[1];
64
+ lazyUtils.logger.debug(
65
+ "fetch data from server, fetchDataPromise: ",
66
+ fetchDataPromise
67
+ );
68
+ if (fetchDataPromise && ((_c = dataFetchMap[dataFetchKey]) == null ? void 0 : _c[2]) !== lazyUtils.MF_DATA_FETCH_STATUS.ERROR) {
69
+ const targetPromise = fetchDataPromise[0];
70
+ const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchKey);
71
+ if (wrappedPromise) {
72
+ const res = await wrappedPromise;
73
+ lazyUtils.logger.log("fetch data from server, fetchDataPromise res: ", res);
74
+ return ctx.json(res);
75
+ }
76
+ lazyUtils.logger.error(
77
+ `Expected a Promise from fetchDataPromise[0] for dataFetchKey ${dataFetchKey}, but received:`,
78
+ targetPromise,
79
+ "Will try call new dataFetch again..."
80
+ );
81
+ }
82
+ if (remoteInfo) {
83
+ try {
84
+ const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
85
+ const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
86
+ if (!hostInstance2) {
87
+ throw new Error("host instance not found!");
88
+ }
89
+ const remote = hostInstance2.options.remotes.find(
90
+ (remote2) => remote2.name === remoteInfo.name
91
+ );
92
+ lazyUtils.logger.debug("find remote: ", JSON.stringify(remote));
93
+ if (!remote) {
94
+ hostInstance2.registerRemotes([
95
+ {
96
+ name: remoteInfo.name,
97
+ entry: remoteEntry,
98
+ entryGlobalName: remoteInfo.globalName
99
+ }
100
+ ]);
101
+ } else if (!("entry" in remote) || !remote.entry.includes(index_esm.MANIFEST_EXT)) {
102
+ const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
103
+ lazyUtils.logger.debug(
104
+ "find hostGlobalSnapshot: ",
105
+ JSON.stringify(hostGlobalSnapshot)
106
+ );
107
+ lazyUtils.logger.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
108
+ if (!hostGlobalSnapshot || !remoteSnapshot) {
109
+ if ("version" in remote) {
110
+ delete remote.version;
111
+ }
112
+ remote.entry = remoteEntry;
113
+ remote.entryGlobalName = remoteInfo.globalName;
114
+ }
115
+ }
116
+ } catch (e) {
117
+ ctx.status(500);
118
+ return ctx.text(
119
+ `failed to fetch ${remoteInfo.name} data, error:
120
+ ${e}`
121
+ );
122
+ }
123
+ }
124
+ const dataFetchItem = dataFetchMap[dataFetchKey];
125
+ lazyUtils.logger.debug("fetch data from server, dataFetchItem: ", dataFetchItem);
126
+ if (dataFetchItem) {
127
+ const callFetchDataPromise = lazyUtils.fetchData(dataFetchKey, {
128
+ ...params,
129
+ isDowngrade: !remoteInfo,
130
+ _id: dataFetchKey
131
+ });
132
+ const wrappedPromise = wrapSetTimeout(
133
+ callFetchDataPromise,
134
+ 2e4,
135
+ dataFetchKey
136
+ );
137
+ if (wrappedPromise) {
138
+ const res = await wrappedPromise;
139
+ lazyUtils.logger.log("fetch data from server, dataFetchItem res: ", res);
140
+ return ctx.json(res);
141
+ }
142
+ }
143
+ const remoteId = dataFetchKey.split(index_esm.SEPARATOR)[0];
144
+ const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
145
+ if (!hostInstance) {
146
+ throw new Error("host instance not found!");
147
+ }
148
+ const dataFetchFn = await lazyUtils.loadDataFetchModule(hostInstance, remoteId);
149
+ const data = await dataFetchFn({
150
+ ...params,
151
+ isDowngrade: !remoteInfo,
152
+ _id: dataFetchKey
153
+ });
154
+ lazyUtils.logger.log("fetch data from server, loadDataFetchModule res: ", data);
155
+ return ctx.json(data);
156
+ } catch (e) {
157
+ lazyUtils.logger.error("server plugin data fetch error: ", e);
158
+ ctx.status(500);
159
+ return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
160
+ ${e}`);
161
+ }
162
+ };
163
+ module.exports = dataFetchServerMiddleware;
@@ -0,0 +1,6 @@
1
+ import { MiddlewareHandler } from 'hono';
2
+
3
+ declare const dataFetchServerMiddleware: MiddlewareHandler;
4
+ export default dataFetchServerMiddleware;
5
+
6
+ export { }
@@ -0,0 +1,164 @@
1
+ import { k as DATA_FETCH_QUERY, l as logger, h as getDataFetchMap, j as initDataFetchMap, f as MF_DATA_FETCH_STATUS, m as fetchData, e as loadDataFetchModule } from "./utils-Bk8hGjjF.mjs";
2
+ import { M as MANIFEST_EXT, S as SEPARATOR } from "./index.esm-BCeUd-x9.mjs";
3
+ function wrapSetTimeout(targetPromise, delay = 2e4, id) {
4
+ if (targetPromise && typeof targetPromise.then === "function") {
5
+ return new Promise((resolve, reject) => {
6
+ const timeoutId = setTimeout(() => {
7
+ logger.warn(`Data fetch for ID ${id} timed out after 20 seconds.`);
8
+ reject(new Error(`Data fetch for ID ${id} timed out after 20 seconds`));
9
+ }, delay);
10
+ targetPromise.then((value) => {
11
+ clearTimeout(timeoutId);
12
+ resolve(value);
13
+ }).catch((err) => {
14
+ clearTimeout(timeoutId);
15
+ reject(err);
16
+ });
17
+ });
18
+ }
19
+ }
20
+ function addProtocol(url) {
21
+ if (url.startsWith("//")) {
22
+ return "https:" + url;
23
+ }
24
+ return url;
25
+ }
26
+ const getDecodeQuery = (url, name) => {
27
+ const res = url.searchParams.get(name);
28
+ if (!res) {
29
+ return null;
30
+ }
31
+ return decodeURIComponent(res);
32
+ };
33
+ const dataFetchServerMiddleware = async (ctx, next) => {
34
+ var _a, _b, _c;
35
+ let url;
36
+ let dataFetchKey;
37
+ let params;
38
+ let remoteInfo;
39
+ try {
40
+ url = new URL(ctx.req.url);
41
+ dataFetchKey = getDecodeQuery(url, DATA_FETCH_QUERY);
42
+ params = JSON.parse(getDecodeQuery(url, "params") || "{}");
43
+ const remoteInfoQuery = getDecodeQuery(url, "remoteInfo");
44
+ remoteInfo = remoteInfoQuery ? JSON.parse(remoteInfoQuery) : null;
45
+ } catch (e) {
46
+ logger.error("fetch data from server, error: ", e);
47
+ return next();
48
+ }
49
+ if (!dataFetchKey) {
50
+ return next();
51
+ }
52
+ logger.log("fetch data from server, dataFetchKey: ", dataFetchKey);
53
+ logger.debug(
54
+ "fetch data from server, moduleInfo: ",
55
+ (_a = globalThis.__FEDERATION__) == null ? void 0 : _a.moduleInfo
56
+ );
57
+ try {
58
+ const dataFetchMap = getDataFetchMap();
59
+ if (!dataFetchMap) {
60
+ initDataFetchMap();
61
+ }
62
+ const fetchDataPromise = (_b = dataFetchMap[dataFetchKey]) == null ? void 0 : _b[1];
63
+ logger.debug(
64
+ "fetch data from server, fetchDataPromise: ",
65
+ fetchDataPromise
66
+ );
67
+ if (fetchDataPromise && ((_c = dataFetchMap[dataFetchKey]) == null ? void 0 : _c[2]) !== MF_DATA_FETCH_STATUS.ERROR) {
68
+ const targetPromise = fetchDataPromise[0];
69
+ const wrappedPromise = wrapSetTimeout(targetPromise, 2e4, dataFetchKey);
70
+ if (wrappedPromise) {
71
+ const res = await wrappedPromise;
72
+ logger.log("fetch data from server, fetchDataPromise res: ", res);
73
+ return ctx.json(res);
74
+ }
75
+ logger.error(
76
+ `Expected a Promise from fetchDataPromise[0] for dataFetchKey ${dataFetchKey}, but received:`,
77
+ targetPromise,
78
+ "Will try call new dataFetch again..."
79
+ );
80
+ }
81
+ if (remoteInfo) {
82
+ try {
83
+ const hostInstance2 = globalThis.__FEDERATION__.__INSTANCES__[0];
84
+ const remoteEntry = `${addProtocol(remoteInfo.ssrPublicPath) + remoteInfo.ssrRemoteEntry}`;
85
+ if (!hostInstance2) {
86
+ throw new Error("host instance not found!");
87
+ }
88
+ const remote = hostInstance2.options.remotes.find(
89
+ (remote2) => remote2.name === remoteInfo.name
90
+ );
91
+ logger.debug("find remote: ", JSON.stringify(remote));
92
+ if (!remote) {
93
+ hostInstance2.registerRemotes([
94
+ {
95
+ name: remoteInfo.name,
96
+ entry: remoteEntry,
97
+ entryGlobalName: remoteInfo.globalName
98
+ }
99
+ ]);
100
+ } else if (!("entry" in remote) || !remote.entry.includes(MANIFEST_EXT)) {
101
+ const { hostGlobalSnapshot, remoteSnapshot } = hostInstance2.snapshotHandler.getGlobalRemoteInfo(remoteInfo);
102
+ logger.debug(
103
+ "find hostGlobalSnapshot: ",
104
+ JSON.stringify(hostGlobalSnapshot)
105
+ );
106
+ logger.debug("find remoteSnapshot: ", JSON.stringify(remoteSnapshot));
107
+ if (!hostGlobalSnapshot || !remoteSnapshot) {
108
+ if ("version" in remote) {
109
+ delete remote.version;
110
+ }
111
+ remote.entry = remoteEntry;
112
+ remote.entryGlobalName = remoteInfo.globalName;
113
+ }
114
+ }
115
+ } catch (e) {
116
+ ctx.status(500);
117
+ return ctx.text(
118
+ `failed to fetch ${remoteInfo.name} data, error:
119
+ ${e}`
120
+ );
121
+ }
122
+ }
123
+ const dataFetchItem = dataFetchMap[dataFetchKey];
124
+ logger.debug("fetch data from server, dataFetchItem: ", dataFetchItem);
125
+ if (dataFetchItem) {
126
+ const callFetchDataPromise = fetchData(dataFetchKey, {
127
+ ...params,
128
+ isDowngrade: !remoteInfo,
129
+ _id: dataFetchKey
130
+ });
131
+ const wrappedPromise = wrapSetTimeout(
132
+ callFetchDataPromise,
133
+ 2e4,
134
+ dataFetchKey
135
+ );
136
+ if (wrappedPromise) {
137
+ const res = await wrappedPromise;
138
+ logger.log("fetch data from server, dataFetchItem res: ", res);
139
+ return ctx.json(res);
140
+ }
141
+ }
142
+ const remoteId = dataFetchKey.split(SEPARATOR)[0];
143
+ const hostInstance = globalThis.__FEDERATION__.__INSTANCES__[0];
144
+ if (!hostInstance) {
145
+ throw new Error("host instance not found!");
146
+ }
147
+ const dataFetchFn = await loadDataFetchModule(hostInstance, remoteId);
148
+ const data = await dataFetchFn({
149
+ ...params,
150
+ isDowngrade: !remoteInfo,
151
+ _id: dataFetchKey
152
+ });
153
+ logger.log("fetch data from server, loadDataFetchModule res: ", data);
154
+ return ctx.json(data);
155
+ } catch (e) {
156
+ logger.error("server plugin data fetch error: ", e);
157
+ ctx.status(500);
158
+ return ctx.text(`failed to fetch ${remoteInfo.name} data, error:
159
+ ${e}`);
160
+ }
161
+ };
162
+ export {
163
+ dataFetchServerMiddleware as default
164
+ };