base64.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.6.1';
  41. /**
  42. * @deprecated use lowercase `version`.
  43. */
  44. const VERSION = version;
  45. const _hasatob = typeof atob === 'function';
  46. const _hasbtoa = typeof btoa === 'function';
  47. const _hasBuffer = typeof Buffer === 'function';
  48. const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
  49. const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
  50. const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  51. const b64chs = [...b64ch];
  52. const b64tab = ((a) => {
  53. let tab = {};
  54. a.forEach((c, i) => tab[c] = i);
  55. return tab;
  56. })(b64chs);
  57. const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
  58. const _fromCC = String.fromCharCode.bind(String);
  59. const _U8Afrom = typeof Uint8Array.from === 'function'
  60. ? Uint8Array.from.bind(Uint8Array)
  61. : (it, fn = (x) => x) => new Uint8Array(Array.prototype.slice.call(it, 0).map(fn));
  62. const _mkUriSafe = (src) => src
  63. .replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_')
  64. .replace(/=+$/m, '');
  65. const _tidyB64 = (s) => s.replace(/[^A-Za-z0-9\+\/]/g, '');
  66. /**
  67. * polyfill version of `btoa`
  68. */
  69. const btoaPolyfill = (bin) => {
  70. // console.log('polyfilled');
  71. let u32, c0, c1, c2, asc = '';
  72. const pad = bin.length % 3;
  73. for (let i = 0; i < bin.length;) {
  74. if ((c0 = bin.charCodeAt(i++)) > 255 ||
  75. (c1 = bin.charCodeAt(i++)) > 255 ||
  76. (c2 = bin.charCodeAt(i++)) > 255)
  77. throw new TypeError('invalid character found');
  78. u32 = (c0 << 16) | (c1 << 8) | c2;
  79. asc += b64chs[u32 >> 18 & 63]
  80. + b64chs[u32 >> 12 & 63]
  81. + b64chs[u32 >> 6 & 63]
  82. + b64chs[u32 & 63];
  83. }
  84. return pad ? asc.slice(0, pad - 3) + "===".substring(pad) : asc;
  85. };
  86. /**
  87. * does what `window.btoa` of web browsers do.
  88. * @param {String} bin binary string
  89. * @returns {string} Base64-encoded string
  90. */
  91. const _btoa = _hasbtoa ? (bin) => btoa(bin)
  92. : _hasBuffer ? (bin) => Buffer.from(bin, 'binary').toString('base64')
  93. : btoaPolyfill;
  94. const _fromUint8Array = _hasBuffer
  95. ? (u8a) => Buffer.from(u8a).toString('base64')
  96. : (u8a) => {
  97. // cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
  98. const maxargs = 0x1000;
  99. let strs = [];
  100. for (let i = 0, l = u8a.length; i < l; i += maxargs) {
  101. strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs)));
  102. }
  103. return _btoa(strs.join(''));
  104. };
  105. /**
  106. * converts a Uint8Array to a Base64 string.
  107. * @param {boolean} [urlsafe] URL-and-filename-safe a la RFC4648 §5
  108. * @returns {string} Base64 string
  109. */
  110. const fromUint8Array = (u8a, urlsafe = false) => urlsafe ? _mkUriSafe(_fromUint8Array(u8a)) : _fromUint8Array(u8a);
  111. // This trick is found broken https://github.com/dankogai/js-base64/issues/130
  112. // const utob = (src: string) => unescape(encodeURIComponent(src));
  113. // reverting good old fationed regexp
  114. const cb_utob = (c) => {
  115. if (c.length < 2) {
  116. var cc = c.charCodeAt(0);
  117. return cc < 0x80 ? c
  118. : cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
  119. + _fromCC(0x80 | (cc & 0x3f)))
  120. : (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
  121. + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
  122. + _fromCC(0x80 | (cc & 0x3f)));
  123. }
  124. else {
  125. var cc = 0x10000
  126. + (c.charCodeAt(0) - 0xD800) * 0x400
  127. + (c.charCodeAt(1) - 0xDC00);
  128. return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
  129. + _fromCC(0x80 | ((cc >>> 12) & 0x3f))
  130. + _fromCC(0x80 | ((cc >>> 6) & 0x3f))
  131. + _fromCC(0x80 | (cc & 0x3f)));
  132. }
  133. };
  134. const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  135. /**
  136. * @deprecated should have been internal use only.
  137. * @param {string} src UTF-8 string
  138. * @returns {string} UTF-16 string
  139. */
  140. const utob = (u) => u.replace(re_utob, cb_utob);
  141. //
  142. const _encode = _hasBuffer
  143. ? (s) => Buffer.from(s, 'utf8').toString('base64')
  144. : _TE
  145. ? (s) => _fromUint8Array(_TE.encode(s))
  146. : (s) => _btoa(utob(s));
  147. /**
  148. * converts a UTF-8-encoded string to a Base64 string.
  149. * @param {boolean} [urlsafe] if `true` make the result URL-safe
  150. * @returns {string} Base64 string
  151. */
  152. const encode = (src, urlsafe = false) => urlsafe
  153. ? _mkUriSafe(_encode(src))
  154. : _encode(src);
  155. /**
  156. * converts a UTF-8-encoded string to URL-safe Base64 RFC4648 §5.
  157. * @returns {string} Base64 string
  158. */
  159. const encodeURI = (src) => encode(src, true);
  160. // This trick is found broken https://github.com/dankogai/js-base64/issues/130
  161. // const btou = (src: string) => decodeURIComponent(escape(src));
  162. // reverting good old fationed regexp
  163. const re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
  164. const cb_btou = (cccc) => {
  165. switch (cccc.length) {
  166. case 4:
  167. var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
  168. | ((0x3f & cccc.charCodeAt(1)) << 12)
  169. | ((0x3f & cccc.charCodeAt(2)) << 6)
  170. | (0x3f & cccc.charCodeAt(3)), offset = cp - 0x10000;
  171. return (_fromCC((offset >>> 10) + 0xD800)
  172. + _fromCC((offset & 0x3FF) + 0xDC00));
  173. case 3:
  174. return _fromCC(((0x0f & cccc.charCodeAt(0)) << 12)
  175. | ((0x3f & cccc.charCodeAt(1)) << 6)
  176. | (0x3f & cccc.charCodeAt(2)));
  177. default:
  178. return _fromCC(((0x1f & cccc.charCodeAt(0)) << 6)
  179. | (0x3f & cccc.charCodeAt(1)));
  180. }
  181. };
  182. /**
  183. * @deprecated should have been internal use only.
  184. * @param {string} src UTF-16 string
  185. * @returns {string} UTF-8 string
  186. */
  187. const btou = (b) => b.replace(re_btou, cb_btou);
  188. /**
  189. * polyfill version of `atob`
  190. */
  191. const atobPolyfill = (asc) => {
  192. // console.log('polyfilled');
  193. asc = asc.replace(/\s+/g, '');
  194. if (!b64re.test(asc))
  195. throw new TypeError('malformed base64.');
  196. asc += '=='.slice(2 - (asc.length & 3));
  197. let u24, bin = '', r1, r2;
  198. for (let i = 0; i < asc.length;) {
  199. u24 = b64tab[asc.charAt(i++)] << 18
  200. | b64tab[asc.charAt(i++)] << 12
  201. | (r1 = b64tab[asc.charAt(i++)]) << 6
  202. | (r2 = b64tab[asc.charAt(i++)]);
  203. bin += r1 === 64 ? _fromCC(u24 >> 16 & 255)
  204. : r2 === 64 ? _fromCC(u24 >> 16 & 255, u24 >> 8 & 255)
  205. : _fromCC(u24 >> 16 & 255, u24 >> 8 & 255, u24 & 255);
  206. }
  207. return bin;
  208. };
  209. /**
  210. * does what `window.atob` of web browsers do.
  211. * @param {String} asc Base64-encoded string
  212. * @returns {string} binary string
  213. */
  214. const _atob = _hasatob ? (asc) => atob(_tidyB64(asc))
  215. : _hasBuffer ? (asc) => Buffer.from(asc, 'base64').toString('binary')
  216. : atobPolyfill;
  217. //
  218. const _toUint8Array = _hasBuffer
  219. ? (a) => _U8Afrom(Buffer.from(a, 'base64'))
  220. : (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
  221. /**
  222. * converts a Base64 string to a Uint8Array.
  223. */
  224. const toUint8Array = (a) => _toUint8Array(_unURI(a));
  225. //
  226. const _decode = _hasBuffer
  227. ? (a) => Buffer.from(a, 'base64').toString('utf8')
  228. : _TD
  229. ? (a) => _TD.decode(_toUint8Array(a))
  230. : (a) => btou(_atob(a));
  231. const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/'));
  232. /**
  233. * converts a Base64 string to a UTF-8 string.
  234. * @param {String} src Base64 string. Both normal and URL-safe are supported
  235. * @returns {string} UTF-8 string
  236. */
  237. const decode = (src) => _decode(_unURI(src));
  238. /**
  239. * check if a value is a valid Base64 string
  240. * @param {String} src a value to check
  241. */
  242. const isValid = (src) => {
  243. if (typeof src !== 'string')
  244. return false;
  245. const s = src.replace(/\s+/g, '').replace(/=+$/, '');
  246. return !/[^\s0-9a-zA-Z\+/]/.test(s) || !/[^\s0-9a-zA-Z\-_]/.test(s);
  247. };
  248. //
  249. const _noEnum = (v) => {
  250. return {
  251. value: v, enumerable: false, writable: true, configurable: true
  252. };
  253. };
  254. /**
  255. * extend String.prototype with relevant methods
  256. */
  257. const extendString = function () {
  258. const _add = (name, body) => Object.defineProperty(String.prototype, name, _noEnum(body));
  259. _add('fromBase64', function () { return decode(this); });
  260. _add('toBase64', function (urlsafe) { return encode(this, urlsafe); });
  261. _add('toBase64URI', function () { return encode(this, true); });
  262. _add('toBase64URL', function () { return encode(this, true); });
  263. _add('toUint8Array', function () { return toUint8Array(this); });
  264. };
  265. /**
  266. * extend Uint8Array.prototype with relevant methods
  267. */
  268. const extendUint8Array = function () {
  269. const _add = (name, body) => Object.defineProperty(Uint8Array.prototype, name, _noEnum(body));
  270. _add('toBase64', function (urlsafe) { return fromUint8Array(this, urlsafe); });
  271. _add('toBase64URI', function () { return fromUint8Array(this, true); });
  272. _add('toBase64URL', function () { return fromUint8Array(this, true); });
  273. };
  274. /**
  275. * extend Builtin prototypes with relevant methods
  276. */
  277. const extendBuiltins = () => {
  278. extendString();
  279. extendUint8Array();
  280. };
  281. const gBase64 = {
  282. version: version,
  283. VERSION: VERSION,
  284. atob: _atob,
  285. atobPolyfill: atobPolyfill,
  286. btoa: _btoa,
  287. btoaPolyfill: btoaPolyfill,
  288. fromBase64: decode,
  289. toBase64: encode,
  290. encode: encode,
  291. encodeURI: encodeURI,
  292. encodeURL: encodeURI,
  293. utob: utob,
  294. btou: btou,
  295. decode: decode,
  296. isValid: isValid,
  297. fromUint8Array: fromUint8Array,
  298. toUint8Array: toUint8Array,
  299. extendString: extendString,
  300. extendUint8Array: extendUint8Array,
  301. extendBuiltins: extendBuiltins,
  302. };
  303. //
  304. // export Base64 to the namespace
  305. //
  306. // ES5 is yet to have Object.assign() that may make transpilers unhappy.
  307. // gBase64.Base64 = Object.assign({}, gBase64);
  308. gBase64.Base64 = {};
  309. Object.keys(gBase64).forEach(k => gBase64.Base64[k] = gBase64[k]);
  310. return gBase64;
  311. }));