Skip to content

Commit

Permalink
feat: glacier checksum middleware (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase Coalwell authored Jan 30, 2020
1 parent c19bb80 commit 2b64381
Show file tree
Hide file tree
Showing 38 changed files with 1,183 additions and 14 deletions.
9 changes: 9 additions & 0 deletions clients/client-glacier/GlacierClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import {
Encoder as __Encoder,
HashConstructor as __HashConstructor,
HttpHandlerOptions as __HttpHandlerOptions,
HttpRequest as __HttpRequest,
MetadataBearer as __MetadataBearer,
Provider as __Provider,
StreamCollector as __StreamCollector,
Expand Down Expand Up @@ -249,6 +250,14 @@ export interface ClientDefaults
* Fetch related hostname, signing name or signing region with given region.
*/
regionInfoProvider?: RegionInfoProvider;

/**
* Function that returns body checksums.
*/
bodyChecksumGenerator?: (
request: __HttpRequest,
options: { sha256: __HashConstructor; utf8Decoder: __Decoder }
) => Promise<[string, string]>;
}

export type GlacierClientConfig = Partial<
Expand Down
4 changes: 3 additions & 1 deletion clients/client-glacier/runtimeConfig.browser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bodyChecksumGenerator } from "@aws-sdk/body-checksum-browser";
import { invalidFunction } from "@aws-sdk/invalid-dependency";
import { Sha256 } from "@aws-crypto/sha256-browser";
import { FetchHttpHandler } from "@aws-sdk/fetch-http-handler";
Expand Down Expand Up @@ -26,5 +27,6 @@ export const ClientDefaultValues: Required<ClientDefaults> = {
runtime: "browser",
signingName: "glacier",
credentialDefaultProvider: invalidFunction("Credential is missing") as any,
regionDefaultProvider: invalidFunction("Region is missing") as any
regionDefaultProvider: invalidFunction("Region is missing") as any,
bodyChecksumGenerator
};
4 changes: 3 additions & 1 deletion clients/client-glacier/runtimeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bodyChecksumGenerator } from "@aws-sdk/body-checksum-node";
import { defaultProvider as credentialDefaultProvider } from "@aws-sdk/credential-provider-node";
import { defaultProvider as regionDefaultProvider } from "@aws-sdk/region-provider";
import { Hash } from "@aws-sdk/hash-node";
Expand Down Expand Up @@ -27,5 +28,6 @@ export const ClientDefaultValues: Required<ClientDefaults> = {
runtime: "node",
signingName: "glacier",
credentialDefaultProvider,
regionDefaultProvider
regionDefaultProvider,
bodyChecksumGenerator
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2019 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.aws.typescript.codegen;

import java.util.Set;
import java.util.logging.Logger;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.typescript.codegen.LanguageTarget;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;

import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.SetUtils;

/**
* Adds blobReader dependency if needed.
*/
public class AddBodyChecksumGeneratorDependency implements TypeScriptIntegration {
private static final Set<String> SERVICE_IDS = SetUtils.of("Glacier");

private static final Logger LOGGER = Logger.getLogger(AddBodyChecksumGeneratorDependency.class.getName());

@Override
public void addConfigInterfaceFields(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
if (!needsBodyChecksumGeneratorDep(settings.getService(model))) {
return;
}
writer.addImport("HttpRequest", "__HttpRequest", "@aws-sdk/types");
writer.writeDocs("Function that returns body checksums.");
writer.write("bodyChecksumGenerator?: (request: __HttpRequest, options: { sha256: __HashConstructor; "
+ "utf8Decoder: __Decoder }) => Promise<[string, string]>;\n");
}

@Override
public void addRuntimeConfigValues(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer,
LanguageTarget target
) {
if (!needsBodyChecksumGeneratorDep(settings.getService(model))) {
return;
}

switch (target) {
case NODE:
writeNodeConfig(writer);
break;
case BROWSER:
writeBrowserConfig(writer);
break;
default:
LOGGER.info("Unknown JavaScript target: " + target);
}
}

private void writeNodeConfig(TypeScriptWriter writer) {
writer.addDependency(AwsDependency.BODY_CHECKSUM_GENERATOR_NODE);
writer.addImport("bodyChecksumGenerator", "bodyChecksumGenerator",
AwsDependency.BODY_CHECKSUM_GENERATOR_NODE.packageName);
writer.write("bodyChecksumGenerator,");
}

private void writeBrowserConfig(TypeScriptWriter writer) {
writer.addDependency(AwsDependency.BODY_CHECKSUM_GENERATOR_BROWSER);
writer.addImport("bodyChecksumGenerator", "bodyChecksumGenerator",
AwsDependency.BODY_CHECKSUM_GENERATOR_BROWSER.packageName);
writer.write("bodyChecksumGenerator,");
}

private static boolean needsBodyChecksumGeneratorDep(ServiceShape service) {
String serviceId = service.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse("");
return SERVICE_IDS.contains(serviceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public enum AwsDependency implements SymbolDependencyContainer {
BODY_CHECKSUM(NORMAL_DEPENDENCY, "@aws-sdk/middleware-apply-body-checksum", "^1.0.0-alpha.1"),
MIDDLEWARE_HOST_HEADER(NORMAL_DEPENDENCY, "@aws-sdk/middleware-host-header", "^1.0.0-alpha.1"),
SQS_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-sqs", "^1.0.0-alpha.0"),
BODY_CHECKSUM_GENERATOR_BROWSER(NORMAL_DEPENDENCY, "@aws-sdk/body-checksum-browser", "^1.0.0-alpha.0"),
BODY_CHECKSUM_GENERATOR_NODE(NORMAL_DEPENDENCY, "@aws-sdk/body-checksum-node", "^1.0.0-alpha.0"),
XML_BUILDER(NORMAL_DEPENDENCY, "@aws-sdk/xml-builder", "^1.0.0-alpha.1"),
XML_PARSER(NORMAL_DEPENDENCY, "pixl-xml", "^1.0.13"),
XML_PARSER_TYPES(DEV_DEPENDENCY, "@types/pixl-xml", "^1.0.1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void addConfigInterfaceFields(
) {
writer.addImport("RegionInfoProvider", "RegionInfoProvider", TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.writeDocs("Fetch related hostname, signing name or signing region with given region.");
writer.write("regionInfoProvider?: RegionInfoProvider;");
writer.write("regionInfoProvider?: RegionInfoProvider;\n");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ software.amazon.smithy.aws.typescript.codegen.AwsServiceIdIntegration
software.amazon.smithy.aws.typescript.codegen.AwsPackageFixturesGeneratorIntegration
software.amazon.smithy.aws.typescript.codegen.AddMd5HashDependency
software.amazon.smithy.aws.typescript.codegen.AddStreamHasherDependency
software.amazon.smithy.aws.typescript.codegen.AddBodyChecksumGeneratorDependency
8 changes: 8 additions & 0 deletions packages/body-checksum-browser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/node_modules/
/build/
/coverage/
/docs/
*.tsbuildinfo
*.tgz
*.log
package-lock.json
13 changes: 13 additions & 0 deletions packages/body-checksum-browser/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/src/
/coverage/
/docs/
tsconfig.test.json
*.tsbuildinfo

*.spec.js
*.spec.d.ts
*.spec.js.map

*.fixture.js
*.fixture.d.ts
*.fixture.js.map
4 changes: 4 additions & 0 deletions packages/body-checksum-browser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
Loading

0 comments on commit 2b64381

Please sign in to comment.