xref: /third_party/node/deps/undici/src/lib/global.js (revision 1cb0ef41)
1'use strict'
2
3// We include a version number for the Dispatcher API. In case of breaking changes,
4// this version number must be increased to avoid conflicts.
5const globalDispatcher = Symbol.for('undici.globalDispatcher.1')
6const { InvalidArgumentError } = require('./core/errors')
7const Agent = require('./agent')
8
9if (getGlobalDispatcher() === undefined) {
10  setGlobalDispatcher(new Agent())
11}
12
13function setGlobalDispatcher (agent) {
14  if (!agent || typeof agent.dispatch !== 'function') {
15    throw new InvalidArgumentError('Argument agent must implement Agent')
16  }
17  Object.defineProperty(globalThis, globalDispatcher, {
18    value: agent,
19    writable: true,
20    enumerable: false,
21    configurable: false
22  })
23}
24
25function getGlobalDispatcher () {
26  return globalThis[globalDispatcher]
27}
28
29module.exports = {
30  setGlobalDispatcher,
31  getGlobalDispatcher
32}
33