@@ -49,6 +49,7 @@ function getSupportedAlgorithms(): string {
4949
5050/**
5151 * Supported compression algorithms.
52+ * @internal (vendored)
5253 * @remarks
5354 * - `gzip` is widely supported.
5455 * - `brotli` is supported by modern servers and can offer better compression rates, but may be slower.
@@ -166,60 +167,62 @@ function getContentEncodingValue(
166167export function compressRequest < C extends RequestCompressionAlgorithm = 'gzip' > (
167168 options ?: RequestCompressionMiddlewareOptions < C >
168169) : HttpMiddleware {
169- return ( middlewareOptions : HttpMiddlewareOptions ) => async requestConfig => {
170- const algorithm : RequestCompressionAlgorithm = options ?. algorithm ?? 'gzip' ;
171-
172- const needsCompression = checkIfNeedsCompression (
173- requestConfig . data ,
174- options
175- ) ;
170+ return ( middlewareOptions : HttpMiddlewareOptions ) =>
171+ async ( requestConfig : HttpRequestConfig ) => {
172+ const algorithm : RequestCompressionAlgorithm =
173+ options ?. algorithm ?? 'gzip' ;
174+
175+ const needsCompression = checkIfNeedsCompression (
176+ requestConfig . data ,
177+ options
178+ ) ;
176179
177- if ( needsCompression === false ) {
178- return middlewareOptions . fn ( requestConfig ) ;
179- }
180+ if ( needsCompression === false ) {
181+ return middlewareOptions . fn ( requestConfig ) ;
182+ }
180183
181- // Check existing Content-Encoding header - append to existing if present
182- const currentValue = pickValueIgnoreCase (
183- requestConfig . headers ,
184- 'content-encoding'
185- ) ;
186- const algorithmValue = getContentEncodingValue ( algorithm ) ;
187- const targetValue = currentValue
188- ? `${ currentValue } , ${ algorithmValue } `
189- : algorithmValue ;
190- requestConfig . headers = mergeIgnoreCase ( requestConfig . headers , {
191- 'content-encoding' : targetValue
192- } ) ;
193-
194- if ( needsCompression === 'header-only' ) {
195- return middlewareOptions . fn ( requestConfig ) ;
196- }
184+ // Check existing Content-Encoding header - append to existing if present
185+ const currentValue = pickValueIgnoreCase (
186+ requestConfig . headers ,
187+ 'content-encoding'
188+ ) ;
189+ const algorithmValue = getContentEncodingValue ( algorithm ) ;
190+ const targetValue = currentValue
191+ ? `${ currentValue } , ${ algorithmValue } `
192+ : algorithmValue ;
193+ requestConfig . headers = mergeIgnoreCase ( requestConfig . headers , {
194+ 'content-encoding' : targetValue
195+ } ) ;
196+
197+ if ( needsCompression === 'header-only' ) {
198+ return middlewareOptions . fn ( requestConfig ) ;
199+ }
197200
198- const compressor = compressors [ algorithm ] ;
199- if ( ! compressor ) {
200- if ( algorithm === 'zstd' ) {
201+ const compressor = compressors [ algorithm ] ;
202+ if ( ! compressor ) {
203+ if ( algorithm === 'zstd' ) {
204+ throw new Error (
205+ `'zstd' compression is not supported in this Node.js versions older than v22.15.0 to use 'zstd'. Supported algorithms for this version are: ${ getSupportedAlgorithms ( ) } .`
206+ ) ;
207+ }
201208 throw new Error (
202- `'zstd' compression is not supported in this Node.js versions older than v22.15.0 to use 'zstd '. Supported algorithms for this version are: ${ getSupportedAlgorithms ( ) } .`
209+ `Unsupported compression algorithm ' ${ algorithm } '. Supported algorithms are: ${ getSupportedAlgorithms ( ) } .`
203210 ) ;
204211 }
205- throw new Error (
206- `Unsupported compression algorithm '${ algorithm } '. Supported algorithms are: ${ getSupportedAlgorithms ( ) } .`
207- ) ;
208- }
209212
210- // TODO: (future) Consider streaming compression for large payloads
211- const compressed = await compressor (
212- requestConfig . data ,
213- options ?. compressOptions as any
214- ) . catch ( ( err : Error ) => {
215- throw new ErrorWithCause (
216- `Failed to compress request payload using '${ algorithm } '.` ,
217- err
218- ) ;
219- } ) ;
213+ // TODO: (future) Consider streaming compression for large payloads
214+ const compressed = await compressor (
215+ requestConfig . data ,
216+ options ?. compressOptions as any
217+ ) . catch ( ( err : Error ) => {
218+ throw new ErrorWithCause (
219+ `Failed to compress request payload using '${ algorithm } '.` ,
220+ err
221+ ) ;
222+ } ) ;
220223
221- requestConfig . data = compressed ;
224+ requestConfig . data = compressed ;
222225
223- return middlewareOptions . fn ( requestConfig ) ;
224- } ;
226+ return middlewareOptions . fn ( requestConfig ) ;
227+ } ;
225228}
0 commit comments