Skip to content

Commit

Permalink
Consume downloads instead of piping them
Browse files Browse the repository at this point in the history
This is a workaround for an issue related to node v18.16.0, the stream module and undici. See nodejs/undici#2173 for the details.
  • Loading branch information
fvictorio committed Jun 27, 2023
1 parent 8bec18b commit 45025fb
Showing 1 changed file with 2 additions and 5 deletions.
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 45025fb

Please sign in to comment.