Skip to content

Commit 1cb5994

Browse files
committed
chore(middleware-bucket-endpoint): use resolved useDualstackEndpoint
The configuration is resolved in endpointsConfig
1 parent 62e555c commit 1cb5994

File tree

3 files changed

+76
-69
lines changed

3 files changed

+76
-69
lines changed

packages/middleware-bucket-endpoint/src/bucketEndpointMiddleware.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe("bucketEndpointMiddleware", () => {
3434
.fn()
3535
.mockResolvedValue({ hostname: "foo.us-foo-2.amazonaws.com", partition: "aws-foo", signingRegion: mockRegion }),
3636
useArnRegion: jest.fn().mockResolvedValue(false),
37+
useDualstackEndpoint: () => Promise.resolve(false),
3738
};
3839

3940
afterEach(() => {
@@ -79,7 +80,7 @@ describe("bucketEndpointMiddleware", () => {
7980
resolveBucketEndpointConfig({
8081
...previouslyResolvedConfig,
8182
useAccelerateEndpoint: true,
82-
useDualstackEndpoint: true,
83+
useDualstackEndpoint: () => Promise.resolve(true),
8384
forcePathStyle: true,
8485
isCustomEndpoint: true,
8586
})

packages/middleware-bucket-endpoint/src/bucketEndpointMiddleware.ts

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,76 +15,84 @@ import { bucketHostname } from "./bucketHostname";
1515
import { getPseudoRegion } from "./bucketHostnameUtils";
1616
import { BucketEndpointResolvedConfig } from "./configurations";
1717

18-
export const bucketEndpointMiddleware = (options: BucketEndpointResolvedConfig): BuildMiddleware<any, any> => <
19-
Output extends MetadataBearer
20-
>(
21-
next: BuildHandler<any, Output>,
22-
context: HandlerExecutionContext
23-
): BuildHandler<any, Output> => async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
24-
const { Bucket: bucketName } = args.input as { Bucket: string };
25-
let replaceBucketInPath = options.bucketEndpoint;
26-
const request = args.request;
27-
if (HttpRequest.isInstance(request)) {
28-
if (options.bucketEndpoint) {
29-
request.hostname = bucketName;
30-
} else if (validateArn(bucketName)) {
31-
const bucketArn = parseArn(bucketName);
32-
const clientRegion = getPseudoRegion(await options.region());
33-
const { partition, signingRegion = clientRegion } = (await options.regionInfoProvider(clientRegion)) || {};
34-
const useArnRegion = await options.useArnRegion();
35-
const { hostname, bucketEndpoint, signingRegion: modifiedSigningRegion, signingService } = bucketHostname({
36-
bucketName: bucketArn,
37-
baseHostname: request.hostname,
38-
accelerateEndpoint: options.useAccelerateEndpoint,
39-
dualstackEndpoint: options.useDualstackEndpoint,
40-
pathStyleEndpoint: options.forcePathStyle,
41-
tlsCompatible: request.protocol === "https:",
42-
useArnRegion,
43-
clientPartition: partition,
44-
clientSigningRegion: signingRegion,
45-
clientRegion: clientRegion,
46-
isCustomEndpoint: options.isCustomEndpoint,
47-
disableMultiregionAccessPoints: await options.disableMultiregionAccessPoints(),
48-
});
18+
export const bucketEndpointMiddleware =
19+
(options: BucketEndpointResolvedConfig): BuildMiddleware<any, any> =>
20+
<Output extends MetadataBearer>(
21+
next: BuildHandler<any, Output>,
22+
context: HandlerExecutionContext
23+
): BuildHandler<any, Output> =>
24+
async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => {
25+
const { Bucket: bucketName } = args.input as { Bucket: string };
26+
let replaceBucketInPath = options.bucketEndpoint;
27+
const request = args.request;
28+
if (HttpRequest.isInstance(request)) {
29+
if (options.bucketEndpoint) {
30+
request.hostname = bucketName;
31+
} else if (validateArn(bucketName)) {
32+
const bucketArn = parseArn(bucketName);
33+
const clientRegion = getPseudoRegion(await options.region());
34+
const { partition, signingRegion = clientRegion } = (await options.regionInfoProvider(clientRegion)) || {};
35+
const useArnRegion = await options.useArnRegion();
36+
const dualstackEndpoint = await options.useDualstackEndpoint();
37+
const {
38+
hostname,
39+
bucketEndpoint,
40+
signingRegion: modifiedSigningRegion,
41+
signingService,
42+
} = bucketHostname({
43+
bucketName: bucketArn,
44+
baseHostname: request.hostname,
45+
accelerateEndpoint: options.useAccelerateEndpoint,
46+
dualstackEndpoint,
47+
pathStyleEndpoint: options.forcePathStyle,
48+
tlsCompatible: request.protocol === "https:",
49+
useArnRegion,
50+
clientPartition: partition,
51+
clientSigningRegion: signingRegion,
52+
clientRegion: clientRegion,
53+
isCustomEndpoint: options.isCustomEndpoint,
54+
disableMultiregionAccessPoints: await options.disableMultiregionAccessPoints(),
55+
});
4956

50-
// If the request needs to use a region or service name inferred from ARN that different from client region, we
51-
// need to set them in the handler context so the signer will use them
52-
if (modifiedSigningRegion && modifiedSigningRegion !== signingRegion) {
53-
context["signing_region"] = modifiedSigningRegion;
54-
}
55-
if (signingService && signingService !== "s3") {
56-
context["signing_service"] = signingService;
57-
}
57+
// If the request needs to use a region or service name inferred from ARN that different from client region, we
58+
// need to set them in the handler context so the signer will use them
59+
if (modifiedSigningRegion && modifiedSigningRegion !== signingRegion) {
60+
context["signing_region"] = modifiedSigningRegion;
61+
}
62+
if (signingService && signingService !== "s3") {
63+
context["signing_service"] = signingService;
64+
}
5865

59-
request.hostname = hostname;
60-
replaceBucketInPath = bucketEndpoint;
61-
} else {
62-
const clientRegion = getPseudoRegion(await options.region());
63-
const { hostname, bucketEndpoint } = bucketHostname({
64-
bucketName,
65-
clientRegion,
66-
baseHostname: request.hostname,
67-
accelerateEndpoint: options.useAccelerateEndpoint,
68-
dualstackEndpoint: options.useDualstackEndpoint,
69-
pathStyleEndpoint: options.forcePathStyle,
70-
tlsCompatible: request.protocol === "https:",
71-
isCustomEndpoint: options.isCustomEndpoint,
72-
});
66+
request.hostname = hostname;
67+
replaceBucketInPath = bucketEndpoint;
68+
} else {
69+
const clientRegion = getPseudoRegion(await options.region());
70+
const dualstackEndpoint = await options.useDualstackEndpoint();
71+
const { hostname, bucketEndpoint } = bucketHostname({
72+
bucketName,
73+
clientRegion,
74+
baseHostname: request.hostname,
75+
accelerateEndpoint: options.useAccelerateEndpoint,
76+
dualstackEndpoint,
77+
pathStyleEndpoint: options.forcePathStyle,
78+
tlsCompatible: request.protocol === "https:",
79+
isCustomEndpoint: options.isCustomEndpoint,
80+
});
7381

74-
request.hostname = hostname;
75-
replaceBucketInPath = bucketEndpoint;
76-
}
82+
request.hostname = hostname;
83+
replaceBucketInPath = bucketEndpoint;
84+
}
7785

78-
if (replaceBucketInPath) {
79-
request.path = request.path.replace(/^(\/)?[^\/]+/, "");
80-
if (request.path === "") {
81-
request.path = "/";
86+
if (replaceBucketInPath) {
87+
request.path = request.path.replace(/^(\/)?[^\/]+/, "");
88+
if (request.path === "") {
89+
request.path = "/";
90+
}
8291
}
8392
}
84-
}
8593

86-
return next({ ...args, request });
87-
};
94+
return next({ ...args, request });
95+
};
8896

8997
export const bucketEndpointMiddlewareOptions: RelativeMiddlewareOptions = {
9098
tags: ["BUCKET_ENDPOINT"],

packages/middleware-bucket-endpoint/src/configurations.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface BucketEndpointInputConfig {
2020
* client environment will automatically prefer the AAAA record and make a connection using the IPv6 address. Note,
2121
* however, that currently on Windows, the IPv4 address will be preferred.
2222
*/
23-
useDualstackEndpoint?: boolean;
23+
useDualstackEndpoint: Provider<boolean>;
2424
/**
2525
* Whether to override the request region with the region inferred from requested resource's ARN. Defaults to false
2626
*/
@@ -57,9 +57,9 @@ export interface BucketEndpointResolvedConfig {
5757
*/
5858
useAccelerateEndpoint: boolean;
5959
/**
60-
* Resolved value for input config {@link BucketEndpointInputConfig.useDualstackEndpoint}
60+
* Enables IPv6/IPv4 dualstack endpoint.
6161
*/
62-
useDualstackEndpoint: boolean;
62+
useDualstackEndpoint: Provider<boolean>;
6363
/**
6464
* Resolved value for input config {@link BucketEndpointInputConfig.useArnRegion}
6565
*/
@@ -83,7 +83,6 @@ export function resolveBucketEndpointConfig<T>(
8383
bucketEndpoint = false,
8484
forcePathStyle = false,
8585
useAccelerateEndpoint = false,
86-
useDualstackEndpoint = false,
8786
useArnRegion = false,
8887
disableMultiregionAccessPoints = false,
8988
} = input;
@@ -92,7 +91,6 @@ export function resolveBucketEndpointConfig<T>(
9291
bucketEndpoint,
9392
forcePathStyle,
9493
useAccelerateEndpoint,
95-
useDualstackEndpoint,
9694
useArnRegion: typeof useArnRegion === "function" ? useArnRegion : () => Promise.resolve(useArnRegion),
9795
disableMultiregionAccessPoints:
9896
typeof disableMultiregionAccessPoints === "function"

0 commit comments

Comments
 (0)