Skip to content

Commit

Permalink
feat: x-amz-user-agent for browser (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase Coalwell committed Jan 31, 2020
1 parent 2b64381 commit 6c3e098
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 60 deletions.
2 changes: 2 additions & 0 deletions packages/middleware-user-agent/src/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ export interface UserAgentInputConfig {
}
interface PreviouslyResolved {
defaultUserAgent: string;
runtime: string;
}
export interface UserAgentResolvedConfig {
defaultUserAgent: string;
customUserAgent?: string;
runtime: string;
}
export function resolveUserAgentConfig<T>(
input: T & PreviouslyResolved & UserAgentInputConfig
Expand Down
4 changes: 2 additions & 2 deletions packages/middleware-user-agent/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
import { HttpRequest } from "@aws-sdk/protocol-http";
import { UserAgentResolvedConfig } from "./configurations";

const userAgentHeader = "user-agent";

export function userAgentMiddleware(options: UserAgentResolvedConfig) {
return <Output extends MetadataBearer>(
next: BuildHandler<any, any>
Expand All @@ -20,6 +18,8 @@ export function userAgentMiddleware(options: UserAgentResolvedConfig) {
let { request } = args;
if (!HttpRequest.isInstance(request)) return next(args);
const { headers } = request;
const userAgentHeader =
options.runtime === "node" ? "user-agent" : "x-amz-user-agent";
if (!headers[userAgentHeader]) {
headers[userAgentHeader] = `${options.defaultUserAgent}`;
} else {
Expand Down
27 changes: 1 addition & 26 deletions packages/util-user-agent-browser/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultUserAgent, appendToUserAgent } from ".";
import { defaultUserAgent } from ".";
import { HttpRequest } from "@aws-sdk/protocol-http";

it("should response basic node default user agent", () => {
Expand All @@ -15,28 +15,3 @@ it("should response basic node default user agent", () => {
`aws-sdk-js-v3-client-s3-node/0.1.0 ${originUserAgent}`
);
});

it("append to user agent", () => {
const defaultValue = defaultUserAgent("client-s3-node", "0.1.0");
const request = new HttpRequest({
headers: { "X-Amz-User-Agent": defaultValue },
method: "GET",
protocol: "json",
hostname: "foo.amazonaws.com",
path: "/"
});
appendToUserAgent(request, "http/2.0");
expect(request.headers["X-Amz-User-Agent"]).toBe(`${defaultValue} http/2.0`);
});

it("append custom user agent when existing user agent was undefined", () => {
const request = {
headers: { "X-Amz-User-Agent": undefined as any },
method: "GET",
protocol: "json",
hostname: "foo.amazonaws.com",
path: "/"
};
appendToUserAgent(request, "http/2.0");
expect(request.headers["X-Amz-User-Agent"]).toBe("http/2.0");
});
11 changes: 0 additions & 11 deletions packages/util-user-agent-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,3 @@ export function defaultUserAgent(
: "";
return `aws-sdk-js-v3-${packageName}/${packageVersion} ${originUserAgent}`;
}

export function appendToUserAgent(
request: HttpRequest,
userAgentPartial: string
): void {
if (request.headers["X-Amz-User-Agent"]) {
request.headers["X-Amz-User-Agent"] += ` ${userAgentPartial}`;
} else {
request.headers["X-Amz-User-Agent"] = userAgentPartial;
}
}
15 changes: 1 addition & 14 deletions packages/util-user-agent-node/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultUserAgent, appendToUserAgent } from ".";
import { defaultUserAgent } from ".";
import { HttpRequest } from "@aws-sdk/protocol-http";
import * as process from "process";

Expand All @@ -20,17 +20,4 @@ describe("defaultUserAgent", () => {
);
if (originEnv) process.env.AWS_EXECUTION_ENV = originEnv;
});

it("append to user agent", () => {
const defaultValue = defaultUserAgent("client-s3-node", "0.1.0");
const request = new HttpRequest({
headers: { "User-Agent": defaultValue },
method: "GET",
protocol: "json",
hostname: "foo.amazonaws.com",
path: "/"
});
appendToUserAgent(request, "http/2.0");
expect(request.headers["User-Agent"]).toBe(`${defaultValue} http/2.0`);
});
});
7 changes: 0 additions & 7 deletions packages/util-user-agent-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,3 @@ export function defaultUserAgent(
}
return `aws-sdk-nodejs-v3-${packageName}/${packageVersion} ${engine}`;
}

export function appendToUserAgent(
request: HttpRequest,
userAgentPartial: string
): void {
request.headers["User-Agent"] += ` ${userAgentPartial}`;
}

0 comments on commit 6c3e098

Please sign in to comment.