Path: blob/master/node_modules/abab/lib/btoa.js
2591 views
"use strict";12/**3* btoa() as defined by the HTML and Infra specs, which mostly just references4* RFC 4648.5*/6function btoa(s) {7let i;8// String conversion as required by Web IDL.9s = `${s}`;10// "The btoa() method must throw an "InvalidCharacterError" DOMException if11// data contains any character whose code point is greater than U+00FF."12for (i = 0; i < s.length; i++) {13if (s.charCodeAt(i) > 255) {14return null;15}16}17let out = "";18for (i = 0; i < s.length; i += 3) {19const groupsOfSix = [undefined, undefined, undefined, undefined];20groupsOfSix[0] = s.charCodeAt(i) >> 2;21groupsOfSix[1] = (s.charCodeAt(i) & 0x03) << 4;22if (s.length > i + 1) {23groupsOfSix[1] |= s.charCodeAt(i + 1) >> 4;24groupsOfSix[2] = (s.charCodeAt(i + 1) & 0x0f) << 2;25}26if (s.length > i + 2) {27groupsOfSix[2] |= s.charCodeAt(i + 2) >> 6;28groupsOfSix[3] = s.charCodeAt(i + 2) & 0x3f;29}30for (let j = 0; j < groupsOfSix.length; j++) {31if (typeof groupsOfSix[j] === "undefined") {32out += "=";33} else {34out += btoaLookup(groupsOfSix[j]);35}36}37}38return out;39}4041/**42* Lookup table for btoa(), which converts a six-bit number into the43* corresponding ASCII character.44*/45const keystr =46"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";4748function btoaLookup(index) {49if (index >= 0 && index < 64) {50return keystr[index];51}5253// Throw INVALID_CHARACTER_ERR exception here -- won't be hit in the tests.54return undefined;55}5657module.exports = btoa;585960