Skip to content

Commit

Permalink
Merge pull request #4075 from NomicFoundation/compiler-download-issue
Browse files Browse the repository at this point in the history
Consume downloads instead of piping them
  • Loading branch information
fvictorio committed Jun 27, 2023
2 parents 8bec18b + 68cf2a2 commit 176f268
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-parents-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Fixed an issue related to compiler downloads in node v18.16.x
7 changes: 2 additions & 5 deletions packages/hardhat-core/src/internal/util/download.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { Dispatcher } from "undici";

import fs from "fs";
import fsExtra from "fs-extra";
import path from "path";
import util from "util";

import { getHardhatVersion } from "./packageInfo";
import { shouldUseProxy } from "./proxy";
Expand All @@ -26,9 +24,7 @@ export async function download(
timeoutMillis = 10000,
extraHeaders: { [name: string]: string } = {}
) {
const { pipeline } = await import("stream");
const { getGlobalDispatcher, ProxyAgent, request } = await import("undici");
const streamPipeline = util.promisify(pipeline);

let dispatcher: Dispatcher;
if (process.env.http_proxy !== undefined && shouldUseProxy(url)) {
Expand All @@ -52,10 +48,11 @@ export async function download(
});

if (response.statusCode >= 200 && response.statusCode <= 299) {
const responseBody = await response.body.arrayBuffer();
const tmpFilePath = resolveTempFileName(filePath);
await fsExtra.ensureDir(path.dirname(filePath));

await streamPipeline(response.body, fs.createWriteStream(tmpFilePath));
await fsExtra.writeFile(tmpFilePath, responseBody);
return fsExtra.move(tmpFilePath, filePath, { overwrite: true });
}
// undici's response bodies must always be consumed to prevent leaks
Expand Down

0 comments on commit 176f268

Please sign in to comment.