js-base64 2.5.2 → 2.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -42,14 +42,29 @@ import { Base64 } from 'js-base64';
42
42
  ## SYNOPSIS
43
43
 
44
44
  ```javascript
45
- Base64.encode('dankogai'); // ZGFua29nYWk=
46
- Base64.encode('小飼弾'); // 5bCP6aO85by+
47
- Base64.encodeURI('小飼弾'); // 5bCP6aO85by-
45
+ Base64.encode('dankogai'); // ZGFua29nYWk=
46
+ Base64.btoa( 'dankogai'); // ZGFua29nYWk=
47
+ Base64.fromUint8Array( // ZGFua29nYWk=
48
+ new Uint8Array([100,97,110,107,111,103,97,105])
49
+ );
50
+ Base64.fromUint8Array( // ZGFua29nYW which is URI safe
51
+ new Uint8Array([100,97,110,107,111,103,97,105]), true
52
+ );
53
+ Base64.encode( '小飼弾'); // 5bCP6aO85by+
54
+ Base64.encodeURI('小飼弾'); // 5bCP6aO85by- which equals to Base64.encode('小飼弾', true)
55
+ Base64.btoa( '小飼弾'); // raises exception
56
+ ```
48
57
 
58
+ ```javascript
49
59
  Base64.decode('ZGFua29nYWk='); // dankogai
60
+ Base64.atob( 'ZGFua29nYWk='); // dankogai
61
+ Base64.toUint8Array( // new Uint8Array([100,97,110,107,111,103,97,105])
62
+ 'ZGFua29nYWk='
63
+ );
50
64
  Base64.decode('5bCP6aO85by+'); // 小飼弾
51
65
  // note .decodeURI() is unnecessary since it accepts both flavors
52
66
  Base64.decode('5bCP6aO85by-'); // 小飼弾
67
+ Base64.atob( '5bCP6aO85by+'); // '小飼弾' which is nonsense
53
68
  ```
54
69
 
55
70
  ### String Extension for ES5
@@ -59,13 +74,13 @@ if (Base64.extendString) {
59
74
  // you have to explicitly extend String.prototype
60
75
  Base64.extendString();
61
76
  // once extended, you can do the following
62
- 'dankogai'.toBase64(); // ZGFua29nYWk=
63
- '小飼弾'.toBase64(); // 5bCP6aO85by+
64
- '小飼弾'.toBase64(true); // 5bCP6aO85by-
65
- '小飼弾'.toBase64URI(); // 5bCP6aO85by-
66
- 'ZGFua29nYWk='.fromBase64(); // dankogai
67
- '5bCP6aO85by+'.fromBase64(); // 小飼弾
68
- '5bCP6aO85by-'.fromBase64(); // 小飼弾
77
+ 'dankogai'.toBase64(); // ZGFua29nYWk=
78
+ '小飼弾'.toBase64(); // 5bCP6aO85by+
79
+ '小飼弾'.toBase64(true); // 5bCP6aO85by-
80
+ '小飼弾'.toBase64URI(); // 5bCP6aO85by-
81
+ 'ZGFua29nYWk='.fromBase64(); // dankogai
82
+ '5bCP6aO85by+'.fromBase64(); // 小飼弾
83
+ '5bCP6aO85by-'.fromBase64(); // 小飼弾
69
84
  }
70
85
  ```
71
86
 
package/base64.js CHANGED
@@ -22,16 +22,7 @@
22
22
  // existing version for noConflict()
23
23
  global = global || {};
24
24
  var _Base64 = global.Base64;
25
- var version = "2.5.2";
26
- // if node.js and NOT React Native, we use Buffer
27
- var buffer;
28
- if (typeof module !== 'undefined' && module.exports) {
29
- try {
30
- buffer = eval("require('buffer').Buffer");
31
- } catch (err) {
32
- buffer = undefined;
33
- }
34
- }
25
+ var version = "2.6.3";
35
26
  // constants
36
27
  var b64chars
37
28
  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
@@ -78,24 +69,41 @@
78
69
  ];
79
70
  return chars.join('');
80
71
  };
81
- var btoa = global.btoa ? function(b) {
82
- return global.btoa(b);
83
- } : function(b) {
72
+ var btoa = global.btoa && typeof global.btoa == 'function'
73
+ ? function(b){ return global.btoa(b) } : function(b) {
74
+ if (b.match(/[^\x00-\xFF]/)) throw new RangeError(
75
+ 'The string contains invalid characters.'
76
+ );
84
77
  return b.replace(/[\s\S]{1,3}/g, cb_encode);
85
78
  };
86
79
  var _encode = function(u) {
87
- var isUint8Array = Object.prototype.toString.call(u) === '[object Uint8Array]';
88
- return isUint8Array ? u.toString('base64')
89
- : btoa(utob(String(u)));
90
- }
80
+ return btoa(utob(String(u)));
81
+ };
82
+ var mkUriSafe = function (b64) {
83
+ return b64.replace(/[+\/]/g, function(m0) {
84
+ return m0 == '+' ? '-' : '_';
85
+ }).replace(/=/g, '');
86
+ };
91
87
  var encode = function(u, urisafe) {
92
- return !urisafe
93
- ? _encode(u)
94
- : _encode(String(u)).replace(/[+\/]/g, function(m0) {
95
- return m0 == '+' ? '-' : '_';
96
- }).replace(/=/g, '');
88
+ return urisafe ? mkUriSafe(_encode(u)) : _encode(u);
97
89
  };
98
90
  var encodeURI = function(u) { return encode(u, true) };
91
+ var fromUint8Array;
92
+ if (global.Uint8Array) fromUint8Array = function(a, urisafe) {
93
+ // return btoa(fromCharCode.apply(null, a));
94
+ var b64 = '';
95
+ for (var i = 0, l = a.length; i < l; i += 3) {
96
+ var a0 = a[i], a1 = a[i+1], a2 = a[i+2];
97
+ var ord = a0 << 16 | a1 << 8 | a2;
98
+ b64 += b64chars.charAt( ord >>> 18)
99
+ + b64chars.charAt((ord >>> 12) & 63)
100
+ + ( typeof a1 != 'undefined'
101
+ ? b64chars.charAt((ord >>> 6) & 63) : '=')
102
+ + ( typeof a2 != 'undefined'
103
+ ? b64chars.charAt( ord & 63) : '=');
104
+ }
105
+ return urisafe ? mkUriSafe(b64) : b64;
106
+ };
99
107
  // decoder stuff
100
108
  var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
101
109
  var cb_btou = function(cccc) {
@@ -139,30 +147,27 @@
139
147
  chars.length -= [0, 0, 2, 1][padlen];
140
148
  return chars.join('');
141
149
  };
142
- var _atob = global.atob ? function(a) {
143
- return global.atob(a);
144
- } : function(a){
150
+ var _atob = global.atob && typeof global.atob == 'function'
151
+ ? function(a){ return global.atob(a) } : function(a){
145
152
  return a.replace(/\S{1,4}/g, cb_decode);
146
153
  };
147
154
  var atob = function(a) {
148
155
  return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g, ''));
149
156
  };
150
- var _decode = buffer ?
151
- buffer.from && Uint8Array && buffer.from !== Uint8Array.from
152
- ? function(a) {
153
- return (a.constructor === buffer.constructor
154
- ? a : buffer.from(a, 'base64')).toString();
155
- }
156
- : function(a) {
157
- return (a.constructor === buffer.constructor
158
- ? a : new buffer(a, 'base64')).toString();
159
- }
160
- : function(a) { return btou(_atob(a)) };
157
+ var _decode = function(a) { return btou(_atob(a)) };
158
+ var _fromURI = function(a) {
159
+ return String(a).replace(/[-_]/g, function(m0) {
160
+ return m0 == '-' ? '+' : '/'
161
+ }).replace(/[^A-Za-z0-9\+\/]/g, '');
162
+ };
161
163
  var decode = function(a){
162
- return _decode(
163
- String(a).replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
164
- .replace(/[^A-Za-z0-9\+\/]/g, '')
165
- );
164
+ return _decode(_fromURI(a));
165
+ };
166
+ var toUint8Array;
167
+ if (global.Uint8Array) toUint8Array = function(a) {
168
+ return Uint8Array.from(atob(_fromURI(a)), function(c) {
169
+ return c.charCodeAt(0);
170
+ });
166
171
  };
167
172
  var noConflict = function() {
168
173
  var Base64 = global.Base64;
@@ -182,7 +187,8 @@
182
187
  btou: btou,
183
188
  decode: decode,
184
189
  noConflict: noConflict,
185
- __buffer__: buffer
190
+ fromUint8Array: fromUint8Array,
191
+ toUint8Array: toUint8Array
186
192
  };
187
193
  // if ES5 is available, make Base64.extendString() available
188
194
  if (typeof Object.defineProperty === 'function') {
@@ -222,4 +228,3 @@
222
228
  // that's it!
223
229
  return {Base64: global.Base64}
224
230
  }));
225
-
package/bower.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-base64",
3
- "version": "2.5.2",
3
+ "version": "2.6.3",
4
4
  "license": "BSD-3-Clause",
5
5
  "main": [
6
6
  "./base64.js"
package/package.js CHANGED
@@ -6,4 +6,4 @@ Package.on_use(function(api){
6
6
  api.export('Base64');
7
7
 
8
8
  api.add_files(['base64.js'], 'server');
9
- });
9
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-base64",
3
- "version": "2.5.2",
3
+ "version": "2.6.3",
4
4
  "description": "Yet another Base64 transcoder in pure-JS",
5
5
  "main": "base64.js",
6
6
  "directories": {
package/test/dankogai.js CHANGED
@@ -42,3 +42,27 @@ describe('Base64', function () {
42
42
  it('.decode', is(Base64.decode('5bCP6aO85by+'), '小飼弾'));
43
43
  it('.decode', is(Base64.decode('5bCP6aO85by-'), '小飼弾'));
44
44
  });
45
+
46
+ if (typeof Uint8Array === 'function') describe('fromUint8Array', function() {
47
+ it('dankogai', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107,111,103,97,105])), Base64.encode('dankogai')));
48
+ it('dankoga', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107,111,103,97])), Base64.encode('dankoga')));
49
+ it('dankog', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107,111,103])), Base64.encode('dankog')));
50
+ it('danko', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107,111])), Base64.encode('danko')));
51
+ it('dank', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107])), Base64.encode('dank')));
52
+ it('dan', is(Base64.fromUint8Array(new Uint8Array([100,97,110])), Base64.encode('dan')));
53
+ it('da', is(Base64.fromUint8Array(new Uint8Array([100,97])), Base64.encode('da')));
54
+ it('d', is(Base64.fromUint8Array(new Uint8Array([100])), Base64.encode('d')));
55
+ it('', is(Base64.fromUint8Array(new Uint8Array([])), Base64.encode('')));
56
+ });
57
+
58
+ if (typeof Uint8Array === 'function') describe('toUint8Array', function() {
59
+ it('ZGFua29nYWk=', is(Base64.toUint8Array('ZGFua29nYWk=').toString(), '100,97,110,107,111,103,97,105'));
60
+ it('ZGFua29nYQ==', is(Base64.toUint8Array('ZGFua29nYQ==').toString(), '100,97,110,107,111,103,97'));
61
+ it('ZGFua29n', is(Base64.toUint8Array('ZGFua29n').toString(), '100,97,110,107,111,103'));
62
+ it('ZGFua28=', is(Base64.toUint8Array('ZGFua28=').toString(), '100,97,110,107,111'));
63
+ it('ZGFuaw==', is(Base64.toUint8Array('ZGFuaw==').toString(), '100,97,110,107'));
64
+ it('ZGFu', is(Base64.toUint8Array('ZGFu').toString(), '100,97,110'));
65
+ it('ZGE=', is(Base64.toUint8Array('ZGE=').toString(), '100,97'));
66
+ it('ZA==', is(Base64.toUint8Array('ZA==').toString(), '100'));
67
+ it('', is(Base64.toUint8Array('').toString(), ''));
68
+ });
package/test/index.html CHANGED
@@ -7,7 +7,7 @@
7
7
  <body>
8
8
  <div id="mocha"></div>
9
9
 
10
- <script src="https://cdn.rawgit.com/jquery/jquery/3.4.0/dist/jquery.min.js"></script>
10
+ <script src="https://cdn.rawgit.com/jquery/jquery/3.5.1/dist/jquery.min.js"></script>
11
11
  <script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
12
12
  <script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
13
13
  <script>
package/base64.min.js DELETED
@@ -1 +0,0 @@
1
- (function(global,factory){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:this,function(global){"use strict";global=global||{};var _Base64=global.Base64;var version="2.5.2";var buffer;if(typeof module!=="undefined"&&module.exports){try{buffer=eval("require('buffer').Buffer")}catch(err){buffer=undefined}}var b64chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var b64tab=function(bin){var t={};for(var i=0,l=bin.length;i<l;i++)t[bin.charAt(i)]=i;return t}(b64chars);var fromCharCode=String.fromCharCode;var cb_utob=function(c){if(c.length<2){var cc=c.charCodeAt(0);return cc<128?c:cc<2048?fromCharCode(192|cc>>>6)+fromCharCode(128|cc&63):fromCharCode(224|cc>>>12&15)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}else{var cc=65536+(c.charCodeAt(0)-55296)*1024+(c.charCodeAt(1)-56320);return fromCharCode(240|cc>>>18&7)+fromCharCode(128|cc>>>12&63)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}};var re_utob=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;var utob=function(u){return u.replace(re_utob,cb_utob)};var cb_encode=function(ccc){var padlen=[0,2,1][ccc.length%3],ord=ccc.charCodeAt(0)<<16|(ccc.length>1?ccc.charCodeAt(1):0)<<8|(ccc.length>2?ccc.charCodeAt(2):0),chars=[b64chars.charAt(ord>>>18),b64chars.charAt(ord>>>12&63),padlen>=2?"=":b64chars.charAt(ord>>>6&63),padlen>=1?"=":b64chars.charAt(ord&63)];return chars.join("")};var btoa=global.btoa?function(b){return global.btoa(b)}:function(b){return b.replace(/[\s\S]{1,3}/g,cb_encode)};var _encode=function(u){var isUint8Array=Object.prototype.toString.call(u)==="[object Uint8Array]";return isUint8Array?u.toString("base64"):btoa(utob(String(u)))};var encode=function(u,urisafe){return!urisafe?_encode(u):_encode(String(u)).replace(/[+\/]/g,function(m0){return m0=="+"?"-":"_"}).replace(/=/g,"")};var encodeURI=function(u){return encode(u,true)};var re_btou=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;var cb_btou=function(cccc){switch(cccc.length){case 4:var cp=(7&cccc.charCodeAt(0))<<18|(63&cccc.charCodeAt(1))<<12|(63&cccc.charCodeAt(2))<<6|63&cccc.charCodeAt(3),offset=cp-65536;return fromCharCode((offset>>>10)+55296)+fromCharCode((offset&1023)+56320);case 3:return fromCharCode((15&cccc.charCodeAt(0))<<12|(63&cccc.charCodeAt(1))<<6|63&cccc.charCodeAt(2));default:return fromCharCode((31&cccc.charCodeAt(0))<<6|63&cccc.charCodeAt(1))}};var btou=function(b){return b.replace(re_btou,cb_btou)};var cb_decode=function(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&255),fromCharCode(n&255)];chars.length-=[0,0,2,1][padlen];return chars.join("")};var _atob=global.atob?function(a){return global.atob(a)}:function(a){return a.replace(/\S{1,4}/g,cb_decode)};var atob=function(a){return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g,""))};var _decode=buffer?buffer.from&&Uint8Array&&buffer.from!==Uint8Array.from?function(a){return(a.constructor===buffer.constructor?a:buffer.from(a,"base64")).toString()}:function(a){return(a.constructor===buffer.constructor?a:new buffer(a,"base64")).toString()}:function(a){return btou(_atob(a))};var decode=function(a){return _decode(String(a).replace(/[-_]/g,function(m0){return m0=="-"?"+":"/"}).replace(/[^A-Za-z0-9\+\/]/g,""))};var noConflict=function(){var Base64=global.Base64;global.Base64=_Base64;return Base64};global.Base64={VERSION:version,atob:atob,btoa:btoa,fromBase64:decode,toBase64:encode,utob:utob,encode:encode,encodeURI:encodeURI,btou:btou,decode:decode,noConflict:noConflict,__buffer__:buffer};if(typeof Object.defineProperty==="function"){var noEnum=function(v){return{value:v,enumerable:false,writable:true,configurable:true}};global.Base64.extendString=function(){Object.defineProperty(String.prototype,"fromBase64",noEnum(function(){return decode(this)}));Object.defineProperty(String.prototype,"toBase64",noEnum(function(urisafe){return encode(this,urisafe)}));Object.defineProperty(String.prototype,"toBase64URI",noEnum(function(){return encode(this,true)}))}}if(global["Meteor"]){Base64=global.Base64}if(typeof module!=="undefined"&&module.exports){module.exports.Base64=global.Base64}else if(typeof define==="function"&&define.amd){define([],function(){return global.Base64})}return{Base64:global.Base64}});