global.js 860 B

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