cborkit
Version:
A modern, extensible CBOR (Concise Binary Object Representation) library for TypeScript and JavaScript.
2 lines (1 loc) • 5.62 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkTHW6UOZ2cjs = require('./chunk-THW6UOZ2.cjs');var A=new TextDecoder,T= exports.a =(e,r=0)=>{let n=_chunkTHW6UOZ2cjs.c.call(void 0, e,r),t=n>>5,o=n&31;if(t===_chunkTHW6UOZ2cjs.a.Special)return W(e,r,t,o);if(o<=23)return{major:t,shortCount:o,headerLength:1,itemLength:o};if(o===24){let i=_chunkTHW6UOZ2cjs.c.call(void 0, e,r+1);return{major:t,shortCount:o,extendedCount:i,headerLength:2,itemLength:i}}else if(o===25){let i=_chunkTHW6UOZ2cjs.d.call(void 0, e,r+1);return{major:t,shortCount:o,extendedCount:i,headerLength:3,itemLength:i}}else if(o===26){let i=_chunkTHW6UOZ2cjs.e.call(void 0, e,r+1);return{major:t,shortCount:o,extendedCount:i,headerLength:5,itemLength:i}}else if(o===27){let i=_chunkTHW6UOZ2cjs.f.call(void 0, e,r+1);return{major:t,shortCount:o,extendedCount:i,headerLength:9,itemLength:_chunkTHW6UOZ2cjs.n.call(void 0, i)}}else{if(o===31)return{major:t,shortCount:o,headerLength:1,itemLength:null};throw new Error("Invalid cbor header")}},W=(e,r,n,t)=>{if(t<=23)return{major:n,shortCount:t,headerLength:1,itemLength:t};if(t===24){let o=_chunkTHW6UOZ2cjs.c.call(void 0, e,r+1);return{major:n,shortCount:t,extendedCount:o,headerLength:2,itemLength:o}}else{if(t===25)return{major:n,shortCount:t,headerLength:1,itemLength:2};if(t===26)return{major:n,shortCount:t,headerLength:1,itemLength:4};if(t===27)return{major:n,shortCount:t,headerLength:1,itemLength:8};if(t===31)return{major:n,shortCount:t,headerLength:1,itemLength:0};throw new Error("Invalid cbor header")}},c= exports.b =(e,r)=>{let n=_nullishCoalesce(_optionalChain([r, 'optionalAccess', _ => _.offset]), () => (0)),t=T(e,n);switch(t.major){case _chunkTHW6UOZ2cjs.a.PosInt:case _chunkTHW6UOZ2cjs.a.NegInt:return U(t);case _chunkTHW6UOZ2cjs.a.Bytes:case _chunkTHW6UOZ2cjs.a.Text:return H(t,e,n);case _chunkTHW6UOZ2cjs.a.Array:return E(t,e,n);case _chunkTHW6UOZ2cjs.a.Map:return S(t,e,n);case _chunkTHW6UOZ2cjs.a.Tag:return M(t,e,n);case _chunkTHW6UOZ2cjs.a.Special:return F(t,e,n)}},U=e=>{if(e.itemLength===null)throw new Error("Indefinite length currently not supported");let r=e.major!==_chunkTHW6UOZ2cjs.a.NegInt,n=e.shortCount<24?e.shortCount:e.extendedCount?_chunkTHW6UOZ2cjs.n.call(void 0, e.extendedCount):void 0;if(n===void 0)throw new Error("Invalid cbor header");return{type:"int",value:typeof n=="bigint"?r?n:-n-1n:r?n:-n-1,length:e.headerLength}},H=(e,r,n)=>{let t=n+e.headerLength;if(e.itemLength===null){let{items:h,length:g}=p(r,t);return e.major===_chunkTHW6UOZ2cjs.a.Text?{type:"text",value:h.reduce((s,u)=>{if(u.type!=="text")throw new Error(`Invalid cbor type '${u.type}' in indefinite length text`);return s+u.value},""),length:e.headerLength+g}:{type:"bytes",value:h.reduce((s,u)=>{if(u.type!=="bytes")throw new Error(`Invalid cbor type '${u.type}' in indefinite length bytes`);return s.push(u.value),s},new _chunkTHW6UOZ2cjs.o).toBytes(),length:e.headerLength+g}}let o=t+_chunkTHW6UOZ2cjs.m.call(void 0, e.itemLength),i=r.slice(t,o),l=o-n;return e.major===_chunkTHW6UOZ2cjs.a.Text?{type:"text",value:A.decode(i),length:l}:{type:"bytes",value:i,length:l}},E=(e,r,n)=>{let t=n+e.headerLength;if(e.itemLength===null){let{items:h,length:g}=p(r,t);return{type:"array",value:h,length:e.headerLength+g}}let o=_chunkTHW6UOZ2cjs.m.call(void 0, e.itemLength),i=[],l=0;for(let h=0;h<o;h++){let g=c(r,{offset:t+l});i.push(g),l+=g.length}return{type:"array",value:i,length:e.headerLength+l}},S=(e,r,n)=>{let t=n+e.headerLength;if(e.itemLength===null){let{items:h,length:g}=p(r,t);if(h.length%2!==0)throw new Error("Infinite map has odd length");let s=[];for(let u=0;u<h.length;u+=2)s.push([h[u],h[u+1]]);return{type:"map",value:s,length:e.headerLength+g}}let o=_chunkTHW6UOZ2cjs.m.call(void 0, e.itemLength),i=[],l=0;for(let h=0;h<o;h++){let g=c(r,{offset:t+l});l+=g.length;let s=c(r,{offset:t+l});l+=s.length,i.push([g,s])}return{type:"map",value:i,length:e.headerLength+l}},p=(e,r)=>{let n=0,t=[];for(;;){let o=T(e,r+n);if(o.major===_chunkTHW6UOZ2cjs.a.Special&&o.shortCount===31){n+=1;break}let i=c(e,{offset:r+n});n+=i.length,t.push(i)}return{length:n,items:t}},M=(e,r,n)=>{if(e.itemLength===null)throw new Error("Illegal cbor header",{cause:e});let t=n+e.headerLength,o=c(r,{offset:t});return{type:"tag",value:e.itemLength,item:o,length:e.headerLength+o.length}},F=(e,r,n)=>{if(e.shortCount<=23)return{type:"simple",value:e.shortCount,length:e.headerLength};if(e.shortCount===24&&e.extendedCount)return{type:"simple",value:Number(e.extendedCount),length:e.headerLength};if(e.shortCount===25&&_chunkTHW6UOZ2cjs.b)return{type:"float",value:_chunkTHW6UOZ2cjs.g.call(void 0, r,n+e.headerLength),length:e.headerLength+2};if(e.shortCount===26)return{type:"float",value:_chunkTHW6UOZ2cjs.h.call(void 0, r,n+e.headerLength),length:e.headerLength+4};if(e.shortCount===27)return{type:"float",value:_chunkTHW6UOZ2cjs.i.call(void 0, r,n+e.headerLength),length:e.headerLength+8};throw new Error("Invalid cbor type")};exports.a = T; exports.b = c;