From 2ddb04f4dd11b5b42ef05d389a08f9a3a5db8589 Mon Sep 17 00:00:00 2001 From: Han Qiao Date: Sat, 30 Dec 2023 01:28:12 +0800 Subject: [PATCH] fix: keep alive tcp connection when downloading binary (#1791) --- scripts/postinstall.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/postinstall.js b/scripts/postinstall.js index c3d64e00b..8c010f71b 100755 --- a/scripts/postinstall.js +++ b/scripts/postinstall.js @@ -8,6 +8,7 @@ import binLinks from "bin-links"; import { createHash } from "crypto"; import fs from "fs"; import fetch from "node-fetch"; +import { Agent } from "https"; import { HttpsProxyAgent } from "https-proxy-agent"; import path from "path"; import tar from "tar"; @@ -128,7 +129,12 @@ async function main() { process.env.npm_config_https_proxy || process.env.npm_config_http_proxy || process.env.npm_config_proxy; - const agent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined; + + // Keeps the TCP connection alive when sending multiple requests + // Ref: https://github.com/node-fetch/node-fetch/issues/1735 + const agent = proxyUrl + ? new HttpsProxyAgent(proxyUrl, { keepAlive: true }) + : new Agent({ keepAlive: true }); const resp = await fetch(url, { agent }); const hash = createHash("sha256");