summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/npm/node_modules/undici/lib/agent.js
diff options
context:
space:
mode:
authorTekky <98614666+xtekky@users.noreply.github.com>2023-10-28 09:22:33 +0200
committerGitHub <noreply@github.com>2023-10-28 09:22:33 +0200
commit1a3b59838e21b98b5cfcafddf1737afb25129cfe (patch)
treeb9c645cbe897af198ea0551509f901a249af35f2 /g4f/Provider/npm/node_modules/undici/lib/agent.js
parentMerge pull request #1176 from hlohaus/history (diff)
parentAdd arkose_token to OpenaiChat (diff)
downloadgpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar
gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.gz
gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.bz2
gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.lz
gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.xz
gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.tar.zst
gpt4free-1a3b59838e21b98b5cfcafddf1737afb25129cfe.zip
Diffstat (limited to 'g4f/Provider/npm/node_modules/undici/lib/agent.js')
-rw-r--r--g4f/Provider/npm/node_modules/undici/lib/agent.js148
1 files changed, 148 insertions, 0 deletions
diff --git a/g4f/Provider/npm/node_modules/undici/lib/agent.js b/g4f/Provider/npm/node_modules/undici/lib/agent.js
new file mode 100644
index 00000000..0b18f2a9
--- /dev/null
+++ b/g4f/Provider/npm/node_modules/undici/lib/agent.js
@@ -0,0 +1,148 @@
+'use strict'
+
+const { InvalidArgumentError } = require('./core/errors')
+const { kClients, kRunning, kClose, kDestroy, kDispatch, kInterceptors } = require('./core/symbols')
+const DispatcherBase = require('./dispatcher-base')
+const Pool = require('./pool')
+const Client = require('./client')
+const util = require('./core/util')
+const createRedirectInterceptor = require('./interceptor/redirectInterceptor')
+const { WeakRef, FinalizationRegistry } = require('./compat/dispatcher-weakref')()
+
+const kOnConnect = Symbol('onConnect')
+const kOnDisconnect = Symbol('onDisconnect')
+const kOnConnectionError = Symbol('onConnectionError')
+const kMaxRedirections = Symbol('maxRedirections')
+const kOnDrain = Symbol('onDrain')
+const kFactory = Symbol('factory')
+const kFinalizer = Symbol('finalizer')
+const kOptions = Symbol('options')
+
+function defaultFactory (origin, opts) {
+ return opts && opts.connections === 1
+ ? new Client(origin, opts)
+ : new Pool(origin, opts)
+}
+
+class Agent extends DispatcherBase {
+ constructor ({ factory = defaultFactory, maxRedirections = 0, connect, ...options } = {}) {
+ super()
+
+ if (typeof factory !== 'function') {
+ throw new InvalidArgumentError('factory must be a function.')
+ }
+
+ if (connect != null && typeof connect !== 'function' && typeof connect !== 'object') {
+ throw new InvalidArgumentError('connect must be a function or an object')
+ }
+
+ if (!Number.isInteger(maxRedirections) || maxRedirections < 0) {
+ throw new InvalidArgumentError('maxRedirections must be a positive number')
+ }
+
+ if (connect && typeof connect !== 'function') {
+ connect = { ...connect }
+ }
+
+ this[kInterceptors] = options.interceptors && options.interceptors.Agent && Array.isArray(options.interceptors.Agent)
+ ? options.interceptors.Agent
+ : [createRedirectInterceptor({ maxRedirections })]
+
+ this[kOptions] = { ...util.deepClone(options), connect }
+ this[kOptions].interceptors = options.interceptors
+ ? { ...options.interceptors }
+ : undefined
+ this[kMaxRedirections] = maxRedirections
+ this[kFactory] = factory
+ this[kClients] = new Map()
+ this[kFinalizer] = new FinalizationRegistry(/* istanbul ignore next: gc is undeterministic */ key => {
+ const ref = this[kClients].get(key)
+ if (ref !== undefined && ref.deref() === undefined) {
+ this[kClients].delete(key)
+ }
+ })
+
+ const agent = this
+
+ this[kOnDrain] = (origin, targets) => {
+ agent.emit('drain', origin, [agent, ...targets])
+ }
+
+ this[kOnConnect] = (origin, targets) => {
+ agent.emit('connect', origin, [agent, ...targets])
+ }
+
+ this[kOnDisconnect] = (origin, targets, err) => {
+ agent.emit('disconnect', origin, [agent, ...targets], err)
+ }
+
+ this[kOnConnectionError] = (origin, targets, err) => {
+ agent.emit('connectionError', origin, [agent, ...targets], err)
+ }
+ }
+
+ get [kRunning] () {
+ let ret = 0
+ for (const ref of this[kClients].values()) {
+ const client = ref.deref()
+ /* istanbul ignore next: gc is undeterministic */
+ if (client) {
+ ret += client[kRunning]
+ }
+ }
+ return ret
+ }
+
+ [kDispatch] (opts, handler) {
+ let key
+ if (opts.origin && (typeof opts.origin === 'string' || opts.origin instanceof URL)) {
+ key = String(opts.origin)
+ } else {
+ throw new InvalidArgumentError('opts.origin must be a non-empty string or URL.')
+ }
+
+ const ref = this[kClients].get(key)
+
+ let dispatcher = ref ? ref.deref() : null
+ if (!dispatcher) {
+ dispatcher = this[kFactory](opts.origin, this[kOptions])
+ .on('drain', this[kOnDrain])
+ .on('connect', this[kOnConnect])
+ .on('disconnect', this[kOnDisconnect])
+ .on('connectionError', this[kOnConnectionError])
+
+ this[kClients].set(key, new WeakRef(dispatcher))
+ this[kFinalizer].register(dispatcher, key)
+ }
+
+ return dispatcher.dispatch(opts, handler)
+ }
+
+ async [kClose] () {
+ const closePromises = []
+ for (const ref of this[kClients].values()) {
+ const client = ref.deref()
+ /* istanbul ignore else: gc is undeterministic */
+ if (client) {
+ closePromises.push(client.close())
+ }
+ }
+
+ await Promise.all(closePromises)
+ }
+
+ async [kDestroy] (err) {
+ const destroyPromises = []
+ for (const ref of this[kClients].values()) {
+ const client = ref.deref()
+ /* istanbul ignore else: gc is undeterministic */
+ if (client) {
+ destroyPromises.push(client.destroy(err))
+ }
+ }
+
+ await Promise.all(destroyPromises)
+ }
+}
+
+module.exports = Agent