-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(middleware-user-agent): update to user agent 2.1 spec
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 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,26 @@ | ||
import { encodeMetrics } from "./encode-metrics"; | ||
|
||
describe(encodeMetrics.name, () => { | ||
it("encodes empty metrics", () => { | ||
expect(encodeMetrics({})).toEqual(""); | ||
}); | ||
|
||
it("encodes metrics", () => { | ||
expect( | ||
encodeMetrics({ | ||
A: "A", | ||
z: "z", | ||
} as any) | ||
).toEqual("A,z"); | ||
}); | ||
|
||
it("drops values that would exceed 1024 bytes", () => { | ||
expect( | ||
encodeMetrics({ | ||
A: "A".repeat(512), | ||
B: "B".repeat(511), | ||
z: "z", | ||
} as any) | ||
).toEqual("A".repeat(512) + "," + "B".repeat(511)); | ||
}); | ||
}); |
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,25 @@ | ||
import type { AwsSdkFeatures } from "@aws-sdk/types"; | ||
|
||
const BYTE_LIMIT = 1024; | ||
|
||
export function encodeMetrics(metrics: AwsSdkFeatures): string { | ||
let buffer = ""; | ||
|
||
// currently all possible values are 1 byte, | ||
// so string length is used. | ||
|
||
for (const key in metrics) { | ||
const val = metrics[key as keyof typeof metrics]!; | ||
if (buffer.length + val!.length + 1 <= BYTE_LIMIT) { | ||
if (buffer.length) { | ||
buffer += "," + val; | ||
} else { | ||
buffer += val; | ||
} | ||
continue; | ||
} | ||
break; | ||
} | ||
|
||
return buffer; | ||
} |
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