xref: /third_party/node/deps/undici/src/lib/global.js (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// We include a version number for the Dispatcher API. In case of breaking changes,
41cb0ef41Sopenharmony_ci// this version number must be increased to avoid conflicts.
51cb0ef41Sopenharmony_ciconst globalDispatcher = Symbol.for('undici.globalDispatcher.1')
61cb0ef41Sopenharmony_ciconst { InvalidArgumentError } = require('./core/errors')
71cb0ef41Sopenharmony_ciconst Agent = require('./agent')
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciif (getGlobalDispatcher() === undefined) {
101cb0ef41Sopenharmony_ci  setGlobalDispatcher(new Agent())
111cb0ef41Sopenharmony_ci}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction setGlobalDispatcher (agent) {
141cb0ef41Sopenharmony_ci  if (!agent || typeof agent.dispatch !== 'function') {
151cb0ef41Sopenharmony_ci    throw new InvalidArgumentError('Argument agent must implement Agent')
161cb0ef41Sopenharmony_ci  }
171cb0ef41Sopenharmony_ci  Object.defineProperty(globalThis, globalDispatcher, {
181cb0ef41Sopenharmony_ci    value: agent,
191cb0ef41Sopenharmony_ci    writable: true,
201cb0ef41Sopenharmony_ci    enumerable: false,
211cb0ef41Sopenharmony_ci    configurable: false
221cb0ef41Sopenharmony_ci  })
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cifunction getGlobalDispatcher () {
261cb0ef41Sopenharmony_ci  return globalThis[globalDispatcher]
271cb0ef41Sopenharmony_ci}
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cimodule.exports = {
301cb0ef41Sopenharmony_ci  setGlobalDispatcher,
311cb0ef41Sopenharmony_ci  getGlobalDispatcher
321cb0ef41Sopenharmony_ci}
33