Skip to content

Commit ecfe618

Browse files
authored
fix: no LocationConstraint for us-east-1 if not explicitly passed (#985)
1 parent ef017ef commit ecfe618

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

Diff for: packages/middleware-location-constraint/src/index.spec.ts

+41-39
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,63 @@ import { locationConstraintMiddleware } from "./";
22

33
describe("locationConstrainMiddleware", () => {
44
const next = jest.fn();
5+
const basicInput = {
6+
foo: "bar"
7+
};
58

69
beforeEach(() => {
710
jest.clearAllMocks();
811
});
912

10-
it("should remove any CreateBucketConfiguration from requests directed at us-east-1", async () => {
13+
describe("for region us-east-1", () => {
1114
const handler = locationConstraintMiddleware({
1215
region: () => Promise.resolve("us-east-1")
1316
})(next, {} as any);
14-
const input = {
15-
CreateBucketConfiguration: { LocationConstraint: "us-east-1" },
16-
foo: "bar"
17-
};
18-
await handler({ input });
19-
20-
expect(next.mock.calls.length).toBe(1);
21-
expect(next.mock.calls[0][0]).toEqual({
22-
input: {
23-
...input,
24-
CreateBucketConfiguration: undefined
25-
}
17+
18+
it("should not update LocationConstraint if it's already set", async () => {
19+
const input = {
20+
...basicInput,
21+
CreateBucketConfiguration: { LocationConstraint: "us-east-1" }
22+
};
23+
await handler({ input });
24+
expect(next.mock.calls.length).toBe(1);
25+
expect(next.mock.calls[0][0]).toEqual({ input });
2626
});
27-
});
2827

29-
it("should apply a CreateBucketConfiguration with a LocationConstraint of the target region for requests directed outside of us-east-1", async () => {
30-
const handler = locationConstraintMiddleware({
31-
region: () => Promise.resolve("us-east-2")
32-
})(next, {} as any);
33-
const input = {
34-
foo: "bar"
35-
};
36-
37-
await handler({ input });
38-
39-
expect(next.mock.calls.length).toBe(1);
40-
expect(next.mock.calls[0][0]).toEqual({
41-
input: {
42-
...input,
43-
CreateBucketConfiguration: { LocationConstraint: "us-east-2" }
44-
}
28+
it("should not add LocationConstraint if it's not set", async () => {
29+
const input = basicInput;
30+
await handler({ input });
31+
expect(next.mock.calls.length).toBe(1);
32+
expect(next.mock.calls[0][0]).toEqual({ input });
4533
});
4634
});
4735

48-
it("should do nothing if a LocationConstraint had already been set on a request directed outside of us-east-1", async () => {
36+
describe("for region not us-east-1", () => {
37+
const region = "us-east-2";
4938
const handler = locationConstraintMiddleware({
50-
region: () => Promise.resolve("us-east-2")
39+
region: () => Promise.resolve(region)
5140
})(next, {} as any);
52-
const input = {
53-
CreateBucketConfiguration: { LocationConstraint: "us-east-1" },
54-
foo: "bar"
55-
};
5641

57-
await handler({ input });
42+
it("should not update LocationConstraint if it's already set", async () => {
43+
const input = {
44+
...basicInput,
45+
CreateBucketConfiguration: { LocationConstraint: "us-east-1" }
46+
};
47+
await handler({ input });
48+
expect(next.mock.calls.length).toBe(1);
49+
expect(next.mock.calls[0][0]).toEqual({ input });
50+
});
5851

59-
expect(next.mock.calls.length).toBe(1);
60-
expect(next.mock.calls[0][0]).toEqual({ input });
52+
it("should add region as LocationConstraint if it's not set", async () => {
53+
const input = basicInput;
54+
await handler({ input });
55+
expect(next.mock.calls.length).toBe(1);
56+
expect(next.mock.calls[0][0]).toEqual({
57+
input: {
58+
...input,
59+
CreateBucketConfiguration: { LocationConstraint: region }
60+
}
61+
});
62+
});
6163
});
6264
});

Diff for: packages/middleware-location-constraint/src/index.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,16 @@ export function locationConstraintMiddleware(
2626
const { CreateBucketConfiguration } = args.input;
2727
//After region config resolution, region is a Provider<string>
2828
const region = await options.region();
29-
if (region === "us-east-1") {
30-
args = {
31-
...args,
32-
input: {
33-
...args.input,
34-
CreateBucketConfiguration: undefined
35-
}
36-
};
37-
} else if (
29+
if (
3830
!CreateBucketConfiguration ||
3931
!CreateBucketConfiguration.LocationConstraint
4032
) {
4133
args = {
4234
...args,
4335
input: {
4436
...args.input,
45-
CreateBucketConfiguration: { LocationConstraint: region }
37+
CreateBucketConfiguration:
38+
region === "us-east-1" ? undefined : { LocationConstraint: region }
4639
}
4740
};
4841
}

0 commit comments

Comments
 (0)