@module-federation/retry-plugin 0.6.13 → 0.6.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @module-federation/retry-plugin
2
2
 
3
+ ## 0.6.15
4
+
5
+ ### Patch Changes
6
+
7
+ - ec31539: fix(retry-plugin): fix script retry logic to make error-boundary should render until script retry finished.
8
+ - 283574b: feat(retry-plugin): allow fallback function to receive failed URL
9
+ - @module-federation/sdk@0.6.15
10
+
11
+ ## 0.6.14
12
+
13
+ ### Patch Changes
14
+
15
+ - ad605d2: chore: unified logger
16
+ - Updated dependencies [ad605d2]
17
+ - @module-federation/sdk@0.6.14
18
+
3
19
  ## 0.6.13
4
20
 
5
21
  ## 0.6.12
package/dist/esm/index.js CHANGED
@@ -4,10 +4,12 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
4
4
  // packages/retry-plugin/src/constant.ts
5
5
  var defaultRetries = 3;
6
6
  var defaultRetryDelay = 1e3;
7
- var loadStatus = {
8
- success: "success",
9
- error: "error"
10
- };
7
+ var PLUGIN_IDENTIFIER = "[ Module Federation RetryPlugin ]";
8
+
9
+ // packages/retry-plugin/src/logger.ts
10
+ import { createLogger } from "@module-federation/sdk";
11
+ var logger = createLogger(PLUGIN_IDENTIFIER);
12
+ var logger_default = logger;
11
13
 
12
14
  // packages/retry-plugin/src/fetch-retry.ts
13
15
  async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries, retryDelay = defaultRetryDelay, fallback }) {
@@ -23,10 +25,10 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
23
25
  return response;
24
26
  } catch (error) {
25
27
  if (retryTimes <= 0) {
26
- console.log(`>>>>>>>>> \u3010Module Federation RetryPlugin\u3011: retry failed after ${retryTimes} times for url: ${url}, now will try fallbackUrl url <<<<<<<<<`);
28
+ logger_default.log(`>>>>>>>>> retry failed after ${retryTimes} times for url: ${url}, now will try fallbackUrl url <<<<<<<<<`);
27
29
  if (fallback && typeof fallback === "function") {
28
30
  return fetchWithRetry({
29
- url: fallback(),
31
+ url: fallback(url),
30
32
  options,
31
33
  retryTimes: 0,
32
34
  retryDelay: 0
@@ -35,10 +37,10 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
35
37
  if (error instanceof Error && error.message.includes("Json parse error")) {
36
38
  throw error;
37
39
  }
38
- throw new Error("\u3010Module Federation RetryPlugin\u3011: The request failed three times and has now been abandoned");
40
+ throw new Error(`${PLUGIN_IDENTIFIER}: The request failed three times and has now been abandoned`);
39
41
  } else {
40
42
  retryDelay > 0 && await new Promise((resolve) => setTimeout(resolve, retryDelay));
41
- console.log(`\u3010Module Federation RetryPlugin\u3011: Trying again. Number of retries available\uFF1A${retryTimes - 1}`);
43
+ logger_default.log(`Trying again. Number of retries available\uFF1A${retryTimes - 1}`);
42
44
  return await fetchWithRetry({
43
45
  url,
44
46
  options,
@@ -51,81 +53,6 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
51
53
  }
52
54
  __name(fetchWithRetry, "fetchWithRetry");
53
55
 
54
- // packages/retry-plugin/src/script-retry.ts
55
- var defaultCreateScript = /* @__PURE__ */ __name((url, attrs) => {
56
- let script = document.createElement("script");
57
- script.src = url;
58
- Object.keys(attrs).forEach((key) => {
59
- if (key === "async" || key === "defer") {
60
- script[key] = attrs[key];
61
- } else if (!script.getAttribute(key)) {
62
- script.setAttribute(key, attrs[key]);
63
- }
64
- });
65
- return script;
66
- }, "defaultCreateScript");
67
- var getScript = /* @__PURE__ */ __name((url, attrs, customCreateScript) => {
68
- let script = null;
69
- if (customCreateScript && typeof customCreateScript === "function") {
70
- script = customCreateScript(url, attrs);
71
- }
72
- if (!script) {
73
- script = defaultCreateScript(url, attrs);
74
- }
75
- return script;
76
- }, "getScript");
77
- async function loadScript(url, attrs, maxRetries = defaultRetries, retryDelay = defaultRetryDelay, customCreateScript) {
78
- let retries = 0;
79
- function attemptLoad() {
80
- return new Promise((resolve, reject) => {
81
- const script = getScript(url, attrs, customCreateScript);
82
- script.onload = (event) => {
83
- resolve({
84
- status: loadStatus.success,
85
- event
86
- });
87
- };
88
- script.onerror = (event) => {
89
- if (retries < maxRetries) {
90
- retries++;
91
- console.warn(`\u3010Module Federation RetryPlugin\u3011: Failed to load script. Retrying... (${retries}/${maxRetries})`);
92
- retryDelay > 0 && setTimeout(() => {
93
- resolve(attemptLoad());
94
- }, retryDelay);
95
- } else {
96
- console.error("\u3010Module Federation RetryPlugin\u3011: Failed to load script after maximum retries. the url is:", url);
97
- resolve({
98
- status: loadStatus.error,
99
- event
100
- });
101
- }
102
- };
103
- document.head.appendChild(script);
104
- });
105
- }
106
- __name(attemptLoad, "attemptLoad");
107
- return attemptLoad();
108
- }
109
- __name(loadScript, "loadScript");
110
- function scriptWithRetry({ url, attrs = {}, retryTimes = defaultRetries, retryDelay = defaultRetryDelay, customCreateScript }) {
111
- const script = getScript(url, attrs, customCreateScript);
112
- const originOnerror = script.onerror;
113
- const originOnLoad = script.onload;
114
- script.onerror = async (event) => {
115
- console.warn(`\u3010Module Federation RetryPlugin\u3011: Script load failed, retrying (${retryTimes + 1}/${defaultRetries}): ${url}`);
116
- const scriptLoader = await loadScript(url, attrs, retryTimes, retryDelay, customCreateScript);
117
- if (scriptLoader.status === loadStatus.success) {
118
- originOnLoad?.call(script, scriptLoader?.event);
119
- return;
120
- } else {
121
- originOnerror?.call(script, scriptLoader?.event);
122
- }
123
- return;
124
- };
125
- return script;
126
- }
127
- __name(scriptWithRetry, "scriptWithRetry");
128
-
129
56
  // packages/retry-plugin/src/index.ts
130
57
  var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOption }) => ({
131
58
  name: "retry-plugin",
@@ -155,31 +82,26 @@ var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOp
155
82
  }
156
83
  return fetch(url, options);
157
84
  },
158
- createScript({ url, attrs }) {
159
- const scriptAttrs = scriptOption?.attrs ? {
160
- ...attrs,
161
- ...scriptOption.attrs
162
- } : attrs;
163
- if (scriptOption) {
164
- if (scriptOption?.url) {
165
- if (url === scriptOption?.url) {
166
- return scriptWithRetry({
167
- url: scriptOption?.url,
168
- attrs: scriptAttrs,
169
- retryTimes: scriptOption?.retryTimes,
170
- customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
171
- });
85
+ async getModuleFactory({ remoteEntryExports, expose, moduleInfo }) {
86
+ let moduleFactory;
87
+ const { retryTimes = defaultRetries, retryDelay = defaultRetryDelay } = scriptOption || {};
88
+ if (scriptOption?.moduleName && scriptOption?.moduleName.some((m) => moduleInfo.name === m || moduleInfo?.alias === m) || scriptOption?.moduleName === void 0) {
89
+ let attempts = 0;
90
+ while (attempts - 1 < retryTimes) {
91
+ try {
92
+ moduleFactory = await remoteEntryExports.get(expose);
93
+ break;
94
+ } catch (error) {
95
+ attempts++;
96
+ if (attempts - 1 >= retryTimes) {
97
+ scriptOption?.cb && await new Promise((resolve) => scriptOption?.cb && scriptOption?.cb(resolve, error));
98
+ throw error;
99
+ }
100
+ await new Promise((resolve) => setTimeout(resolve, retryDelay));
172
101
  }
173
- } else {
174
- return scriptWithRetry({
175
- url,
176
- attrs: scriptAttrs,
177
- retryTimes: scriptOption?.retryTimes,
178
- customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
179
- });
180
102
  }
181
103
  }
182
- return {};
104
+ return moduleFactory;
183
105
  }
184
106
  }), "RetryPlugin");
185
107
  export {
package/dist/index.d.mts CHANGED
@@ -1,31 +1,27 @@
1
1
  import { FederationRuntimePlugin } from '@module-federation/runtime/types';
2
2
 
3
3
  interface FetchWithRetryOptions {
4
- url?: string;
4
+ url: string;
5
5
  options?: RequestInit;
6
6
  retryTimes?: number;
7
7
  retryDelay?: number;
8
- fallback?: () => string;
8
+ fallback?:
9
+ | (() => string)
10
+ | ((url: string | URL | globalThis.Request) => string);
9
11
  }
10
12
 
11
13
  interface ScriptWithRetryOptions {
12
- url?: string;
13
- attrs?: Record<string, string>;
14
14
  retryTimes?: number;
15
15
  retryDelay?: number;
16
- customCreateScript?: CreateScriptFunc;
16
+ moduleName?: Array<string>;
17
+ cb?: (resolve: (value: unknown) => void, error: any) => void;
17
18
  }
18
19
 
19
20
  type RetryPluginParams = {
20
- fetch?: FetchWithRetryOptions; // fetch retry options
21
- script?: ScriptWithRetryOptions; // script retry options
21
+ fetch?: FetchWithRetryOptions;
22
+ script?: ScriptWithRetryOptions;
22
23
  };
23
24
 
24
- type CreateScriptFunc = (
25
- url: string,
26
- attrs: Record<string, any>,
27
- ) => HTMLScriptElement;
28
-
29
25
  declare const RetryPlugin: (params: RetryPluginParams) => FederationRuntimePlugin;
30
26
 
31
27
  export { FetchWithRetryOptions, RetryPlugin, RetryPluginParams, ScriptWithRetryOptions };
package/dist/index.d.ts CHANGED
@@ -1,31 +1,27 @@
1
1
  import { FederationRuntimePlugin } from '@module-federation/runtime/types';
2
2
 
3
3
  interface FetchWithRetryOptions {
4
- url?: string;
4
+ url: string;
5
5
  options?: RequestInit;
6
6
  retryTimes?: number;
7
7
  retryDelay?: number;
8
- fallback?: () => string;
8
+ fallback?:
9
+ | (() => string)
10
+ | ((url: string | URL | globalThis.Request) => string);
9
11
  }
10
12
 
11
13
  interface ScriptWithRetryOptions {
12
- url?: string;
13
- attrs?: Record<string, string>;
14
14
  retryTimes?: number;
15
15
  retryDelay?: number;
16
- customCreateScript?: CreateScriptFunc;
16
+ moduleName?: Array<string>;
17
+ cb?: (resolve: (value: unknown) => void, error: any) => void;
17
18
  }
18
19
 
19
20
  type RetryPluginParams = {
20
- fetch?: FetchWithRetryOptions; // fetch retry options
21
- script?: ScriptWithRetryOptions; // script retry options
21
+ fetch?: FetchWithRetryOptions;
22
+ script?: ScriptWithRetryOptions;
22
23
  };
23
24
 
24
- type CreateScriptFunc = (
25
- url: string,
26
- attrs: Record<string, any>,
27
- ) => HTMLScriptElement;
28
-
29
25
  declare const RetryPlugin: (params: RetryPluginParams) => FederationRuntimePlugin;
30
26
 
31
27
  export { FetchWithRetryOptions, RetryPlugin, RetryPluginParams, ScriptWithRetryOptions };
package/dist/index.js CHANGED
@@ -28,10 +28,12 @@ module.exports = __toCommonJS(src_exports);
28
28
  // packages/retry-plugin/src/constant.ts
29
29
  var defaultRetries = 3;
30
30
  var defaultRetryDelay = 1e3;
31
- var loadStatus = {
32
- success: "success",
33
- error: "error"
34
- };
31
+ var PLUGIN_IDENTIFIER = "[ Module Federation RetryPlugin ]";
32
+
33
+ // packages/retry-plugin/src/logger.ts
34
+ var import_sdk = require("@module-federation/sdk");
35
+ var logger = (0, import_sdk.createLogger)(PLUGIN_IDENTIFIER);
36
+ var logger_default = logger;
35
37
 
36
38
  // packages/retry-plugin/src/fetch-retry.ts
37
39
  async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries, retryDelay = defaultRetryDelay, fallback }) {
@@ -47,10 +49,10 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
47
49
  return response;
48
50
  } catch (error) {
49
51
  if (retryTimes <= 0) {
50
- console.log(`>>>>>>>>> \u3010Module Federation RetryPlugin\u3011: retry failed after ${retryTimes} times for url: ${url}, now will try fallbackUrl url <<<<<<<<<`);
52
+ logger_default.log(`>>>>>>>>> retry failed after ${retryTimes} times for url: ${url}, now will try fallbackUrl url <<<<<<<<<`);
51
53
  if (fallback && typeof fallback === "function") {
52
54
  return fetchWithRetry({
53
- url: fallback(),
55
+ url: fallback(url),
54
56
  options,
55
57
  retryTimes: 0,
56
58
  retryDelay: 0
@@ -59,10 +61,10 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
59
61
  if (error instanceof Error && error.message.includes("Json parse error")) {
60
62
  throw error;
61
63
  }
62
- throw new Error("\u3010Module Federation RetryPlugin\u3011: The request failed three times and has now been abandoned");
64
+ throw new Error(`${PLUGIN_IDENTIFIER}: The request failed three times and has now been abandoned`);
63
65
  } else {
64
66
  retryDelay > 0 && await new Promise((resolve) => setTimeout(resolve, retryDelay));
65
- console.log(`\u3010Module Federation RetryPlugin\u3011: Trying again. Number of retries available\uFF1A${retryTimes - 1}`);
67
+ logger_default.log(`Trying again. Number of retries available\uFF1A${retryTimes - 1}`);
66
68
  return await fetchWithRetry({
67
69
  url,
68
70
  options,
@@ -75,81 +77,6 @@ async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries,
75
77
  }
76
78
  __name(fetchWithRetry, "fetchWithRetry");
77
79
 
78
- // packages/retry-plugin/src/script-retry.ts
79
- var defaultCreateScript = /* @__PURE__ */ __name((url, attrs) => {
80
- let script = document.createElement("script");
81
- script.src = url;
82
- Object.keys(attrs).forEach((key) => {
83
- if (key === "async" || key === "defer") {
84
- script[key] = attrs[key];
85
- } else if (!script.getAttribute(key)) {
86
- script.setAttribute(key, attrs[key]);
87
- }
88
- });
89
- return script;
90
- }, "defaultCreateScript");
91
- var getScript = /* @__PURE__ */ __name((url, attrs, customCreateScript) => {
92
- let script = null;
93
- if (customCreateScript && typeof customCreateScript === "function") {
94
- script = customCreateScript(url, attrs);
95
- }
96
- if (!script) {
97
- script = defaultCreateScript(url, attrs);
98
- }
99
- return script;
100
- }, "getScript");
101
- async function loadScript(url, attrs, maxRetries = defaultRetries, retryDelay = defaultRetryDelay, customCreateScript) {
102
- let retries = 0;
103
- function attemptLoad() {
104
- return new Promise((resolve, reject) => {
105
- const script = getScript(url, attrs, customCreateScript);
106
- script.onload = (event) => {
107
- resolve({
108
- status: loadStatus.success,
109
- event
110
- });
111
- };
112
- script.onerror = (event) => {
113
- if (retries < maxRetries) {
114
- retries++;
115
- console.warn(`\u3010Module Federation RetryPlugin\u3011: Failed to load script. Retrying... (${retries}/${maxRetries})`);
116
- retryDelay > 0 && setTimeout(() => {
117
- resolve(attemptLoad());
118
- }, retryDelay);
119
- } else {
120
- console.error("\u3010Module Federation RetryPlugin\u3011: Failed to load script after maximum retries. the url is:", url);
121
- resolve({
122
- status: loadStatus.error,
123
- event
124
- });
125
- }
126
- };
127
- document.head.appendChild(script);
128
- });
129
- }
130
- __name(attemptLoad, "attemptLoad");
131
- return attemptLoad();
132
- }
133
- __name(loadScript, "loadScript");
134
- function scriptWithRetry({ url, attrs = {}, retryTimes = defaultRetries, retryDelay = defaultRetryDelay, customCreateScript }) {
135
- const script = getScript(url, attrs, customCreateScript);
136
- const originOnerror = script.onerror;
137
- const originOnLoad = script.onload;
138
- script.onerror = async (event) => {
139
- console.warn(`\u3010Module Federation RetryPlugin\u3011: Script load failed, retrying (${retryTimes + 1}/${defaultRetries}): ${url}`);
140
- const scriptLoader = await loadScript(url, attrs, retryTimes, retryDelay, customCreateScript);
141
- if (scriptLoader.status === loadStatus.success) {
142
- originOnLoad?.call(script, scriptLoader?.event);
143
- return;
144
- } else {
145
- originOnerror?.call(script, scriptLoader?.event);
146
- }
147
- return;
148
- };
149
- return script;
150
- }
151
- __name(scriptWithRetry, "scriptWithRetry");
152
-
153
80
  // packages/retry-plugin/src/index.ts
154
81
  var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOption }) => ({
155
82
  name: "retry-plugin",
@@ -179,31 +106,26 @@ var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOp
179
106
  }
180
107
  return fetch(url, options);
181
108
  },
182
- createScript({ url, attrs }) {
183
- const scriptAttrs = scriptOption?.attrs ? {
184
- ...attrs,
185
- ...scriptOption.attrs
186
- } : attrs;
187
- if (scriptOption) {
188
- if (scriptOption?.url) {
189
- if (url === scriptOption?.url) {
190
- return scriptWithRetry({
191
- url: scriptOption?.url,
192
- attrs: scriptAttrs,
193
- retryTimes: scriptOption?.retryTimes,
194
- customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
195
- });
109
+ async getModuleFactory({ remoteEntryExports, expose, moduleInfo }) {
110
+ let moduleFactory;
111
+ const { retryTimes = defaultRetries, retryDelay = defaultRetryDelay } = scriptOption || {};
112
+ if (scriptOption?.moduleName && scriptOption?.moduleName.some((m) => moduleInfo.name === m || moduleInfo?.alias === m) || scriptOption?.moduleName === void 0) {
113
+ let attempts = 0;
114
+ while (attempts - 1 < retryTimes) {
115
+ try {
116
+ moduleFactory = await remoteEntryExports.get(expose);
117
+ break;
118
+ } catch (error) {
119
+ attempts++;
120
+ if (attempts - 1 >= retryTimes) {
121
+ scriptOption?.cb && await new Promise((resolve) => scriptOption?.cb && scriptOption?.cb(resolve, error));
122
+ throw error;
123
+ }
124
+ await new Promise((resolve) => setTimeout(resolve, retryDelay));
196
125
  }
197
- } else {
198
- return scriptWithRetry({
199
- url,
200
- attrs: scriptAttrs,
201
- retryTimes: scriptOption?.retryTimes,
202
- customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
203
- });
204
126
  }
205
127
  }
206
- return {};
128
+ return moduleFactory;
207
129
  }
208
130
  }), "RetryPlugin");
209
131
  // Annotate the CommonJS export names for ESM import in node:
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/retry-plugin",
3
- "version": "0.6.13",
3
+ "version": "0.6.15",
4
4
  "author": "danpeen <dapeen.feng@gmail.com>",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -29,5 +29,8 @@
29
29
  },
30
30
  "devDependencies": {
31
31
  "@module-federation/runtime": "workspace:*"
32
+ },
33
+ "dependencies": {
34
+ "@module-federation/sdk": "workspace:*"
32
35
  }
33
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/retry-plugin",
3
- "version": "0.6.13",
3
+ "version": "0.6.15",
4
4
  "author": "danpeen <dapeen.feng@gmail.com>",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -28,6 +28,9 @@
28
28
  }
29
29
  },
30
30
  "devDependencies": {
31
- "@module-federation/runtime": "0.6.13"
31
+ "@module-federation/runtime": "0.6.15"
32
+ },
33
+ "dependencies": {
34
+ "@module-federation/sdk": "0.6.15"
32
35
  }
33
36
  }