Skip to content

Commit 0ba4243

Browse files
author
Chase Coalwell
authored
feat: Migrate and apply ApplyMd5BodyChecksumMiddleware (#493)
* feat: add md5 body checksum middleware * feat: add ApplyMd5BodyChecksum plugin
1 parent 6a18cf3 commit 0ba4243

File tree

22 files changed

+419
-248
lines changed

22 files changed

+419
-248
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddBuiltinPlugins.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public class AddBuiltinPlugins implements TypeScriptIntegration {
3939

4040
private static final Set<String> SSEC_OPERATIONS = SetUtils.of("SSECustomerKey", "CopySourceSSECustomerKey");
4141

42+
private static final Set<String> S3_MD5_OPERATIONS = SetUtils.of(
43+
"DeleteObjects",
44+
"PutBucketCors",
45+
"PutBucketLifecycle",
46+
"PutBucketLifecycleConfiguration",
47+
"PutBucketPolicy",
48+
"PutBucketTagging",
49+
"PutBucketReplication"
50+
);
51+
4252
@Override
4353
public List<RuntimeClientPlugin> getClientPlugins() {
4454
// Note that order is significant because configurations might
@@ -93,6 +103,12 @@ public List<RuntimeClientPlugin> getClientPlugins() {
93103
HAS_MIDDLEWARE)
94104
.servicePredicate((m, s) -> testServiceId(s, "S3"))
95105
.operationPredicate((m, s, o) -> o.getId().getName().equals("CreateBucket"))
106+
.build(),
107+
RuntimeClientPlugin.builder()
108+
.withConventions(AwsDependency.BODY_CHECKSUM.dependency, "ApplyMd5BodyChecksum",
109+
HAS_MIDDLEWARE)
110+
.servicePredicate((m, s) -> testServiceId(s, "S3"))
111+
.operationPredicate((m, s, o) -> S3_MD5_OPERATIONS.contains(o.getId().getName()))
96112
.build()
97113
);
98114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.smithy.aws.typescript.codegen;
17+
18+
import software.amazon.smithy.aws.traits.ServiceTrait;
19+
import software.amazon.smithy.codegen.core.SymbolProvider;
20+
import software.amazon.smithy.model.Model;
21+
import software.amazon.smithy.model.shapes.ServiceShape;
22+
import software.amazon.smithy.typescript.codegen.LanguageTarget;
23+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
24+
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
25+
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
26+
import software.amazon.smithy.utils.SetUtils;
27+
28+
import java.util.Set;
29+
30+
/**
31+
* Adds Md5Hash if needed.
32+
*/
33+
public class AddMd5HashDependency implements TypeScriptIntegration {
34+
private static final Set<String> SERVICE_IDS = SetUtils.of("S3", "SQS");
35+
36+
@Override
37+
public void addConfigInterfaceFields(
38+
TypeScriptSettings settings,
39+
Model model,
40+
SymbolProvider symbolProvider,
41+
TypeScriptWriter writer
42+
) {
43+
if (!needsMd5Dep(settings.getService(model))) {
44+
return;
45+
}
46+
47+
writer.addImport("HashConstructor", "__HashConstructor", "@aws-sdk/types");
48+
writer.writeDocs("A constructor for a class implementing the @aws-sdk/types.Hash interface \n"
49+
+ "that computes MD5 hashes");
50+
writer.write("md5?: __HashConstructor;\n");
51+
}
52+
53+
@Override
54+
public void addRuntimeConfigValues(
55+
TypeScriptSettings settings,
56+
Model model,
57+
SymbolProvider symbolProvider,
58+
TypeScriptWriter writer,
59+
LanguageTarget target
60+
) {
61+
if (!needsMd5Dep(settings.getService(model))) {
62+
return;
63+
}
64+
65+
switch (target) {
66+
case NODE:
67+
writer.addImport("HashConstructor", "__HashConstructor", "@aws-sdk/types");
68+
writer.write("md5: Hash.bind(null, \"md5\"),");
69+
break;
70+
case BROWSER:
71+
writer.addDependency(AwsDependency.MD5_BROWSER);
72+
writer.addImport("Md5", "Md5", AwsDependency.MD5_BROWSER.packageName);
73+
writer.write("md5: Md5,");
74+
break;
75+
default:
76+
// do nothing
77+
}
78+
}
79+
80+
private static boolean needsMd5Dep(ServiceShape service) {
81+
String serviceId = service.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse("");
82+
return SERVICE_IDS.contains(serviceId);
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.smithy.aws.typescript.codegen;
17+
18+
import software.amazon.smithy.aws.traits.ServiceTrait;
19+
import software.amazon.smithy.codegen.core.SymbolProvider;
20+
import software.amazon.smithy.model.Model;
21+
import software.amazon.smithy.model.shapes.ServiceShape;
22+
import software.amazon.smithy.typescript.codegen.LanguageTarget;
23+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
24+
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
25+
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
26+
27+
/**
28+
* Adds StreamHasher if needed.
29+
*/
30+
public class AddStreamHasherDependency implements TypeScriptIntegration {
31+
32+
@Override
33+
public void addConfigInterfaceFields(
34+
TypeScriptSettings settings,
35+
Model model,
36+
SymbolProvider symbolProvider,
37+
TypeScriptWriter writer
38+
) {
39+
if (!needsStreamHasher(settings.getService(model))) {
40+
return;
41+
}
42+
43+
writer.addImport("Readable", "Readable", "stream");
44+
writer.addImport("StreamHasher", "__StreamHasher", "@aws-sdk/types");
45+
writer.writeDocs("A function that, given a hash constructor and a stream, calculates the \n"
46+
+ "hash of the streamed value");
47+
writer.write("streamHasher?: __StreamHasher<Readable|Blob>;\n");
48+
}
49+
50+
@Override
51+
public void addRuntimeConfigValues(
52+
TypeScriptSettings settings,
53+
Model model,
54+
SymbolProvider symbolProvider,
55+
TypeScriptWriter writer,
56+
LanguageTarget target
57+
) {
58+
if (!needsStreamHasher(settings.getService(model))) {
59+
return;
60+
}
61+
62+
switch (target) {
63+
case NODE:
64+
writer.addDependency(AwsDependency.STREAM_HASHER_NODE);
65+
writer.addImport("calculateSha256", "streamHasher", AwsDependency.STREAM_HASHER_NODE.packageName);
66+
writer.write("streamHasher,");
67+
break;
68+
case BROWSER:
69+
writer.addDependency(AwsDependency.STREAM_HASHER_BROWSER);
70+
writer.addImport("calculateSha256", "streamHasher", AwsDependency.STREAM_HASHER_BROWSER.packageName);
71+
writer.write("streamHasher,");
72+
break;
73+
default:
74+
// do nothing
75+
}
76+
}
77+
78+
private static boolean needsStreamHasher(ServiceShape service) {
79+
String serviceId = service.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse("");
80+
if (serviceId.equals("S3")) {
81+
return true;
82+
}
83+
84+
return false;
85+
}
86+
}

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ public enum AwsDependency implements SymbolDependencyContainer {
3636
ADD_EXPECT_CONTINUE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-expect-continue", "^0.1.0-preview.5"),
3737
ADD_GLACIER_API_VERSION(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-glacier", "^0.1.0-preview.7"),
3838
SSEC_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-ssec", "^0.1.0-preview.5"),
39-
LOCATION_CONSTRAINT(NORMAL_DEPENDENCY, "@aws-sdk/middleware-location-constraint", "^0.1.0-preview.5");
39+
LOCATION_CONSTRAINT(NORMAL_DEPENDENCY, "@aws-sdk/middleware-location-constraint", "^0.1.0-preview.5"),
40+
MD5_BROWSER(NORMAL_DEPENDENCY, "@aws-sdk/md5-js", "^0.1.0-preview.8"),
41+
STREAM_HASHER_NODE(NORMAL_DEPENDENCY, "@aws-sdk/hash-stream-node", "^0.1.0-preview.4"),
42+
STREAM_HASHER_BROWSER(NORMAL_DEPENDENCY, "@aws-sdk/hash-blob-browser", "^0.1.0-preview.4"),
43+
BODY_CHECKSUM(NORMAL_DEPENDENCY, "@aws-sdk/middleware-apply-body-checksum", "^0.1.0-preview.5");
4044

4145
public final String packageName;
4246
public final String version;

codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ software.amazon.smithy.aws.typescript.codegen.AddAwsRuntimeConfig
22
software.amazon.smithy.aws.typescript.codegen.AddBuiltinPlugins
33
software.amazon.smithy.aws.typescript.codegen.AddProtocols
44
software.amazon.smithy.aws.typescript.codegen.AwsServiceIdIntegration
5+
software.amazon.smithy.aws.typescript.codegen.AddMd5HashDependency
6+
software.amazon.smithy.aws.typescript.codegen.AddStreamHasherDependency

packages/apply-body-checksum-middleware/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/apply-body-checksum-middleware/src/index.spec.ts

Lines changed: 0 additions & 163 deletions
This file was deleted.

0 commit comments

Comments
 (0)