File tree Expand file tree Collapse file tree 4 files changed +52
-0
lines changed Expand file tree Collapse file tree 4 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
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
+ */
1
9
export interface Encoder {
2
10
( input : Uint8Array ) : string ;
3
11
}
4
12
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
+ */
5
21
export interface Decoder {
6
22
( input : string ) : Uint8Array ;
7
23
}
Original file line number Diff line number Diff line change @@ -39,6 +39,13 @@ const bitsPerLetter = 6;
39
39
const bitsPerByte = 8 ;
40
40
const maxLetterValue = 0b111111 ;
41
41
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
+ */
42
49
export function fromBase64 ( input : string ) : Uint8Array {
43
50
let totalByteLength = ( input . length / 4 ) * 3 ;
44
51
if ( input . substr ( - 2 ) === "==" ) {
@@ -72,6 +79,13 @@ export function fromBase64(input: string): Uint8Array {
72
79
return new Uint8Array ( out ) ;
73
80
}
74
81
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
+ */
75
89
export function toBase64 ( input : Uint8Array ) : string {
76
90
let str = "" ;
77
91
for ( let i = 0 ; i < input . length ; i += 3 ) {
Original file line number Diff line number Diff line change 1
1
import { Buffer } from "buffer" ;
2
2
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
+ */
3
9
export function fromBase64 ( input : string ) : Uint8Array {
4
10
if ( typeof input === "number" ) {
5
11
throw new Error ( "Cannot base64 decode a number" ) ;
@@ -19,6 +25,12 @@ export function fromBase64(input: string): Uint8Array {
19
25
) ;
20
26
}
21
27
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
+ */
22
34
export function toBase64 ( input : Uint8Array ) : string {
23
35
if ( typeof input === "number" ) {
24
36
throw new Error ( "Cannot base64 encode a number" ) ;
Original file line number Diff line number Diff line change @@ -8,6 +8,11 @@ import {
8
8
toBase64 as nodeToBase64
9
9
} from "@aws/util-base64-node" ;
10
10
11
+ /**
12
+ * Converts a base-64 encoded string to a Uint8Array of bytes.
13
+ *
14
+ * @param input The base-64 encoded string
15
+ */
11
16
export function fromBase64 ( input : string ) : Uint8Array {
12
17
if ( isNode ( ) ) {
13
18
return nodeFromBase64 ( input ) ;
@@ -16,6 +21,11 @@ export function fromBase64(input: string): Uint8Array {
16
21
return browserFromBase64 ( input ) ;
17
22
}
18
23
24
+ /**
25
+ * Converts a Uint8Array of binary data to a base-64 encoded string.
26
+ *
27
+ * @param input The binary data to encode
28
+ */
19
29
export function toBase64 ( input : Uint8Array ) : string {
20
30
if ( isNode ( ) ) {
21
31
return nodeToBase64 ( input ) ;
You can’t perform that action at this time.
0 commit comments