UNPKG

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) 3.3 kB
var l=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var _=(e,r)=>{for(var n in r)l(e,n,{get:r[n],enumerable:!0})},w=(e,r,n,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of m(r))!p.call(e,s)&&s!==n&&l(e,s,{get:()=>r[s],enumerable:!(t=g(r,s))||t.enumerable});return e};var B=e=>w(l({},"__esModule",{value:!0}),e);var C={};_(C,{calcSizeBase64:()=>d,decodeBase64:()=>O,encodeBase64:()=>F,encodeIntoBase64:()=>E});module.exports=B(C);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((e,r)=>A.base64[e]=r);y.base64url.forEach((e,r)=>A.base64url[e]=r);function d(e){return((e+2)/3|0)*4}function x(e,r,n,t,s){for(r+=2;r<e.length;r+=3){let a=e[r-2]<<16|e[r-1]<<8|e[r];e[n++]=t[a>>18],e[n++]=t[a>>12&63],e[n++]=t[a>>6&63],e[n++]=t[a&63]}switch(r){case e.length+1:{let a=e[r-2]<<16;e[n++]=t[a>>18],e[n++]=t[a>>12&63],e[n++]=s,e[n++]=s;break}case e.length:{let a=e[r-2]<<16|e[r-1]<<8;e[n++]=t[a>>18],e[n++]=t[a>>12&63],e[n++]=t[a>>6&63],e[n++]=s;break}}return n}function h(e,r,n,t,s){for(let a=e.length-2;a<e.length;++a)if(e[a]===s){for(let c=a+1;c<e.length;++c)if(e[c]!==s)throw new TypeError(`Cannot decode input as base64: Invalid character (${String.fromCharCode(e[c])})`);e=e.subarray(0,a);break}if((e.length-n)%4===1)throw new RangeError(`Cannot decode input as base64: Length (${e.length-n}), excluding padding, must not have a remainder of 1 when divided by 4`);for(r+=3;r<e.length;r+=4){let a=o(e[r-3],t)<<18|o(e[r-2],t)<<12|o(e[r-1],t)<<6|o(e[r],t);e[n++]=a>>16,e[n++]=a>>8&255,e[n++]=a&255}switch(r){case e.length+1:{let a=o(e[r-3],t)<<18|o(e[r-2],t)<<12;e[n++]=a>>16;break}case e.length:{let a=o(e[r-3],t)<<18|o(e[r-2],t)<<12|o(e[r-1],t)<<6;e[n++]=a>>16,e[n++]=a>>8&255;break}}return n}function o(e,r){let n=r[e]??64;if(n===64)throw new TypeError(`Cannot decode input as base64: Invalid character (${String.fromCharCode(e)})`);return n}function U(e,r){let n=e.length;if(e.byteOffset){let t=new Uint8Array(e.buffer);t.set(e),e=t.subarray(0,n)}return e=new Uint8Array(e.buffer.transfer(r)),e.set(e.subarray(0,n),r-n),[e,r-n]}function F(e,r={}){r.alphabet??="base64",typeof e=="string"?e=new TextEncoder().encode(e):e instanceof ArrayBuffer&&(e=new Uint8Array(e));let[n,t]=U(e,d(e.length)),s=x(n,t,0,y[r.alphabet],61);return r.alphabet==="base64url"&&(s=n.indexOf(61,s-2),s>0&&(n=n.subarray(0,s))),new TextDecoder().decode(n)}function E(e,r,n={}){n.alphabet??="base64",typeof e=="string"?e=new TextEncoder().encode(e):e instanceof ArrayBuffer&&(e=new Uint8Array(e));let t=d(e.length);if(r.length<t)throw new RangeError("Cannot encode input as base64: Output too small");r=r.subarray(0,t);let s=t-e.length;r.set(e,s);let a=x(r,s,0,y[n.alphabet],61);if(n.alphabet==="base64url"){let c=r.indexOf(61,a-2);if(c>0)return c}return a}function O(e,r={}){return r.alphabet??="base64",typeof e=="string"&&(e=new TextEncoder().encode(e)),e.subarray(0,h(e,0,0,A[r.alphabet],61))}0&&(module.exports={calcSizeBase64,decodeBase64,encodeBase64,encodeIntoBase64});