Skip to content

Commit 2b4d278

Browse files
authored
Support request compression (#1129)
1 parent 660d4f5 commit 2b4d278

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

.changeset/good-drinks-sit.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public enum TypeScriptDependency implements Dependency {
102102
AWS_SDK_EVENTSTREAM_SERDE_NODE("dependencies", "@smithy/eventstream-serde-node", false),
103103
AWS_SDK_EVENTSTREAM_SERDE_BROWSER("dependencies", "@smithy/eventstream-serde-browser", false),
104104

105+
// Conditionally added if a requestCompression shape is found on any model operation.
106+
MIDDLEWARE_COMPRESSION("dependencies", "@smithy/middleware-compression", false),
107+
105108
// Conditionally added if a big decimal shape is found in a model.
106109
BIG_JS("dependencies", "big.js", "^6.0.0", false),
107110
TYPES_BIG_JS("devDependencies", "@types/big.js", "^6.0.0", false),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 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.typescript.codegen.integration;
17+
18+
import java.util.Collections;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Set;
22+
import java.util.TreeMap;
23+
import java.util.function.Consumer;
24+
import java.util.logging.Logger;
25+
import software.amazon.smithy.codegen.core.SymbolProvider;
26+
import software.amazon.smithy.model.Model;
27+
import software.amazon.smithy.model.knowledge.TopDownIndex;
28+
import software.amazon.smithy.model.shapes.OperationShape;
29+
import software.amazon.smithy.model.shapes.ServiceShape;
30+
import software.amazon.smithy.model.traits.RequestCompressionTrait;
31+
import software.amazon.smithy.typescript.codegen.LanguageTarget;
32+
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
33+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
34+
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
35+
import software.amazon.smithy.utils.ListUtils;
36+
import software.amazon.smithy.utils.MapUtils;
37+
import software.amazon.smithy.utils.SmithyInternalApi;
38+
39+
/**
40+
* Adds compression dependencies if needed.
41+
*/
42+
@SmithyInternalApi
43+
public final class AddCompressionDependency implements TypeScriptIntegration {
44+
45+
private static final Logger LOGGER = Logger.getLogger(AddCompressionDependency.class.getName());
46+
47+
@Override
48+
public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
49+
TypeScriptSettings settings,
50+
Model model,
51+
SymbolProvider symbolProvider,
52+
LanguageTarget target
53+
) {
54+
if (!hasRequestCompressionTrait(model, settings.getService(model))) {
55+
return Collections.emptyMap();
56+
}
57+
58+
switch (target) {
59+
case NODE:
60+
return MapUtils.of(
61+
"disableRequestCompression", writer -> {
62+
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER);
63+
writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION);
64+
writer.addImport("loadConfig", "loadNodeConfig",
65+
TypeScriptDependency.NODE_CONFIG_PROVIDER);
66+
writer.addImport("NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS", null,
67+
TypeScriptDependency.MIDDLEWARE_COMPRESSION);
68+
writer.write("loadNodeConfig(NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS)");
69+
},
70+
"requestMinCompressionSizeBytes", writer -> {
71+
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER);
72+
writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION);
73+
writer.addImport("loadConfig", "loadNodeConfig",
74+
TypeScriptDependency.NODE_CONFIG_PROVIDER);
75+
writer.addImport("NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS", null,
76+
TypeScriptDependency.MIDDLEWARE_COMPRESSION);
77+
writer.write("loadNodeConfig(NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS)");
78+
}
79+
);
80+
case BROWSER:
81+
return MapUtils.of(
82+
"disableRequestCompression", writer -> {
83+
writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION);
84+
writer.addImport("DEFAULT_DISABLE_REQUEST_COMPRESSION", null,
85+
TypeScriptDependency.MIDDLEWARE_COMPRESSION);
86+
writer.write("DEFAULT_DISABLE_REQUEST_COMPRESSION");
87+
},
88+
"requestMinCompressionSizeBytes", writer -> {
89+
writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION);
90+
writer.addImport("DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES", null,
91+
TypeScriptDependency.MIDDLEWARE_COMPRESSION);
92+
writer.write("DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES");
93+
}
94+
);
95+
default:
96+
return Collections.emptyMap();
97+
}
98+
}
99+
100+
@Override
101+
public List<RuntimeClientPlugin> getClientPlugins() {
102+
return ListUtils.of(
103+
RuntimeClientPlugin.builder()
104+
.withConventions(TypeScriptDependency.MIDDLEWARE_COMPRESSION.dependency,
105+
"Compression", RuntimeClientPlugin.Convention.HAS_CONFIG)
106+
.servicePredicate((m, s) -> hasRequestCompressionTrait(m, s))
107+
.build(),
108+
RuntimeClientPlugin.builder()
109+
.withConventions(TypeScriptDependency.MIDDLEWARE_COMPRESSION.dependency,
110+
"Compression", RuntimeClientPlugin.Convention.HAS_MIDDLEWARE)
111+
.additionalPluginFunctionParamsSupplier((m, s, o) -> getPluginFunctionParams(m, s, o))
112+
.operationPredicate((m, s, o) -> hasRequestCompressionTrait(o))
113+
.build()
114+
);
115+
}
116+
117+
private static Map<String, Object> getPluginFunctionParams(
118+
Model model,
119+
ServiceShape service,
120+
OperationShape operation
121+
) {
122+
Map<String, Object> params = new TreeMap<String, Object>();
123+
124+
// Populate encodings from requestCompression trait
125+
RequestCompressionTrait requestCompressionTrait = operation.expectTrait(RequestCompressionTrait.class);
126+
params.put("encodings", requestCompressionTrait.getEncodings());
127+
128+
return params;
129+
}
130+
131+
// return true if operation shape is decorated with `requestCompression` trait.
132+
private static boolean hasRequestCompressionTrait(OperationShape operation) {
133+
return operation.hasTrait(RequestCompressionTrait.class);
134+
}
135+
136+
private static boolean hasRequestCompressionTrait(
137+
Model model,
138+
ServiceShape service
139+
) {
140+
TopDownIndex topDownIndex = TopDownIndex.of(model);
141+
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
142+
for (OperationShape operation : operations) {
143+
if (hasRequestCompressionTrait(operation)) {
144+
return true;
145+
}
146+
}
147+
return false;
148+
}
149+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ software.amazon.smithy.typescript.codegen.integration.AddHttpApiKeyAuthPlugin
1212
software.amazon.smithy.typescript.codegen.integration.AddBaseServiceExceptionClass
1313
software.amazon.smithy.typescript.codegen.integration.AddSdkStreamMixinDependency
1414
software.amazon.smithy.typescript.codegen.integration.DefaultReadmeGenerator
15+
software.amazon.smithy.typescript.codegen.integration.AddCompressionDependency

0 commit comments

Comments
 (0)