From 74c63eb358e94ea4df1be57786c41bf8b75f1a2d Mon Sep 17 00:00:00 2001 From: Stefan Guggisberg Date: Wed, 10 Apr 2024 14:03:02 +0200 Subject: [PATCH] fix: disable h1 keep-alive by default --- src/fetch/index.js | 25 +++++++++++++++++-------- src/index.d.ts | 10 ++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/fetch/index.js b/src/fetch/index.js index 614071a..e5878b3 100644 --- a/src/fetch/index.js +++ b/src/fetch/index.js @@ -442,20 +442,23 @@ class FetchContext { noCache: (options = {}) => new FetchContext({ ...options, maxCacheSize: 0 }).api(), /** - * Convenience function which creates a new context with enforced HTTP/1.1 protocol, - * the equivalent of `context({ alpnProtocols: [ALPN_HTTP1_1] })`. + * Convenience function which creates a new context with enforced HTTP/1.1 protocol + * and disabled persistent connections (keep-alive), the equivalent of + * `context({ alpnProtocols: [ALPN_HTTP1_1], h1: { keepAlive: false } })`. * * The optional `options` parameter allows to specify further options. * * @param {Object} [options={}] */ h1: (options = {}) => new FetchContext({ - ...options, alpnProtocols: [this.context.ALPN_HTTP1_1], + ...options, + alpnProtocols: [this.context.ALPN_HTTP1_1], + h1: { keepAlive: false }, }).api(), /** * Convenience function which creates a new context with enforced HTTP/1.1 protocol - * and persistent connections (keep-alive), the equivalent of + * with persistent connections (keep-alive), the equivalent of * `context({ alpnProtocols: [ALPN_HTTP1_1], h1: { keepAlive: true } })`. * * The optional `options` parameter allows to specify further options. @@ -463,19 +466,25 @@ class FetchContext { * @param {Object} [options={}] */ keepAlive: (options = {}) => new FetchContext({ - ...options, alpnProtocols: [this.context.ALPN_HTTP1_1], h1: { keepAlive: true }, + ...options, + alpnProtocols: [this.context.ALPN_HTTP1_1], + h1: { keepAlive: true }, }).api(), /** - * Convenience function which creates a new context with disabled caching - * and enforced HTTP/1.1 protocol, a combination of `h1()` and `noCache()`. + * Convenience function which creates a new context with disabled caching, + * enforced HTTP/1.1 protocol and disabled persistent connections (keep-alive), + * a combination of `h1()` and `noCache()`. * * The optional `options` parameter allows to specify further options. * * @param {Object} [options={}] */ h1NoCache: (options = {}) => new FetchContext({ - ...options, maxCacheSize: 0, alpnProtocols: [this.context.ALPN_HTTP1_1], + ...options, + maxCacheSize: 0, + alpnProtocols: [this.context.ALPN_HTTP1_1], + h1: { keepAlive: false }, }).api(), /** diff --git a/src/index.d.ts b/src/index.d.ts index 68007ab..11e83d8 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -36,8 +36,9 @@ export declare function context(options?: ContextOptions): FetchAPI; export declare function noCache(options?: ContextOptions): FetchAPI; /** - * Convenience function which creates a new context with enforced HTTP/1.1 protocol, - * the equivalent of `context({ alpnProtocols: [ALPN_HTTP1_1] })`. + * Convenience function which creates a new context with enforced HTTP/1.1 protocol + * and disabled persistent connections (keep-alive), the equivalent of + * `context({ alpnProtocols: [ALPN_HTTP1_1], h1: { keepAlive: false } })`. * * The optional `options` parameter allows to specify further options. * @@ -57,8 +58,9 @@ export declare function noCache(options?: ContextOptions): FetchAPI; export declare function keepAlive(options?: ContextOptions): FetchAPI; /** - * Convenience function which creates a new context with disabled caching - * and enforced HTTP/1.1 protocol, a combination of `h1()` and `noCache()`. + * Convenience function which creates a new context with disabled caching, + * and enforced HTTP/1.1 protocol with disabled persistent connections (keep-alive), + * a combination of `h1()` and `noCache()`. * * The optional `options` parameter allows to specify further options. *