egg 3.33.1 → 3.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +5 -1
- package/lib/core/fetch_factory.js +21 -5
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -295,6 +295,10 @@ declare module 'egg' {
|
|
|
295
295
|
maxFreeSockets?: number;
|
|
296
296
|
}
|
|
297
297
|
|
|
298
|
+
type Dispatcher = FetchFactory['getDispatcher'] extends () => infer R
|
|
299
|
+
? R
|
|
300
|
+
: never;
|
|
301
|
+
|
|
298
302
|
/** HttpClient config */
|
|
299
303
|
export interface HttpClientConfig extends HttpClientBaseConfig {
|
|
300
304
|
/** http.Agent */
|
|
@@ -319,8 +323,8 @@ declare module 'egg' {
|
|
|
319
323
|
allowH2?: boolean;
|
|
320
324
|
/** Custom lookup function for DNS resolution */
|
|
321
325
|
lookup?: LookupFunction;
|
|
326
|
+
interceptors?: Parameters<Dispatcher['compose']>;
|
|
322
327
|
}
|
|
323
|
-
|
|
324
328
|
export interface EggAppConfig {
|
|
325
329
|
workerStartTimeout: number;
|
|
326
330
|
baseDir: string;
|
|
@@ -3,7 +3,8 @@ const debug = require('util').debuglog('egg:lib:core:fetch_factory');
|
|
|
3
3
|
const mainNodejsVersion = parseInt(process.versions.node.split('.')[0]);
|
|
4
4
|
let FetchFactory;
|
|
5
5
|
let fetch;
|
|
6
|
-
|
|
6
|
+
// Track initialization per app instance by storing a WeakMap
|
|
7
|
+
const fetchInitializedMap = new WeakMap();
|
|
7
8
|
let safeFetch;
|
|
8
9
|
let ssrfFetchFactory;
|
|
9
10
|
|
|
@@ -14,15 +15,24 @@ if (mainNodejsVersion >= 20) {
|
|
|
14
15
|
FetchFactory = urllib4.FetchFactory;
|
|
15
16
|
debug('urllib4 enable');
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (!fetchInitialized) {
|
|
18
|
+
fetch = function(url, init) {
|
|
19
|
+
if (!fetchInitializedMap.get(this)) {
|
|
20
20
|
const clientOptions = {};
|
|
21
21
|
if (this.config.httpclient?.lookup) {
|
|
22
22
|
clientOptions.lookup = this.config.httpclient.lookup;
|
|
23
23
|
}
|
|
24
24
|
FetchFactory.setClientOptions(clientOptions);
|
|
25
|
-
|
|
25
|
+
|
|
26
|
+
// Support custom interceptors via dispatcher.compose
|
|
27
|
+
// Must be set after setClientOptions because setClientOptions resets dispatcher
|
|
28
|
+
// interceptors is an array of interceptor functions that follow undici's dispatcher API(undici have not supported clientOptions.interceptors natively yet)
|
|
29
|
+
if (this.config.httpclient?.interceptors) {
|
|
30
|
+
const interceptors = this.config.httpclient.interceptors;
|
|
31
|
+
const originalDispatcher = FetchFactory.getDispatcher();
|
|
32
|
+
FetchFactory.setDispatcher(originalDispatcher.compose(interceptors));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fetchInitializedMap.set(this, true);
|
|
26
36
|
}
|
|
27
37
|
return FetchFactory.fetch(url, init);
|
|
28
38
|
};
|
|
@@ -41,6 +51,12 @@ if (mainNodejsVersion >= 20) {
|
|
|
41
51
|
}
|
|
42
52
|
ssrfFetchFactory = new FetchFactory();
|
|
43
53
|
ssrfFetchFactory.setClientOptions(clientOptions);
|
|
54
|
+
|
|
55
|
+
if (this.config.httpclient?.interceptors) {
|
|
56
|
+
const interceptors = this.config.httpclient.interceptors;
|
|
57
|
+
const originalDispatcher = ssrfFetchFactory.getDispatcher();
|
|
58
|
+
ssrfFetchFactory.setDispatcher(originalDispatcher.compose(interceptors));
|
|
59
|
+
}
|
|
44
60
|
}
|
|
45
61
|
return ssrfFetchFactory.fetch(url, init);
|
|
46
62
|
};
|