@@ -15,81 +15,76 @@ import { bucketHostname } from "./bucketHostname";
1515import { getPseudoRegion } from "./bucketHostnameUtils" ;
1616import { BucketEndpointResolvedConfig } from "./configurations" ;
1717
18- export const bucketEndpointMiddleware =
19- ( options : BucketEndpointResolvedConfig ) : BuildMiddleware < any , any > =>
20- < Output extends MetadataBearer > (
21- next : BuildHandler < any , Output > ,
22- context : HandlerExecutionContext
23- ) : BuildHandler < any , Output > =>
24- async ( args : BuildHandlerArguments < any > ) : Promise < BuildHandlerOutput < Output > > => {
25- const { Bucket : bucketName } = args . input as { Bucket : string } ;
26- let replaceBucketInPath = options . bucketEndpoint ;
27- const request = args . request ;
28- if ( HttpRequest . isInstance ( request ) ) {
29- if ( options . bucketEndpoint ) {
30- request . hostname = bucketName ;
31- } else if ( validateArn ( bucketName ) ) {
32- const bucketArn = parseArn ( bucketName ) ;
33- const clientRegion = getPseudoRegion ( await options . region ( ) ) ;
34- const { partition, signingRegion = clientRegion } = ( await options . regionInfoProvider ( clientRegion ) ) || { } ;
35- const useArnRegion = await options . useArnRegion ( ) ;
36- const {
37- hostname,
38- bucketEndpoint,
39- signingRegion : modifiedSigningRegion ,
40- signingService,
41- } = bucketHostname ( {
42- bucketName : bucketArn ,
43- baseHostname : request . hostname ,
44- accelerateEndpoint : options . useAccelerateEndpoint ,
45- dualstackEndpoint : options . useDualstackEndpoint ,
46- pathStyleEndpoint : options . forcePathStyle ,
47- tlsCompatible : request . protocol === "https:" ,
48- useArnRegion,
49- clientPartition : partition ,
50- clientSigningRegion : signingRegion ,
51- clientRegion : clientRegion ,
52- isCustomEndpoint : options . isCustomEndpoint ,
53- } ) ;
18+ export const bucketEndpointMiddleware = ( options : BucketEndpointResolvedConfig ) : BuildMiddleware < any , any > => <
19+ Output extends MetadataBearer
20+ > (
21+ next : BuildHandler < any , Output > ,
22+ context : HandlerExecutionContext
23+ ) : BuildHandler < any , Output > => async ( args : BuildHandlerArguments < any > ) : Promise < BuildHandlerOutput < Output > > => {
24+ const { Bucket : bucketName } = args . input as { Bucket : string } ;
25+ let replaceBucketInPath = options . bucketEndpoint ;
26+ const request = args . request ;
27+ if ( HttpRequest . isInstance ( request ) ) {
28+ if ( options . bucketEndpoint ) {
29+ request . hostname = bucketName ;
30+ } else if ( validateArn ( bucketName ) ) {
31+ const bucketArn = parseArn ( bucketName ) ;
32+ const clientRegion = getPseudoRegion ( await options . region ( ) ) ;
33+ const { partition, signingRegion = clientRegion } = ( await options . regionInfoProvider ( clientRegion ) ) || { } ;
34+ const useArnRegion = await options . useArnRegion ( ) ;
35+ const { hostname, bucketEndpoint, signingRegion : modifiedSigningRegion , signingService } = bucketHostname ( {
36+ bucketName : bucketArn ,
37+ baseHostname : request . hostname ,
38+ accelerateEndpoint : options . useAccelerateEndpoint ,
39+ dualstackEndpoint : options . useDualstackEndpoint ,
40+ pathStyleEndpoint : options . forcePathStyle ,
41+ tlsCompatible : request . protocol === "https:" ,
42+ useArnRegion,
43+ clientPartition : partition ,
44+ clientSigningRegion : signingRegion ,
45+ clientRegion : clientRegion ,
46+ isCustomEndpoint : options . isCustomEndpoint ,
47+ disableMultiregionAccessPoints : await options . disableMultiregionAccessPoints ( ) ,
48+ } ) ;
5449
55- // If the request needs to use a region or service name inferred from ARN that different from client region, we
56- // need to set them in the handler context so the signer will use them
57- if ( modifiedSigningRegion && modifiedSigningRegion !== signingRegion ) {
58- context [ "signing_region" ] = modifiedSigningRegion ;
59- }
60- if ( signingService && signingService !== "s3" ) {
61- context [ "signing_service" ] = signingService ;
62- }
50+ // If the request needs to use a region or service name inferred from ARN that different from client region, we
51+ // need to set them in the handler context so the signer will use them
52+ if ( modifiedSigningRegion && modifiedSigningRegion !== signingRegion ) {
53+ context [ "signing_region" ] = modifiedSigningRegion ;
54+ }
55+ if ( signingService && signingService !== "s3" ) {
56+ context [ "signing_service" ] = signingService ;
57+ }
6358
64- request . hostname = hostname ;
65- replaceBucketInPath = bucketEndpoint ;
66- } else {
67- const clientRegion = getPseudoRegion ( await options . region ( ) ) ;
68- const { hostname, bucketEndpoint } = bucketHostname ( {
69- bucketName,
70- clientRegion,
71- baseHostname : request . hostname ,
72- accelerateEndpoint : options . useAccelerateEndpoint ,
73- dualstackEndpoint : options . useDualstackEndpoint ,
74- pathStyleEndpoint : options . forcePathStyle ,
75- tlsCompatible : request . protocol === "https:" ,
76- isCustomEndpoint : options . isCustomEndpoint ,
77- } ) ;
59+ request . hostname = hostname ;
60+ replaceBucketInPath = bucketEndpoint ;
61+ } else {
62+ const clientRegion = getPseudoRegion ( await options . region ( ) ) ;
63+ const { hostname, bucketEndpoint } = bucketHostname ( {
64+ bucketName,
65+ clientRegion,
66+ baseHostname : request . hostname ,
67+ accelerateEndpoint : options . useAccelerateEndpoint ,
68+ dualstackEndpoint : options . useDualstackEndpoint ,
69+ pathStyleEndpoint : options . forcePathStyle ,
70+ tlsCompatible : request . protocol === "https:" ,
71+ isCustomEndpoint : options . isCustomEndpoint ,
72+ } ) ;
7873
79- request . hostname = hostname ;
80- replaceBucketInPath = bucketEndpoint ;
81- }
74+ request . hostname = hostname ;
75+ replaceBucketInPath = bucketEndpoint ;
76+ }
8277
83- if ( replaceBucketInPath ) {
84- request . path = request . path . replace ( / ^ ( \/ ) ? [ ^ \/ ] + / , "" ) ;
85- if ( request . path === "" ) {
86- request . path = "/" ;
87- }
78+ if ( replaceBucketInPath ) {
79+ request . path = request . path . replace ( / ^ ( \/ ) ? [ ^ \/ ] + / , "" ) ;
80+ if ( request . path === "" ) {
81+ request . path = "/" ;
8882 }
8983 }
84+ }
9085
91- return next ( { ...args , request } ) ;
92- } ;
86+ return next ( { ...args , request } ) ;
87+ } ;
9388
9489export const bucketEndpointMiddlewareOptions : RelativeMiddlewareOptions = {
9590 tags : [ "BUCKET_ENDPOINT" ] ,
0 commit comments