Skip to content

Commit

Permalink
chore(middleware-user-agent): use hyphen for escaping instead of unde…
Browse files Browse the repository at this point in the history
…rscore (#4784)
  • Loading branch information
trivikr committed Jun 2, 2023
1 parent 76abf6b commit a8695aa
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/middleware-user-agent/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export const X_AMZ_USER_AGENT = "x-amz-user-agent";
export const SPACE = " ";

export const UA_ESCAPE_REGEX = /[^\!\#\$\%\&\'\*\+\-\.\^\_\`\|\~\d\w]/g;

export const UA_ESCAPE_CHAR = "-";
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe("userAgentMiddleware", () => {
{ ua: ["Name", "1.0.0"], expected: "Name/1.0.0" },
{ ua: ["md/name", "1.0.0"], expected: "md/name#1.0.0" },
{ ua: ["$prefix/&name", "1.0.0"], expected: "$prefix/&name#1.0.0" },
{ ua: ["name(or not)", "1.0.0"], expected: "name_or_not_/1.0.0" },
{ ua: ["name", "1.0.0(test_version)"], expected: "name/1.0.0_test_version" },
{ ua: ["name(or not)", "1.0.0"], expected: "name-or-not-/1.0.0" },
{ ua: ["name", "1.0.0(test_version)"], expected: "name/1.0.0-test_version" },
{ ua: ["api/Service", "1.0.0"], expected: "api/service#1.0.0" },
];
[
Expand Down
4 changes: 2 additions & 2 deletions packages/middleware-user-agent/src/user-agent-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { getUserAgentPrefix } from "@aws-sdk/util-endpoints";

import { UserAgentResolvedConfig } from "./configurations";
import { SPACE, UA_ESCAPE_REGEX, USER_AGENT, X_AMZ_USER_AGENT } from "./constants";
import { SPACE, UA_ESCAPE_CHAR, UA_ESCAPE_REGEX, USER_AGENT, X_AMZ_USER_AGENT } from "./constants";

/**
* Build user agent header sections from:
Expand Down Expand Up @@ -86,7 +86,7 @@ const escapeUserAgent = ([name, version]: UserAgentPair): string => {

return [prefix, uaName, version]
.filter((item) => item && item.length > 0)
.map((item) => item?.replace(UA_ESCAPE_REGEX, "_"))
.map((item) => item?.replace(UA_ESCAPE_REGEX, UA_ESCAPE_CHAR))
.reduce((acc, item, index) => {
switch (index) {
case 0:
Expand Down

0 comments on commit a8695aa

Please sign in to comment.