|
| 1 | +import { memoize } from "@aws-sdk/property-provider"; |
1 | 2 | import { SignatureV4 } from "@aws-sdk/signature-v4";
|
2 | 3 | import { Credentials, HashConstructor, Provider, RegionInfo, RegionInfoProvider, RequestSigner } from "@aws-sdk/types";
|
3 | 4 |
|
@@ -42,9 +43,13 @@ export interface AwsAuthResolvedConfig {
|
42 | 43 | signingEscapePath: boolean;
|
43 | 44 | systemClockOffset: number;
|
44 | 45 | }
|
45 |
| -export function resolveAwsAuthConfig<T>(input: T & AwsAuthInputConfig & PreviouslyResolved): T & AwsAuthResolvedConfig { |
46 |
| - const credentials = input.credentials || input.credentialDefaultProvider(input as any); |
47 |
| - const normalizedCreds = normalizeProvider(credentials); |
| 46 | + |
| 47 | +export const resolveAwsAuthConfig = <T>( |
| 48 | + input: T & AwsAuthInputConfig & PreviouslyResolved |
| 49 | +): T & AwsAuthResolvedConfig => { |
| 50 | + const normalizedCreds = input.credentials |
| 51 | + ? normalizeCredentialProvider(input.credentials) |
| 52 | + : input.credentialDefaultProvider(input as any); |
48 | 53 | const { signingEscapePath = true, systemClockOffset = input.systemClockOffset || 0, sha256 } = input;
|
49 | 54 | let signer: Provider<RequestSigner>;
|
50 | 55 | if (input.signer) {
|
@@ -81,12 +86,23 @@ export function resolveAwsAuthConfig<T>(input: T & AwsAuthInputConfig & Previous
|
81 | 86 | credentials: normalizedCreds,
|
82 | 87 | signer,
|
83 | 88 | };
|
84 |
| -} |
| 89 | +}; |
85 | 90 |
|
86 |
| -function normalizeProvider<T>(input: T | Provider<T>): Provider<T> { |
| 91 | +const normalizeProvider = <T>(input: T | Provider<T>): Provider<T> => { |
87 | 92 | if (typeof input === "object") {
|
88 | 93 | const promisified = Promise.resolve(input);
|
89 | 94 | return () => promisified;
|
90 | 95 | }
|
91 | 96 | return input as Provider<T>;
|
92 |
| -} |
| 97 | +}; |
| 98 | + |
| 99 | +const normalizeCredentialProvider = (credentials: Credentials | Provider<Credentials>): Provider<Credentials> => { |
| 100 | + if (typeof credentials === "function") { |
| 101 | + return memoize( |
| 102 | + credentials, |
| 103 | + (credentials) => credentials.expiration !== undefined && credentials.expiration.getTime() - Date.now() < 300000, |
| 104 | + (credentials) => credentials.expiration !== undefined |
| 105 | + ); |
| 106 | + } |
| 107 | + return normalizeProvider(credentials); |
| 108 | +}; |
0 commit comments