global.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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('./dispatcher/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. // These are the globals that can be installed by undici.install().
  25. // Not exported by index.js to avoid use outside of this module.
  26. const installedExports = /** @type {const} */ (
  27. [
  28. 'fetch',
  29. 'Headers',
  30. 'Response',
  31. 'Request',
  32. 'FormData',
  33. 'WebSocket',
  34. 'CloseEvent',
  35. 'ErrorEvent',
  36. 'MessageEvent',
  37. 'EventSource'
  38. ]
  39. )
  40. module.exports = {
  41. setGlobalDispatcher,
  42. getGlobalDispatcher,
  43. installedExports
  44. }