@waiting/base64
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -5,2 +5,11 @@ # Changelog | ||
## [4.2.0](https://github.com/waitingsong/base64/compare/v4.1.0...v4.2.0) (2019-06-13) | ||
### Features | ||
* b64urlFromBuffer() ([0652596](https://github.com/waitingsong/base64/commit/0652596)) | ||
## [4.1.0](https://github.com/waitingsong/base64/compare/v4.0.1...v4.1.0) (2019-06-12) | ||
@@ -7,0 +16,0 @@ |
/** | ||
* @waiting/base64 | ||
* Base64 encoding/decoding in pure JS on both modern Browsers and Node.js | ||
* Base64 encoding/decoding in pure JS on both modern Browsers and Node.js. Also supports URL-safe base64 | ||
* | ||
* @version 4.1.0 | ||
* @version 4.2.0 | ||
* @author waiting | ||
@@ -11,3 +11,3 @@ * @license MIT | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define("base64",["exports"],e):e((n=n||self).base64={})}(this,function(n){"use strict";const e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",t=[],r=[];for(let n=0,o=e.length;n<o;++n)t[n]=e[n],r[e.charCodeAt(n)]=n;r["-".charCodeAt(0)]=62,r["_".charCodeAt(0)]=63;const o={base64Invalidlength:"Invalid string. Length must be a multiple of 4 and positive",base64InvalidEqualPosition:'Invalid base64 string, char "=" should not exists or at posiont >= 2',encodeInvalidParam:"Invalid value of parameter of encode(), should be string|number|bigint",fromArrayBufferInvalidParam:"Invalid input, should be ArrayBuffer or Uint8Array",notString:"Invalid value of parameter, should be string",notValidB64String:"Valid base64 string only matches /^[a-zA-Z0-9+/_-]+={0,2}$/",notValidB64URLString:"Valid URL-safe base64 string only matchs /^[a-zA-Z0-9_-]+$/",notValidB64Length:"Valid base64 string contains as least 4 chars",notValidB64URLLength:"Valid URL-safe base64 string contains as least 2 chars",startMustGrossOrEqualToEnd:"Parameters of start should less then or equal to end",startMustGrossToEnd:"Parameters of start should less then end",textEncoderUndefined:"TextEncoder undefined!",textDecoderUndefined:"TextDecoder undefined!"},i={forceBrowser:!1};function f(n){const e=n.length,r=e%3,o=e-r,i=12e3,f=new Array(Math.ceil(o/12e3)+(r?1:0));let c=0;for(let e=0,t=0;e<o;e=t)t=e+12e3,f[c]=a(n,e,Math.min(t,o)),c+=1;if(1===r){const e=255&n[o];f[c]=t[e>>2]+t[e<<4&63]+"=="}else if(2===r){const e=(255&n[o])<<8|255&n[o+1];f[c]=t[e>>10]+t[e>>4&63]+t[e<<2&63]+"="}return f.join("")}function a(n,e,t){if(e>=t)throw new Error(o.startMustGrossToEnd);const r=Math.ceil((t-e)/3),i=new Array(r);for(let r=e,o=0;r<t;r+=3,o+=1)i[o]=c((255&n[r])<<16|(255&n[r+1])<<8|255&n[r+2]);return i.join("")}function c(n){return t[n>>18&63]+t[n>>12&63]+t[n>>6&63]+t[63&n]}function s(n){const e="string"==typeof n?n:"number"==typeof n||"bigint"==typeof n?n.toString():null;if(null===e)throw new TypeError(o.encodeInvalidParam);return e}function u(n){if("string"!=typeof n)throw new TypeError(o.notString);if(!y(n))throw new TypeError(o.notValidB64String);return n}function d(n){const e="function"==typeof n?n:"function"==typeof TextEncoder?TextEncoder:null;return h(e),e}function l(n){const e="function"==typeof n?n:"function"==typeof TextDecoder?TextDecoder:null;return g(e),e}function h(n){if(null===n)throw new TypeError(o.textEncoderUndefined)}function g(n){if(null===n)throw new TypeError(o.textDecoderUndefined)}function y(n){return/^[a-zA-Z0-9+\/_-]+={0,2}$/.test(n)}function p(n){return/^[a-zA-Z0-9_-]+$/.test(n)}function w(n){const e=B(n);if(!0!==e)throw new Error(e)}function b(n){const e=A(n);if(!0!==e)throw new Error(e)}function B(n){return"string"!=typeof n?o.notString:y(n)?n.length<4?o.notValidB64Length:n.length%4==0||o.base64Invalidlength:o.notValidB64String}function A(n){return"string"!=typeof n?o.notString:p(n)?!(n.length<2)||o.notValidB64URLLength:o.notValidB64URLString}function m(){return"object"==typeof process&&"function"==typeof Buffer&&"undefined"==typeof window}function E(n){return!!(n&&n instanceof ArrayBuffer)}function v(n){return!!(ArrayBuffer.isView(n)&&n instanceof Uint8Array)}function U(n){w(n);const e=n.indexOf("=");return e>0?n.slice(0,e).replace(/\+/g,"-").replace(/\//g,"_"):n.replace(/\+/g,"-").replace(/\//g,"_")}function L(n){b(n);const e=n.replace(/-/g,"+").replace(/_/g,"/");return T(e)}function T(n){let e=0;const t=n.length%4;switch(t){case 3:e=1;break;case 2:e=2;break;case 0:e=0;break;default:throw new Error(o.notValidB64URLLength)}return n+"=".repeat(e)}function x(n){const e=I(n),t=e[0],o=e[1],i=new Uint8Array(S(t,o));let f=0;const a=o?t-4:t;let c=0;for(;c<a;c+=4){const e=r[n.charCodeAt(c)]<<18|r[n.charCodeAt(c+1)]<<12|r[n.charCodeAt(c+2)]<<6|r[n.charCodeAt(c+3)];i[f++]=e>>16&255,i[f++]=e>>8&255,i[f++]=255&e}if(2===o)i[f]=r[n.charCodeAt(c)]<<2|r[n.charCodeAt(c+1)]>>4;else if(1===o){const e=r[n.charCodeAt(c)]<<10|r[n.charCodeAt(c+1)]<<4|r[n.charCodeAt(c+2)]>>2;i[f++]=e>>8&255,i[f]=255&e}return i}function I(n){const e=n.length;if(3&e||e<=0)throw new Error(o.base64Invalidlength);let t=n.indexOf("=");-1===t&&(t=e);const r=3-(t+3&3);return[t,r]}function S(n,e){return(3*(n+e)>>2)-e}function V(n,e){const t=s(n),r=d(e),o=(new r).encode(t),i=C(o);return i}function C(n){let e;if(!n)throw new TypeError(o.fromArrayBufferInvalidParam);if(v(n))e=n;else{if(!E(n))throw new TypeError(o.fromArrayBufferInvalidParam);e=new Uint8Array(n)}return f(e)}function P(n,e,t){const r=l(t),o=R(n),i=new r(e).decode(o);return i}function R(n){const e=u(n),t=x(e);return t}function _(n){const e=s(n),t=Buffer.from(e).toString("base64");return t}function M(n,e){const t=u(n),r=Buffer.from(t,"base64").toString(e);return r}function D(n){let e;if(!n)throw new TypeError(o.fromArrayBufferInvalidParam);if(v(n))e=Buffer.from(n);else{if(!E(n))throw new TypeError(o.fromArrayBufferInvalidParam);e=Buffer.from(n)}const t=e.toString("base64");return t}function j(n,e){const t=m()&&!i.forceBrowser?_(n):V(n,e);return t}function z(n,e="utf-8",t){const r=m()&&!i.forceBrowser?M(n,e):P(n,e,t);return r}function O(n){const e=m()&&!i.forceBrowser?D(n):C(n);return e}function Z(n){const e=I(n),t=e[0],r=e[1];return S(t,r)}function k(n,e){const t=j(n,e);return U(t)}function q(n,e="utf-8",t){const r=T(n);return z(r,e,t)}n.ErrMsg=o,n.b64byteLength=Z,n.b64decode=z,n.b64encode=j,n.b64fromBuffer=O,n.b64fromURLSafe=L,n.b64toURLSafe=U,n.b64urlDecode=q,n.b64urlEncode=k,n.isArrayBuffer=E,n.isUint8Array=v,n.testB64=B,n.testB64URL=A,n.validB64Chars=y,n.validB64URLChars=p,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define("base64",["exports"],e):e((n=n||self).base64={})}(this,function(n){"use strict";const e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",t=[],r=[];for(let n=0,o=e.length;n<o;++n)t[n]=e[n],r[e.charCodeAt(n)]=n;r["-".charCodeAt(0)]=62,r["_".charCodeAt(0)]=63;const o={base64Invalidlength:"Invalid string. Length must be a multiple of 4 and positive",base64InvalidEqualPosition:'Invalid base64 string, char "=" should not exists or at posiont >= 2',encodeInvalidParam:"Invalid value of parameter of encode(), should be string|number|bigint",fromArrayBufferInvalidParam:"Invalid input, should be ArrayBuffer or Uint8Array",notString:"Invalid value of parameter, should be string",notValidB64String:"Valid base64 string only matches /^[a-zA-Z0-9+/_-]+={0,2}$/",notValidB64URLString:"Valid URL-safe base64 string only matchs /^[a-zA-Z0-9_-]+$/",notValidB64Length:"Valid base64 string contains as least 4 chars",notValidB64URLLength:"Valid URL-safe base64 string contains as least 2 chars",startMustGrossOrEqualToEnd:"Parameters of start should less then or equal to end",startMustGrossToEnd:"Parameters of start should less then end",textEncoderUndefined:"TextEncoder undefined!",textDecoderUndefined:"TextDecoder undefined!"},i={forceBrowser:!1};function f(n){const e=n.length,r=e%3,o=e-r,i=12e3,f=new Array(Math.ceil(o/12e3)+(r?1:0));let c=0;for(let e=0,t=0;e<o;e=t)t=e+12e3,f[c]=a(n,e,Math.min(t,o)),c+=1;if(1===r){const e=255&n[o];f[c]=t[e>>2]+t[e<<4&63]+"=="}else if(2===r){const e=(255&n[o])<<8|255&n[o+1];f[c]=t[e>>10]+t[e>>4&63]+t[e<<2&63]+"="}return f.join("")}function a(n,e,t){if(e>=t)throw new Error(o.startMustGrossToEnd);const r=Math.ceil((t-e)/3),i=new Array(r);for(let r=e,o=0;r<t;r+=3,o+=1)i[o]=c((255&n[r])<<16|(255&n[r+1])<<8|255&n[r+2]);return i.join("")}function c(n){return t[n>>18&63]+t[n>>12&63]+t[n>>6&63]+t[63&n]}function s(n){const e="string"==typeof n?n:"number"==typeof n||"bigint"==typeof n?n.toString():null;if(null===e)throw new TypeError(o.encodeInvalidParam);return e}function u(n){if("string"!=typeof n)throw new TypeError(o.notString);if(!y(n))throw new TypeError(o.notValidB64String);return n}function d(n){const e="function"==typeof n?n:"function"==typeof TextEncoder?TextEncoder:null;return h(e),e}function l(n){const e="function"==typeof n?n:"function"==typeof TextDecoder?TextDecoder:null;return g(e),e}function h(n){if(null===n)throw new TypeError(o.textEncoderUndefined)}function g(n){if(null===n)throw new TypeError(o.textDecoderUndefined)}function y(n){return/^[a-zA-Z0-9+\/_-]+={0,2}$/.test(n)}function p(n){return/^[a-zA-Z0-9_-]+$/.test(n)}function w(n){const e=B(n);if(!0!==e)throw new Error(e)}function b(n){const e=m(n);if(!0!==e)throw new Error(e)}function B(n){return"string"!=typeof n?o.notString:y(n)?n.length<4?o.notValidB64Length:n.length%4==0||o.base64Invalidlength:o.notValidB64String}function m(n){return"string"!=typeof n?o.notString:p(n)?!(n.length<2)||o.notValidB64URLLength:o.notValidB64URLString}function A(){return"object"==typeof process&&"function"==typeof Buffer&&"undefined"==typeof window}function E(n){return!!(n&&n instanceof ArrayBuffer)}function v(n){return!!(ArrayBuffer.isView(n)&&n instanceof Uint8Array)}function U(n){w(n);const e=n.indexOf("=");return e>0?n.slice(0,e).replace(/\+/g,"-").replace(/\//g,"_"):n.replace(/\+/g,"-").replace(/\//g,"_")}function L(n){b(n);const e=n.replace(/-/g,"+").replace(/_/g,"/");return T(e)}function T(n){let e=0;const t=n.length%4;switch(t){case 3:e=1;break;case 2:e=2;break;case 0:e=0;break;default:throw new Error(o.notValidB64URLLength)}return n+"=".repeat(e)}function x(n){const e=I(n),t=e[0],o=e[1],i=new Uint8Array(S(t,o));let f=0;const a=o?t-4:t;let c=0;for(;c<a;c+=4){const e=r[n.charCodeAt(c)]<<18|r[n.charCodeAt(c+1)]<<12|r[n.charCodeAt(c+2)]<<6|r[n.charCodeAt(c+3)];i[f++]=e>>16&255,i[f++]=e>>8&255,i[f++]=255&e}if(2===o)i[f]=r[n.charCodeAt(c)]<<2|r[n.charCodeAt(c+1)]>>4;else if(1===o){const e=r[n.charCodeAt(c)]<<10|r[n.charCodeAt(c+1)]<<4|r[n.charCodeAt(c+2)]>>2;i[f++]=e>>8&255,i[f]=255&e}return i}function I(n){const e=n.length;if(3&e||e<=0)throw new Error(o.base64Invalidlength);let t=n.indexOf("=");-1===t&&(t=e);const r=3-(t+3&3);return[t,r]}function S(n,e){return(3*(n+e)>>2)-e}function V(n,e){const t=s(n),r=d(e),o=(new r).encode(t),i=C(o);return i}function C(n){let e;if(!n)throw new TypeError(o.fromArrayBufferInvalidParam);if(v(n))e=n;else{if(!E(n))throw new TypeError(o.fromArrayBufferInvalidParam);e=new Uint8Array(n)}return f(e)}function P(n,e,t){const r=l(t),o=R(n),i=new r(e).decode(o);return i}function R(n){const e=u(n),t=x(e);return t}function _(n){const e=s(n),t=Buffer.from(e).toString("base64");return t}function M(n,e){const t=u(n),r=Buffer.from(t,"base64").toString(e);return r}function D(n){let e;if(!n)throw new TypeError(o.fromArrayBufferInvalidParam);if(v(n))e=Buffer.from(n);else{if(!E(n))throw new TypeError(o.fromArrayBufferInvalidParam);e=Buffer.from(n)}const t=e.toString("base64");return t}function j(n,e){const t=A()&&!i.forceBrowser?_(n):V(n,e);return t}function z(n,e="utf-8",t){const r=A()&&!i.forceBrowser?M(n,e):P(n,e,t);return r}function O(n){const e=A()&&!i.forceBrowser?D(n):C(n);return e}function Z(n){const e=I(n),t=e[0],r=e[1];return S(t,r)}function k(n,e){const t=j(n,e);return U(t)}function q(n){const e=O(n);return U(e)}function G(n,e="utf-8",t){const r=T(n);return z(r,e,t)}n.ErrMsg=o,n.b64byteLength=Z,n.b64decode=z,n.b64encode=j,n.b64fromBuffer=O,n.b64fromURLSafe=L,n.b64toURLSafe=U,n.b64urlDecode=G,n.b64urlEncode=k,n.b64urlFromBuffer=q,n.isArrayBuffer=E,n.isUint8Array=v,n.testB64=B,n.testB64URL=m,n.validB64Chars=y,n.validB64URLChars=p,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=base64.umd.min.js.map |
/** | ||
* @waiting/base64 | ||
* Base64 encoding/decoding in pure JS on both modern Browsers and Node.js | ||
* Base64 encoding/decoding in pure JS on both modern Browsers and Node.js. Also supports URL-safe base64 | ||
* | ||
* @version 4.1.0 | ||
* @version 4.2.0 | ||
* @author waiting | ||
@@ -455,2 +455,12 @@ * @license MIT | ||
/** | ||
* Encode to URL-safe base64, source from ArrayBuffer or Uint8Array | ||
* | ||
* @see https://en.wikipedia.org/wiki/Base64#URL_applications | ||
*/ | ||
function b64urlFromBuffer(buffer) { | ||
const b64 = b64fromBuffer(buffer); | ||
return b64toURLSafe(b64) | ||
} | ||
/** | ||
* Decode URL-safe base64 to original string. | ||
@@ -477,2 +487,3 @@ * | ||
exports.b64urlEncode = b64urlEncode; | ||
exports.b64urlFromBuffer = b64urlFromBuffer; | ||
exports.isArrayBuffer = isArrayBuffer; | ||
@@ -479,0 +490,0 @@ exports.isUint8Array = isUint8Array; |
/** | ||
* @waiting/base64 | ||
* Base64 encoding/decoding in pure JS on both modern Browsers and Node.js | ||
* Base64 encoding/decoding in pure JS on both modern Browsers and Node.js. Also supports URL-safe base64 | ||
* | ||
* @version 4.1.0 | ||
* @version 4.2.0 | ||
* @author waiting | ||
@@ -451,2 +451,12 @@ * @license MIT | ||
/** | ||
* Encode to URL-safe base64, source from ArrayBuffer or Uint8Array | ||
* | ||
* @see https://en.wikipedia.org/wiki/Base64#URL_applications | ||
*/ | ||
function b64urlFromBuffer(buffer) { | ||
const b64 = b64fromBuffer(buffer); | ||
return b64toURLSafe(b64) | ||
} | ||
/** | ||
* Decode URL-safe base64 to original string. | ||
@@ -464,2 +474,2 @@ * | ||
export { ErrMsg, b64byteLength, b64decode, b64encode, b64fromBuffer, b64fromURLSafe, b64toURLSafe, b64urlDecode, b64urlEncode, isArrayBuffer, isUint8Array, testB64, testB64URL, validB64Chars, validB64URLChars }; | ||
export { ErrMsg, b64byteLength, b64decode, b64encode, b64fromBuffer, b64fromURLSafe, b64toURLSafe, b64urlDecode, b64urlEncode, b64urlFromBuffer, isArrayBuffer, isUint8Array, testB64, testB64URL, validB64Chars, validB64URLChars }; |
/** | ||
* @waiting/base64 | ||
* Base64 encoding/decoding in pure JS on both modern Browsers and Node.js | ||
* Base64 encoding/decoding in pure JS on both modern Browsers and Node.js. Also supports URL-safe base64 | ||
* | ||
* @version 4.1.0 | ||
* @version 4.2.0 | ||
* @author waiting | ||
@@ -11,3 +11,3 @@ * @license MIT | ||
const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",t=[],r=[];for(let e=0,o=n.length;e<o;++e)t[e]=n[e],r[n.charCodeAt(e)]=e;r["-".charCodeAt(0)]=62,r["_".charCodeAt(0)]=63;const e={base64Invalidlength:"Invalid string. Length must be a multiple of 4 and positive",base64InvalidEqualPosition:'Invalid base64 string, char "=" should not exists or at posiont >= 2',encodeInvalidParam:"Invalid value of parameter of encode(), should be string|number|bigint",fromArrayBufferInvalidParam:"Invalid input, should be ArrayBuffer or Uint8Array",notString:"Invalid value of parameter, should be string",notValidB64String:"Valid base64 string only matches /^[a-zA-Z0-9+/_-]+={0,2}$/",notValidB64URLString:"Valid URL-safe base64 string only matchs /^[a-zA-Z0-9_-]+$/",notValidB64Length:"Valid base64 string contains as least 4 chars",notValidB64URLLength:"Valid URL-safe base64 string contains as least 2 chars",startMustGrossOrEqualToEnd:"Parameters of start should less then or equal to end",startMustGrossToEnd:"Parameters of start should less then end",textEncoderUndefined:"TextEncoder undefined!",textDecoderUndefined:"TextDecoder undefined!"},o={forceBrowser:!1};function i(n){const r=n.length,e=r%3,o=r-e,i=12e3,f=new Array(Math.ceil(o/12e3)+(e?1:0));let c=0;for(let t=0,r=0;t<o;t=r)r=t+12e3,f[c]=a(n,t,Math.min(r,o)),c+=1;if(1===e){const r=255&n[o];f[c]=t[r>>2]+t[r<<4&63]+"=="}else if(2===e){const r=(255&n[o])<<8|255&n[o+1];f[c]=t[r>>10]+t[r>>4&63]+t[r<<2&63]+"="}return f.join("")}function a(n,t,r){if(t>=r)throw new Error(e.startMustGrossToEnd);const o=Math.ceil((r-t)/3),i=new Array(o);for(let e=t,o=0;e<r;e+=3,o+=1)i[o]=f((255&n[e])<<16|(255&n[e+1])<<8|255&n[e+2]);return i.join("")}function f(n){return t[n>>18&63]+t[n>>12&63]+t[n>>6&63]+t[63&n]}function c(n){const t="string"==typeof n?n:"number"==typeof n||"bigint"==typeof n?n.toString():null;if(null===t)throw new TypeError(e.encodeInvalidParam);return t}function s(n){if("string"!=typeof n)throw new TypeError(e.notString);if(!g(n))throw new TypeError(e.notValidB64String);return n}function u(n){const t="function"==typeof n?n:"function"==typeof TextEncoder?TextEncoder:null;return d(t),t}function l(n){const t="function"==typeof n?n:"function"==typeof TextDecoder?TextDecoder:null;return h(t),t}function d(n){if(null===n)throw new TypeError(e.textEncoderUndefined)}function h(n){if(null===n)throw new TypeError(e.textDecoderUndefined)}function g(n){return/^[a-zA-Z0-9+\/_-]+={0,2}$/.test(n)}function w(n){return/^[a-zA-Z0-9_-]+$/.test(n)}function y(n){const t=A(n);if(!0!==t)throw new Error(t)}function p(n){const t=m(n);if(!0!==t)throw new Error(t)}function A(n){return"string"!=typeof n?e.notString:g(n)?n.length<4?e.notValidB64Length:n.length%4==0||e.base64Invalidlength:e.notValidB64String}function m(n){return"string"!=typeof n?e.notString:w(n)?!(n.length<2)||e.notValidB64URLLength:e.notValidB64URLString}function B(){return"object"==typeof process&&"function"==typeof Buffer&&"undefined"==typeof window}function b(n){return!!(n&&n instanceof ArrayBuffer)}function E(n){return!!(ArrayBuffer.isView(n)&&n instanceof Uint8Array)}function v(n){y(n);const t=n.indexOf("=");return t>0?n.slice(0,t).replace(/\+/g,"-").replace(/\//g,"_"):n.replace(/\+/g,"-").replace(/\//g,"_")}function T(n){p(n);const t=n.replace(/-/g,"+").replace(/_/g,"/");return I(t)}function I(n){let t=0;const r=n.length%4;switch(r){case 3:t=1;break;case 2:t=2;break;case 0:t=0;break;default:throw new Error(e.notValidB64URLLength)}return n+"=".repeat(t)}function U(n){const t=V(n),e=t[0],o=t[1],i=new Uint8Array(x(e,o));let a=0;const f=o?e-4:e;let c=0;for(;c<f;c+=4){const t=r[n.charCodeAt(c)]<<18|r[n.charCodeAt(c+1)]<<12|r[n.charCodeAt(c+2)]<<6|r[n.charCodeAt(c+3)];i[a++]=t>>16&255,i[a++]=t>>8&255,i[a++]=255&t}if(2===o)i[a]=r[n.charCodeAt(c)]<<2|r[n.charCodeAt(c+1)]>>4;else if(1===o){const t=r[n.charCodeAt(c)]<<10|r[n.charCodeAt(c+1)]<<4|r[n.charCodeAt(c+2)]>>2;i[a++]=t>>8&255,i[a]=255&t}return i}function V(n){const t=n.length;if(3&t||t<=0)throw new Error(e.base64Invalidlength);let r=n.indexOf("=");-1===r&&(r=t);const o=3-(r+3&3);return[r,o]}function x(n,t){return(3*(n+t)>>2)-t}function L(n,t){const r=c(n),e=u(t),o=(new e).encode(r),i=S(o);return i}function S(n){let t;if(!n)throw new TypeError(e.fromArrayBufferInvalidParam);if(E(n))t=n;else{if(!b(n))throw new TypeError(e.fromArrayBufferInvalidParam);t=new Uint8Array(n)}return i(t)}function C(n,t,r){const e=l(r),o=P(n),i=new e(t).decode(o);return i}function P(n){const t=s(n),r=U(t);return r}function R(n){const t=c(n),r=Buffer.from(t).toString("base64");return r}function _(n,t){const r=s(n),e=Buffer.from(r,"base64").toString(t);return e}function M(n){let t;if(!n)throw new TypeError(e.fromArrayBufferInvalidParam);if(E(n))t=Buffer.from(n);else{if(!b(n))throw new TypeError(e.fromArrayBufferInvalidParam);t=Buffer.from(n)}const r=t.toString("base64");return r}function D(n,t){const r=B()&&!o.forceBrowser?R(n):L(n,t);return r}function z(n,t="utf-8",r){const e=B()&&!o.forceBrowser?_(n,t):C(n,t,r);return e}function Z(n){const t=B()&&!o.forceBrowser?M(n):S(n);return t}function j(n){const t=V(n),r=t[0],e=t[1];return x(r,e)}function k(n,t){const r=D(n,t);return v(r)}function q(n,t="utf-8",r){const e=I(n);return z(e,t,r)}export{e as ErrMsg,j as b64byteLength,z as b64decode,D as b64encode,Z as b64fromBuffer,T as b64fromURLSafe,v as b64toURLSafe,q as b64urlDecode,k as b64urlEncode,b as isArrayBuffer,E as isUint8Array,A as testB64,m as testB64URL,g as validB64Chars,w as validB64URLChars}; | ||
const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",t=[],r=[];for(let e=0,o=n.length;e<o;++e)t[e]=n[e],r[n.charCodeAt(e)]=e;r["-".charCodeAt(0)]=62,r["_".charCodeAt(0)]=63;const e={base64Invalidlength:"Invalid string. Length must be a multiple of 4 and positive",base64InvalidEqualPosition:'Invalid base64 string, char "=" should not exists or at posiont >= 2',encodeInvalidParam:"Invalid value of parameter of encode(), should be string|number|bigint",fromArrayBufferInvalidParam:"Invalid input, should be ArrayBuffer or Uint8Array",notString:"Invalid value of parameter, should be string",notValidB64String:"Valid base64 string only matches /^[a-zA-Z0-9+/_-]+={0,2}$/",notValidB64URLString:"Valid URL-safe base64 string only matchs /^[a-zA-Z0-9_-]+$/",notValidB64Length:"Valid base64 string contains as least 4 chars",notValidB64URLLength:"Valid URL-safe base64 string contains as least 2 chars",startMustGrossOrEqualToEnd:"Parameters of start should less then or equal to end",startMustGrossToEnd:"Parameters of start should less then end",textEncoderUndefined:"TextEncoder undefined!",textDecoderUndefined:"TextDecoder undefined!"},o={forceBrowser:!1};function i(n){const r=n.length,e=r%3,o=r-e,i=12e3,a=new Array(Math.ceil(o/12e3)+(e?1:0));let c=0;for(let t=0,r=0;t<o;t=r)r=t+12e3,a[c]=f(n,t,Math.min(r,o)),c+=1;if(1===e){const r=255&n[o];a[c]=t[r>>2]+t[r<<4&63]+"=="}else if(2===e){const r=(255&n[o])<<8|255&n[o+1];a[c]=t[r>>10]+t[r>>4&63]+t[r<<2&63]+"="}return a.join("")}function f(n,t,r){if(t>=r)throw new Error(e.startMustGrossToEnd);const o=Math.ceil((r-t)/3),i=new Array(o);for(let e=t,o=0;e<r;e+=3,o+=1)i[o]=a((255&n[e])<<16|(255&n[e+1])<<8|255&n[e+2]);return i.join("")}function a(n){return t[n>>18&63]+t[n>>12&63]+t[n>>6&63]+t[63&n]}function c(n){const t="string"==typeof n?n:"number"==typeof n||"bigint"==typeof n?n.toString():null;if(null===t)throw new TypeError(e.encodeInvalidParam);return t}function s(n){if("string"!=typeof n)throw new TypeError(e.notString);if(!g(n))throw new TypeError(e.notValidB64String);return n}function u(n){const t="function"==typeof n?n:"function"==typeof TextEncoder?TextEncoder:null;return d(t),t}function l(n){const t="function"==typeof n?n:"function"==typeof TextDecoder?TextDecoder:null;return h(t),t}function d(n){if(null===n)throw new TypeError(e.textEncoderUndefined)}function h(n){if(null===n)throw new TypeError(e.textDecoderUndefined)}function g(n){return/^[a-zA-Z0-9+\/_-]+={0,2}$/.test(n)}function w(n){return/^[a-zA-Z0-9_-]+$/.test(n)}function y(n){const t=A(n);if(!0!==t)throw new Error(t)}function p(n){const t=m(n);if(!0!==t)throw new Error(t)}function A(n){return"string"!=typeof n?e.notString:g(n)?n.length<4?e.notValidB64Length:n.length%4==0||e.base64Invalidlength:e.notValidB64String}function m(n){return"string"!=typeof n?e.notString:w(n)?!(n.length<2)||e.notValidB64URLLength:e.notValidB64URLString}function B(){return"object"==typeof process&&"function"==typeof Buffer&&"undefined"==typeof window}function b(n){return!!(n&&n instanceof ArrayBuffer)}function E(n){return!!(ArrayBuffer.isView(n)&&n instanceof Uint8Array)}function v(n){y(n);const t=n.indexOf("=");return t>0?n.slice(0,t).replace(/\+/g,"-").replace(/\//g,"_"):n.replace(/\+/g,"-").replace(/\//g,"_")}function T(n){p(n);const t=n.replace(/-/g,"+").replace(/_/g,"/");return I(t)}function I(n){let t=0;const r=n.length%4;switch(r){case 3:t=1;break;case 2:t=2;break;case 0:t=0;break;default:throw new Error(e.notValidB64URLLength)}return n+"=".repeat(t)}function U(n){const t=V(n),e=t[0],o=t[1],i=new Uint8Array(x(e,o));let f=0;const a=o?e-4:e;let c=0;for(;c<a;c+=4){const t=r[n.charCodeAt(c)]<<18|r[n.charCodeAt(c+1)]<<12|r[n.charCodeAt(c+2)]<<6|r[n.charCodeAt(c+3)];i[f++]=t>>16&255,i[f++]=t>>8&255,i[f++]=255&t}if(2===o)i[f]=r[n.charCodeAt(c)]<<2|r[n.charCodeAt(c+1)]>>4;else if(1===o){const t=r[n.charCodeAt(c)]<<10|r[n.charCodeAt(c+1)]<<4|r[n.charCodeAt(c+2)]>>2;i[f++]=t>>8&255,i[f]=255&t}return i}function V(n){const t=n.length;if(3&t||t<=0)throw new Error(e.base64Invalidlength);let r=n.indexOf("=");-1===r&&(r=t);const o=3-(r+3&3);return[r,o]}function x(n,t){return(3*(n+t)>>2)-t}function L(n,t){const r=c(n),e=u(t),o=(new e).encode(r),i=S(o);return i}function S(n){let t;if(!n)throw new TypeError(e.fromArrayBufferInvalidParam);if(E(n))t=n;else{if(!b(n))throw new TypeError(e.fromArrayBufferInvalidParam);t=new Uint8Array(n)}return i(t)}function C(n,t,r){const e=l(r),o=P(n),i=new e(t).decode(o);return i}function P(n){const t=s(n),r=U(t);return r}function R(n){const t=c(n),r=Buffer.from(t).toString("base64");return r}function _(n,t){const r=s(n),e=Buffer.from(r,"base64").toString(t);return e}function M(n){let t;if(!n)throw new TypeError(e.fromArrayBufferInvalidParam);if(E(n))t=Buffer.from(n);else{if(!b(n))throw new TypeError(e.fromArrayBufferInvalidParam);t=Buffer.from(n)}const r=t.toString("base64");return r}function D(n,t){const r=B()&&!o.forceBrowser?R(n):L(n,t);return r}function z(n,t="utf-8",r){const e=B()&&!o.forceBrowser?_(n,t):C(n,t,r);return e}function Z(n){const t=B()&&!o.forceBrowser?M(n):S(n);return t}function j(n){const t=V(n),r=t[0],e=t[1];return x(r,e)}function k(n,t){const r=D(n,t);return v(r)}function q(n){const t=Z(n);return v(t)}function G(n,t="utf-8",r){const e=I(n);return z(e,t,r)}export{e as ErrMsg,j as b64byteLength,z as b64decode,D as b64encode,Z as b64fromBuffer,T as b64fromURLSafe,v as b64toURLSafe,G as b64urlDecode,k as b64urlEncode,q as b64urlFromBuffer,b as isArrayBuffer,E as isUint8Array,A as testB64,m as testB64URL,g as validB64Chars,w as validB64URLChars}; | ||
//# sourceMappingURL=index.esm.min.js.map |
@@ -24,2 +24,8 @@ import { TextDecoderFn, TextEncoderFn } from './model'; | ||
/** | ||
* Encode to URL-safe base64, source from ArrayBuffer or Uint8Array | ||
* | ||
* @see https://en.wikipedia.org/wiki/Base64#URL_applications | ||
*/ | ||
export declare function b64urlFromBuffer(buffer: ArrayBuffer | Uint8Array): string; | ||
/** | ||
* Decode URL-safe base64 to original string. | ||
@@ -26,0 +32,0 @@ * |
@@ -56,2 +56,12 @@ import { browserDecode, browserEncode, fromBuffer as browserFromBuffer, } from './browser' | ||
/** | ||
* Encode to URL-safe base64, source from ArrayBuffer or Uint8Array | ||
* | ||
* @see https://en.wikipedia.org/wiki/Base64#URL_applications | ||
*/ | ||
export function b64urlFromBuffer(buffer) { | ||
const b64 = b64fromBuffer(buffer) | ||
return b64toURLSafe(b64) | ||
} | ||
/** | ||
* Decode URL-safe base64 to original string. | ||
@@ -58,0 +68,0 @@ * |
{ | ||
"name": "@waiting/base64", | ||
"author": "waiting", | ||
"version": "4.1.0", | ||
"description": "Base64 encoding/decoding in pure JS on both modern Browsers and Node.js", | ||
"version": "4.2.0", | ||
"description": "Base64 encoding/decoding in pure JS on both modern Browsers and Node.js. Also supports URL-safe base64", | ||
"keywords": [ | ||
"base64", | ||
"URL-safe", | ||
"browser", | ||
@@ -9,0 +10,0 @@ "Node.js", |
# Base64 | ||
Base64 encoding/decoding in pure JS on both modern Browsers and Node.js based on [base64-js](https://www.npmjs.com/package/base64-js) | ||
Base64 encoding/decoding in pure JS on both modern Browsers and Node.js based on [base64-js](https://www.npmjs.com/package/base64-js). | ||
Also supports [URL-safe base64](https://en.wikipedia.org/wiki/Base64#URL_applications) | ||
@@ -39,3 +40,3 @@ [![Version](https://img.shields.io/npm/v/@waiting/base64.svg)](https://www.npmjs.com/package/@waiting/base64) | ||
```ts | ||
import { b64encode, b64fromBuffer } from '@waiting/base64' | ||
import { b64encode, b64fromBuffer, b64urlEncode } from '@waiting/base64' | ||
@@ -50,13 +51,29 @@ b64encode('A') === 'QQ==' | ||
b64fromBuffer(u8arr) === b64encode('中文') // '5Lit5paH' | ||
// URL-safe | ||
b64urlEncode('A') === 'QQ' | ||
``` | ||
### Decoding | ||
```ts | ||
import { b64decode } from '@waiting/base64' | ||
import { b64decode, b64urlDecode } from '@waiting/base64' | ||
b64decode('MQ==') === '1' | ||
b64urlDecode('MQ') === '1' | ||
``` | ||
### Transfer | ||
```ts | ||
import { b64toURLSafe, b64fromURLSafe } from '@waiting/base64' | ||
// base64 -> URL-safe base64 | ||
b64toURLSafe('QQ==') === 'QQ' | ||
b64toURLSafe('5Lit5paH5rWL6K+V') === '5Lit5paH5rWL6K-V' | ||
// URL-safe base64 -> base64 | ||
b64fromURLSafe('QQ') === 'QQ==' | ||
b64fromURLSafe('0J_RgNC40LLQtdGCLCDQvNC40YAh') === '0J/RgNC40LLQtdGCLCDQvNC40YAh' | ||
``` | ||
## Testing | ||
@@ -63,0 +80,0 @@ ```sh |
# Base64 | ||
Base64 编码、解码 JS 实现, 支持长青浏览器和 Node.js,基于 [base64-js](https://www.npmjs.com/package/base64-js) | ||
Base64 编码、解码 JS 实现,支持长青浏览器和 Node.js,基于 [base64-js](https://www.npmjs.com/package/base64-js) 。 | ||
同时支持 [URL安全的base64](https://zh.wikipedia.org/zh-hans/Base64#%E5%9C%A8URL%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8) | ||
@@ -37,3 +38,3 @@ [![Version](https://img.shields.io/npm/v/@waiting/base64.svg)](https://www.npmjs.com/package/@waiting/base64) | ||
```ts | ||
import { b64encode, b64fromBuffer } from '@waiting/base64' | ||
import { b64encode, b64fromBuffer, b64urlEncode } from '@waiting/base64' | ||
@@ -48,2 +49,5 @@ b64encode('A') === 'QQ==' | ||
b64fromBuffer(u8arr) === b64encode('中文') // '5Lit5paH' | ||
// URL-safe | ||
b64urlEncode('A') === 'QQ' | ||
``` | ||
@@ -53,8 +57,22 @@ | ||
```ts | ||
import { b64decode } from '@waiting/base64' | ||
import { b64decode, b64urlDecode } from '@waiting/base64' | ||
b64decode('MQ==') === '1' | ||
b64urlDecode('MQ') === '1' | ||
``` | ||
### 转换 | ||
```ts | ||
import { b64toURLSafe, b64fromURLSafe } from '@waiting/base64' | ||
// base64 -> URL-safe base64 | ||
b64toURLSafe('QQ==') === 'QQ' | ||
b64toURLSafe('5Lit5paH5rWL6K+V') === '5Lit5paH5rWL6K-V' | ||
// URL-safe base64 -> base64 | ||
b64fromURLSafe('QQ') === 'QQ==' | ||
b64fromURLSafe('0J_RgNC40LLQtdGCLCDQvNC40YAh') === '0J/RgNC40LLQtdGCLCDQvNC40YAh' | ||
``` | ||
## 测试 | ||
@@ -61,0 +79,0 @@ ```sh |
Sorry, the diff of this file is not supported yet
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
126175
1508
102