index.js 5.9 KB

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