@@ -11,7 +11,7 @@ import {
11
11
12
12
import { compressStream } from "./compressStream" ;
13
13
import { compressString } from "./compressString" ;
14
- import { CompressionResolvedConfig } from "./configurations" ;
14
+ import { CompressionPreviouslyResolved , CompressionResolvedConfig } from "./configurations" ;
15
15
import { CLIENT_SUPPORTED_ALGORITHMS , CompressionAlgorithm } from "./constants" ;
16
16
import { isStreaming } from "./isStreaming" ;
17
17
@@ -34,67 +34,75 @@ export interface CompressionMiddlewareConfig {
34
34
/**
35
35
* @internal
36
36
*/
37
- export const compressionMiddleware =
38
- ( config : CompressionResolvedConfig , middlewareConfig : CompressionMiddlewareConfig ) : BuildMiddleware < any , any > =>
39
- < Output extends MetadataBearer > ( next : BuildHandler < any , Output > ) : BuildHandler < any , Output > =>
40
- async ( args : BuildHandlerArguments < any > ) : Promise < BuildHandlerOutput < Output > > => {
41
- if ( ! HttpRequest . isInstance ( args . request ) || config . disableRequestCompression ) {
42
- return next ( args ) ;
43
- }
37
+ export const compressionMiddleware = (
38
+ config : CompressionResolvedConfig & CompressionPreviouslyResolved ,
39
+ middlewareConfig : CompressionMiddlewareConfig
40
+ ) : BuildMiddleware < any , any > => < Output extends MetadataBearer > (
41
+ next : BuildHandler < any , Output >
42
+ ) : BuildHandler < any , Output > => async ( args : BuildHandlerArguments < any > ) : Promise < BuildHandlerOutput < Output > > => {
43
+ if ( ! HttpRequest . isInstance ( args . request ) ) {
44
+ return next ( args ) ;
45
+ }
46
+
47
+ const disableRequestCompression = await config . disableRequestCompression ( ) ;
48
+ if ( disableRequestCompression ) {
49
+ return next ( args ) ;
50
+ }
44
51
45
- const { request } = args ;
46
- const { body, headers } = request ;
47
- const { encodings, streamRequiresLength } = middlewareConfig ;
52
+ const { request } = args ;
53
+ const { body, headers } = request ;
54
+ const { encodings, streamRequiresLength } = middlewareConfig ;
48
55
49
- let updatedBody = body ;
50
- let updatedHeaders = headers ;
56
+ let updatedBody = body ;
57
+ let updatedHeaders = headers ;
51
58
52
- for ( const algorithm of encodings ) {
53
- if ( CLIENT_SUPPORTED_ALGORITHMS . includes ( algorithm as CompressionAlgorithm ) ) {
54
- let isRequestCompressed = false ;
55
- if ( isStreaming ( body ) ) {
56
- if ( ! streamRequiresLength ) {
57
- updatedBody = await compressStream ( body ) ;
58
- isRequestCompressed = true ;
59
- } else {
60
- // Invalid case. We should never get here.
61
- throw new Error ( "Compression is not supported for streaming blobs that require a length." ) ;
62
- }
59
+ for ( const algorithm of encodings ) {
60
+ if ( CLIENT_SUPPORTED_ALGORITHMS . includes ( algorithm as CompressionAlgorithm ) ) {
61
+ let isRequestCompressed = false ;
62
+ if ( isStreaming ( body ) ) {
63
+ if ( ! streamRequiresLength ) {
64
+ updatedBody = await compressStream ( body ) ;
65
+ isRequestCompressed = true ;
63
66
} else {
64
- const bodyLength = config . bodyLengthChecker ( body ) ;
65
- if ( bodyLength && bodyLength >= config . requestMinCompressionSizeBytes ) {
66
- updatedBody = await compressString ( body ) ;
67
- isRequestCompressed = true ;
68
- }
67
+ // Invalid case. We should never get here.
68
+ throw new Error ( "Compression is not supported for streaming blobs that require a length." ) ;
69
69
}
70
+ } else {
71
+ const bodyLength = config . bodyLengthChecker ( body ) ;
72
+ const requestMinCompressionSizeBytes = await config . requestMinCompressionSizeBytes ( ) ;
73
+ if ( bodyLength && bodyLength >= requestMinCompressionSizeBytes ) {
74
+ updatedBody = await compressString ( body ) ;
75
+ isRequestCompressed = true ;
76
+ }
77
+ }
70
78
71
- if ( isRequestCompressed ) {
72
- // Either append to the header if it already exists, else set it
73
- if ( headers [ "Content-Encoding" ] ) {
74
- updatedHeaders = {
75
- ...headers ,
76
- "Content-Encoding" : `${ headers [ "Content-Encoding" ] } ,${ algorithm } ` ,
77
- } ;
78
- } else {
79
- updatedHeaders = { ...headers , "Content-Encoding" : algorithm } ;
80
- }
81
-
82
- // We've matched on one supported algorithm in the
83
- // priority-ordered list, so we're finished.
84
- break ;
79
+ if ( isRequestCompressed ) {
80
+ // Either append to the header if it already exists, else set it
81
+ if ( headers [ "Content-Encoding" ] ) {
82
+ updatedHeaders = {
83
+ ...headers ,
84
+ "Content-Encoding" : `${ headers [ "Content-Encoding" ] } ,${ algorithm } ` ,
85
+ } ;
86
+ } else {
87
+ updatedHeaders = { ...headers , "Content-Encoding" : algorithm } ;
85
88
}
89
+
90
+ // We've matched on one supported algorithm in the
91
+ // priority-ordered list, so we're finished.
92
+ break ;
86
93
}
87
94
}
95
+ }
88
96
89
- return next ( {
90
- ...args ,
91
- request : {
92
- ...request ,
93
- body : updatedBody ,
94
- headers : updatedHeaders ,
95
- } ,
96
- } ) ;
97
- } ;
97
+ return next ( {
98
+ ...args ,
99
+ request : {
100
+ ...request ,
101
+ body : updatedBody ,
102
+ headers : updatedHeaders ,
103
+ } ,
104
+ } ) ;
105
+ } ;
98
106
99
107
export const compressionMiddlewareOptions : BuildHandlerOptions & AbsoluteLocation = {
100
108
name : "compressionMiddleware" ,
0 commit comments