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) 1.76 kB
var c=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var y=(n,t)=>{for(var r in t)c(n,r,{get:t[r],enumerable:!0})},g=(n,t,r,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of p(t))!x.call(n,o)&&o!==r&&c(n,o,{get:()=>t[o],enumerable:!(e=w(t,o))||e.enumerable});return n};var M=n=>g(c({},"__esModule",{value:!0}),n);var m={};y(m,{MaxUint64:()=>b,MaxVarintLen32:()=>f,MaxVarintLen64:()=>U,decodeVarint:()=>A,decodeVarint32:()=>V,encodeVarint:()=>E});module.exports=M(m);var b=18446744073709551615n,U=10,f=5;var d=0x80n,v=7n,s=new ArrayBuffer(8),l=new Uint32Array(s),h=new BigUint64Array(s);function A(n,t=0){h[0]=0n;let r=0,e=0,o=t;if(n.length===0)throw new RangeError("Cannot read empty buffer");let i;do i=n[o],r|=(i&127)<<e,e===28&&(l[0]=r,r=(i&112)>>>4,e=-4),e+=7,o++;while((i&128)===128);let a=o-t;if(a===10&&r>-1||a===11||o>n.length)throw new RangeError("Cannot decode the varint input: Malformed or overflow varint");return l[+(a>4)]=r,[h[0],o]}function V(n,t=0){let r=0,e=0;for(let o=t;o<=Math.min(n.length,t+5);o+=1,r+=7){let i=n[o];if(e+=(i&127)*Math.pow(2,r),!(i&128))return[e,o+1]}throw new RangeError("Cannot decode the varint input: Malformed or overflow varint")}function E(n,t=new Uint8Array(10),r=0){if(n=BigInt(n),n<0n)throw new RangeError(`Cannot encode the input into varint as it should be non-negative integer: received ${n}`);for(let e=r;e<=Math.min(t.length,10);e+=1){if(n<d)return t[e]=Number(n),e+=1,[t.slice(r,e),e];t[e]=Number(n&0xFFn|d),n>>=v}throw new RangeError(`Cannot encode the input ${n} into varint as it overflows uint64`)}0&&(module.exports={MaxUint64,MaxVarintLen32,MaxVarintLen64,decodeVarint,decodeVarint32,encodeVarint});