index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict'
  2. const Client = require('./lib/client')
  3. const Dispatcher = require('./lib/dispatcher')
  4. const errors = require('./lib/core/errors')
  5. const Pool = require('./lib/pool')
  6. const BalancedPool = require('./lib/balanced-pool')
  7. const Agent = require('./lib/agent')
  8. const util = require('./lib/core/util')
  9. const { InvalidArgumentError } = errors
  10. const api = require('./lib/api')
  11. const buildConnector = require('./lib/core/connect')
  12. const MockClient = require('./lib/mock/mock-client')
  13. const MockAgent = require('./lib/mock/mock-agent')
  14. const MockPool = require('./lib/mock/mock-pool')
  15. const mockErrors = require('./lib/mock/mock-errors')
  16. const ProxyAgent = require('./lib/proxy-agent')
  17. const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global')
  18. const DecoratorHandler = require('./lib/handler/DecoratorHandler')
  19. const RedirectHandler = require('./lib/handler/RedirectHandler')
  20. const createRedirectInterceptor = require('./lib/interceptor/redirectInterceptor')
  21. let hasCrypto
  22. try {
  23. require('crypto')
  24. hasCrypto = true
  25. } catch {
  26. hasCrypto = false
  27. }
  28. Object.assign(Dispatcher.prototype, api)
  29. module.exports.Dispatcher = Dispatcher
  30. module.exports.Client = Client
  31. module.exports.Pool = Pool
  32. module.exports.BalancedPool = BalancedPool
  33. module.exports.Agent = Agent
  34. module.exports.ProxyAgent = ProxyAgent
  35. module.exports.DecoratorHandler = DecoratorHandler
  36. module.exports.RedirectHandler = RedirectHandler
  37. module.exports.createRedirectInterceptor = createRedirectInterceptor
  38. module.exports.buildConnector = buildConnector
  39. module.exports.errors = errors
  40. function makeDispatcher (fn) {
  41. return (url, opts, handler) => {
  42. if (typeof opts === 'function') {
  43. handler = opts
  44. opts = null
  45. }
  46. if (!url || (typeof url !== 'string' && typeof url !== 'object' && !(url instanceof URL))) {
  47. throw new InvalidArgumentError('invalid url')
  48. }
  49. if (opts != null && typeof opts !== 'object') {
  50. throw new InvalidArgumentError('invalid opts')
  51. }
  52. if (opts && opts.path != null) {
  53. if (typeof opts.path !== 'string') {
  54. throw new InvalidArgumentError('invalid opts.path')
  55. }
  56. let path = opts.path
  57. if (!opts.path.startsWith('/')) {
  58. path = `/${path}`
  59. }
  60. url = new URL(util.parseOrigin(url).origin + path)
  61. } else {
  62. if (!opts) {
  63. opts = typeof url === 'object' ? url : {}
  64. }
  65. url = util.parseURL(url)
  66. }
  67. const { agent, dispatcher = getGlobalDispatcher() } = opts
  68. if (agent) {
  69. throw new InvalidArgumentError('unsupported opts.agent. Did you mean opts.client?')
  70. }
  71. return fn.call(dispatcher, {
  72. ...opts,
  73. origin: url.origin,
  74. path: url.search ? `${url.pathname}${url.search}` : url.pathname,
  75. method: opts.method || (opts.body ? 'PUT' : 'GET')
  76. }, handler)
  77. }
  78. }
  79. module.exports.setGlobalDispatcher = setGlobalDispatcher
  80. module.exports.getGlobalDispatcher = getGlobalDispatcher
  81. if (util.nodeMajor > 16 || (util.nodeMajor === 16 && util.nodeMinor >= 8)) {
  82. let fetchImpl = null
  83. module.exports.fetch = async function fetch (resource) {
  84. if (!fetchImpl) {
  85. fetchImpl = require('./lib/fetch').fetch
  86. }
  87. try {
  88. return await fetchImpl(...arguments)
  89. } catch (err) {
  90. Error.captureStackTrace(err, this)
  91. throw err
  92. }
  93. }
  94. module.exports.Headers = require('./lib/fetch/headers').Headers
  95. module.exports.Response = require('./lib/fetch/response').Response
  96. module.exports.Request = require('./lib/fetch/request').Request
  97. module.exports.FormData = require('./lib/fetch/formdata').FormData
  98. module.exports.File = require('./lib/fetch/file').File
  99. module.exports.FileReader = require('./lib/fileapi/filereader').FileReader
  100. const { setGlobalOrigin, getGlobalOrigin } = require('./lib/fetch/global')
  101. module.exports.setGlobalOrigin = setGlobalOrigin
  102. module.exports.getGlobalOrigin = getGlobalOrigin
  103. const { CacheStorage } = require('./lib/cache/cachestorage')
  104. const { kConstruct } = require('./lib/cache/symbols')
  105. // Cache & CacheStorage are tightly coupled with fetch. Even if it may run
  106. // in an older version of Node, it doesn't have any use without fetch.
  107. module.exports.caches = new CacheStorage(kConstruct)
  108. }
  109. if (util.nodeMajor >= 16) {
  110. const { deleteCookie, getCookies, getSetCookies, setCookie } = require('./lib/cookies')
  111. module.exports.deleteCookie = deleteCookie
  112. module.exports.getCookies = getCookies
  113. module.exports.getSetCookies = getSetCookies
  114. module.exports.setCookie = setCookie
  115. const { parseMIMEType, serializeAMimeType } = require('./lib/fetch/dataURL')
  116. module.exports.parseMIMEType = parseMIMEType
  117. module.exports.serializeAMimeType = serializeAMimeType
  118. }
  119. if (util.nodeMajor >= 18 && hasCrypto) {
  120. const { WebSocket } = require('./lib/websocket/websocket')
  121. module.exports.WebSocket = WebSocket
  122. }
  123. module.exports.request = makeDispatcher(api.request)
  124. module.exports.stream = makeDispatcher(api.stream)
  125. module.exports.pipeline = makeDispatcher(api.pipeline)
  126. module.exports.connect = makeDispatcher(api.connect)
  127. module.exports.upgrade = makeDispatcher(api.upgrade)
  128. module.exports.MockClient = MockClient
  129. module.exports.MockPool = MockPool
  130. module.exports.MockAgent = MockAgent
  131. module.exports.mockErrors = mockErrors