Comparing version 3.2.4 to 3.3.1
142
base64.js
@@ -10,3 +10,3 @@ | ||
: typeof define === 'function' && define.amd | ||
? define(factory) : | ||
? define(factory) : | ||
// cf. https://github.com/dankogai/js-base64/issues/119 | ||
@@ -34,3 +34,3 @@ (function() { | ||
/** | ||
* base64.mjs | ||
* base64.ts | ||
* | ||
@@ -42,36 +42,35 @@ * Licensed under the BSD 3-Clause License. | ||
* http://en.wikipedia.org/wiki/Base64 | ||
* | ||
* | ||
* @author Dan Kogai (https://github.com/dankogai) | ||
*/ | ||
const version = '3.2.4'; | ||
const _b64chars | ||
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
const _b64tab = ((bin) => { | ||
*/ | ||
const version = '3.3.1'; | ||
const _b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
const _b64tab = ((chars) => { | ||
let tab = {}, i = 0; | ||
for (const c of bin) tab[c] = i++; | ||
for (const c of chars) | ||
tab[c] = i++; | ||
return tab; | ||
})(_b64chars); | ||
const _fromCharCode = String.fromCharCode; | ||
const _mkUriSafe = (src) => String(src) | ||
.replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_') | ||
.replace(/=/g, ''); | ||
const _mkUriSafe = (src) => src | ||
.replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_') | ||
.replace(/=/g, ''); | ||
/** | ||
* converts a Uint8Array to a Base64 string | ||
* @param {Uint8Array} src | ||
* @param {Boolean} urisafe URL-and-filename-safe a la RFC4648 | ||
* @param {Boolean} [rfc4648] URL-and-filename-safe a la RFC4648 | ||
* @returns {String} Base64 string | ||
*/ | ||
const fromUint8Array = (src, urisafe) => { | ||
const fromUint8Array = (src, rfc4648 = false) => { | ||
let b64 = ''; | ||
for (let i = 0, l = src.length; i < l; i += 3) { | ||
const a0 = src[i], a1 = src[i+1], a2 = src[i+2]; | ||
const a0 = src[i], a1 = src[i + 1], a2 = src[i + 2]; | ||
const ord = a0 << 16 | a1 << 8 | a2; | ||
b64 += _b64chars.charAt( ord >>> 18) | ||
+ _b64chars.charAt((ord >>> 12) & 63) | ||
+ ( typeof a1 != 'undefined' | ||
? _b64chars.charAt((ord >>> 6) & 63) : '=') | ||
+ ( typeof a2 != 'undefined' | ||
? _b64chars.charAt( ord & 63) : '='); | ||
b64 += _b64chars.charAt(ord >>> 18) | ||
+ _b64chars.charAt((ord >>> 12) & 63) | ||
+ (typeof a1 != 'undefined' | ||
? _b64chars.charAt((ord >>> 6) & 63) : '=') | ||
+ (typeof a2 != 'undefined' | ||
? _b64chars.charAt(ord & 63) : '='); | ||
} | ||
return urisafe ? _mkUriSafe(b64) : b64; | ||
return rfc4648 ? _mkUriSafe(b64) : b64; | ||
}; | ||
@@ -84,14 +83,10 @@ /** | ||
const _btoa = typeof btoa === 'function' | ||
? (s) => btoa(s) | ||
: (s) => { | ||
if (s.match(/[^\x00-\xFF]/)) throw new RangeError( | ||
'The string contains invalid characters.' | ||
); | ||
return fromUint8Array( | ||
Uint8Array.from(s,c => c.charCodeAt(0)) | ||
); | ||
}; | ||
? (s) => btoa(s) | ||
: (s) => { | ||
if (s.match(/[^\x00-\xFF]/)) | ||
throw new RangeError('The string contains invalid characters.'); | ||
return fromUint8Array(Uint8Array.from(s, c => c.charCodeAt(0))); | ||
}; | ||
/** | ||
* @deprecated since 3.0.0 | ||
* @param {string} src UTF-8 string | ||
* @returns {string} UTF-16 string | ||
@@ -102,7 +97,6 @@ */ | ||
* converts a UTF-8-encoded string to a Base64 string | ||
* @param {String} src the string to convert | ||
* @param {Boolean} rfc4648 if `true` make the result URL-safe | ||
* @param {Boolean} [rfc4648] if `true` make the result URL-safe | ||
* @returns {String} Base64 string | ||
*/ | ||
const encode = (src, rfc4648) => { | ||
const encode = (src, rfc4648 = false) => { | ||
const b64 = _btoa(utob(src)); | ||
@@ -113,3 +107,2 @@ return rfc4648 ? _mkUriSafe(b64) : b64; | ||
* converts a UTF-8-encoded string to URL-safe Base64 RFC4648 | ||
* @param {String} src the string to convert | ||
* @returns {String} Base64 string | ||
@@ -125,13 +118,10 @@ */ | ||
const _cb_decode = (cccc) => { | ||
let len = cccc.length, | ||
padlen = len % 4, | ||
n = (len > 0 ? _b64tab[cccc.charAt(0)] << 18 : 0) | ||
let 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) & 0xff), | ||
_fromCharCode( n & 0xff) | ||
]; | ||
| (len > 2 ? _b64tab[cccc.charAt(2)] << 6 : 0) | ||
| (len > 3 ? _b64tab[cccc.charAt(3)] : 0), chars = [ | ||
_fromCharCode(n >>> 16), | ||
_fromCharCode((n >>> 8) & 0xff), | ||
_fromCharCode(n & 0xff) | ||
]; | ||
chars.length -= [0, 0, 2, 1][padlen]; | ||
@@ -146,11 +136,11 @@ return chars.join(''); | ||
const _atob = typeof atob === 'function' | ||
? (a) => atob(a) | ||
: (a) => { | ||
return String(a) | ||
.replace(/[^A-Za-z0-9\+\/]/g, '') | ||
.replace(/\S{1,4}/g, _cb_decode); | ||
}; | ||
? (a) => atob(a) | ||
: (a) => { | ||
return String(a) | ||
.replace(/[^A-Za-z0-9\+\/]/g, '') | ||
.replace(/\S{1,4}/g, _cb_decode); | ||
}; | ||
const _decode = (a) => btou(_atob(a)); | ||
const _fromURI = (a) => { | ||
return String(a) | ||
const _unURI = (a) => { | ||
return a | ||
.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/') | ||
@@ -164,47 +154,41 @@ .replace(/[^A-Za-z0-9\+\/]/g, ''); | ||
*/ | ||
const decode = (src) => _decode(_fromURI(src)); | ||
const decode = (src) => _decode(_unURI(src)); | ||
/** | ||
* converts a Base64 string to a Uint8Array | ||
* @param {String} src Base64 string. Both normal and URL-safe are supported | ||
* @returns {Uint8Array} UTF-8 string | ||
*/ | ||
const toUint8Array = (a) => { | ||
return Uint8Array.from(_atob(_fromURI(a)), c => c.charCodeAt(0)); | ||
const toUint8Array = (a) => { | ||
return Uint8Array.from(_atob(_unURI(a)), c => c.charCodeAt(0)); | ||
}; | ||
const _noEnum = (v) => { | ||
return { | ||
value:v, enumerable:false, writable:true, configurable:true | ||
value: v, enumerable: false, writable: true, configurable: true | ||
}; | ||
}; | ||
const extendString = function() { | ||
const _add = (name, body) => Object.defineProperty( | ||
String.prototype, name, _noEnum(body) | ||
); | ||
_add('fromBase64', function() { | ||
const extendString = function () { | ||
const _add = (name, body) => Object.defineProperty(String.prototype, name, _noEnum(body)); | ||
_add('fromBase64', function () { | ||
return decode(this); | ||
}); | ||
_add('toBase64', function(rfc4648) { | ||
_add('toBase64', function (rfc4648) { | ||
return encode(this, rfc4648); | ||
}); | ||
_add('toBase64URI', function() { | ||
_add('toBase64URI', function () { | ||
return encode(this, true); | ||
}); | ||
_add('toBase64URL', function() { | ||
_add('toBase64URL', function () { | ||
return encode(this, true); | ||
}); | ||
_add('toUint8Array', function() { | ||
_add('toUint8Array', function () { | ||
return toUint8Array(this); | ||
}); | ||
}; | ||
const extendUint8Array = function() { | ||
const _add = (name, body) => Object.defineProperty( | ||
Uint8Array.prototype, name, _noEnum(body) | ||
); | ||
_add('toBase64', function(rfc4648) { | ||
const extendUint8Array = function () { | ||
const _add = (name, body) => Object.defineProperty(Uint8Array.prototype, name, _noEnum(body)); | ||
_add('toBase64', function (rfc4648) { | ||
return fromUint8Array(this, rfc4648); | ||
}); | ||
_add('toBase64URI', function() { | ||
_add('toBase64URI', function () { | ||
return fromUint8Array(this, true); | ||
}); | ||
_add('toBase64URL', function() { | ||
_add('toBase64URL', function () { | ||
return fromUint8Array(this, true); | ||
@@ -216,3 +200,3 @@ }); | ||
extendUint8Array(); | ||
} | ||
}; | ||
const gBase64 = { | ||
@@ -235,3 +219,3 @@ VERSION: version, | ||
extendBuiltins: extendBuiltins | ||
} | ||
}; | ||
@@ -238,0 +222,0 @@ // |
{ | ||
"name": "js-base64", | ||
"version": "3.2.4", | ||
"version": "3.3.1", | ||
"description": "Yet another Base64 transcoder in pure-JS", | ||
"main": "base64.js", | ||
"module": "base64.mjs", | ||
"types": "base64.d.ts", | ||
"files": [ | ||
"base64.js", | ||
"base64.mjs" | ||
"base64.mjs", | ||
"base64.ts", | ||
"base64.d.ts" | ||
], | ||
"scripts": { | ||
"makecjs": "util/makecjs > base64.js", | ||
"test": "npm run makecjs && mocha --require @babel/register" | ||
"test": "make test" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.10.0", | ||
"@babel/plugin-proposal-export-namespace-from": "^7.10.4", | ||
"@babel/preset-env": "^7.10.0", | ||
"@babel/register": "^7.10.0", | ||
"typescript" : "^3.9.7", | ||
"@types/node" : "^14.0.26", | ||
"esm": "^3.2.25", | ||
"mocha": "^8.0.0" | ||
@@ -21,0 +22,0 @@ }, |
@@ -7,14 +7,8 @@ [![build status](https://secure.travis-ci.org/dankogai/js-base64.png)](http://travis-ci.org/dankogai/js-base64) | ||
## HEADS UP: ES2015 support required since version 3 | ||
## HEADS UP: switch to TypeScript since version 3.3 | ||
Version 3 is completely rewritten with ES2015 features like arrow functions. All modern browsers and node.js are directly supported. Your codes should run unchanged. IE is no longer supported directly but you can transpile the script to use it (see below). | ||
In version 3.0 `js-base64` switch to ES2015 module. That made it easy to switch to TypeScript(just renaming `base64.mjs` to `base64.ts` was almost enough). Now `base64.mjs` is compiled from `base64.ts` then `base64.js` is generated from `base64.mjs`. | ||
The hardest part of maintaining this module was not Base64 features, but cross-platform support (eg. nodejs vs web browsers). By making ES2015 mandatory virtually all codes are common (except `atob()` and `btoa()`). | ||
## Install | ||
If you need to support legacy browsers like IE, use version 2 or transpile. | ||
## Usage | ||
### Install | ||
```shell | ||
@@ -24,2 +18,4 @@ $ npm install --save js-base64 | ||
## Usage | ||
### In Browser | ||
@@ -36,7 +32,9 @@ | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/js-base64@3.2.4/base64.min.js"> | ||
<script src="https://cdn.jsdelivr.net/npm/js-base64@3.3.1/base64.min.js"></script> | ||
``` | ||
### node.js | ||
This good old way loads `Base64` in the global context (`window`). Though `Base64.noConflict()` is made available, you should consider using ES6 Module to avoid tainting `window`. | ||
### node.js (commonjs) | ||
```javascript | ||
@@ -46,4 +44,6 @@ const Base64 = require('js-base64').Base64; | ||
## As a ES6 Module | ||
Unlike the case above, the global context is no longer modified. | ||
### As a ES6 Module | ||
locally… | ||
@@ -53,3 +53,5 @@ | ||
import { Base64 } from 'js-base64'; | ||
``` | ||
```javascript | ||
// or if you prefer no Base64 namespace | ||
@@ -64,6 +66,10 @@ import { encode, decode } from 'js-base64'; | ||
// note jsdelivr.net does not automatically minify .mjs | ||
import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.2.4/base64.mjs'; | ||
import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.3.1/base64.mjs'; | ||
</script> | ||
``` | ||
```html | ||
<script type="module"> | ||
// or if you prefer no Base64 namespace | ||
import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.2.4/base64.mjs'; | ||
import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.3.1/base64.mjs'; | ||
</script> | ||
@@ -131,10 +137,2 @@ ``` | ||
### TypeScript | ||
TypeScript 2.0 type definition was added to the [DefinitelyTyped repository](https://github.com/DefinitelyTyped/DefinitelyTyped). | ||
```bash | ||
$ npm install --save @types/js-base64 | ||
``` | ||
## `.decode()` vs `.atob` (and `.encode()` vs `btoa()`) | ||
@@ -141,0 +139,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28026
4
7
716
147