1
+ import { toEndpointV1 } from "@aws-sdk/middleware-endpoint" ;
1
2
import { HttpRequest } from "@aws-sdk/protocol-http" ;
2
3
import { SignatureV4 } from "@aws-sdk/signature-v4" ;
3
4
import {
4
5
Credentials ,
5
6
Endpoint ,
7
+ HandlerExecutionContext ,
6
8
HashConstructor ,
7
9
InitializeHandler ,
8
10
InitializeHandlerArguments ,
9
- InitializeHandlerOptions ,
10
11
InitializeHandlerOutput ,
11
12
InitializeMiddleware ,
12
13
MemoizedProvider ,
13
14
MetadataBearer ,
14
15
Pluggable ,
15
16
Provider ,
17
+ RelativeMiddlewareOptions ,
18
+ SerializeHandlerOptions ,
16
19
} from "@aws-sdk/types" ;
17
20
import { formatUrl } from "@aws-sdk/util-format-url" ;
18
21
@@ -30,7 +33,7 @@ const version = "2014-10-31";
30
33
31
34
interface PreviouslyResolved {
32
35
credentials : MemoizedProvider < Credentials > ;
33
- endpoint : Provider < Endpoint > ;
36
+ endpoint ? : Provider < Endpoint > ;
34
37
region : Provider < string > ;
35
38
sha256 : HashConstructor ;
36
39
signingEscapePath : boolean ;
@@ -41,7 +44,10 @@ interface PreviouslyResolved {
41
44
* The presigned URL is generated by sigV4
42
45
*/
43
46
export function crossRegionPresignedUrlMiddleware ( options : PreviouslyResolved ) : InitializeMiddleware < any , any > {
44
- return < Output extends MetadataBearer > ( next : InitializeHandler < any , Output > ) : InitializeHandler < any , Output > =>
47
+ return < Output extends MetadataBearer > (
48
+ next : InitializeHandler < any , Output > ,
49
+ context : HandlerExecutionContext
50
+ ) : InitializeHandler < any , Output > =>
45
51
async ( args : InitializeHandlerArguments < any > ) : Promise < InitializeHandlerOutput < Output > > => {
46
52
const { input } = args ;
47
53
const region = await options . region ( ) ;
@@ -54,7 +60,14 @@ export function crossRegionPresignedUrlMiddleware(options: PreviouslyResolved):
54
60
const command = sourceIdToCommandKeyMap [ sourceIdKey ] ;
55
61
if ( ! input . PreSignedUrl && isARN ( input [ sourceIdKey ] ) && region !== getEndpointFromARN ( input [ sourceIdKey ] ) ) {
56
62
const sourceRegion = getEndpointFromARN ( input [ sourceIdKey ] ) ;
57
- const resolvedEndpoint = await options . endpoint ( ) ;
63
+
64
+ let resolvedEndpoint : Endpoint ;
65
+ if ( typeof options . endpoint === "function" ) {
66
+ resolvedEndpoint = await options . endpoint ( ) ;
67
+ } else {
68
+ resolvedEndpoint = toEndpointV1 ( context . endpointV2 ! ) ;
69
+ }
70
+
58
71
resolvedEndpoint . hostname = `rds.${ sourceRegion } .amazonaws.com` ;
59
72
const request = new HttpRequest ( {
60
73
...resolvedEndpoint ,
@@ -92,16 +105,18 @@ export function crossRegionPresignedUrlMiddleware(options: PreviouslyResolved):
92
105
} ;
93
106
}
94
107
95
- export const crossRegionPresignedUrlMiddlewareOptions : InitializeHandlerOptions = {
96
- step : "initialize " ,
108
+ export const crossRegionPresignedUrlMiddlewareOptions : SerializeHandlerOptions & RelativeMiddlewareOptions = {
109
+ step : "serialize " ,
97
110
tags : [ "CROSS_REGION_PRESIGNED_URL" ] ,
98
111
name : "crossRegionPresignedUrlMiddleware" ,
99
112
override : true ,
113
+ relation : "after" ,
114
+ toMiddleware : "endpointV2Middleware" ,
100
115
} ;
101
116
102
117
export const getCrossRegionPresignedUrlPlugin = ( config : PreviouslyResolved ) : Pluggable < any , any > => ( {
103
118
applyToStack : ( clientStack ) => {
104
- clientStack . add ( crossRegionPresignedUrlMiddleware ( config ) , crossRegionPresignedUrlMiddlewareOptions ) ;
119
+ clientStack . addRelativeTo ( crossRegionPresignedUrlMiddleware ( config ) , crossRegionPresignedUrlMiddlewareOptions ) ;
105
120
} ,
106
121
} ) ;
107
122
0 commit comments