UNPKG

1.04 kBPlain TextView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3
4/**
5 * Encodes a string in base64 format.
6 * @param value the string to encode
7 */
8export function encodeString(value: string): string {
9 return Buffer.from(value).toString("base64");
10}
11
12/**
13 * Encodes a byte array in base64 format.
14 * @param value the Uint8Aray to encode
15 */
16export function encodeByteArray(value: Uint8Array): string {
17 // Buffer.from accepts <ArrayBuffer> | <SharedArrayBuffer>-- the TypeScript definition is off here
18 // https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length
19 const bufferValue = value instanceof Buffer ? value : Buffer.from(value.buffer as ArrayBuffer);
20 return bufferValue.toString("base64");
21}
22
23/**
24 * Decodes a base64 string into a byte array.
25 * @param value the base64 string to decode
26 */
27export function decodeString(value: string): Uint8Array {
28 return Buffer.from(value, "base64");
29}