Skip to content

Commit

Permalink
make use of Text{En,De}coder where available
Browse files Browse the repository at this point in the history
  • Loading branch information
dankogai committed Sep 12, 2020
1 parent 6ab5b3d commit e66f5b9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 44 deletions.
18 changes: 13 additions & 5 deletions base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const VERSION = version;
const _hasatob = typeof atob === 'function';
const _hasbtoa = typeof btoa === 'function';
const _hasBuffer = typeof Buffer === 'function';
const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const b64chs = [...b64ch];
const b64tab = ((a) => {
Expand Down Expand Up @@ -142,7 +144,8 @@ const utob = (u) => u.replace(re_utob, cb_utob);
//
const _encode = _hasBuffer
? (s) => Buffer.from(s, 'utf8').toString('base64')
: (s) => _btoa(utob(s));
: _TE ? (s) => _fromUint8Array(_TE.encode(s))
: (s) => _btoa(utob(s));
/**
* converts a UTF-8-encoded string to a Base64 string.
* @param {boolean} [urlsafe] if `true` make the result URL-safe
Expand Down Expand Up @@ -215,20 +218,25 @@ const _atob = _hasatob ? (asc) => atob(_tidyB64(asc))
: atobPolyfill;
const _decode = _hasBuffer
? (a) => Buffer.from(a, 'base64').toString('utf8')
: (a) => btou(_atob(a));
: _TD
? (a) => _TD.decode(_toUint8Array(a))
: (a) => btou(_atob(a));
const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/'));
/**
* converts a Base64 string to a UTF-8 string.
* @param {String} src Base64 string. Both normal and URL-safe are supported
* @returns {string} UTF-8 string
*/
const decode = (src) => _decode(_unURI(src));
//
const _toUint8Array = _hasBuffer
? (a) => _U8Afrom(Buffer.from(a, 'base64'))
: (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = _hasBuffer
? (a) => _U8Afrom(Buffer.from(_unURI(a), 'base64'))
: (a) => _U8Afrom(_atob(_unURI(a)), c => c.charCodeAt(0));
const toUint8Array = (a) => _toUint8Array(_unURI(a));
//
const _noEnum = (v) => {
return {
value: v, enumerable: false, writable: true, configurable: true
Expand Down
18 changes: 13 additions & 5 deletions base64.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const VERSION = version;
const _hasatob = typeof atob === 'function';
const _hasbtoa = typeof btoa === 'function';
const _hasBuffer = typeof Buffer === 'function';
const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const b64chs = [...b64ch];
const b64tab = ((a) => {
Expand Down Expand Up @@ -111,7 +113,8 @@ const utob = (u) => u.replace(re_utob, cb_utob);
//
const _encode = _hasBuffer
? (s) => Buffer.from(s, 'utf8').toString('base64')
: (s) => _btoa(utob(s));
: _TE ? (s) => _fromUint8Array(_TE.encode(s))
: (s) => _btoa(utob(s));
/**
* converts a UTF-8-encoded string to a Base64 string.
* @param {boolean} [urlsafe] if `true` make the result URL-safe
Expand Down Expand Up @@ -184,20 +187,25 @@ const _atob = _hasatob ? (asc) => atob(_tidyB64(asc))
: atobPolyfill;
const _decode = _hasBuffer
? (a) => Buffer.from(a, 'base64').toString('utf8')
: (a) => btou(_atob(a));
: _TD
? (a) => _TD.decode(_toUint8Array(a))
: (a) => btou(_atob(a));
const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/'));
/**
* converts a Base64 string to a UTF-8 string.
* @param {String} src Base64 string. Both normal and URL-safe are supported
* @returns {string} UTF-8 string
*/
const decode = (src) => _decode(_unURI(src));
//
const _toUint8Array = _hasBuffer
? (a) => _U8Afrom(Buffer.from(a, 'base64'))
: (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = _hasBuffer
? (a) => _U8Afrom(Buffer.from(_unURI(a), 'base64'))
: (a) => _U8Afrom(_atob(_unURI(a)), c => c.charCodeAt(0));
const toUint8Array = (a) => _toUint8Array(_unURI(a));
//
const _noEnum = (v) => {
return {
value: v, enumerable: false, writable: true, configurable: true
Expand Down
76 changes: 42 additions & 34 deletions base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const VERSION = version;
const _hasatob = typeof atob === 'function';
const _hasbtoa = typeof btoa === 'function';
const _hasBuffer = typeof Buffer === 'function';
const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
const b64ch =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const b64chs = [...b64ch];
Expand Down Expand Up @@ -85,23 +87,23 @@ const fromUint8Array = (u8a: Uint8Array, urlsafe = false) =>
// This trick is found broken https://github.com/dankogai/js-base64/issues/130
// const utob = (src: string) => unescape(encodeURIComponent(src));
// reverting good old fationed regexp
const cb_utob = (c:string) => {
const cb_utob = (c: string) => {
if (c.length < 2) {
var cc = c.charCodeAt(0);
return cc < 0x80 ? c
: cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
+ _fromCC(0x80 | (cc & 0x3f)))
: (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
+ _fromCC(0x80 | ((cc >>> 6) & 0x3f))
+ _fromCC(0x80 | ( cc & 0x3f)));
+ _fromCC(0x80 | (cc & 0x3f)))
: (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
+ _fromCC(0x80 | ((cc >>> 6) & 0x3f))
+ _fromCC(0x80 | (cc & 0x3f)));
} else {
var cc = 0x10000
+ (c.charCodeAt(0) - 0xD800) * 0x400
+ (c.charCodeAt(1) - 0xDC00);
return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
+ _fromCC(0x80 | ((cc >>> 12) & 0x3f))
+ _fromCC(0x80 | ((cc >>> 6) & 0x3f))
+ _fromCC(0x80 | ( cc & 0x3f)));
+ _fromCC(0x80 | ((cc >>> 12) & 0x3f))
+ _fromCC(0x80 | ((cc >>> 6) & 0x3f))
+ _fromCC(0x80 | (cc & 0x3f)));
}
};
const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
Expand All @@ -110,11 +112,12 @@ const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
* @param {string} src UTF-8 string
* @returns {string} UTF-16 string
*/
const utob =(u:string) => u.replace(re_utob, cb_utob);
const utob = (u: string) => u.replace(re_utob, cb_utob);
//
const _encode = _hasBuffer
? (s: string) => Buffer.from(s, 'utf8').toString('base64')
: (s: string) => _btoa(utob(s));
: _TE ? (s: string) => _fromUint8Array(_TE.encode(s))
: (s: string) => _btoa(utob(s));
/**
* converts a UTF-8-encoded string to a Base64 string.
* @param {boolean} [urlsafe] if `true` make the result URL-safe
Expand All @@ -132,35 +135,35 @@ const encodeURI = (src: string) => encode(src, true);
// const btou = (src: string) => decodeURIComponent(escape(src));
// reverting good old fationed regexp
const re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
const cb_btou = (cccc:string) => {
switch(cccc.length) {
case 4:
var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
| ((0x3f & cccc.charCodeAt(1)) << 12)
| ((0x3f & cccc.charCodeAt(2)) << 6)
| (0x3f & cccc.charCodeAt(3)),
offset = cp - 0x10000;
return (_fromCC((offset >>> 10) + 0xD800)
const cb_btou = (cccc: string) => {
switch (cccc.length) {
case 4:
var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
| ((0x3f & cccc.charCodeAt(1)) << 12)
| ((0x3f & cccc.charCodeAt(2)) << 6)
| (0x3f & cccc.charCodeAt(3)),
offset = cp - 0x10000;
return (_fromCC((offset >>> 10) + 0xD800)
+ _fromCC((offset & 0x3FF) + 0xDC00));
case 3:
return _fromCC(
((0x0f & cccc.charCodeAt(0)) << 12)
case 3:
return _fromCC(
((0x0f & cccc.charCodeAt(0)) << 12)
| ((0x3f & cccc.charCodeAt(1)) << 6)
| (0x3f & cccc.charCodeAt(2))
);
default:
return _fromCC(
((0x1f & cccc.charCodeAt(0)) << 6)
| (0x3f & cccc.charCodeAt(1))
);
| (0x3f & cccc.charCodeAt(2))
);
default:
return _fromCC(
((0x1f & cccc.charCodeAt(0)) << 6)
| (0x3f & cccc.charCodeAt(1))
);
}
};
/**
* @deprecated should have been internal use only.
* @param {string} src UTF-16 string
* @returns {string} UTF-8 string
*/
const btou = (b:string) => b.replace(re_btou, cb_btou);
const btou = (b: string) => b.replace(re_btou, cb_btou);
/**
* polyfill version of `atob`
*/
Expand Down Expand Up @@ -191,7 +194,9 @@ const _atob = _hasatob ? (asc: string) => atob(_tidyB64(asc))
: atobPolyfill;
const _decode = _hasBuffer
? (a: string) => Buffer.from(a, 'base64').toString('utf8')
: (a: string) => btou(_atob(a));
: _TD
? (a: string) => _TD.decode(_toUint8Array(a))
: (a: string) => btou(_atob(a));
const _unURI = (a: string) =>
_tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/'));
/**
Expand All @@ -200,12 +205,15 @@ const _unURI = (a: string) =>
* @returns {string} UTF-8 string
*/
const decode = (src: string) => _decode(_unURI(src));
//
const _toUint8Array = _hasBuffer
? (a: string) => _U8Afrom(Buffer.from(a, 'base64'))
: (a: string) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = _hasBuffer
? (a: string) => _U8Afrom(Buffer.from(_unURI(a), 'base64'))
: (a: string) => _U8Afrom(_atob(_unURI(a)), c => c.charCodeAt(0));
const toUint8Array = (a: string) => _toUint8Array(_unURI(a));
//
const _noEnum = (v) => {
return {
value: v, enumerable: false, writable: true, configurable: true
Expand Down

0 comments on commit e66f5b9

Please sign in to comment.