@@ -15,23 +15,35 @@ const {httpClient} = require('@digitalbazaar/http-client');
15
15
* Creates a built-in node document loader.
16
16
*
17
17
* @param options the options to use:
18
- * secure: require all URLs to use HTTPS.
19
- * strictSSL: true to require SSL certificates to be valid,
20
- * false not to (default: true).
21
- * maxRedirects: the maximum number of redirects to permit, none by
22
- * default.
23
- * headers: an object (map) of headers which will be passed as request
24
- * headers for the requested document. Accept is not allowed.
18
+ * [secure]: require all URLs to use HTTPS. (default: false)
19
+ * [strictSSL]: true to require SSL certificates to be valid,
20
+ * false not to. (default: true)
21
+ * [maxRedirects]: the maximum number of redirects to permit.
22
+ * (default: none)
23
+ * [headers]: an object (map) of headers which will be passed as
24
+ * request headers for the requested document. Accept is not
25
+ * allowed. (default: none).
26
+ * [httpAgent]: a Node.js `http.Agent` to use with 'http' requests.
27
+ * (default: none)
28
+ * [httpsAgent]: a Node.js `https.Agent` to use with 'https' requests.
29
+ * (default: An agent with rejectUnauthorized to the strictSSL
30
+ * value)
25
31
*
26
32
* @return the node document loader.
27
33
*/
28
34
module . exports = ( {
29
35
secure,
30
36
strictSSL = true ,
31
37
maxRedirects = - 1 ,
32
- headers = { }
38
+ headers = { } ,
39
+ httpAgent,
40
+ httpsAgent
33
41
} = { strictSSL : true , maxRedirects : - 1 , headers : { } } ) => {
34
42
headers = buildHeaders ( headers ) ;
43
+ // set a default header if none was set
44
+ if ( ! ( 'user-agent' in headers ) ) {
45
+ headers [ 'user-agent' ] = 'jsonld.js' ;
46
+ }
35
47
const http = require ( 'http' ) ;
36
48
37
49
const queue = new RequestQueue ( ) ;
@@ -40,13 +52,15 @@ module.exports = ({
40
52
} ) ;
41
53
42
54
async function loadDocument ( url , redirects ) {
43
- if ( url . indexOf ( 'http:' ) !== 0 && url . indexOf ( 'https:' ) !== 0 ) {
55
+ const isHttp = url . startsWith ( 'http:' ) ;
56
+ const isHttps = url . startsWith ( 'https:' ) ;
57
+ if ( ! isHttp && ! isHttps ) {
44
58
throw new JsonLdError (
45
59
'URL could not be dereferenced; only "http" and "https" URLs are ' +
46
60
'supported.' ,
47
61
'jsonld.InvalidUrl' , { code : 'loading document failed' , url} ) ;
48
62
}
49
- if ( secure && url . indexOf ( 'https' ) !== 0 ) {
63
+ if ( secure && ! isHttps ) {
50
64
throw new JsonLdError (
51
65
'URL could not be dereferenced; secure mode is enabled and ' +
52
66
'the URL\'s scheme is not "https".' ,
@@ -60,7 +74,9 @@ module.exports = ({
60
74
61
75
let alternate = null ;
62
76
63
- const { res, body} = await _fetch ( { url, headers, strictSSL} ) ;
77
+ const { res, body} = await _fetch ( {
78
+ url, headers, strictSSL, httpAgent, httpsAgent
79
+ } ) ;
64
80
doc = { contextUrl : null , documentUrl : url , document : body || null } ;
65
81
// handle error
66
82
const statusText = http . STATUS_CODES [ res . status ] ;
@@ -143,11 +159,19 @@ module.exports = ({
143
159
}
144
160
} ;
145
161
146
- async function _fetch ( { url, headers, strictSSL} ) {
162
+ async function _fetch ( { url, headers, strictSSL, httpAgent , httpsAgent } ) {
147
163
try {
148
- const agent = new https . Agent ( { rejectUnauthorized : strictSSL } ) ;
149
- const res = await httpClient . get (
150
- url , { headers, agent, redirect : 'manual' } ) ;
164
+ const options = { headers, redirect : 'manual' } ;
165
+ const isHttps = url . startsWith ( 'https:' ) ;
166
+ if ( isHttps ) {
167
+ options . agent =
168
+ httpsAgent || new https . Agent ( { rejectUnauthorized : strictSSL } ) ;
169
+ } else {
170
+ if ( httpAgent ) {
171
+ options . agent = httpAgent ;
172
+ }
173
+ }
174
+ const res = await httpClient . get ( url , options ) ;
151
175
const body = JSON . stringify ( res . data , null , 2 ) ;
152
176
return { res, body} ;
153
177
} catch ( e ) {
0 commit comments