forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make dispatcher truly global (nodejs#1405)
* Make the dispatcher truly global * make sure the global dispatcher is used for Node.js fetch * Fix test * added version number
- Loading branch information
Showing
6 changed files
with
48 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npm run lint | ||
npm run test | ||
npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
// We include a version number for the Dispatcher API. In case of breaking changes, | ||
// this version number must be increased to avoid conflicts. | ||
const globalDispatcher = Symbol.for('undici.globalDispatcher.1') | ||
const { InvalidArgumentError } = require('./core/errors') | ||
const Agent = require('./agent') | ||
|
||
if (getGlobalDispatcher() === undefined) { | ||
setGlobalDispatcher(new Agent()) | ||
} | ||
|
||
function setGlobalDispatcher (agent) { | ||
if (!agent || typeof agent.dispatch !== 'function') { | ||
throw new InvalidArgumentError('Argument agent must implement Agent') | ||
} | ||
Object.defineProperty(globalThis, globalDispatcher, { | ||
value: agent, | ||
writable: true, | ||
enumerable: false, | ||
configurable: false | ||
}) | ||
} | ||
|
||
function getGlobalDispatcher () { | ||
return globalThis[globalDispatcher] | ||
} | ||
|
||
module.exports = { | ||
setGlobalDispatcher, | ||
getGlobalDispatcher | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters