xref: /third_party/node/deps/undici/src/index.js (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst Client = require('./lib/client')
41cb0ef41Sopenharmony_ciconst Dispatcher = require('./lib/dispatcher')
51cb0ef41Sopenharmony_ciconst errors = require('./lib/core/errors')
61cb0ef41Sopenharmony_ciconst Pool = require('./lib/pool')
71cb0ef41Sopenharmony_ciconst BalancedPool = require('./lib/balanced-pool')
81cb0ef41Sopenharmony_ciconst Agent = require('./lib/agent')
91cb0ef41Sopenharmony_ciconst util = require('./lib/core/util')
101cb0ef41Sopenharmony_ciconst { InvalidArgumentError } = errors
111cb0ef41Sopenharmony_ciconst api = require('./lib/api')
121cb0ef41Sopenharmony_ciconst buildConnector = require('./lib/core/connect')
131cb0ef41Sopenharmony_ciconst MockClient = require('./lib/mock/mock-client')
141cb0ef41Sopenharmony_ciconst MockAgent = require('./lib/mock/mock-agent')
151cb0ef41Sopenharmony_ciconst MockPool = require('./lib/mock/mock-pool')
161cb0ef41Sopenharmony_ciconst mockErrors = require('./lib/mock/mock-errors')
171cb0ef41Sopenharmony_ciconst ProxyAgent = require('./lib/proxy-agent')
181cb0ef41Sopenharmony_ciconst RetryHandler = require('./lib/handler/RetryHandler')
191cb0ef41Sopenharmony_ciconst { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global')
201cb0ef41Sopenharmony_ciconst DecoratorHandler = require('./lib/handler/DecoratorHandler')
211cb0ef41Sopenharmony_ciconst RedirectHandler = require('./lib/handler/RedirectHandler')
221cb0ef41Sopenharmony_ciconst createRedirectInterceptor = require('./lib/interceptor/redirectInterceptor')
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cilet hasCrypto
251cb0ef41Sopenharmony_citry {
261cb0ef41Sopenharmony_ci  require('crypto')
271cb0ef41Sopenharmony_ci  hasCrypto = true
281cb0ef41Sopenharmony_ci} catch {
291cb0ef41Sopenharmony_ci  hasCrypto = false
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciObject.assign(Dispatcher.prototype, api)
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cimodule.exports.Dispatcher = Dispatcher
351cb0ef41Sopenharmony_cimodule.exports.Client = Client
361cb0ef41Sopenharmony_cimodule.exports.Pool = Pool
371cb0ef41Sopenharmony_cimodule.exports.BalancedPool = BalancedPool
381cb0ef41Sopenharmony_cimodule.exports.Agent = Agent
391cb0ef41Sopenharmony_cimodule.exports.ProxyAgent = ProxyAgent
401cb0ef41Sopenharmony_cimodule.exports.RetryHandler = RetryHandler
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cimodule.exports.DecoratorHandler = DecoratorHandler
431cb0ef41Sopenharmony_cimodule.exports.RedirectHandler = RedirectHandler
441cb0ef41Sopenharmony_cimodule.exports.createRedirectInterceptor = createRedirectInterceptor
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cimodule.exports.buildConnector = buildConnector
471cb0ef41Sopenharmony_cimodule.exports.errors = errors
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cifunction makeDispatcher (fn) {
501cb0ef41Sopenharmony_ci  return (url, opts, handler) => {
511cb0ef41Sopenharmony_ci    if (typeof opts === 'function') {
521cb0ef41Sopenharmony_ci      handler = opts
531cb0ef41Sopenharmony_ci      opts = null
541cb0ef41Sopenharmony_ci    }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    if (!url || (typeof url !== 'string' && typeof url !== 'object' && !(url instanceof URL))) {
571cb0ef41Sopenharmony_ci      throw new InvalidArgumentError('invalid url')
581cb0ef41Sopenharmony_ci    }
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    if (opts != null && typeof opts !== 'object') {
611cb0ef41Sopenharmony_ci      throw new InvalidArgumentError('invalid opts')
621cb0ef41Sopenharmony_ci    }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    if (opts && opts.path != null) {
651cb0ef41Sopenharmony_ci      if (typeof opts.path !== 'string') {
661cb0ef41Sopenharmony_ci        throw new InvalidArgumentError('invalid opts.path')
671cb0ef41Sopenharmony_ci      }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci      let path = opts.path
701cb0ef41Sopenharmony_ci      if (!opts.path.startsWith('/')) {
711cb0ef41Sopenharmony_ci        path = `/${path}`
721cb0ef41Sopenharmony_ci      }
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci      url = new URL(util.parseOrigin(url).origin + path)
751cb0ef41Sopenharmony_ci    } else {
761cb0ef41Sopenharmony_ci      if (!opts) {
771cb0ef41Sopenharmony_ci        opts = typeof url === 'object' ? url : {}
781cb0ef41Sopenharmony_ci      }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci      url = util.parseURL(url)
811cb0ef41Sopenharmony_ci    }
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci    const { agent, dispatcher = getGlobalDispatcher() } = opts
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    if (agent) {
861cb0ef41Sopenharmony_ci      throw new InvalidArgumentError('unsupported opts.agent. Did you mean opts.client?')
871cb0ef41Sopenharmony_ci    }
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci    return fn.call(dispatcher, {
901cb0ef41Sopenharmony_ci      ...opts,
911cb0ef41Sopenharmony_ci      origin: url.origin,
921cb0ef41Sopenharmony_ci      path: url.search ? `${url.pathname}${url.search}` : url.pathname,
931cb0ef41Sopenharmony_ci      method: opts.method || (opts.body ? 'PUT' : 'GET')
941cb0ef41Sopenharmony_ci    }, handler)
951cb0ef41Sopenharmony_ci  }
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_cimodule.exports.setGlobalDispatcher = setGlobalDispatcher
991cb0ef41Sopenharmony_cimodule.exports.getGlobalDispatcher = getGlobalDispatcher
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciif (util.nodeMajor > 16 || (util.nodeMajor === 16 && util.nodeMinor >= 8)) {
1021cb0ef41Sopenharmony_ci  let fetchImpl = null
1031cb0ef41Sopenharmony_ci  module.exports.fetch = async function fetch (resource) {
1041cb0ef41Sopenharmony_ci    if (!fetchImpl) {
1051cb0ef41Sopenharmony_ci      fetchImpl = require('./lib/fetch').fetch
1061cb0ef41Sopenharmony_ci    }
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci    try {
1091cb0ef41Sopenharmony_ci      return await fetchImpl(...arguments)
1101cb0ef41Sopenharmony_ci    } catch (err) {
1111cb0ef41Sopenharmony_ci      if (typeof err === 'object') {
1121cb0ef41Sopenharmony_ci        Error.captureStackTrace(err, this)
1131cb0ef41Sopenharmony_ci      }
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci      throw err
1161cb0ef41Sopenharmony_ci    }
1171cb0ef41Sopenharmony_ci  }
1181cb0ef41Sopenharmony_ci  module.exports.Headers = require('./lib/fetch/headers').Headers
1191cb0ef41Sopenharmony_ci  module.exports.Response = require('./lib/fetch/response').Response
1201cb0ef41Sopenharmony_ci  module.exports.Request = require('./lib/fetch/request').Request
1211cb0ef41Sopenharmony_ci  module.exports.FormData = require('./lib/fetch/formdata').FormData
1221cb0ef41Sopenharmony_ci  module.exports.File = require('./lib/fetch/file').File
1231cb0ef41Sopenharmony_ci  module.exports.FileReader = require('./lib/fileapi/filereader').FileReader
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  const { setGlobalOrigin, getGlobalOrigin } = require('./lib/fetch/global')
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  module.exports.setGlobalOrigin = setGlobalOrigin
1281cb0ef41Sopenharmony_ci  module.exports.getGlobalOrigin = getGlobalOrigin
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  const { CacheStorage } = require('./lib/cache/cachestorage')
1311cb0ef41Sopenharmony_ci  const { kConstruct } = require('./lib/cache/symbols')
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  // Cache & CacheStorage are tightly coupled with fetch. Even if it may run
1341cb0ef41Sopenharmony_ci  // in an older version of Node, it doesn't have any use without fetch.
1351cb0ef41Sopenharmony_ci  module.exports.caches = new CacheStorage(kConstruct)
1361cb0ef41Sopenharmony_ci}
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ciif (util.nodeMajor >= 16) {
1391cb0ef41Sopenharmony_ci  const { deleteCookie, getCookies, getSetCookies, setCookie } = require('./lib/cookies')
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci  module.exports.deleteCookie = deleteCookie
1421cb0ef41Sopenharmony_ci  module.exports.getCookies = getCookies
1431cb0ef41Sopenharmony_ci  module.exports.getSetCookies = getSetCookies
1441cb0ef41Sopenharmony_ci  module.exports.setCookie = setCookie
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci  const { parseMIMEType, serializeAMimeType } = require('./lib/fetch/dataURL')
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  module.exports.parseMIMEType = parseMIMEType
1491cb0ef41Sopenharmony_ci  module.exports.serializeAMimeType = serializeAMimeType
1501cb0ef41Sopenharmony_ci}
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ciif (util.nodeMajor >= 18 && hasCrypto) {
1531cb0ef41Sopenharmony_ci  const { WebSocket } = require('./lib/websocket/websocket')
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  module.exports.WebSocket = WebSocket
1561cb0ef41Sopenharmony_ci}
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_cimodule.exports.request = makeDispatcher(api.request)
1591cb0ef41Sopenharmony_cimodule.exports.stream = makeDispatcher(api.stream)
1601cb0ef41Sopenharmony_cimodule.exports.pipeline = makeDispatcher(api.pipeline)
1611cb0ef41Sopenharmony_cimodule.exports.connect = makeDispatcher(api.connect)
1621cb0ef41Sopenharmony_cimodule.exports.upgrade = makeDispatcher(api.upgrade)
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_cimodule.exports.MockClient = MockClient
1651cb0ef41Sopenharmony_cimodule.exports.MockPool = MockPool
1661cb0ef41Sopenharmony_cimodule.exports.MockAgent = MockAgent
1671cb0ef41Sopenharmony_cimodule.exports.mockErrors = mockErrors
168