Skip to content
Open
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
9 changes: 7 additions & 2 deletions lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ async function download (gyp, url) {
}
}

// undici 8 negotiates HTTP/2 by default. Under parallel installs on Windows,
// HTTP/2 downloads from nodejs.org are occasionally cut short ("terminated"),
// which RetryAgent does not retry. Stick to HTTP/1.1 as before undici 8.
const agentOpts = { allowH2: false }

async function createDispatcher (gyp) {
const env = process.env
const hasProxyEnv = env.http_proxy || env.HTTP_PROXY || env.https_proxy || env.HTTPS_PROXY
if (!gyp.opts.proxy && !gyp.opts.cafile && !hasProxyEnv) {
return new RetryAgent(new Agent(), { maxRetries: 3 })
return new RetryAgent(new Agent(agentOpts), { maxRetries: 3 })
}

const opts = {}
const opts = { ...agentOpts }
if (gyp.opts.cafile) {
const ca = await readCAFile(gyp.opts.cafile)
// EnvHttpProxyAgent forwards opts to both its internal Agent (direct) and
Expand Down
22 changes: 16 additions & 6 deletions test/test-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const assert = require('assert')
const fs = require('fs/promises')
const path = require('path')
const http = require('http')
const https = require('https')
const http2 = require('http2')
const net = require('net')
const install = require('../lib/install')
const { download, readCAFile } = require('../lib/download')
Expand Down Expand Up @@ -44,9 +44,12 @@ describe('download', function () {

assert.strictEqual(ca.length, 1)

const options = { ca, cert, key }
const server = https.createServer(options, (req, res) => {
// Offers HTTP/2 so the test catches downloads negotiating it
const options = { ca, cert, key, allowHTTP1: true }
let httpVersion
const server = http2.createSecureServer(options, (req, res) => {
assert.strictEqual(req.headers['user-agent'], `node-gyp v42 (node ${process.version})`)
httpVersion = req.httpVersion
res.end('ok')
})

Expand All @@ -67,6 +70,7 @@ describe('download', function () {
const url = `https://${host}:${port}`
const res = await download(gyp, url)
assert.strictEqual(await res.text(), 'ok')
assert.strictEqual(httpVersion, '1.1')
})

it('download over http with proxy', async function () {
Expand Down Expand Up @@ -134,11 +138,16 @@ describe('download', function () {
const cafile = path.join(__dirname, 'fixtures/ca-proxy.crt')
await fs.writeFile(cafile, certs['ca.crt'], 'utf8')

const server = https.createServer({
let httpVersion
const server = http2.createSecureServer({
ca: await readCAFile(cafile),
cert: certs['server.crt'],
key: certs['server.key']
}, (_, res) => res.end('ok'))
key: certs['server.key'],
allowHTTP1: true
}, (req, res) => {
httpVersion = req.httpVersion
res.end('ok')
})

let proxyUsed = false
const pserver = http.createServer()
Expand Down Expand Up @@ -176,6 +185,7 @@ describe('download', function () {
const res = await download(gyp, `https://${host}:${port}`)
assert.strictEqual(await res.text(), 'ok')
assert.strictEqual(proxyUsed, true)
assert.strictEqual(httpVersion, '1.1')
})

it('download over http with noproxy', async function () {
Expand Down
Loading