From 2ed4a5f64f5af38dc570a6cc762b134697ccc628 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Fri, 29 Dec 2023 19:13:03 +0000 Subject: [PATCH 1/8] Add dependency for middleware compression --- .../smithy/typescript/codegen/TypeScriptDependency.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java index 61e1f9d85e7..b676832ddaa 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java @@ -102,6 +102,10 @@ public enum TypeScriptDependency implements Dependency { AWS_SDK_EVENTSTREAM_SERDE_NODE("dependencies", "@smithy/eventstream-serde-node", false), AWS_SDK_EVENTSTREAM_SERDE_BROWSER("dependencies", "@smithy/eventstream-serde-browser", false), + // Conditionally added if a requestCompression shape is found on any model operation. + // ToDo: Change to @smithy/middleware-compression when it is moved. + MIDDLEWARE_COMPRESSION("dependencies", "@aws-sdk/middleware-compression", false), + // Conditionally added if a big decimal shape is found in a model. BIG_JS("dependencies", "big.js", "^6.0.0", false), TYPES_BIG_JS("devDependencies", "@types/big.js", "^6.0.0", false), From 8837fd26d505c1237f1b98031f86cceccb024f14 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Fri, 29 Dec 2023 19:13:54 +0000 Subject: [PATCH 2/8] Add empty changeset --- .changeset/good-drinks-sit.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changeset/good-drinks-sit.md diff --git a/.changeset/good-drinks-sit.md b/.changeset/good-drinks-sit.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/good-drinks-sit.md @@ -0,0 +1,2 @@ +--- +--- From 2d54316a08d6dd9f76c3abe6d0414f460d895076 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Wed, 3 Jan 2024 08:11:51 +0000 Subject: [PATCH 3/8] Update MIDDLEWARE_COMPRESSION package name --- .../amazon/smithy/typescript/codegen/TypeScriptDependency.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java index b676832ddaa..ae85e1bdfe0 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptDependency.java @@ -103,8 +103,7 @@ public enum TypeScriptDependency implements Dependency { AWS_SDK_EVENTSTREAM_SERDE_BROWSER("dependencies", "@smithy/eventstream-serde-browser", false), // Conditionally added if a requestCompression shape is found on any model operation. - // ToDo: Change to @smithy/middleware-compression when it is moved. - MIDDLEWARE_COMPRESSION("dependencies", "@aws-sdk/middleware-compression", false), + MIDDLEWARE_COMPRESSION("dependencies", "@smithy/middleware-compression", false), // Conditionally added if a big decimal shape is found in a model. BIG_JS("dependencies", "big.js", "^6.0.0", false), From 3bd33902b5806566c3ec6c9c4b68effcae7900c4 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Wed, 3 Jan 2024 08:38:58 +0000 Subject: [PATCH 4/8] Add AddCompressionDependency with runtimeConfigWriters --- .../integration/AddCompressionDependency.java | 115 ++++++++++++++++++ ....codegen.integration.TypeScriptIntegration | 1 + 2 files changed, 116 insertions(+) create mode 100644 smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java new file mode 100644 index 00000000000..0d9fadd6cd4 --- /dev/null +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java @@ -0,0 +1,115 @@ +/* + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.smithy.typescript.codegen.integration; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.function.Consumer; +import java.util.logging.Logger; +import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.knowledge.TopDownIndex; +import software.amazon.smithy.model.shapes.OperationShape; +import software.amazon.smithy.model.shapes.ServiceShape; +import software.amazon.smithy.model.traits.RequestCompressionTrait; +import software.amazon.smithy.typescript.codegen.LanguageTarget; +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; +import software.amazon.smithy.utils.MapUtils; +import software.amazon.smithy.utils.SmithyInternalApi; + +/** + * Adds compression dependencies if needed. + */ +@SmithyInternalApi +public final class AddCompressionDependency implements TypeScriptIntegration { + + private static final Logger LOGGER = Logger.getLogger(AddCompressionDependency.class.getName()); + + @Override + public Map> getRuntimeConfigWriters( + TypeScriptSettings settings, + Model model, + SymbolProvider symbolProvider, + LanguageTarget target + ) { + if (!hasRequestCompressionTrait(model, settings.getService(model))) { + return Collections.emptyMap(); + } + + switch (target) { + case NODE: + return MapUtils.of( + "disableRequestCompression", writer -> { + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.addImport("loadConfig", "loadNodeConfig", + TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addImport("NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS", null, + TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.write("loadNodeConfig(NODE_DISABLE_REQUEST_COMPRESSION_CONFIG_OPTIONS)"); + }, + "requestMinCompressionSizeBytes", writer -> { + writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.addImport("loadConfig", "loadNodeConfig", + TypeScriptDependency.NODE_CONFIG_PROVIDER); + writer.addImport("NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS", null, + TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.write("loadNodeConfig(NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES_CONFIG_OPTIONS)"); + } + ); + case BROWSER: + return MapUtils.of( + "disableRequestCompression", writer -> { + writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.addImport("DEFAULT_DISABLE_REQUEST_COMPRESSION", null, + TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.write("DEFAULT_DISABLE_REQUEST_COMPRESSION"); + }, + "requestMinCompressionSizeBytes", writer -> { + writer.addDependency(TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.addImport("DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES", null, + TypeScriptDependency.MIDDLEWARE_COMPRESSION); + writer.write("DEFAULT_NODE_REQUEST_MIN_COMPRESSION_SIZE_BYTES"); + } + ); + default: + return Collections.emptyMap(); + } + } + + // return true if operation shape is decorated with `httpChecksum` trait. + private static boolean hasRequestCompressionTrait(OperationShape operation) { + return operation.hasTrait(RequestCompressionTrait.class); + } + + private static boolean hasRequestCompressionTrait( + Model model, + ServiceShape service + ) { + TopDownIndex topDownIndex = TopDownIndex.of(model); + Set operations = topDownIndex.getContainedOperations(service); + for (OperationShape operation : operations) { + if (hasRequestCompressionTrait(operation)) { + return true; + } + } + return false; + } +} diff --git a/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration b/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration index ccce6dca0a5..5bbd45c4fdc 100644 --- a/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration +++ b/smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration @@ -12,3 +12,4 @@ software.amazon.smithy.typescript.codegen.integration.AddHttpApiKeyAuthPlugin software.amazon.smithy.typescript.codegen.integration.AddBaseServiceExceptionClass software.amazon.smithy.typescript.codegen.integration.AddSdkStreamMixinDependency software.amazon.smithy.typescript.codegen.integration.DefaultReadmeGenerator +software.amazon.smithy.typescript.codegen.integration.AddCompressionDependency From 0c5239a05c3b8717e1d0ff29ed3afd9fb666f28e Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Wed, 3 Jan 2024 09:01:47 +0000 Subject: [PATCH 5/8] Add ClientPlugin to AddCompressionDependency --- .../integration/AddCompressionDependency.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java index 0d9fadd6cd4..faa31c2c295 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java @@ -16,6 +16,7 @@ package software.amazon.smithy.typescript.codegen.integration; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Consumer; @@ -30,6 +31,7 @@ import software.amazon.smithy.typescript.codegen.TypeScriptDependency; import software.amazon.smithy.typescript.codegen.TypeScriptSettings; import software.amazon.smithy.typescript.codegen.TypeScriptWriter; +import software.amazon.smithy.utils.ListUtils; import software.amazon.smithy.utils.MapUtils; import software.amazon.smithy.utils.SmithyInternalApi; @@ -94,6 +96,17 @@ public Map> getRuntimeConfigWriters( } } + @Override + public List getClientPlugins() { + return ListUtils.of( + RuntimeClientPlugin.builder() + .withConventions(TypeScriptDependency.MIDDLEWARE_COMPRESSION.dependency, + "Compression", RuntimeClientPlugin.Convention.HAS_CONFIG) + .servicePredicate((m, s) -> hasRequestCompressionTrait(m, s)) + .build() + ); + } + // return true if operation shape is decorated with `httpChecksum` trait. private static boolean hasRequestCompressionTrait(OperationShape operation) { return operation.hasTrait(RequestCompressionTrait.class); From de1038792d6a21f84407fecd5aae83cd7c90c3c6 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Wed, 3 Jan 2024 09:14:47 +0000 Subject: [PATCH 6/8] Add ClientPlugin with middleware --- .../integration/AddCompressionDependency.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java index faa31c2c295..ed6fd646778 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java @@ -19,6 +19,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeMap; import java.util.function.Consumer; import java.util.logging.Logger; import software.amazon.smithy.codegen.core.SymbolProvider; @@ -103,10 +104,30 @@ public List getClientPlugins() { .withConventions(TypeScriptDependency.MIDDLEWARE_COMPRESSION.dependency, "Compression", RuntimeClientPlugin.Convention.HAS_CONFIG) .servicePredicate((m, s) -> hasRequestCompressionTrait(m, s)) + .build(), + RuntimeClientPlugin.builder() + .withConventions(TypeScriptDependency.MIDDLEWARE_COMPRESSION.dependency, + "Compression", RuntimeClientPlugin.Convention.HAS_MIDDLEWARE) + .additionalPluginFunctionParamsSupplier((m, s, o) -> getPluginFunctionParams(m, s, o)) + .operationPredicate((m, s, o) -> hasRequestCompressionTrait(o)) .build() ); } + private static Map getPluginFunctionParams( + Model model, + ServiceShape service, + OperationShape operation + ) { + Map params = new TreeMap(); + + // Populate encodings from requestCompression trait + RequestCompressionTrait requestCompressionTrait = operation.expectTrait(RequestCompressionTrait.class); + params.put("encodings", requestCompressionTrait.getEncodings()); + + return params; + } + // return true if operation shape is decorated with `httpChecksum` trait. private static boolean hasRequestCompressionTrait(OperationShape operation) { return operation.hasTrait(RequestCompressionTrait.class); From e920e9ab2fde42729d96b5b89e563249cc28d8cd Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Wed, 3 Jan 2024 09:27:47 -0800 Subject: [PATCH 7/8] Remove year from header --- .../codegen/integration/AddCompressionDependency.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java index ed6fd646778..b8f2c323207 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. From a5a1f2b2cb9e54065d53ccba71b94d1214102273 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Wed, 3 Jan 2024 09:28:46 -0800 Subject: [PATCH 8/8] Fix typo httpChecksum -> requestCompression --- .../codegen/integration/AddCompressionDependency.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java index b8f2c323207..45c7929813e 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddCompressionDependency.java @@ -128,7 +128,7 @@ private static Map getPluginFunctionParams( return params; } - // return true if operation shape is decorated with `httpChecksum` trait. + // return true if operation shape is decorated with `requestCompression` trait. private static boolean hasRequestCompressionTrait(OperationShape operation) { return operation.hasTrait(RequestCompressionTrait.class); }