Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consume downloads instead of piping them #4075

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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