base64.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // THIS FILE IS AUTOMATICALLY GENERATED! DO NOT EDIT BY HAND!
  3. //
  4. ;(function(global, factory) {
  5. typeof exports === 'object' && typeof module !== 'undefined'
  6. ? module.exports = factory()
  7. : typeof define === 'function' && define.amd
  8. ? define(factory) :
  9. // cf. https://github.com/dankogai/js-base64/issues/119
  10. (function() {
  11. // existing version for noConflict()
  12. const _Base64 = global.Base64;
  13. const gBase64 = factory();
  14. gBase64.noConflict = () => {
  15. global.Base64 = _Base64;
  16. return gBase64;
  17. };
  18. if (global.Meteor) { // Meteor.js
  19. Base64 = gBase64;
  20. }
  21. global.Base64 = gBase64;
  22. })();
  23. }((typeof self !== 'undefined' ? self
  24. : typeof window !== 'undefined' ? window
  25. : typeof global !== 'undefined' ? global
  26. : this
  27. ), function() {
  28. 'use strict';
  29. /**
  30. * base64.ts
  31. *
  32. * Licensed under the BSD 3-Clause License.
  33. * http://opensource.org/licenses/BSD-3-Clause
  34. *
  35. * References:
  36. * http://en.wikipedia.org/wiki/Base64
  37. *
  38. * @author Dan Kogai (https://github.com/dankogai)
  39. */
  40. const version = '3.3.3';
  41. /**
  42. * @deprecated use lowercase `version`.
  43. */
  44. const VERSION = version;
  45. const _b64chars = [
  46. ...'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  47. ];
  48. const _b64tab = ((chars) => {
  49. let tab = {};
  50. _b64chars.forEach((c, i) => tab[c] = i);
  51. return tab;
  52. })(_b64chars);
  53. const _fromCharCode = String.fromCharCode;
  54. const _mkUriSafe = (src) => src
  55. .replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_')
  56. .replace(/=+$/m, '');
  57. /**
  58. * converts a Uint8Array to a Base64 string.
  59. * @param {Boolean} [rfc4648] URL-and-filename-safe a la RFC4648
  60. * @returns {String} Base64 string
  61. */
  62. const fromUint8Array = (src, rfc4648 = false) => {
  63. let b64 = '';
  64. for (let i = 0, l = src.length; i < l; i += 3) {
  65. const [a0, a1, a2] = [src[i], src[i + 1], src[i + 2]];
  66. const ord = (a0 << 16) | (a1 << 8) | a2;
  67. b64 += _b64chars[(ord >>> 18)];
  68. b64 += _b64chars[(ord >>> 12) & 63];
  69. b64 += typeof a1 !== 'undefined' ? _b64chars[(ord >>> 6) & 63] : '=';
  70. b64 += typeof a2 !== 'undefined' ? _b64chars[ord & 63] : '=';
  71. }
  72. return rfc4648 ? _mkUriSafe(b64) : b64;
  73. };
  74. /**
  75. * does what `window.btoa` of web browsers does.
  76. * @param {String} src binary string
  77. * @returns {String} Base64-encoded string
  78. */
  79. const _btoa = typeof btoa === 'function'
  80. ? (s) => btoa(s)
  81. : (s) => {
  82. if (s.match(/[^\x00-\xFF]/))
  83. throw new RangeError('The string contains invalid characters.');
  84. return fromUint8Array(Uint8Array.from(s, c => c.charCodeAt(0)));
  85. };
  86. /**
  87. * @deprecated should have been internal use only.
  88. * @param {string} src UTF-8 string
  89. * @returns {string} UTF-16 string
  90. */
  91. const utob = (src) => unescape(encodeURIComponent(src));
  92. /**
  93. * converts a UTF-8-encoded string to a Base64 string.
  94. * @param {Boolean} [rfc4648] if `true` make the result URL-safe
  95. * @returns {String} Base64 string
  96. */
  97. const encode = (src, rfc4648 = false) => {
  98. const b64 = _btoa(utob(src));
  99. return rfc4648 ? _mkUriSafe(b64) : b64;
  100. };
  101. /**
  102. * converts a UTF-8-encoded string to URL-safe Base64 RFC4648.
  103. * @returns {String} Base64 string
  104. */
  105. const encodeURI = (src) => encode(src, true);
  106. /**
  107. * @deprecated should have been internal use only.
  108. * @param {string} src UTF-16 string
  109. * @returns {string} UTF-8 string
  110. */
  111. const btou = (src) => decodeURIComponent(escape(src));
  112. const _cb_decode = (cccc) => {
  113. let len = cccc.length, padlen = len % 4, n = (len > 0 ? _b64tab[cccc.charAt(0)] << 18 : 0)
  114. | (len > 1 ? _b64tab[cccc.charAt(1)] << 12 : 0)
  115. | (len > 2 ? _b64tab[cccc.charAt(2)] << 6 : 0)
  116. | (len > 3 ? _b64tab[cccc.charAt(3)] : 0), chars = [
  117. _fromCharCode(n >>> 16),
  118. _fromCharCode((n >>> 8) & 0xff),
  119. _fromCharCode(n & 0xff)
  120. ];
  121. chars.length -= [0, 0, 2, 1][padlen];
  122. return chars.join('');
  123. };
  124. /**
  125. * does what `window.atob` of web browsers does.
  126. * @param {String} src Base64-encoded string
  127. * @returns {String} binary string
  128. */
  129. const _atob = typeof atob === 'function'
  130. ? (a) => atob(a)
  131. : (a) => a.replace(/[^A-Za-z0-9\+\/]/g, '')
  132. .replace(/\S{1,4}/g, _cb_decode);
  133. const _decode = (a) => btou(_atob(a));
  134. const _unURI = (a) => {
  135. return a
  136. .replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/')
  137. .replace(/[^A-Za-z0-9\+\/]/g, '');
  138. };
  139. /**
  140. * converts a Base64 string to a UTF-8 string.
  141. * @param {String} src Base64 string. Both normal and URL-safe are supported
  142. * @returns {String} UTF-8 string
  143. */
  144. const decode = (src) => _decode(_unURI(src));
  145. /**
  146. * converts a Base64 string to a Uint8Array.
  147. */
  148. const toUint8Array = (a) => {
  149. return Uint8Array.from(_atob(_unURI(a)), c => c.charCodeAt(0));
  150. };
  151. const _noEnum = (v) => {
  152. return {
  153. value: v, enumerable: false, writable: true, configurable: true
  154. };
  155. };
  156. const extendString = function () {
  157. const _add = (name, body) => Object.defineProperty(String.prototype, name, _noEnum(body));
  158. _add('fromBase64', function () {
  159. return decode(this);
  160. });
  161. _add('toBase64', function (rfc4648) {
  162. return encode(this, rfc4648);
  163. });
  164. _add('toBase64URI', function () {
  165. return encode(this, true);
  166. });
  167. _add('toBase64URL', function () {
  168. return encode(this, true);
  169. });
  170. _add('toUint8Array', function () {
  171. return toUint8Array(this);
  172. });
  173. };
  174. const extendUint8Array = function () {
  175. const _add = (name, body) => Object.defineProperty(Uint8Array.prototype, name, _noEnum(body));
  176. _add('toBase64', function (rfc4648) {
  177. return fromUint8Array(this, rfc4648);
  178. });
  179. _add('toBase64URI', function () {
  180. return fromUint8Array(this, true);
  181. });
  182. _add('toBase64URL', function () {
  183. return fromUint8Array(this, true);
  184. });
  185. };
  186. const extendBuiltins = () => {
  187. extendString();
  188. extendUint8Array();
  189. };
  190. const gBase64 = {
  191. version: version,
  192. VERSION: VERSION,
  193. atob: _atob,
  194. btoa: _btoa,
  195. fromBase64: decode,
  196. toBase64: encode,
  197. encode: encode,
  198. encodeURI: encodeURI,
  199. encodeURL: encodeURI,
  200. utob: utob,
  201. btou: btou,
  202. decode: decode,
  203. fromUint8Array: fromUint8Array,
  204. toUint8Array: toUint8Array,
  205. extendString: extendString,
  206. extendUint8Array: extendUint8Array,
  207. extendBuiltins: extendBuiltins
  208. };
  209. //
  210. // export Base64 to the namespace
  211. //
  212. gBase64.Base64 = Object.assign({}, gBase64);
  213. return gBase64;
  214. }));