@waiting/base64
Version:
Base64 encoding/decoding in pure JS on both modern Browsers and Node.js. Also supports URL-safe base64
13 lines (11 loc) • 6.03 kB
JavaScript
/**
* @waiting/base64
* Base64 encoding/decoding in pure JS on both modern Browsers and Node.js. Also supports URL-safe base64
*
* @version 4.2.9
* @author waiting
* @license MIT
* @link https://github.com/waitingsong/base64#readme
*/
!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(!h(n))throw new TypeError(o.notValidB64String);return n}function d(n){if("function"==typeof n)return n;if("function"==typeof TextEncoder)return TextEncoder;throw new TypeError(o.textEncoderUndefined)}function l(n){if("function"==typeof n)return n;if("function"==typeof TextDecoder)return TextDecoder;throw new TypeError(o.textDecoderUndefined)}function h(n){return/^[a-zA-Z0-9+\/_-]+={0,2}$/.test(n)}function g(n){return/^[a-zA-Z0-9_-]+$/.test(n)}function y(n){const e=w(n);if(!0!==e)throw new Error(e)}function p(n){const e=b(n);if(!0!==e)throw new Error(e)}function w(n){return"string"!=typeof n?o.notString:h(n)?n.length<4?o.notValidB64Length:n.length%4==0||o.base64Invalidlength:o.notValidB64String}function b(n){return"string"!=typeof n?o.notString:g(n)?!(n.length<2)||o.notValidB64URLLength:o.notValidB64URLString}function B(){return"object"==typeof process&&"function"==typeof Buffer&&"undefined"==typeof window}function m(n){return!!(n&&n instanceof ArrayBuffer)}function A(n){return!!(ArrayBuffer.isView(n)&&n instanceof Uint8Array)}function E(n){y(n);const e=n.indexOf("=");return e>0?n.slice(0,e).replace(/\+/g,"-").replace(/\//g,"_"):n.replace(/\+/g,"-").replace(/\//g,"_")}function v(n){p(n);const e=n.replace(/-/g,"+").replace(/_/g,"/");return U(e)}function U(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 L(n){const e=T(n),t=e[0],o=e[1],i=new Uint8Array(x(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 T(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 x(n,e){return(3*(n+e)>>2)-e}function I(n,e){const t=s(n),r=d(e),o=(new r).encode(t),i=S(o);return i}function S(n){let e;if(!n)throw new TypeError(o.fromArrayBufferInvalidParam);if(A(n))e=n;else{if(!m(n))throw new TypeError(o.fromArrayBufferInvalidParam);e=new Uint8Array(n)}return f(e)}function V(n,e,t){const r=l(t),o=C(n),i=new r(e).decode(o);return i}function C(n){const e=u(n),t=L(e);return t}function P(n){const e=s(n),t=Buffer.from(e).toString("base64");return t}function R(n,e){const t=u(n),r=Buffer.from(t,"base64").toString(e);return r}function _(n){let e;if(!n)throw new TypeError(o.fromArrayBufferInvalidParam);if(A(n))e=Buffer.from(n);else{if(!m(n))throw new TypeError(o.fromArrayBufferInvalidParam);e=Buffer.from(n)}const t=e.toString("base64");return t}function M(n,e){const t=B()&&!i.forceBrowser?P(n):I(n,e);return t}function D(n,e="utf-8",t){const r=B()&&!i.forceBrowser?R(n,e):V(n,e,t);return r}function j(n){const e=B()&&!i.forceBrowser?_(n):S(n);return e}function z(n){const e=T(n),t=e[0],r=e[1];return x(t,r)}function O(n,e){const t=M(n,e);return E(t)}function Z(n){const e=j(n);return E(e)}function k(n,e="utf-8",t){const r=U(n);return D(r,e,t)}n.ErrMsg=o,n.b64byteLength=z,n.b64decode=D,n.b64encode=M,n.b64fromBuffer=j,n.b64fromURLSafe=v,n.b64toURLSafe=E,n.b64urlDecode=k,n.b64urlEncode=O,n.b64urlFromBuffer=Z,n.isArrayBuffer=m,n.isUint8Array=A,n.testB64=w,n.testB64URL=b,n.validB64Chars=h,n.validB64URLChars=g,Object.defineProperty(n,"__esModule",{value:!0})});
//# sourceMappingURL=base64.umd.min.js.map