Skip to content

Commit 9a9a601

Browse files
Chase Coalwelltrivikr
Chase Coalwell
authored andcommitted
feat: remove headerName config option (#561)
1 parent cd96262 commit 9a9a601

File tree

3 files changed

+38
-45
lines changed

3 files changed

+38
-45
lines changed

Diff for: packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.spec.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ describe("applyMd5BodyChecksumMiddleware", () => {
2727
new Uint8Array(10),
2828
void 0
2929
]) {
30-
it("should calculate the body hash, encode the result, and set the encoded hash to the provided header", async () => {
30+
it("should calculate the body hash, encode the result, and set the encoded hash to Content-MD5 header", async () => {
3131
const handler = applyMd5BodyChecksumMiddleware({
32-
headerName: "checksumHeader",
3332
md5: MockHash,
3433
base64Encoder: mockEncoder,
3534
streamHasher: async (stream: ExoticStream) => new Uint8Array(5)
@@ -44,13 +43,12 @@ describe("applyMd5BodyChecksumMiddleware", () => {
4443

4544
expect(next.mock.calls.length).toBe(1);
4645
const { request } = next.mock.calls[0][0];
47-
expect(request.headers["checksumHeader"]).toBe("encoded");
46+
expect(request.headers["Content-MD5"]).toBe("encoded");
4847
expect(mockHashUpdate.mock.calls).toEqual([[body || ""]]);
4948
});
5049

5150
it("should do nothing if a case-insenitive match for the desired header has already been set", async () => {
5251
const handler = applyMd5BodyChecksumMiddleware({
53-
headerName: "checksumHeader",
5452
md5: MockHash,
5553
base64Encoder: mockEncoder,
5654
streamHasher: async (stream: ExoticStream) => new Uint8Array(5)
@@ -61,15 +59,15 @@ describe("applyMd5BodyChecksumMiddleware", () => {
6159
request: new HttpRequest({
6260
body: body,
6361
headers: {
64-
cHeCkSuMhEaDeR: "foo"
62+
"CoNtEnT-Md5": "foo"
6563
}
6664
})
6765
});
6866

6967
expect(next.mock.calls.length).toBe(1);
7068
const { request } = next.mock.calls[0][0];
71-
expect(request.headers["cHeCkSuMhEaDeR"]).toBe("foo");
72-
expect(request.headers["checksumHeader"]).toBe(undefined);
69+
expect(request.headers["CoNtEnT-Md5"]).toBe("foo");
70+
expect(request.headers["Content-MD5"]).toBe(undefined);
7371
expect(mockHashUpdate.mock.calls.length).toBe(0);
7472
expect(mockHashDigest.mock.calls.length).toBe(0);
7573
expect(mockEncoder.mock.calls.length).toBe(0);
@@ -78,7 +76,6 @@ describe("applyMd5BodyChecksumMiddleware", () => {
7876

7977
it("should use the supplied stream hasher to calculate the hash of a streaming body", async () => {
8078
const handler = applyMd5BodyChecksumMiddleware({
81-
headerName: "checksumHeader",
8279
md5: MockHash,
8380
base64Encoder: mockEncoder,
8481
streamHasher: async (stream: ExoticStream) => new Uint8Array(5)
@@ -94,7 +91,7 @@ describe("applyMd5BodyChecksumMiddleware", () => {
9491
expect(next.mock.calls.length).toBe(1);
9592
const { request } = next.mock.calls[0][0];
9693
expect(request.body).toStrictEqual(new ExoticStream());
97-
expect(request.headers["checksumHeader"]).toBe("encoded");
94+
expect(request.headers["Content-MD5"]).toBe("encoded");
9895
expect(mockHashDigest.mock.calls.length).toBe(0);
9996
expect(mockEncoder.mock.calls.length).toBe(1);
10097
expect(mockEncoder.mock.calls).toEqual([[new Uint8Array(5)]]);

Diff for: packages/middleware-apply-body-checksum/src/applyMd5BodyChecksumMiddleware.ts

+30-30
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,38 @@ export function applyMd5BodyChecksumMiddleware(
2020
): BuildHandler<any, Output> => async (
2121
args: BuildHandlerArguments<any>
2222
): Promise<BuildHandlerOutput<Output>> => {
23-
let request = { ...args.request };
24-
if (HttpRequest.isInstance(request)) {
25-
const { body, headers } = request;
26-
if (!hasHeader(options.headerName, headers)) {
27-
let digest: Promise<Uint8Array>;
28-
if (
29-
body === undefined ||
30-
typeof body === "string" ||
31-
ArrayBuffer.isView(body) ||
32-
isArrayBuffer(body)
33-
) {
34-
const hash = new options.md5();
35-
hash.update(body || "");
36-
digest = hash.digest();
37-
} else {
38-
digest = options.streamHasher(options.md5, body);
39-
}
40-
41-
request = {
42-
...request,
43-
headers: {
44-
...headers,
45-
[options.headerName]: options.base64Encoder(await digest)
46-
}
47-
};
23+
let { request } = args;
24+
if (HttpRequest.isInstance(request)) {
25+
const { body, headers } = request;
26+
if (!hasHeader("Content-MD5", headers)) {
27+
let digest: Promise<Uint8Array>;
28+
if (
29+
body === undefined ||
30+
typeof body === "string" ||
31+
ArrayBuffer.isView(body) ||
32+
isArrayBuffer(body)
33+
) {
34+
const hash = new options.md5();
35+
hash.update(body || "");
36+
digest = hash.digest();
37+
} else {
38+
digest = options.streamHasher(options.md5, body);
4839
}
40+
41+
request = {
42+
...request,
43+
headers: {
44+
...headers,
45+
"Content-MD5": options.base64Encoder(await digest)
46+
}
47+
};
4948
}
50-
return next({
51-
...args,
52-
request
53-
});
54-
};
49+
}
50+
return next({
51+
...args,
52+
request
53+
});
54+
};
5555
}
5656

5757
export const applyMd5BodyChecksumMiddlewareOptions: BuildHandlerOptions = {
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { Encoder, Hash, StreamHasher } from "@aws-sdk/types";
22

3-
export interface Md5BodyChecksumInputConfig {
4-
headerName?: string;
5-
}
3+
export interface Md5BodyChecksumInputConfig {}
64
interface PreviouslyResolved {
75
md5: { new (): Hash };
86
base64Encoder: Encoder;
97
streamHasher: StreamHasher<any>;
108
}
119
export interface Md5BodyChecksumResolvedConfig {
12-
headerName: string;
1310
md5: { new (): Hash };
1411
base64Encoder: Encoder;
1512
streamHasher: StreamHasher<any>;
@@ -18,7 +15,6 @@ export function resolveMd5BodyChecksumConfig<T>(
1815
input: T & PreviouslyResolved & Md5BodyChecksumInputConfig
1916
): T & Md5BodyChecksumResolvedConfig {
2017
return {
21-
...input,
22-
headerName: input.headerName || "Content-MD5"
18+
...input
2319
};
2420
}

0 commit comments

Comments
 (0)