byte-encodings
Version:
Utilities for encoding and decoding common formats like hex, base64, and varint. Ported from Deno's @std/encoding.
2 lines (1 loc) • 2.85 kB
JavaScript
var i=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var B=(n,t)=>{for(var e in t)i(n,e,{get:t[e],enumerable:!0})},F=(n,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of w(t))!_.call(n,s)&&s!==e&&i(n,s,{get:()=>t[s],enumerable:!(o=h(t,s))||o.enumerable});return n};var E=n=>F(i({},"__esModule",{value:!0}),n);var k={};B(k,{decodeBase64:()=>T,encodeBase64:()=>C});module.exports=E(k);var y={base64:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),base64url:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")},A={base64:new Uint8Array(128).fill(64),base64url:new Uint8Array(128).fill(64)};y.base64.forEach((n,t)=>A.base64[n]=t);y.base64url.forEach((n,t)=>A.base64url[n]=t);function x(n){return((n+2)/3|0)*4}function d(n,t,e,o,s){for(t+=2;t<n.length;t+=3){let r=n[t-2]<<16|n[t-1]<<8|n[t];n[e++]=o[r>>18],n[e++]=o[r>>12&63],n[e++]=o[r>>6&63],n[e++]=o[r&63]}switch(t){case n.length+1:{let r=n[t-2]<<16;n[e++]=o[r>>18],n[e++]=o[r>>12&63],n[e++]=s,n[e++]=s;break}case n.length:{let r=n[t-2]<<16|n[t-1]<<8;n[e++]=o[r>>18],n[e++]=o[r>>12&63],n[e++]=o[r>>6&63],n[e++]=s;break}}return e}function p(n,t,e,o,s){for(let r=n.length-2;r<n.length;++r)if(n[r]===s){for(let a=r+1;a<n.length;++a)if(n[a]!==s)throw new TypeError(`Cannot decode input as base64: Invalid character (${String.fromCharCode(n[a])})`);n=n.subarray(0,r);break}if((n.length-e)%4===1)throw new RangeError(`Cannot decode input as base64: Length (${n.length-e}), excluding padding, must not have a remainder of 1 when divided by 4`);for(t+=3;t<n.length;t+=4){let r=c(n[t-3],o)<<18|c(n[t-2],o)<<12|c(n[t-1],o)<<6|c(n[t],o);n[e++]=r>>16,n[e++]=r>>8&255,n[e++]=r&255}switch(t){case n.length+1:{let r=c(n[t-3],o)<<18|c(n[t-2],o)<<12;n[e++]=r>>16;break}case n.length:{let r=c(n[t-3],o)<<18|c(n[t-2],o)<<12|c(n[t-1],o)<<6;n[e++]=r>>16,n[e++]=r>>8&255;break}}return e}function c(n,t){let e=t[n]??64;if(e===64)throw new TypeError(`Cannot decode input as base64: Invalid character (${String.fromCharCode(n)})`);return e}function l(n,t){let e=n.length;if(n.byteOffset){let o=new Uint8Array(n.buffer);o.set(n),n=o.subarray(0,e)}return n=new Uint8Array(n.buffer.transfer(t)),n.set(n.subarray(0,e),t-e),[n,t-e]}var U=61,m=new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),g=new Uint8Array(128).fill(64);m.forEach((n,t)=>g[n]=t);function C(n){typeof n=="string"?n=new TextEncoder().encode(n):n instanceof ArrayBuffer?n=new Uint8Array(n).slice():n=n.slice();let[t,e]=l(n,x(n.length));return d(t,e,0,m,U),new TextDecoder().decode(t)}function T(n){let t=new TextEncoder().encode(n);return new Uint8Array(t.buffer.transfer(p(t,0,0,g,U)))}0&&(module.exports={decodeBase64,encodeBase64});