UNPKG

844 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.toBase64 = exports.fromBase64 = void 0;
4const util_buffer_from_1 = require("@aws-sdk/util-buffer-from");
5const BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/;
6function fromBase64(input) {
7 if ((input.length * 3) % 4 !== 0) {
8 throw new TypeError(`Incorrect padding on base64 string.`);
9 }
10 if (!BASE64_REGEX.exec(input)) {
11 throw new TypeError(`Invalid base64 string.`);
12 }
13 const buffer = (0, util_buffer_from_1.fromString)(input, "base64");
14 return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
15}
16exports.fromBase64 = fromBase64;
17function toBase64(input) {
18 return (0, util_buffer_from_1.fromArrayBuffer)(input.buffer, input.byteOffset, input.byteLength).toString("base64");
19}
20exports.toBase64 = toBase64;