byte-encodings
Version:
Utilities for encoding and decoding common formats like hex, base64, and varint. Ported from Deno's @std/encoding.
4 lines (3 loc) • 2.61 kB
JavaScript
var s=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var u=Object.prototype.hasOwnProperty;var h=(e,t)=>{for(var a in t)s(e,a,{get:t[a],enumerable:!0})},m=(e,t,a,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of p(t))!u.call(e,n)&&n!==a&&s(e,n,{get:()=>t[n],enumerable:!(i=g(t,n))||i.enumerable});return e};var w=e=>m(s({},"__esModule",{value:!0}),e);var O={};h(O,{decodeAscii85:()=>U,encodeAscii85:()=>k});module.exports=w(O);var x=new TextEncoder;function C(e){var a;let t=typeof e;return t!=="object"?t:e===null?"null":((a=e==null?void 0:e.constructor)==null?void 0:a.name)??"object"}function A(e){if(typeof e=="string")return x.encode(e);if(e instanceof Uint8Array)return e;if(e instanceof ArrayBuffer)return new Uint8Array(e);throw new TypeError(`Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type ${C(e)}`)}var b="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~",y="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#";function k(e,t={}){let a=A(e),{standard:i="Adobe"}=t,n=[],c,o=0,l=0;if(a.length%4!==0){let r=a;l=4-r.length%4,a=new Uint8Array(r.length+l),a.set(r)}let d=new DataView(a.buffer,a.byteOffset,a.byteLength);for(let r=0;r<a.length;r+=4){if(c=d.getUint32(r),(i==="Adobe"||i==="btoa")&&c===0&&r<a.length-l-3){n[o++]="z";continue}if(i==="btoa"&&c===538976288){n[o++]="y";continue}for(let f=4;f>=0;f--)n[o+f]=String.fromCharCode(c%85+33),c=Math.trunc(c/85);o+=5}switch(i){case"Adobe":if(t!=null&&t.delimiter)return`<~${n.slice(0,n.length-l).join("")}~>`;break;case"btoa":if(t!=null&&t.delimiter)return`xbtoa Begin
${n.slice(0,n.length-l).join("")}
xbtoa End`;break;case"RFC 1924":n=n.map(r=>b[r.charCodeAt(0)-33]);break;case"Z85":n=n.map(r=>y[r.charCodeAt(0)-33]);break}return n.slice(0,n.length-l).join("")}function U(e,t={}){let{standard:a="Adobe"}=t;switch(a){case"Adobe":e=e.replaceAll(/(<~|~>)/g,"").replaceAll("z","!!!!!");break;case"btoa":e=e.replaceAll(/(xbtoa Begin|xbtoa End|\n)/g,"").replaceAll("z","!!!!!").replaceAll("y","+<VdL");break;case"RFC 1924":e=e.replaceAll(/./g,r=>String.fromCharCode(b.indexOf(r)+33));break;case"Z85":e=e.replaceAll(/./g,r=>String.fromCharCode(y.indexOf(r)+33));break}e=e.replaceAll(/[^!-u]/g,"");let i=e.length,n=new Uint8Array(i+4-i%4),c=new DataView(n.buffer),o=0,l=0,d=0;for(let r=0;r<i;){for(d+=5;r<d;r++)o=o*85+(r<i?e.charCodeAt(r):117)-33;c.setUint32(l,o),o=0,l+=4}return n.slice(0,Math.trunc(i*.8))}0&&(module.exports={decodeAscii85,encodeAscii85});