UNPKG

js-base64

Version:

Yet another Base64 transcoder in pure-JS

298 lines (238 loc) 9.74 kB
"use strict"; function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } /** * base64.js * * Licensed under the BSD 3-Clause License. * http://opensource.org/licenses/BSD-3-Clause * * References: * http://en.wikipedia.org/wiki/Base64 * * @author Dan Kogai (https://github.com/dankogai) */ ; (function (global, factory) { (typeof exports === "undefined" ? "undefined" : _typeof(exports)) === 'object' && typeof module !== 'undefined' ? module.exports = factory(global) : typeof define === 'function' && define.amd ? define(factory) : factory(global); })(typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : void 0, function (global) { 'use strict'; global = global || {}; // existing version for noConflict() var _Base64 = global.Base64; var version = '3.0.2'; var _b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; var _b64tab = function (bin) { var tab = {}, i = 0; var _iterator = _createForOfIteratorHelper(bin), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var c = _step.value; tab[c] = i++; } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return tab; }(_b64chars); var _fromCharCode = String.fromCharCode; var _mkUriSafe = function _mkUriSafe(src) { return String(src).replace(/[+\/]/g, function (m0) { return m0 == '+' ? '-' : '_'; }).replace(/=/g, ''); }; /** * converts a Uint8Array to a Base64 string * @param {Uint8Array} src * @param {Boolean} urisafe URL-and-filename-safe a la RFC4648 * @returns {String} Base64 string */ var fromUint8Array = function fromUint8Array(src, urisafe) { var b64 = ''; for (var i = 0, l = src.length; i < l; i += 3) { var a0 = src[i], a1 = src[i + 1], a2 = src[i + 2]; var ord = a0 << 16 | a1 << 8 | a2; b64 += _b64chars.charAt(ord >>> 18) + _b64chars.charAt(ord >>> 12 & 63) + (typeof a1 != 'undefined' ? _b64chars.charAt(ord >>> 6 & 63) : '=') + (typeof a2 != 'undefined' ? _b64chars.charAt(ord & 63) : '='); } return urisafe ? _mkUriSafe(b64) : b64; }; /** * 100% compatibile with `window.btoa` of web browsers * @param {String} src binary string * @returns {String} Base64-encoded string */ var btoa = global.btoa && typeof global.btoa == 'function' ? global.btoa.bind(global) : function (src) { if (src.match(/[^\x00-\xFF]/)) throw new RangeError('The string contains invalid characters.'); return fromUint8Array(Uint8Array.from(src, function (c) { return c.charCodeAt(0); })); }; /** * @deprecated since 3.0.0 * @param {string} src UTF-8 string * @returns {string} UTF-16 string */ var utob = function utob(src) { return unescape(encodeURIComponent(src)); }; /** * converts a UTF-8-encoded string to a Base64 string * @param {String} src the string to convert * @param {Boolean} rfc4648 if `true` make the result URL-safe * @returns {String} Base64 string */ var encode = function encode(src, rfc4648) { var b64 = btoa(utob(src)); return rfc4648 ? _mkUriSafe(b64) : b64; }; /** * converts a UTF-8-encoded string to URL-safe Base64 RFC4648 * @param {String} src the string to convert * @returns {String} Base64 string */ var encodeURI = function encodeURI(src) { return encode(src, true); }; /** * @deprecated since 3.0.0 * @param {string} src UTF-16 string * @returns {string} UTF-8 string */ var btou = function btou(src) { return decodeURIComponent(escape(src)); }; var _cb_decode = function _cb_decode(cccc) { var len = cccc.length, padlen = len % 4, n = (len > 0 ? _b64tab[cccc.charAt(0)] << 18 : 0) | (len > 1 ? _b64tab[cccc.charAt(1)] << 12 : 0) | (len > 2 ? _b64tab[cccc.charAt(2)] << 6 : 0) | (len > 3 ? _b64tab[cccc.charAt(3)] : 0), chars = [_fromCharCode(n >>> 16), _fromCharCode(n >>> 8 & 0xff), _fromCharCode(n & 0xff)]; chars.length -= [0, 0, 2, 1][padlen]; return chars.join(''); }; /** * 100% compatibile with `window.atob` of web browsers * @param {String} src Base64-encoded string * @returns {String} binary string */ var atob = global.atob && typeof global.atob == 'function' ? global.atob.bind(global) : function (a) { return String(a).replace(/[^A-Za-z0-9\+\/]/g, '').replace(/\S{1,4}/g, _cb_decode); }; var _decode = function _decode(a) { return btou(atob(a)); }; var _fromURI = function _fromURI(a) { return String(a).replace(/[-_]/g, function (m0) { return m0 == '-' ? '+' : '/'; }).replace(/[^A-Za-z0-9\+\/]/g, ''); }; /** * converts a Base64 string to a UTF-8 string * @param {String} src Base64 string. Both normal and URL-safe are supported * @returns {String} UTF-8 string */ var decode = function decode(src) { return _decode(_fromURI(src)); }; /** * converts a Base64 string to a Uint8Array * @param {String} src Base64 string. Both normal and URL-safe are supported * @returns {Uint8Array} UTF-8 string */ var toUint8Array = function toUint8Array(a) { return Uint8Array.from(atob(_fromURI(a)), function (c) { return c.charCodeAt(0); }); }; var noConflict = function noConflict() { var Base64 = global.Base64; global.Base64 = _Base64; return Base64; }; // export Base64 global.Base64 = { VERSION: version, atob: atob, btoa: btoa, fromBase64: decode, toBase64: encode, utob: utob, encode: encode, encodeURI: encodeURI, encodeURL: encodeURI, btou: btou, decode: decode, noConflict: noConflict, fromUint8Array: fromUint8Array, toUint8Array: toUint8Array }; // make Base64.extendString() available var _noEnum = function _noEnum(v) { return { value: v, enumerable: false, writable: true, configurable: true }; }; // make Base64.extendString() available global.Base64.extendString = function () { var _add = function _add(name, body) { return Object.defineProperty(String.prototype, name, _noEnum(body)); }; _add('fromBase64', function () { return decode(this); }); _add('toBase64', function (rfc4648) { return encode(this, rfc4648); }); _add('toBase64URI', function () { return encode(this, true); }); _add('toBase64URL', function () { return encode(this, true); }); _add('toUint8Array', function () { return toUint8Array(this); }); }; // make Base64.extendUint8Array() available global.Base64.extendUint8Array = function () { var _add = function _add(name, body) { return Object.defineProperty(Uint8Array.prototype, name, _noEnum(body)); }; _add('toBase64', function (rfc4648) { return fromUint8Array(this, rfc4648); }); _add('toBase64URI', function () { return fromUint8Array(this, true); }); _add('toBase64URL', function () { return fromUint8Array(this, true); }); }; global.Base64.extendBuiltins = function () { global.Base64.extendString(); global.Base64.extendUint8Array(); }; // // export Base64 to the namespace // if (global['Meteor']) { // Meteor.js Base64 = global.Base64; } // module.exports and AMD are mutually exclusive. // module.exports has precedence. if (typeof module !== 'undefined' && module.exports) { module.exports.Base64 = global.Base64; } else if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define([], function () { return global.Base64; }); } // that's it! return { Base64: global.Base64 }; });