Skip to content

Commit b322db7

Browse files
authored
fix: node-fetch hanging issue when fetching checksum after fetching binary (#2804)
1 parent 2fe2da6 commit b322db7

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

scripts/postinstall.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -98,45 +98,53 @@ async function main() {
9898
throw errUnsupported;
9999
}
100100

101+
// Read from package.json and prepare for the installation.
101102
const pkg = await readPackageJson();
102103
if (platform === "windows") {
103104
// Update bin path in package.json
104105
pkg.bin[pkg.name] += ".exe";
105106
}
106107

108+
// Prepare the installation path by creating the directory if it doesn't exist.
107109
const binPath = pkg.bin[pkg.name];
108110
const binDir = path.dirname(binPath);
109111
await fs.promises.mkdir(binDir, { recursive: true });
110112

111-
// First we will Un-GZip, then we will untar.
112-
const ungz = zlib.createGunzip();
113-
const binName = path.basename(binPath);
114-
const untar = extract({ cwd: binDir }, [binName]);
115-
116-
const url = getDownloadUrl(pkg);
117-
console.info("Downloading", url);
113+
// Create the agent that will be used for all the fetch requests later.
118114
const proxyUrl =
119115
process.env.npm_config_https_proxy ||
120116
process.env.npm_config_http_proxy ||
121117
process.env.npm_config_proxy;
122-
123118
// Keeps the TCP connection alive when sending multiple requests
124119
// Ref: https://github.com/node-fetch/node-fetch/issues/1735
125120
const agent = proxyUrl
126121
? new HttpsProxyAgent(proxyUrl, { keepAlive: true })
127122
: new Agent({ keepAlive: true });
128-
const resp = await fetch(url, { agent });
129123

124+
// First, fetch the checksum map.
125+
const checksumMap = await fetchAndParseCheckSumFile(pkg, agent);
126+
127+
// Then, download the binary.
128+
const url = getDownloadUrl(pkg);
129+
console.info("Downloading", url);
130+
const resp = await fetch(url, { agent });
130131
const hash = createHash("sha256");
131132
const pkgNameWithPlatform = `${pkg.name}_${platform}_${arch}.tar.gz`;
132-
const checksumMap = await fetchAndParseCheckSumFile(pkg, agent);
133133

134+
// Then, decompress the binary -- we will first Un-GZip, then we will untar.
135+
const ungz = zlib.createGunzip();
136+
const binName = path.basename(binPath);
137+
const untar = extract({ cwd: binDir }, [binName]);
138+
139+
// Update the hash with the binary data as it's being downloaded.
134140
resp.body
135141
.on("data", (chunk) => {
136142
hash.update(chunk);
137143
})
144+
// Pipe the data to the ungz stream.
138145
.pipe(ungz);
139146

147+
// After the ungz stream has ended, verify the checksum.
140148
ungz
141149
.on("end", () => {
142150
const expectedChecksum = checksumMap?.[pkgNameWithPlatform];
@@ -151,8 +159,10 @@ async function main() {
151159
}
152160
console.info("Checksum verified.");
153161
})
162+
// Pipe the data to the untar stream.
154163
.pipe(untar);
155164

165+
// Wait for the untar stream to finish.
156166
await new Promise((resolve, reject) => {
157167
untar.on("error", reject);
158168
untar.on("end", () => resolve());

0 commit comments

Comments
 (0)