@module-federation/retry-plugin 0.0.0-docs-remove-invalid-lark-link-20251205062649
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 +347 -0
- package/dist/CHANGELOG.md +451 -0
- package/dist/README.md +347 -0
- package/dist/esm/index.js +361 -0
- package/dist/index.d.mts +110 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.js +390 -0
- package/dist/package.json +41 -0
- package/package.json +41 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { ModuleFederationRuntimePlugin } from '@module-federation/runtime/types';
|
|
2
|
+
|
|
3
|
+
type CommonRetryOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* retry request options
|
|
6
|
+
*/
|
|
7
|
+
fetchOptions?: RequestInit;
|
|
8
|
+
/**
|
|
9
|
+
* retry times
|
|
10
|
+
*/
|
|
11
|
+
retryTimes?: number;
|
|
12
|
+
/**
|
|
13
|
+
* retry success times
|
|
14
|
+
*/
|
|
15
|
+
successTimes?: number;
|
|
16
|
+
/**
|
|
17
|
+
* retry delay
|
|
18
|
+
*/
|
|
19
|
+
retryDelay?: number;
|
|
20
|
+
/**
|
|
21
|
+
* retry path
|
|
22
|
+
*/
|
|
23
|
+
getRetryPath?: (url: string) => string;
|
|
24
|
+
/**
|
|
25
|
+
* add query parameter
|
|
26
|
+
*/
|
|
27
|
+
addQuery?:
|
|
28
|
+
| boolean
|
|
29
|
+
| ((context: { times: number; originalQuery: string }) => string);
|
|
30
|
+
/**
|
|
31
|
+
* retry domains
|
|
32
|
+
*/
|
|
33
|
+
domains?: string[];
|
|
34
|
+
/**
|
|
35
|
+
* retry manifest domains
|
|
36
|
+
*/
|
|
37
|
+
manifestDomains?: string[];
|
|
38
|
+
/**
|
|
39
|
+
* retry callback
|
|
40
|
+
*/
|
|
41
|
+
onRetry?: ({
|
|
42
|
+
times,
|
|
43
|
+
domains,
|
|
44
|
+
url,
|
|
45
|
+
}: {
|
|
46
|
+
times?: number;
|
|
47
|
+
domains?: string[];
|
|
48
|
+
url?: string;
|
|
49
|
+
tagName?: string;
|
|
50
|
+
}) => void;
|
|
51
|
+
/**
|
|
52
|
+
* retry success callback
|
|
53
|
+
*/
|
|
54
|
+
onSuccess?: ({
|
|
55
|
+
domains,
|
|
56
|
+
url,
|
|
57
|
+
tagName,
|
|
58
|
+
}: {
|
|
59
|
+
domains?: string[];
|
|
60
|
+
url?: string;
|
|
61
|
+
tagName?: string;
|
|
62
|
+
}) => void;
|
|
63
|
+
/**
|
|
64
|
+
* retry failure callback
|
|
65
|
+
*/
|
|
66
|
+
onError?: ({
|
|
67
|
+
domains,
|
|
68
|
+
url,
|
|
69
|
+
tagName,
|
|
70
|
+
}: {
|
|
71
|
+
domains?: string[];
|
|
72
|
+
url?: string;
|
|
73
|
+
tagName?: string;
|
|
74
|
+
}) => void;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
type FetchRetryOptions = {
|
|
78
|
+
url?: string;
|
|
79
|
+
fetchOptions?: RequestInit;
|
|
80
|
+
} & CommonRetryOptions;
|
|
81
|
+
|
|
82
|
+
type ScriptRetryOptions = {
|
|
83
|
+
retryOptions: CommonRetryOptions;
|
|
84
|
+
retryFn: (...args: any[]) => Promise<any> | (() => Promise<any>);
|
|
85
|
+
beforeExecuteRetry?: (...args: any[]) => void;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
declare function rewriteWithNextDomain(currentUrl: string, domains?: string[]): string | null;
|
|
89
|
+
declare function appendRetryCountQuery(url: string, retryIndex: number, key?: string): string;
|
|
90
|
+
declare function getRetryUrl(baseUrl: string, opts?: {
|
|
91
|
+
domains?: string[];
|
|
92
|
+
addQuery?: boolean | ((context: {
|
|
93
|
+
times: number;
|
|
94
|
+
originalQuery: string;
|
|
95
|
+
}) => string);
|
|
96
|
+
retryIndex?: number;
|
|
97
|
+
queryKey?: string;
|
|
98
|
+
}): string;
|
|
99
|
+
/**
|
|
100
|
+
* Extract domain/host info from a URL and combine it with path/query from another URL
|
|
101
|
+
* This is useful for domain rotation while preserving original path and query parameters
|
|
102
|
+
* @param domainUrl - URL containing the target domain/host
|
|
103
|
+
* @param pathQueryUrl - URL containing the target path and query parameters
|
|
104
|
+
* @returns Combined URL with domain from domainUrl and path/query from pathQueryUrl
|
|
105
|
+
*/
|
|
106
|
+
declare function combineUrlDomainWithPathQuery(domainUrl: string, pathQueryUrl: string): string;
|
|
107
|
+
|
|
108
|
+
declare const RetryPlugin: (params?: CommonRetryOptions) => ModuleFederationRuntimePlugin;
|
|
109
|
+
|
|
110
|
+
export { type CommonRetryOptions, type FetchRetryOptions, RetryPlugin, type ScriptRetryOptions, appendRetryCountQuery, combineUrlDomainWithPathQuery, getRetryUrl, rewriteWithNextDomain };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { ModuleFederationRuntimePlugin } from '@module-federation/runtime/types';
|
|
2
|
+
|
|
3
|
+
type CommonRetryOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* retry request options
|
|
6
|
+
*/
|
|
7
|
+
fetchOptions?: RequestInit;
|
|
8
|
+
/**
|
|
9
|
+
* retry times
|
|
10
|
+
*/
|
|
11
|
+
retryTimes?: number;
|
|
12
|
+
/**
|
|
13
|
+
* retry success times
|
|
14
|
+
*/
|
|
15
|
+
successTimes?: number;
|
|
16
|
+
/**
|
|
17
|
+
* retry delay
|
|
18
|
+
*/
|
|
19
|
+
retryDelay?: number;
|
|
20
|
+
/**
|
|
21
|
+
* retry path
|
|
22
|
+
*/
|
|
23
|
+
getRetryPath?: (url: string) => string;
|
|
24
|
+
/**
|
|
25
|
+
* add query parameter
|
|
26
|
+
*/
|
|
27
|
+
addQuery?:
|
|
28
|
+
| boolean
|
|
29
|
+
| ((context: { times: number; originalQuery: string }) => string);
|
|
30
|
+
/**
|
|
31
|
+
* retry domains
|
|
32
|
+
*/
|
|
33
|
+
domains?: string[];
|
|
34
|
+
/**
|
|
35
|
+
* retry manifest domains
|
|
36
|
+
*/
|
|
37
|
+
manifestDomains?: string[];
|
|
38
|
+
/**
|
|
39
|
+
* retry callback
|
|
40
|
+
*/
|
|
41
|
+
onRetry?: ({
|
|
42
|
+
times,
|
|
43
|
+
domains,
|
|
44
|
+
url,
|
|
45
|
+
}: {
|
|
46
|
+
times?: number;
|
|
47
|
+
domains?: string[];
|
|
48
|
+
url?: string;
|
|
49
|
+
tagName?: string;
|
|
50
|
+
}) => void;
|
|
51
|
+
/**
|
|
52
|
+
* retry success callback
|
|
53
|
+
*/
|
|
54
|
+
onSuccess?: ({
|
|
55
|
+
domains,
|
|
56
|
+
url,
|
|
57
|
+
tagName,
|
|
58
|
+
}: {
|
|
59
|
+
domains?: string[];
|
|
60
|
+
url?: string;
|
|
61
|
+
tagName?: string;
|
|
62
|
+
}) => void;
|
|
63
|
+
/**
|
|
64
|
+
* retry failure callback
|
|
65
|
+
*/
|
|
66
|
+
onError?: ({
|
|
67
|
+
domains,
|
|
68
|
+
url,
|
|
69
|
+
tagName,
|
|
70
|
+
}: {
|
|
71
|
+
domains?: string[];
|
|
72
|
+
url?: string;
|
|
73
|
+
tagName?: string;
|
|
74
|
+
}) => void;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
type FetchRetryOptions = {
|
|
78
|
+
url?: string;
|
|
79
|
+
fetchOptions?: RequestInit;
|
|
80
|
+
} & CommonRetryOptions;
|
|
81
|
+
|
|
82
|
+
type ScriptRetryOptions = {
|
|
83
|
+
retryOptions: CommonRetryOptions;
|
|
84
|
+
retryFn: (...args: any[]) => Promise<any> | (() => Promise<any>);
|
|
85
|
+
beforeExecuteRetry?: (...args: any[]) => void;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
declare function rewriteWithNextDomain(currentUrl: string, domains?: string[]): string | null;
|
|
89
|
+
declare function appendRetryCountQuery(url: string, retryIndex: number, key?: string): string;
|
|
90
|
+
declare function getRetryUrl(baseUrl: string, opts?: {
|
|
91
|
+
domains?: string[];
|
|
92
|
+
addQuery?: boolean | ((context: {
|
|
93
|
+
times: number;
|
|
94
|
+
originalQuery: string;
|
|
95
|
+
}) => string);
|
|
96
|
+
retryIndex?: number;
|
|
97
|
+
queryKey?: string;
|
|
98
|
+
}): string;
|
|
99
|
+
/**
|
|
100
|
+
* Extract domain/host info from a URL and combine it with path/query from another URL
|
|
101
|
+
* This is useful for domain rotation while preserving original path and query parameters
|
|
102
|
+
* @param domainUrl - URL containing the target domain/host
|
|
103
|
+
* @param pathQueryUrl - URL containing the target path and query parameters
|
|
104
|
+
* @returns Combined URL with domain from domainUrl and path/query from pathQueryUrl
|
|
105
|
+
*/
|
|
106
|
+
declare function combineUrlDomainWithPathQuery(domainUrl: string, pathQueryUrl: string): string;
|
|
107
|
+
|
|
108
|
+
declare const RetryPlugin: (params?: CommonRetryOptions) => ModuleFederationRuntimePlugin;
|
|
109
|
+
|
|
110
|
+
export { type CommonRetryOptions, type FetchRetryOptions, RetryPlugin, type ScriptRetryOptions, appendRetryCountQuery, combineUrlDomainWithPathQuery, getRetryUrl, rewriteWithNextDomain };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
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
|
+
appendRetryCountQuery: () => appendRetryCountQuery,
|
|
26
|
+
combineUrlDomainWithPathQuery: () => combineUrlDomainWithPathQuery,
|
|
27
|
+
getRetryUrl: () => getRetryUrl,
|
|
28
|
+
rewriteWithNextDomain: () => rewriteWithNextDomain
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(src_exports);
|
|
31
|
+
|
|
32
|
+
// packages/retry-plugin/src/constant.ts
|
|
33
|
+
var defaultRetries = 3;
|
|
34
|
+
var defaultRetryDelay = 1e3;
|
|
35
|
+
var PLUGIN_IDENTIFIER = "[ Module Federation RetryPlugin ]";
|
|
36
|
+
var ERROR_ABANDONED = "The request failed and has now been abandoned";
|
|
37
|
+
var RUNTIME_008 = "RUNTIME-008";
|
|
38
|
+
|
|
39
|
+
// packages/retry-plugin/src/logger.ts
|
|
40
|
+
var import_sdk = require("@module-federation/sdk");
|
|
41
|
+
var logger = (0, import_sdk.createLogger)(PLUGIN_IDENTIFIER);
|
|
42
|
+
var logger_default = logger;
|
|
43
|
+
|
|
44
|
+
// packages/retry-plugin/src/utils.ts
|
|
45
|
+
function rewriteWithNextDomain(currentUrl, domains) {
|
|
46
|
+
if (!domains || domains.length === 0)
|
|
47
|
+
return null;
|
|
48
|
+
try {
|
|
49
|
+
const u = new URL(currentUrl);
|
|
50
|
+
const currentHostname = u.hostname;
|
|
51
|
+
const currentPort = u.port;
|
|
52
|
+
const currentHost = `${currentHostname}${currentPort ? `:${currentPort}` : ""}`;
|
|
53
|
+
const normalized = domains.map((d) => {
|
|
54
|
+
try {
|
|
55
|
+
const du = new URL(d.startsWith("http") ? d : `https://${d}`);
|
|
56
|
+
return {
|
|
57
|
+
hostname: du.hostname,
|
|
58
|
+
port: du.port,
|
|
59
|
+
protocol: du.protocol
|
|
60
|
+
};
|
|
61
|
+
} catch {
|
|
62
|
+
return {
|
|
63
|
+
hostname: d,
|
|
64
|
+
port: "",
|
|
65
|
+
protocol: u.protocol
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}).filter((d) => !!d.hostname);
|
|
69
|
+
if (normalized.length === 0)
|
|
70
|
+
return null;
|
|
71
|
+
let idx = -1;
|
|
72
|
+
for (let i = normalized.length - 1; i >= 0; i--) {
|
|
73
|
+
const candHost = `${normalized[i].hostname}${normalized[i].port ? `:${normalized[i].port}` : ""}`;
|
|
74
|
+
if (candHost === currentHost) {
|
|
75
|
+
idx = i;
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const total = normalized.length;
|
|
80
|
+
for (let step = 1; step <= total; step++) {
|
|
81
|
+
const nextIdx = ((idx >= 0 ? idx : -1) + step) % total;
|
|
82
|
+
const candidate = normalized[nextIdx];
|
|
83
|
+
const candidateHost = `${candidate.hostname}${candidate.port ? `:${candidate.port}` : ""}`;
|
|
84
|
+
if (candidateHost !== currentHost) {
|
|
85
|
+
u.hostname = candidate.hostname;
|
|
86
|
+
if (candidate.port !== void 0 && candidate.port !== null && candidate.port !== "") {
|
|
87
|
+
u.port = candidate.port;
|
|
88
|
+
} else {
|
|
89
|
+
u.port = "";
|
|
90
|
+
}
|
|
91
|
+
u.protocol = candidate.protocol || u.protocol;
|
|
92
|
+
return u.toString();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
} catch {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
__name(rewriteWithNextDomain, "rewriteWithNextDomain");
|
|
101
|
+
function appendRetryCountQuery(url, retryIndex, key = "retryCount") {
|
|
102
|
+
try {
|
|
103
|
+
const u = new URL(url);
|
|
104
|
+
u.searchParams.delete(key);
|
|
105
|
+
u.searchParams.set(key, String(retryIndex));
|
|
106
|
+
return u.toString();
|
|
107
|
+
} catch {
|
|
108
|
+
return url;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
__name(appendRetryCountQuery, "appendRetryCountQuery");
|
|
112
|
+
function getRetryUrl(baseUrl, opts = {}) {
|
|
113
|
+
const { domains, addQuery, retryIndex = 0, queryKey = "retryCount" } = opts;
|
|
114
|
+
let cleanBaseUrl = baseUrl;
|
|
115
|
+
try {
|
|
116
|
+
const urlObj = new URL(baseUrl);
|
|
117
|
+
urlObj.searchParams.delete(queryKey);
|
|
118
|
+
cleanBaseUrl = urlObj.toString();
|
|
119
|
+
} catch {
|
|
120
|
+
}
|
|
121
|
+
let nextUrl = rewriteWithNextDomain(cleanBaseUrl, domains) ?? cleanBaseUrl;
|
|
122
|
+
if (retryIndex > 0 && addQuery) {
|
|
123
|
+
try {
|
|
124
|
+
const u = new URL(nextUrl);
|
|
125
|
+
const originalUrl = new URL(baseUrl);
|
|
126
|
+
originalUrl.searchParams.delete(queryKey);
|
|
127
|
+
const originalQuery = originalUrl.search.startsWith("?") ? originalUrl.search.slice(1) : originalUrl.search;
|
|
128
|
+
if (typeof addQuery === "function") {
|
|
129
|
+
const newQuery = addQuery({
|
|
130
|
+
times: retryIndex,
|
|
131
|
+
originalQuery
|
|
132
|
+
});
|
|
133
|
+
u.search = newQuery ? `?${newQuery.replace(/^\?/, "")}` : "";
|
|
134
|
+
nextUrl = u.toString();
|
|
135
|
+
} else if (addQuery === true) {
|
|
136
|
+
u.searchParams.delete(queryKey);
|
|
137
|
+
u.searchParams.set(queryKey, String(retryIndex));
|
|
138
|
+
nextUrl = u.toString();
|
|
139
|
+
}
|
|
140
|
+
} catch {
|
|
141
|
+
if (addQuery === true) {
|
|
142
|
+
nextUrl = appendRetryCountQuery(nextUrl, retryIndex, queryKey);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return nextUrl;
|
|
147
|
+
}
|
|
148
|
+
__name(getRetryUrl, "getRetryUrl");
|
|
149
|
+
function combineUrlDomainWithPathQuery(domainUrl, pathQueryUrl) {
|
|
150
|
+
try {
|
|
151
|
+
const domainUrlObj = new URL(domainUrl);
|
|
152
|
+
const pathQueryUrlObj = new URL(pathQueryUrl);
|
|
153
|
+
domainUrlObj.pathname = pathQueryUrlObj.pathname;
|
|
154
|
+
domainUrlObj.search = pathQueryUrlObj.search;
|
|
155
|
+
return domainUrlObj.toString();
|
|
156
|
+
} catch {
|
|
157
|
+
return pathQueryUrl;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
__name(combineUrlDomainWithPathQuery, "combineUrlDomainWithPathQuery");
|
|
161
|
+
|
|
162
|
+
// packages/retry-plugin/src/fetch-retry.ts
|
|
163
|
+
function autoParseResponse(url, response) {
|
|
164
|
+
try {
|
|
165
|
+
const parsed = new URL(url);
|
|
166
|
+
if (parsed.pathname.endsWith(".js") || parsed.pathname.endsWith(".cjs") || parsed.pathname.endsWith(".mjs")) {
|
|
167
|
+
return response.text();
|
|
168
|
+
}
|
|
169
|
+
return response.json();
|
|
170
|
+
} catch (error) {
|
|
171
|
+
return response.json();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
__name(autoParseResponse, "autoParseResponse");
|
|
175
|
+
async function fetchRetry(params, lastRequestUrl, originalTotal) {
|
|
176
|
+
const {
|
|
177
|
+
url,
|
|
178
|
+
fetchOptions = {},
|
|
179
|
+
retryTimes = defaultRetries,
|
|
180
|
+
retryDelay = defaultRetryDelay,
|
|
181
|
+
// List of retry domains when resource loading fails. In the domains array, the first item is the default domain for static resources, and the subsequent items are backup domains. When a request to a domain fails, the system will find that domain in the array and replace it with the next domain in the array.
|
|
182
|
+
domains,
|
|
183
|
+
// Whether to add query parameters during resource retry to avoid being affected by browser and CDN cache. When set to true, retry=${times} will be added to the query, requesting in the order of retry=1, retry=2, retry=3.
|
|
184
|
+
addQuery,
|
|
185
|
+
onRetry,
|
|
186
|
+
onSuccess,
|
|
187
|
+
onError
|
|
188
|
+
} = params;
|
|
189
|
+
if (!url) {
|
|
190
|
+
throw new Error(`${PLUGIN_IDENTIFIER}: url is required in fetchWithRetry`);
|
|
191
|
+
}
|
|
192
|
+
const total = originalTotal ?? params.retryTimes ?? defaultRetries;
|
|
193
|
+
const isFirstAttempt = !lastRequestUrl;
|
|
194
|
+
let baseUrl = url;
|
|
195
|
+
if (!isFirstAttempt && lastRequestUrl) {
|
|
196
|
+
baseUrl = combineUrlDomainWithPathQuery(lastRequestUrl, url);
|
|
197
|
+
}
|
|
198
|
+
let requestUrl = baseUrl;
|
|
199
|
+
if (!isFirstAttempt) {
|
|
200
|
+
requestUrl = getRetryUrl(baseUrl, {
|
|
201
|
+
domains,
|
|
202
|
+
addQuery,
|
|
203
|
+
retryIndex: total - retryTimes,
|
|
204
|
+
queryKey: "retryCount"
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
if (!isFirstAttempt && retryDelay > 0) {
|
|
209
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
210
|
+
}
|
|
211
|
+
const response = await fetch(requestUrl, fetchOptions);
|
|
212
|
+
const responseClone = response.clone();
|
|
213
|
+
if (!response.ok) {
|
|
214
|
+
throw new Error(`${PLUGIN_IDENTIFIER}: Request failed: ${response.status} ${response.statusText || ""} | url: ${requestUrl}`);
|
|
215
|
+
}
|
|
216
|
+
await autoParseResponse(requestUrl, responseClone).catch((error) => {
|
|
217
|
+
throw new Error(`${PLUGIN_IDENTIFIER}: JSON parse failed: ${error?.message || String(error)} | url: ${requestUrl}`);
|
|
218
|
+
});
|
|
219
|
+
if (!isFirstAttempt) {
|
|
220
|
+
onSuccess && requestUrl && onSuccess({
|
|
221
|
+
domains,
|
|
222
|
+
url: requestUrl,
|
|
223
|
+
tagName: "fetch"
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return response;
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (retryTimes <= 0) {
|
|
229
|
+
const attemptedRetries = total - retryTimes;
|
|
230
|
+
if (!isFirstAttempt && attemptedRetries > 0) {
|
|
231
|
+
onError && onError({
|
|
232
|
+
domains,
|
|
233
|
+
url: requestUrl,
|
|
234
|
+
tagName: "fetch"
|
|
235
|
+
});
|
|
236
|
+
logger_default.log(`${PLUGIN_IDENTIFIER}: retry failed, no retries left for url: ${requestUrl}`);
|
|
237
|
+
}
|
|
238
|
+
throw new Error(`${RUNTIME_008}: ${PLUGIN_IDENTIFIER}: ${ERROR_ABANDONED} | url: ${requestUrl}`);
|
|
239
|
+
} else {
|
|
240
|
+
const nextIndex = total - retryTimes + 1;
|
|
241
|
+
const predictedBaseUrl = combineUrlDomainWithPathQuery(requestUrl, url);
|
|
242
|
+
const predictedNextUrl = getRetryUrl(predictedBaseUrl, {
|
|
243
|
+
domains,
|
|
244
|
+
addQuery,
|
|
245
|
+
retryIndex: nextIndex,
|
|
246
|
+
queryKey: "retryCount"
|
|
247
|
+
});
|
|
248
|
+
onRetry && onRetry({
|
|
249
|
+
times: nextIndex,
|
|
250
|
+
domains,
|
|
251
|
+
url: predictedNextUrl,
|
|
252
|
+
tagName: "fetch"
|
|
253
|
+
});
|
|
254
|
+
logger_default.log(`${PLUGIN_IDENTIFIER}: Trying again. Number of retries left: ${retryTimes - 1}`);
|
|
255
|
+
return await fetchRetry({
|
|
256
|
+
...params,
|
|
257
|
+
retryTimes: retryTimes - 1
|
|
258
|
+
}, requestUrl, total);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
__name(fetchRetry, "fetchRetry");
|
|
263
|
+
|
|
264
|
+
// packages/retry-plugin/src/script-retry.ts
|
|
265
|
+
function scriptRetry({ retryOptions, retryFn, beforeExecuteRetry = /* @__PURE__ */ __name(() => {
|
|
266
|
+
}, "beforeExecuteRetry") }) {
|
|
267
|
+
return async function(params) {
|
|
268
|
+
let retryWrapper;
|
|
269
|
+
let lastError;
|
|
270
|
+
let lastRequestUrl;
|
|
271
|
+
let originalUrl;
|
|
272
|
+
const { retryTimes = defaultRetries, retryDelay = defaultRetryDelay, domains, addQuery, onRetry, onSuccess, onError } = retryOptions || {};
|
|
273
|
+
let attempts = 0;
|
|
274
|
+
const maxAttempts = retryTimes;
|
|
275
|
+
while (attempts < maxAttempts) {
|
|
276
|
+
try {
|
|
277
|
+
beforeExecuteRetry();
|
|
278
|
+
if (retryDelay > 0 && attempts > 0) {
|
|
279
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
280
|
+
}
|
|
281
|
+
const retryIndex = attempts + 1;
|
|
282
|
+
retryWrapper = await retryFn({
|
|
283
|
+
...params,
|
|
284
|
+
getEntryUrl: (url) => {
|
|
285
|
+
if (!originalUrl) {
|
|
286
|
+
originalUrl = url;
|
|
287
|
+
}
|
|
288
|
+
let baseUrl = originalUrl;
|
|
289
|
+
if (lastRequestUrl) {
|
|
290
|
+
baseUrl = combineUrlDomainWithPathQuery(lastRequestUrl, originalUrl);
|
|
291
|
+
}
|
|
292
|
+
const next = getRetryUrl(baseUrl, {
|
|
293
|
+
domains,
|
|
294
|
+
addQuery,
|
|
295
|
+
retryIndex,
|
|
296
|
+
queryKey: "retryCount"
|
|
297
|
+
});
|
|
298
|
+
onRetry && onRetry({
|
|
299
|
+
times: retryIndex,
|
|
300
|
+
domains,
|
|
301
|
+
url: next,
|
|
302
|
+
tagName: "script"
|
|
303
|
+
});
|
|
304
|
+
lastRequestUrl = next;
|
|
305
|
+
return next;
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
onSuccess && lastRequestUrl && onSuccess({
|
|
309
|
+
domains,
|
|
310
|
+
url: lastRequestUrl,
|
|
311
|
+
tagName: "script"
|
|
312
|
+
});
|
|
313
|
+
break;
|
|
314
|
+
} catch (error) {
|
|
315
|
+
lastError = error;
|
|
316
|
+
attempts++;
|
|
317
|
+
if (attempts >= maxAttempts) {
|
|
318
|
+
onError && lastRequestUrl && onError({
|
|
319
|
+
domains,
|
|
320
|
+
url: lastRequestUrl,
|
|
321
|
+
tagName: "script"
|
|
322
|
+
});
|
|
323
|
+
throw new Error(`${PLUGIN_IDENTIFIER}: ${ERROR_ABANDONED} | url: ${lastRequestUrl || "unknown"}`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return retryWrapper;
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
__name(scriptRetry, "scriptRetry");
|
|
331
|
+
|
|
332
|
+
// packages/retry-plugin/src/index.ts
|
|
333
|
+
var RetryPlugin = /* @__PURE__ */ __name((params) => {
|
|
334
|
+
if (params?.fetch || params?.script) {
|
|
335
|
+
logger_default.warn(`${PLUGIN_IDENTIFIER}: params is ${params}, fetch or script config is deprecated, please use the new config style. See docs: https://module-federation.io/plugin/plugins/retry-plugin.html`);
|
|
336
|
+
}
|
|
337
|
+
const { fetchOptions = {}, retryTimes = defaultRetries, successTimes = 0, retryDelay = defaultRetryDelay, domains = [], manifestDomains = [], addQuery, onRetry, onSuccess, onError } = params || {};
|
|
338
|
+
return {
|
|
339
|
+
name: "retry-plugin",
|
|
340
|
+
async fetch(manifestUrl, options) {
|
|
341
|
+
return fetchRetry({
|
|
342
|
+
url: manifestUrl,
|
|
343
|
+
fetchOptions: {
|
|
344
|
+
...options,
|
|
345
|
+
...fetchOptions
|
|
346
|
+
},
|
|
347
|
+
domains: manifestDomains || domains,
|
|
348
|
+
addQuery,
|
|
349
|
+
onRetry,
|
|
350
|
+
onSuccess,
|
|
351
|
+
onError,
|
|
352
|
+
retryTimes,
|
|
353
|
+
successTimes,
|
|
354
|
+
retryDelay
|
|
355
|
+
});
|
|
356
|
+
},
|
|
357
|
+
async loadEntryError({ getRemoteEntry, origin, remoteInfo, remoteEntryExports, globalLoading, uniqueKey }) {
|
|
358
|
+
const beforeExecuteRetry = /* @__PURE__ */ __name(() => {
|
|
359
|
+
delete globalLoading[uniqueKey];
|
|
360
|
+
}, "beforeExecuteRetry");
|
|
361
|
+
const getRemoteEntryRetry = scriptRetry({
|
|
362
|
+
retryOptions: {
|
|
363
|
+
retryTimes,
|
|
364
|
+
retryDelay,
|
|
365
|
+
domains,
|
|
366
|
+
addQuery,
|
|
367
|
+
onRetry,
|
|
368
|
+
onSuccess,
|
|
369
|
+
onError
|
|
370
|
+
},
|
|
371
|
+
retryFn: getRemoteEntry,
|
|
372
|
+
beforeExecuteRetry
|
|
373
|
+
});
|
|
374
|
+
const result = await getRemoteEntryRetry({
|
|
375
|
+
origin,
|
|
376
|
+
remoteInfo,
|
|
377
|
+
remoteEntryExports
|
|
378
|
+
});
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
}, "RetryPlugin");
|
|
383
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
384
|
+
0 && (module.exports = {
|
|
385
|
+
RetryPlugin,
|
|
386
|
+
appendRetryCountQuery,
|
|
387
|
+
combineUrlDomainWithPathQuery,
|
|
388
|
+
getRetryUrl,
|
|
389
|
+
rewriteWithNextDomain
|
|
390
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@module-federation/retry-plugin",
|
|
3
|
+
"version": "0.0.0-docs-remove-invalid-lark-link-20251205062649",
|
|
4
|
+
"author": "danpeen <dapeen.feng@gmail.com>",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/esm/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/module-federation/core.git",
|
|
12
|
+
"directory": "packages/retry-plugin"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist/",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"import": "./dist/esm/index.js",
|
|
25
|
+
"require": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"typesVersions": {
|
|
29
|
+
"*": {
|
|
30
|
+
".": [
|
|
31
|
+
"./dist/index.d.ts"
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@module-federation/runtime": "workspace:*"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@module-federation/sdk": "workspace:*"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@module-federation/retry-plugin",
|
|
3
|
+
"version": "0.0.0-docs-remove-invalid-lark-link-20251205062649",
|
|
4
|
+
"author": "danpeen <dapeen.feng@gmail.com>",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/esm/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/module-federation/core.git",
|
|
12
|
+
"directory": "packages/retry-plugin"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist/",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"import": "./dist/esm/index.js",
|
|
25
|
+
"require": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"typesVersions": {
|
|
29
|
+
"*": {
|
|
30
|
+
".": [
|
|
31
|
+
"./dist/index.d.ts"
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@module-federation/runtime": "0.0.0-docs-remove-invalid-lark-link-20251205062649"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@module-federation/sdk": "0.0.0-docs-remove-invalid-lark-link-20251205062649"
|
|
40
|
+
}
|
|
41
|
+
}
|