@module-federation/retry-plugin 0.0.0-next-20240905093149
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/LICENSE +21 -0
- package/README.md +54 -0
- package/dist/CHANGELOG.md +1 -0
- package/dist/README.md +54 -0
- package/dist/esm/index.js +187 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +212 -0
- package/dist/package.json +33 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present zhouxiao(zhoushaw)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# @module-federation/retry-plugin
|
|
2
|
+
|
|
3
|
+
> A plugin for retrying failed module requests.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
// ./src/runtime-plugin/retry.ts
|
|
9
|
+
import { RetryPlugin } from '@module-federation/retry-plugin';
|
|
10
|
+
const retryPlugin = () => RetryPlugin({
|
|
11
|
+
fetch: {
|
|
12
|
+
url: 'http://localhost:2008/not-exist-mf-manifest.json',
|
|
13
|
+
fallback: () => 'http://localhost:2001/mf-manifest.json',
|
|
14
|
+
},
|
|
15
|
+
script: {
|
|
16
|
+
// url: 'http://localhost:2008/not-exist-mf-manifest.json',
|
|
17
|
+
url: 'http://localhost:2001/static/js/async/src_App_tsx.js',
|
|
18
|
+
customCreateScript: (url: string, attrs: Record<string, string>) => {
|
|
19
|
+
let script = document.createElement('script');
|
|
20
|
+
script.src = `http://localhost:2011/static/js/async/src_App_tsx.js`;
|
|
21
|
+
script.setAttribute('loader-hoos', 'isTrue');
|
|
22
|
+
script.setAttribute('crossorigin', 'anonymous');
|
|
23
|
+
return script;
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
export default defineConfig({
|
|
29
|
+
tools: {
|
|
30
|
+
rspack: (config, { appendPlugins }) => {
|
|
31
|
+
appendPlugins([
|
|
32
|
+
new ModuleFederationPlugin({
|
|
33
|
+
...,
|
|
34
|
+
+ runtimePlugins: [
|
|
35
|
+
+ path.join(__dirname, './src/runtime-plugin/retry.ts'),
|
|
36
|
+
+ ],
|
|
37
|
+
}),
|
|
38
|
+
]);
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
plugins: [pluginReact()],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## Documentation
|
|
49
|
+
|
|
50
|
+
See [https://module-federation.io/plugin/plugins/retry-plugin.html](https://module-federation.io/plugin/plugins/retry-plugin.html) for details.
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
`@module-federation/retry-plugin` is [MIT licensed](https://github.com/module-federation/core/blob/main/packages/retry-plugin/LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @module-federation/retry-plugin
|
package/dist/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# @module-federation/retry-plugin
|
|
2
|
+
|
|
3
|
+
> A plugin for retrying failed module requests.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
// ./src/runtime-plugin/retry.ts
|
|
9
|
+
import { RetryPlugin } from '@module-federation/retry-plugin';
|
|
10
|
+
const retryPlugin = () => RetryPlugin({
|
|
11
|
+
fetch: {
|
|
12
|
+
url: 'http://localhost:2008/not-exist-mf-manifest.json',
|
|
13
|
+
fallback: () => 'http://localhost:2001/mf-manifest.json',
|
|
14
|
+
},
|
|
15
|
+
script: {
|
|
16
|
+
// url: 'http://localhost:2008/not-exist-mf-manifest.json',
|
|
17
|
+
url: 'http://localhost:2001/static/js/async/src_App_tsx.js',
|
|
18
|
+
customCreateScript: (url: string, attrs: Record<string, string>) => {
|
|
19
|
+
let script = document.createElement('script');
|
|
20
|
+
script.src = `http://localhost:2011/static/js/async/src_App_tsx.js`;
|
|
21
|
+
script.setAttribute('loader-hoos', 'isTrue');
|
|
22
|
+
script.setAttribute('crossorigin', 'anonymous');
|
|
23
|
+
return script;
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
export default defineConfig({
|
|
29
|
+
tools: {
|
|
30
|
+
rspack: (config, { appendPlugins }) => {
|
|
31
|
+
appendPlugins([
|
|
32
|
+
new ModuleFederationPlugin({
|
|
33
|
+
...,
|
|
34
|
+
+ runtimePlugins: [
|
|
35
|
+
+ path.join(__dirname, './src/runtime-plugin/retry.ts'),
|
|
36
|
+
+ ],
|
|
37
|
+
}),
|
|
38
|
+
]);
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
plugins: [pluginReact()],
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## Documentation
|
|
49
|
+
|
|
50
|
+
See [https://module-federation.io/plugin/plugins/retry-plugin.html](https://module-federation.io/plugin/plugins/retry-plugin.html) for details.
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
`@module-federation/retry-plugin` is [MIT licensed](https://github.com/module-federation/core/blob/main/packages/retry-plugin/LICENSE).
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// packages/retry-plugin/src/constant.ts
|
|
5
|
+
var defaultRetries = 3;
|
|
6
|
+
var defaultRetryDelay = 1e3;
|
|
7
|
+
var loadStatus = {
|
|
8
|
+
success: "success",
|
|
9
|
+
error: "error"
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// packages/retry-plugin/src/fetch-retry.ts
|
|
13
|
+
async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries, retryDelay = defaultRetryDelay, fallback }) {
|
|
14
|
+
try {
|
|
15
|
+
const response = await fetch(url, options);
|
|
16
|
+
const responseClone = response.clone();
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
throw new Error(`Server error\uFF1A${response.status}`);
|
|
19
|
+
}
|
|
20
|
+
await responseClone.json().catch((error) => {
|
|
21
|
+
throw new Error(`Json parse error: ${error}, url is: ${url}`);
|
|
22
|
+
});
|
|
23
|
+
return response;
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (retryTimes <= 0) {
|
|
26
|
+
console.log(`>>>>>>>>> \u3010Module Federation RetryPlugin\u3011: retry failed after ${retryTimes} times for url: ${url}, now will try fallbackUrl url <<<<<<<<<`);
|
|
27
|
+
if (fallback && typeof fallback === "function") {
|
|
28
|
+
return fetchWithRetry({
|
|
29
|
+
url: fallback(),
|
|
30
|
+
options,
|
|
31
|
+
retryTimes: 0,
|
|
32
|
+
retryDelay: 0
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
if (error instanceof Error && error.message.includes("Json parse error")) {
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
throw new Error("\u3010Module Federation RetryPlugin\u3011: The request failed three times and has now been abandoned");
|
|
39
|
+
} else {
|
|
40
|
+
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}`);
|
|
42
|
+
return await fetchWithRetry({
|
|
43
|
+
url,
|
|
44
|
+
options,
|
|
45
|
+
retryTimes: retryTimes - 1,
|
|
46
|
+
retryDelay,
|
|
47
|
+
fallback
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
__name(fetchWithRetry, "fetchWithRetry");
|
|
53
|
+
|
|
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
|
+
// packages/retry-plugin/src/index.ts
|
|
130
|
+
var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOption }) => ({
|
|
131
|
+
name: "retry-plugin",
|
|
132
|
+
async fetch(url, options) {
|
|
133
|
+
const _options = {
|
|
134
|
+
...options,
|
|
135
|
+
...fetchOption?.options
|
|
136
|
+
};
|
|
137
|
+
if (fetchOption) {
|
|
138
|
+
if (fetchOption.url) {
|
|
139
|
+
if (url === fetchOption?.url) {
|
|
140
|
+
return fetchWithRetry({
|
|
141
|
+
url: fetchOption.url,
|
|
142
|
+
options: _options,
|
|
143
|
+
retryTimes: fetchOption?.retryTimes,
|
|
144
|
+
fallback: fetchOption?.fallback
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
return fetchWithRetry({
|
|
149
|
+
url,
|
|
150
|
+
options: _options,
|
|
151
|
+
retryTimes: fetchOption?.retryTimes,
|
|
152
|
+
fallback: fetchOption?.fallback
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return fetch(url, options);
|
|
157
|
+
},
|
|
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
|
+
});
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
return scriptWithRetry({
|
|
175
|
+
url,
|
|
176
|
+
attrs: scriptAttrs,
|
|
177
|
+
retryTimes: scriptOption?.retryTimes,
|
|
178
|
+
customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return {};
|
|
183
|
+
}
|
|
184
|
+
}), "RetryPlugin");
|
|
185
|
+
export {
|
|
186
|
+
RetryPlugin
|
|
187
|
+
};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { FederationRuntimePlugin } from '@module-federation/runtime/types';
|
|
2
|
+
|
|
3
|
+
interface FetchWithRetryOptions {
|
|
4
|
+
url?: string;
|
|
5
|
+
options?: RequestInit;
|
|
6
|
+
retryTimes?: number;
|
|
7
|
+
retryDelay?: number;
|
|
8
|
+
fallback?: () => string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ScriptWithRetryOptions {
|
|
12
|
+
url?: string;
|
|
13
|
+
attrs?: Record<string, string>;
|
|
14
|
+
retryTimes?: number;
|
|
15
|
+
retryDelay?: number;
|
|
16
|
+
customCreateScript?: CreateScriptFunc;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type RetryPluginParams = {
|
|
20
|
+
fetch?: FetchWithRetryOptions; // fetch retry options
|
|
21
|
+
script?: ScriptWithRetryOptions; // script retry options
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type CreateScriptFunc = (
|
|
25
|
+
url: string,
|
|
26
|
+
attrs: Record<string, any>,
|
|
27
|
+
) => HTMLScriptElement;
|
|
28
|
+
|
|
29
|
+
declare const RetryPlugin: (params: RetryPluginParams) => FederationRuntimePlugin;
|
|
30
|
+
|
|
31
|
+
export { FetchWithRetryOptions, RetryPlugin, RetryPluginParams, ScriptWithRetryOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { FederationRuntimePlugin } from '@module-federation/runtime/types';
|
|
2
|
+
|
|
3
|
+
interface FetchWithRetryOptions {
|
|
4
|
+
url?: string;
|
|
5
|
+
options?: RequestInit;
|
|
6
|
+
retryTimes?: number;
|
|
7
|
+
retryDelay?: number;
|
|
8
|
+
fallback?: () => string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ScriptWithRetryOptions {
|
|
12
|
+
url?: string;
|
|
13
|
+
attrs?: Record<string, string>;
|
|
14
|
+
retryTimes?: number;
|
|
15
|
+
retryDelay?: number;
|
|
16
|
+
customCreateScript?: CreateScriptFunc;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type RetryPluginParams = {
|
|
20
|
+
fetch?: FetchWithRetryOptions; // fetch retry options
|
|
21
|
+
script?: ScriptWithRetryOptions; // script retry options
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type CreateScriptFunc = (
|
|
25
|
+
url: string,
|
|
26
|
+
attrs: Record<string, any>,
|
|
27
|
+
) => HTMLScriptElement;
|
|
28
|
+
|
|
29
|
+
declare const RetryPlugin: (params: RetryPluginParams) => FederationRuntimePlugin;
|
|
30
|
+
|
|
31
|
+
export { FetchWithRetryOptions, RetryPlugin, RetryPluginParams, ScriptWithRetryOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// packages/retry-plugin/src/index.ts
|
|
22
|
+
var src_exports = {};
|
|
23
|
+
__export(src_exports, {
|
|
24
|
+
RetryPlugin: () => RetryPlugin
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// packages/retry-plugin/src/constant.ts
|
|
29
|
+
var defaultRetries = 3;
|
|
30
|
+
var defaultRetryDelay = 1e3;
|
|
31
|
+
var loadStatus = {
|
|
32
|
+
success: "success",
|
|
33
|
+
error: "error"
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// packages/retry-plugin/src/fetch-retry.ts
|
|
37
|
+
async function fetchWithRetry({ url, options = {}, retryTimes = defaultRetries, retryDelay = defaultRetryDelay, fallback }) {
|
|
38
|
+
try {
|
|
39
|
+
const response = await fetch(url, options);
|
|
40
|
+
const responseClone = response.clone();
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
throw new Error(`Server error\uFF1A${response.status}`);
|
|
43
|
+
}
|
|
44
|
+
await responseClone.json().catch((error) => {
|
|
45
|
+
throw new Error(`Json parse error: ${error}, url is: ${url}`);
|
|
46
|
+
});
|
|
47
|
+
return response;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (retryTimes <= 0) {
|
|
50
|
+
console.log(`>>>>>>>>> \u3010Module Federation RetryPlugin\u3011: retry failed after ${retryTimes} times for url: ${url}, now will try fallbackUrl url <<<<<<<<<`);
|
|
51
|
+
if (fallback && typeof fallback === "function") {
|
|
52
|
+
return fetchWithRetry({
|
|
53
|
+
url: fallback(),
|
|
54
|
+
options,
|
|
55
|
+
retryTimes: 0,
|
|
56
|
+
retryDelay: 0
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (error instanceof Error && error.message.includes("Json parse error")) {
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
throw new Error("\u3010Module Federation RetryPlugin\u3011: The request failed three times and has now been abandoned");
|
|
63
|
+
} else {
|
|
64
|
+
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}`);
|
|
66
|
+
return await fetchWithRetry({
|
|
67
|
+
url,
|
|
68
|
+
options,
|
|
69
|
+
retryTimes: retryTimes - 1,
|
|
70
|
+
retryDelay,
|
|
71
|
+
fallback
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
__name(fetchWithRetry, "fetchWithRetry");
|
|
77
|
+
|
|
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
|
+
// packages/retry-plugin/src/index.ts
|
|
154
|
+
var RetryPlugin = /* @__PURE__ */ __name(({ fetch: fetchOption, script: scriptOption }) => ({
|
|
155
|
+
name: "retry-plugin",
|
|
156
|
+
async fetch(url, options) {
|
|
157
|
+
const _options = {
|
|
158
|
+
...options,
|
|
159
|
+
...fetchOption?.options
|
|
160
|
+
};
|
|
161
|
+
if (fetchOption) {
|
|
162
|
+
if (fetchOption.url) {
|
|
163
|
+
if (url === fetchOption?.url) {
|
|
164
|
+
return fetchWithRetry({
|
|
165
|
+
url: fetchOption.url,
|
|
166
|
+
options: _options,
|
|
167
|
+
retryTimes: fetchOption?.retryTimes,
|
|
168
|
+
fallback: fetchOption?.fallback
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
} else {
|
|
172
|
+
return fetchWithRetry({
|
|
173
|
+
url,
|
|
174
|
+
options: _options,
|
|
175
|
+
retryTimes: fetchOption?.retryTimes,
|
|
176
|
+
fallback: fetchOption?.fallback
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return fetch(url, options);
|
|
181
|
+
},
|
|
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
|
+
});
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
return scriptWithRetry({
|
|
199
|
+
url,
|
|
200
|
+
attrs: scriptAttrs,
|
|
201
|
+
retryTimes: scriptOption?.retryTimes,
|
|
202
|
+
customCreateScript: scriptOption?.customCreateScript ? scriptOption.customCreateScript : void 0
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return {};
|
|
207
|
+
}
|
|
208
|
+
}), "RetryPlugin");
|
|
209
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
210
|
+
0 && (module.exports = {
|
|
211
|
+
RetryPlugin
|
|
212
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@module-federation/retry-plugin",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"author": "danpeen <dapeen.feng@gmail.com>",
|
|
5
|
+
"main": "./dist/index.cjs.js",
|
|
6
|
+
"module": "./dist/index.esm.js",
|
|
7
|
+
"types": "./dist/index.cjs.d.ts",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/esm/index.js",
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"typesVersions": {
|
|
24
|
+
"*": {
|
|
25
|
+
".": [
|
|
26
|
+
"./dist/index.d.ts"
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@module-federation/runtime": "workspace:*"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@module-federation/retry-plugin",
|
|
3
|
+
"version": "0.0.0-next-20240905093149",
|
|
4
|
+
"author": "danpeen <dapeen.feng@gmail.com>",
|
|
5
|
+
"main": "./dist/index.cjs.js",
|
|
6
|
+
"module": "./dist/index.esm.js",
|
|
7
|
+
"types": "./dist/index.cjs.d.ts",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist/",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/esm/index.js",
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"typesVersions": {
|
|
24
|
+
"*": {
|
|
25
|
+
".": [
|
|
26
|
+
"./dist/index.d.ts"
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@module-federation/runtime": "0.0.0-next-20240905093149"
|
|
32
|
+
}
|
|
33
|
+
}
|