Skip to content

Commit 5201103

Browse files
committed
fix(core/protocols): defer content length calculation to middleware
1 parent 7d90b33 commit 5201103

File tree

9 files changed

+8
-46
lines changed

9 files changed

+8
-46
lines changed

packages/core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
"@smithy/smithy-client": "^4.6.4",
9292
"@smithy/types": "^4.5.0",
9393
"@smithy/util-base64": "^4.1.0",
94-
"@smithy/util-body-length-browser": "^4.1.0",
9594
"@smithy/util-middleware": "^4.1.1",
9695
"@smithy/util-utf8": "^4.1.0",
9796
"tslib": "^2.6.2"

packages/core/src/submodules/protocols/ProtocolLib.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import { ErrorSchema, NormalizedSchema, TypeRegistry } from "@smithy/core/schema";
2-
import type {
3-
BodyLengthCalculator,
4-
HttpResponse as IHttpResponse,
5-
MetadataBearer,
6-
ResponseMetadata,
7-
SerdeFunctions,
8-
} from "@smithy/types";
9-
import { calculateBodyLength } from "@smithy/util-body-length-browser";
2+
import type { HttpResponse as IHttpResponse, MetadataBearer, ResponseMetadata } from "@smithy/types";
103

114
/**
125
* @internal
@@ -22,23 +15,6 @@ type ErrorMetadataBearer = MetadataBearer & {
2215
* @internal
2316
*/
2417
export class ProtocolLib {
25-
/**
26-
* @param body - to be inspected.
27-
* @param serdeContext - this is a subset type but in practice is the client.config having a property called bodyLengthChecker.
28-
*
29-
* @returns content-length value for the body if possible.
30-
* @throws Error and should be caught and handled if not possible to determine length.
31-
*/
32-
public calculateContentLength(body: any, serdeContext?: SerdeFunctions) {
33-
const bodyLengthCalculator: BodyLengthCalculator =
34-
(
35-
serdeContext as SerdeFunctions & {
36-
bodyLengthChecker?: BodyLengthCalculator;
37-
}
38-
)?.bodyLengthChecker ?? calculateBodyLength;
39-
return String(bodyLengthCalculator(body));
40-
}
41-
4218
/**
4319
* This is only for REST protocols.
4420
*

packages/core/src/submodules/protocols/json/AwsJson1_0Protocol.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ describe(AwsJson1_0Protocol.name, () => {
115115
);
116116

117117
expect(request.headers).toEqual({
118-
"content-length": "60",
119118
"content-type": "application/x-amz-json-1.0",
120119
"x-amz-target": "JsonRpc10.MyOperation",
121120
});

packages/core/src/submodules/protocols/json/AwsJsonRpcProtocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ export abstract class AwsJsonRpcProtocol extends RpcProtocol {
7171
if (deref(operationSchema.input) === "unit" || !request.body) {
7272
request.body = "{}";
7373
}
74-
try {
75-
request.headers["content-length"] = this.mixin.calculateContentLength(request.body, this.serdeContext);
76-
} catch (e) {}
74+
75+
// content-length header is set by the contentLengthMiddleware.
76+
7777
return request;
7878
}
7979

packages/core/src/submodules/protocols/json/AwsRestJsonProtocol.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ describe(AwsRestJsonProtocol.name, () => {
161161
);
162162

163163
expect(request.headers).toEqual({
164-
"content-length": "25",
165164
"content-type": "application/json",
166165
header: "hello",
167166
a: "1",
@@ -265,7 +264,6 @@ describe(AwsRestJsonProtocol.name, () => {
265264

266265
expect(request.headers).toEqual({
267266
"content-type": "application/json",
268-
"content-length": "24",
269267
"header-default-date": "Thu, 01 Jan 1970 00:00:00 GMT",
270268
"header-member-trait-date": "1970-01-01T00:00:00Z",
271269
"header-epoch-seconds": "0",

packages/core/src/submodules/protocols/json/AwsRestJsonProtocol.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ export class AwsRestJsonProtocol extends HttpBindingProtocol {
7878
request.body = "{}";
7979
}
8080

81-
if (request.body) {
82-
try {
83-
request.headers["content-length"] = this.mixin.calculateContentLength(request.body, this.serdeContext);
84-
} catch (e) {}
85-
}
81+
// content-length header is set by the contentLengthMiddleware.
8682

8783
return request;
8884
}

packages/core/src/submodules/protocols/query/AwsQueryProtocol.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ export class AwsQueryProtocol extends RpcProtocol {
8282
request.body = request.body.slice(-1);
8383
}
8484

85-
try {
86-
request.headers["content-length"] = this.mixin.calculateContentLength(request.body, this.serdeContext);
87-
} catch (e) {}
85+
// content-length header is set by the contentLengthMiddleware.
86+
8887
return request;
8988
}
9089

packages/core/src/submodules/protocols/xml/AwsRestXmlProtocol.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ describe(AwsRestXmlProtocol.name, () => {
3737
method: "POST",
3838
headers: {
3939
"content-type": "application/xml",
40-
"content-length": "167",
4140
},
4241
query: {
4342
delete: "",

packages/core/src/submodules/protocols/xml/AwsRestXmlProtocol.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ export class AwsRestXmlProtocol extends HttpBindingProtocol {
7474
}
7575
}
7676

77-
if (request.body) {
78-
try {
79-
request.headers["content-length"] = this.mixin.calculateContentLength(request.body, this.serdeContext);
80-
} catch (e) {}
81-
}
77+
// content-length header is set by the contentLengthMiddleware.
8278

8379
return request;
8480
}

0 commit comments

Comments
 (0)