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