-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: encoders accept strings (#1176)
* feat: encoders accept strings * add test cases * typecheck in Encoder functions
- Loading branch information
Showing
18 changed files
with
177 additions
and
35 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 @@ | ||
--- | ||
"@smithy/middleware-serde": minor | ||
"@smithy/util-base64": minor | ||
"@smithy/util-utf8": minor | ||
"@smithy/types": minor | ||
--- | ||
|
||
encoders allow string inputs |
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
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
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,11 +1,22 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import type { Encoder } from "@smithy/types"; | ||
|
||
import testCases from "./__mocks__/testCases.json"; | ||
import { toBase64 } from "./toBase64.browser"; | ||
|
||
describe(toBase64.name, () => { | ||
it.each(testCases as Array<[string, string, number[]]>)("%s", (desc, encoded, decoded) => { | ||
expect(toBase64(new Uint8Array(decoded))).toEqual(encoded); | ||
}); | ||
|
||
it("also converts strings", () => { | ||
expect(toBase64("hello")).toEqual("aGVsbG8="); | ||
}); | ||
|
||
it("throws on non-string non-Uint8Array", () => { | ||
expect(() => (toBase64 as Encoder)(new Date())).toThrow(); | ||
expect(() => (toBase64 as Encoder)({})).toThrow(); | ||
}); | ||
}); |
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,10 +1,22 @@ | ||
import { fromArrayBuffer } from "@smithy/util-buffer-from"; | ||
import { fromUtf8 } from "@smithy/util-utf8"; | ||
|
||
/** | ||
* Converts a Uint8Array of binary data to a base-64 encoded string using | ||
* Converts a Uint8Array of binary data or a utf-8 string to a base-64 encoded string using | ||
* Node.JS's `buffer` module. | ||
* | ||
* @param input The binary data to encode | ||
* @param _input - the binary data or string to encode. | ||
* @returns base64 string. | ||
*/ | ||
export const toBase64 = (input: Uint8Array): string => | ||
fromArrayBuffer(input.buffer, input.byteOffset, input.byteLength).toString("base64"); | ||
export const toBase64 = (_input: Uint8Array | string): string => { | ||
let input: Uint8Array; | ||
if (typeof _input === "string") { | ||
input = fromUtf8(_input); | ||
} else { | ||
input = _input as Uint8Array; | ||
} | ||
if (typeof input !== "object" || typeof input.byteOffset !== "number" || typeof input.byteLength !== "number") { | ||
throw new Error("@smithy/util-base64: toBase64 encoder function only accepts string | Uint8Array."); | ||
} | ||
return fromArrayBuffer(input.buffer, input.byteOffset, input.byteLength).toString("base64"); | ||
}; |
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 +1,15 @@ | ||
export const toUtf8 = (input: Uint8Array): string => new TextDecoder("utf-8").decode(input); | ||
/** | ||
* | ||
* This does not convert non-utf8 strings to utf8, it only passes through strings if | ||
* a string is received instead of a Uint8Array. | ||
* | ||
*/ | ||
export const toUtf8 = (input: Uint8Array | string): string => { | ||
if (typeof input === "string") { | ||
return input; | ||
} | ||
if (typeof input !== "object" || typeof input.byteOffset !== "number" || typeof input.byteLength !== "number") { | ||
throw new Error("@smithy/util-utf8: toUtf8 encoder function only accepts string | Uint8Array."); | ||
} | ||
return new TextDecoder("utf-8").decode(input); | ||
}; |
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,4 +1,17 @@ | ||
import { fromArrayBuffer } from "@smithy/util-buffer-from"; | ||
|
||
export const toUtf8 = (input: Uint8Array): string => | ||
fromArrayBuffer(input.buffer, input.byteOffset, input.byteLength).toString("utf8"); | ||
/** | ||
* | ||
* This does not convert non-utf8 strings to utf8, it only passes through strings if | ||
* a string is received instead of a Uint8Array. | ||
* | ||
*/ | ||
export const toUtf8 = (input: Uint8Array | string): string => { | ||
if (typeof input === "string") { | ||
return input; | ||
} | ||
if (typeof input !== "object" || typeof input.byteOffset !== "number" || typeof input.byteLength !== "number") { | ||
throw new Error("@smithy/util-utf8: toUtf8 encoder function only accepts string | Uint8Array."); | ||
} | ||
return fromArrayBuffer(input.buffer, input.byteOffset, input.byteLength).toString("utf8"); | ||
}; |
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