cbor2
Version:
Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC8949).
2 lines (1 loc) • 1.53 kB
JavaScript
const g=Symbol("CBOR_RANGES");function c(r,n){Object.defineProperty(r,g,{configurable:!1,enumerable:!1,writable:!1,value:n})}function f(r){return r[g]}function l(r){return f(r)!==void 0}function R(r,n=0,t=r.length-1){const o=r.subarray(n,t),a=f(r);if(a){const s=[];for(const e of a)if(e[0]>=n&&e[0]+e[1]<=t){const i=[...e];i[0]-=n,s.push(i)}s.length&&c(o,s)}return o}function b(r){let n=Math.ceil(r.length/2);const t=new Uint8Array(n);n--;for(let o=r.length,a=o-2;o>=0;o=a,a-=2,n--)t[n]=parseInt(r.substring(a,o),16);return t}function A(r){return r.reduce((n,t)=>n+t.toString(16).padStart(2,"0"),"")}function d(r){const n=r.reduce((e,i)=>e+i.length,0),t=r.some(e=>l(e)),o=[],a=new Uint8Array(n);let s=0;for(const e of r){if(!(e instanceof Uint8Array))throw new TypeError(`Invalid array: ${e}`);if(a.set(e,s),t){const i=e[g]??[[0,e.length]];for(const u of i)u[0]+=s;o.push(...i)}s+=e.length}return t&&c(a,o),a}function y(r){const n=atob(r);return Uint8Array.from(n,t=>t.codePointAt(0))}const p={"-":"+",_:"/"};function x(r){const n=r.replace(/[_-]/g,t=>p[t]);return y(n.padEnd(Math.ceil(n.length/4)*4,"="))}function h(){const r=new Uint8Array(4),n=new Uint32Array(r.buffer);return!((n[0]=1)&r[0])}function U(r){let n="";for(const t of r){const o=t.codePointAt(0)?.toString(16).padStart(4,"0");n&&(n+=", "),n+=`U+${o}`}return n}export{g as CBOR_RANGES,y as base64ToBytes,x as base64UrlToBytes,f as getRanges,l as hasRanges,b as hexToU8,h as isBigEndian,c as setRanges,U as stringToHex,R as subarrayRanges,d as u8concat,A as u8toHex};