From 0708f875ede37d21cc8315b0a600faf1e0a4abd7 Mon Sep 17 00:00:00 2001 From: Chase Coalwell <782571+srchase@users.noreply.github.com> Date: Wed, 5 Jul 2023 11:36:28 -0600 Subject: [PATCH] fix(packages): re-export from @smithy/property-provider (#4925) --- packages/property-provider/package.json | 2 +- .../src/CredentialsProviderError.ts | 22 +---- .../property-provider/src/ProviderError.ts | 23 +---- .../src/TokenProviderError.ts | 22 +---- packages/property-provider/src/chain.ts | 38 +------ packages/property-provider/src/fromStatic.ts | 10 +- packages/property-provider/src/index.ts | 18 ---- packages/property-provider/src/memoize.ts | 99 +------------------ 8 files changed, 7 insertions(+), 227 deletions(-) diff --git a/packages/property-provider/package.json b/packages/property-provider/package.json index 82380eea98ce..a3b7913303e8 100644 --- a/packages/property-provider/package.json +++ b/packages/property-provider/package.json @@ -20,7 +20,7 @@ }, "license": "Apache-2.0", "dependencies": { - "@aws-sdk/types": "*", + "@smithy/property-provider": "^1.0.1", "tslib": "^2.5.0" }, "engines": { diff --git a/packages/property-provider/src/CredentialsProviderError.ts b/packages/property-provider/src/CredentialsProviderError.ts index dea73a142dce..00c3f23c143e 100644 --- a/packages/property-provider/src/CredentialsProviderError.ts +++ b/packages/property-provider/src/CredentialsProviderError.ts @@ -1,21 +1 @@ -import { ProviderError } from "./ProviderError"; - -/** - * @internal - * - * An error representing a failure of an individual credential provider. - * - * This error class has special meaning to the {@link chain} method. If a - * provider in the chain is rejected with an error, the chain will only proceed - * to the next provider if the value of the `tryNextLink` property on the error - * is truthy. This allows individual providers to halt the chain and also - * ensures the chain will stop if an entirely unexpected error is encountered. - */ -export class CredentialsProviderError extends ProviderError { - name = "CredentialsProviderError"; - constructor(message: string, public readonly tryNextLink: boolean = true) { - super(message, tryNextLink); - // Remove once we stop targetting ES5. - Object.setPrototypeOf(this, CredentialsProviderError.prototype); - } -} +export { CredentialsProviderError } from "@smithy/property-provider"; diff --git a/packages/property-provider/src/ProviderError.ts b/packages/property-provider/src/ProviderError.ts index 3afcfc168157..e6f606aeff65 100644 --- a/packages/property-provider/src/ProviderError.ts +++ b/packages/property-provider/src/ProviderError.ts @@ -1,22 +1 @@ -/** - * @internal - * - * An error representing a failure of an individual provider. - * - * This error class has special meaning to the {@link chain} method. If a - * provider in the chain is rejected with an error, the chain will only proceed - * to the next provider if the value of the `tryNextLink` property on the error - * is truthy. This allows individual providers to halt the chain and also - * ensures the chain will stop if an entirely unexpected error is encountered. - */ -export class ProviderError extends Error { - name = "ProviderError"; - constructor(message: string, public readonly tryNextLink: boolean = true) { - super(message); - // Remove once we stop targetting ES5. - Object.setPrototypeOf(this, ProviderError.prototype); - } - static from(error: Error, tryNextLink = true): ProviderError { - return Object.assign(new this(error.message, tryNextLink), error); - } -} +export { ProviderError } from "@smithy/property-provider"; diff --git a/packages/property-provider/src/TokenProviderError.ts b/packages/property-provider/src/TokenProviderError.ts index 91bf9fbbd308..b1e50b7c7a50 100644 --- a/packages/property-provider/src/TokenProviderError.ts +++ b/packages/property-provider/src/TokenProviderError.ts @@ -1,21 +1 @@ -import { ProviderError } from "./ProviderError"; - -/** - * @internal - * - * An error representing a failure of an individual token provider. - * - * This error class has special meaning to the {@link chain} method. If a - * provider in the chain is rejected with an error, the chain will only proceed - * to the next provider if the value of the `tryNextLink` property on the error - * is truthy. This allows individual providers to halt the chain and also - * ensures the chain will stop if an entirely unexpected error is encountered. - */ -export class TokenProviderError extends ProviderError { - name = "TokenProviderError"; - constructor(message: string, public readonly tryNextLink: boolean = true) { - super(message, tryNextLink); - // Remove once we stop targetting ES5. - Object.setPrototypeOf(this, TokenProviderError.prototype); - } -} +export { TokenProviderError } from "@smithy/property-provider"; diff --git a/packages/property-provider/src/chain.ts b/packages/property-provider/src/chain.ts index 57b2c3530a17..eb521001decc 100755 --- a/packages/property-provider/src/chain.ts +++ b/packages/property-provider/src/chain.ts @@ -1,37 +1 @@ -import { Provider } from "@aws-sdk/types"; - -import { ProviderError } from "./ProviderError"; - -/** - * @internal - * - * Compose a single credential provider function from multiple credential - * providers. The first provider in the argument list will always be invoked; - * subsequent providers in the list will be invoked in the order in which the - * were received if the preceding provider did not successfully resolve. - * - * If no providers were received or no provider resolves successfully, the - * returned promise will be rejected. - */ -export const chain = - (...providers: Array>): Provider => - async () => { - if (providers.length === 0) { - throw new ProviderError("No providers in chain"); - } - - let lastProviderError: Error | undefined; - for (const provider of providers) { - try { - const credentials = await provider(); - return credentials; - } catch (err) { - lastProviderError = err; - if (err?.tryNextLink) { - continue; - } - throw err; - } - } - throw lastProviderError; - }; +export { chain } from "@smithy/property-provider"; diff --git a/packages/property-provider/src/fromStatic.ts b/packages/property-provider/src/fromStatic.ts index 22c0ff7de04e..1aeeca2f547e 100644 --- a/packages/property-provider/src/fromStatic.ts +++ b/packages/property-provider/src/fromStatic.ts @@ -1,9 +1 @@ -import { Provider } from "@aws-sdk/types"; - -/** - * @internal - */ -export const fromStatic = - (staticValue: T): Provider => - () => - Promise.resolve(staticValue); +export { fromStatic } from "@smithy/property-provider"; diff --git a/packages/property-provider/src/index.ts b/packages/property-provider/src/index.ts index 6326994ce444..15d14e5b6437 100644 --- a/packages/property-provider/src/index.ts +++ b/packages/property-provider/src/index.ts @@ -1,24 +1,6 @@ -/** - * @internal - */ export * from "./CredentialsProviderError"; -/** - * @internal - */ export * from "./ProviderError"; -/** - * @internal - */ export * from "./TokenProviderError"; -/** - * @internal - */ export * from "./chain"; -/** - * @internal - */ export * from "./fromStatic"; -/** - * @internal - */ export * from "./memoize"; diff --git a/packages/property-provider/src/memoize.ts b/packages/property-provider/src/memoize.ts index 6fc2e0f1a9ae..90b021aaca1c 100755 --- a/packages/property-provider/src/memoize.ts +++ b/packages/property-provider/src/memoize.ts @@ -1,98 +1 @@ -import { MemoizedProvider, Provider } from "@aws-sdk/types"; - -interface MemoizeOverload { - /** - * - * Decorates a provider function with either static memoization. - * - * To create a statically memoized provider, supply a provider as the only - * argument to this function. The provider will be invoked once, and all - * invocations of the provider returned by `memoize` will return the same - * promise object. - * - * @param provider The provider whose result should be cached indefinitely. - */ - (provider: Provider): MemoizedProvider; - - /** - * Decorates a provider function with refreshing memoization. - * - * @param provider The provider whose result should be cached. - * @param isExpired A function that will evaluate the resolved value and - * determine if it is expired. For example, when - * memoizing AWS credential providers, this function - * should return `true` when the credential's - * expiration is in the past (or very near future) and - * `false` otherwise. - * @param requiresRefresh A function that will evaluate the resolved value and - * determine if it represents static value or one that - * will eventually need to be refreshed. For example, - * AWS credentials that have no defined expiration will - * never need to be refreshed, so this function would - * return `true` if the credentials resolved by the - * underlying provider had an expiration and `false` - * otherwise. - */ - ( - provider: Provider, - isExpired: (resolved: T) => boolean, - requiresRefresh?: (resolved: T) => boolean - ): MemoizedProvider; -} - -/** - * @internal - */ -export const memoize: MemoizeOverload = ( - provider: Provider, - isExpired?: (resolved: T) => boolean, - requiresRefresh?: (resolved: T) => boolean -): MemoizedProvider => { - let resolved: T; - let pending: Promise | undefined; - let hasResult: boolean; - let isConstant = false; - // Wrapper over supplied provider with side effect to handle concurrent invocation. - const coalesceProvider: Provider = async () => { - if (!pending) { - pending = provider(); - } - try { - resolved = await pending; - hasResult = true; - isConstant = false; - } finally { - pending = undefined; - } - return resolved; - }; - - if (isExpired === undefined) { - // This is a static memoization; no need to incorporate refreshing unless using forceRefresh; - return async (options) => { - if (!hasResult || options?.forceRefresh) { - resolved = await coalesceProvider(); - } - return resolved; - }; - } - - return async (options) => { - if (!hasResult || options?.forceRefresh) { - resolved = await coalesceProvider(); - } - if (isConstant) { - return resolved; - } - - if (requiresRefresh && !requiresRefresh(resolved)) { - isConstant = true; - return resolved; - } - if (isExpired(resolved)) { - await coalesceProvider(); - return resolved; - } - return resolved; - }; -}; +export { memoize } from "@smithy/property-provider";