-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(middleware-sdk-s3): split region redirect middlewares in differ…
…ent files
- Loading branch information
Showing
4 changed files
with
62 additions
and
49 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
packages/middleware-sdk-s3/src/region-redirect-endpoint-middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
HandlerExecutionContext, | ||
MetadataBearer, | ||
Pluggable, | ||
RelativeMiddlewareOptions, | ||
SerializeHandler, | ||
SerializeHandlerArguments, | ||
SerializeHandlerOutput, | ||
SerializeMiddleware, | ||
} from "@smithy/types"; | ||
|
||
import { PreviouslyResolved } from "./region-redirect-middleware"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const regionRedirectEndpointMiddleware = (config: PreviouslyResolved): SerializeMiddleware<any, any> => { | ||
return <Output extends MetadataBearer>( | ||
next: SerializeHandler<any, Output>, | ||
context: HandlerExecutionContext | ||
): SerializeHandler<any, Output> => | ||
async (args: SerializeHandlerArguments<any>): Promise<SerializeHandlerOutput<Output>> => { | ||
const originalRegion = await config.region(); | ||
if (context.__s3RegionRedirect) { | ||
config.region = async () => { | ||
return context.__s3RegionRedirect; | ||
}; | ||
} | ||
const result = await next(args); | ||
if (context.__s3RegionRedirect) { | ||
const region = await config.region(); | ||
if (originalRegion !== region) { | ||
throw new Error("Region was not restored following S3 region redirect."); | ||
} | ||
} | ||
return result; | ||
}; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const regionRedirectEndpointMiddlewareOptions: RelativeMiddlewareOptions = { | ||
tags: ["REGION_REDIRECT", "S3"], | ||
name: "regionRedirectEndpointMiddleware", | ||
override: true, | ||
relation: "before", | ||
toMiddleware: "endpointV2Middleware", | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const getRegionRedirectMiddlewarePlugin = (clientConfig: PreviouslyResolved): Pluggable<any, any> => ({ | ||
applyToStack: (clientStack) => { | ||
clientStack.addRelativeTo(regionRedirectEndpointMiddleware(clientConfig), regionRedirectEndpointMiddlewareOptions); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters