|
| 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 |
|
| 5 | +// 5 minutes buffer time the refresh the credential before it really expires |
| 6 | +const CREDENTIAL_EXPIRE_WINDOW = 300000; |
| 7 | + |
4 | 8 | export interface AwsAuthInputConfig {
|
5 | 9 | /**
|
6 | 10 | * The credentials used to sign requests.
|
@@ -42,9 +46,13 @@ export interface AwsAuthResolvedConfig {
|
42 | 46 | signingEscapePath: boolean;
|
43 | 47 | systemClockOffset: number;
|
44 | 48 | }
|
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); |
| 49 | + |
| 50 | +export const resolveAwsAuthConfig = <T>( |
| 51 | + input: T & AwsAuthInputConfig & PreviouslyResolved |
| 52 | +): T & AwsAuthResolvedConfig => { |
| 53 | + const normalizedCreds = input.credentials |
| 54 | + ? normalizeCredentialProvider(input.credentials) |
| 55 | + : input.credentialDefaultProvider(input as any); |
48 | 56 | const { signingEscapePath = true, systemClockOffset = input.systemClockOffset || 0, sha256 } = input;
|
49 | 57 | let signer: Provider<RequestSigner>;
|
50 | 58 | if (input.signer) {
|
@@ -81,12 +89,25 @@ export function resolveAwsAuthConfig<T>(input: T & AwsAuthInputConfig & Previous
|
81 | 89 | credentials: normalizedCreds,
|
82 | 90 | signer,
|
83 | 91 | };
|
84 |
| -} |
| 92 | +}; |
85 | 93 |
|
86 |
| -function normalizeProvider<T>(input: T | Provider<T>): Provider<T> { |
| 94 | +const normalizeProvider = <T>(input: T | Provider<T>): Provider<T> => { |
87 | 95 | if (typeof input === "object") {
|
88 | 96 | const promisified = Promise.resolve(input);
|
89 | 97 | return () => promisified;
|
90 | 98 | }
|
91 | 99 | return input as Provider<T>;
|
92 |
| -} |
| 100 | +}; |
| 101 | + |
| 102 | +const normalizeCredentialProvider = (credentials: Credentials | Provider<Credentials>): Provider<Credentials> => { |
| 103 | + if (typeof credentials === "function") { |
| 104 | + return memoize( |
| 105 | + credentials, |
| 106 | + (credentials) => |
| 107 | + credentials.expiration !== undefined && |
| 108 | + credentials.expiration.getTime() - Date.now() < CREDENTIAL_EXPIRE_WINDOW, |
| 109 | + (credentials) => credentials.expiration !== undefined |
| 110 | + ); |
| 111 | + } |
| 112 | + return normalizeProvider(credentials); |
| 113 | +}; |
0 commit comments