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