UNPKG

@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) 5.79 kB
/** * @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 */ 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(!l(n))throw new TypeError(e.notValidB64String);return n}function u(n){if("function"==typeof n)return n;if("function"==typeof TextEncoder)return TextEncoder;throw new TypeError(e.textEncoderUndefined)}function d(n){if("function"==typeof n)return n;if("function"==typeof TextDecoder)return TextDecoder;throw new TypeError(e.textDecoderUndefined)}function l(n){return/^[a-zA-Z0-9+\/_-]+={0,2}$/.test(n)}function h(n){return/^[a-zA-Z0-9_-]+$/.test(n)}function g(n){const t=y(n);if(!0!==t)throw new Error(t)}function w(n){const t=p(n);if(!0!==t)throw new Error(t)}function y(n){return"string"!=typeof n?e.notString:l(n)?n.length<4?e.notValidB64Length:n.length%4==0||e.base64Invalidlength:e.notValidB64String}function p(n){return"string"!=typeof n?e.notString:h(n)?!(n.length<2)||e.notValidB64URLLength:e.notValidB64URLString}function A(){return"object"==typeof process&&"function"==typeof Buffer&&"undefined"==typeof window}function m(n){return!!(n&&n instanceof ArrayBuffer)}function B(n){return!!(ArrayBuffer.isView(n)&&n instanceof Uint8Array)}function b(n){g(n);const t=n.indexOf("=");return t>0?n.slice(0,t).replace(/\+/g,"-").replace(/\//g,"_"):n.replace(/\+/g,"-").replace(/\//g,"_")}function E(n){w(n);const t=n.replace(/-/g,"+").replace(/_/g,"/");return v(t)}function v(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 T(n){const t=I(n),e=t[0],o=t[1],i=new Uint8Array(U(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 I(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 U(n,t){return(3*(n+t)>>2)-t}function V(n,t){const r=c(n),e=u(t),o=(new e).encode(r),i=x(o);return i}function x(n){let t;if(!n)throw new TypeError(e.fromArrayBufferInvalidParam);if(B(n))t=n;else{if(!m(n))throw new TypeError(e.fromArrayBufferInvalidParam);t=new Uint8Array(n)}return i(t)}function L(n,t,r){const e=d(r),o=S(n),i=new e(t).decode(o);return i}function S(n){const t=s(n),r=T(t);return r}function C(n){const t=c(n),r=Buffer.from(t).toString("base64");return r}function P(n,t){const r=s(n),e=Buffer.from(r,"base64").toString(t);return e}function R(n){let t;if(!n)throw new TypeError(e.fromArrayBufferInvalidParam);if(B(n))t=Buffer.from(n);else{if(!m(n))throw new TypeError(e.fromArrayBufferInvalidParam);t=Buffer.from(n)}const r=t.toString("base64");return r}function _(n,t){const r=A()&&!o.forceBrowser?C(n):V(n,t);return r}function M(n,t="utf-8",r){const e=A()&&!o.forceBrowser?P(n,t):L(n,t,r);return e}function D(n){const t=A()&&!o.forceBrowser?R(n):x(n);return t}function z(n){const t=I(n),r=t[0],e=t[1];return U(r,e)}function Z(n,t){const r=_(n,t);return b(r)}function j(n){const t=D(n);return b(t)}function k(n,t="utf-8",r){const e=v(n);return M(e,t,r)}export{e as ErrMsg,z as b64byteLength,M as b64decode,_ as b64encode,D as b64fromBuffer,E as b64fromURLSafe,b as b64toURLSafe,k as b64urlDecode,Z as b64urlEncode,j as b64urlFromBuffer,m as isArrayBuffer,B as isUint8Array,y as testB64,p as testB64URL,l as validB64Chars,h as validB64URLChars}; //# sourceMappingURL=base64.esm.min.js.map