Skip to content

Commit ae6828f

Browse files
authored
Merge pull request #7121 from NomicFoundation/dont-use-global-dispatcher
Don't use `undici`'s global dispatcher
2 parents a1f64c6 + 100c8ef commit ae6828f

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

.changeset/pink-schools-exercise.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"hardhat": patch
3+
"@nomicfoundation/hardhat-verify": patch
4+
---
5+
6+
Don't use `undici`'s global dispatcher, making Hardhat more stable across Node.js versions

packages/hardhat-core/src/internal/util/download.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ export async function download(
2424
timeoutMillis = 10000,
2525
extraHeaders: { [name: string]: string } = {}
2626
) {
27-
const { getGlobalDispatcher, ProxyAgent, request } = await import("undici");
27+
const { Agent, ProxyAgent, request } = await import("undici");
2828

2929
let dispatcher: Dispatcher;
3030
if (process.env.http_proxy !== undefined && shouldUseProxy(url)) {
3131
dispatcher = new ProxyAgent(process.env.http_proxy);
3232
} else {
33-
dispatcher = getGlobalDispatcher();
33+
dispatcher = new Agent();
3434
}
3535

3636
const hardhatVersion = getHardhatVersion();

packages/hardhat-verify/src/internal/undici.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,25 @@ export async function sendPostRequest(
2828
});
2929
}
3030

31+
let mockDispatcher: Undici.Dispatcher | undefined;
32+
3133
function getDispatcher(): Undici.Dispatcher {
32-
const { ProxyAgent, getGlobalDispatcher } =
33-
require("undici") as typeof Undici;
34+
if (mockDispatcher !== undefined) {
35+
return mockDispatcher;
36+
}
37+
38+
const { ProxyAgent, Agent } = require("undici") as typeof Undici;
3439
if (process.env.http_proxy !== undefined) {
3540
return new ProxyAgent(process.env.http_proxy);
3641
}
3742

38-
return getGlobalDispatcher();
43+
return new Agent();
44+
}
45+
46+
export function setMockDispatcher(
47+
dispatcher: Undici.Dispatcher | undefined
48+
): void {
49+
mockDispatcher = dispatcher;
3950
}
4051

4152
export function isSuccessStatusCode(statusCode: number): boolean {

packages/hardhat-verify/test/integration/mocks/etherscan.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import {
2-
Dispatcher,
3-
getGlobalDispatcher,
4-
MockAgent,
5-
setGlobalDispatcher,
6-
} from "undici";
1+
import { MockAgent } from "undici";
2+
import { setMockDispatcher } from "../../../src/internal/undici";
73

84
const mockAgent = new MockAgent({
95
keepAliveTimeout: 10,
@@ -13,17 +9,15 @@ const mockAgent = new MockAgent({
139
const client = mockAgent.get("https://api-hardhat.etherscan.io");
1410

1511
export const mockEnvironment = () => {
16-
let globalDispatcher: Dispatcher;
1712
// enable network connections for everything but etherscan API
1813
mockAgent.enableNetConnect(/^(?!https:\/\/api-hardhat\.etherscan\.io)/);
1914

2015
before(() => {
21-
globalDispatcher = getGlobalDispatcher();
22-
setGlobalDispatcher(mockAgent);
16+
setMockDispatcher(mockAgent);
2317
});
2418

2519
after(() => {
26-
setGlobalDispatcher(globalDispatcher);
20+
setMockDispatcher(undefined);
2721
});
2822
};
2923

packages/hardhat-verify/test/integration/mocks/sourcify.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import {
2-
Dispatcher,
3-
getGlobalDispatcher,
4-
MockAgent,
5-
setGlobalDispatcher,
6-
} from "undici";
1+
import { MockAgent } from "undici";
2+
import { setMockDispatcher } from "../../../src/internal/undici";
73

84
const mockAgent = new MockAgent({
95
keepAliveTimeout: 10,
@@ -13,17 +9,15 @@ const mockAgent = new MockAgent({
139
const client = mockAgent.get("https://sourcify.dev");
1410

1511
export const mockEnvironmentSourcify = () => {
16-
let globalDispatcher: Dispatcher;
1712
// enable network connections for everything but etherscan API
1813
mockAgent.enableNetConnect(/^(?!https:\/\/sourcify\.dev)/);
1914

2015
before(() => {
21-
globalDispatcher = getGlobalDispatcher();
22-
setGlobalDispatcher(mockAgent);
16+
setMockDispatcher(mockAgent);
2317
});
2418

2519
after(() => {
26-
setGlobalDispatcher(globalDispatcher);
20+
setMockDispatcher(undefined);
2721
});
2822
};
2923

0 commit comments

Comments
 (0)