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

fix: deal with node v19 breaking change (keep-alive enabled on global agent) #478

Merged
merged 3 commits into from
Jun 7, 2024
Merged
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
30 changes: 28 additions & 2 deletions src/core/h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,20 @@ const getAgent = (ctx, protocol) => {
return h1.httpsAgent;
}
// use default (global) agent
return undefined;
/* c8 ignore next 13 */
/* => code coverage depends on the node version */
if (https.globalAgent.keepAlive) /* node >= 19 */ {
// As of Node.js v19 the global agent has keep-alive enabled by default:
// https://nodejs.org/api/http.html#class-httpagent
// https://github.com/nodejs/node/issues/37184
// https://github.com/nodejs/node/pull/43522/files#diff-494d2deee304c672124ecd82d090283595fd3d8c5a80a1825d972a2d229e4944L334-R334
// In order to guarantee consistent behavior across node versions we
// always create a new agent with keep-alive disabled on Node.js v19+.
h1.httpsAgent = new https.Agent({ keepAlive: false });
return h1.httpsAgent;
} else /* node <= 18 */ {
return undefined;
}
} else {
// plain http
if (h1.httpAgent) {
Expand All @@ -47,7 +60,20 @@ const getAgent = (ctx, protocol) => {
return h1.httpAgent;
}
// use default (global) agent
return undefined;
/* c8 ignore next 13 */
/* => code coverage depends on the node version */
if (http.globalAgent.keepAlive) /* node >= 19 */ {
// As of Node.js v19 the global agent has keep-alive enabled by default:
// https://nodejs.org/api/http.html#class-httpagent
// https://github.com/nodejs/node/issues/37184
// https://github.com/nodejs/node/pull/43522/files#diff-494d2deee304c672124ecd82d090283595fd3d8c5a80a1825d972a2d229e4944L334-R334
// In order to guarantee consistent behavior across node versions we
// always create a new agent with keep-alive disabled on Node.js v19+.
h1.httpAgent = new http.Agent({ keepAlive: false });
return h1.httpAgent;
} else /* node <= 18 */ {
return undefined;
}
}
};

Expand Down
Loading