-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update default useragent string to comply metrics requirements
- Loading branch information
1 parent
1f4b7cf
commit fa9b5fd
Showing
8 changed files
with
166 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"java.project.referencedLibraries": [ | ||
"lib/**/*.jar", | ||
"/home/ec2-user/workspace/smithy-typescript/**/*.jar", | ||
"/home/ec2-user/workspace/smithy/**/*.jar" | ||
], | ||
"java.checkstyle.configuration": "${workspaceFolder}/config/checkstyle/checkstyle.xml" | ||
} |
84 changes: 84 additions & 0 deletions
84
...rc/main/java/software/amazon/smithy/aws/typescript/codegen/AddDefaultUserAgentConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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.Collections; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import software.amazon.smithy.codegen.core.SymbolProvider; | ||
import software.amazon.smithy.model.Model; | ||
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.MapUtils; | ||
|
||
/** | ||
* Adds default user agent to AWS clients | ||
*/ | ||
public final class AddDefaultUserAgentConfig implements TypeScriptIntegration { | ||
|
||
@Override | ||
public void addConfigInterfaceFields( | ||
TypeScriptSettings settings, | ||
Model model, | ||
SymbolProvider symbolProvider, | ||
TypeScriptWriter writer | ||
) { | ||
writer.writeDocs("The string that will be used to populate default value in 'User-Agent' header"); | ||
writer.write("defaultUserAgent?: string;\n"); | ||
} | ||
|
||
@Override | ||
public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( | ||
TypeScriptSettings settings, | ||
Model model, | ||
SymbolProvider symbolProvider, | ||
LanguageTarget target | ||
) { | ||
switch (target) { | ||
case NODE: | ||
return MapUtils.of( | ||
"defaultUserAgent", writer -> { | ||
writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE.dependency); | ||
writer.addImport("defaultUserAgent", "defaultUserAgent", AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE.packageName); | ||
writer.addDefaultImport("packageInfo", "./package.json"); | ||
writer.write("defaultUserAgent: defaultUserAgent(packageInfo.name, packageInfo.version),"); | ||
} | ||
); | ||
case BROWSER: | ||
return MapUtils.of( | ||
"defaultUserAgent", writer -> { | ||
writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.dependency); | ||
writer.addImport("defaultUserAgent", "defaultUserAgent", AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.packageName); | ||
writer.addDefaultImport("packageInfo", "./package.json"); | ||
writer.write("defaultUserAgent: defaultUserAgent(packageInfo.name, packageInfo.version),"); | ||
} | ||
); | ||
case REACT_NATIVE: | ||
return MapUtils.of( | ||
"defaultUserAgent", writer -> { | ||
writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.dependency); | ||
writer.addImport("defaultUserAgent", "defaultUserAgent", AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.packageName); | ||
writer.addDefaultImport("packageInfo", "./package.json"); | ||
writer.write("defaultUserAgent: defaultUserAgent(packageInfo.name, packageInfo.version),"); | ||
} | ||
); | ||
default: | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
import { defaultUserAgent } from "."; | ||
|
||
it("should response basic node default user agent", () => { | ||
it("should response basic browser default user agent", () => { | ||
let originUserAgent = ""; | ||
if (typeof navigator === "undefined") { | ||
navigator = {} as any; | ||
originUserAgent = "foo/1.0"; | ||
originUserAgent = | ||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"; | ||
(navigator.userAgent as any) = originUserAgent; | ||
} else { | ||
originUserAgent = navigator.userAgent; | ||
} | ||
|
||
expect(defaultUserAgent("client-s3-node", "0.1.0")).toBe(`aws-sdk-js-v3-client-s3-node/0.1.0 ${originUserAgent}`); | ||
const userAgent = defaultUserAgent("client-s3", "0.1.0"); | ||
expect(userAgent).toEqual(expect.stringContaining("aws-sdk-js/0.1.0")); | ||
expect(userAgent).toEqual(expect.stringContaining(originUserAgent)); | ||
expect(userAgent).toEqual(expect.stringContaining("sdk-client/client-s3")); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
export function defaultUserAgent(packageName: string, packageVersion: string): string { | ||
const originUserAgent = | ||
typeof navigator !== "undefined" && typeof navigator.userAgent === "string" ? navigator.userAgent : ""; | ||
return `aws-sdk-js-v3-${packageName}/${packageVersion} ${originUserAgent}`; | ||
} | ||
export const defaultUserAgent = (packageName: string, packageVersion: string): string => | ||
[ | ||
// sdk-metadata | ||
// TODO: remove this post GA and version changed to 3.x.x | ||
`aws-sdk-js/${packageVersion.replace(/^1\./, "3.")}`, | ||
// os-metadata | ||
window?.navigator?.product, | ||
window?.navigator?.userAgent, | ||
// language-metadata | ||
`sdk-client/${packageName}`, | ||
] | ||
.filter((section) => section && section.length > 0) | ||
.join(" "); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
import process from "process"; | ||
|
||
export function defaultUserAgent(packageName: string, packageVersion: string): string { | ||
let engine = `${process.platform}/${process.version}`; | ||
if (process.env.AWS_EXECUTION_ENV) { | ||
engine += ` exec-env/${process.env.AWS_EXECUTION_ENV}`; | ||
} | ||
return `aws-sdk-nodejs-v3-${packageName}/${packageVersion} ${engine}`; | ||
} | ||
export const defaultUserAgent = (packageName: string, packageVersion: string): string => | ||
[ | ||
// sdk-metadata | ||
// TODO: remove this post GA and version changed to 3.x.x | ||
`aws-sdk-nodejs/${packageVersion.replace(/^1\./, "3.")}`, | ||
// os-metadata | ||
process.platform, | ||
`nodejs/${process.versions.node}`, | ||
// language-metadata | ||
`sdk-client/${packageName}`, | ||
// env-metadata | ||
process.env.AWS_EXECUTION_ENV ? `exec-env/${process.env.AWS_EXECUTION_ENV}` : undefined, | ||
] | ||
.filter((section) => section && section.length > 0) | ||
.join(" "); |