js-base64 2.5.0 → 2.6.1
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 +22 -10
- package/base64.html +1 -1
- package/base64.js +31 -49
- package/bower.json +1 -1
- package/package.json +2 -2
- package/test/index.html +1 -1
- package/base64.min.js +0 -1
package/README.md
CHANGED
|
@@ -42,14 +42,26 @@ import { Base64 } from 'js-base64';
|
|
|
42
42
|
## SYNOPSIS
|
|
43
43
|
|
|
44
44
|
```javascript
|
|
45
|
-
Base64.encode('dankogai');
|
|
46
|
-
Base64.
|
|
47
|
-
Base64.
|
|
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.encode( '小飼弾'); // 5bCP6aO85by+
|
|
51
|
+
Base64.encodeURI('小飼弾'); // 5bCP6aO85by- which equals to Base64.encode('小飼弾', true)
|
|
52
|
+
Base64.btoa( '小飼弾'); // raises exception
|
|
53
|
+
```
|
|
48
54
|
|
|
55
|
+
```javascript
|
|
49
56
|
Base64.decode('ZGFua29nYWk='); // dankogai
|
|
57
|
+
Base64.atob( 'ZGFua29nYWk='); // dankogai
|
|
58
|
+
Base64.toUint8Array( // new Uint8Array([100,97,110,107,111,103,97,105])
|
|
59
|
+
'ZGFua29nYWk='
|
|
60
|
+
);
|
|
50
61
|
Base64.decode('5bCP6aO85by+'); // 小飼弾
|
|
51
62
|
// note .decodeURI() is unnecessary since it accepts both flavors
|
|
52
63
|
Base64.decode('5bCP6aO85by-'); // 小飼弾
|
|
64
|
+
Base64.atob( '5bCP6aO85by+'); // 'å°é£¼å¼¾' which is nonsense
|
|
53
65
|
```
|
|
54
66
|
|
|
55
67
|
### String Extension for ES5
|
|
@@ -59,13 +71,13 @@ if (Base64.extendString) {
|
|
|
59
71
|
// you have to explicitly extend String.prototype
|
|
60
72
|
Base64.extendString();
|
|
61
73
|
// once extended, you can do the following
|
|
62
|
-
'dankogai'.toBase64();
|
|
63
|
-
'小飼弾'.toBase64();
|
|
64
|
-
'小飼弾'.toBase64(true);
|
|
65
|
-
'小飼弾'.toBase64URI();
|
|
66
|
-
'ZGFua29nYWk='.fromBase64();
|
|
67
|
-
'5bCP6aO85by+'.fromBase64();
|
|
68
|
-
'5bCP6aO85by-'.fromBase64();
|
|
74
|
+
'dankogai'.toBase64(); // ZGFua29nYWk=
|
|
75
|
+
'小飼弾'.toBase64(); // 5bCP6aO85by+
|
|
76
|
+
'小飼弾'.toBase64(true); // 5bCP6aO85by-
|
|
77
|
+
'小飼弾'.toBase64URI(); // 5bCP6aO85by-
|
|
78
|
+
'ZGFua29nYWk='.fromBase64(); // dankogai
|
|
79
|
+
'5bCP6aO85by+'.fromBase64(); // 小飼弾
|
|
80
|
+
'5bCP6aO85by-'.fromBase64(); // 小飼弾
|
|
69
81
|
}
|
|
70
82
|
```
|
|
71
83
|
|
package/base64.html
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
</tr>
|
|
23
23
|
<tr><th width="50%">Roundtrip</th><th>iframe w/ data: (no IE)</th></tr>
|
|
24
24
|
<tr>
|
|
25
|
-
<th><textarea id="roundtrip" cols=32" rows="4" disabled></textarea></th>
|
|
25
|
+
<th><textarea id="roundtrip" cols="32" rows="4" disabled></textarea></th>
|
|
26
26
|
<th><iframe id="data" width="80%" height="64"></iframe></th>
|
|
27
27
|
</tr>
|
|
28
28
|
</tbody></table>
|
package/base64.js
CHANGED
|
@@ -20,17 +20,9 @@
|
|
|
20
20
|
), function(global) {
|
|
21
21
|
'use strict';
|
|
22
22
|
// existing version for noConflict()
|
|
23
|
+
global = global || {};
|
|
23
24
|
var _Base64 = global.Base64;
|
|
24
|
-
var version = "2.
|
|
25
|
-
// if node.js and NOT React Native, we use Buffer
|
|
26
|
-
var buffer;
|
|
27
|
-
if (typeof module !== 'undefined' && module.exports) {
|
|
28
|
-
try {
|
|
29
|
-
buffer = eval("require('buffer').Buffer");
|
|
30
|
-
} catch (err) {
|
|
31
|
-
buffer = undefined;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
25
|
+
var version = "2.6.1";
|
|
34
26
|
// constants
|
|
35
27
|
var b64chars
|
|
36
28
|
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
@@ -48,8 +40,8 @@
|
|
|
48
40
|
: cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))
|
|
49
41
|
+ fromCharCode(0x80 | (cc & 0x3f)))
|
|
50
42
|
: (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
+ fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
|
|
44
|
+
+ fromCharCode(0x80 | ( cc & 0x3f)));
|
|
53
45
|
} else {
|
|
54
46
|
var cc = 0x10000
|
|
55
47
|
+ (c.charCodeAt(0) - 0xD800) * 0x400
|
|
@@ -77,23 +69,16 @@
|
|
|
77
69
|
];
|
|
78
70
|
return chars.join('');
|
|
79
71
|
};
|
|
80
|
-
var btoa = global.btoa
|
|
81
|
-
return global.btoa(b)
|
|
82
|
-
|
|
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
|
+
);
|
|
83
77
|
return b.replace(/[\s\S]{1,3}/g, cb_encode);
|
|
84
78
|
};
|
|
85
|
-
var _encode =
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return (u.constructor === buffer.constructor ? u : buffer.from(u))
|
|
89
|
-
.toString('base64')
|
|
90
|
-
}
|
|
91
|
-
: function (u) {
|
|
92
|
-
return (u.constructor === buffer.constructor ? u : new buffer(u))
|
|
93
|
-
.toString('base64')
|
|
94
|
-
}
|
|
95
|
-
: function (u) { return btoa(utob(u)) }
|
|
96
|
-
;
|
|
79
|
+
var _encode = function(u) {
|
|
80
|
+
return btoa(utob(String(u)));
|
|
81
|
+
};
|
|
97
82
|
var encode = function(u, urisafe) {
|
|
98
83
|
return !urisafe
|
|
99
84
|
? _encode(String(u))
|
|
@@ -102,12 +87,13 @@
|
|
|
102
87
|
}).replace(/=/g, '');
|
|
103
88
|
};
|
|
104
89
|
var encodeURI = function(u) { return encode(u, true) };
|
|
90
|
+
var fromUint8Array = function(a) {
|
|
91
|
+
return btoa(Array.from(a, function(c) {
|
|
92
|
+
return String.fromCharCode(c)
|
|
93
|
+
}).join(''));
|
|
94
|
+
};
|
|
105
95
|
// decoder stuff
|
|
106
|
-
var re_btou =
|
|
107
|
-
'[\xC0-\xDF][\x80-\xBF]',
|
|
108
|
-
'[\xE0-\xEF][\x80-\xBF]{2}',
|
|
109
|
-
'[\xF0-\xF7][\x80-\xBF]{3}'
|
|
110
|
-
].join('|'), 'g');
|
|
96
|
+
var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
|
|
111
97
|
var cb_btou = function(cccc) {
|
|
112
98
|
switch(cccc.length) {
|
|
113
99
|
case 4:
|
|
@@ -149,31 +135,26 @@
|
|
|
149
135
|
chars.length -= [0, 0, 2, 1][padlen];
|
|
150
136
|
return chars.join('');
|
|
151
137
|
};
|
|
152
|
-
var _atob = global.atob
|
|
153
|
-
return global.atob(a)
|
|
154
|
-
} : function(a){
|
|
138
|
+
var _atob = global.atob && typeof global.atob == 'function'
|
|
139
|
+
? function(a){ return global.atob(a) } : function(a){
|
|
155
140
|
return a.replace(/\S{1,4}/g, cb_decode);
|
|
156
141
|
};
|
|
157
142
|
var atob = function(a) {
|
|
158
143
|
return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g, ''));
|
|
159
144
|
};
|
|
160
|
-
var _decode =
|
|
161
|
-
buffer.from && Uint8Array && buffer.from !== Uint8Array.from
|
|
162
|
-
? function(a) {
|
|
163
|
-
return (a.constructor === buffer.constructor
|
|
164
|
-
? a : buffer.from(a, 'base64')).toString();
|
|
165
|
-
}
|
|
166
|
-
: function(a) {
|
|
167
|
-
return (a.constructor === buffer.constructor
|
|
168
|
-
? a : new buffer(a, 'base64')).toString();
|
|
169
|
-
}
|
|
170
|
-
: function(a) { return btou(_atob(a)) };
|
|
145
|
+
var _decode = function(a) { return btou(_atob(a)) };
|
|
171
146
|
var decode = function(a){
|
|
172
147
|
return _decode(
|
|
173
|
-
String(a).replace(/[-_]/g, function(m0) {
|
|
174
|
-
|
|
148
|
+
String(a).replace(/[-_]/g, function(m0) {
|
|
149
|
+
return m0 == '-' ? '+' : '/'
|
|
150
|
+
}).replace(/[^A-Za-z0-9\+\/]/g, '')
|
|
175
151
|
);
|
|
176
152
|
};
|
|
153
|
+
var toUint8Array = function(a) {
|
|
154
|
+
return Uint8Array.from(atob(a), function(c) {
|
|
155
|
+
return c.charCodeAt(0);
|
|
156
|
+
});
|
|
157
|
+
};
|
|
177
158
|
var noConflict = function() {
|
|
178
159
|
var Base64 = global.Base64;
|
|
179
160
|
global.Base64 = _Base64;
|
|
@@ -192,7 +173,8 @@
|
|
|
192
173
|
btou: btou,
|
|
193
174
|
decode: decode,
|
|
194
175
|
noConflict: noConflict,
|
|
195
|
-
|
|
176
|
+
fromUint8Array: fromUint8Array,
|
|
177
|
+
toUint8Array: toUint8Array
|
|
196
178
|
};
|
|
197
179
|
// if ES5 is available, make Base64.extendString() available
|
|
198
180
|
if (typeof Object.defineProperty === 'function') {
|
package/bower.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "js-base64",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.1",
|
|
4
4
|
"description": "Yet another Base64 transcoder in pure-JS",
|
|
5
5
|
"main": "base64.js",
|
|
6
6
|
"directories": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"babel-preset-env": "^1.7.0",
|
|
14
14
|
"babel-register": "^6.26.0",
|
|
15
|
-
"mocha": "
|
|
15
|
+
"mocha": "5.x.x"
|
|
16
16
|
},
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
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/
|
|
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";var _Base64=global.Base64;var version="2.5.0";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=buffer?buffer.from&&Uint8Array&&buffer.from!==Uint8Array.from?function(u){return(u.constructor===buffer.constructor?u:buffer.from(u)).toString("base64")}:function(u){return(u.constructor===buffer.constructor?u:new buffer(u)).toString("base64")}:function(u){return btoa(utob(u))};var encode=function(u,urisafe){return!urisafe?_encode(String(u)):_encode(String(u)).replace(/[+\/]/g,function(m0){return m0=="+"?"-":"_"}).replace(/=/g,"")};var encodeURI=function(u){return encode(u,true)};var re_btou=new RegExp(["[À-ß][-¿]","[à-ï][-¿]{2}","[ð-÷][-¿]{3}"].join("|"),"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}});
|