-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_factory.js
More file actions
56 lines (50 loc) · 1.58 KB
/
fetch_factory.js
File metadata and controls
56 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const debug = require('util').debuglog('egg:lib:core:fetch_factory');
const mainNodejsVersion = parseInt(process.versions.node.split('.')[0]);
let FetchFactory;
let fetch;
let fetchInitialized = false;
let safeFetch;
let ssrfFetchFactory;
if (mainNodejsVersion >= 20) {
// urllib@4 only works on Node.js >= 20
try {
const urllib4 = require('urllib4');
FetchFactory = urllib4.FetchFactory;
debug('urllib4 enable');
fetch = function fetch(url, init) {
if (!fetchInitialized) {
const clientOptions = {};
if (this.config.httpclient?.lookup) {
clientOptions.lookup = this.config.httpclient.lookup;
}
FetchFactory.setClientOptions(clientOptions);
fetchInitialized = true;
}
return FetchFactory.fetch(url, init);
};
safeFetch = function safeFetch(url, init) {
if (!ssrfFetchFactory) {
const ssrfConfig = this.config.security?.ssrf;
const clientOptions = {};
if (ssrfConfig?.checkAddress) {
clientOptions.checkAddress = ssrfConfig.checkAddress;
} else {
this.logger.warn('[egg-security] please configure `config.security.ssrf` first');
}
if (this.config.httpclient?.lookup) {
clientOptions.lookup = this.config.httpclient.lookup;
}
ssrfFetchFactory = new FetchFactory();
ssrfFetchFactory.setClientOptions(clientOptions);
}
return ssrfFetchFactory.fetch(url, init);
};
} catch (err) {
debug('require urllib4 error: %s', err);
}
}
module.exports = {
FetchFactory,
safeFetch,
fetch,
};