@@ -17,18 +17,10 @@ import { formatUrl } from "@aws-sdk/util-format-url";
17
17
18
18
const regARN = / a r n : [ \w + = / , . @ - ] + : [ \w + = / , . @ - ] + : ( [ \w + = / , . @ - ] * ) ? : [ 0 - 9 ] + : [ \w + = / , . @ - ] + ( : [ \w + = / , . @ - ] + ) ? ( : [ \w + = / , . @ - ] + ) ? / ;
19
19
20
- const sourceIds : string [ ] = [
21
- "SourceDBSnapshotIdentifier" ,
22
- "SourceDBInstanceIdentifier" ,
23
- "ReplicationSourceIdentifier" ,
24
- "SourceDBClusterSnapshotIdentifier" ,
25
- "SourceDBInstanceArn" ,
26
- ] ;
27
-
28
20
const sourceIdToCommandKeyMap : { [ key : string ] : string } = {
29
21
SourceDBSnapshotIdentifier : "CopyDBSnapshot" ,
30
22
SourceDBInstanceIdentifier : "CreateDBInstanceReadReplica" ,
31
- ReplicationSourceIdentifier : "CreateDBCluster" ,
23
+ ReplicationSourceIdentifier : "CreateDBCluster" , // This key is optional.
32
24
SourceDBClusterSnapshotIdentifier : "CopyDBClusterSnapshot" ,
33
25
SourceDBInstanceArn : "StartDBInstanceAutomatedBackupsReplication" ,
34
26
} ;
@@ -47,24 +39,20 @@ interface PreviouslyResolved {
47
39
* Config of the middleware to automatically add presigned URL to request.
48
40
* The presigned URL is generated by sigV4
49
41
*/
50
-
51
42
export function crossRegionPresignedUrlMiddleware ( options : PreviouslyResolved ) : InitializeMiddleware < any , any > {
52
43
return < Output extends MetadataBearer > ( next : InitializeHandler < any , Output > ) : InitializeHandler < any , Output > =>
53
44
async ( args : InitializeHandlerArguments < any > ) : Promise < InitializeHandlerOutput < Output > > => {
54
45
const { input } = args ;
55
46
const region = await options . region ( ) ;
56
- let command , sourceId ;
57
- for ( const id of sourceIds ) {
58
- if ( input . hasOwnProperty ( id ) ) {
59
- sourceId = id ;
60
- command = sourceIdToCommandKeyMap [ id ] ;
61
- }
62
- }
63
- if ( ! sourceId ) {
64
- throw new Error ( "Source identifier key not set" ) ;
65
- }
66
- if ( ! input . PreSignedUrl && isARN ( input [ sourceId ] ) && region !== getEndpointFromARN ( input [ sourceId ] ) ) {
67
- const sourceRegion = getEndpointFromARN ( input [ sourceId ] ) ;
47
+ const sourceIdKey = Object . keys ( sourceIdToCommandKeyMap ) . filter ( ( sourceKeyId ) =>
48
+ input . hasOwnProperty ( sourceKeyId )
49
+ ) [ 0 ] ;
50
+ // Source id is optional.
51
+ if ( ! sourceIdKey ) return next ( args ) ;
52
+
53
+ const command = sourceIdToCommandKeyMap [ sourceIdKey ] ;
54
+ if ( ! input . PreSignedUrl && isARN ( input [ sourceIdKey ] ) && region !== getEndpointFromARN ( input [ sourceIdKey ] ) ) {
55
+ const sourceRegion = getEndpointFromARN ( input [ sourceIdKey ] ) ;
68
56
const resolvedEndpoint = await options . endpoint ( ) ;
69
57
resolvedEndpoint . hostname = `rds.${ sourceRegion } .amazonaws.com` ;
70
58
const request = new HttpRequest ( {
@@ -78,7 +66,7 @@ export function crossRegionPresignedUrlMiddleware(options: PreviouslyResolved):
78
66
Version : version ,
79
67
KmsKeyId : input . KmsKeyId ,
80
68
DestinationRegion : region ,
81
- [ sourceId ] : input [ sourceId ] ,
69
+ [ sourceIdKey ] : input [ sourceIdKey ] ,
82
70
} ,
83
71
} ) ;
84
72
const signer = new SignatureV4 ( {
0 commit comments