| 1 | 'use strict';
|
|---|
| 2 | var $ = require('../internals/export');
|
|---|
| 3 | var globalThis = require('../internals/global-this');
|
|---|
| 4 | var getBuiltIn = require('../internals/get-built-in');
|
|---|
| 5 | var uncurryThis = require('../internals/function-uncurry-this');
|
|---|
| 6 | var call = require('../internals/function-call');
|
|---|
| 7 | var fails = require('../internals/fails');
|
|---|
| 8 | var toString = require('../internals/to-string');
|
|---|
| 9 | var validateArgumentsLength = require('../internals/validate-arguments-length');
|
|---|
| 10 | var i2c = require('../internals/base64-map').i2c;
|
|---|
| 11 |
|
|---|
| 12 | var $btoa = getBuiltIn('btoa');
|
|---|
| 13 | var $Array = Array;
|
|---|
| 14 | var join = uncurryThis([].join);
|
|---|
| 15 | var charAt = uncurryThis(''.charAt);
|
|---|
| 16 | var charCodeAt = uncurryThis(''.charCodeAt);
|
|---|
| 17 |
|
|---|
| 18 | var BASIC = !!$btoa && !fails(function () {
|
|---|
| 19 | return $btoa('hi') !== 'aGk=';
|
|---|
| 20 | });
|
|---|
| 21 |
|
|---|
| 22 | var NO_ARG_RECEIVING_CHECK = BASIC && !fails(function () {
|
|---|
| 23 | $btoa();
|
|---|
| 24 | });
|
|---|
| 25 |
|
|---|
| 26 | var WRONG_ARG_CONVERSION = BASIC && fails(function () {
|
|---|
| 27 | return $btoa(null) !== 'bnVsbA==';
|
|---|
| 28 | });
|
|---|
| 29 |
|
|---|
| 30 | var WRONG_ARITY = BASIC && $btoa.length !== 1;
|
|---|
| 31 |
|
|---|
| 32 | // `btoa` method
|
|---|
| 33 | // https://html.spec.whatwg.org/multipage/webappapis.html#dom-btoa
|
|---|
| 34 | $({ global: true, bind: true, enumerable: true, forced: !BASIC || NO_ARG_RECEIVING_CHECK || WRONG_ARG_CONVERSION || WRONG_ARITY }, {
|
|---|
| 35 | btoa: function btoa(data) {
|
|---|
| 36 | validateArgumentsLength(arguments.length, 1);
|
|---|
| 37 | // `webpack` dev server bug on IE global methods - use call(fn, global, ...)
|
|---|
| 38 | if (BASIC) return call($btoa, globalThis, toString(data));
|
|---|
| 39 | var string = toString(data);
|
|---|
| 40 | // (string.length + 2) / 3) and then truncating to integer
|
|---|
| 41 | // does the ceil automatically. << 2 will truncate the integer
|
|---|
| 42 | // while also doing *4. ceil(length / 3) quanta, 4 bytes output
|
|---|
| 43 | // per quanta for base64.
|
|---|
| 44 | var output = new $Array((string.length + 2) / 3 << 2);
|
|---|
| 45 | var outputIndex = 0;
|
|---|
| 46 | var position = 0;
|
|---|
| 47 | var map = i2c;
|
|---|
| 48 | var block, charCode;
|
|---|
| 49 | while (charAt(string, position) || (map = '=', position % 1)) {
|
|---|
| 50 | charCode = charCodeAt(string, position += 3 / 4);
|
|---|
| 51 | if (charCode > 0xFF) {
|
|---|
| 52 | throw new (getBuiltIn('DOMException'))('The string contains characters outside of the Latin1 range', 'InvalidCharacterError');
|
|---|
| 53 | }
|
|---|
| 54 | block = block << 8 | charCode;
|
|---|
| 55 | output[outputIndex++] = charAt(map, 63 & block >> 8 - position % 1 * 8);
|
|---|
| 56 | } return join(output, '');
|
|---|
| 57 | }
|
|---|
| 58 | });
|
|---|