options.js 1.6 KB

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