cbor2
Version:
Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC8949).
9 lines (8 loc) • 3.43 kB
JavaScript
import{MT as u,NUMBYTES as b,SYMS as E}from"./constants.js";import{getEncoded as B,saveEncoded as R}from"./box.js";import{getRanges as S,subarrayRanges as I,u8toHex as C}from"./utils.js";import{CBORcontainer as T}from"./container.js";import{DecodeStream as M}from"./decodeStream.js";import{Simple as N}from"./simple.js";import{Tag as P}from"./tag.js";import{diagnose as U}from"./diagnostic.js";const H=new TextDecoder;class A extends T{depth=0;leaf=!1;value;length;[E.ENCODED];constructor(a,f,e,n){super(a,f,e,n),this.parent?this.depth=this.parent.depth+1:this.depth=n.initialDepth,[,,this.value,,this.length]=a}numBytes(){switch(this.ai){case b.ONE:return 1;case b.TWO:return 2;case b.FOUR:return 4;case b.EIGHT:return 8}return 0}}function k(t){return t instanceof A}function O(t,a){return t===1/0?"Indefinite":a?`${t} ${a}${t!==1&&t!==1n?"s":""}`:String(t)}function y(t){return"".padStart(t," ")}function x(t,a,f){let e="";e+=y(t.depth*2);const n=B(t);e+=C(n.subarray(0,1));const r=t.numBytes();r&&(e+=" ",e+=C(n.subarray(1,r+1))),e=e.padEnd(a.minCol+1," "),e+="-- ",f!==void 0&&(e+=y(t.depth*2),f!==""&&(e+=`[${f}] `));let p=!1;const[s]=t.children;switch(t.mt){case u.POS_INT:e+=`Unsigned: ${s}`,typeof s=="bigint"&&(e+="n");break;case u.NEG_INT:e+=`Negative: ${s}`,typeof s=="bigint"&&(e+="n");break;case u.BYTE_STRING:e+=`Bytes (Length: ${O(t.length)})`;break;case u.UTF8_STRING:e+=`UTF8 (Length: ${O(t.length)})`,t.length!==1/0&&(e+=`: ${JSON.stringify(s)}`);break;case u.ARRAY:e+=`Array (Length: ${O(t.value,"item")})`;break;case u.MAP:e+=`Map (Length: ${O(t.value,"pair")})`;break;case u.TAG:{e+=`Tag #${t.value}`;const o=t.children,[m]=o.contents.children,i=new P(o.tag,m);R(i,n);const l=i.comment(a,t.depth);l&&(e+=": ",e+=l),p||=i.noChildren;break}case u.SIMPLE_FLOAT:s===E.BREAK?e+="BREAK":t.ai>b.ONE?Object.is(s,-0)?e+="Float: -0":e+=`Float: ${s}`:(e+="Simple: ",s instanceof N?e+=s.value:e+=s);break}if(!p)if(t.leaf){if(e+=`
`,n.length>r+1){const o=y((t.depth+1)*2),m=S(n);if(m?.length){m.sort((l,c)=>{const g=l[0]-c[0];return g||c[1]-l[1]});let i=0;for(const[l,c,g]of m)if(!(l<i)){if(i=l+c,g==="<<"){e+=y(a.minCol+1),e+="--",e+=o,e+="<< ";const d=I(n,l,l+c),h=S(d);if(h){const $=h.findIndex(([w,D,v])=>w===0&&D===c&&v==="<<");$>=0&&h.splice($,1)}e+=U(d),e+=` >>
`,e+=L(d,{initialDepth:t.depth+1,minCol:a.minCol,noPrefixHex:!0});continue}else g==="'"&&(e+=y(a.minCol+1),e+="--",e+=o,e+="'",e+=H.decode(n.subarray(l,l+c)),e+=`'
`);if(l>r)for(let d=l;d<l+c;d+=8){const h=Math.min(d+8,l+c);e+=o,e+=C(n.subarray(d,h)),e+=`
`}}}else for(let i=r+1;i<n.length;i+=8)e+=o,e+=C(n.subarray(i,i+8)),e+=`
`}}else{e+=`
`;let o=0;for(const m of t.children){if(k(m)){let i=String(o);t.mt===u.MAP?i=o%2?`val ${(o-1)/2}`:`key ${o/2}`:t.mt===u.TAG&&(i=""),e+=x(m,a,i)}o++}}return e}const q={...T.defaultDecodeOptions,initialDepth:0,noPrefixHex:!1,minCol:0};function L(t,a){const f={...q,...a,ParentType:A,saveOriginal:!0},e=new M(t,f);let n,r;for(const s of e){if(r=T.create(s,n,f,e),s[2]===E.BREAK)if(n?.isStreaming)n.left=1;else throw new Error("Unexpected BREAK");if(!k(r)){const i=new A(s,0,n,f);i.leaf=!0,i.children.push(r),R(i,e.toHere(s[3])),r=i}let o=(r.depth+1)*2;const m=r.numBytes();for(m&&(o+=1,o+=m*2),f.minCol=Math.max(f.minCol,o),n&&n.push(r,e,s[3]),n=r;n?.done;)r=n,r.leaf||R(r,e.toHere(r.offset)),{parent:n}=n}a&&(a.minCol=f.minCol);let p=f.noPrefixHex?"":`0x${C(e.toHere(0))}
`;return p+=x(r,f),p}export{L as comment};