Skip to content

Commit 8a45ba2

Browse files
committed
Add documentation blocks for all base64 encoders and decoders
1 parent b45e494 commit 8a45ba2

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

packages/types/util.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1+
/**
2+
* A function that, given a TypedArray of bytes, can produce a string
3+
* representation thereof.
4+
*
5+
* @example An encoder function that converts bytes to hexadecimal
6+
* representation would return `'deadbeef'` when given `new
7+
* Uint8Array([0xde, 0xad, 0xbe, 0xef])`.
8+
*/
19
export interface Encoder {
210
(input: Uint8Array): string;
311
}
412

13+
/**
14+
* A function that, given a string, can derive the bytes represented by that
15+
* string.
16+
*
17+
* @example A decoder function that converts bytes to hexadecimal
18+
* representation would return `new Uint8Array([0xde, 0xad, 0xbe, 0xef])` when
19+
* given the string `'deadbeef'`.
20+
*/
521
export interface Decoder {
622
(input: string): Uint8Array;
723
}

packages/util-base64-browser/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ const bitsPerLetter = 6;
3939
const bitsPerByte = 8;
4040
const maxLetterValue = 0b111111;
4141

42+
/**
43+
* Converts a base-64 encoded string to a Uint8Array of bytes.
44+
*
45+
* @param input The base-64 encoded string
46+
*
47+
* @see https://tools.ietf.org/html/rfc4648#section-4
48+
*/
4249
export function fromBase64(input: string): Uint8Array {
4350
let totalByteLength = (input.length / 4) * 3;
4451
if (input.substr(-2) === "==") {
@@ -72,6 +79,13 @@ export function fromBase64(input: string): Uint8Array {
7279
return new Uint8Array(out);
7380
}
7481

82+
/**
83+
* Converts a Uint8Array of binary data to a base-64 encoded string.
84+
*
85+
* @param input The binary data to encode
86+
*
87+
* @see https://tools.ietf.org/html/rfc4648#section-4
88+
*/
7589
export function toBase64(input: Uint8Array): string {
7690
let str = "";
7791
for (let i = 0; i < input.length; i += 3) {

packages/util-base64-node/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { Buffer } from "buffer";
22

3+
/**
4+
* Converts a base-64 encoded string to a Uint8Array of bytes using Node.JS's
5+
* `buffer` module.
6+
*
7+
* @param input The base-64 encoded string
8+
*/
39
export function fromBase64(input: string): Uint8Array {
410
if (typeof input === "number") {
511
throw new Error("Cannot base64 decode a number");
@@ -19,6 +25,12 @@ export function fromBase64(input: string): Uint8Array {
1925
);
2026
}
2127

28+
/**
29+
* Converts a Uint8Array of binary data to a base-64 encoded string using
30+
* Node.JS's `buffer` module.
31+
*
32+
* @param input The binary data to encode
33+
*/
2234
export function toBase64(input: Uint8Array): string {
2335
if (typeof input === "number") {
2436
throw new Error("Cannot base64 encode a number");

packages/util-base64-universal/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
toBase64 as nodeToBase64
99
} from "@aws/util-base64-node";
1010

11+
/**
12+
* Converts a base-64 encoded string to a Uint8Array of bytes.
13+
*
14+
* @param input The base-64 encoded string
15+
*/
1116
export function fromBase64(input: string): Uint8Array {
1217
if (isNode()) {
1318
return nodeFromBase64(input);
@@ -16,6 +21,11 @@ export function fromBase64(input: string): Uint8Array {
1621
return browserFromBase64(input);
1722
}
1823

24+
/**
25+
* Converts a Uint8Array of binary data to a base-64 encoded string.
26+
*
27+
* @param input The binary data to encode
28+
*/
1929
export function toBase64(input: Uint8Array): string {
2030
if (isNode()) {
2131
return nodeToBase64(input);

0 commit comments

Comments
 (0)