codekingpro/portable-devtools
114k
1const dns = require('dns')
2
3const conditionalHeaders = [
4 'if-modified-since',
5 'if-none-match',
6 'if-unmodified-since',
7 'if-match',
8 'if-range',
9]
10
11const configureOptions = (opts) => {
12 const { strictSSL, ...options } = { ...opts }
13 options.method = options.method ? options.method.toUpperCase() : 'GET'
14
15 if (strictSSL === undefined || strictSSL === null) {
16 options.rejectUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0'
17 } else {
18 options.rejectUnauthorized = strictSSL !== false
19 }
20
21 if (!options.retry) {
22 options.retry = { retries: 0 }
23 } else if (typeof options.retry === 'string') {
24 const retries = parseInt(options.retry, 10)
25 if (isFinite(retries)) {
26 options.retry = { retries }
27 } else {
28 options.retry = { retries: 0 }
29 }
30 } else if (typeof options.retry === 'number') {
31 options.retry = { retries: options.retry }
32 } else {
33 options.retry = { retries: 0, ...options.retry }
34 }
35
36 options.dns = { ttl: 5 * 60 * 1000, lookup: dns.lookup, ...options.dns }
37
38 options.cache = options.cache || 'default'
39 if (options.cache === 'default') {
40 const hasConditionalHeader = Object.keys(options.headers || {}).some((name) => {
41 return conditionalHeaders.includes(name.toLowerCase())
42 })
43 if (hasConditionalHeader) {
44 options.cache = 'no-store'
45 }
46 }
47
48 options.cacheAdditionalHeaders = options.cacheAdditionalHeaders || []
49
50 // cacheManager is deprecated, but if it's set and
51 // cachePath is not we should copy it to the new field
52 if (options.cacheManager && !options.cachePath) {
53 options.cachePath = options.cacheManager
54 }
55
56 return options
57}
58
59module.exports = configureOptions
60 